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.

3150 lines
104KB

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