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.

2796 lines
91KB

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