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.

2879 lines
92KB

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