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.

3192 lines
105KB

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