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.

2222 lines
69KB

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