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.

2728 lines
87KB

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