1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000-2009 Josh Coalson
3 * Copyright (C) 2011-2013 Xiph.Org Foundation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * - Neither the name of the Xiph.org Foundation nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <string.h> /* for strlen() */
39 #include "private/stream_encoder_framing.h"
40 #include "private/crc.h"
41 #include "FLAC/assert.h"
43 static FLAC__bool add_entropy_coding_method_(FLAC__BitWriter *bw, const FLAC__EntropyCodingMethod *method);
44 static FLAC__bool add_residual_partitioned_rice_(FLAC__BitWriter *bw, const FLAC__int32 residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned rice_parameters[], const unsigned raw_bits[], const unsigned partition_order, const FLAC__bool is_extended);
46 FLAC__bool FLAC__add_metadata_block(const FLAC__StreamMetadata *metadata, FLAC__BitWriter *bw)
49 const unsigned vendor_string_length = (unsigned)strlen(FLAC__VENDOR_STRING);
51 if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->is_last, FLAC__STREAM_METADATA_IS_LAST_LEN))
54 if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->type, FLAC__STREAM_METADATA_TYPE_LEN))
58 * First, for VORBIS_COMMENTs, adjust the length to reflect our vendor string
61 if(metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
62 FLAC__ASSERT(metadata->data.vorbis_comment.vendor_string.length == 0 || 0 != metadata->data.vorbis_comment.vendor_string.entry);
63 i -= metadata->data.vorbis_comment.vendor_string.length;
64 i += vendor_string_length;
66 FLAC__ASSERT(i < (1u << FLAC__STREAM_METADATA_LENGTH_LEN));
67 if(!FLAC__bitwriter_write_raw_uint32(bw, i, FLAC__STREAM_METADATA_LENGTH_LEN))
70 switch(metadata->type) {
71 case FLAC__METADATA_TYPE_STREAMINFO:
72 FLAC__ASSERT(metadata->data.stream_info.min_blocksize < (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN));
73 if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.min_blocksize, FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN))
75 FLAC__ASSERT(metadata->data.stream_info.max_blocksize < (1u << FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN));
76 if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.max_blocksize, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN))
78 FLAC__ASSERT(metadata->data.stream_info.min_framesize < (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN));
79 if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.min_framesize, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN))
81 FLAC__ASSERT(metadata->data.stream_info.max_framesize < (1u << FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN));
82 if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.max_framesize, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN))
84 FLAC__ASSERT(FLAC__format_sample_rate_is_valid(metadata->data.stream_info.sample_rate));
85 if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.sample_rate, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN))
87 FLAC__ASSERT(metadata->data.stream_info.channels > 0);
88 FLAC__ASSERT(metadata->data.stream_info.channels <= (1u << FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN));
89 if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.channels-1, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN))
91 FLAC__ASSERT(metadata->data.stream_info.bits_per_sample > 0);
92 FLAC__ASSERT(metadata->data.stream_info.bits_per_sample <= (1u << FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN));
93 if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.bits_per_sample-1, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN))
95 if(!FLAC__bitwriter_write_raw_uint64(bw, metadata->data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN))
97 if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.stream_info.md5sum, 16))
100 case FLAC__METADATA_TYPE_PADDING:
101 if(!FLAC__bitwriter_write_zeroes(bw, metadata->length * 8))
104 case FLAC__METADATA_TYPE_APPLICATION:
105 if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8))
107 if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.application.data, metadata->length - (FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8)))
110 case FLAC__METADATA_TYPE_SEEKTABLE:
111 for(i = 0; i < metadata->data.seek_table.num_points; i++) {
112 if(!FLAC__bitwriter_write_raw_uint64(bw, metadata->data.seek_table.points[i].sample_number, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN))
114 if(!FLAC__bitwriter_write_raw_uint64(bw, metadata->data.seek_table.points[i].stream_offset, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN))
116 if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.seek_table.points[i].frame_samples, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN))
120 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
121 if(!FLAC__bitwriter_write_raw_uint32_little_endian(bw, vendor_string_length))
123 if(!FLAC__bitwriter_write_byte_block(bw, (const FLAC__byte*)FLAC__VENDOR_STRING, vendor_string_length))
125 if(!FLAC__bitwriter_write_raw_uint32_little_endian(bw, metadata->data.vorbis_comment.num_comments))
127 for(i = 0; i < metadata->data.vorbis_comment.num_comments; i++) {
128 if(!FLAC__bitwriter_write_raw_uint32_little_endian(bw, metadata->data.vorbis_comment.comments[i].length))
130 if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.vorbis_comment.comments[i].entry, metadata->data.vorbis_comment.comments[i].length))
134 case FLAC__METADATA_TYPE_CUESHEET:
135 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
136 if(!FLAC__bitwriter_write_byte_block(bw, (const FLAC__byte*)metadata->data.cue_sheet.media_catalog_number, FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN/8))
138 if(!FLAC__bitwriter_write_raw_uint64(bw, metadata->data.cue_sheet.lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN))
140 if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.cue_sheet.is_cd? 1 : 0, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN))
142 if(!FLAC__bitwriter_write_zeroes(bw, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN))
144 if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.cue_sheet.num_tracks, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN))
146 for(i = 0; i < metadata->data.cue_sheet.num_tracks; i++) {
147 const FLAC__StreamMetadata_CueSheet_Track *track = metadata->data.cue_sheet.tracks + i;
149 if(!FLAC__bitwriter_write_raw_uint64(bw, track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN))
151 if(!FLAC__bitwriter_write_raw_uint32(bw, track->number, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN))
153 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
154 if(!FLAC__bitwriter_write_byte_block(bw, (const FLAC__byte*)track->isrc, FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN/8))
156 if(!FLAC__bitwriter_write_raw_uint32(bw, track->type, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN))
158 if(!FLAC__bitwriter_write_raw_uint32(bw, track->pre_emphasis, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN))
160 if(!FLAC__bitwriter_write_zeroes(bw, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN))
162 if(!FLAC__bitwriter_write_raw_uint32(bw, track->num_indices, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN))
164 for(j = 0; j < track->num_indices; j++) {
165 const FLAC__StreamMetadata_CueSheet_Index *indx = track->indices + j;
167 if(!FLAC__bitwriter_write_raw_uint64(bw, indx->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN))
169 if(!FLAC__bitwriter_write_raw_uint32(bw, indx->number, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN))
171 if(!FLAC__bitwriter_write_zeroes(bw, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN))
176 case FLAC__METADATA_TYPE_PICTURE:
179 if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.picture.type, FLAC__STREAM_METADATA_PICTURE_TYPE_LEN))
181 len = strlen(metadata->data.picture.mime_type);
182 if(!FLAC__bitwriter_write_raw_uint32(bw, len, FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN))
184 if(!FLAC__bitwriter_write_byte_block(bw, (const FLAC__byte*)metadata->data.picture.mime_type, len))
186 len = strlen((const char *)metadata->data.picture.description);
187 if(!FLAC__bitwriter_write_raw_uint32(bw, len, FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN))
189 if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.picture.description, len))
191 if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.picture.width, FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN))
193 if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.picture.height, FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN))
195 if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.picture.depth, FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN))
197 if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.picture.colors, FLAC__STREAM_METADATA_PICTURE_COLORS_LEN))
199 if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.picture.data_length, FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN))
201 if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.picture.data, metadata->data.picture.data_length))
206 if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.unknown.data, metadata->length))
211 FLAC__ASSERT(FLAC__bitwriter_is_byte_aligned(bw));
215 FLAC__bool FLAC__frame_add_header(const FLAC__FrameHeader *header, FLAC__BitWriter *bw)
217 unsigned u, blocksize_hint, sample_rate_hint;
220 FLAC__ASSERT(FLAC__bitwriter_is_byte_aligned(bw));
222 if(!FLAC__bitwriter_write_raw_uint32(bw, FLAC__FRAME_HEADER_SYNC, FLAC__FRAME_HEADER_SYNC_LEN))
225 if(!FLAC__bitwriter_write_raw_uint32(bw, 0, FLAC__FRAME_HEADER_RESERVED_LEN))
228 if(!FLAC__bitwriter_write_raw_uint32(bw, (header->number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER)? 0 : 1, FLAC__FRAME_HEADER_BLOCKING_STRATEGY_LEN))
231 FLAC__ASSERT(header->blocksize > 0 && header->blocksize <= FLAC__MAX_BLOCK_SIZE);
232 /* when this assertion holds true, any legal blocksize can be expressed in the frame header */
233 FLAC__ASSERT(FLAC__MAX_BLOCK_SIZE <= 65535u);
235 switch(header->blocksize) {
236 case 192: u = 1; break;
237 case 576: u = 2; break;
238 case 1152: u = 3; break;
239 case 2304: u = 4; break;
240 case 4608: u = 5; break;
241 case 256: u = 8; break;
242 case 512: u = 9; break;
243 case 1024: u = 10; break;
244 case 2048: u = 11; break;
245 case 4096: u = 12; break;
246 case 8192: u = 13; break;
247 case 16384: u = 14; break;
248 case 32768: u = 15; break;
250 if(header->blocksize <= 0x100)
251 blocksize_hint = u = 6;
253 blocksize_hint = u = 7;
256 if(!FLAC__bitwriter_write_raw_uint32(bw, u, FLAC__FRAME_HEADER_BLOCK_SIZE_LEN))
259 FLAC__ASSERT(FLAC__format_sample_rate_is_valid(header->sample_rate));
260 sample_rate_hint = 0;
261 switch(header->sample_rate) {
262 case 88200: u = 1; break;
263 case 176400: u = 2; break;
264 case 192000: u = 3; break;
265 case 8000: u = 4; break;
266 case 16000: u = 5; break;
267 case 22050: u = 6; break;
268 case 24000: u = 7; break;
269 case 32000: u = 8; break;
270 case 44100: u = 9; break;
271 case 48000: u = 10; break;
272 case 96000: u = 11; break;
274 if(header->sample_rate <= 255000 && header->sample_rate % 1000 == 0)
275 sample_rate_hint = u = 12;
276 else if(header->sample_rate % 10 == 0)
277 sample_rate_hint = u = 14;
278 else if(header->sample_rate <= 0xffff)
279 sample_rate_hint = u = 13;
284 if(!FLAC__bitwriter_write_raw_uint32(bw, u, FLAC__FRAME_HEADER_SAMPLE_RATE_LEN))
287 FLAC__ASSERT(header->channels > 0 && header->channels <= (1u << FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN) && header->channels <= FLAC__MAX_CHANNELS);
288 switch(header->channel_assignment) {
289 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
290 u = header->channels - 1;
292 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
293 FLAC__ASSERT(header->channels == 2);
296 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
297 FLAC__ASSERT(header->channels == 2);
300 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
301 FLAC__ASSERT(header->channels == 2);
307 if(!FLAC__bitwriter_write_raw_uint32(bw, u, FLAC__FRAME_HEADER_CHANNEL_ASSIGNMENT_LEN))
310 FLAC__ASSERT(header->bits_per_sample > 0 && header->bits_per_sample <= (1u << FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN));
311 switch(header->bits_per_sample) {
312 case 8 : u = 1; break;
313 case 12: u = 2; break;
314 case 16: u = 4; break;
315 case 20: u = 5; break;
316 case 24: u = 6; break;
317 default: u = 0; break;
319 if(!FLAC__bitwriter_write_raw_uint32(bw, u, FLAC__FRAME_HEADER_BITS_PER_SAMPLE_LEN))
322 if(!FLAC__bitwriter_write_raw_uint32(bw, 0, FLAC__FRAME_HEADER_ZERO_PAD_LEN))
325 if(header->number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER) {
326 if(!FLAC__bitwriter_write_utf8_uint32(bw, header->number.frame_number))
330 if(!FLAC__bitwriter_write_utf8_uint64(bw, header->number.sample_number))
335 if(!FLAC__bitwriter_write_raw_uint32(bw, header->blocksize-1, (blocksize_hint==6)? 8:16))
338 switch(sample_rate_hint) {
340 if(!FLAC__bitwriter_write_raw_uint32(bw, header->sample_rate / 1000, 8))
344 if(!FLAC__bitwriter_write_raw_uint32(bw, header->sample_rate, 16))
348 if(!FLAC__bitwriter_write_raw_uint32(bw, header->sample_rate / 10, 16))
354 if(!FLAC__bitwriter_get_write_crc8(bw, &crc))
356 if(!FLAC__bitwriter_write_raw_uint32(bw, crc, FLAC__FRAME_HEADER_CRC_LEN))
362 FLAC__bool FLAC__subframe_add_constant(const FLAC__Subframe_Constant *subframe, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitWriter *bw)
367 FLAC__bitwriter_write_raw_uint32(bw, FLAC__SUBFRAME_TYPE_CONSTANT_BYTE_ALIGNED_MASK | (wasted_bits? 1:0), FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN) &&
368 (wasted_bits? FLAC__bitwriter_write_unary_unsigned(bw, wasted_bits-1) : true) &&
369 FLAC__bitwriter_write_raw_int32(bw, subframe->value, subframe_bps)
375 FLAC__bool FLAC__subframe_add_fixed(const FLAC__Subframe_Fixed *subframe, unsigned residual_samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitWriter *bw)
379 if(!FLAC__bitwriter_write_raw_uint32(bw, FLAC__SUBFRAME_TYPE_FIXED_BYTE_ALIGNED_MASK | (subframe->order<<1) | (wasted_bits? 1:0), FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN))
382 if(!FLAC__bitwriter_write_unary_unsigned(bw, wasted_bits-1))
385 for(i = 0; i < subframe->order; i++)
386 if(!FLAC__bitwriter_write_raw_int32(bw, subframe->warmup[i], subframe_bps))
389 if(!add_entropy_coding_method_(bw, &subframe->entropy_coding_method))
391 switch(subframe->entropy_coding_method.type) {
392 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
393 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
394 if(!add_residual_partitioned_rice_(
399 subframe->entropy_coding_method.data.partitioned_rice.contents->parameters,
400 subframe->entropy_coding_method.data.partitioned_rice.contents->raw_bits,
401 subframe->entropy_coding_method.data.partitioned_rice.order,
402 /*is_extended=*/subframe->entropy_coding_method.type == FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2
413 FLAC__bool FLAC__subframe_add_lpc(const FLAC__Subframe_LPC *subframe, unsigned residual_samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitWriter *bw)
417 if(!FLAC__bitwriter_write_raw_uint32(bw, FLAC__SUBFRAME_TYPE_LPC_BYTE_ALIGNED_MASK | ((subframe->order-1)<<1) | (wasted_bits? 1:0), FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN))
420 if(!FLAC__bitwriter_write_unary_unsigned(bw, wasted_bits-1))
423 for(i = 0; i < subframe->order; i++)
424 if(!FLAC__bitwriter_write_raw_int32(bw, subframe->warmup[i], subframe_bps))
427 if(!FLAC__bitwriter_write_raw_uint32(bw, subframe->qlp_coeff_precision-1, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
429 if(!FLAC__bitwriter_write_raw_int32(bw, subframe->quantization_level, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN))
431 for(i = 0; i < subframe->order; i++)
432 if(!FLAC__bitwriter_write_raw_int32(bw, subframe->qlp_coeff[i], subframe->qlp_coeff_precision))
435 if(!add_entropy_coding_method_(bw, &subframe->entropy_coding_method))
437 switch(subframe->entropy_coding_method.type) {
438 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
439 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
440 if(!add_residual_partitioned_rice_(
445 subframe->entropy_coding_method.data.partitioned_rice.contents->parameters,
446 subframe->entropy_coding_method.data.partitioned_rice.contents->raw_bits,
447 subframe->entropy_coding_method.data.partitioned_rice.order,
448 /*is_extended=*/subframe->entropy_coding_method.type == FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2
459 FLAC__bool FLAC__subframe_add_verbatim(const FLAC__Subframe_Verbatim *subframe, unsigned samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitWriter *bw)
462 const FLAC__int32 *signal = subframe->data;
464 if(!FLAC__bitwriter_write_raw_uint32(bw, FLAC__SUBFRAME_TYPE_VERBATIM_BYTE_ALIGNED_MASK | (wasted_bits? 1:0), FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN))
467 if(!FLAC__bitwriter_write_unary_unsigned(bw, wasted_bits-1))
470 for(i = 0; i < samples; i++)
471 if(!FLAC__bitwriter_write_raw_int32(bw, signal[i], subframe_bps))
477 FLAC__bool add_entropy_coding_method_(FLAC__BitWriter *bw, const FLAC__EntropyCodingMethod *method)
479 if(!FLAC__bitwriter_write_raw_uint32(bw, method->type, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
481 switch(method->type) {
482 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
483 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2:
484 if(!FLAC__bitwriter_write_raw_uint32(bw, method->data.partitioned_rice.order, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
493 FLAC__bool add_residual_partitioned_rice_(FLAC__BitWriter *bw, const FLAC__int32 residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned rice_parameters[], const unsigned raw_bits[], const unsigned partition_order, const FLAC__bool is_extended)
495 const unsigned plen = is_extended? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
496 const unsigned pesc = is_extended? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
498 if(partition_order == 0) {
501 if(raw_bits[0] == 0) {
502 if(!FLAC__bitwriter_write_raw_uint32(bw, rice_parameters[0], plen))
504 if(!FLAC__bitwriter_write_rice_signed_block(bw, residual, residual_samples, rice_parameters[0]))
508 FLAC__ASSERT(rice_parameters[0] == 0);
509 if(!FLAC__bitwriter_write_raw_uint32(bw, pesc, plen))
511 if(!FLAC__bitwriter_write_raw_uint32(bw, raw_bits[0], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN))
513 for(i = 0; i < residual_samples; i++) {
514 if(!FLAC__bitwriter_write_raw_int32(bw, residual[i], raw_bits[0]))
521 unsigned i, j, k = 0, k_last = 0;
522 unsigned partition_samples;
523 const unsigned default_partition_samples = (residual_samples+predictor_order) >> partition_order;
524 for(i = 0; i < (1u<<partition_order); i++) {
525 partition_samples = default_partition_samples;
527 partition_samples -= predictor_order;
528 k += partition_samples;
529 if(raw_bits[i] == 0) {
530 if(!FLAC__bitwriter_write_raw_uint32(bw, rice_parameters[i], plen))
532 if(!FLAC__bitwriter_write_rice_signed_block(bw, residual+k_last, k-k_last, rice_parameters[i]))
536 if(!FLAC__bitwriter_write_raw_uint32(bw, pesc, plen))
538 if(!FLAC__bitwriter_write_raw_uint32(bw, raw_bits[i], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN))
540 for(j = k_last; j < k; j++) {
541 if(!FLAC__bitwriter_write_raw_int32(bw, residual[j], raw_bits[i]))