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.

2215 lines
66KB

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