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.

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