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.

2856 lines
94KB

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