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.

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