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.

2739 lines
88KB

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