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.

2880 lines
95KB

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