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.

2829 lines
90KB

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