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.

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