3 * Copyright (c) 2009 Justin Ruggles
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/channel_layout.h"
23 #include "libavutil/crc.h"
24 #include "libavutil/log.h"
25 #include "bytestream.h"
29 #include "flac_parse.h"
31 static const int8_t sample_size_table
[] = { 0, 8, 12, 0, 16, 20, 24, 32 };
33 static const AVChannelLayout flac_channel_layouts
[8] = {
34 AV_CHANNEL_LAYOUT_MONO
,
35 AV_CHANNEL_LAYOUT_STEREO
,
36 AV_CHANNEL_LAYOUT_SURROUND
,
37 AV_CHANNEL_LAYOUT_QUAD
,
38 AV_CHANNEL_LAYOUT_5POINT0
,
39 AV_CHANNEL_LAYOUT_5POINT1
,
40 AV_CHANNEL_LAYOUT_6POINT1
,
41 AV_CHANNEL_LAYOUT_7POINT1
44 static int64_t get_utf8(GetBitContext
*gb
)
47 GET_UTF8(val
, get_bits(gb
, 8), return -1;)
51 int ff_flac_decode_frame_header(void *logctx
, GetBitContext
*gb
,
52 FLACFrameInfo
*fi
, int log_level_offset
)
54 int bs_code
, sr_code
, bps_code
;
57 if ((get_bits(gb
, 15) & 0x7FFF) != 0x7FFC) {
58 av_log(logctx
, AV_LOG_ERROR
+ log_level_offset
, "invalid sync code\n");
59 return AVERROR_INVALIDDATA
;
62 /* variable block size stream code */
63 fi
->is_var_size
= get_bits1(gb
);
65 /* block size and sample rate codes */
66 bs_code
= get_bits(gb
, 4);
67 sr_code
= get_bits(gb
, 4);
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;
73 fi
->ch_mode
= FLAC_CHMODE_INDEPENDENT
;
74 } else if (fi
->ch_mode
< FLAC_MAX_CHANNELS
+ FLAC_CHMODE_MID_SIDE
) {
76 fi
->ch_mode
-= FLAC_MAX_CHANNELS
- 1;
78 av_log(logctx
, AV_LOG_ERROR
+ log_level_offset
,
79 "invalid channel mode: %d\n", fi
->ch_mode
);
80 return AVERROR_INVALIDDATA
;
84 bps_code
= get_bits(gb
, 3);
86 av_log(logctx
, AV_LOG_ERROR
+ log_level_offset
,
87 "invalid sample size code (%d)\n",
89 return AVERROR_INVALIDDATA
;
91 fi
->bps
= sample_size_table
[bps_code
];
95 av_log(logctx
, AV_LOG_ERROR
+ log_level_offset
,
96 "broken stream, invalid padding\n");
97 return AVERROR_INVALIDDATA
;
100 /* sample or frame count */
101 fi
->frame_or_sample_num
= get_utf8(gb
);
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
;
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;
118 fi
->blocksize
= ff_flac_blocksize_table
[bs_code
];
123 fi
->samplerate
= ff_flac_sample_rate_table
[sr_code
];
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;
131 av_log(logctx
, AV_LOG_ERROR
+ log_level_offset
,
132 "illegal sample rate code %d\n",
134 return AVERROR_INVALIDDATA
;
137 /* header CRC-8 check */
139 if (av_crc(av_crc_get_table(AV_CRC_8_ATM
), 0, gb
->buffer
,
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
;
149 int ff_flac_is_extradata_valid(AVCodecContext
*avctx
,
150 uint8_t **streaminfo_start
)
152 if (!avctx
->extradata
|| avctx
->extradata_size
< FLAC_STREAMINFO_SIZE
) {
153 av_log(avctx
, AV_LOG_ERROR
, "extradata NULL or too small.\n");
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",
160 FLAC_STREAMINFO_SIZE
-avctx
->extradata_size
);
162 *streaminfo_start
= avctx
->extradata
;
164 if (avctx
->extradata_size
< 8+FLAC_STREAMINFO_SIZE
) {
165 av_log(avctx
, AV_LOG_ERROR
, "extradata too small.\n");
168 *streaminfo_start
= &avctx
->extradata
[8];
173 void ff_flac_set_channel_layout(AVCodecContext
*avctx
, int channels
)
175 if (channels
== avctx
->ch_layout
.nb_channels
&&
176 avctx
->ch_layout
.order
!= AV_CHANNEL_ORDER_UNSPEC
)
179 av_channel_layout_uninit(&avctx
->ch_layout
);
180 if (channels
<= FF_ARRAY_ELEMS(flac_channel_layouts
))
181 avctx
->ch_layout
= flac_channel_layouts
[channels
- 1];
183 avctx
->ch_layout
= (AVChannelLayout
){ .order
= AV_CHANNEL_ORDER_UNSPEC
,
184 .nb_channels
= channels
};
187 int ff_flac_parse_streaminfo(AVCodecContext
*avctx
, struct FLACStreaminfo
*s
,
188 const uint8_t *buffer
)
191 init_get_bits(&gb
, buffer
, FLAC_STREAMINFO_SIZE
*8);
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",
198 s
->max_blocksize
= 16;
199 return AVERROR_INVALIDDATA
;
202 skip_bits(&gb
, 24); /* skip min frame size */
203 s
->max_framesize
= get_bits(&gb
, 24);
205 s
->samplerate
= get_bits(&gb
, 20);
206 s
->channels
= get_bits(&gb
, 3) + 1;
207 s
->bps
= get_bits(&gb
, 5) + 1;
210 av_log(avctx
, AV_LOG_ERROR
, "invalid bps: %d\n", s
->bps
);
212 return AVERROR_INVALIDDATA
;
215 avctx
->sample_rate
= s
->samplerate
;
216 avctx
->bits_per_raw_sample
= s
->bps
;
217 ff_flac_set_channel_layout(avctx
, s
->channels
);
219 s
->samples
= get_bits64(&gb
, 36);
221 skip_bits_long(&gb
, 64); /* md5 sum */
222 skip_bits_long(&gb
, 64); /* md5 sum */