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.

3142 lines
103KB

  1. /*
  2. * FFmpeg main
  3. * Copyright (c) 2000-2003 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. #include <signal.h>
  30. #endif
  31. #ifdef CONFIG_OS2
  32. #include <sys/types.h>
  33. #include <sys/select.h>
  34. #include <stdlib.h>
  35. #endif
  36. #include <time.h>
  37. #include "cmdutils.h"
  38. #if !defined(INFINITY) && defined(HUGE_VAL)
  39. #define INFINITY HUGE_VAL
  40. #endif
  41. /* select an input stream for an output stream */
  42. typedef struct AVStreamMap {
  43. int file_index;
  44. int stream_index;
  45. } AVStreamMap;
  46. extern const OptionDef options[];
  47. static void show_help(void);
  48. static void show_license(void);
  49. #define MAX_FILES 20
  50. static AVFormatContext *input_files[MAX_FILES];
  51. static int nb_input_files = 0;
  52. static AVFormatContext *output_files[MAX_FILES];
  53. static int nb_output_files = 0;
  54. static AVStreamMap stream_maps[MAX_FILES];
  55. static int nb_stream_maps;
  56. static AVInputFormat *file_iformat;
  57. static AVOutputFormat *file_oformat;
  58. static AVImageFormat *image_format;
  59. static int frame_width = 160;
  60. static int frame_height = 128;
  61. static float frame_aspect_ratio = 0;
  62. static enum PixelFormat frame_pix_fmt = PIX_FMT_YUV420P;
  63. static int frame_topBand = 0;
  64. static int frame_bottomBand = 0;
  65. static int frame_leftBand = 0;
  66. static int frame_rightBand = 0;
  67. static int frame_rate = 25;
  68. static int frame_rate_base = 1;
  69. static int video_bit_rate = 200*1000;
  70. static int video_bit_rate_tolerance = 4000*1000;
  71. static float video_qscale = 0;
  72. static int video_qmin = 2;
  73. static int video_qmax = 31;
  74. static int video_mb_qmin = 2;
  75. static int video_mb_qmax = 31;
  76. static int video_qdiff = 3;
  77. static float video_qblur = 0.5;
  78. static float video_qcomp = 0.5;
  79. static uint16_t *intra_matrix = NULL;
  80. static uint16_t *inter_matrix = NULL;
  81. #if 0 //experimental, (can be removed)
  82. static float video_rc_qsquish=1.0;
  83. static float video_rc_qmod_amp=0;
  84. static int video_rc_qmod_freq=0;
  85. #endif
  86. static char *video_rc_override_string=NULL;
  87. static char *video_rc_eq="tex^qComp";
  88. static int video_rc_buffer_size=0;
  89. static float video_rc_buffer_aggressivity=1.0;
  90. static int video_rc_max_rate=0;
  91. static int video_rc_min_rate=0;
  92. static float video_rc_initial_cplx=0;
  93. static float video_b_qfactor = 1.25;
  94. static float video_b_qoffset = 1.25;
  95. static float video_i_qfactor = -0.8;
  96. static float video_i_qoffset = 0.0;
  97. static int me_method = ME_EPZS;
  98. static int video_disable = 0;
  99. static int video_codec_id = CODEC_ID_NONE;
  100. static int same_quality = 0;
  101. static int b_frames = 0;
  102. static int mb_decision = FF_MB_DECISION_SIMPLE;
  103. static int mb_cmp = FF_CMP_SAD;
  104. static int sub_cmp = FF_CMP_SAD;
  105. static int cmp = FF_CMP_SAD;
  106. static int use_4mv = 0;
  107. static int use_obmc = 0;
  108. static int use_aic = 0;
  109. static int use_aiv = 0;
  110. static int use_umv = 0;
  111. static int do_deinterlace = 0;
  112. static int do_interlace = 0;
  113. static int workaround_bugs = FF_BUG_AUTODETECT;
  114. static int error_resilience = 2;
  115. static int error_concealment = 3;
  116. static int dct_algo = 0;
  117. static int idct_algo = 0;
  118. static int use_part = 0;
  119. static int packet_size = 0;
  120. static int error_rate = 0;
  121. static int strict = 0;
  122. static int debug = 0;
  123. extern int loop_input; /* currently a hack */
  124. static int gop_size = 12;
  125. static int intra_only = 0;
  126. static int audio_sample_rate = 44100;
  127. static int audio_bit_rate = 64000;
  128. static int audio_disable = 0;
  129. static int audio_channels = 1;
  130. static int audio_codec_id = CODEC_ID_NONE;
  131. static int64_t recording_time = 0;
  132. static int64_t start_time = 0;
  133. static int file_overwrite = 0;
  134. static char *str_title = NULL;
  135. static char *str_author = NULL;
  136. static char *str_copyright = NULL;
  137. static char *str_comment = NULL;
  138. static int do_benchmark = 0;
  139. static int do_hex_dump = 0;
  140. static int do_pkt_dump = 0;
  141. static int do_psnr = 0;
  142. static int do_vstats = 0;
  143. static int do_pass = 0;
  144. static int bitexact = 0;
  145. static char *pass_logfilename = NULL;
  146. static int audio_stream_copy = 0;
  147. static int video_stream_copy = 0;
  148. static int rate_emu = 0;
  149. static char *video_grab_format = "video4linux";
  150. static char *video_device = NULL;
  151. static int video_channel = 0;
  152. static char *video_standard = "ntsc";
  153. static char *audio_grab_format = "audio_device";
  154. static char *audio_device = NULL;
  155. static int using_stdin = 0;
  156. static int using_vhook = 0;
  157. static int verbose = 1;
  158. #define DEFAULT_PASS_LOGFILENAME "ffmpeg2pass"
  159. typedef struct AVOutputStream {
  160. int file_index; /* file index */
  161. int index; /* stream index in the output file */
  162. int source_index; /* AVInputStream index */
  163. AVStream *st; /* stream in the output file */
  164. int encoding_needed; /* true if encoding needed for this stream */
  165. int frame_number;
  166. /* input pts and corresponding output pts
  167. for A/V sync */
  168. double sync_ipts;
  169. double sync_ipts_offset;
  170. int64_t sync_opts;
  171. /* video only */
  172. int video_resample; /* video_resample and video_crop are mutually exclusive */
  173. AVPicture pict_tmp; /* temporary image for resampling */
  174. ImgReSampleContext *img_resample_ctx; /* for image resampling */
  175. int video_crop; /* video_resample and video_crop are mutually exclusive */
  176. int topBand; /* cropping area sizes */
  177. int leftBand;
  178. /* audio only */
  179. int audio_resample;
  180. ReSampleContext *resample; /* for audio resampling */
  181. FifoBuffer fifo; /* for compression: one audio fifo per codec */
  182. FILE *logfile;
  183. } AVOutputStream;
  184. typedef struct AVInputStream {
  185. int file_index;
  186. int index;
  187. AVStream *st;
  188. int discard; /* true if stream data should be discarded */
  189. int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
  190. int64_t sample_index; /* current sample */
  191. int64_t start; /* time when read started */
  192. unsigned long frame; /* current frame */
  193. int64_t next_pts; /* synthetic pts for cases where pkt.pts
  194. is not defined */
  195. int64_t pts; /* current pts */
  196. } AVInputStream;
  197. typedef struct AVInputFile {
  198. int eof_reached; /* true if eof reached */
  199. int ist_index; /* index of first stream in ist_table */
  200. int buffer_size; /* current total buffer size */
  201. int buffer_size_max; /* buffer size at which we consider we can stop
  202. buffering */
  203. int nb_streams; /* nb streams we are aware of */
  204. } AVInputFile;
  205. #ifndef CONFIG_WIN32
  206. /* init terminal so that we can grab keys */
  207. static struct termios oldtty;
  208. static void term_exit(void)
  209. {
  210. tcsetattr (0, TCSANOW, &oldtty);
  211. }
  212. static volatile sig_atomic_t received_sigterm = 0;
  213. static void
  214. sigterm_handler(int sig)
  215. {
  216. received_sigterm = sig;
  217. term_exit();
  218. }
  219. static void term_init(void)
  220. {
  221. struct termios tty;
  222. tcgetattr (0, &tty);
  223. oldtty = tty;
  224. tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
  225. |INLCR|IGNCR|ICRNL|IXON);
  226. tty.c_oflag |= OPOST;
  227. tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
  228. tty.c_cflag &= ~(CSIZE|PARENB);
  229. tty.c_cflag |= CS8;
  230. tty.c_cc[VMIN] = 1;
  231. tty.c_cc[VTIME] = 0;
  232. tcsetattr (0, TCSANOW, &tty);
  233. signal(SIGINT , sigterm_handler); /* Interrupt (ANSI). */
  234. signal(SIGQUIT, sigterm_handler); /* Quit (POSIX). */
  235. signal(SIGTERM, sigterm_handler); /* Termination (ANSI). */
  236. /*
  237. register a function to be called at normal program termination
  238. */
  239. atexit(term_exit);
  240. #ifdef CONFIG_BEOS_NETSERVER
  241. fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK);
  242. #endif
  243. }
  244. /* read a key without blocking */
  245. static int read_key(void)
  246. {
  247. int n = 1;
  248. unsigned char ch;
  249. #ifndef CONFIG_BEOS_NETSERVER
  250. struct timeval tv;
  251. fd_set rfds;
  252. FD_ZERO(&rfds);
  253. FD_SET(0, &rfds);
  254. tv.tv_sec = 0;
  255. tv.tv_usec = 0;
  256. n = select(1, &rfds, NULL, NULL, &tv);
  257. #endif
  258. if (n > 0) {
  259. n = read(0, &ch, 1);
  260. if (n == 1)
  261. return ch;
  262. return n;
  263. }
  264. return -1;
  265. }
  266. #else
  267. static volatile int received_sigterm = 0;
  268. /* no interactive support */
  269. static void term_exit(void)
  270. {
  271. }
  272. static void term_init(void)
  273. {
  274. }
  275. static int read_key(void)
  276. {
  277. return 0;
  278. }
  279. #endif
  280. static int read_ffserver_streams(AVFormatContext *s, const char *filename)
  281. {
  282. int i, err;
  283. AVFormatContext *ic;
  284. err = av_open_input_file(&ic, filename, NULL, FFM_PACKET_SIZE, NULL);
  285. if (err < 0)
  286. return err;
  287. /* copy stream format */
  288. s->nb_streams = ic->nb_streams;
  289. for(i=0;i<ic->nb_streams;i++) {
  290. AVStream *st;
  291. st = av_mallocz(sizeof(AVStream));
  292. memcpy(st, ic->streams[i], sizeof(AVStream));
  293. s->streams[i] = st;
  294. }
  295. av_close_input_file(ic);
  296. return 0;
  297. }
  298. #define MAX_AUDIO_PACKET_SIZE (128 * 1024)
  299. static void do_audio_out(AVFormatContext *s,
  300. AVOutputStream *ost,
  301. AVInputStream *ist,
  302. unsigned char *buf, int size)
  303. {
  304. uint8_t *buftmp;
  305. static uint8_t *audio_buf = NULL;
  306. static uint8_t *audio_out = NULL;
  307. const int audio_out_size= 4*MAX_AUDIO_PACKET_SIZE;
  308. int size_out, frame_bytes, ret;
  309. AVCodecContext *enc;
  310. /* SC: dynamic allocation of buffers */
  311. if (!audio_buf)
  312. audio_buf = av_malloc(2*MAX_AUDIO_PACKET_SIZE);
  313. if (!audio_out)
  314. audio_out = av_malloc(audio_out_size);
  315. if (!audio_buf || !audio_out)
  316. return; /* Should signal an error ! */
  317. enc = &ost->st->codec;
  318. if (ost->audio_resample) {
  319. buftmp = audio_buf;
  320. size_out = audio_resample(ost->resample,
  321. (short *)buftmp, (short *)buf,
  322. size / (ist->st->codec.channels * 2));
  323. size_out = size_out * enc->channels * 2;
  324. } else {
  325. buftmp = buf;
  326. size_out = size;
  327. }
  328. /* now encode as many frames as possible */
  329. if (enc->frame_size > 1) {
  330. /* output resampled raw samples */
  331. fifo_write(&ost->fifo, buftmp, size_out,
  332. &ost->fifo.wptr);
  333. frame_bytes = enc->frame_size * 2 * enc->channels;
  334. while (fifo_read(&ost->fifo, audio_buf, frame_bytes,
  335. &ost->fifo.rptr) == 0) {
  336. ret = avcodec_encode_audio(enc, audio_out, audio_out_size,
  337. (short *)audio_buf);
  338. av_write_frame(s, ost->index, audio_out, ret);
  339. }
  340. } else {
  341. /* output a pcm frame */
  342. /* XXX: change encoding codec API to avoid this ? */
  343. switch(enc->codec->id) {
  344. case CODEC_ID_PCM_S16LE:
  345. case CODEC_ID_PCM_S16BE:
  346. case CODEC_ID_PCM_U16LE:
  347. case CODEC_ID_PCM_U16BE:
  348. break;
  349. default:
  350. size_out = size_out >> 1;
  351. break;
  352. }
  353. ret = avcodec_encode_audio(enc, audio_out, size_out,
  354. (short *)buftmp);
  355. av_write_frame(s, ost->index, audio_out, ret);
  356. }
  357. }
  358. static void pre_process_video_frame(AVInputStream *ist, AVPicture *picture, void **bufp)
  359. {
  360. AVCodecContext *dec;
  361. AVPicture *picture2;
  362. AVPicture picture_tmp;
  363. uint8_t *buf = 0;
  364. dec = &ist->st->codec;
  365. /* deinterlace : must be done before any resize */
  366. if (do_deinterlace || using_vhook) {
  367. int size;
  368. /* create temporary picture */
  369. size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
  370. buf = av_malloc(size);
  371. if (!buf)
  372. return;
  373. picture2 = &picture_tmp;
  374. avpicture_fill(picture2, buf, dec->pix_fmt, dec->width, dec->height);
  375. if (do_deinterlace){
  376. if(avpicture_deinterlace(picture2, picture,
  377. dec->pix_fmt, dec->width, dec->height) < 0) {
  378. /* if error, do not deinterlace */
  379. av_free(buf);
  380. buf = NULL;
  381. picture2 = picture;
  382. }
  383. } else {
  384. if (img_convert(picture2, dec->pix_fmt, picture,
  385. dec->pix_fmt, dec->width, dec->height) < 0) {
  386. /* if error, do not copy */
  387. av_free(buf);
  388. buf = NULL;
  389. picture2 = picture;
  390. }
  391. }
  392. } else {
  393. picture2 = picture;
  394. }
  395. frame_hook_process(picture2, dec->pix_fmt, dec->width, dec->height);
  396. if (picture != picture2)
  397. *picture = *picture2;
  398. *bufp = buf;
  399. }
  400. /* we begin to correct av delay at this threshold */
  401. #define AV_DELAY_MAX 0.100
  402. static void do_video_out(AVFormatContext *s,
  403. AVOutputStream *ost,
  404. AVInputStream *ist,
  405. AVFrame *in_picture,
  406. int *frame_size, AVOutputStream *audio_sync)
  407. {
  408. int nb_frames, i, ret;
  409. AVPicture *final_picture, *formatted_picture;
  410. AVPicture picture_format_temp, picture_crop_temp;
  411. static uint8_t *video_buffer;
  412. uint8_t *buf = NULL, *buf1 = NULL;
  413. AVCodecContext *enc, *dec;
  414. enum PixelFormat target_pixfmt;
  415. #define VIDEO_BUFFER_SIZE (1024*1024)
  416. enc = &ost->st->codec;
  417. dec = &ist->st->codec;
  418. /* by default, we output a single frame */
  419. nb_frames = 1;
  420. *frame_size = 0;
  421. /* NOTE: the A/V sync is always done by considering the audio is
  422. the master clock. It is suffisant for transcoding or playing,
  423. but not for the general case */
  424. if (audio_sync) {
  425. /* compute the A-V delay and duplicate/remove frames if needed */
  426. double adelta, vdelta, av_delay;
  427. adelta = audio_sync->sync_ipts - ((double)audio_sync->sync_opts *
  428. s->pts_num / s->pts_den);
  429. vdelta = ost->sync_ipts - ((double)ost->sync_opts *
  430. s->pts_num / s->pts_den);
  431. av_delay = adelta - vdelta;
  432. // printf("delay=%f\n", av_delay);
  433. if (av_delay < -AV_DELAY_MAX)
  434. nb_frames = 2;
  435. else if (av_delay > AV_DELAY_MAX)
  436. nb_frames = 0;
  437. } else {
  438. double vdelta;
  439. vdelta = (double)(ost->st->pts.val) * s->pts_num / s->pts_den - (ost->sync_ipts - ost->sync_ipts_offset);
  440. if (vdelta < 100 && vdelta > -100 && ost->sync_ipts_offset) {
  441. if (vdelta < -AV_DELAY_MAX)
  442. nb_frames = 2;
  443. else if (vdelta > AV_DELAY_MAX)
  444. nb_frames = 0;
  445. } else {
  446. ost->sync_ipts_offset -= vdelta;
  447. if (!ost->sync_ipts_offset)
  448. ost->sync_ipts_offset = 0.000001; /* one microsecond */
  449. }
  450. }
  451. #if defined(AVSYNC_DEBUG)
  452. {
  453. static char *action[] = { "drop frame", "copy frame", "dup frame" };
  454. if (audio_sync)
  455. fprintf(stderr, "Input APTS %12.6f, output APTS %12.6f, ",
  456. (double) audio_sync->sync_ipts,
  457. (double) audio_sync->st->pts.val * s->pts_num / s->pts_den);
  458. fprintf(stderr, "Input VPTS %12.6f, output VPTS %12.6f: %s\n",
  459. (double) ost->sync_ipts,
  460. (double) ost->st->pts.val * s->pts_num / s->pts_den,
  461. action[nb_frames]);
  462. }
  463. #endif
  464. if (nb_frames <= 0)
  465. return;
  466. if (!video_buffer)
  467. video_buffer = av_malloc(VIDEO_BUFFER_SIZE);
  468. if (!video_buffer)
  469. return;
  470. /* convert pixel format if needed */
  471. target_pixfmt = ost->video_resample ? PIX_FMT_YUV420P : enc->pix_fmt;
  472. if (dec->pix_fmt != target_pixfmt) {
  473. int size;
  474. /* create temporary picture */
  475. size = avpicture_get_size(target_pixfmt, dec->width, dec->height);
  476. buf = av_malloc(size);
  477. if (!buf)
  478. return;
  479. formatted_picture = &picture_format_temp;
  480. avpicture_fill(formatted_picture, buf, target_pixfmt, dec->width, dec->height);
  481. if (img_convert(formatted_picture, target_pixfmt,
  482. (AVPicture *)in_picture, dec->pix_fmt,
  483. dec->width, dec->height) < 0) {
  484. fprintf(stderr, "pixel format conversion not handled\n");
  485. goto the_end;
  486. }
  487. } else {
  488. formatted_picture = (AVPicture *)in_picture;
  489. }
  490. /* XXX: resampling could be done before raw format convertion in
  491. some cases to go faster */
  492. /* XXX: only works for YUV420P */
  493. if (ost->video_resample) {
  494. final_picture = &ost->pict_tmp;
  495. img_resample(ost->img_resample_ctx, final_picture, formatted_picture);
  496. if (enc->pix_fmt != PIX_FMT_YUV420P) {
  497. int size;
  498. av_free(buf);
  499. /* create temporary picture */
  500. size = avpicture_get_size(enc->pix_fmt, enc->width, enc->height);
  501. buf = av_malloc(size);
  502. if (!buf)
  503. return;
  504. final_picture = &picture_format_temp;
  505. avpicture_fill(final_picture, buf, enc->pix_fmt, enc->width, enc->height);
  506. if (img_convert(final_picture, enc->pix_fmt,
  507. &ost->pict_tmp, PIX_FMT_YUV420P,
  508. enc->width, enc->height) < 0) {
  509. fprintf(stderr, "pixel format conversion not handled\n");
  510. goto the_end;
  511. }
  512. }
  513. } else if (ost->video_crop) {
  514. picture_crop_temp.data[0] = formatted_picture->data[0] +
  515. (ost->topBand * formatted_picture->linesize[0]) + ost->leftBand;
  516. picture_crop_temp.data[1] = formatted_picture->data[1] +
  517. ((ost->topBand >> 1) * formatted_picture->linesize[1]) +
  518. (ost->leftBand >> 1);
  519. picture_crop_temp.data[2] = formatted_picture->data[2] +
  520. ((ost->topBand >> 1) * formatted_picture->linesize[2]) +
  521. (ost->leftBand >> 1);
  522. picture_crop_temp.linesize[0] = formatted_picture->linesize[0];
  523. picture_crop_temp.linesize[1] = formatted_picture->linesize[1];
  524. picture_crop_temp.linesize[2] = formatted_picture->linesize[2];
  525. final_picture = &picture_crop_temp;
  526. } else {
  527. final_picture = formatted_picture;
  528. }
  529. /* duplicates frame if needed */
  530. /* XXX: pb because no interleaving */
  531. for(i=0;i<nb_frames;i++) {
  532. if (s->oformat->flags & AVFMT_RAWPICTURE) {
  533. /* raw pictures are written as AVPicture structure to
  534. avoid any copies. We support temorarily the older
  535. method. */
  536. AVFrame* old_frame = enc->coded_frame;
  537. enc->coded_frame = dec->coded_frame;
  538. av_write_frame(s, ost->index,
  539. (uint8_t *)final_picture, sizeof(AVPicture));
  540. enc->coded_frame = old_frame;
  541. } else {
  542. AVFrame big_picture;
  543. memset(&big_picture, 0, sizeof(AVFrame));
  544. *(AVPicture*)&big_picture= *final_picture;
  545. /* better than nothing: use input picture interlaced
  546. settings */
  547. big_picture.interlaced_frame = in_picture->interlaced_frame;
  548. big_picture.top_field_first = in_picture->top_field_first;
  549. /* handles sameq here. This is not correct because it may
  550. not be a global option */
  551. if (same_quality) {
  552. big_picture.quality = ist->st->quality;
  553. }else
  554. big_picture.quality = ost->st->quality;
  555. ret = avcodec_encode_video(enc,
  556. video_buffer, VIDEO_BUFFER_SIZE,
  557. &big_picture);
  558. //enc->frame_number = enc->real_pict_num;
  559. av_write_frame(s, ost->index, video_buffer, ret);
  560. *frame_size = ret;
  561. //fprintf(stderr,"\nFrame: %3d %3d size: %5d type: %d",
  562. // enc->frame_number-1, enc->real_pict_num, ret,
  563. // enc->pict_type);
  564. /* if two pass, output log */
  565. if (ost->logfile && enc->stats_out) {
  566. fprintf(ost->logfile, "%s", enc->stats_out);
  567. }
  568. }
  569. ost->frame_number++;
  570. }
  571. the_end:
  572. av_free(buf);
  573. av_free(buf1);
  574. }
  575. static double psnr(double d){
  576. if(d==0) return INFINITY;
  577. return -10.0*log(d)/log(10.0);
  578. }
  579. static void do_video_stats(AVFormatContext *os, AVOutputStream *ost,
  580. int frame_size)
  581. {
  582. static FILE *fvstats=NULL;
  583. static int64_t total_size = 0;
  584. char filename[40];
  585. time_t today2;
  586. struct tm *today;
  587. AVCodecContext *enc;
  588. int frame_number;
  589. int64_t ti;
  590. double ti1, bitrate, avg_bitrate;
  591. if (!fvstats) {
  592. today2 = time(NULL);
  593. today = localtime(&today2);
  594. sprintf(filename, "vstats_%02d%02d%02d.log", today->tm_hour,
  595. today->tm_min,
  596. today->tm_sec);
  597. fvstats = fopen(filename,"w");
  598. if (!fvstats) {
  599. perror("fopen");
  600. exit(1);
  601. }
  602. }
  603. ti = MAXINT64;
  604. enc = &ost->st->codec;
  605. total_size += frame_size;
  606. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  607. frame_number = ost->frame_number;
  608. fprintf(fvstats, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality/(float)FF_QP2LAMBDA);
  609. if (enc->flags&CODEC_FLAG_PSNR)
  610. fprintf(fvstats, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0]/(enc->width*enc->height*255.0*255.0)));
  611. fprintf(fvstats,"f_size= %6d ", frame_size);
  612. /* compute pts value */
  613. ti1 = (double)ost->st->pts.val * os->pts_num / os->pts_den;
  614. if (ti1 < 0.01)
  615. ti1 = 0.01;
  616. bitrate = (double)(frame_size * 8) * enc->frame_rate / enc->frame_rate_base / 1000.0;
  617. avg_bitrate = (double)(total_size * 8) / ti1 / 1000.0;
  618. fprintf(fvstats, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
  619. (double)total_size / 1024, ti1, bitrate, avg_bitrate);
  620. fprintf(fvstats,"type= %c\n", av_get_pict_type_char(enc->coded_frame->pict_type));
  621. }
  622. }
  623. static void print_report(AVFormatContext **output_files,
  624. AVOutputStream **ost_table, int nb_ostreams,
  625. int is_last_report)
  626. {
  627. char buf[1024];
  628. AVOutputStream *ost;
  629. AVFormatContext *oc, *os;
  630. int64_t total_size;
  631. AVCodecContext *enc;
  632. int frame_number, vid, i;
  633. double bitrate, ti1, pts;
  634. static int64_t last_time = -1;
  635. if (!is_last_report) {
  636. int64_t cur_time;
  637. /* display the report every 0.5 seconds */
  638. cur_time = av_gettime();
  639. if (last_time == -1) {
  640. last_time = cur_time;
  641. return;
  642. }
  643. if ((cur_time - last_time) < 500000)
  644. return;
  645. last_time = cur_time;
  646. }
  647. oc = output_files[0];
  648. total_size = url_ftell(&oc->pb);
  649. buf[0] = '\0';
  650. ti1 = 1e10;
  651. vid = 0;
  652. for(i=0;i<nb_ostreams;i++) {
  653. ost = ost_table[i];
  654. os = output_files[ost->file_index];
  655. enc = &ost->st->codec;
  656. if (vid && enc->codec_type == CODEC_TYPE_VIDEO) {
  657. sprintf(buf + strlen(buf), "q=%2.1f ",
  658. enc->coded_frame->quality/(float)FF_QP2LAMBDA);
  659. }
  660. if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
  661. frame_number = ost->frame_number;
  662. sprintf(buf + strlen(buf), "frame=%5d q=%2.1f ",
  663. frame_number, enc->coded_frame ? enc->coded_frame->quality/(float)FF_QP2LAMBDA : 0);
  664. if(is_last_report)
  665. sprintf(buf + strlen(buf), "L");
  666. if (enc->flags&CODEC_FLAG_PSNR){
  667. int j;
  668. double error, error_sum=0;
  669. double scale, scale_sum=0;
  670. char type[3]= {'Y','U','V'};
  671. sprintf(buf + strlen(buf), "PSNR=");
  672. for(j=0; j<3; j++){
  673. if(is_last_report){
  674. error= enc->error[j];
  675. scale= enc->width*enc->height*255.0*255.0*frame_number;
  676. }else{
  677. error= enc->coded_frame->error[j];
  678. scale= enc->width*enc->height*255.0*255.0;
  679. }
  680. if(j) scale/=4;
  681. error_sum += error;
  682. scale_sum += scale;
  683. sprintf(buf + strlen(buf), "%c:%2.2f ", type[j], psnr(error/scale));
  684. }
  685. sprintf(buf + strlen(buf), "*:%2.2f ", psnr(error_sum/scale_sum));
  686. }
  687. vid = 1;
  688. }
  689. /* compute min output value */
  690. pts = (double)ost->st->pts.val * os->pts_num / os->pts_den;
  691. if ((pts < ti1) && (pts > 0))
  692. ti1 = pts;
  693. }
  694. if (ti1 < 0.01)
  695. ti1 = 0.01;
  696. if (verbose || is_last_report) {
  697. bitrate = (double)(total_size * 8) / ti1 / 1000.0;
  698. sprintf(buf + strlen(buf),
  699. "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
  700. (double)total_size / 1024, ti1, bitrate);
  701. fprintf(stderr, "%s \r", buf);
  702. fflush(stderr);
  703. }
  704. if (is_last_report)
  705. fprintf(stderr, "\n");
  706. }
  707. /* pkt = NULL means EOF (needed to flush decoder buffers) */
  708. static int output_packet(AVInputStream *ist, int ist_index,
  709. AVOutputStream **ost_table, int nb_ostreams,
  710. AVPacket *pkt)
  711. {
  712. AVFormatContext *os;
  713. AVOutputStream *ost;
  714. uint8_t *ptr;
  715. int len, ret, i;
  716. uint8_t *data_buf;
  717. int data_size, got_picture;
  718. AVFrame picture;
  719. short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
  720. void *buffer_to_free;
  721. if (pkt && pkt->pts != AV_NOPTS_VALUE) {
  722. ist->pts = pkt->pts;
  723. } else {
  724. ist->pts = ist->next_pts;
  725. }
  726. if (pkt == NULL) {
  727. /* EOF handling */
  728. ptr = NULL;
  729. len = 0;
  730. goto handle_eof;
  731. }
  732. len = pkt->size;
  733. ptr = pkt->data;
  734. while (len > 0) {
  735. handle_eof:
  736. /* decode the packet if needed */
  737. data_buf = NULL; /* fail safe */
  738. data_size = 0;
  739. if (ist->decoding_needed) {
  740. switch(ist->st->codec.codec_type) {
  741. case CODEC_TYPE_AUDIO:
  742. /* XXX: could avoid copy if PCM 16 bits with same
  743. endianness as CPU */
  744. ret = avcodec_decode_audio(&ist->st->codec, samples, &data_size,
  745. ptr, len);
  746. if (ret < 0)
  747. goto fail_decode;
  748. ptr += ret;
  749. len -= ret;
  750. /* Some bug in mpeg audio decoder gives */
  751. /* data_size < 0, it seems they are overflows */
  752. if (data_size <= 0) {
  753. /* no audio frame */
  754. continue;
  755. }
  756. data_buf = (uint8_t *)samples;
  757. ist->next_pts += ((int64_t)AV_TIME_BASE * data_size) /
  758. (2 * ist->st->codec.channels);
  759. break;
  760. case CODEC_TYPE_VIDEO:
  761. data_size = (ist->st->codec.width * ist->st->codec.height * 3) / 2;
  762. /* XXX: allocate picture correctly */
  763. memset(&picture, 0, sizeof(picture));
  764. ret = avcodec_decode_video(&ist->st->codec,
  765. &picture, &got_picture, ptr, len);
  766. ist->st->quality= picture.quality;
  767. if (ret < 0)
  768. goto fail_decode;
  769. if (!got_picture) {
  770. /* no picture yet */
  771. goto discard_packet;
  772. }
  773. if (ist->st->codec.frame_rate_base != 0) {
  774. ist->next_pts += ((int64_t)AV_TIME_BASE *
  775. ist->st->codec.frame_rate_base) /
  776. ist->st->codec.frame_rate;
  777. }
  778. len = 0;
  779. break;
  780. default:
  781. goto fail_decode;
  782. }
  783. } else {
  784. data_buf = ptr;
  785. data_size = len;
  786. ret = len;
  787. len = 0;
  788. }
  789. buffer_to_free = NULL;
  790. if (ist->st->codec.codec_type == CODEC_TYPE_VIDEO) {
  791. pre_process_video_frame(ist, (AVPicture *)&picture,
  792. &buffer_to_free);
  793. }
  794. /* frame rate emulation */
  795. if (ist->st->codec.rate_emu) {
  796. int64_t pts = av_rescale((int64_t) ist->frame * ist->st->codec.frame_rate_base, 1000000, ist->st->codec.frame_rate);
  797. int64_t now = av_gettime() - ist->start;
  798. if (pts > now)
  799. usleep(pts - now);
  800. ist->frame++;
  801. }
  802. #if 0
  803. /* mpeg PTS deordering : if it is a P or I frame, the PTS
  804. is the one of the next displayed one */
  805. /* XXX: add mpeg4 too ? */
  806. if (ist->st->codec.codec_id == CODEC_ID_MPEG1VIDEO) {
  807. if (ist->st->codec.pict_type != B_TYPE) {
  808. int64_t tmp;
  809. tmp = ist->last_ip_pts;
  810. ist->last_ip_pts = ist->frac_pts.val;
  811. ist->frac_pts.val = tmp;
  812. }
  813. }
  814. #endif
  815. /* if output time reached then transcode raw format,
  816. encode packets and output them */
  817. if (start_time == 0 || ist->pts >= start_time)
  818. for(i=0;i<nb_ostreams;i++) {
  819. int frame_size;
  820. ost = ost_table[i];
  821. if (ost->source_index == ist_index) {
  822. os = output_files[ost->file_index];
  823. #if 0
  824. printf("%d: got pts=%0.3f %0.3f\n", i,
  825. (double)pkt->pts / AV_TIME_BASE,
  826. ((double)ist->pts / AV_TIME_BASE) -
  827. ((double)ost->st->pts.val * os->pts_num / os->pts_den));
  828. #endif
  829. /* set the input output pts pairs */
  830. ost->sync_ipts = (double)ist->pts / AV_TIME_BASE;
  831. /* XXX: take into account the various fifos,
  832. in particular for audio */
  833. ost->sync_opts = ost->st->pts.val;
  834. //printf("ipts=%lld sync_ipts=%f sync_opts=%lld pts.val=%lld pkt->pts=%lld\n", ist->pts, ost->sync_ipts, ost->sync_opts, ost->st->pts.val, pkt->pts);
  835. if (ost->encoding_needed) {
  836. switch(ost->st->codec.codec_type) {
  837. case CODEC_TYPE_AUDIO:
  838. do_audio_out(os, ost, ist, data_buf, data_size);
  839. break;
  840. case CODEC_TYPE_VIDEO:
  841. /* find an audio stream for synchro */
  842. {
  843. int i;
  844. AVOutputStream *audio_sync, *ost1;
  845. audio_sync = NULL;
  846. for(i=0;i<nb_ostreams;i++) {
  847. ost1 = ost_table[i];
  848. if (ost1->file_index == ost->file_index &&
  849. ost1->st->codec.codec_type == CODEC_TYPE_AUDIO) {
  850. audio_sync = ost1;
  851. break;
  852. }
  853. }
  854. do_video_out(os, ost, ist, &picture, &frame_size, audio_sync);
  855. if (do_vstats && frame_size)
  856. do_video_stats(os, ost, frame_size);
  857. }
  858. break;
  859. default:
  860. av_abort();
  861. }
  862. } else {
  863. AVFrame avframe;
  864. /* no reencoding needed : output the packet directly */
  865. /* force the input stream PTS */
  866. memset(&avframe, 0, sizeof(AVFrame));
  867. ost->st->codec.coded_frame= &avframe;
  868. avframe.key_frame = pkt->flags & PKT_FLAG_KEY;
  869. av_write_frame(os, ost->index, data_buf, data_size);
  870. ost->st->codec.frame_number++;
  871. ost->frame_number++;
  872. }
  873. }
  874. }
  875. av_free(buffer_to_free);
  876. }
  877. discard_packet:
  878. return 0;
  879. fail_decode:
  880. return -1;
  881. }
  882. /*
  883. * The following code is the main loop of the file converter
  884. */
  885. static int av_encode(AVFormatContext **output_files,
  886. int nb_output_files,
  887. AVFormatContext **input_files,
  888. int nb_input_files,
  889. AVStreamMap *stream_maps, int nb_stream_maps)
  890. {
  891. int ret, i, j, k, n, nb_istreams = 0, nb_ostreams = 0;
  892. AVFormatContext *is, *os;
  893. AVCodecContext *codec, *icodec;
  894. AVOutputStream *ost, **ost_table = NULL;
  895. AVInputStream *ist, **ist_table = NULL;
  896. AVInputFile *file_table;
  897. AVFormatContext *stream_no_data;
  898. int key;
  899. file_table= (AVInputFile*) av_mallocz(nb_input_files * sizeof(AVInputFile));
  900. if (!file_table)
  901. goto fail;
  902. /* input stream init */
  903. j = 0;
  904. for(i=0;i<nb_input_files;i++) {
  905. is = input_files[i];
  906. file_table[i].ist_index = j;
  907. file_table[i].nb_streams = is->nb_streams;
  908. j += is->nb_streams;
  909. }
  910. nb_istreams = j;
  911. ist_table = av_mallocz(nb_istreams * sizeof(AVInputStream *));
  912. if (!ist_table)
  913. goto fail;
  914. for(i=0;i<nb_istreams;i++) {
  915. ist = av_mallocz(sizeof(AVInputStream));
  916. if (!ist)
  917. goto fail;
  918. ist_table[i] = ist;
  919. }
  920. j = 0;
  921. for(i=0;i<nb_input_files;i++) {
  922. is = input_files[i];
  923. for(k=0;k<is->nb_streams;k++) {
  924. ist = ist_table[j++];
  925. ist->st = is->streams[k];
  926. ist->file_index = i;
  927. ist->index = k;
  928. ist->discard = 1; /* the stream is discarded by default
  929. (changed later) */
  930. if (ist->st->codec.rate_emu) {
  931. ist->start = av_gettime();
  932. ist->frame = 0;
  933. }
  934. }
  935. }
  936. /* output stream init */
  937. nb_ostreams = 0;
  938. for(i=0;i<nb_output_files;i++) {
  939. os = output_files[i];
  940. nb_ostreams += os->nb_streams;
  941. }
  942. if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) {
  943. fprintf(stderr, "Number of stream maps must match number of output streams\n");
  944. exit(1);
  945. }
  946. /* Sanity check the mapping args -- do the input files & streams exist? */
  947. for(i=0;i<nb_stream_maps;i++) {
  948. int fi = stream_maps[i].file_index;
  949. int si = stream_maps[i].stream_index;
  950. if (fi < 0 || fi > nb_input_files - 1 ||
  951. si < 0 || si > file_table[fi].nb_streams - 1) {
  952. fprintf(stderr,"Could not find input stream #%d.%d\n", fi, si);
  953. exit(1);
  954. }
  955. }
  956. ost_table = av_mallocz(sizeof(AVOutputStream *) * nb_ostreams);
  957. if (!ost_table)
  958. goto fail;
  959. for(i=0;i<nb_ostreams;i++) {
  960. ost = av_mallocz(sizeof(AVOutputStream));
  961. if (!ost)
  962. goto fail;
  963. ost_table[i] = ost;
  964. }
  965. n = 0;
  966. for(k=0;k<nb_output_files;k++) {
  967. os = output_files[k];
  968. for(i=0;i<os->nb_streams;i++) {
  969. int found;
  970. ost = ost_table[n++];
  971. ost->file_index = k;
  972. ost->index = i;
  973. ost->st = os->streams[i];
  974. if (nb_stream_maps > 0) {
  975. ost->source_index = file_table[stream_maps[n-1].file_index].ist_index +
  976. stream_maps[n-1].stream_index;
  977. /* Sanity check that the stream types match */
  978. if (ist_table[ost->source_index]->st->codec.codec_type != ost->st->codec.codec_type) {
  979. fprintf(stderr, "Codec type mismatch for mapping #%d.%d -> #%d.%d\n",
  980. stream_maps[n-1].file_index, stream_maps[n-1].stream_index,
  981. ost->file_index, ost->index);
  982. exit(1);
  983. }
  984. } else {
  985. /* get corresponding input stream index : we select the first one with the right type */
  986. found = 0;
  987. for(j=0;j<nb_istreams;j++) {
  988. ist = ist_table[j];
  989. if (ist->discard &&
  990. ist->st->codec.codec_type == ost->st->codec.codec_type) {
  991. ost->source_index = j;
  992. found = 1;
  993. }
  994. }
  995. if (!found) {
  996. /* try again and reuse existing stream */
  997. for(j=0;j<nb_istreams;j++) {
  998. ist = ist_table[j];
  999. if (ist->st->codec.codec_type == ost->st->codec.codec_type) {
  1000. ost->source_index = j;
  1001. found = 1;
  1002. }
  1003. }
  1004. if (!found) {
  1005. fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n",
  1006. ost->file_index, ost->index);
  1007. exit(1);
  1008. }
  1009. }
  1010. }
  1011. ist = ist_table[ost->source_index];
  1012. ist->discard = 0;
  1013. }
  1014. }
  1015. /* for each output stream, we compute the right encoding parameters */
  1016. for(i=0;i<nb_ostreams;i++) {
  1017. ost = ost_table[i];
  1018. ist = ist_table[ost->source_index];
  1019. codec = &ost->st->codec;
  1020. icodec = &ist->st->codec;
  1021. if (ost->st->stream_copy) {
  1022. /* if stream_copy is selected, no need to decode or encode */
  1023. codec->codec_id = icodec->codec_id;
  1024. codec->codec_type = icodec->codec_type;
  1025. codec->codec_tag = icodec->codec_tag;
  1026. codec->bit_rate = icodec->bit_rate;
  1027. switch(codec->codec_type) {
  1028. case CODEC_TYPE_AUDIO:
  1029. codec->sample_rate = icodec->sample_rate;
  1030. codec->channels = icodec->channels;
  1031. break;
  1032. case CODEC_TYPE_VIDEO:
  1033. codec->frame_rate = icodec->frame_rate;
  1034. codec->frame_rate_base = icodec->frame_rate_base;
  1035. codec->width = icodec->width;
  1036. codec->height = icodec->height;
  1037. break;
  1038. default:
  1039. av_abort();
  1040. }
  1041. } else {
  1042. switch(codec->codec_type) {
  1043. case CODEC_TYPE_AUDIO:
  1044. if (fifo_init(&ost->fifo, 2 * MAX_AUDIO_PACKET_SIZE))
  1045. goto fail;
  1046. if (codec->channels == icodec->channels &&
  1047. codec->sample_rate == icodec->sample_rate) {
  1048. ost->audio_resample = 0;
  1049. } else {
  1050. if (codec->channels != icodec->channels &&
  1051. icodec->codec_id == CODEC_ID_AC3) {
  1052. /* Special case for 5:1 AC3 input */
  1053. /* and mono or stereo output */
  1054. /* Request specific number of channels */
  1055. icodec->channels = codec->channels;
  1056. if (codec->sample_rate == icodec->sample_rate)
  1057. ost->audio_resample = 0;
  1058. else {
  1059. ost->audio_resample = 1;
  1060. ost->resample = audio_resample_init(codec->channels, icodec->channels,
  1061. codec->sample_rate,
  1062. icodec->sample_rate);
  1063. if(!ost->resample)
  1064. {
  1065. printf("Can't resample. Aborting.\n");
  1066. av_abort();
  1067. }
  1068. }
  1069. /* Request specific number of channels */
  1070. icodec->channels = codec->channels;
  1071. } else {
  1072. ost->audio_resample = 1;
  1073. ost->resample = audio_resample_init(codec->channels, icodec->channels,
  1074. codec->sample_rate,
  1075. icodec->sample_rate);
  1076. if(!ost->resample)
  1077. {
  1078. printf("Can't resample. Aborting.\n");
  1079. av_abort();
  1080. }
  1081. }
  1082. }
  1083. ist->decoding_needed = 1;
  1084. ost->encoding_needed = 1;
  1085. break;
  1086. case CODEC_TYPE_VIDEO:
  1087. if (codec->width == icodec->width &&
  1088. codec->height == icodec->height &&
  1089. frame_topBand == 0 &&
  1090. frame_bottomBand == 0 &&
  1091. frame_leftBand == 0 &&
  1092. frame_rightBand == 0)
  1093. {
  1094. ost->video_resample = 0;
  1095. ost->video_crop = 0;
  1096. } else if ((codec->width == icodec->width -
  1097. (frame_leftBand + frame_rightBand)) &&
  1098. (codec->height == icodec->height -
  1099. (frame_topBand + frame_bottomBand)))
  1100. {
  1101. ost->video_resample = 0;
  1102. ost->video_crop = 1;
  1103. ost->topBand = frame_topBand;
  1104. ost->leftBand = frame_leftBand;
  1105. } else {
  1106. uint8_t *buf;
  1107. ost->video_resample = 1;
  1108. ost->video_crop = 0; // cropping is handled as part of resample
  1109. buf = av_malloc((codec->width * codec->height * 3) / 2);
  1110. if (!buf)
  1111. goto fail;
  1112. ost->pict_tmp.data[0] = buf;
  1113. ost->pict_tmp.data[1] = ost->pict_tmp.data[0] + (codec->width * codec->height);
  1114. ost->pict_tmp.data[2] = ost->pict_tmp.data[1] + (codec->width * codec->height) / 4;
  1115. ost->pict_tmp.linesize[0] = codec->width;
  1116. ost->pict_tmp.linesize[1] = codec->width / 2;
  1117. ost->pict_tmp.linesize[2] = codec->width / 2;
  1118. ost->img_resample_ctx = img_resample_full_init(
  1119. ost->st->codec.width, ost->st->codec.height,
  1120. ist->st->codec.width, ist->st->codec.height,
  1121. frame_topBand, frame_bottomBand,
  1122. frame_leftBand, frame_rightBand);
  1123. }
  1124. ost->encoding_needed = 1;
  1125. ist->decoding_needed = 1;
  1126. break;
  1127. default:
  1128. av_abort();
  1129. }
  1130. /* two pass mode */
  1131. if (ost->encoding_needed &&
  1132. (codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))) {
  1133. char logfilename[1024];
  1134. FILE *f;
  1135. int size;
  1136. char *logbuffer;
  1137. snprintf(logfilename, sizeof(logfilename), "%s-%d.log",
  1138. pass_logfilename ?
  1139. pass_logfilename : DEFAULT_PASS_LOGFILENAME, i);
  1140. if (codec->flags & CODEC_FLAG_PASS1) {
  1141. f = fopen(logfilename, "w");
  1142. if (!f) {
  1143. perror(logfilename);
  1144. exit(1);
  1145. }
  1146. ost->logfile = f;
  1147. } else {
  1148. /* read the log file */
  1149. f = fopen(logfilename, "r");
  1150. if (!f) {
  1151. perror(logfilename);
  1152. exit(1);
  1153. }
  1154. fseek(f, 0, SEEK_END);
  1155. size = ftell(f);
  1156. fseek(f, 0, SEEK_SET);
  1157. logbuffer = av_malloc(size + 1);
  1158. if (!logbuffer) {
  1159. fprintf(stderr, "Could not allocate log buffer\n");
  1160. exit(1);
  1161. }
  1162. fread(logbuffer, 1, size, f);
  1163. fclose(f);
  1164. logbuffer[size] = '\0';
  1165. codec->stats_in = logbuffer;
  1166. }
  1167. }
  1168. }
  1169. }
  1170. /* dump the file output parameters - cannot be done before in case
  1171. of stream copy */
  1172. for(i=0;i<nb_output_files;i++) {
  1173. dump_format(output_files[i], i, output_files[i]->filename, 1);
  1174. }
  1175. /* dump the stream mapping */
  1176. fprintf(stderr, "Stream mapping:\n");
  1177. for(i=0;i<nb_ostreams;i++) {
  1178. ost = ost_table[i];
  1179. fprintf(stderr, " Stream #%d.%d -> #%d.%d\n",
  1180. ist_table[ost->source_index]->file_index,
  1181. ist_table[ost->source_index]->index,
  1182. ost->file_index,
  1183. ost->index);
  1184. }
  1185. /* open each encoder */
  1186. for(i=0;i<nb_ostreams;i++) {
  1187. ost = ost_table[i];
  1188. if (ost->encoding_needed) {
  1189. AVCodec *codec;
  1190. codec = avcodec_find_encoder(ost->st->codec.codec_id);
  1191. if (!codec) {
  1192. fprintf(stderr, "Unsupported codec for output stream #%d.%d\n",
  1193. ost->file_index, ost->index);
  1194. exit(1);
  1195. }
  1196. if (avcodec_open(&ost->st->codec, codec) < 0) {
  1197. fprintf(stderr, "Error while opening codec for stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height\n",
  1198. ost->file_index, ost->index);
  1199. exit(1);
  1200. }
  1201. }
  1202. }
  1203. /* open each decoder */
  1204. for(i=0;i<nb_istreams;i++) {
  1205. ist = ist_table[i];
  1206. if (ist->decoding_needed) {
  1207. AVCodec *codec;
  1208. codec = avcodec_find_decoder(ist->st->codec.codec_id);
  1209. if (!codec) {
  1210. fprintf(stderr, "Unsupported codec (id=%d) for input stream #%d.%d\n",
  1211. ist->st->codec.codec_id, ist->file_index, ist->index);
  1212. exit(1);
  1213. }
  1214. if (avcodec_open(&ist->st->codec, codec) < 0) {
  1215. fprintf(stderr, "Error while opening codec for input stream #%d.%d\n",
  1216. ist->file_index, ist->index);
  1217. exit(1);
  1218. }
  1219. //if (ist->st->codec.codec_type == CODEC_TYPE_VIDEO)
  1220. // ist->st->codec.flags |= CODEC_FLAG_REPEAT_FIELD;
  1221. }
  1222. }
  1223. /* init pts */
  1224. for(i=0;i<nb_istreams;i++) {
  1225. ist = ist_table[i];
  1226. is = input_files[ist->file_index];
  1227. ist->pts = 0;
  1228. ist->next_pts = 0;
  1229. }
  1230. /* compute buffer size max (should use a complete heuristic) */
  1231. for(i=0;i<nb_input_files;i++) {
  1232. file_table[i].buffer_size_max = 2048;
  1233. }
  1234. /* open files and write file headers */
  1235. for(i=0;i<nb_output_files;i++) {
  1236. os = output_files[i];
  1237. if (av_write_header(os) < 0) {
  1238. fprintf(stderr, "Could not write header for output file #%d (incorrect codec paramters ?)\n", i);
  1239. ret = -EINVAL;
  1240. goto fail;
  1241. }
  1242. }
  1243. #ifndef CONFIG_WIN32
  1244. if ( !using_stdin )
  1245. fprintf(stderr, "Press [q] to stop encoding\n");
  1246. #endif
  1247. term_init();
  1248. stream_no_data = 0;
  1249. key = -1;
  1250. for(; received_sigterm == 0;) {
  1251. int file_index, ist_index;
  1252. AVPacket pkt;
  1253. double pts_min;
  1254. redo:
  1255. /* if 'q' pressed, exits */
  1256. if (!using_stdin) {
  1257. /* read_key() returns 0 on EOF */
  1258. key = read_key();
  1259. if (key == 'q')
  1260. break;
  1261. }
  1262. /* select the stream that we must read now by looking at the
  1263. smallest output pts */
  1264. file_index = -1;
  1265. pts_min = 1e10;
  1266. for(i=0;i<nb_ostreams;i++) {
  1267. double pts;
  1268. ost = ost_table[i];
  1269. os = output_files[ost->file_index];
  1270. ist = ist_table[ost->source_index];
  1271. pts = (double)ost->st->pts.val * os->pts_num / os->pts_den;
  1272. if (!file_table[ist->file_index].eof_reached &&
  1273. pts < pts_min) {
  1274. pts_min = pts;
  1275. file_index = ist->file_index;
  1276. }
  1277. }
  1278. /* if none, if is finished */
  1279. if (file_index < 0) {
  1280. break;
  1281. }
  1282. /* finish if recording time exhausted */
  1283. if (recording_time > 0 && pts_min >= (recording_time / 1000000.0))
  1284. break;
  1285. /* read a frame from it and output it in the fifo */
  1286. is = input_files[file_index];
  1287. if (av_read_frame(is, &pkt) < 0) {
  1288. file_table[file_index].eof_reached = 1;
  1289. continue;
  1290. }
  1291. if (!pkt.size) {
  1292. stream_no_data = is;
  1293. } else {
  1294. stream_no_data = 0;
  1295. }
  1296. if (do_pkt_dump) {
  1297. av_pkt_dump(stdout, &pkt, do_hex_dump);
  1298. }
  1299. /* the following test is needed in case new streams appear
  1300. dynamically in stream : we ignore them */
  1301. if (pkt.stream_index >= file_table[file_index].nb_streams)
  1302. goto discard_packet;
  1303. ist_index = file_table[file_index].ist_index + pkt.stream_index;
  1304. ist = ist_table[ist_index];
  1305. if (ist->discard)
  1306. goto discard_packet;
  1307. //fprintf(stderr,"read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size);
  1308. if (output_packet(ist, ist_index, ost_table, nb_ostreams, &pkt) < 0) {
  1309. fprintf(stderr, "Error while decoding stream #%d.%d\n",
  1310. ist->file_index, ist->index);
  1311. av_free_packet(&pkt);
  1312. goto redo;
  1313. }
  1314. discard_packet:
  1315. av_free_packet(&pkt);
  1316. /* dump report by using the output first video and audio streams */
  1317. print_report(output_files, ost_table, nb_ostreams, 0);
  1318. }
  1319. /* at the end of stream, we must flush the decoder buffers */
  1320. for(i=0;i<nb_istreams;i++) {
  1321. ist = ist_table[i];
  1322. if (ist->decoding_needed) {
  1323. output_packet(ist, i, ost_table, nb_ostreams, NULL);
  1324. }
  1325. }
  1326. term_exit();
  1327. /* dump report by using the first video and audio streams */
  1328. print_report(output_files, ost_table, nb_ostreams, 1);
  1329. /* close each encoder */
  1330. for(i=0;i<nb_ostreams;i++) {
  1331. ost = ost_table[i];
  1332. if (ost->encoding_needed) {
  1333. av_freep(&ost->st->codec.stats_in);
  1334. avcodec_close(&ost->st->codec);
  1335. }
  1336. }
  1337. /* close each decoder */
  1338. for(i=0;i<nb_istreams;i++) {
  1339. ist = ist_table[i];
  1340. if (ist->decoding_needed) {
  1341. avcodec_close(&ist->st->codec);
  1342. }
  1343. }
  1344. /* write the trailer if needed and close file */
  1345. for(i=0;i<nb_output_files;i++) {
  1346. os = output_files[i];
  1347. av_write_trailer(os);
  1348. }
  1349. /* finished ! */
  1350. ret = 0;
  1351. fail1:
  1352. av_free(file_table);
  1353. if (ist_table) {
  1354. for(i=0;i<nb_istreams;i++) {
  1355. ist = ist_table[i];
  1356. av_free(ist);
  1357. }
  1358. av_free(ist_table);
  1359. }
  1360. if (ost_table) {
  1361. for(i=0;i<nb_ostreams;i++) {
  1362. ost = ost_table[i];
  1363. if (ost) {
  1364. if (ost->logfile) {
  1365. fclose(ost->logfile);
  1366. ost->logfile = NULL;
  1367. }
  1368. fifo_free(&ost->fifo); /* works even if fifo is not
  1369. initialized but set to zero */
  1370. av_free(ost->pict_tmp.data[0]);
  1371. if (ost->video_resample)
  1372. img_resample_close(ost->img_resample_ctx);
  1373. if (ost->audio_resample)
  1374. audio_resample_close(ost->resample);
  1375. av_free(ost);
  1376. }
  1377. }
  1378. av_free(ost_table);
  1379. }
  1380. return ret;
  1381. fail:
  1382. ret = -ENOMEM;
  1383. goto fail1;
  1384. }
  1385. #if 0
  1386. int file_read(const char *filename)
  1387. {
  1388. URLContext *h;
  1389. unsigned char buffer[1024];
  1390. int len, i;
  1391. if (url_open(&h, filename, O_RDONLY) < 0) {
  1392. printf("could not open '%s'\n", filename);
  1393. return -1;
  1394. }
  1395. for(;;) {
  1396. len = url_read(h, buffer, sizeof(buffer));
  1397. if (len <= 0)
  1398. break;
  1399. for(i=0;i<len;i++) putchar(buffer[i]);
  1400. }
  1401. url_close(h);
  1402. return 0;
  1403. }
  1404. #endif
  1405. static void opt_image_format(const char *arg)
  1406. {
  1407. AVImageFormat *f;
  1408. for(f = first_image_format; f != NULL; f = f->next) {
  1409. if (!strcmp(arg, f->name))
  1410. break;
  1411. }
  1412. if (!f) {
  1413. fprintf(stderr, "Unknown image format: '%s'\n", arg);
  1414. exit(1);
  1415. }
  1416. image_format = f;
  1417. }
  1418. static void opt_format(const char *arg)
  1419. {
  1420. /* compatibility stuff for pgmyuv */
  1421. if (!strcmp(arg, "pgmyuv")) {
  1422. opt_image_format(arg);
  1423. arg = "image";
  1424. }
  1425. file_iformat = av_find_input_format(arg);
  1426. file_oformat = guess_format(arg, NULL, NULL);
  1427. if (!file_iformat && !file_oformat) {
  1428. fprintf(stderr, "Unknown input or output format: %s\n", arg);
  1429. exit(1);
  1430. }
  1431. }
  1432. static void opt_video_bitrate(const char *arg)
  1433. {
  1434. video_bit_rate = atoi(arg) * 1000;
  1435. }
  1436. static void opt_video_bitrate_tolerance(const char *arg)
  1437. {
  1438. video_bit_rate_tolerance = atoi(arg) * 1000;
  1439. }
  1440. static void opt_video_bitrate_max(const char *arg)
  1441. {
  1442. video_rc_max_rate = atoi(arg) * 1000;
  1443. }
  1444. static void opt_video_bitrate_min(const char *arg)
  1445. {
  1446. video_rc_min_rate = atoi(arg) * 1000;
  1447. }
  1448. static void opt_video_buffer_size(const char *arg)
  1449. {
  1450. video_rc_buffer_size = atoi(arg) * 1024;
  1451. }
  1452. static void opt_video_rc_eq(char *arg)
  1453. {
  1454. video_rc_eq = arg;
  1455. }
  1456. static void opt_video_rc_override_string(char *arg)
  1457. {
  1458. video_rc_override_string = arg;
  1459. }
  1460. static void opt_workaround_bugs(const char *arg)
  1461. {
  1462. workaround_bugs = atoi(arg);
  1463. }
  1464. static void opt_dct_algo(const char *arg)
  1465. {
  1466. dct_algo = atoi(arg);
  1467. }
  1468. static void opt_idct_algo(const char *arg)
  1469. {
  1470. idct_algo = atoi(arg);
  1471. }
  1472. static void opt_error_resilience(const char *arg)
  1473. {
  1474. error_resilience = atoi(arg);
  1475. }
  1476. static void opt_error_concealment(const char *arg)
  1477. {
  1478. error_concealment = atoi(arg);
  1479. }
  1480. static void opt_debug(const char *arg)
  1481. {
  1482. debug = atoi(arg);
  1483. }
  1484. static void opt_verbose(const char *arg)
  1485. {
  1486. verbose = atoi(arg);
  1487. }
  1488. static void opt_frame_rate(const char *arg)
  1489. {
  1490. if (parse_frame_rate(&frame_rate, &frame_rate_base, arg) < 0) {
  1491. fprintf(stderr, "Incorrect frame rate\n");
  1492. exit(1);
  1493. }
  1494. }
  1495. static void opt_frame_crop_top(const char *arg)
  1496. {
  1497. frame_topBand = atoi(arg);
  1498. if (frame_topBand < 0) {
  1499. fprintf(stderr, "Incorrect top crop size\n");
  1500. exit(1);
  1501. }
  1502. if ((frame_topBand % 2) != 0) {
  1503. fprintf(stderr, "Top crop size must be a multiple of 2\n");
  1504. exit(1);
  1505. }
  1506. if ((frame_topBand) >= frame_height){
  1507. fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
  1508. exit(1);
  1509. }
  1510. frame_height -= frame_topBand;
  1511. }
  1512. static void opt_frame_crop_bottom(const char *arg)
  1513. {
  1514. frame_bottomBand = atoi(arg);
  1515. if (frame_bottomBand < 0) {
  1516. fprintf(stderr, "Incorrect bottom crop size\n");
  1517. exit(1);
  1518. }
  1519. if ((frame_bottomBand % 2) != 0) {
  1520. fprintf(stderr, "Bottom crop size must be a multiple of 2\n");
  1521. exit(1);
  1522. }
  1523. if ((frame_bottomBand) >= frame_height){
  1524. fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
  1525. exit(1);
  1526. }
  1527. frame_height -= frame_bottomBand;
  1528. }
  1529. static void opt_frame_crop_left(const char *arg)
  1530. {
  1531. frame_leftBand = atoi(arg);
  1532. if (frame_leftBand < 0) {
  1533. fprintf(stderr, "Incorrect left crop size\n");
  1534. exit(1);
  1535. }
  1536. if ((frame_leftBand % 2) != 0) {
  1537. fprintf(stderr, "Left crop size must be a multiple of 2\n");
  1538. exit(1);
  1539. }
  1540. if ((frame_leftBand) >= frame_width){
  1541. fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
  1542. exit(1);
  1543. }
  1544. frame_width -= frame_leftBand;
  1545. }
  1546. static void opt_frame_crop_right(const char *arg)
  1547. {
  1548. frame_rightBand = atoi(arg);
  1549. if (frame_rightBand < 0) {
  1550. fprintf(stderr, "Incorrect right crop size\n");
  1551. exit(1);
  1552. }
  1553. if ((frame_rightBand % 2) != 0) {
  1554. fprintf(stderr, "Right crop size must be a multiple of 2\n");
  1555. exit(1);
  1556. }
  1557. if ((frame_rightBand) >= frame_width){
  1558. fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
  1559. exit(1);
  1560. }
  1561. frame_width -= frame_rightBand;
  1562. }
  1563. static void opt_frame_size(const char *arg)
  1564. {
  1565. if (parse_image_size(&frame_width, &frame_height, arg) < 0) {
  1566. fprintf(stderr, "Incorrect frame size\n");
  1567. exit(1);
  1568. }
  1569. if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
  1570. fprintf(stderr, "Frame size must be a multiple of 2\n");
  1571. exit(1);
  1572. }
  1573. }
  1574. static void opt_frame_pix_fmt(const char *arg)
  1575. {
  1576. frame_pix_fmt = avcodec_get_pix_fmt(arg);
  1577. }
  1578. static void opt_frame_aspect_ratio(const char *arg)
  1579. {
  1580. int x = 0, y = 0;
  1581. double ar = 0;
  1582. const char *p;
  1583. p = strchr(arg, ':');
  1584. if (p) {
  1585. x = strtol(arg, (char **)&arg, 10);
  1586. if (arg == p)
  1587. y = strtol(arg+1, (char **)&arg, 10);
  1588. if (x > 0 && y > 0)
  1589. ar = (double)x / (double)y;
  1590. } else
  1591. ar = strtod(arg, (char **)&arg);
  1592. if (!ar) {
  1593. fprintf(stderr, "Incorrect aspect ratio specification.\n");
  1594. exit(1);
  1595. }
  1596. frame_aspect_ratio = ar;
  1597. }
  1598. static void opt_gop_size(const char *arg)
  1599. {
  1600. gop_size = atoi(arg);
  1601. }
  1602. static void opt_b_frames(const char *arg)
  1603. {
  1604. b_frames = atoi(arg);
  1605. if (b_frames > FF_MAX_B_FRAMES) {
  1606. fprintf(stderr, "\nCannot have more than %d B frames, increase FF_MAX_B_FRAMES.\n", FF_MAX_B_FRAMES);
  1607. exit(1);
  1608. } else if (b_frames < 1) {
  1609. fprintf(stderr, "\nNumber of B frames must be higher than 0\n");
  1610. exit(1);
  1611. }
  1612. }
  1613. static void opt_mb_decision(const char *arg)
  1614. {
  1615. mb_decision = atoi(arg);
  1616. }
  1617. static void opt_mb_cmp(const char *arg)
  1618. {
  1619. mb_cmp = atoi(arg);
  1620. }
  1621. static void opt_sub_cmp(const char *arg)
  1622. {
  1623. sub_cmp = atoi(arg);
  1624. }
  1625. static void opt_cmp(const char *arg)
  1626. {
  1627. cmp = atoi(arg);
  1628. }
  1629. static void opt_qscale(const char *arg)
  1630. {
  1631. video_qscale = atof(arg);
  1632. if (video_qscale < 0.01 ||
  1633. video_qscale > 255) {
  1634. fprintf(stderr, "qscale must be >= 0.01 and <= 255\n");
  1635. exit(1);
  1636. }
  1637. }
  1638. static void opt_qmin(const char *arg)
  1639. {
  1640. video_qmin = atoi(arg);
  1641. if (video_qmin < 0 ||
  1642. video_qmin > 31) {
  1643. fprintf(stderr, "qmin must be >= 1 and <= 31\n");
  1644. exit(1);
  1645. }
  1646. }
  1647. static void opt_qmax(const char *arg)
  1648. {
  1649. video_qmax = atoi(arg);
  1650. if (video_qmax < 0 ||
  1651. video_qmax > 31) {
  1652. fprintf(stderr, "qmax must be >= 1 and <= 31\n");
  1653. exit(1);
  1654. }
  1655. }
  1656. static void opt_mb_qmin(const char *arg)
  1657. {
  1658. video_mb_qmin = atoi(arg);
  1659. if (video_mb_qmin < 0 ||
  1660. video_mb_qmin > 31) {
  1661. fprintf(stderr, "qmin must be >= 1 and <= 31\n");
  1662. exit(1);
  1663. }
  1664. }
  1665. static void opt_mb_qmax(const char *arg)
  1666. {
  1667. video_mb_qmax = atoi(arg);
  1668. if (video_mb_qmax < 0 ||
  1669. video_mb_qmax > 31) {
  1670. fprintf(stderr, "qmax must be >= 1 and <= 31\n");
  1671. exit(1);
  1672. }
  1673. }
  1674. static void opt_qdiff(const char *arg)
  1675. {
  1676. video_qdiff = atoi(arg);
  1677. if (video_qdiff < 0 ||
  1678. video_qdiff > 31) {
  1679. fprintf(stderr, "qdiff must be >= 1 and <= 31\n");
  1680. exit(1);
  1681. }
  1682. }
  1683. static void opt_qblur(const char *arg)
  1684. {
  1685. video_qblur = atof(arg);
  1686. }
  1687. static void opt_qcomp(const char *arg)
  1688. {
  1689. video_qcomp = atof(arg);
  1690. }
  1691. static void opt_rc_initial_cplx(const char *arg)
  1692. {
  1693. video_rc_initial_cplx = atof(arg);
  1694. }
  1695. static void opt_b_qfactor(const char *arg)
  1696. {
  1697. video_b_qfactor = atof(arg);
  1698. }
  1699. static void opt_i_qfactor(const char *arg)
  1700. {
  1701. video_i_qfactor = atof(arg);
  1702. }
  1703. static void opt_b_qoffset(const char *arg)
  1704. {
  1705. video_b_qoffset = atof(arg);
  1706. }
  1707. static void opt_i_qoffset(const char *arg)
  1708. {
  1709. video_i_qoffset = atof(arg);
  1710. }
  1711. static void opt_packet_size(const char *arg)
  1712. {
  1713. packet_size= atoi(arg);
  1714. }
  1715. static void opt_error_rate(const char *arg)
  1716. {
  1717. error_rate= atoi(arg);
  1718. }
  1719. static void opt_strict(const char *arg)
  1720. {
  1721. strict= atoi(arg);
  1722. }
  1723. static void opt_audio_bitrate(const char *arg)
  1724. {
  1725. audio_bit_rate = atoi(arg) * 1000;
  1726. }
  1727. static void opt_audio_rate(const char *arg)
  1728. {
  1729. audio_sample_rate = atoi(arg);
  1730. }
  1731. static void opt_audio_channels(const char *arg)
  1732. {
  1733. audio_channels = atoi(arg);
  1734. }
  1735. static void opt_video_device(const char *arg)
  1736. {
  1737. video_device = av_strdup(arg);
  1738. }
  1739. static void opt_video_channel(const char *arg)
  1740. {
  1741. video_channel = strtol(arg, NULL, 0);
  1742. }
  1743. static void opt_video_standard(const char *arg)
  1744. {
  1745. video_standard = av_strdup(arg);
  1746. }
  1747. static void opt_audio_device(const char *arg)
  1748. {
  1749. audio_device = av_strdup(arg);
  1750. }
  1751. static void opt_dv1394(const char *arg)
  1752. {
  1753. video_grab_format = "dv1394";
  1754. audio_grab_format = NULL;
  1755. }
  1756. static void opt_audio_codec(const char *arg)
  1757. {
  1758. AVCodec *p;
  1759. if (!strcmp(arg, "copy")) {
  1760. audio_stream_copy = 1;
  1761. } else {
  1762. p = first_avcodec;
  1763. while (p) {
  1764. if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
  1765. break;
  1766. p = p->next;
  1767. }
  1768. if (p == NULL) {
  1769. fprintf(stderr, "Unknown audio codec '%s'\n", arg);
  1770. exit(1);
  1771. } else {
  1772. audio_codec_id = p->id;
  1773. }
  1774. }
  1775. }
  1776. static void add_frame_hooker(const char *arg)
  1777. {
  1778. int argc = 0;
  1779. char *argv[64];
  1780. int i;
  1781. char *args = av_strdup(arg);
  1782. using_vhook = 1;
  1783. argv[0] = strtok(args, " ");
  1784. while (argc < 62 && (argv[++argc] = strtok(NULL, " "))) {
  1785. }
  1786. i = frame_hook_add(argc, argv);
  1787. if (i != 0) {
  1788. fprintf(stderr, "Failed to add video hook function: %s\n", arg);
  1789. exit(1);
  1790. }
  1791. }
  1792. const char *motion_str[] = {
  1793. "zero",
  1794. "full",
  1795. "log",
  1796. "phods",
  1797. "epzs",
  1798. "x1",
  1799. NULL,
  1800. };
  1801. static void opt_motion_estimation(const char *arg)
  1802. {
  1803. const char **p;
  1804. p = motion_str;
  1805. for(;;) {
  1806. if (!*p) {
  1807. fprintf(stderr, "Unknown motion estimation method '%s'\n", arg);
  1808. exit(1);
  1809. }
  1810. if (!strcmp(*p, arg))
  1811. break;
  1812. p++;
  1813. }
  1814. me_method = (p - motion_str) + 1;
  1815. }
  1816. static void opt_video_codec(const char *arg)
  1817. {
  1818. AVCodec *p;
  1819. if (!strcmp(arg, "copy")) {
  1820. video_stream_copy = 1;
  1821. } else {
  1822. p = first_avcodec;
  1823. while (p) {
  1824. if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
  1825. break;
  1826. p = p->next;
  1827. }
  1828. if (p == NULL) {
  1829. fprintf(stderr, "Unknown video codec '%s'\n", arg);
  1830. exit(1);
  1831. } else {
  1832. video_codec_id = p->id;
  1833. }
  1834. }
  1835. }
  1836. static void opt_map(const char *arg)
  1837. {
  1838. AVStreamMap *m;
  1839. const char *p;
  1840. p = arg;
  1841. m = &stream_maps[nb_stream_maps++];
  1842. m->file_index = strtol(arg, (char **)&p, 0);
  1843. if (*p)
  1844. p++;
  1845. m->stream_index = strtol(p, (char **)&p, 0);
  1846. }
  1847. static void opt_recording_time(const char *arg)
  1848. {
  1849. recording_time = parse_date(arg, 1);
  1850. }
  1851. static void opt_start_time(const char *arg)
  1852. {
  1853. start_time = parse_date(arg, 1);
  1854. }
  1855. static void opt_input_file(const char *filename)
  1856. {
  1857. AVFormatContext *ic;
  1858. AVFormatParameters params, *ap = &params;
  1859. int err, i, ret, rfps, rfps_base;
  1860. if (!strcmp(filename, "-"))
  1861. filename = "pipe:";
  1862. using_stdin |= !strcmp(filename, "pipe:" ) ||
  1863. !strcmp( filename, "/dev/stdin" );
  1864. /* get default parameters from command line */
  1865. memset(ap, 0, sizeof(*ap));
  1866. ap->sample_rate = audio_sample_rate;
  1867. ap->channels = audio_channels;
  1868. ap->frame_rate = frame_rate;
  1869. ap->frame_rate_base = frame_rate_base;
  1870. ap->width = frame_width;
  1871. ap->height = frame_height;
  1872. ap->image_format = image_format;
  1873. ap->pix_fmt = frame_pix_fmt;
  1874. /* open the input file with generic libav function */
  1875. err = av_open_input_file(&ic, filename, file_iformat, 0, ap);
  1876. if (err < 0) {
  1877. print_error(filename, err);
  1878. exit(1);
  1879. }
  1880. /* If not enough info to get the stream parameters, we decode the
  1881. first frames to get it. (used in mpeg case for example) */
  1882. ret = av_find_stream_info(ic);
  1883. if (ret < 0) {
  1884. fprintf(stderr, "%s: could not find codec parameters\n", filename);
  1885. exit(1);
  1886. }
  1887. /* if seeking requested, we execute it */
  1888. if (start_time != 0) {
  1889. int64_t timestamp;
  1890. timestamp = start_time;
  1891. /* add the stream start time */
  1892. if (ic->start_time != AV_NOPTS_VALUE)
  1893. timestamp += ic->start_time;
  1894. ret = av_seek_frame(ic, -1, timestamp);
  1895. if (ret < 0) {
  1896. fprintf(stderr, "%s: could not seek to position %0.3f\n",
  1897. filename, (double)timestamp / AV_TIME_BASE);
  1898. }
  1899. /* reset seek info */
  1900. start_time = 0;
  1901. }
  1902. /* update the current parameters so that they match the one of the input stream */
  1903. for(i=0;i<ic->nb_streams;i++) {
  1904. AVCodecContext *enc = &ic->streams[i]->codec;
  1905. switch(enc->codec_type) {
  1906. case CODEC_TYPE_AUDIO:
  1907. //fprintf(stderr, "\nInput Audio channels: %d", enc->channels);
  1908. audio_channels = enc->channels;
  1909. audio_sample_rate = enc->sample_rate;
  1910. break;
  1911. case CODEC_TYPE_VIDEO:
  1912. frame_height = enc->height;
  1913. frame_width = enc->width;
  1914. frame_aspect_ratio = av_q2d(enc->sample_aspect_ratio) * enc->width / enc->height;
  1915. frame_pix_fmt = enc->pix_fmt;
  1916. rfps = ic->streams[i]->r_frame_rate;
  1917. rfps_base = ic->streams[i]->r_frame_rate_base;
  1918. enc->workaround_bugs = workaround_bugs;
  1919. enc->error_resilience = error_resilience;
  1920. enc->error_concealment = error_concealment;
  1921. enc->idct_algo= idct_algo;
  1922. enc->debug= debug;
  1923. if(bitexact)
  1924. enc->flags|= CODEC_FLAG_BITEXACT;
  1925. assert(enc->frame_rate_base == rfps_base); // should be true for now
  1926. if (enc->frame_rate != rfps) {
  1927. fprintf(stderr,"\nSeems that stream %d comes from film source: %2.2f->%2.2f\n",
  1928. i, (float)enc->frame_rate / enc->frame_rate_base,
  1929. (float)rfps / rfps_base);
  1930. }
  1931. /* update the current frame rate to match the stream frame rate */
  1932. frame_rate = rfps;
  1933. frame_rate_base = rfps_base;
  1934. enc->rate_emu = rate_emu;
  1935. break;
  1936. case CODEC_TYPE_DATA:
  1937. break;
  1938. default:
  1939. av_abort();
  1940. }
  1941. }
  1942. input_files[nb_input_files] = ic;
  1943. /* dump the file content */
  1944. dump_format(ic, nb_input_files, filename, 0);
  1945. nb_input_files++;
  1946. file_iformat = NULL;
  1947. file_oformat = NULL;
  1948. image_format = NULL;
  1949. rate_emu = 0;
  1950. }
  1951. static void check_audio_video_inputs(int *has_video_ptr, int *has_audio_ptr)
  1952. {
  1953. int has_video, has_audio, i, j;
  1954. AVFormatContext *ic;
  1955. has_video = 0;
  1956. has_audio = 0;
  1957. for(j=0;j<nb_input_files;j++) {
  1958. ic = input_files[j];
  1959. for(i=0;i<ic->nb_streams;i++) {
  1960. AVCodecContext *enc = &ic->streams[i]->codec;
  1961. switch(enc->codec_type) {
  1962. case CODEC_TYPE_AUDIO:
  1963. has_audio = 1;
  1964. break;
  1965. case CODEC_TYPE_VIDEO:
  1966. has_video = 1;
  1967. break;
  1968. default:
  1969. av_abort();
  1970. }
  1971. }
  1972. }
  1973. *has_video_ptr = has_video;
  1974. *has_audio_ptr = has_audio;
  1975. }
  1976. static void opt_output_file(const char *filename)
  1977. {
  1978. AVStream *st;
  1979. AVFormatContext *oc;
  1980. int use_video, use_audio, nb_streams, input_has_video, input_has_audio;
  1981. int codec_id;
  1982. AVFormatParameters params, *ap = &params;
  1983. if (!strcmp(filename, "-"))
  1984. filename = "pipe:";
  1985. oc = av_mallocz(sizeof(AVFormatContext));
  1986. if (!file_oformat) {
  1987. file_oformat = guess_format(NULL, filename, NULL);
  1988. if (!file_oformat) {
  1989. fprintf(stderr, "Unable for find a suitable output format for '%s'\n",
  1990. filename);
  1991. exit(1);
  1992. }
  1993. }
  1994. oc->oformat = file_oformat;
  1995. if (!strcmp(file_oformat->name, "ffm") &&
  1996. strstart(filename, "http:", NULL)) {
  1997. /* special case for files sent to ffserver: we get the stream
  1998. parameters from ffserver */
  1999. if (read_ffserver_streams(oc, filename) < 0) {
  2000. fprintf(stderr, "Could not read stream parameters from '%s'\n", filename);
  2001. exit(1);
  2002. }
  2003. } else {
  2004. use_video = file_oformat->video_codec != CODEC_ID_NONE;
  2005. use_audio = file_oformat->audio_codec != CODEC_ID_NONE;
  2006. /* disable if no corresponding type found and at least one
  2007. input file */
  2008. if (nb_input_files > 0) {
  2009. check_audio_video_inputs(&input_has_video, &input_has_audio);
  2010. if (!input_has_video)
  2011. use_video = 0;
  2012. if (!input_has_audio)
  2013. use_audio = 0;
  2014. }
  2015. /* manual disable */
  2016. if (audio_disable) {
  2017. use_audio = 0;
  2018. }
  2019. if (video_disable) {
  2020. use_video = 0;
  2021. }
  2022. nb_streams = 0;
  2023. if (use_video) {
  2024. AVCodecContext *video_enc;
  2025. st = av_mallocz(sizeof(AVStream));
  2026. if (!st) {
  2027. fprintf(stderr, "Could not alloc stream\n");
  2028. exit(1);
  2029. }
  2030. avcodec_get_context_defaults(&st->codec);
  2031. video_enc = &st->codec;
  2032. if(!strcmp(file_oformat->name, "mp4") || !strcmp(file_oformat->name, "mov") || !strcmp(file_oformat->name, "3gp"))
  2033. video_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
  2034. if (video_stream_copy) {
  2035. st->stream_copy = 1;
  2036. video_enc->codec_type = CODEC_TYPE_VIDEO;
  2037. } else {
  2038. char *p;
  2039. int i;
  2040. codec_id = file_oformat->video_codec;
  2041. if (video_codec_id != CODEC_ID_NONE)
  2042. codec_id = video_codec_id;
  2043. video_enc->codec_id = codec_id;
  2044. video_enc->bit_rate = video_bit_rate;
  2045. video_enc->bit_rate_tolerance = video_bit_rate_tolerance;
  2046. video_enc->frame_rate = frame_rate;
  2047. video_enc->frame_rate_base = frame_rate_base;
  2048. video_enc->width = frame_width;
  2049. video_enc->height = frame_height;
  2050. video_enc->sample_aspect_ratio = av_d2q(frame_aspect_ratio*frame_height/frame_width, 255);
  2051. video_enc->pix_fmt = frame_pix_fmt;
  2052. if (!intra_only)
  2053. video_enc->gop_size = gop_size;
  2054. else
  2055. video_enc->gop_size = 0;
  2056. if (video_qscale || same_quality) {
  2057. video_enc->flags |= CODEC_FLAG_QSCALE;
  2058. st->quality = FF_QP2LAMBDA * video_qscale;
  2059. }
  2060. if(intra_matrix)
  2061. video_enc->intra_matrix = intra_matrix;
  2062. if(inter_matrix)
  2063. video_enc->inter_matrix = inter_matrix;
  2064. if(bitexact)
  2065. video_enc->flags |= CODEC_FLAG_BITEXACT;
  2066. video_enc->mb_decision = mb_decision;
  2067. video_enc->mb_cmp = mb_cmp;
  2068. video_enc->me_sub_cmp = sub_cmp;
  2069. video_enc->me_cmp = cmp;
  2070. if (use_umv) {
  2071. video_enc->flags |= CODEC_FLAG_H263P_UMV;
  2072. }
  2073. if (use_aic) {
  2074. video_enc->flags |= CODEC_FLAG_H263P_AIC;
  2075. }
  2076. if (use_aiv) {
  2077. video_enc->flags |= CODEC_FLAG_H263P_AIV;
  2078. }
  2079. if (use_4mv) {
  2080. video_enc->flags |= CODEC_FLAG_4MV;
  2081. }
  2082. if (use_obmc) {
  2083. video_enc->flags |= CODEC_FLAG_OBMC;
  2084. }
  2085. if(use_part) {
  2086. video_enc->flags |= CODEC_FLAG_PART;
  2087. }
  2088. if (b_frames) {
  2089. video_enc->max_b_frames = b_frames;
  2090. video_enc->b_frame_strategy = 0;
  2091. video_enc->b_quant_factor = 2.0;
  2092. }
  2093. if (do_interlace) {
  2094. video_enc->flags |= CODEC_FLAG_INTERLACED_DCT;
  2095. }
  2096. video_enc->qmin = video_qmin;
  2097. video_enc->qmax = video_qmax;
  2098. video_enc->mb_qmin = video_mb_qmin;
  2099. video_enc->mb_qmax = video_mb_qmax;
  2100. video_enc->max_qdiff = video_qdiff;
  2101. video_enc->qblur = video_qblur;
  2102. video_enc->qcompress = video_qcomp;
  2103. video_enc->rc_eq = video_rc_eq;
  2104. video_enc->debug= debug;
  2105. p= video_rc_override_string;
  2106. for(i=0; p; i++){
  2107. int start, end, q;
  2108. int e=sscanf(p, "%d,%d,%d", &start, &end, &q);
  2109. if(e!=3){
  2110. fprintf(stderr, "error parsing rc_override\n");
  2111. exit(1);
  2112. }
  2113. video_enc->rc_override=
  2114. av_realloc(video_enc->rc_override,
  2115. sizeof(RcOverride)*(i+1));
  2116. video_enc->rc_override[i].start_frame= start;
  2117. video_enc->rc_override[i].end_frame = end;
  2118. if(q>0){
  2119. video_enc->rc_override[i].qscale= q;
  2120. video_enc->rc_override[i].quality_factor= 1.0;
  2121. }
  2122. else{
  2123. video_enc->rc_override[i].qscale= 0;
  2124. video_enc->rc_override[i].quality_factor= -q/100.0;
  2125. }
  2126. p= strchr(p, '/');
  2127. if(p) p++;
  2128. }
  2129. video_enc->rc_override_count=i;
  2130. video_enc->rc_max_rate = video_rc_max_rate;
  2131. video_enc->rc_min_rate = video_rc_min_rate;
  2132. video_enc->rc_buffer_size = video_rc_buffer_size;
  2133. video_enc->rc_buffer_aggressivity= video_rc_buffer_aggressivity;
  2134. video_enc->rc_initial_cplx= video_rc_initial_cplx;
  2135. video_enc->i_quant_factor = video_i_qfactor;
  2136. video_enc->b_quant_factor = video_b_qfactor;
  2137. video_enc->i_quant_offset = video_i_qoffset;
  2138. video_enc->b_quant_offset = video_b_qoffset;
  2139. video_enc->dct_algo = dct_algo;
  2140. video_enc->idct_algo = idct_algo;
  2141. video_enc->strict_std_compliance = strict;
  2142. video_enc->error_rate = error_rate;
  2143. if(packet_size){
  2144. video_enc->rtp_mode= 1;
  2145. video_enc->rtp_payload_size= packet_size;
  2146. }
  2147. if (do_psnr)
  2148. video_enc->flags|= CODEC_FLAG_PSNR;
  2149. video_enc->me_method = me_method;
  2150. /* two pass mode */
  2151. if (do_pass) {
  2152. if (do_pass == 1) {
  2153. video_enc->flags |= CODEC_FLAG_PASS1;
  2154. } else {
  2155. video_enc->flags |= CODEC_FLAG_PASS2;
  2156. }
  2157. }
  2158. }
  2159. oc->streams[nb_streams] = st;
  2160. nb_streams++;
  2161. }
  2162. if (use_audio) {
  2163. AVCodecContext *audio_enc;
  2164. st = av_mallocz(sizeof(AVStream));
  2165. if (!st) {
  2166. fprintf(stderr, "Could not alloc stream\n");
  2167. exit(1);
  2168. }
  2169. avcodec_get_context_defaults(&st->codec);
  2170. audio_enc = &st->codec;
  2171. audio_enc->codec_type = CODEC_TYPE_AUDIO;
  2172. if(!strcmp(file_oformat->name, "mp4") || !strcmp(file_oformat->name, "mov") || !strcmp(file_oformat->name, "3gp"))
  2173. audio_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
  2174. if (audio_stream_copy) {
  2175. st->stream_copy = 1;
  2176. } else {
  2177. codec_id = file_oformat->audio_codec;
  2178. if (audio_codec_id != CODEC_ID_NONE)
  2179. codec_id = audio_codec_id;
  2180. audio_enc->codec_id = codec_id;
  2181. audio_enc->bit_rate = audio_bit_rate;
  2182. audio_enc->sample_rate = audio_sample_rate;
  2183. /* For audio codecs other than AC3 we limit */
  2184. /* the number of coded channels to stereo */
  2185. if (audio_channels > 2 && codec_id != CODEC_ID_AC3) {
  2186. audio_enc->channels = 2;
  2187. } else
  2188. audio_enc->channels = audio_channels;
  2189. }
  2190. oc->streams[nb_streams] = st;
  2191. nb_streams++;
  2192. }
  2193. oc->nb_streams = nb_streams;
  2194. if (!nb_streams) {
  2195. fprintf(stderr, "No audio or video streams available\n");
  2196. exit(1);
  2197. }
  2198. if (str_title)
  2199. pstrcpy(oc->title, sizeof(oc->title), str_title);
  2200. if (str_author)
  2201. pstrcpy(oc->author, sizeof(oc->author), str_author);
  2202. if (str_copyright)
  2203. pstrcpy(oc->copyright, sizeof(oc->copyright), str_copyright);
  2204. if (str_comment)
  2205. pstrcpy(oc->comment, sizeof(oc->comment), str_comment);
  2206. }
  2207. output_files[nb_output_files++] = oc;
  2208. strcpy(oc->filename, filename);
  2209. /* check filename in case of an image number is expected */
  2210. if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
  2211. if (filename_number_test(oc->filename) < 0) {
  2212. print_error(oc->filename, AVERROR_NUMEXPECTED);
  2213. exit(1);
  2214. }
  2215. }
  2216. if (!(oc->oformat->flags & AVFMT_NOFILE)) {
  2217. /* test if it already exists to avoid loosing precious files */
  2218. if (!file_overwrite &&
  2219. (strchr(filename, ':') == NULL ||
  2220. strstart(filename, "file:", NULL))) {
  2221. if (url_exist(filename)) {
  2222. int c;
  2223. if ( !using_stdin ) {
  2224. fprintf(stderr,"File '%s' already exists. Overwrite ? [y/N] ", filename);
  2225. fflush(stderr);
  2226. c = getchar();
  2227. if (toupper(c) != 'Y') {
  2228. fprintf(stderr, "Not overwriting - exiting\n");
  2229. exit(1);
  2230. }
  2231. }
  2232. else {
  2233. fprintf(stderr,"File '%s' already exists. Exiting.\n", filename);
  2234. exit(1);
  2235. }
  2236. }
  2237. }
  2238. /* open the file */
  2239. if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
  2240. fprintf(stderr, "Could not open '%s'\n", filename);
  2241. exit(1);
  2242. }
  2243. }
  2244. memset(ap, 0, sizeof(*ap));
  2245. ap->image_format = image_format;
  2246. if (av_set_parameters(oc, ap) < 0) {
  2247. fprintf(stderr, "%s: Invalid encoding parameters\n",
  2248. oc->filename);
  2249. exit(1);
  2250. }
  2251. /* reset some options */
  2252. file_oformat = NULL;
  2253. file_iformat = NULL;
  2254. image_format = NULL;
  2255. audio_disable = 0;
  2256. video_disable = 0;
  2257. audio_codec_id = CODEC_ID_NONE;
  2258. video_codec_id = CODEC_ID_NONE;
  2259. audio_stream_copy = 0;
  2260. video_stream_copy = 0;
  2261. }
  2262. /* prepare dummy protocols for grab */
  2263. static void prepare_grab(void)
  2264. {
  2265. int has_video, has_audio, i, j;
  2266. AVFormatContext *oc;
  2267. AVFormatContext *ic;
  2268. AVFormatParameters vp1, *vp = &vp1;
  2269. AVFormatParameters ap1, *ap = &ap1;
  2270. /* see if audio/video inputs are needed */
  2271. has_video = 0;
  2272. has_audio = 0;
  2273. memset(ap, 0, sizeof(*ap));
  2274. memset(vp, 0, sizeof(*vp));
  2275. for(j=0;j<nb_output_files;j++) {
  2276. oc = output_files[j];
  2277. for(i=0;i<oc->nb_streams;i++) {
  2278. AVCodecContext *enc = &oc->streams[i]->codec;
  2279. switch(enc->codec_type) {
  2280. case CODEC_TYPE_AUDIO:
  2281. if (enc->sample_rate > ap->sample_rate)
  2282. ap->sample_rate = enc->sample_rate;
  2283. if (enc->channels > ap->channels)
  2284. ap->channels = enc->channels;
  2285. has_audio = 1;
  2286. break;
  2287. case CODEC_TYPE_VIDEO:
  2288. if (enc->width > vp->width)
  2289. vp->width = enc->width;
  2290. if (enc->height > vp->height)
  2291. vp->height = enc->height;
  2292. assert(enc->frame_rate_base == DEFAULT_FRAME_RATE_BASE);
  2293. if (enc->frame_rate > vp->frame_rate){
  2294. vp->frame_rate = enc->frame_rate;
  2295. vp->frame_rate_base = enc->frame_rate_base;
  2296. }
  2297. has_video = 1;
  2298. break;
  2299. default:
  2300. av_abort();
  2301. }
  2302. }
  2303. }
  2304. if (has_video == 0 && has_audio == 0) {
  2305. fprintf(stderr, "Output file must have at least one audio or video stream\n");
  2306. exit(1);
  2307. }
  2308. if (has_video) {
  2309. AVInputFormat *fmt1;
  2310. fmt1 = av_find_input_format(video_grab_format);
  2311. vp->device = video_device;
  2312. vp->channel = video_channel;
  2313. vp->standard = video_standard;
  2314. if (av_open_input_file(&ic, "", fmt1, 0, vp) < 0) {
  2315. fprintf(stderr, "Could not find video grab device\n");
  2316. exit(1);
  2317. }
  2318. /* If not enough info to get the stream parameters, we decode the
  2319. first frames to get it. */
  2320. if ((ic->ctx_flags & AVFMTCTX_NOHEADER) && av_find_stream_info(ic) < 0) {
  2321. fprintf(stderr, "Could not find video grab parameters\n");
  2322. exit(1);
  2323. }
  2324. /* by now video grab has one stream */
  2325. ic->streams[0]->r_frame_rate = vp->frame_rate;
  2326. ic->streams[0]->r_frame_rate_base = vp->frame_rate_base;
  2327. input_files[nb_input_files] = ic;
  2328. dump_format(ic, nb_input_files, "", 0);
  2329. nb_input_files++;
  2330. }
  2331. if (has_audio && audio_grab_format) {
  2332. AVInputFormat *fmt1;
  2333. fmt1 = av_find_input_format(audio_grab_format);
  2334. ap->device = audio_device;
  2335. if (av_open_input_file(&ic, "", fmt1, 0, ap) < 0) {
  2336. fprintf(stderr, "Could not find audio grab device\n");
  2337. exit(1);
  2338. }
  2339. input_files[nb_input_files] = ic;
  2340. dump_format(ic, nb_input_files, "", 0);
  2341. nb_input_files++;
  2342. }
  2343. }
  2344. /* same option as mencoder */
  2345. static void opt_pass(const char *pass_str)
  2346. {
  2347. int pass;
  2348. pass = atoi(pass_str);
  2349. if (pass != 1 && pass != 2) {
  2350. fprintf(stderr, "pass number can be only 1 or 2\n");
  2351. exit(1);
  2352. }
  2353. do_pass = pass;
  2354. }
  2355. #if defined(CONFIG_WIN32) || defined(CONFIG_OS2)
  2356. static int64_t getutime(void)
  2357. {
  2358. return av_gettime();
  2359. }
  2360. #else
  2361. static int64_t getutime(void)
  2362. {
  2363. struct rusage rusage;
  2364. getrusage(RUSAGE_SELF, &rusage);
  2365. return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
  2366. }
  2367. #endif
  2368. extern int ffm_nopts;
  2369. static void opt_bitexact(void)
  2370. {
  2371. bitexact=1;
  2372. /* disable generate of real time pts in ffm (need to be supressed anyway) */
  2373. ffm_nopts = 1;
  2374. }
  2375. static void show_formats(void)
  2376. {
  2377. AVInputFormat *ifmt;
  2378. AVOutputFormat *ofmt;
  2379. AVImageFormat *image_fmt;
  2380. URLProtocol *up;
  2381. AVCodec *p;
  2382. const char **pp;
  2383. printf("Output audio/video file formats:");
  2384. for(ofmt = first_oformat; ofmt != NULL; ofmt = ofmt->next) {
  2385. printf(" %s", ofmt->name);
  2386. }
  2387. printf("\n");
  2388. printf("Input audio/video file formats:");
  2389. for(ifmt = first_iformat; ifmt != NULL; ifmt = ifmt->next) {
  2390. printf(" %s", ifmt->name);
  2391. }
  2392. printf("\n");
  2393. printf("Output image formats:");
  2394. for(image_fmt = first_image_format; image_fmt != NULL;
  2395. image_fmt = image_fmt->next) {
  2396. if (image_fmt->img_write)
  2397. printf(" %s", image_fmt->name);
  2398. }
  2399. printf("\n");
  2400. printf("Input image formats:");
  2401. for(image_fmt = first_image_format; image_fmt != NULL;
  2402. image_fmt = image_fmt->next) {
  2403. if (image_fmt->img_read)
  2404. printf(" %s", image_fmt->name);
  2405. }
  2406. printf("\n");
  2407. printf("Codecs:\n");
  2408. printf(" Encoders:");
  2409. for(p = first_avcodec; p != NULL; p = p->next) {
  2410. if (p->encode)
  2411. printf(" %s", p->name);
  2412. }
  2413. printf("\n");
  2414. printf(" Decoders:");
  2415. for(p = first_avcodec; p != NULL; p = p->next) {
  2416. if (p->decode)
  2417. printf(" %s", p->name);
  2418. }
  2419. printf("\n");
  2420. printf("Supported file protocols:");
  2421. for(up = first_protocol; up != NULL; up = up->next)
  2422. printf(" %s:", up->name);
  2423. printf("\n");
  2424. printf("Frame size, frame rate abbreviations: ntsc pal qntsc qpal sntsc spal film ntsc-film sqcif qcif cif 4cif\n");
  2425. printf("Motion estimation methods:");
  2426. pp = motion_str;
  2427. while (*pp) {
  2428. printf(" %s", *pp);
  2429. if ((pp - motion_str + 1) == ME_ZERO)
  2430. printf("(fastest)");
  2431. else if ((pp - motion_str + 1) == ME_FULL)
  2432. printf("(slowest)");
  2433. else if ((pp - motion_str + 1) == ME_EPZS)
  2434. printf("(default)");
  2435. pp++;
  2436. }
  2437. printf("\n");
  2438. exit(1);
  2439. }
  2440. void parse_matrix_coeffs(uint16_t *dest, const char *str)
  2441. {
  2442. int i;
  2443. const char *p = str;
  2444. for(i = 0;; i++) {
  2445. dest[i] = atoi(p);
  2446. if(i == 63)
  2447. break;
  2448. p = strchr(p, ',');
  2449. if(!p) {
  2450. fprintf(stderr, "Syntax error in matrix \"%s\" at coeff %d\n", str, i);
  2451. exit(1);
  2452. }
  2453. p++;
  2454. }
  2455. }
  2456. void opt_inter_matrix(const char *arg)
  2457. {
  2458. inter_matrix = av_mallocz(sizeof(uint16_t) * 64);
  2459. parse_matrix_coeffs(inter_matrix, arg);
  2460. }
  2461. void opt_intra_matrix(const char *arg)
  2462. {
  2463. intra_matrix = av_mallocz(sizeof(uint16_t) * 64);
  2464. parse_matrix_coeffs(intra_matrix, arg);
  2465. }
  2466. static void opt_target(const char *arg)
  2467. {
  2468. int norm = -1;
  2469. if(!strncmp(arg, "pal-", 4)) {
  2470. norm = 0;
  2471. arg += 4;
  2472. } else if(!strncmp(arg, "ntsc-", 5)) {
  2473. norm = 1;
  2474. arg += 5;
  2475. } else {
  2476. int fr;
  2477. /* Calculate FR via float to avoid int overflow */
  2478. fr = (int)(frame_rate * 1000.0 / frame_rate_base);
  2479. if(fr == 25000) {
  2480. norm = 0;
  2481. } else if((fr == 29970) || (fr == 23976)) {
  2482. norm = 1;
  2483. } else {
  2484. /* Try to determine PAL/NTSC by peeking in the input files */
  2485. if(nb_input_files) {
  2486. int i, j;
  2487. for(j = 0; j < nb_input_files; j++) {
  2488. for(i = 0; i < input_files[j]->nb_streams; i++) {
  2489. AVCodecContext *c = &input_files[j]->streams[i]->codec;
  2490. if(c->codec_type != CODEC_TYPE_VIDEO)
  2491. continue;
  2492. fr = c->frame_rate * 1000 / c->frame_rate_base;
  2493. if(fr == 25000) {
  2494. norm = 0;
  2495. break;
  2496. } else if((fr == 29970) || (fr == 23976)) {
  2497. norm = 1;
  2498. break;
  2499. }
  2500. }
  2501. if(norm >= 0)
  2502. break;
  2503. }
  2504. }
  2505. }
  2506. if(verbose && norm >= 0)
  2507. printf("Assuming %s for target.\n", norm ? "NTSC" : "PAL");
  2508. }
  2509. if(norm < 0) {
  2510. fprintf(stderr, "Could not determine norm (PAL/NTSC) for target.\n");
  2511. fprintf(stderr, "Please prefix target with \"pal-\" or \"ntsc-\",\n");
  2512. fprintf(stderr, "or set a framerate with \"-r xxx\".\n");
  2513. exit(1);
  2514. }
  2515. if(!strcmp(arg, "vcd")) {
  2516. opt_video_codec("mpeg1video");
  2517. opt_audio_codec("mp2");
  2518. opt_format("vcd");
  2519. opt_frame_size(norm ? "352x240" : "352x288");
  2520. video_bit_rate = 1150000;
  2521. video_rc_max_rate = 1150000;
  2522. video_rc_min_rate = 1150000;
  2523. video_rc_buffer_size = 40*1024*8;
  2524. audio_bit_rate = 224000;
  2525. audio_sample_rate = 44100;
  2526. } else if(!strcmp(arg, "svcd")) {
  2527. opt_video_codec("mpeg2video");
  2528. opt_audio_codec("mp2");
  2529. opt_format("vcd");
  2530. opt_frame_size(norm ? "480x480" : "480x576");
  2531. opt_gop_size(norm ? "18" : "15");
  2532. video_bit_rate = 2040000;
  2533. video_rc_max_rate = 2516000;
  2534. video_rc_min_rate = 1145000;
  2535. video_rc_buffer_size = 224*1024*8;
  2536. audio_bit_rate = 224000;
  2537. audio_sample_rate = 44100;
  2538. } else if(!strcmp(arg, "dvd")) {
  2539. opt_video_codec("mpeg2video");
  2540. opt_audio_codec("ac3");
  2541. opt_format("vob");
  2542. opt_frame_size(norm ? "720x480" : "720x576");
  2543. opt_gop_size(norm ? "18" : "15");
  2544. video_bit_rate = 6000000;
  2545. video_rc_max_rate = 9000000;
  2546. video_rc_min_rate = 1500000;
  2547. video_rc_buffer_size = 224*1024*8;
  2548. audio_bit_rate = 448000;
  2549. audio_sample_rate = 48000;
  2550. } else {
  2551. fprintf(stderr, "Unknown target: %s\n", arg);
  2552. exit(1);
  2553. }
  2554. }
  2555. const OptionDef options[] = {
  2556. /* main options */
  2557. { "L", 0, {(void*)show_license}, "show license" },
  2558. { "h", 0, {(void*)show_help}, "show help" },
  2559. { "formats", 0, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
  2560. { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
  2561. { "img", HAS_ARG, {(void*)opt_image_format}, "force image format", "img_fmt" },
  2562. { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" },
  2563. { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
  2564. { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file:stream" },
  2565. { "t", HAS_ARG, {(void*)opt_recording_time}, "set the recording time", "duration" },
  2566. { "ss", HAS_ARG, {(void*)opt_start_time}, "set the start time offset", "time_off" },
  2567. { "title", HAS_ARG | OPT_STRING, {(void*)&str_title}, "set the title", "string" },
  2568. { "author", HAS_ARG | OPT_STRING, {(void*)&str_author}, "set the author", "string" },
  2569. { "copyright", HAS_ARG | OPT_STRING, {(void*)&str_copyright}, "set the copyright", "string" },
  2570. { "comment", HAS_ARG | OPT_STRING, {(void*)&str_comment}, "set the comment", "string" },
  2571. { "debug", HAS_ARG | OPT_EXPERT, {(void*)opt_debug}, "print specific debug info", "" },
  2572. { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark},
  2573. "add timings for benchmarking" },
  2574. { "dump", OPT_BOOL | OPT_EXPERT, {(void*)&do_pkt_dump},
  2575. "dump each input packet" },
  2576. { "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump},
  2577. "when dumping packets, also dump the payload" },
  2578. { "bitexact", OPT_EXPERT, {(void*)opt_bitexact}, "only use bit exact algorithms (for codec testing)" },
  2579. { "re", OPT_BOOL | OPT_EXPERT, {(void*)&rate_emu}, "read input at native frame rate", "" },
  2580. { "loop", OPT_BOOL | OPT_EXPERT, {(void*)&loop_input}, "loop (current only works with images)" },
  2581. { "v", HAS_ARG, {(void*)opt_verbose}, "control amount of logging", "verbose" },
  2582. { "target", HAS_ARG, {(void*)opt_target}, "specify target file type (\"vcd\", \"svcd\" or \"dvd\")", "type" },
  2583. /* video options */
  2584. { "b", HAS_ARG | OPT_VIDEO, {(void*)opt_video_bitrate}, "set video bitrate (in kbit/s)", "bitrate" },
  2585. { "r", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_rate}, "set frame rate (Hz value, fraction or abbreviation)", "rate" },
  2586. { "s", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" },
  2587. { "aspect", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_aspect_ratio}, "set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)", "aspect" },
  2588. { "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_frame_pix_fmt}, "set pixel format", "format" },
  2589. { "croptop", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop_top}, "set top crop band size (in pixels)", "size" },
  2590. { "cropbottom", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop_bottom}, "set bottom crop band size (in pixels)", "size" },
  2591. { "cropleft", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop_left}, "set left crop band size (in pixels)", "size" },
  2592. { "cropright", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop_right}, "set right crop band size (in pixels)", "size" },
  2593. { "g", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_gop_size}, "set the group of picture size", "gop_size" },
  2594. { "intra", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&intra_only}, "use only intra frames"},
  2595. { "vn", OPT_BOOL | OPT_VIDEO, {(void*)&video_disable}, "disable video" },
  2596. { "qscale", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qscale}, "use fixed video quantiser scale (VBR)", "q" },
  2597. { "qmin", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qmin}, "min video quantiser scale (VBR)", "q" },
  2598. { "qmax", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qmax}, "max video quantiser scale (VBR)", "q" },
  2599. { "mbqmin", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_mb_qmin}, "min macroblock quantiser scale (VBR)", "q" },
  2600. { "mbqmax", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_mb_qmax}, "max macroblock quantiser scale (VBR)", "q" },
  2601. { "qdiff", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qdiff}, "max difference between the quantiser scale (VBR)", "q" },
  2602. { "qblur", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qblur}, "video quantiser scale blur (VBR)", "blur" },
  2603. { "qcomp", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qcomp}, "video quantiser scale compression (VBR)", "compression" },
  2604. { "rc_init_cplx", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_rc_initial_cplx}, "initial complexity for 1-pass encoding", "complexity" },
  2605. { "b_qfactor", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_b_qfactor}, "qp factor between p and b frames", "factor" },
  2606. { "i_qfactor", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_i_qfactor}, "qp factor between p and i frames", "factor" },
  2607. { "b_qoffset", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_b_qoffset}, "qp offset between p and b frames", "offset" },
  2608. { "i_qoffset", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_i_qoffset}, "qp offset between p and i frames", "offset" },
  2609. // { "b_strategy", HAS_ARG | OPT_EXPERT, {(void*)opt_b_strategy}, "dynamic b frame selection strategy", "strategy" },
  2610. { "rc_eq", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_video_rc_eq}, "set rate control equation", "equation" },
  2611. { "rc_override", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_video_rc_override_string}, "rate control override for specific intervals", "override" },
  2612. { "bt", HAS_ARG | OPT_VIDEO, {(void*)opt_video_bitrate_tolerance}, "set video bitrate tolerance (in kbit/s)", "tolerance" },
  2613. { "maxrate", HAS_ARG | OPT_VIDEO, {(void*)opt_video_bitrate_max}, "set max video bitrate tolerance (in kbit/s)", "bitrate" },
  2614. { "minrate", HAS_ARG | OPT_VIDEO, {(void*)opt_video_bitrate_min}, "set min video bitrate tolerance (in kbit/s)", "bitrate" },
  2615. { "bufsize", HAS_ARG | OPT_VIDEO, {(void*)opt_video_buffer_size}, "set ratecontrol buffere size (in kbit)", "size" },
  2616. { "vcodec", HAS_ARG | OPT_VIDEO, {(void*)opt_video_codec}, "force video codec ('copy' to copy stream)", "codec" },
  2617. { "me", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_motion_estimation}, "set motion estimation method",
  2618. "method" },
  2619. { "dct_algo", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_dct_algo}, "set dct algo", "algo" },
  2620. { "idct_algo", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_idct_algo}, "set idct algo", "algo" },
  2621. { "er", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_error_resilience}, "set error resilience", "n" },
  2622. { "ec", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_error_concealment}, "set error concealment", "bit_mask" },
  2623. { "bf", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_b_frames}, "use 'frames' B frames", "frames" },
  2624. { "hq", OPT_BOOL, {(void*)&mb_decision}, "activate high quality settings" },
  2625. { "mbd", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_mb_decision}, "macroblock decision", "mode" },
  2626. { "mbcmp", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_mb_cmp}, "macroblock compare function", "cmp function" },
  2627. { "subcmp", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_sub_cmp}, "subpel compare function", "cmp function" },
  2628. { "cmp", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_cmp}, "fullpel compare function", "cmp function" },
  2629. { "4mv", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_4mv}, "use four motion vector by macroblock (MPEG4)" },
  2630. { "obmc", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_obmc}, "use overlapped block motion compensation (h263+)" },
  2631. { "part", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_part}, "use data partitioning (MPEG4)" },
  2632. { "bug", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_workaround_bugs}, "workaround not auto detected encoder bugs", "param" },
  2633. { "ps", HAS_ARG | OPT_EXPERT, {(void*)opt_packet_size}, "set packet size in bits", "size" },
  2634. { "error", HAS_ARG | OPT_EXPERT, {(void*)opt_error_rate}, "error rate", "rate" },
  2635. { "strict", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_strict}, "how strictly to follow the standarts", "strictness" },
  2636. { "sameq", OPT_BOOL | OPT_VIDEO, {(void*)&same_quality},
  2637. "use same video quality as source (implies VBR)" },
  2638. { "pass", HAS_ARG | OPT_VIDEO, {(void*)&opt_pass}, "select the pass number (1 or 2)", "n" },
  2639. { "passlogfile", HAS_ARG | OPT_STRING | OPT_VIDEO, {(void*)&pass_logfilename}, "select two pass log file name", "file" },
  2640. { "deinterlace", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&do_deinterlace},
  2641. "deinterlace pictures" },
  2642. { "interlace", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&do_interlace},
  2643. "force interlacing support in encoder (MPEG2/MPEG4)" },
  2644. { "psnr", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&do_psnr}, "calculate PSNR of compressed frames" },
  2645. { "vstats", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&do_vstats}, "dump video coding statistics to file" },
  2646. { "vhook", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)add_frame_hooker}, "insert video processing module", "module" },
  2647. { "aic", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_aic}, "enable Advanced intra coding (h263+)" },
  2648. { "aiv", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_aiv}, "enable Alternative inter vlc (h263+)" },
  2649. { "umv", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_umv}, "enable Unlimited Motion Vector (h263+)" },
  2650. { "intra_matrix", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_intra_matrix}, "specify intra matrix coeffs", "matrix" },
  2651. { "inter_matrix", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_inter_matrix}, "specify inter matrix coeffs", "matrix" },
  2652. /* audio options */
  2653. { "ab", HAS_ARG | OPT_AUDIO, {(void*)opt_audio_bitrate}, "set audio bitrate (in kbit/s)", "bitrate", },
  2654. { "ar", HAS_ARG | OPT_AUDIO, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" },
  2655. { "ac", HAS_ARG | OPT_AUDIO, {(void*)opt_audio_channels}, "set number of audio channels", "channels" },
  2656. { "an", OPT_BOOL | OPT_AUDIO, {(void*)&audio_disable}, "disable audio" },
  2657. { "acodec", HAS_ARG | OPT_AUDIO, {(void*)opt_audio_codec}, "force audio codec ('copy' to copy stream)", "codec" },
  2658. /* grab options */
  2659. { "vd", HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_GRAB, {(void*)opt_video_device}, "set video grab device", "device" },
  2660. { "vc", HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_GRAB, {(void*)opt_video_channel}, "set video grab channel (DV1394 only)", "channel" },
  2661. { "tvstd", HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_GRAB, {(void*)opt_video_standard}, "set television standard (NTSC, PAL (SECAM))", "standard" },
  2662. { "dv1394", OPT_EXPERT | OPT_GRAB, {(void*)opt_dv1394}, "set DV1394 grab", "" },
  2663. { "ad", HAS_ARG | OPT_EXPERT | OPT_AUDIO | OPT_GRAB, {(void*)opt_audio_device}, "set audio device", "device" },
  2664. { NULL, },
  2665. };
  2666. static void show_banner(void)
  2667. {
  2668. printf("ffmpeg version " FFMPEG_VERSION ", Copyright (c) 2000-2003 Fabrice Bellard\n");
  2669. }
  2670. static void show_license(void)
  2671. {
  2672. show_banner();
  2673. printf(
  2674. "This library is free software; you can redistribute it and/or\n"
  2675. "modify it under the terms of the GNU Lesser General Public\n"
  2676. "License as published by the Free Software Foundation; either\n"
  2677. "version 2 of the License, or (at your option) any later version.\n"
  2678. "\n"
  2679. "This library is distributed in the hope that it will be useful,\n"
  2680. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  2681. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
  2682. "Lesser General Public License for more details.\n"
  2683. "\n"
  2684. "You should have received a copy of the GNU Lesser General Public\n"
  2685. "License along with this library; if not, write to the Free Software\n"
  2686. "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n"
  2687. );
  2688. exit(1);
  2689. }
  2690. static void show_help(void)
  2691. {
  2692. show_banner();
  2693. printf("usage: ffmpeg [[options] -i input_file]... {[options] outfile}...\n"
  2694. "Hyper fast Audio and Video encoder\n");
  2695. printf("\n");
  2696. show_help_options(options, "Main options:\n",
  2697. OPT_EXPERT | OPT_AUDIO | OPT_VIDEO, 0);
  2698. show_help_options(options, "\nVideo options:\n",
  2699. OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_GRAB,
  2700. OPT_VIDEO);
  2701. show_help_options(options, "\nAdvanced Video options:\n",
  2702. OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_GRAB,
  2703. OPT_VIDEO | OPT_EXPERT);
  2704. show_help_options(options, "\nAudio options:\n",
  2705. OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_GRAB,
  2706. OPT_AUDIO);
  2707. show_help_options(options, "\nAdvanced Audio options:\n",
  2708. OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_GRAB,
  2709. OPT_AUDIO | OPT_EXPERT);
  2710. show_help_options(options, "\nAudio/Video grab options:\n",
  2711. OPT_GRAB,
  2712. OPT_GRAB);
  2713. show_help_options(options, "\nAdvanced options:\n",
  2714. OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_GRAB,
  2715. OPT_EXPERT);
  2716. exit(1);
  2717. }
  2718. void parse_arg_file(const char *filename)
  2719. {
  2720. opt_output_file(filename);
  2721. }
  2722. int main(int argc, char **argv)
  2723. {
  2724. int i;
  2725. int64_t ti;
  2726. av_register_all();
  2727. if (argc <= 1)
  2728. show_help();
  2729. /* parse options */
  2730. parse_options(argc, argv, options);
  2731. /* file converter / grab */
  2732. if (nb_output_files <= 0) {
  2733. fprintf(stderr, "Must supply at least one output file\n");
  2734. exit(1);
  2735. }
  2736. if (nb_input_files == 0) {
  2737. prepare_grab();
  2738. }
  2739. ti = getutime();
  2740. av_encode(output_files, nb_output_files, input_files, nb_input_files,
  2741. stream_maps, nb_stream_maps);
  2742. ti = getutime() - ti;
  2743. if (do_benchmark) {
  2744. printf("bench: utime=%0.3fs\n", ti / 1000000.0);
  2745. }
  2746. /* close files */
  2747. for(i=0;i<nb_output_files;i++) {
  2748. /* maybe av_close_output_file ??? */
  2749. AVFormatContext *s = output_files[i];
  2750. int j;
  2751. if (!(s->oformat->flags & AVFMT_NOFILE))
  2752. url_fclose(&s->pb);
  2753. for(j=0;j<s->nb_streams;j++)
  2754. av_free(s->streams[j]);
  2755. av_free(s);
  2756. }
  2757. for(i=0;i<nb_input_files;i++)
  2758. av_close_input_file(input_files[i]);
  2759. av_free_static();
  2760. if(intra_matrix)
  2761. av_free(intra_matrix);
  2762. if(inter_matrix)
  2763. av_free(inter_matrix);
  2764. #ifdef POWERPC_PERFORMANCE_REPORT
  2765. extern void powerpc_display_perf_report(void);
  2766. powerpc_display_perf_report();
  2767. #endif /* POWERPC_PERFORMANCE_REPORT */
  2768. #ifndef CONFIG_WIN32
  2769. if (received_sigterm) {
  2770. fprintf(stderr,
  2771. "Received signal %d: terminating.\n",
  2772. (int) received_sigterm);
  2773. exit (255);
  2774. }
  2775. #endif
  2776. exit(0); /* not all OS-es handle main() return value */
  2777. return 0;
  2778. }