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.

2855 lines
94KB

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