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.

3121 lines
103KB

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