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.

2941 lines
94KB

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