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 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, crc_start, blocksize_hint, sample_rate_hint;
96 assert(bb->bits == 0); /* assert that we're byte-aligned before writing */
98 crc_start = bb->bytes;
100 if(!FLAC__bitbuffer_write_raw_uint32(bb, FLAC__FRAME_HEADER_SYNC, FLAC__FRAME_HEADER_SYNC_LEN))
103 assert(header->blocksize > 0 && header->blocksize <= FLAC__MAX_BLOCK_SIZE);
105 switch(header->blocksize) {
106 case 192: u = 1; break;
107 case 576: u = 2; break;
108 case 1152: u = 3; break;
109 case 2304: u = 4; break;
110 case 4608: u = 5; break;
112 if(streamable_subset || is_last_block) {
113 if(header->blocksize <= 0x100)
114 blocksize_hint = u = 6;
116 blocksize_hint = u = 7;
122 if(!FLAC__bitbuffer_write_raw_uint32(bb, u, FLAC__FRAME_HEADER_BLOCK_SIZE_LEN))
125 assert(header->sample_rate > 0 && header->sample_rate < (1u << FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN));
126 sample_rate_hint = 0;
127 switch(header->sample_rate) {
128 case 8000: u = 4; break;
129 case 16000: u = 5; break;
130 case 22050: u = 6; break;
131 case 24000: u = 7; break;
132 case 32000: u = 8; break;
133 case 44100: u = 9; break;
134 case 48000: u = 10; break;
135 case 96000: u = 11; break;
137 if(streamable_subset) {
138 if(header->sample_rate % 1000 == 0)
139 sample_rate_hint = u = 12;
140 else if(header->sample_rate % 10 == 0)
141 sample_rate_hint = u = 14;
143 sample_rate_hint = u = 13;
149 if(!FLAC__bitbuffer_write_raw_uint32(bb, u, FLAC__FRAME_HEADER_SAMPLE_RATE_LEN))
152 assert(header->channels > 0 && header->channels <= (1u << FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN) && header->channels <= FLAC__MAX_CHANNELS);
153 switch(header->channel_assignment) {
154 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
155 u = header->channels - 1;
157 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
158 assert(header->channels == 2);
161 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
162 assert(header->channels == 2);
165 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
166 assert(header->channels == 2);
172 if(!FLAC__bitbuffer_write_raw_uint32(bb, u, FLAC__FRAME_HEADER_CHANNEL_ASSIGNMENT_LEN))
175 assert(header->bits_per_sample > 0 && header->bits_per_sample <= (1u << FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN));
176 switch(header->bits_per_sample) {
177 case 8 : u = 1; break;
178 case 12: u = 2; break;
179 case 16: u = 4; break;
180 case 20: u = 5; break;
181 case 24: u = 6; break;
182 default: u = 0; break;
184 if(!FLAC__bitbuffer_write_raw_uint32(bb, u, FLAC__FRAME_HEADER_BITS_PER_SAMPLE_LEN))
187 if(!FLAC__bitbuffer_write_raw_uint32(bb, 0, FLAC__FRAME_HEADER_ZERO_PAD_LEN))
190 if(!FLAC__bitbuffer_write_utf8_uint32(bb, header->number.frame_number))
194 if(!FLAC__bitbuffer_write_raw_uint32(bb, header->blocksize-1, (blocksize_hint==6)? 8:16))
197 switch(sample_rate_hint) {
199 if(!FLAC__bitbuffer_write_raw_uint32(bb, header->sample_rate / 1000, 8))
203 if(!FLAC__bitbuffer_write_raw_uint32(bb, header->sample_rate, 16))
207 if(!FLAC__bitbuffer_write_raw_uint32(bb, header->sample_rate / 10, 16))
213 assert(bb->buffer[crc_start] == 0xff); /* MAGIC NUMBER for the first byte of the sync code */
214 assert(bb->bits == 0); /* assert that we're byte-aligned */
215 crc = FLAC__crc8(bb->buffer+crc_start, bb->bytes-crc_start);
216 if(!FLAC__bitbuffer_write_raw_uint32(bb, crc, FLAC__FRAME_HEADER_CRC8_LEN))
222 bool FLAC__subframe_add_constant(const FLAC__Subframe_Constant *subframe, unsigned bits_per_sample, FLAC__BitBuffer *bb)
227 FLAC__bitbuffer_write_raw_uint32(bb, FLAC__SUBFRAME_TYPE_CONSTANT_BITS, FLAC__SUBFRAME_TYPE_LEN) &&
228 FLAC__bitbuffer_write_raw_int32(bb, subframe->value, bits_per_sample)
234 bool FLAC__subframe_add_fixed(const FLAC__Subframe_Fixed *subframe, unsigned residual_samples, unsigned bits_per_sample, FLAC__BitBuffer *bb)
238 if(!FLAC__bitbuffer_write_raw_uint32(bb, FLAC__SUBFRAME_TYPE_FIXED_BITS | (subframe->order<<1), FLAC__SUBFRAME_TYPE_LEN))
241 for(i = 0; i < subframe->order; i++)
242 if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->warmup[i], bits_per_sample))
245 if(!subframe_add_entropy_coding_method_(bb, &subframe->entropy_coding_method))
247 switch(subframe->entropy_coding_method.type) {
248 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
249 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.order))
259 bool FLAC__subframe_add_lpc(const FLAC__Subframe_LPC *subframe, unsigned residual_samples, unsigned bits_per_sample, FLAC__BitBuffer *bb)
263 if(!FLAC__bitbuffer_write_raw_uint32(bb, FLAC__SUBFRAME_TYPE_LPC_BITS | ((subframe->order-1)<<1), FLAC__SUBFRAME_TYPE_LEN))
266 for(i = 0; i < subframe->order; i++)
267 if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->warmup[i], bits_per_sample))
270 if(!FLAC__bitbuffer_write_raw_uint32(bb, subframe->qlp_coeff_precision-1, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
272 if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->quantization_level, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN))
274 for(i = 0; i < subframe->order; i++)
275 if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->qlp_coeff[i], subframe->qlp_coeff_precision))
278 if(!subframe_add_entropy_coding_method_(bb, &subframe->entropy_coding_method))
280 switch(subframe->entropy_coding_method.type) {
281 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
282 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.order))
292 bool FLAC__subframe_add_verbatim(const FLAC__Subframe_Verbatim *subframe, unsigned samples, unsigned bits_per_sample, FLAC__BitBuffer *bb)
295 const int32 *signal = subframe->data;
297 if(!FLAC__bitbuffer_write_raw_uint32(bb, FLAC__SUBFRAME_TYPE_VERBATIM_BITS, FLAC__SUBFRAME_TYPE_LEN))
300 for(i = 0; i < samples; i++)
301 if(!FLAC__bitbuffer_write_raw_int32(bb, signal[i], bits_per_sample))
307 bool subframe_add_entropy_coding_method_(FLAC__BitBuffer *bb, const FLAC__EntropyCodingMethod *method)
309 if(!FLAC__bitbuffer_write_raw_uint32(bb, method->type, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
311 switch(method->type) {
312 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
313 if(!FLAC__bitbuffer_write_raw_uint32(bb, method->data.partitioned_rice.order, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
322 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 partition_order)
324 if(partition_order == 0) {
326 if(!FLAC__bitbuffer_write_raw_uint32(bb, rice_parameters[0], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
328 for(i = 0; i < residual_samples; i++) {
329 if(!FLAC__bitbuffer_write_rice_signed(bb, residual[i], rice_parameters[0]))
335 unsigned i, j, k = 0, k_last = 0;
336 unsigned partition_samples;
337 for(i = 0; i < (1u<<partition_order); i++) {
338 if(!FLAC__bitbuffer_write_raw_uint32(bb, rice_parameters[i], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
340 partition_samples = (residual_samples+predictor_order) >> partition_order;
342 partition_samples -= predictor_order;
343 k += partition_samples;
344 for(j = k_last; j < k; j++) {
345 if(!FLAC__bitbuffer_write_rice_signed(bb, residual[j], rice_parameters[i]))