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