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.

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