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.

2481 lines
75KB

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