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.

3230 lines
107KB

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