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.

2659 lines
84KB

  1. /*
  2. * FFmpeg main
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library 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 GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #define HAVE_AV_CONFIG_H
  20. #include "avformat.h"
  21. #include "framehook.h"
  22. #ifndef CONFIG_WIN32
  23. #include <unistd.h>
  24. #include <fcntl.h>
  25. #include <sys/ioctl.h>
  26. #include <sys/time.h>
  27. #include <termios.h>
  28. #include <sys/resource.h>
  29. #endif
  30. #include <time.h>
  31. #include <ctype.h>
  32. #define MAXINT64 INT64_C(0x7fffffffffffffff)
  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 AVInputFormat *file_iformat;
  63. static AVOutputFormat *file_oformat;
  64. static int frame_width = 160;
  65. static int frame_height = 128;
  66. static int frame_topBand = 0;
  67. static int frame_bottomBand = 0;
  68. static int frame_leftBand = 0;
  69. static int frame_rightBand = 0;
  70. static int frame_rate = 25 * FRAME_RATE_BASE;
  71. static int video_bit_rate = 200*1000;
  72. static int video_bit_rate_tolerance = 4000*1000;
  73. static int video_qscale = 0;
  74. static int video_qmin = 2;
  75. static int video_qmax = 31;
  76. static int video_qdiff = 3;
  77. static float video_qblur = 0.5;
  78. static float video_qcomp = 0.5;
  79. #if 0 //experimental, (can be removed)
  80. static float video_rc_qsquish=1.0;
  81. static float video_rc_qmod_amp=0;
  82. static int video_rc_qmod_freq=0;
  83. #endif
  84. static char *video_rc_override_string=NULL;
  85. static char *video_rc_eq="tex^qComp";
  86. static int video_rc_buffer_size=0;
  87. static float video_rc_buffer_aggressivity=1.0;
  88. static int video_rc_max_rate=0;
  89. static int video_rc_min_rate=0;
  90. static float video_rc_initial_cplx=0;
  91. static float video_b_qfactor = 1.25;
  92. static float video_b_qoffset = 1.25;
  93. static float video_i_qfactor = -0.8;
  94. static float video_i_qoffset = 0.0;
  95. static int me_method = 0;
  96. static int video_disable = 0;
  97. static int video_codec_id = CODEC_ID_NONE;
  98. static int same_quality = 0;
  99. static int b_frames = 0;
  100. static int use_hq = 0;
  101. static int use_4mv = 0;
  102. static int do_deinterlace = 0;
  103. static int workaround_bugs = FF_BUG_AUTODETECT;
  104. static int error_resilience = 2;
  105. static int error_concealment = 3;
  106. static int dct_algo = 0;
  107. static int idct_algo = 0;
  108. static int use_part = 0;
  109. static int packet_size = 0;
  110. static int gop_size = 12;
  111. static int intra_only = 0;
  112. static int audio_sample_rate = 44100;
  113. static int audio_bit_rate = 64000;
  114. static int audio_disable = 0;
  115. static int audio_channels = 1;
  116. static int audio_codec_id = CODEC_ID_NONE;
  117. static INT64 recording_time = 0;
  118. static int file_overwrite = 0;
  119. static char *str_title = NULL;
  120. static char *str_author = NULL;
  121. static char *str_copyright = NULL;
  122. static char *str_comment = NULL;
  123. static int do_benchmark = 0;
  124. static int do_hex_dump = 0;
  125. static int do_play = 0;
  126. static int do_psnr = 0;
  127. static int do_vstats = 0;
  128. static int do_pass = 0;
  129. static char *pass_logfilename = NULL;
  130. static int audio_stream_copy = 0;
  131. static int video_stream_copy = 0;
  132. #define DEFAULT_PASS_LOGFILENAME "ffmpeg2pass"
  133. #if !defined(CONFIG_AUDIO_OSS) && !defined(CONFIG_AUDIO_BEOS)
  134. const char *audio_device = "none";
  135. #endif
  136. #ifndef CONFIG_VIDEO4LINUX
  137. const char *v4l_device = "none";
  138. #endif
  139. typedef struct AVOutputStream {
  140. int file_index; /* file index */
  141. int index; /* stream index in the output file */
  142. int source_index; /* AVInputStream index */
  143. AVStream *st; /* stream in the output file */
  144. int encoding_needed; /* true if encoding needed for this stream */
  145. int frame_number;
  146. /* input pts and corresponding output pts
  147. for A/V sync */
  148. double sync_ipts;
  149. INT64 sync_opts;
  150. /* video only */
  151. AVPicture pict_tmp; /* temporary image for resizing */
  152. int video_resample;
  153. ImgReSampleContext *img_resample_ctx; /* for image resampling */
  154. /* audio only */
  155. int audio_resample;
  156. ReSampleContext *resample; /* for audio resampling */
  157. FifoBuffer fifo; /* for compression: one audio fifo per codec */
  158. FILE *logfile;
  159. } AVOutputStream;
  160. typedef struct AVInputStream {
  161. int file_index;
  162. int index;
  163. AVStream *st;
  164. int discard; /* true if stream data should be discarded */
  165. int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
  166. INT64 sample_index; /* current sample */
  167. int frame_decoded; /* true if a video or audio frame has been decoded */
  168. } AVInputStream;
  169. typedef struct AVInputFile {
  170. int eof_reached; /* true if eof reached */
  171. int ist_index; /* index of first stream in ist_table */
  172. int buffer_size; /* current total buffer size */
  173. int buffer_size_max; /* buffer size at which we consider we can stop
  174. buffering */
  175. int nb_streams; /* nb streams we are aware of */
  176. } AVInputFile;
  177. #ifndef CONFIG_WIN32
  178. /* init terminal so that we can grab keys */
  179. static struct termios oldtty;
  180. static void term_exit(void)
  181. {
  182. tcsetattr (0, TCSANOW, &oldtty);
  183. }
  184. static void term_init(void)
  185. {
  186. struct termios tty;
  187. tcgetattr (0, &tty);
  188. oldtty = tty;
  189. tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
  190. |INLCR|IGNCR|ICRNL|IXON);
  191. tty.c_oflag |= OPOST;
  192. tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
  193. tty.c_cflag &= ~(CSIZE|PARENB);
  194. tty.c_cflag |= CS8;
  195. tty.c_cc[VMIN] = 1;
  196. tty.c_cc[VTIME] = 0;
  197. tcsetattr (0, TCSANOW, &tty);
  198. atexit(term_exit);
  199. #ifdef CONFIG_BEOS_NETSERVER
  200. fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK);
  201. #endif
  202. }
  203. /* read a key without blocking */
  204. static int read_key(void)
  205. {
  206. int n = 1;
  207. unsigned char ch;
  208. #ifndef CONFIG_BEOS_NETSERVER
  209. struct timeval tv;
  210. fd_set rfds;
  211. FD_ZERO(&rfds);
  212. FD_SET(0, &rfds);
  213. tv.tv_sec = 0;
  214. tv.tv_usec = 0;
  215. n = select(1, &rfds, NULL, NULL, &tv);
  216. #endif
  217. if (n > 0) {
  218. n = read(0, &ch, 1);
  219. if (n == 1)
  220. return ch;
  221. return n;
  222. }
  223. return -1;
  224. }
  225. #else
  226. /* no interactive support */
  227. static void term_exit(void)
  228. {
  229. }
  230. static void term_init(void)
  231. {
  232. }
  233. static int read_key(void)
  234. {
  235. return 0;
  236. }
  237. #endif
  238. int read_ffserver_streams(AVFormatContext *s, const char *filename)
  239. {
  240. int i, err;
  241. AVFormatContext *ic;
  242. err = av_open_input_file(&ic, filename, NULL, FFM_PACKET_SIZE, NULL);
  243. if (err < 0)
  244. return err;
  245. /* copy stream format */
  246. s->nb_streams = ic->nb_streams;
  247. for(i=0;i<ic->nb_streams;i++) {
  248. AVStream *st;
  249. st = av_mallocz(sizeof(AVFormatContext));
  250. memcpy(st, ic->streams[i], sizeof(AVStream));
  251. s->streams[i] = st;
  252. }
  253. av_close_input_file(ic);
  254. return 0;
  255. }
  256. #define MAX_AUDIO_PACKET_SIZE 16384
  257. static void do_audio_out(AVFormatContext *s,
  258. AVOutputStream *ost,
  259. AVInputStream *ist,
  260. unsigned char *buf, int size)
  261. {
  262. UINT8 *buftmp;
  263. UINT8 audio_buf[2*MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
  264. UINT8 audio_out[4*MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it - yep really WMA */
  265. int size_out, frame_bytes, ret;
  266. AVCodecContext *enc;
  267. enc = &ost->st->codec;
  268. if (ost->audio_resample) {
  269. buftmp = audio_buf;
  270. size_out = audio_resample(ost->resample,
  271. (short *)buftmp, (short *)buf,
  272. size / (ist->st->codec.channels * 2));
  273. size_out = size_out * enc->channels * 2;
  274. } else {
  275. buftmp = buf;
  276. size_out = size;
  277. }
  278. /* now encode as many frames as possible */
  279. if (enc->frame_size > 1) {
  280. /* output resampled raw samples */
  281. fifo_write(&ost->fifo, buftmp, size_out,
  282. &ost->fifo.wptr);
  283. frame_bytes = enc->frame_size * 2 * enc->channels;
  284. while (fifo_read(&ost->fifo, audio_buf, frame_bytes,
  285. &ost->fifo.rptr) == 0) {
  286. ret = avcodec_encode_audio(enc, audio_out, sizeof(audio_out),
  287. (short *)audio_buf);
  288. av_write_frame(s, ost->index, audio_out, ret);
  289. }
  290. } else {
  291. /* output a pcm frame */
  292. /* XXX: change encoding codec API to avoid this ? */
  293. switch(enc->codec->id) {
  294. case CODEC_ID_PCM_S16LE:
  295. case CODEC_ID_PCM_S16BE:
  296. case CODEC_ID_PCM_U16LE:
  297. case CODEC_ID_PCM_U16BE:
  298. break;
  299. default:
  300. size_out = size_out >> 1;
  301. break;
  302. }
  303. ret = avcodec_encode_audio(enc, audio_out, size_out,
  304. (short *)buftmp);
  305. av_write_frame(s, ost->index, audio_out, ret);
  306. }
  307. }
  308. /* write a picture to a raw mux */
  309. static void write_picture(AVFormatContext *s, int index, AVPicture *picture,
  310. int pix_fmt, int w, int h)
  311. {
  312. UINT8 *buf, *src, *dest;
  313. int size, j, i;
  314. /* XXX: not efficient, should add test if we can take
  315. directly the AVPicture */
  316. switch(pix_fmt) {
  317. case PIX_FMT_YUV420P:
  318. size = avpicture_get_size(pix_fmt, w, h);
  319. buf = av_malloc(size);
  320. if (!buf)
  321. return;
  322. dest = buf;
  323. for(i=0;i<3;i++) {
  324. if (i == 1) {
  325. w >>= 1;
  326. h >>= 1;
  327. }
  328. src = picture->data[i];
  329. for(j=0;j<h;j++) {
  330. memcpy(dest, src, w);
  331. dest += w;
  332. src += picture->linesize[i];
  333. }
  334. }
  335. break;
  336. case PIX_FMT_YUV422P:
  337. size = (w * h) * 2;
  338. buf = av_malloc(size);
  339. if (!buf)
  340. return;
  341. dest = buf;
  342. for(i=0;i<3;i++) {
  343. if (i == 1) {
  344. w >>= 1;
  345. }
  346. src = picture->data[i];
  347. for(j=0;j<h;j++) {
  348. memcpy(dest, src, w);
  349. dest += w;
  350. src += picture->linesize[i];
  351. }
  352. }
  353. break;
  354. case PIX_FMT_YUV444P:
  355. size = (w * h) * 3;
  356. buf = av_malloc(size);
  357. if (!buf)
  358. return;
  359. dest = buf;
  360. for(i=0;i<3;i++) {
  361. src = picture->data[i];
  362. for(j=0;j<h;j++) {
  363. memcpy(dest, src, w);
  364. dest += w;
  365. src += picture->linesize[i];
  366. }
  367. }
  368. break;
  369. case PIX_FMT_YUV422:
  370. size = (w * h) * 2;
  371. buf = av_malloc(size);
  372. if (!buf)
  373. return;
  374. dest = buf;
  375. src = picture->data[0];
  376. for(j=0;j<h;j++) {
  377. memcpy(dest, src, w * 2);
  378. dest += w * 2;
  379. src += picture->linesize[0];
  380. }
  381. break;
  382. case PIX_FMT_RGB24:
  383. case PIX_FMT_BGR24:
  384. size = (w * h) * 3;
  385. buf = av_malloc(size);
  386. if (!buf)
  387. return;
  388. dest = buf;
  389. src = picture->data[0];
  390. for(j=0;j<h;j++) {
  391. memcpy(dest, src, w * 3);
  392. dest += w * 3;
  393. src += picture->linesize[0];
  394. }
  395. break;
  396. default:
  397. return;
  398. }
  399. av_write_frame(s, index, buf, size);
  400. av_free(buf);
  401. }
  402. static void pre_process_video_frame(AVInputStream *ist, AVPicture *picture, void **bufp)
  403. {
  404. AVCodecContext *dec;
  405. AVPicture *picture2;
  406. AVPicture picture_tmp;
  407. UINT8 *buf = 0;
  408. dec = &ist->st->codec;
  409. /* deinterlace : must be done before any resize */
  410. if (do_deinterlace) {
  411. int size;
  412. /* create temporary picture */
  413. size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
  414. buf = av_malloc(size);
  415. if (!buf)
  416. return;
  417. picture2 = &picture_tmp;
  418. avpicture_fill(picture2, buf, dec->pix_fmt, dec->width, dec->height);
  419. if (avpicture_deinterlace(picture2, picture,
  420. dec->pix_fmt, dec->width, dec->height) < 0) {
  421. /* if error, do not deinterlace */
  422. av_free(buf);
  423. buf = NULL;
  424. picture2 = picture;
  425. }
  426. } else {
  427. picture2 = picture;
  428. }
  429. frame_hook_process(picture2, dec->pix_fmt, dec->width, dec->height);
  430. if (picture != picture2)
  431. *picture = *picture2;
  432. *bufp = buf;
  433. }
  434. /* we begin to correct av delay at this threshold */
  435. #define AV_DELAY_MAX 0.100
  436. static void do_video_out(AVFormatContext *s,
  437. AVOutputStream *ost,
  438. AVInputStream *ist,
  439. AVPicture *picture1,
  440. int *frame_size, AVOutputStream *audio_sync)
  441. {
  442. int nb_frames, i, ret;
  443. AVPicture *picture, *pict;
  444. AVPicture picture_tmp1;
  445. static UINT8 *video_buffer;
  446. UINT8 *buf = NULL, *buf1 = NULL;
  447. AVCodecContext *enc, *dec;
  448. #define VIDEO_BUFFER_SIZE (1024*1024)
  449. enc = &ost->st->codec;
  450. dec = &ist->st->codec;
  451. /* by default, we output a single frame */
  452. nb_frames = 1;
  453. /* NOTE: the A/V sync is always done by considering the audio is
  454. the master clock. It is suffisant for transcoding or playing,
  455. but not for the general case */
  456. if (audio_sync) {
  457. /* compute the A-V delay and duplicate/remove frames if needed */
  458. double adelta, vdelta, apts, vpts, av_delay;
  459. if (audio_sync->sync_ipts != AV_NOPTS_VALUE &&
  460. ost->sync_ipts != AV_NOPTS_VALUE) {
  461. adelta = (double)(ost->st->pts.val - audio_sync->sync_opts) *
  462. s->pts_num / s->pts_den;
  463. apts = audio_sync->sync_ipts + adelta;
  464. vdelta = (double)(ost->st->pts.val - ost->sync_opts) *
  465. s->pts_num / s->pts_den;
  466. vpts = ost->sync_ipts + vdelta;
  467. av_delay = apts - vpts;
  468. // printf("delay=%f\n", av_delay);
  469. if (av_delay < -AV_DELAY_MAX)
  470. nb_frames = 2;
  471. else if (av_delay > AV_DELAY_MAX)
  472. nb_frames = 0;
  473. }
  474. } else {
  475. double vdelta;
  476. if (ost->sync_ipts != AV_NOPTS_VALUE) {
  477. vdelta = (double)(ost->st->pts.val) * s->pts_num / s->pts_den - ost->sync_ipts;
  478. if (vdelta < -AV_DELAY_MAX)
  479. nb_frames = 2;
  480. else if (vdelta > AV_DELAY_MAX)
  481. nb_frames = 0;
  482. //printf("vdelta=%f\n", vdelta);
  483. }
  484. }
  485. if (nb_frames <= 0)
  486. return;
  487. if (!video_buffer)
  488. video_buffer = av_malloc(VIDEO_BUFFER_SIZE);
  489. if (!video_buffer)
  490. return;
  491. /* convert pixel format if needed */
  492. if (enc->pix_fmt != dec->pix_fmt) {
  493. int size;
  494. /* create temporary picture */
  495. size = avpicture_get_size(enc->pix_fmt, dec->width, dec->height);
  496. buf = av_malloc(size);
  497. if (!buf)
  498. return;
  499. pict = &picture_tmp1;
  500. avpicture_fill(pict, buf, enc->pix_fmt, dec->width, dec->height);
  501. if (img_convert(pict, enc->pix_fmt,
  502. picture1, dec->pix_fmt,
  503. dec->width, dec->height) < 0) {
  504. fprintf(stderr, "pixel format conversion not handled\n");
  505. goto the_end;
  506. }
  507. } else {
  508. pict = picture1;
  509. }
  510. /* XXX: resampling could be done before raw format convertion in
  511. some cases to go faster */
  512. /* XXX: only works for YUV420P */
  513. if (ost->video_resample) {
  514. picture = &ost->pict_tmp;
  515. img_resample(ost->img_resample_ctx, picture, pict);
  516. } else {
  517. picture = pict;
  518. }
  519. /* duplicates frame if needed */
  520. /* XXX: pb because no interleaving */
  521. for(i=0;i<nb_frames;i++) {
  522. if (enc->codec_id != CODEC_ID_RAWVIDEO) {
  523. /* handles sameq here. This is not correct because it may
  524. not be a global option */
  525. if (same_quality) {
  526. enc->quality = dec->quality;
  527. }
  528. ret = avcodec_encode_video(enc,
  529. video_buffer, VIDEO_BUFFER_SIZE,
  530. picture);
  531. //enc->frame_number = enc->real_pict_num;
  532. av_write_frame(s, ost->index, video_buffer, ret);
  533. *frame_size = ret;
  534. //fprintf(stderr,"\nFrame: %3d %3d size: %5d type: %d",
  535. // enc->frame_number-1, enc->real_pict_num, ret,
  536. // enc->pict_type);
  537. /* if two pass, output log */
  538. if (ost->logfile && enc->stats_out) {
  539. fprintf(ost->logfile, "%s", enc->stats_out);
  540. }
  541. } else {
  542. if (s->oformat->flags & AVFMT_RAWPICTURE) {
  543. /* raw pictures are written as AVPicture structure to
  544. avoid any copies. We support temorarily the older
  545. method. */
  546. av_write_frame(s, ost->index,
  547. (UINT8 *)picture, sizeof(AVPicture));
  548. } else {
  549. write_picture(s, ost->index, picture, enc->pix_fmt,
  550. enc->width, enc->height);
  551. }
  552. }
  553. ost->frame_number++;
  554. }
  555. the_end:
  556. av_free(buf);
  557. av_free(buf1);
  558. }
  559. static void do_video_stats(AVFormatContext *os, AVOutputStream *ost,
  560. int frame_size)
  561. {
  562. static FILE *fvstats=NULL;
  563. static INT64 total_size = 0;
  564. char filename[40];
  565. time_t today2;
  566. struct tm *today;
  567. AVCodecContext *enc;
  568. int frame_number;
  569. INT64 ti;
  570. double ti1, bitrate, avg_bitrate;
  571. if (!fvstats) {
  572. today2 = time(NULL);
  573. today = localtime(&today2);
  574. sprintf(filename, "vstats_%02d%02d%02d.log", today->tm_hour,
  575. today->tm_min,
  576. today->tm_sec);
  577. fvstats = fopen(filename,"w");
  578. if (!fvstats) {
  579. perror("fopen");
  580. exit(1);
  581. }
  582. }
  583. ti = MAXINT64;
  584. enc = &ost->st->codec;
  585. total_size += frame_size;
  586. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  587. frame_number = ost->frame_number;
  588. fprintf(fvstats, "frame= %5d q= %2d ", frame_number, enc->quality);
  589. if (do_psnr)
  590. fprintf(fvstats, "PSNR= %6.2f ", enc->psnr_y);
  591. fprintf(fvstats,"f_size= %6d ", frame_size);
  592. /* compute pts value */
  593. ti1 = (double)ost->st->pts.val * os->pts_num / os->pts_den;
  594. if (ti1 < 0.01)
  595. ti1 = 0.01;
  596. bitrate = (double)(frame_size * 8) * enc->frame_rate / FRAME_RATE_BASE / 1000.0;
  597. avg_bitrate = (double)(total_size * 8) / ti1 / 1000.0;
  598. fprintf(fvstats, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
  599. (double)total_size / 1024, ti1, bitrate, avg_bitrate);
  600. fprintf(fvstats,"type= %s\n", enc->key_frame == 1 ? "I" : "P");
  601. }
  602. }
  603. void print_report(AVFormatContext **output_files,
  604. AVOutputStream **ost_table, int nb_ostreams,
  605. int is_last_report)
  606. {
  607. char buf[1024];
  608. AVOutputStream *ost;
  609. AVFormatContext *oc, *os;
  610. INT64 total_size;
  611. AVCodecContext *enc;
  612. int frame_number, vid, i;
  613. double bitrate, ti1, pts;
  614. static INT64 last_time = -1;
  615. if (!is_last_report) {
  616. INT64 cur_time;
  617. /* display the report every 0.5 seconds */
  618. cur_time = av_gettime();
  619. if (last_time == -1) {
  620. last_time = cur_time;
  621. return;
  622. }
  623. if ((cur_time - last_time) < 500000)
  624. return;
  625. last_time = cur_time;
  626. }
  627. oc = output_files[0];
  628. total_size = url_ftell(&oc->pb);
  629. buf[0] = '\0';
  630. ti1 = 1e10;
  631. vid = 0;
  632. for(i=0;i<nb_ostreams;i++) {
  633. ost = ost_table[i];
  634. os = output_files[ost->file_index];
  635. enc = &ost->st->codec;
  636. if (vid && enc->codec_type == CODEC_TYPE_VIDEO) {
  637. sprintf(buf + strlen(buf), "q=%2d ",
  638. enc->quality);
  639. }
  640. if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
  641. frame_number = ost->frame_number;
  642. sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
  643. frame_number, enc->quality);
  644. if (do_psnr)
  645. sprintf(buf + strlen(buf), "PSNR=%6.2f ", enc->psnr_y);
  646. vid = 1;
  647. }
  648. /* compute min output value */
  649. pts = (double)ost->st->pts.val * os->pts_num / os->pts_den;
  650. if ((pts < ti1) && (pts > 0))
  651. ti1 = pts;
  652. }
  653. if (ti1 < 0.01)
  654. ti1 = 0.01;
  655. bitrate = (double)(total_size * 8) / ti1 / 1000.0;
  656. sprintf(buf + strlen(buf),
  657. "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
  658. (double)total_size / 1024, ti1, bitrate);
  659. fprintf(stderr, "%s ", buf);
  660. if (is_last_report) {
  661. fprintf(stderr, "\n");
  662. } else {
  663. fprintf(stderr, "\r");
  664. fflush(stderr);
  665. }
  666. }
  667. /*
  668. * The following code is the main loop of the file converter
  669. */
  670. static int av_encode(AVFormatContext **output_files,
  671. int nb_output_files,
  672. AVFormatContext **input_files,
  673. int nb_input_files,
  674. AVStreamMap *stream_maps, int nb_stream_maps)
  675. {
  676. int ret, i, j, k, n, nb_istreams = 0, nb_ostreams = 0, pts_set;
  677. AVFormatContext *is, *os;
  678. AVCodecContext *codec, *icodec;
  679. AVOutputStream *ost, **ost_table = NULL;
  680. AVInputStream *ist, **ist_table = NULL;
  681. AVInputFile *file_table;
  682. AVFormatContext *stream_no_data;
  683. int key;
  684. file_table= (AVInputFile*) av_mallocz(nb_input_files * sizeof(AVInputFile));
  685. if (!file_table)
  686. goto fail;
  687. /* input stream init */
  688. j = 0;
  689. for(i=0;i<nb_input_files;i++) {
  690. is = input_files[i];
  691. file_table[i].ist_index = j;
  692. file_table[i].nb_streams = is->nb_streams;
  693. j += is->nb_streams;
  694. }
  695. nb_istreams = j;
  696. ist_table = av_mallocz(nb_istreams * sizeof(AVInputStream *));
  697. if (!ist_table)
  698. goto fail;
  699. for(i=0;i<nb_istreams;i++) {
  700. ist = av_mallocz(sizeof(AVInputStream));
  701. if (!ist)
  702. goto fail;
  703. ist_table[i] = ist;
  704. }
  705. j = 0;
  706. for(i=0;i<nb_input_files;i++) {
  707. is = input_files[i];
  708. for(k=0;k<is->nb_streams;k++) {
  709. ist = ist_table[j++];
  710. ist->st = is->streams[k];
  711. ist->file_index = i;
  712. ist->index = k;
  713. ist->discard = 1; /* the stream is discarded by default
  714. (changed later) */
  715. }
  716. }
  717. /* output stream init */
  718. nb_ostreams = 0;
  719. for(i=0;i<nb_output_files;i++) {
  720. os = output_files[i];
  721. nb_ostreams += os->nb_streams;
  722. }
  723. if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) {
  724. fprintf(stderr, "Number of stream maps must match number of output streams\n");
  725. exit(1);
  726. }
  727. ost_table = av_mallocz(sizeof(AVOutputStream *) * nb_ostreams);
  728. if (!ost_table)
  729. goto fail;
  730. for(i=0;i<nb_ostreams;i++) {
  731. ost = av_mallocz(sizeof(AVOutputStream));
  732. if (!ost)
  733. goto fail;
  734. ost_table[i] = ost;
  735. }
  736. n = 0;
  737. for(k=0;k<nb_output_files;k++) {
  738. os = output_files[k];
  739. for(i=0;i<os->nb_streams;i++) {
  740. int found;
  741. ost = ost_table[n++];
  742. ost->file_index = k;
  743. ost->index = i;
  744. ost->st = os->streams[i];
  745. if (nb_stream_maps > 0) {
  746. ost->source_index = file_table[stream_maps[n-1].file_index].ist_index +
  747. stream_maps[n-1].stream_index;
  748. } else {
  749. /* get corresponding input stream index : we select the first one with the right type */
  750. found = 0;
  751. for(j=0;j<nb_istreams;j++) {
  752. ist = ist_table[j];
  753. if (ist->discard &&
  754. ist->st->codec.codec_type == ost->st->codec.codec_type) {
  755. ost->source_index = j;
  756. found = 1;
  757. }
  758. }
  759. if (!found) {
  760. /* try again and reuse existing stream */
  761. for(j=0;j<nb_istreams;j++) {
  762. ist = ist_table[j];
  763. if (ist->st->codec.codec_type == ost->st->codec.codec_type) {
  764. ost->source_index = j;
  765. found = 1;
  766. }
  767. }
  768. if (!found) {
  769. fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n",
  770. ost->file_index, ost->index);
  771. exit(1);
  772. }
  773. }
  774. }
  775. ist = ist_table[ost->source_index];
  776. ist->discard = 0;
  777. }
  778. }
  779. /* for each output stream, we compute the right encoding parameters */
  780. for(i=0;i<nb_ostreams;i++) {
  781. ost = ost_table[i];
  782. ist = ist_table[ost->source_index];
  783. codec = &ost->st->codec;
  784. icodec = &ist->st->codec;
  785. if (ost->st->stream_copy) {
  786. /* if stream_copy is selected, no need to decode or encode */
  787. codec->codec_id = icodec->codec_id;
  788. codec->codec_type = icodec->codec_type;
  789. codec->codec_tag = icodec->codec_tag;
  790. codec->bit_rate = icodec->bit_rate;
  791. switch(codec->codec_type) {
  792. case CODEC_TYPE_AUDIO:
  793. codec->sample_rate = icodec->sample_rate;
  794. codec->channels = icodec->channels;
  795. break;
  796. case CODEC_TYPE_VIDEO:
  797. codec->frame_rate = icodec->frame_rate;
  798. codec->width = icodec->width;
  799. codec->height = icodec->height;
  800. break;
  801. default:
  802. av_abort();
  803. }
  804. } else {
  805. switch(codec->codec_type) {
  806. case CODEC_TYPE_AUDIO:
  807. if (fifo_init(&ost->fifo, 2 * MAX_AUDIO_PACKET_SIZE))
  808. goto fail;
  809. if (codec->channels == icodec->channels &&
  810. codec->sample_rate == icodec->sample_rate) {
  811. ost->audio_resample = 0;
  812. } else {
  813. if (codec->channels != icodec->channels &&
  814. icodec->codec_id == CODEC_ID_AC3) {
  815. /* Special case for 5:1 AC3 input */
  816. /* and mono or stereo output */
  817. /* Request specific number of channels */
  818. icodec->channels = codec->channels;
  819. if (codec->sample_rate == icodec->sample_rate)
  820. ost->audio_resample = 0;
  821. else {
  822. ost->audio_resample = 1;
  823. ost->resample = audio_resample_init(codec->channels, icodec->channels,
  824. codec->sample_rate,
  825. icodec->sample_rate);
  826. }
  827. /* Request specific number of channels */
  828. icodec->channels = codec->channels;
  829. } else {
  830. ost->audio_resample = 1;
  831. ost->resample = audio_resample_init(codec->channels, icodec->channels,
  832. codec->sample_rate,
  833. icodec->sample_rate);
  834. }
  835. }
  836. ist->decoding_needed = 1;
  837. ost->encoding_needed = 1;
  838. break;
  839. case CODEC_TYPE_VIDEO:
  840. if (codec->width == icodec->width &&
  841. codec->height == icodec->height) {
  842. ost->video_resample = 0;
  843. } else {
  844. UINT8 *buf;
  845. ost->video_resample = 1;
  846. buf = av_malloc((codec->width * codec->height * 3) / 2);
  847. if (!buf)
  848. goto fail;
  849. ost->pict_tmp.data[0] = buf;
  850. ost->pict_tmp.data[1] = ost->pict_tmp.data[0] + (codec->width * codec->height);
  851. ost->pict_tmp.data[2] = ost->pict_tmp.data[1] + (codec->width * codec->height) / 4;
  852. ost->pict_tmp.linesize[0] = codec->width;
  853. ost->pict_tmp.linesize[1] = codec->width / 2;
  854. ost->pict_tmp.linesize[2] = codec->width / 2;
  855. ost->img_resample_ctx = img_resample_full_init(
  856. ost->st->codec.width, ost->st->codec.height,
  857. ist->st->codec.width, ist->st->codec.height,
  858. frame_topBand, frame_bottomBand,
  859. frame_leftBand, frame_rightBand);
  860. }
  861. ost->encoding_needed = 1;
  862. ist->decoding_needed = 1;
  863. break;
  864. default:
  865. av_abort();
  866. }
  867. /* two pass mode */
  868. if (ost->encoding_needed &&
  869. (codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))) {
  870. char logfilename[1024];
  871. FILE *f;
  872. int size;
  873. char *logbuffer;
  874. snprintf(logfilename, sizeof(logfilename), "%s-%d.log",
  875. pass_logfilename ?
  876. pass_logfilename : DEFAULT_PASS_LOGFILENAME, i);
  877. if (codec->flags & CODEC_FLAG_PASS1) {
  878. f = fopen(logfilename, "w");
  879. if (!f) {
  880. perror(logfilename);
  881. exit(1);
  882. }
  883. ost->logfile = f;
  884. } else {
  885. /* read the log file */
  886. f = fopen(logfilename, "r");
  887. if (!f) {
  888. perror(logfilename);
  889. exit(1);
  890. }
  891. fseek(f, 0, SEEK_END);
  892. size = ftell(f);
  893. fseek(f, 0, SEEK_SET);
  894. logbuffer = av_malloc(size + 1);
  895. if (!logbuffer) {
  896. fprintf(stderr, "Could not allocate log buffer\n");
  897. exit(1);
  898. }
  899. fread(logbuffer, 1, size, f);
  900. fclose(f);
  901. logbuffer[size] = '\0';
  902. codec->stats_in = logbuffer;
  903. }
  904. }
  905. }
  906. }
  907. /* dump the file output parameters - cannot be done before in case
  908. of stream copy */
  909. for(i=0;i<nb_output_files;i++) {
  910. dump_format(output_files[i], i, output_files[i]->filename, 1);
  911. }
  912. /* dump the stream mapping */
  913. fprintf(stderr, "Stream mapping:\n");
  914. for(i=0;i<nb_ostreams;i++) {
  915. ost = ost_table[i];
  916. fprintf(stderr, " Stream #%d.%d -> #%d.%d\n",
  917. ist_table[ost->source_index]->file_index,
  918. ist_table[ost->source_index]->index,
  919. ost->file_index,
  920. ost->index);
  921. }
  922. /* open each encoder */
  923. for(i=0;i<nb_ostreams;i++) {
  924. ost = ost_table[i];
  925. if (ost->encoding_needed) {
  926. AVCodec *codec;
  927. codec = avcodec_find_encoder(ost->st->codec.codec_id);
  928. if (!codec) {
  929. fprintf(stderr, "Unsupported codec for output stream #%d.%d\n",
  930. ost->file_index, ost->index);
  931. exit(1);
  932. }
  933. if (avcodec_open(&ost->st->codec, codec) < 0) {
  934. fprintf(stderr, "Error while opening codec for stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height\n",
  935. ost->file_index, ost->index);
  936. exit(1);
  937. }
  938. }
  939. }
  940. /* open each decoder */
  941. for(i=0;i<nb_istreams;i++) {
  942. ist = ist_table[i];
  943. if (ist->decoding_needed) {
  944. AVCodec *codec;
  945. codec = avcodec_find_decoder(ist->st->codec.codec_id);
  946. if (!codec) {
  947. fprintf(stderr, "Unsupported codec (id=%d) for input stream #%d.%d\n",
  948. ist->st->codec.codec_id, ist->file_index, ist->index);
  949. exit(1);
  950. }
  951. if (avcodec_open(&ist->st->codec, codec) < 0) {
  952. fprintf(stderr, "Error while opening codec for input stream #%d.%d\n",
  953. ist->file_index, ist->index);
  954. exit(1);
  955. }
  956. //if (ist->st->codec.codec_type == CODEC_TYPE_VIDEO)
  957. // ist->st->codec.flags |= CODEC_FLAG_REPEAT_FIELD;
  958. ist->frame_decoded = 1;
  959. }
  960. }
  961. /* init pts */
  962. for(i=0;i<nb_istreams;i++) {
  963. ist = ist_table[i];
  964. }
  965. /* compute buffer size max (should use a complete heuristic) */
  966. for(i=0;i<nb_input_files;i++) {
  967. file_table[i].buffer_size_max = 2048;
  968. }
  969. /* open files and write file headers */
  970. for(i=0;i<nb_output_files;i++) {
  971. os = output_files[i];
  972. if (av_write_header(os) < 0) {
  973. fprintf(stderr, "Could not write header for output file #%d (incorrect codec paramters ?)\n", i);
  974. ret = -EINVAL;
  975. goto fail;
  976. }
  977. }
  978. #ifndef CONFIG_WIN32
  979. if (!do_play) {
  980. fprintf(stderr, "Press [q] to stop encoding\n");
  981. } else {
  982. fprintf(stderr, "Press [q] to stop playing\n");
  983. }
  984. #endif
  985. term_init();
  986. stream_no_data = 0;
  987. key = -1;
  988. for(;;) {
  989. int file_index, ist_index;
  990. AVPacket pkt;
  991. UINT8 *ptr;
  992. int len;
  993. UINT8 *data_buf;
  994. int data_size, got_picture;
  995. AVPicture picture;
  996. short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
  997. void *buffer_to_free;
  998. double pts_min;
  999. redo:
  1000. /* if 'q' pressed, exits */
  1001. if (key) {
  1002. /* read_key() returns 0 on EOF */
  1003. key = read_key();
  1004. if (key == 'q')
  1005. break;
  1006. }
  1007. /* select the stream that we must read now by looking at the
  1008. smallest output pts */
  1009. file_index = -1;
  1010. pts_min = 1e10;
  1011. for(i=0;i<nb_ostreams;i++) {
  1012. double pts;
  1013. ost = ost_table[i];
  1014. os = output_files[ost->file_index];
  1015. ist = ist_table[ost->source_index];
  1016. pts = (double)ost->st->pts.val * os->pts_num / os->pts_den;
  1017. if (!file_table[ist->file_index].eof_reached &&
  1018. pts < pts_min) {
  1019. pts_min = pts;
  1020. file_index = ist->file_index;
  1021. }
  1022. }
  1023. /* if none, if is finished */
  1024. if (file_index < 0) {
  1025. break;
  1026. }
  1027. /* finish if recording time exhausted */
  1028. if (recording_time > 0 && pts_min >= (recording_time / 1000000.0))
  1029. break;
  1030. /* read a packet from it and output it in the fifo */
  1031. is = input_files[file_index];
  1032. if (av_read_packet(is, &pkt) < 0) {
  1033. file_table[file_index].eof_reached = 1;
  1034. continue;
  1035. }
  1036. if (!pkt.size) {
  1037. stream_no_data = is;
  1038. } else {
  1039. stream_no_data = 0;
  1040. }
  1041. /* the following test is needed in case new streams appear
  1042. dynamically in stream : we ignore them */
  1043. if (pkt.stream_index >= file_table[file_index].nb_streams)
  1044. goto discard_packet;
  1045. ist_index = file_table[file_index].ist_index + pkt.stream_index;
  1046. ist = ist_table[ist_index];
  1047. if (ist->discard)
  1048. goto discard_packet;
  1049. if (do_hex_dump) {
  1050. printf("stream #%d, size=%d:\n", pkt.stream_index, pkt.size);
  1051. av_hex_dump(pkt.data, pkt.size);
  1052. }
  1053. // printf("read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size);
  1054. len = pkt.size;
  1055. ptr = pkt.data;
  1056. pts_set = 0;
  1057. while (len > 0) {
  1058. INT64 ipts;
  1059. ipts = AV_NOPTS_VALUE;
  1060. /* decode the packet if needed */
  1061. data_buf = NULL; /* fail safe */
  1062. data_size = 0;
  1063. if (ist->decoding_needed) {
  1064. /* NOTE1: we only take into account the PTS if a new
  1065. frame has begun (MPEG semantics) */
  1066. /* NOTE2: even if the fraction is not initialized,
  1067. av_frac_set can be used to set the integer part */
  1068. if (ist->frame_decoded &&
  1069. pkt.pts != AV_NOPTS_VALUE &&
  1070. !pts_set) {
  1071. ipts = pkt.pts;
  1072. ist->frame_decoded = 0;
  1073. pts_set = 1;
  1074. }
  1075. switch(ist->st->codec.codec_type) {
  1076. case CODEC_TYPE_AUDIO:
  1077. /* XXX: could avoid copy if PCM 16 bits with same
  1078. endianness as CPU */
  1079. ret = avcodec_decode_audio(&ist->st->codec, samples, &data_size,
  1080. ptr, len);
  1081. if (ret < 0)
  1082. goto fail_decode;
  1083. /* Some bug in mpeg audio decoder gives */
  1084. /* data_size < 0, it seems they are overflows */
  1085. if (data_size <= 0) {
  1086. /* no audio frame */
  1087. ptr += ret;
  1088. len -= ret;
  1089. continue;
  1090. }
  1091. data_buf = (UINT8 *)samples;
  1092. break;
  1093. case CODEC_TYPE_VIDEO:
  1094. if (ist->st->codec.codec_id == CODEC_ID_RAWVIDEO) {
  1095. int size;
  1096. size = (ist->st->codec.width * ist->st->codec.height);
  1097. avpicture_fill(&picture, ptr,
  1098. ist->st->codec.pix_fmt,
  1099. ist->st->codec.width,
  1100. ist->st->codec.height);
  1101. ret = len;
  1102. } else {
  1103. data_size = (ist->st->codec.width * ist->st->codec.height * 3) / 2;
  1104. ret = avcodec_decode_video(&ist->st->codec,
  1105. &picture, &got_picture, ptr, len);
  1106. if (ret < 0) {
  1107. fail_decode:
  1108. fprintf(stderr, "Error while decoding stream #%d.%d\n",
  1109. ist->file_index, ist->index);
  1110. av_free_packet(&pkt);
  1111. goto redo;
  1112. }
  1113. if (!got_picture) {
  1114. /* no picture yet */
  1115. ptr += ret;
  1116. len -= ret;
  1117. continue;
  1118. }
  1119. }
  1120. break;
  1121. default:
  1122. goto fail_decode;
  1123. }
  1124. } else {
  1125. data_buf = ptr;
  1126. data_size = len;
  1127. ret = len;
  1128. }
  1129. ptr += ret;
  1130. len -= ret;
  1131. buffer_to_free = 0;
  1132. if (ist->st->codec.codec_type == CODEC_TYPE_VIDEO) {
  1133. pre_process_video_frame(ist, &picture, &buffer_to_free);
  1134. }
  1135. ist->frame_decoded = 1;
  1136. #if 0
  1137. /* mpeg PTS deordering : if it is a P or I frame, the PTS
  1138. is the one of the next displayed one */
  1139. /* XXX: add mpeg4 too ? */
  1140. if (ist->st->codec.codec_id == CODEC_ID_MPEG1VIDEO) {
  1141. if (ist->st->codec.pict_type != B_TYPE) {
  1142. INT64 tmp;
  1143. tmp = ist->last_ip_pts;
  1144. ist->last_ip_pts = ist->frac_pts.val;
  1145. ist->frac_pts.val = tmp;
  1146. }
  1147. }
  1148. #endif
  1149. /* transcode raw format, encode packets and output them */
  1150. for(i=0;i<nb_ostreams;i++) {
  1151. int frame_size;
  1152. ost = ost_table[i];
  1153. if (ost->source_index == ist_index) {
  1154. os = output_files[ost->file_index];
  1155. if (ipts != AV_NOPTS_VALUE) {
  1156. #if 0
  1157. printf("%d: got pts=%f %f\n",
  1158. i, pkt.pts / 90000.0,
  1159. (ipts - ost->st->pts.val) / 90000.0);
  1160. #endif
  1161. /* set the input output pts pairs */
  1162. ost->sync_ipts = (double)ipts * is->pts_num /
  1163. is->pts_den;
  1164. /* XXX: take into account the various fifos,
  1165. in particular for audio */
  1166. ost->sync_opts = ost->st->pts.val;
  1167. //printf("ipts=%lld sync_ipts=%f sync_opts=%lld pts.val=%lld pkt.pts=%lld\n", ipts, ost->sync_ipts, ost->sync_opts, ost->st->pts.val, pkt.pts);
  1168. } else {
  1169. //printf("pts.val=%lld\n", ost->st->pts.val);
  1170. }
  1171. if (ost->encoding_needed) {
  1172. switch(ost->st->codec.codec_type) {
  1173. case CODEC_TYPE_AUDIO:
  1174. do_audio_out(os, ost, ist, data_buf, data_size);
  1175. break;
  1176. case CODEC_TYPE_VIDEO:
  1177. /* find an audio stream for synchro */
  1178. {
  1179. int i;
  1180. AVOutputStream *audio_sync, *ost1;
  1181. audio_sync = NULL;
  1182. for(i=0;i<nb_ostreams;i++) {
  1183. ost1 = ost_table[i];
  1184. if (ost1->file_index == ost->file_index &&
  1185. ost1->st->codec.codec_type == CODEC_TYPE_AUDIO) {
  1186. audio_sync = ost1;
  1187. break;
  1188. }
  1189. }
  1190. do_video_out(os, ost, ist, &picture, &frame_size, audio_sync);
  1191. if (do_vstats)
  1192. do_video_stats(os, ost, frame_size);
  1193. }
  1194. break;
  1195. default:
  1196. av_abort();
  1197. }
  1198. } else {
  1199. /* no reencoding needed : output the packet directly */
  1200. /* force the input stream PTS */
  1201. av_write_frame(os, ost->index, data_buf, data_size);
  1202. ost->st->codec.frame_number++;
  1203. ost->frame_number++;
  1204. }
  1205. }
  1206. }
  1207. av_free(buffer_to_free);
  1208. ipts = AV_NOPTS_VALUE;
  1209. }
  1210. discard_packet:
  1211. av_free_packet(&pkt);
  1212. /* dump report by using the output first video and audio streams */
  1213. print_report(output_files, ost_table, nb_ostreams, 0);
  1214. }
  1215. term_exit();
  1216. /* dump report by using the first video and audio streams */
  1217. print_report(output_files, ost_table, nb_ostreams, 1);
  1218. /* close each encoder */
  1219. for(i=0;i<nb_ostreams;i++) {
  1220. ost = ost_table[i];
  1221. if (ost->encoding_needed) {
  1222. av_freep(&ost->st->codec.stats_in);
  1223. avcodec_close(&ost->st->codec);
  1224. }
  1225. }
  1226. /* close each decoder */
  1227. for(i=0;i<nb_istreams;i++) {
  1228. ist = ist_table[i];
  1229. if (ist->decoding_needed) {
  1230. avcodec_close(&ist->st->codec);
  1231. }
  1232. }
  1233. /* write the trailer if needed and close file */
  1234. for(i=0;i<nb_output_files;i++) {
  1235. os = output_files[i];
  1236. av_write_trailer(os);
  1237. }
  1238. /* finished ! */
  1239. ret = 0;
  1240. fail1:
  1241. av_free(file_table);
  1242. if (ist_table) {
  1243. for(i=0;i<nb_istreams;i++) {
  1244. ist = ist_table[i];
  1245. av_free(ist);
  1246. }
  1247. av_free(ist_table);
  1248. }
  1249. if (ost_table) {
  1250. for(i=0;i<nb_ostreams;i++) {
  1251. ost = ost_table[i];
  1252. if (ost) {
  1253. if (ost->logfile) {
  1254. fclose(ost->logfile);
  1255. ost->logfile = NULL;
  1256. }
  1257. fifo_free(&ost->fifo); /* works even if fifo is not
  1258. initialized but set to zero */
  1259. av_free(ost->pict_tmp.data[0]);
  1260. if (ost->video_resample)
  1261. img_resample_close(ost->img_resample_ctx);
  1262. if (ost->audio_resample)
  1263. audio_resample_close(ost->resample);
  1264. av_free(ost);
  1265. }
  1266. }
  1267. av_free(ost_table);
  1268. }
  1269. return ret;
  1270. fail:
  1271. ret = -ENOMEM;
  1272. goto fail1;
  1273. }
  1274. #if 0
  1275. int file_read(const char *filename)
  1276. {
  1277. URLContext *h;
  1278. unsigned char buffer[1024];
  1279. int len, i;
  1280. if (url_open(&h, filename, O_RDONLY) < 0) {
  1281. printf("could not open '%s'\n", filename);
  1282. return -1;
  1283. }
  1284. for(;;) {
  1285. len = url_read(h, buffer, sizeof(buffer));
  1286. if (len <= 0)
  1287. break;
  1288. for(i=0;i<len;i++) putchar(buffer[i]);
  1289. }
  1290. url_close(h);
  1291. return 0;
  1292. }
  1293. #endif
  1294. void show_licence(void)
  1295. {
  1296. printf(
  1297. "ffmpeg version " FFMPEG_VERSION "\n"
  1298. "Copyright (c) 2000, 2001, 2002 Fabrice Bellard\n"
  1299. "This library is free software; you can redistribute it and/or\n"
  1300. "modify it under the terms of the GNU Lesser General Public\n"
  1301. "License as published by the Free Software Foundation; either\n"
  1302. "version 2 of the License, or (at your option) any later version.\n"
  1303. "\n"
  1304. "This library is distributed in the hope that it will be useful,\n"
  1305. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1306. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
  1307. "Lesser General Public License for more details.\n"
  1308. "\n"
  1309. "You should have received a copy of the GNU Lesser General Public\n"
  1310. "License along with this library; if not, write to the Free Software\n"
  1311. "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n"
  1312. );
  1313. exit(1);
  1314. }
  1315. void opt_format(const char *arg)
  1316. {
  1317. file_iformat = av_find_input_format(arg);
  1318. file_oformat = guess_format(arg, NULL, NULL);
  1319. if (!file_iformat && !file_oformat) {
  1320. fprintf(stderr, "Unknown input or output format: %s\n", arg);
  1321. exit(1);
  1322. }
  1323. }
  1324. void opt_video_bitrate(const char *arg)
  1325. {
  1326. video_bit_rate = atoi(arg) * 1000;
  1327. }
  1328. void opt_video_bitrate_tolerance(const char *arg)
  1329. {
  1330. video_bit_rate_tolerance = atoi(arg) * 1000;
  1331. }
  1332. void opt_video_bitrate_max(const char *arg)
  1333. {
  1334. video_rc_max_rate = atoi(arg) * 1000;
  1335. }
  1336. void opt_video_bitrate_min(const char *arg)
  1337. {
  1338. video_rc_min_rate = atoi(arg) * 1000;
  1339. }
  1340. void opt_video_buffer_size(const char *arg)
  1341. {
  1342. video_rc_buffer_size = atoi(arg) * 1000;
  1343. }
  1344. void opt_video_rc_eq(char *arg)
  1345. {
  1346. video_rc_eq = arg;
  1347. }
  1348. void opt_video_rc_override_string(char *arg)
  1349. {
  1350. video_rc_override_string = arg;
  1351. }
  1352. void opt_workaround_bugs(const char *arg)
  1353. {
  1354. workaround_bugs = atoi(arg);
  1355. }
  1356. void opt_dct_algo(const char *arg)
  1357. {
  1358. dct_algo = atoi(arg);
  1359. }
  1360. void opt_idct_algo(const char *arg)
  1361. {
  1362. idct_algo = atoi(arg);
  1363. }
  1364. void opt_error_resilience(const char *arg)
  1365. {
  1366. error_resilience = atoi(arg);
  1367. }
  1368. void opt_error_concealment(const char *arg)
  1369. {
  1370. error_concealment = atoi(arg);
  1371. }
  1372. void opt_frame_rate(const char *arg)
  1373. {
  1374. frame_rate = (int)(strtod(arg, 0) * FRAME_RATE_BASE);
  1375. }
  1376. void opt_frame_crop_top(const char *arg)
  1377. {
  1378. frame_topBand = atoi(arg);
  1379. if (frame_topBand < 0) {
  1380. fprintf(stderr, "Incorrect top crop size\n");
  1381. exit(1);
  1382. }
  1383. if ((frame_topBand % 2) != 0) {
  1384. fprintf(stderr, "Top crop size must be a multiple of 2\n");
  1385. exit(1);
  1386. }
  1387. if ((frame_topBand) >= frame_height){
  1388. fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
  1389. exit(1);
  1390. }
  1391. frame_height -= frame_topBand;
  1392. }
  1393. void opt_frame_crop_bottom(const char *arg)
  1394. {
  1395. frame_bottomBand = atoi(arg);
  1396. if (frame_bottomBand < 0) {
  1397. fprintf(stderr, "Incorrect bottom crop size\n");
  1398. exit(1);
  1399. }
  1400. if ((frame_bottomBand % 2) != 0) {
  1401. fprintf(stderr, "Bottom crop size must be a multiple of 2\n");
  1402. exit(1);
  1403. }
  1404. if ((frame_bottomBand) >= frame_height){
  1405. fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
  1406. exit(1);
  1407. }
  1408. frame_height -= frame_bottomBand;
  1409. }
  1410. void opt_frame_crop_left(const char *arg)
  1411. {
  1412. frame_leftBand = atoi(arg);
  1413. if (frame_leftBand < 0) {
  1414. fprintf(stderr, "Incorrect left crop size\n");
  1415. exit(1);
  1416. }
  1417. if ((frame_leftBand % 2) != 0) {
  1418. fprintf(stderr, "Left crop size must be a multiple of 2\n");
  1419. exit(1);
  1420. }
  1421. if ((frame_leftBand) >= frame_width){
  1422. fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
  1423. exit(1);
  1424. }
  1425. frame_width -= frame_leftBand;
  1426. }
  1427. void opt_frame_crop_right(const char *arg)
  1428. {
  1429. frame_rightBand = atoi(arg);
  1430. if (frame_rightBand < 0) {
  1431. fprintf(stderr, "Incorrect right crop size\n");
  1432. exit(1);
  1433. }
  1434. if ((frame_rightBand % 2) != 0) {
  1435. fprintf(stderr, "Right crop size must be a multiple of 2\n");
  1436. exit(1);
  1437. }
  1438. if ((frame_rightBand) >= frame_width){
  1439. fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
  1440. exit(1);
  1441. }
  1442. frame_width -= frame_rightBand;
  1443. }
  1444. void opt_frame_size(const char *arg)
  1445. {
  1446. parse_image_size(&frame_width, &frame_height, arg);
  1447. if (frame_width <= 0 || frame_height <= 0) {
  1448. fprintf(stderr, "Incorrect frame size\n");
  1449. exit(1);
  1450. }
  1451. if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
  1452. fprintf(stderr, "Frame size must be a multiple of 2\n");
  1453. exit(1);
  1454. }
  1455. }
  1456. void opt_gop_size(const char *arg)
  1457. {
  1458. gop_size = atoi(arg);
  1459. }
  1460. void opt_b_frames(const char *arg)
  1461. {
  1462. b_frames = atoi(arg);
  1463. if (b_frames > FF_MAX_B_FRAMES) {
  1464. fprintf(stderr, "\nCannot have more than %d B frames, increase FF_MAX_B_FRAMES.\n", FF_MAX_B_FRAMES);
  1465. exit(1);
  1466. } else if (b_frames < 1) {
  1467. fprintf(stderr, "\nNumber of B frames must be higher than 0\n");
  1468. exit(1);
  1469. }
  1470. }
  1471. void opt_qscale(const char *arg)
  1472. {
  1473. video_qscale = atoi(arg);
  1474. if (video_qscale < 0 ||
  1475. video_qscale > 31) {
  1476. fprintf(stderr, "qscale must be >= 1 and <= 31\n");
  1477. exit(1);
  1478. }
  1479. }
  1480. void opt_qmin(const char *arg)
  1481. {
  1482. video_qmin = atoi(arg);
  1483. if (video_qmin < 0 ||
  1484. video_qmin > 31) {
  1485. fprintf(stderr, "qmin must be >= 1 and <= 31\n");
  1486. exit(1);
  1487. }
  1488. }
  1489. void opt_qmax(const char *arg)
  1490. {
  1491. video_qmax = atoi(arg);
  1492. if (video_qmax < 0 ||
  1493. video_qmax > 31) {
  1494. fprintf(stderr, "qmax must be >= 1 and <= 31\n");
  1495. exit(1);
  1496. }
  1497. }
  1498. void opt_qdiff(const char *arg)
  1499. {
  1500. video_qdiff = atoi(arg);
  1501. if (video_qdiff < 0 ||
  1502. video_qdiff > 31) {
  1503. fprintf(stderr, "qdiff must be >= 1 and <= 31\n");
  1504. exit(1);
  1505. }
  1506. }
  1507. void opt_qblur(const char *arg)
  1508. {
  1509. video_qblur = atof(arg);
  1510. }
  1511. void opt_qcomp(const char *arg)
  1512. {
  1513. video_qcomp = atof(arg);
  1514. }
  1515. void opt_rc_initial_cplx(const char *arg)
  1516. {
  1517. video_rc_initial_cplx = atof(arg);
  1518. }
  1519. void opt_b_qfactor(const char *arg)
  1520. {
  1521. video_b_qfactor = atof(arg);
  1522. }
  1523. void opt_i_qfactor(const char *arg)
  1524. {
  1525. video_i_qfactor = atof(arg);
  1526. }
  1527. void opt_b_qoffset(const char *arg)
  1528. {
  1529. video_b_qoffset = atof(arg);
  1530. }
  1531. void opt_i_qoffset(const char *arg)
  1532. {
  1533. video_i_qoffset = atof(arg);
  1534. }
  1535. void opt_packet_size(const char *arg)
  1536. {
  1537. packet_size= atoi(arg);
  1538. }
  1539. void opt_audio_bitrate(const char *arg)
  1540. {
  1541. audio_bit_rate = atoi(arg) * 1000;
  1542. }
  1543. void opt_audio_rate(const char *arg)
  1544. {
  1545. audio_sample_rate = atoi(arg);
  1546. }
  1547. void opt_audio_channels(const char *arg)
  1548. {
  1549. audio_channels = atoi(arg);
  1550. }
  1551. void opt_video_device(const char *arg)
  1552. {
  1553. v4l_device = strdup(arg);
  1554. }
  1555. void opt_audio_device(const char *arg)
  1556. {
  1557. audio_device = strdup(arg);
  1558. }
  1559. void opt_audio_codec(const char *arg)
  1560. {
  1561. AVCodec *p;
  1562. if (!strcmp(arg, "copy")) {
  1563. audio_stream_copy = 1;
  1564. } else {
  1565. p = first_avcodec;
  1566. while (p) {
  1567. if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
  1568. break;
  1569. p = p->next;
  1570. }
  1571. if (p == NULL) {
  1572. fprintf(stderr, "Unknown audio codec '%s'\n", arg);
  1573. exit(1);
  1574. } else {
  1575. audio_codec_id = p->id;
  1576. }
  1577. }
  1578. }
  1579. void add_frame_hooker(const char *arg)
  1580. {
  1581. int argc = 0;
  1582. char *argv[64];
  1583. int i;
  1584. char *args = strdup(arg);
  1585. argv[0] = strtok(args, " ");
  1586. while (argc < 62 && (argv[++argc] = strtok(NULL, " "))) {
  1587. }
  1588. i = frame_hook_add(argc, argv);
  1589. if (i != 0) {
  1590. fprintf(stderr, "Failed to add video hook function: %s\n", arg);
  1591. exit(1);
  1592. }
  1593. }
  1594. const char *motion_str[] = {
  1595. "zero",
  1596. "full",
  1597. "log",
  1598. "phods",
  1599. "epzs",
  1600. "x1",
  1601. NULL,
  1602. };
  1603. void opt_motion_estimation(const char *arg)
  1604. {
  1605. const char **p;
  1606. p = motion_str;
  1607. for(;;) {
  1608. if (!*p) {
  1609. fprintf(stderr, "Unknown motion estimation method '%s'\n", arg);
  1610. exit(1);
  1611. }
  1612. if (!strcmp(*p, arg))
  1613. break;
  1614. p++;
  1615. }
  1616. me_method = (p - motion_str) + 1;
  1617. }
  1618. void opt_video_codec(const char *arg)
  1619. {
  1620. AVCodec *p;
  1621. if (!strcmp(arg, "copy")) {
  1622. video_stream_copy = 1;
  1623. } else {
  1624. p = first_avcodec;
  1625. while (p) {
  1626. if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
  1627. break;
  1628. p = p->next;
  1629. }
  1630. if (p == NULL) {
  1631. fprintf(stderr, "Unknown video codec '%s'\n", arg);
  1632. exit(1);
  1633. } else {
  1634. video_codec_id = p->id;
  1635. }
  1636. }
  1637. }
  1638. void opt_map(const char *arg)
  1639. {
  1640. AVStreamMap *m;
  1641. const char *p;
  1642. p = arg;
  1643. m = &stream_maps[nb_stream_maps++];
  1644. m->file_index = strtol(arg, (char **)&p, 0);
  1645. if (*p)
  1646. p++;
  1647. m->stream_index = strtol(p, (char **)&p, 0);
  1648. }
  1649. void opt_recording_time(const char *arg)
  1650. {
  1651. recording_time = parse_date(arg, 1);
  1652. }
  1653. void print_error(const char *filename, int err)
  1654. {
  1655. switch(err) {
  1656. case AVERROR_NUMEXPECTED:
  1657. fprintf(stderr, "%s: Incorrect image filename syntax.\n"
  1658. "Use '%%d' to specify the image number:\n"
  1659. " for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
  1660. " for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
  1661. filename);
  1662. break;
  1663. case AVERROR_INVALIDDATA:
  1664. fprintf(stderr, "%s: Error while parsing header\n", filename);
  1665. break;
  1666. case AVERROR_NOFMT:
  1667. fprintf(stderr, "%s: Unknown format\n", filename);
  1668. break;
  1669. default:
  1670. fprintf(stderr, "%s: Error while opening file\n", filename);
  1671. break;
  1672. }
  1673. }
  1674. void opt_input_file(const char *filename)
  1675. {
  1676. AVFormatContext *ic;
  1677. AVFormatParameters params, *ap = &params;
  1678. int err, i, ret, rfps;
  1679. if (!strcmp(filename, "-"))
  1680. filename = "pipe:";
  1681. /* get default parameters from command line */
  1682. memset(ap, 0, sizeof(*ap));
  1683. ap->sample_rate = audio_sample_rate;
  1684. ap->channels = audio_channels;
  1685. ap->frame_rate = frame_rate;
  1686. ap->width = frame_width;
  1687. ap->height = frame_height;
  1688. /* open the input file with generic libav function */
  1689. err = av_open_input_file(&ic, filename, file_iformat, 0, ap);
  1690. if (err < 0) {
  1691. print_error(filename, err);
  1692. exit(1);
  1693. }
  1694. /* If not enough info to get the stream parameters, we decode the
  1695. first frames to get it. (used in mpeg case for example) */
  1696. ret = av_find_stream_info(ic);
  1697. if (ret < 0) {
  1698. fprintf(stderr, "%s: could not find codec parameters\n", filename);
  1699. exit(1);
  1700. }
  1701. /* update the current parameters so that they match the one of the input stream */
  1702. for(i=0;i<ic->nb_streams;i++) {
  1703. AVCodecContext *enc = &ic->streams[i]->codec;
  1704. switch(enc->codec_type) {
  1705. case CODEC_TYPE_AUDIO:
  1706. //fprintf(stderr, "\nInput Audio channels: %d", enc->channels);
  1707. audio_channels = enc->channels;
  1708. audio_sample_rate = enc->sample_rate;
  1709. break;
  1710. case CODEC_TYPE_VIDEO:
  1711. frame_height = enc->height;
  1712. frame_width = enc->width;
  1713. rfps = ic->streams[i]->r_frame_rate;
  1714. enc->workaround_bugs = workaround_bugs;
  1715. enc->error_resilience = error_resilience;
  1716. enc->error_concealment = error_concealment;
  1717. enc->idct_algo= idct_algo;
  1718. /* if(enc->codec->capabilities & CODEC_CAP_TRUNCATED)
  1719. enc->flags|= CODEC_FLAG_TRUNCATED; */
  1720. if(/*enc->codec_id==CODEC_ID_MPEG4 || */enc->codec_id==CODEC_ID_MPEG1VIDEO)
  1721. enc->flags|= CODEC_FLAG_TRUNCATED;
  1722. if (enc->frame_rate != rfps) {
  1723. fprintf(stderr,"\nSeems that stream %d comes from film source: %2.2f->%2.2f\n",
  1724. i, (float)enc->frame_rate / FRAME_RATE_BASE,
  1725. (float)rfps / FRAME_RATE_BASE);
  1726. }
  1727. /* update the current frame rate to match the stream frame rate */
  1728. frame_rate = rfps;
  1729. break;
  1730. default:
  1731. av_abort();
  1732. }
  1733. }
  1734. input_files[nb_input_files] = ic;
  1735. /* dump the file content */
  1736. dump_format(ic, nb_input_files, filename, 0);
  1737. nb_input_files++;
  1738. file_iformat = NULL;
  1739. file_oformat = NULL;
  1740. }
  1741. void check_audio_video_inputs(int *has_video_ptr, int *has_audio_ptr)
  1742. {
  1743. int has_video, has_audio, i, j;
  1744. AVFormatContext *ic;
  1745. has_video = 0;
  1746. has_audio = 0;
  1747. for(j=0;j<nb_input_files;j++) {
  1748. ic = input_files[j];
  1749. for(i=0;i<ic->nb_streams;i++) {
  1750. AVCodecContext *enc = &ic->streams[i]->codec;
  1751. switch(enc->codec_type) {
  1752. case CODEC_TYPE_AUDIO:
  1753. has_audio = 1;
  1754. break;
  1755. case CODEC_TYPE_VIDEO:
  1756. has_video = 1;
  1757. break;
  1758. default:
  1759. av_abort();
  1760. }
  1761. }
  1762. }
  1763. *has_video_ptr = has_video;
  1764. *has_audio_ptr = has_audio;
  1765. }
  1766. void opt_output_file(const char *filename)
  1767. {
  1768. AVStream *st;
  1769. AVFormatContext *oc;
  1770. int use_video, use_audio, nb_streams, input_has_video, input_has_audio;
  1771. int codec_id;
  1772. if (!strcmp(filename, "-"))
  1773. filename = "pipe:";
  1774. oc = av_mallocz(sizeof(AVFormatContext));
  1775. if (!file_oformat) {
  1776. file_oformat = guess_format(NULL, filename, NULL);
  1777. if (!file_oformat) {
  1778. fprintf(stderr, "Unable for find a suitable output format for '%s'\n",
  1779. filename);
  1780. exit(1);
  1781. }
  1782. }
  1783. oc->oformat = file_oformat;
  1784. if (!strcmp(file_oformat->name, "ffm") &&
  1785. strstart(filename, "http:", NULL)) {
  1786. /* special case for files sent to ffserver: we get the stream
  1787. parameters from ffserver */
  1788. if (read_ffserver_streams(oc, filename) < 0) {
  1789. fprintf(stderr, "Could not read stream parameters from '%s'\n", filename);
  1790. exit(1);
  1791. }
  1792. } else {
  1793. use_video = file_oformat->video_codec != CODEC_ID_NONE;
  1794. use_audio = file_oformat->audio_codec != CODEC_ID_NONE;
  1795. /* disable if no corresponding type found and at least one
  1796. input file */
  1797. if (nb_input_files > 0) {
  1798. check_audio_video_inputs(&input_has_video, &input_has_audio);
  1799. if (!input_has_video)
  1800. use_video = 0;
  1801. if (!input_has_audio)
  1802. use_audio = 0;
  1803. }
  1804. /* manual disable */
  1805. if (audio_disable) {
  1806. use_audio = 0;
  1807. }
  1808. if (video_disable) {
  1809. use_video = 0;
  1810. }
  1811. nb_streams = 0;
  1812. if (use_video) {
  1813. AVCodecContext *video_enc;
  1814. st = av_mallocz(sizeof(AVStream));
  1815. if (!st) {
  1816. fprintf(stderr, "Could not alloc stream\n");
  1817. exit(1);
  1818. }
  1819. video_enc = &st->codec;
  1820. if (video_stream_copy) {
  1821. st->stream_copy = 1;
  1822. video_enc->codec_type = CODEC_TYPE_VIDEO;
  1823. } else {
  1824. char *p;
  1825. int i;
  1826. codec_id = file_oformat->video_codec;
  1827. if (video_codec_id != CODEC_ID_NONE)
  1828. codec_id = video_codec_id;
  1829. video_enc->codec_id = codec_id;
  1830. video_enc->bit_rate = video_bit_rate;
  1831. video_enc->bit_rate_tolerance = video_bit_rate_tolerance;
  1832. video_enc->frame_rate = frame_rate;
  1833. video_enc->width = frame_width;
  1834. video_enc->height = frame_height;
  1835. if (!intra_only)
  1836. video_enc->gop_size = gop_size;
  1837. else
  1838. video_enc->gop_size = 0;
  1839. if (video_qscale || same_quality) {
  1840. video_enc->flags |= CODEC_FLAG_QSCALE;
  1841. video_enc->quality = video_qscale;
  1842. }
  1843. if (use_hq) {
  1844. video_enc->flags |= CODEC_FLAG_HQ;
  1845. }
  1846. if (use_4mv) {
  1847. video_enc->flags |= CODEC_FLAG_HQ;
  1848. video_enc->flags |= CODEC_FLAG_4MV;
  1849. }
  1850. if(use_part)
  1851. video_enc->flags |= CODEC_FLAG_PART;
  1852. if (b_frames) {
  1853. if (codec_id != CODEC_ID_MPEG4) {
  1854. fprintf(stderr, "\nB frames encoding only supported by MPEG-4.\n");
  1855. exit(1);
  1856. }
  1857. video_enc->max_b_frames = b_frames;
  1858. video_enc->b_frame_strategy = 0;
  1859. video_enc->b_quant_factor = 2.0;
  1860. }
  1861. video_enc->qmin = video_qmin;
  1862. video_enc->qmax = video_qmax;
  1863. video_enc->max_qdiff = video_qdiff;
  1864. video_enc->qblur = video_qblur;
  1865. video_enc->qcompress = video_qcomp;
  1866. video_enc->rc_eq = video_rc_eq;
  1867. p= video_rc_override_string;
  1868. for(i=0; p; i++){
  1869. int start, end, q;
  1870. int e=sscanf(p, "%d,%d,%d", &start, &end, &q);
  1871. if(e!=3){
  1872. fprintf(stderr, "error parsing rc_override\n");
  1873. exit(1);
  1874. }
  1875. video_enc->rc_override=
  1876. realloc(video_enc->rc_override, sizeof(RcOverride)*(i+1));
  1877. video_enc->rc_override[i].start_frame= start;
  1878. video_enc->rc_override[i].end_frame = end;
  1879. if(q>0){
  1880. video_enc->rc_override[i].qscale= q;
  1881. video_enc->rc_override[i].quality_factor= 1.0;
  1882. }
  1883. else{
  1884. video_enc->rc_override[i].qscale= 0;
  1885. video_enc->rc_override[i].quality_factor= -q/100.0;
  1886. }
  1887. p= strchr(p, '/');
  1888. if(p) p++;
  1889. }
  1890. video_enc->rc_override_count=i;
  1891. video_enc->rc_max_rate = video_rc_max_rate;
  1892. video_enc->rc_min_rate = video_rc_min_rate;
  1893. video_enc->rc_buffer_size = video_rc_buffer_size;
  1894. video_enc->rc_buffer_aggressivity= video_rc_buffer_aggressivity;
  1895. video_enc->rc_initial_cplx= video_rc_initial_cplx;
  1896. video_enc->i_quant_factor = video_i_qfactor;
  1897. video_enc->b_quant_factor = video_b_qfactor;
  1898. video_enc->i_quant_offset = video_i_qoffset;
  1899. video_enc->b_quant_offset = video_b_qoffset;
  1900. video_enc->dct_algo = dct_algo;
  1901. video_enc->idct_algo = idct_algo;
  1902. if(packet_size){
  1903. video_enc->rtp_mode= 1;
  1904. video_enc->rtp_payload_size= packet_size;
  1905. }
  1906. if (do_psnr)
  1907. video_enc->get_psnr = 1;
  1908. else
  1909. video_enc->get_psnr = 0;
  1910. video_enc->me_method = me_method;
  1911. /* two pass mode */
  1912. if (do_pass) {
  1913. if (do_pass == 1) {
  1914. video_enc->flags |= CODEC_FLAG_PASS1;
  1915. } else {
  1916. video_enc->flags |= CODEC_FLAG_PASS2;
  1917. }
  1918. }
  1919. /* XXX: need to find a way to set codec parameters */
  1920. if (oc->oformat->flags & AVFMT_RGB24) {
  1921. video_enc->pix_fmt = PIX_FMT_RGB24;
  1922. }
  1923. }
  1924. oc->streams[nb_streams] = st;
  1925. nb_streams++;
  1926. }
  1927. if (use_audio) {
  1928. AVCodecContext *audio_enc;
  1929. st = av_mallocz(sizeof(AVStream));
  1930. if (!st) {
  1931. fprintf(stderr, "Could not alloc stream\n");
  1932. exit(1);
  1933. }
  1934. audio_enc = &st->codec;
  1935. audio_enc->codec_type = CODEC_TYPE_AUDIO;
  1936. if (audio_stream_copy) {
  1937. st->stream_copy = 1;
  1938. } else {
  1939. codec_id = file_oformat->audio_codec;
  1940. if (audio_codec_id != CODEC_ID_NONE)
  1941. codec_id = audio_codec_id;
  1942. audio_enc->codec_id = codec_id;
  1943. audio_enc->bit_rate = audio_bit_rate;
  1944. audio_enc->sample_rate = audio_sample_rate;
  1945. /* For audio codecs other than AC3 we limit */
  1946. /* the number of coded channels to stereo */
  1947. if (audio_channels > 2 && codec_id != CODEC_ID_AC3) {
  1948. audio_enc->channels = 2;
  1949. } else
  1950. audio_enc->channels = audio_channels;
  1951. }
  1952. oc->streams[nb_streams] = st;
  1953. nb_streams++;
  1954. }
  1955. oc->nb_streams = nb_streams;
  1956. if (!nb_streams) {
  1957. fprintf(stderr, "No audio or video streams available\n");
  1958. exit(1);
  1959. }
  1960. if (str_title)
  1961. pstrcpy(oc->title, sizeof(oc->title), str_title);
  1962. if (str_author)
  1963. pstrcpy(oc->author, sizeof(oc->author), str_author);
  1964. if (str_copyright)
  1965. pstrcpy(oc->copyright, sizeof(oc->copyright), str_copyright);
  1966. if (str_comment)
  1967. pstrcpy(oc->comment, sizeof(oc->comment), str_comment);
  1968. }
  1969. output_files[nb_output_files++] = oc;
  1970. strcpy(oc->filename, filename);
  1971. /* check filename in case of an image number is expected */
  1972. if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
  1973. if (filename_number_test(oc->filename) < 0) {
  1974. print_error(oc->filename, AVERROR_NUMEXPECTED);
  1975. exit(1);
  1976. }
  1977. }
  1978. if (!(oc->oformat->flags & AVFMT_NOFILE)) {
  1979. /* test if it already exists to avoid loosing precious files */
  1980. if (!file_overwrite &&
  1981. (strchr(filename, ':') == NULL ||
  1982. strstart(filename, "file:", NULL))) {
  1983. if (url_exist(filename)) {
  1984. int c;
  1985. printf("File '%s' already exists. Overwrite ? [y/N] ", filename);
  1986. fflush(stdout);
  1987. c = getchar();
  1988. if (toupper(c) != 'Y') {
  1989. fprintf(stderr, "Not overwriting - exiting\n");
  1990. exit(1);
  1991. }
  1992. }
  1993. }
  1994. /* open the file */
  1995. if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
  1996. fprintf(stderr, "Could not open '%s'\n", filename);
  1997. exit(1);
  1998. }
  1999. }
  2000. /* reset some options */
  2001. file_oformat = NULL;
  2002. file_iformat = NULL;
  2003. audio_disable = 0;
  2004. video_disable = 0;
  2005. audio_codec_id = CODEC_ID_NONE;
  2006. video_codec_id = CODEC_ID_NONE;
  2007. audio_stream_copy = 0;
  2008. video_stream_copy = 0;
  2009. }
  2010. /* prepare dummy protocols for grab */
  2011. void prepare_grab(void)
  2012. {
  2013. int has_video, has_audio, i, j;
  2014. AVFormatContext *oc;
  2015. AVFormatContext *ic;
  2016. AVFormatParameters ap1, *ap = &ap1;
  2017. /* see if audio/video inputs are needed */
  2018. has_video = 0;
  2019. has_audio = 0;
  2020. memset(ap, 0, sizeof(*ap));
  2021. for(j=0;j<nb_output_files;j++) {
  2022. oc = output_files[j];
  2023. for(i=0;i<oc->nb_streams;i++) {
  2024. AVCodecContext *enc = &oc->streams[i]->codec;
  2025. switch(enc->codec_type) {
  2026. case CODEC_TYPE_AUDIO:
  2027. if (enc->sample_rate > ap->sample_rate)
  2028. ap->sample_rate = enc->sample_rate;
  2029. if (enc->channels > ap->channels)
  2030. ap->channels = enc->channels;
  2031. has_audio = 1;
  2032. break;
  2033. case CODEC_TYPE_VIDEO:
  2034. if (enc->width > ap->width)
  2035. ap->width = enc->width;
  2036. if (enc->height > ap->height)
  2037. ap->height = enc->height;
  2038. if (enc->frame_rate > ap->frame_rate)
  2039. ap->frame_rate = enc->frame_rate;
  2040. has_video = 1;
  2041. break;
  2042. default:
  2043. av_abort();
  2044. }
  2045. }
  2046. }
  2047. if (has_video == 0 && has_audio == 0) {
  2048. fprintf(stderr, "Output file must have at least one audio or video stream\n");
  2049. exit(1);
  2050. }
  2051. if (has_video) {
  2052. AVInputFormat *fmt1;
  2053. fmt1 = av_find_input_format("video_grab_device");
  2054. if (av_open_input_file(&ic, "", fmt1, 0, ap) < 0) {
  2055. fprintf(stderr, "Could not find video grab device\n");
  2056. exit(1);
  2057. }
  2058. /* by now video grab has one stream */
  2059. ic->streams[0]->r_frame_rate = ap->frame_rate;
  2060. input_files[nb_input_files] = ic;
  2061. dump_format(ic, nb_input_files, v4l_device, 0);
  2062. nb_input_files++;
  2063. }
  2064. if (has_audio) {
  2065. AVInputFormat *fmt1;
  2066. fmt1 = av_find_input_format("audio_device");
  2067. if (av_open_input_file(&ic, "", fmt1, 0, ap) < 0) {
  2068. fprintf(stderr, "Could not find audio grab device\n");
  2069. exit(1);
  2070. }
  2071. input_files[nb_input_files] = ic;
  2072. dump_format(ic, nb_input_files, audio_device, 0);
  2073. nb_input_files++;
  2074. }
  2075. }
  2076. /* open the necessary output devices for playing */
  2077. void prepare_play(void)
  2078. {
  2079. file_iformat = NULL;
  2080. file_oformat = guess_format("audio_device", NULL, NULL);
  2081. if (!file_oformat) {
  2082. fprintf(stderr, "Could not find audio device\n");
  2083. exit(1);
  2084. }
  2085. opt_output_file(audio_device);
  2086. }
  2087. /* same option as mencoder */
  2088. void opt_pass(const char *pass_str)
  2089. {
  2090. int pass;
  2091. pass = atoi(pass_str);
  2092. if (pass != 1 && pass != 2) {
  2093. fprintf(stderr, "pass number can be only 1 or 2\n");
  2094. exit(1);
  2095. }
  2096. do_pass = pass;
  2097. }
  2098. #ifndef CONFIG_WIN32
  2099. INT64 getutime(void)
  2100. {
  2101. struct rusage rusage;
  2102. getrusage(RUSAGE_SELF, &rusage);
  2103. return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
  2104. }
  2105. #else
  2106. INT64 getutime(void)
  2107. {
  2108. return av_gettime();
  2109. }
  2110. #endif
  2111. extern int ffm_nopts;
  2112. void opt_bitexact(void)
  2113. {
  2114. avcodec_set_bit_exact();
  2115. /* disable generate of real time pts in ffm (need to be supressed anyway) */
  2116. ffm_nopts = 1;
  2117. }
  2118. void show_formats(void)
  2119. {
  2120. AVInputFormat *ifmt;
  2121. AVOutputFormat *ofmt;
  2122. URLProtocol *up;
  2123. AVCodec *p;
  2124. const char **pp;
  2125. printf("File formats:\n");
  2126. printf(" Encoding:");
  2127. for(ofmt = first_oformat; ofmt != NULL; ofmt = ofmt->next) {
  2128. printf(" %s", ofmt->name);
  2129. }
  2130. printf("\n");
  2131. printf(" Decoding:");
  2132. for(ifmt = first_iformat; ifmt != NULL; ifmt = ifmt->next) {
  2133. printf(" %s", ifmt->name);
  2134. }
  2135. printf("\n");
  2136. printf("Codecs:\n");
  2137. printf(" Encoders:");
  2138. for(p = first_avcodec; p != NULL; p = p->next) {
  2139. if (p->encode)
  2140. printf(" %s", p->name);
  2141. }
  2142. printf("\n");
  2143. printf(" Decoders:");
  2144. for(p = first_avcodec; p != NULL; p = p->next) {
  2145. if (p->decode)
  2146. printf(" %s", p->name);
  2147. }
  2148. printf("\n");
  2149. printf("Supported file protocols:");
  2150. for(up = first_protocol; up != NULL; up = up->next)
  2151. printf(" %s:", up->name);
  2152. printf("\n");
  2153. printf("Frame size abbreviations: sqcif qcif cif 4cif\n");
  2154. printf("Motion estimation methods:");
  2155. pp = motion_str;
  2156. while (*pp) {
  2157. printf(" %s", *pp);
  2158. if ((pp - motion_str + 1) == ME_ZERO)
  2159. printf("(fastest)");
  2160. else if ((pp - motion_str + 1) == ME_FULL)
  2161. printf("(slowest)");
  2162. else if ((pp - motion_str + 1) == ME_EPZS)
  2163. printf("(default)");
  2164. pp++;
  2165. }
  2166. printf("\n");
  2167. exit(1);
  2168. }
  2169. void show_help(void)
  2170. {
  2171. const char *prog;
  2172. const OptionDef *po;
  2173. int i, expert;
  2174. prog = do_play ? "ffplay" : "ffmpeg";
  2175. printf("%s version " FFMPEG_VERSION ", Copyright (c) 2000, 2001, 2002 Fabrice Bellard\n",
  2176. prog);
  2177. if (!do_play) {
  2178. printf("usage: ffmpeg [[options] -i input_file]... {[options] outfile}...\n"
  2179. "Hyper fast MPEG1/MPEG4/H263/RV and AC3/MPEG audio encoder\n");
  2180. } else {
  2181. printf("usage: ffplay [options] input_file...\n"
  2182. "Simple audio player\n");
  2183. }
  2184. printf("\n"
  2185. "Main options are:\n");
  2186. for(i=0;i<2;i++) {
  2187. if (i == 1)
  2188. printf("\nAdvanced options are:\n");
  2189. for(po = options; po->name != NULL; po++) {
  2190. char buf[64];
  2191. expert = (po->flags & OPT_EXPERT) != 0;
  2192. if (expert == i) {
  2193. strcpy(buf, po->name);
  2194. if (po->flags & HAS_ARG) {
  2195. strcat(buf, " ");
  2196. strcat(buf, po->argname);
  2197. }
  2198. printf("-%-17s %s\n", buf, po->help);
  2199. }
  2200. }
  2201. }
  2202. exit(1);
  2203. }
  2204. const OptionDef options[] = {
  2205. { "L", 0, {(void*)show_licence}, "show license" },
  2206. { "h", 0, {(void*)show_help}, "show help" },
  2207. { "formats", 0, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
  2208. { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
  2209. { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" },
  2210. { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
  2211. { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file:stream" },
  2212. { "t", HAS_ARG, {(void*)opt_recording_time}, "set the recording time", "duration" },
  2213. { "title", HAS_ARG | OPT_STRING, {(void*)&str_title}, "set the title", "string" },
  2214. { "author", HAS_ARG | OPT_STRING, {(void*)&str_author}, "set the author", "string" },
  2215. { "copyright", HAS_ARG | OPT_STRING, {(void*)&str_copyright}, "set the copyright", "string" },
  2216. { "comment", HAS_ARG | OPT_STRING, {(void*)&str_comment}, "set the comment", "string" },
  2217. { "pass", HAS_ARG, {(void*)&opt_pass}, "select the pass number (1 or 2)", "n" },
  2218. { "passlogfile", HAS_ARG | OPT_STRING, {(void*)&pass_logfilename}, "select two pass log file name", "file" },
  2219. /* video options */
  2220. { "b", HAS_ARG, {(void*)opt_video_bitrate}, "set video bitrate (in kbit/s)", "bitrate" },
  2221. { "r", HAS_ARG, {(void*)opt_frame_rate}, "set frame rate (in Hz)", "rate" },
  2222. { "s", HAS_ARG, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" },
  2223. { "croptop", HAS_ARG, {(void*)opt_frame_crop_top}, "set top crop band size (in pixels)", "size" },
  2224. { "cropbottom", HAS_ARG, {(void*)opt_frame_crop_bottom}, "set bottom crop band size (in pixels)", "size" },
  2225. { "cropleft", HAS_ARG, {(void*)opt_frame_crop_left}, "set left crop band size (in pixels)", "size" },
  2226. { "cropright", HAS_ARG, {(void*)opt_frame_crop_right}, "set right crop band size (in pixels)", "size" },
  2227. { "g", HAS_ARG | OPT_EXPERT, {(void*)opt_gop_size}, "set the group of picture size", "gop_size" },
  2228. { "intra", OPT_BOOL | OPT_EXPERT, {(void*)&intra_only}, "use only intra frames"},
  2229. { "vn", OPT_BOOL, {(void*)&video_disable}, "disable video" },
  2230. { "qscale", HAS_ARG | OPT_EXPERT, {(void*)opt_qscale}, "use fixed video quantiser scale (VBR)", "q" },
  2231. { "qmin", HAS_ARG | OPT_EXPERT, {(void*)opt_qmin}, "min video quantiser scale (VBR)", "q" },
  2232. { "qmax", HAS_ARG | OPT_EXPERT, {(void*)opt_qmax}, "max video quantiser scale (VBR)", "q" },
  2233. { "qdiff", HAS_ARG | OPT_EXPERT, {(void*)opt_qdiff}, "max difference between the quantiser scale (VBR)", "q" },
  2234. { "qblur", HAS_ARG | OPT_EXPERT, {(void*)opt_qblur}, "video quantiser scale blur (VBR)", "blur" },
  2235. { "qcomp", HAS_ARG | OPT_EXPERT, {(void*)opt_qcomp}, "video quantiser scale compression (VBR)", "compression" },
  2236. { "rc_init_cplx", HAS_ARG | OPT_EXPERT, {(void*)opt_rc_initial_cplx}, "initial complexity for 1-pass encoding", "complexity" },
  2237. { "b_qfactor", HAS_ARG | OPT_EXPERT, {(void*)opt_b_qfactor}, "qp factor between p and b frames", "factor" },
  2238. { "i_qfactor", HAS_ARG | OPT_EXPERT, {(void*)opt_i_qfactor}, "qp factor between p and i frames", "factor" },
  2239. { "b_qoffset", HAS_ARG | OPT_EXPERT, {(void*)opt_b_qoffset}, "qp offset between p and b frames", "offset" },
  2240. { "i_qoffset", HAS_ARG | OPT_EXPERT, {(void*)opt_i_qoffset}, "qp offset between p and i frames", "offset" },
  2241. { "rc_eq", HAS_ARG | OPT_EXPERT, {(void*)opt_video_rc_eq}, "", "equation" },
  2242. { "rc_override", HAS_ARG | OPT_EXPERT, {(void*)opt_video_rc_override_string}, "Rate control override", "qualities for specific intervals" },
  2243. { "bt", HAS_ARG, {(void*)opt_video_bitrate_tolerance}, "set video bitrate tolerance (in kbit/s)", "tolerance" },
  2244. { "maxrate", HAS_ARG, {(void*)opt_video_bitrate_max}, "set max video bitrate tolerance (in kbit/s)", "bitrate" },
  2245. { "minrate", HAS_ARG, {(void*)opt_video_bitrate_min}, "set min video bitrate tolerance (in kbit/s)", "bitrate" },
  2246. { "bufsize", HAS_ARG, {(void*)opt_video_buffer_size}, "set ratecontrol buffere size (in kbit)", "size" },
  2247. { "vd", HAS_ARG | OPT_EXPERT, {(void*)opt_video_device}, "set video grab device", "device" },
  2248. { "vcodec", HAS_ARG | OPT_EXPERT, {(void*)opt_video_codec}, "force video codec ('copy' to copy stream)", "codec" },
  2249. { "me", HAS_ARG | OPT_EXPERT, {(void*)opt_motion_estimation}, "set motion estimation method",
  2250. "method" },
  2251. { "dct_algo", HAS_ARG | OPT_EXPERT, {(void*)opt_dct_algo}, "set dct algo", "algo" },
  2252. { "idct_algo", HAS_ARG | OPT_EXPERT, {(void*)opt_idct_algo}, "set idct algo", "algo" },
  2253. { "er", HAS_ARG | OPT_EXPERT, {(void*)opt_error_resilience}, "set error resilience", "" },
  2254. { "ec", HAS_ARG | OPT_EXPERT, {(void*)opt_error_resilience}, "set error concealment", "" },
  2255. { "bf", HAS_ARG | OPT_EXPERT, {(void*)opt_b_frames}, "use 'frames' B frames (only MPEG-4)", "frames" },
  2256. { "hq", OPT_BOOL | OPT_EXPERT, {(void*)&use_hq}, "activate high quality settings" },
  2257. { "4mv", OPT_BOOL | OPT_EXPERT, {(void*)&use_4mv}, "use four motion vector by macroblock (only MPEG-4)" },
  2258. { "part", OPT_BOOL | OPT_EXPERT, {(void*)&use_part}, "use data partitioning (only MPEG-4)" },
  2259. { "bug", HAS_ARG | OPT_EXPERT, {(void*)opt_workaround_bugs}, "workaround not auto detected encoder bugs", "param" },
  2260. { "ps", HAS_ARG | OPT_EXPERT, {(void*)opt_packet_size}, "packet size", "size in bits" },
  2261. { "sameq", OPT_BOOL, {(void*)&same_quality},
  2262. "use same video quality as source (implies VBR)" },
  2263. /* audio options */
  2264. { "ab", HAS_ARG, {(void*)opt_audio_bitrate}, "set audio bitrate (in kbit/s)", "bitrate", },
  2265. { "ar", HAS_ARG, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" },
  2266. { "ac", HAS_ARG, {(void*)opt_audio_channels}, "set number of audio channels", "channels" },
  2267. { "an", OPT_BOOL, {(void*)&audio_disable}, "disable audio" },
  2268. { "ad", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_device}, "set audio device", "device" },
  2269. { "acodec", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_codec}, "force audio codec ('copy' to copy stream)", "codec" },
  2270. { "deinterlace", OPT_BOOL | OPT_EXPERT, {(void*)&do_deinterlace},
  2271. "deinterlace pictures" },
  2272. { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark},
  2273. "add timings for benchmarking" },
  2274. { "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump},
  2275. "dump each input packet" },
  2276. { "psnr", OPT_BOOL | OPT_EXPERT, {(void*)&do_psnr}, "calculate PSNR of compressed frames" },
  2277. { "vstats", OPT_BOOL | OPT_EXPERT, {(void*)&do_vstats}, "dump video coding statistics to file" },
  2278. { "bitexact", OPT_EXPERT, {(void*)opt_bitexact}, "only use bit exact algorithms (for codec testing)" },
  2279. { "vhook", HAS_ARG | OPT_EXPERT, {(void*)add_frame_hooker}, "insert video processing module", "module name and parameters" },
  2280. { NULL, },
  2281. };
  2282. int main(int argc, char **argv)
  2283. {
  2284. int optindex, i;
  2285. const char *opt, *arg;
  2286. const OptionDef *po;
  2287. INT64 ti;
  2288. av_register_all();
  2289. /* detect if invoked as player */
  2290. i = strlen(argv[0]);
  2291. if (i >= 6 && !strcmp(argv[0] + i - 6, "ffplay"))
  2292. do_play = 1;
  2293. if (argc <= 1)
  2294. show_help();
  2295. /* parse options */
  2296. optindex = 1;
  2297. while (optindex < argc) {
  2298. opt = argv[optindex++];
  2299. if (opt[0] == '-' && opt[1] != '\0') {
  2300. po = options;
  2301. while (po->name != NULL) {
  2302. if (!strcmp(opt + 1, po->name))
  2303. break;
  2304. po++;
  2305. }
  2306. if (!po->name) {
  2307. fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
  2308. exit(1);
  2309. }
  2310. arg = NULL;
  2311. if (po->flags & HAS_ARG) {
  2312. arg = argv[optindex++];
  2313. if (!arg) {
  2314. fprintf(stderr, "%s: missing argument for option '%s'\n", argv[0], opt);
  2315. exit(1);
  2316. }
  2317. }
  2318. if (po->flags & OPT_STRING) {
  2319. char *str;
  2320. str = strdup(arg);
  2321. *po->u.str_arg = str;
  2322. } else if (po->flags & OPT_BOOL) {
  2323. *po->u.int_arg = 1;
  2324. } else {
  2325. po->u.func_arg(arg);
  2326. }
  2327. } else {
  2328. if (!do_play) {
  2329. opt_output_file(opt);
  2330. } else {
  2331. opt_input_file(opt);
  2332. }
  2333. }
  2334. }
  2335. if (!do_play) {
  2336. /* file converter / grab */
  2337. if (nb_output_files <= 0) {
  2338. fprintf(stderr, "Must supply at least one output file\n");
  2339. exit(1);
  2340. }
  2341. if (nb_input_files == 0) {
  2342. prepare_grab();
  2343. }
  2344. } else {
  2345. /* player */
  2346. if (nb_input_files <= 0) {
  2347. fprintf(stderr, "Must supply at least one input file\n");
  2348. exit(1);
  2349. }
  2350. prepare_play();
  2351. }
  2352. ti = getutime();
  2353. av_encode(output_files, nb_output_files, input_files, nb_input_files,
  2354. stream_maps, nb_stream_maps);
  2355. ti = getutime() - ti;
  2356. if (do_benchmark) {
  2357. printf("bench: utime=%0.3fs\n", ti / 1000000.0);
  2358. }
  2359. /* close files */
  2360. for(i=0;i<nb_output_files;i++) {
  2361. /* maybe av_close_output_file ??? */
  2362. AVFormatContext *s = output_files[i];
  2363. int j;
  2364. if (!(s->oformat->flags & AVFMT_NOFILE))
  2365. url_fclose(&s->pb);
  2366. for(j=0;j<s->nb_streams;j++)
  2367. av_free(s->streams[j]);
  2368. av_free(s);
  2369. }
  2370. for(i=0;i<nb_input_files;i++)
  2371. av_close_input_file(input_files[i]);
  2372. return 0;
  2373. }