X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Fflac.c;h=d624beb61f21fb09833dd7dd691afd940a1b85c1;hb=9bf41210a9a7511f44ea1d7fd2646430889a7c2b;hp=2e3197f483b4fb731b0a79874346551e6b5c822a;hpb=7f4e432148779b338a6199f50eb70845c78fd060;p=ffmpeg diff --git a/libavcodec/flac.c b/libavcodec/flac.c index 2e3197f483b..d624beb61f2 100644 --- a/libavcodec/flac.c +++ b/libavcodec/flac.c @@ -2,24 +2,27 @@ * FLAC common code * Copyright (c) 2009 Justin Ruggles * - * This file is part of FFmpeg. + * This file is part of Libav. * - * FFmpeg is free software; you can redistribute it and/or + * Libav is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * - * FFmpeg is distributed in the hope that it will be useful, + * Libav is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with FFmpeg; if not, write to the Free Software + * License along with Libav; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "libavutil/crc.h" +#include "libavutil/log.h" +#include "bytestream.h" +#include "get_bits.h" #include "flac.h" #include "flacdata.h" @@ -33,18 +36,18 @@ static int64_t get_utf8(GetBitContext *gb) } int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb, - FLACFrameInfo *fi) + FLACFrameInfo *fi, int log_level_offset) { int bs_code, sr_code, bps_code; /* frame sync code */ if ((get_bits(gb, 15) & 0x7FFF) != 0x7FFC) { - av_log(avctx, AV_LOG_ERROR, "invalid sync code\n"); + av_log(avctx, AV_LOG_ERROR + log_level_offset, "invalid sync code\n"); return -1; } /* variable block size stream code */ - skip_bits1(gb); + fi->is_var_size = get_bits1(gb); /* block size and sample rate codes */ bs_code = get_bits(gb, 4); @@ -55,17 +58,20 @@ int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb, if (fi->ch_mode < FLAC_MAX_CHANNELS) { fi->channels = fi->ch_mode + 1; fi->ch_mode = FLAC_CHMODE_INDEPENDENT; - } else if (fi->ch_mode <= FLAC_CHMODE_MID_SIDE) { + } else if (fi->ch_mode < FLAC_MAX_CHANNELS + FLAC_CHMODE_MID_SIDE) { fi->channels = 2; + fi->ch_mode -= FLAC_MAX_CHANNELS - 1; } else { - av_log(avctx, AV_LOG_ERROR, "invalid channel mode: %d\n", fi->ch_mode); + av_log(avctx, AV_LOG_ERROR + log_level_offset, + "invalid channel mode: %d\n", fi->ch_mode); return -1; } /* bits per sample */ bps_code = get_bits(gb, 3); if (bps_code == 3 || bps_code == 7) { - av_log(avctx, AV_LOG_ERROR, "invalid sample size code (%d)\n", + av_log(avctx, AV_LOG_ERROR + log_level_offset, + "invalid sample size code (%d)\n", bps_code); return -1; } @@ -73,19 +79,23 @@ int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb, /* reserved bit */ if (get_bits1(gb)) { - av_log(avctx, AV_LOG_ERROR, "broken stream, invalid padding\n"); + av_log(avctx, AV_LOG_ERROR + log_level_offset, + "broken stream, invalid padding\n"); return -1; } /* sample or frame count */ - if (get_utf8(gb) < 0) { - av_log(avctx, AV_LOG_ERROR, "utf8 fscked\n"); + fi->frame_or_sample_num = get_utf8(gb); + if (fi->frame_or_sample_num < 0) { + av_log(avctx, AV_LOG_ERROR + log_level_offset, + "sample/frame number invalid; utf8 fscked\n"); return -1; } /* blocksize */ if (bs_code == 0) { - av_log(avctx, AV_LOG_ERROR, "reserved blocksize code: 0\n"); + av_log(avctx, AV_LOG_ERROR + log_level_offset, + "reserved blocksize code: 0\n"); return -1; } else if (bs_code == 6) { fi->blocksize = get_bits(gb, 8) + 1; @@ -105,7 +115,8 @@ int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb, } else if (sr_code == 14) { fi->samplerate = get_bits(gb, 16) * 10; } else { - av_log(avctx, AV_LOG_ERROR, "illegal sample rate code %d\n", + av_log(avctx, AV_LOG_ERROR + log_level_offset, + "illegal sample rate code %d\n", sr_code); return -1; } @@ -114,7 +125,8 @@ int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb, skip_bits(gb, 8); if (av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, gb->buffer, get_bits_count(gb)/8)) { - av_log(avctx, AV_LOG_ERROR, "header crc mismatch\n"); + av_log(avctx, AV_LOG_ERROR + log_level_offset, + "header crc mismatch\n"); return -1; } @@ -141,3 +153,74 @@ int ff_flac_get_max_frame_size(int blocksize, int ch, int bps) return count; } + +int avpriv_flac_is_extradata_valid(AVCodecContext *avctx, + enum FLACExtradataFormat *format, + uint8_t **streaminfo_start) +{ + if (!avctx->extradata || avctx->extradata_size < FLAC_STREAMINFO_SIZE) { + av_log(avctx, AV_LOG_ERROR, "extradata NULL or too small.\n"); + return 0; + } + if (AV_RL32(avctx->extradata) != MKTAG('f','L','a','C')) { + /* extradata contains STREAMINFO only */ + if (avctx->extradata_size != FLAC_STREAMINFO_SIZE) { + av_log(avctx, AV_LOG_WARNING, "extradata contains %d bytes too many.\n", + FLAC_STREAMINFO_SIZE-avctx->extradata_size); + } + *format = FLAC_EXTRADATA_FORMAT_STREAMINFO; + *streaminfo_start = avctx->extradata; + } else { + if (avctx->extradata_size < 8+FLAC_STREAMINFO_SIZE) { + av_log(avctx, AV_LOG_ERROR, "extradata too small.\n"); + return 0; + } + *format = FLAC_EXTRADATA_FORMAT_FULL_HEADER; + *streaminfo_start = &avctx->extradata[8]; + } + return 1; +} + +void avpriv_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s, + const uint8_t *buffer) +{ + GetBitContext gb; + init_get_bits(&gb, buffer, FLAC_STREAMINFO_SIZE*8); + + skip_bits(&gb, 16); /* skip min blocksize */ + s->max_blocksize = get_bits(&gb, 16); + if (s->max_blocksize < FLAC_MIN_BLOCKSIZE) { + av_log(avctx, AV_LOG_WARNING, "invalid max blocksize: %d\n", + s->max_blocksize); + s->max_blocksize = 16; + } + + skip_bits(&gb, 24); /* skip min frame size */ + s->max_framesize = get_bits_long(&gb, 24); + + s->samplerate = get_bits_long(&gb, 20); + s->channels = get_bits(&gb, 3) + 1; + s->bps = get_bits(&gb, 5) + 1; + + avctx->channels = s->channels; + avctx->sample_rate = s->samplerate; + avctx->bits_per_raw_sample = s->bps; + + s->samples = get_bits_long(&gb, 32) << 4; + s->samples |= get_bits(&gb, 4); + + skip_bits_long(&gb, 64); /* md5 sum */ + skip_bits_long(&gb, 64); /* md5 sum */ +} + +void avpriv_flac_parse_block_header(const uint8_t *block_header, + int *last, int *type, int *size) +{ + int tmp = bytestream_get_byte(&block_header); + if (last) + *last = tmp & 0x80; + if (type) + *type = tmp & 0x7F; + if (size) + *size = bytestream_get_be24(&block_header); +}