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.

2832 lines
91KB

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