History

avcodec/mlpenc: restructure code and resolve several bugs

Do not use put_sbits() where only unsigned is stored. Reduce size of data_check_present field. Reduce size of table of codebook_extremes[]. Avoid anonymously typedeffed structs. Use encoder private context to store parameters. Fix wrapping when calculating offsets. Restructure arrays in encoder private context so to keep arrays belonging to same subblock into separate structure. Disable matrix coefficients as they are sometimes producing wrong results.

Paul B Mahol committed 3 years ago

Commit d7579181 parent 81cc0e11 file · +115 −194Browse files

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

Unified · Split

libavcodec/mlpenc.c+115−194
@@ -42,10 +42,10 @@ · 41 unmodified lines
42 42
43#define MLP_MIN_LPC_ORDER 143#define MLP_MIN_LPC_ORDER 1
44#define MLP_MAX_LPC_ORDER 844#define MLP_MAX_LPC_ORDER 8
45#define MLP_MIN_LPC_SHIFT 045#define MLP_MIN_LPC_SHIFT 8
46#define MLP_MAX_LPC_SHIFT 1546#define MLP_MAX_LPC_SHIFT 15
47 47
48typedef struct {48typedef struct RestartHeader {
49 uint8_t min_channel; ///< The index of the first channel coded in this substream.49 uint8_t min_channel; ///< The index of the first channel coded in this substream.
50 uint8_t max_channel; ///< The index of the last channel coded in this substream.50 uint8_t max_channel; ///< The index of the last channel coded in this substream.
51 uint8_t max_matrix_channel; ///< The number of channels input into the rematrix stage.51 uint8_t max_matrix_channel; ///< The number of channels input into the rematrix stage.
@@ -53,7 +53,7 @@ typedef struct { · 1 unmodified line
53 uint8_t noise_shift; ///< The left shift applied to random noise in 0x31ea substreams.53 uint8_t noise_shift; ///< The left shift applied to random noise in 0x31ea substreams.
54 uint32_t noisegen_seed; ///< The current seed value for the pseudorandom noise generator(s).54 uint32_t noisegen_seed; ///< The current seed value for the pseudorandom noise generator(s).
55 55
56 int data_check_present; ///< Set if the substream contains extra info to check the size of VLC blocks.56 uint8_t data_check_present; ///< Set if the substream contains extra info to check the size of VLC blocks.
57 57
58 int32_t lossless_check_data; ///< XOR of all output samples58 int32_t lossless_check_data; ///< XOR of all output samples
59 59
@@ -61,7 +61,7 @@ typedef struct { · 1 unmodified line
61 uint8_t max_output_bits; ///< largest output bit-depth61 uint8_t max_output_bits; ///< largest output bit-depth
62} RestartHeader;62} RestartHeader;
63 63
64typedef struct {64typedef struct MatrixParams {
65 uint8_t count; ///< number of matrices to apply65 uint8_t count; ///< number of matrices to apply
66 66
67 uint8_t outch[MAX_MATRICES]; ///< output channel for each matrix67 uint8_t outch[MAX_MATRICES]; ///< output channel for each matrix
@@ -85,7 +85,7 @@ enum ParamFlags { · 17 unmodified lines
85 PARAM_PRESENT = 1 << 0,85 PARAM_PRESENT = 1 << 0,
86};86};
87 87
88typedef struct {88typedef struct DecodingParams {
89 uint16_t blocksize; ///< number of PCM samples in current audio block89 uint16_t blocksize; ///< number of PCM samples in current audio block
90 uint8_t quant_step_size[MAX_CHANNELS]; ///< left shift to apply to Huffman-decoded residuals90 uint8_t quant_step_size[MAX_CHANNELS]; ///< left shift to apply to Huffman-decoded residuals
91 91
@@ -96,8 +96,8 @@ typedef struct { · 4 unmodified lines
96 96
97typedef struct BestOffset {97typedef struct BestOffset {
98 int32_t offset;98 int32_t offset;
99 int bitcount;99 uint32_t bitcount;
100 int lsb_bits;100 uint8_t lsb_bits;
101 int32_t min;101 int32_t min;
102 int32_t max;102 int32_t max;
103} BestOffset;103} BestOffset;
@@ -108,6 +108,18 @@ typedef struct BestOffset { · 4 unmodified lines
108/** Number of possible codebooks (counting "no codebooks") */108/** Number of possible codebooks (counting "no codebooks") */
109#define NUM_CODEBOOKS 4109#define NUM_CODEBOOKS 4
110 110
111typedef struct MLPBlock {
112 unsigned int seq_size;
113 DecodingParams decoding_params;
114 int32_t lossless_check_data; ///< Array with lossless_check_data for each access unit.
115 unsigned int max_output_bits; ///< largest output bit-depth
116 BestOffset best_offset[MAX_CHANNELS][NUM_CODEBOOKS];
117 ChannelParams channel_params[MAX_CHANNELS];
118 ChannelParams major_channel_params[MAX_CHANNELS]; ///< ChannelParams to be written to bitstream.
119 DecodingParams major_decoding_params; ///< DecodingParams to be written to bitstream.
120 int major_params_changed; ///< params_changed to be written to bitstream.
121} MLPBlock;
122
111typedef struct MLPEncodeContext {123typedef struct MLPEncodeContext {
112 AVClass *class;124 AVClass *class;
113 AVCodecContext *avctx;125 AVCodecContext *avctx;
@@ -142,17 +154,12 @@ typedef struct MLPEncodeContext { · 28 unmodified lines
142 int32_t *major_scratch_buffer; ///< Scratch buffer big enough to fit all data for one entire major frame interval.154 int32_t *major_scratch_buffer; ///< Scratch buffer big enough to fit all data for one entire major frame interval.
143 int32_t last_frames; ///< Signal last frames.155 int32_t last_frames; ///< Signal last frames.
144 156
145 int32_t *lpc_sample_buffer;
146
147 unsigned int major_number_of_frames;157 unsigned int major_number_of_frames;
148 unsigned int next_major_number_of_frames;158 unsigned int next_major_number_of_frames;
149 159
150 unsigned int major_frame_size; ///< Number of samples in current major frame being encoded.160 unsigned int major_frame_size; ///< Number of samples in current major frame being encoded.
151 unsigned int next_major_frame_size; ///< Counter of number of samples for next major frame.161 unsigned int next_major_frame_size; ///< Counter of number of samples for next major frame.
152 162
153 int32_t *lossless_check_data; ///< Array with lossless_check_data for each access unit.
154
155 unsigned int *max_output_bits; ///< largest output bit-depth
156 unsigned int frame_index; ///< Index of current frame being encoded.163 unsigned int frame_index; ///< Index of current frame being encoded.
157 164
158 unsigned int one_sample_buffer_size; ///< Number of samples*channel for one access unit.165 unsigned int one_sample_buffer_size; ///< Number of samples*channel for one access unit.
@@ -169,21 +176,11 @@ typedef struct MLPEncodeContext { · 10 unmodified lines
169 uint8_t ch_modifier_thd0; ///< channel modifier for TrueHD stream 0176 uint8_t ch_modifier_thd0; ///< channel modifier for TrueHD stream 0
170 uint8_t ch_modifier_thd1; ///< channel modifier for TrueHD stream 1177 uint8_t ch_modifier_thd1; ///< channel modifier for TrueHD stream 1
171 uint8_t ch_modifier_thd2; ///< channel modifier for TrueHD stream 2178 uint8_t ch_modifier_thd2; ///< channel modifier for TrueHD stream 2
172
173 unsigned int seq_size [MAJOR_HEADER_INTERVAL];
174 unsigned int seq_offset[MAJOR_HEADER_INTERVAL];
175 unsigned int sequence_size;
176
177 ChannelParams *channel_params;
178
179 BestOffset best_offset[MAJOR_HEADER_INTERVAL+1][MAX_CHANNELS][NUM_CODEBOOKS];
180
181 DecodingParams *decoding_params;
182 RestartHeader restart_header;179 RestartHeader restart_header;
183 180
184 ChannelParams major_channel_params[MAJOR_HEADER_INTERVAL+1][MAX_CHANNELS]; ///< ChannelParams to be written to bitstream.181 MLPBlock b[MAJOR_HEADER_INTERVAL + 1];
185 DecodingParams major_decoding_params[MAJOR_HEADER_INTERVAL+1]; ///< DecodingParams to be written to bitstream.182 int32_t lpc_sample_buffer[MAJOR_HEADER_INTERVAL * MAX_BLOCKSIZE];
186 int major_params_changed[MAJOR_HEADER_INTERVAL+1]; ///< params_changed to be written to bitstream.183 int32_t filter_state_buffer[NUM_FILTERS][MAX_BLOCKSIZE * MAJOR_HEADER_INTERVAL];
187 184
188 unsigned int major_cur_subblock_index;185 unsigned int major_cur_subblock_index;
189 unsigned int major_filter_state_subblock;186 unsigned int major_filter_state_subblock;
@@ -200,16 +197,10 @@ typedef struct MLPEncodeContext { · 10 unmodified lines
200 unsigned int number_of_frames;197 unsigned int number_of_frames;
201 unsigned int number_of_samples;198 unsigned int number_of_samples;
202 unsigned int number_of_subblocks;199 unsigned int number_of_subblocks;
203 unsigned int seq_index; ///< Sequence index for high compression levels.
204 200
205 const ChannelParams *prev_channel_params;201 const ChannelParams *prev_channel_params;
206 const DecodingParams *prev_decoding_params;202 const DecodingParams *prev_decoding_params;
207 203
208 ChannelParams *seq_channel_params;
209 DecodingParams *seq_decoding_params;
210
211 int32_t *filter_state_buffer[NUM_FILTERS];
212
213 unsigned int max_codebook_search;204 unsigned int max_codebook_search;
214 205
215 int shorten_by;206 int shorten_by;
@@ -387,12 +378,12 @@ static void copy_matrix_params(MatrixParams *dst, MatrixParams *src) · 171 unmodified lines
387static void copy_restart_frame_params(MLPEncodeContext *ctx)378static void copy_restart_frame_params(MLPEncodeContext *ctx)
388{379{
389 for (unsigned int index = 0; index < ctx->number_of_subblocks; index++) {380 for (unsigned int index = 0; index < ctx->number_of_subblocks; index++) {
390 DecodingParams *dp = ctx->seq_decoding_params + index;381 DecodingParams *dp = &ctx->b[index].decoding_params;
391 382
392 copy_matrix_params(&dp->matrix_params, &ctx->cur_decoding_params->matrix_params);383 copy_matrix_params(&dp->matrix_params, &ctx->cur_decoding_params->matrix_params);
393 384
394 for (unsigned int channel = 0; channel < ctx->avctx->ch_layout.nb_channels; channel++) {385 for (unsigned int channel = 0; channel < ctx->avctx->ch_layout.nb_channels; channel++) {
395 ChannelParams *cp = ctx->seq_channel_params + index*(ctx->avctx->ch_layout.nb_channels) + channel;386 ChannelParams *cp = &ctx->b[index].channel_params[channel];
396 387
397 dp->quant_step_size[channel] = ctx->cur_decoding_params->quant_step_size[channel];388 dp->quant_step_size[channel] = ctx->cur_decoding_params->quant_step_size[channel];
398 dp->matrix_params.shift[channel] = ctx->cur_decoding_params->matrix_params.shift[channel];389 dp->matrix_params.shift[channel] = ctx->cur_decoding_params->matrix_params.shift[channel];
@@ -417,7 +408,7 @@ static void clear_decoding_params(DecodingParams *decoding_params) · 18 unmodified lines
417}408}
418 409
419/** Clears a ChannelParams struct the way it should be after a restart header. */410/** Clears a ChannelParams struct the way it should be after a restart header. */
420static void clear_channel_params(ChannelParams channel_params[MAX_CHANNELS], int nb_channels)411static void clear_channel_params(ChannelParams *channel_params, int nb_channels)
421{412{
422 for (unsigned channel = 0; channel < nb_channels; channel++) {413 for (unsigned channel = 0; channel < nb_channels; channel++) {
423 ChannelParams *cp = &channel_params[channel];414 ChannelParams *cp = &channel_params[channel];
@@ -456,12 +447,9 @@ static void default_decoding_params(MLPEncodeContext *ctx, DecodingParams *decod · 32 unmodified lines
456/** Calculates the smallest number of bits it takes to encode a given signed447/** Calculates the smallest number of bits it takes to encode a given signed
457 * value in two's complement.448 * value in two's complement.
458 */449 */
459static int inline number_sbits(int number)450static int inline number_sbits(int32_t n)
460{451{
461 if (number < -1)452 return 33 - ff_clz(FFABS(n)|1);
462 number++;
463
464 return av_log2(FFABS(number)) + 1 + !!number;
465}453}
466 454
467enum InputBitDepth {455enum InputBitDepth {
@@ -488,7 +476,6 @@ static av_cold int mlp_encode_init(AVCodecContext *avctx) · 20 unmodified lines
488 MLPEncodeContext *ctx = avctx->priv_data;476 MLPEncodeContext *ctx = avctx->priv_data;
489 RestartHeader *const rh = &ctx->restart_header;477 RestartHeader *const rh = &ctx->restart_header;
490 uint64_t channels_present;478 uint64_t channels_present;
491 unsigned int sum = 0;
492 size_t size;479 size_t size;
493 int ret;480 int ret;
494 481
@@ -579,11 +566,6 @@ static av_cold int mlp_encode_init(AVCodecContext *avctx) · 84 unmodified lines
579 ctx->min_restart_interval = MAJOR_HEADER_INTERVAL;566 ctx->min_restart_interval = MAJOR_HEADER_INTERVAL;
580 ctx->restart_intervals = ctx->max_restart_interval / ctx->min_restart_interval;567 ctx->restart_intervals = ctx->max_restart_interval / ctx->min_restart_interval;
581 568
582 size = avctx->frame_size * ctx->max_restart_interval;
583 ctx->lpc_sample_buffer = av_calloc(size, sizeof(*ctx->lpc_sample_buffer));
584 if (!ctx->lpc_sample_buffer)
585 return AVERROR(ENOMEM);
586
587 size = ctx->one_sample_buffer_size * ctx->max_restart_interval;569 size = ctx->one_sample_buffer_size * ctx->max_restart_interval;
588 ctx->major_scratch_buffer = av_calloc(size, sizeof(*ctx->major_scratch_buffer));570 ctx->major_scratch_buffer = av_calloc(size, sizeof(*ctx->major_scratch_buffer));
589 if (!ctx->major_scratch_buffer)571 if (!ctx->major_scratch_buffer)
@@ -651,31 +633,8 @@ static av_cold int mlp_encode_init(AVCodecContext *avctx) · 61 unmodified lines
651 ctx->summary_info = 0;633 ctx->summary_info = 0;
652 }634 }
653 635
654 size = ctx->max_restart_interval;636 for (unsigned int index = 0; index < ctx->restart_intervals; index++)
655 ctx->max_output_bits = av_calloc(size, sizeof(*ctx->max_output_bits));637 ctx->b[index].seq_size = ((index + 1) * ctx->min_restart_interval) + 1;
656 if (!ctx->max_output_bits)
657 return AVERROR(ENOMEM);
658
659 size = ctx->max_restart_interval;
660 ctx->lossless_check_data = av_calloc(size, sizeof(*ctx->lossless_check_data));
661 if (!ctx->lossless_check_data)
662 return AVERROR(ENOMEM);
663
664 for (unsigned int index = 0; index < ctx->restart_intervals; index++) {
665 ctx->seq_offset[index] = sum;
666 ctx->seq_size [index] = ((index + 1) * ctx->min_restart_interval) + 1;
667 sum += ctx->seq_size[index];
668 }
669 ctx->sequence_size = sum;
670 size = ctx->restart_intervals * ctx->sequence_size * ctx->avctx->ch_layout.nb_channels;
671 ctx->channel_params = av_calloc(size, sizeof(*ctx->channel_params));
672 if (!ctx->channel_params)
673 return AVERROR(ENOMEM);
674
675 size = ctx->restart_intervals * ctx->sequence_size;
676 ctx->decoding_params = av_calloc(size, sizeof(*ctx->decoding_params));
677 if (!ctx->decoding_params)
678 return AVERROR(ENOMEM);
679 638
680 /* TODO see if noisegen_seed is really worth it. */639 /* TODO see if noisegen_seed is really worth it. */
681 rh->noisegen_seed = 0;640 rh->noisegen_seed = 0;
@@ -689,13 +648,6 @@ static av_cold int mlp_encode_init(AVCodecContext *avctx) · 7 unmodified lines
689 MLP_MAX_LPC_ORDER, ctx->lpc_type)) < 0)648 MLP_MAX_LPC_ORDER, ctx->lpc_type)) < 0)
690 return ret;649 return ret;
691 650
692 for (int i = 0; i < NUM_FILTERS; i++) {
693 ctx->filter_state_buffer[i] = av_calloc(avctx->frame_size * ctx->max_restart_interval,
694 sizeof(*ctx->filter_state_buffer[0]));
695 if (!ctx->filter_state_buffer[i])
696 return AVERROR(ENOMEM);
697 }
698
699 ff_af_queue_init(avctx, &ctx->afq);651 ff_af_queue_init(avctx, &ctx->afq);
700 652
701 ff_thread_once(&init_static_once, mlp_encode_init_static);653 ff_thread_once(&init_static_once, mlp_encode_init_static);
@@ -967,7 +919,7 @@ static void write_decoding_params(MLPEncodeContext *ctx, PutBitContext *pb, · 265 unmodified lines
967 }919 }
968 }920 }
969 if (cp->codebook > 0 && cp->huff_lsbs > 24) {921 if (cp->codebook > 0 && cp->huff_lsbs > 24) {
970 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid Huff LSBs\n");922 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid Huff LSBs %d\n", cp->huff_lsbs);
971 }923 }
972 924
973 put_bits(pb, 2, cp->codebook );925 put_bits(pb, 2, cp->codebook );
@@ -1017,9 +969,10 @@ static void write_block_data(MLPEncodeContext *ctx, PutBitContext *pb) · 43 unmodified lines
1017 int vlc = sample >> lsb_bits[ch];969 int vlc = sample >> lsb_bits[ch];
1018 put_bits(pb, ff_mlp_huffman_tables[codebook_index[ch]][vlc][1],970 put_bits(pb, ff_mlp_huffman_tables[codebook_index[ch]][vlc][1],
1019 ff_mlp_huffman_tables[codebook_index[ch]][vlc][0]);971 ff_mlp_huffman_tables[codebook_index[ch]][vlc][0]);
972 sample &= ((1 << lsb_bits[ch]) - 1);
1020 }973 }
1021 974
1022 put_sbits(pb, lsb_bits[ch], sample);975 put_bits(pb, lsb_bits[ch], sample);
1023 }976 }
1024 sample_buffer += 2; /* noise channels */977 sample_buffer += 2; /* noise channels */
1025 }978 }
@@ -1032,7 +985,7 @@ static uint8_t *write_substr(MLPEncodeContext *ctx, uint8_t *buf, int buf_size, · 6 unmodified lines
1032 int restart_frame,985 int restart_frame,
1033 uint16_t substream_data_len[MAX_SUBSTREAMS])986 uint16_t substream_data_len[MAX_SUBSTREAMS])
1034{987{
1035 int32_t *lossless_check_data = ctx->lossless_check_data;988 int32_t *lossless_check_data = &ctx->b[ctx->frame_index].lossless_check_data;
1036 unsigned int cur_subblock_index = ctx->major_cur_subblock_index;989 unsigned int cur_subblock_index = ctx->major_cur_subblock_index;
1037 unsigned int num_subblocks = ctx->major_filter_state_subblock;990 unsigned int num_subblocks = ctx->major_filter_state_subblock;
1038 RestartHeader *rh = &ctx->restart_header;991 RestartHeader *rh = &ctx->restart_header;
@@ -1042,7 +995,6 @@ static uint8_t *write_substr(MLPEncodeContext *ctx, uint8_t *buf, int buf_size, · 3 unmodified lines
1042 int params_changed;995 int params_changed;
1043 int end = 0;996 int end = 0;
1044 997
1045 lossless_check_data += ctx->frame_index;
1046 ctx->cur_restart_header = rh;998 ctx->cur_restart_header = rh;
1047 999
1048 init_put_bits(&pb, buf, buf_size);1000 init_put_bits(&pb, buf, buf_size);
@@ -1052,10 +1004,10 @@ static uint8_t *write_substr(MLPEncodeContext *ctx, uint8_t *buf, int buf_size, · 3 unmodified lines
1052 1004
1053 subblock_index = cur_subblock_index++;1005 subblock_index = cur_subblock_index++;
1054 1006
1055 ctx->cur_decoding_params = &ctx->major_decoding_params[subblock_index];1007 ctx->cur_decoding_params = &ctx->b[subblock_index].major_decoding_params;
1056 ctx->cur_channel_params = ctx->major_channel_params[subblock_index];1008 ctx->cur_channel_params = ctx->b[subblock_index].major_channel_params;
1057 1009
1058 params_changed = ctx->major_params_changed[subblock_index];1010 params_changed = ctx->b[subblock_index].major_params_changed;
1059 1011
1060 if (substr_restart_frame || params_changed) {1012 if (substr_restart_frame || params_changed) {
1061 put_bits(&pb, 1, 1);1013 put_bits(&pb, 1, 1);
@@ -1202,26 +1154,21 @@ static void input_data_internal(MLPEncodeContext *ctx, const uint8_t *samples, · 140 unmodified lines
1202 int nb_samples,1154 int nb_samples,
1203 int is24)1155 int is24)
1204{1156{
1205 int32_t *lossless_check_data = ctx->lossless_check_data;1157 int32_t *lossless_check_data = &ctx->b[ctx->frame_index].lossless_check_data;
1206 const int32_t *samples_32 = (const int32_t *) samples;1158 const int32_t *samples_32 = (const int32_t *) samples;
1207 const int16_t *samples_16 = (const int16_t *) samples;1159 const int16_t *samples_16 = (const int16_t *) samples;
1208 RestartHeader *rh = &ctx->restart_header;1160 RestartHeader *rh = &ctx->restart_header;
1209 int32_t *sample_buffer = ctx->inout_buffer;1161 int32_t *sample_buffer = ctx->inout_buffer;
1210 int32_t temp_lossless_check_data = 0;1162 int32_t temp_lossless_check_data = 0;
1211 uint32_t greatest = 0;1163 uint32_t bits = 0;
1212
1213 lossless_check_data += ctx->frame_index;
1214 1164
1215 for (int i = 0; i < nb_samples; i++) {1165 for (int i = 0; i < nb_samples; i++) {
1216 for (unsigned int channel = 0; channel <= rh->max_channel; channel++) {1166 for (unsigned int channel = 0; channel <= rh->max_channel; channel++) {
1217 uint32_t abs_sample;
1218 int32_t sample;1167 int32_t sample;
1219 1168
1220 sample = is24 ? *samples_32++ >> 8 : *samples_16++ * 256;1169 sample = is24 ? *samples_32++ >> 8 : *samples_16++ * 256;
1221 1170
1222 /* TODO Find out if number_sbits can be used for negative values. */1171 bits = FFMAX(number_sbits(sample), bits);
1223 abs_sample = FFABS(sample);
1224 greatest = FFMAX(greatest, abs_sample);
1225 1172
1226 temp_lossless_check_data ^= (sample & 0x00ffffff) << channel;1173 temp_lossless_check_data ^= (sample & 0x00ffffff) << channel;
1227 *sample_buffer++ = sample;1174 *sample_buffer++ = sample;
@@ -1230,7 +1177,7 @@ static void input_data_internal(MLPEncodeContext *ctx, const uint8_t *samples, · 2 unmodified lines
1230 sample_buffer += 2; /* noise channels */1177 sample_buffer += 2; /* noise channels */
1231 }1178 }
1232 1179
1233 ctx->max_output_bits[ctx->frame_index] = number_sbits(greatest);1180 ctx->b[ctx->frame_index].max_output_bits = bits;
1234 1181
1235 *lossless_check_data++ = temp_lossless_check_data;1182 *lossless_check_data++ = temp_lossless_check_data;
1236}1183}
@@ -1265,7 +1212,7 @@ static void input_to_sample_buffer(MLPEncodeContext *ctx) · 28 unmodified lines
1265/** Counts the number of trailing zeroes in a value */1212/** Counts the number of trailing zeroes in a value */
1266static int number_trailing_zeroes(int32_t sample)1213static int number_trailing_zeroes(int32_t sample)
1267{1214{
1268 return FFMIN(15, ff_ctz(sample));1215 return sample ? FFMIN(15, ff_ctz(sample)) : 0;
1269}1216}
1270 1217
1271/** Determines how many bits are zero at the end of all samples so they can be1218/** Determines how many bits are zero at the end of all samples so they can be
@@ -1298,27 +1245,21 @@ static void determine_quant_step_size(MLPEncodeContext *ctx) · 26 unmodified lines
1298 */1245 */
1299static void code_filter_coeffs(MLPEncodeContext *ctx, FilterParams *fp, int32_t *fcoeff)1246static void code_filter_coeffs(MLPEncodeContext *ctx, FilterParams *fp, int32_t *fcoeff)
1300{1247{
1301 int min = INT_MAX, max = INT_MIN;1248 uint32_t coeff_mask = 0;
1302 int bits, shift;1249 int bits = 0, shift;
1303 int coeff_mask = 0;
1304 1250
1305 for (int order = 0; order < fp->order; order++) {1251 for (int order = 0; order < fp->order; order++) {
1306 int coeff = fcoeff[order];1252 int32_t coeff = fcoeff[order];
1307 1253
1308 if (coeff < min)1254 bits = FFMAX(number_sbits(coeff), bits);
1309 min = coeff;
1310 if (coeff > max)
1311 max = coeff;
1312 1255
1313 coeff_mask |= coeff;1256 coeff_mask |= coeff;
1314 }1257 }
1315 1258
1316 bits = FFMAX(number_sbits(min), number_sbits(max));1259 shift = FFMIN(7, coeff_mask ? ff_ctz(coeff_mask) : 0);
1317 1260
1318 for (shift = 0; shift < 7 && bits + shift < 16 && !(coeff_mask & (1<<shift)); shift++);1261 fp->coeff_bits = FFMAX(1, bits - shift);
1319 1262 fp->coeff_shift = FFMIN(shift, 16 - fp->coeff_bits);
1320 fp->coeff_bits = bits;
1321 fp->coeff_shift = shift;
1322}1263}
1323 1264
1324/** Determines the best filter parameters for the given data and writes the1265/** Determines the best filter parameters for the given data and writes the
@@ -1332,8 +1273,7 @@ static void set_filter_params(MLPEncodeContext *ctx, · 7 unmodified lines
1332 ChannelParams *cp = &ctx->cur_channel_params[channel];1273 ChannelParams *cp = &ctx->cur_channel_params[channel];
1333 FilterParams *fp = &cp->filter_params[filter];1274 FilterParams *fp = &cp->filter_params[filter];
1334 1275
1335 if ((filter == IIR && ctx->substream_info & SUBSTREAM_INFO_HIGH_RATE) ||1276 if (clear_filter) {
1336 clear_filter) {
1337 fp->order = 0;1277 fp->order = 0;
1338 } else if (filter == IIR) {1278 } else if (filter == IIR) {
1339 fp->order = 0;1279 fp->order = 0;
@@ -1446,8 +1386,7 @@ static void lossless_matrix_coeffs(MLPEncodeContext *ctx) · 106 unmodified lines
1446 unsigned int shift = 0;1386 unsigned int shift = 0;
1447 enum MLPChMode mode;1387 enum MLPChMode mode;
1448 1388
1449 /* No decorrelation for non-stereo. */1389 if (1 || ctx->num_channels - 2 != 2) {
1450 if (ctx->num_channels - 2 != 2) {
1451 mp->count = 0;1390 mp->count = 0;
1452 return;1391 return;
1453 }1392 }
@@ -1490,7 +1429,7 @@ static void lossless_matrix_coeffs(MLPEncodeContext *ctx) · 36 unmodified lines
1490 * codebook is outside the coded value, so it has one more bit of precision.1429 * codebook is outside the coded value, so it has one more bit of precision.
1491 * It should actually be -7 -> 7, shifted down by 0.5.1430 * It should actually be -7 -> 7, shifted down by 0.5.
1492 */1431 */
1493static const int codebook_extremes[3][2] = {1432static const int8_t codebook_extremes[3][2] = {
1494 {-9, 8}, {-8, 7}, {-15, 14},1433 {-9, 8}, {-8, 7}, {-15, 14},
1495};1434};
1496 1435
@@ -1498,7 +1437,7 @@ static const int codebook_extremes[3][2] = { · 1 unmodified line
1498 * codebooks and a specified offset.1437 * codebooks and a specified offset.
1499 */1438 */
1500static void no_codebook_bits_offset(MLPEncodeContext *ctx,1439static void no_codebook_bits_offset(MLPEncodeContext *ctx,
1501 unsigned int channel, int16_t offset,1440 unsigned int channel, int32_t offset,
1502 int32_t min, int32_t max,1441 int32_t min, int32_t max,
1503 BestOffset *bo)1442 BestOffset *bo)
1504{1443{
@@ -1514,7 +1453,7 @@ static void no_codebook_bits_offset(MLPEncodeContext *ctx, · 9 unmodified lines
1514 lsb_bits += !!lsb_bits;1453 lsb_bits += !!lsb_bits;
1515 1454
1516 if (lsb_bits > 0)1455 if (lsb_bits > 0)
1517 unsign = 1 << (lsb_bits - 1);1456 unsign = 1U << (lsb_bits - 1);
1518 1457
1519 bo->offset = offset;1458 bo->offset = offset;
1520 bo->lsb_bits = lsb_bits;1459 bo->lsb_bits = lsb_bits;
@@ -1532,10 +1471,8 @@ static void no_codebook_bits(MLPEncodeContext *ctx, · 11 unmodified lines
1532 BestOffset *bo)1471 BestOffset *bo)
1533{1472{
1534 DecodingParams *dp = ctx->cur_decoding_params;1473 DecodingParams *dp = ctx->cur_decoding_params;
1535 int16_t offset;1474 int32_t offset, unsign = 0;
1536 int32_t unsign = 0;1475 uint8_t lsb_bits;
1537 uint32_t diff;
1538 int lsb_bits;
1539 1476
1540 /* Set offset inside huffoffset's boundaries by adjusting extremes1477 /* Set offset inside huffoffset's boundaries by adjusting extremes
1541 * so that more bits are used, thus shifting the offset. */1478 * so that more bits are used, thus shifting the offset. */
@@ -1544,23 +1481,22 @@ static void no_codebook_bits(MLPEncodeContext *ctx, · 2 unmodified lines
1544 if (max > HUFF_OFFSET_MAX)1481 if (max > HUFF_OFFSET_MAX)
1545 min = FFMIN(min, 2 * HUFF_OFFSET_MAX - max - 1);1482 min = FFMIN(min, 2 * HUFF_OFFSET_MAX - max - 1);
1546 1483
1547 /* Determine offset and minimum number of bits. */1484 lsb_bits = FFMAX(number_sbits(min), number_sbits(max));
1548 diff = max - min;
1549
1550 lsb_bits = number_sbits(diff) - 1;
1551 1485
1552 if (lsb_bits > 0)1486 if (lsb_bits > 0)
1553 unsign = 1 << (lsb_bits - 1);1487 unsign = 1 << (lsb_bits - 1);
1554 1488
1555 /* If all samples are the same (lsb_bits == 0), offset must be1489 /* If all samples are the same (lsb_bits == 0), offset must be
1556 * adjusted because of sign_shift. */1490 * adjusted because of sign_shift. */
1557 offset = min + diff / 2 + !!lsb_bits;1491 offset = min + (max - min) / 2 + !!lsb_bits;
1558 1492
1559 bo->offset = offset;1493 bo->offset = offset;
1560 bo->lsb_bits = lsb_bits;1494 bo->lsb_bits = lsb_bits;
1561 bo->bitcount = lsb_bits * dp->blocksize;1495 bo->bitcount = lsb_bits * dp->blocksize;
1562 bo->min = max - unsign + 1;1496 bo->min = max - unsign + 1;
1563 bo->max = min + unsign;1497 bo->max = min + unsign;
1498 bo->min = FFMAX(bo->min, HUFF_OFFSET_MIN);
1499 bo->max = FFMIN(bo->max, HUFF_OFFSET_MAX);
1564}1500}
1565 1501
1566/** Determines the least amount of bits needed to encode the samples using a1502/** Determines the least amount of bits needed to encode the samples using a
@@ -1569,7 +1505,7 @@ static void no_codebook_bits(MLPEncodeContext *ctx, · 2 unmodified lines
1569static inline void codebook_bits_offset(MLPEncodeContext *ctx,1505static inline void codebook_bits_offset(MLPEncodeContext *ctx,
1570 unsigned int channel, int codebook,1506 unsigned int channel, int codebook,
1571 int32_t sample_min, int32_t sample_max,1507 int32_t sample_min, int32_t sample_max,
1572 int16_t offset, BestOffset *bo)1508 int32_t offset, BestOffset *bo)
1573{1509{
1574 int32_t codebook_min = codebook_extremes[codebook][0];1510 int32_t codebook_min = codebook_extremes[codebook][0];
1575 int32_t codebook_max = codebook_extremes[codebook][1];1511 int32_t codebook_max = codebook_extremes[codebook][1];
@@ -1577,7 +1513,8 @@ static inline void codebook_bits_offset(MLPEncodeContext *ctx, · 1 unmodified line
1577 DecodingParams *dp = ctx->cur_decoding_params;1513 DecodingParams *dp = ctx->cur_decoding_params;
1578 int codebook_offset = 7 + (2 - codebook);1514 int codebook_offset = 7 + (2 - codebook);
1579 int32_t unsign_offset = offset;1515 int32_t unsign_offset = offset;
1580 int lsb_bits = 0, bitcount = 0;1516 uint32_t bitcount = 0;
1517 int lsb_bits = 0;
1581 int offset_min = INT_MAX, offset_max = INT_MAX;1518 int offset_min = INT_MAX, offset_max = INT_MAX;
1582 int unsign, mask;1519 int unsign, mask;
1583 1520
@@ -1634,7 +1571,7 @@ static inline void codebook_bits(MLPEncodeContext *ctx, · 50 unmodified lines
1634 int offset, int32_t min, int32_t max,1571 int offset, int32_t min, int32_t max,
1635 BestOffset *bo, int direction)1572 BestOffset *bo, int direction)
1636{1573{
1637 int previous_count = INT_MAX;1574 uint32_t previous_count = UINT32_MAX;
1638 int offset_min, offset_max;1575 int offset_min, offset_max;
1639 int is_greater = 0;1576 int is_greater = 0;
1640 1577
@@ -1704,8 +1641,8 @@ static void determine_bits(MLPEncodeContext *ctx) · 63 unmodified lines
1704 }1641 }
1705 1642
1706 for (int i = 1; i < NUM_CODEBOOKS; i++) {1643 for (int i = 1; i < NUM_CODEBOOKS; i++) {
1707 BestOffset temp_bo = { 0, INT_MAX, 0, 0, 0, };1644 BestOffset temp_bo = { 0, UINT32_MAX, 0, 0, 0, };
1708 int16_t offset_max;1645 int32_t offset_max;
1709 1646
1710 codebook_bits_offset(ctx, channel, i - 1,1647 codebook_bits_offset(ctx, channel, i - 1,
1711 min, max, offset,1648 min, max, offset,
@@ -1741,13 +1678,14 @@ static void determine_bits(MLPEncodeContext *ctx) · 29 unmodified lines
1741 */1678 */
1742static int apply_filter(MLPEncodeContext *ctx, unsigned int channel)1679static int apply_filter(MLPEncodeContext *ctx, unsigned int channel)
1743{1680{
1744 FilterParams *fp[NUM_FILTERS] = { &ctx->cur_channel_params[channel].filter_params[FIR],1681 ChannelParams *cp = &ctx->cur_channel_params[channel];
1745 &ctx->cur_channel_params[channel].filter_params[IIR], };1682 FilterParams *fp[NUM_FILTERS] = { &cp->filter_params[FIR],
1683 &cp->filter_params[IIR], };
1684 const uint8_t codebook = cp->codebook;
1746 int32_t mask = MSB_MASK(ctx->cur_decoding_params->quant_step_size[channel]);1685 int32_t mask = MSB_MASK(ctx->cur_decoding_params->quant_step_size[channel]);
1747 int32_t *sample_buffer = ctx->sample_buffer + channel;1686 int32_t *sample_buffer = ctx->sample_buffer + channel;
1748 unsigned int number_of_samples = ctx->number_of_samples;1687 unsigned int number_of_samples = ctx->number_of_samples;
1749 unsigned int filter_shift = fp[FIR]->shift;1688 unsigned int filter_shift = fp[FIR]->shift;
1750 int ret = 0;
1751 1689
1752 for (int i = 0; i < 8; i++) {1690 for (int i = 0; i < 8; i++) {
1753 ctx->filter_state_buffer[FIR][i] = *sample_buffer;1691 ctx->filter_state_buffer[FIR][i] = *sample_buffer;
@@ -1759,10 +1697,10 @@ static int apply_filter(MLPEncodeContext *ctx, unsigned int channel) · 5 unmodified lines
1759 for (int i = 8; i < number_of_samples; i++) {1697 for (int i = 8; i < number_of_samples; i++) {
1760 int32_t sample = *sample_buffer;1698 int32_t sample = *sample_buffer;
1761 int64_t accum = 0;1699 int64_t accum = 0;
1762 int64_t residual;1700 int32_t residual;
1763 1701
1764 for (int filter = 0; filter < NUM_FILTERS; filter++) {1702 for (int filter = 0; filter < NUM_FILTERS; filter++) {
1765 int32_t *fcoeff = ctx->cur_channel_params[channel].coeff[filter];1703 int32_t *fcoeff = cp->coeff[filter];
1766 for (unsigned int order = 0; order < fp[filter]->order; order++)1704 for (unsigned int order = 0; order < fp[filter]->order; order++)
1767 accum += (int64_t)ctx->filter_state_buffer[filter][i - 1 - order] *1705 accum += (int64_t)ctx->filter_state_buffer[filter][i - 1 - order] *
1768 fcoeff[order];1706 fcoeff[order];
@@ -1771,13 +1709,13 @@ static int apply_filter(MLPEncodeContext *ctx, unsigned int channel) · 2 unmodified lines
1771 accum >>= filter_shift;1709 accum >>= filter_shift;
1772 residual = sample - (accum & mask);1710 residual = sample - (accum & mask);
1773 1711
1774 if (residual < SAMPLE_MIN(24) || residual > SAMPLE_MAX(24)) {1712 if ((codebook > 0) &&
1775 ret = AVERROR_INVALIDDATA;1713 (residual < SAMPLE_MIN(24) ||
1776 return ret;1714 residual > SAMPLE_MAX(24)))
1777 }1715 return -1;
1778 1716
1779 ctx->filter_state_buffer[FIR][i] = sample;1717 ctx->filter_state_buffer[FIR][i] = sample;
1780 ctx->filter_state_buffer[IIR][i] = (int32_t) residual;1718 ctx->filter_state_buffer[IIR][i] = residual;
1781 1719
1782 sample_buffer += ctx->num_channels;1720 sample_buffer += ctx->num_channels;
1783 }1721 }
@@ -1789,7 +1727,7 @@ static int apply_filter(MLPEncodeContext *ctx, unsigned int channel) · 5 unmodified lines
1789 sample_buffer += ctx->num_channels;1727 sample_buffer += ctx->num_channels;
1790 }1728 }
1791 1729
1792 return ret;1730 return 0;
1793}1731}
1794 1732
1795static void apply_filters(MLPEncodeContext *ctx)1733static void apply_filters(MLPEncodeContext *ctx)
@@ -1859,10 +1797,10 @@ static void rematrix_channels(MLPEncodeContext *ctx) · 63 unmodified lines
1859 **** Functions that deal with determining the best parameters and output ***1797 **** Functions that deal with determining the best parameters and output ***
1860 ****************************************************************************/1798 ****************************************************************************/
1861 1799
1862typedef struct {1800typedef struct PathCounter {
1863 char path[MAJOR_HEADER_INTERVAL + 2];1801 char path[MAJOR_HEADER_INTERVAL + 2];
1864 int cur_idx;1802 int cur_idx;
1865 int bitcount;1803 uint32_t bitcount;
1866} PathCounter;1804} PathCounter;
1867 1805
1868#define CODEBOOK_CHANGE_BITS 211806#define CODEBOOK_CHANGE_BITS 21
@@ -1877,14 +1815,14 @@ static int compare_best_offset(const BestOffset *prev, const BestOffset *cur) · 8 unmodified lines
1877 return prev->lsb_bits != cur->lsb_bits;1815 return prev->lsb_bits != cur->lsb_bits;
1878}1816}
1879 1817
1880static int best_codebook_path_cost(MLPEncodeContext *ctx, unsigned int channel,1818static uint32_t best_codebook_path_cost(MLPEncodeContext *ctx, unsigned int channel,
1881 PathCounter *src, int cur_codebook)1819 PathCounter *src, int cur_codebook)
1882{1820{
1883 int idx = src->cur_idx;1821 int idx = src->cur_idx;
1884 const BestOffset *cur_bo = ctx->best_offset[idx][channel],1822 const BestOffset *cur_bo = ctx->b[idx].best_offset[channel],
1885 *prev_bo = idx ? ctx->best_offset[idx - 1][channel] :1823 *prev_bo = idx ? ctx->b[idx - 1].best_offset[channel] :
1886 restart_best_offset;1824 restart_best_offset;
1887 int bitcount = src->bitcount;1825 uint32_t bitcount = src->bitcount;
1888 int prev_codebook = src->path[idx];1826 int prev_codebook = src->path[idx];
1889 1827
1890 bitcount += cur_bo[cur_codebook].bitcount;1828 bitcount += cur_bo[cur_codebook].bitcount;
@@ -1898,7 +1836,6 @@ static int best_codebook_path_cost(MLPEncodeContext *ctx, unsigned int channel, · 7 unmodified lines
1898 1836
1899static void set_best_codebook(MLPEncodeContext *ctx)1837static void set_best_codebook(MLPEncodeContext *ctx)
1900{1838{
1901 DecodingParams *dp = ctx->cur_decoding_params;
1902 RestartHeader *rh = ctx->cur_restart_header;1839 RestartHeader *rh = ctx->cur_restart_header;
1903 1840
1904 for (unsigned int channel = rh->min_channel; channel <= rh->max_channel; channel++) {1841 for (unsigned int channel = rh->min_channel; channel <= rh->max_channel; channel++) {
@@ -1911,17 +1848,17 @@ static void set_best_codebook(MLPEncodeContext *ctx) · 6 unmodified lines
1911 clear_path_counter(path_counter);1848 clear_path_counter(path_counter);
1912 1849
1913 for (unsigned int index = 0; index < ctx->number_of_subblocks; index++) {1850 for (unsigned int index = 0; index < ctx->number_of_subblocks; index++) {
1914 unsigned int best_bitcount = INT_MAX;1851 uint32_t best_bitcount = UINT32_MAX;
1915 1852
1916 cur_bo = ctx->best_offset[index][channel];1853 cur_bo = ctx->b[index].best_offset[channel];
1917 1854
1918 for (unsigned int codebook = 0; codebook < NUM_CODEBOOKS; codebook++) {1855 for (unsigned int codebook = 0; codebook < NUM_CODEBOOKS; codebook++) {
1919 int prev_best_bitcount = INT_MAX;1856 uint32_t prev_best_bitcount = UINT32_MAX;
1920 1857
1921 for (unsigned int last_best = 0; last_best < 2; last_best++) {1858 for (unsigned int last_best = 0; last_best < 2; last_best++) {
1922 PathCounter *dst_path = &path_counter[codebook];1859 PathCounter *dst_path = &path_counter[codebook];
1923 PathCounter *src_path;1860 PathCounter *src_path;
1924 int temp_bitcount;1861 uint32_t temp_bitcount;
1925 1862
1926 /* First test last path with same headers,1863 /* First test last path with same headers,
1927 * then with last best. */1864 * then with last best. */
@@ -1961,10 +1898,11 @@ static void set_best_codebook(MLPEncodeContext *ctx) · 33 unmodified lines
1961 1898
1962 /* Update context. */1899 /* Update context. */
1963 for (unsigned int index = 0; index < ctx->number_of_subblocks; index++) {1900 for (unsigned int index = 0; index < ctx->number_of_subblocks; index++) {
1964 ChannelParams *cp = ctx->seq_channel_params + index*(ctx->avctx->ch_layout.nb_channels) + channel;1901 ChannelParams *cp = &ctx->b[index].channel_params[channel];
1902 DecodingParams *dp = &ctx->b[index].decoding_params;
1965 1903
1966 best_codebook = *best_path++;1904 best_codebook = *best_path++;
1967 cur_bo = &ctx->best_offset[index][channel][best_codebook];1905 cur_bo = &ctx->b[index].best_offset[channel][best_codebook];
1968 1906
1969 cp->huff_offset = cur_bo->offset;1907 cp->huff_offset = cur_bo->offset;
1970 cp->huff_lsbs = cur_bo->lsb_bits + dp->quant_step_size[channel];1908 cp->huff_lsbs = cur_bo->lsb_bits + dp->quant_step_size[channel];
@@ -1980,20 +1918,18 @@ static void set_best_codebook(MLPEncodeContext *ctx) · 9 unmodified lines
1980static void set_major_params(MLPEncodeContext *ctx)1918static void set_major_params(MLPEncodeContext *ctx)
1981{1919{
1982 RestartHeader *rh = ctx->cur_restart_header;1920 RestartHeader *rh = ctx->cur_restart_header;
1983 uint8_t max_huff_lsbs = 0;
1984 uint8_t max_output_bits = 0;
1985 int channels = ctx->avctx->ch_layout.nb_channels;1921 int channels = ctx->avctx->ch_layout.nb_channels;
1986 DecodingParams *seq_dp = ctx->decoding_params + ctx->seq_offset[0] * channels;1922 uint8_t max_huff_lsbs = 0, max_output_bits = 0;
1987 ChannelParams *seq_cp = ctx->channel_params + ctx->seq_offset[0] * channels;
1988 1923
1989 for (unsigned int index = 0; index < ctx->seq_size[ctx->restart_intervals-1]; index++) {1924 for (unsigned int index = 0; index < ctx->b[ctx->restart_intervals-1].seq_size; index++) {
1990 memcpy(&ctx->major_decoding_params[index], seq_dp + index, sizeof(DecodingParams));1925 memcpy(&ctx->b[index].major_decoding_params,
1926 &ctx->b[index].decoding_params, sizeof(DecodingParams));
1991 for (unsigned int channel = 0; channel < channels; channel++) {1927 for (unsigned int channel = 0; channel < channels; channel++) {
1992 uint8_t huff_lsbs = (seq_cp + index*(channels) + channel)->huff_lsbs;1928 uint8_t huff_lsbs = ctx->b[index].channel_params[channel].huff_lsbs;
1993 if (max_huff_lsbs < huff_lsbs)1929 if (max_huff_lsbs < huff_lsbs)
1994 max_huff_lsbs = huff_lsbs;1930 max_huff_lsbs = huff_lsbs;
1995 memcpy(&ctx->major_channel_params[index][channel],1931 memcpy(&ctx->b[index].major_channel_params[channel],
1996 (seq_cp + index*(channels) + channel),1932 &ctx->b[index].channel_params[channel],
1997 sizeof(ChannelParams));1933 sizeof(ChannelParams));
1998 }1934 }
1999 }1935 }
@@ -2001,8 +1937,8 @@ static void set_major_params(MLPEncodeContext *ctx) · 1 unmodified line
2001 rh->max_huff_lsbs = max_huff_lsbs;1937 rh->max_huff_lsbs = max_huff_lsbs;
2002 1938
2003 for (unsigned int index = 0; index < ctx->number_of_frames; index++)1939 for (unsigned int index = 0; index < ctx->number_of_frames; index++)
2004 if (max_output_bits < ctx->max_output_bits[index])1940 if (max_output_bits < ctx->b[index].max_output_bits)
2005 max_output_bits = ctx->max_output_bits[index];1941 max_output_bits = ctx->b[index].max_output_bits;
2006 rh->max_output_bits = max_output_bits;1942 rh->max_output_bits = max_output_bits;
2007 1943
2008 ctx->cur_restart_header = &ctx->restart_header;1944 ctx->cur_restart_header = &ctx->restart_header;
@@ -2011,10 +1947,10 @@ static void set_major_params(MLPEncodeContext *ctx) · 2 unmodified lines
2011 ctx->prev_channel_params = restart_channel_params;1947 ctx->prev_channel_params = restart_channel_params;
2012 1948
2013 for (unsigned int index = 0; index < MAJOR_HEADER_INTERVAL + 1; index++) {1949 for (unsigned int index = 0; index < MAJOR_HEADER_INTERVAL + 1; index++) {
2014 ctx->cur_decoding_params = &ctx->major_decoding_params[index];1950 ctx->cur_decoding_params = &ctx->b[index].major_decoding_params;
2015 ctx->cur_channel_params = ctx->major_channel_params[index];1951 ctx->cur_channel_params = ctx->b[index].major_channel_params;
2016 1952
2017 ctx->major_params_changed[index] = compare_decoding_params(ctx);1953 ctx->b[index].major_params_changed = compare_decoding_params(ctx);
2018 1954
2019 ctx->prev_decoding_params = ctx->cur_decoding_params;1955 ctx->prev_decoding_params = ctx->cur_decoding_params;
2020 ctx->prev_channel_params = ctx->cur_channel_params;1956 ctx->prev_channel_params = ctx->cur_channel_params;
@@ -2027,12 +1963,9 @@ static void set_major_params(MLPEncodeContext *ctx) · 6 unmodified lines
2027 1963
2028static void analyze_sample_buffer(MLPEncodeContext *ctx)1964static void analyze_sample_buffer(MLPEncodeContext *ctx)
2029{1965{
2030 ChannelParams *seq_cp = ctx->seq_channel_params;
2031 DecodingParams *seq_dp = ctx->seq_decoding_params;
2032
2033 ctx->cur_restart_header = &ctx->restart_header;1966 ctx->cur_restart_header = &ctx->restart_header;
2034 ctx->cur_decoding_params = seq_dp + 1;1967 ctx->cur_decoding_params = &ctx->b[1].decoding_params;
2035 ctx->cur_channel_params = seq_cp + ctx->avctx->ch_layout.nb_channels;1968 ctx->cur_channel_params = ctx->b[1].channel_params;
2036 1969
2037 determine_quant_step_size(ctx);1970 determine_quant_step_size(ctx);
2038 generate_2_noise_channels(ctx);1971 generate_2_noise_channels(ctx);
@@ -2047,20 +1980,20 @@ static void analyze_sample_buffer(MLPEncodeContext *ctx) · 8 unmodified lines
2047 * decoding_params[0] is for the filter state subblock.1980 * decoding_params[0] is for the filter state subblock.
2048 */1981 */
2049 for (unsigned int index = 0; index < ctx->number_of_frames; index++) {1982 for (unsigned int index = 0; index < ctx->number_of_frames; index++) {
2050 DecodingParams *dp = seq_dp + (index + 1);1983 DecodingParams *dp = &ctx->b[index + 1].decoding_params;
2051 dp->blocksize = ctx->avctx->frame_size;1984 dp->blocksize = ctx->avctx->frame_size;
2052 }1985 }
2053 /* The official encoder seems to always encode a filter state subblock1986 /* The official encoder seems to always encode a filter state subblock
2054 * even if there are no filters. TODO check if it is possible to skip1987 * even if there are no filters. TODO check if it is possible to skip
2055 * the filter state subblock for no filters.1988 * the filter state subblock for no filters.
2056 */1989 */
2057 (seq_dp + 0)->blocksize = 8;1990 ctx->b[0].decoding_params.blocksize = 8;
2058 (seq_dp + 1)->blocksize -= 8;1991 ctx->b[1].decoding_params.blocksize -= 8;
2059 1992
2060 for (unsigned int index = 0; index < ctx->number_of_subblocks; index++) {1993 for (unsigned int index = 0; index < ctx->number_of_subblocks; index++) {
2061 ctx->cur_decoding_params = seq_dp + index;1994 ctx->cur_decoding_params = &ctx->b[index].decoding_params;
2062 ctx->cur_channel_params = seq_cp + index*(ctx->avctx->ch_layout.nb_channels);1995 ctx->cur_channel_params = ctx->b[index].channel_params;
2063 ctx->cur_best_offset = ctx->best_offset[index];1996 ctx->cur_best_offset = ctx->b[index].best_offset;
2064 determine_bits(ctx);1997 determine_bits(ctx);
2065 ctx->sample_buffer += ctx->cur_decoding_params->blocksize * ctx->num_channels;1998 ctx->sample_buffer += ctx->cur_decoding_params->blocksize * ctx->num_channels;
2066 }1999 }
@@ -2077,8 +2010,8 @@ static void process_major_frame(MLPEncodeContext *ctx) · 10 unmodified lines
2077 2010
2078 ctx->cur_restart_header = &ctx->restart_header;2011 ctx->cur_restart_header = &ctx->restart_header;
2079 2012
2080 ctx->cur_decoding_params = &ctx->major_decoding_params[1];2013 ctx->cur_decoding_params = &ctx->b[1].major_decoding_params;
2081 ctx->cur_channel_params = ctx->major_channel_params[1];2014 ctx->cur_channel_params = ctx->b[1].major_channel_params;
2082 2015
2083 generate_2_noise_channels(ctx);2016 generate_2_noise_channels(ctx);
2084 rematrix_channels (ctx);2017 rematrix_channels (ctx);
@@ -2168,16 +2101,12 @@ input_and_return: · 83 unmodified lines
2168 ctx->number_of_frames = ctx->next_major_number_of_frames;2101 ctx->number_of_frames = ctx->next_major_number_of_frames;
2169 ctx->number_of_subblocks = ctx->next_major_number_of_frames + 1;2102 ctx->number_of_subblocks = ctx->next_major_number_of_frames + 1;
2170 2103
2171 ctx->seq_channel_params = ctx->channel_params + ctx->seq_offset[seq_index] * channels;
2172
2173 ctx->seq_decoding_params = ctx->decoding_params + ctx->seq_offset[seq_index];
2174
2175 number_of_samples = avctx->frame_size * ctx->number_of_frames;2104 number_of_samples = avctx->frame_size * ctx->number_of_frames;
2176 ctx->number_of_samples = number_of_samples;2105 ctx->number_of_samples = number_of_samples;
2177 2106
2178 for (unsigned int index = 0; index < ctx->seq_size[seq_index]; index++) {2107 for (unsigned int index = 0; index < ctx->b[seq_index].seq_size; index++) {
2179 clear_channel_params(ctx->seq_channel_params + index * channels, channels);2108 clear_channel_params(ctx->b[index].channel_params, channels);
2180 default_decoding_params(ctx, ctx->seq_decoding_params + index);2109 default_decoding_params(ctx, &ctx->b[index].decoding_params);
2181 }2110 }
2182 2111
2183 input_to_sample_buffer(ctx);2112 input_to_sample_buffer(ctx);
@@ -2219,18 +2148,10 @@ static av_cold int mlp_encode_close(AVCodecContext *avctx) · 35 unmodified lines
2219 2148
2220 ff_lpc_end(&ctx->lpc_ctx);2149 ff_lpc_end(&ctx->lpc_ctx);
2221 2150
2222 av_freep(&ctx->lossless_check_data);
2223 av_freep(&ctx->major_scratch_buffer);2151 av_freep(&ctx->major_scratch_buffer);
2224 av_freep(&ctx->major_inout_buffer);2152 av_freep(&ctx->major_inout_buffer);
2225 av_freep(&ctx->lpc_sample_buffer);
2226 av_freep(&ctx->decoding_params);
2227 av_freep(&ctx->channel_params);
2228 av_freep(&ctx->max_output_bits);
2229 ff_af_queue_close(&ctx->afq);2153 ff_af_queue_close(&ctx->afq);
2230 2154
2231 for (int i = 0; i < NUM_FILTERS; i++)
2232 av_freep(&ctx->filter_state_buffer[i]);
2233
2234 return 0;2155 return 0;
2235}2156}
2236 2157

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