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.

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