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

Unified · Split

libavcodec/mlpenc.c+52−5
@@ -54,6 +54,7 @@ typedef struct RestartHeader { · 53 unmodified lines
5454 uint8_t max_channel; ///< The index of the last channel coded in this substream.
5555 uint8_t max_matrix_channel; ///< The number of channels input into the rematrix stage.
5656
57+ int8_t max_shift;
5758 uint8_t noise_shift; ///< The left shift applied to random noise in 0x31ea substreams.
5859 uint32_t noisegen_seed; ///< The current seed value for the pseudorandom noise generator(s).
5960
@@ -84,7 +85,7 @@ typedef struct MatrixParams { · 24 unmodified lines
8485 typedef struct DecodingParams {
8586 uint16_t blocksize; ///< number of PCM samples in current audio block
8687 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.
8889
8990 MatrixParams matrix_params;
9091
@@ -383,11 +384,13 @@ static void copy_restart_frame_params(MLPEncodeContext *ctx, MLPSubstream *s) · 292 unmodified lines
383384
384385 copy_matrix_params(&dp->matrix_params, &s->b[1].decoding_params.matrix_params);
385386
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+
386390 for (int ch = 0; ch <= rh->max_channel; ch++) {
387391 ChannelParams *cp = &s->b[index].channel_params[ch];
388392
389393 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];
391394
392395 if (index)
393396 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
758761 put_bits(pb, 4, rh->max_matrix_channel);
759762 put_bits(pb, 4, rh->noise_shift );
760763 put_bits(pb, 23, rh->noisegen_seed );
761− put_bits(pb, 4, 0 ); /* TODO max_shift */
764+ put_bits(pb, 4, rh->max_shift );
762765 put_bits(pb, 5, rh->max_huff_lsbs );
763766 put_bits(pb, 5, rh->max_output_bits );
764767 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
12531256 return sample ? FFMIN(max, ff_ctz(sample)) : def;
12541257 }
12551258
1259+static 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+
12561294 /** Determines how many bits are zero at the end of all samples so they can be
12571295 * shifted out.
12581296 */
@@ -2016,14 +2054,21 @@ static void set_major_params(MLPEncodeContext *ctx, MLPSubstream *s) · 757 unmodified lines
20162054 {
20172055 RestartHeader *rh = s->cur_restart_header;
20182056 uint8_t max_huff_lsbs = 0, max_output_bits = 0;
2057+ int8_t max_shift = 0;
20192058
20202059 for (int index = 0; index < s->b[ctx->restart_intervals-1].seq_size; index++) {
20212060 memcpy(&s->b[index].major_decoding_params,
20222061 &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+ }
20232067 for (int ch = rh->min_channel; ch <= rh->max_channel; ch++) {
20242068 uint8_t huff_lsbs = s->b[index].channel_params[ch].huff_lsbs;
2025− if (max_huff_lsbs < huff_lsbs)
2026− max_huff_lsbs = huff_lsbs;
2069+
2070+ max_huff_lsbs = FFMAX(max_huff_lsbs, huff_lsbs);
2071+
20272072 memcpy(&s->b[index].major_channel_params[ch],
20282073 &s->b[index].channel_params[ch],
20292074 sizeof(ChannelParams));
@@ -2031,6 +2076,7 @@ static void set_major_params(MLPEncodeContext *ctx, MLPSubstream *s) · 1 unmodified line
20312076 }
20322077
20332078 rh->max_huff_lsbs = max_huff_lsbs;
2079+ rh->max_shift = max_shift;
20342080
20352081 for (int index = 0; index < ctx->number_of_frames; index++)
20362082 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
20652111 s->b[1].decoding_params.blocksize -= 8;
20662112
20672113 input_to_sample_buffer (ctx, s);
2114+ determine_output_shift (ctx, s);
20682115 generate_2_noise_channels(ctx, s);
20692116 lossless_matrix_coeffs (ctx, s);
20702117 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