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.

2317 lines
70KB

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