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