210
275
unsigned max_partition_order
213
#ifdef DONT_ESTIMATE_RICE_BITS
214
static FLAC__bool set_partitioned_rice_(
215
const FLAC__uint32 abs_residual[],
216
const FLAC__int32 residual[],
217
const unsigned residual_samples,
218
const unsigned predictor_order,
219
const unsigned suggested_rice_parameter,
220
const unsigned rice_parameter_search_dist,
221
const unsigned partition_order,
222
FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
226
static FLAC__bool set_partitioned_rice_with_precompute_(
227
const FLAC__int32 residual[],
228
const FLAC__uint64 abs_residual_partition_sums[],
229
const unsigned raw_bits_per_partition[],
230
const unsigned residual_samples,
231
const unsigned predictor_order,
232
const unsigned suggested_rice_parameter,
233
const unsigned rice_parameter_search_dist,
234
const unsigned partition_order,
235
const FLAC__bool search_for_escapes,
236
FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
240
static FLAC__bool set_partitioned_rice_(
241
const FLAC__uint32 abs_residual[],
242
const unsigned residual_samples,
243
const unsigned predictor_order,
244
const unsigned suggested_rice_parameter,
245
const unsigned rice_parameter_search_dist,
246
const unsigned partition_order,
247
FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
251
static FLAC__bool set_partitioned_rice_with_precompute_(
252
const FLAC__uint32 abs_residual[],
253
const FLAC__uint64 abs_residual_partition_sums[],
254
const unsigned raw_bits_per_partition[],
255
const unsigned residual_samples,
256
const unsigned predictor_order,
257
const unsigned suggested_rice_parameter,
258
const unsigned rice_parameter_search_dist,
259
const unsigned partition_order,
260
const FLAC__bool search_for_escapes,
261
FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
278
static FLAC__bool set_partitioned_rice_(
279
#ifdef EXACT_RICE_BITS_CALCULATION
280
const FLAC__int32 residual[],
282
const FLAC__uint64 abs_residual_partition_sums[],
283
const unsigned raw_bits_per_partition[],
284
const unsigned residual_samples,
285
const unsigned predictor_order,
286
const unsigned suggested_rice_parameter,
287
const unsigned rice_parameter_limit,
288
const unsigned rice_parameter_search_dist,
289
const unsigned partition_order,
290
const FLAC__bool search_for_escapes,
291
FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
266
295
static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
422
451
FLAC_API const char * const FLAC__StreamEncoderStateString[] = {
423
452
"FLAC__STREAM_ENCODER_OK",
453
"FLAC__STREAM_ENCODER_UNINITIALIZED",
454
"FLAC__STREAM_ENCODER_OGG_ERROR",
424
455
"FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
425
456
"FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA",
426
"FLAC__STREAM_ENCODER_INVALID_CALLBACK",
427
"FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS",
428
"FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE",
429
"FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE",
430
"FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE",
431
"FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER",
432
"FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION",
433
"FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH",
434
"FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
435
"FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE",
436
"FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
437
"FLAC__STREAM_ENCODER_NOT_STREAMABLE",
457
"FLAC__STREAM_ENCODER_CLIENT_ERROR",
458
"FLAC__STREAM_ENCODER_IO_ERROR",
438
459
"FLAC__STREAM_ENCODER_FRAMING_ERROR",
439
"FLAC__STREAM_ENCODER_INVALID_METADATA",
440
"FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING",
441
"FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING",
442
"FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR",
443
"FLAC__STREAM_ENCODER_ALREADY_INITIALIZED",
444
"FLAC__STREAM_ENCODER_UNINITIALIZED"
460
"FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR"
463
FLAC_API const char * const FLAC__StreamEncoderInitStatusString[] = {
464
"FLAC__STREAM_ENCODER_INIT_STATUS_OK",
465
"FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR",
466
"FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER",
467
"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS",
468
"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS",
469
"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE",
470
"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE",
471
"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE",
472
"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER",
473
"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION",
474
"FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
475
"FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE",
476
"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA",
477
"FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED"
480
FLAC_API const char * const FLAC__treamEncoderReadStatusString[] = {
481
"FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE",
482
"FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM",
483
"FLAC__STREAM_ENCODER_READ_STATUS_ABORT",
484
"FLAC__STREAM_ENCODER_READ_STATUS_UNSUPPORTED"
447
487
FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = {
564
631
***********************************************************************/
566
FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder)
633
static FLAC__StreamEncoderInitStatus init_stream_internal_(
634
FLAC__StreamEncoder *encoder,
635
FLAC__StreamEncoderReadCallback read_callback,
636
FLAC__StreamEncoderWriteCallback write_callback,
637
FLAC__StreamEncoderSeekCallback seek_callback,
638
FLAC__StreamEncoderTellCallback tell_callback,
639
FLAC__StreamEncoderMetadataCallback metadata_callback,
569
FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment;
645
FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment, metadata_picture_has_type1, metadata_picture_has_type2;
571
647
FLAC__ASSERT(0 != encoder);
573
649
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
574
return encoder->protected_->state = FLAC__STREAM_ENCODER_ALREADY_INITIALIZED;
576
encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
578
if(0 == encoder->private_->write_callback || 0 == encoder->private_->metadata_callback)
579
return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_CALLBACK;
650
return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
654
return FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER;
657
if(0 == write_callback || (seek_callback && 0 == tell_callback))
658
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS;
581
660
if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
582
return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS;
584
if(encoder->protected_->do_mid_side_stereo && encoder->protected_->channels != 2)
585
return encoder->protected_->state = FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH;
587
if(encoder->protected_->loose_mid_side_stereo && !encoder->protected_->do_mid_side_stereo)
588
return encoder->protected_->state = FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE;
661
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS;
663
if(encoder->protected_->channels != 2) {
664
encoder->protected_->do_mid_side_stereo = false;
665
encoder->protected_->loose_mid_side_stereo = false;
667
else if(!encoder->protected_->do_mid_side_stereo)
668
encoder->protected_->loose_mid_side_stereo = false;
590
670
if(encoder->protected_->bits_per_sample >= 32)
591
encoder->protected_->do_mid_side_stereo = false; /* since we do 32-bit math, the side channel would have 33 bps and overflow */
671
encoder->protected_->do_mid_side_stereo = false; /* since we currenty do 32-bit math, the side channel would have 33 bps and overflow */
593
673
if(encoder->protected_->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected_->bits_per_sample > FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE)
594
return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE;
674
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE;
596
676
if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
597
return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE;
677
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE;
679
if(encoder->protected_->blocksize == 0) {
680
if(encoder->protected_->max_lpc_order == 0)
681
encoder->protected_->blocksize = 1152;
683
encoder->protected_->blocksize = 4096;
599
686
if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
600
return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE;
687
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE;
602
689
if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
603
return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER;
690
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER;
605
692
if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
606
return encoder->protected_->state = FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
693
return FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
608
695
if(encoder->protected_->qlp_coeff_precision == 0) {
609
696
if(encoder->protected_->bits_per_sample < 16) {
684
771
if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
685
772
encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
775
/* reorder metadata if necessary to ensure that any VORBIS_COMMENT is the first, according to the mapping spec */
776
if(is_ogg && 0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 1) {
778
for(i = 1; i < encoder->protected_->num_metadata_blocks; i++) {
779
if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
780
FLAC__StreamMetadata *vc = encoder->protected_->metadata[i];
782
encoder->protected_->metadata[i] = encoder->protected_->metadata[i-1];
783
encoder->protected_->metadata[0] = vc;
789
/* keep track of any SEEKTABLE block */
790
if(0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0) {
792
for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
793
if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
794
encoder->private_->seek_table = &encoder->protected_->metadata[i]->data.seek_table;
795
break; /* take only the first one */
687
800
/* validate metadata */
688
801
if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
689
return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
802
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
690
803
metadata_has_seektable = false;
691
804
metadata_has_vorbis_comment = false;
805
metadata_picture_has_type1 = false;
806
metadata_picture_has_type2 = false;
692
807
for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
693
if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_STREAMINFO)
694
return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
695
else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
808
const FLAC__StreamMetadata *m = encoder->protected_->metadata[i];
809
if(m->type == FLAC__METADATA_TYPE_STREAMINFO)
810
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
811
else if(m->type == FLAC__METADATA_TYPE_SEEKTABLE) {
696
812
if(metadata_has_seektable) /* only one is allowed */
697
return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
813
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
698
814
metadata_has_seektable = true;
699
if(!FLAC__format_seektable_is_legal(&encoder->protected_->metadata[i]->data.seek_table))
700
return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
815
if(!FLAC__format_seektable_is_legal(&m->data.seek_table))
816
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
702
else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
818
else if(m->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
703
819
if(metadata_has_vorbis_comment) /* only one is allowed */
704
return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
820
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
705
821
metadata_has_vorbis_comment = true;
707
else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_CUESHEET) {
708
if(!FLAC__format_cuesheet_is_legal(&encoder->protected_->metadata[i]->data.cue_sheet, encoder->protected_->metadata[i]->data.cue_sheet.is_cd, /*violation=*/0))
709
return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
823
else if(m->type == FLAC__METADATA_TYPE_CUESHEET) {
824
if(!FLAC__format_cuesheet_is_legal(&m->data.cue_sheet, m->data.cue_sheet.is_cd, /*violation=*/0))
825
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
827
else if(m->type == FLAC__METADATA_TYPE_PICTURE) {
828
if(!FLAC__format_picture_is_legal(&m->data.picture, /*violation=*/0))
829
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
830
if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD) {
831
if(metadata_picture_has_type1) /* there should only be 1 per stream */
832
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
833
metadata_picture_has_type1 = true;
834
/* standard icon must be 32x32 pixel PNG */
836
m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD &&
838
(strcmp(m->data.picture.mime_type, "image/png") && strcmp(m->data.picture.mime_type, "-->")) ||
839
m->data.picture.width != 32 ||
840
m->data.picture.height != 32
843
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
845
else if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON) {
846
if(metadata_picture_has_type2) /* there should only be 1 per stream */
847
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
848
metadata_picture_has_type2 = true;
881
1052
if(encoder->protected_->verify)
882
1053
encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
883
encoder->private_->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
884
encoder->private_->metadata.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
885
encoder->private_->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
886
encoder->private_->metadata.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
887
encoder->private_->metadata.data.stream_info.max_blocksize = encoder->protected_->blocksize;
888
encoder->private_->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
889
encoder->private_->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
890
encoder->private_->metadata.data.stream_info.sample_rate = encoder->protected_->sample_rate;
891
encoder->private_->metadata.data.stream_info.channels = encoder->protected_->channels;
892
encoder->private_->metadata.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
893
encoder->private_->metadata.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
894
memset(encoder->private_->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
895
FLAC__MD5Init(&encoder->private_->md5context);
896
if(!FLAC__bitbuffer_clear(encoder->private_->frame))
897
return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
898
if(!FLAC__add_metadata_block(&encoder->private_->metadata, encoder->private_->frame))
899
return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
900
if(!write_bitbuffer_(encoder, 0)) {
1054
encoder->private_->streaminfo.type = FLAC__METADATA_TYPE_STREAMINFO;
1055
encoder->private_->streaminfo.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
1056
encoder->private_->streaminfo.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
1057
encoder->private_->streaminfo.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
1058
encoder->private_->streaminfo.data.stream_info.max_blocksize = encoder->protected_->blocksize;
1059
encoder->private_->streaminfo.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
1060
encoder->private_->streaminfo.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
1061
encoder->private_->streaminfo.data.stream_info.sample_rate = encoder->protected_->sample_rate;
1062
encoder->private_->streaminfo.data.stream_info.channels = encoder->protected_->channels;
1063
encoder->private_->streaminfo.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
1064
encoder->private_->streaminfo.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
1065
memset(encoder->private_->streaminfo.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
1066
if(encoder->protected_->do_md5)
1067
FLAC__MD5Init(&encoder->private_->md5context);
1068
if(!FLAC__add_metadata_block(&encoder->private_->streaminfo, encoder->private_->frame)) {
1069
encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1070
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1072
if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
901
1073
/* the above function sets the state for us in case of an error */
902
return encoder->protected_->state;
1074
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
906
1078
* Now that the STREAMINFO block is written, we can init this to an
907
1079
* absurdly-high value...
909
encoder->private_->metadata.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
1081
encoder->private_->streaminfo.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
910
1082
/* ... and clear this to 0 */
911
encoder->private_->metadata.data.stream_info.total_samples = 0;
1083
encoder->private_->streaminfo.data.stream_info.total_samples = 0;
914
1086
* Check to see if the supplied metadata contains a VORBIS_COMMENT;
915
1087
* if not, we will write an empty one (FLAC__add_metadata_block()
916
1088
* automatically supplies the vendor string).
918
* WATCHOUT: libOggFLAC depends on us to write this block after the
919
* STREAMINFO since that's what the mapping requires. (In the case
920
* that metadata_has_vorbis_comment is true it will have already
921
* insured that the metadata list is properly ordered.)
1090
* WATCHOUT: the Ogg FLAC mapping requires us to write this block after
1091
* the STREAMINFO. (In the case that metadata_has_vorbis_comment is
1092
* true it will have already insured that the metadata list is properly
923
1095
if(!metadata_has_vorbis_comment) {
924
1096
FLAC__StreamMetadata vorbis_comment;
945
1117
for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
946
1118
encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
947
if(!FLAC__bitbuffer_clear(encoder->private_->frame))
948
return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
949
if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame))
950
return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
951
if(!write_bitbuffer_(encoder, 0)) {
1119
if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame)) {
1120
encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1121
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1123
if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
952
1124
/* the above function sets the state for us in case of an error */
953
return encoder->protected_->state;
1125
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1129
/* now that all the metadata is written, we save the stream offset */
1130
if(encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &encoder->protected_->audio_offset, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) { /* FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED just means we didn't get the offset; no error */
1131
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
1132
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
957
1135
if(encoder->protected_->verify)
958
1136
encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
960
return encoder->protected_->state;
963
FLAC_API void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
965
FLAC__ASSERT(0 != encoder);
1138
return FLAC__STREAM_ENCODER_INIT_STATUS_OK;
1141
FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_stream(
1142
FLAC__StreamEncoder *encoder,
1143
FLAC__StreamEncoderWriteCallback write_callback,
1144
FLAC__StreamEncoderSeekCallback seek_callback,
1145
FLAC__StreamEncoderTellCallback tell_callback,
1146
FLAC__StreamEncoderMetadataCallback metadata_callback,
1150
return init_stream_internal_(
1152
/*read_callback=*/0,
1162
FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_stream(
1163
FLAC__StreamEncoder *encoder,
1164
FLAC__StreamEncoderReadCallback read_callback,
1165
FLAC__StreamEncoderWriteCallback write_callback,
1166
FLAC__StreamEncoderSeekCallback seek_callback,
1167
FLAC__StreamEncoderTellCallback tell_callback,
1168
FLAC__StreamEncoderMetadataCallback metadata_callback,
1172
return init_stream_internal_(
1184
static FLAC__StreamEncoderInitStatus init_FILE_internal_(
1185
FLAC__StreamEncoder *encoder,
1187
FLAC__StreamEncoderProgressCallback progress_callback,
1192
FLAC__StreamEncoderInitStatus init_status;
1194
FLAC__ASSERT(0 != encoder);
1195
FLAC__ASSERT(0 != file);
1197
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1198
return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1200
/* double protection */
1202
encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1203
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1207
* To make sure that our file does not go unclosed after an error, we
1208
* must assign the FILE pointer before any further error can occur in
1212
file = get_binary_stdout_(); /* just to be safe */
1214
encoder->private_->file = file;
1216
encoder->private_->progress_callback = progress_callback;
1217
encoder->private_->bytes_written = 0;
1218
encoder->private_->samples_written = 0;
1219
encoder->private_->frames_written = 0;
1221
init_status = init_stream_internal_(
1223
encoder->private_->file == stdout? 0 : is_ogg? file_read_callback_ : 0,
1224
file_write_callback_,
1225
encoder->private_->file == stdout? 0 : file_seek_callback_,
1226
encoder->private_->file == stdout? 0 : file_tell_callback_,
1227
/*metadata_callback=*/0,
1231
if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
1232
/* the above function sets the state for us in case of an error */
1237
unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
1239
FLAC__ASSERT(blocksize != 0);
1240
encoder->private_->total_frames_estimate = (unsigned)((FLAC__stream_encoder_get_total_samples_estimate(encoder) + blocksize - 1) / blocksize);
1246
FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_FILE(
1247
FLAC__StreamEncoder *encoder,
1249
FLAC__StreamEncoderProgressCallback progress_callback,
1253
return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/false);
1256
FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_FILE(
1257
FLAC__StreamEncoder *encoder,
1259
FLAC__StreamEncoderProgressCallback progress_callback,
1263
return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/true);
1266
static FLAC__StreamEncoderInitStatus init_file_internal_(
1267
FLAC__StreamEncoder *encoder,
1268
const char *filename,
1269
FLAC__StreamEncoderProgressCallback progress_callback,
1276
FLAC__ASSERT(0 != encoder);
1279
* To make sure that our file does not go unclosed after an error, we
1280
* have to do the same entrance checks here that are later performed
1281
* in FLAC__stream_encoder_init_FILE() before the FILE* is assigned.
1283
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1284
return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1286
file = filename? fopen(filename, "w+b") : stdout;
1289
encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1290
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1293
return init_FILE_internal_(encoder, file, progress_callback, client_data, is_ogg);
1296
FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_file(
1297
FLAC__StreamEncoder *encoder,
1298
const char *filename,
1299
FLAC__StreamEncoderProgressCallback progress_callback,
1303
return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/false);
1306
FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_file(
1307
FLAC__StreamEncoder *encoder,
1308
const char *filename,
1309
FLAC__StreamEncoderProgressCallback progress_callback,
1313
return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/true);
1316
FLAC_API FLAC__bool FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
1318
FLAC__bool error = false;
1320
FLAC__ASSERT(0 != encoder);
1321
FLAC__ASSERT(0 != encoder->private_);
1322
FLAC__ASSERT(0 != encoder->protected_);
967
1324
if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
970
1327
if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
971
1328
if(encoder->private_->current_sample_number != 0) {
1329
const FLAC__bool is_fractional_block = encoder->protected_->blocksize != encoder->private_->current_sample_number;
972
1330
encoder->protected_->blocksize = encoder->private_->current_sample_number;
973
process_frame_(encoder, true); /* true => is last frame */
977
FLAC__MD5Final(encoder->private_->metadata.data.stream_info.md5sum, &encoder->private_->md5context);
979
if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
980
encoder->private_->metadata_callback(encoder, &encoder->private_->metadata, encoder->private_->client_data);
983
if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
984
FLAC__stream_decoder_finish(encoder->private_->verify.decoder);
1331
if(!process_frame_(encoder, is_fractional_block, /*is_last_block=*/true))
1336
if(encoder->protected_->do_md5)
1337
FLAC__MD5Final(encoder->private_->streaminfo.data.stream_info.md5sum, &encoder->private_->md5context);
1339
if(!encoder->private_->is_being_deleted) {
1340
if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK) {
1341
if(encoder->private_->seek_callback) {
1343
if(encoder->private_->is_ogg)
1344
update_ogg_metadata_(encoder);
1347
update_metadata_(encoder);
1349
/* check if an error occurred while updating metadata */
1350
if(encoder->protected_->state != FLAC__STREAM_ENCODER_OK)
1353
if(encoder->private_->metadata_callback)
1354
encoder->private_->metadata_callback(encoder, &encoder->private_->streaminfo, encoder->private_->client_data);
1357
if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder && !FLAC__stream_decoder_finish(encoder->private_->verify.decoder)) {
1359
encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
1364
if(0 != encoder->private_->file) {
1365
if(encoder->private_->file != stdout)
1366
fclose(encoder->private_->file);
1367
encoder->private_->file = 0;
1371
if(encoder->private_->is_ogg)
1372
FLAC__ogg_encoder_aspect_finish(&encoder->protected_->ogg_encoder_aspect);
987
1376
set_defaults_(encoder);
989
encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
1379
encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
1384
FLAC_API FLAC__bool FLAC__stream_encoder_set_ogg_serial_number(FLAC__StreamEncoder *encoder, long value)
1386
FLAC__ASSERT(0 != encoder);
1387
FLAC__ASSERT(0 != encoder->private_);
1388
FLAC__ASSERT(0 != encoder->protected_);
1389
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1392
/* can't check encoder->private_->is_ogg since that's not set until init time */
1393
FLAC__ogg_encoder_aspect_set_serial_number(&encoder->protected_->ogg_encoder_aspect, value);
992
1401
FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
994
1403
FLAC__ASSERT(0 != encoder);
1404
FLAC__ASSERT(0 != encoder->private_);
1405
FLAC__ASSERT(0 != encoder->protected_);
995
1406
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
997
1408
#ifndef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
1048
1458
FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
1050
1460
FLAC__ASSERT(0 != encoder);
1461
FLAC__ASSERT(0 != encoder->private_);
1462
FLAC__ASSERT(0 != encoder->protected_);
1051
1463
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1053
1465
encoder->protected_->sample_rate = value;
1469
FLAC_API FLAC__bool FLAC__stream_encoder_set_compression_level(FLAC__StreamEncoder *encoder, unsigned value)
1471
FLAC__bool ok = true;
1472
FLAC__ASSERT(0 != encoder);
1473
FLAC__ASSERT(0 != encoder->private_);
1474
FLAC__ASSERT(0 != encoder->protected_);
1475
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1477
if(value >= sizeof(compression_levels_)/sizeof(compression_levels_[0]))
1478
value = sizeof(compression_levels_)/sizeof(compression_levels_[0]) - 1;
1479
ok &= FLAC__stream_encoder_set_do_mid_side_stereo (encoder, compression_levels_[value].do_mid_side_stereo);
1480
ok &= FLAC__stream_encoder_set_loose_mid_side_stereo (encoder, compression_levels_[value].loose_mid_side_stereo);
1481
#ifndef FLAC__INTEGER_ONLY_LIBRARY
1484
ok &= FLAC__stream_encoder_set_apodization (encoder, compression_levels_[value].apodization);
1485
/* but it's too hard to specify the string in a locale-specific way */
1487
encoder->protected_->num_apodizations = 1;
1488
encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
1489
encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
1492
ok &= FLAC__stream_encoder_set_max_lpc_order (encoder, compression_levels_[value].max_lpc_order);
1493
ok &= FLAC__stream_encoder_set_qlp_coeff_precision (encoder, compression_levels_[value].qlp_coeff_precision);
1494
ok &= FLAC__stream_encoder_set_do_qlp_coeff_prec_search (encoder, compression_levels_[value].do_qlp_coeff_prec_search);
1495
ok &= FLAC__stream_encoder_set_do_escape_coding (encoder, compression_levels_[value].do_escape_coding);
1496
ok &= FLAC__stream_encoder_set_do_exhaustive_model_search (encoder, compression_levels_[value].do_exhaustive_model_search);
1497
ok &= FLAC__stream_encoder_set_min_residual_partition_order(encoder, compression_levels_[value].min_residual_partition_order);
1498
ok &= FLAC__stream_encoder_set_max_residual_partition_order(encoder, compression_levels_[value].max_residual_partition_order);
1499
ok &= FLAC__stream_encoder_set_rice_parameter_search_dist (encoder, compression_levels_[value].rice_parameter_search_dist);
1057
1503
FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
1059
1505
FLAC__ASSERT(0 != encoder);
1506
FLAC__ASSERT(0 != encoder->private_);
1507
FLAC__ASSERT(0 != encoder->protected_);
1060
1508
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1062
1510
encoder->protected_->blocksize = value;
1514
FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
1516
FLAC__ASSERT(0 != encoder);
1517
FLAC__ASSERT(0 != encoder->private_);
1518
FLAC__ASSERT(0 != encoder->protected_);
1519
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1521
encoder->protected_->do_mid_side_stereo = value;
1525
FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
1527
FLAC__ASSERT(0 != encoder);
1528
FLAC__ASSERT(0 != encoder->private_);
1529
FLAC__ASSERT(0 != encoder->protected_);
1530
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1532
encoder->protected_->loose_mid_side_stereo = value;
1536
/*@@@@add to tests*/
1537
FLAC_API FLAC__bool FLAC__stream_encoder_set_apodization(FLAC__StreamEncoder *encoder, const char *specification)
1539
FLAC__ASSERT(0 != encoder);
1540
FLAC__ASSERT(0 != encoder->private_);
1541
FLAC__ASSERT(0 != encoder->protected_);
1542
FLAC__ASSERT(0 != specification);
1543
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1545
#ifdef FLAC__INTEGER_ONLY_LIBRARY
1546
(void)specification; /* silently ignore since we haven't integerized; will always use a rectangular window */
1548
encoder->protected_->num_apodizations = 0;
1550
const char *s = strchr(specification, ';');
1551
const size_t n = s? (size_t)(s - specification) : strlen(specification);
1552
if (n==8 && 0 == strncmp("bartlett" , specification, n))
1553
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT;
1554
else if(n==13 && 0 == strncmp("bartlett_hann", specification, n))
1555
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT_HANN;
1556
else if(n==8 && 0 == strncmp("blackman" , specification, n))
1557
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN;
1558
else if(n==26 && 0 == strncmp("blackman_harris_4term_92db", specification, n))
1559
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE;
1560
else if(n==6 && 0 == strncmp("connes" , specification, n))
1561
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_CONNES;
1562
else if(n==7 && 0 == strncmp("flattop" , specification, n))
1563
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_FLATTOP;
1564
else if(n>7 && 0 == strncmp("gauss(" , specification, 6)) {
1565
FLAC__real stddev = (FLAC__real)strtod(specification+6, 0);
1566
if (stddev > 0.0 && stddev <= 0.5) {
1567
encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.gauss.stddev = stddev;
1568
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_GAUSS;
1571
else if(n==7 && 0 == strncmp("hamming" , specification, n))
1572
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HAMMING;
1573
else if(n==4 && 0 == strncmp("hann" , specification, n))
1574
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HANN;
1575
else if(n==13 && 0 == strncmp("kaiser_bessel", specification, n))
1576
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_KAISER_BESSEL;
1577
else if(n==7 && 0 == strncmp("nuttall" , specification, n))
1578
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_NUTTALL;
1579
else if(n==9 && 0 == strncmp("rectangle" , specification, n))
1580
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_RECTANGLE;
1581
else if(n==8 && 0 == strncmp("triangle" , specification, n))
1582
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TRIANGLE;
1583
else if(n>7 && 0 == strncmp("tukey(" , specification, 6)) {
1584
FLAC__real p = (FLAC__real)strtod(specification+6, 0);
1585
if (p >= 0.0 && p <= 1.0) {
1586
encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.tukey.p = p;
1587
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TUKEY;
1590
else if(n==5 && 0 == strncmp("welch" , specification, n))
1591
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_WELCH;
1592
if (encoder->protected_->num_apodizations == 32)
1595
specification = s+1;
1599
if(encoder->protected_->num_apodizations == 0) {
1600
encoder->protected_->num_apodizations = 1;
1601
encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
1602
encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
1066
1608
FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
1068
1610
FLAC__ASSERT(0 != encoder);
1611
FLAC__ASSERT(0 != encoder->private_);
1612
FLAC__ASSERT(0 != encoder->protected_);
1069
1613
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1071
1615
encoder->protected_->max_lpc_order = value;
1267
1835
FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
1269
1837
FLAC__ASSERT(0 != encoder);
1838
FLAC__ASSERT(0 != encoder->private_);
1839
FLAC__ASSERT(0 != encoder->protected_);
1270
1840
return encoder->protected_->verify;
1273
1843
FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
1275
1845
FLAC__ASSERT(0 != encoder);
1846
FLAC__ASSERT(0 != encoder->private_);
1847
FLAC__ASSERT(0 != encoder->protected_);
1276
1848
return encoder->protected_->streamable_subset;
1279
FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1281
FLAC__ASSERT(0 != encoder);
1282
return encoder->protected_->do_mid_side_stereo;
1285
FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1287
FLAC__ASSERT(0 != encoder);
1288
return encoder->protected_->loose_mid_side_stereo;
1851
FLAC_API FLAC__bool FLAC__stream_encoder_get_do_md5(const FLAC__StreamEncoder *encoder)
1853
FLAC__ASSERT(0 != encoder);
1854
FLAC__ASSERT(0 != encoder->private_);
1855
FLAC__ASSERT(0 != encoder->protected_);
1856
return encoder->protected_->do_md5;
1291
1859
FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
1293
1861
FLAC__ASSERT(0 != encoder);
1862
FLAC__ASSERT(0 != encoder->private_);
1863
FLAC__ASSERT(0 != encoder->protected_);
1294
1864
return encoder->protected_->channels;
1297
1867
FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
1299
1869
FLAC__ASSERT(0 != encoder);
1870
FLAC__ASSERT(0 != encoder->private_);
1871
FLAC__ASSERT(0 != encoder->protected_);
1300
1872
return encoder->protected_->bits_per_sample;
1303
1875
FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
1305
1877
FLAC__ASSERT(0 != encoder);
1878
FLAC__ASSERT(0 != encoder->private_);
1879
FLAC__ASSERT(0 != encoder->protected_);
1306
1880
return encoder->protected_->sample_rate;
1309
1883
FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
1311
1885
FLAC__ASSERT(0 != encoder);
1886
FLAC__ASSERT(0 != encoder->private_);
1887
FLAC__ASSERT(0 != encoder->protected_);
1312
1888
return encoder->protected_->blocksize;
1891
FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1893
FLAC__ASSERT(0 != encoder);
1894
FLAC__ASSERT(0 != encoder->private_);
1895
FLAC__ASSERT(0 != encoder->protected_);
1896
return encoder->protected_->do_mid_side_stereo;
1899
FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1901
FLAC__ASSERT(0 != encoder);
1902
FLAC__ASSERT(0 != encoder->private_);
1903
FLAC__ASSERT(0 != encoder->protected_);
1904
return encoder->protected_->loose_mid_side_stereo;
1315
1907
FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
1317
1909
FLAC__ASSERT(0 != encoder);
1910
FLAC__ASSERT(0 != encoder->private_);
1911
FLAC__ASSERT(0 != encoder->protected_);
1318
1912
return encoder->protected_->max_lpc_order;
1321
1915
FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
1323
1917
FLAC__ASSERT(0 != encoder);
1918
FLAC__ASSERT(0 != encoder->private_);
1919
FLAC__ASSERT(0 != encoder->protected_);
1324
1920
return encoder->protected_->qlp_coeff_precision;
1327
1923
FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
1329
1925
FLAC__ASSERT(0 != encoder);
1926
FLAC__ASSERT(0 != encoder->private_);
1927
FLAC__ASSERT(0 != encoder->protected_);
1330
1928
return encoder->protected_->do_qlp_coeff_prec_search;
1333
1931
FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
1335
1933
FLAC__ASSERT(0 != encoder);
1934
FLAC__ASSERT(0 != encoder->private_);
1935
FLAC__ASSERT(0 != encoder->protected_);
1336
1936
return encoder->protected_->do_escape_coding;
1339
1939
FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
1341
1941
FLAC__ASSERT(0 != encoder);
1942
FLAC__ASSERT(0 != encoder->private_);
1943
FLAC__ASSERT(0 != encoder->protected_);
1342
1944
return encoder->protected_->do_exhaustive_model_search;
1345
1947
FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
1347
1949
FLAC__ASSERT(0 != encoder);
1950
FLAC__ASSERT(0 != encoder->private_);
1951
FLAC__ASSERT(0 != encoder->protected_);
1348
1952
return encoder->protected_->min_residual_partition_order;
1351
1955
FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
1353
1957
FLAC__ASSERT(0 != encoder);
1958
FLAC__ASSERT(0 != encoder->private_);
1959
FLAC__ASSERT(0 != encoder->protected_);
1354
1960
return encoder->protected_->max_residual_partition_order;
1357
1963
FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
1359
1965
FLAC__ASSERT(0 != encoder);
1966
FLAC__ASSERT(0 != encoder->private_);
1967
FLAC__ASSERT(0 != encoder->protected_);
1360
1968
return encoder->protected_->rice_parameter_search_dist;
1363
1971
FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
1365
1973
FLAC__ASSERT(0 != encoder);
1974
FLAC__ASSERT(0 != encoder->private_);
1975
FLAC__ASSERT(0 != encoder->protected_);
1366
1976
return encoder->protected_->total_samples_estimate;
1369
1979
FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
1371
unsigned i, j, channel;
1372
FLAC__int32 x, mid, side;
1981
unsigned i, j = 0, channel;
1373
1982
const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
1375
1984
FLAC__ASSERT(0 != encoder);
1985
FLAC__ASSERT(0 != encoder->private_);
1986
FLAC__ASSERT(0 != encoder->protected_);
1376
1987
FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1380
* we have several flavors of the same basic loop, optimized for
1381
* different conditions:
1383
if(encoder->protected_->max_lpc_order > 0) {
1384
if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1386
* stereo coding: unroll channel loop
1387
* with LPC: calculate floating point version of signal
1390
if(encoder->protected_->verify)
1391
append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1393
for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1394
x = mid = side = buffer[0][j];
1395
encoder->private_->integer_signal[0][i] = x;
1396
#ifndef FLAC__INTEGER_ONLY_LIBRARY
1397
encoder->private_->real_signal[0][i] = (FLAC__real)x;
1400
encoder->private_->integer_signal[1][i] = x;
1401
#ifndef FLAC__INTEGER_ONLY_LIBRARY
1402
encoder->private_->real_signal[1][i] = (FLAC__real)x;
1406
mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
1407
encoder->private_->integer_signal_mid_side[1][i] = side;
1408
encoder->private_->integer_signal_mid_side[0][i] = mid;
1409
#ifndef FLAC__INTEGER_ONLY_LIBRARY
1410
encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1411
encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1413
encoder->private_->current_sample_number++;
1415
if(i == blocksize) {
1416
if(!process_frame_(encoder, false)) /* false => not last frame */
1419
} while(j < samples);
1423
* independent channel coding: buffer each channel in inner loop
1424
* with LPC: calculate floating point version of signal
1427
if(encoder->protected_->verify)
1428
append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1430
for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1431
for(channel = 0; channel < channels; channel++) {
1432
x = buffer[channel][j];
1433
encoder->private_->integer_signal[channel][i] = x;
1434
#ifndef FLAC__INTEGER_ONLY_LIBRARY
1435
encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1438
encoder->private_->current_sample_number++;
1440
if(i == blocksize) {
1441
if(!process_frame_(encoder, false)) /* false => not last frame */
1444
} while(j < samples);
1448
if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1450
* stereo coding: unroll channel loop
1451
* without LPC: no need to calculate floating point version of signal
1454
if(encoder->protected_->verify)
1455
append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1457
for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1458
encoder->private_->integer_signal[0][i] = mid = side = buffer[0][j];
1460
encoder->private_->integer_signal[1][i] = x;
1463
mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
1464
encoder->private_->integer_signal_mid_side[1][i] = side;
1465
encoder->private_->integer_signal_mid_side[0][i] = mid;
1466
encoder->private_->current_sample_number++;
1468
if(i == blocksize) {
1469
if(!process_frame_(encoder, false)) /* false => not last frame */
1472
} while(j < samples);
1476
* independent channel coding: buffer each channel in inner loop
1477
* without LPC: no need to calculate floating point version of signal
1480
if(encoder->protected_->verify)
1481
append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1483
for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1484
for(channel = 0; channel < channels; channel++)
1485
encoder->private_->integer_signal[channel][i] = buffer[channel][j];
1486
encoder->private_->current_sample_number++;
1488
if(i == blocksize) {
1489
if(!process_frame_(encoder, false)) /* false => not last frame */
1492
} while(j < samples);
1990
const unsigned n = min(blocksize+OVERREAD_-encoder->private_->current_sample_number, samples-j);
1992
if(encoder->protected_->verify)
1993
append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, n);
1995
for(channel = 0; channel < channels; channel++)
1996
memcpy(&encoder->private_->integer_signal[channel][encoder->private_->current_sample_number], &buffer[channel][j], sizeof(buffer[channel][0]) * n);
1998
if(encoder->protected_->do_mid_side_stereo) {
1999
FLAC__ASSERT(channels == 2);
2000
/* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2001
for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
2002
encoder->private_->integer_signal_mid_side[1][i] = buffer[0][j] - buffer[1][j];
2003
encoder->private_->integer_signal_mid_side[0][i] = (buffer[0][j] + buffer[1][j]) >> 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
2009
encoder->private_->current_sample_number += n;
2011
/* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2012
if(encoder->private_->current_sample_number > blocksize) {
2013
FLAC__ASSERT(encoder->private_->current_sample_number == blocksize+OVERREAD_);
2014
FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2015
if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2017
/* move unprocessed overread samples to beginnings of arrays */
2018
for(channel = 0; channel < channels; channel++)
2019
encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][blocksize];
2020
if(encoder->protected_->do_mid_side_stereo) {
2021
encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][blocksize];
2022
encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][blocksize];
2024
encoder->private_->current_sample_number = 1;
2026
} while(j < samples);
1510
2044
* we have several flavors of the same basic loop, optimized for
1511
2045
* different conditions:
1513
if(encoder->protected_->max_lpc_order > 0) {
1514
if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1516
* stereo coding: unroll channel loop
1517
* with LPC: calculate floating point version of signal
1520
if(encoder->protected_->verify)
1521
append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1523
for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1524
x = mid = side = buffer[k++];
1525
encoder->private_->integer_signal[0][i] = x;
1526
#ifndef FLAC__INTEGER_ONLY_LIBRARY
1527
encoder->private_->real_signal[0][i] = (FLAC__real)x;
1530
encoder->private_->integer_signal[1][i] = x;
1531
#ifndef FLAC__INTEGER_ONLY_LIBRARY
1532
encoder->private_->real_signal[1][i] = (FLAC__real)x;
1536
mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
1537
encoder->private_->integer_signal_mid_side[1][i] = side;
1538
encoder->private_->integer_signal_mid_side[0][i] = mid;
1539
#ifndef FLAC__INTEGER_ONLY_LIBRARY
1540
encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
1541
encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
1543
encoder->private_->current_sample_number++;
1545
if(i == blocksize) {
1546
if(!process_frame_(encoder, false)) /* false => not last frame */
1549
} while(j < samples);
1553
* independent channel coding: buffer each channel in inner loop
1554
* with LPC: calculate floating point version of signal
1557
if(encoder->protected_->verify)
1558
append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1560
for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1561
for(channel = 0; channel < channels; channel++) {
1563
encoder->private_->integer_signal[channel][i] = x;
1564
#ifndef FLAC__INTEGER_ONLY_LIBRARY
1565
encoder->private_->real_signal[channel][i] = (FLAC__real)x;
1568
encoder->private_->current_sample_number++;
1570
if(i == blocksize) {
1571
if(!process_frame_(encoder, false)) /* false => not last frame */
1574
} while(j < samples);
2047
if(encoder->protected_->do_mid_side_stereo && channels == 2) {
2049
* stereo coding: unroll channel loop
2052
if(encoder->protected_->verify)
2053
append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+OVERREAD_-encoder->private_->current_sample_number, samples-j));
2055
/* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2056
for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
2057
encoder->private_->integer_signal[0][i] = mid = side = buffer[k++];
2059
encoder->private_->integer_signal[1][i] = x;
2062
mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
2063
encoder->private_->integer_signal_mid_side[1][i] = side;
2064
encoder->private_->integer_signal_mid_side[0][i] = mid;
2066
encoder->private_->current_sample_number = i;
2067
/* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2069
if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2071
/* move unprocessed overread samples to beginnings of arrays */
2072
FLAC__ASSERT(i == blocksize+OVERREAD_);
2073
FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2074
encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][blocksize];
2075
encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][blocksize];
2076
encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][blocksize];
2077
encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][blocksize];
2078
encoder->private_->current_sample_number = 1;
2080
} while(j < samples);
1578
if(encoder->protected_->do_mid_side_stereo && channels == 2) {
1580
* stereo coding: unroll channel loop
1581
* without LPC: no need to calculate floating point version of signal
1584
if(encoder->protected_->verify)
1585
append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1587
for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1588
encoder->private_->integer_signal[0][i] = mid = side = buffer[k++];
1590
encoder->private_->integer_signal[1][i] = x;
1593
mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
1594
encoder->private_->integer_signal_mid_side[1][i] = side;
1595
encoder->private_->integer_signal_mid_side[0][i] = mid;
1596
encoder->private_->current_sample_number++;
1598
if(i == blocksize) {
1599
if(!process_frame_(encoder, false)) /* false => not last frame */
1602
} while(j < samples);
1606
* independent channel coding: buffer each channel in inner loop
1607
* without LPC: no need to calculate floating point version of signal
1610
if(encoder->protected_->verify)
1611
append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
1613
for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
1614
for(channel = 0; channel < channels; channel++)
1615
encoder->private_->integer_signal[channel][i] = buffer[k++];
1616
encoder->private_->current_sample_number++;
1618
if(i == blocksize) {
1619
if(!process_frame_(encoder, false)) /* false => not last frame */
1622
} while(j < samples);
2084
* independent channel coding: buffer each channel in inner loop
2087
if(encoder->protected_->verify)
2088
append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize+OVERREAD_-encoder->private_->current_sample_number, samples-j));
2090
/* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
2091
for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
2092
for(channel = 0; channel < channels; channel++)
2093
encoder->private_->integer_signal[channel][i] = buffer[k++];
2095
encoder->private_->current_sample_number = i;
2096
/* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
2098
if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
2100
/* move unprocessed overread samples to beginnings of arrays */
2101
FLAC__ASSERT(i == blocksize+OVERREAD_);
2102
FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
2103
for(channel = 0; channel < channels; channel++)
2104
encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][blocksize];
2105
encoder->private_->current_sample_number = 1;
2107
} while(j < samples);
1754
2269
/* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx()
1755
2270
* requires that the input arrays (in our case the integer signals)
1756
2271
* have a buffer of up to 3 zeroes in front (at negative indices) for
1757
* alignment purposes; we use 4 to keep the data well-aligned.
2272
* alignment purposes; we use 4 in front to keep the data well-aligned.
1760
2275
for(i = 0; ok && i < encoder->protected_->channels; i++) {
1761
ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
1762
#ifndef FLAC__INTEGER_ONLY_LIBRARY
1763
if(encoder->protected_->max_lpc_order > 0)
1764
ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
2276
ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize+4+OVERREAD_, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
1766
2277
memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
1767
2278
encoder->private_->integer_signal[i] += 4;
2279
#ifndef FLAC__INTEGER_ONLY_LIBRARY
2280
#if 0 /* @@@ currently unused */
2281
if(encoder->protected_->max_lpc_order > 0)
2282
ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize+OVERREAD_, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
1769
2286
for(i = 0; ok && i < 2; i++) {
1770
ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size+4, &encoder->private_->integer_signal_mid_side_unaligned[i], &encoder->private_->integer_signal_mid_side[i]);
1771
#ifndef FLAC__INTEGER_ONLY_LIBRARY
1772
if(encoder->protected_->max_lpc_order > 0)
1773
ok = ok && FLAC__memory_alloc_aligned_real_array(new_size, &encoder->private_->real_signal_mid_side_unaligned[i], &encoder->private_->real_signal_mid_side[i]);
2287
ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize+4+OVERREAD_, &encoder->private_->integer_signal_mid_side_unaligned[i], &encoder->private_->integer_signal_mid_side[i]);
1775
2288
memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
1776
2289
encoder->private_->integer_signal_mid_side[i] += 4;
2290
#ifndef FLAC__INTEGER_ONLY_LIBRARY
2291
#if 0 /* @@@ currently unused */
2292
if(encoder->protected_->max_lpc_order > 0)
2293
ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize+OVERREAD_, &encoder->private_->real_signal_mid_side_unaligned[i], &encoder->private_->real_signal_mid_side[i]);
2297
#ifndef FLAC__INTEGER_ONLY_LIBRARY
2298
if(ok && encoder->protected_->max_lpc_order > 0) {
2299
for(i = 0; ok && i < encoder->protected_->num_apodizations; i++)
2300
ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize, &encoder->private_->window_unaligned[i], &encoder->private_->window[i]);
2301
ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize, &encoder->private_->windowed_signal_unaligned, &encoder->private_->windowed_signal);
1778
2304
for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
1779
2305
for(i = 0; ok && i < 2; i++) {
1780
ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]);
2306
ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]);
1783
2309
for(channel = 0; ok && channel < 2; channel++) {
1784
2310
for(i = 0; ok && i < 2; i++) {
1785
ok = ok && FLAC__memory_alloc_aligned_int32_array(new_size, &encoder->private_->residual_workspace_mid_side_unaligned[channel][i], &encoder->private_->residual_workspace_mid_side[channel][i]);
2311
ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize, &encoder->private_->residual_workspace_mid_side_unaligned[channel][i], &encoder->private_->residual_workspace_mid_side[channel][i]);
1788
ok = ok && FLAC__memory_alloc_aligned_uint32_array(new_size, &encoder->private_->abs_residual_unaligned, &encoder->private_->abs_residual);
1789
if(encoder->private_->precompute_partition_sums || encoder->protected_->do_escape_coding) /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
1790
ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_size * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
2314
/* the *2 is an approximation to the series 1 + 1/2 + 1/4 + ... that sums tree occupies in a flat array */
2315
/*@@@ new_blocksize*2 is too pessimistic, but to fix, we need smarter logic because a smaller new_blocksize can actually increase the # of partitions; would require moving this out into a separate function, then checking its capacity against the need of the current blocksize&min/max_partition_order (and maybe predictor order) */
2316
ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_blocksize * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
1791
2317
if(encoder->protected_->do_escape_coding)
1792
ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_size * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition);
2318
ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_blocksize * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition);
2320
/* now adjust the windows if the blocksize has changed */
2321
#ifndef FLAC__INTEGER_ONLY_LIBRARY
2322
if(ok && new_blocksize != encoder->private_->input_capacity && encoder->protected_->max_lpc_order > 0) {
2323
for(i = 0; ok && i < encoder->protected_->num_apodizations; i++) {
2324
switch(encoder->protected_->apodizations[i].type) {
2325
case FLAC__APODIZATION_BARTLETT:
2326
FLAC__window_bartlett(encoder->private_->window[i], new_blocksize);
2328
case FLAC__APODIZATION_BARTLETT_HANN:
2329
FLAC__window_bartlett_hann(encoder->private_->window[i], new_blocksize);
2331
case FLAC__APODIZATION_BLACKMAN:
2332
FLAC__window_blackman(encoder->private_->window[i], new_blocksize);
2334
case FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE:
2335
FLAC__window_blackman_harris_4term_92db_sidelobe(encoder->private_->window[i], new_blocksize);
2337
case FLAC__APODIZATION_CONNES:
2338
FLAC__window_connes(encoder->private_->window[i], new_blocksize);
2340
case FLAC__APODIZATION_FLATTOP:
2341
FLAC__window_flattop(encoder->private_->window[i], new_blocksize);
2343
case FLAC__APODIZATION_GAUSS:
2344
FLAC__window_gauss(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.gauss.stddev);
2346
case FLAC__APODIZATION_HAMMING:
2347
FLAC__window_hamming(encoder->private_->window[i], new_blocksize);
2349
case FLAC__APODIZATION_HANN:
2350
FLAC__window_hann(encoder->private_->window[i], new_blocksize);
2352
case FLAC__APODIZATION_KAISER_BESSEL:
2353
FLAC__window_kaiser_bessel(encoder->private_->window[i], new_blocksize);
2355
case FLAC__APODIZATION_NUTTALL:
2356
FLAC__window_nuttall(encoder->private_->window[i], new_blocksize);
2358
case FLAC__APODIZATION_RECTANGLE:
2359
FLAC__window_rectangle(encoder->private_->window[i], new_blocksize);
2361
case FLAC__APODIZATION_TRIANGLE:
2362
FLAC__window_triangle(encoder->private_->window[i], new_blocksize);
2364
case FLAC__APODIZATION_TUKEY:
2365
FLAC__window_tukey(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.tukey.p);
2367
case FLAC__APODIZATION_WELCH:
2368
FLAC__window_welch(encoder->private_->window[i], new_blocksize);
2372
/* double protection */
2373
FLAC__window_hann(encoder->private_->window[i], new_blocksize);
1795
encoder->private_->input_capacity = new_size;
2381
encoder->private_->input_capacity = new_blocksize;
1797
2383
encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1802
FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples)
2388
FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples, FLAC__bool is_last_block)
1804
2390
const FLAC__byte *buffer;
1807
FLAC__ASSERT(FLAC__bitbuffer_is_byte_aligned(encoder->private_->frame));
1809
FLAC__bitbuffer_get_buffer(encoder->private_->frame, &buffer, &bytes);
2393
FLAC__ASSERT(FLAC__bitwriter_is_byte_aligned(encoder->private_->frame));
2395
if(!FLAC__bitwriter_get_buffer(encoder->private_->frame, &buffer, &bytes)) {
2396
encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1811
2400
if(encoder->protected_->verify) {
1812
2401
encoder->private_->verify.output.data = buffer;
1827
if(encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
1828
FLAC__bitbuffer_release_buffer(encoder->private_->frame);
1829
encoder->protected_->state = FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING;
2417
if(write_frame_(encoder, buffer, bytes, samples, is_last_block) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2418
FLAC__bitwriter_release_buffer(encoder->private_->frame);
2419
FLAC__bitwriter_clear(encoder->private_->frame);
2420
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
1833
FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2424
FLAC__bitwriter_release_buffer(encoder->private_->frame);
2425
FLAC__bitwriter_clear(encoder->private_->frame);
1835
2427
if(samples > 0) {
1836
encoder->private_->metadata.data.stream_info.min_framesize = min(bytes, encoder->private_->metadata.data.stream_info.min_framesize);
1837
encoder->private_->metadata.data.stream_info.max_framesize = max(bytes, encoder->private_->metadata.data.stream_info.max_framesize);
2428
encoder->private_->streaminfo.data.stream_info.min_framesize = min(bytes, encoder->private_->streaminfo.data.stream_info.min_framesize);
2429
encoder->private_->streaminfo.data.stream_info.max_framesize = max(bytes, encoder->private_->streaminfo.data.stream_info.max_framesize);
1843
FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame)
2435
FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, FLAC__bool is_last_block)
2437
FLAC__StreamEncoderWriteStatus status;
2438
FLAC__uint64 output_position = 0;
2440
/* FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED just means we didn't get the offset; no error */
2441
if(encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &output_position, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) {
2442
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2443
return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
2447
* Watch for the STREAMINFO block and first SEEKTABLE block to go by and store their offsets.
2450
FLAC__MetadataType type = (buffer[0] & 0x7f);
2451
if(type == FLAC__METADATA_TYPE_STREAMINFO)
2452
encoder->protected_->streaminfo_offset = output_position;
2453
else if(type == FLAC__METADATA_TYPE_SEEKTABLE && encoder->protected_->seektable_offset == 0)
2454
encoder->protected_->seektable_offset = output_position;
2458
* Mark the current seek point if hit (if audio_offset == 0 that
2459
* means we're still writing metadata and haven't hit the first
2462
if(0 != encoder->private_->seek_table && encoder->protected_->audio_offset > 0 && encoder->private_->seek_table->num_points > 0) {
2463
const unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
2464
const FLAC__uint64 frame_first_sample = encoder->private_->samples_written;
2465
const FLAC__uint64 frame_last_sample = frame_first_sample + (FLAC__uint64)blocksize - 1;
2466
FLAC__uint64 test_sample;
2468
for(i = encoder->private_->first_seekpoint_to_check; i < encoder->private_->seek_table->num_points; i++) {
2469
test_sample = encoder->private_->seek_table->points[i].sample_number;
2470
if(test_sample > frame_last_sample) {
2473
else if(test_sample >= frame_first_sample) {
2474
encoder->private_->seek_table->points[i].sample_number = frame_first_sample;
2475
encoder->private_->seek_table->points[i].stream_offset = output_position - encoder->protected_->audio_offset;
2476
encoder->private_->seek_table->points[i].frame_samples = blocksize;
2477
encoder->private_->first_seekpoint_to_check++;
2478
/* DO NOT: "break;" and here's why:
2479
* The seektable template may contain more than one target
2480
* sample for any given frame; we will keep looping, generating
2481
* duplicate seekpoints for them, and we'll clean it up later,
2482
* just before writing the seektable back to the metadata.
2486
encoder->private_->first_seekpoint_to_check++;
2492
if(encoder->private_->is_ogg) {
2493
status = FLAC__ogg_encoder_aspect_write_callback_wrapper(
2494
&encoder->protected_->ogg_encoder_aspect,
2498
encoder->private_->current_frame_number,
2500
(FLAC__OggEncoderAspectWriteCallbackProxy)encoder->private_->write_callback,
2502
encoder->private_->client_data
2507
status = encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data);
2509
if(status == FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2510
encoder->private_->bytes_written += bytes;
2511
encoder->private_->samples_written += samples;
2512
/* we keep a high watermark on the number of frames written because
2513
* when the encoder goes back to write metadata, 'current_frame'
2514
* will drop back to 0.
2516
encoder->private_->frames_written = max(encoder->private_->frames_written, encoder->private_->current_frame_number+1);
2519
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2524
/* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */
2525
void update_metadata_(const FLAC__StreamEncoder *encoder)
2527
FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
2528
const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
2529
const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
2530
const unsigned min_framesize = metadata->data.stream_info.min_framesize;
2531
const unsigned max_framesize = metadata->data.stream_info.max_framesize;
2532
const unsigned bps = metadata->data.stream_info.bits_per_sample;
2533
FLAC__StreamEncoderSeekStatus seek_status;
2535
FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
2537
/* All this is based on intimate knowledge of the stream header
2538
* layout, but a change to the header format that would break this
2539
* would also break all streams encoded in the previous format.
2543
* Write MD5 signature
2546
const unsigned md5_offset =
2547
FLAC__STREAM_METADATA_HEADER_LENGTH +
2549
FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2550
FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2551
FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2552
FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2553
FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2554
FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2555
FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
2556
FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
2559
if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + md5_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
2560
if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2561
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2564
if(encoder->private_->write_callback(encoder, metadata->data.stream_info.md5sum, 16, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2565
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2571
* Write total samples
2574
const unsigned total_samples_byte_offset =
2575
FLAC__STREAM_METADATA_HEADER_LENGTH +
2577
FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2578
FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2579
FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2580
FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2581
FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2582
FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2583
FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
2587
b[0] = ((FLAC__byte)(bps-1) << 4) | (FLAC__byte)((samples >> 32) & 0x0F);
2588
b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
2589
b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
2590
b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
2591
b[4] = (FLAC__byte)(samples & 0xFF);
2592
if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + total_samples_byte_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
2593
if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2594
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2597
if(encoder->private_->write_callback(encoder, b, 5, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2598
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2604
* Write min/max framesize
2607
const unsigned min_framesize_offset =
2608
FLAC__STREAM_METADATA_HEADER_LENGTH +
2610
FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2611
FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
2614
b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
2615
b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
2616
b[2] = (FLAC__byte)(min_framesize & 0xFF);
2617
b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
2618
b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
2619
b[5] = (FLAC__byte)(max_framesize & 0xFF);
2620
if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + min_framesize_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
2621
if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2622
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2625
if(encoder->private_->write_callback(encoder, b, 6, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2626
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2634
if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
2637
FLAC__format_seektable_sort(encoder->private_->seek_table);
2639
FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
2641
if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->seektable_offset + FLAC__STREAM_METADATA_HEADER_LENGTH, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
2642
if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2643
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2647
for(i = 0; i < encoder->private_->seek_table->num_points; i++) {
2650
xx = encoder->private_->seek_table->points[i].sample_number;
2651
b[7] = (FLAC__byte)xx; xx >>= 8;
2652
b[6] = (FLAC__byte)xx; xx >>= 8;
2653
b[5] = (FLAC__byte)xx; xx >>= 8;
2654
b[4] = (FLAC__byte)xx; xx >>= 8;
2655
b[3] = (FLAC__byte)xx; xx >>= 8;
2656
b[2] = (FLAC__byte)xx; xx >>= 8;
2657
b[1] = (FLAC__byte)xx; xx >>= 8;
2658
b[0] = (FLAC__byte)xx; xx >>= 8;
2659
xx = encoder->private_->seek_table->points[i].stream_offset;
2660
b[15] = (FLAC__byte)xx; xx >>= 8;
2661
b[14] = (FLAC__byte)xx; xx >>= 8;
2662
b[13] = (FLAC__byte)xx; xx >>= 8;
2663
b[12] = (FLAC__byte)xx; xx >>= 8;
2664
b[11] = (FLAC__byte)xx; xx >>= 8;
2665
b[10] = (FLAC__byte)xx; xx >>= 8;
2666
b[9] = (FLAC__byte)xx; xx >>= 8;
2667
b[8] = (FLAC__byte)xx; xx >>= 8;
2668
x = encoder->private_->seek_table->points[i].frame_samples;
2669
b[17] = (FLAC__byte)x; x >>= 8;
2670
b[16] = (FLAC__byte)x; x >>= 8;
2671
if(encoder->private_->write_callback(encoder, b, 18, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2672
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2680
/* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */
2681
void update_ogg_metadata_(FLAC__StreamEncoder *encoder)
2683
/* the # of bytes in the 1st packet that precede the STREAMINFO */
2684
static const unsigned FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH =
2685
FLAC__OGG_MAPPING_PACKET_TYPE_LENGTH +
2686
FLAC__OGG_MAPPING_MAGIC_LENGTH +
2687
FLAC__OGG_MAPPING_VERSION_MAJOR_LENGTH +
2688
FLAC__OGG_MAPPING_VERSION_MINOR_LENGTH +
2689
FLAC__OGG_MAPPING_NUM_HEADERS_LENGTH +
2690
FLAC__STREAM_SYNC_LENGTH
2692
FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
2693
const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
2694
const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
2695
const unsigned min_framesize = metadata->data.stream_info.min_framesize;
2696
const unsigned max_framesize = metadata->data.stream_info.max_framesize;
2699
FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
2700
FLAC__ASSERT(0 != encoder->private_->seek_callback);
2702
/* Pre-check that client supports seeking, since we don't want the
2703
* ogg_helper code to ever have to deal with this condition.
2705
if(encoder->private_->seek_callback(encoder, 0, encoder->private_->client_data) == FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED)
2708
/* All this is based on intimate knowledge of the stream header
2709
* layout, but a change to the header format that would break this
2710
* would also break all streams encoded in the previous format.
2714
** Write STREAMINFO stats
2716
simple_ogg_page__init(&page);
2717
if(!simple_ogg_page__get_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
2718
simple_ogg_page__clear(&page);
2719
return; /* state already set */
2723
* Write MD5 signature
2726
const unsigned md5_offset =
2727
FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
2728
FLAC__STREAM_METADATA_HEADER_LENGTH +
2730
FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2731
FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2732
FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2733
FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2734
FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2735
FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2736
FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
2737
FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
2740
if(md5_offset + 16 > (unsigned)page.body_len) {
2741
encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2742
simple_ogg_page__clear(&page);
2745
memcpy(page.body + md5_offset, metadata->data.stream_info.md5sum, 16);
2749
* Write total samples
2752
const unsigned total_samples_byte_offset =
2753
FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
2754
FLAC__STREAM_METADATA_HEADER_LENGTH +
2756
FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2757
FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2758
FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2759
FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2760
FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2761
FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2762
FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
2766
if(total_samples_byte_offset + 5 > (unsigned)page.body_len) {
2767
encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2768
simple_ogg_page__clear(&page);
2771
b[0] = (FLAC__byte)page.body[total_samples_byte_offset] & 0xF0;
2772
b[0] |= (FLAC__byte)((samples >> 32) & 0x0F);
2773
b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
2774
b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
2775
b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
2776
b[4] = (FLAC__byte)(samples & 0xFF);
2777
memcpy(page.body + total_samples_byte_offset, b, 5);
2781
* Write min/max framesize
2784
const unsigned min_framesize_offset =
2785
FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
2786
FLAC__STREAM_METADATA_HEADER_LENGTH +
2788
FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2789
FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
2792
if(min_framesize_offset + 6 > (unsigned)page.body_len) {
2793
encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2794
simple_ogg_page__clear(&page);
2797
b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
2798
b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
2799
b[2] = (FLAC__byte)(min_framesize & 0xFF);
2800
b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
2801
b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
2802
b[5] = (FLAC__byte)(max_framesize & 0xFF);
2803
memcpy(page.body + min_framesize_offset, b, 6);
2805
if(!simple_ogg_page__set_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
2806
simple_ogg_page__clear(&page);
2807
return; /* state already set */
2809
simple_ogg_page__clear(&page);
2814
if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
2818
FLAC__format_seektable_sort(encoder->private_->seek_table);
2820
FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
2822
simple_ogg_page__init(&page);
2823
if(!simple_ogg_page__get_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
2824
simple_ogg_page__clear(&page);
2825
return; /* state already set */
2828
if((FLAC__STREAM_METADATA_HEADER_LENGTH + 18*encoder->private_->seek_table->num_points) != (unsigned)page.body_len) {
2829
encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2830
simple_ogg_page__clear(&page);
2834
for(i = 0, p = page.body + FLAC__STREAM_METADATA_HEADER_LENGTH; i < encoder->private_->seek_table->num_points; i++, p += 18) {
2837
xx = encoder->private_->seek_table->points[i].sample_number;
2838
b[7] = (FLAC__byte)xx; xx >>= 8;
2839
b[6] = (FLAC__byte)xx; xx >>= 8;
2840
b[5] = (FLAC__byte)xx; xx >>= 8;
2841
b[4] = (FLAC__byte)xx; xx >>= 8;
2842
b[3] = (FLAC__byte)xx; xx >>= 8;
2843
b[2] = (FLAC__byte)xx; xx >>= 8;
2844
b[1] = (FLAC__byte)xx; xx >>= 8;
2845
b[0] = (FLAC__byte)xx; xx >>= 8;
2846
xx = encoder->private_->seek_table->points[i].stream_offset;
2847
b[15] = (FLAC__byte)xx; xx >>= 8;
2848
b[14] = (FLAC__byte)xx; xx >>= 8;
2849
b[13] = (FLAC__byte)xx; xx >>= 8;
2850
b[12] = (FLAC__byte)xx; xx >>= 8;
2851
b[11] = (FLAC__byte)xx; xx >>= 8;
2852
b[10] = (FLAC__byte)xx; xx >>= 8;
2853
b[9] = (FLAC__byte)xx; xx >>= 8;
2854
b[8] = (FLAC__byte)xx; xx >>= 8;
2855
x = encoder->private_->seek_table->points[i].frame_samples;
2856
b[17] = (FLAC__byte)x; x >>= 8;
2857
b[16] = (FLAC__byte)x; x >>= 8;
2861
if(!simple_ogg_page__set_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
2862
simple_ogg_page__clear(&page);
2863
return; /* state already set */
2865
simple_ogg_page__clear(&page);
2870
FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block, FLAC__bool is_last_block)
1845
2873
FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
1848
2876
* Accumulate raw signal to the MD5 signature
1850
if(!FLAC__MD5Accumulate(&encoder->private_->md5context, (const FLAC__int32 * const *)encoder->private_->integer_signal, encoder->protected_->channels, encoder->protected_->blocksize, (encoder->protected_->bits_per_sample+7) / 8)) {
2878
if(encoder->protected_->do_md5 && !FLAC__MD5Accumulate(&encoder->private_->md5context, (const FLAC__int32 * const *)encoder->private_->integer_signal, encoder->protected_->channels, encoder->protected_->blocksize, (encoder->protected_->bits_per_sample+7) / 8)) {
1851
2879
encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
2280
3305
max_lpc_order = encoder->protected_->max_lpc_order;
2281
3306
if(max_lpc_order > 0) {
2282
encoder->private_->local_lpc_compute_autocorrelation(real_signal, frame_header->blocksize, max_lpc_order+1, autoc);
2283
/* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
2284
if(autoc[0] != 0.0) {
2285
FLAC__lpc_compute_lp_coefficients(autoc, max_lpc_order, encoder->private_->lp_coeff, lpc_error);
2286
if(encoder->protected_->do_exhaustive_model_search) {
2290
unsigned guess_lpc_order = FLAC__lpc_compute_best_order(lpc_error, max_lpc_order, frame_header->blocksize, subframe_bps);
2291
min_lpc_order = max_lpc_order = guess_lpc_order;
2293
for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
2294
lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
2295
if(lpc_residual_bits_per_sample >= (FLAC__double)subframe_bps)
2296
continue; /* don't even try */
2297
rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
2298
#ifndef FLAC__SYMMETRIC_RICE
2299
rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
2301
if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2302
#ifdef DEBUG_VERBOSE
2303
fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2305
rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2307
if(encoder->protected_->do_qlp_coeff_prec_search) {
2308
min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
2309
/* ensure a 32-bit datapath throughout for 16bps or less */
2310
if(subframe_bps <= 16)
2311
max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
2313
max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
3308
for (a = 0; a < encoder->protected_->num_apodizations; a++) {
3309
FLAC__lpc_window_data(integer_signal, encoder->private_->window[a], encoder->private_->windowed_signal, frame_header->blocksize);
3310
encoder->private_->local_lpc_compute_autocorrelation(encoder->private_->windowed_signal, frame_header->blocksize, max_lpc_order+1, autoc);
3311
/* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
3312
if(autoc[0] != 0.0) {
3313
FLAC__lpc_compute_lp_coefficients(autoc, &max_lpc_order, encoder->private_->lp_coeff, lpc_error);
3314
if(encoder->protected_->do_exhaustive_model_search) {
2316
min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
2318
for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
2320
evaluate_lpc_subframe_(
2323
residual[!_best_subframe],
2324
encoder->private_->abs_residual,
2325
encoder->private_->abs_residual_partition_sums,
2326
encoder->private_->raw_bits_per_partition,
2327
encoder->private_->lp_coeff[lpc_order-1],
3318
const unsigned guess_lpc_order =
3319
FLAC__lpc_compute_best_order(
2328
3322
frame_header->blocksize,
2331
qlp_coeff_precision,
2333
min_partition_order,
2334
max_partition_order,
2335
precompute_partition_sums,
2336
encoder->protected_->do_escape_coding,
2337
encoder->protected_->rice_parameter_search_dist,
2338
subframe[!_best_subframe],
2339
partitioned_rice_contents[!_best_subframe]
3324
encoder->protected_->do_qlp_coeff_prec_search?
3325
FLAC__MIN_QLP_COEFF_PRECISION : /* have to guess; use the min possible size to avoid accidentally favoring lower orders */
3326
encoder->protected_->qlp_coeff_precision
2341
if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
2342
if(_candidate_bits < _best_bits) {
2343
_best_subframe = !_best_subframe;
2344
_best_bits = _candidate_bits;
3329
min_lpc_order = max_lpc_order = guess_lpc_order;
3331
if(max_lpc_order >= frame_header->blocksize)
3332
max_lpc_order = frame_header->blocksize - 1;
3333
for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
3334
lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
3335
if(lpc_residual_bits_per_sample >= (FLAC__double)subframe_bps)
3336
continue; /* don't even try */
3337
rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
3338
rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
3339
if(rice_parameter >= rice_parameter_limit) {
3340
#ifdef DEBUG_VERBOSE
3341
fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, rice_parameter_limit - 1);
3343
rice_parameter = rice_parameter_limit - 1;
3345
if(encoder->protected_->do_qlp_coeff_prec_search) {
3346
min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
3347
/* try to ensure a 32-bit datapath throughout for 16bps(+1bps for side channel) or less */
3348
if(subframe_bps <= 17) {
3349
max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
3350
max_qlp_coeff_precision = max(max_qlp_coeff_precision, min_qlp_coeff_precision);
3353
max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
3356
min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
3358
for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
3360
evaluate_lpc_subframe_(
3363
residual[!_best_subframe],
3364
encoder->private_->abs_residual_partition_sums,
3365
encoder->private_->raw_bits_per_partition,
3366
encoder->private_->lp_coeff[lpc_order-1],
3367
frame_header->blocksize,
3370
qlp_coeff_precision,
3372
rice_parameter_limit,
3373
min_partition_order,
3374
max_partition_order,
3375
encoder->protected_->do_escape_coding,
3376
encoder->protected_->rice_parameter_search_dist,
3377
subframe[!_best_subframe],
3378
partitioned_rice_contents[!_best_subframe]
3380
if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
3381
if(_candidate_bits < _best_bits) {
3382
_best_subframe = !_best_subframe;
3383
_best_bits = _candidate_bits;
2551
3637
for(i = 0; i < order; i++)
2552
3638
subframe->data.lpc.warmup[i] = signal[i];
2554
return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN + FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN + (order * (qlp_coeff_precision + subframe_bps)) + residual_bits;
3640
estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN + FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN + (order * (qlp_coeff_precision + subframe_bps)) + residual_bits;
3642
#if SPOTCHECK_ESTIMATE
3643
spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
2558
3650
unsigned evaluate_verbatim_subframe_(
3651
FLAC__StreamEncoder *encoder,
2559
3652
const FLAC__int32 signal[],
2560
3653
unsigned blocksize,
2561
3654
unsigned subframe_bps,
2562
3655
FLAC__Subframe *subframe
2565
3660
subframe->type = FLAC__SUBFRAME_TYPE_VERBATIM;
2567
3662
subframe->data.verbatim.data = signal;
2569
return FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + (blocksize * subframe_bps);
3664
estimate = FLAC__SUBFRAME_ZERO_PAD_LEN + FLAC__SUBFRAME_TYPE_LEN + FLAC__SUBFRAME_WASTED_BITS_FLAG_LEN + subframe->wasted_bits + (blocksize * subframe_bps);
3666
#if SPOTCHECK_ESTIMATE
3667
spotcheck_subframe_estimate_(encoder, blocksize, subframe_bps, subframe, estimate);
2572
3675
unsigned find_best_partition_order_(
2573
3676
FLAC__StreamEncoderPrivate *private_,
2574
3677
const FLAC__int32 residual[],
2575
FLAC__uint32 abs_residual[],
2576
3678
FLAC__uint64 abs_residual_partition_sums[],
2577
3679
unsigned raw_bits_per_partition[],
2578
3680
unsigned residual_samples,
2579
3681
unsigned predictor_order,
2580
3682
unsigned rice_parameter,
3683
unsigned rice_parameter_limit,
2581
3684
unsigned min_partition_order,
2582
3685
unsigned max_partition_order,
2583
FLAC__bool precompute_partition_sums,
2584
3687
FLAC__bool do_escape_coding,
2585
3688
unsigned rice_parameter_search_dist,
2586
FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
3689
FLAC__EntropyCodingMethod *best_ecm
2590
3692
unsigned residual_bits, best_residual_bits = 0;
2591
unsigned residual_sample;
2592
3693
unsigned best_parameters_index = 0;
3694
unsigned best_partition_order = 0;
2593
3695
const unsigned blocksize = residual_samples + predictor_order;
2595
/* compute abs(residual) for use later */
2596
for(residual_sample = 0; residual_sample < residual_samples; residual_sample++) {
2597
r = residual[residual_sample];
2598
abs_residual[residual_sample] = (FLAC__uint32)(r<0? -r : r);
2601
3697
max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(max_partition_order, blocksize, predictor_order);
2602
3698
min_partition_order = min(min_partition_order, max_partition_order);
2604
if(precompute_partition_sums) {
3700
precompute_partition_info_sums_(residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order, bps);
3702
if(do_escape_coding)
3703
precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
2605
3706
int partition_order;
2608
precompute_partition_info_sums_(abs_residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order);
2610
if(do_escape_coding)
2611
precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
2613
3709
for(partition_order = (int)max_partition_order, sum = 0; partition_order >= (int)min_partition_order; partition_order--) {
2614
#ifdef DONT_ESTIMATE_RICE_BITS
2616
set_partitioned_rice_with_precompute_(
3711
set_partitioned_rice_(
3712
#ifdef EXACT_RICE_BITS_CALCULATION
2618
abs_residual_partition_sums+sum,
2619
raw_bits_per_partition+sum,
2623
rice_parameter_search_dist,
2624
(unsigned)partition_order,
2626
&private_->partitioned_rice_contents_extra[!best_parameters_index],
2632
set_partitioned_rice_with_precompute_(
2634
abs_residual_partition_sums+sum,
2635
raw_bits_per_partition+sum,
2639
rice_parameter_search_dist,
2640
(unsigned)partition_order,
2642
&private_->partitioned_rice_contents_extra[!best_parameters_index],
3715
abs_residual_partition_sums+sum,
3716
raw_bits_per_partition+sum,
3720
rice_parameter_limit,
3721
rice_parameter_search_dist,
3722
(unsigned)partition_order,
3724
&private_->partitioned_rice_contents_extra[!best_parameters_index],
2648
3729
FLAC__ASSERT(best_residual_bits != 0);
2652
3733
if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2653
3734
best_residual_bits = residual_bits;
2654
3735
best_parameters_index = !best_parameters_index;
2655
best_partitioned_rice->order = partition_order;
3736
best_partition_order = partition_order;
2660
unsigned partition_order;
2661
for(partition_order = min_partition_order; partition_order <= max_partition_order; partition_order++) {
2662
#ifdef DONT_ESTIMATE_RICE_BITS
2664
set_partitioned_rice_(
2670
rice_parameter_search_dist,
2672
&private_->partitioned_rice_contents_extra[!best_parameters_index],
2678
set_partitioned_rice_(
2683
rice_parameter_search_dist,
2685
&private_->partitioned_rice_contents_extra[!best_parameters_index],
2691
FLAC__ASSERT(best_residual_bits != 0);
3741
best_ecm->data.partitioned_rice.order = best_partition_order;
3745
* We are allowed to de-const the pointer based on our special
3746
* knowledge; it is const to the outside world.
3748
FLAC__EntropyCodingMethod_PartitionedRiceContents* prc = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_ecm->data.partitioned_rice.contents;
3751
/* save best parameters and raw_bits */
3752
FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(prc, max(6, best_partition_order));
3753
memcpy(prc->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partition_order)));
3754
if(do_escape_coding)
3755
memcpy(prc->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partition_order)));
3757
* Now need to check if the type should be changed to
3758
* FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2 based on the
3759
* size of the rice parameters.
3761
for(partition = 0; partition < (1u<<best_partition_order); partition++) {
3762
if(prc->parameters[partition] >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3763
best_ecm->type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2;
2694
if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
2695
best_residual_bits = residual_bits;
2696
best_parameters_index = !best_parameters_index;
2697
best_partitioned_rice->order = partition_order;
2703
* We are allowed to de-const the pointer based on our special knowledge;
2704
* it is const to the outside world.
2707
FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents;
2708
FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order));
2709
memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2710
memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
2713
3769
return best_residual_bits;
3772
#if defined(FLAC__CPU_IA32) && !defined FLAC__NO_ASM && defined FLAC__HAS_NASM
3773
extern void precompute_partition_info_sums_32bit_asm_ia32_(
3774
const FLAC__int32 residual[],
3775
FLAC__uint64 abs_residual_partition_sums[],
3777
unsigned predictor_order,
3778
unsigned min_partition_order,
3779
unsigned max_partition_order
2716
3783
void precompute_partition_info_sums_(
2717
const FLAC__uint32 abs_residual[],
3784
const FLAC__int32 residual[],
2718
3785
FLAC__uint64 abs_residual_partition_sums[],
2719
3786
unsigned residual_samples,
2720
3787
unsigned predictor_order,
2721
3788
unsigned min_partition_order,
2722
unsigned max_partition_order
3789
unsigned max_partition_order,
2725
int partition_order;
2726
unsigned from_partition, to_partition = 0;
2727
const unsigned blocksize = residual_samples + predictor_order;
3793
const unsigned default_partition_samples = (residual_samples + predictor_order) >> max_partition_order;
3794
unsigned partitions = 1u << max_partition_order;
3796
FLAC__ASSERT(default_partition_samples > predictor_order);
3798
#if defined(FLAC__CPU_IA32) && !defined FLAC__NO_ASM && defined FLAC__HAS_NASM
3799
/* slightly pessimistic but still catches all common cases */
3800
/* WATCHOUT: "+ bps" is an assumption that the average residual magnitude will not be more than "bps" bits */
3801
if(FLAC__bitmath_ilog2(default_partition_samples) + bps < 32) {
3802
precompute_partition_info_sums_32bit_asm_ia32_(residual, abs_residual_partition_sums, residual_samples + predictor_order, predictor_order, min_partition_order, max_partition_order);
2729
3807
/* first do max_partition_order */
2730
for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
2731
FLAC__uint64 abs_residual_partition_sum;
2733
unsigned partition, partition_sample, partition_samples, residual_sample;
2734
const unsigned partitions = 1u << partition_order;
2735
const unsigned default_partition_samples = blocksize >> partition_order;
2737
FLAC__ASSERT(default_partition_samples > predictor_order);
2739
for(partition = residual_sample = 0; partition < partitions; partition++) {
2740
partition_samples = default_partition_samples;
2742
partition_samples -= predictor_order;
2743
abs_residual_partition_sum = 0;
2744
for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
2745
abs_r = abs_residual[residual_sample];
2746
abs_residual_partition_sum += abs_r;
2749
abs_residual_partition_sums[partition] = abs_residual_partition_sum;
2751
to_partition = partitions;
3809
unsigned partition, residual_sample, end = (unsigned)(-(int)predictor_order);
3810
/* slightly pessimistic but still catches all common cases */
3811
/* WATCHOUT: "+ bps" is an assumption that the average residual magnitude will not be more than "bps" bits */
3812
if(FLAC__bitmath_ilog2(default_partition_samples) + bps < 32) {
3813
FLAC__uint32 abs_residual_partition_sum;
3815
for(partition = residual_sample = 0; partition < partitions; partition++) {
3816
end += default_partition_samples;
3817
abs_residual_partition_sum = 0;
3818
for( ; residual_sample < end; residual_sample++)
3819
abs_residual_partition_sum += abs(residual[residual_sample]); /* abs(INT_MIN) is undefined, but if the residual is INT_MIN we have bigger problems */
3820
abs_residual_partition_sums[partition] = abs_residual_partition_sum;
3823
else { /* have to pessimistically use 64 bits for accumulator */
3824
FLAC__uint64 abs_residual_partition_sum;
3826
for(partition = residual_sample = 0; partition < partitions; partition++) {
3827
end += default_partition_samples;
3828
abs_residual_partition_sum = 0;
3829
for( ; residual_sample < end; residual_sample++)
3830
abs_residual_partition_sum += abs(residual[residual_sample]); /* abs(INT_MIN) is undefined, but if the residual is INT_MIN we have bigger problems */
3831
abs_residual_partition_sums[partition] = abs_residual_partition_sum;
2755
3836
/* now merge partitions for lower orders */
2756
for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
2759
const unsigned partitions = 1u << partition_order;
2760
for(i = 0; i < partitions; i++) {
2761
s = abs_residual_partition_sums[from_partition];
2763
abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
3838
unsigned from_partition = 0, to_partition = partitions;
3839
int partition_order;
3840
for(partition_order = (int)max_partition_order - 1; partition_order >= (int)min_partition_order; partition_order--) {
3843
for(i = 0; i < partitions; i++) {
3844
abs_residual_partition_sums[to_partition++] =
3845
abs_residual_partition_sums[from_partition ] +
3846
abs_residual_partition_sums[from_partition+1];
3847
from_partition += 2;
2829
#ifdef VARIABLE_RICE_BITS
2830
#undef VARIABLE_RICE_BITS
2832
#ifndef DONT_ESTIMATE_RICE_BITS
2833
#define VARIABLE_RICE_BITS(value, parameter) ((value) >> (parameter))
2836
#ifdef DONT_ESTIMATE_RICE_BITS
2837
FLAC__bool set_partitioned_rice_(
2838
const FLAC__uint32 abs_residual[],
2839
const FLAC__int32 residual[],
2840
const unsigned residual_samples,
2841
const unsigned predictor_order,
2842
const unsigned suggested_rice_parameter,
2843
const unsigned rice_parameter_search_dist,
2844
const unsigned partition_order,
2845
FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2849
FLAC__bool set_partitioned_rice_(
2850
const FLAC__uint32 abs_residual[],
2851
const unsigned residual_samples,
2852
const unsigned predictor_order,
2853
const unsigned suggested_rice_parameter,
2854
const unsigned rice_parameter_search_dist,
2855
const unsigned partition_order,
2856
FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
2861
unsigned rice_parameter, partition_bits;
2862
#ifndef NO_RICE_SEARCH
2863
unsigned best_partition_bits;
2864
unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
2866
unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
2867
unsigned *parameters;
2869
FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
2871
FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
2872
parameters = partitioned_rice_contents->parameters;
2874
if(partition_order == 0) {
2877
#ifndef NO_RICE_SEARCH
2878
if(rice_parameter_search_dist) {
2879
if(suggested_rice_parameter < rice_parameter_search_dist)
2880
min_rice_parameter = 0;
2882
min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
2883
max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
2884
if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2885
#ifdef DEBUG_VERBOSE
2886
fprintf(stderr, "clipping rice_parameter (%u -> %u) @2\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2888
max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2892
min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
2894
best_partition_bits = 0xffffffff;
2895
for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2897
#ifdef VARIABLE_RICE_BITS
2898
#ifdef FLAC__SYMMETRIC_RICE
2899
partition_bits = (2+rice_parameter) * residual_samples;
2901
const unsigned rice_parameter_estimate = rice_parameter-1;
2902
partition_bits = (1+rice_parameter) * residual_samples;
2907
partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
2908
for(i = 0; i < residual_samples; i++) {
2909
#ifdef VARIABLE_RICE_BITS
2910
#ifdef FLAC__SYMMETRIC_RICE
2911
partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
2913
partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
2916
partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
2919
#ifndef NO_RICE_SEARCH
2920
if(partition_bits < best_partition_bits) {
2921
best_rice_parameter = rice_parameter;
2922
best_partition_bits = partition_bits;
2926
parameters[0] = best_rice_parameter;
2927
bits_ += best_partition_bits;
2930
unsigned partition, residual_sample, save_residual_sample, partition_sample;
2931
unsigned partition_samples;
2932
FLAC__uint64 mean, k;
2933
const unsigned partitions = 1u << partition_order;
2934
for(partition = residual_sample = 0; partition < partitions; partition++) {
2935
partition_samples = (residual_samples+predictor_order) >> partition_order;
2936
if(partition == 0) {
2937
if(partition_samples <= predictor_order)
2940
partition_samples -= predictor_order;
2943
save_residual_sample = residual_sample;
2944
for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++)
2945
mean += abs_residual[residual_sample];
2946
residual_sample = save_residual_sample;
2947
#ifdef FLAC__SYMMETRIC_RICE
2948
mean += partition_samples >> 1; /* for rounding effect */
2949
mean /= partition_samples;
2951
/* calc rice_parameter = floor(log2(mean)) */
2959
/* we are basically calculating the size in bits of the
2960
* average residual magnitude in the partition:
2961
* rice_parameter = floor(log2(mean/partition_samples))
2962
* 'mean' is not a good name for the variable, it is
2963
* actually the sum of magnitudes of all residual values
2964
* in the partition, so the actual mean is
2965
* mean/partition_samples
2967
for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
2970
if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2971
#ifdef DEBUG_VERBOSE
2972
fprintf(stderr, "clipping rice_parameter (%u -> %u) @3\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2974
rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2977
#ifndef NO_RICE_SEARCH
2978
if(rice_parameter_search_dist) {
2979
if(rice_parameter < rice_parameter_search_dist)
2980
min_rice_parameter = 0;
2982
min_rice_parameter = rice_parameter - rice_parameter_search_dist;
2983
max_rice_parameter = rice_parameter + rice_parameter_search_dist;
2984
if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
2985
#ifdef DEBUG_VERBOSE
2986
fprintf(stderr, "clipping rice_parameter (%u -> %u) @4\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
2988
max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
2992
min_rice_parameter = max_rice_parameter = rice_parameter;
2994
best_partition_bits = 0xffffffff;
2995
for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
2997
#ifdef VARIABLE_RICE_BITS
2998
#ifdef FLAC__SYMMETRIC_RICE
2999
partition_bits = (2+rice_parameter) * partition_samples;
3001
const unsigned rice_parameter_estimate = rice_parameter-1;
3002
partition_bits = (1+rice_parameter) * partition_samples;
3007
partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
3008
save_residual_sample = residual_sample;
3009
for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
3010
#ifdef VARIABLE_RICE_BITS
3011
#ifdef FLAC__SYMMETRIC_RICE
3012
partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
3014
partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
3017
partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] in addition to abs_residual[] */
3020
#ifndef NO_RICE_SEARCH
3021
if(rice_parameter != max_rice_parameter)
3022
residual_sample = save_residual_sample;
3023
if(partition_bits < best_partition_bits) {
3024
best_rice_parameter = rice_parameter;
3025
best_partition_bits = partition_bits;
3029
parameters[partition] = best_rice_parameter;
3030
bits_ += best_partition_bits;
3038
#ifdef DONT_ESTIMATE_RICE_BITS
3039
FLAC__bool set_partitioned_rice_with_precompute_(
3040
const FLAC__int32 residual[],
3041
const FLAC__uint64 abs_residual_partition_sums[],
3042
const unsigned raw_bits_per_partition[],
3043
const unsigned residual_samples,
3044
const unsigned predictor_order,
3045
const unsigned suggested_rice_parameter,
3046
const unsigned rice_parameter_search_dist,
3047
const unsigned partition_order,
3048
const FLAC__bool search_for_escapes,
3049
FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3053
FLAC__bool set_partitioned_rice_with_precompute_(
3054
const FLAC__uint32 abs_residual[],
3055
const FLAC__uint64 abs_residual_partition_sums[],
3056
const unsigned raw_bits_per_partition[],
3057
const unsigned residual_samples,
3058
const unsigned predictor_order,
3059
const unsigned suggested_rice_parameter,
3060
const unsigned rice_parameter_search_dist,
3061
const unsigned partition_order,
3062
const FLAC__bool search_for_escapes,
3063
FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3068
unsigned rice_parameter, partition_bits;
3069
#ifndef NO_RICE_SEARCH
3070
unsigned best_partition_bits;
3071
unsigned min_rice_parameter, max_rice_parameter, best_rice_parameter = 0;
3911
#ifdef EXACT_RICE_BITS_CALCULATION
3912
static FLaC__INLINE unsigned count_rice_bits_in_partition_(
3913
const unsigned rice_parameter,
3914
const unsigned partition_samples,
3915
const FLAC__int32 *residual
3918
unsigned i, partition_bits =
3919
FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + /* actually could end up being FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN but err on side of 16bps */
3920
(1+rice_parameter) * partition_samples /* 1 for unary stop bit + rice_parameter for the binary portion */
3922
for(i = 0; i < partition_samples; i++)
3923
partition_bits += ( (FLAC__uint32)((residual[i]<<1)^(residual[i]>>31)) >> rice_parameter );
3924
return partition_bits;
3927
static FLaC__INLINE unsigned count_rice_bits_in_partition_(
3928
const unsigned rice_parameter,
3929
const unsigned partition_samples,
3930
const FLAC__uint64 abs_residual_partition_sum
3934
FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + /* actually could end up being FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN but err on side of 16bps */
3935
(1+rice_parameter) * partition_samples + /* 1 for unary stop bit + rice_parameter for the binary portion */
3938
(unsigned)(abs_residual_partition_sum >> (rice_parameter-1)) /* rice_parameter-1 because the real coder sign-folds instead of using a sign bit */
3939
: (unsigned)(abs_residual_partition_sum << 1) /* can't shift by negative number, so reverse */
3941
- (partition_samples >> 1)
3942
/* -(partition_samples>>1) to subtract out extra contributions to the abs_residual_partition_sum.
3943
* The actual number of bits used is closer to the sum(for all i in the partition) of abs(residual[i])>>(rice_parameter-1)
3944
* By using the abs_residual_partition sum, we also add in bits in the LSBs that would normally be shifted out.
3945
* So the subtraction term tries to guess how many extra bits were contributed.
3946
* If the LSBs are randomly distributed, this should average to 0.5 extra bits per sample.
3952
FLAC__bool set_partitioned_rice_(
3953
#ifdef EXACT_RICE_BITS_CALCULATION
3954
const FLAC__int32 residual[],
3956
const FLAC__uint64 abs_residual_partition_sums[],
3957
const unsigned raw_bits_per_partition[],
3958
const unsigned residual_samples,
3959
const unsigned predictor_order,
3960
const unsigned suggested_rice_parameter,
3961
const unsigned rice_parameter_limit,
3962
const unsigned rice_parameter_search_dist,
3963
const unsigned partition_order,
3964
const FLAC__bool search_for_escapes,
3965
FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
3969
unsigned rice_parameter, partition_bits;
3970
unsigned best_partition_bits, best_rice_parameter = 0;
3074
3971
unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
3075
3972
unsigned *parameters, *raw_bits;
3973
#ifdef ENABLE_RICE_PARAMETER_SEARCH
3974
unsigned min_rice_parameter, max_rice_parameter;
3976
(void)rice_parameter_search_dist;
3077
FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
3979
FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER);
3980
FLAC__ASSERT(rice_parameter_limit <= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER);
3079
3982
FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
3080
3983
parameters = partitioned_rice_contents->parameters;
3081
3984
raw_bits = partitioned_rice_contents->raw_bits;
3083
3986
if(partition_order == 0) {
3086
#ifndef NO_RICE_SEARCH
3987
best_partition_bits = (unsigned)(-1);
3988
#ifdef ENABLE_RICE_PARAMETER_SEARCH
3087
3989
if(rice_parameter_search_dist) {
3088
3990
if(suggested_rice_parameter < rice_parameter_search_dist)
3089
3991
min_rice_parameter = 0;
3091
3993
min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
3092
3994
max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
3093
if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3995
if(max_rice_parameter >= rice_parameter_limit) {
3094
3996
#ifdef DEBUG_VERBOSE
3095
fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3997
fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, rice_parameter_limit - 1);
3097
max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3999
max_rice_parameter = rice_parameter_limit - 1;
3101
4003
min_rice_parameter = max_rice_parameter = suggested_rice_parameter;
3103
best_partition_bits = 0xffffffff;
3104
4005
for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
3106
#ifdef VARIABLE_RICE_BITS
3107
#ifdef FLAC__SYMMETRIC_RICE
3108
partition_bits = (2+rice_parameter) * residual_samples;
3110
const unsigned rice_parameter_estimate = rice_parameter-1;
3111
partition_bits = (1+rice_parameter) * residual_samples;
3116
partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
3117
for(i = 0; i < residual_samples; i++) {
3118
#ifdef VARIABLE_RICE_BITS
3119
#ifdef FLAC__SYMMETRIC_RICE
3120
partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter);
3122
partition_bits += VARIABLE_RICE_BITS(abs_residual[i], rice_parameter_estimate);
3125
partition_bits += FLAC__bitbuffer_rice_bits(residual[i], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
3128
#ifndef NO_RICE_SEARCH
4007
rice_parameter = suggested_rice_parameter;
4009
#ifdef EXACT_RICE_BITS_CALCULATION
4010
partition_bits = count_rice_bits_in_partition_(rice_parameter, residual_samples, residual);
4012
partition_bits = count_rice_bits_in_partition_(rice_parameter, residual_samples, abs_residual_partition_sums[0]);
3129
4014
if(partition_bits < best_partition_bits) {
3130
4015
best_rice_parameter = rice_parameter;
3131
4016
best_partition_bits = partition_bits;
4018
#ifdef ENABLE_RICE_PARAMETER_SEARCH
3135
4021
if(search_for_escapes) {
3136
flat_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[0] * residual_samples;
3137
if(flat_bits <= best_partition_bits) {
4022
partition_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[0] * residual_samples;
4023
if(partition_bits <= best_partition_bits) {
3138
4024
raw_bits[0] = raw_bits_per_partition[0];
3139
best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
3140
best_partition_bits = flat_bits;
4025
best_rice_parameter = 0; /* will be converted to appropriate escape parameter later */
4026
best_partition_bits = partition_bits;
3143
4031
parameters[0] = best_rice_parameter;
3144
4032
bits_ += best_partition_bits;
3147
unsigned partition, residual_sample, save_residual_sample, partition_sample;
4035
unsigned partition, residual_sample;
3148
4036
unsigned partition_samples;
3149
4037
FLAC__uint64 mean, k;
3150
4038
const unsigned partitions = 1u << partition_order;
3180
4056
for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
3183
if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
4058
if(rice_parameter >= rice_parameter_limit) {
3184
4059
#ifdef DEBUG_VERBOSE
3185
fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4060
fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, rice_parameter_limit - 1);
3187
rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4062
rice_parameter = rice_parameter_limit - 1;
3190
#ifndef NO_RICE_SEARCH
4065
best_partition_bits = (unsigned)(-1);
4066
#ifdef ENABLE_RICE_PARAMETER_SEARCH
3191
4067
if(rice_parameter_search_dist) {
3192
4068
if(rice_parameter < rice_parameter_search_dist)
3193
4069
min_rice_parameter = 0;
3195
4071
min_rice_parameter = rice_parameter - rice_parameter_search_dist;
3196
4072
max_rice_parameter = rice_parameter + rice_parameter_search_dist;
3197
if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
4073
if(max_rice_parameter >= rice_parameter_limit) {
3198
4074
#ifdef DEBUG_VERBOSE
3199
fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
4075
fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, rice_parameter_limit - 1);
3201
max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
4077
max_rice_parameter = rice_parameter_limit - 1;
3205
4081
min_rice_parameter = max_rice_parameter = rice_parameter;
3207
best_partition_bits = 0xffffffff;
3208
4083
for(rice_parameter = min_rice_parameter; rice_parameter <= max_rice_parameter; rice_parameter++) {
3210
#ifdef VARIABLE_RICE_BITS
3211
#ifdef FLAC__SYMMETRIC_RICE
3212
partition_bits = (2+rice_parameter) * partition_samples;
3214
const unsigned rice_parameter_estimate = rice_parameter-1;
3215
partition_bits = (1+rice_parameter) * partition_samples;
3220
partition_bits += FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN;
3221
save_residual_sample = residual_sample;
3222
for(partition_sample = 0; partition_sample < partition_samples; residual_sample++, partition_sample++) {
3223
#ifdef VARIABLE_RICE_BITS
3224
#ifdef FLAC__SYMMETRIC_RICE
3225
partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter);
3227
partition_bits += VARIABLE_RICE_BITS(abs_residual[residual_sample], rice_parameter_estimate);
3230
partition_bits += FLAC__bitbuffer_rice_bits(residual[residual_sample], rice_parameter); /* NOTE: we will need to pass in residual[] instead of abs_residual[] */
3233
#ifndef NO_RICE_SEARCH
3234
if(rice_parameter != max_rice_parameter)
3235
residual_sample = save_residual_sample;
4085
#ifdef EXACT_RICE_BITS_CALCULATION
4086
partition_bits = count_rice_bits_in_partition_(rice_parameter, partition_samples, residual+residual_sample);
4088
partition_bits = count_rice_bits_in_partition_(rice_parameter, partition_samples, abs_residual_partition_sums[partition]);
3236
4090
if(partition_bits < best_partition_bits) {
3237
4091
best_rice_parameter = rice_parameter;
3238
4092
best_partition_bits = partition_bits;
4094
#ifdef ENABLE_RICE_PARAMETER_SEARCH
3242
4097
if(search_for_escapes) {
3243
flat_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[partition] * partition_samples;
3244
if(flat_bits <= best_partition_bits) {
4098
partition_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[partition] * partition_samples;
4099
if(partition_bits <= best_partition_bits) {
3245
4100
raw_bits[partition] = raw_bits_per_partition[partition];
3246
best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
3247
best_partition_bits = flat_bits;
4101
best_rice_parameter = 0; /* will be converted to appropriate escape parameter later */
4102
best_partition_bits = partition_bits;
4105
raw_bits[partition] = 0;
3250
4107
parameters[partition] = best_rice_parameter;
3251
4108
bits_ += best_partition_bits;
4109
residual_sample += partition_samples;
3392
4252
(void)decoder, (void)status;
3393
4253
encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
4256
FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
4260
*bytes = fread(buffer, 1, *bytes, encoder->private_->file);
4262
if (feof(encoder->private_->file))
4263
return FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM;
4264
else if (ferror(encoder->private_->file))
4265
return FLAC__STREAM_ENCODER_READ_STATUS_ABORT;
4267
return FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE;
4270
FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
4274
if(fseeko(encoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0)
4275
return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR;
4277
return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
4280
FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
4286
offset = ftello(encoder->private_->file);
4289
return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR;
4292
*absolute_byte_offset = (FLAC__uint64)offset;
4293
return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
4297
#ifdef FLAC__VALGRIND_TESTING
4298
static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
4300
size_t ret = fwrite(ptr, size, nmemb, stream);
4306
#define local__fwrite fwrite
4309
FLAC__StreamEncoderWriteStatus file_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, void *client_data)
4311
(void)client_data, (void)current_frame;
4313
if(local__fwrite(buffer, sizeof(FLAC__byte), bytes, encoder->private_->file) == bytes) {
4314
FLAC__bool call_it = 0 != encoder->private_->progress_callback && (
4316
/* We would like to be able to use 'samples > 0' in the
4317
* clause here but currently because of the nature of our
4318
* Ogg writing implementation, 'samples' is always 0 (see
4319
* ogg_encoder_aspect.c). The downside is extra progress
4322
encoder->private_->is_ogg? true :
4327
/* NOTE: We have to add +bytes, +samples, and +1 to the stats
4328
* because at this point in the callback chain, the stats
4329
* have not been updated. Only after we return and control
4330
* gets back to write_frame_() are the stats updated
4332
encoder->private_->progress_callback(encoder, encoder->private_->bytes_written+bytes, encoder->private_->samples_written+samples, encoder->private_->frames_written+(samples?1:0), encoder->private_->total_frames_estimate, encoder->private_->client_data);
4334
return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
4337
return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
4341
* This will forcibly set stdout to binary mode (for OSes that require it)
4343
FILE *get_binary_stdout_(void)
4345
/* if something breaks here it is probably due to the presence or
4346
* absence of an underscore before the identifiers 'setmode',
4347
* 'fileno', and/or 'O_BINARY'; check your system header files.
4349
#if defined _MSC_VER || defined __MINGW32__
4350
_setmode(_fileno(stdout), _O_BINARY);
4351
#elif defined __CYGWIN__
4352
/* almost certainly not needed for any modern Cygwin, but let's be safe... */
4353
setmode(_fileno(stdout), _O_BINARY);
4354
#elif defined __EMX__
4355
setmode(fileno(stdout), O_BINARY);