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.

2826 lines
93KB

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