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.

2202 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. "epzs",
  1204. "x1",
  1205. NULL,
  1206. };
  1207. void opt_motion_estimation(const char *arg)
  1208. {
  1209. const char **p;
  1210. p = motion_str;
  1211. for(;;) {
  1212. if (!*p) {
  1213. fprintf(stderr, "Unknown motion estimation method '%s'\n", arg);
  1214. exit(1);
  1215. }
  1216. if (!strcmp(*p, arg))
  1217. break;
  1218. p++;
  1219. }
  1220. motion_estimation_method = p - motion_str;
  1221. }
  1222. void opt_video_codec(const char *arg)
  1223. {
  1224. AVCodec *p;
  1225. p = first_avcodec;
  1226. while (p) {
  1227. if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
  1228. break;
  1229. p = p->next;
  1230. }
  1231. if (p == NULL) {
  1232. fprintf(stderr, "Unknown video codec '%s'\n", arg);
  1233. exit(1);
  1234. } else {
  1235. video_codec_id = p->id;
  1236. }
  1237. }
  1238. void opt_map(const char *arg)
  1239. {
  1240. AVStreamMap *m;
  1241. const char *p;
  1242. p = arg;
  1243. m = &stream_maps[nb_stream_maps++];
  1244. m->file_index = strtol(arg, (char **)&p, 0);
  1245. if (*p)
  1246. p++;
  1247. m->stream_index = strtol(arg, (char **)&p, 0);
  1248. }
  1249. void opt_recording_time(const char *arg)
  1250. {
  1251. recording_time = parse_date(arg, 1);
  1252. }
  1253. /* return the number of packet read to find the codec parameters */
  1254. int find_codec_parameters(AVFormatContext *ic)
  1255. {
  1256. int val, i, count, ret, got_picture, size;
  1257. AVCodec *codec;
  1258. AVCodecContext *enc;
  1259. AVStream *st;
  1260. AVPacket *pkt;
  1261. AVPicture picture;
  1262. AVPacketList *pktl, **ppktl;
  1263. short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
  1264. UINT8 *ptr;
  1265. count = 0;
  1266. ppktl = &ic->packet_buffer;
  1267. for(;;) {
  1268. for(i=0;i<ic->nb_streams;i++) {
  1269. enc = &ic->streams[i]->codec;
  1270. switch(enc->codec_type) {
  1271. case CODEC_TYPE_AUDIO:
  1272. val = enc->sample_rate;
  1273. break;
  1274. case CODEC_TYPE_VIDEO:
  1275. val = enc->width;
  1276. break;
  1277. default:
  1278. val = 1;
  1279. break;
  1280. }
  1281. /* if no parameters supplied, then we should read it from
  1282. the stream */
  1283. if (val == 0)
  1284. break;
  1285. }
  1286. if (i == ic->nb_streams) {
  1287. ret = count;
  1288. break;
  1289. }
  1290. if (count == 0) {
  1291. /* open each codec */
  1292. for(i=0;i<ic->nb_streams;i++) {
  1293. st = ic->streams[i];
  1294. codec = avcodec_find_decoder(st->codec.codec_id);
  1295. if (codec == NULL) {
  1296. ret = -1;
  1297. goto the_end;
  1298. }
  1299. avcodec_open(&st->codec, codec);
  1300. }
  1301. }
  1302. pktl = av_mallocz(sizeof(AVPacketList));
  1303. if (!pktl) {
  1304. ret = -1;
  1305. break;
  1306. }
  1307. /* add the packet in the buffered packet list */
  1308. *ppktl = pktl;
  1309. ppktl = &pktl->next;
  1310. pkt = &pktl->pkt;
  1311. if (ic->format->read_packet(ic, pkt) < 0) {
  1312. ret = -1;
  1313. break;
  1314. }
  1315. st = ic->streams[pkt->stream_index];
  1316. /* decode the data and update codec parameters */
  1317. ptr = pkt->data;
  1318. size = pkt->size;
  1319. while (size > 0) {
  1320. switch(st->codec.codec_type) {
  1321. case CODEC_TYPE_VIDEO:
  1322. ret = avcodec_decode_video(&st->codec, &picture, &got_picture, ptr, size);
  1323. break;
  1324. case CODEC_TYPE_AUDIO:
  1325. ret = avcodec_decode_audio(&st->codec, samples, &got_picture, ptr, size);
  1326. break;
  1327. default:
  1328. ret = -1;
  1329. break;
  1330. }
  1331. if (ret < 0) {
  1332. ret = -1;
  1333. goto the_end;
  1334. }
  1335. if (got_picture)
  1336. break;
  1337. ptr += ret;
  1338. size -= ret;
  1339. }
  1340. count++;
  1341. }
  1342. the_end:
  1343. if (count > 0) {
  1344. /* close each codec */
  1345. for(i=0;i<ic->nb_streams;i++) {
  1346. st = ic->streams[i];
  1347. avcodec_close(&st->codec);
  1348. }
  1349. }
  1350. return ret;
  1351. }
  1352. int filename_number_test(const char *filename)
  1353. {
  1354. char buf[1024];
  1355. if (get_frame_filename(buf, sizeof(buf), filename, 1) < 0) {
  1356. fprintf(stderr, "%s: Incorrect image filename syntax.\n"
  1357. "Use '%%d' to specify the image number:\n"
  1358. " for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
  1359. " for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
  1360. filename);
  1361. return -1;
  1362. } else {
  1363. return 0;
  1364. }
  1365. }
  1366. void opt_input_file(const char *filename)
  1367. {
  1368. AVFormatContext *ic;
  1369. AVFormatParameters params, *ap = &params;
  1370. URLFormat url_format;
  1371. AVFormat *fmt;
  1372. int err, i, ret;
  1373. ic = av_mallocz(sizeof(AVFormatContext));
  1374. strcpy(ic->filename, filename);
  1375. /* first format guess to know if we must open file */
  1376. fmt = file_format;
  1377. if (!fmt)
  1378. fmt = guess_format(NULL, filename, NULL);
  1379. if (fmt == NULL || !(fmt->flags & AVFMT_NOFILE)) {
  1380. /* open file */
  1381. if (url_fopen(&ic->pb, filename, URL_RDONLY) < 0) {
  1382. fprintf(stderr, "Could not open '%s'\n", filename);
  1383. exit(1);
  1384. }
  1385. /* find format and set default parameters */
  1386. fmt = file_format;
  1387. err = url_getformat(url_fileno(&ic->pb), &url_format);
  1388. if (err >= 0) {
  1389. if (!fmt)
  1390. fmt = guess_format(url_format.format_name, NULL, NULL);
  1391. ap->sample_rate = url_format.sample_rate;
  1392. ap->frame_rate = url_format.frame_rate;
  1393. ap->channels = url_format.channels;
  1394. ap->width = url_format.width;
  1395. ap->height = url_format.height;
  1396. ap->pix_fmt = url_format.pix_fmt;
  1397. } else {
  1398. if (!fmt)
  1399. fmt = guess_format(NULL, filename, NULL);
  1400. memset(ap, 0, sizeof(*ap));
  1401. }
  1402. } else {
  1403. memset(ap, 0, sizeof(*ap));
  1404. }
  1405. if (!fmt || !fmt->read_header) {
  1406. fprintf(stderr, "%s: Unknown file format\n", filename);
  1407. exit(1);
  1408. }
  1409. ic->format = fmt;
  1410. /* get default parameters from command line */
  1411. if (!ap->sample_rate)
  1412. ap->sample_rate = audio_sample_rate;
  1413. if (!ap->channels)
  1414. ap->channels = audio_channels;
  1415. if (!ap->frame_rate)
  1416. ap->frame_rate = frame_rate;
  1417. if (!ap->width)
  1418. ap->width = frame_width;
  1419. if (!ap->height)
  1420. ap->height = frame_height;
  1421. /* check filename in case of an image number is expected */
  1422. if (ic->format->flags & AVFMT_NEEDNUMBER) {
  1423. if (filename_number_test(ic->filename) < 0)
  1424. exit(1);
  1425. }
  1426. err = ic->format->read_header(ic, ap);
  1427. if (err < 0) {
  1428. fprintf(stderr, "%s: Error while parsing header\n", filename);
  1429. exit(1);
  1430. }
  1431. /* If not enough info for the codecs, we decode the first frames
  1432. to get it. (used in mpeg case for example) */
  1433. ret = find_codec_parameters(ic);
  1434. if (ret < 0) {
  1435. fprintf(stderr, "%s: could not find codec parameters\n", filename);
  1436. exit(1);
  1437. }
  1438. /* update the current parameters so that they match the one of the input stream */
  1439. for(i=0;i<ic->nb_streams;i++) {
  1440. AVCodecContext *enc = &ic->streams[i]->codec;
  1441. switch(enc->codec_type) {
  1442. case CODEC_TYPE_AUDIO:
  1443. audio_channels = enc->channels;
  1444. audio_sample_rate = enc->sample_rate;
  1445. break;
  1446. case CODEC_TYPE_VIDEO:
  1447. frame_height = enc->height;
  1448. frame_width = enc->width;
  1449. frame_rate = enc->frame_rate;
  1450. break;
  1451. }
  1452. }
  1453. input_files[nb_input_files] = ic;
  1454. /* dump the file content */
  1455. dump_format(ic, nb_input_files, filename, 0);
  1456. nb_input_files++;
  1457. file_format = NULL;
  1458. }
  1459. void check_audio_video_inputs(int *has_video_ptr, int *has_audio_ptr)
  1460. {
  1461. int has_video, has_audio, i, j;
  1462. AVFormatContext *ic;
  1463. has_video = 0;
  1464. has_audio = 0;
  1465. for(j=0;j<nb_input_files;j++) {
  1466. ic = input_files[j];
  1467. for(i=0;i<ic->nb_streams;i++) {
  1468. AVCodecContext *enc = &ic->streams[i]->codec;
  1469. switch(enc->codec_type) {
  1470. case CODEC_TYPE_AUDIO:
  1471. has_audio = 1;
  1472. break;
  1473. case CODEC_TYPE_VIDEO:
  1474. has_video = 1;
  1475. break;
  1476. }
  1477. }
  1478. }
  1479. *has_video_ptr = has_video;
  1480. *has_audio_ptr = has_audio;
  1481. }
  1482. void opt_output_file(const char *filename)
  1483. {
  1484. AVStream *st;
  1485. AVFormatContext *oc;
  1486. int use_video, use_audio, nb_streams, input_has_video, input_has_audio;
  1487. int codec_id;
  1488. if (!strcmp(filename, "-"))
  1489. filename = "pipe:";
  1490. oc = av_mallocz(sizeof(AVFormatContext));
  1491. if (!file_format) {
  1492. file_format = guess_format(NULL, filename, NULL);
  1493. if (!file_format)
  1494. file_format = &mpeg_mux_format;
  1495. }
  1496. oc->format = file_format;
  1497. if (!strcmp(file_format->name, "ffm") &&
  1498. strstart(filename, "http:", NULL)) {
  1499. /* special case for files sent to ffserver: we get the stream
  1500. parameters from ffserver */
  1501. if (read_ffserver_streams(oc, filename) < 0) {
  1502. fprintf(stderr, "Could not read stream parameters from '%s'\n", filename);
  1503. exit(1);
  1504. }
  1505. } else {
  1506. use_video = file_format->video_codec != CODEC_ID_NONE;
  1507. use_audio = file_format->audio_codec != CODEC_ID_NONE;
  1508. /* disable if no corresponding type found and at least one
  1509. input file */
  1510. if (nb_input_files > 0) {
  1511. check_audio_video_inputs(&input_has_video, &input_has_audio);
  1512. if (!input_has_video)
  1513. use_video = 0;
  1514. if (!input_has_audio)
  1515. use_audio = 0;
  1516. }
  1517. /* manual disable */
  1518. if (audio_disable) {
  1519. use_audio = 0;
  1520. }
  1521. if (video_disable) {
  1522. use_video = 0;
  1523. }
  1524. nb_streams = 0;
  1525. if (use_video) {
  1526. AVCodecContext *video_enc;
  1527. st = av_mallocz(sizeof(AVStream));
  1528. if (!st) {
  1529. fprintf(stderr, "Could not alloc stream\n");
  1530. exit(1);
  1531. }
  1532. video_enc = &st->codec;
  1533. codec_id = file_format->video_codec;
  1534. if (video_codec_id != CODEC_ID_NONE)
  1535. codec_id = video_codec_id;
  1536. video_enc->codec_id = codec_id;
  1537. video_enc->codec_type = CODEC_TYPE_VIDEO;
  1538. video_enc->bit_rate = video_bit_rate;
  1539. video_enc->bit_rate_tolerance = video_bit_rate_tolerance;
  1540. video_enc->frame_rate = frame_rate;
  1541. video_enc->width = frame_width;
  1542. video_enc->height = frame_height;
  1543. if (!intra_only)
  1544. video_enc->gop_size = gop_size;
  1545. else
  1546. video_enc->gop_size = 0;
  1547. if (video_qscale || same_quality) {
  1548. video_enc->flags |= CODEC_FLAG_QSCALE;
  1549. video_enc->quality = video_qscale;
  1550. }
  1551. video_enc->qmin= video_qmin;
  1552. video_enc->qmax= video_qmax;
  1553. video_enc->max_qdiff= video_qdiff;
  1554. video_enc->qblur= video_qblur;
  1555. video_enc->qcompress= video_qcomp;
  1556. if (do_psnr)
  1557. video_enc->get_psnr = 1;
  1558. else
  1559. video_enc->get_psnr = 0;
  1560. /* XXX: need to find a way to set codec parameters */
  1561. if (oc->format == &ppm_format ||
  1562. oc->format == &ppmpipe_format) {
  1563. video_enc->pix_fmt = PIX_FMT_RGB24;
  1564. }
  1565. oc->streams[nb_streams] = st;
  1566. nb_streams++;
  1567. }
  1568. if (use_audio) {
  1569. AVCodecContext *audio_enc;
  1570. st = av_mallocz(sizeof(AVStream));
  1571. if (!st) {
  1572. fprintf(stderr, "Could not alloc stream\n");
  1573. exit(1);
  1574. }
  1575. audio_enc = &st->codec;
  1576. codec_id = file_format->audio_codec;
  1577. if (audio_codec_id != CODEC_ID_NONE)
  1578. codec_id = audio_codec_id;
  1579. audio_enc->codec_id = codec_id;
  1580. audio_enc->codec_type = CODEC_TYPE_AUDIO;
  1581. audio_enc->bit_rate = audio_bit_rate;
  1582. audio_enc->sample_rate = audio_sample_rate;
  1583. audio_enc->channels = audio_channels;
  1584. oc->streams[nb_streams] = st;
  1585. nb_streams++;
  1586. }
  1587. oc->nb_streams = nb_streams;
  1588. if (!nb_streams) {
  1589. fprintf(stderr, "No audio or video streams available\n");
  1590. exit(1);
  1591. }
  1592. if (str_title)
  1593. nstrcpy(oc->title, sizeof(oc->title), str_title);
  1594. if (str_author)
  1595. nstrcpy(oc->author, sizeof(oc->author), str_author);
  1596. if (str_copyright)
  1597. nstrcpy(oc->copyright, sizeof(oc->copyright), str_copyright);
  1598. if (str_comment)
  1599. nstrcpy(oc->comment, sizeof(oc->comment), str_comment);
  1600. }
  1601. output_files[nb_output_files] = oc;
  1602. /* dump the file content */
  1603. dump_format(oc, nb_output_files, filename, 1);
  1604. nb_output_files++;
  1605. strcpy(oc->filename, filename);
  1606. /* check filename in case of an image number is expected */
  1607. if (oc->format->flags & AVFMT_NEEDNUMBER) {
  1608. if (filename_number_test(oc->filename) < 0)
  1609. exit(1);
  1610. }
  1611. if (!(oc->format->flags & AVFMT_NOFILE)) {
  1612. /* test if it already exists to avoid loosing precious files */
  1613. if (!file_overwrite &&
  1614. (strchr(filename, ':') == NULL ||
  1615. strstart(filename, "file:", NULL))) {
  1616. if (url_exist(filename)) {
  1617. int c;
  1618. printf("File '%s' already exists. Overwrite ? [y/N] ", filename);
  1619. fflush(stdout);
  1620. c = getchar();
  1621. if (toupper(c) != 'Y') {
  1622. fprintf(stderr, "Not overwriting - exiting\n");
  1623. exit(1);
  1624. }
  1625. }
  1626. }
  1627. /* open the file */
  1628. if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
  1629. fprintf(stderr, "Could not open '%s'\n", filename);
  1630. exit(1);
  1631. }
  1632. }
  1633. /* reset some options */
  1634. file_format = NULL;
  1635. audio_disable = 0;
  1636. video_disable = 0;
  1637. audio_codec_id = CODEC_ID_NONE;
  1638. video_codec_id = CODEC_ID_NONE;
  1639. }
  1640. #ifdef CONFIG_GRAB
  1641. /* prepare dummy protocols for grab */
  1642. void prepare_grab(void)
  1643. {
  1644. int has_video, has_audio, i, j;
  1645. AVFormatContext *oc;
  1646. AVFormatContext *ic;
  1647. AVFormatParameters ap1, *ap = &ap1;
  1648. /* see if audio/video inputs are needed */
  1649. has_video = 0;
  1650. has_audio = 0;
  1651. memset(ap, 0, sizeof(*ap));
  1652. for(j=0;j<nb_output_files;j++) {
  1653. oc = output_files[j];
  1654. for(i=0;i<oc->nb_streams;i++) {
  1655. AVCodecContext *enc = &oc->streams[i]->codec;
  1656. switch(enc->codec_type) {
  1657. case CODEC_TYPE_AUDIO:
  1658. if (enc->sample_rate > ap->sample_rate)
  1659. ap->sample_rate = enc->sample_rate;
  1660. if (enc->channels > ap->channels)
  1661. ap->channels = enc->channels;
  1662. has_audio = 1;
  1663. break;
  1664. case CODEC_TYPE_VIDEO:
  1665. if (enc->width > ap->width)
  1666. ap->width = enc->width;
  1667. if (enc->height > ap->height)
  1668. ap->height = enc->height;
  1669. if (enc->frame_rate > ap->frame_rate)
  1670. ap->frame_rate = enc->frame_rate;
  1671. has_video = 1;
  1672. break;
  1673. }
  1674. }
  1675. }
  1676. if (has_video == 0 && has_audio == 0) {
  1677. fprintf(stderr, "Output file must have at least one audio or video stream\n");
  1678. exit(1);
  1679. }
  1680. if (has_video) {
  1681. ic = av_open_input_file("", "video_grab_device", 0, ap);
  1682. if (!ic) {
  1683. fprintf(stderr, "Could not open video grab device\n");
  1684. exit(1);
  1685. }
  1686. input_files[nb_input_files] = ic;
  1687. dump_format(ic, nb_input_files, v4l_device, 0);
  1688. nb_input_files++;
  1689. }
  1690. if (has_audio) {
  1691. ic = av_open_input_file("", "audio_device", 0, ap);
  1692. if (!ic) {
  1693. fprintf(stderr, "Could not open audio grab device\n");
  1694. exit(1);
  1695. }
  1696. input_files[nb_input_files] = ic;
  1697. dump_format(ic, nb_input_files, audio_device, 0);
  1698. nb_input_files++;
  1699. }
  1700. }
  1701. #else
  1702. void prepare_grab(void)
  1703. {
  1704. fprintf(stderr, "Must supply at least one input file\n");
  1705. exit(1);
  1706. }
  1707. #endif
  1708. /* open the necessary output devices for playing */
  1709. void prepare_play(void)
  1710. {
  1711. #ifndef __BEOS__
  1712. file_format = guess_format("audio_device", NULL, NULL);
  1713. if (!file_format) {
  1714. fprintf(stderr, "Could not find audio device\n");
  1715. exit(1);
  1716. }
  1717. opt_output_file(audio_device);
  1718. #endif
  1719. }
  1720. #ifndef CONFIG_WIN32
  1721. INT64 getutime(void)
  1722. {
  1723. struct rusage rusage;
  1724. getrusage(RUSAGE_SELF, &rusage);
  1725. return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
  1726. }
  1727. #else
  1728. INT64 getutime(void)
  1729. {
  1730. return gettime();
  1731. }
  1732. #endif
  1733. void show_formats(void)
  1734. {
  1735. AVFormat *f;
  1736. URLProtocol *up;
  1737. AVCodec *p;
  1738. const char **pp;
  1739. printf("File formats:\n");
  1740. printf(" Encoding:");
  1741. for(f = first_format; f != NULL; f = f->next) {
  1742. if (f->write_header)
  1743. printf(" %s", f->name);
  1744. }
  1745. printf("\n");
  1746. printf(" Decoding:");
  1747. for(f = first_format; f != NULL; f = f->next) {
  1748. if (f->read_header)
  1749. printf(" %s", f->name);
  1750. }
  1751. printf("\n");
  1752. printf("Codecs:\n");
  1753. printf(" Encoders:");
  1754. for(p = first_avcodec; p != NULL; p = p->next) {
  1755. if (p->encode)
  1756. printf(" %s", p->name);
  1757. }
  1758. printf("\n");
  1759. printf(" Decoders:");
  1760. for(p = first_avcodec; p != NULL; p = p->next) {
  1761. if (p->decode)
  1762. printf(" %s", p->name);
  1763. }
  1764. printf("\n");
  1765. printf("Supported file protocols:");
  1766. for(up = first_protocol; up != NULL; up = up->next)
  1767. printf(" %s:", up->name);
  1768. printf("\n");
  1769. printf("Frame size abbreviations: sqcif qcif cif 4cif\n");
  1770. printf("Motion estimation methods:");
  1771. pp = motion_str;
  1772. while (*pp) {
  1773. printf(" %s", *pp);
  1774. if ((pp - motion_str) == ME_ZERO)
  1775. printf("(fastest)");
  1776. else if ((pp - motion_str) == ME_FULL)
  1777. printf("(slowest)");
  1778. else if ((pp - motion_str) == ME_LOG)
  1779. printf("(default)");
  1780. pp++;
  1781. }
  1782. printf("\n");
  1783. exit(1);
  1784. }
  1785. void show_help(void)
  1786. {
  1787. const char *prog;
  1788. const OptionDef *po;
  1789. int i, expert;
  1790. prog = do_play ? "ffplay" : "ffmpeg";
  1791. printf("%s version " FFMPEG_VERSION ", Copyright (c) 2000, 2001 Gerard Lantau\n",
  1792. prog);
  1793. if (!do_play) {
  1794. printf("usage: ffmpeg [[options] -i input_file]... {[options] outfile}...\n"
  1795. "Hyper fast MPEG1/MPEG4/H263/RV and AC3/MPEG audio encoder\n");
  1796. } else {
  1797. printf("usage: ffplay [options] input_file...\n"
  1798. "Simple audio player\n");
  1799. }
  1800. printf("\n"
  1801. "Main options are:\n");
  1802. for(i=0;i<2;i++) {
  1803. if (i == 1)
  1804. printf("\nAdvanced options are:\n");
  1805. for(po = options; po->name != NULL; po++) {
  1806. char buf[64];
  1807. expert = (po->flags & OPT_EXPERT) != 0;
  1808. if (expert == i) {
  1809. strcpy(buf, po->name);
  1810. if (po->flags & HAS_ARG) {
  1811. strcat(buf, " ");
  1812. strcat(buf, po->argname);
  1813. }
  1814. printf("-%-17s %s\n", buf, po->help);
  1815. }
  1816. }
  1817. }
  1818. exit(1);
  1819. }
  1820. const OptionDef options[] = {
  1821. { "L", 0, {(void*)show_licence}, "show license" },
  1822. { "h", 0, {(void*)show_help}, "show help" },
  1823. { "formats", 0, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
  1824. { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
  1825. { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" },
  1826. { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
  1827. { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file:stream" },
  1828. { "t", HAS_ARG, {(void*)opt_recording_time}, "set the recording time", "duration" },
  1829. { "title", HAS_ARG | OPT_STRING, {(void*)&str_title}, "set the title", "string" },
  1830. { "author", HAS_ARG | OPT_STRING, {(void*)&str_author}, "set the author", "string" },
  1831. { "copyright", HAS_ARG | OPT_STRING, {(void*)&str_copyright}, "set the copyright", "string" },
  1832. { "comment", HAS_ARG | OPT_STRING, {(void*)&str_comment}, "set the comment", "string" },
  1833. /* video options */
  1834. { "b", HAS_ARG, {(void*)opt_video_bitrate}, "set video bitrate (in kbit/s)", "bitrate" },
  1835. { "r", HAS_ARG, {(void*)opt_frame_rate}, "set frame rate (in Hz)", "rate" },
  1836. { "s", HAS_ARG, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" },
  1837. { "g", HAS_ARG | OPT_EXPERT, {(void*)opt_gop_size}, "set the group of picture size", "gop_size" },
  1838. { "intra", OPT_BOOL | OPT_EXPERT, {(void*)&intra_only}, "use only intra frames"},
  1839. { "vn", OPT_BOOL, {(void*)&video_disable}, "disable video" },
  1840. { "qscale", HAS_ARG | OPT_EXPERT, {(void*)opt_qscale}, "use fixed video quantiser scale (VBR)", "q" },
  1841. { "qmin", HAS_ARG | OPT_EXPERT, {(void*)opt_qmin}, "min video quantiser scale (VBR)", "q" },
  1842. { "qmax", HAS_ARG | OPT_EXPERT, {(void*)opt_qmax}, "max video quantiser scale (VBR)", "q" },
  1843. { "qdiff", HAS_ARG | OPT_EXPERT, {(void*)opt_qdiff}, "max difference between the quantiser scale (VBR)", "q" },
  1844. { "qblur", HAS_ARG | OPT_EXPERT, {(void*)opt_qblur}, "video quantiser scale blur (VBR)", "blur" },
  1845. { "qcomp", HAS_ARG | OPT_EXPERT, {(void*)opt_qcomp}, "video quantiser scale compression (VBR)", "compression" },
  1846. { "bt", HAS_ARG, {(void*)opt_video_bitrate_tolerance}, "set video bitrate tolerance (in kbit/s)", "tolerance" },
  1847. #ifdef CONFIG_GRAB
  1848. { "vd", HAS_ARG | OPT_EXPERT, {(void*)opt_video_device}, "set video device", "device" },
  1849. #endif
  1850. { "vcodec", HAS_ARG | OPT_EXPERT, {(void*)opt_video_codec}, "force video codec", "codec" },
  1851. { "me", HAS_ARG | OPT_EXPERT, {(void*)opt_motion_estimation}, "set motion estimation method",
  1852. "method" },
  1853. { "sameq", OPT_BOOL, {(void*)&same_quality},
  1854. "use same video quality as source (implies VBR)" },
  1855. /* audio options */
  1856. { "ab", HAS_ARG, {(void*)opt_audio_bitrate}, "set audio bitrate (in kbit/s)", "bitrate", },
  1857. { "ar", HAS_ARG, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" },
  1858. { "ac", HAS_ARG, {(void*)opt_audio_channels}, "set number of audio channels", "channels" },
  1859. { "an", OPT_BOOL, {(void*)&audio_disable}, "disable audio" },
  1860. #ifdef CONFIG_GRAB
  1861. { "ad", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_device}, "set audio device", "device" },
  1862. #endif
  1863. { "acodec", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_codec}, "force audio codec", "codec" },
  1864. { "deinterlace", OPT_BOOL | OPT_EXPERT, {(void*)&do_deinterlace},
  1865. "deinterlace pictures" },
  1866. { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark},
  1867. "add timings for benchmarking" },
  1868. { "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump},
  1869. "dump each input packet" },
  1870. { "psnr", OPT_BOOL | OPT_EXPERT, {(void*)&do_psnr}, "calculate PSNR of compressed frames" },
  1871. { "vstats", OPT_BOOL | OPT_EXPERT, {(void*)&do_vstats}, "dump video coding statistics to file" },
  1872. { NULL, },
  1873. };
  1874. int main(int argc, char **argv)
  1875. {
  1876. int optindex, i;
  1877. const char *opt, *arg;
  1878. const OptionDef *po;
  1879. INT64 ti;
  1880. register_all();
  1881. /* detect if invoked as player */
  1882. i = strlen(argv[0]);
  1883. if (i >= 6 && !strcmp(argv[0] + i - 6, "ffplay"))
  1884. do_play = 1;
  1885. if (argc <= 1)
  1886. show_help();
  1887. /* parse options */
  1888. optindex = 1;
  1889. while (optindex < argc) {
  1890. opt = argv[optindex++];
  1891. if (opt[0] == '-' && opt[1] != '\0') {
  1892. po = options;
  1893. while (po->name != NULL) {
  1894. if (!strcmp(opt + 1, po->name))
  1895. break;
  1896. po++;
  1897. }
  1898. if (!po->name) {
  1899. fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
  1900. exit(1);
  1901. }
  1902. arg = NULL;
  1903. if (po->flags & HAS_ARG)
  1904. arg = argv[optindex++];
  1905. if (po->flags & OPT_STRING) {
  1906. char *str;
  1907. str = strdup(arg);
  1908. *po->u.str_arg = str;
  1909. } else if (po->flags & OPT_BOOL) {
  1910. *po->u.int_arg = 1;
  1911. } else {
  1912. po->u.func_arg(arg);
  1913. }
  1914. } else {
  1915. if (!do_play) {
  1916. opt_output_file(opt);
  1917. } else {
  1918. opt_input_file(opt);
  1919. }
  1920. }
  1921. }
  1922. if (!do_play) {
  1923. /* file converter / grab */
  1924. if (nb_output_files <= 0) {
  1925. fprintf(stderr, "Must supply at least one output file\n");
  1926. exit(1);
  1927. }
  1928. if (nb_input_files == 0) {
  1929. prepare_grab();
  1930. }
  1931. } else {
  1932. /* player */
  1933. if (nb_input_files <= 0) {
  1934. fprintf(stderr, "Must supply at least one input file\n");
  1935. exit(1);
  1936. }
  1937. prepare_play();
  1938. }
  1939. ti = getutime();
  1940. av_encode(output_files, nb_output_files, input_files, nb_input_files,
  1941. stream_maps, nb_stream_maps);
  1942. ti = getutime() - ti;
  1943. if (do_benchmark) {
  1944. printf("bench: utime=%0.3fs\n", ti / 1000000.0);
  1945. }
  1946. /* close files */
  1947. for(i=0;i<nb_output_files;i++) {
  1948. if (!(output_files[i]->format->flags & AVFMT_NOFILE))
  1949. url_fclose(&output_files[i]->pb);
  1950. }
  1951. for(i=0;i<nb_input_files;i++) {
  1952. if (!(input_files[i]->format->flags & AVFMT_NOFILE))
  1953. url_fclose(&input_files[i]->pb);
  1954. }
  1955. return 0;
  1956. }