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.

2246 lines
67KB

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