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.

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