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.

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