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.

2672 lines
84KB

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