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.

3186 lines
105KB

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