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.

2312 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. /* Some bug in mpeg audio decoder gives */
  865. /* data_size < 0, it seems they are overflows */
  866. if (data_size <= 0) {
  867. /* no audio frame */
  868. ptr += ret;
  869. len -= ret;
  870. continue;
  871. }
  872. data_buf = (UINT8 *)samples;
  873. break;
  874. case CODEC_TYPE_VIDEO:
  875. if (ist->st->codec.codec_id == CODEC_ID_RAWVIDEO) {
  876. int size;
  877. size = (ist->st->codec.width * ist->st->codec.height);
  878. avpicture_fill(&picture, ptr,
  879. ist->st->codec.pix_fmt,
  880. ist->st->codec.width,
  881. ist->st->codec.height);
  882. ret = len;
  883. } else {
  884. data_size = (ist->st->codec.width * ist->st->codec.height * 3) / 2;
  885. ret = avcodec_decode_video(&ist->st->codec,
  886. &picture, &got_picture, ptr, len);
  887. if (ret < 0) {
  888. fail_decode:
  889. fprintf(stderr, "Error while decoding stream #%d.%d\n",
  890. ist->file_index, ist->index);
  891. av_free_packet(&pkt);
  892. goto redo;
  893. }
  894. if (!got_picture) {
  895. /* no picture yet */
  896. ptr += ret;
  897. len -= ret;
  898. continue;
  899. }
  900. }
  901. break;
  902. default:
  903. goto fail_decode;
  904. }
  905. } else {
  906. data_buf = ptr;
  907. data_size = len;
  908. ret = len;
  909. }
  910. /* update pts */
  911. switch(ist->st->codec.codec_type) {
  912. case CODEC_TYPE_AUDIO:
  913. ist->pts = (INT64)1000000 * ist->sample_index / ist->st->codec.sample_rate;
  914. ist->sample_index += data_size / (2 * ist->st->codec.channels);
  915. ist->pts_increment = (INT64) (data_size / (2 * ist->st->codec.channels)) * 1000000 / ist->st->codec.sample_rate;
  916. break;
  917. case CODEC_TYPE_VIDEO:
  918. ist->frame_number++;
  919. ist->pts = ((INT64)ist->frame_number * 1000000 * FRAME_RATE_BASE) /
  920. ist->st->codec.frame_rate;
  921. ist->pts_increment = ((INT64) 1000000 * FRAME_RATE_BASE) /
  922. ist->st->codec.frame_rate;
  923. break;
  924. default:
  925. abort();
  926. }
  927. ptr += ret;
  928. len -= ret;
  929. /* transcode raw format, encode packets and output them */
  930. for(i=0;i<nb_ostreams;i++) {
  931. int frame_size;
  932. ost = ost_table[i];
  933. if (ost->source_index == ist_index) {
  934. os = output_files[ost->file_index];
  935. if (ost->encoding_needed) {
  936. switch(ost->st->codec.codec_type) {
  937. case CODEC_TYPE_AUDIO:
  938. do_audio_out(os, ost, ist, data_buf, data_size);
  939. break;
  940. case CODEC_TYPE_VIDEO:
  941. do_video_out(os, ost, ist, &picture, &frame_size);
  942. if (do_vstats)
  943. do_video_stats(ost, ist, frame_size);
  944. break;
  945. default:
  946. abort();
  947. }
  948. } else {
  949. /* no reencoding needed : output the packet directly */
  950. /* force the input stream PTS */
  951. os->format->write_packet(os, ost->index, data_buf, data_size, pkt.pts);
  952. }
  953. }
  954. }
  955. }
  956. av_free_packet(&pkt);
  957. /* dump report by using the first video and audio streams */
  958. {
  959. char buf[1024];
  960. AVFormatContext *oc;
  961. INT64 total_size, ti;
  962. AVCodecContext *enc;
  963. int frame_number, vid;
  964. double bitrate, ti1;
  965. static INT64 last_time;
  966. if ((min_pts - last_time) >= 500000) {
  967. last_time = min_pts;
  968. oc = output_files[0];
  969. total_size = url_ftell(&oc->pb);
  970. buf[0] = '\0';
  971. ti = MAXINT64;
  972. vid = 0;
  973. for(i=0;i<nb_ostreams;i++) {
  974. ost = ost_table[i];
  975. enc = &ost->st->codec;
  976. ist = ist_table[ost->source_index];
  977. if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
  978. frame_number = ist->frame_number;
  979. sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
  980. frame_number, enc->quality);
  981. if (do_psnr)
  982. sprintf(buf + strlen(buf), "PSNR=%6.2f ", enc->psnr_y);
  983. vid = 1;
  984. }
  985. /* compute min pts value */
  986. if (!ist->discard && ist->pts < ti) {
  987. ti = ist->pts;
  988. }
  989. }
  990. ti1 = (double)ti / 1000000.0;
  991. if (ti1 < 0.01)
  992. ti1 = 0.01;
  993. bitrate = (double)(total_size * 8) / ti1 / 1000.0;
  994. sprintf(buf + strlen(buf),
  995. "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
  996. (double)total_size / 1024, ti1, bitrate);
  997. fprintf(stderr, "%s \r", buf);
  998. fflush(stderr);
  999. }
  1000. }
  1001. }
  1002. term_exit();
  1003. /* dump report by using the first video and audio streams */
  1004. {
  1005. char buf[1024];
  1006. AVFormatContext *oc;
  1007. INT64 total_size, ti;
  1008. AVCodecContext *enc;
  1009. int frame_number, vid;
  1010. double bitrate, ti1;
  1011. oc = output_files[0];
  1012. total_size = url_ftell(&oc->pb);
  1013. buf[0] = '\0';
  1014. ti = MAXINT64;
  1015. vid = 0;
  1016. for(i=0;i<nb_ostreams;i++) {
  1017. ost = ost_table[i];
  1018. enc = &ost->st->codec;
  1019. ist = ist_table[ost->source_index];
  1020. if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
  1021. frame_number = ist->frame_number;
  1022. sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
  1023. frame_number, enc->quality);
  1024. if (do_psnr)
  1025. sprintf(buf + strlen(buf), "PSNR=%6.2f ", enc->psnr_y);
  1026. vid = 1;
  1027. }
  1028. /* compute min pts value */
  1029. if (!ist->discard && ist->pts < ti) {
  1030. ti = ist->pts;
  1031. }
  1032. }
  1033. ti1 = ti / 1000000.0;
  1034. if (ti1 < 0.01)
  1035. ti1 = 0.01;
  1036. bitrate = (double)(total_size * 8) / ti1 / 1000.0;
  1037. sprintf(buf + strlen(buf),
  1038. "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
  1039. (double)total_size / 1024, ti1, bitrate);
  1040. fprintf(stderr, "%s \n", buf);
  1041. }
  1042. /* close each encoder */
  1043. for(i=0;i<nb_ostreams;i++) {
  1044. ost = ost_table[i];
  1045. if (ost->encoding_needed) {
  1046. avcodec_close(&ost->st->codec);
  1047. }
  1048. }
  1049. /* close each decoder */
  1050. for(i=0;i<nb_istreams;i++) {
  1051. ist = ist_table[i];
  1052. if (ist->decoding_needed) {
  1053. avcodec_close(&ist->st->codec);
  1054. }
  1055. }
  1056. /* write the trailer if needed and close file */
  1057. for(i=0;i<nb_output_files;i++) {
  1058. os = output_files[i];
  1059. os->format->write_trailer(os);
  1060. }
  1061. /* finished ! */
  1062. ret = 0;
  1063. fail1:
  1064. free(file_table);
  1065. if (ist_table) {
  1066. for(i=0;i<nb_istreams;i++) {
  1067. ist = ist_table[i];
  1068. if (ist) {
  1069. free(ist);
  1070. }
  1071. }
  1072. free(ist_table);
  1073. }
  1074. if (ost_table) {
  1075. for(i=0;i<nb_ostreams;i++) {
  1076. ost = ost_table[i];
  1077. if (ost) {
  1078. if (ost->pict_tmp.data[0])
  1079. free(ost->pict_tmp.data[0]);
  1080. if (ost->video_resample)
  1081. img_resample_close(ost->img_resample_ctx);
  1082. if (ost->audio_resample)
  1083. audio_resample_close(ost->resample);
  1084. free(ost);
  1085. }
  1086. }
  1087. free(ost_table);
  1088. }
  1089. return ret;
  1090. fail:
  1091. ret = -ENOMEM;
  1092. goto fail1;
  1093. }
  1094. #if 0
  1095. int file_read(const char *filename)
  1096. {
  1097. URLContext *h;
  1098. unsigned char buffer[1024];
  1099. int len, i;
  1100. if (url_open(&h, filename, O_RDONLY) < 0) {
  1101. printf("could not open '%s'\n", filename);
  1102. return -1;
  1103. }
  1104. for(;;) {
  1105. len = url_read(h, buffer, sizeof(buffer));
  1106. if (len <= 0)
  1107. break;
  1108. for(i=0;i<len;i++) putchar(buffer[i]);
  1109. }
  1110. url_close(h);
  1111. return 0;
  1112. }
  1113. #endif
  1114. void show_licence(void)
  1115. {
  1116. printf(
  1117. "ffmpeg version " FFMPEG_VERSION "\n"
  1118. "Copyright (c) 2000,2001 Gerard Lantau\n"
  1119. "This program is free software; you can redistribute it and/or modify\n"
  1120. "it under the terms of the GNU General Public License as published by\n"
  1121. "the Free Software Foundation; either version 2 of the License, or\n"
  1122. "(at your option) any later version.\n"
  1123. "\n"
  1124. "This program is distributed in the hope that it will be useful,\n"
  1125. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1126. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  1127. "GNU General Public License for more details.\n"
  1128. "\n"
  1129. "You should have received a copy of the GNU General Public License\n"
  1130. "along with this program; if not, write to the Free Software\n"
  1131. "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"
  1132. );
  1133. exit(1);
  1134. }
  1135. void opt_format(const char *arg)
  1136. {
  1137. AVFormat *f;
  1138. f = first_format;
  1139. while (f != NULL && strcmp(f->name, arg) != 0) f = f->next;
  1140. if (f == NULL) {
  1141. fprintf(stderr, "Invalid format: %s\n", arg);
  1142. exit(1);
  1143. }
  1144. file_format = f;
  1145. }
  1146. void opt_video_bitrate(const char *arg)
  1147. {
  1148. video_bit_rate = atoi(arg) * 1000;
  1149. }
  1150. void opt_video_bitrate_tolerance(const char *arg)
  1151. {
  1152. video_bit_rate_tolerance = atoi(arg) * 1000;
  1153. }
  1154. void opt_frame_rate(const char *arg)
  1155. {
  1156. frame_rate = (int)(strtod(arg, 0) * FRAME_RATE_BASE);
  1157. }
  1158. void opt_frame_size(const char *arg)
  1159. {
  1160. parse_image_size(&frame_width, &frame_height, arg);
  1161. if (frame_width <= 0 || frame_height <= 0) {
  1162. fprintf(stderr, "Incorrect frame size\n");
  1163. exit(1);
  1164. }
  1165. if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
  1166. fprintf(stderr, "Frame size must be a multiple of 2\n");
  1167. exit(1);
  1168. }
  1169. }
  1170. void opt_gop_size(const char *arg)
  1171. {
  1172. gop_size = atoi(arg);
  1173. }
  1174. void opt_b_frames(const char *arg)
  1175. {
  1176. b_frames = atoi(arg);
  1177. if (b_frames > FF_MAX_B_FRAMES) {
  1178. fprintf(stderr, "\nCannot have more than %d B frames, increase FF_MAX_B_FRAMES.\n", FF_MAX_B_FRAMES);
  1179. exit(1);
  1180. } else if (b_frames < 1) {
  1181. fprintf(stderr, "\nNumber of B frames must be higher than 0\n");
  1182. exit(1);
  1183. }
  1184. }
  1185. void opt_qscale(const char *arg)
  1186. {
  1187. video_qscale = atoi(arg);
  1188. if (video_qscale < 0 ||
  1189. video_qscale > 31) {
  1190. fprintf(stderr, "qscale must be >= 1 and <= 31\n");
  1191. exit(1);
  1192. }
  1193. }
  1194. void opt_qmin(const char *arg)
  1195. {
  1196. video_qmin = atoi(arg);
  1197. if (video_qmin < 0 ||
  1198. video_qmin > 31) {
  1199. fprintf(stderr, "qmin must be >= 1 and <= 31\n");
  1200. exit(1);
  1201. }
  1202. }
  1203. void opt_qmax(const char *arg)
  1204. {
  1205. video_qmax = atoi(arg);
  1206. if (video_qmax < 0 ||
  1207. video_qmax > 31) {
  1208. fprintf(stderr, "qmax must be >= 1 and <= 31\n");
  1209. exit(1);
  1210. }
  1211. }
  1212. void opt_qdiff(const char *arg)
  1213. {
  1214. video_qdiff = atoi(arg);
  1215. if (video_qdiff < 0 ||
  1216. video_qdiff > 31) {
  1217. fprintf(stderr, "qdiff must be >= 1 and <= 31\n");
  1218. exit(1);
  1219. }
  1220. }
  1221. void opt_qblur(const char *arg)
  1222. {
  1223. video_qblur = atof(arg);
  1224. }
  1225. void opt_qcomp(const char *arg)
  1226. {
  1227. video_qcomp = atof(arg);
  1228. }
  1229. void opt_audio_bitrate(const char *arg)
  1230. {
  1231. audio_bit_rate = atoi(arg) * 1000;
  1232. }
  1233. void opt_audio_rate(const char *arg)
  1234. {
  1235. audio_sample_rate = atoi(arg);
  1236. }
  1237. void opt_audio_channels(const char *arg)
  1238. {
  1239. audio_channels = atoi(arg);
  1240. }
  1241. #ifdef CONFIG_GRAB
  1242. void opt_video_device(const char *arg)
  1243. {
  1244. v4l_device = strdup(arg);
  1245. }
  1246. void opt_audio_device(const char *arg)
  1247. {
  1248. audio_device = strdup(arg);
  1249. }
  1250. #endif
  1251. void opt_audio_codec(const char *arg)
  1252. {
  1253. AVCodec *p;
  1254. p = first_avcodec;
  1255. while (p) {
  1256. if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
  1257. break;
  1258. p = p->next;
  1259. }
  1260. if (p == NULL) {
  1261. fprintf(stderr, "Unknown audio codec '%s'\n", arg);
  1262. exit(1);
  1263. } else {
  1264. audio_codec_id = p->id;
  1265. }
  1266. }
  1267. const char *motion_str[] = {
  1268. "zero",
  1269. "full",
  1270. "log",
  1271. "phods",
  1272. "epzs",
  1273. "x1",
  1274. NULL,
  1275. };
  1276. void opt_motion_estimation(const char *arg)
  1277. {
  1278. const char **p;
  1279. p = motion_str;
  1280. for(;;) {
  1281. if (!*p) {
  1282. fprintf(stderr, "Unknown motion estimation method '%s'\n", arg);
  1283. exit(1);
  1284. }
  1285. if (!strcmp(*p, arg))
  1286. break;
  1287. p++;
  1288. }
  1289. me_method = (p - motion_str) + 1;
  1290. }
  1291. void opt_video_codec(const char *arg)
  1292. {
  1293. AVCodec *p;
  1294. p = first_avcodec;
  1295. while (p) {
  1296. if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
  1297. break;
  1298. p = p->next;
  1299. }
  1300. if (p == NULL) {
  1301. fprintf(stderr, "Unknown video codec '%s'\n", arg);
  1302. exit(1);
  1303. } else {
  1304. video_codec_id = p->id;
  1305. }
  1306. }
  1307. void opt_map(const char *arg)
  1308. {
  1309. AVStreamMap *m;
  1310. const char *p;
  1311. p = arg;
  1312. m = &stream_maps[nb_stream_maps++];
  1313. m->file_index = strtol(arg, (char **)&p, 0);
  1314. if (*p)
  1315. p++;
  1316. m->stream_index = strtol(arg, (char **)&p, 0);
  1317. }
  1318. void opt_recording_time(const char *arg)
  1319. {
  1320. recording_time = parse_date(arg, 1);
  1321. }
  1322. /* return the number of packet read to find the codec parameters */
  1323. int find_codec_parameters(AVFormatContext *ic)
  1324. {
  1325. int val, i, count, ret, got_picture, size;
  1326. AVCodec *codec;
  1327. AVCodecContext *enc;
  1328. AVStream *st;
  1329. AVPacket *pkt;
  1330. AVPicture picture;
  1331. AVPacketList *pktl, **ppktl;
  1332. short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
  1333. UINT8 *ptr;
  1334. count = 0;
  1335. ppktl = &ic->packet_buffer;
  1336. for(;;) {
  1337. for(i=0;i<ic->nb_streams;i++) {
  1338. enc = &ic->streams[i]->codec;
  1339. switch(enc->codec_type) {
  1340. case CODEC_TYPE_AUDIO:
  1341. val = enc->sample_rate;
  1342. break;
  1343. case CODEC_TYPE_VIDEO:
  1344. val = enc->width;
  1345. break;
  1346. default:
  1347. val = 1;
  1348. break;
  1349. }
  1350. /* if no parameters supplied, then we should read it from
  1351. the stream */
  1352. if (val == 0)
  1353. break;
  1354. }
  1355. if (i == ic->nb_streams) {
  1356. ret = count;
  1357. break;
  1358. }
  1359. if (count == 0) {
  1360. /* open each codec */
  1361. for(i=0;i<ic->nb_streams;i++) {
  1362. st = ic->streams[i];
  1363. codec = avcodec_find_decoder(st->codec.codec_id);
  1364. if (codec == NULL) {
  1365. ret = -1;
  1366. goto the_end;
  1367. }
  1368. ret = avcodec_open(&st->codec, codec);
  1369. if (ret < 0)
  1370. goto the_end;
  1371. }
  1372. }
  1373. pktl = av_mallocz(sizeof(AVPacketList));
  1374. if (!pktl) {
  1375. ret = -1;
  1376. break;
  1377. }
  1378. /* add the packet in the buffered packet list */
  1379. *ppktl = pktl;
  1380. ppktl = &pktl->next;
  1381. pkt = &pktl->pkt;
  1382. if (ic->format->read_packet(ic, pkt) < 0) {
  1383. ret = -1;
  1384. break;
  1385. }
  1386. st = ic->streams[pkt->stream_index];
  1387. /* decode the data and update codec parameters */
  1388. ptr = pkt->data;
  1389. size = pkt->size;
  1390. while (size > 0) {
  1391. switch(st->codec.codec_type) {
  1392. case CODEC_TYPE_VIDEO:
  1393. ret = avcodec_decode_video(&st->codec, &picture, &got_picture, ptr, size);
  1394. break;
  1395. case CODEC_TYPE_AUDIO:
  1396. ret = avcodec_decode_audio(&st->codec, samples, &got_picture, ptr, size);
  1397. break;
  1398. default:
  1399. ret = -1;
  1400. break;
  1401. }
  1402. if (ret < 0) {
  1403. ret = -1;
  1404. goto the_end;
  1405. }
  1406. if (got_picture)
  1407. break;
  1408. ptr += ret;
  1409. size -= ret;
  1410. }
  1411. count++;
  1412. }
  1413. the_end:
  1414. if (count > 0) {
  1415. /* close each codec */
  1416. for(i=0;i<ic->nb_streams;i++) {
  1417. st = ic->streams[i];
  1418. avcodec_close(&st->codec);
  1419. }
  1420. }
  1421. return ret;
  1422. }
  1423. int filename_number_test(const char *filename)
  1424. {
  1425. char buf[1024];
  1426. if (get_frame_filename(buf, sizeof(buf), filename, 1) < 0) {
  1427. fprintf(stderr, "%s: Incorrect image filename syntax.\n"
  1428. "Use '%%d' to specify the image number:\n"
  1429. " for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
  1430. " for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
  1431. filename);
  1432. return -1;
  1433. } else {
  1434. return 0;
  1435. }
  1436. }
  1437. void opt_input_file(const char *filename)
  1438. {
  1439. AVFormatContext *ic;
  1440. AVFormatParameters params, *ap = &params;
  1441. URLFormat url_format;
  1442. AVFormat *fmt;
  1443. int err, i, ret;
  1444. ic = av_mallocz(sizeof(AVFormatContext));
  1445. strcpy(ic->filename, filename);
  1446. /* first format guess to know if we must open file */
  1447. fmt = file_format;
  1448. if (!fmt)
  1449. fmt = guess_format(NULL, filename, NULL);
  1450. if (fmt == NULL || !(fmt->flags & AVFMT_NOFILE)) {
  1451. /* open file */
  1452. if (url_fopen(&ic->pb, filename, URL_RDONLY) < 0) {
  1453. fprintf(stderr, "Could not open '%s'\n", filename);
  1454. exit(1);
  1455. }
  1456. /* find format and set default parameters */
  1457. fmt = file_format;
  1458. err = url_getformat(url_fileno(&ic->pb), &url_format);
  1459. if (err >= 0) {
  1460. if (!fmt)
  1461. fmt = guess_format(url_format.format_name, NULL, NULL);
  1462. ap->sample_rate = url_format.sample_rate;
  1463. ap->frame_rate = url_format.frame_rate;
  1464. ap->channels = url_format.channels;
  1465. ap->width = url_format.width;
  1466. ap->height = url_format.height;
  1467. ap->pix_fmt = url_format.pix_fmt;
  1468. } else {
  1469. if (!fmt)
  1470. fmt = guess_format(NULL, filename, NULL);
  1471. memset(ap, 0, sizeof(*ap));
  1472. }
  1473. } else {
  1474. memset(ap, 0, sizeof(*ap));
  1475. }
  1476. if (!fmt || !fmt->read_header) {
  1477. fprintf(stderr, "%s: Unknown file format\n", filename);
  1478. exit(1);
  1479. }
  1480. ic->format = fmt;
  1481. /* get default parameters from command line */
  1482. if (!ap->sample_rate)
  1483. ap->sample_rate = audio_sample_rate;
  1484. if (!ap->channels)
  1485. ap->channels = audio_channels;
  1486. if (!ap->frame_rate)
  1487. ap->frame_rate = frame_rate;
  1488. if (!ap->width)
  1489. ap->width = frame_width;
  1490. if (!ap->height)
  1491. ap->height = frame_height;
  1492. /* check filename in case of an image number is expected */
  1493. if (ic->format->flags & AVFMT_NEEDNUMBER) {
  1494. if (filename_number_test(ic->filename) < 0)
  1495. exit(1);
  1496. }
  1497. err = ic->format->read_header(ic, ap);
  1498. if (err < 0) {
  1499. fprintf(stderr, "%s: Error while parsing header\n", filename);
  1500. exit(1);
  1501. }
  1502. /* If not enough info for the codecs, we decode the first frames
  1503. to get it. (used in mpeg case for example) */
  1504. ret = find_codec_parameters(ic);
  1505. if (ret < 0) {
  1506. fprintf(stderr, "%s: could not find codec parameters\n", filename);
  1507. exit(1);
  1508. }
  1509. /* update the current parameters so that they match the one of the input stream */
  1510. for(i=0;i<ic->nb_streams;i++) {
  1511. AVCodecContext *enc = &ic->streams[i]->codec;
  1512. switch(enc->codec_type) {
  1513. case CODEC_TYPE_AUDIO:
  1514. //fprintf(stderr, "\nInput Audio channels: %d", enc->channels);
  1515. audio_channels = enc->channels;
  1516. audio_sample_rate = enc->sample_rate;
  1517. break;
  1518. case CODEC_TYPE_VIDEO:
  1519. frame_height = enc->height;
  1520. frame_width = enc->width;
  1521. frame_rate = enc->frame_rate;
  1522. break;
  1523. default:
  1524. abort();
  1525. }
  1526. }
  1527. input_files[nb_input_files] = ic;
  1528. /* dump the file content */
  1529. dump_format(ic, nb_input_files, filename, 0);
  1530. nb_input_files++;
  1531. file_format = NULL;
  1532. }
  1533. void check_audio_video_inputs(int *has_video_ptr, int *has_audio_ptr)
  1534. {
  1535. int has_video, has_audio, i, j;
  1536. AVFormatContext *ic;
  1537. has_video = 0;
  1538. has_audio = 0;
  1539. for(j=0;j<nb_input_files;j++) {
  1540. ic = input_files[j];
  1541. for(i=0;i<ic->nb_streams;i++) {
  1542. AVCodecContext *enc = &ic->streams[i]->codec;
  1543. switch(enc->codec_type) {
  1544. case CODEC_TYPE_AUDIO:
  1545. has_audio = 1;
  1546. break;
  1547. case CODEC_TYPE_VIDEO:
  1548. has_video = 1;
  1549. break;
  1550. default:
  1551. abort();
  1552. }
  1553. }
  1554. }
  1555. *has_video_ptr = has_video;
  1556. *has_audio_ptr = has_audio;
  1557. }
  1558. void opt_output_file(const char *filename)
  1559. {
  1560. AVStream *st;
  1561. AVFormatContext *oc;
  1562. int use_video, use_audio, nb_streams, input_has_video, input_has_audio;
  1563. int codec_id;
  1564. if (!strcmp(filename, "-"))
  1565. filename = "pipe:";
  1566. oc = av_mallocz(sizeof(AVFormatContext));
  1567. if (!file_format) {
  1568. file_format = guess_format(NULL, filename, NULL);
  1569. if (!file_format)
  1570. file_format = &mpeg_mux_format;
  1571. }
  1572. oc->format = file_format;
  1573. if (!strcmp(file_format->name, "ffm") &&
  1574. strstart(filename, "http:", NULL)) {
  1575. /* special case for files sent to ffserver: we get the stream
  1576. parameters from ffserver */
  1577. if (read_ffserver_streams(oc, filename) < 0) {
  1578. fprintf(stderr, "Could not read stream parameters from '%s'\n", filename);
  1579. exit(1);
  1580. }
  1581. } else {
  1582. use_video = file_format->video_codec != CODEC_ID_NONE;
  1583. use_audio = file_format->audio_codec != CODEC_ID_NONE;
  1584. /* disable if no corresponding type found and at least one
  1585. input file */
  1586. if (nb_input_files > 0) {
  1587. check_audio_video_inputs(&input_has_video, &input_has_audio);
  1588. if (!input_has_video)
  1589. use_video = 0;
  1590. if (!input_has_audio)
  1591. use_audio = 0;
  1592. }
  1593. /* manual disable */
  1594. if (audio_disable) {
  1595. use_audio = 0;
  1596. }
  1597. if (video_disable) {
  1598. use_video = 0;
  1599. }
  1600. nb_streams = 0;
  1601. if (use_video) {
  1602. AVCodecContext *video_enc;
  1603. st = av_mallocz(sizeof(AVStream));
  1604. if (!st) {
  1605. fprintf(stderr, "Could not alloc stream\n");
  1606. exit(1);
  1607. }
  1608. video_enc = &st->codec;
  1609. codec_id = file_format->video_codec;
  1610. if (video_codec_id != CODEC_ID_NONE)
  1611. codec_id = video_codec_id;
  1612. video_enc->codec_id = codec_id;
  1613. video_enc->codec_type = CODEC_TYPE_VIDEO;
  1614. video_enc->bit_rate = video_bit_rate;
  1615. video_enc->bit_rate_tolerance = video_bit_rate_tolerance;
  1616. video_enc->frame_rate = frame_rate;
  1617. video_enc->width = frame_width;
  1618. video_enc->height = frame_height;
  1619. if (!intra_only)
  1620. video_enc->gop_size = gop_size;
  1621. else
  1622. video_enc->gop_size = 0;
  1623. if (video_qscale || same_quality) {
  1624. video_enc->flags |= CODEC_FLAG_QSCALE;
  1625. video_enc->quality = video_qscale;
  1626. }
  1627. if (use_hq) {
  1628. video_enc->flags |= CODEC_FLAG_HQ;
  1629. }
  1630. if (use_4mv) {
  1631. video_enc->flags |= CODEC_FLAG_HQ;
  1632. video_enc->flags |= CODEC_FLAG_4MV;
  1633. }
  1634. if (b_frames) {
  1635. if (codec_id != CODEC_ID_MPEG4) {
  1636. fprintf(stderr, "\nB frames encoding only supported by MPEG-4.\n");
  1637. exit(1);
  1638. }
  1639. video_enc->max_b_frames = b_frames;
  1640. video_enc->b_frame_strategy = 0;
  1641. video_enc->b_quant_factor = 2.0;
  1642. }
  1643. video_enc->qmin = video_qmin;
  1644. video_enc->qmax = video_qmax;
  1645. video_enc->max_qdiff = video_qdiff;
  1646. video_enc->qblur = video_qblur;
  1647. video_enc->qcompress = video_qcomp;
  1648. if (do_psnr)
  1649. video_enc->get_psnr = 1;
  1650. else
  1651. video_enc->get_psnr = 0;
  1652. video_enc->me_method = me_method;
  1653. /* XXX: need to find a way to set codec parameters */
  1654. if (oc->format == &ppm_format ||
  1655. oc->format == &ppmpipe_format) {
  1656. video_enc->pix_fmt = PIX_FMT_RGB24;
  1657. }
  1658. oc->streams[nb_streams] = st;
  1659. nb_streams++;
  1660. }
  1661. if (use_audio) {
  1662. AVCodecContext *audio_enc;
  1663. st = av_mallocz(sizeof(AVStream));
  1664. if (!st) {
  1665. fprintf(stderr, "Could not alloc stream\n");
  1666. exit(1);
  1667. }
  1668. audio_enc = &st->codec;
  1669. codec_id = file_format->audio_codec;
  1670. if (audio_codec_id != CODEC_ID_NONE)
  1671. codec_id = audio_codec_id;
  1672. audio_enc->codec_id = codec_id;
  1673. audio_enc->codec_type = CODEC_TYPE_AUDIO;
  1674. audio_enc->bit_rate = audio_bit_rate;
  1675. audio_enc->sample_rate = audio_sample_rate;
  1676. /* For audio codecs other than AC3 we limit */
  1677. /* the number of coded channels to stereo */
  1678. if (audio_channels > 2 && codec_id != CODEC_ID_AC3) {
  1679. audio_enc->channels = 2;
  1680. } else
  1681. audio_enc->channels = audio_channels;
  1682. oc->streams[nb_streams] = st;
  1683. nb_streams++;
  1684. }
  1685. oc->nb_streams = nb_streams;
  1686. if (!nb_streams) {
  1687. fprintf(stderr, "No audio or video streams available\n");
  1688. exit(1);
  1689. }
  1690. if (str_title)
  1691. nstrcpy(oc->title, sizeof(oc->title), str_title);
  1692. if (str_author)
  1693. nstrcpy(oc->author, sizeof(oc->author), str_author);
  1694. if (str_copyright)
  1695. nstrcpy(oc->copyright, sizeof(oc->copyright), str_copyright);
  1696. if (str_comment)
  1697. nstrcpy(oc->comment, sizeof(oc->comment), str_comment);
  1698. }
  1699. output_files[nb_output_files] = oc;
  1700. /* dump the file content */
  1701. dump_format(oc, nb_output_files, filename, 1);
  1702. nb_output_files++;
  1703. strcpy(oc->filename, filename);
  1704. /* check filename in case of an image number is expected */
  1705. if (oc->format->flags & AVFMT_NEEDNUMBER) {
  1706. if (filename_number_test(oc->filename) < 0)
  1707. exit(1);
  1708. }
  1709. if (!(oc->format->flags & AVFMT_NOFILE)) {
  1710. /* test if it already exists to avoid loosing precious files */
  1711. if (!file_overwrite &&
  1712. (strchr(filename, ':') == NULL ||
  1713. strstart(filename, "file:", NULL))) {
  1714. if (url_exist(filename)) {
  1715. int c;
  1716. printf("File '%s' already exists. Overwrite ? [y/N] ", filename);
  1717. fflush(stdout);
  1718. c = getchar();
  1719. if (toupper(c) != 'Y') {
  1720. fprintf(stderr, "Not overwriting - exiting\n");
  1721. exit(1);
  1722. }
  1723. }
  1724. }
  1725. /* open the file */
  1726. if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
  1727. fprintf(stderr, "Could not open '%s'\n", filename);
  1728. exit(1);
  1729. }
  1730. }
  1731. /* reset some options */
  1732. file_format = NULL;
  1733. audio_disable = 0;
  1734. video_disable = 0;
  1735. audio_codec_id = CODEC_ID_NONE;
  1736. video_codec_id = CODEC_ID_NONE;
  1737. }
  1738. #ifdef CONFIG_GRAB
  1739. /* prepare dummy protocols for grab */
  1740. void prepare_grab(void)
  1741. {
  1742. int has_video, has_audio, i, j;
  1743. AVFormatContext *oc;
  1744. AVFormatContext *ic;
  1745. AVFormatParameters ap1, *ap = &ap1;
  1746. /* see if audio/video inputs are needed */
  1747. has_video = 0;
  1748. has_audio = 0;
  1749. memset(ap, 0, sizeof(*ap));
  1750. for(j=0;j<nb_output_files;j++) {
  1751. oc = output_files[j];
  1752. for(i=0;i<oc->nb_streams;i++) {
  1753. AVCodecContext *enc = &oc->streams[i]->codec;
  1754. switch(enc->codec_type) {
  1755. case CODEC_TYPE_AUDIO:
  1756. if (enc->sample_rate > ap->sample_rate)
  1757. ap->sample_rate = enc->sample_rate;
  1758. if (enc->channels > ap->channels)
  1759. ap->channels = enc->channels;
  1760. has_audio = 1;
  1761. break;
  1762. case CODEC_TYPE_VIDEO:
  1763. if (enc->width > ap->width)
  1764. ap->width = enc->width;
  1765. if (enc->height > ap->height)
  1766. ap->height = enc->height;
  1767. if (enc->frame_rate > ap->frame_rate)
  1768. ap->frame_rate = enc->frame_rate;
  1769. has_video = 1;
  1770. break;
  1771. default:
  1772. abort();
  1773. }
  1774. }
  1775. }
  1776. if (has_video == 0 && has_audio == 0) {
  1777. fprintf(stderr, "Output file must have at least one audio or video stream\n");
  1778. exit(1);
  1779. }
  1780. if (has_video) {
  1781. ic = av_open_input_file("", "video_grab_device", 0, ap);
  1782. if (!ic) {
  1783. fprintf(stderr, "Could not open video grab device\n");
  1784. exit(1);
  1785. }
  1786. input_files[nb_input_files] = ic;
  1787. dump_format(ic, nb_input_files, v4l_device, 0);
  1788. nb_input_files++;
  1789. }
  1790. if (has_audio) {
  1791. ic = av_open_input_file("", "audio_device", 0, ap);
  1792. if (!ic) {
  1793. fprintf(stderr, "Could not open audio grab device\n");
  1794. exit(1);
  1795. }
  1796. input_files[nb_input_files] = ic;
  1797. dump_format(ic, nb_input_files, audio_device, 0);
  1798. nb_input_files++;
  1799. }
  1800. }
  1801. #else
  1802. void prepare_grab(void)
  1803. {
  1804. fprintf(stderr, "Must supply at least one input file\n");
  1805. exit(1);
  1806. }
  1807. #endif
  1808. /* open the necessary output devices for playing */
  1809. void prepare_play(void)
  1810. {
  1811. #ifndef __BEOS__
  1812. file_format = guess_format("audio_device", NULL, NULL);
  1813. if (!file_format) {
  1814. fprintf(stderr, "Could not find audio device\n");
  1815. exit(1);
  1816. }
  1817. opt_output_file(audio_device);
  1818. #endif
  1819. }
  1820. #ifndef CONFIG_WIN32
  1821. INT64 getutime(void)
  1822. {
  1823. struct rusage rusage;
  1824. getrusage(RUSAGE_SELF, &rusage);
  1825. return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
  1826. }
  1827. #else
  1828. INT64 getutime(void)
  1829. {
  1830. return gettime();
  1831. }
  1832. #endif
  1833. void show_formats(void)
  1834. {
  1835. AVFormat *f;
  1836. URLProtocol *up;
  1837. AVCodec *p;
  1838. const char **pp;
  1839. printf("File formats:\n");
  1840. printf(" Encoding:");
  1841. for(f = first_format; f != NULL; f = f->next) {
  1842. if (f->write_header)
  1843. printf(" %s", f->name);
  1844. }
  1845. printf("\n");
  1846. printf(" Decoding:");
  1847. for(f = first_format; f != NULL; f = f->next) {
  1848. if (f->read_header)
  1849. printf(" %s", f->name);
  1850. }
  1851. printf("\n");
  1852. printf("Codecs:\n");
  1853. printf(" Encoders:");
  1854. for(p = first_avcodec; p != NULL; p = p->next) {
  1855. if (p->encode)
  1856. printf(" %s", p->name);
  1857. }
  1858. printf("\n");
  1859. printf(" Decoders:");
  1860. for(p = first_avcodec; p != NULL; p = p->next) {
  1861. if (p->decode)
  1862. printf(" %s", p->name);
  1863. }
  1864. printf("\n");
  1865. printf("Supported file protocols:");
  1866. for(up = first_protocol; up != NULL; up = up->next)
  1867. printf(" %s:", up->name);
  1868. printf("\n");
  1869. printf("Frame size abbreviations: sqcif qcif cif 4cif\n");
  1870. printf("Motion estimation methods:");
  1871. pp = motion_str;
  1872. while (*pp) {
  1873. printf(" %s", *pp);
  1874. if ((pp - motion_str + 1) == ME_ZERO)
  1875. printf("(fastest)");
  1876. else if ((pp - motion_str + 1) == ME_FULL)
  1877. printf("(slowest)");
  1878. else if ((pp - motion_str + 1) == ME_EPZS)
  1879. printf("(default)");
  1880. pp++;
  1881. }
  1882. printf("\n");
  1883. exit(1);
  1884. }
  1885. void show_help(void)
  1886. {
  1887. const char *prog;
  1888. const OptionDef *po;
  1889. int i, expert;
  1890. prog = do_play ? "ffplay" : "ffmpeg";
  1891. printf("%s version " FFMPEG_VERSION ", Copyright (c) 2000, 2001 Gerard Lantau\n",
  1892. prog);
  1893. if (!do_play) {
  1894. printf("usage: ffmpeg [[options] -i input_file]... {[options] outfile}...\n"
  1895. "Hyper fast MPEG1/MPEG4/H263/RV and AC3/MPEG audio encoder\n");
  1896. } else {
  1897. printf("usage: ffplay [options] input_file...\n"
  1898. "Simple audio player\n");
  1899. }
  1900. printf("\n"
  1901. "Main options are:\n");
  1902. for(i=0;i<2;i++) {
  1903. if (i == 1)
  1904. printf("\nAdvanced options are:\n");
  1905. for(po = options; po->name != NULL; po++) {
  1906. char buf[64];
  1907. expert = (po->flags & OPT_EXPERT) != 0;
  1908. if (expert == i) {
  1909. strcpy(buf, po->name);
  1910. if (po->flags & HAS_ARG) {
  1911. strcat(buf, " ");
  1912. strcat(buf, po->argname);
  1913. }
  1914. printf("-%-17s %s\n", buf, po->help);
  1915. }
  1916. }
  1917. }
  1918. exit(1);
  1919. }
  1920. const OptionDef options[] = {
  1921. { "L", 0, {(void*)show_licence}, "show license" },
  1922. { "h", 0, {(void*)show_help}, "show help" },
  1923. { "formats", 0, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
  1924. { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
  1925. { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" },
  1926. { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
  1927. { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file:stream" },
  1928. { "t", HAS_ARG, {(void*)opt_recording_time}, "set the recording time", "duration" },
  1929. { "title", HAS_ARG | OPT_STRING, {(void*)&str_title}, "set the title", "string" },
  1930. { "author", HAS_ARG | OPT_STRING, {(void*)&str_author}, "set the author", "string" },
  1931. { "copyright", HAS_ARG | OPT_STRING, {(void*)&str_copyright}, "set the copyright", "string" },
  1932. { "comment", HAS_ARG | OPT_STRING, {(void*)&str_comment}, "set the comment", "string" },
  1933. /* video options */
  1934. { "b", HAS_ARG, {(void*)opt_video_bitrate}, "set video bitrate (in kbit/s)", "bitrate" },
  1935. { "r", HAS_ARG, {(void*)opt_frame_rate}, "set frame rate (in Hz)", "rate" },
  1936. { "s", HAS_ARG, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" },
  1937. { "g", HAS_ARG | OPT_EXPERT, {(void*)opt_gop_size}, "set the group of picture size", "gop_size" },
  1938. { "intra", OPT_BOOL | OPT_EXPERT, {(void*)&intra_only}, "use only intra frames"},
  1939. { "vn", OPT_BOOL, {(void*)&video_disable}, "disable video" },
  1940. { "qscale", HAS_ARG | OPT_EXPERT, {(void*)opt_qscale}, "use fixed video quantiser scale (VBR)", "q" },
  1941. { "qmin", HAS_ARG | OPT_EXPERT, {(void*)opt_qmin}, "min video quantiser scale (VBR)", "q" },
  1942. { "qmax", HAS_ARG | OPT_EXPERT, {(void*)opt_qmax}, "max video quantiser scale (VBR)", "q" },
  1943. { "qdiff", HAS_ARG | OPT_EXPERT, {(void*)opt_qdiff}, "max difference between the quantiser scale (VBR)", "q" },
  1944. { "qblur", HAS_ARG | OPT_EXPERT, {(void*)opt_qblur}, "video quantiser scale blur (VBR)", "blur" },
  1945. { "qcomp", HAS_ARG | OPT_EXPERT, {(void*)opt_qcomp}, "video quantiser scale compression (VBR)", "compression" },
  1946. { "bt", HAS_ARG, {(void*)opt_video_bitrate_tolerance}, "set video bitrate tolerance (in kbit/s)", "tolerance" },
  1947. #ifdef CONFIG_GRAB
  1948. { "vd", HAS_ARG | OPT_EXPERT, {(void*)opt_video_device}, "set video device", "device" },
  1949. #endif
  1950. { "vcodec", HAS_ARG | OPT_EXPERT, {(void*)opt_video_codec}, "force video codec", "codec" },
  1951. { "me", HAS_ARG | OPT_EXPERT, {(void*)opt_motion_estimation}, "set motion estimation method",
  1952. "method" },
  1953. { "bf", HAS_ARG | OPT_EXPERT, {(void*)opt_b_frames}, "use 'frames' B frames (only MPEG-4)", "frames" },
  1954. { "hq", OPT_BOOL | OPT_EXPERT, {(void*)&use_hq}, "activate high quality settings" },
  1955. { "4mv", OPT_BOOL | OPT_EXPERT, {(void*)&use_4mv}, "use four motion vector by macroblock (only MPEG-4)" },
  1956. { "sameq", OPT_BOOL, {(void*)&same_quality},
  1957. "use same video quality as source (implies VBR)" },
  1958. /* audio options */
  1959. { "ab", HAS_ARG, {(void*)opt_audio_bitrate}, "set audio bitrate (in kbit/s)", "bitrate", },
  1960. { "ar", HAS_ARG, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" },
  1961. { "ac", HAS_ARG, {(void*)opt_audio_channels}, "set number of audio channels", "channels" },
  1962. { "an", OPT_BOOL, {(void*)&audio_disable}, "disable audio" },
  1963. #ifdef CONFIG_GRAB
  1964. { "ad", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_device}, "set audio device", "device" },
  1965. #endif
  1966. { "acodec", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_codec}, "force audio codec", "codec" },
  1967. { "deinterlace", OPT_BOOL | OPT_EXPERT, {(void*)&do_deinterlace},
  1968. "deinterlace pictures" },
  1969. { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark},
  1970. "add timings for benchmarking" },
  1971. { "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump},
  1972. "dump each input packet" },
  1973. { "psnr", OPT_BOOL | OPT_EXPERT, {(void*)&do_psnr}, "calculate PSNR of compressed frames" },
  1974. { "vstats", OPT_BOOL | OPT_EXPERT, {(void*)&do_vstats}, "dump video coding statistics to file" },
  1975. { NULL, },
  1976. };
  1977. int main(int argc, char **argv)
  1978. {
  1979. int optindex, i;
  1980. const char *opt, *arg;
  1981. const OptionDef *po;
  1982. INT64 ti;
  1983. register_all();
  1984. /* detect if invoked as player */
  1985. i = strlen(argv[0]);
  1986. if (i >= 6 && !strcmp(argv[0] + i - 6, "ffplay"))
  1987. do_play = 1;
  1988. if (argc <= 1)
  1989. show_help();
  1990. /* parse options */
  1991. optindex = 1;
  1992. while (optindex < argc) {
  1993. opt = argv[optindex++];
  1994. if (opt[0] == '-' && opt[1] != '\0') {
  1995. po = options;
  1996. while (po->name != NULL) {
  1997. if (!strcmp(opt + 1, po->name))
  1998. break;
  1999. po++;
  2000. }
  2001. if (!po->name) {
  2002. fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
  2003. exit(1);
  2004. }
  2005. arg = NULL;
  2006. if (po->flags & HAS_ARG)
  2007. arg = argv[optindex++];
  2008. if (po->flags & OPT_STRING) {
  2009. char *str;
  2010. str = strdup(arg);
  2011. *po->u.str_arg = str;
  2012. } else if (po->flags & OPT_BOOL) {
  2013. *po->u.int_arg = 1;
  2014. } else {
  2015. po->u.func_arg(arg);
  2016. }
  2017. } else {
  2018. if (!do_play) {
  2019. opt_output_file(opt);
  2020. } else {
  2021. opt_input_file(opt);
  2022. }
  2023. }
  2024. }
  2025. if (!do_play) {
  2026. /* file converter / grab */
  2027. if (nb_output_files <= 0) {
  2028. fprintf(stderr, "Must supply at least one output file\n");
  2029. exit(1);
  2030. }
  2031. if (nb_input_files == 0) {
  2032. prepare_grab();
  2033. }
  2034. } else {
  2035. /* player */
  2036. if (nb_input_files <= 0) {
  2037. fprintf(stderr, "Must supply at least one input file\n");
  2038. exit(1);
  2039. }
  2040. prepare_play();
  2041. }
  2042. ti = getutime();
  2043. av_encode(output_files, nb_output_files, input_files, nb_input_files,
  2044. stream_maps, nb_stream_maps);
  2045. ti = getutime() - ti;
  2046. if (do_benchmark) {
  2047. printf("bench: utime=%0.3fs\n", ti / 1000000.0);
  2048. }
  2049. /* close files */
  2050. for(i=0;i<nb_output_files;i++) {
  2051. if (!(output_files[i]->format->flags & AVFMT_NOFILE))
  2052. url_fclose(&output_files[i]->pb);
  2053. }
  2054. for(i=0;i<nb_input_files;i++) {
  2055. if (!(input_files[i]->format->flags & AVFMT_NOFILE))
  2056. url_fclose(&input_files[i]->pb);
  2057. }
  2058. return 0;
  2059. }