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.

2895 lines
93KB

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