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.

2875 lines
92KB

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