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