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.

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