yading@11: /* yading@11: * Copyright (C) 2005 Matthieu CASTET yading@11: * yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: #include yading@11: #include "libavcodec/get_bits.h" yading@11: #include "libavcodec/flac.h" yading@11: #include "avformat.h" yading@11: #include "internal.h" yading@11: #include "oggdec.h" yading@11: yading@11: #define OGG_FLAC_METADATA_TYPE_STREAMINFO 0x7F yading@11: yading@11: static int yading@11: flac_header (AVFormatContext * s, int idx) yading@11: { yading@11: struct ogg *ogg = s->priv_data; yading@11: struct ogg_stream *os = ogg->streams + idx; yading@11: AVStream *st = s->streams[idx]; yading@11: GetBitContext gb; yading@11: FLACStreaminfo si; yading@11: int mdt; yading@11: yading@11: if (os->buf[os->pstart] == 0xff) yading@11: return 0; yading@11: yading@11: init_get_bits(&gb, os->buf + os->pstart, os->psize*8); yading@11: skip_bits1(&gb); /* metadata_last */ yading@11: mdt = get_bits(&gb, 7); yading@11: yading@11: if (mdt == OGG_FLAC_METADATA_TYPE_STREAMINFO) { yading@11: uint8_t *streaminfo_start = os->buf + os->pstart + 5 + 4 + 4 + 4; yading@11: skip_bits_long(&gb, 4*8); /* "FLAC" */ yading@11: if(get_bits(&gb, 8) != 1) /* unsupported major version */ yading@11: return -1; yading@11: skip_bits_long(&gb, 8 + 16); /* minor version + header count */ yading@11: skip_bits_long(&gb, 4*8); /* "fLaC" */ yading@11: yading@11: /* METADATA_BLOCK_HEADER */ yading@11: if (get_bits_long(&gb, 32) != FLAC_STREAMINFO_SIZE) yading@11: return -1; yading@11: yading@11: avpriv_flac_parse_streaminfo(st->codec, &si, streaminfo_start); yading@11: yading@11: st->codec->codec_type = AVMEDIA_TYPE_AUDIO; yading@11: st->codec->codec_id = AV_CODEC_ID_FLAC; yading@11: st->need_parsing = AVSTREAM_PARSE_HEADERS; yading@11: yading@11: st->codec->extradata = yading@11: av_malloc(FLAC_STREAMINFO_SIZE + FF_INPUT_BUFFER_PADDING_SIZE); yading@11: memcpy(st->codec->extradata, streaminfo_start, FLAC_STREAMINFO_SIZE); yading@11: st->codec->extradata_size = FLAC_STREAMINFO_SIZE; yading@11: yading@11: avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate); yading@11: } else if (mdt == FLAC_METADATA_TYPE_VORBIS_COMMENT) { yading@11: ff_vorbis_comment (s, &st->metadata, os->buf + os->pstart + 4, os->psize - 4); yading@11: } yading@11: yading@11: return 1; yading@11: } yading@11: yading@11: static int yading@11: old_flac_header (AVFormatContext * s, int idx) yading@11: { yading@11: AVStream *st = s->streams[idx]; yading@11: st->codec->codec_type = AVMEDIA_TYPE_AUDIO; yading@11: st->codec->codec_id = AV_CODEC_ID_FLAC; yading@11: yading@11: return 0; yading@11: } yading@11: yading@11: const struct ogg_codec ff_flac_codec = { yading@11: .magic = "\177FLAC", yading@11: .magicsize = 5, yading@11: .header = flac_header, yading@11: .nb_header = 2, yading@11: }; yading@11: yading@11: const struct ogg_codec ff_old_flac_codec = { yading@11: .magic = "fLaC", yading@11: .magicsize = 4, yading@11: .header = old_flac_header, yading@11: .nb_header = 0, yading@11: };