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.

2263 lines
71KB

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