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.

2310 lines
69KB

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