blob: 8f5dde870497bb3446fe0d5810b3aff0c1f9f079 [file] [log] [blame]
Josh Coalson26560dd2001-02-08 00:38:41 +00001/* libFLAC - Free Lossless Audio Codec library
Josh Coalsonafae69f2003-01-02 07:03:16 +00002 * Copyright (C) 2000,2001,2002,2003 Josh Coalson
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003 *
Josh Coalsonafd81072003-01-31 23:34:56 +00004 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00007 *
Josh Coalsonafd81072003-01-31 23:34:56 +00008 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000010 *
Josh Coalsonafd81072003-01-31 23:34:56 +000011 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of the Xiph.org Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000030 */
31
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000032#include <stdio.h>
Josh Coalson5a5de732002-08-01 06:37:11 +000033#include <stdlib.h> /* for qsort() */
Josh Coalson0d36cbd2002-07-15 05:24:56 +000034#include "FLAC/assert.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000035#include "FLAC/format.h"
Josh Coalsoncda4c3c2002-08-17 15:21:29 +000036#include "private/format.h"
37
Matt Zimmermane5259172002-10-10 16:51:06 +000038#ifdef HAVE_CONFIG_H
39#include <config.h>
40#endif
41
Josh Coalsoncda4c3c2002-08-17 15:21:29 +000042#ifdef min
43#undef min
44#endif
45#define min(a,b) ((a)<(b)?(a):(b))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000046
Josh Coalson82738b32002-09-25 06:09:47 +000047/* VERSION should come from configure */
Josh Coalson6afed9f2002-10-16 22:29:47 +000048FLAC_API const char *FLAC__VERSION_STRING = VERSION;
Josh Coalson82738b32002-09-25 06:09:47 +000049
Josh Coalson0aad38b2002-09-09 05:06:51 +000050#if defined _MSC_VER || defined __MINW32__
51/* yet one more hack because of MSVC6: */
Josh Coalson53496dd2003-01-25 06:04:55 +000052FLAC_API const char *FLAC__VENDOR_STRING = "reference libFLAC 1.1.0 20030126";
Josh Coalson0aad38b2002-09-09 05:06:51 +000053#else
Josh Coalson53496dd2003-01-25 06:04:55 +000054FLAC_API const char *FLAC__VENDOR_STRING = "reference libFLAC " VERSION " 20030126";
Josh Coalson0aad38b2002-09-09 05:06:51 +000055#endif
56
Josh Coalson6afed9f2002-10-16 22:29:47 +000057FLAC_API const FLAC__byte FLAC__STREAM_SYNC_STRING[4] = { 'f','L','a','C' };
58FLAC_API const unsigned FLAC__STREAM_SYNC = 0x664C6143;
59FLAC_API const unsigned FLAC__STREAM_SYNC_LEN = 32; /* bits */;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000060
Josh Coalson6afed9f2002-10-16 22:29:47 +000061FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN = 16; /* bits */
62FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN = 16; /* bits */
63FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN = 24; /* bits */
64FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN = 24; /* bits */
65FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN = 20; /* bits */
66FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN = 3; /* bits */
67FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN = 5; /* bits */
68FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN = 36; /* bits */
69FLAC_API const unsigned FLAC__STREAM_METADATA_STREAMINFO_MD5SUM_LEN = 128; /* bits */
Josh Coalson20135722001-02-23 21:05:53 +000070
Josh Coalson6afed9f2002-10-16 22:29:47 +000071FLAC_API const unsigned FLAC__STREAM_METADATA_APPLICATION_ID_LEN = 32; /* bits */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000072
Josh Coalson6afed9f2002-10-16 22:29:47 +000073FLAC_API const unsigned FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN = 64; /* bits */
74FLAC_API const unsigned FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN = 64; /* bits */
75FLAC_API const unsigned FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN = 16; /* bits */
Josh Coalsonc5d08e02001-04-10 19:13:50 +000076
Josh Coalson6afed9f2002-10-16 22:29:47 +000077FLAC_API const FLAC__uint64 FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER = 0xffffffffffffffff;
Josh Coalson167658a2001-04-13 18:22:25 +000078
Josh Coalson6afed9f2002-10-16 22:29:47 +000079FLAC_API const unsigned FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN = 32; /* bits */
80FLAC_API const unsigned FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN = 32; /* bits */
Josh Coalsoncc059bc2002-05-04 17:35:51 +000081
Josh Coalson8e9c4512002-11-14 05:00:24 +000082FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN = 64; /* bits */
83FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN = 8; /* bits */
Josh Coalsonb3538c82003-01-12 08:42:23 +000084FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN = 3*8; /* bits */
Josh Coalson8e9c4512002-11-14 05:00:24 +000085
86FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN = 64; /* bits */
87FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN = 8; /* bits */
88FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN = 12*8; /* bits */
89FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN = 1; /* bit */
90FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN = 1; /* bit */
Josh Coalsonb3538c82003-01-12 08:42:23 +000091FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN = 6+13*8; /* bits */
Josh Coalson8e9c4512002-11-14 05:00:24 +000092FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN = 8; /* bits */
93
94FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN = 128*8; /* bits */
95FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN = 64; /* bits */
Josh Coalson8f0c71b2002-12-05 06:37:46 +000096FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN = 1; /* bit */
Josh Coalsonb3538c82003-01-12 08:42:23 +000097FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN = 7+258*8; /* bits */
Josh Coalson8e9c4512002-11-14 05:00:24 +000098FLAC_API const unsigned FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN = 8; /* bits */
99
Josh Coalson6afed9f2002-10-16 22:29:47 +0000100FLAC_API const unsigned FLAC__STREAM_METADATA_IS_LAST_LEN = 1; /* bits */
101FLAC_API const unsigned FLAC__STREAM_METADATA_TYPE_LEN = 7; /* bits */
102FLAC_API const unsigned FLAC__STREAM_METADATA_LENGTH_LEN = 24; /* bits */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000103
Josh Coalson6afed9f2002-10-16 22:29:47 +0000104FLAC_API const unsigned FLAC__FRAME_HEADER_SYNC = 0x3ffe;
105FLAC_API const unsigned FLAC__FRAME_HEADER_SYNC_LEN = 14; /* bits */
106FLAC_API const unsigned FLAC__FRAME_HEADER_RESERVED_LEN = 2; /* bits */
107FLAC_API const unsigned FLAC__FRAME_HEADER_BLOCK_SIZE_LEN = 4; /* bits */
108FLAC_API const unsigned FLAC__FRAME_HEADER_SAMPLE_RATE_LEN = 4; /* bits */
109FLAC_API const unsigned FLAC__FRAME_HEADER_CHANNEL_ASSIGNMENT_LEN = 4; /* bits */
110FLAC_API const unsigned FLAC__FRAME_HEADER_BITS_PER_SAMPLE_LEN = 3; /* bits */
111FLAC_API const unsigned FLAC__FRAME_HEADER_ZERO_PAD_LEN = 1; /* bits */
112FLAC_API const unsigned FLAC__FRAME_HEADER_CRC_LEN = 8; /* bits */
Josh Coalson215af572001-03-27 01:15:58 +0000113
Josh Coalson6afed9f2002-10-16 22:29:47 +0000114FLAC_API const unsigned FLAC__FRAME_FOOTER_CRC_LEN = 16; /* bits */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000115
Josh Coalson6afed9f2002-10-16 22:29:47 +0000116FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_TYPE_LEN = 2; /* bits */
117FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN = 4; /* bits */
118FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN = 4; /* bits */
119FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN = 5; /* bits */
Josh Coalson2051dd42001-04-12 22:22:34 +0000120
Josh Coalson6afed9f2002-10-16 22:29:47 +0000121FLAC_API const unsigned FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER = 15; /* == (1<<FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN)-1 */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000122
Josh Coalson6afed9f2002-10-16 22:29:47 +0000123FLAC_API const char * const FLAC__EntropyCodingMethodTypeString[] = {
Josh Coalson75d0a0b2001-01-24 00:42:16 +0000124 "PARTITIONED_RICE"
125};
126
Josh Coalson6afed9f2002-10-16 22:29:47 +0000127FLAC_API const unsigned FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN = 4; /* bits */
128FLAC_API const unsigned FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN = 5; /* bits */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000129
Josh Coalson6afed9f2002-10-16 22:29:47 +0000130FLAC_API const unsigned FLAC__SUBFRAME_ZERO_PAD_LEN = 1; /* bits */
131FLAC_API const unsigned FLAC__SUBFRAME_TYPE_LEN = 6; /* bits */
132FLAC_API const unsigned FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN = 1; /* bits */
Josh Coalson215af572001-03-27 01:15:58 +0000133
Josh Coalson6afed9f2002-10-16 22:29:47 +0000134FLAC_API const unsigned FLAC__SUBFRAME_TYPE_CONSTANT_BYTE_ALIGNED_MASK = 0x00;
135FLAC_API const unsigned FLAC__SUBFRAME_TYPE_VERBATIM_BYTE_ALIGNED_MASK = 0x02;
136FLAC_API const unsigned FLAC__SUBFRAME_TYPE_FIXED_BYTE_ALIGNED_MASK = 0x10;
137FLAC_API const unsigned FLAC__SUBFRAME_TYPE_LPC_BYTE_ALIGNED_MASK = 0x40;
Josh Coalson75d0a0b2001-01-24 00:42:16 +0000138
Josh Coalson6afed9f2002-10-16 22:29:47 +0000139FLAC_API const char * const FLAC__SubframeTypeString[] = {
Josh Coalson75d0a0b2001-01-24 00:42:16 +0000140 "CONSTANT",
141 "VERBATIM",
142 "FIXED",
143 "LPC"
144};
145
Josh Coalson6afed9f2002-10-16 22:29:47 +0000146FLAC_API const char * const FLAC__ChannelAssignmentString[] = {
Josh Coalson75d0a0b2001-01-24 00:42:16 +0000147 "INDEPENDENT",
148 "LEFT_SIDE",
149 "RIGHT_SIDE",
150 "MID_SIDE"
151};
152
Josh Coalson6afed9f2002-10-16 22:29:47 +0000153FLAC_API const char * const FLAC__FrameNumberTypeString[] = {
Josh Coalson96084632001-07-16 18:07:12 +0000154 "FRAME_NUMBER_TYPE_FRAME_NUMBER",
155 "FRAME_NUMBER_TYPE_SAMPLE_NUMBER"
156};
157
Josh Coalson6afed9f2002-10-16 22:29:47 +0000158FLAC_API const char * const FLAC__MetadataTypeString[] = {
Josh Coalson20135722001-02-23 21:05:53 +0000159 "STREAMINFO",
160 "PADDING",
Josh Coalson9b04be72001-06-14 18:57:53 +0000161 "APPLICATION",
Josh Coalson7e19b542002-04-25 05:53:09 +0000162 "SEEKTABLE",
Josh Coalsone4869382002-11-15 05:41:48 +0000163 "VORBIS_COMMENT",
164 "CUESHEET"
Josh Coalson75d0a0b2001-01-24 00:42:16 +0000165};
Josh Coalsoncc059bc2002-05-04 17:35:51 +0000166
Josh Coalson6afed9f2002-10-16 22:29:47 +0000167FLAC_API FLAC__bool FLAC__format_sample_rate_is_valid(unsigned sample_rate)
Josh Coalsoncc059bc2002-05-04 17:35:51 +0000168{
169 if(
170 sample_rate == 0 ||
171 sample_rate > FLAC__MAX_SAMPLE_RATE ||
172 (
Josh Coalson765ff502002-08-27 05:46:11 +0000173 sample_rate >= (1u << 16) &&
Josh Coalsoncc059bc2002-05-04 17:35:51 +0000174 !(sample_rate % 1000 == 0 || sample_rate % 10 == 0)
175 )
176 ) {
177 return false;
178 }
179 else
180 return true;
181}
Josh Coalson0d36cbd2002-07-15 05:24:56 +0000182
Josh Coalson6afed9f2002-10-16 22:29:47 +0000183FLAC_API FLAC__bool FLAC__format_seektable_is_legal(const FLAC__StreamMetadata_SeekTable *seek_table)
Josh Coalson0d36cbd2002-07-15 05:24:56 +0000184{
185 unsigned i;
186 FLAC__uint64 prev_sample_number = 0;
187 FLAC__bool got_prev = false;
188
189 FLAC__ASSERT(0 != seek_table);
190
191 for(i = 0; i < seek_table->num_points; i++) {
192 if(got_prev) {
Josh Coalson5a5de732002-08-01 06:37:11 +0000193 if(
194 seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER &&
195 seek_table->points[i].sample_number <= prev_sample_number
196 )
Josh Coalson0d36cbd2002-07-15 05:24:56 +0000197 return false;
198 }
199 prev_sample_number = seek_table->points[i].sample_number;
200 got_prev = true;
201 }
202
203 return true;
204}
Josh Coalson5a5de732002-08-01 06:37:11 +0000205
206/* used as the sort predicate for qsort() */
207static int seekpoint_compare_(const FLAC__StreamMetadata_SeekPoint *l, const FLAC__StreamMetadata_SeekPoint *r)
208{
209 /* we don't just 'return l->sample_number - r->sample_number' since the result (FLAC__int64) might overflow an 'int' */
210 if(l->sample_number == r->sample_number)
211 return 0;
212 else if(l->sample_number < r->sample_number)
213 return -1;
214 else
215 return 1;
216}
217
Josh Coalson6afed9f2002-10-16 22:29:47 +0000218FLAC_API unsigned FLAC__format_seektable_sort(FLAC__StreamMetadata_SeekTable *seek_table)
Josh Coalson5a5de732002-08-01 06:37:11 +0000219{
220 unsigned i, j;
221 FLAC__bool first;
222
Josh Coalsona0ac09a2002-08-16 05:40:16 +0000223 FLAC__ASSERT(0 != seek_table);
224
Josh Coalson5a5de732002-08-01 06:37:11 +0000225 /* sort the seekpoints */
226 qsort(seek_table->points, seek_table->num_points, sizeof(FLAC__StreamMetadata_SeekPoint), (int (*)(const void *, const void *))seekpoint_compare_);
227
228 /* uniquify the seekpoints */
229 first = true;
230 for(i = j = 0; i < seek_table->num_points; i++) {
231 if(seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER) {
232 if(!first) {
233 if(seek_table->points[i].sample_number == seek_table->points[j-1].sample_number)
234 continue;
235 }
236 }
237 first = false;
238 seek_table->points[j++] = seek_table->points[i];
239 }
240
Josh Coalson08a62822002-08-01 07:33:36 +0000241 for(i = j; i < seek_table->num_points; i++) {
242 seek_table->points[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
243 seek_table->points[i].stream_offset = 0;
244 seek_table->points[i].frame_samples = 0;
Josh Coalson5a5de732002-08-01 06:37:11 +0000245 }
246
247 return j;
248}
Josh Coalsoncda4c3c2002-08-17 15:21:29 +0000249
Josh Coalson8e9c4512002-11-14 05:00:24 +0000250FLAC_API FLAC__bool FLAC__format_cuesheet_is_legal(const FLAC__StreamMetadata_CueSheet *cue_sheet, FLAC__bool check_cd_da_subset, const char **violation)
251{
252 unsigned i, j;
253
254 if(check_cd_da_subset) {
255 if(cue_sheet->lead_in < 2 * 44100) {
256 if(violation) *violation = "CD-DA cue sheet must have a lead-in length of at least 2 seconds";
257 return false;
258 }
259 if(cue_sheet->lead_in % 588 != 0) {
260 if(violation) *violation = "CD-DA cue sheet lead-in length must be evenly divisible by 588 samples";
261 return false;
262 }
263 }
264
265 if(cue_sheet->num_tracks == 0) {
266 if(violation) *violation = "cue sheet must have at least one track (the lead-out)";
267 return false;
268 }
269
270 if(check_cd_da_subset && cue_sheet->tracks[cue_sheet->num_tracks-1].number != 170) {
271 if(violation) *violation = "CD-DA cue sheet must have a lead-out track number 170 (0xAA)";
272 return false;
273 }
274
275 for(i = 0; i < cue_sheet->num_tracks; i++) {
276 if(cue_sheet->tracks[i].number == 0) {
277 if(violation) *violation = "cue sheet may not have a track number 0";
278 return false;
279 }
280
281 if(check_cd_da_subset) {
282 if(!((cue_sheet->tracks[i].number >= 1 && cue_sheet->tracks[i].number <= 99) || cue_sheet->tracks[i].number == 170)) {
283 if(violation) *violation = "CD-DA cue sheet track number must be 1-99 or 170";
284 return false;
285 }
286 }
287
288 if(check_cd_da_subset && cue_sheet->tracks[i].offset % 588 != 0) {
289 if(violation) *violation = "CD-DA cue sheet track offset must be evenly divisible by 588 samples";
290 return false;
291 }
292
Josh Coalson3f86cc72002-11-21 06:41:46 +0000293 if(i < cue_sheet->num_tracks - 1) {
294 if(cue_sheet->tracks[i].num_indices == 0) {
295 if(violation) *violation = "cue sheet track must have at least one index point";
296 return false;
297 }
Josh Coalson8e9c4512002-11-14 05:00:24 +0000298
Josh Coalson3f86cc72002-11-21 06:41:46 +0000299 if(cue_sheet->tracks[i].indices[0].number > 1) {
300 if(violation) *violation = "cue sheet track's first index number must be 0 or 1";
301 return false;
302 }
Josh Coalson8e9c4512002-11-14 05:00:24 +0000303 }
304
305 for(j = 0; j < cue_sheet->tracks[i].num_indices; j++) {
306 if(check_cd_da_subset && cue_sheet->tracks[i].indices[j].offset % 588 != 0) {
307 if(violation) *violation = "CD-DA cue sheet track index offset must be evenly divisible by 588 samples";
308 return false;
309 }
310
311 if(j > 0) {
312 if(cue_sheet->tracks[i].indices[j].number != cue_sheet->tracks[i].indices[j-1].number + 1) {
313 if(violation) *violation = "cue sheet track index numbers must increase by 1";
314 return false;
315 }
316 }
317 }
318 }
319
320 return true;
321}
322
323/*
324 * These routines are private to libFLAC
325 */
Josh Coalsoncda4c3c2002-08-17 15:21:29 +0000326unsigned FLAC__format_get_max_rice_partition_order(unsigned blocksize, unsigned predictor_order)
327{
328 return
329 FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(
330 FLAC__format_get_max_rice_partition_order_from_blocksize(blocksize),
331 blocksize,
332 predictor_order
333 );
334}
335
336unsigned FLAC__format_get_max_rice_partition_order_from_blocksize(unsigned blocksize)
337{
338 unsigned max_rice_partition_order = 0;
339 while(!(blocksize & 1)) {
340 max_rice_partition_order++;
341 blocksize >>= 1;
342 }
343 return min(FLAC__MAX_RICE_PARTITION_ORDER, max_rice_partition_order);
344}
345
346unsigned FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(unsigned limit, unsigned blocksize, unsigned predictor_order)
347{
348 unsigned max_rice_partition_order = limit;
349
350 while(max_rice_partition_order > 0 && (blocksize >> max_rice_partition_order) <= predictor_order)
351 max_rice_partition_order--;
352
Josh Coalsonf12edc62002-10-11 06:24:33 +0000353 FLAC__ASSERT(
354 (max_rice_partition_order == 0 && blocksize >= predictor_order) ||
355 (max_rice_partition_order > 0 && blocksize >> max_rice_partition_order > predictor_order)
356 );
Josh Coalsoncda4c3c2002-08-17 15:21:29 +0000357
358 return max_rice_partition_order;
359}
360
Josh Coalsona37ba462002-08-19 21:36:39 +0000361void FLAC__format_entropy_coding_method_partitioned_rice_contents_init(FLAC__EntropyCodingMethod_PartitionedRiceContents *object)
Josh Coalsoncda4c3c2002-08-17 15:21:29 +0000362{
363 FLAC__ASSERT(0 != object);
364
Josh Coalsoncda4c3c2002-08-17 15:21:29 +0000365 object->parameters = 0;
366 object->raw_bits = 0;
367 object->capacity_by_order = 0;
368}
369
Josh Coalsona37ba462002-08-19 21:36:39 +0000370void FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(FLAC__EntropyCodingMethod_PartitionedRiceContents *object)
Josh Coalsoncda4c3c2002-08-17 15:21:29 +0000371{
372 FLAC__ASSERT(0 != object);
373
374 if(0 != object->parameters)
375 free(object->parameters);
376 if(0 != object->raw_bits)
377 free(object->raw_bits);
Josh Coalsona37ba462002-08-19 21:36:39 +0000378 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(object);
Josh Coalsoncda4c3c2002-08-17 15:21:29 +0000379}
380
Josh Coalsona37ba462002-08-19 21:36:39 +0000381FLAC__bool FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(FLAC__EntropyCodingMethod_PartitionedRiceContents *object, unsigned max_partition_order)
Josh Coalsoncda4c3c2002-08-17 15:21:29 +0000382{
383 FLAC__ASSERT(0 != object);
384
385 FLAC__ASSERT(object->capacity_by_order > 0 || (0 == object->parameters && 0 == object->raw_bits));
386
387 if(object->capacity_by_order < max_partition_order) {
Josh Coalson5e3fcb22002-11-06 07:11:26 +0000388 if(0 == (object->parameters = (unsigned*)realloc(object->parameters, sizeof(unsigned)*(1 << max_partition_order))))
Josh Coalsoncda4c3c2002-08-17 15:21:29 +0000389 return false;
Josh Coalson5e3fcb22002-11-06 07:11:26 +0000390 if(0 == (object->raw_bits = (unsigned*)realloc(object->raw_bits, sizeof(unsigned)*(1 << max_partition_order))))
Josh Coalsoncda4c3c2002-08-17 15:21:29 +0000391 return false;
392 object->capacity_by_order = max_partition_order;
393 }
394
395 return true;
396}