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.

2834 lines
91KB

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