bump version number to 1.1.3
[flac.git] / src / libFLAC / stream_encoder_framing.c
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000,2001,2002,2003,2004,2005,2006  Josh Coalson
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * - Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * - Neither the name of the Xiph.org Foundation nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #if HAVE_CONFIG_H
33 #  include <config.h>
34 #endif
35
36 #include <stdio.h>
37 #include <string.h> /* for strlen() */
38 #include "private/stream_encoder_framing.h"
39 #include "private/crc.h"
40 #include "FLAC/assert.h"
41
42 #ifdef max
43 #undef max
44 #endif
45 #define max(x,y) ((x)>(y)?(x):(y))
46
47 static FLAC__bool add_entropy_coding_method_(FLAC__BitBuffer *bb, const FLAC__EntropyCodingMethod *method);
48 static FLAC__bool 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);
49
50 FLAC__bool FLAC__add_metadata_block(const FLAC__StreamMetadata *metadata, FLAC__BitBuffer *bb)
51 {
52         unsigned i, j;
53         const unsigned vendor_string_length = (unsigned)strlen(FLAC__VENDOR_STRING);
54
55         if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->is_last, FLAC__STREAM_METADATA_IS_LAST_LEN))
56                 return false;
57
58         if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->type, FLAC__STREAM_METADATA_TYPE_LEN))
59                 return false;
60
61         /*
62          * First, for VORBIS_COMMENTs, adjust the length to reflect our vendor string
63          */
64         i = metadata->length;
65         if(metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
66                 FLAC__ASSERT(metadata->data.vorbis_comment.vendor_string.length == 0 || 0 != metadata->data.vorbis_comment.vendor_string.entry);
67                 i -= metadata->data.vorbis_comment.vendor_string.length;
68                 i += vendor_string_length;
69         }
70         FLAC__ASSERT(i < (1u << FLAC__STREAM_METADATA_LENGTH_LEN));
71         if(!FLAC__bitbuffer_write_raw_uint32(bb, i, FLAC__STREAM_METADATA_LENGTH_LEN))
72                 return false;
73
74         switch(metadata->type) {
75                 case FLAC__METADATA_TYPE_STREAMINFO:
76                         FLAC__ASSERT(metadata->data.stream_info.min_blocksize < (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN));
77                         if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.stream_info.min_blocksize, FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN))
78                                 return false;
79                         FLAC__ASSERT(metadata->data.stream_info.max_blocksize < (1u << FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN));
80                         if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.stream_info.max_blocksize, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN))
81                                 return false;
82                         FLAC__ASSERT(metadata->data.stream_info.min_framesize < (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN));
83                         if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.stream_info.min_framesize, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN))
84                                 return false;
85                         FLAC__ASSERT(metadata->data.stream_info.max_framesize < (1u << FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN));
86                         if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.stream_info.max_framesize, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN))
87                                 return false;
88                         FLAC__ASSERT(FLAC__format_sample_rate_is_valid(metadata->data.stream_info.sample_rate));
89                         if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.stream_info.sample_rate, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN))
90                                 return false;
91                         FLAC__ASSERT(metadata->data.stream_info.channels > 0);
92                         FLAC__ASSERT(metadata->data.stream_info.channels <= (1u << FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN));
93                         if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.stream_info.channels-1, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN))
94                                 return false;
95                         FLAC__ASSERT(metadata->data.stream_info.bits_per_sample > 0);
96                         FLAC__ASSERT(metadata->data.stream_info.bits_per_sample <= (1u << FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN));
97                         if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.stream_info.bits_per_sample-1, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN))
98                                 return false;
99                         if(!FLAC__bitbuffer_write_raw_uint64(bb, metadata->data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN))
100                                 return false;
101                         if(!FLAC__bitbuffer_write_byte_block(bb, metadata->data.stream_info.md5sum, 16))
102                                 return false;
103                         break;
104                 case FLAC__METADATA_TYPE_PADDING:
105                         if(!FLAC__bitbuffer_write_zeroes(bb, metadata->length * 8))
106                                 return false;
107                         break;
108                 case FLAC__METADATA_TYPE_APPLICATION:
109                         if(!FLAC__bitbuffer_write_byte_block(bb, metadata->data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8))
110                                 return false;
111                         if(!FLAC__bitbuffer_write_byte_block(bb, metadata->data.application.data, metadata->length - (FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8)))
112                                 return false;
113                         break;
114                 case FLAC__METADATA_TYPE_SEEKTABLE:
115                         for(i = 0; i < metadata->data.seek_table.num_points; i++) {
116                                 if(!FLAC__bitbuffer_write_raw_uint64(bb, metadata->data.seek_table.points[i].sample_number, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN))
117                                         return false;
118                                 if(!FLAC__bitbuffer_write_raw_uint64(bb, metadata->data.seek_table.points[i].stream_offset, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN))
119                                         return false;
120                                 if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.seek_table.points[i].frame_samples, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN))
121                                         return false;
122                         }
123                         break;
124                 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
125                         if(!FLAC__bitbuffer_write_raw_uint32_little_endian(bb, vendor_string_length))
126                                 return false;
127                         if(!FLAC__bitbuffer_write_byte_block(bb, (const FLAC__byte*)FLAC__VENDOR_STRING, vendor_string_length))
128                                 return false;
129                         if(!FLAC__bitbuffer_write_raw_uint32_little_endian(bb, metadata->data.vorbis_comment.num_comments))
130                                 return false;
131                         for(i = 0; i < metadata->data.vorbis_comment.num_comments; i++) {
132                                 if(!FLAC__bitbuffer_write_raw_uint32_little_endian(bb, metadata->data.vorbis_comment.comments[i].length))
133                                         return false;
134                                 if(!FLAC__bitbuffer_write_byte_block(bb, metadata->data.vorbis_comment.comments[i].entry, metadata->data.vorbis_comment.comments[i].length))
135                                         return false;
136                         }
137                         break;
138                 case FLAC__METADATA_TYPE_CUESHEET:
139                         FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
140                         if(!FLAC__bitbuffer_write_byte_block(bb, (const FLAC__byte*)metadata->data.cue_sheet.media_catalog_number, FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN/8))
141                                 return false;
142                         if(!FLAC__bitbuffer_write_raw_uint64(bb, metadata->data.cue_sheet.lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN))
143                                 return false;
144                         if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.cue_sheet.is_cd? 1 : 0, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN))
145                                 return false;
146                         if(!FLAC__bitbuffer_write_zeroes(bb, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN))
147                                 return false;
148                         if(!FLAC__bitbuffer_write_raw_uint32(bb, metadata->data.cue_sheet.num_tracks, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN))
149                                 return false;
150                         for(i = 0; i < metadata->data.cue_sheet.num_tracks; i++) {
151                                 const FLAC__StreamMetadata_CueSheet_Track *track = metadata->data.cue_sheet.tracks + i;
152
153                                 if(!FLAC__bitbuffer_write_raw_uint64(bb, track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN))
154                                         return false;
155                                 if(!FLAC__bitbuffer_write_raw_uint32(bb, track->number, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN))
156                                         return false;
157                                 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
158                                 if(!FLAC__bitbuffer_write_byte_block(bb, (const FLAC__byte*)track->isrc, FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN/8))
159                                         return false;
160                                 if(!FLAC__bitbuffer_write_raw_uint32(bb, track->type, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN))
161                                         return false;
162                                 if(!FLAC__bitbuffer_write_raw_uint32(bb, track->pre_emphasis, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN))
163                                         return false;
164                                 if(!FLAC__bitbuffer_write_zeroes(bb, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN))
165                                         return false;
166                                 if(!FLAC__bitbuffer_write_raw_uint32(bb, track->num_indices, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN))
167                                         return false;
168                                 for(j = 0; j < track->num_indices; j++) {
169                                         const FLAC__StreamMetadata_CueSheet_Index *index = track->indices + j;
170
171                                         if(!FLAC__bitbuffer_write_raw_uint64(bb, index->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN))
172                                                 return false;
173                                         if(!FLAC__bitbuffer_write_raw_uint32(bb, index->number, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN))
174                                                 return false;
175                                         if(!FLAC__bitbuffer_write_zeroes(bb, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN))
176                                                 return false;
177                                 }
178                         }
179                         break;
180                 default:
181                         if(!FLAC__bitbuffer_write_byte_block(bb, metadata->data.unknown.data, metadata->length))
182                                 return false;
183                         break;
184         }
185
186         FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(bb));
187         return true;
188 }
189
190 FLAC__bool FLAC__frame_add_header(const FLAC__FrameHeader *header, FLAC__bool streamable_subset, FLAC__BitBuffer *bb)
191 {
192         unsigned u, blocksize_hint, sample_rate_hint;
193
194         FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(bb));
195
196         if(!FLAC__bitbuffer_write_raw_uint32(bb, FLAC__FRAME_HEADER_SYNC, FLAC__FRAME_HEADER_SYNC_LEN))
197                 return false;
198
199         if(!FLAC__bitbuffer_write_raw_uint32(bb, 0, FLAC__FRAME_HEADER_RESERVED_LEN))
200                 return false;
201
202         FLAC__ASSERT(header->blocksize > 0 && header->blocksize <= FLAC__MAX_BLOCK_SIZE);
203         /* when this assertion holds true, any legal blocksize can be expressed in the frame header */
204         FLAC__ASSERT(FLAC__MAX_BLOCK_SIZE <= 65535u);
205         blocksize_hint = 0;
206         switch(header->blocksize) {
207                 case   192: u = 1; break;
208                 case   576: u = 2; break;
209                 case  1152: u = 3; break;
210                 case  2304: u = 4; break;
211                 case  4608: u = 5; break;
212                 case   256: u = 8; break;
213                 case   512: u = 9; break;
214                 case  1024: u = 10; break;
215                 case  2048: u = 11; break;
216                 case  4096: u = 12; break;
217                 case  8192: u = 13; break;
218                 case 16384: u = 14; break;
219                 case 32768: u = 15; break;
220                 default:
221                         if(header->blocksize <= 0x100)
222                                 blocksize_hint = u = 6;
223                         else if(header->blocksize <= 0x10000)
224                                 blocksize_hint = u = 7;
225                         else {
226                                 FLAC__ASSERT(0);
227                                 return false;
228                         }
229                         break;
230         }
231         if(!FLAC__bitbuffer_write_raw_uint32(bb, u, FLAC__FRAME_HEADER_BLOCK_SIZE_LEN))
232                 return false;
233
234         FLAC__ASSERT(FLAC__format_sample_rate_is_valid(header->sample_rate));
235         sample_rate_hint = 0;
236         switch(header->sample_rate) {
237                 case  8000: u = 4; break;
238                 case 16000: u = 5; break;
239                 case 22050: u = 6; break;
240                 case 24000: u = 7; break;
241                 case 32000: u = 8; break;
242                 case 44100: u = 9; break;
243                 case 48000: u = 10; break;
244                 case 96000: u = 11; break;
245                 default:
246                         if(header->sample_rate <= 255000 && header->sample_rate % 1000 == 0)
247                                 sample_rate_hint = u = 12;
248                         else if(header->sample_rate % 10 == 0)
249                                 sample_rate_hint = u = 14;
250                         else if(header->sample_rate <= 0xffff)
251                                 sample_rate_hint = u = 13;
252                         else if(streamable_subset) {
253                                 FLAC__ASSERT(0);
254                                 return false;
255                         }
256                         else
257                                 u = 0;
258                         break;
259         }
260         if(!FLAC__bitbuffer_write_raw_uint32(bb, u, FLAC__FRAME_HEADER_SAMPLE_RATE_LEN))
261                 return false;
262
263         FLAC__ASSERT(header->channels > 0 && header->channels <= (1u << FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN) && header->channels <= FLAC__MAX_CHANNELS);
264         switch(header->channel_assignment) {
265                 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
266                         u = header->channels - 1;
267                         break;
268                 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
269                         FLAC__ASSERT(header->channels == 2);
270                         u = 8;
271                         break;
272                 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
273                         FLAC__ASSERT(header->channels == 2);
274                         u = 9;
275                         break;
276                 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
277                         FLAC__ASSERT(header->channels == 2);
278                         u = 10;
279                         break;
280                 default:
281                         FLAC__ASSERT(0);
282         }
283         if(!FLAC__bitbuffer_write_raw_uint32(bb, u, FLAC__FRAME_HEADER_CHANNEL_ASSIGNMENT_LEN))
284                 return false;
285
286         FLAC__ASSERT(header->bits_per_sample > 0 && header->bits_per_sample <= (1u << FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN));
287         switch(header->bits_per_sample) {
288                 case 8 : u = 1; break;
289                 case 12: u = 2; break;
290                 case 16: u = 4; break;
291                 case 20: u = 5; break;
292                 case 24: u = 6; break;
293                 default: u = 0; break;
294         }
295         if(!FLAC__bitbuffer_write_raw_uint32(bb, u, FLAC__FRAME_HEADER_BITS_PER_SAMPLE_LEN))
296                 return false;
297
298         if(!FLAC__bitbuffer_write_raw_uint32(bb, 0, FLAC__FRAME_HEADER_ZERO_PAD_LEN))
299                 return false;
300
301         FLAC__ASSERT(header->number_type == FLAC__FRAME_NUMBER_TYPE_FRAME_NUMBER);
302         if(!FLAC__bitbuffer_write_utf8_uint32(bb, header->number.frame_number))
303                 return false;
304
305         if(blocksize_hint)
306                 if(!FLAC__bitbuffer_write_raw_uint32(bb, header->blocksize-1, (blocksize_hint==6)? 8:16))
307                         return false;
308
309         switch(sample_rate_hint) {
310                 case 12:
311                         if(!FLAC__bitbuffer_write_raw_uint32(bb, header->sample_rate / 1000, 8))
312                                 return false;
313                         break;
314                 case 13:
315                         if(!FLAC__bitbuffer_write_raw_uint32(bb, header->sample_rate, 16))
316                                 return false;
317                         break;
318                 case 14:
319                         if(!FLAC__bitbuffer_write_raw_uint32(bb, header->sample_rate / 10, 16))
320                                 return false;
321                         break;
322         }
323
324         /* write the CRC */
325         if(!FLAC__bitbuffer_write_raw_uint32(bb, FLAC__bitbuffer_get_write_crc8(bb), FLAC__FRAME_HEADER_CRC_LEN))
326                 return false;
327
328         return true;
329 }
330
331 FLAC__bool FLAC__subframe_add_constant(const FLAC__Subframe_Constant *subframe, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitBuffer *bb)
332 {
333         FLAC__bool ok;
334
335         ok =
336                 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) &&
337                 (wasted_bits? FLAC__bitbuffer_write_unary_unsigned(bb, wasted_bits-1) : true) &&
338                 FLAC__bitbuffer_write_raw_int32(bb, subframe->value, subframe_bps)
339         ;
340
341         return ok;
342 }
343
344 FLAC__bool FLAC__subframe_add_fixed(const FLAC__Subframe_Fixed *subframe, unsigned residual_samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitBuffer *bb)
345 {
346         unsigned i;
347
348         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))
349                 return false;
350         if(wasted_bits)
351                 if(!FLAC__bitbuffer_write_unary_unsigned(bb, wasted_bits-1))
352                         return false;
353
354         for(i = 0; i < subframe->order; i++)
355                 if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->warmup[i], subframe_bps))
356                         return false;
357
358         if(!add_entropy_coding_method_(bb, &subframe->entropy_coding_method))
359                 return false;
360         switch(subframe->entropy_coding_method.type) {
361                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
362                         if(!add_residual_partitioned_rice_(bb, subframe->residual, residual_samples, subframe->order, subframe->entropy_coding_method.data.partitioned_rice.contents->parameters, subframe->entropy_coding_method.data.partitioned_rice.contents->raw_bits, subframe->entropy_coding_method.data.partitioned_rice.order))
363                                 return false;
364                         break;
365                 default:
366                         FLAC__ASSERT(0);
367         }
368
369         return true;
370 }
371
372 FLAC__bool FLAC__subframe_add_lpc(const FLAC__Subframe_LPC *subframe, unsigned residual_samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitBuffer *bb)
373 {
374         unsigned i;
375
376         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))
377                 return false;
378         if(wasted_bits)
379                 if(!FLAC__bitbuffer_write_unary_unsigned(bb, wasted_bits-1))
380                         return false;
381
382         for(i = 0; i < subframe->order; i++)
383                 if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->warmup[i], subframe_bps))
384                         return false;
385
386         if(!FLAC__bitbuffer_write_raw_uint32(bb, subframe->qlp_coeff_precision-1, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
387                 return false;
388         if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->quantization_level, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN))
389                 return false;
390         for(i = 0; i < subframe->order; i++)
391                 if(!FLAC__bitbuffer_write_raw_int32(bb, subframe->qlp_coeff[i], subframe->qlp_coeff_precision))
392                         return false;
393
394         if(!add_entropy_coding_method_(bb, &subframe->entropy_coding_method))
395                 return false;
396         switch(subframe->entropy_coding_method.type) {
397                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
398                         if(!add_residual_partitioned_rice_(bb, subframe->residual, residual_samples, subframe->order, subframe->entropy_coding_method.data.partitioned_rice.contents->parameters, subframe->entropy_coding_method.data.partitioned_rice.contents->raw_bits, subframe->entropy_coding_method.data.partitioned_rice.order))
399                                 return false;
400                         break;
401                 default:
402                         FLAC__ASSERT(0);
403         }
404
405         return true;
406 }
407
408 FLAC__bool FLAC__subframe_add_verbatim(const FLAC__Subframe_Verbatim *subframe, unsigned samples, unsigned subframe_bps, unsigned wasted_bits, FLAC__BitBuffer *bb)
409 {
410         unsigned i;
411         const FLAC__int32 *signal = subframe->data;
412
413         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))
414                 return false;
415         if(wasted_bits)
416                 if(!FLAC__bitbuffer_write_unary_unsigned(bb, wasted_bits-1))
417                         return false;
418
419         for(i = 0; i < samples; i++)
420                 if(!FLAC__bitbuffer_write_raw_int32(bb, signal[i], subframe_bps))
421                         return false;
422
423         return true;
424 }
425
426 FLAC__bool add_entropy_coding_method_(FLAC__BitBuffer *bb, const FLAC__EntropyCodingMethod *method)
427 {
428         if(!FLAC__bitbuffer_write_raw_uint32(bb, method->type, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
429                 return false;
430         switch(method->type) {
431                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
432                         if(!FLAC__bitbuffer_write_raw_uint32(bb, method->data.partitioned_rice.order, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
433                                 return false;
434                         break;
435                 default:
436                         FLAC__ASSERT(0);
437         }
438         return true;
439 }
440
441 FLAC__bool 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)
442 {
443         if(partition_order == 0) {
444                 unsigned i;
445
446                 if(!FLAC__bitbuffer_write_raw_uint32(bb, rice_parameters[0], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
447                         return false;
448                 if(rice_parameters[0] < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
449                         for(i = 0; i < residual_samples; i++) {
450                                 if(!FLAC__bitbuffer_write_rice_signed(bb, residual[i], rice_parameters[0]))
451                                         return false;
452                         }
453                 }
454                 else {
455                         if(!FLAC__bitbuffer_write_raw_uint32(bb, raw_bits[0], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN))
456                                 return false;
457                         for(i = 0; i < residual_samples; i++) {
458                                 if(!FLAC__bitbuffer_write_raw_int32(bb, residual[i], raw_bits[0]))
459                                         return false;
460                         }
461                 }
462                 return true;
463         }
464         else {
465                 unsigned i, j, k = 0, k_last = 0;
466                 unsigned partition_samples;
467                 const unsigned default_partition_samples = (residual_samples+predictor_order) >> partition_order;
468                 for(i = 0; i < (1u<<partition_order); i++) {
469                         if(!FLAC__bitbuffer_write_raw_uint32(bb, rice_parameters[i], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
470                                 return false;
471                         partition_samples = default_partition_samples;
472                         if(i == 0)
473                                 partition_samples -= predictor_order;
474                         k += partition_samples;
475                         if(rice_parameters[i] < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
476                                 for(j = k_last; j < k; j++) {
477                                         if(!FLAC__bitbuffer_write_rice_signed(bb, residual[j], rice_parameters[i]))
478                                                 return false;
479                                 }
480                         }
481                         else {
482                                 if(!FLAC__bitbuffer_write_raw_uint32(bb, raw_bits[i], FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN))
483                                         return false;
484                                 for(j = k_last; j < k; j++) {
485                                         if(!FLAC__bitbuffer_write_raw_int32(bb, residual[j], raw_bits[i]))
486                                                 return false;
487                                 }
488                         }
489                         k_last = k;
490                 }
491                 return true;
492         }
493 }