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.

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