You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2196 lines
68KB

  1. /*
  2. * FFmpeg main
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #define HAVE_AV_CONFIG_H
  20. #include "avformat.h"
  21. #include "tick.h"
  22. #ifndef CONFIG_WIN32
  23. #include <unistd.h>
  24. #include <fcntl.h>
  25. #include <sys/ioctl.h>
  26. #include <sys/time.h>
  27. #include <termios.h>
  28. #include <sys/resource.h>
  29. #endif
  30. #include <time.h>
  31. #include <ctype.h>
  32. #define MAXINT64 INT64_C(0x7fffffffffffffff)
  33. typedef struct {
  34. const char *name;
  35. int flags;
  36. #define HAS_ARG 0x0001
  37. #define OPT_BOOL 0x0002
  38. #define OPT_EXPERT 0x0004
  39. #define OPT_STRING 0x0008
  40. union {
  41. void (*func_arg)();
  42. int *int_arg;
  43. char **str_arg;
  44. } u;
  45. const char *help;
  46. const char *argname;
  47. } OptionDef;
  48. /* select an input stream for an output stream */
  49. typedef struct AVStreamMap {
  50. int file_index;
  51. int stream_index;
  52. } AVStreamMap;
  53. extern const OptionDef options[];
  54. void show_help(void);
  55. #define MAX_FILES 20
  56. static AVFormatContext *input_files[MAX_FILES];
  57. static int nb_input_files = 0;
  58. static AVFormatContext *output_files[MAX_FILES];
  59. static int nb_output_files = 0;
  60. static AVStreamMap stream_maps[MAX_FILES];
  61. static int nb_stream_maps;
  62. static AVInputFormat *file_iformat;
  63. static AVOutputFormat *file_oformat;
  64. static int frame_width = 160;
  65. static int frame_height = 128;
  66. static int frame_rate = 25 * FRAME_RATE_BASE;
  67. static int video_bit_rate = 200000;
  68. static int video_bit_rate_tolerance = 200000;
  69. static int video_qscale = 0;
  70. static int video_qmin = 3;
  71. static int video_qmax = 15;
  72. static int video_qdiff = 3;
  73. static float video_qblur = 0.5;
  74. static float video_qcomp = 0.5;
  75. static int me_method = 0;
  76. static int video_disable = 0;
  77. static int video_codec_id = CODEC_ID_NONE;
  78. static int same_quality = 0;
  79. static int b_frames = 0;
  80. static int use_hq = 0;
  81. static int use_4mv = 0;
  82. static int do_deinterlace = 0;
  83. static int gop_size = 12;
  84. static int intra_only = 0;
  85. static int audio_sample_rate = 44100;
  86. static int audio_bit_rate = 64000;
  87. static int audio_disable = 0;
  88. static int audio_channels = 1;
  89. static int audio_codec_id = CODEC_ID_NONE;
  90. static INT64 recording_time = 0;
  91. static int file_overwrite = 0;
  92. static char *str_title = NULL;
  93. static char *str_author = NULL;
  94. static char *str_copyright = NULL;
  95. static char *str_comment = NULL;
  96. static int do_benchmark = 0;
  97. static int do_hex_dump = 0;
  98. static int do_play = 0;
  99. static int do_psnr = 0;
  100. static int do_vstats = 0;
  101. static int mpeg_vcd = 0;
  102. #ifndef CONFIG_AUDIO_OSS
  103. const char *audio_device = "none";
  104. #endif
  105. #ifndef CONFIG_VIDEO4LINUX
  106. const char *v4l_device = "none";
  107. #endif
  108. typedef struct AVOutputStream {
  109. int file_index; /* file index */
  110. int index; /* stream index in the output file */
  111. int source_index; /* AVInputStream index */
  112. AVStream *st; /* stream in the output file */
  113. int encoding_needed; /* true if encoding needed for this stream */
  114. /* video only */
  115. AVPicture pict_tmp; /* temporary image for resizing */
  116. int video_resample;
  117. ImgReSampleContext *img_resample_ctx; /* for image resampling */
  118. /* audio only */
  119. int audio_resample;
  120. ReSampleContext *resample; /* for audio resampling */
  121. FifoBuffer fifo; /* for compression: one audio fifo per codec */
  122. } AVOutputStream;
  123. typedef struct AVInputStream {
  124. int file_index;
  125. int index;
  126. AVStream *st;
  127. int discard; /* true if stream data should be discarded */
  128. int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
  129. Ticker pts_ticker; /* Ticker for PTS calculation */
  130. int ticker_inited; /* to signal if the ticker was initialized */
  131. INT64 pts; /* current pts */
  132. int pts_increment; /* expected pts increment for next packet */
  133. int frame_number; /* current frame */
  134. INT64 sample_index; /* current sample */
  135. } AVInputStream;
  136. typedef struct AVInputFile {
  137. int eof_reached; /* true if eof reached */
  138. int ist_index; /* index of first stream in ist_table */
  139. int buffer_size; /* current total buffer size */
  140. int buffer_size_max; /* buffer size at which we consider we can stop
  141. buffering */
  142. int nb_streams; /* nb streams we are aware of */
  143. } AVInputFile;
  144. #ifndef CONFIG_WIN32
  145. /* init terminal so that we can grab keys */
  146. static struct termios oldtty;
  147. static void term_exit(void)
  148. {
  149. tcsetattr (0, TCSANOW, &oldtty);
  150. }
  151. static void term_init(void)
  152. {
  153. struct termios tty;
  154. tcgetattr (0, &tty);
  155. oldtty = tty;
  156. tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
  157. |INLCR|IGNCR|ICRNL|IXON);
  158. tty.c_oflag |= OPOST;
  159. tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
  160. tty.c_cflag &= ~(CSIZE|PARENB);
  161. tty.c_cflag |= CS8;
  162. tty.c_cc[VMIN] = 1;
  163. tty.c_cc[VTIME] = 0;
  164. tcsetattr (0, TCSANOW, &tty);
  165. atexit(term_exit);
  166. }
  167. /* read a key without blocking */
  168. static int read_key(void)
  169. {
  170. struct timeval tv;
  171. int n;
  172. unsigned char ch;
  173. fd_set rfds;
  174. FD_ZERO(&rfds);
  175. FD_SET(0, &rfds);
  176. tv.tv_sec = 0;
  177. tv.tv_usec = 0;
  178. n = select(1, &rfds, NULL, NULL, &tv);
  179. if (n > 0) {
  180. if (read(0, &ch, 1) == 1)
  181. return ch;
  182. }
  183. return -1;
  184. }
  185. #else
  186. /* no interactive support */
  187. static void term_exit(void)
  188. {
  189. }
  190. static void term_init(void)
  191. {
  192. }
  193. static int read_key(void)
  194. {
  195. return -1;
  196. }
  197. #endif
  198. int read_ffserver_streams(AVFormatContext *s, const char *filename)
  199. {
  200. int i, err;
  201. AVFormatContext *ic;
  202. err = av_open_input_file(&ic, filename, NULL, FFM_PACKET_SIZE, NULL);
  203. if (err < 0)
  204. return err;
  205. /* copy stream format */
  206. s->nb_streams = ic->nb_streams;
  207. for(i=0;i<ic->nb_streams;i++) {
  208. AVStream *st;
  209. st = av_mallocz(sizeof(AVFormatContext));
  210. memcpy(st, ic->streams[i], sizeof(AVStream));
  211. s->streams[i] = st;
  212. }
  213. av_close_input_file(ic);
  214. return 0;
  215. }
  216. #define MAX_AUDIO_PACKET_SIZE 16384
  217. static void do_audio_out(AVFormatContext *s,
  218. AVOutputStream *ost,
  219. AVInputStream *ist,
  220. unsigned char *buf, int size)
  221. {
  222. UINT8 *buftmp;
  223. UINT8 audio_buf[2*MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
  224. UINT8 audio_out[MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
  225. int size_out, frame_bytes, ret;
  226. AVCodecContext *enc;
  227. enc = &ost->st->codec;
  228. if (ost->audio_resample) {
  229. buftmp = audio_buf;
  230. size_out = audio_resample(ost->resample,
  231. (short *)buftmp, (short *)buf,
  232. size / (ist->st->codec.channels * 2));
  233. size_out = size_out * enc->channels * 2;
  234. } else {
  235. buftmp = buf;
  236. size_out = size;
  237. }
  238. /* now encode as many frames as possible */
  239. if (enc->frame_size > 1) {
  240. /* output resampled raw samples */
  241. fifo_write(&ost->fifo, buftmp, size_out,
  242. &ost->fifo.wptr);
  243. frame_bytes = enc->frame_size * 2 * enc->channels;
  244. while (fifo_read(&ost->fifo, audio_buf, frame_bytes,
  245. &ost->fifo.rptr) == 0) {
  246. ret = avcodec_encode_audio(enc, audio_out, sizeof(audio_out),
  247. (short *)audio_buf);
  248. s->oformat->write_packet(s, ost->index, audio_out, ret, 0);
  249. }
  250. } else {
  251. /* output a pcm frame */
  252. /* XXX: change encoding codec API to avoid this ? */
  253. switch(enc->codec->id) {
  254. case CODEC_ID_PCM_S16LE:
  255. case CODEC_ID_PCM_S16BE:
  256. case CODEC_ID_PCM_U16LE:
  257. case CODEC_ID_PCM_U16BE:
  258. break;
  259. default:
  260. size_out = size_out >> 1;
  261. break;
  262. }
  263. ret = avcodec_encode_audio(enc, audio_out, size_out,
  264. (short *)buftmp);
  265. s->oformat->write_packet(s, ost->index, audio_out, ret, 0);
  266. }
  267. }
  268. /* write a picture to a raw mux */
  269. static void write_picture(AVFormatContext *s, int index, AVPicture *picture,
  270. int pix_fmt, int w, int h)
  271. {
  272. UINT8 *buf, *src, *dest;
  273. int size, j, i;
  274. /* XXX: not efficient, should add test if we can take
  275. directly the AVPicture */
  276. switch(pix_fmt) {
  277. case PIX_FMT_YUV420P:
  278. size = avpicture_get_size(pix_fmt, w, h);
  279. buf = av_malloc(size);
  280. if (!buf)
  281. return;
  282. dest = buf;
  283. for(i=0;i<3;i++) {
  284. if (i == 1) {
  285. w >>= 1;
  286. h >>= 1;
  287. }
  288. src = picture->data[i];
  289. for(j=0;j<h;j++) {
  290. memcpy(dest, src, w);
  291. dest += w;
  292. src += picture->linesize[i];
  293. }
  294. }
  295. break;
  296. case PIX_FMT_YUV422P:
  297. size = (w * h) * 2;
  298. buf = av_malloc(size);
  299. if (!buf)
  300. return;
  301. dest = buf;
  302. for(i=0;i<3;i++) {
  303. if (i == 1) {
  304. w >>= 1;
  305. }
  306. src = picture->data[i];
  307. for(j=0;j<h;j++) {
  308. memcpy(dest, src, w);
  309. dest += w;
  310. src += picture->linesize[i];
  311. }
  312. }
  313. break;
  314. case PIX_FMT_YUV444P:
  315. size = (w * h) * 3;
  316. buf = av_malloc(size);
  317. if (!buf)
  318. return;
  319. dest = buf;
  320. for(i=0;i<3;i++) {
  321. src = picture->data[i];
  322. for(j=0;j<h;j++) {
  323. memcpy(dest, src, w);
  324. dest += w;
  325. src += picture->linesize[i];
  326. }
  327. }
  328. break;
  329. case PIX_FMT_YUV422:
  330. size = (w * h) * 2;
  331. buf = av_malloc(size);
  332. if (!buf)
  333. return;
  334. dest = buf;
  335. src = picture->data[0];
  336. for(j=0;j<h;j++) {
  337. memcpy(dest, src, w * 2);
  338. dest += w * 2;
  339. src += picture->linesize[0];
  340. }
  341. break;
  342. case PIX_FMT_RGB24:
  343. case PIX_FMT_BGR24:
  344. size = (w * h) * 3;
  345. buf = av_malloc(size);
  346. if (!buf)
  347. return;
  348. dest = buf;
  349. src = picture->data[0];
  350. for(j=0;j<h;j++) {
  351. memcpy(dest, src, w * 3);
  352. dest += w * 3;
  353. src += picture->linesize[0];
  354. }
  355. break;
  356. default:
  357. return;
  358. }
  359. s->oformat->write_packet(s, index, buf, size, 0);
  360. av_free(buf);
  361. }
  362. static void do_video_out(AVFormatContext *s,
  363. AVOutputStream *ost,
  364. AVInputStream *ist,
  365. AVPicture *picture1,
  366. int *frame_size)
  367. {
  368. int n1, n2, nb, i, ret, frame_number, dec_frame_rate;
  369. AVPicture *picture, *picture2, *pict;
  370. AVPicture picture_tmp1, picture_tmp2;
  371. UINT8 *video_buffer;
  372. UINT8 *buf = NULL, *buf1 = NULL;
  373. AVCodecContext *enc, *dec;
  374. #define VIDEO_BUFFER_SIZE (1024*1024)
  375. video_buffer= av_malloc(VIDEO_BUFFER_SIZE);
  376. if(!video_buffer) return;
  377. enc = &ost->st->codec;
  378. dec = &ist->st->codec;
  379. frame_number = ist->frame_number;
  380. dec_frame_rate = ist->st->r_frame_rate;
  381. // fprintf(stderr, "\n%d", dec_frame_rate);
  382. /* first drop frame if needed */
  383. n1 = ((INT64)frame_number * enc->frame_rate) / dec_frame_rate;
  384. n2 = (((INT64)frame_number + 1) * enc->frame_rate) / dec_frame_rate;
  385. nb = n2 - n1;
  386. if (nb <= 0)
  387. return;
  388. /* deinterlace : must be done before any resize */
  389. if (do_deinterlace) {
  390. int size;
  391. /* create temporary picture */
  392. size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
  393. buf1 = av_malloc(size);
  394. if (!buf1)
  395. return;
  396. picture2 = &picture_tmp2;
  397. avpicture_fill(picture2, buf1, dec->pix_fmt, dec->width, dec->height);
  398. if (avpicture_deinterlace(picture2, picture1,
  399. dec->pix_fmt, dec->width, dec->height) < 0) {
  400. /* if error, do not deinterlace */
  401. av_free(buf1);
  402. buf1 = NULL;
  403. picture2 = picture1;
  404. }
  405. } else {
  406. picture2 = picture1;
  407. }
  408. /* convert pixel format if needed */
  409. if (enc->pix_fmt != dec->pix_fmt) {
  410. int size;
  411. /* create temporary picture */
  412. size = avpicture_get_size(enc->pix_fmt, dec->width, dec->height);
  413. buf = av_malloc(size);
  414. if (!buf)
  415. return;
  416. pict = &picture_tmp1;
  417. avpicture_fill(pict, buf, enc->pix_fmt, dec->width, dec->height);
  418. if (img_convert(pict, enc->pix_fmt,
  419. picture2, dec->pix_fmt,
  420. dec->width, dec->height) < 0) {
  421. fprintf(stderr, "pixel format conversion not handled\n");
  422. goto the_end;
  423. }
  424. } else {
  425. pict = picture2;
  426. }
  427. /* XXX: resampling could be done before raw format convertion in
  428. some cases to go faster */
  429. /* XXX: only works for YUV420P */
  430. if (ost->video_resample) {
  431. picture = &ost->pict_tmp;
  432. img_resample(ost->img_resample_ctx, picture, pict);
  433. } else {
  434. picture = pict;
  435. }
  436. nb=1;
  437. /* duplicates frame if needed */
  438. /* XXX: pb because no interleaving */
  439. for(i=0;i<nb;i++) {
  440. if (enc->codec_id != CODEC_ID_RAWVIDEO) {
  441. /* handles sameq here. This is not correct because it may
  442. not be a global option */
  443. if (same_quality) {
  444. enc->quality = dec->quality;
  445. }
  446. ret = avcodec_encode_video(enc,
  447. video_buffer, VIDEO_BUFFER_SIZE,
  448. picture);
  449. //enc->frame_number = enc->real_pict_num;
  450. s->oformat->write_packet(s, ost->index, video_buffer, ret, 0);
  451. *frame_size = ret;
  452. //fprintf(stderr,"\nFrame: %3d %3d size: %5d type: %d",
  453. // enc->frame_number-1, enc->real_pict_num, ret,
  454. // enc->pict_type);
  455. } else {
  456. write_picture(s, ost->index, picture, enc->pix_fmt, enc->width, enc->height);
  457. }
  458. }
  459. the_end:
  460. av_free(buf);
  461. av_free(buf1);
  462. av_free(video_buffer);
  463. }
  464. static void do_video_stats(AVOutputStream *ost,
  465. AVInputStream *ist,
  466. int frame_size)
  467. {
  468. static FILE *fvstats=NULL;
  469. static INT64 total_size = 0;
  470. char filename[40];
  471. time_t today2;
  472. struct tm *today;
  473. AVCodecContext *enc;
  474. int frame_number;
  475. INT64 ti;
  476. double ti1, bitrate, avg_bitrate;
  477. if (!fvstats) {
  478. today2 = time(NULL);
  479. today = localtime(&today2);
  480. sprintf(filename, "vstats_%02d%02d%02d.log", today->tm_hour,
  481. today->tm_min,
  482. today->tm_sec);
  483. fvstats = fopen(filename,"w");
  484. if (!fvstats) {
  485. perror("fopen");
  486. exit(1);
  487. }
  488. }
  489. ti = MAXINT64;
  490. enc = &ost->st->codec;
  491. total_size += frame_size;
  492. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  493. frame_number = ist->frame_number;
  494. fprintf(fvstats, "frame= %5d q= %2d ", frame_number, enc->quality);
  495. if (do_psnr)
  496. fprintf(fvstats, "PSNR= %6.2f ", enc->psnr_y);
  497. fprintf(fvstats,"f_size= %6d ", frame_size);
  498. /* compute min pts value */
  499. if (!ist->discard && ist->pts < ti) {
  500. ti = ist->pts;
  501. }
  502. ti1 = (double)ti / 1000000.0;
  503. if (ti1 < 0.01)
  504. ti1 = 0.01;
  505. bitrate = (double)(frame_size * 8) * enc->frame_rate / FRAME_RATE_BASE / 1000.0;
  506. avg_bitrate = (double)(total_size * 8) / ti1 / 1000.0;
  507. fprintf(fvstats, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
  508. (double)total_size / 1024, ti1, bitrate, avg_bitrate);
  509. fprintf(fvstats,"type= %s\n", enc->key_frame == 1 ? "I" : "P");
  510. }
  511. }
  512. /*
  513. * The following code is the main loop of the file converter
  514. */
  515. static int av_encode(AVFormatContext **output_files,
  516. int nb_output_files,
  517. AVFormatContext **input_files,
  518. int nb_input_files,
  519. AVStreamMap *stream_maps, int nb_stream_maps)
  520. {
  521. int ret, i, j, k, n, nb_istreams = 0, nb_ostreams = 0;
  522. AVFormatContext *is, *os;
  523. AVCodecContext *codec, *icodec;
  524. AVOutputStream *ost, **ost_table = NULL;
  525. AVInputStream *ist, **ist_table = NULL;
  526. INT64 min_pts, start_time;
  527. AVInputFile *file_table;
  528. AVFormatContext *stream_no_data;
  529. file_table= (AVInputFile*) av_mallocz(nb_input_files * sizeof(AVInputFile));
  530. if (!file_table)
  531. goto fail;
  532. /* input stream init */
  533. j = 0;
  534. for(i=0;i<nb_input_files;i++) {
  535. is = input_files[i];
  536. file_table[i].ist_index = j;
  537. file_table[i].nb_streams = is->nb_streams;
  538. j += is->nb_streams;
  539. }
  540. nb_istreams = j;
  541. ist_table = av_mallocz(nb_istreams * sizeof(AVInputStream *));
  542. if (!ist_table)
  543. goto fail;
  544. for(i=0;i<nb_istreams;i++) {
  545. ist = av_mallocz(sizeof(AVInputStream));
  546. if (!ist)
  547. goto fail;
  548. ist_table[i] = ist;
  549. }
  550. j = 0;
  551. for(i=0;i<nb_input_files;i++) {
  552. is = input_files[i];
  553. for(k=0;k<is->nb_streams;k++) {
  554. ist = ist_table[j++];
  555. ist->st = is->streams[k];
  556. ist->file_index = i;
  557. ist->index = k;
  558. ist->discard = 1; /* the stream is discarded by default
  559. (changed later) */
  560. }
  561. }
  562. /* output stream init */
  563. nb_ostreams = 0;
  564. for(i=0;i<nb_output_files;i++) {
  565. os = output_files[i];
  566. nb_ostreams += os->nb_streams;
  567. if (mpeg_vcd)
  568. os->flags |= AVF_FLAG_VCD;
  569. }
  570. if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) {
  571. fprintf(stderr, "Number of stream maps must match number of output streams\n");
  572. exit(1);
  573. }
  574. ost_table = av_mallocz(sizeof(AVOutputStream *) * nb_ostreams);
  575. if (!ost_table)
  576. goto fail;
  577. for(i=0;i<nb_ostreams;i++) {
  578. ost = av_mallocz(sizeof(AVOutputStream));
  579. if (!ost)
  580. goto fail;
  581. ost_table[i] = ost;
  582. }
  583. n = 0;
  584. for(k=0;k<nb_output_files;k++) {
  585. os = output_files[k];
  586. for(i=0;i<os->nb_streams;i++) {
  587. int found;
  588. ost = ost_table[n++];
  589. ost->file_index = k;
  590. ost->index = i;
  591. ost->st = os->streams[i];
  592. if (nb_stream_maps > 0) {
  593. ost->source_index = file_table[stream_maps[n-1].file_index].ist_index +
  594. stream_maps[n-1].stream_index;
  595. } else {
  596. /* get corresponding input stream index : we select the first one with the right type */
  597. found = 0;
  598. for(j=0;j<nb_istreams;j++) {
  599. ist = ist_table[j];
  600. if (ist->discard &&
  601. ist->st->codec.codec_type == ost->st->codec.codec_type) {
  602. ost->source_index = j;
  603. found = 1;
  604. }
  605. }
  606. if (!found) {
  607. /* try again and reuse existing stream */
  608. for(j=0;j<nb_istreams;j++) {
  609. ist = ist_table[j];
  610. if (ist->st->codec.codec_type == ost->st->codec.codec_type) {
  611. ost->source_index = j;
  612. found = 1;
  613. }
  614. }
  615. if (!found) {
  616. fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n",
  617. ost->file_index, ost->index);
  618. exit(1);
  619. }
  620. }
  621. }
  622. ist = ist_table[ost->source_index];
  623. ist->discard = 0;
  624. }
  625. }
  626. /* dump the stream mapping */
  627. fprintf(stderr, "Stream mapping:\n");
  628. for(i=0;i<nb_ostreams;i++) {
  629. ost = ost_table[i];
  630. fprintf(stderr, " Stream #%d.%d -> #%d.%d\n",
  631. ist_table[ost->source_index]->file_index,
  632. ist_table[ost->source_index]->index,
  633. ost->file_index,
  634. ost->index);
  635. }
  636. /* for each output stream, we compute the right encoding parameters */
  637. for(i=0;i<nb_ostreams;i++) {
  638. ost = ost_table[i];
  639. ist = ist_table[ost->source_index];
  640. codec = &ost->st->codec;
  641. icodec = &ist->st->codec;
  642. switch(codec->codec_type) {
  643. case CODEC_TYPE_AUDIO:
  644. /* check if same codec with same parameters. If so, no
  645. reencoding is needed */
  646. if (codec->codec_id == icodec->codec_id &&
  647. codec->bit_rate == icodec->bit_rate &&
  648. codec->sample_rate == icodec->sample_rate &&
  649. codec->channels == icodec->channels) {
  650. /* no reencoding */
  651. /* use the same frame size */
  652. codec->frame_size = icodec->frame_size;
  653. //codec->frame_size = 8*icodec->sample_rate*icodec->frame_size/
  654. // icodec->bit_rate;
  655. //fprintf(stderr,"\nFrame size: %d", codec->frame_size);
  656. } else {
  657. if (fifo_init(&ost->fifo, 2 * MAX_AUDIO_PACKET_SIZE))
  658. goto fail;
  659. if (codec->channels == icodec->channels &&
  660. codec->sample_rate == icodec->sample_rate) {
  661. ost->audio_resample = 0;
  662. } else {
  663. if (codec->channels != icodec->channels &&
  664. icodec->codec_id == CODEC_ID_AC3) {
  665. /* Special case for 5:1 AC3 input */
  666. /* and mono or stereo output */
  667. /* Request specific number of channels */
  668. icodec->channels = codec->channels;
  669. if (codec->sample_rate == icodec->sample_rate)
  670. ost->audio_resample = 0;
  671. else {
  672. ost->audio_resample = 1;
  673. ost->resample = audio_resample_init(codec->channels, icodec->channels,
  674. codec->sample_rate,
  675. icodec->sample_rate);
  676. }
  677. /* Request specific number of channels */
  678. icodec->channels = codec->channels;
  679. } else {
  680. ost->audio_resample = 1;
  681. ost->resample = audio_resample_init(codec->channels, icodec->channels,
  682. codec->sample_rate,
  683. icodec->sample_rate);
  684. }
  685. }
  686. ist->decoding_needed = 1;
  687. ost->encoding_needed = 1;
  688. }
  689. break;
  690. case CODEC_TYPE_VIDEO:
  691. /* check if same codec with same parameters. If so, no
  692. reencoding is needed */
  693. if (codec->codec_id == icodec->codec_id &&
  694. codec->bit_rate == icodec->bit_rate &&
  695. codec->frame_rate == icodec->frame_rate &&
  696. codec->width == icodec->width &&
  697. codec->height == icodec->height) {
  698. /* no reencoding */
  699. } else {
  700. if (codec->width == icodec->width &&
  701. codec->height == icodec->height) {
  702. ost->video_resample = 0;
  703. } else {
  704. UINT8 *buf;
  705. ost->video_resample = 1;
  706. buf = av_malloc((codec->width * codec->height * 3) / 2);
  707. if (!buf)
  708. goto fail;
  709. ost->pict_tmp.data[0] = buf;
  710. ost->pict_tmp.data[1] = ost->pict_tmp.data[0] + (codec->width * codec->height);
  711. ost->pict_tmp.data[2] = ost->pict_tmp.data[1] + (codec->width * codec->height) / 4;
  712. ost->pict_tmp.linesize[0] = codec->width;
  713. ost->pict_tmp.linesize[1] = codec->width / 2;
  714. ost->pict_tmp.linesize[2] = codec->width / 2;
  715. ost->img_resample_ctx = img_resample_init(
  716. ost->st->codec.width, ost->st->codec.height,
  717. ist->st->codec.width, ist->st->codec.height);
  718. }
  719. ost->encoding_needed = 1;
  720. ist->decoding_needed = 1;
  721. }
  722. break;
  723. default:
  724. abort();
  725. }
  726. }
  727. /* open each encoder */
  728. for(i=0;i<nb_ostreams;i++) {
  729. ost = ost_table[i];
  730. if (ost->encoding_needed) {
  731. AVCodec *codec;
  732. codec = avcodec_find_encoder(ost->st->codec.codec_id);
  733. if (!codec) {
  734. fprintf(stderr, "Unsupported codec for output stream #%d.%d\n",
  735. ost->file_index, ost->index);
  736. exit(1);
  737. }
  738. if (avcodec_open(&ost->st->codec, codec) < 0) {
  739. fprintf(stderr, "Error while opening codec for stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height\n",
  740. ost->file_index, ost->index);
  741. exit(1);
  742. }
  743. }
  744. }
  745. /* open each decoder */
  746. for(i=0;i<nb_istreams;i++) {
  747. ist = ist_table[i];
  748. if (ist->decoding_needed) {
  749. AVCodec *codec;
  750. codec = avcodec_find_decoder(ist->st->codec.codec_id);
  751. if (!codec) {
  752. fprintf(stderr, "Unsupported codec for input stream #%d.%d\n",
  753. ist->file_index, ist->index);
  754. exit(1);
  755. }
  756. if (avcodec_open(&ist->st->codec, codec) < 0) {
  757. fprintf(stderr, "Error while opening codec for input stream #%d.%d\n",
  758. ist->file_index, ist->index);
  759. exit(1);
  760. }
  761. //if (ist->st->codec.codec_type == CODEC_TYPE_VIDEO)
  762. // ist->st->codec.flags |= CODEC_FLAG_REPEAT_FIELD;
  763. }
  764. }
  765. /* init pts */
  766. for(i=0;i<nb_istreams;i++) {
  767. ist = ist_table[i];
  768. ist->pts = 0;
  769. ist->frame_number = 0;
  770. }
  771. /* compute buffer size max (should use a complete heuristic) */
  772. for(i=0;i<nb_input_files;i++) {
  773. file_table[i].buffer_size_max = 2048;
  774. }
  775. /* open files and write file headers */
  776. for(i=0;i<nb_output_files;i++) {
  777. os = output_files[i];
  778. if (av_write_header(os) < 0) {
  779. fprintf(stderr, "Could not write header for output file #%d (incorrect codec paramters ?)\n", i);
  780. ret = -EINVAL;
  781. goto fail;
  782. }
  783. }
  784. #ifndef CONFIG_WIN32
  785. if (!do_play) {
  786. fprintf(stderr, "Press [q] to stop encoding\n");
  787. } else {
  788. fprintf(stderr, "Press [q] to stop playing\n");
  789. }
  790. #endif
  791. term_init();
  792. start_time = gettime();
  793. min_pts = 0;
  794. stream_no_data = 0;
  795. for(;;) {
  796. int file_index, ist_index;
  797. AVPacket pkt;
  798. UINT8 *ptr;
  799. int len;
  800. UINT8 *data_buf;
  801. int data_size, got_picture;
  802. AVPicture picture;
  803. short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
  804. redo:
  805. /* if 'q' pressed, exits */
  806. if (read_key() == 'q')
  807. break;
  808. /* select the input file with the smallest pts */
  809. file_index = -1;
  810. min_pts = MAXINT64;
  811. for(i=0;i<nb_istreams;i++) {
  812. ist = ist_table[i];
  813. /* For some reason, the pts_increment code breaks q estimation?!? */
  814. if (!ist->discard && !file_table[ist->file_index].eof_reached &&
  815. ist->pts /* + ist->pts_increment */ < min_pts && input_files[ist->file_index] != stream_no_data) {
  816. min_pts = ist->pts /* + ist->pts_increment */;
  817. file_index = ist->file_index;
  818. }
  819. }
  820. /* if none, if is finished */
  821. if (file_index < 0) {
  822. if (stream_no_data) {
  823. #ifndef CONFIG_WIN32
  824. struct timespec ts;
  825. ts.tv_sec = 0;
  826. ts.tv_nsec = 1000 * 1000 * 10;
  827. nanosleep(&ts, 0);
  828. #endif
  829. stream_no_data = 0;
  830. continue;
  831. }
  832. break;
  833. }
  834. /* finish if recording time exhausted */
  835. if (recording_time > 0 && min_pts >= recording_time)
  836. break;
  837. /* read a packet from it and output it in the fifo */
  838. is = input_files[file_index];
  839. if (av_read_packet(is, &pkt) < 0) {
  840. file_table[file_index].eof_reached = 1;
  841. continue;
  842. }
  843. if (!pkt.size) {
  844. stream_no_data = is;
  845. } else {
  846. stream_no_data = 0;
  847. }
  848. /* the following test is needed in case new streams appear
  849. dynamically in stream : we ignore them */
  850. if (pkt.stream_index >= file_table[file_index].nb_streams)
  851. goto discard_packet;
  852. ist_index = file_table[file_index].ist_index + pkt.stream_index;
  853. ist = ist_table[ist_index];
  854. if (ist->discard)
  855. goto discard_packet;
  856. if (pkt.flags & PKT_FLAG_DROPPED_FRAME)
  857. ist->frame_number++;
  858. if (do_hex_dump) {
  859. printf("stream #%d, size=%d:\n", pkt.stream_index, pkt.size);
  860. av_hex_dump(pkt.data, pkt.size);
  861. }
  862. // printf("read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size);
  863. len = pkt.size;
  864. ptr = pkt.data;
  865. while (len > 0) {
  866. /* decode the packet if needed */
  867. data_buf = NULL; /* fail safe */
  868. data_size = 0;
  869. if (ist->decoding_needed) {
  870. switch(ist->st->codec.codec_type) {
  871. case CODEC_TYPE_AUDIO:
  872. /* XXX: could avoid copy if PCM 16 bits with same
  873. endianness as CPU */
  874. ret = avcodec_decode_audio(&ist->st->codec, samples, &data_size,
  875. ptr, len);
  876. if (ret < 0)
  877. goto fail_decode;
  878. /* Some bug in mpeg audio decoder gives */
  879. /* data_size < 0, it seems they are overflows */
  880. if (data_size <= 0) {
  881. /* no audio frame */
  882. ptr += ret;
  883. len -= ret;
  884. continue;
  885. }
  886. data_buf = (UINT8 *)samples;
  887. break;
  888. case CODEC_TYPE_VIDEO:
  889. if (ist->st->codec.codec_id == CODEC_ID_RAWVIDEO) {
  890. int size;
  891. size = (ist->st->codec.width * ist->st->codec.height);
  892. avpicture_fill(&picture, ptr,
  893. ist->st->codec.pix_fmt,
  894. ist->st->codec.width,
  895. ist->st->codec.height);
  896. ret = len;
  897. } else {
  898. data_size = (ist->st->codec.width * ist->st->codec.height * 3) / 2;
  899. ret = avcodec_decode_video(&ist->st->codec,
  900. &picture, &got_picture, ptr, len);
  901. if (ret < 0) {
  902. fail_decode:
  903. fprintf(stderr, "Error while decoding stream #%d.%d\n",
  904. ist->file_index, ist->index);
  905. av_free_packet(&pkt);
  906. goto redo;
  907. }
  908. if (!got_picture) {
  909. /* no picture yet */
  910. ptr += ret;
  911. len -= ret;
  912. continue;
  913. }
  914. }
  915. break;
  916. default:
  917. goto fail_decode;
  918. }
  919. } else {
  920. data_buf = ptr;
  921. data_size = len;
  922. ret = len;
  923. }
  924. /* init tickers */
  925. if (!ist->ticker_inited) {
  926. switch (ist->st->codec.codec_type) {
  927. case CODEC_TYPE_AUDIO:
  928. ticker_init(&ist->pts_ticker,
  929. (INT64)ist->st->codec.sample_rate,
  930. (INT64)(1000000));
  931. ist->ticker_inited = 1;
  932. break;
  933. case CODEC_TYPE_VIDEO:
  934. ticker_init(&ist->pts_ticker,
  935. (INT64)ist->st->r_frame_rate,
  936. ((INT64)1000000 * FRAME_RATE_BASE));
  937. ist->ticker_inited = 1;
  938. break;
  939. default:
  940. abort();
  941. }
  942. }
  943. /* update pts */
  944. switch(ist->st->codec.codec_type) {
  945. case CODEC_TYPE_AUDIO:
  946. //ist->pts = (INT64)1000000 * ist->sample_index / ist->st->codec.sample_rate;
  947. ist->pts = ticker_tick(&ist->pts_ticker, ist->sample_index);
  948. ist->sample_index += data_size / (2 * ist->st->codec.channels);
  949. ist->pts_increment = (INT64) (data_size / (2 * ist->st->codec.channels)) * 1000000 / ist->st->codec.sample_rate;
  950. break;
  951. case CODEC_TYPE_VIDEO:
  952. ist->frame_number++;
  953. //ist->pts = ((INT64)ist->frame_number * 1000000 * FRAME_RATE_BASE) /
  954. // ist->st->codec.frame_rate;
  955. ist->pts = ticker_tick(&ist->pts_ticker, ist->frame_number);
  956. ist->pts_increment = ((INT64) 1000000 * FRAME_RATE_BASE) /
  957. ist->st->codec.frame_rate;
  958. break;
  959. default:
  960. abort();
  961. }
  962. ptr += ret;
  963. len -= ret;
  964. /* transcode raw format, encode packets and output them */
  965. for(i=0;i<nb_ostreams;i++) {
  966. int frame_size;
  967. ost = ost_table[i];
  968. if (ost->source_index == ist_index) {
  969. os = output_files[ost->file_index];
  970. if (ost->encoding_needed) {
  971. switch(ost->st->codec.codec_type) {
  972. case CODEC_TYPE_AUDIO:
  973. do_audio_out(os, ost, ist, data_buf, data_size);
  974. break;
  975. case CODEC_TYPE_VIDEO:
  976. do_video_out(os, ost, ist, &picture, &frame_size);
  977. if (do_vstats)
  978. do_video_stats(ost, ist, frame_size);
  979. break;
  980. default:
  981. abort();
  982. }
  983. } else {
  984. /* no reencoding needed : output the packet directly */
  985. /* force the input stream PTS */
  986. os->oformat->write_packet(os, ost->index, data_buf, data_size, pkt.pts);
  987. }
  988. }
  989. }
  990. }
  991. discard_packet:
  992. av_free_packet(&pkt);
  993. /* dump report by using the first video and audio streams */
  994. {
  995. char buf[1024];
  996. AVFormatContext *oc;
  997. INT64 total_size, ti;
  998. AVCodecContext *enc;
  999. int frame_number, vid;
  1000. double bitrate, ti1;
  1001. static INT64 last_time;
  1002. if ((min_pts - last_time) >= 500000) {
  1003. last_time = min_pts;
  1004. oc = output_files[0];
  1005. total_size = url_ftell(&oc->pb);
  1006. buf[0] = '\0';
  1007. ti = MAXINT64;
  1008. vid = 0;
  1009. for(i=0;i<nb_ostreams;i++) {
  1010. ost = ost_table[i];
  1011. enc = &ost->st->codec;
  1012. ist = ist_table[ost->source_index];
  1013. if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
  1014. frame_number = ist->frame_number;
  1015. sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
  1016. frame_number, enc->quality);
  1017. if (do_psnr)
  1018. sprintf(buf + strlen(buf), "PSNR=%6.2f ", enc->psnr_y);
  1019. vid = 1;
  1020. }
  1021. /* compute min pts value */
  1022. if (!ist->discard && ist->pts < ti) {
  1023. ti = ist->pts;
  1024. }
  1025. }
  1026. ti1 = (double)ti / 1000000.0;
  1027. if (ti1 < 0.01)
  1028. ti1 = 0.01;
  1029. bitrate = (double)(total_size * 8) / ti1 / 1000.0;
  1030. sprintf(buf + strlen(buf),
  1031. "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
  1032. (double)total_size / 1024, ti1, bitrate);
  1033. fprintf(stderr, "%s \r", buf);
  1034. fflush(stderr);
  1035. }
  1036. }
  1037. }
  1038. term_exit();
  1039. /* dump report by using the first video and audio streams */
  1040. {
  1041. char buf[1024];
  1042. AVFormatContext *oc;
  1043. INT64 total_size, ti;
  1044. AVCodecContext *enc;
  1045. int frame_number, vid;
  1046. double bitrate, ti1;
  1047. oc = output_files[0];
  1048. total_size = url_ftell(&oc->pb);
  1049. buf[0] = '\0';
  1050. ti = MAXINT64;
  1051. vid = 0;
  1052. for(i=0;i<nb_ostreams;i++) {
  1053. ost = ost_table[i];
  1054. enc = &ost->st->codec;
  1055. ist = ist_table[ost->source_index];
  1056. if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
  1057. frame_number = ist->frame_number;
  1058. sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
  1059. frame_number, enc->quality);
  1060. if (do_psnr)
  1061. sprintf(buf + strlen(buf), "PSNR=%6.2f ", enc->psnr_y);
  1062. vid = 1;
  1063. }
  1064. /* compute min pts value */
  1065. if (!ist->discard && ist->pts < ti) {
  1066. ti = ist->pts;
  1067. }
  1068. }
  1069. ti1 = ti / 1000000.0;
  1070. if (ti1 < 0.01)
  1071. ti1 = 0.01;
  1072. bitrate = (double)(total_size * 8) / ti1 / 1000.0;
  1073. sprintf(buf + strlen(buf),
  1074. "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
  1075. (double)total_size / 1024, ti1, bitrate);
  1076. fprintf(stderr, "%s \n", buf);
  1077. }
  1078. /* close each encoder */
  1079. for(i=0;i<nb_ostreams;i++) {
  1080. ost = ost_table[i];
  1081. if (ost->encoding_needed) {
  1082. avcodec_close(&ost->st->codec);
  1083. }
  1084. }
  1085. /* close each decoder */
  1086. for(i=0;i<nb_istreams;i++) {
  1087. ist = ist_table[i];
  1088. if (ist->decoding_needed) {
  1089. avcodec_close(&ist->st->codec);
  1090. }
  1091. }
  1092. /* write the trailer if needed and close file */
  1093. for(i=0;i<nb_output_files;i++) {
  1094. os = output_files[i];
  1095. av_write_trailer(os);
  1096. }
  1097. /* finished ! */
  1098. ret = 0;
  1099. fail1:
  1100. av_free(file_table);
  1101. if (ist_table) {
  1102. for(i=0;i<nb_istreams;i++) {
  1103. ist = ist_table[i];
  1104. av_free(ist);
  1105. }
  1106. av_free(ist_table);
  1107. }
  1108. if (ost_table) {
  1109. for(i=0;i<nb_ostreams;i++) {
  1110. ost = ost_table[i];
  1111. if (ost) {
  1112. fifo_free(&ost->fifo); /* works even if fifo is not
  1113. initialized but set to zero */
  1114. av_free(ost->pict_tmp.data[0]);
  1115. if (ost->video_resample)
  1116. img_resample_close(ost->img_resample_ctx);
  1117. if (ost->audio_resample)
  1118. audio_resample_close(ost->resample);
  1119. av_free(ost);
  1120. }
  1121. }
  1122. av_free(ost_table);
  1123. }
  1124. return ret;
  1125. fail:
  1126. ret = -ENOMEM;
  1127. goto fail1;
  1128. }
  1129. #if 0
  1130. int file_read(const char *filename)
  1131. {
  1132. URLContext *h;
  1133. unsigned char buffer[1024];
  1134. int len, i;
  1135. if (url_open(&h, filename, O_RDONLY) < 0) {
  1136. printf("could not open '%s'\n", filename);
  1137. return -1;
  1138. }
  1139. for(;;) {
  1140. len = url_read(h, buffer, sizeof(buffer));
  1141. if (len <= 0)
  1142. break;
  1143. for(i=0;i<len;i++) putchar(buffer[i]);
  1144. }
  1145. url_close(h);
  1146. return 0;
  1147. }
  1148. #endif
  1149. void show_licence(void)
  1150. {
  1151. printf(
  1152. "ffmpeg version " FFMPEG_VERSION "\n"
  1153. "Copyright (c) 2000, 2001, 2002 Fabrice Bellard\n"
  1154. "This library is free software; you can redistribute it and/or\n"
  1155. "modify it under the terms of the GNU Lesser General Public\n"
  1156. "License as published by the Free Software Foundation; either\n"
  1157. "version 2 of the License, or (at your option) any later version.\n"
  1158. "\n"
  1159. "This library is distributed in the hope that it will be useful,\n"
  1160. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1161. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
  1162. "Lesser General Public License for more details.\n"
  1163. "\n"
  1164. "You should have received a copy of the GNU Lesser General Public\n"
  1165. "License along with this library; if not, write to the Free Software\n"
  1166. "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n"
  1167. );
  1168. exit(1);
  1169. }
  1170. void opt_format(const char *arg)
  1171. {
  1172. file_iformat = av_find_input_format(arg);
  1173. file_oformat = guess_format(arg, NULL, NULL);
  1174. if (!file_iformat && !file_oformat) {
  1175. fprintf(stderr, "Unknown input or output format: %s\n", arg);
  1176. exit(1);
  1177. }
  1178. }
  1179. void opt_video_bitrate(const char *arg)
  1180. {
  1181. video_bit_rate = atoi(arg) * 1000;
  1182. }
  1183. void opt_video_bitrate_tolerance(const char *arg)
  1184. {
  1185. video_bit_rate_tolerance = atoi(arg) * 1000;
  1186. }
  1187. void opt_frame_rate(const char *arg)
  1188. {
  1189. frame_rate = (int)(strtod(arg, 0) * FRAME_RATE_BASE);
  1190. }
  1191. void opt_frame_size(const char *arg)
  1192. {
  1193. parse_image_size(&frame_width, &frame_height, arg);
  1194. if (frame_width <= 0 || frame_height <= 0) {
  1195. fprintf(stderr, "Incorrect frame size\n");
  1196. exit(1);
  1197. }
  1198. if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
  1199. fprintf(stderr, "Frame size must be a multiple of 2\n");
  1200. exit(1);
  1201. }
  1202. }
  1203. void opt_gop_size(const char *arg)
  1204. {
  1205. gop_size = atoi(arg);
  1206. }
  1207. void opt_b_frames(const char *arg)
  1208. {
  1209. b_frames = atoi(arg);
  1210. if (b_frames > FF_MAX_B_FRAMES) {
  1211. fprintf(stderr, "\nCannot have more than %d B frames, increase FF_MAX_B_FRAMES.\n", FF_MAX_B_FRAMES);
  1212. exit(1);
  1213. } else if (b_frames < 1) {
  1214. fprintf(stderr, "\nNumber of B frames must be higher than 0\n");
  1215. exit(1);
  1216. }
  1217. }
  1218. void opt_qscale(const char *arg)
  1219. {
  1220. video_qscale = atoi(arg);
  1221. if (video_qscale < 0 ||
  1222. video_qscale > 31) {
  1223. fprintf(stderr, "qscale must be >= 1 and <= 31\n");
  1224. exit(1);
  1225. }
  1226. }
  1227. void opt_qmin(const char *arg)
  1228. {
  1229. video_qmin = atoi(arg);
  1230. if (video_qmin < 0 ||
  1231. video_qmin > 31) {
  1232. fprintf(stderr, "qmin must be >= 1 and <= 31\n");
  1233. exit(1);
  1234. }
  1235. }
  1236. void opt_qmax(const char *arg)
  1237. {
  1238. video_qmax = atoi(arg);
  1239. if (video_qmax < 0 ||
  1240. video_qmax > 31) {
  1241. fprintf(stderr, "qmax must be >= 1 and <= 31\n");
  1242. exit(1);
  1243. }
  1244. }
  1245. void opt_qdiff(const char *arg)
  1246. {
  1247. video_qdiff = atoi(arg);
  1248. if (video_qdiff < 0 ||
  1249. video_qdiff > 31) {
  1250. fprintf(stderr, "qdiff must be >= 1 and <= 31\n");
  1251. exit(1);
  1252. }
  1253. }
  1254. void opt_qblur(const char *arg)
  1255. {
  1256. video_qblur = atof(arg);
  1257. }
  1258. void opt_qcomp(const char *arg)
  1259. {
  1260. video_qcomp = atof(arg);
  1261. }
  1262. void opt_audio_bitrate(const char *arg)
  1263. {
  1264. audio_bit_rate = atoi(arg) * 1000;
  1265. }
  1266. void opt_audio_rate(const char *arg)
  1267. {
  1268. audio_sample_rate = atoi(arg);
  1269. }
  1270. void opt_audio_channels(const char *arg)
  1271. {
  1272. audio_channels = atoi(arg);
  1273. }
  1274. void opt_video_device(const char *arg)
  1275. {
  1276. v4l_device = strdup(arg);
  1277. }
  1278. void opt_audio_device(const char *arg)
  1279. {
  1280. audio_device = strdup(arg);
  1281. }
  1282. void opt_audio_codec(const char *arg)
  1283. {
  1284. AVCodec *p;
  1285. p = first_avcodec;
  1286. while (p) {
  1287. if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
  1288. break;
  1289. p = p->next;
  1290. }
  1291. if (p == NULL) {
  1292. fprintf(stderr, "Unknown audio codec '%s'\n", arg);
  1293. exit(1);
  1294. } else {
  1295. audio_codec_id = p->id;
  1296. }
  1297. }
  1298. const char *motion_str[] = {
  1299. "zero",
  1300. "full",
  1301. "log",
  1302. "phods",
  1303. "epzs",
  1304. "x1",
  1305. NULL,
  1306. };
  1307. void opt_motion_estimation(const char *arg)
  1308. {
  1309. const char **p;
  1310. p = motion_str;
  1311. for(;;) {
  1312. if (!*p) {
  1313. fprintf(stderr, "Unknown motion estimation method '%s'\n", arg);
  1314. exit(1);
  1315. }
  1316. if (!strcmp(*p, arg))
  1317. break;
  1318. p++;
  1319. }
  1320. me_method = (p - motion_str) + 1;
  1321. }
  1322. void opt_video_codec(const char *arg)
  1323. {
  1324. AVCodec *p;
  1325. p = first_avcodec;
  1326. while (p) {
  1327. if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
  1328. break;
  1329. p = p->next;
  1330. }
  1331. if (p == NULL) {
  1332. fprintf(stderr, "Unknown video codec '%s'\n", arg);
  1333. exit(1);
  1334. } else {
  1335. video_codec_id = p->id;
  1336. }
  1337. }
  1338. void opt_map(const char *arg)
  1339. {
  1340. AVStreamMap *m;
  1341. const char *p;
  1342. p = arg;
  1343. m = &stream_maps[nb_stream_maps++];
  1344. m->file_index = strtol(arg, (char **)&p, 0);
  1345. if (*p)
  1346. p++;
  1347. m->stream_index = strtol(p, (char **)&p, 0);
  1348. }
  1349. void opt_recording_time(const char *arg)
  1350. {
  1351. recording_time = parse_date(arg, 1);
  1352. }
  1353. void print_error(const char *filename, int err)
  1354. {
  1355. switch(err) {
  1356. case AVERROR_NUMEXPECTED:
  1357. fprintf(stderr, "%s: Incorrect image filename syntax.\n"
  1358. "Use '%%d' to specify the image number:\n"
  1359. " for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
  1360. " for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
  1361. filename);
  1362. break;
  1363. case AVERROR_INVALIDDATA:
  1364. fprintf(stderr, "%s: Error while parsing header\n", filename);
  1365. break;
  1366. case AVERROR_NOFMT:
  1367. fprintf(stderr, "%s: Unknown format\n", filename);
  1368. break;
  1369. default:
  1370. fprintf(stderr, "%s: Error while opening file\n", filename);
  1371. break;
  1372. }
  1373. }
  1374. void opt_input_file(const char *filename)
  1375. {
  1376. AVFormatContext *ic;
  1377. AVFormatParameters params, *ap = &params;
  1378. int err, i, ret, rfps;
  1379. /* get default parameters from command line */
  1380. memset(ap, 0, sizeof(*ap));
  1381. ap->sample_rate = audio_sample_rate;
  1382. ap->channels = audio_channels;
  1383. ap->frame_rate = frame_rate;
  1384. ap->width = frame_width;
  1385. ap->height = frame_height;
  1386. /* open the input file with generic libav function */
  1387. err = av_open_input_file(&ic, filename, file_iformat, 0, ap);
  1388. if (err < 0) {
  1389. print_error(filename, err);
  1390. exit(1);
  1391. }
  1392. /* If not enough info to get the stream parameters, we decode the
  1393. first frames to get it. (used in mpeg case for example) */
  1394. ret = av_find_stream_info(ic);
  1395. if (ret < 0) {
  1396. fprintf(stderr, "%s: could not find codec parameters\n", filename);
  1397. exit(1);
  1398. }
  1399. /* update the current parameters so that they match the one of the input stream */
  1400. for(i=0;i<ic->nb_streams;i++) {
  1401. AVCodecContext *enc = &ic->streams[i]->codec;
  1402. switch(enc->codec_type) {
  1403. case CODEC_TYPE_AUDIO:
  1404. //fprintf(stderr, "\nInput Audio channels: %d", enc->channels);
  1405. audio_channels = enc->channels;
  1406. audio_sample_rate = enc->sample_rate;
  1407. break;
  1408. case CODEC_TYPE_VIDEO:
  1409. frame_height = enc->height;
  1410. frame_width = enc->width;
  1411. rfps = ic->streams[i]->r_frame_rate;
  1412. if (enc->frame_rate != rfps) {
  1413. fprintf(stderr,"\nSeems that stream %d comes from film source: %2.2f->%2.2f\n",
  1414. i, (float)enc->frame_rate / FRAME_RATE_BASE,
  1415. (float)rfps / FRAME_RATE_BASE);
  1416. }
  1417. /* update the current frame rate to match the stream frame rate */
  1418. frame_rate = rfps;
  1419. break;
  1420. default:
  1421. abort();
  1422. }
  1423. }
  1424. input_files[nb_input_files] = ic;
  1425. /* dump the file content */
  1426. dump_format(ic, nb_input_files, filename, 0);
  1427. nb_input_files++;
  1428. file_iformat = NULL;
  1429. file_oformat = NULL;
  1430. }
  1431. void check_audio_video_inputs(int *has_video_ptr, int *has_audio_ptr)
  1432. {
  1433. int has_video, has_audio, i, j;
  1434. AVFormatContext *ic;
  1435. has_video = 0;
  1436. has_audio = 0;
  1437. for(j=0;j<nb_input_files;j++) {
  1438. ic = input_files[j];
  1439. for(i=0;i<ic->nb_streams;i++) {
  1440. AVCodecContext *enc = &ic->streams[i]->codec;
  1441. switch(enc->codec_type) {
  1442. case CODEC_TYPE_AUDIO:
  1443. has_audio = 1;
  1444. break;
  1445. case CODEC_TYPE_VIDEO:
  1446. has_video = 1;
  1447. break;
  1448. default:
  1449. abort();
  1450. }
  1451. }
  1452. }
  1453. *has_video_ptr = has_video;
  1454. *has_audio_ptr = has_audio;
  1455. }
  1456. void opt_output_file(const char *filename)
  1457. {
  1458. AVStream *st;
  1459. AVFormatContext *oc;
  1460. int use_video, use_audio, nb_streams, input_has_video, input_has_audio;
  1461. int codec_id;
  1462. if (!strcmp(filename, "-"))
  1463. filename = "pipe:";
  1464. oc = av_mallocz(sizeof(AVFormatContext));
  1465. if (!file_oformat) {
  1466. file_oformat = guess_format(NULL, filename, NULL);
  1467. if (!file_oformat) {
  1468. fprintf(stderr, "Unable for find a suitable output format for '%s'\n",
  1469. filename);
  1470. exit(1);
  1471. }
  1472. }
  1473. oc->oformat = file_oformat;
  1474. if (!strcmp(file_oformat->name, "ffm") &&
  1475. strstart(filename, "http:", NULL)) {
  1476. /* special case for files sent to ffserver: we get the stream
  1477. parameters from ffserver */
  1478. if (read_ffserver_streams(oc, filename) < 0) {
  1479. fprintf(stderr, "Could not read stream parameters from '%s'\n", filename);
  1480. exit(1);
  1481. }
  1482. } else {
  1483. use_video = file_oformat->video_codec != CODEC_ID_NONE;
  1484. use_audio = file_oformat->audio_codec != CODEC_ID_NONE;
  1485. /* disable if no corresponding type found and at least one
  1486. input file */
  1487. if (nb_input_files > 0) {
  1488. check_audio_video_inputs(&input_has_video, &input_has_audio);
  1489. if (!input_has_video)
  1490. use_video = 0;
  1491. if (!input_has_audio)
  1492. use_audio = 0;
  1493. }
  1494. /* manual disable */
  1495. if (audio_disable) {
  1496. use_audio = 0;
  1497. }
  1498. if (video_disable) {
  1499. use_video = 0;
  1500. }
  1501. nb_streams = 0;
  1502. if (use_video) {
  1503. AVCodecContext *video_enc;
  1504. st = av_mallocz(sizeof(AVStream));
  1505. if (!st) {
  1506. fprintf(stderr, "Could not alloc stream\n");
  1507. exit(1);
  1508. }
  1509. video_enc = &st->codec;
  1510. codec_id = file_oformat->video_codec;
  1511. if (video_codec_id != CODEC_ID_NONE)
  1512. codec_id = video_codec_id;
  1513. video_enc->codec_id = codec_id;
  1514. video_enc->codec_type = CODEC_TYPE_VIDEO;
  1515. video_enc->bit_rate = video_bit_rate;
  1516. video_enc->bit_rate_tolerance = video_bit_rate_tolerance;
  1517. video_enc->frame_rate = frame_rate;
  1518. video_enc->width = frame_width;
  1519. video_enc->height = frame_height;
  1520. if (!intra_only)
  1521. video_enc->gop_size = gop_size;
  1522. else
  1523. video_enc->gop_size = 0;
  1524. if (video_qscale || same_quality) {
  1525. video_enc->flags |= CODEC_FLAG_QSCALE;
  1526. video_enc->quality = video_qscale;
  1527. }
  1528. if (use_hq) {
  1529. video_enc->flags |= CODEC_FLAG_HQ;
  1530. }
  1531. if (use_4mv) {
  1532. video_enc->flags |= CODEC_FLAG_HQ;
  1533. video_enc->flags |= CODEC_FLAG_4MV;
  1534. }
  1535. if (b_frames) {
  1536. if (codec_id != CODEC_ID_MPEG4) {
  1537. fprintf(stderr, "\nB frames encoding only supported by MPEG-4.\n");
  1538. exit(1);
  1539. }
  1540. video_enc->max_b_frames = b_frames;
  1541. video_enc->b_frame_strategy = 0;
  1542. video_enc->b_quant_factor = 2.0;
  1543. }
  1544. video_enc->qmin = video_qmin;
  1545. video_enc->qmax = video_qmax;
  1546. video_enc->max_qdiff = video_qdiff;
  1547. video_enc->qblur = video_qblur;
  1548. video_enc->qcompress = video_qcomp;
  1549. if (do_psnr)
  1550. video_enc->get_psnr = 1;
  1551. else
  1552. video_enc->get_psnr = 0;
  1553. video_enc->me_method = me_method;
  1554. /* XXX: need to find a way to set codec parameters */
  1555. if (oc->oformat->flags & AVFMT_RGB24) {
  1556. video_enc->pix_fmt = PIX_FMT_RGB24;
  1557. }
  1558. oc->streams[nb_streams] = st;
  1559. nb_streams++;
  1560. }
  1561. if (use_audio) {
  1562. AVCodecContext *audio_enc;
  1563. st = av_mallocz(sizeof(AVStream));
  1564. if (!st) {
  1565. fprintf(stderr, "Could not alloc stream\n");
  1566. exit(1);
  1567. }
  1568. audio_enc = &st->codec;
  1569. codec_id = file_oformat->audio_codec;
  1570. if (audio_codec_id != CODEC_ID_NONE)
  1571. codec_id = audio_codec_id;
  1572. audio_enc->codec_id = codec_id;
  1573. audio_enc->codec_type = CODEC_TYPE_AUDIO;
  1574. audio_enc->bit_rate = audio_bit_rate;
  1575. audio_enc->sample_rate = audio_sample_rate;
  1576. /* For audio codecs other than AC3 we limit */
  1577. /* the number of coded channels to stereo */
  1578. if (audio_channels > 2 && codec_id != CODEC_ID_AC3) {
  1579. audio_enc->channels = 2;
  1580. } else
  1581. audio_enc->channels = audio_channels;
  1582. oc->streams[nb_streams] = st;
  1583. nb_streams++;
  1584. }
  1585. oc->nb_streams = nb_streams;
  1586. if (!nb_streams) {
  1587. fprintf(stderr, "No audio or video streams available\n");
  1588. exit(1);
  1589. }
  1590. if (str_title)
  1591. pstrcpy(oc->title, sizeof(oc->title), str_title);
  1592. if (str_author)
  1593. pstrcpy(oc->author, sizeof(oc->author), str_author);
  1594. if (str_copyright)
  1595. pstrcpy(oc->copyright, sizeof(oc->copyright), str_copyright);
  1596. if (str_comment)
  1597. pstrcpy(oc->comment, sizeof(oc->comment), str_comment);
  1598. }
  1599. output_files[nb_output_files] = oc;
  1600. /* dump the file content */
  1601. dump_format(oc, nb_output_files, filename, 1);
  1602. nb_output_files++;
  1603. strcpy(oc->filename, filename);
  1604. /* check filename in case of an image number is expected */
  1605. if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
  1606. if (filename_number_test(oc->filename) < 0) {
  1607. print_error(oc->filename, AVERROR_NUMEXPECTED);
  1608. exit(1);
  1609. }
  1610. }
  1611. if (!(oc->oformat->flags & AVFMT_NOFILE)) {
  1612. /* test if it already exists to avoid loosing precious files */
  1613. if (!file_overwrite &&
  1614. (strchr(filename, ':') == NULL ||
  1615. strstart(filename, "file:", NULL))) {
  1616. if (url_exist(filename)) {
  1617. int c;
  1618. printf("File '%s' already exists. Overwrite ? [y/N] ", filename);
  1619. fflush(stdout);
  1620. c = getchar();
  1621. if (toupper(c) != 'Y') {
  1622. fprintf(stderr, "Not overwriting - exiting\n");
  1623. exit(1);
  1624. }
  1625. }
  1626. }
  1627. /* open the file */
  1628. if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
  1629. fprintf(stderr, "Could not open '%s'\n", filename);
  1630. exit(1);
  1631. }
  1632. }
  1633. /* reset some options */
  1634. file_oformat = NULL;
  1635. file_iformat = NULL;
  1636. audio_disable = 0;
  1637. video_disable = 0;
  1638. audio_codec_id = CODEC_ID_NONE;
  1639. video_codec_id = CODEC_ID_NONE;
  1640. }
  1641. /* prepare dummy protocols for grab */
  1642. void prepare_grab(void)
  1643. {
  1644. int has_video, has_audio, i, j;
  1645. AVFormatContext *oc;
  1646. AVFormatContext *ic;
  1647. AVFormatParameters ap1, *ap = &ap1;
  1648. /* see if audio/video inputs are needed */
  1649. has_video = 0;
  1650. has_audio = 0;
  1651. memset(ap, 0, sizeof(*ap));
  1652. for(j=0;j<nb_output_files;j++) {
  1653. oc = output_files[j];
  1654. for(i=0;i<oc->nb_streams;i++) {
  1655. AVCodecContext *enc = &oc->streams[i]->codec;
  1656. switch(enc->codec_type) {
  1657. case CODEC_TYPE_AUDIO:
  1658. if (enc->sample_rate > ap->sample_rate)
  1659. ap->sample_rate = enc->sample_rate;
  1660. if (enc->channels > ap->channels)
  1661. ap->channels = enc->channels;
  1662. has_audio = 1;
  1663. break;
  1664. case CODEC_TYPE_VIDEO:
  1665. if (enc->width > ap->width)
  1666. ap->width = enc->width;
  1667. if (enc->height > ap->height)
  1668. ap->height = enc->height;
  1669. if (enc->frame_rate > ap->frame_rate)
  1670. ap->frame_rate = enc->frame_rate;
  1671. has_video = 1;
  1672. break;
  1673. default:
  1674. abort();
  1675. }
  1676. }
  1677. }
  1678. if (has_video == 0 && has_audio == 0) {
  1679. fprintf(stderr, "Output file must have at least one audio or video stream\n");
  1680. exit(1);
  1681. }
  1682. if (has_video) {
  1683. AVInputFormat *fmt1;
  1684. fmt1 = av_find_input_format("video_grab_device");
  1685. if (av_open_input_file(&ic, "", fmt1, 0, ap) < 0) {
  1686. fprintf(stderr, "Could not find video grab device\n");
  1687. exit(1);
  1688. }
  1689. /* by now video grab has one stream */
  1690. ic->streams[0]->r_frame_rate = ap->frame_rate;
  1691. input_files[nb_input_files] = ic;
  1692. dump_format(ic, nb_input_files, v4l_device, 0);
  1693. nb_input_files++;
  1694. }
  1695. if (has_audio) {
  1696. AVInputFormat *fmt1;
  1697. fmt1 = av_find_input_format("audio_device");
  1698. if (av_open_input_file(&ic, "", fmt1, 0, ap) < 0) {
  1699. fprintf(stderr, "Could not find audio grab device\n");
  1700. exit(1);
  1701. }
  1702. input_files[nb_input_files] = ic;
  1703. dump_format(ic, nb_input_files, audio_device, 0);
  1704. nb_input_files++;
  1705. }
  1706. }
  1707. /* open the necessary output devices for playing */
  1708. void prepare_play(void)
  1709. {
  1710. file_iformat = NULL;
  1711. file_oformat = guess_format("audio_device", NULL, NULL);
  1712. if (!file_oformat) {
  1713. fprintf(stderr, "Could not find audio device\n");
  1714. exit(1);
  1715. }
  1716. opt_output_file(audio_device);
  1717. }
  1718. #ifndef CONFIG_WIN32
  1719. INT64 getutime(void)
  1720. {
  1721. struct rusage rusage;
  1722. getrusage(RUSAGE_SELF, &rusage);
  1723. return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
  1724. }
  1725. #else
  1726. INT64 getutime(void)
  1727. {
  1728. return gettime();
  1729. }
  1730. #endif
  1731. extern int ffm_nopts;
  1732. void opt_bitexact(void)
  1733. {
  1734. avcodec_set_bit_exact();
  1735. /* disable generate of real time pts in ffm (need to be supressed anyway) */
  1736. ffm_nopts = 1;
  1737. }
  1738. void show_formats(void)
  1739. {
  1740. AVInputFormat *ifmt;
  1741. AVOutputFormat *ofmt;
  1742. URLProtocol *up;
  1743. AVCodec *p;
  1744. const char **pp;
  1745. printf("File formats:\n");
  1746. printf(" Encoding:");
  1747. for(ofmt = first_oformat; ofmt != NULL; ofmt = ofmt->next) {
  1748. printf(" %s", ofmt->name);
  1749. }
  1750. printf("\n");
  1751. printf(" Decoding:");
  1752. for(ifmt = first_iformat; ifmt != NULL; ifmt = ifmt->next) {
  1753. printf(" %s", ifmt->name);
  1754. }
  1755. printf("\n");
  1756. printf("Codecs:\n");
  1757. printf(" Encoders:");
  1758. for(p = first_avcodec; p != NULL; p = p->next) {
  1759. if (p->encode)
  1760. printf(" %s", p->name);
  1761. }
  1762. printf("\n");
  1763. printf(" Decoders:");
  1764. for(p = first_avcodec; p != NULL; p = p->next) {
  1765. if (p->decode)
  1766. printf(" %s", p->name);
  1767. }
  1768. printf("\n");
  1769. printf("Supported file protocols:");
  1770. for(up = first_protocol; up != NULL; up = up->next)
  1771. printf(" %s:", up->name);
  1772. printf("\n");
  1773. printf("Frame size abbreviations: sqcif qcif cif 4cif\n");
  1774. printf("Motion estimation methods:");
  1775. pp = motion_str;
  1776. while (*pp) {
  1777. printf(" %s", *pp);
  1778. if ((pp - motion_str + 1) == ME_ZERO)
  1779. printf("(fastest)");
  1780. else if ((pp - motion_str + 1) == ME_FULL)
  1781. printf("(slowest)");
  1782. else if ((pp - motion_str + 1) == ME_EPZS)
  1783. printf("(default)");
  1784. pp++;
  1785. }
  1786. printf("\n");
  1787. exit(1);
  1788. }
  1789. void show_help(void)
  1790. {
  1791. const char *prog;
  1792. const OptionDef *po;
  1793. int i, expert;
  1794. prog = do_play ? "ffplay" : "ffmpeg";
  1795. printf("%s version " FFMPEG_VERSION ", Copyright (c) 2000, 2001, 2002 Fabrice Bellard\n",
  1796. prog);
  1797. if (!do_play) {
  1798. printf("usage: ffmpeg [[options] -i input_file]... {[options] outfile}...\n"
  1799. "Hyper fast MPEG1/MPEG4/H263/RV and AC3/MPEG audio encoder\n");
  1800. } else {
  1801. printf("usage: ffplay [options] input_file...\n"
  1802. "Simple audio player\n");
  1803. }
  1804. printf("\n"
  1805. "Main options are:\n");
  1806. for(i=0;i<2;i++) {
  1807. if (i == 1)
  1808. printf("\nAdvanced options are:\n");
  1809. for(po = options; po->name != NULL; po++) {
  1810. char buf[64];
  1811. expert = (po->flags & OPT_EXPERT) != 0;
  1812. if (expert == i) {
  1813. strcpy(buf, po->name);
  1814. if (po->flags & HAS_ARG) {
  1815. strcat(buf, " ");
  1816. strcat(buf, po->argname);
  1817. }
  1818. printf("-%-17s %s\n", buf, po->help);
  1819. }
  1820. }
  1821. }
  1822. exit(1);
  1823. }
  1824. const OptionDef options[] = {
  1825. { "L", 0, {(void*)show_licence}, "show license" },
  1826. { "h", 0, {(void*)show_help}, "show help" },
  1827. { "formats", 0, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
  1828. { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
  1829. { "vcd", OPT_BOOL, {(void*)&mpeg_vcd}, "output Video CD MPEG-PS compliant file" },
  1830. { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" },
  1831. { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
  1832. { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file:stream" },
  1833. { "t", HAS_ARG, {(void*)opt_recording_time}, "set the recording time", "duration" },
  1834. { "title", HAS_ARG | OPT_STRING, {(void*)&str_title}, "set the title", "string" },
  1835. { "author", HAS_ARG | OPT_STRING, {(void*)&str_author}, "set the author", "string" },
  1836. { "copyright", HAS_ARG | OPT_STRING, {(void*)&str_copyright}, "set the copyright", "string" },
  1837. { "comment", HAS_ARG | OPT_STRING, {(void*)&str_comment}, "set the comment", "string" },
  1838. /* video options */
  1839. { "b", HAS_ARG, {(void*)opt_video_bitrate}, "set video bitrate (in kbit/s)", "bitrate" },
  1840. { "r", HAS_ARG, {(void*)opt_frame_rate}, "set frame rate (in Hz)", "rate" },
  1841. { "s", HAS_ARG, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" },
  1842. { "g", HAS_ARG | OPT_EXPERT, {(void*)opt_gop_size}, "set the group of picture size", "gop_size" },
  1843. { "intra", OPT_BOOL | OPT_EXPERT, {(void*)&intra_only}, "use only intra frames"},
  1844. { "vn", OPT_BOOL, {(void*)&video_disable}, "disable video" },
  1845. { "qscale", HAS_ARG | OPT_EXPERT, {(void*)opt_qscale}, "use fixed video quantiser scale (VBR)", "q" },
  1846. { "qmin", HAS_ARG | OPT_EXPERT, {(void*)opt_qmin}, "min video quantiser scale (VBR)", "q" },
  1847. { "qmax", HAS_ARG | OPT_EXPERT, {(void*)opt_qmax}, "max video quantiser scale (VBR)", "q" },
  1848. { "qdiff", HAS_ARG | OPT_EXPERT, {(void*)opt_qdiff}, "max difference between the quantiser scale (VBR)", "q" },
  1849. { "qblur", HAS_ARG | OPT_EXPERT, {(void*)opt_qblur}, "video quantiser scale blur (VBR)", "blur" },
  1850. { "qcomp", HAS_ARG | OPT_EXPERT, {(void*)opt_qcomp}, "video quantiser scale compression (VBR)", "compression" },
  1851. { "bt", HAS_ARG, {(void*)opt_video_bitrate_tolerance}, "set video bitrate tolerance (in kbit/s)", "tolerance" },
  1852. { "vd", HAS_ARG | OPT_EXPERT, {(void*)opt_video_device}, "set video grab device", "device" },
  1853. { "vcodec", HAS_ARG | OPT_EXPERT, {(void*)opt_video_codec}, "force video codec", "codec" },
  1854. { "me", HAS_ARG | OPT_EXPERT, {(void*)opt_motion_estimation}, "set motion estimation method",
  1855. "method" },
  1856. { "bf", HAS_ARG | OPT_EXPERT, {(void*)opt_b_frames}, "use 'frames' B frames (only MPEG-4)", "frames" },
  1857. { "hq", OPT_BOOL | OPT_EXPERT, {(void*)&use_hq}, "activate high quality settings" },
  1858. { "4mv", OPT_BOOL | OPT_EXPERT, {(void*)&use_4mv}, "use four motion vector by macroblock (only MPEG-4)" },
  1859. { "sameq", OPT_BOOL, {(void*)&same_quality},
  1860. "use same video quality as source (implies VBR)" },
  1861. /* audio options */
  1862. { "ab", HAS_ARG, {(void*)opt_audio_bitrate}, "set audio bitrate (in kbit/s)", "bitrate", },
  1863. { "ar", HAS_ARG, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" },
  1864. { "ac", HAS_ARG, {(void*)opt_audio_channels}, "set number of audio channels", "channels" },
  1865. { "an", OPT_BOOL, {(void*)&audio_disable}, "disable audio" },
  1866. { "ad", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_device}, "set audio device", "device" },
  1867. { "acodec", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_codec}, "force audio codec", "codec" },
  1868. { "deinterlace", OPT_BOOL | OPT_EXPERT, {(void*)&do_deinterlace},
  1869. "deinterlace pictures" },
  1870. { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark},
  1871. "add timings for benchmarking" },
  1872. { "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump},
  1873. "dump each input packet" },
  1874. { "psnr", OPT_BOOL | OPT_EXPERT, {(void*)&do_psnr}, "calculate PSNR of compressed frames" },
  1875. { "vstats", OPT_BOOL | OPT_EXPERT, {(void*)&do_vstats}, "dump video coding statistics to file" },
  1876. { "bitexact", OPT_EXPERT, {(void*)opt_bitexact}, "only use bit exact algorithms (for codec testing)" },
  1877. { NULL, },
  1878. };
  1879. int main(int argc, char **argv)
  1880. {
  1881. int optindex, i;
  1882. const char *opt, *arg;
  1883. const OptionDef *po;
  1884. INT64 ti;
  1885. register_all();
  1886. /* detect if invoked as player */
  1887. i = strlen(argv[0]);
  1888. if (i >= 6 && !strcmp(argv[0] + i - 6, "ffplay"))
  1889. do_play = 1;
  1890. if (argc <= 1)
  1891. show_help();
  1892. /* parse options */
  1893. optindex = 1;
  1894. while (optindex < argc) {
  1895. opt = argv[optindex++];
  1896. if (opt[0] == '-' && opt[1] != '\0') {
  1897. po = options;
  1898. while (po->name != NULL) {
  1899. if (!strcmp(opt + 1, po->name))
  1900. break;
  1901. po++;
  1902. }
  1903. if (!po->name) {
  1904. fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
  1905. exit(1);
  1906. }
  1907. arg = NULL;
  1908. if (po->flags & HAS_ARG)
  1909. arg = argv[optindex++];
  1910. if (po->flags & OPT_STRING) {
  1911. char *str;
  1912. str = strdup(arg);
  1913. *po->u.str_arg = str;
  1914. } else if (po->flags & OPT_BOOL) {
  1915. *po->u.int_arg = 1;
  1916. } else {
  1917. po->u.func_arg(arg);
  1918. }
  1919. } else {
  1920. if (!do_play) {
  1921. opt_output_file(opt);
  1922. } else {
  1923. opt_input_file(opt);
  1924. }
  1925. }
  1926. }
  1927. if (!do_play) {
  1928. /* file converter / grab */
  1929. if (nb_output_files <= 0) {
  1930. fprintf(stderr, "Must supply at least one output file\n");
  1931. exit(1);
  1932. }
  1933. if (nb_input_files == 0) {
  1934. prepare_grab();
  1935. }
  1936. } else {
  1937. /* player */
  1938. if (nb_input_files <= 0) {
  1939. fprintf(stderr, "Must supply at least one input file\n");
  1940. exit(1);
  1941. }
  1942. prepare_play();
  1943. }
  1944. ti = getutime();
  1945. av_encode(output_files, nb_output_files, input_files, nb_input_files,
  1946. stream_maps, nb_stream_maps);
  1947. ti = getutime() - ti;
  1948. if (do_benchmark) {
  1949. printf("bench: utime=%0.3fs\n", ti / 1000000.0);
  1950. }
  1951. /* close files */
  1952. for(i=0;i<nb_output_files;i++) {
  1953. if (!(output_files[i]->oformat->flags & AVFMT_NOFILE))
  1954. url_fclose(&output_files[i]->pb);
  1955. }
  1956. for(i=0;i<nb_input_files;i++)
  1957. av_close_input_file(input_files[i]);
  1958. return 0;
  1959. }