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.

2490 lines
76KB

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