Initial revision
[flac.git] / src / libFLAC / encoder_framing.c
1 /* libFLAC - Free Lossless Audio Coder library
2  * Copyright (C) 2000  Josh Coalson
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include <assert.h>
21 #include <stdio.h>
22 #include "private/encoder_framing.h"
23 #include "private/crc.h"
24
25 #ifdef max
26 #undef max
27 #endif
28 #define max(x,y) ((x)>(y)?(x):(y))
29
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);
32
33 bool FLAC__add_metadata_block(const FLAC__StreamMetaData *metadata, FLAC__BitBuffer *bb)
34 {
35         if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->is_last, FLAC__STREAM_METADATA_IS_LAST_LEN))
36                 return false;
37
38         if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->type, FLAC__STREAM_METADATA_TYPE_LEN))
39                 return false;
40
41         assert(metadata->length < (1u << FLAC__STREAM_METADATA_LENGTH_LEN));
42         if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->length, FLAC__STREAM_METADATA_LENGTH_LEN))
43                 return false;
44
45         switch(metadata->type) {
46                 case FLAC__METADATA_TYPE_ENCODING:
47                         assert(metadata->data.encoding.min_blocksize < (1u << FLAC__STREAM_METADATA_ENCODING_MIN_BLOCK_SIZE_LEN));
48                         if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.encoding.min_blocksize, FLAC__STREAM_METADATA_ENCODING_MIN_BLOCK_SIZE_LEN))
49                                 return false;
50                         assert(metadata->data.encoding.max_blocksize < (1u << FLAC__STREAM_METADATA_ENCODING_MAX_BLOCK_SIZE_LEN));
51                         if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.encoding.max_blocksize, FLAC__STREAM_METADATA_ENCODING_MAX_BLOCK_SIZE_LEN))
52                                 return false;
53                         assert(metadata->data.encoding.min_framesize < (1u << FLAC__STREAM_METADATA_ENCODING_MIN_FRAME_SIZE_LEN));
54                         if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.encoding.min_framesize, FLAC__STREAM_METADATA_ENCODING_MIN_FRAME_SIZE_LEN))
55                                 return false;
56                         assert(metadata->data.encoding.max_framesize < (1u << FLAC__STREAM_METADATA_ENCODING_MAX_FRAME_SIZE_LEN));
57                         if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.encoding.max_framesize, FLAC__STREAM_METADATA_ENCODING_MAX_FRAME_SIZE_LEN))
58                                 return false;
59                         assert(metadata->data.encoding.sample_rate > 0);
60                         assert(metadata->data.encoding.sample_rate < (1u << FLAC__STREAM_METADATA_ENCODING_SAMPLE_RATE_LEN));
61                         if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.encoding.sample_rate, FLAC__STREAM_METADATA_ENCODING_SAMPLE_RATE_LEN))
62                                 return false;
63                         assert(metadata->data.encoding.channels > 0);
64                         assert(metadata->data.encoding.channels <= (1u << FLAC__STREAM_METADATA_ENCODING_CHANNELS_LEN));
65                         if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.encoding.channels-1, FLAC__STREAM_METADATA_ENCODING_CHANNELS_LEN))
66                                 return false;
67                         assert(metadata->data.encoding.bits_per_sample > 0);
68                         assert(metadata->data.encoding.bits_per_sample <= (1u << FLAC__STREAM_METADATA_ENCODING_BITS_PER_SAMPLE_LEN));
69                         if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.encoding.bits_per_sample-1, FLAC__STREAM_METADATA_ENCODING_BITS_PER_SAMPLE_LEN))
70                                 return false;
71                         if(!FLAC__bitbuffer_write_raw_uint64(bb, metadata->data.encoding.total_samples, FLAC__STREAM_METADATA_ENCODING_TOTAL_SAMPLES_LEN))
72                                 return false;
73                         break;
74                 default:
75                         assert(0);
76         }
77
78         return true;
79 }
80
81 bool FLAC__frame_add_header(const FLAC__FrameHeader *header, bool streamable_subset, bool is_last_block, FLAC__BitBuffer *bb)
82 {
83         unsigned u, crc_start, blocksize_hint, sample_rate_hint;
84         byte crc;
85
86         assert(bb->bits == 0); /* assert that we're byte-aligned before writing */
87
88         crc_start = bb->bytes;
89
90         if(!FLAC__bitbuffer_write_raw_uint32(bb, FLAC__FRAME_HEADER_SYNC, FLAC__FRAME_HEADER_SYNC_LEN))
91                 return false;
92
93         assert(header->blocksize > 0 && header->blocksize <= FLAC__MAX_BLOCK_SIZE);
94         blocksize_hint = 0;
95         switch(header->blocksize) {
96                 case  192: u = 1; break;
97                 case  576: u = 2; break;
98                 case 1152: u = 3; break;
99                 case 2304: u = 4; break;
100                 case 4608: u = 5; break;
101                 default:
102                         if(streamable_subset || is_last_block) {
103                                 if(header->blocksize <= 0x100)
104                                         blocksize_hint = u = 6;
105                                 else
106                                         blocksize_hint = u = 7;
107                         }
108                         else
109                                 u = 0;
110                         break;
111         }
112         if(!FLAC__bitbuffer_write_raw_uint32(bb, u, FLAC__FRAME_HEADER_BLOCK_SIZE_LEN))
113                 return false;
114
115         assert(header->sample_rate > 0 && header->sample_rate < (1u << FLAC__STREAM_METADATA_ENCODING_SAMPLE_RATE_LEN));
116         sample_rate_hint = 0;
117         switch(header->sample_rate) {
118                 case  8000: u = 4; break;
119                 case 16000: u = 5; break;
120                 case 22050: u = 6; break;
121                 case 24000: u = 7; break;
122                 case 32000: u = 8; break;
123                 case 44100: u = 9; break;
124                 case 48000: u = 10; break;
125                 case 96000: u = 11; break;
126                 default:
127                         if(streamable_subset) {
128                                 if(header->sample_rate % 1000 == 0)
129                                         sample_rate_hint = u = 12;
130                                 else if(header->sample_rate % 10 == 0)
131                                         sample_rate_hint = u = 14;
132                                 else
133                                         sample_rate_hint = u = 13;
134                         }
135                         else
136                                 u = 0;
137                         break;
138         }
139         if(!FLAC__bitbuffer_write_raw_uint32(bb, u, FLAC__FRAME_HEADER_SAMPLE_RATE_LEN))
140                 return false;
141
142         assert(header->channels > 0 && header->channels <= (1u << FLAC__STREAM_METADATA_ENCODING_CHANNELS_LEN) && header->channels <= FLAC__MAX_CHANNELS);
143         switch(header->channel_assignment) {
144                 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
145                         u = header->channels - 1;
146                         break;
147                 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
148                         assert(header->channels == 2);
149                         u = 8;
150                         break;
151                 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
152                         assert(header->channels == 2);
153                         u = 9;
154                         break;
155                 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
156                         assert(header->channels == 2);
157                         u = 10;
158                         break;
159                 default:
160                         assert(0);
161         }
162         if(!FLAC__bitbuffer_write_raw_uint32(bb, u, FLAC__FRAME_HEADER_CHANNEL_ASSIGNMENT_LEN))
163                 return false;
164
165         assert(header->bits_per_sample > 0 && header->bits_per_sample <= (1u << FLAC__STREAM_METADATA_ENCODING_BITS_PER_SAMPLE_LEN));
166         switch(header->bits_per_sample) {
167                 case 8 : u = 1; break;
168                 case 12: u = 2; break;
169                 case 16: u = 4; break;
170                 case 20: u = 5; break;
171                 case 24: u = 6; break;
172                 default: u = 0; break;
173         }
174         if(!FLAC__bitbuffer_write_raw_uint32(bb, u, FLAC__FRAME_HEADER_BITS_PER_SAMPLE_LEN))
175                 return false;
176
177         if(!FLAC__bitbuffer_write_raw_uint32(bb, 0, FLAC__FRAME_HEADER_ZERO_PAD_LEN))
178                 return false;
179
180         if(!FLAC__bitbuffer_write_utf8_uint32(bb, header->number.frame_number))
181                 return false;
182
183         if(blocksize_hint)
184                 if(!FLAC__bitbuffer_write_raw_uint32(bb, header->blocksize-1, (blocksize_hint==6)? 8:16))
185                         return false;
186
187         switch(sample_rate_hint) {
188                 case 12:
189                         if(!FLAC__bitbuffer_write_raw_uint32(bb, header->sample_rate / 1000, 8))
190                                 return false;
191                         break;
192                 case 13:
193                         if(!FLAC__bitbuffer_write_raw_uint32(bb, header->sample_rate, 16))
194                                 return false;
195                         break;
196                 case 14:
197                         if(!FLAC__bitbuffer_write_raw_uint32(bb, header->sample_rate / 10, 16))
198                                 return false;
199                         break;
200         }
201
202         /* write the CRC */
203         assert(bb->buffer[crc_start] == 0xff); /* MAGIC NUMBER for the first byte of the sync code */
204         assert(bb->bits == 0); /* assert that we're byte-aligned */
205         crc = FLAC__crc8(bb->buffer+crc_start, bb->bytes-crc_start);
206         if(!FLAC__bitbuffer_write_raw_uint32(bb, crc, FLAC__FRAME_HEADER_CRC8_LEN))
207                 return false;
208
209         return true;
210 }
211
212 bool FLAC__subframe_add_constant(unsigned bits_per_sample, const FLAC__SubframeHeader *subframe, FLAC__BitBuffer *bb)
213 {
214         bool ok;
215
216         ok =
217                 FLAC__bitbuffer_write_raw_uint32(bb, FLAC__SUBFRAME_HEADER_TYPE_CONSTANT, FLAC__SUBFRAME_HEADER_TYPE_LEN) &&
218                 FLAC__bitbuffer_write_raw_int32(bb, subframe->data.constant.value, bits_per_sample)
219         ;
220
221         return ok;
222 }
223
224 bool FLAC__subframe_add_fixed(const int32 residual[], unsigned residual_samples, unsigned bits_per_sample, const FLAC__SubframeHeader *subframe, FLAC__BitBuffer *bb)
225 {
226         unsigned i;
227
228         if(!FLAC__bitbuffer_write_raw_uint32(bb, FLAC__SUBFRAME_HEADER_TYPE_FIXED | (subframe->data.fixed.order<<1), FLAC__SUBFRAME_HEADER_TYPE_LEN))
229                 return false;
230
231         for(i = 0; i < subframe->data.fixed.order; i++)
232                 if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->data.fixed.warmup[i], bits_per_sample))
233                         return false;
234
235         if(!subframe_add_entropy_coding_method_(bb, &subframe->data.fixed.entropy_coding_method))
236                 return false;
237         switch(subframe->data.fixed.entropy_coding_method.type) {
238                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
239                         if(!subframe_add_residual_partitioned_rice_(bb, residual, residual_samples, subframe->data.fixed.order, subframe->data.fixed.entropy_coding_method.data.partitioned_rice.parameters, subframe->data.fixed.entropy_coding_method.data.partitioned_rice.order))
240                                 return false;
241                         break;
242                 default:
243                         assert(0);
244         }
245
246         return true;
247 }
248
249 bool FLAC__subframe_add_lpc(const int32 residual[], unsigned residual_samples, unsigned bits_per_sample, const FLAC__SubframeHeader *subframe, FLAC__BitBuffer *bb)
250 {
251         unsigned i;
252
253         if(!FLAC__bitbuffer_write_raw_uint32(bb, FLAC__SUBFRAME_HEADER_TYPE_LPC | ((subframe->data.lpc.order-1)<<1), FLAC__SUBFRAME_HEADER_TYPE_LEN))
254                 return false;
255
256         for(i = 0; i < subframe->data.lpc.order; i++)
257                 if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->data.lpc.warmup[i], bits_per_sample))
258                         return false;
259
260         if(!FLAC__bitbuffer_write_raw_uint32(bb, subframe->data.lpc.qlp_coeff_precision-1, FLAC__SUBFRAME_HEADER_LPC_QLP_COEFF_PRECISION_LEN))
261                 return false;
262         if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->data.lpc.quantization_level, FLAC__SUBFRAME_HEADER_LPC_QLP_SHIFT_LEN))
263                 return false;
264         for(i = 0; i < subframe->data.lpc.order; i++)
265                 if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->data.lpc.qlp_coeff[i], subframe->data.lpc.qlp_coeff_precision))
266                         return false;
267
268         if(!subframe_add_entropy_coding_method_(bb, &subframe->data.lpc.entropy_coding_method))
269                 return false;
270         switch(subframe->data.lpc.entropy_coding_method.type) {
271                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
272                         if(!subframe_add_residual_partitioned_rice_(bb, residual, residual_samples, subframe->data.lpc.order, subframe->data.fixed.entropy_coding_method.data.partitioned_rice.parameters, subframe->data.lpc.entropy_coding_method.data.partitioned_rice.order))
273                                 return false;
274                         break;
275                 default:
276                         assert(0);
277         }
278
279         return true;
280 }
281
282 bool FLAC__subframe_add_verbatim(const int32 signal[], unsigned samples, unsigned bits_per_sample, FLAC__BitBuffer *bb)
283 {
284         unsigned i;
285
286         if(!FLAC__bitbuffer_write_raw_uint32(bb, FLAC__SUBFRAME_HEADER_TYPE_VERBATIM, FLAC__SUBFRAME_HEADER_TYPE_LEN))
287                 return false;
288
289         for(i = 0; i < samples; i++)
290                 if(!FLAC__bitbuffer_write_raw_int32(bb, signal[i], bits_per_sample))
291                         return false;
292
293         return true;
294 }
295
296 bool subframe_add_entropy_coding_method_(FLAC__BitBuffer *bb, const FLAC__EntropyCodingMethod *method)
297 {
298         if(!FLAC__bitbuffer_write_raw_uint32(bb, method->type, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
299                 return false;
300         switch(method->type) {
301                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
302                         if(!FLAC__bitbuffer_write_raw_uint32(bb, method->data.partitioned_rice.order, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
303                                 return false;
304                         break;
305                 default:
306                         assert(0);
307         }
308         return true;
309 }
310
311 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)
312 {
313         if(partition_order == 0) {
314                 unsigned i;
315                 if(!FLAC__bitbuffer_write_raw_uint32(bb, rice_parameters[0], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
316                         return false;
317                 for(i = 0; i < residual_samples; i++) {
318                         if(!FLAC__bitbuffer_write_rice_signed(bb, residual[i], rice_parameters[0]))
319                                 return false;
320                 }
321                 return true;
322         }
323         else {
324                 unsigned i, j, k = 0, k_last = 0;
325                 unsigned partition_samples;
326                 for(i = 0; i < (1u<<partition_order); i++) {
327                         if(!FLAC__bitbuffer_write_raw_uint32(bb, rice_parameters[i], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
328                                 return false;
329                         partition_samples = (residual_samples+predictor_order) >> partition_order;
330                         if(i == 0)
331                                 partition_samples -= predictor_order;
332                         k += partition_samples;
333                         for(j = k_last; j < k; j++)
334                                 if(!FLAC__bitbuffer_write_rice_signed(bb, residual[j], rice_parameters[i]))
335                                         return false;
336                         k_last = k;
337                 }
338                 return true;
339         }
340 }