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.

2319 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->real_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->real_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. if (ist->st->codec.codec_type == CODEC_TYPE_VIDEO)
  759. ist->st->codec.repeat_pict = 1;
  760. }
  761. }
  762. /* init pts */
  763. for(i=0;i<nb_istreams;i++) {
  764. ist = ist_table[i];
  765. ist->pts = 0;
  766. ist->frame_number = 0;
  767. }
  768. /* compute buffer size max (should use a complete heuristic) */
  769. for(i=0;i<nb_input_files;i++) {
  770. file_table[i].buffer_size_max = 2048;
  771. }
  772. /* open files and write file headers */
  773. for(i=0;i<nb_output_files;i++) {
  774. os = output_files[i];
  775. if (os->format->write_header(os) < 0) {
  776. fprintf(stderr, "Could not write header for output file #%d (incorrect codec paramters ?)\n", i);
  777. ret = -EINVAL;
  778. goto fail;
  779. }
  780. }
  781. #ifndef CONFIG_WIN32
  782. if (!do_play) {
  783. fprintf(stderr, "Press [q] to stop encoding\n");
  784. } else {
  785. fprintf(stderr, "Press [q] to stop playing\n");
  786. }
  787. #endif
  788. term_init();
  789. start_time = gettime();
  790. min_pts = 0;
  791. stream_no_data = 0;
  792. for(;;) {
  793. int file_index, ist_index;
  794. AVPacket pkt;
  795. UINT8 *ptr;
  796. int len;
  797. UINT8 *data_buf;
  798. int data_size, got_picture;
  799. AVPicture picture;
  800. short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
  801. redo:
  802. /* if 'q' pressed, exits */
  803. if (read_key() == 'q')
  804. break;
  805. /* select the input file with the smallest pts */
  806. file_index = -1;
  807. min_pts = MAXINT64;
  808. for(i=0;i<nb_istreams;i++) {
  809. ist = ist_table[i];
  810. /* For some reason, the pts_increment code breaks q estimation?!? */
  811. if (!ist->discard && !file_table[ist->file_index].eof_reached &&
  812. ist->pts /* + ist->pts_increment */ < min_pts && input_files[ist->file_index] != stream_no_data) {
  813. min_pts = ist->pts /* + ist->pts_increment */;
  814. file_index = ist->file_index;
  815. }
  816. }
  817. /* if none, if is finished */
  818. if (file_index < 0) {
  819. if (stream_no_data) {
  820. struct timespec ts;
  821. ts.tv_sec = 0;
  822. ts.tv_nsec = 1000 * 1000 * 10;
  823. nanosleep(&ts, 0);
  824. stream_no_data = 0;
  825. continue;
  826. }
  827. break;
  828. }
  829. /* finish if recording time exhausted */
  830. if (recording_time > 0 && min_pts >= recording_time)
  831. break;
  832. /* read a packet from it and output it in the fifo */
  833. is = input_files[file_index];
  834. if (av_read_packet(is, &pkt) < 0) {
  835. file_table[file_index].eof_reached = 1;
  836. continue;
  837. }
  838. if (!pkt.size) {
  839. stream_no_data = is;
  840. } else {
  841. stream_no_data = 0;
  842. }
  843. ist_index = file_table[file_index].ist_index + pkt.stream_index;
  844. ist = ist_table[ist_index];
  845. if (ist->discard) {
  846. continue;
  847. }
  848. if (pkt.flags & PKT_FLAG_DROPPED_FRAME)
  849. ist->frame_number++;
  850. if (do_hex_dump) {
  851. printf("stream #%d, size=%d:\n", pkt.stream_index, pkt.size);
  852. hex_dump(pkt.data, pkt.size);
  853. }
  854. // printf("read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size);
  855. len = pkt.size;
  856. ptr = pkt.data;
  857. while (len > 0) {
  858. /* decode the packet if needed */
  859. data_buf = NULL; /* fail safe */
  860. data_size = 0;
  861. if (ist->decoding_needed) {
  862. switch(ist->st->codec.codec_type) {
  863. case CODEC_TYPE_AUDIO:
  864. /* XXX: could avoid copy if PCM 16 bits with same
  865. endianness as CPU */
  866. ret = avcodec_decode_audio(&ist->st->codec, samples, &data_size,
  867. ptr, len);
  868. if (ret < 0)
  869. goto fail_decode;
  870. /* Some bug in mpeg audio decoder gives */
  871. /* data_size < 0, it seems they are overflows */
  872. if (data_size <= 0) {
  873. /* no audio frame */
  874. ptr += ret;
  875. len -= ret;
  876. continue;
  877. }
  878. data_buf = (UINT8 *)samples;
  879. break;
  880. case CODEC_TYPE_VIDEO:
  881. if (ist->st->codec.codec_id == CODEC_ID_RAWVIDEO) {
  882. int size;
  883. size = (ist->st->codec.width * ist->st->codec.height);
  884. avpicture_fill(&picture, ptr,
  885. ist->st->codec.pix_fmt,
  886. ist->st->codec.width,
  887. ist->st->codec.height);
  888. ret = len;
  889. } else {
  890. data_size = (ist->st->codec.width * ist->st->codec.height * 3) / 2;
  891. ret = avcodec_decode_video(&ist->st->codec,
  892. &picture, &got_picture, ptr, len);
  893. if (ret < 0) {
  894. fail_decode:
  895. fprintf(stderr, "Error while decoding stream #%d.%d\n",
  896. ist->file_index, ist->index);
  897. av_free_packet(&pkt);
  898. goto redo;
  899. }
  900. if (!got_picture) {
  901. /* no picture yet */
  902. ptr += ret;
  903. len -= ret;
  904. continue;
  905. }
  906. }
  907. break;
  908. default:
  909. goto fail_decode;
  910. }
  911. } else {
  912. data_buf = ptr;
  913. data_size = len;
  914. ret = len;
  915. }
  916. /* update pts */
  917. switch(ist->st->codec.codec_type) {
  918. case CODEC_TYPE_AUDIO:
  919. ist->pts = (INT64)1000000 * ist->sample_index / ist->st->codec.sample_rate;
  920. ist->sample_index += data_size / (2 * ist->st->codec.channels);
  921. ist->pts_increment = (INT64) (data_size / (2 * ist->st->codec.channels)) * 1000000 / ist->st->codec.sample_rate;
  922. break;
  923. case CODEC_TYPE_VIDEO:
  924. ist->frame_number++;
  925. ist->pts = ((INT64)ist->frame_number * 1000000 * FRAME_RATE_BASE) /
  926. ist->st->codec.frame_rate;
  927. ist->pts_increment = ((INT64) 1000000 * FRAME_RATE_BASE) /
  928. ist->st->codec.frame_rate;
  929. break;
  930. default:
  931. abort();
  932. }
  933. ptr += ret;
  934. len -= ret;
  935. /* transcode raw format, encode packets and output them */
  936. for(i=0;i<nb_ostreams;i++) {
  937. int frame_size;
  938. ost = ost_table[i];
  939. if (ost->source_index == ist_index) {
  940. os = output_files[ost->file_index];
  941. if (ost->encoding_needed) {
  942. switch(ost->st->codec.codec_type) {
  943. case CODEC_TYPE_AUDIO:
  944. do_audio_out(os, ost, ist, data_buf, data_size);
  945. break;
  946. case CODEC_TYPE_VIDEO:
  947. do_video_out(os, ost, ist, &picture, &frame_size);
  948. if (do_vstats)
  949. do_video_stats(ost, ist, frame_size);
  950. break;
  951. default:
  952. abort();
  953. }
  954. } else {
  955. /* no reencoding needed : output the packet directly */
  956. /* force the input stream PTS */
  957. os->format->write_packet(os, ost->index, data_buf, data_size, pkt.pts);
  958. }
  959. }
  960. }
  961. }
  962. av_free_packet(&pkt);
  963. /* dump report by using the first video and audio streams */
  964. {
  965. char buf[1024];
  966. AVFormatContext *oc;
  967. INT64 total_size, ti;
  968. AVCodecContext *enc;
  969. int frame_number, vid;
  970. double bitrate, ti1;
  971. static INT64 last_time;
  972. if ((min_pts - last_time) >= 500000) {
  973. last_time = min_pts;
  974. oc = output_files[0];
  975. total_size = url_ftell(&oc->pb);
  976. buf[0] = '\0';
  977. ti = MAXINT64;
  978. vid = 0;
  979. for(i=0;i<nb_ostreams;i++) {
  980. ost = ost_table[i];
  981. enc = &ost->st->codec;
  982. ist = ist_table[ost->source_index];
  983. if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
  984. frame_number = ist->frame_number;
  985. sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
  986. frame_number, enc->quality);
  987. if (do_psnr)
  988. sprintf(buf + strlen(buf), "PSNR=%6.2f ", enc->psnr_y);
  989. vid = 1;
  990. }
  991. /* compute min pts value */
  992. if (!ist->discard && ist->pts < ti) {
  993. ti = ist->pts;
  994. }
  995. }
  996. ti1 = (double)ti / 1000000.0;
  997. if (ti1 < 0.01)
  998. ti1 = 0.01;
  999. bitrate = (double)(total_size * 8) / ti1 / 1000.0;
  1000. sprintf(buf + strlen(buf),
  1001. "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
  1002. (double)total_size / 1024, ti1, bitrate);
  1003. fprintf(stderr, "%s \r", buf);
  1004. fflush(stderr);
  1005. }
  1006. }
  1007. }
  1008. term_exit();
  1009. /* dump report by using the first video and audio streams */
  1010. {
  1011. char buf[1024];
  1012. AVFormatContext *oc;
  1013. INT64 total_size, ti;
  1014. AVCodecContext *enc;
  1015. int frame_number, vid;
  1016. double bitrate, ti1;
  1017. oc = output_files[0];
  1018. total_size = url_ftell(&oc->pb);
  1019. buf[0] = '\0';
  1020. ti = MAXINT64;
  1021. vid = 0;
  1022. for(i=0;i<nb_ostreams;i++) {
  1023. ost = ost_table[i];
  1024. enc = &ost->st->codec;
  1025. ist = ist_table[ost->source_index];
  1026. if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
  1027. frame_number = ist->frame_number;
  1028. sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
  1029. frame_number, enc->quality);
  1030. if (do_psnr)
  1031. sprintf(buf + strlen(buf), "PSNR=%6.2f ", enc->psnr_y);
  1032. vid = 1;
  1033. }
  1034. /* compute min pts value */
  1035. if (!ist->discard && ist->pts < ti) {
  1036. ti = ist->pts;
  1037. }
  1038. }
  1039. ti1 = ti / 1000000.0;
  1040. if (ti1 < 0.01)
  1041. ti1 = 0.01;
  1042. bitrate = (double)(total_size * 8) / ti1 / 1000.0;
  1043. sprintf(buf + strlen(buf),
  1044. "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
  1045. (double)total_size / 1024, ti1, bitrate);
  1046. fprintf(stderr, "%s \n", buf);
  1047. }
  1048. /* close each encoder */
  1049. for(i=0;i<nb_ostreams;i++) {
  1050. ost = ost_table[i];
  1051. if (ost->encoding_needed) {
  1052. avcodec_close(&ost->st->codec);
  1053. }
  1054. }
  1055. /* close each decoder */
  1056. for(i=0;i<nb_istreams;i++) {
  1057. ist = ist_table[i];
  1058. if (ist->decoding_needed) {
  1059. avcodec_close(&ist->st->codec);
  1060. }
  1061. }
  1062. /* write the trailer if needed and close file */
  1063. for(i=0;i<nb_output_files;i++) {
  1064. os = output_files[i];
  1065. os->format->write_trailer(os);
  1066. }
  1067. /* finished ! */
  1068. ret = 0;
  1069. fail1:
  1070. free(file_table);
  1071. if (ist_table) {
  1072. for(i=0;i<nb_istreams;i++) {
  1073. ist = ist_table[i];
  1074. if (ist) {
  1075. free(ist);
  1076. }
  1077. }
  1078. free(ist_table);
  1079. }
  1080. if (ost_table) {
  1081. for(i=0;i<nb_ostreams;i++) {
  1082. ost = ost_table[i];
  1083. if (ost) {
  1084. if (ost->pict_tmp.data[0])
  1085. free(ost->pict_tmp.data[0]);
  1086. if (ost->video_resample)
  1087. img_resample_close(ost->img_resample_ctx);
  1088. if (ost->audio_resample)
  1089. audio_resample_close(ost->resample);
  1090. free(ost);
  1091. }
  1092. }
  1093. free(ost_table);
  1094. }
  1095. return ret;
  1096. fail:
  1097. ret = -ENOMEM;
  1098. goto fail1;
  1099. }
  1100. #if 0
  1101. int file_read(const char *filename)
  1102. {
  1103. URLContext *h;
  1104. unsigned char buffer[1024];
  1105. int len, i;
  1106. if (url_open(&h, filename, O_RDONLY) < 0) {
  1107. printf("could not open '%s'\n", filename);
  1108. return -1;
  1109. }
  1110. for(;;) {
  1111. len = url_read(h, buffer, sizeof(buffer));
  1112. if (len <= 0)
  1113. break;
  1114. for(i=0;i<len;i++) putchar(buffer[i]);
  1115. }
  1116. url_close(h);
  1117. return 0;
  1118. }
  1119. #endif
  1120. void show_licence(void)
  1121. {
  1122. printf(
  1123. "ffmpeg version " FFMPEG_VERSION "\n"
  1124. "Copyright (c) 2000,2001 Gerard Lantau\n"
  1125. "This program is free software; you can redistribute it and/or modify\n"
  1126. "it under the terms of the GNU General Public License as published by\n"
  1127. "the Free Software Foundation; either version 2 of the License, or\n"
  1128. "(at your option) any later version.\n"
  1129. "\n"
  1130. "This program is distributed in the hope that it will be useful,\n"
  1131. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1132. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  1133. "GNU General Public License for more details.\n"
  1134. "\n"
  1135. "You should have received a copy of the GNU General Public License\n"
  1136. "along with this program; if not, write to the Free Software\n"
  1137. "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"
  1138. );
  1139. exit(1);
  1140. }
  1141. void opt_format(const char *arg)
  1142. {
  1143. AVFormat *f;
  1144. f = first_format;
  1145. while (f != NULL && strcmp(f->name, arg) != 0) f = f->next;
  1146. if (f == NULL) {
  1147. fprintf(stderr, "Invalid format: %s\n", arg);
  1148. exit(1);
  1149. }
  1150. file_format = f;
  1151. }
  1152. void opt_video_bitrate(const char *arg)
  1153. {
  1154. video_bit_rate = atoi(arg) * 1000;
  1155. }
  1156. void opt_video_bitrate_tolerance(const char *arg)
  1157. {
  1158. video_bit_rate_tolerance = atoi(arg) * 1000;
  1159. }
  1160. void opt_frame_rate(const char *arg)
  1161. {
  1162. frame_rate = (int)(strtod(arg, 0) * FRAME_RATE_BASE);
  1163. }
  1164. void opt_frame_size(const char *arg)
  1165. {
  1166. parse_image_size(&frame_width, &frame_height, arg);
  1167. if (frame_width <= 0 || frame_height <= 0) {
  1168. fprintf(stderr, "Incorrect frame size\n");
  1169. exit(1);
  1170. }
  1171. if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
  1172. fprintf(stderr, "Frame size must be a multiple of 2\n");
  1173. exit(1);
  1174. }
  1175. }
  1176. void opt_gop_size(const char *arg)
  1177. {
  1178. gop_size = atoi(arg);
  1179. }
  1180. void opt_b_frames(const char *arg)
  1181. {
  1182. b_frames = atoi(arg);
  1183. if (b_frames > FF_MAX_B_FRAMES) {
  1184. fprintf(stderr, "\nCannot have more than %d B frames, increase FF_MAX_B_FRAMES.\n", FF_MAX_B_FRAMES);
  1185. exit(1);
  1186. } else if (b_frames < 1) {
  1187. fprintf(stderr, "\nNumber of B frames must be higher than 0\n");
  1188. exit(1);
  1189. }
  1190. }
  1191. void opt_qscale(const char *arg)
  1192. {
  1193. video_qscale = atoi(arg);
  1194. if (video_qscale < 0 ||
  1195. video_qscale > 31) {
  1196. fprintf(stderr, "qscale must be >= 1 and <= 31\n");
  1197. exit(1);
  1198. }
  1199. }
  1200. void opt_qmin(const char *arg)
  1201. {
  1202. video_qmin = atoi(arg);
  1203. if (video_qmin < 0 ||
  1204. video_qmin > 31) {
  1205. fprintf(stderr, "qmin must be >= 1 and <= 31\n");
  1206. exit(1);
  1207. }
  1208. }
  1209. void opt_qmax(const char *arg)
  1210. {
  1211. video_qmax = atoi(arg);
  1212. if (video_qmax < 0 ||
  1213. video_qmax > 31) {
  1214. fprintf(stderr, "qmax must be >= 1 and <= 31\n");
  1215. exit(1);
  1216. }
  1217. }
  1218. void opt_qdiff(const char *arg)
  1219. {
  1220. video_qdiff = atoi(arg);
  1221. if (video_qdiff < 0 ||
  1222. video_qdiff > 31) {
  1223. fprintf(stderr, "qdiff must be >= 1 and <= 31\n");
  1224. exit(1);
  1225. }
  1226. }
  1227. void opt_qblur(const char *arg)
  1228. {
  1229. video_qblur = atof(arg);
  1230. }
  1231. void opt_qcomp(const char *arg)
  1232. {
  1233. video_qcomp = atof(arg);
  1234. }
  1235. void opt_audio_bitrate(const char *arg)
  1236. {
  1237. audio_bit_rate = atoi(arg) * 1000;
  1238. }
  1239. void opt_audio_rate(const char *arg)
  1240. {
  1241. audio_sample_rate = atoi(arg);
  1242. }
  1243. void opt_audio_channels(const char *arg)
  1244. {
  1245. audio_channels = atoi(arg);
  1246. }
  1247. #ifdef CONFIG_GRAB
  1248. void opt_video_device(const char *arg)
  1249. {
  1250. v4l_device = strdup(arg);
  1251. }
  1252. void opt_audio_device(const char *arg)
  1253. {
  1254. audio_device = strdup(arg);
  1255. }
  1256. #endif
  1257. void opt_audio_codec(const char *arg)
  1258. {
  1259. AVCodec *p;
  1260. p = first_avcodec;
  1261. while (p) {
  1262. if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
  1263. break;
  1264. p = p->next;
  1265. }
  1266. if (p == NULL) {
  1267. fprintf(stderr, "Unknown audio codec '%s'\n", arg);
  1268. exit(1);
  1269. } else {
  1270. audio_codec_id = p->id;
  1271. }
  1272. }
  1273. const char *motion_str[] = {
  1274. "zero",
  1275. "full",
  1276. "log",
  1277. "phods",
  1278. "epzs",
  1279. "x1",
  1280. NULL,
  1281. };
  1282. void opt_motion_estimation(const char *arg)
  1283. {
  1284. const char **p;
  1285. p = motion_str;
  1286. for(;;) {
  1287. if (!*p) {
  1288. fprintf(stderr, "Unknown motion estimation method '%s'\n", arg);
  1289. exit(1);
  1290. }
  1291. if (!strcmp(*p, arg))
  1292. break;
  1293. p++;
  1294. }
  1295. me_method = (p - motion_str) + 1;
  1296. }
  1297. void opt_video_codec(const char *arg)
  1298. {
  1299. AVCodec *p;
  1300. p = first_avcodec;
  1301. while (p) {
  1302. if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
  1303. break;
  1304. p = p->next;
  1305. }
  1306. if (p == NULL) {
  1307. fprintf(stderr, "Unknown video codec '%s'\n", arg);
  1308. exit(1);
  1309. } else {
  1310. video_codec_id = p->id;
  1311. }
  1312. }
  1313. void opt_map(const char *arg)
  1314. {
  1315. AVStreamMap *m;
  1316. const char *p;
  1317. p = arg;
  1318. m = &stream_maps[nb_stream_maps++];
  1319. m->file_index = strtol(arg, (char **)&p, 0);
  1320. if (*p)
  1321. p++;
  1322. m->stream_index = strtol(p, (char **)&p, 0);
  1323. }
  1324. void opt_recording_time(const char *arg)
  1325. {
  1326. recording_time = parse_date(arg, 1);
  1327. }
  1328. /* return the number of packet read to find the codec parameters */
  1329. int find_codec_parameters(AVFormatContext *ic)
  1330. {
  1331. int val, i, count, ret, got_picture, size;
  1332. AVCodec *codec;
  1333. AVCodecContext *enc;
  1334. AVStream *st;
  1335. AVPacket *pkt;
  1336. AVPicture picture;
  1337. AVPacketList *pktl, **ppktl;
  1338. short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
  1339. UINT8 *ptr;
  1340. count = 0;
  1341. ppktl = &ic->packet_buffer;
  1342. for(;;) {
  1343. for(i=0;i<ic->nb_streams;i++) {
  1344. enc = &ic->streams[i]->codec;
  1345. switch(enc->codec_type) {
  1346. case CODEC_TYPE_AUDIO:
  1347. val = enc->sample_rate;
  1348. break;
  1349. case CODEC_TYPE_VIDEO:
  1350. val = enc->width;
  1351. break;
  1352. default:
  1353. val = 1;
  1354. break;
  1355. }
  1356. /* if no parameters supplied, then we should read it from
  1357. the stream */
  1358. if (val == 0)
  1359. break;
  1360. }
  1361. if (i == ic->nb_streams) {
  1362. ret = count;
  1363. break;
  1364. }
  1365. if (count == 0) {
  1366. /* open each codec */
  1367. for(i=0;i<ic->nb_streams;i++) {
  1368. st = ic->streams[i];
  1369. codec = avcodec_find_decoder(st->codec.codec_id);
  1370. if (codec == NULL) {
  1371. ret = -1;
  1372. goto the_end;
  1373. }
  1374. ret = avcodec_open(&st->codec, codec);
  1375. if (ret < 0)
  1376. goto the_end;
  1377. }
  1378. }
  1379. pktl = av_mallocz(sizeof(AVPacketList));
  1380. if (!pktl) {
  1381. ret = -1;
  1382. break;
  1383. }
  1384. /* add the packet in the buffered packet list */
  1385. *ppktl = pktl;
  1386. ppktl = &pktl->next;
  1387. pkt = &pktl->pkt;
  1388. if (ic->format->read_packet(ic, pkt) < 0) {
  1389. ret = -1;
  1390. break;
  1391. }
  1392. st = ic->streams[pkt->stream_index];
  1393. /* decode the data and update codec parameters */
  1394. ptr = pkt->data;
  1395. size = pkt->size;
  1396. while (size > 0) {
  1397. switch(st->codec.codec_type) {
  1398. case CODEC_TYPE_VIDEO:
  1399. ret = avcodec_decode_video(&st->codec, &picture, &got_picture, ptr, size);
  1400. break;
  1401. case CODEC_TYPE_AUDIO:
  1402. ret = avcodec_decode_audio(&st->codec, samples, &got_picture, ptr, size);
  1403. break;
  1404. default:
  1405. ret = -1;
  1406. break;
  1407. }
  1408. if (ret < 0) {
  1409. ret = -1;
  1410. goto the_end;
  1411. }
  1412. if (got_picture)
  1413. break;
  1414. ptr += ret;
  1415. size -= ret;
  1416. }
  1417. count++;
  1418. }
  1419. the_end:
  1420. if (count > 0) {
  1421. /* close each codec */
  1422. for(i=0;i<ic->nb_streams;i++) {
  1423. st = ic->streams[i];
  1424. avcodec_close(&st->codec);
  1425. }
  1426. }
  1427. return ret;
  1428. }
  1429. int filename_number_test(const char *filename)
  1430. {
  1431. char buf[1024];
  1432. if (get_frame_filename(buf, sizeof(buf), filename, 1) < 0) {
  1433. fprintf(stderr, "%s: Incorrect image filename syntax.\n"
  1434. "Use '%%d' to specify the image number:\n"
  1435. " for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
  1436. " for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
  1437. filename);
  1438. return -1;
  1439. } else {
  1440. return 0;
  1441. }
  1442. }
  1443. void opt_input_file(const char *filename)
  1444. {
  1445. AVFormatContext *ic;
  1446. AVFormatParameters params, *ap = &params;
  1447. URLFormat url_format;
  1448. AVFormat *fmt;
  1449. int err, i, ret;
  1450. ic = av_mallocz(sizeof(AVFormatContext));
  1451. strcpy(ic->filename, filename);
  1452. /* first format guess to know if we must open file */
  1453. fmt = file_format;
  1454. if (!fmt)
  1455. fmt = guess_format(NULL, filename, NULL);
  1456. if (fmt == NULL || !(fmt->flags & AVFMT_NOFILE)) {
  1457. /* open file */
  1458. if (url_fopen(&ic->pb, filename, URL_RDONLY) < 0) {
  1459. fprintf(stderr, "Could not open '%s'\n", filename);
  1460. exit(1);
  1461. }
  1462. /* find format and set default parameters */
  1463. fmt = file_format;
  1464. err = url_getformat(url_fileno(&ic->pb), &url_format);
  1465. if (err >= 0) {
  1466. if (!fmt)
  1467. fmt = guess_format(url_format.format_name, NULL, NULL);
  1468. ap->sample_rate = url_format.sample_rate;
  1469. ap->frame_rate = url_format.frame_rate;
  1470. ap->channels = url_format.channels;
  1471. ap->width = url_format.width;
  1472. ap->height = url_format.height;
  1473. ap->pix_fmt = url_format.pix_fmt;
  1474. } else {
  1475. if (!fmt)
  1476. fmt = guess_format(NULL, filename, NULL);
  1477. memset(ap, 0, sizeof(*ap));
  1478. }
  1479. } else {
  1480. memset(ap, 0, sizeof(*ap));
  1481. }
  1482. if (!fmt || !fmt->read_header) {
  1483. fprintf(stderr, "%s: Unknown file format\n", filename);
  1484. exit(1);
  1485. }
  1486. ic->format = fmt;
  1487. /* get default parameters from command line */
  1488. if (!ap->sample_rate)
  1489. ap->sample_rate = audio_sample_rate;
  1490. if (!ap->channels)
  1491. ap->channels = audio_channels;
  1492. if (!ap->frame_rate)
  1493. ap->frame_rate = frame_rate;
  1494. if (!ap->width)
  1495. ap->width = frame_width;
  1496. if (!ap->height)
  1497. ap->height = frame_height;
  1498. /* check filename in case of an image number is expected */
  1499. if (ic->format->flags & AVFMT_NEEDNUMBER) {
  1500. if (filename_number_test(ic->filename) < 0)
  1501. exit(1);
  1502. }
  1503. err = ic->format->read_header(ic, ap);
  1504. if (err < 0) {
  1505. fprintf(stderr, "%s: Error while parsing header\n", filename);
  1506. exit(1);
  1507. }
  1508. /* If not enough info for the codecs, we decode the first frames
  1509. to get it. (used in mpeg case for example) */
  1510. ret = find_codec_parameters(ic);
  1511. if (ret < 0) {
  1512. fprintf(stderr, "%s: could not find codec parameters\n", filename);
  1513. exit(1);
  1514. }
  1515. /* update the current parameters so that they match the one of the input stream */
  1516. for(i=0;i<ic->nb_streams;i++) {
  1517. AVCodecContext *enc = &ic->streams[i]->codec;
  1518. switch(enc->codec_type) {
  1519. case CODEC_TYPE_AUDIO:
  1520. //fprintf(stderr, "\nInput Audio channels: %d", enc->channels);
  1521. audio_channels = enc->channels;
  1522. audio_sample_rate = enc->sample_rate;
  1523. break;
  1524. case CODEC_TYPE_VIDEO:
  1525. frame_height = enc->height;
  1526. frame_width = enc->width;
  1527. frame_rate = enc->frame_rate;
  1528. break;
  1529. default:
  1530. abort();
  1531. }
  1532. }
  1533. input_files[nb_input_files] = ic;
  1534. /* dump the file content */
  1535. dump_format(ic, nb_input_files, filename, 0);
  1536. nb_input_files++;
  1537. file_format = NULL;
  1538. }
  1539. void check_audio_video_inputs(int *has_video_ptr, int *has_audio_ptr)
  1540. {
  1541. int has_video, has_audio, i, j;
  1542. AVFormatContext *ic;
  1543. has_video = 0;
  1544. has_audio = 0;
  1545. for(j=0;j<nb_input_files;j++) {
  1546. ic = input_files[j];
  1547. for(i=0;i<ic->nb_streams;i++) {
  1548. AVCodecContext *enc = &ic->streams[i]->codec;
  1549. switch(enc->codec_type) {
  1550. case CODEC_TYPE_AUDIO:
  1551. has_audio = 1;
  1552. break;
  1553. case CODEC_TYPE_VIDEO:
  1554. has_video = 1;
  1555. break;
  1556. default:
  1557. abort();
  1558. }
  1559. }
  1560. }
  1561. *has_video_ptr = has_video;
  1562. *has_audio_ptr = has_audio;
  1563. }
  1564. void opt_output_file(const char *filename)
  1565. {
  1566. AVStream *st;
  1567. AVFormatContext *oc;
  1568. int use_video, use_audio, nb_streams, input_has_video, input_has_audio;
  1569. int codec_id;
  1570. if (!strcmp(filename, "-"))
  1571. filename = "pipe:";
  1572. oc = av_mallocz(sizeof(AVFormatContext));
  1573. if (!file_format) {
  1574. file_format = guess_format(NULL, filename, NULL);
  1575. if (!file_format)
  1576. file_format = &mpeg_mux_format;
  1577. }
  1578. oc->format = file_format;
  1579. if (!strcmp(file_format->name, "ffm") &&
  1580. strstart(filename, "http:", NULL)) {
  1581. /* special case for files sent to ffserver: we get the stream
  1582. parameters from ffserver */
  1583. if (read_ffserver_streams(oc, filename) < 0) {
  1584. fprintf(stderr, "Could not read stream parameters from '%s'\n", filename);
  1585. exit(1);
  1586. }
  1587. } else {
  1588. use_video = file_format->video_codec != CODEC_ID_NONE;
  1589. use_audio = file_format->audio_codec != CODEC_ID_NONE;
  1590. /* disable if no corresponding type found and at least one
  1591. input file */
  1592. if (nb_input_files > 0) {
  1593. check_audio_video_inputs(&input_has_video, &input_has_audio);
  1594. if (!input_has_video)
  1595. use_video = 0;
  1596. if (!input_has_audio)
  1597. use_audio = 0;
  1598. }
  1599. /* manual disable */
  1600. if (audio_disable) {
  1601. use_audio = 0;
  1602. }
  1603. if (video_disable) {
  1604. use_video = 0;
  1605. }
  1606. nb_streams = 0;
  1607. if (use_video) {
  1608. AVCodecContext *video_enc;
  1609. st = av_mallocz(sizeof(AVStream));
  1610. if (!st) {
  1611. fprintf(stderr, "Could not alloc stream\n");
  1612. exit(1);
  1613. }
  1614. video_enc = &st->codec;
  1615. codec_id = file_format->video_codec;
  1616. if (video_codec_id != CODEC_ID_NONE)
  1617. codec_id = video_codec_id;
  1618. video_enc->codec_id = codec_id;
  1619. video_enc->codec_type = CODEC_TYPE_VIDEO;
  1620. video_enc->bit_rate = video_bit_rate;
  1621. video_enc->bit_rate_tolerance = video_bit_rate_tolerance;
  1622. video_enc->frame_rate = frame_rate;
  1623. video_enc->width = frame_width;
  1624. video_enc->height = frame_height;
  1625. if (!intra_only)
  1626. video_enc->gop_size = gop_size;
  1627. else
  1628. video_enc->gop_size = 0;
  1629. if (video_qscale || same_quality) {
  1630. video_enc->flags |= CODEC_FLAG_QSCALE;
  1631. video_enc->quality = video_qscale;
  1632. }
  1633. if (use_hq) {
  1634. video_enc->flags |= CODEC_FLAG_HQ;
  1635. }
  1636. if (use_4mv) {
  1637. video_enc->flags |= CODEC_FLAG_HQ;
  1638. video_enc->flags |= CODEC_FLAG_4MV;
  1639. }
  1640. if (b_frames) {
  1641. if (codec_id != CODEC_ID_MPEG4) {
  1642. fprintf(stderr, "\nB frames encoding only supported by MPEG-4.\n");
  1643. exit(1);
  1644. }
  1645. video_enc->max_b_frames = b_frames;
  1646. video_enc->b_frame_strategy = 0;
  1647. video_enc->b_quant_factor = 2.0;
  1648. }
  1649. video_enc->qmin = video_qmin;
  1650. video_enc->qmax = video_qmax;
  1651. video_enc->max_qdiff = video_qdiff;
  1652. video_enc->qblur = video_qblur;
  1653. video_enc->qcompress = video_qcomp;
  1654. if (do_psnr)
  1655. video_enc->get_psnr = 1;
  1656. else
  1657. video_enc->get_psnr = 0;
  1658. video_enc->me_method = me_method;
  1659. /* XXX: need to find a way to set codec parameters */
  1660. if (oc->format == &ppm_format ||
  1661. oc->format == &ppmpipe_format) {
  1662. video_enc->pix_fmt = PIX_FMT_RGB24;
  1663. }
  1664. oc->streams[nb_streams] = st;
  1665. nb_streams++;
  1666. }
  1667. if (use_audio) {
  1668. AVCodecContext *audio_enc;
  1669. st = av_mallocz(sizeof(AVStream));
  1670. if (!st) {
  1671. fprintf(stderr, "Could not alloc stream\n");
  1672. exit(1);
  1673. }
  1674. audio_enc = &st->codec;
  1675. codec_id = file_format->audio_codec;
  1676. if (audio_codec_id != CODEC_ID_NONE)
  1677. codec_id = audio_codec_id;
  1678. audio_enc->codec_id = codec_id;
  1679. audio_enc->codec_type = CODEC_TYPE_AUDIO;
  1680. audio_enc->bit_rate = audio_bit_rate;
  1681. audio_enc->sample_rate = audio_sample_rate;
  1682. /* For audio codecs other than AC3 we limit */
  1683. /* the number of coded channels to stereo */
  1684. if (audio_channels > 2 && codec_id != CODEC_ID_AC3) {
  1685. audio_enc->channels = 2;
  1686. } else
  1687. audio_enc->channels = audio_channels;
  1688. oc->streams[nb_streams] = st;
  1689. nb_streams++;
  1690. }
  1691. oc->nb_streams = nb_streams;
  1692. if (!nb_streams) {
  1693. fprintf(stderr, "No audio or video streams available\n");
  1694. exit(1);
  1695. }
  1696. if (str_title)
  1697. nstrcpy(oc->title, sizeof(oc->title), str_title);
  1698. if (str_author)
  1699. nstrcpy(oc->author, sizeof(oc->author), str_author);
  1700. if (str_copyright)
  1701. nstrcpy(oc->copyright, sizeof(oc->copyright), str_copyright);
  1702. if (str_comment)
  1703. nstrcpy(oc->comment, sizeof(oc->comment), str_comment);
  1704. }
  1705. output_files[nb_output_files] = oc;
  1706. /* dump the file content */
  1707. dump_format(oc, nb_output_files, filename, 1);
  1708. nb_output_files++;
  1709. strcpy(oc->filename, filename);
  1710. /* check filename in case of an image number is expected */
  1711. if (oc->format->flags & AVFMT_NEEDNUMBER) {
  1712. if (filename_number_test(oc->filename) < 0)
  1713. exit(1);
  1714. }
  1715. if (!(oc->format->flags & AVFMT_NOFILE)) {
  1716. /* test if it already exists to avoid loosing precious files */
  1717. if (!file_overwrite &&
  1718. (strchr(filename, ':') == NULL ||
  1719. strstart(filename, "file:", NULL))) {
  1720. if (url_exist(filename)) {
  1721. int c;
  1722. printf("File '%s' already exists. Overwrite ? [y/N] ", filename);
  1723. fflush(stdout);
  1724. c = getchar();
  1725. if (toupper(c) != 'Y') {
  1726. fprintf(stderr, "Not overwriting - exiting\n");
  1727. exit(1);
  1728. }
  1729. }
  1730. }
  1731. /* open the file */
  1732. if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
  1733. fprintf(stderr, "Could not open '%s'\n", filename);
  1734. exit(1);
  1735. }
  1736. }
  1737. /* reset some options */
  1738. file_format = NULL;
  1739. audio_disable = 0;
  1740. video_disable = 0;
  1741. audio_codec_id = CODEC_ID_NONE;
  1742. video_codec_id = CODEC_ID_NONE;
  1743. }
  1744. #ifdef CONFIG_GRAB
  1745. /* prepare dummy protocols for grab */
  1746. void prepare_grab(void)
  1747. {
  1748. int has_video, has_audio, i, j;
  1749. AVFormatContext *oc;
  1750. AVFormatContext *ic;
  1751. AVFormatParameters ap1, *ap = &ap1;
  1752. /* see if audio/video inputs are needed */
  1753. has_video = 0;
  1754. has_audio = 0;
  1755. memset(ap, 0, sizeof(*ap));
  1756. for(j=0;j<nb_output_files;j++) {
  1757. oc = output_files[j];
  1758. for(i=0;i<oc->nb_streams;i++) {
  1759. AVCodecContext *enc = &oc->streams[i]->codec;
  1760. switch(enc->codec_type) {
  1761. case CODEC_TYPE_AUDIO:
  1762. if (enc->sample_rate > ap->sample_rate)
  1763. ap->sample_rate = enc->sample_rate;
  1764. if (enc->channels > ap->channels)
  1765. ap->channels = enc->channels;
  1766. has_audio = 1;
  1767. break;
  1768. case CODEC_TYPE_VIDEO:
  1769. if (enc->width > ap->width)
  1770. ap->width = enc->width;
  1771. if (enc->height > ap->height)
  1772. ap->height = enc->height;
  1773. if (enc->frame_rate > ap->frame_rate)
  1774. ap->frame_rate = enc->frame_rate;
  1775. has_video = 1;
  1776. break;
  1777. default:
  1778. abort();
  1779. }
  1780. }
  1781. }
  1782. if (has_video == 0 && has_audio == 0) {
  1783. fprintf(stderr, "Output file must have at least one audio or video stream\n");
  1784. exit(1);
  1785. }
  1786. if (has_video) {
  1787. ic = av_open_input_file("", "video_grab_device", 0, ap);
  1788. if (!ic) {
  1789. fprintf(stderr, "Could not open video grab device\n");
  1790. exit(1);
  1791. }
  1792. input_files[nb_input_files] = ic;
  1793. dump_format(ic, nb_input_files, v4l_device, 0);
  1794. nb_input_files++;
  1795. }
  1796. if (has_audio) {
  1797. ic = av_open_input_file("", "audio_device", 0, ap);
  1798. if (!ic) {
  1799. fprintf(stderr, "Could not open audio grab device\n");
  1800. exit(1);
  1801. }
  1802. input_files[nb_input_files] = ic;
  1803. dump_format(ic, nb_input_files, audio_device, 0);
  1804. nb_input_files++;
  1805. }
  1806. }
  1807. #else
  1808. void prepare_grab(void)
  1809. {
  1810. fprintf(stderr, "Must supply at least one input file\n");
  1811. exit(1);
  1812. }
  1813. #endif
  1814. /* open the necessary output devices for playing */
  1815. void prepare_play(void)
  1816. {
  1817. #ifndef __BEOS__
  1818. file_format = guess_format("audio_device", NULL, NULL);
  1819. if (!file_format) {
  1820. fprintf(stderr, "Could not find audio device\n");
  1821. exit(1);
  1822. }
  1823. opt_output_file(audio_device);
  1824. #endif
  1825. }
  1826. #ifndef CONFIG_WIN32
  1827. INT64 getutime(void)
  1828. {
  1829. struct rusage rusage;
  1830. getrusage(RUSAGE_SELF, &rusage);
  1831. return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
  1832. }
  1833. #else
  1834. INT64 getutime(void)
  1835. {
  1836. return gettime();
  1837. }
  1838. #endif
  1839. void show_formats(void)
  1840. {
  1841. AVFormat *f;
  1842. URLProtocol *up;
  1843. AVCodec *p;
  1844. const char **pp;
  1845. printf("File formats:\n");
  1846. printf(" Encoding:");
  1847. for(f = first_format; f != NULL; f = f->next) {
  1848. if (f->write_header)
  1849. printf(" %s", f->name);
  1850. }
  1851. printf("\n");
  1852. printf(" Decoding:");
  1853. for(f = first_format; f != NULL; f = f->next) {
  1854. if (f->read_header)
  1855. printf(" %s", f->name);
  1856. }
  1857. printf("\n");
  1858. printf("Codecs:\n");
  1859. printf(" Encoders:");
  1860. for(p = first_avcodec; p != NULL; p = p->next) {
  1861. if (p->encode)
  1862. printf(" %s", p->name);
  1863. }
  1864. printf("\n");
  1865. printf(" Decoders:");
  1866. for(p = first_avcodec; p != NULL; p = p->next) {
  1867. if (p->decode)
  1868. printf(" %s", p->name);
  1869. }
  1870. printf("\n");
  1871. printf("Supported file protocols:");
  1872. for(up = first_protocol; up != NULL; up = up->next)
  1873. printf(" %s:", up->name);
  1874. printf("\n");
  1875. printf("Frame size abbreviations: sqcif qcif cif 4cif\n");
  1876. printf("Motion estimation methods:");
  1877. pp = motion_str;
  1878. while (*pp) {
  1879. printf(" %s", *pp);
  1880. if ((pp - motion_str + 1) == ME_ZERO)
  1881. printf("(fastest)");
  1882. else if ((pp - motion_str + 1) == ME_FULL)
  1883. printf("(slowest)");
  1884. else if ((pp - motion_str + 1) == ME_EPZS)
  1885. printf("(default)");
  1886. pp++;
  1887. }
  1888. printf("\n");
  1889. exit(1);
  1890. }
  1891. void show_help(void)
  1892. {
  1893. const char *prog;
  1894. const OptionDef *po;
  1895. int i, expert;
  1896. prog = do_play ? "ffplay" : "ffmpeg";
  1897. printf("%s version " FFMPEG_VERSION ", Copyright (c) 2000, 2001 Gerard Lantau\n",
  1898. prog);
  1899. if (!do_play) {
  1900. printf("usage: ffmpeg [[options] -i input_file]... {[options] outfile}...\n"
  1901. "Hyper fast MPEG1/MPEG4/H263/RV and AC3/MPEG audio encoder\n");
  1902. } else {
  1903. printf("usage: ffplay [options] input_file...\n"
  1904. "Simple audio player\n");
  1905. }
  1906. printf("\n"
  1907. "Main options are:\n");
  1908. for(i=0;i<2;i++) {
  1909. if (i == 1)
  1910. printf("\nAdvanced options are:\n");
  1911. for(po = options; po->name != NULL; po++) {
  1912. char buf[64];
  1913. expert = (po->flags & OPT_EXPERT) != 0;
  1914. if (expert == i) {
  1915. strcpy(buf, po->name);
  1916. if (po->flags & HAS_ARG) {
  1917. strcat(buf, " ");
  1918. strcat(buf, po->argname);
  1919. }
  1920. printf("-%-17s %s\n", buf, po->help);
  1921. }
  1922. }
  1923. }
  1924. exit(1);
  1925. }
  1926. const OptionDef options[] = {
  1927. { "L", 0, {(void*)show_licence}, "show license" },
  1928. { "h", 0, {(void*)show_help}, "show help" },
  1929. { "formats", 0, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
  1930. { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
  1931. { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" },
  1932. { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
  1933. { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file:stream" },
  1934. { "t", HAS_ARG, {(void*)opt_recording_time}, "set the recording time", "duration" },
  1935. { "title", HAS_ARG | OPT_STRING, {(void*)&str_title}, "set the title", "string" },
  1936. { "author", HAS_ARG | OPT_STRING, {(void*)&str_author}, "set the author", "string" },
  1937. { "copyright", HAS_ARG | OPT_STRING, {(void*)&str_copyright}, "set the copyright", "string" },
  1938. { "comment", HAS_ARG | OPT_STRING, {(void*)&str_comment}, "set the comment", "string" },
  1939. /* video options */
  1940. { "b", HAS_ARG, {(void*)opt_video_bitrate}, "set video bitrate (in kbit/s)", "bitrate" },
  1941. { "r", HAS_ARG, {(void*)opt_frame_rate}, "set frame rate (in Hz)", "rate" },
  1942. { "s", HAS_ARG, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" },
  1943. { "g", HAS_ARG | OPT_EXPERT, {(void*)opt_gop_size}, "set the group of picture size", "gop_size" },
  1944. { "intra", OPT_BOOL | OPT_EXPERT, {(void*)&intra_only}, "use only intra frames"},
  1945. { "vn", OPT_BOOL, {(void*)&video_disable}, "disable video" },
  1946. { "qscale", HAS_ARG | OPT_EXPERT, {(void*)opt_qscale}, "use fixed video quantiser scale (VBR)", "q" },
  1947. { "qmin", HAS_ARG | OPT_EXPERT, {(void*)opt_qmin}, "min video quantiser scale (VBR)", "q" },
  1948. { "qmax", HAS_ARG | OPT_EXPERT, {(void*)opt_qmax}, "max video quantiser scale (VBR)", "q" },
  1949. { "qdiff", HAS_ARG | OPT_EXPERT, {(void*)opt_qdiff}, "max difference between the quantiser scale (VBR)", "q" },
  1950. { "qblur", HAS_ARG | OPT_EXPERT, {(void*)opt_qblur}, "video quantiser scale blur (VBR)", "blur" },
  1951. { "qcomp", HAS_ARG | OPT_EXPERT, {(void*)opt_qcomp}, "video quantiser scale compression (VBR)", "compression" },
  1952. { "bt", HAS_ARG, {(void*)opt_video_bitrate_tolerance}, "set video bitrate tolerance (in kbit/s)", "tolerance" },
  1953. #ifdef CONFIG_GRAB
  1954. { "vd", HAS_ARG | OPT_EXPERT, {(void*)opt_video_device}, "set video device", "device" },
  1955. #endif
  1956. { "vcodec", HAS_ARG | OPT_EXPERT, {(void*)opt_video_codec}, "force video codec", "codec" },
  1957. { "me", HAS_ARG | OPT_EXPERT, {(void*)opt_motion_estimation}, "set motion estimation method",
  1958. "method" },
  1959. { "bf", HAS_ARG | OPT_EXPERT, {(void*)opt_b_frames}, "use 'frames' B frames (only MPEG-4)", "frames" },
  1960. { "hq", OPT_BOOL | OPT_EXPERT, {(void*)&use_hq}, "activate high quality settings" },
  1961. { "4mv", OPT_BOOL | OPT_EXPERT, {(void*)&use_4mv}, "use four motion vector by macroblock (only MPEG-4)" },
  1962. { "sameq", OPT_BOOL, {(void*)&same_quality},
  1963. "use same video quality as source (implies VBR)" },
  1964. /* audio options */
  1965. { "ab", HAS_ARG, {(void*)opt_audio_bitrate}, "set audio bitrate (in kbit/s)", "bitrate", },
  1966. { "ar", HAS_ARG, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" },
  1967. { "ac", HAS_ARG, {(void*)opt_audio_channels}, "set number of audio channels", "channels" },
  1968. { "an", OPT_BOOL, {(void*)&audio_disable}, "disable audio" },
  1969. #ifdef CONFIG_GRAB
  1970. { "ad", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_device}, "set audio device", "device" },
  1971. #endif
  1972. { "acodec", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_codec}, "force audio codec", "codec" },
  1973. { "deinterlace", OPT_BOOL | OPT_EXPERT, {(void*)&do_deinterlace},
  1974. "deinterlace pictures" },
  1975. { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark},
  1976. "add timings for benchmarking" },
  1977. { "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump},
  1978. "dump each input packet" },
  1979. { "psnr", OPT_BOOL | OPT_EXPERT, {(void*)&do_psnr}, "calculate PSNR of compressed frames" },
  1980. { "vstats", OPT_BOOL | OPT_EXPERT, {(void*)&do_vstats}, "dump video coding statistics to file" },
  1981. { NULL, },
  1982. };
  1983. int main(int argc, char **argv)
  1984. {
  1985. int optindex, i;
  1986. const char *opt, *arg;
  1987. const OptionDef *po;
  1988. INT64 ti;
  1989. register_all();
  1990. /* detect if invoked as player */
  1991. i = strlen(argv[0]);
  1992. if (i >= 6 && !strcmp(argv[0] + i - 6, "ffplay"))
  1993. do_play = 1;
  1994. if (argc <= 1)
  1995. show_help();
  1996. /* parse options */
  1997. optindex = 1;
  1998. while (optindex < argc) {
  1999. opt = argv[optindex++];
  2000. if (opt[0] == '-' && opt[1] != '\0') {
  2001. po = options;
  2002. while (po->name != NULL) {
  2003. if (!strcmp(opt + 1, po->name))
  2004. break;
  2005. po++;
  2006. }
  2007. if (!po->name) {
  2008. fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
  2009. exit(1);
  2010. }
  2011. arg = NULL;
  2012. if (po->flags & HAS_ARG)
  2013. arg = argv[optindex++];
  2014. if (po->flags & OPT_STRING) {
  2015. char *str;
  2016. str = strdup(arg);
  2017. *po->u.str_arg = str;
  2018. } else if (po->flags & OPT_BOOL) {
  2019. *po->u.int_arg = 1;
  2020. } else {
  2021. po->u.func_arg(arg);
  2022. }
  2023. } else {
  2024. if (!do_play) {
  2025. opt_output_file(opt);
  2026. } else {
  2027. opt_input_file(opt);
  2028. }
  2029. }
  2030. }
  2031. if (!do_play) {
  2032. /* file converter / grab */
  2033. if (nb_output_files <= 0) {
  2034. fprintf(stderr, "Must supply at least one output file\n");
  2035. exit(1);
  2036. }
  2037. if (nb_input_files == 0) {
  2038. prepare_grab();
  2039. }
  2040. } else {
  2041. /* player */
  2042. if (nb_input_files <= 0) {
  2043. fprintf(stderr, "Must supply at least one input file\n");
  2044. exit(1);
  2045. }
  2046. prepare_play();
  2047. }
  2048. ti = getutime();
  2049. av_encode(output_files, nb_output_files, input_files, nb_input_files,
  2050. stream_maps, nb_stream_maps);
  2051. ti = getutime() - ti;
  2052. if (do_benchmark) {
  2053. printf("bench: utime=%0.3fs\n", ti / 1000000.0);
  2054. }
  2055. /* close files */
  2056. for(i=0;i<nb_output_files;i++) {
  2057. if (!(output_files[i]->format->flags & AVFMT_NOFILE))
  2058. url_fclose(&output_files[i]->pb);
  2059. }
  2060. for(i=0;i<nb_input_files;i++) {
  2061. if (!(input_files[i]->format->flags & AVFMT_NOFILE))
  2062. url_fclose(&input_files[i]->pb);
  2063. }
  2064. return 0;
  2065. }