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.

2843 lines
92KB

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