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