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.

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