00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <stdlib.h>
00022 #include "libavcodec/get_bits.h"
00023 #include "libavcodec/flac.h"
00024 #include "avformat.h"
00025 #include "internal.h"
00026 #include "oggdec.h"
00027
00028 #define OGG_FLAC_METADATA_TYPE_STREAMINFO 0x7F
00029
00030 static int
00031 flac_header (AVFormatContext * s, int idx)
00032 {
00033 struct ogg *ogg = s->priv_data;
00034 struct ogg_stream *os = ogg->streams + idx;
00035 AVStream *st = s->streams[idx];
00036 GetBitContext gb;
00037 FLACStreaminfo si;
00038 int mdt;
00039
00040 if (os->buf[os->pstart] == 0xff)
00041 return 0;
00042
00043 init_get_bits(&gb, os->buf + os->pstart, os->psize*8);
00044 skip_bits1(&gb);
00045 mdt = get_bits(&gb, 7);
00046
00047 if (mdt == OGG_FLAC_METADATA_TYPE_STREAMINFO) {
00048 uint8_t *streaminfo_start = os->buf + os->pstart + 5 + 4 + 4 + 4;
00049 skip_bits_long(&gb, 4*8);
00050 if(get_bits(&gb, 8) != 1)
00051 return -1;
00052 skip_bits_long(&gb, 8 + 16);
00053 skip_bits_long(&gb, 4*8);
00054
00055
00056 if (get_bits_long(&gb, 32) != FLAC_STREAMINFO_SIZE)
00057 return -1;
00058
00059 avpriv_flac_parse_streaminfo(st->codec, &si, streaminfo_start);
00060
00061 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00062 st->codec->codec_id = AV_CODEC_ID_FLAC;
00063 st->need_parsing = AVSTREAM_PARSE_HEADERS;
00064
00065 st->codec->extradata =
00066 av_malloc(FLAC_STREAMINFO_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
00067 memcpy(st->codec->extradata, streaminfo_start, FLAC_STREAMINFO_SIZE);
00068 st->codec->extradata_size = FLAC_STREAMINFO_SIZE;
00069
00070 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
00071 } else if (mdt == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
00072 ff_vorbis_comment (s, &st->metadata, os->buf + os->pstart + 4, os->psize - 4);
00073 }
00074
00075 return 1;
00076 }
00077
00078 static int
00079 old_flac_header (AVFormatContext * s, int idx)
00080 {
00081 AVStream *st = s->streams[idx];
00082 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00083 st->codec->codec_id = AV_CODEC_ID_FLAC;
00084
00085 return 0;
00086 }
00087
00088 const struct ogg_codec ff_flac_codec = {
00089 .magic = "\177FLAC",
00090 .magicsize = 5,
00091 .header = flac_header,
00092 .nb_header = 2,
00093 };
00094
00095 const struct ogg_codec ff_old_flac_codec = {
00096 .magic = "fLaC",
00097 .magicsize = 4,
00098 .header = old_flac_header,
00099 .nb_header = 0,
00100 };