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.

2895 lines
96KB

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