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.

2776 lines
90KB

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