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.

2730 lines
87KB

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