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.

2852 lines
94KB

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