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.

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