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.

2833 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. static int using_stdin = 0;
  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. if ( !using_stdin )
  1014. fprintf(stderr, "Press [q] to stop encoding\n");
  1015. #endif
  1016. term_init();
  1017. stream_no_data = 0;
  1018. key = -1;
  1019. for(; received_sigterm == 0;) {
  1020. int file_index, ist_index;
  1021. AVPacket pkt;
  1022. uint8_t *ptr;
  1023. int len;
  1024. uint8_t *data_buf;
  1025. int data_size, got_picture;
  1026. AVPicture picture;
  1027. short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
  1028. void *buffer_to_free;
  1029. double pts_min;
  1030. redo:
  1031. /* if 'q' pressed, exits */
  1032. if (!using_stdin) {
  1033. /* read_key() returns 0 on EOF */
  1034. key = read_key();
  1035. if (key == 'q')
  1036. break;
  1037. }
  1038. /* select the stream that we must read now by looking at the
  1039. smallest output pts */
  1040. file_index = -1;
  1041. pts_min = 1e10;
  1042. for(i=0;i<nb_ostreams;i++) {
  1043. double pts;
  1044. ost = ost_table[i];
  1045. os = output_files[ost->file_index];
  1046. ist = ist_table[ost->source_index];
  1047. pts = (double)ost->st->pts.val * os->pts_num / os->pts_den;
  1048. if (!file_table[ist->file_index].eof_reached &&
  1049. pts < pts_min) {
  1050. pts_min = pts;
  1051. file_index = ist->file_index;
  1052. }
  1053. }
  1054. /* if none, if is finished */
  1055. if (file_index < 0) {
  1056. break;
  1057. }
  1058. /* finish if recording time exhausted */
  1059. if (recording_time > 0 && pts_min >= (recording_time / 1000000.0))
  1060. break;
  1061. /* read a packet from it and output it in the fifo */
  1062. is = input_files[file_index];
  1063. if (av_read_packet(is, &pkt) < 0) {
  1064. file_table[file_index].eof_reached = 1;
  1065. continue;
  1066. }
  1067. if (!pkt.size) {
  1068. stream_no_data = is;
  1069. } else {
  1070. stream_no_data = 0;
  1071. }
  1072. if (do_hex_dump) {
  1073. printf("stream #%d, size=%d:\n", pkt.stream_index, pkt.size);
  1074. av_hex_dump(pkt.data, pkt.size);
  1075. }
  1076. /* the following test is needed in case new streams appear
  1077. dynamically in stream : we ignore them */
  1078. if (pkt.stream_index >= file_table[file_index].nb_streams)
  1079. goto discard_packet;
  1080. ist_index = file_table[file_index].ist_index + pkt.stream_index;
  1081. ist = ist_table[ist_index];
  1082. if (ist->discard)
  1083. goto discard_packet;
  1084. // printf("read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size);
  1085. len = pkt.size;
  1086. ptr = pkt.data;
  1087. while (len > 0) {
  1088. /* decode the packet if needed */
  1089. data_buf = NULL; /* fail safe */
  1090. data_size = 0;
  1091. if (ist->decoding_needed) {
  1092. /* NOTE1: we only take into account the PTS if a new
  1093. frame has begun (MPEG semantics) */
  1094. /* NOTE2: even if the fraction is not initialized,
  1095. av_frac_set can be used to set the integer part */
  1096. if (ist->frame_decoded) {
  1097. /* If pts is unavailable -- we have to use synthetic one */
  1098. if( pkt.pts != AV_NOPTS_VALUE )
  1099. {
  1100. ist->pts = ist->next_pts.val = pkt.pts;
  1101. }
  1102. else
  1103. {
  1104. ist->pts = ist->next_pts.val;
  1105. }
  1106. ist->frame_decoded = 0;
  1107. }
  1108. switch(ist->st->codec.codec_type) {
  1109. case CODEC_TYPE_AUDIO:
  1110. /* XXX: could avoid copy if PCM 16 bits with same
  1111. endianness as CPU */
  1112. ret = avcodec_decode_audio(&ist->st->codec, samples, &data_size,
  1113. ptr, len);
  1114. if (ret < 0)
  1115. goto fail_decode;
  1116. /* Some bug in mpeg audio decoder gives */
  1117. /* data_size < 0, it seems they are overflows */
  1118. if (data_size <= 0) {
  1119. /* no audio frame */
  1120. ptr += ret;
  1121. len -= ret;
  1122. continue;
  1123. }
  1124. data_buf = (uint8_t *)samples;
  1125. av_frac_add(&ist->next_pts,
  1126. is->pts_den * data_size / (2 * ist->st->codec.channels));
  1127. break;
  1128. case CODEC_TYPE_VIDEO:
  1129. {
  1130. AVFrame big_picture;
  1131. data_size = (ist->st->codec.width * ist->st->codec.height * 3) / 2;
  1132. ret = avcodec_decode_video(&ist->st->codec,
  1133. &big_picture, &got_picture, ptr, len);
  1134. picture= *(AVPicture*)&big_picture;
  1135. ist->st->quality= big_picture.quality;
  1136. if (ret < 0) {
  1137. fail_decode:
  1138. fprintf(stderr, "Error while decoding stream #%d.%d\n",
  1139. ist->file_index, ist->index);
  1140. av_free_packet(&pkt);
  1141. goto redo;
  1142. }
  1143. if (!got_picture) {
  1144. /* no picture yet */
  1145. ptr += ret;
  1146. len -= ret;
  1147. continue;
  1148. }
  1149. av_frac_add(&ist->next_pts,
  1150. is->pts_den * ist->st->codec.frame_rate_base);
  1151. }
  1152. break;
  1153. default:
  1154. goto fail_decode;
  1155. }
  1156. } else {
  1157. data_buf = ptr;
  1158. data_size = len;
  1159. ret = len;
  1160. }
  1161. ptr += ret;
  1162. len -= ret;
  1163. buffer_to_free = 0;
  1164. if (ist->st->codec.codec_type == CODEC_TYPE_VIDEO) {
  1165. pre_process_video_frame(ist, &picture, &buffer_to_free);
  1166. }
  1167. ist->frame_decoded = 1;
  1168. /* frame rate emulation */
  1169. if (ist->st->codec.rate_emu) {
  1170. int64_t pts = av_rescale((int64_t) ist->frame * ist->st->codec.frame_rate_base, 1000000, ist->st->codec.frame_rate);
  1171. int64_t now = av_gettime() - ist->start;
  1172. if (pts > now)
  1173. usleep(pts - now);
  1174. ist->frame++;
  1175. }
  1176. #if 0
  1177. /* mpeg PTS deordering : if it is a P or I frame, the PTS
  1178. is the one of the next displayed one */
  1179. /* XXX: add mpeg4 too ? */
  1180. if (ist->st->codec.codec_id == CODEC_ID_MPEG1VIDEO) {
  1181. if (ist->st->codec.pict_type != B_TYPE) {
  1182. int64_t tmp;
  1183. tmp = ist->last_ip_pts;
  1184. ist->last_ip_pts = ist->frac_pts.val;
  1185. ist->frac_pts.val = tmp;
  1186. }
  1187. }
  1188. #endif
  1189. /* transcode raw format, encode packets and output them */
  1190. for(i=0;i<nb_ostreams;i++) {
  1191. int frame_size;
  1192. ost = ost_table[i];
  1193. if (ost->source_index == ist_index) {
  1194. os = output_files[ost->file_index];
  1195. #if 0
  1196. printf("%d: got pts=%f %f\n", i, pkt.pts / 90000.0,
  1197. (ist->pts - ost->st->pts.val) / 90000.0);
  1198. #endif
  1199. /* set the input output pts pairs */
  1200. ost->sync_ipts = (double)ist->pts * is->pts_num /
  1201. is->pts_den;
  1202. /* XXX: take into account the various fifos,
  1203. in particular for audio */
  1204. ost->sync_opts = ost->st->pts.val;
  1205. //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);
  1206. if (ost->encoding_needed) {
  1207. switch(ost->st->codec.codec_type) {
  1208. case CODEC_TYPE_AUDIO:
  1209. do_audio_out(os, ost, ist, data_buf, data_size);
  1210. break;
  1211. case CODEC_TYPE_VIDEO:
  1212. /* find an audio stream for synchro */
  1213. {
  1214. int i;
  1215. AVOutputStream *audio_sync, *ost1;
  1216. audio_sync = NULL;
  1217. for(i=0;i<nb_ostreams;i++) {
  1218. ost1 = ost_table[i];
  1219. if (ost1->file_index == ost->file_index &&
  1220. ost1->st->codec.codec_type == CODEC_TYPE_AUDIO) {
  1221. audio_sync = ost1;
  1222. break;
  1223. }
  1224. }
  1225. do_video_out(os, ost, ist, &picture, &frame_size, audio_sync);
  1226. if (do_vstats && frame_size)
  1227. do_video_stats(os, ost, frame_size);
  1228. }
  1229. break;
  1230. default:
  1231. av_abort();
  1232. }
  1233. } else {
  1234. AVFrame avframe;
  1235. /* no reencoding needed : output the packet directly */
  1236. /* force the input stream PTS */
  1237. memset(&avframe, 0, sizeof(AVFrame));
  1238. ost->st->codec.coded_frame= &avframe;
  1239. avframe.key_frame = pkt.flags & PKT_FLAG_KEY;
  1240. av_write_frame(os, ost->index, data_buf, data_size);
  1241. ost->st->codec.frame_number++;
  1242. ost->frame_number++;
  1243. }
  1244. }
  1245. }
  1246. av_free(buffer_to_free);
  1247. }
  1248. discard_packet:
  1249. av_free_packet(&pkt);
  1250. /* dump report by using the output first video and audio streams */
  1251. print_report(output_files, ost_table, nb_ostreams, 0);
  1252. }
  1253. term_exit();
  1254. /* dump report by using the first video and audio streams */
  1255. print_report(output_files, ost_table, nb_ostreams, 1);
  1256. /* close each encoder */
  1257. for(i=0;i<nb_ostreams;i++) {
  1258. ost = ost_table[i];
  1259. if (ost->encoding_needed) {
  1260. av_freep(&ost->st->codec.stats_in);
  1261. avcodec_close(&ost->st->codec);
  1262. }
  1263. }
  1264. /* close each decoder */
  1265. for(i=0;i<nb_istreams;i++) {
  1266. ist = ist_table[i];
  1267. if (ist->decoding_needed) {
  1268. avcodec_close(&ist->st->codec);
  1269. }
  1270. }
  1271. /* write the trailer if needed and close file */
  1272. for(i=0;i<nb_output_files;i++) {
  1273. os = output_files[i];
  1274. av_write_trailer(os);
  1275. }
  1276. /* finished ! */
  1277. ret = 0;
  1278. fail1:
  1279. av_free(file_table);
  1280. if (ist_table) {
  1281. for(i=0;i<nb_istreams;i++) {
  1282. ist = ist_table[i];
  1283. av_free(ist);
  1284. }
  1285. av_free(ist_table);
  1286. }
  1287. if (ost_table) {
  1288. for(i=0;i<nb_ostreams;i++) {
  1289. ost = ost_table[i];
  1290. if (ost) {
  1291. if (ost->logfile) {
  1292. fclose(ost->logfile);
  1293. ost->logfile = NULL;
  1294. }
  1295. fifo_free(&ost->fifo); /* works even if fifo is not
  1296. initialized but set to zero */
  1297. av_free(ost->pict_tmp.data[0]);
  1298. if (ost->video_resample)
  1299. img_resample_close(ost->img_resample_ctx);
  1300. if (ost->audio_resample)
  1301. audio_resample_close(ost->resample);
  1302. av_free(ost);
  1303. }
  1304. }
  1305. av_free(ost_table);
  1306. }
  1307. return ret;
  1308. fail:
  1309. ret = -ENOMEM;
  1310. goto fail1;
  1311. }
  1312. #if 0
  1313. int file_read(const char *filename)
  1314. {
  1315. URLContext *h;
  1316. unsigned char buffer[1024];
  1317. int len, i;
  1318. if (url_open(&h, filename, O_RDONLY) < 0) {
  1319. printf("could not open '%s'\n", filename);
  1320. return -1;
  1321. }
  1322. for(;;) {
  1323. len = url_read(h, buffer, sizeof(buffer));
  1324. if (len <= 0)
  1325. break;
  1326. for(i=0;i<len;i++) putchar(buffer[i]);
  1327. }
  1328. url_close(h);
  1329. return 0;
  1330. }
  1331. #endif
  1332. static void opt_image_format(const char *arg)
  1333. {
  1334. AVImageFormat *f;
  1335. for(f = first_image_format; f != NULL; f = f->next) {
  1336. if (!strcmp(arg, f->name))
  1337. break;
  1338. }
  1339. if (!f) {
  1340. fprintf(stderr, "Unknown image format: '%s'\n", arg);
  1341. exit(1);
  1342. }
  1343. image_format = f;
  1344. }
  1345. static void opt_format(const char *arg)
  1346. {
  1347. /* compatibility stuff for pgmyuv */
  1348. if (!strcmp(arg, "pgmyuv")) {
  1349. opt_image_format(arg);
  1350. arg = "image";
  1351. }
  1352. file_iformat = av_find_input_format(arg);
  1353. file_oformat = guess_format(arg, NULL, NULL);
  1354. if (!file_iformat && !file_oformat) {
  1355. fprintf(stderr, "Unknown input or output format: %s\n", arg);
  1356. exit(1);
  1357. }
  1358. }
  1359. static void opt_video_bitrate(const char *arg)
  1360. {
  1361. video_bit_rate = atoi(arg) * 1000;
  1362. }
  1363. static void opt_video_bitrate_tolerance(const char *arg)
  1364. {
  1365. video_bit_rate_tolerance = atoi(arg) * 1000;
  1366. }
  1367. static void opt_video_bitrate_max(const char *arg)
  1368. {
  1369. video_rc_max_rate = atoi(arg) * 1000;
  1370. }
  1371. static void opt_video_bitrate_min(const char *arg)
  1372. {
  1373. video_rc_min_rate = atoi(arg) * 1000;
  1374. }
  1375. static void opt_video_buffer_size(const char *arg)
  1376. {
  1377. video_rc_buffer_size = atoi(arg) * 1000;
  1378. }
  1379. static void opt_video_rc_eq(char *arg)
  1380. {
  1381. video_rc_eq = arg;
  1382. }
  1383. static void opt_video_rc_override_string(char *arg)
  1384. {
  1385. video_rc_override_string = arg;
  1386. }
  1387. static void opt_workaround_bugs(const char *arg)
  1388. {
  1389. workaround_bugs = atoi(arg);
  1390. }
  1391. static void opt_dct_algo(const char *arg)
  1392. {
  1393. dct_algo = atoi(arg);
  1394. }
  1395. static void opt_idct_algo(const char *arg)
  1396. {
  1397. idct_algo = atoi(arg);
  1398. }
  1399. static void opt_error_resilience(const char *arg)
  1400. {
  1401. error_resilience = atoi(arg);
  1402. }
  1403. static void opt_error_concealment(const char *arg)
  1404. {
  1405. error_concealment = atoi(arg);
  1406. }
  1407. static void opt_debug(const char *arg)
  1408. {
  1409. debug = atoi(arg);
  1410. }
  1411. static void opt_frame_rate(const char *arg)
  1412. {
  1413. if (parse_frame_rate(&frame_rate, &frame_rate_base, arg) < 0) {
  1414. fprintf(stderr, "Incorrect frame rate\n");
  1415. exit(1);
  1416. }
  1417. }
  1418. static void opt_frame_crop_top(const char *arg)
  1419. {
  1420. frame_topBand = atoi(arg);
  1421. if (frame_topBand < 0) {
  1422. fprintf(stderr, "Incorrect top crop size\n");
  1423. exit(1);
  1424. }
  1425. if ((frame_topBand % 2) != 0) {
  1426. fprintf(stderr, "Top crop size must be a multiple of 2\n");
  1427. exit(1);
  1428. }
  1429. if ((frame_topBand) >= frame_height){
  1430. fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
  1431. exit(1);
  1432. }
  1433. frame_height -= frame_topBand;
  1434. }
  1435. static void opt_frame_crop_bottom(const char *arg)
  1436. {
  1437. frame_bottomBand = atoi(arg);
  1438. if (frame_bottomBand < 0) {
  1439. fprintf(stderr, "Incorrect bottom crop size\n");
  1440. exit(1);
  1441. }
  1442. if ((frame_bottomBand % 2) != 0) {
  1443. fprintf(stderr, "Bottom crop size must be a multiple of 2\n");
  1444. exit(1);
  1445. }
  1446. if ((frame_bottomBand) >= frame_height){
  1447. fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
  1448. exit(1);
  1449. }
  1450. frame_height -= frame_bottomBand;
  1451. }
  1452. static void opt_frame_crop_left(const char *arg)
  1453. {
  1454. frame_leftBand = atoi(arg);
  1455. if (frame_leftBand < 0) {
  1456. fprintf(stderr, "Incorrect left crop size\n");
  1457. exit(1);
  1458. }
  1459. if ((frame_leftBand % 2) != 0) {
  1460. fprintf(stderr, "Left crop size must be a multiple of 2\n");
  1461. exit(1);
  1462. }
  1463. if ((frame_leftBand) >= frame_width){
  1464. fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
  1465. exit(1);
  1466. }
  1467. frame_width -= frame_leftBand;
  1468. }
  1469. static void opt_frame_crop_right(const char *arg)
  1470. {
  1471. frame_rightBand = atoi(arg);
  1472. if (frame_rightBand < 0) {
  1473. fprintf(stderr, "Incorrect right crop size\n");
  1474. exit(1);
  1475. }
  1476. if ((frame_rightBand % 2) != 0) {
  1477. fprintf(stderr, "Right crop size must be a multiple of 2\n");
  1478. exit(1);
  1479. }
  1480. if ((frame_rightBand) >= frame_width){
  1481. fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
  1482. exit(1);
  1483. }
  1484. frame_width -= frame_rightBand;
  1485. }
  1486. static void opt_frame_size(const char *arg)
  1487. {
  1488. if (parse_image_size(&frame_width, &frame_height, arg) < 0) {
  1489. fprintf(stderr, "Incorrect frame size\n");
  1490. exit(1);
  1491. }
  1492. if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
  1493. fprintf(stderr, "Frame size must be a multiple of 2\n");
  1494. exit(1);
  1495. }
  1496. }
  1497. static void opt_frame_pix_fmt(const char *arg)
  1498. {
  1499. frame_pix_fmt = avcodec_get_pix_fmt(arg);
  1500. }
  1501. static void opt_frame_aspect_ratio(const char *arg)
  1502. {
  1503. int x = 0, y = 0;
  1504. double ar = 0;
  1505. const char *p;
  1506. p = strchr(arg, ':');
  1507. if (p) {
  1508. x = strtol(arg, (char **)&arg, 10);
  1509. if (arg == p)
  1510. y = strtol(arg+1, (char **)&arg, 10);
  1511. if (x > 0 && y > 0)
  1512. ar = (double)x / (double)y;
  1513. } else
  1514. ar = strtod(arg, (char **)&arg);
  1515. if (!ar) {
  1516. fprintf(stderr, "Incorrect aspect ratio specification.\n");
  1517. exit(1);
  1518. }
  1519. frame_aspect_ratio = ar;
  1520. }
  1521. static void opt_gop_size(const char *arg)
  1522. {
  1523. gop_size = atoi(arg);
  1524. }
  1525. static void opt_b_frames(const char *arg)
  1526. {
  1527. b_frames = atoi(arg);
  1528. if (b_frames > FF_MAX_B_FRAMES) {
  1529. fprintf(stderr, "\nCannot have more than %d B frames, increase FF_MAX_B_FRAMES.\n", FF_MAX_B_FRAMES);
  1530. exit(1);
  1531. } else if (b_frames < 1) {
  1532. fprintf(stderr, "\nNumber of B frames must be higher than 0\n");
  1533. exit(1);
  1534. }
  1535. }
  1536. static void opt_mb_decision(const char *arg)
  1537. {
  1538. mb_decision = atoi(arg);
  1539. }
  1540. static void opt_qscale(const char *arg)
  1541. {
  1542. video_qscale = atoi(arg);
  1543. if (video_qscale < 0 ||
  1544. video_qscale > 31) {
  1545. fprintf(stderr, "qscale must be >= 1 and <= 31\n");
  1546. exit(1);
  1547. }
  1548. }
  1549. static void opt_qmin(const char *arg)
  1550. {
  1551. video_qmin = atoi(arg);
  1552. if (video_qmin < 0 ||
  1553. video_qmin > 31) {
  1554. fprintf(stderr, "qmin must be >= 1 and <= 31\n");
  1555. exit(1);
  1556. }
  1557. }
  1558. static void opt_qmax(const char *arg)
  1559. {
  1560. video_qmax = atoi(arg);
  1561. if (video_qmax < 0 ||
  1562. video_qmax > 31) {
  1563. fprintf(stderr, "qmax must be >= 1 and <= 31\n");
  1564. exit(1);
  1565. }
  1566. }
  1567. static void opt_mb_qmin(const char *arg)
  1568. {
  1569. video_mb_qmin = atoi(arg);
  1570. if (video_mb_qmin < 0 ||
  1571. video_mb_qmin > 31) {
  1572. fprintf(stderr, "qmin must be >= 1 and <= 31\n");
  1573. exit(1);
  1574. }
  1575. }
  1576. static void opt_mb_qmax(const char *arg)
  1577. {
  1578. video_mb_qmax = atoi(arg);
  1579. if (video_mb_qmax < 0 ||
  1580. video_mb_qmax > 31) {
  1581. fprintf(stderr, "qmax must be >= 1 and <= 31\n");
  1582. exit(1);
  1583. }
  1584. }
  1585. static void opt_qdiff(const char *arg)
  1586. {
  1587. video_qdiff = atoi(arg);
  1588. if (video_qdiff < 0 ||
  1589. video_qdiff > 31) {
  1590. fprintf(stderr, "qdiff must be >= 1 and <= 31\n");
  1591. exit(1);
  1592. }
  1593. }
  1594. static void opt_qblur(const char *arg)
  1595. {
  1596. video_qblur = atof(arg);
  1597. }
  1598. static void opt_qcomp(const char *arg)
  1599. {
  1600. video_qcomp = atof(arg);
  1601. }
  1602. static void opt_rc_initial_cplx(const char *arg)
  1603. {
  1604. video_rc_initial_cplx = atof(arg);
  1605. }
  1606. static void opt_b_qfactor(const char *arg)
  1607. {
  1608. video_b_qfactor = atof(arg);
  1609. }
  1610. static void opt_i_qfactor(const char *arg)
  1611. {
  1612. video_i_qfactor = atof(arg);
  1613. }
  1614. static void opt_b_qoffset(const char *arg)
  1615. {
  1616. video_b_qoffset = atof(arg);
  1617. }
  1618. static void opt_i_qoffset(const char *arg)
  1619. {
  1620. video_i_qoffset = atof(arg);
  1621. }
  1622. static void opt_packet_size(const char *arg)
  1623. {
  1624. packet_size= atoi(arg);
  1625. }
  1626. static void opt_strict(const char *arg)
  1627. {
  1628. strict= atoi(arg);
  1629. }
  1630. static void opt_audio_bitrate(const char *arg)
  1631. {
  1632. audio_bit_rate = atoi(arg) * 1000;
  1633. }
  1634. static void opt_audio_rate(const char *arg)
  1635. {
  1636. audio_sample_rate = atoi(arg);
  1637. }
  1638. static void opt_audio_channels(const char *arg)
  1639. {
  1640. audio_channels = atoi(arg);
  1641. }
  1642. static void opt_video_device(const char *arg)
  1643. {
  1644. video_device = av_strdup(arg);
  1645. }
  1646. static void opt_video_channel(const char *arg)
  1647. {
  1648. video_channel = strtol(arg, NULL, 0);
  1649. }
  1650. static void opt_video_standard(const char *arg)
  1651. {
  1652. video_standard = av_strdup(arg);
  1653. }
  1654. static void opt_audio_device(const char *arg)
  1655. {
  1656. audio_device = av_strdup(arg);
  1657. }
  1658. static void opt_dv1394(const char *arg)
  1659. {
  1660. video_grab_format = "dv1394";
  1661. audio_grab_format = NULL;
  1662. }
  1663. static void opt_audio_codec(const char *arg)
  1664. {
  1665. AVCodec *p;
  1666. if (!strcmp(arg, "copy")) {
  1667. audio_stream_copy = 1;
  1668. } else {
  1669. p = first_avcodec;
  1670. while (p) {
  1671. if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
  1672. break;
  1673. p = p->next;
  1674. }
  1675. if (p == NULL) {
  1676. fprintf(stderr, "Unknown audio codec '%s'\n", arg);
  1677. exit(1);
  1678. } else {
  1679. audio_codec_id = p->id;
  1680. }
  1681. }
  1682. }
  1683. static void add_frame_hooker(const char *arg)
  1684. {
  1685. int argc = 0;
  1686. char *argv[64];
  1687. int i;
  1688. char *args = av_strdup(arg);
  1689. argv[0] = strtok(args, " ");
  1690. while (argc < 62 && (argv[++argc] = strtok(NULL, " "))) {
  1691. }
  1692. i = frame_hook_add(argc, argv);
  1693. if (i != 0) {
  1694. fprintf(stderr, "Failed to add video hook function: %s\n", arg);
  1695. exit(1);
  1696. }
  1697. }
  1698. const char *motion_str[] = {
  1699. "zero",
  1700. "full",
  1701. "log",
  1702. "phods",
  1703. "epzs",
  1704. "x1",
  1705. NULL,
  1706. };
  1707. static void opt_motion_estimation(const char *arg)
  1708. {
  1709. const char **p;
  1710. p = motion_str;
  1711. for(;;) {
  1712. if (!*p) {
  1713. fprintf(stderr, "Unknown motion estimation method '%s'\n", arg);
  1714. exit(1);
  1715. }
  1716. if (!strcmp(*p, arg))
  1717. break;
  1718. p++;
  1719. }
  1720. me_method = (p - motion_str) + 1;
  1721. }
  1722. static void opt_video_codec(const char *arg)
  1723. {
  1724. AVCodec *p;
  1725. if (!strcmp(arg, "copy")) {
  1726. video_stream_copy = 1;
  1727. } else {
  1728. p = first_avcodec;
  1729. while (p) {
  1730. if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
  1731. break;
  1732. p = p->next;
  1733. }
  1734. if (p == NULL) {
  1735. fprintf(stderr, "Unknown video codec '%s'\n", arg);
  1736. exit(1);
  1737. } else {
  1738. video_codec_id = p->id;
  1739. }
  1740. }
  1741. }
  1742. static void opt_map(const char *arg)
  1743. {
  1744. AVStreamMap *m;
  1745. const char *p;
  1746. p = arg;
  1747. m = &stream_maps[nb_stream_maps++];
  1748. m->file_index = strtol(arg, (char **)&p, 0);
  1749. if (*p)
  1750. p++;
  1751. m->stream_index = strtol(p, (char **)&p, 0);
  1752. }
  1753. static void opt_recording_time(const char *arg)
  1754. {
  1755. recording_time = parse_date(arg, 1);
  1756. }
  1757. static void opt_input_file(const char *filename)
  1758. {
  1759. AVFormatContext *ic;
  1760. AVFormatParameters params, *ap = &params;
  1761. int err, i, ret, rfps, rfps_base;
  1762. if (!strcmp(filename, "-"))
  1763. filename = "pipe:";
  1764. using_stdin |= !strcmp(filename, "pipe:" ) ||
  1765. !strcmp( filename, "/dev/stdin" );
  1766. /* get default parameters from command line */
  1767. memset(ap, 0, sizeof(*ap));
  1768. ap->sample_rate = audio_sample_rate;
  1769. ap->channels = audio_channels;
  1770. ap->frame_rate = frame_rate;
  1771. ap->frame_rate_base = frame_rate_base;
  1772. ap->width = frame_width;
  1773. ap->height = frame_height;
  1774. ap->image_format = image_format;
  1775. ap->pix_fmt = frame_pix_fmt;
  1776. /* open the input file with generic libav function */
  1777. err = av_open_input_file(&ic, filename, file_iformat, 0, ap);
  1778. if (err < 0) {
  1779. print_error(filename, err);
  1780. exit(1);
  1781. }
  1782. /* If not enough info to get the stream parameters, we decode the
  1783. first frames to get it. (used in mpeg case for example) */
  1784. ret = av_find_stream_info(ic);
  1785. if (ret < 0) {
  1786. fprintf(stderr, "%s: could not find codec parameters\n", filename);
  1787. exit(1);
  1788. }
  1789. /* update the current parameters so that they match the one of the input stream */
  1790. for(i=0;i<ic->nb_streams;i++) {
  1791. AVCodecContext *enc = &ic->streams[i]->codec;
  1792. switch(enc->codec_type) {
  1793. case CODEC_TYPE_AUDIO:
  1794. //fprintf(stderr, "\nInput Audio channels: %d", enc->channels);
  1795. audio_channels = enc->channels;
  1796. audio_sample_rate = enc->sample_rate;
  1797. break;
  1798. case CODEC_TYPE_VIDEO:
  1799. frame_height = enc->height;
  1800. frame_width = enc->width;
  1801. frame_aspect_ratio = enc->aspect_ratio;
  1802. frame_pix_fmt = enc->pix_fmt;
  1803. rfps = ic->streams[i]->r_frame_rate;
  1804. rfps_base = ic->streams[i]->r_frame_rate_base;
  1805. enc->workaround_bugs = workaround_bugs;
  1806. enc->error_resilience = error_resilience;
  1807. enc->error_concealment = error_concealment;
  1808. enc->idct_algo= idct_algo;
  1809. enc->debug= debug;
  1810. /* if(enc->codec->capabilities & CODEC_CAP_TRUNCATED)
  1811. enc->flags|= CODEC_FLAG_TRUNCATED; */
  1812. if(/*enc->codec_id==CODEC_ID_MPEG4 || */enc->codec_id==CODEC_ID_MPEG1VIDEO || enc->codec_id==CODEC_ID_H264)
  1813. enc->flags|= CODEC_FLAG_TRUNCATED;
  1814. if(bitexact)
  1815. enc->flags|= CODEC_FLAG_BITEXACT;
  1816. assert(enc->frame_rate_base == rfps_base); // should be true for now
  1817. if (enc->frame_rate != rfps) {
  1818. fprintf(stderr,"\nSeems that stream %d comes from film source: %2.2f->%2.2f\n",
  1819. i, (float)enc->frame_rate / enc->frame_rate_base,
  1820. (float)rfps / rfps_base);
  1821. }
  1822. /* update the current frame rate to match the stream frame rate */
  1823. frame_rate = rfps;
  1824. frame_rate_base = rfps_base;
  1825. enc->rate_emu = rate_emu;
  1826. break;
  1827. default:
  1828. av_abort();
  1829. }
  1830. }
  1831. input_files[nb_input_files] = ic;
  1832. /* dump the file content */
  1833. dump_format(ic, nb_input_files, filename, 0);
  1834. nb_input_files++;
  1835. file_iformat = NULL;
  1836. file_oformat = NULL;
  1837. image_format = NULL;
  1838. rate_emu = 0;
  1839. }
  1840. static void check_audio_video_inputs(int *has_video_ptr, int *has_audio_ptr)
  1841. {
  1842. int has_video, has_audio, i, j;
  1843. AVFormatContext *ic;
  1844. has_video = 0;
  1845. has_audio = 0;
  1846. for(j=0;j<nb_input_files;j++) {
  1847. ic = input_files[j];
  1848. for(i=0;i<ic->nb_streams;i++) {
  1849. AVCodecContext *enc = &ic->streams[i]->codec;
  1850. switch(enc->codec_type) {
  1851. case CODEC_TYPE_AUDIO:
  1852. has_audio = 1;
  1853. break;
  1854. case CODEC_TYPE_VIDEO:
  1855. has_video = 1;
  1856. break;
  1857. default:
  1858. av_abort();
  1859. }
  1860. }
  1861. }
  1862. *has_video_ptr = has_video;
  1863. *has_audio_ptr = has_audio;
  1864. }
  1865. static void opt_output_file(const char *filename)
  1866. {
  1867. AVStream *st;
  1868. AVFormatContext *oc;
  1869. int use_video, use_audio, nb_streams, input_has_video, input_has_audio;
  1870. int codec_id;
  1871. AVFormatParameters params, *ap = &params;
  1872. if (!strcmp(filename, "-"))
  1873. filename = "pipe:";
  1874. oc = av_mallocz(sizeof(AVFormatContext));
  1875. if (!file_oformat) {
  1876. file_oformat = guess_format(NULL, filename, NULL);
  1877. if (!file_oformat) {
  1878. fprintf(stderr, "Unable for find a suitable output format for '%s'\n",
  1879. filename);
  1880. exit(1);
  1881. }
  1882. }
  1883. oc->oformat = file_oformat;
  1884. if (!strcmp(file_oformat->name, "ffm") &&
  1885. strstart(filename, "http:", NULL)) {
  1886. /* special case for files sent to ffserver: we get the stream
  1887. parameters from ffserver */
  1888. if (read_ffserver_streams(oc, filename) < 0) {
  1889. fprintf(stderr, "Could not read stream parameters from '%s'\n", filename);
  1890. exit(1);
  1891. }
  1892. } else {
  1893. use_video = file_oformat->video_codec != CODEC_ID_NONE;
  1894. use_audio = file_oformat->audio_codec != CODEC_ID_NONE;
  1895. /* disable if no corresponding type found and at least one
  1896. input file */
  1897. if (nb_input_files > 0) {
  1898. check_audio_video_inputs(&input_has_video, &input_has_audio);
  1899. if (!input_has_video)
  1900. use_video = 0;
  1901. if (!input_has_audio)
  1902. use_audio = 0;
  1903. }
  1904. /* manual disable */
  1905. if (audio_disable) {
  1906. use_audio = 0;
  1907. }
  1908. if (video_disable) {
  1909. use_video = 0;
  1910. }
  1911. nb_streams = 0;
  1912. if (use_video) {
  1913. AVCodecContext *video_enc;
  1914. st = av_mallocz(sizeof(AVStream));
  1915. if (!st) {
  1916. fprintf(stderr, "Could not alloc stream\n");
  1917. exit(1);
  1918. }
  1919. avcodec_get_context_defaults(&st->codec);
  1920. video_enc = &st->codec;
  1921. if (video_stream_copy) {
  1922. st->stream_copy = 1;
  1923. video_enc->codec_type = CODEC_TYPE_VIDEO;
  1924. } else {
  1925. char *p;
  1926. int i;
  1927. codec_id = file_oformat->video_codec;
  1928. if (video_codec_id != CODEC_ID_NONE)
  1929. codec_id = video_codec_id;
  1930. video_enc->codec_id = codec_id;
  1931. video_enc->bit_rate = video_bit_rate;
  1932. video_enc->bit_rate_tolerance = video_bit_rate_tolerance;
  1933. video_enc->frame_rate = frame_rate;
  1934. video_enc->frame_rate_base = frame_rate_base;
  1935. video_enc->width = frame_width;
  1936. video_enc->height = frame_height;
  1937. video_enc->aspect_ratio = frame_aspect_ratio;
  1938. video_enc->pix_fmt = frame_pix_fmt;
  1939. if (!intra_only)
  1940. video_enc->gop_size = gop_size;
  1941. else
  1942. video_enc->gop_size = 0;
  1943. if (video_qscale || same_quality) {
  1944. video_enc->flags |= CODEC_FLAG_QSCALE;
  1945. st->quality = video_qscale;
  1946. }
  1947. if(bitexact)
  1948. video_enc->flags |= CODEC_FLAG_BITEXACT;
  1949. video_enc->mb_decision = mb_decision;
  1950. if (use_umv) {
  1951. video_enc->flags |= CODEC_FLAG_H263P_UMV;
  1952. }
  1953. if (use_aic) {
  1954. video_enc->flags |= CODEC_FLAG_H263P_AIC;
  1955. }
  1956. if (use_4mv) {
  1957. video_enc->mb_decision = FF_MB_DECISION_BITS; //FIXME remove
  1958. video_enc->flags |= CODEC_FLAG_4MV;
  1959. }
  1960. if(use_part)
  1961. video_enc->flags |= CODEC_FLAG_PART;
  1962. if (b_frames) {
  1963. video_enc->max_b_frames = b_frames;
  1964. video_enc->b_frame_strategy = 0;
  1965. video_enc->b_quant_factor = 2.0;
  1966. }
  1967. video_enc->qmin = video_qmin;
  1968. video_enc->qmax = video_qmax;
  1969. video_enc->mb_qmin = video_mb_qmin;
  1970. video_enc->mb_qmax = video_mb_qmax;
  1971. video_enc->max_qdiff = video_qdiff;
  1972. video_enc->qblur = video_qblur;
  1973. video_enc->qcompress = video_qcomp;
  1974. video_enc->rc_eq = video_rc_eq;
  1975. video_enc->debug= debug;
  1976. p= video_rc_override_string;
  1977. for(i=0; p; i++){
  1978. int start, end, q;
  1979. int e=sscanf(p, "%d,%d,%d", &start, &end, &q);
  1980. if(e!=3){
  1981. fprintf(stderr, "error parsing rc_override\n");
  1982. exit(1);
  1983. }
  1984. video_enc->rc_override=
  1985. av_realloc(video_enc->rc_override,
  1986. sizeof(RcOverride)*(i+1));
  1987. video_enc->rc_override[i].start_frame= start;
  1988. video_enc->rc_override[i].end_frame = end;
  1989. if(q>0){
  1990. video_enc->rc_override[i].qscale= q;
  1991. video_enc->rc_override[i].quality_factor= 1.0;
  1992. }
  1993. else{
  1994. video_enc->rc_override[i].qscale= 0;
  1995. video_enc->rc_override[i].quality_factor= -q/100.0;
  1996. }
  1997. p= strchr(p, '/');
  1998. if(p) p++;
  1999. }
  2000. video_enc->rc_override_count=i;
  2001. video_enc->rc_max_rate = video_rc_max_rate;
  2002. video_enc->rc_min_rate = video_rc_min_rate;
  2003. video_enc->rc_buffer_size = video_rc_buffer_size;
  2004. video_enc->rc_buffer_aggressivity= video_rc_buffer_aggressivity;
  2005. video_enc->rc_initial_cplx= video_rc_initial_cplx;
  2006. video_enc->i_quant_factor = video_i_qfactor;
  2007. video_enc->b_quant_factor = video_b_qfactor;
  2008. video_enc->i_quant_offset = video_i_qoffset;
  2009. video_enc->b_quant_offset = video_b_qoffset;
  2010. video_enc->dct_algo = dct_algo;
  2011. video_enc->idct_algo = idct_algo;
  2012. video_enc->strict_std_compliance = strict;
  2013. if(packet_size){
  2014. video_enc->rtp_mode= 1;
  2015. video_enc->rtp_payload_size= packet_size;
  2016. }
  2017. if (do_psnr)
  2018. video_enc->flags|= CODEC_FLAG_PSNR;
  2019. video_enc->me_method = me_method;
  2020. /* two pass mode */
  2021. if (do_pass) {
  2022. if (do_pass == 1) {
  2023. video_enc->flags |= CODEC_FLAG_PASS1;
  2024. } else {
  2025. video_enc->flags |= CODEC_FLAG_PASS2;
  2026. }
  2027. }
  2028. }
  2029. oc->streams[nb_streams] = st;
  2030. nb_streams++;
  2031. }
  2032. if (use_audio) {
  2033. AVCodecContext *audio_enc;
  2034. st = av_mallocz(sizeof(AVStream));
  2035. if (!st) {
  2036. fprintf(stderr, "Could not alloc stream\n");
  2037. exit(1);
  2038. }
  2039. avcodec_get_context_defaults(&st->codec);
  2040. audio_enc = &st->codec;
  2041. audio_enc->codec_type = CODEC_TYPE_AUDIO;
  2042. if (audio_stream_copy) {
  2043. st->stream_copy = 1;
  2044. } else {
  2045. codec_id = file_oformat->audio_codec;
  2046. if (audio_codec_id != CODEC_ID_NONE)
  2047. codec_id = audio_codec_id;
  2048. audio_enc->codec_id = codec_id;
  2049. audio_enc->bit_rate = audio_bit_rate;
  2050. audio_enc->sample_rate = audio_sample_rate;
  2051. /* For audio codecs other than AC3 we limit */
  2052. /* the number of coded channels to stereo */
  2053. if (audio_channels > 2 && codec_id != CODEC_ID_AC3) {
  2054. audio_enc->channels = 2;
  2055. } else
  2056. audio_enc->channels = audio_channels;
  2057. }
  2058. oc->streams[nb_streams] = st;
  2059. nb_streams++;
  2060. }
  2061. oc->nb_streams = nb_streams;
  2062. if (!nb_streams) {
  2063. fprintf(stderr, "No audio or video streams available\n");
  2064. exit(1);
  2065. }
  2066. if (str_title)
  2067. pstrcpy(oc->title, sizeof(oc->title), str_title);
  2068. if (str_author)
  2069. pstrcpy(oc->author, sizeof(oc->author), str_author);
  2070. if (str_copyright)
  2071. pstrcpy(oc->copyright, sizeof(oc->copyright), str_copyright);
  2072. if (str_comment)
  2073. pstrcpy(oc->comment, sizeof(oc->comment), str_comment);
  2074. }
  2075. output_files[nb_output_files++] = oc;
  2076. strcpy(oc->filename, filename);
  2077. /* check filename in case of an image number is expected */
  2078. if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
  2079. if (filename_number_test(oc->filename) < 0) {
  2080. print_error(oc->filename, AVERROR_NUMEXPECTED);
  2081. exit(1);
  2082. }
  2083. }
  2084. if (!(oc->oformat->flags & AVFMT_NOFILE)) {
  2085. /* test if it already exists to avoid loosing precious files */
  2086. if (!file_overwrite &&
  2087. (strchr(filename, ':') == NULL ||
  2088. strstart(filename, "file:", NULL))) {
  2089. if (url_exist(filename)) {
  2090. int c;
  2091. if ( !using_stdin ) {
  2092. fprintf(stderr,"File '%s' already exists. Overwrite ? [y/N] ", filename);
  2093. fflush(stderr);
  2094. c = getchar();
  2095. if (toupper(c) != 'Y') {
  2096. fprintf(stderr, "Not overwriting - exiting\n");
  2097. exit(1);
  2098. }
  2099. }
  2100. else {
  2101. fprintf(stderr,"File '%s' already exists. Exiting.\n", filename);
  2102. exit(1);
  2103. }
  2104. }
  2105. }
  2106. /* open the file */
  2107. if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
  2108. fprintf(stderr, "Could not open '%s'\n", filename);
  2109. exit(1);
  2110. }
  2111. }
  2112. memset(ap, 0, sizeof(*ap));
  2113. ap->image_format = image_format;
  2114. if (av_set_parameters(oc, ap) < 0) {
  2115. fprintf(stderr, "%s: Invalid encoding parameters\n",
  2116. oc->filename);
  2117. exit(1);
  2118. }
  2119. /* reset some options */
  2120. file_oformat = NULL;
  2121. file_iformat = NULL;
  2122. image_format = NULL;
  2123. audio_disable = 0;
  2124. video_disable = 0;
  2125. audio_codec_id = CODEC_ID_NONE;
  2126. video_codec_id = CODEC_ID_NONE;
  2127. audio_stream_copy = 0;
  2128. video_stream_copy = 0;
  2129. }
  2130. /* prepare dummy protocols for grab */
  2131. static void prepare_grab(void)
  2132. {
  2133. int has_video, has_audio, i, j;
  2134. AVFormatContext *oc;
  2135. AVFormatContext *ic;
  2136. AVFormatParameters vp1, *vp = &vp1;
  2137. AVFormatParameters ap1, *ap = &ap1;
  2138. /* see if audio/video inputs are needed */
  2139. has_video = 0;
  2140. has_audio = 0;
  2141. memset(ap, 0, sizeof(*ap));
  2142. memset(vp, 0, sizeof(*vp));
  2143. for(j=0;j<nb_output_files;j++) {
  2144. oc = output_files[j];
  2145. for(i=0;i<oc->nb_streams;i++) {
  2146. AVCodecContext *enc = &oc->streams[i]->codec;
  2147. switch(enc->codec_type) {
  2148. case CODEC_TYPE_AUDIO:
  2149. if (enc->sample_rate > ap->sample_rate)
  2150. ap->sample_rate = enc->sample_rate;
  2151. if (enc->channels > ap->channels)
  2152. ap->channels = enc->channels;
  2153. has_audio = 1;
  2154. break;
  2155. case CODEC_TYPE_VIDEO:
  2156. if (enc->width > vp->width)
  2157. vp->width = enc->width;
  2158. if (enc->height > vp->height)
  2159. vp->height = enc->height;
  2160. assert(enc->frame_rate_base == DEFAULT_FRAME_RATE_BASE);
  2161. if (enc->frame_rate > vp->frame_rate){
  2162. vp->frame_rate = enc->frame_rate;
  2163. vp->frame_rate_base = enc->frame_rate_base;
  2164. }
  2165. has_video = 1;
  2166. break;
  2167. default:
  2168. av_abort();
  2169. }
  2170. }
  2171. }
  2172. if (has_video == 0 && has_audio == 0) {
  2173. fprintf(stderr, "Output file must have at least one audio or video stream\n");
  2174. exit(1);
  2175. }
  2176. if (has_video) {
  2177. AVInputFormat *fmt1;
  2178. fmt1 = av_find_input_format(video_grab_format);
  2179. vp->device = video_device;
  2180. vp->channel = video_channel;
  2181. vp->standard = video_standard;
  2182. if (av_open_input_file(&ic, "", fmt1, 0, vp) < 0) {
  2183. fprintf(stderr, "Could not find video grab device\n");
  2184. exit(1);
  2185. }
  2186. /* by now video grab has one stream */
  2187. ic->streams[0]->r_frame_rate = vp->frame_rate;
  2188. ic->streams[0]->r_frame_rate_base = vp->frame_rate_base;
  2189. input_files[nb_input_files] = ic;
  2190. dump_format(ic, nb_input_files, "", 0);
  2191. nb_input_files++;
  2192. }
  2193. if (has_audio && audio_grab_format) {
  2194. AVInputFormat *fmt1;
  2195. fmt1 = av_find_input_format(audio_grab_format);
  2196. ap->device = audio_device;
  2197. if (av_open_input_file(&ic, "", fmt1, 0, ap) < 0) {
  2198. fprintf(stderr, "Could not find audio grab device\n");
  2199. exit(1);
  2200. }
  2201. input_files[nb_input_files] = ic;
  2202. dump_format(ic, nb_input_files, "", 0);
  2203. nb_input_files++;
  2204. }
  2205. }
  2206. /* same option as mencoder */
  2207. static void opt_pass(const char *pass_str)
  2208. {
  2209. int pass;
  2210. pass = atoi(pass_str);
  2211. if (pass != 1 && pass != 2) {
  2212. fprintf(stderr, "pass number can be only 1 or 2\n");
  2213. exit(1);
  2214. }
  2215. do_pass = pass;
  2216. }
  2217. #if defined(CONFIG_WIN32) || defined(CONFIG_OS2)
  2218. static int64_t getutime(void)
  2219. {
  2220. return av_gettime();
  2221. }
  2222. #else
  2223. static int64_t getutime(void)
  2224. {
  2225. struct rusage rusage;
  2226. getrusage(RUSAGE_SELF, &rusage);
  2227. return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
  2228. }
  2229. #endif
  2230. extern int ffm_nopts;
  2231. static void opt_bitexact(void)
  2232. {
  2233. bitexact=1;
  2234. /* disable generate of real time pts in ffm (need to be supressed anyway) */
  2235. ffm_nopts = 1;
  2236. }
  2237. static void show_formats(void)
  2238. {
  2239. AVInputFormat *ifmt;
  2240. AVOutputFormat *ofmt;
  2241. AVImageFormat *image_fmt;
  2242. URLProtocol *up;
  2243. AVCodec *p;
  2244. const char **pp;
  2245. printf("Output audio/video file formats:");
  2246. for(ofmt = first_oformat; ofmt != NULL; ofmt = ofmt->next) {
  2247. printf(" %s", ofmt->name);
  2248. }
  2249. printf("\n");
  2250. printf("Input audio/video file formats:");
  2251. for(ifmt = first_iformat; ifmt != NULL; ifmt = ifmt->next) {
  2252. printf(" %s", ifmt->name);
  2253. }
  2254. printf("\n");
  2255. printf("Output image formats:");
  2256. for(image_fmt = first_image_format; image_fmt != NULL;
  2257. image_fmt = image_fmt->next) {
  2258. if (image_fmt->img_write)
  2259. printf(" %s", image_fmt->name);
  2260. }
  2261. printf("\n");
  2262. printf("Input image formats:");
  2263. for(image_fmt = first_image_format; image_fmt != NULL;
  2264. image_fmt = image_fmt->next) {
  2265. if (image_fmt->img_read)
  2266. printf(" %s", image_fmt->name);
  2267. }
  2268. printf("\n");
  2269. printf("Codecs:\n");
  2270. printf(" Encoders:");
  2271. for(p = first_avcodec; p != NULL; p = p->next) {
  2272. if (p->encode)
  2273. printf(" %s", p->name);
  2274. }
  2275. printf("\n");
  2276. printf(" Decoders:");
  2277. for(p = first_avcodec; p != NULL; p = p->next) {
  2278. if (p->decode)
  2279. printf(" %s", p->name);
  2280. }
  2281. printf("\n");
  2282. printf("Supported file protocols:");
  2283. for(up = first_protocol; up != NULL; up = up->next)
  2284. printf(" %s:", up->name);
  2285. printf("\n");
  2286. printf("Frame size, frame rate abbreviations: ntsc pal qntsc qpal sntsc spal film ntsc-film sqcif qcif cif 4cif\n");
  2287. printf("Motion estimation methods:");
  2288. pp = motion_str;
  2289. while (*pp) {
  2290. printf(" %s", *pp);
  2291. if ((pp - motion_str + 1) == ME_ZERO)
  2292. printf("(fastest)");
  2293. else if ((pp - motion_str + 1) == ME_FULL)
  2294. printf("(slowest)");
  2295. else if ((pp - motion_str + 1) == ME_EPZS)
  2296. printf("(default)");
  2297. pp++;
  2298. }
  2299. printf("\n");
  2300. exit(1);
  2301. }
  2302. const OptionDef options[] = {
  2303. /* main options */
  2304. { "L", 0, {(void*)show_license}, "show license" },
  2305. { "h", 0, {(void*)show_help}, "show help" },
  2306. { "formats", 0, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
  2307. { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
  2308. { "img", HAS_ARG, {(void*)opt_image_format}, "force image format", "img_fmt" },
  2309. { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" },
  2310. { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
  2311. { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file:stream" },
  2312. { "t", HAS_ARG, {(void*)opt_recording_time}, "set the recording time", "duration" },
  2313. { "title", HAS_ARG | OPT_STRING, {(void*)&str_title}, "set the title", "string" },
  2314. { "author", HAS_ARG | OPT_STRING, {(void*)&str_author}, "set the author", "string" },
  2315. { "copyright", HAS_ARG | OPT_STRING, {(void*)&str_copyright}, "set the copyright", "string" },
  2316. { "comment", HAS_ARG | OPT_STRING, {(void*)&str_comment}, "set the comment", "string" },
  2317. { "debug", HAS_ARG | OPT_EXPERT, {(void*)opt_debug}, "print specific debug info", "" },
  2318. { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark},
  2319. "add timings for benchmarking" },
  2320. { "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump},
  2321. "dump each input packet" },
  2322. { "bitexact", OPT_EXPERT, {(void*)opt_bitexact}, "only use bit exact algorithms (for codec testing)" },
  2323. { "re", OPT_BOOL | OPT_EXPERT, {(void*)&rate_emu}, "read input at native frame rate", "" },
  2324. { "loop", OPT_BOOL | OPT_EXPERT, {(void*)&loop_input}, "loop (current only works with images)" },
  2325. /* video options */
  2326. { "b", HAS_ARG | OPT_VIDEO, {(void*)opt_video_bitrate}, "set video bitrate (in kbit/s)", "bitrate" },
  2327. { "r", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_rate}, "set frame rate (Hz value, fraction or abbreviation)", "rate" },
  2328. { "s", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" },
  2329. { "aspect", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_aspect_ratio}, "set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)", "aspect" },
  2330. { "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_frame_pix_fmt}, "set pixel format", "format" },
  2331. { "croptop", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop_top}, "set top crop band size (in pixels)", "size" },
  2332. { "cropbottom", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop_bottom}, "set bottom crop band size (in pixels)", "size" },
  2333. { "cropleft", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop_left}, "set left crop band size (in pixels)", "size" },
  2334. { "cropright", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop_right}, "set right crop band size (in pixels)", "size" },
  2335. { "g", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_gop_size}, "set the group of picture size", "gop_size" },
  2336. { "intra", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&intra_only}, "use only intra frames"},
  2337. { "vn", OPT_BOOL | OPT_VIDEO, {(void*)&video_disable}, "disable video" },
  2338. { "qscale", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qscale}, "use fixed video quantiser scale (VBR)", "q" },
  2339. { "qmin", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qmin}, "min video quantiser scale (VBR)", "q" },
  2340. { "qmax", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qmax}, "max video quantiser scale (VBR)", "q" },
  2341. { "mbqmin", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_mb_qmin}, "min macroblock quantiser scale (VBR)", "q" },
  2342. { "mbqmax", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_mb_qmax}, "max macroblock quantiser scale (VBR)", "q" },
  2343. { "qdiff", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qdiff}, "max difference between the quantiser scale (VBR)", "q" },
  2344. { "qblur", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qblur}, "video quantiser scale blur (VBR)", "blur" },
  2345. { "qcomp", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qcomp}, "video quantiser scale compression (VBR)", "compression" },
  2346. { "rc_init_cplx", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_rc_initial_cplx}, "initial complexity for 1-pass encoding", "complexity" },
  2347. { "b_qfactor", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_b_qfactor}, "qp factor between p and b frames", "factor" },
  2348. { "i_qfactor", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_i_qfactor}, "qp factor between p and i frames", "factor" },
  2349. { "b_qoffset", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_b_qoffset}, "qp offset between p and b frames", "offset" },
  2350. { "i_qoffset", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_i_qoffset}, "qp offset between p and i frames", "offset" },
  2351. // { "b_strategy", HAS_ARG | OPT_EXPERT, {(void*)opt_b_strategy}, "dynamic b frame selection strategy", "strategy" },
  2352. { "rc_eq", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_video_rc_eq}, "set rate control equation", "equation" },
  2353. { "rc_override", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_video_rc_override_string}, "rate control override for specific intervals", "override" },
  2354. { "bt", HAS_ARG | OPT_VIDEO, {(void*)opt_video_bitrate_tolerance}, "set video bitrate tolerance (in kbit/s)", "tolerance" },
  2355. { "maxrate", HAS_ARG | OPT_VIDEO, {(void*)opt_video_bitrate_max}, "set max video bitrate tolerance (in kbit/s)", "bitrate" },
  2356. { "minrate", HAS_ARG | OPT_VIDEO, {(void*)opt_video_bitrate_min}, "set min video bitrate tolerance (in kbit/s)", "bitrate" },
  2357. { "bufsize", HAS_ARG | OPT_VIDEO, {(void*)opt_video_buffer_size}, "set ratecontrol buffere size (in kbit)", "size" },
  2358. { "vcodec", HAS_ARG | OPT_VIDEO, {(void*)opt_video_codec}, "force video codec ('copy' to copy stream)", "codec" },
  2359. { "me", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_motion_estimation}, "set motion estimation method",
  2360. "method" },
  2361. { "dct_algo", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_dct_algo}, "set dct algo", "algo" },
  2362. { "idct_algo", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_idct_algo}, "set idct algo", "algo" },
  2363. { "er", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_error_resilience}, "set error resilience", "n" },
  2364. { "ec", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_error_concealment}, "set error concealment", "bit_mask" },
  2365. { "bf", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_b_frames}, "use 'frames' B frames (only MPEG-4)", "frames" },
  2366. { "hq", OPT_BOOL, {(void*)&mb_decision}, "activate high quality settings" },
  2367. { "mbd", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_mb_decision}, "macroblock decision", "mode" },
  2368. { "4mv", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_4mv}, "use four motion vector by macroblock (only MPEG-4)" },
  2369. { "part", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_part}, "use data partitioning (only MPEG-4)" },
  2370. { "bug", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_workaround_bugs}, "workaround not auto detected encoder bugs", "param" },
  2371. { "ps", HAS_ARG | OPT_EXPERT, {(void*)opt_packet_size}, "set packet size in bits", "size" },
  2372. { "strict", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_strict}, "how strictly to follow the standarts", "strictness" },
  2373. { "sameq", OPT_BOOL | OPT_VIDEO, {(void*)&same_quality},
  2374. "use same video quality as source (implies VBR)" },
  2375. { "pass", HAS_ARG | OPT_VIDEO, {(void*)&opt_pass}, "select the pass number (1 or 2)", "n" },
  2376. { "passlogfile", HAS_ARG | OPT_STRING | OPT_VIDEO, {(void*)&pass_logfilename}, "select two pass log file name", "file" },
  2377. { "deinterlace", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&do_deinterlace},
  2378. "deinterlace pictures" },
  2379. { "psnr", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&do_psnr}, "calculate PSNR of compressed frames" },
  2380. { "vstats", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&do_vstats}, "dump video coding statistics to file" },
  2381. { "vhook", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)add_frame_hooker}, "insert video processing module", "module" },
  2382. { "aic", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_aic}, "enable Advanced intra coding (h263+)" },
  2383. { "umv", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_umv}, "enable Unlimited Motion Vector (h263+)" },
  2384. /* audio options */
  2385. { "ab", HAS_ARG | OPT_AUDIO, {(void*)opt_audio_bitrate}, "set audio bitrate (in kbit/s)", "bitrate", },
  2386. { "ar", HAS_ARG | OPT_AUDIO, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" },
  2387. { "ac", HAS_ARG | OPT_AUDIO, {(void*)opt_audio_channels}, "set number of audio channels", "channels" },
  2388. { "an", OPT_BOOL | OPT_AUDIO, {(void*)&audio_disable}, "disable audio" },
  2389. { "acodec", HAS_ARG | OPT_AUDIO, {(void*)opt_audio_codec}, "force audio codec ('copy' to copy stream)", "codec" },
  2390. /* grab options */
  2391. { "vd", HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_GRAB, {(void*)opt_video_device}, "set video grab device", "device" },
  2392. { "vc", HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_GRAB, {(void*)opt_video_channel}, "set video grab channel (DV1394 only)", "channel" },
  2393. { "tvstd", HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_GRAB, {(void*)opt_video_standard}, "set television standard (NTSC, PAL (SECAM))", "standard" },
  2394. { "dv1394", OPT_EXPERT | OPT_GRAB, {(void*)opt_dv1394}, "set DV1394 grab", "" },
  2395. { "ad", HAS_ARG | OPT_EXPERT | OPT_AUDIO | OPT_GRAB, {(void*)opt_audio_device}, "set audio device", "device" },
  2396. { NULL, },
  2397. };
  2398. static void show_banner(void)
  2399. {
  2400. printf("ffmpeg version " FFMPEG_VERSION ", Copyright (c) 2000-2003 Fabrice Bellard\n");
  2401. }
  2402. static void show_license(void)
  2403. {
  2404. show_banner();
  2405. printf(
  2406. "This library is free software; you can redistribute it and/or\n"
  2407. "modify it under the terms of the GNU Lesser General Public\n"
  2408. "License as published by the Free Software Foundation; either\n"
  2409. "version 2 of the License, or (at your option) any later version.\n"
  2410. "\n"
  2411. "This library is distributed in the hope that it will be useful,\n"
  2412. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  2413. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
  2414. "Lesser General Public License for more details.\n"
  2415. "\n"
  2416. "You should have received a copy of the GNU Lesser General Public\n"
  2417. "License along with this library; if not, write to the Free Software\n"
  2418. "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n"
  2419. );
  2420. exit(1);
  2421. }
  2422. static void show_help(void)
  2423. {
  2424. show_banner();
  2425. printf("usage: ffmpeg [[options] -i input_file]... {[options] outfile}...\n"
  2426. "Hyper fast Audio and Video encoder\n");
  2427. printf("\n");
  2428. show_help_options(options, "Main options:\n",
  2429. OPT_EXPERT | OPT_AUDIO | OPT_VIDEO, 0);
  2430. show_help_options(options, "\nVideo options:\n",
  2431. OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_GRAB,
  2432. OPT_VIDEO);
  2433. show_help_options(options, "\nAdvanced Video options:\n",
  2434. OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_GRAB,
  2435. OPT_VIDEO | OPT_EXPERT);
  2436. show_help_options(options, "\nAudio options:\n",
  2437. OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_GRAB,
  2438. OPT_AUDIO);
  2439. show_help_options(options, "\nAdvanced Audio options:\n",
  2440. OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_GRAB,
  2441. OPT_AUDIO | OPT_EXPERT);
  2442. show_help_options(options, "\nAudio/Video grab options:\n",
  2443. OPT_GRAB,
  2444. OPT_GRAB);
  2445. show_help_options(options, "\nAdvanced options:\n",
  2446. OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_GRAB,
  2447. OPT_EXPERT);
  2448. exit(1);
  2449. }
  2450. void parse_arg_file(const char *filename)
  2451. {
  2452. opt_output_file(filename);
  2453. }
  2454. int main(int argc, char **argv)
  2455. {
  2456. int i;
  2457. int64_t ti;
  2458. av_register_all();
  2459. if (argc <= 1)
  2460. show_help();
  2461. /* parse options */
  2462. parse_options(argc, argv, options);
  2463. /* file converter / grab */
  2464. if (nb_output_files <= 0) {
  2465. fprintf(stderr, "Must supply at least one output file\n");
  2466. exit(1);
  2467. }
  2468. if (nb_input_files == 0) {
  2469. prepare_grab();
  2470. }
  2471. ti = getutime();
  2472. av_encode(output_files, nb_output_files, input_files, nb_input_files,
  2473. stream_maps, nb_stream_maps);
  2474. ti = getutime() - ti;
  2475. if (do_benchmark) {
  2476. printf("bench: utime=%0.3fs\n", ti / 1000000.0);
  2477. }
  2478. /* close files */
  2479. for(i=0;i<nb_output_files;i++) {
  2480. /* maybe av_close_output_file ??? */
  2481. AVFormatContext *s = output_files[i];
  2482. int j;
  2483. if (!(s->oformat->flags & AVFMT_NOFILE))
  2484. url_fclose(&s->pb);
  2485. for(j=0;j<s->nb_streams;j++)
  2486. av_free(s->streams[j]);
  2487. av_free(s);
  2488. }
  2489. for(i=0;i<nb_input_files;i++)
  2490. av_close_input_file(input_files[i]);
  2491. av_free_static();
  2492. #ifdef POWERPC_PERFORMANCE_REPORT
  2493. extern void powerpc_display_perf_report(void);
  2494. powerpc_display_perf_report();
  2495. #endif /* POWERPC_PERFORMANCE_REPORT */
  2496. #ifndef CONFIG_WIN32
  2497. if (received_sigterm) {
  2498. fprintf(stderr,
  2499. "Received signal %d: terminating.\n",
  2500. (int) received_sigterm);
  2501. exit (255);
  2502. }
  2503. #endif
  2504. exit(0); /* not all OS-es handle main() return value */
  2505. return 0;
  2506. }