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.

2783 lines
89KB

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