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.

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