update copyright date to include 2002
[platform/upstream/flac.git] / src / libFLAC / stream_encoder_framing.c
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000,2001,2002  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 <stdio.h>
21 #include "private/stream_encoder_framing.h"
22 #include "private/crc.h"
23 #include "FLAC/assert.h"
24
25 #ifdef max
26 #undef max
27 #endif
28 #define max(x,y) ((x)>(y)?(x):(y))
29
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);
32
33 FLAC__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         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))
45                 return false;
46
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))
51                                 return false;
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))
54                                 return false;
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))
57                                 return false;
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))
60                                 return false;
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))
64                                 return false;
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))
68                                 return false;
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))
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                 case FLAC__METADATA_TYPE_APPLICATION:
85                         if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.application.id[0], 8))
86                                 return false;
87                         if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.application.id[1], 8))
88                                 return false;
89                         if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.application.id[2], 8))
90                                 return false;
91                         if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.application.id[3], 8))
92                                 return false;
93                         for(i = 0; i < metadata->length; i++) {
94                                 if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.application.data[i], 8))
95                                         return false;
96                         }
97                         break;
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))
101                                         return false;
102                                 if(!FLAC__bitbuffer_write_raw_uint64(bb, metadata->data.seek_table.points[i].stream_offset, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN))
103                                         return false;
104                                 if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.seek_table.points[i].frame_samples, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN))
105                                         return false;
106                         }
107                         break;
108                 default:
109                         FLAC__ASSERT(0);
110         }
111
112         return true;
113 }
114
115 FLAC__bool FLAC__frame_add_header(const FLAC__FrameHeader *header, FLAC__bool streamable_subset, FLAC__bool is_last_block, FLAC__BitBuffer *bb)
116 {
117         unsigned u, crc8_start, blocksize_hint, sample_rate_hint;
118         FLAC__byte crc8;
119
120         FLAC__ASSERT(bb->bits == 0); /* assert that we're byte-aligned before writing */
121
122         crc8_start = bb->bytes;
123
124         if(!FLAC__bitbuffer_write_raw_uint32(bb, FLAC__FRAME_HEADER_SYNC, FLAC__FRAME_HEADER_SYNC_LEN))
125                 return false;
126
127         if(!FLAC__bitbuffer_write_raw_uint32(bb, 0, FLAC__FRAME_HEADER_RESERVED_LEN))
128                 return false;
129
130         FLAC__ASSERT(header->blocksize > 0 && header->blocksize <= FLAC__MAX_BLOCK_SIZE);
131         blocksize_hint = 0;
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;
146                 default:
147                         if(streamable_subset || is_last_block) {
148                                 if(header->blocksize <= 0x100)
149                                         blocksize_hint = u = 6;
150                                 else
151                                         blocksize_hint = u = 7;
152                         }
153                         else
154                                 u = 0;
155                         break;
156         }
157         if(!FLAC__bitbuffer_write_raw_uint32(bb, u, FLAC__FRAME_HEADER_BLOCK_SIZE_LEN))
158                 return false;
159
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;
171                 default:
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;
177                                 else
178                                         sample_rate_hint = u = 13;
179                         }
180                         else
181                                 u = 0;
182                         break;
183         }
184         if(!FLAC__bitbuffer_write_raw_uint32(bb, u, FLAC__FRAME_HEADER_SAMPLE_RATE_LEN))
185                 return false;
186
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;
191                         break;
192                 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
193                         FLAC__ASSERT(header->channels == 2);
194                         u = 8;
195                         break;
196                 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
197                         FLAC__ASSERT(header->channels == 2);
198                         u = 9;
199                         break;
200                 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
201                         FLAC__ASSERT(header->channels == 2);
202                         u = 10;
203                         break;
204                 default:
205                         FLAC__ASSERT(0);
206         }
207         if(!FLAC__bitbuffer_write_raw_uint32(bb, u, FLAC__FRAME_HEADER_CHANNEL_ASSIGNMENT_LEN))
208                 return false;
209
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;
218         }
219         if(!FLAC__bitbuffer_write_raw_uint32(bb, u, FLAC__FRAME_HEADER_BITS_PER_SAMPLE_LEN))
220                 return false;
221
222         if(!FLAC__bitbuffer_write_raw_uint32(bb, 0, FLAC__FRAME_HEADER_ZERO_PAD_LEN))
223                 return false;
224
225         FLAC__ASSERT(header->number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER);
226         if(!FLAC__bitbuffer_write_utf8_uint32(bb, header->number.frame_number))
227                 return false;
228
229         if(blocksize_hint)
230                 if(!FLAC__bitbuffer_write_raw_uint32(bb, header->blocksize-1, (blocksize_hint==6)? 8:16))
231                         return false;
232
233         switch(sample_rate_hint) {
234                 case 12:
235                         if(!FLAC__bitbuffer_write_raw_uint32(bb, header->sample_rate / 1000, 8))
236                                 return false;
237                         break;
238                 case 13:
239                         if(!FLAC__bitbuffer_write_raw_uint32(bb, header->sample_rate, 16))
240                                 return false;
241                         break;
242                 case 14:
243                         if(!FLAC__bitbuffer_write_raw_uint32(bb, header->sample_rate / 10, 16))
244                                 return false;
245                         break;
246         }
247
248         /* write the CRC */
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))
253                 return false;
254
255         return true;
256 }
257
258 FLAC__bool FLAC__subframe_add_constant(const FLAC__Subframe_Constant *subframe, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitBuffer *bb)
259 {
260         FLAC__bool ok;
261
262         ok =
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)
266         ;
267
268         return ok;
269 }
270
271 FLAC__bool FLAC__subframe_add_fixed(const FLAC__Subframe_Fixed *subframe, unsigned residual_samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitBuffer *bb)
272 {
273         unsigned i;
274
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))
276                 return false;
277         if(wasted_bits)
278                 if(!FLAC__bitbuffer_write_unary_unsigned(bb, wasted_bits-1))
279                         return false;
280
281         for(i = 0; i < subframe->order; i++)
282                 if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->warmup[i], subframe_bps))
283                         return false;
284
285         if(!subframe_add_entropy_coding_method_(bb, &subframe->entropy_coding_method))
286                 return false;
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))
290                                 return false;
291                         break;
292                 default:
293                         FLAC__ASSERT(0);
294         }
295
296         return true;
297 }
298
299 FLAC__bool FLAC__subframe_add_lpc(const FLAC__Subframe_LPC *subframe, unsigned residual_samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitBuffer *bb)
300 {
301         unsigned i;
302
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))
304                 return false;
305         if(wasted_bits)
306                 if(!FLAC__bitbuffer_write_unary_unsigned(bb, wasted_bits-1))
307                         return false;
308
309         for(i = 0; i < subframe->order; i++)
310                 if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->warmup[i], subframe_bps))
311                         return false;
312
313         if(!FLAC__bitbuffer_write_raw_uint32(bb, subframe->qlp_coeff_precision-1, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
314                 return false;
315         if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->quantization_level, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN))
316                 return false;
317         for(i = 0; i < subframe->order; i++)
318                 if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->qlp_coeff[i], subframe->qlp_coeff_precision))
319                         return false;
320
321         if(!subframe_add_entropy_coding_method_(bb, &subframe->entropy_coding_method))
322                 return false;
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))
326                                 return false;
327                         break;
328                 default:
329                         FLAC__ASSERT(0);
330         }
331
332         return true;
333 }
334
335 FLAC__bool FLAC__subframe_add_verbatim(const FLAC__Subframe_Verbatim *subframe, unsigned samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitBuffer *bb)
336 {
337         unsigned i;
338         const FLAC__int32 *signal = subframe->data;
339
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))
341                 return false;
342         if(wasted_bits)
343                 if(!FLAC__bitbuffer_write_unary_unsigned(bb, wasted_bits-1))
344                         return false;
345
346         for(i = 0; i < samples; i++)
347                 if(!FLAC__bitbuffer_write_raw_int32(bb, signal[i], subframe_bps))
348                         return false;
349
350         return true;
351 }
352
353 FLAC__bool subframe_add_entropy_coding_method_(FLAC__BitBuffer *bb, const FLAC__EntropyCodingMethod *method)
354 {
355         if(!FLAC__bitbuffer_write_raw_uint32(bb, method->type, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
356                 return false;
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))
360                                 return false;
361                         break;
362                 default:
363                         FLAC__ASSERT(0);
364         }
365         return true;
366 }
367
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)
369 {
370         if(partition_order == 0) {
371                 unsigned i;
372
373                 if(!FLAC__bitbuffer_write_raw_uint32(bb, rice_parameters[0], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
374                         return false;
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]))
379                                         return false;
380 #else
381                                 if(!FLAC__bitbuffer_write_rice_signed(bb, residual[i], rice_parameters[0]))
382                                         return false;
383 #endif
384                         }
385                 }
386                 else {
387                         if(!FLAC__bitbuffer_write_raw_uint32(bb, raw_bits[0], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN))
388                                 return false;
389                         for(i = 0; i < residual_samples; i++) {
390                                 if(!FLAC__bitbuffer_write_raw_int32(bb, residual[i], raw_bits[0]))
391                                         return false;
392                         }
393                 }
394                 return true;
395         }
396         else {
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))
402                                 return false;
403                         partition_samples = default_partition_samples;
404                         if(i == 0)
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]))
411                                                 return false;
412 #else
413                                         if(!FLAC__bitbuffer_write_rice_signed(bb, residual[j], rice_parameters[i]))
414                                                 return false;
415 #endif
416                                 }
417                         }
418                         else {
419                                 if(!FLAC__bitbuffer_write_raw_uint32(bb, raw_bits[i], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN))
420                                         return false;
421                                 for(j = k_last; j < k; j++) {
422                                         if(!FLAC__bitbuffer_write_raw_int32(bb, residual[j], raw_bits[i]))
423                                                 return false;
424                                 }
425                         }
426                         k_last = k;
427                 }
428                 return true;
429         }
430 }