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.

2356 lines
74KB

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