1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002 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.
21 #include "private/stream_encoder_framing.h"
22 #include "private/crc.h"
23 #include "FLAC/assert.h"
28 #define max(x,y) ((x)>(y)?(x):(y))
30 static FLAC__bool subframe_add_entropy_coding_method_(FLAC__BitBuffer *bb, const FLAC__EntropyCodingMethod *method);
31 static FLAC__bool subframe_add_residual_partitioned_rice_(FLAC__BitBuffer *bb, const FLAC__int32 residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned rice_parameters[], const unsigned raw_bits[], const unsigned partition_order);
33 FLAC__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 FLAC__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 FLAC__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 FLAC__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 FLAC__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 FLAC__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 FLAC__ASSERT(metadata->data.stream_info.sample_rate > 0);
62 FLAC__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 FLAC__ASSERT(metadata->data.stream_info.channels > 0);
66 FLAC__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 FLAC__ASSERT(metadata->data.stream_info.bits_per_sample > 0);
70 FLAC__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))
84 case FLAC__METADATA_TYPE_APPLICATION:
85 if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.application.id[0], 8))
87 if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.application.id[1], 8))
89 if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.application.id[2], 8))
91 if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.application.id[3], 8))
93 for(i = 0; i < metadata->length; i++) {
94 if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.application.data[i], 8))
98 case FLAC__METADATA_TYPE_SEEKTABLE:
99 for(i = 0; i < metadata->data.seek_table.num_points; i++) {
100 if(!FLAC__bitbuffer_write_raw_uint64(bb, metadata->data.seek_table.points[i].sample_number, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN))
102 if(!FLAC__bitbuffer_write_raw_uint64(bb, metadata->data.seek_table.points[i].stream_offset, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN))
104 if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.seek_table.points[i].frame_samples, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN))
115 FLAC__bool FLAC__frame_add_header(const FLAC__FrameHeader *header, FLAC__bool streamable_subset, FLAC__bool is_last_block, FLAC__BitBuffer *bb)
117 unsigned u, crc8_start, blocksize_hint, sample_rate_hint;
120 FLAC__ASSERT(bb->bits == 0); /* assert that we're byte-aligned before writing */
122 crc8_start = bb->bytes;
124 if(!FLAC__bitbuffer_write_raw_uint32(bb, FLAC__FRAME_HEADER_SYNC, FLAC__FRAME_HEADER_SYNC_LEN))
127 if(!FLAC__bitbuffer_write_raw_uint32(bb, 0, FLAC__FRAME_HEADER_RESERVED_LEN))
130 FLAC__ASSERT(header->blocksize > 0 && header->blocksize <= FLAC__MAX_BLOCK_SIZE);
132 switch(header->blocksize) {
133 case 192: u = 1; break;
134 case 576: u = 2; break;
135 case 1152: u = 3; break;
136 case 2304: u = 4; break;
137 case 4608: u = 5; break;
138 case 256: u = 8; break;
139 case 512: u = 9; break;
140 case 1024: u = 10; break;
141 case 2048: u = 11; break;
142 case 4096: u = 12; break;
143 case 8192: u = 13; break;
144 case 16384: u = 14; break;
145 case 32768: u = 15; break;
147 if(streamable_subset || is_last_block) {
148 if(header->blocksize <= 0x100)
149 blocksize_hint = u = 6;
151 blocksize_hint = u = 7;
157 if(!FLAC__bitbuffer_write_raw_uint32(bb, u, FLAC__FRAME_HEADER_BLOCK_SIZE_LEN))
160 FLAC__ASSERT(header->sample_rate > 0 && header->sample_rate < (1u << FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN));
161 sample_rate_hint = 0;
162 switch(header->sample_rate) {
163 case 8000: u = 4; break;
164 case 16000: u = 5; break;
165 case 22050: u = 6; break;
166 case 24000: u = 7; break;
167 case 32000: u = 8; break;
168 case 44100: u = 9; break;
169 case 48000: u = 10; break;
170 case 96000: u = 11; break;
172 if(streamable_subset) {
173 if(header->sample_rate % 1000 == 0)
174 sample_rate_hint = u = 12;
175 else if(header->sample_rate % 10 == 0)
176 sample_rate_hint = u = 14;
178 sample_rate_hint = u = 13;
184 if(!FLAC__bitbuffer_write_raw_uint32(bb, u, FLAC__FRAME_HEADER_SAMPLE_RATE_LEN))
187 FLAC__ASSERT(header->channels > 0 && header->channels <= (1u << FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN) && header->channels <= FLAC__MAX_CHANNELS);
188 switch(header->channel_assignment) {
189 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
190 u = header->channels - 1;
192 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
193 FLAC__ASSERT(header->channels == 2);
196 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
197 FLAC__ASSERT(header->channels == 2);
200 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
201 FLAC__ASSERT(header->channels == 2);
207 if(!FLAC__bitbuffer_write_raw_uint32(bb, u, FLAC__FRAME_HEADER_CHANNEL_ASSIGNMENT_LEN))
210 FLAC__ASSERT(header->bits_per_sample > 0 && header->bits_per_sample <= (1u << FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN));
211 switch(header->bits_per_sample) {
212 case 8 : u = 1; break;
213 case 12: u = 2; break;
214 case 16: u = 4; break;
215 case 20: u = 5; break;
216 case 24: u = 6; break;
217 default: u = 0; break;
219 if(!FLAC__bitbuffer_write_raw_uint32(bb, u, FLAC__FRAME_HEADER_BITS_PER_SAMPLE_LEN))
222 if(!FLAC__bitbuffer_write_raw_uint32(bb, 0, FLAC__FRAME_HEADER_ZERO_PAD_LEN))
225 FLAC__ASSERT(header->number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER);
226 if(!FLAC__bitbuffer_write_utf8_uint32(bb, header->number.frame_number))
230 if(!FLAC__bitbuffer_write_raw_uint32(bb, header->blocksize-1, (blocksize_hint==6)? 8:16))
233 switch(sample_rate_hint) {
235 if(!FLAC__bitbuffer_write_raw_uint32(bb, header->sample_rate / 1000, 8))
239 if(!FLAC__bitbuffer_write_raw_uint32(bb, header->sample_rate, 16))
243 if(!FLAC__bitbuffer_write_raw_uint32(bb, header->sample_rate / 10, 16))
249 FLAC__ASSERT(bb->buffer[crc8_start] == 0xff); /* MAGIC NUMBER for the first byte of the sync code */
250 FLAC__ASSERT(bb->bits == 0); /* assert that we're byte-aligned */
251 crc8 = FLAC__crc8(bb->buffer+crc8_start, bb->bytes-crc8_start);
252 if(!FLAC__bitbuffer_write_raw_uint32(bb, crc8, FLAC__FRAME_HEADER_CRC_LEN))
258 FLAC__bool FLAC__subframe_add_constant(const FLAC__Subframe_Constant *subframe, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitBuffer *bb)
263 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) &&
264 (wasted_bits? FLAC__bitbuffer_write_unary_unsigned(bb, wasted_bits-1) : true) &&
265 FLAC__bitbuffer_write_raw_int32(bb, subframe->value, subframe_bps)
271 FLAC__bool FLAC__subframe_add_fixed(const FLAC__Subframe_Fixed *subframe, unsigned residual_samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitBuffer *bb)
275 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))
278 if(!FLAC__bitbuffer_write_unary_unsigned(bb, wasted_bits-1))
281 for(i = 0; i < subframe->order; i++)
282 if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->warmup[i], subframe_bps))
285 if(!subframe_add_entropy_coding_method_(bb, &subframe->entropy_coding_method))
287 switch(subframe->entropy_coding_method.type) {
288 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
289 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))
299 FLAC__bool FLAC__subframe_add_lpc(const FLAC__Subframe_LPC *subframe, unsigned residual_samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitBuffer *bb)
303 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))
306 if(!FLAC__bitbuffer_write_unary_unsigned(bb, wasted_bits-1))
309 for(i = 0; i < subframe->order; i++)
310 if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->warmup[i], subframe_bps))
313 if(!FLAC__bitbuffer_write_raw_uint32(bb, subframe->qlp_coeff_precision-1, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
315 if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->quantization_level, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN))
317 for(i = 0; i < subframe->order; i++)
318 if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->qlp_coeff[i], subframe->qlp_coeff_precision))
321 if(!subframe_add_entropy_coding_method_(bb, &subframe->entropy_coding_method))
323 switch(subframe->entropy_coding_method.type) {
324 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
325 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))
335 FLAC__bool FLAC__subframe_add_verbatim(const FLAC__Subframe_Verbatim *subframe, unsigned samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitBuffer *bb)
338 const FLAC__int32 *signal = subframe->data;
340 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))
343 if(!FLAC__bitbuffer_write_unary_unsigned(bb, wasted_bits-1))
346 for(i = 0; i < samples; i++)
347 if(!FLAC__bitbuffer_write_raw_int32(bb, signal[i], subframe_bps))
353 FLAC__bool subframe_add_entropy_coding_method_(FLAC__BitBuffer *bb, const FLAC__EntropyCodingMethod *method)
355 if(!FLAC__bitbuffer_write_raw_uint32(bb, method->type, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
357 switch(method->type) {
358 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
359 if(!FLAC__bitbuffer_write_raw_uint32(bb, method->data.partitioned_rice.order, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
368 FLAC__bool subframe_add_residual_partitioned_rice_(FLAC__BitBuffer *bb, const FLAC__int32 residual[], const unsigned residual_samples, const unsigned predictor_order, const unsigned rice_parameters[], const unsigned raw_bits[], const unsigned partition_order)
370 if(partition_order == 0) {
373 if(!FLAC__bitbuffer_write_raw_uint32(bb, rice_parameters[0], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
375 if(rice_parameters[0] < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
376 for(i = 0; i < residual_samples; i++) {
377 #ifdef FLAC__SYMMETRIC_RICE
378 if(!FLAC__bitbuffer_write_symmetric_rice_signed(bb, residual[i], rice_parameters[0]))
381 if(!FLAC__bitbuffer_write_rice_signed(bb, residual[i], rice_parameters[0]))
387 if(!FLAC__bitbuffer_write_raw_uint32(bb, raw_bits[0], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN))
389 for(i = 0; i < residual_samples; i++) {
390 if(!FLAC__bitbuffer_write_raw_int32(bb, residual[i], raw_bits[0]))
397 unsigned i, j, k = 0, k_last = 0;
398 unsigned partition_samples;
399 const unsigned default_partition_samples = (residual_samples+predictor_order) >> partition_order;
400 for(i = 0; i < (1u<<partition_order); i++) {
401 if(!FLAC__bitbuffer_write_raw_uint32(bb, rice_parameters[i], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
403 partition_samples = default_partition_samples;
405 partition_samples -= predictor_order;
406 k += partition_samples;
407 if(rice_parameters[i] < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
408 for(j = k_last; j < k; j++) {
409 #ifdef FLAC__SYMMETRIC_RICE
410 if(!FLAC__bitbuffer_write_symmetric_rice_signed(bb, residual[j], rice_parameters[i]))
413 if(!FLAC__bitbuffer_write_rice_signed(bb, residual[j], rice_parameters[i]))
419 if(!FLAC__bitbuffer_write_raw_uint32(bb, raw_bits[i], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN))
421 for(j = k_last; j < k; j++) {
422 if(!FLAC__bitbuffer_write_raw_int32(bb, residual[j], raw_bits[i]))