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.

2925 lines
94KB

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