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