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.

2271 lines
68KB

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