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.

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