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.

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