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.

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