File 0005-Move-entropy-partitioning-result-allocation-so-it-ca.patch of Package flac.30364
From a2c8ae599ec2f9fc619198438bcc34723edcea97 Mon Sep 17 00:00:00 2001
From: Martijn van Beurden <mvanb1@gmail.com>
Date: Fri, 12 Aug 2022 16:31:06 +0200
Subject: [PATCH] Move entropy partitioning result allocation so it can be
checked
For some reason realloc failing to allocate space to store the
results of entropy partitioning was not handled at all. The realloc
is moved to a spot where it is possible to set encoder state. This
also moves it from deep within a loop to being called only
occasionally
---
src/libFLAC/format.c | 2 +-
src/libFLAC/stream_encoder.c | 23 +++++++++++++++++++++--
2 files changed, 22 insertions(+), 3 deletions(-)
--- a/src/libFLAC/format.c
+++ b/src/libFLAC/format.c
@@ -576,7 +576,7 @@ FLAC__bool FLAC__format_entropy_coding_m
FLAC__ASSERT(object->capacity_by_order > 0 || (0 == object->parameters && 0 == object->raw_bits));
- if(object->capacity_by_order < max_partition_order) {
+ if(object->capacity_by_order < max_partition_order || object->parameters == NULL || object->raw_bits == NULL) {
if(0 == (object->parameters = safe_realloc_(object->parameters, sizeof(unsigned)*(1 << max_partition_order))))
return false;
if(0 == (object->raw_bits = safe_realloc_(object->raw_bits, sizeof(unsigned)*(1 << max_partition_order))))
--- a/src/libFLAC/stream_encoder.c
+++ b/src/libFLAC/stream_encoder.c
@@ -2466,11 +2466,32 @@ FLAC__bool resize_buffers_(FLAC__StreamE
ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize, &encoder->private_->residual_workspace_unaligned[channel][i], &encoder->private_->residual_workspace[channel][i]);
}
}
+
+
+ for(channel = 0; ok && channel < encoder->protected_->channels; channel++) {
+ for(i = 0; ok && i < 2; i++) {
+ ok = ok && FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(&encoder->private_->partitioned_rice_contents_workspace[channel][i], encoder->protected_->max_residual_partition_order);
+ ok = ok && FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(&encoder->private_->partitioned_rice_contents_workspace[channel][i], encoder->protected_->max_residual_partition_order);
+ }
+ }
+
for(channel = 0; ok && channel < 2; channel++) {
for(i = 0; ok && i < 2; i++) {
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]);
}
}
+
+ for(channel = 0; ok && channel < 2; channel++) {
+ for(i = 0; ok && i < 2; i++) {
+ ok = ok && FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(&encoder->private_->partitioned_rice_contents_workspace_mid_side[channel][i], encoder->protected_->max_residual_partition_order);
+ }
+ }
+
+ for(i = 0; ok && i < 2; i++) {
+ ok = ok && FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(&encoder->private_->partitioned_rice_contents_extra[i], encoder->protected_->max_residual_partition_order);
+ }
+
+
/* the *2 is an approximation to the series 1 + 1/2 + 1/4 + ... that sums tree occupies in a flat array */
/*@@@ 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) */
ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_blocksize * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
@@ -3930,7 +3951,6 @@ unsigned find_best_partition_order_(
unsigned partition;
/* save best parameters and raw_bits */
- FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(prc, flac_max(6u, best_partition_order));
memcpy(prc->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partition_order)));
if(do_escape_coding)
memcpy(prc->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partition_order)));
@@ -4136,7 +4156,6 @@ FLAC__bool set_partitioned_rice_(
FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER);
FLAC__ASSERT(rice_parameter_limit <= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER);
- FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, flac_max(6u, partition_order));
parameters = partitioned_rice_contents->parameters;
raw_bits = partitioned_rice_contents->raw_bits;