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.

2323 lines
70KB

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