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.

2209 lines
65KB

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