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