Showing the changes to libavcodec (1 of 1 file). Show all
Unified · Split
| @@ -54,6 +54,7 @@ typedef struct RestartHeader { · 53 unmodified lines | ||
| 54 | 54 | uint8_t max_channel; ///< The index of the last channel coded in this substream. |
| 55 | 55 | uint8_t max_matrix_channel; ///< The number of channels input into the rematrix stage. |
| 56 | 56 | |
| 57 | + int8_t max_shift; | |
| 57 | 58 | uint8_t noise_shift; ///< The left shift applied to random noise in 0x31ea substreams. |
| 58 | 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 | ||
| 84 | 85 | typedef struct DecodingParams { |
| 85 | 86 | uint16_t blocksize; ///< number of PCM samples in current audio block |
| 86 | 87 | 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 | 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 | 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 | 390 | for (int ch = 0; ch <= rh->max_channel; ch++) { |
| 387 | 391 | ChannelParams *cp = &s->b[index].channel_params[ch]; |
| 388 | 392 | |
| 389 | 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 | 395 | if (index) |
| 393 | 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 | 761 | put_bits(pb, 4, rh->max_matrix_channel); |
| 759 | 762 | put_bits(pb, 4, rh->noise_shift ); |
| 760 | 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 | 765 | put_bits(pb, 5, rh->max_huff_lsbs ); |
| 763 | 766 | put_bits(pb, 5, rh->max_output_bits ); |
| 764 | 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 | 1256 | return sample ? FFMIN(max, ff_ctz(sample)) : def; |
| 1254 | 1257 | } |
| 1255 | 1258 | |
| 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 | + | |
| 1256 | 1294 | /** Determines how many bits are zero at the end of all samples so they can be |
| 1257 | 1295 | * shifted out. |
| 1258 | 1296 | */ |
| @@ -2016,14 +2054,21 @@ static void set_major_params(MLPEncodeContext *ctx, MLPSubstream *s) · 757 unmodified lines | ||
| 2016 | 2054 | { |
| 2017 | 2055 | RestartHeader *rh = s->cur_restart_header; |
| 2018 | 2056 | uint8_t max_huff_lsbs = 0, max_output_bits = 0; |
| 2057 | + int8_t max_shift = 0; | |
| 2019 | 2058 | |
| 2020 | 2059 | for (int index = 0; index < s->b[ctx->restart_intervals-1].seq_size; index++) { |
| 2021 | 2060 | memcpy(&s->b[index].major_decoding_params, |
| 2022 | 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 | 2067 | for (int ch = rh->min_channel; ch <= rh->max_channel; ch++) { |
| 2024 | 2068 | 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 | + | |
| 2027 | 2072 | memcpy(&s->b[index].major_channel_params[ch], |
| 2028 | 2073 | &s->b[index].channel_params[ch], |
| 2029 | 2074 | sizeof(ChannelParams)); |
| @@ -2031,6 +2076,7 @@ static void set_major_params(MLPEncodeContext *ctx, MLPSubstream *s) · 1 unmodified line | ||
| 2031 | 2076 | } |
| 2032 | 2077 | |
| 2033 | 2078 | rh->max_huff_lsbs = max_huff_lsbs; |
| 2079 | + rh->max_shift = max_shift; | |
| 2034 | 2080 | |
| 2035 | 2081 | for (int index = 0; index < ctx->number_of_frames; index++) |
| 2036 | 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 | 2111 | s->b[1].decoding_params.blocksize -= 8; |
| 2066 | 2112 | |
| 2067 | 2113 | input_to_sample_buffer (ctx, s); |
| 2114 | + determine_output_shift (ctx, s); | |
| 2068 | 2115 | generate_2_noise_channels(ctx, s); |
| 2069 | 2116 | lossless_matrix_coeffs (ctx, s); |
| 2070 | 2117 | rematrix_channels (ctx, s); |