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.

2214 lines
69KB

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