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.

2689 lines
85KB

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