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.

2191 lines
68KB

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