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.

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