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.

2196 lines
68KB

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