FFmpeg
Loading...
Searching...
No Matches
flac.c
Go to the documentation of this file.
1/*
2 * FLAC common code
3 * Copyright (c) 2009 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
23#include "libavutil/crc.h"
24#include "libavutil/log.h"
25#include "bytestream.h"
26#include "get_bits.h"
27#include "flac.h"
28#include "flacdata.h"
29#include "flac_parse.h"
30
31static const int8_t sample_size_table[] = { 0, 8, 12, 0, 16, 20, 24, 32 };
32
43
45{
47 GET_UTF8(val, get_bits(gb, 8), return -1;)
48 return val;
49}
50
52 FLACFrameInfo *fi, int log_level_offset)
53{
54 int bs_code, sr_code, bps_code;
55
56 /* frame sync code */
57 if ((get_bits(gb, 15) & 0x7FFF) != 0x7FFC) {
58 av_log(logctx, AV_LOG_ERROR + log_level_offset, "invalid sync code\n");
60 }
61
62 /* variable block size stream code */
63 fi->is_var_size = get_bits1(gb);
64
65 /* block size and sample rate codes */
66 bs_code = get_bits(gb, 4);
67 sr_code = get_bits(gb, 4);
68
69 /* channels and decorrelation */
70 fi->ch_mode = get_bits(gb, 4);
71 if (fi->ch_mode < FLAC_MAX_CHANNELS) {
72 fi->channels = fi->ch_mode + 1;
75 fi->channels = 2;
76 fi->ch_mode -= FLAC_MAX_CHANNELS - 1;
77 } else {
78 av_log(logctx, AV_LOG_ERROR + log_level_offset,
79 "invalid channel mode: %d\n", fi->ch_mode);
81 }
82
83 /* bits per sample */
84 bps_code = get_bits(gb, 3);
85 if (bps_code == 3) {
86 av_log(logctx, AV_LOG_ERROR + log_level_offset,
87 "invalid sample size code (%d)\n",
88 bps_code);
90 }
91 fi->bps = sample_size_table[bps_code];
92
93 /* reserved bit */
94 if (get_bits1(gb)) {
95 av_log(logctx, AV_LOG_ERROR + log_level_offset,
96 "broken stream, invalid padding\n");
98 }
99
100 /* sample or frame count */
102 if (fi->frame_or_sample_num < 0) {
103 av_log(logctx, AV_LOG_ERROR + log_level_offset,
104 "sample/frame number invalid; utf8 fscked\n");
105 return AVERROR_INVALIDDATA;
106 }
107
108 /* blocksize */
109 if (bs_code == 0) {
110 av_log(logctx, AV_LOG_ERROR + log_level_offset,
111 "reserved blocksize code: 0\n");
112 return AVERROR_INVALIDDATA;
113 } else if (bs_code == 6) {
114 fi->blocksize = get_bits(gb, 8) + 1;
115 } else if (bs_code == 7) {
116 fi->blocksize = get_bits(gb, 16) + 1;
117 } else {
118 fi->blocksize = ff_flac_blocksize_table[bs_code];
119 }
120
121 /* sample rate */
122 if (sr_code < 12) {
124 } else if (sr_code == 12) {
125 fi->samplerate = get_bits(gb, 8) * 1000;
126 } else if (sr_code == 13) {
127 fi->samplerate = get_bits(gb, 16);
128 } else if (sr_code == 14) {
129 fi->samplerate = get_bits(gb, 16) * 10;
130 } else {
131 av_log(logctx, AV_LOG_ERROR + log_level_offset,
132 "illegal sample rate code %d\n",
133 sr_code);
134 return AVERROR_INVALIDDATA;
135 }
136
137 /* header CRC-8 check */
138 skip_bits(gb, 8);
140 get_bits_count(gb)/8)) {
141 av_log(logctx, AV_LOG_ERROR + log_level_offset,
142 "header crc mismatch\n");
143 return AVERROR_INVALIDDATA;
144 }
145
146 return 0;
147}
148
150 uint8_t **streaminfo_start)
151{
152 if (!avctx->extradata || avctx->extradata_size < FLAC_STREAMINFO_SIZE) {
153 av_log(avctx, AV_LOG_ERROR, "extradata NULL or too small.\n");
154 return 0;
155 }
156 if (AV_RL32(avctx->extradata) != MKTAG('f','L','a','C')) {
157 /* extradata contains STREAMINFO only */
158 if (avctx->extradata_size != FLAC_STREAMINFO_SIZE) {
159 av_log(avctx, AV_LOG_WARNING, "extradata contains %d bytes too many.\n",
161 }
162 *streaminfo_start = avctx->extradata;
163 } else {
164 if (avctx->extradata_size < 8+FLAC_STREAMINFO_SIZE) {
165 av_log(avctx, AV_LOG_ERROR, "extradata too small.\n");
166 return 0;
167 }
168 *streaminfo_start = &avctx->extradata[8];
169 }
170 return 1;
171}
172
174{
175 if (channels == avctx->ch_layout.nb_channels &&
177 return;
178
182 else
184 .nb_channels = channels };
185}
186
188 const uint8_t *buffer)
189{
190 GetBitContext gb;
192
193 skip_bits(&gb, 16); /* skip min blocksize */
194 s->max_blocksize = get_bits(&gb, 16);
195 if (s->max_blocksize < FLAC_MIN_BLOCKSIZE) {
196 av_log(avctx, AV_LOG_WARNING, "invalid max blocksize: %d\n",
197 s->max_blocksize);
198 s->max_blocksize = 16;
199 return AVERROR_INVALIDDATA;
200 }
201
202 skip_bits(&gb, 24); /* skip min frame size */
203 s->max_framesize = get_bits(&gb, 24);
204
205 s->samplerate = get_bits(&gb, 20);
206 s->channels = get_bits(&gb, 3) + 1;
207 s->bps = get_bits(&gb, 5) + 1;
208
209 if (s->bps < 4) {
210 av_log(avctx, AV_LOG_ERROR, "invalid bps: %d\n", s->bps);
211 s->bps = 16;
212 return AVERROR_INVALIDDATA;
213 }
214
215 avctx->sample_rate = s->samplerate;
216 avctx->bits_per_raw_sample = s->bps;
217 ff_flac_set_channel_layout(avctx, s->channels);
218
219 s->samples = get_bits64(&gb, 36);
220
221 skip_bits_long(&gb, 64); /* md5 sum */
222 skip_bits_long(&gb, 64); /* md5 sum */
223
224 return 0;
225}
static double val(void *priv, double ch)
Definition aeval.c:77
channels
Definition aptx.h:31
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define GET_UTF8(val, GET_BYTE, ERROR)
Convert a UTF-8 character (up to 4 bytes) to its 32-bit UCS-4 encoded form.
Definition common.h:477
long long int64_t
Definition coverity.c:34
Public header for CRC hash function implementation.
int ff_flac_decode_frame_header(void *logctx, GetBitContext *gb, FLACFrameInfo *fi, int log_level_offset)
Validate and decode a frame header.
Definition flac.c:51
int ff_flac_is_extradata_valid(AVCodecContext *avctx, uint8_t **streaminfo_start)
Validate the FLAC extradata.
Definition flac.c:149
static int64_t get_utf8(GetBitContext *gb)
Definition flac.c:44
static const int8_t sample_size_table[]
Definition flac.c:31
void ff_flac_set_channel_layout(AVCodecContext *avctx, int channels)
Definition flac.c:173
int ff_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s, const uint8_t *buffer)
Parse the Streaminfo metadata block.
Definition flac.c:187
static const AVChannelLayout flac_channel_layouts[8]
Definition flac.c:33
FLAC (Free Lossless Audio Codec) common stuff.
@ FLAC_CHMODE_MID_SIDE
Definition flac.h:42
@ FLAC_CHMODE_INDEPENDENT
Definition flac.h:39
#define FLAC_MIN_BLOCKSIZE
Definition flac.h:34
#define FLAC_MAX_CHANNELS
Definition flac.h:33
#define FLAC_STREAMINFO_SIZE
Definition flac.h:32
FLAC (Free Lossless Audio Codec) decoder/parser common functions.
const int ff_flac_sample_rate_table[16]
Definition flacdata.c:24
const int32_t ff_flac_blocksize_table[16]
Definition flacdata.c:30
bitstream reader API header.
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition get_bits.h:280
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static void skip_bits(GetBitContext *s, int n)
Definition get_bits.h:383
static uint64_t get_bits64(GetBitContext *s, int n)
Read 0-64 bits.
Definition get_bits.h:456
static int get_bits_count(const GetBitContext *s)
Definition get_bits.h:254
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition get_bits.h:517
#define AV_CHANNEL_LAYOUT_5POINT0
#define AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_5POINT1
#define AV_CHANNEL_LAYOUT_MONO
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
#define AV_CHANNEL_LAYOUT_SURROUND
#define AV_CHANNEL_LAYOUT_7POINT1
#define AV_CHANNEL_LAYOUT_QUAD
#define AV_CHANNEL_LAYOUT_6POINT1
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition crc.c:389
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition crc.c:421
@ AV_CRC_8_ATM
Definition crc.h:49
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
#define AV_RL32(p)
#define MKTAG(a, b, c, d)
Definition macros.h:55
#define FF_ARRAY_ELEMS(a)
An AVChannelLayout holds information about the channel layout of audio data.
enum AVChannelOrder order
Channel order used in this layout.
int nb_channels
Number of channels in this layout.
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition avcodec.h:1571
int sample_rate
samples per second
Definition avcodec.h:1040
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int extradata_size
Definition avcodec.h:527
int blocksize
block size of the frame
Definition flac_parse.h:46
int channels
number of channels
Definition flac_parse.h:44
int samplerate
sample rate
Definition flac_parse.h:43
int is_var_size
specifies if the stream uses variable block sizes or a fixed block size; also determines the meaning ...
Definition flac_parse.h:49
int64_t frame_or_sample_num
frame number or sample number
Definition flac_parse.h:48
int ch_mode
channel decorrelation mode
Definition flac_parse.h:47
int bps
bits-per-sample
Definition flac_parse.h:45
const uint8_t * buffer
Definition get_bits.h:110
#define av_log(a,...)
static char buffer[20]
Definition seek.c:32