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.

2916 lines
94KB

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