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.

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