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.

2868 lines
92KB

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