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.

3306 lines
109KB

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