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.

2200 lines
65KB

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