History

avcodec/mlpenc: add proper support for output bit shift

Paul B Mahol committed 3 years ago

Commit 79c568d1 parent 3f773d81 file · +52 −5Browse files

Showing the changes to libavcodec (1 of 1 file). Show all

Unified · Split

libavcodec/mlpenc.c+52−5
@@ -54,6 +54,7 @@ typedef struct RestartHeader { · 53 unmodified lines
54 uint8_t max_channel; ///< The index of the last channel coded in this substream.54 uint8_t max_channel; ///< The index of the last channel coded in this substream.
55 uint8_t max_matrix_channel; ///< The number of channels input into the rematrix stage.55 uint8_t max_matrix_channel; ///< The number of channels input into the rematrix stage.
56 56
57 int8_t max_shift;
57 uint8_t noise_shift; ///< The left shift applied to random noise in 0x31ea substreams.58 uint8_t noise_shift; ///< The left shift applied to random noise in 0x31ea substreams.
58 uint32_t noisegen_seed; ///< The current seed value for the pseudorandom noise generator(s).59 uint32_t noisegen_seed; ///< The current seed value for the pseudorandom noise generator(s).
59 60
@@ -84,7 +85,7 @@ typedef struct MatrixParams { · 24 unmodified lines
84typedef struct DecodingParams {85typedef struct DecodingParams {
85 uint16_t blocksize; ///< number of PCM samples in current audio block86 uint16_t blocksize; ///< number of PCM samples in current audio block
86 uint8_t quant_step_size[MAX_CHANNELS]; ///< left shift to apply to Huffman-decoded residuals87 uint8_t quant_step_size[MAX_CHANNELS]; ///< left shift to apply to Huffman-decoded residuals
87 uint8_t output_shift[MAX_CHANNELS]; ///< Left shift to apply to decoded PCM values to get final 24-bit output.88 int8_t output_shift[MAX_CHANNELS]; ///< Left shift to apply to decoded PCM values to get final 24-bit output.
88 89
89 MatrixParams matrix_params;90 MatrixParams matrix_params;
90 91
@@ -383,11 +384,13 @@ static void copy_restart_frame_params(MLPEncodeContext *ctx, MLPSubstream *s) · 292 unmodified lines
383 384
384 copy_matrix_params(&dp->matrix_params, &s->b[1].decoding_params.matrix_params);385 copy_matrix_params(&dp->matrix_params, &s->b[1].decoding_params.matrix_params);
385 386
387 for (int ch = 0; ch <= rh->max_matrix_channel; ch++)
388 dp->output_shift[ch] = s->b[1].decoding_params.output_shift[ch];
389
386 for (int ch = 0; ch <= rh->max_channel; ch++) {390 for (int ch = 0; ch <= rh->max_channel; ch++) {
387 ChannelParams *cp = &s->b[index].channel_params[ch];391 ChannelParams *cp = &s->b[index].channel_params[ch];
388 392
389 dp->quant_step_size[ch] = s->b[1].decoding_params.quant_step_size[ch];393 dp->quant_step_size[ch] = s->b[1].decoding_params.quant_step_size[ch];
390 dp->output_shift[ch] = s->b[1].decoding_params.output_shift[ch];
391 394
392 if (index)395 if (index)
393 for (unsigned int filter = 0; filter < NUM_FILTERS; filter++)396 for (unsigned int filter = 0; filter < NUM_FILTERS; filter++)
@@ -758,7 +761,7 @@ static void write_restart_header(MLPEncodeContext *ctx, MLPSubstream *s, · 364 unmodified lines
758 put_bits(pb, 4, rh->max_matrix_channel);761 put_bits(pb, 4, rh->max_matrix_channel);
759 put_bits(pb, 4, rh->noise_shift );762 put_bits(pb, 4, rh->noise_shift );
760 put_bits(pb, 23, rh->noisegen_seed );763 put_bits(pb, 23, rh->noisegen_seed );
761 put_bits(pb, 4, 0 ); /* TODO max_shift */764 put_bits(pb, 4, rh->max_shift );
762 put_bits(pb, 5, rh->max_huff_lsbs );765 put_bits(pb, 5, rh->max_huff_lsbs );
763 put_bits(pb, 5, rh->max_output_bits );766 put_bits(pb, 5, rh->max_output_bits );
764 put_bits(pb, 5, rh->max_output_bits );767 put_bits(pb, 5, rh->max_output_bits );
@@ -1253,6 +1256,41 @@ static int number_trailing_zeroes(int32_t sample, unsigned int max, unsigned int · 488 unmodified lines
1253 return sample ? FFMIN(max, ff_ctz(sample)) : def;1256 return sample ? FFMIN(max, ff_ctz(sample)) : def;
1254}1257}
1255 1258
1259static void determine_output_shift(MLPEncodeContext *ctx, MLPSubstream *s)
1260{
1261 RestartHeader *rh = s->cur_restart_header;
1262 DecodingParams *dp1 = &s->b[1].decoding_params;
1263 int32_t sample_mask[MAX_CHANNELS];
1264
1265 memset(sample_mask, 0, sizeof(sample_mask));
1266
1267 for (int j = 0; j <= ctx->cur_restart_interval; j++) {
1268 DecodingParams *dp = &s->b[j].decoding_params;
1269
1270 for (int ch = 0; ch <= rh->max_matrix_channel; ch++) {
1271 int32_t *sample_buffer = dp->sample_buffer[ch];
1272
1273 for (int i = 0; i < dp->blocksize; i++)
1274 sample_mask[ch] |= sample_buffer[i];
1275 }
1276 }
1277
1278 for (int ch = 0; ch <= rh->max_matrix_channel; ch++)
1279 dp1->output_shift[ch] = number_trailing_zeroes(sample_mask[ch], 7, 0);
1280
1281 for (int j = 0; j <= ctx->cur_restart_interval; j++) {
1282 DecodingParams *dp = &s->b[j].decoding_params;
1283
1284 for (int ch = 0; ch <= rh->max_matrix_channel; ch++) {
1285 int32_t *sample_buffer = dp->sample_buffer[ch];
1286 const int shift = dp1->output_shift[ch];
1287
1288 for (int i = 0; i < dp->blocksize; i++)
1289 sample_buffer[i] >>= shift;
1290 }
1291 }
1292}
1293
1256/** Determines how many bits are zero at the end of all samples so they can be1294/** Determines how many bits are zero at the end of all samples so they can be
1257 * shifted out.1295 * shifted out.
1258 */1296 */
@@ -2016,14 +2054,21 @@ static void set_major_params(MLPEncodeContext *ctx, MLPSubstream *s) · 757 unmodified lines
2016{2054{
2017 RestartHeader *rh = s->cur_restart_header;2055 RestartHeader *rh = s->cur_restart_header;
2018 uint8_t max_huff_lsbs = 0, max_output_bits = 0;2056 uint8_t max_huff_lsbs = 0, max_output_bits = 0;
2057 int8_t max_shift = 0;
2019 2058
2020 for (int index = 0; index < s->b[ctx->restart_intervals-1].seq_size; index++) {2059 for (int index = 0; index < s->b[ctx->restart_intervals-1].seq_size; index++) {
2021 memcpy(&s->b[index].major_decoding_params,2060 memcpy(&s->b[index].major_decoding_params,
2022 &s->b[index].decoding_params, sizeof(DecodingParams));2061 &s->b[index].decoding_params, sizeof(DecodingParams));
2062 for (int ch = 0; ch <= rh->max_matrix_channel; ch++) {
2063 int8_t shift = s->b[index].decoding_params.output_shift[ch];
2064
2065 max_shift = FFMAX(max_shift, shift);
2066 }
2023 for (int ch = rh->min_channel; ch <= rh->max_channel; ch++) {2067 for (int ch = rh->min_channel; ch <= rh->max_channel; ch++) {
2024 uint8_t huff_lsbs = s->b[index].channel_params[ch].huff_lsbs;2068 uint8_t huff_lsbs = s->b[index].channel_params[ch].huff_lsbs;
2025 if (max_huff_lsbs < huff_lsbs)2069
2026 max_huff_lsbs = huff_lsbs;2070 max_huff_lsbs = FFMAX(max_huff_lsbs, huff_lsbs);
2071
2027 memcpy(&s->b[index].major_channel_params[ch],2072 memcpy(&s->b[index].major_channel_params[ch],
2028 &s->b[index].channel_params[ch],2073 &s->b[index].channel_params[ch],
2029 sizeof(ChannelParams));2074 sizeof(ChannelParams));
@@ -2031,6 +2076,7 @@ static void set_major_params(MLPEncodeContext *ctx, MLPSubstream *s) · 1 unmodified line
2031 }2076 }
2032 2077
2033 rh->max_huff_lsbs = max_huff_lsbs;2078 rh->max_huff_lsbs = max_huff_lsbs;
2079 rh->max_shift = max_shift;
2034 2080
2035 for (int index = 0; index < ctx->number_of_frames; index++)2081 for (int index = 0; index < ctx->number_of_frames; index++)
2036 if (max_output_bits < s->b[index].max_output_bits)2082 if (max_output_bits < s->b[index].max_output_bits)
@@ -2065,6 +2111,7 @@ static void analyze_sample_buffer(MLPEncodeContext *ctx, MLPSubstream *s) · 28 unmodified lines
2065 s->b[1].decoding_params.blocksize -= 8;2111 s->b[1].decoding_params.blocksize -= 8;
2066 2112
2067 input_to_sample_buffer (ctx, s);2113 input_to_sample_buffer (ctx, s);
2114 determine_output_shift (ctx, s);
2068 generate_2_noise_channels(ctx, s);2115 generate_2_noise_channels(ctx, s);
2069 lossless_matrix_coeffs (ctx, s);2116 lossless_matrix_coeffs (ctx, s);
2070 rematrix_channels (ctx, s);2117 rematrix_channels (ctx, s);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t