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.

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