210
268
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,
271
static FLAC__bool set_partitioned_rice_(
272
#ifdef EXACT_RICE_BITS_CALCULATION
273
const FLAC__int32 residual[],
275
const FLAC__uint64 abs_residual_partition_sums[],
276
const unsigned raw_bits_per_partition[],
277
const unsigned residual_samples,
278
const unsigned predictor_order,
279
const unsigned suggested_rice_parameter,
280
const unsigned rice_parameter_search_dist,
281
const unsigned partition_order,
282
const FLAC__bool search_for_escapes,
283
FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
266
287
static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
422
443
FLAC_API const char * const FLAC__StreamEncoderStateString[] = {
423
444
"FLAC__STREAM_ENCODER_OK",
445
"FLAC__STREAM_ENCODER_UNINITIALIZED",
446
"FLAC__STREAM_ENCODER_OGG_ERROR",
424
447
"FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
425
448
"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",
449
"FLAC__STREAM_ENCODER_CLIENT_ERROR",
450
"FLAC__STREAM_ENCODER_IO_ERROR",
438
451
"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"
452
"FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR"
455
FLAC_API const char * const FLAC__StreamEncoderInitStatusString[] = {
456
"FLAC__STREAM_ENCODER_INIT_STATUS_OK",
457
"FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR",
458
"FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER",
459
"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS",
460
"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS",
461
"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE",
462
"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE",
463
"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE",
464
"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER",
465
"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION",
466
"FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
467
"FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE",
468
"FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA",
469
"FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED"
472
FLAC_API const char * const FLAC__treamEncoderReadStatusString[] = {
473
"FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE",
474
"FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM",
475
"FLAC__STREAM_ENCODER_READ_STATUS_ABORT",
476
"FLAC__STREAM_ENCODER_READ_STATUS_UNSUPPORTED"
447
479
FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = {
564
623
***********************************************************************/
566
FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder)
625
static FLAC__StreamEncoderInitStatus init_stream_internal_(
626
FLAC__StreamEncoder *encoder,
627
FLAC__StreamEncoderReadCallback read_callback,
628
FLAC__StreamEncoderWriteCallback write_callback,
629
FLAC__StreamEncoderSeekCallback seek_callback,
630
FLAC__StreamEncoderTellCallback tell_callback,
631
FLAC__StreamEncoderMetadataCallback metadata_callback,
569
FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment;
637
FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment, metadata_picture_has_type1, metadata_picture_has_type2;
571
639
FLAC__ASSERT(0 != encoder);
573
641
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;
642
return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
646
return FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER;
649
if(0 == write_callback || (seek_callback && 0 == tell_callback))
650
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS;
581
652
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;
653
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS;
655
if(encoder->protected_->channels != 2) {
656
encoder->protected_->do_mid_side_stereo = false;
657
encoder->protected_->loose_mid_side_stereo = false;
659
else if(!encoder->protected_->do_mid_side_stereo)
660
encoder->protected_->loose_mid_side_stereo = false;
590
662
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 */
663
encoder->protected_->do_mid_side_stereo = false; /* since we currenty do 32-bit math, the side channel would have 33 bps and overflow */
593
665
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;
666
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE;
596
668
if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
597
return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE;
669
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE;
671
if(encoder->protected_->blocksize == 0) {
672
if(encoder->protected_->max_lpc_order == 0)
673
encoder->protected_->blocksize = 1152;
675
encoder->protected_->blocksize = 4096;
599
678
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;
679
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE;
602
681
if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
603
return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER;
682
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER;
605
684
if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
606
return encoder->protected_->state = FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
685
return FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
608
687
if(encoder->protected_->qlp_coeff_precision == 0) {
609
688
if(encoder->protected_->bits_per_sample < 16) {
684
772
if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
685
773
encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
776
/* reorder metadata if necessary to ensure that any VORBIS_COMMENT is the first, according to the mapping spec */
777
if(is_ogg && 0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 1) {
779
for(i = 1; i < encoder->protected_->num_metadata_blocks; i++) {
780
if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
781
FLAC__StreamMetadata *vc = encoder->protected_->metadata[i];
783
encoder->protected_->metadata[i] = encoder->protected_->metadata[i-1];
784
encoder->protected_->metadata[0] = vc;
790
/* keep track of any SEEKTABLE block */
791
if(0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0) {
793
for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
794
if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
795
encoder->private_->seek_table = &encoder->protected_->metadata[i]->data.seek_table;
796
break; /* take only the first one */
687
801
/* validate metadata */
688
802
if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
689
return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
803
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
690
804
metadata_has_seektable = false;
691
805
metadata_has_vorbis_comment = false;
806
metadata_picture_has_type1 = false;
807
metadata_picture_has_type2 = false;
692
808
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) {
809
const FLAC__StreamMetadata *m = encoder->protected_->metadata[i];
810
if(m->type == FLAC__METADATA_TYPE_STREAMINFO)
811
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
812
else if(m->type == FLAC__METADATA_TYPE_SEEKTABLE) {
696
813
if(metadata_has_seektable) /* only one is allowed */
697
return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
814
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
698
815
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;
816
if(!FLAC__format_seektable_is_legal(&m->data.seek_table))
817
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
702
else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
819
else if(m->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
703
820
if(metadata_has_vorbis_comment) /* only one is allowed */
704
return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
821
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
705
822
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;
824
else if(m->type == FLAC__METADATA_TYPE_CUESHEET) {
825
if(!FLAC__format_cuesheet_is_legal(&m->data.cue_sheet, m->data.cue_sheet.is_cd, /*violation=*/0))
826
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
828
else if(m->type == FLAC__METADATA_TYPE_PICTURE) {
829
if(!FLAC__format_picture_is_legal(&m->data.picture, /*violation=*/0))
830
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
831
if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD) {
832
if(metadata_picture_has_type1) /* there should only be 1 per stream */
833
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
834
metadata_picture_has_type1 = true;
835
/* standard icon must be 32x32 pixel PNG */
837
m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD &&
839
(strcmp(m->data.picture.mime_type, "image/png") && strcmp(m->data.picture.mime_type, "-->")) ||
840
m->data.picture.width != 32 ||
841
m->data.picture.height != 32
844
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
846
else if(m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON) {
847
if(metadata_picture_has_type2) /* there should only be 1 per stream */
848
return FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA;
849
metadata_picture_has_type2 = true;
881
1056
if(encoder->protected_->verify)
882
1057
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 */
1058
encoder->private_->streaminfo.type = FLAC__METADATA_TYPE_STREAMINFO;
1059
encoder->private_->streaminfo.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
1060
encoder->private_->streaminfo.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
1061
encoder->private_->streaminfo.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
1062
encoder->private_->streaminfo.data.stream_info.max_blocksize = encoder->protected_->blocksize;
1063
encoder->private_->streaminfo.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
1064
encoder->private_->streaminfo.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
1065
encoder->private_->streaminfo.data.stream_info.sample_rate = encoder->protected_->sample_rate;
1066
encoder->private_->streaminfo.data.stream_info.channels = encoder->protected_->channels;
1067
encoder->private_->streaminfo.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
1068
encoder->private_->streaminfo.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
1069
memset(encoder->private_->streaminfo.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
895
1070
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)) {
1071
if(!FLAC__add_metadata_block(&encoder->private_->streaminfo, encoder->private_->frame)) {
1072
encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1073
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1075
if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
901
1076
/* the above function sets the state for us in case of an error */
902
return encoder->protected_->state;
1077
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
906
1081
* Now that the STREAMINFO block is written, we can init this to an
907
1082
* absurdly-high value...
909
encoder->private_->metadata.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
1084
encoder->private_->streaminfo.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
910
1085
/* ... and clear this to 0 */
911
encoder->private_->metadata.data.stream_info.total_samples = 0;
1086
encoder->private_->streaminfo.data.stream_info.total_samples = 0;
914
1089
* Check to see if the supplied metadata contains a VORBIS_COMMENT;
915
1090
* if not, we will write an empty one (FLAC__add_metadata_block()
916
1091
* 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.)
1093
* WATCHOUT: the Ogg FLAC mapping requires us to write this block after
1094
* the STREAMINFO. (In the case that metadata_has_vorbis_comment is
1095
* true it will have already insured that the metadata list is properly
923
1098
if(!metadata_has_vorbis_comment) {
924
1099
FLAC__StreamMetadata vorbis_comment;
945
1120
for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
946
1121
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)) {
1122
if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame)) {
1123
encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
1124
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1126
if(!write_bitbuffer_(encoder, 0, /*is_last_block=*/false)) {
952
1127
/* the above function sets the state for us in case of an error */
953
return encoder->protected_->state;
1128
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1132
/* now that all the metadata is written, we save the stream offset */
1133
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 */
1134
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
1135
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
957
1138
if(encoder->protected_->verify)
958
1139
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);
1141
return FLAC__STREAM_ENCODER_INIT_STATUS_OK;
1144
FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_stream(
1145
FLAC__StreamEncoder *encoder,
1146
FLAC__StreamEncoderWriteCallback write_callback,
1147
FLAC__StreamEncoderSeekCallback seek_callback,
1148
FLAC__StreamEncoderTellCallback tell_callback,
1149
FLAC__StreamEncoderMetadataCallback metadata_callback,
1153
return init_stream_internal_(
1155
/*read_callback=*/0,
1165
FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_stream(
1166
FLAC__StreamEncoder *encoder,
1167
FLAC__StreamEncoderReadCallback read_callback,
1168
FLAC__StreamEncoderWriteCallback write_callback,
1169
FLAC__StreamEncoderSeekCallback seek_callback,
1170
FLAC__StreamEncoderTellCallback tell_callback,
1171
FLAC__StreamEncoderMetadataCallback metadata_callback,
1175
return init_stream_internal_(
1187
static FLAC__StreamEncoderInitStatus init_FILE_internal_(
1188
FLAC__StreamEncoder *encoder,
1190
FLAC__StreamEncoderProgressCallback progress_callback,
1195
FLAC__StreamEncoderInitStatus init_status;
1197
FLAC__ASSERT(0 != encoder);
1198
FLAC__ASSERT(0 != file);
1200
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1201
return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1203
/* double protection */
1205
encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1206
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1210
* To make sure that our file does not go unclosed after an error, we
1211
* must assign the FILE pointer before any further error can occur in
1215
file = get_binary_stdout_(); /* just to be safe */
1217
encoder->private_->file = file;
1219
encoder->private_->progress_callback = progress_callback;
1220
encoder->private_->bytes_written = 0;
1221
encoder->private_->samples_written = 0;
1222
encoder->private_->frames_written = 0;
1224
init_status = init_stream_internal_(
1226
encoder->private_->file == stdout? 0 : is_ogg? file_read_callback_ : 0,
1227
file_write_callback_,
1228
encoder->private_->file == stdout? 0 : file_seek_callback_,
1229
encoder->private_->file == stdout? 0 : file_tell_callback_,
1230
/*metadata_callback=*/0,
1234
if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) {
1235
/* the above function sets the state for us in case of an error */
1240
unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
1242
FLAC__ASSERT(blocksize != 0);
1243
encoder->private_->total_frames_estimate = (unsigned)((FLAC__stream_encoder_get_total_samples_estimate(encoder) + blocksize - 1) / blocksize);
1249
FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_FILE(
1250
FLAC__StreamEncoder *encoder,
1252
FLAC__StreamEncoderProgressCallback progress_callback,
1256
return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/false);
1259
FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_FILE(
1260
FLAC__StreamEncoder *encoder,
1262
FLAC__StreamEncoderProgressCallback progress_callback,
1266
return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/true);
1269
static FLAC__StreamEncoderInitStatus init_file_internal_(
1270
FLAC__StreamEncoder *encoder,
1271
const char *filename,
1272
FLAC__StreamEncoderProgressCallback progress_callback,
1279
FLAC__ASSERT(0 != encoder);
1282
* To make sure that our file does not go unclosed after an error, we
1283
* have to do the same entrance checks here that are later performed
1284
* in FLAC__stream_encoder_init_FILE() before the FILE* is assigned.
1286
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1287
return FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED;
1289
file = filename? fopen(filename, "w+b") : stdout;
1292
encoder->protected_->state = FLAC__STREAM_ENCODER_IO_ERROR;
1293
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
1296
return init_FILE_internal_(encoder, file, progress_callback, client_data, is_ogg);
1299
FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_file(
1300
FLAC__StreamEncoder *encoder,
1301
const char *filename,
1302
FLAC__StreamEncoderProgressCallback progress_callback,
1306
return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/false);
1309
FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_file(
1310
FLAC__StreamEncoder *encoder,
1311
const char *filename,
1312
FLAC__StreamEncoderProgressCallback progress_callback,
1316
return init_file_internal_(encoder, filename, progress_callback, client_data, /*is_ogg=*/true);
1319
FLAC_API FLAC__bool FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
1321
FLAC__bool error = false;
1323
FLAC__ASSERT(0 != encoder);
1324
FLAC__ASSERT(0 != encoder->private_);
1325
FLAC__ASSERT(0 != encoder->protected_);
967
1327
if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
970
1330
if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
971
1331
if(encoder->private_->current_sample_number != 0) {
1332
const FLAC__bool is_fractional_block = encoder->protected_->blocksize != encoder->private_->current_sample_number;
972
1333
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);
1334
if(!process_frame_(encoder, is_fractional_block, /*is_last_block=*/true))
1339
FLAC__MD5Final(encoder->private_->streaminfo.data.stream_info.md5sum, &encoder->private_->md5context);
1341
if(!encoder->private_->is_being_deleted) {
1342
if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK) {
1343
if(encoder->private_->seek_callback) {
1345
if(encoder->private_->is_ogg)
1346
update_ogg_metadata_(encoder);
1349
update_metadata_(encoder);
1351
/* check if an error occurred while updating metadata */
1352
if(encoder->protected_->state != FLAC__STREAM_ENCODER_OK)
1355
if(encoder->private_->metadata_callback)
1356
encoder->private_->metadata_callback(encoder, &encoder->private_->streaminfo, encoder->private_->client_data);
1359
if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder && !FLAC__stream_decoder_finish(encoder->private_->verify.decoder)) {
1361
encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA;
1366
if(0 != encoder->private_->file) {
1367
if(encoder->private_->file != stdout)
1368
fclose(encoder->private_->file);
1369
encoder->private_->file = 0;
1373
if(encoder->private_->is_ogg)
1374
FLAC__ogg_encoder_aspect_finish(&encoder->protected_->ogg_encoder_aspect);
987
1378
set_defaults_(encoder);
989
encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
1381
encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
1386
FLAC_API FLAC__bool FLAC__stream_encoder_set_ogg_serial_number(FLAC__StreamEncoder *encoder, long value)
1388
FLAC__ASSERT(0 != encoder);
1389
FLAC__ASSERT(0 != encoder->private_);
1390
FLAC__ASSERT(0 != encoder->protected_);
1391
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1394
/* can't check encoder->private_->is_ogg since that's not set until init time */
1395
FLAC__ogg_encoder_aspect_set_serial_number(&encoder->protected_->ogg_encoder_aspect, value);
992
1403
FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
994
1405
FLAC__ASSERT(0 != encoder);
1406
FLAC__ASSERT(0 != encoder->private_);
1407
FLAC__ASSERT(0 != encoder->protected_);
995
1408
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
997
1410
#ifndef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
1048
1449
FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
1050
1451
FLAC__ASSERT(0 != encoder);
1452
FLAC__ASSERT(0 != encoder->private_);
1453
FLAC__ASSERT(0 != encoder->protected_);
1051
1454
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1053
1456
encoder->protected_->sample_rate = value;
1460
FLAC_API FLAC__bool FLAC__stream_encoder_set_compression_level(FLAC__StreamEncoder *encoder, unsigned value)
1462
FLAC__bool ok = true;
1463
FLAC__ASSERT(0 != encoder);
1464
FLAC__ASSERT(0 != encoder->private_);
1465
FLAC__ASSERT(0 != encoder->protected_);
1466
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1468
if(value >= sizeof(compression_levels_)/sizeof(compression_levels_[0]))
1469
value = sizeof(compression_levels_)/sizeof(compression_levels_[0]) - 1;
1470
ok &= FLAC__stream_encoder_set_do_mid_side_stereo (encoder, compression_levels_[value].do_mid_side_stereo);
1471
ok &= FLAC__stream_encoder_set_loose_mid_side_stereo (encoder, compression_levels_[value].loose_mid_side_stereo);
1474
ok &= FLAC__stream_encoder_set_apodization (encoder, compression_levels_[value].apodization);
1475
/* but it's too hard to specify the string in a locale-specific way */
1477
encoder->protected_->num_apodizations = 1;
1478
encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
1479
encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
1481
ok &= FLAC__stream_encoder_set_max_lpc_order (encoder, compression_levels_[value].max_lpc_order);
1482
ok &= FLAC__stream_encoder_set_qlp_coeff_precision (encoder, compression_levels_[value].qlp_coeff_precision);
1483
ok &= FLAC__stream_encoder_set_do_qlp_coeff_prec_search (encoder, compression_levels_[value].do_qlp_coeff_prec_search);
1484
ok &= FLAC__stream_encoder_set_do_escape_coding (encoder, compression_levels_[value].do_escape_coding);
1485
ok &= FLAC__stream_encoder_set_do_exhaustive_model_search (encoder, compression_levels_[value].do_exhaustive_model_search);
1486
ok &= FLAC__stream_encoder_set_min_residual_partition_order(encoder, compression_levels_[value].min_residual_partition_order);
1487
ok &= FLAC__stream_encoder_set_max_residual_partition_order(encoder, compression_levels_[value].max_residual_partition_order);
1488
ok &= FLAC__stream_encoder_set_rice_parameter_search_dist (encoder, compression_levels_[value].rice_parameter_search_dist);
1057
1492
FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
1059
1494
FLAC__ASSERT(0 != encoder);
1495
FLAC__ASSERT(0 != encoder->private_);
1496
FLAC__ASSERT(0 != encoder->protected_);
1060
1497
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1062
1499
encoder->protected_->blocksize = value;
1503
FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
1505
FLAC__ASSERT(0 != encoder);
1506
FLAC__ASSERT(0 != encoder->private_);
1507
FLAC__ASSERT(0 != encoder->protected_);
1508
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1510
encoder->protected_->do_mid_side_stereo = value;
1514
FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_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_->loose_mid_side_stereo = value;
1525
FLAC_API FLAC__bool FLAC__stream_encoder_set_apodization(FLAC__StreamEncoder *encoder, const char *specification)
1527
FLAC__ASSERT(0 != encoder);
1528
FLAC__ASSERT(0 != encoder->private_);
1529
FLAC__ASSERT(0 != encoder->protected_);
1530
FLAC__ASSERT(0 != specification);
1531
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1533
#ifdef FLAC__INTEGER_ONLY_LIBRARY
1534
(void)specification; /* silently ignore since we haven't integerized; will always use a rectangular window */
1536
encoder->protected_->num_apodizations = 0;
1538
const char *s = strchr(specification, ';');
1539
const size_t n = s? (size_t)(s - specification) : strlen(specification);
1540
if (n==8 && 0 == strncmp("bartlett" , specification, n))
1541
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT;
1542
else if(n==13 && 0 == strncmp("bartlett_hann", specification, n))
1543
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BARTLETT_HANN;
1544
else if(n==8 && 0 == strncmp("blackman" , specification, n))
1545
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN;
1546
else if(n==26 && 0 == strncmp("blackman_harris_4term_92db", specification, n))
1547
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE;
1548
else if(n==6 && 0 == strncmp("connes" , specification, n))
1549
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_CONNES;
1550
else if(n==7 && 0 == strncmp("flattop" , specification, n))
1551
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_FLATTOP;
1552
else if(n>7 && 0 == strncmp("gauss(" , specification, 6)) {
1553
FLAC__real stddev = (FLAC__real)strtod(specification+6, 0);
1554
if (stddev > 0.0 && stddev <= 0.5) {
1555
encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.gauss.stddev = stddev;
1556
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_GAUSS;
1559
else if(n==7 && 0 == strncmp("hamming" , specification, n))
1560
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HAMMING;
1561
else if(n==4 && 0 == strncmp("hann" , specification, n))
1562
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_HANN;
1563
else if(n==13 && 0 == strncmp("kaiser_bessel", specification, n))
1564
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_KAISER_BESSEL;
1565
else if(n==7 && 0 == strncmp("nuttall" , specification, n))
1566
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_NUTTALL;
1567
else if(n==9 && 0 == strncmp("rectangle" , specification, n))
1568
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_RECTANGLE;
1569
else if(n==8 && 0 == strncmp("triangle" , specification, n))
1570
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TRIANGLE;
1571
else if(n>7 && 0 == strncmp("tukey(" , specification, 6)) {
1572
FLAC__real p = (FLAC__real)strtod(specification+6, 0);
1573
if (p >= 0.0 && p <= 1.0) {
1574
encoder->protected_->apodizations[encoder->protected_->num_apodizations].parameters.tukey.p = p;
1575
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_TUKEY;
1578
else if(n==5 && 0 == strncmp("welch" , specification, n))
1579
encoder->protected_->apodizations[encoder->protected_->num_apodizations++].type = FLAC__APODIZATION_WELCH;
1580
if (encoder->protected_->num_apodizations == 32)
1583
specification = s+1;
1587
if(encoder->protected_->num_apodizations == 0) {
1588
encoder->protected_->num_apodizations = 1;
1589
encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
1590
encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
1066
1596
FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
1068
1598
FLAC__ASSERT(0 != encoder);
1599
FLAC__ASSERT(0 != encoder->private_);
1600
FLAC__ASSERT(0 != encoder->protected_);
1069
1601
if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
1071
1603
encoder->protected_->max_lpc_order = value;
1267
1823
FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
1269
1825
FLAC__ASSERT(0 != encoder);
1826
FLAC__ASSERT(0 != encoder->private_);
1827
FLAC__ASSERT(0 != encoder->protected_);
1270
1828
return encoder->protected_->verify;
1273
1831
FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
1275
1833
FLAC__ASSERT(0 != encoder);
1834
FLAC__ASSERT(0 != encoder->private_);
1835
FLAC__ASSERT(0 != encoder->protected_);
1276
1836
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;
1291
1839
FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
1293
1841
FLAC__ASSERT(0 != encoder);
1842
FLAC__ASSERT(0 != encoder->private_);
1843
FLAC__ASSERT(0 != encoder->protected_);
1294
1844
return encoder->protected_->channels;
1297
1847
FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
1299
1849
FLAC__ASSERT(0 != encoder);
1850
FLAC__ASSERT(0 != encoder->private_);
1851
FLAC__ASSERT(0 != encoder->protected_);
1300
1852
return encoder->protected_->bits_per_sample;
1303
1855
FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
1305
1857
FLAC__ASSERT(0 != encoder);
1858
FLAC__ASSERT(0 != encoder->private_);
1859
FLAC__ASSERT(0 != encoder->protected_);
1306
1860
return encoder->protected_->sample_rate;
1309
1863
FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
1311
1865
FLAC__ASSERT(0 != encoder);
1866
FLAC__ASSERT(0 != encoder->private_);
1867
FLAC__ASSERT(0 != encoder->protected_);
1312
1868
return encoder->protected_->blocksize;
1871
FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1873
FLAC__ASSERT(0 != encoder);
1874
FLAC__ASSERT(0 != encoder->private_);
1875
FLAC__ASSERT(0 != encoder->protected_);
1876
return encoder->protected_->do_mid_side_stereo;
1879
FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
1881
FLAC__ASSERT(0 != encoder);
1882
FLAC__ASSERT(0 != encoder->private_);
1883
FLAC__ASSERT(0 != encoder->protected_);
1884
return encoder->protected_->loose_mid_side_stereo;
1315
1887
FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
1317
1889
FLAC__ASSERT(0 != encoder);
1890
FLAC__ASSERT(0 != encoder->private_);
1891
FLAC__ASSERT(0 != encoder->protected_);
1318
1892
return encoder->protected_->max_lpc_order;
1321
1895
FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
1323
1897
FLAC__ASSERT(0 != encoder);
1898
FLAC__ASSERT(0 != encoder->private_);
1899
FLAC__ASSERT(0 != encoder->protected_);
1324
1900
return encoder->protected_->qlp_coeff_precision;
1327
1903
FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
1329
1905
FLAC__ASSERT(0 != encoder);
1906
FLAC__ASSERT(0 != encoder->private_);
1907
FLAC__ASSERT(0 != encoder->protected_);
1330
1908
return encoder->protected_->do_qlp_coeff_prec_search;
1333
1911
FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
1335
1913
FLAC__ASSERT(0 != encoder);
1914
FLAC__ASSERT(0 != encoder->private_);
1915
FLAC__ASSERT(0 != encoder->protected_);
1336
1916
return encoder->protected_->do_escape_coding;
1339
1919
FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
1341
1921
FLAC__ASSERT(0 != encoder);
1922
FLAC__ASSERT(0 != encoder->private_);
1923
FLAC__ASSERT(0 != encoder->protected_);
1342
1924
return encoder->protected_->do_exhaustive_model_search;
1345
1927
FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
1347
1929
FLAC__ASSERT(0 != encoder);
1930
FLAC__ASSERT(0 != encoder->private_);
1931
FLAC__ASSERT(0 != encoder->protected_);
1348
1932
return encoder->protected_->min_residual_partition_order;
1351
1935
FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
1353
1937
FLAC__ASSERT(0 != encoder);
1938
FLAC__ASSERT(0 != encoder->private_);
1939
FLAC__ASSERT(0 != encoder->protected_);
1354
1940
return encoder->protected_->max_residual_partition_order;
1357
1943
FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
1359
1945
FLAC__ASSERT(0 != encoder);
1946
FLAC__ASSERT(0 != encoder->private_);
1947
FLAC__ASSERT(0 != encoder->protected_);
1360
1948
return encoder->protected_->rice_parameter_search_dist;
1363
1951
FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
1365
1953
FLAC__ASSERT(0 != encoder);
1954
FLAC__ASSERT(0 != encoder->private_);
1955
FLAC__ASSERT(0 != encoder->protected_);
1366
1956
return encoder->protected_->total_samples_estimate;
1754
2478
/* WATCHOUT: FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx()
1755
2479
* requires that the input arrays (in our case the integer signals)
1756
2480
* 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.
2481
* alignment purposes; we use 4 in front to keep the data well-aligned.
1760
2484
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]);
2485
ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize+4+OVERREAD_, &encoder->private_->integer_signal_unaligned[i], &encoder->private_->integer_signal[i]);
1766
2486
memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
1767
2487
encoder->private_->integer_signal[i] += 4;
2488
#ifndef FLAC__INTEGER_ONLY_LIBRARY
2489
if(encoder->protected_->max_lpc_order > 0)
2490
ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize+OVERREAD_, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
1769
2493
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]);
2494
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
2495
memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
1776
2496
encoder->private_->integer_signal_mid_side[i] += 4;
2497
#ifndef FLAC__INTEGER_ONLY_LIBRARY
2498
if(encoder->protected_->max_lpc_order > 0)
2499
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]);
2502
#ifndef FLAC__INTEGER_ONLY_LIBRARY
2503
if(ok && encoder->protected_->max_lpc_order > 0) {
2504
for(i = 0; ok && i < encoder->protected_->num_apodizations; i++)
2505
ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize, &encoder->private_->window_unaligned[i], &encoder->private_->window[i]);
2506
ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize, &encoder->private_->windowed_signal_unaligned, &encoder->private_->windowed_signal);
1778
2509
for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
1779
2510
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]);
2511
ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]);
1783
2514
for(channel = 0; ok && channel < 2; channel++) {
1784
2515
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]);
2516
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);
2519
/* the *2 is an approximation to the series 1 + 1/2 + 1/4 + ... that sums tree occupies in a flat array */
2520
/*@@@ 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) */
2521
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
2522
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);
2523
ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_blocksize * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition);
2525
/* now adjust the windows if the blocksize has changed */
2526
#ifndef FLAC__INTEGER_ONLY_LIBRARY
2527
if(ok && new_blocksize != encoder->private_->input_capacity && encoder->protected_->max_lpc_order > 0) {
2528
for(i = 0; ok && i < encoder->protected_->num_apodizations; i++) {
2529
switch(encoder->protected_->apodizations[i].type) {
2530
case FLAC__APODIZATION_BARTLETT:
2531
FLAC__window_bartlett(encoder->private_->window[i], new_blocksize);
2533
case FLAC__APODIZATION_BARTLETT_HANN:
2534
FLAC__window_bartlett_hann(encoder->private_->window[i], new_blocksize);
2536
case FLAC__APODIZATION_BLACKMAN:
2537
FLAC__window_blackman(encoder->private_->window[i], new_blocksize);
2539
case FLAC__APODIZATION_BLACKMAN_HARRIS_4TERM_92DB_SIDELOBE:
2540
FLAC__window_blackman_harris_4term_92db_sidelobe(encoder->private_->window[i], new_blocksize);
2542
case FLAC__APODIZATION_CONNES:
2543
FLAC__window_connes(encoder->private_->window[i], new_blocksize);
2545
case FLAC__APODIZATION_FLATTOP:
2546
FLAC__window_flattop(encoder->private_->window[i], new_blocksize);
2548
case FLAC__APODIZATION_GAUSS:
2549
FLAC__window_gauss(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.gauss.stddev);
2551
case FLAC__APODIZATION_HAMMING:
2552
FLAC__window_hamming(encoder->private_->window[i], new_blocksize);
2554
case FLAC__APODIZATION_HANN:
2555
FLAC__window_hann(encoder->private_->window[i], new_blocksize);
2557
case FLAC__APODIZATION_KAISER_BESSEL:
2558
FLAC__window_kaiser_bessel(encoder->private_->window[i], new_blocksize);
2560
case FLAC__APODIZATION_NUTTALL:
2561
FLAC__window_nuttall(encoder->private_->window[i], new_blocksize);
2563
case FLAC__APODIZATION_RECTANGLE:
2564
FLAC__window_rectangle(encoder->private_->window[i], new_blocksize);
2566
case FLAC__APODIZATION_TRIANGLE:
2567
FLAC__window_triangle(encoder->private_->window[i], new_blocksize);
2569
case FLAC__APODIZATION_TUKEY:
2570
FLAC__window_tukey(encoder->private_->window[i], new_blocksize, encoder->protected_->apodizations[i].parameters.tukey.p);
2572
case FLAC__APODIZATION_WELCH:
2573
FLAC__window_welch(encoder->private_->window[i], new_blocksize);
2577
/* double protection */
2578
FLAC__window_hann(encoder->private_->window[i], new_blocksize);
1795
encoder->private_->input_capacity = new_size;
2586
encoder->private_->input_capacity = new_blocksize;
1797
2588
encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1802
FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples)
2593
FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples, FLAC__bool is_last_block)
1804
2595
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);
2598
FLAC__ASSERT(FLAC__bitwriter_is_byte_aligned(encoder->private_->frame));
2600
if(!FLAC__bitwriter_get_buffer(encoder->private_->frame, &buffer, &bytes)) {
2601
encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
1811
2605
if(encoder->protected_->verify) {
1812
2606
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;
2622
if(write_frame_(encoder, buffer, bytes, samples, is_last_block) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2623
FLAC__bitwriter_release_buffer(encoder->private_->frame);
2624
FLAC__bitwriter_clear(encoder->private_->frame);
2625
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
1833
FLAC__bitbuffer_release_buffer(encoder->private_->frame);
2629
FLAC__bitwriter_release_buffer(encoder->private_->frame);
2630
FLAC__bitwriter_clear(encoder->private_->frame);
1835
2632
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);
2633
encoder->private_->streaminfo.data.stream_info.min_framesize = min(bytes, encoder->private_->streaminfo.data.stream_info.min_framesize);
2634
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)
2640
FLAC__StreamEncoderWriteStatus write_frame_(FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, FLAC__bool is_last_block)
2642
FLAC__StreamEncoderWriteStatus status;
2643
FLAC__uint64 output_position = 0;
2645
/* FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED just means we didn't get the offset; no error */
2646
if(encoder->private_->tell_callback && encoder->private_->tell_callback(encoder, &output_position, encoder->private_->client_data) == FLAC__STREAM_ENCODER_TELL_STATUS_ERROR) {
2647
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2648
return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
2652
* Watch for the STREAMINFO block and first SEEKTABLE block to go by and store their offsets.
2655
FLAC__MetadataType type = (buffer[0] & 0x7f);
2656
if(type == FLAC__METADATA_TYPE_STREAMINFO)
2657
encoder->protected_->streaminfo_offset = output_position;
2658
else if(type == FLAC__METADATA_TYPE_SEEKTABLE && encoder->protected_->seektable_offset == 0)
2659
encoder->protected_->seektable_offset = output_position;
2663
* Mark the current seek point if hit (if audio_offset == 0 that
2664
* means we're still writing metadata and haven't hit the first
2667
if(0 != encoder->private_->seek_table && encoder->protected_->audio_offset > 0 && encoder->private_->seek_table->num_points > 0) {
2668
const unsigned blocksize = FLAC__stream_encoder_get_blocksize(encoder);
2669
const FLAC__uint64 frame_first_sample = encoder->private_->samples_written;
2670
const FLAC__uint64 frame_last_sample = frame_first_sample + (FLAC__uint64)blocksize - 1;
2671
FLAC__uint64 test_sample;
2673
for(i = encoder->private_->first_seekpoint_to_check; i < encoder->private_->seek_table->num_points; i++) {
2674
test_sample = encoder->private_->seek_table->points[i].sample_number;
2675
if(test_sample > frame_last_sample) {
2678
else if(test_sample >= frame_first_sample) {
2679
encoder->private_->seek_table->points[i].sample_number = frame_first_sample;
2680
encoder->private_->seek_table->points[i].stream_offset = output_position - encoder->protected_->audio_offset;
2681
encoder->private_->seek_table->points[i].frame_samples = blocksize;
2682
encoder->private_->first_seekpoint_to_check++;
2683
/* DO NOT: "break;" and here's why:
2684
* The seektable template may contain more than one target
2685
* sample for any given frame; we will keep looping, generating
2686
* duplicate seekpoints for them, and we'll clean it up later,
2687
* just before writing the seektable back to the metadata.
2691
encoder->private_->first_seekpoint_to_check++;
2697
if(encoder->private_->is_ogg) {
2698
status = FLAC__ogg_encoder_aspect_write_callback_wrapper(
2699
&encoder->protected_->ogg_encoder_aspect,
2703
encoder->private_->current_frame_number,
2705
(FLAC__OggEncoderAspectWriteCallbackProxy)encoder->private_->write_callback,
2707
encoder->private_->client_data
2712
status = encoder->private_->write_callback(encoder, buffer, bytes, samples, encoder->private_->current_frame_number, encoder->private_->client_data);
2714
if(status == FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2715
encoder->private_->bytes_written += bytes;
2716
encoder->private_->samples_written += samples;
2717
/* we keep a high watermark on the number of frames written because
2718
* when the encoder goes back to write metadata, 'current_frame'
2719
* will drop back to 0.
2721
encoder->private_->frames_written = max(encoder->private_->frames_written, encoder->private_->current_frame_number+1);
2724
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2729
/* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */
2730
void update_metadata_(const FLAC__StreamEncoder *encoder)
2732
FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
2733
const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
2734
const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
2735
const unsigned min_framesize = metadata->data.stream_info.min_framesize;
2736
const unsigned max_framesize = metadata->data.stream_info.max_framesize;
2737
const unsigned bps = metadata->data.stream_info.bits_per_sample;
2738
FLAC__StreamEncoderSeekStatus seek_status;
2740
FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
2742
/* All this is based on intimate knowledge of the stream header
2743
* layout, but a change to the header format that would break this
2744
* would also break all streams encoded in the previous format.
2748
* Write MD5 signature
2751
const unsigned md5_offset =
2752
FLAC__STREAM_METADATA_HEADER_LENGTH +
2754
FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2755
FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2756
FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2757
FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2758
FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2759
FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2760
FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
2761
FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
2764
if((seek_status = encoder->private_->seek_callback(encoder, encoder->protected_->streaminfo_offset + md5_offset, encoder->private_->client_data)) != FLAC__STREAM_ENCODER_SEEK_STATUS_OK) {
2765
if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2766
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2769
if(encoder->private_->write_callback(encoder, metadata->data.stream_info.md5sum, 16, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2770
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2776
* Write total samples
2779
const unsigned total_samples_byte_offset =
2780
FLAC__STREAM_METADATA_HEADER_LENGTH +
2782
FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2783
FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2784
FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2785
FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2786
FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2787
FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2788
FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
2792
b[0] = ((FLAC__byte)(bps-1) << 4) | (FLAC__byte)((samples >> 32) & 0x0F);
2793
b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
2794
b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
2795
b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
2796
b[4] = (FLAC__byte)(samples & 0xFF);
2797
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) {
2798
if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2799
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2802
if(encoder->private_->write_callback(encoder, b, 5, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2803
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2809
* Write min/max framesize
2812
const unsigned min_framesize_offset =
2813
FLAC__STREAM_METADATA_HEADER_LENGTH +
2815
FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2816
FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
2819
b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
2820
b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
2821
b[2] = (FLAC__byte)(min_framesize & 0xFF);
2822
b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
2823
b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
2824
b[5] = (FLAC__byte)(max_framesize & 0xFF);
2825
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) {
2826
if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2827
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2830
if(encoder->private_->write_callback(encoder, b, 6, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2831
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2839
if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
2842
FLAC__format_seektable_sort(encoder->private_->seek_table);
2844
FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
2846
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) {
2847
if(seek_status == FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR)
2848
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2852
for(i = 0; i < encoder->private_->seek_table->num_points; i++) {
2855
xx = encoder->private_->seek_table->points[i].sample_number;
2856
b[7] = (FLAC__byte)xx; xx >>= 8;
2857
b[6] = (FLAC__byte)xx; xx >>= 8;
2858
b[5] = (FLAC__byte)xx; xx >>= 8;
2859
b[4] = (FLAC__byte)xx; xx >>= 8;
2860
b[3] = (FLAC__byte)xx; xx >>= 8;
2861
b[2] = (FLAC__byte)xx; xx >>= 8;
2862
b[1] = (FLAC__byte)xx; xx >>= 8;
2863
b[0] = (FLAC__byte)xx; xx >>= 8;
2864
xx = encoder->private_->seek_table->points[i].stream_offset;
2865
b[15] = (FLAC__byte)xx; xx >>= 8;
2866
b[14] = (FLAC__byte)xx; xx >>= 8;
2867
b[13] = (FLAC__byte)xx; xx >>= 8;
2868
b[12] = (FLAC__byte)xx; xx >>= 8;
2869
b[11] = (FLAC__byte)xx; xx >>= 8;
2870
b[10] = (FLAC__byte)xx; xx >>= 8;
2871
b[9] = (FLAC__byte)xx; xx >>= 8;
2872
b[8] = (FLAC__byte)xx; xx >>= 8;
2873
x = encoder->private_->seek_table->points[i].frame_samples;
2874
b[17] = (FLAC__byte)x; x >>= 8;
2875
b[16] = (FLAC__byte)x; x >>= 8;
2876
if(encoder->private_->write_callback(encoder, b, 18, 0, 0, encoder->private_->client_data) != FLAC__STREAM_ENCODER_WRITE_STATUS_OK) {
2877
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
2885
/* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */
2886
void update_ogg_metadata_(FLAC__StreamEncoder *encoder)
2888
/* the # of bytes in the 1st packet that precede the STREAMINFO */
2889
static const unsigned FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH =
2890
FLAC__OGG_MAPPING_PACKET_TYPE_LENGTH +
2891
FLAC__OGG_MAPPING_MAGIC_LENGTH +
2892
FLAC__OGG_MAPPING_VERSION_MAJOR_LENGTH +
2893
FLAC__OGG_MAPPING_VERSION_MINOR_LENGTH +
2894
FLAC__OGG_MAPPING_NUM_HEADERS_LENGTH +
2895
FLAC__STREAM_SYNC_LENGTH
2897
FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
2898
const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
2899
const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
2900
const unsigned min_framesize = metadata->data.stream_info.min_framesize;
2901
const unsigned max_framesize = metadata->data.stream_info.max_framesize;
2904
FLAC__ASSERT(metadata->type == FLAC__METADATA_TYPE_STREAMINFO);
2905
FLAC__ASSERT(0 != encoder->private_->seek_callback);
2907
/* Pre-check that client supports seeking, since we don't want the
2908
* ogg_helper code to ever have to deal with this condition.
2910
if(encoder->private_->seek_callback(encoder, 0, encoder->private_->client_data) == FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED)
2913
/* All this is based on intimate knowledge of the stream header
2914
* layout, but a change to the header format that would break this
2915
* would also break all streams encoded in the previous format.
2919
** Write STREAMINFO stats
2921
simple_ogg_page__init(&page);
2922
if(!simple_ogg_page__get_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
2923
simple_ogg_page__clear(&page);
2924
return; /* state already set */
2928
* Write MD5 signature
2931
const unsigned md5_offset =
2932
FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
2933
FLAC__STREAM_METADATA_HEADER_LENGTH +
2935
FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2936
FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2937
FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2938
FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2939
FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2940
FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2941
FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN +
2942
FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN
2945
if(md5_offset + 16 > (unsigned)page.body_len) {
2946
encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2947
simple_ogg_page__clear(&page);
2950
memcpy(page.body + md5_offset, metadata->data.stream_info.md5sum, 16);
2954
* Write total samples
2957
const unsigned total_samples_byte_offset =
2958
FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
2959
FLAC__STREAM_METADATA_HEADER_LENGTH +
2961
FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2962
FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN +
2963
FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN +
2964
FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN +
2965
FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN +
2966
FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN +
2967
FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN
2971
if(total_samples_byte_offset + 5 > (unsigned)page.body_len) {
2972
encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2973
simple_ogg_page__clear(&page);
2976
b[0] = (FLAC__byte)page.body[total_samples_byte_offset] & 0xF0;
2977
b[0] |= (FLAC__byte)((samples >> 32) & 0x0F);
2978
b[1] = (FLAC__byte)((samples >> 24) & 0xFF);
2979
b[2] = (FLAC__byte)((samples >> 16) & 0xFF);
2980
b[3] = (FLAC__byte)((samples >> 8) & 0xFF);
2981
b[4] = (FLAC__byte)(samples & 0xFF);
2982
memcpy(page.body + total_samples_byte_offset, b, 5);
2986
* Write min/max framesize
2989
const unsigned min_framesize_offset =
2990
FIRST_OGG_PACKET_STREAMINFO_PREFIX_LENGTH +
2991
FLAC__STREAM_METADATA_HEADER_LENGTH +
2993
FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN +
2994
FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN
2997
if(min_framesize_offset + 6 > (unsigned)page.body_len) {
2998
encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
2999
simple_ogg_page__clear(&page);
3002
b[0] = (FLAC__byte)((min_framesize >> 16) & 0xFF);
3003
b[1] = (FLAC__byte)((min_framesize >> 8) & 0xFF);
3004
b[2] = (FLAC__byte)(min_framesize & 0xFF);
3005
b[3] = (FLAC__byte)((max_framesize >> 16) & 0xFF);
3006
b[4] = (FLAC__byte)((max_framesize >> 8) & 0xFF);
3007
b[5] = (FLAC__byte)(max_framesize & 0xFF);
3008
memcpy(page.body + min_framesize_offset, b, 6);
3010
if(!simple_ogg_page__set_at(encoder, encoder->protected_->streaminfo_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
3011
simple_ogg_page__clear(&page);
3012
return; /* state already set */
3014
simple_ogg_page__clear(&page);
3019
if(0 != encoder->private_->seek_table && encoder->private_->seek_table->num_points > 0 && encoder->protected_->seektable_offset > 0) {
3023
FLAC__format_seektable_sort(encoder->private_->seek_table);
3025
FLAC__ASSERT(FLAC__format_seektable_is_legal(encoder->private_->seek_table));
3027
simple_ogg_page__init(&page);
3028
if(!simple_ogg_page__get_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->read_callback, encoder->private_->client_data)) {
3029
simple_ogg_page__clear(&page);
3030
return; /* state already set */
3033
if((FLAC__STREAM_METADATA_HEADER_LENGTH + 18*encoder->private_->seek_table->num_points) != (unsigned)page.body_len) {
3034
encoder->protected_->state = FLAC__STREAM_ENCODER_OGG_ERROR;
3035
simple_ogg_page__clear(&page);
3039
for(i = 0, p = page.body + FLAC__STREAM_METADATA_HEADER_LENGTH; i < encoder->private_->seek_table->num_points; i++, p += 18) {
3042
xx = encoder->private_->seek_table->points[i].sample_number;
3043
b[7] = (FLAC__byte)xx; xx >>= 8;
3044
b[6] = (FLAC__byte)xx; xx >>= 8;
3045
b[5] = (FLAC__byte)xx; xx >>= 8;
3046
b[4] = (FLAC__byte)xx; xx >>= 8;
3047
b[3] = (FLAC__byte)xx; xx >>= 8;
3048
b[2] = (FLAC__byte)xx; xx >>= 8;
3049
b[1] = (FLAC__byte)xx; xx >>= 8;
3050
b[0] = (FLAC__byte)xx; xx >>= 8;
3051
xx = encoder->private_->seek_table->points[i].stream_offset;
3052
b[15] = (FLAC__byte)xx; xx >>= 8;
3053
b[14] = (FLAC__byte)xx; xx >>= 8;
3054
b[13] = (FLAC__byte)xx; xx >>= 8;
3055
b[12] = (FLAC__byte)xx; xx >>= 8;
3056
b[11] = (FLAC__byte)xx; xx >>= 8;
3057
b[10] = (FLAC__byte)xx; xx >>= 8;
3058
b[9] = (FLAC__byte)xx; xx >>= 8;
3059
b[8] = (FLAC__byte)xx; xx >>= 8;
3060
x = encoder->private_->seek_table->points[i].frame_samples;
3061
b[17] = (FLAC__byte)x; x >>= 8;
3062
b[16] = (FLAC__byte)x; x >>= 8;
3066
if(!simple_ogg_page__set_at(encoder, encoder->protected_->seektable_offset, &page, encoder->private_->seek_callback, encoder->private_->write_callback, encoder->private_->client_data)) {
3067
simple_ogg_page__clear(&page);
3068
return; /* state already set */
3070
simple_ogg_page__clear(&page);
3075
FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_fractional_block, FLAC__bool is_last_block)
1845
3078
FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
2280
3516
max_lpc_order = encoder->protected_->max_lpc_order;
2281
3517
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;
3519
for (a = 0; a < encoder->protected_->num_apodizations; a++) {
3520
FLAC__lpc_window_data(real_signal, encoder->private_->window[a], encoder->private_->windowed_signal, frame_header->blocksize);
3521
encoder->private_->local_lpc_compute_autocorrelation(encoder->private_->windowed_signal, frame_header->blocksize, max_lpc_order+1, autoc);
3522
/* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
3523
if(autoc[0] != 0.0) {
3524
FLAC__lpc_compute_lp_coefficients(autoc, &max_lpc_order, encoder->private_->lp_coeff, lpc_error);
3525
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],
3529
const unsigned guess_lpc_order =
3530
FLAC__lpc_compute_best_order(
2328
3533
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]
3535
encoder->protected_->do_qlp_coeff_prec_search?
3536
FLAC__MIN_QLP_COEFF_PRECISION : /* have to guess; use the min possible size to avoid accidentally favoring lower orders */
3537
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;
3540
min_lpc_order = max_lpc_order = guess_lpc_order;
3542
if(max_lpc_order >= frame_header->blocksize)
3543
max_lpc_order = frame_header->blocksize - 1;
3544
for(lpc_order = min_lpc_order; lpc_order <= max_lpc_order; lpc_order++) {
3545
lpc_residual_bits_per_sample = FLAC__lpc_compute_expected_bits_per_residual_sample(lpc_error[lpc_order-1], frame_header->blocksize-lpc_order);
3546
if(lpc_residual_bits_per_sample >= (FLAC__double)subframe_bps)
3547
continue; /* don't even try */
3548
rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
3549
rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
3550
if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
3551
#ifdef DEBUG_VERBOSE
3552
fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
3554
rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
3556
if(encoder->protected_->do_qlp_coeff_prec_search) {
3557
min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
3558
/* try to ensure a 32-bit datapath throughout for 16bps(+1bps for side channel) or less */
3559
if(subframe_bps <= 17) {
3560
max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
3561
max_qlp_coeff_precision = max(max_qlp_coeff_precision, min_qlp_coeff_precision);
3564
max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
3567
min_qlp_coeff_precision = max_qlp_coeff_precision = encoder->protected_->qlp_coeff_precision;
3569
for(qlp_coeff_precision = min_qlp_coeff_precision; qlp_coeff_precision <= max_qlp_coeff_precision; qlp_coeff_precision++) {
3571
evaluate_lpc_subframe_(
3574
residual[!_best_subframe],
3575
encoder->private_->abs_residual_partition_sums,
3576
encoder->private_->raw_bits_per_partition,
3577
encoder->private_->lp_coeff[lpc_order-1],
3578
frame_header->blocksize,
3581
qlp_coeff_precision,
3583
min_partition_order,
3584
max_partition_order,
3585
encoder->protected_->do_escape_coding,
3586
encoder->protected_->rice_parameter_search_dist,
3587
subframe[!_best_subframe],
3588
partitioned_rice_contents[!_best_subframe]
3590
if(_candidate_bits > 0) { /* if == 0, there was a problem quantizing the lpcoeffs */
3591
if(_candidate_bits < _best_bits) {
3592
_best_subframe = !_best_subframe;
3593
_best_bits = _candidate_bits;
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;
4072
#ifdef EXACT_RICE_BITS_CALCULATION
4073
static __inline unsigned count_rice_bits_in_partition_(
4074
const unsigned rice_parameter,
4075
const unsigned partition_samples,
4076
const FLAC__int32 *residual
4079
unsigned i, partition_bits =
4080
FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN +
4081
(1+rice_parameter) * partition_samples /* 1 for unary stop bit + rice_parameter for the binary portion */
4083
for(i = 0; i < partition_samples; i++)
4084
partition_bits += ( (FLAC__uint32)((residual[i]<<1)^(residual[i]>>31)) >> rice_parameter );
4085
return partition_bits;
4088
static __inline unsigned count_rice_bits_in_partition_(
4089
const unsigned rice_parameter,
4090
const unsigned partition_samples,
4091
const FLAC__uint64 abs_residual_partition_sum
4095
FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN +
4096
(1+rice_parameter) * partition_samples + /* 1 for unary stop bit + rice_parameter for the binary portion */
4099
(unsigned)(abs_residual_partition_sum >> (rice_parameter-1)) /* rice_parameter-1 because the real coder sign-folds instead of using a sign bit */
4100
: (unsigned)(abs_residual_partition_sum << 1) /* can't shift by negative number, so reverse */
4102
- (partition_samples >> 1)
4103
/* -(partition_samples>>1) to subtract out extra contributions to the abs_residual_partition_sum.
4104
* The actual number of bits used is closer to the sum for all i in the partition of abs(residual[i])>>(rice_parameter-1)
4105
* By using the abs_residual_partition sum, we also add in bits in the LSBs that would normally be shifted out.
4106
* So the subtraction term tries to guess how many extra bits were contributed.
4107
* If the LSBs are randomly distributed, this should average to 0.5 extra bits per sample.
4113
FLAC__bool set_partitioned_rice_(
4114
#ifdef EXACT_RICE_BITS_CALCULATION
4115
const FLAC__int32 residual[],
4117
const FLAC__uint64 abs_residual_partition_sums[],
4118
const unsigned raw_bits_per_partition[],
4119
const unsigned residual_samples,
4120
const unsigned predictor_order,
4121
const unsigned suggested_rice_parameter,
4122
const unsigned rice_parameter_search_dist,
4123
const unsigned partition_order,
4124
const FLAC__bool search_for_escapes,
4125
FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
4129
unsigned rice_parameter, partition_bits;
4130
unsigned best_partition_bits, best_rice_parameter = 0;
3074
4131
unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
3075
4132
unsigned *parameters, *raw_bits;
4133
#ifdef ENABLE_RICE_PARAMETER_SEARCH
4134
unsigned min_rice_parameter, max_rice_parameter;
4136
(void)rice_parameter_search_dist;
3077
4139
FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
3392
4407
(void)decoder, (void)status;
3393
4408
encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
4411
FLAC__StreamEncoderReadStatus file_read_callback_(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
4415
*bytes = fread(buffer, 1, *bytes, encoder->private_->file);
4417
if (feof(encoder->private_->file))
4418
return FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM;
4419
else if (ferror(encoder->private_->file))
4420
return FLAC__STREAM_ENCODER_READ_STATUS_ABORT;
4422
return FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE;
4425
FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data)
4429
if(fseeko(encoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0)
4430
return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR;
4432
return FLAC__STREAM_ENCODER_SEEK_STATUS_OK;
4435
FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
4441
offset = ftello(encoder->private_->file);
4444
return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR;
4447
*absolute_byte_offset = (FLAC__uint64)offset;
4448
return FLAC__STREAM_ENCODER_TELL_STATUS_OK;
4452
#ifdef FLAC__VALGRIND_TESTING
4453
static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
4455
size_t ret = fwrite(ptr, size, nmemb, stream);
4461
#define local__fwrite fwrite
4464
FLAC__StreamEncoderWriteStatus file_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, void *client_data)
4466
(void)client_data, (void)current_frame;
4468
if(local__fwrite(buffer, sizeof(FLAC__byte), bytes, encoder->private_->file) == bytes) {
4469
FLAC__bool call_it = 0 != encoder->private_->progress_callback && (
4471
/* We would like to be able to use 'samples > 0' in the
4472
* clause here but currently because of the nature of our
4473
* Ogg writing implementation, 'samples' is always 0 (see
4474
* ogg_encoder_aspect.c). The downside is extra progress
4477
encoder->private_->is_ogg? true :
4482
/* NOTE: We have to add +bytes, +samples, and +1 to the stats
4483
* because at this point in the callback chain, the stats
4484
* have not been updated. Only after we return and control
4485
* gets back to write_frame_() are the stats updated
4487
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);
4489
return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
4492
return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
4496
* This will forcibly set stdout to binary mode (for OSes that require it)
4498
FILE *get_binary_stdout_(void)
4500
/* if something breaks here it is probably due to the presence or
4501
* absence of an underscore before the identifiers 'setmode',
4502
* 'fileno', and/or 'O_BINARY'; check your system header files.
4504
#if defined _MSC_VER || defined __MINGW32__
4505
_setmode(_fileno(stdout), _O_BINARY);
4506
#elif defined __CYGWIN__
4507
/* almost certainly not needed for any modern Cygwin, but let's be safe... */
4508
setmode(_fileno(stdout), _O_BINARY);
4509
#elif defined __EMX__
4510
setmode(fileno(stdout), O_BINARY);