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.

2073 lines
61KB

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