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.

3200 lines
106KB

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