move folded rice stuff sideways, re-add symmetric rice in prep for escape codes
[platform/upstream/flac.git] / src / libFLAC / encoder_framing.c
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000,2001  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_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))
51                                 return false;
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))
54                                 return false;
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))
57                                 return false;
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))
60                                 return false;
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))
64                                 return false;
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))
68                                 return false;
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))
72                                 return false;
73                         if(!FLAC__bitbuffer_write_raw_uint64(bb, metadata->data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN))
74                                 return false;
75                         for(i = 0; i < 16; i++) {
76                                 if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.stream_info.md5sum[i], 8))
77                                         return false;
78                         }
79                         break;
80                 case FLAC__METADATA_TYPE_PADDING:
81                         if(!FLAC__bitbuffer_write_zeroes(bb, metadata->length * 8))
82                                 return false;
83                         break;
84                 default:
85                         assert(0);
86         }
87
88         return true;
89 }
90
91 bool FLAC__frame_add_header(const FLAC__FrameHeader *header, bool streamable_subset, bool is_last_block, FLAC__BitBuffer *bb)
92 {
93         unsigned u, crc_start, blocksize_hint, sample_rate_hint;
94         byte crc;
95
96         assert(bb->bits == 0); /* assert that we're byte-aligned before writing */
97
98         crc_start = bb->bytes;
99
100         if(!FLAC__bitbuffer_write_raw_uint32(bb, FLAC__FRAME_HEADER_SYNC, FLAC__FRAME_HEADER_SYNC_LEN))
101                 return false;
102
103         assert(header->blocksize > 0 && header->blocksize <= FLAC__MAX_BLOCK_SIZE);
104         blocksize_hint = 0;
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;
111                 default:
112                         if(streamable_subset || is_last_block) {
113                                 if(header->blocksize <= 0x100)
114                                         blocksize_hint = u = 6;
115                                 else
116                                         blocksize_hint = u = 7;
117                         }
118                         else
119                                 u = 0;
120                         break;
121         }
122         if(!FLAC__bitbuffer_write_raw_uint32(bb, u, FLAC__FRAME_HEADER_BLOCK_SIZE_LEN))
123                 return false;
124
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;
136                 default:
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;
142                                 else
143                                         sample_rate_hint = u = 13;
144                         }
145                         else
146                                 u = 0;
147                         break;
148         }
149         if(!FLAC__bitbuffer_write_raw_uint32(bb, u, FLAC__FRAME_HEADER_SAMPLE_RATE_LEN))
150                 return false;
151
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;
156                         break;
157                 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
158                         assert(header->channels == 2);
159                         u = 8;
160                         break;
161                 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
162                         assert(header->channels == 2);
163                         u = 9;
164                         break;
165                 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
166                         assert(header->channels == 2);
167                         u = 10;
168                         break;
169                 default:
170                         assert(0);
171         }
172         if(!FLAC__bitbuffer_write_raw_uint32(bb, u, FLAC__FRAME_HEADER_CHANNEL_ASSIGNMENT_LEN))
173                 return false;
174
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;
183         }
184         if(!FLAC__bitbuffer_write_raw_uint32(bb, u, FLAC__FRAME_HEADER_BITS_PER_SAMPLE_LEN))
185                 return false;
186
187         if(!FLAC__bitbuffer_write_raw_uint32(bb, 0, FLAC__FRAME_HEADER_ZERO_PAD_LEN))
188                 return false;
189
190         if(!FLAC__bitbuffer_write_utf8_uint32(bb, header->number.frame_number))
191                 return false;
192
193         if(blocksize_hint)
194                 if(!FLAC__bitbuffer_write_raw_uint32(bb, header->blocksize-1, (blocksize_hint==6)? 8:16))
195                         return false;
196
197         switch(sample_rate_hint) {
198                 case 12:
199                         if(!FLAC__bitbuffer_write_raw_uint32(bb, header->sample_rate / 1000, 8))
200                                 return false;
201                         break;
202                 case 13:
203                         if(!FLAC__bitbuffer_write_raw_uint32(bb, header->sample_rate, 16))
204                                 return false;
205                         break;
206                 case 14:
207                         if(!FLAC__bitbuffer_write_raw_uint32(bb, header->sample_rate / 10, 16))
208                                 return false;
209                         break;
210         }
211
212         /* write the CRC */
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))
217                 return false;
218
219         return true;
220 }
221
222 bool FLAC__subframe_add_constant(const FLAC__Subframe_Constant *subframe, unsigned bits_per_sample, FLAC__BitBuffer *bb)
223 {
224         bool ok;
225
226         ok =
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)
229         ;
230
231         return ok;
232 }
233
234 bool FLAC__subframe_add_fixed(const FLAC__Subframe_Fixed *subframe, unsigned residual_samples, unsigned bits_per_sample, FLAC__BitBuffer *bb)
235 {
236         unsigned i;
237
238         if(!FLAC__bitbuffer_write_raw_uint32(bb, FLAC__SUBFRAME_TYPE_FIXED_BITS | (subframe->order<<1), FLAC__SUBFRAME_TYPE_LEN))
239                 return false;
240
241         for(i = 0; i < subframe->order; i++)
242                 if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->warmup[i], bits_per_sample))
243                         return false;
244
245         if(!subframe_add_entropy_coding_method_(bb, &subframe->entropy_coding_method))
246                 return false;
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))
250                                 return false;
251                         break;
252                 default:
253                         assert(0);
254         }
255
256         return true;
257 }
258
259 bool FLAC__subframe_add_lpc(const FLAC__Subframe_LPC *subframe, unsigned residual_samples, unsigned bits_per_sample, FLAC__BitBuffer *bb)
260 {
261         unsigned i;
262
263         if(!FLAC__bitbuffer_write_raw_uint32(bb, FLAC__SUBFRAME_TYPE_LPC_BITS | ((subframe->order-1)<<1), FLAC__SUBFRAME_TYPE_LEN))
264                 return false;
265
266         for(i = 0; i < subframe->order; i++)
267                 if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->warmup[i], bits_per_sample))
268                         return false;
269
270         if(!FLAC__bitbuffer_write_raw_uint32(bb, subframe->qlp_coeff_precision-1, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
271                 return false;
272         if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->quantization_level, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN))
273                 return false;
274         for(i = 0; i < subframe->order; i++)
275                 if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->qlp_coeff[i], subframe->qlp_coeff_precision))
276                         return false;
277
278         if(!subframe_add_entropy_coding_method_(bb, &subframe->entropy_coding_method))
279                 return false;
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))
283                                 return false;
284                         break;
285                 default:
286                         assert(0);
287         }
288
289         return true;
290 }
291
292 bool FLAC__subframe_add_verbatim(const FLAC__Subframe_Verbatim *subframe, unsigned samples, unsigned bits_per_sample, FLAC__BitBuffer *bb)
293 {
294         unsigned i;
295         const int32 *signal = subframe->data;
296
297         if(!FLAC__bitbuffer_write_raw_uint32(bb, FLAC__SUBFRAME_TYPE_VERBATIM_BITS, FLAC__SUBFRAME_TYPE_LEN))
298                 return false;
299
300         for(i = 0; i < samples; i++)
301                 if(!FLAC__bitbuffer_write_raw_int32(bb, signal[i], bits_per_sample))
302                         return false;
303
304         return true;
305 }
306
307 bool subframe_add_entropy_coding_method_(FLAC__BitBuffer *bb, const FLAC__EntropyCodingMethod *method)
308 {
309         if(!FLAC__bitbuffer_write_raw_uint32(bb, method->type, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
310                 return false;
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))
314                                 return false;
315                         break;
316                 default:
317                         assert(0);
318         }
319         return true;
320 }
321
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)
323 {
324         if(partition_order == 0) {
325                 unsigned i;
326                 if(!FLAC__bitbuffer_write_raw_uint32(bb, rice_parameters[0], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
327                         return false;
328                 for(i = 0; i < residual_samples; i++) {
329 #ifdef FOLDED_RICE
330                         if(!FLAC__bitbuffer_write_rice_signed(bb, residual[i], rice_parameters[0]))
331                                 return false;
332 #else
333                         /* symmetric Rice coding ala Shorten */
334                         if(!FLAC__bitbuffer_write_symmetric_rice_signed(bb, residual[i], rice_parameters[0]))
335                                 return false;
336 #endif
337                 }
338                 return true;
339         }
340         else {
341                 unsigned i, j, k = 0, k_last = 0;
342                 unsigned partition_samples;
343                 for(i = 0; i < (1u<<partition_order); i++) {
344                         if(!FLAC__bitbuffer_write_raw_uint32(bb, rice_parameters[i], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
345                                 return false;
346                         partition_samples = (residual_samples+predictor_order) >> partition_order;
347                         if(i == 0)
348                                 partition_samples -= predictor_order;
349                         k += partition_samples;
350                         for(j = k_last; j < k; j++) {
351 #ifdef FOLDED_RICE
352                                 if(!FLAC__bitbuffer_write_rice_signed(bb, residual[j], rice_parameters[i]))
353                                         return false;
354 #else
355                                 /* symmetric Rice coding ala Shorten */
356                                 if(!FLAC__bitbuffer_write_symmetric_rice_signed(bb, residual[j], rice_parameters[i]))
357                                         return false;
358 #endif
359                         }
360                         k_last = k;
361                 }
362                 return true;
363         }
364 }