1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001 Josh Coalson
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include "private/encoder_framing.h"
23 #include "private/crc.h"
28 #define max(x,y) ((x)>(y)?(x):(y))
30 static bool subframe_add_entropy_coding_method_(FLAC__BitBuffer *bb, const FLAC__EntropyCodingMethod *method);
31 static bool subframe_add_residual_partitioned_rice_(FLAC__BitBuffer *bb, const int32 residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned rice_parameters[], const unsigned raw_bits[], const unsigned partition_order);
33 bool FLAC__add_metadata_block(const FLAC__StreamMetaData *metadata, FLAC__BitBuffer *bb)
37 if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->is_last, FLAC__STREAM_METADATA_IS_LAST_LEN))
40 if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->type, FLAC__STREAM_METADATA_TYPE_LEN))
43 assert(metadata->length < (1u << FLAC__STREAM_METADATA_LENGTH_LEN));
44 if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->length, FLAC__STREAM_METADATA_LENGTH_LEN))
47 switch(metadata->type) {
48 case FLAC__METADATA_TYPE_STREAMINFO:
49 assert(metadata->data.stream_info.min_blocksize < (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN));
50 if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.stream_info.min_blocksize, FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN))
52 assert(metadata->data.stream_info.max_blocksize < (1u << FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN));
53 if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.stream_info.max_blocksize, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN))
55 assert(metadata->data.stream_info.min_framesize < (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN));
56 if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.stream_info.min_framesize, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN))
58 assert(metadata->data.stream_info.max_framesize < (1u << FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN));
59 if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.stream_info.max_framesize, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN))
61 assert(metadata->data.stream_info.sample_rate > 0);
62 assert(metadata->data.stream_info.sample_rate < (1u << FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN));
63 if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.stream_info.sample_rate, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN))
65 assert(metadata->data.stream_info.channels > 0);
66 assert(metadata->data.stream_info.channels <= (1u << FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN));
67 if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.stream_info.channels-1, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN))
69 assert(metadata->data.stream_info.bits_per_sample > 0);
70 assert(metadata->data.stream_info.bits_per_sample <= (1u << FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN));
71 if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.stream_info.bits_per_sample-1, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN))
73 if(!FLAC__bitbuffer_write_raw_uint64(bb, metadata->data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN))
75 for(i = 0; i < 16; i++) {
76 if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.stream_info.md5sum[i], 8))
80 case FLAC__METADATA_TYPE_PADDING:
81 if(!FLAC__bitbuffer_write_zeroes(bb, metadata->length * 8))
91 bool FLAC__frame_add_header(const FLAC__FrameHeader *header, bool streamable_subset, bool is_last_block, FLAC__BitBuffer *bb)
93 unsigned u, crc8_start, blocksize_hint, sample_rate_hint;
96 assert(bb->bits == 0); /* assert that we're byte-aligned before writing */
98 crc8_start = bb->bytes;
100 if(!FLAC__bitbuffer_write_raw_uint32(bb, FLAC__FRAME_HEADER_SYNC, FLAC__FRAME_HEADER_SYNC_LEN))
103 if(!FLAC__bitbuffer_write_raw_uint32(bb, 0, FLAC__FRAME_HEADER_RESERVED_LEN))
106 assert(header->blocksize > 0 && header->blocksize <= FLAC__MAX_BLOCK_SIZE);
108 switch(header->blocksize) {
109 case 192: u = 1; break;
110 case 576: u = 2; break;
111 case 1152: u = 3; break;
112 case 2304: u = 4; break;
113 case 4608: u = 5; break;
114 case 256: u = 8; break;
115 case 512: u = 9; break;
116 case 1024: u = 10; break;
117 case 2048: u = 11; break;
118 case 4096: u = 12; break;
119 case 8192: u = 13; break;
120 case 16384: u = 14; break;
121 case 32768: u = 15; break;
123 if(streamable_subset || is_last_block) {
124 if(header->blocksize <= 0x100)
125 blocksize_hint = u = 6;
127 blocksize_hint = u = 7;
133 if(!FLAC__bitbuffer_write_raw_uint32(bb, u, FLAC__FRAME_HEADER_BLOCK_SIZE_LEN))
136 assert(header->sample_rate > 0 && header->sample_rate < (1u << FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN));
137 sample_rate_hint = 0;
138 switch(header->sample_rate) {
139 case 8000: u = 4; break;
140 case 16000: u = 5; break;
141 case 22050: u = 6; break;
142 case 24000: u = 7; break;
143 case 32000: u = 8; break;
144 case 44100: u = 9; break;
145 case 48000: u = 10; break;
146 case 96000: u = 11; break;
148 if(streamable_subset) {
149 if(header->sample_rate % 1000 == 0)
150 sample_rate_hint = u = 12;
151 else if(header->sample_rate % 10 == 0)
152 sample_rate_hint = u = 14;
154 sample_rate_hint = u = 13;
160 if(!FLAC__bitbuffer_write_raw_uint32(bb, u, FLAC__FRAME_HEADER_SAMPLE_RATE_LEN))
163 assert(header->channels > 0 && header->channels <= (1u << FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN) && header->channels <= FLAC__MAX_CHANNELS);
164 switch(header->channel_assignment) {
165 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
166 u = header->channels - 1;
168 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
169 assert(header->channels == 2);
172 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
173 assert(header->channels == 2);
176 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
177 assert(header->channels == 2);
183 if(!FLAC__bitbuffer_write_raw_uint32(bb, u, FLAC__FRAME_HEADER_CHANNEL_ASSIGNMENT_LEN))
186 assert(header->bits_per_sample > 0 && header->bits_per_sample <= (1u << FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN));
187 switch(header->bits_per_sample) {
188 case 8 : u = 1; break;
189 case 12: u = 2; break;
190 case 16: u = 4; break;
191 case 20: u = 5; break;
192 case 24: u = 6; break;
193 default: u = 0; break;
195 if(!FLAC__bitbuffer_write_raw_uint32(bb, u, FLAC__FRAME_HEADER_BITS_PER_SAMPLE_LEN))
198 if(!FLAC__bitbuffer_write_raw_uint32(bb, 0, FLAC__FRAME_HEADER_ZERO_PAD_LEN))
201 if(!FLAC__bitbuffer_write_utf8_uint32(bb, header->number.frame_number))
205 if(!FLAC__bitbuffer_write_raw_uint32(bb, header->blocksize-1, (blocksize_hint==6)? 8:16))
208 switch(sample_rate_hint) {
210 if(!FLAC__bitbuffer_write_raw_uint32(bb, header->sample_rate / 1000, 8))
214 if(!FLAC__bitbuffer_write_raw_uint32(bb, header->sample_rate, 16))
218 if(!FLAC__bitbuffer_write_raw_uint32(bb, header->sample_rate / 10, 16))
224 assert(bb->buffer[crc8_start] == 0xff); /* MAGIC NUMBER for the first byte of the sync code */
225 assert(bb->bits == 0); /* assert that we're byte-aligned */
226 crc8 = FLAC__crc8(bb->buffer+crc8_start, bb->bytes-crc8_start);
227 if(!FLAC__bitbuffer_write_raw_uint32(bb, crc8, FLAC__FRAME_HEADER_CRC_LEN))
233 bool FLAC__subframe_add_constant(const FLAC__Subframe_Constant *subframe, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitBuffer *bb)
238 FLAC__bitbuffer_write_raw_uint32(bb, 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) &&
239 (wasted_bits? FLAC__bitbuffer_write_unary_unsigned(bb, wasted_bits-1) : true) &&
240 FLAC__bitbuffer_write_raw_int32(bb, subframe->value, subframe_bps)
246 bool FLAC__subframe_add_fixed(const FLAC__Subframe_Fixed *subframe, unsigned residual_samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitBuffer *bb)
250 if(!FLAC__bitbuffer_write_raw_uint32(bb, 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))
253 if(!FLAC__bitbuffer_write_unary_unsigned(bb, wasted_bits-1))
256 for(i = 0; i < subframe->order; i++)
257 if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->warmup[i], subframe_bps))
260 if(!subframe_add_entropy_coding_method_(bb, &subframe->entropy_coding_method))
262 switch(subframe->entropy_coding_method.type) {
263 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
264 if(!subframe_add_residual_partitioned_rice_(bb, subframe->residual, residual_samples, subframe->order, subframe->entropy_coding_method.data.partitioned_rice.parameters, subframe->entropy_coding_method.data.partitioned_rice.raw_bits, subframe->entropy_coding_method.data.partitioned_rice.order))
274 bool FLAC__subframe_add_lpc(const FLAC__Subframe_LPC *subframe, unsigned residual_samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitBuffer *bb)
278 if(!FLAC__bitbuffer_write_raw_uint32(bb, 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))
281 if(!FLAC__bitbuffer_write_unary_unsigned(bb, wasted_bits-1))
284 for(i = 0; i < subframe->order; i++)
285 if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->warmup[i], subframe_bps))
288 if(!FLAC__bitbuffer_write_raw_uint32(bb, subframe->qlp_coeff_precision-1, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
290 if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->quantization_level, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN))
292 for(i = 0; i < subframe->order; i++)
293 if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->qlp_coeff[i], subframe->qlp_coeff_precision))
296 if(!subframe_add_entropy_coding_method_(bb, &subframe->entropy_coding_method))
298 switch(subframe->entropy_coding_method.type) {
299 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
300 if(!subframe_add_residual_partitioned_rice_(bb, subframe->residual, residual_samples, subframe->order, subframe->entropy_coding_method.data.partitioned_rice.parameters, subframe->entropy_coding_method.data.partitioned_rice.raw_bits, subframe->entropy_coding_method.data.partitioned_rice.order))
310 bool FLAC__subframe_add_verbatim(const FLAC__Subframe_Verbatim *subframe, unsigned samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitBuffer *bb)
313 const int32 *signal = subframe->data;
315 if(!FLAC__bitbuffer_write_raw_uint32(bb, 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))
318 if(!FLAC__bitbuffer_write_unary_unsigned(bb, wasted_bits-1))
321 for(i = 0; i < samples; i++)
322 if(!FLAC__bitbuffer_write_raw_int32(bb, signal[i], subframe_bps))
328 bool subframe_add_entropy_coding_method_(FLAC__BitBuffer *bb, const FLAC__EntropyCodingMethod *method)
330 if(!FLAC__bitbuffer_write_raw_uint32(bb, method->type, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
332 switch(method->type) {
333 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
334 if(!FLAC__bitbuffer_write_raw_uint32(bb, method->data.partitioned_rice.order, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
343 bool subframe_add_residual_partitioned_rice_(FLAC__BitBuffer *bb, const int32 residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned rice_parameters[], const unsigned raw_bits[], const unsigned partition_order)
345 if(partition_order == 0) {
348 if(!FLAC__bitbuffer_write_raw_uint32(bb, rice_parameters[0], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
350 if(rice_parameters[0] < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
351 for(i = 0; i < residual_samples; i++) {
352 #ifdef SYMMETRIC_RICE
353 if(!FLAC__bitbuffer_write_symmetric_rice_signed(bb, residual[i], rice_parameters[0]))
356 if(!FLAC__bitbuffer_write_rice_signed(bb, residual[i], rice_parameters[0]))
362 if(!FLAC__bitbuffer_write_raw_uint32(bb, raw_bits[0], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN))
364 for(i = 0; i < residual_samples; i++) {
365 if(!FLAC__bitbuffer_write_raw_int32(bb, residual[i], raw_bits[0]))
372 unsigned i, j, k = 0, k_last = 0;
373 unsigned partition_samples;
374 for(i = 0; i < (1u<<partition_order); i++) {
375 if(!FLAC__bitbuffer_write_raw_uint32(bb, rice_parameters[i], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
377 partition_samples = (residual_samples+predictor_order) >> partition_order;
379 partition_samples -= predictor_order;
380 k += partition_samples;
381 if(rice_parameters[i] < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
382 for(j = k_last; j < k; j++) {
383 #ifdef SYMMETRIC_RICE
384 if(!FLAC__bitbuffer_write_symmetric_rice_signed(bb, residual[j], rice_parameters[i]))
387 if(!FLAC__bitbuffer_write_rice_signed(bb, residual[j], rice_parameters[i]))
393 if(!FLAC__bitbuffer_write_raw_uint32(bb, raw_bits[i], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN))
395 for(j = k_last; j < k; j++) {
396 if(!FLAC__bitbuffer_write_raw_int32(bb, residual[j], raw_bits[i]))