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.

2137 lines
63KB

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