1/* 2 * Copyright (C) 2005 Matthieu CASTET 3 * 4 * This file is part of FFmpeg. 5 * 6 * FFmpeg is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * FFmpeg is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with FFmpeg; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 */ 20 21#include <stdlib.h> 22#include "libavcodec/get_bits.h" 23#include "libavcodec/flac.h" 24#include "avformat.h" 25#include "internal.h" 26#include "oggdec.h" 27 28#define OGG_FLAC_METADATA_TYPE_STREAMINFO 0x7F 29 30static int 31flac_header (AVFormatContext * s, int idx) 32{ 33 struct ogg *ogg = s->priv_data; 34 struct ogg_stream *os = ogg->streams + idx; 35 AVStream *st = s->streams[idx]; 36 GetBitContext gb; 37 int mdt, ret; 38 39 if (os->buf[os->pstart] == 0xff) 40 return 0; 41 42 init_get_bits(&gb, os->buf + os->pstart, os->psize*8); 43 skip_bits1(&gb); /* metadata_last */ 44 mdt = get_bits(&gb, 7); 45 46 if (mdt == OGG_FLAC_METADATA_TYPE_STREAMINFO) { 47 uint8_t *streaminfo_start = os->buf + os->pstart + 5 + 4 + 4 + 4; 48 uint32_t samplerate; 49 50 skip_bits_long(&gb, 4*8); /* "FLAC" */ 51 if(get_bits(&gb, 8) != 1) /* unsupported major version */ 52 return -1; 53 skip_bits(&gb, 8 + 16); /* minor version + header count */ 54 skip_bits_long(&gb, 4*8); /* "fLaC" */ 55 56 /* METADATA_BLOCK_HEADER */ 57 if (get_bits_long(&gb, 32) != FLAC_STREAMINFO_SIZE) 58 return -1; 59 60 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; 61 st->codecpar->codec_id = AV_CODEC_ID_FLAC; 62 ffstream(st)->need_parsing = AVSTREAM_PARSE_HEADERS; 63 64 if ((ret = ff_alloc_extradata(st->codecpar, FLAC_STREAMINFO_SIZE)) < 0) 65 return ret; 66 memcpy(st->codecpar->extradata, streaminfo_start, st->codecpar->extradata_size); 67 68 samplerate = AV_RB24(st->codecpar->extradata + 10) >> 4; 69 if (!samplerate) 70 return AVERROR_INVALIDDATA; 71 72 avpriv_set_pts_info(st, 64, 1, samplerate); 73 } else if (mdt == FLAC_METADATA_TYPE_VORBIS_COMMENT) { 74 ff_vorbis_stream_comment(s, st, os->buf + os->pstart + 4, os->psize - 4); 75 } 76 77 return 1; 78} 79 80static int 81old_flac_header (AVFormatContext * s, int idx) 82{ 83 struct ogg *ogg = s->priv_data; 84 AVStream *st = s->streams[idx]; 85 struct ogg_stream *os = ogg->streams + idx; 86 AVCodecParserContext *parser = av_parser_init(AV_CODEC_ID_FLAC); 87 AVCodecContext *avctx; 88 int size, ret; 89 uint8_t *data; 90 91 if (!parser) 92 return -1; 93 94 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; 95 st->codecpar->codec_id = AV_CODEC_ID_FLAC; 96 97 avctx = avcodec_alloc_context3(NULL); 98 if (!avctx) { 99 ret = AVERROR(ENOMEM); 100 goto fail; 101 } 102 103 ret = avcodec_parameters_to_context(avctx, st->codecpar); 104 if (ret < 0) 105 goto fail; 106 107 parser->flags = PARSER_FLAG_COMPLETE_FRAMES; 108 av_parser_parse2(parser, avctx, 109 &data, &size, os->buf + os->pstart, os->psize, 110 AV_NOPTS_VALUE, AV_NOPTS_VALUE, -1); 111 112 av_parser_close(parser); 113 114 if (avctx->sample_rate) { 115 avpriv_set_pts_info(st, 64, 1, avctx->sample_rate); 116 avcodec_free_context(&avctx); 117 return 0; 118 } 119 120 avcodec_free_context(&avctx); 121 return 1; 122fail: 123 av_parser_close(parser); 124 avcodec_free_context(&avctx); 125 return ret; 126} 127 128const struct ogg_codec ff_flac_codec = { 129 .magic = "\177FLAC", 130 .magicsize = 5, 131 .header = flac_header, 132 .nb_header = 2, 133}; 134 135const struct ogg_codec ff_old_flac_codec = { 136 .magic = "fLaC", 137 .magicsize = 4, 138 .header = old_flac_header, 139 .nb_header = 0, 140}; 141