| 1 | /* |
| 2 | * FLAC (Free Lossless Audio Codec) decoder/demuxer common functions |
| 3 | * Copyright (c) 2008 Justin Ruggles |
| 4 | * |
| 5 | * This file is part of FFmpeg. |
| 6 | * |
| 7 | * FFmpeg is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation; either |
| 10 | * version 2.1 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * FFmpeg is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with FFmpeg; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * @file |
| 24 | * FLAC (Free Lossless Audio Codec) decoder/demuxer common functions |
| 25 | */ |
| 26 | |
| 27 | #ifndef AVCODEC_FLAC_H |
| 28 | #define AVCODEC_FLAC_H |
| 29 | |
| 30 | #include "avcodec.h" |
| 31 | #include "bytestream.h" |
| 32 | #include "get_bits.h" |
| 33 | |
| 34 | #define FLAC_STREAMINFO_SIZE 34 |
| 35 | #define FLAC_MAX_CHANNELS 8 |
| 36 | #define FLAC_MIN_BLOCKSIZE 16 |
| 37 | #define FLAC_MAX_BLOCKSIZE 65535 |
| 38 | #define FLAC_MIN_FRAME_SIZE 11 |
| 39 | |
| 40 | enum { |
| 41 | FLAC_CHMODE_INDEPENDENT = 0, |
| 42 | FLAC_CHMODE_LEFT_SIDE = 1, |
| 43 | FLAC_CHMODE_RIGHT_SIDE = 2, |
| 44 | FLAC_CHMODE_MID_SIDE = 3, |
| 45 | }; |
| 46 | |
| 47 | enum { |
| 48 | FLAC_METADATA_TYPE_STREAMINFO = 0, |
| 49 | FLAC_METADATA_TYPE_PADDING, |
| 50 | FLAC_METADATA_TYPE_APPLICATION, |
| 51 | FLAC_METADATA_TYPE_SEEKTABLE, |
| 52 | FLAC_METADATA_TYPE_VORBIS_COMMENT, |
| 53 | FLAC_METADATA_TYPE_CUESHEET, |
| 54 | FLAC_METADATA_TYPE_PICTURE, |
| 55 | FLAC_METADATA_TYPE_INVALID = 127 |
| 56 | }; |
| 57 | |
| 58 | enum FLACExtradataFormat { |
| 59 | FLAC_EXTRADATA_FORMAT_STREAMINFO = 0, |
| 60 | FLAC_EXTRADATA_FORMAT_FULL_HEADER = 1 |
| 61 | }; |
| 62 | |
| 63 | #define FLACCOMMONINFO \ |
| 64 | int samplerate; /**< sample rate */\ |
| 65 | int channels; /**< number of channels */\ |
| 66 | int bps; /**< bits-per-sample */\ |
| 67 | |
| 68 | /** |
| 69 | * Data needed from the Streaminfo header for use by the raw FLAC demuxer |
| 70 | * and/or the FLAC decoder. |
| 71 | */ |
| 72 | #define FLACSTREAMINFO \ |
| 73 | FLACCOMMONINFO \ |
| 74 | int max_blocksize; /**< maximum block size, in samples */\ |
| 75 | int max_framesize; /**< maximum frame size, in bytes */\ |
| 76 | int64_t samples; /**< total number of samples */\ |
| 77 | |
| 78 | typedef struct FLACStreaminfo { |
| 79 | FLACSTREAMINFO |
| 80 | } FLACStreaminfo; |
| 81 | |
| 82 | typedef struct FLACFrameInfo { |
| 83 | FLACCOMMONINFO |
| 84 | int blocksize; /**< block size of the frame */ |
| 85 | int ch_mode; /**< channel decorrelation mode */ |
| 86 | int64_t frame_or_sample_num; /**< frame number or sample number */ |
| 87 | int is_var_size; /**< specifies if the stream uses variable |
| 88 | block sizes or a fixed block size; |
| 89 | also determines the meaning of |
| 90 | frame_or_sample_num */ |
| 91 | } FLACFrameInfo; |
| 92 | |
| 93 | /** |
| 94 | * Parse the Streaminfo metadata block |
| 95 | * @param[out] avctx codec context to set basic stream parameters |
| 96 | * @param[out] s where parsed information is stored |
| 97 | * @param[in] buffer pointer to start of 34-byte streaminfo data |
| 98 | */ |
| 99 | void avpriv_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s, |
| 100 | const uint8_t *buffer); |
| 101 | |
| 102 | /** |
| 103 | * Validate the FLAC extradata. |
| 104 | * @param[in] avctx codec context containing the extradata. |
| 105 | * @param[out] format extradata format. |
| 106 | * @param[out] streaminfo_start pointer to start of 34-byte STREAMINFO data. |
| 107 | * @return 1 if valid, 0 if not valid. |
| 108 | */ |
| 109 | int avpriv_flac_is_extradata_valid(AVCodecContext *avctx, |
| 110 | enum FLACExtradataFormat *format, |
| 111 | uint8_t **streaminfo_start); |
| 112 | |
| 113 | /** |
| 114 | * Calculate an estimate for the maximum frame size based on verbatim mode. |
| 115 | * @param blocksize block size, in samples |
| 116 | * @param ch number of channels |
| 117 | * @param bps bits-per-sample |
| 118 | */ |
| 119 | int ff_flac_get_max_frame_size(int blocksize, int ch, int bps); |
| 120 | |
| 121 | /** |
| 122 | * Validate and decode a frame header. |
| 123 | * @param avctx AVCodecContext to use as av_log() context |
| 124 | * @param gb GetBitContext from which to read frame header |
| 125 | * @param[out] fi frame information |
| 126 | * @param log_level_offset log level offset. can be used to silence error messages. |
| 127 | * @return non-zero on error, 0 if ok |
| 128 | */ |
| 129 | int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb, |
| 130 | FLACFrameInfo *fi, int log_level_offset); |
| 131 | |
| 132 | void ff_flac_set_channel_layout(AVCodecContext *avctx); |
| 133 | |
| 134 | /** |
| 135 | * Parse the metadata block parameters from the header. |
| 136 | * @param[in] block_header header data, at least 4 bytes |
| 137 | * @param[out] last indicator for last metadata block |
| 138 | * @param[out] type metadata block type |
| 139 | * @param[out] size metadata block size |
| 140 | */ |
| 141 | static av_always_inline void flac_parse_block_header(const uint8_t *block_header, |
| 142 | int *last, int *type, int *size) |
| 143 | { |
| 144 | int tmp = bytestream_get_byte(&block_header); |
| 145 | if (last) |
| 146 | *last = tmp & 0x80; |
| 147 | if (type) |
| 148 | *type = tmp & 0x7F; |
| 149 | if (size) |
| 150 | *size = bytestream_get_be24(&block_header); |
| 151 | } |
| 152 | |
| 153 | #endif /* AVCODEC_FLAC_H */ |