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.

2765 lines
88KB

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