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
4242
4343 #define MLP_MIN_LPC_ORDER 1
4444 #define MLP_MAX_LPC_ORDER 8
45−#define MLP_MIN_LPC_SHIFT 0
45+#define MLP_MIN_LPC_SHIFT 8
4646 #define MLP_MAX_LPC_SHIFT 15
4747
48−typedef struct {
48+typedef struct RestartHeader {
4949 uint8_t min_channel; ///< The index of the first channel coded in this substream.
5050 uint8_t max_channel; ///< The index of the last channel coded in this substream.
5151 uint8_t max_matrix_channel; ///< The number of channels input into the rematrix stage.
@@ -53,7 +53,7 @@ typedef struct { · 1 unmodified line
5353 uint8_t noise_shift; ///< The left shift applied to random noise in 0x31ea substreams.
5454 uint32_t noisegen_seed; ///< The current seed value for the pseudorandom noise generator(s).
5555
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.
5757
5858 int32_t lossless_check_data; ///< XOR of all output samples
5959
@@ -61,7 +61,7 @@ typedef struct { · 1 unmodified line
6161 uint8_t max_output_bits; ///< largest output bit-depth
6262 } RestartHeader;
6363
64−typedef struct {
64+typedef struct MatrixParams {
6565 uint8_t count; ///< number of matrices to apply
6666
6767 uint8_t outch[MAX_MATRICES]; ///< output channel for each matrix
@@ -85,7 +85,7 @@ enum ParamFlags { · 17 unmodified lines
8585 PARAM_PRESENT = 1 << 0,
8686 };
8787
88−typedef struct {
88+typedef struct DecodingParams {
8989 uint16_t blocksize; ///< number of PCM samples in current audio block
9090 uint8_t quant_step_size[MAX_CHANNELS]; ///< left shift to apply to Huffman-decoded residuals
9191
@@ -96,8 +96,8 @@ typedef struct { · 4 unmodified lines
9696
9797 typedef struct BestOffset {
9898 int32_t offset;
99− int bitcount;
100− int lsb_bits;
99+ uint32_t bitcount;
100+ uint8_t lsb_bits;
101101 int32_t min;
102102 int32_t max;
103103 } BestOffset;
@@ -108,6 +108,18 @@ typedef struct BestOffset { · 4 unmodified lines
108108 /** Number of possible codebooks (counting "no codebooks") */
109109 #define NUM_CODEBOOKS 4
110110
111+typedef 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+
111123 typedef struct MLPEncodeContext {
112124 AVClass *class;
113125 AVCodecContext *avctx;
@@ -142,17 +154,12 @@ typedef struct MLPEncodeContext { · 28 unmodified lines
142154 int32_t *major_scratch_buffer; ///< Scratch buffer big enough to fit all data for one entire major frame interval.
143155 int32_t last_frames; ///< Signal last frames.
144156
145− int32_t *lpc_sample_buffer;
146−
147157 unsigned int major_number_of_frames;
148158 unsigned int next_major_number_of_frames;
149159
150160 unsigned int major_frame_size; ///< Number of samples in current major frame being encoded.
151161 unsigned int next_major_frame_size; ///< Counter of number of samples for next major frame.
152162
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
156163 unsigned int frame_index; ///< Index of current frame being encoded.
157164
158165 unsigned int one_sample_buffer_size; ///< Number of samples*channel for one access unit.
@@ -169,21 +176,11 @@ typedef struct MLPEncodeContext { · 10 unmodified lines
169176 uint8_t ch_modifier_thd0; ///< channel modifier for TrueHD stream 0
170177 uint8_t ch_modifier_thd1; ///< channel modifier for TrueHD stream 1
171178 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;
182179 RestartHeader restart_header;
183180
184− ChannelParams major_channel_params[MAJOR_HEADER_INTERVAL+1][MAX_CHANNELS]; ///< ChannelParams to be written to bitstream.
185− DecodingParams major_decoding_params[MAJOR_HEADER_INTERVAL+1]; ///< DecodingParams to be written to bitstream.
186− int major_params_changed[MAJOR_HEADER_INTERVAL+1]; ///< params_changed to be written to bitstream.
181+ MLPBlock b[MAJOR_HEADER_INTERVAL + 1];
182+ int32_t lpc_sample_buffer[MAJOR_HEADER_INTERVAL * MAX_BLOCKSIZE];
183+ int32_t filter_state_buffer[NUM_FILTERS][MAX_BLOCKSIZE * MAJOR_HEADER_INTERVAL];
187184
188185 unsigned int major_cur_subblock_index;
189186 unsigned int major_filter_state_subblock;
@@ -200,16 +197,10 @@ typedef struct MLPEncodeContext { · 10 unmodified lines
200197 unsigned int number_of_frames;
201198 unsigned int number_of_samples;
202199 unsigned int number_of_subblocks;
203− unsigned int seq_index; ///< Sequence index for high compression levels.
204200
205201 const ChannelParams *prev_channel_params;
206202 const DecodingParams *prev_decoding_params;
207203
208− ChannelParams *seq_channel_params;
209− DecodingParams *seq_decoding_params;
210−
211− int32_t *filter_state_buffer[NUM_FILTERS];
212−
213204 unsigned int max_codebook_search;
214205
215206 int shorten_by;
@@ -387,12 +378,12 @@ static void copy_matrix_params(MatrixParams *dst, MatrixParams *src) · 171 unmodified lines
387378 static void copy_restart_frame_params(MLPEncodeContext *ctx)
388379 {
389380 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;
391382
392383 copy_matrix_params(&dp->matrix_params, &ctx->cur_decoding_params->matrix_params);
393384
394385 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];
396387
397388 dp->quant_step_size[channel] = ctx->cur_decoding_params->quant_step_size[channel];
398389 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
417408 }
418409
419410 /** Clears a ChannelParams struct the way it should be after a restart header. */
420−static void clear_channel_params(ChannelParams channel_params[MAX_CHANNELS], int nb_channels)
411+static void clear_channel_params(ChannelParams *channel_params, int nb_channels)
421412 {
422413 for (unsigned channel = 0; channel < nb_channels; channel++) {
423414 ChannelParams *cp = &channel_params[channel];
@@ -456,12 +447,9 @@ static void default_decoding_params(MLPEncodeContext *ctx, DecodingParams *decod · 32 unmodified lines
456447 /** Calculates the smallest number of bits it takes to encode a given signed
457448 * value in two's complement.
458449 */
459−static int inline number_sbits(int number)
450+static int inline number_sbits(int32_t n)
460451 {
461− if (number < -1)
462− number++;
463−
464− return av_log2(FFABS(number)) + 1 + !!number;
452+ return 33 - ff_clz(FFABS(n)|1);
465453 }
466454
467455 enum InputBitDepth {
@@ -488,7 +476,6 @@ static av_cold int mlp_encode_init(AVCodecContext *avctx) · 20 unmodified lines
488476 MLPEncodeContext *ctx = avctx->priv_data;
489477 RestartHeader *const rh = &ctx->restart_header;
490478 uint64_t channels_present;
491− unsigned int sum = 0;
492479 size_t size;
493480 int ret;
494481
@@ -579,11 +566,6 @@ static av_cold int mlp_encode_init(AVCodecContext *avctx) · 84 unmodified lines
579566 ctx->min_restart_interval = MAJOR_HEADER_INTERVAL;
580567 ctx->restart_intervals = ctx->max_restart_interval / ctx->min_restart_interval;
581568
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−
587569 size = ctx->one_sample_buffer_size * ctx->max_restart_interval;
588570 ctx->major_scratch_buffer = av_calloc(size, sizeof(*ctx->major_scratch_buffer));
589571 if (!ctx->major_scratch_buffer)
@@ -651,31 +633,8 @@ static av_cold int mlp_encode_init(AVCodecContext *avctx) · 61 unmodified lines
651633 ctx->summary_info = 0;
652634 }
653635
654− size = ctx->max_restart_interval;
655− ctx->max_output_bits = av_calloc(size, sizeof(*ctx->max_output_bits));
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);
636+ for (unsigned int index = 0; index < ctx->restart_intervals; index++)
637+ ctx->b[index].seq_size = ((index + 1) * ctx->min_restart_interval) + 1;
679638
680639 /* TODO see if noisegen_seed is really worth it. */
681640 rh->noisegen_seed = 0;
@@ -689,13 +648,6 @@ static av_cold int mlp_encode_init(AVCodecContext *avctx) · 7 unmodified lines
689648 MLP_MAX_LPC_ORDER, ctx->lpc_type)) < 0)
690649 return ret;
691650
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−
699651 ff_af_queue_init(avctx, &ctx->afq);
700652
701653 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
967919 }
968920 }
969921 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);
971923 }
972924
973925 put_bits(pb, 2, cp->codebook );
@@ -1017,9 +969,10 @@ static void write_block_data(MLPEncodeContext *ctx, PutBitContext *pb) · 43 unmodified lines
1017969 int vlc = sample >> lsb_bits[ch];
1018970 put_bits(pb, ff_mlp_huffman_tables[codebook_index[ch]][vlc][1],
1019971 ff_mlp_huffman_tables[codebook_index[ch]][vlc][0]);
972+ sample &= ((1 << lsb_bits[ch]) - 1);
1020973 }
1021974
1022− put_sbits(pb, lsb_bits[ch], sample);
975+ put_bits(pb, lsb_bits[ch], sample);
1023976 }
1024977 sample_buffer += 2; /* noise channels */
1025978 }
@@ -1032,7 +985,7 @@ static uint8_t *write_substr(MLPEncodeContext *ctx, uint8_t *buf, int buf_size, · 6 unmodified lines
1032985 int restart_frame,
1033986 uint16_t substream_data_len[MAX_SUBSTREAMS])
1034987 {
1035− int32_t *lossless_check_data = ctx->lossless_check_data;
988+ int32_t *lossless_check_data = &ctx->b[ctx->frame_index].lossless_check_data;
1036989 unsigned int cur_subblock_index = ctx->major_cur_subblock_index;
1037990 unsigned int num_subblocks = ctx->major_filter_state_subblock;
1038991 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
1042995 int params_changed;
1043996 int end = 0;
1044997
1045− lossless_check_data += ctx->frame_index;
1046998 ctx->cur_restart_header = rh;
1047999
10481000 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
10521004
10531005 subblock_index = cur_subblock_index++;
10541006
1055− ctx->cur_decoding_params = &ctx->major_decoding_params[subblock_index];
1056− ctx->cur_channel_params = ctx->major_channel_params[subblock_index];
1007+ ctx->cur_decoding_params = &ctx->b[subblock_index].major_decoding_params;
1008+ ctx->cur_channel_params = ctx->b[subblock_index].major_channel_params;
10571009
1058− params_changed = ctx->major_params_changed[subblock_index];
1010+ params_changed = ctx->b[subblock_index].major_params_changed;
10591011
10601012 if (substr_restart_frame || params_changed) {
10611013 put_bits(&pb, 1, 1);
@@ -1202,26 +1154,21 @@ static void input_data_internal(MLPEncodeContext *ctx, const uint8_t *samples, · 140 unmodified lines
12021154 int nb_samples,
12031155 int is24)
12041156 {
1205− int32_t *lossless_check_data = ctx->lossless_check_data;
1157+ int32_t *lossless_check_data = &ctx->b[ctx->frame_index].lossless_check_data;
12061158 const int32_t *samples_32 = (const int32_t *) samples;
12071159 const int16_t *samples_16 = (const int16_t *) samples;
12081160 RestartHeader *rh = &ctx->restart_header;
12091161 int32_t *sample_buffer = ctx->inout_buffer;
12101162 int32_t temp_lossless_check_data = 0;
1211− uint32_t greatest = 0;
1212−
1213− lossless_check_data += ctx->frame_index;
1163+ uint32_t bits = 0;
12141164
12151165 for (int i = 0; i < nb_samples; i++) {
12161166 for (unsigned int channel = 0; channel <= rh->max_channel; channel++) {
1217− uint32_t abs_sample;
12181167 int32_t sample;
12191168
12201169 sample = is24 ? *samples_32++ >> 8 : *samples_16++ * 256;
12211170
1222− /* TODO Find out if number_sbits can be used for negative values. */
1223− abs_sample = FFABS(sample);
1224− greatest = FFMAX(greatest, abs_sample);
1171+ bits = FFMAX(number_sbits(sample), bits);
12251172
12261173 temp_lossless_check_data ^= (sample & 0x00ffffff) << channel;
12271174 *sample_buffer++ = sample;
@@ -1230,7 +1177,7 @@ static void input_data_internal(MLPEncodeContext *ctx, const uint8_t *samples, · 2 unmodified lines
12301177 sample_buffer += 2; /* noise channels */
12311178 }
12321179
1233− ctx->max_output_bits[ctx->frame_index] = number_sbits(greatest);
1180+ ctx->b[ctx->frame_index].max_output_bits = bits;
12341181
12351182 *lossless_check_data++ = temp_lossless_check_data;
12361183 }
@@ -1265,7 +1212,7 @@ static void input_to_sample_buffer(MLPEncodeContext *ctx) · 28 unmodified lines
12651212 /** Counts the number of trailing zeroes in a value */
12661213 static int number_trailing_zeroes(int32_t sample)
12671214 {
1268− return FFMIN(15, ff_ctz(sample));
1215+ return sample ? FFMIN(15, ff_ctz(sample)) : 0;
12691216 }
12701217
12711218 /** 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
12981245 */
12991246 static void code_filter_coeffs(MLPEncodeContext *ctx, FilterParams *fp, int32_t *fcoeff)
13001247 {
1301− int min = INT_MAX, max = INT_MIN;
1302− int bits, shift;
1303− int coeff_mask = 0;
1248+ uint32_t coeff_mask = 0;
1249+ int bits = 0, shift;
13041250
13051251 for (int order = 0; order < fp->order; order++) {
1306− int coeff = fcoeff[order];
1252+ int32_t coeff = fcoeff[order];
13071253
1308− if (coeff < min)
1309− min = coeff;
1310− if (coeff > max)
1311− max = coeff;
1254+ bits = FFMAX(number_sbits(coeff), bits);
13121255
13131256 coeff_mask |= coeff;
13141257 }
13151258
1316− bits = FFMAX(number_sbits(min), number_sbits(max));
1259+ shift = FFMIN(7, coeff_mask ? ff_ctz(coeff_mask) : 0);
13171260
1318− for (shift = 0; shift < 7 && bits + shift < 16 && !(coeff_mask & (1<<shift)); shift++);
1319−
1320− fp->coeff_bits = bits;
1321− fp->coeff_shift = shift;
1261+ fp->coeff_bits = FFMAX(1, bits - shift);
1262+ fp->coeff_shift = FFMIN(shift, 16 - fp->coeff_bits);
13221263 }
13231264
13241265 /** 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
13321273 ChannelParams *cp = &ctx->cur_channel_params[channel];
13331274 FilterParams *fp = &cp->filter_params[filter];
13341275
1335− if ((filter == IIR && ctx->substream_info & SUBSTREAM_INFO_HIGH_RATE) ||
1336− clear_filter) {
1276+ if (clear_filter) {
13371277 fp->order = 0;
13381278 } else if (filter == IIR) {
13391279 fp->order = 0;
@@ -1446,8 +1386,7 @@ static void lossless_matrix_coeffs(MLPEncodeContext *ctx) · 106 unmodified lines
14461386 unsigned int shift = 0;
14471387 enum MLPChMode mode;
14481388
1449− /* No decorrelation for non-stereo. */
1450− if (ctx->num_channels - 2 != 2) {
1389+ if (1 || ctx->num_channels - 2 != 2) {
14511390 mp->count = 0;
14521391 return;
14531392 }
@@ -1490,7 +1429,7 @@ static void lossless_matrix_coeffs(MLPEncodeContext *ctx) · 36 unmodified lines
14901429 * codebook is outside the coded value, so it has one more bit of precision.
14911430 * It should actually be -7 -> 7, shifted down by 0.5.
14921431 */
1493−static const int codebook_extremes[3][2] = {
1432+static const int8_t codebook_extremes[3][2] = {
14941433 {-9, 8}, {-8, 7}, {-15, 14},
14951434 };
14961435
@@ -1498,7 +1437,7 @@ static const int codebook_extremes[3][2] = { · 1 unmodified line
14981437 * codebooks and a specified offset.
14991438 */
15001439 static void no_codebook_bits_offset(MLPEncodeContext *ctx,
1501− unsigned int channel, int16_t offset,
1440+ unsigned int channel, int32_t offset,
15021441 int32_t min, int32_t max,
15031442 BestOffset *bo)
15041443 {
@@ -1514,7 +1453,7 @@ static void no_codebook_bits_offset(MLPEncodeContext *ctx, · 9 unmodified lines
15141453 lsb_bits += !!lsb_bits;
15151454
15161455 if (lsb_bits > 0)
1517− unsign = 1 << (lsb_bits - 1);
1456+ unsign = 1U << (lsb_bits - 1);
15181457
15191458 bo->offset = offset;
15201459 bo->lsb_bits = lsb_bits;
@@ -1532,10 +1471,8 @@ static void no_codebook_bits(MLPEncodeContext *ctx, · 11 unmodified lines
15321471 BestOffset *bo)
15331472 {
15341473 DecodingParams *dp = ctx->cur_decoding_params;
1535− int16_t offset;
1536− int32_t unsign = 0;
1537− uint32_t diff;
1538− int lsb_bits;
1474+ int32_t offset, unsign = 0;
1475+ uint8_t lsb_bits;
15391476
15401477 /* Set offset inside huffoffset's boundaries by adjusting extremes
15411478 * so that more bits are used, thus shifting the offset. */
@@ -1544,23 +1481,22 @@ static void no_codebook_bits(MLPEncodeContext *ctx, · 2 unmodified lines
15441481 if (max > HUFF_OFFSET_MAX)
15451482 min = FFMIN(min, 2 * HUFF_OFFSET_MAX - max - 1);
15461483
1547− /* Determine offset and minimum number of bits. */
1548− diff = max - min;
1549−
1550− lsb_bits = number_sbits(diff) - 1;
1484+ lsb_bits = FFMAX(number_sbits(min), number_sbits(max));
15511485
15521486 if (lsb_bits > 0)
15531487 unsign = 1 << (lsb_bits - 1);
15541488
15551489 /* If all samples are the same (lsb_bits == 0), offset must be
15561490 * adjusted because of sign_shift. */
1557− offset = min + diff / 2 + !!lsb_bits;
1491+ offset = min + (max - min) / 2 + !!lsb_bits;
15581492
15591493 bo->offset = offset;
15601494 bo->lsb_bits = lsb_bits;
15611495 bo->bitcount = lsb_bits * dp->blocksize;
15621496 bo->min = max - unsign + 1;
15631497 bo->max = min + unsign;
1498+ bo->min = FFMAX(bo->min, HUFF_OFFSET_MIN);
1499+ bo->max = FFMIN(bo->max, HUFF_OFFSET_MAX);
15641500 }
15651501
15661502 /** 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
15691505 static inline void codebook_bits_offset(MLPEncodeContext *ctx,
15701506 unsigned int channel, int codebook,
15711507 int32_t sample_min, int32_t sample_max,
1572− int16_t offset, BestOffset *bo)
1508+ int32_t offset, BestOffset *bo)
15731509 {
15741510 int32_t codebook_min = codebook_extremes[codebook][0];
15751511 int32_t codebook_max = codebook_extremes[codebook][1];
@@ -1577,7 +1513,8 @@ static inline void codebook_bits_offset(MLPEncodeContext *ctx, · 1 unmodified line
15771513 DecodingParams *dp = ctx->cur_decoding_params;
15781514 int codebook_offset = 7 + (2 - codebook);
15791515 int32_t unsign_offset = offset;
1580− int lsb_bits = 0, bitcount = 0;
1516+ uint32_t bitcount = 0;
1517+ int lsb_bits = 0;
15811518 int offset_min = INT_MAX, offset_max = INT_MAX;
15821519 int unsign, mask;
15831520
@@ -1634,7 +1571,7 @@ static inline void codebook_bits(MLPEncodeContext *ctx, · 50 unmodified lines
16341571 int offset, int32_t min, int32_t max,
16351572 BestOffset *bo, int direction)
16361573 {
1637− int previous_count = INT_MAX;
1574+ uint32_t previous_count = UINT32_MAX;
16381575 int offset_min, offset_max;
16391576 int is_greater = 0;
16401577
@@ -1704,8 +1641,8 @@ static void determine_bits(MLPEncodeContext *ctx) · 63 unmodified lines
17041641 }
17051642
17061643 for (int i = 1; i < NUM_CODEBOOKS; i++) {
1707− BestOffset temp_bo = { 0, INT_MAX, 0, 0, 0, };
1708− int16_t offset_max;
1644+ BestOffset temp_bo = { 0, UINT32_MAX, 0, 0, 0, };
1645+ int32_t offset_max;
17091646
17101647 codebook_bits_offset(ctx, channel, i - 1,
17111648 min, max, offset,
@@ -1741,13 +1678,14 @@ static void determine_bits(MLPEncodeContext *ctx) · 29 unmodified lines
17411678 */
17421679 static int apply_filter(MLPEncodeContext *ctx, unsigned int channel)
17431680 {
1744− FilterParams *fp[NUM_FILTERS] = { &ctx->cur_channel_params[channel].filter_params[FIR],
1745− &ctx->cur_channel_params[channel].filter_params[IIR], };
1681+ ChannelParams *cp = &ctx->cur_channel_params[channel];
1682+ FilterParams *fp[NUM_FILTERS] = { &cp->filter_params[FIR],
1683+ &cp->filter_params[IIR], };
1684+ const uint8_t codebook = cp->codebook;
17461685 int32_t mask = MSB_MASK(ctx->cur_decoding_params->quant_step_size[channel]);
17471686 int32_t *sample_buffer = ctx->sample_buffer + channel;
17481687 unsigned int number_of_samples = ctx->number_of_samples;
17491688 unsigned int filter_shift = fp[FIR]->shift;
1750− int ret = 0;
17511689
17521690 for (int i = 0; i < 8; i++) {
17531691 ctx->filter_state_buffer[FIR][i] = *sample_buffer;
@@ -1759,10 +1697,10 @@ static int apply_filter(MLPEncodeContext *ctx, unsigned int channel) · 5 unmodified lines
17591697 for (int i = 8; i < number_of_samples; i++) {
17601698 int32_t sample = *sample_buffer;
17611699 int64_t accum = 0;
1762− int64_t residual;
1700+ int32_t residual;
17631701
17641702 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];
17661704 for (unsigned int order = 0; order < fp[filter]->order; order++)
17671705 accum += (int64_t)ctx->filter_state_buffer[filter][i - 1 - order] *
17681706 fcoeff[order];
@@ -1771,13 +1709,13 @@ static int apply_filter(MLPEncodeContext *ctx, unsigned int channel) · 2 unmodified lines
17711709 accum >>= filter_shift;
17721710 residual = sample - (accum & mask);
17731711
1774− if (residual < SAMPLE_MIN(24) || residual > SAMPLE_MAX(24)) {
1775− ret = AVERROR_INVALIDDATA;
1776− return ret;
1777− }
1712+ if ((codebook > 0) &&
1713+ (residual < SAMPLE_MIN(24) ||
1714+ residual > SAMPLE_MAX(24)))
1715+ return -1;
17781716
17791717 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;
17811719
17821720 sample_buffer += ctx->num_channels;
17831721 }
@@ -1789,7 +1727,7 @@ static int apply_filter(MLPEncodeContext *ctx, unsigned int channel) · 5 unmodified lines
17891727 sample_buffer += ctx->num_channels;
17901728 }
17911729
1792− return ret;
1730+ return 0;
17931731 }
17941732
17951733 static void apply_filters(MLPEncodeContext *ctx)
@@ -1859,10 +1797,10 @@ static void rematrix_channels(MLPEncodeContext *ctx) · 63 unmodified lines
18591797 **** Functions that deal with determining the best parameters and output ***
18601798 ****************************************************************************/
18611799
1862−typedef struct {
1800+typedef struct PathCounter {
18631801 char path[MAJOR_HEADER_INTERVAL + 2];
18641802 int cur_idx;
1865− int bitcount;
1803+ uint32_t bitcount;
18661804 } PathCounter;
18671805
18681806 #define CODEBOOK_CHANGE_BITS 21
@@ -1877,14 +1815,14 @@ static int compare_best_offset(const BestOffset *prev, const BestOffset *cur) · 8 unmodified lines
18771815 return prev->lsb_bits != cur->lsb_bits;
18781816 }
18791817
1880−static int best_codebook_path_cost(MLPEncodeContext *ctx, unsigned int channel,
1818+static uint32_t best_codebook_path_cost(MLPEncodeContext *ctx, unsigned int channel,
18811819 PathCounter *src, int cur_codebook)
18821820 {
18831821 int idx = src->cur_idx;
1884− const BestOffset *cur_bo = ctx->best_offset[idx][channel],
1885− *prev_bo = idx ? ctx->best_offset[idx - 1][channel] :
1822+ const BestOffset *cur_bo = ctx->b[idx].best_offset[channel],
1823+ *prev_bo = idx ? ctx->b[idx - 1].best_offset[channel] :
18861824 restart_best_offset;
1887− int bitcount = src->bitcount;
1825+ uint32_t bitcount = src->bitcount;
18881826 int prev_codebook = src->path[idx];
18891827
18901828 bitcount += cur_bo[cur_codebook].bitcount;
@@ -1898,7 +1836,6 @@ static int best_codebook_path_cost(MLPEncodeContext *ctx, unsigned int channel, · 7 unmodified lines
18981836
18991837 static void set_best_codebook(MLPEncodeContext *ctx)
19001838 {
1901− DecodingParams *dp = ctx->cur_decoding_params;
19021839 RestartHeader *rh = ctx->cur_restart_header;
19031840
19041841 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
19111848 clear_path_counter(path_counter);
19121849
19131850 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;
19151852
1916− cur_bo = ctx->best_offset[index][channel];
1853+ cur_bo = ctx->b[index].best_offset[channel];
19171854
19181855 for (unsigned int codebook = 0; codebook < NUM_CODEBOOKS; codebook++) {
1919− int prev_best_bitcount = INT_MAX;
1856+ uint32_t prev_best_bitcount = UINT32_MAX;
19201857
19211858 for (unsigned int last_best = 0; last_best < 2; last_best++) {
19221859 PathCounter *dst_path = &path_counter[codebook];
19231860 PathCounter *src_path;
1924− int temp_bitcount;
1861+ uint32_t temp_bitcount;
19251862
19261863 /* First test last path with same headers,
19271864 * then with last best. */
@@ -1961,10 +1898,11 @@ static void set_best_codebook(MLPEncodeContext *ctx) · 33 unmodified lines
19611898
19621899 /* Update context. */
19631900 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;
19651903
19661904 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];
19681906
19691907 cp->huff_offset = cur_bo->offset;
19701908 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
19801918 static void set_major_params(MLPEncodeContext *ctx)
19811919 {
19821920 RestartHeader *rh = ctx->cur_restart_header;
1983− uint8_t max_huff_lsbs = 0;
1984− uint8_t max_output_bits = 0;
19851921 int channels = ctx->avctx->ch_layout.nb_channels;
1986− DecodingParams *seq_dp = ctx->decoding_params + ctx->seq_offset[0] * channels;
1987− ChannelParams *seq_cp = ctx->channel_params + ctx->seq_offset[0] * channels;
1922+ uint8_t max_huff_lsbs = 0, max_output_bits = 0;
19881923
1989− for (unsigned int index = 0; index < ctx->seq_size[ctx->restart_intervals-1]; index++) {
1990− memcpy(&ctx->major_decoding_params[index], seq_dp + index, sizeof(DecodingParams));
1924+ for (unsigned int index = 0; index < ctx->b[ctx->restart_intervals-1].seq_size; index++) {
1925+ memcpy(&ctx->b[index].major_decoding_params,
1926+ &ctx->b[index].decoding_params, sizeof(DecodingParams));
19911927 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;
19931929 if (max_huff_lsbs < huff_lsbs)
19941930 max_huff_lsbs = huff_lsbs;
1995− memcpy(&ctx->major_channel_params[index][channel],
1996− (seq_cp + index*(channels) + channel),
1931+ memcpy(&ctx->b[index].major_channel_params[channel],
1932+ &ctx->b[index].channel_params[channel],
19971933 sizeof(ChannelParams));
19981934 }
19991935 }
@@ -2001,8 +1937,8 @@ static void set_major_params(MLPEncodeContext *ctx) · 1 unmodified line
20011937 rh->max_huff_lsbs = max_huff_lsbs;
20021938
20031939 for (unsigned int index = 0; index < ctx->number_of_frames; index++)
2004− if (max_output_bits < ctx->max_output_bits[index])
2005− max_output_bits = ctx->max_output_bits[index];
1940+ if (max_output_bits < ctx->b[index].max_output_bits)
1941+ max_output_bits = ctx->b[index].max_output_bits;
20061942 rh->max_output_bits = max_output_bits;
20071943
20081944 ctx->cur_restart_header = &ctx->restart_header;
@@ -2011,10 +1947,10 @@ static void set_major_params(MLPEncodeContext *ctx) · 2 unmodified lines
20111947 ctx->prev_channel_params = restart_channel_params;
20121948
20131949 for (unsigned int index = 0; index < MAJOR_HEADER_INTERVAL + 1; index++) {
2014− ctx->cur_decoding_params = &ctx->major_decoding_params[index];
2015− ctx->cur_channel_params = ctx->major_channel_params[index];
1950+ ctx->cur_decoding_params = &ctx->b[index].major_decoding_params;
1951+ ctx->cur_channel_params = ctx->b[index].major_channel_params;
20161952
2017− ctx->major_params_changed[index] = compare_decoding_params(ctx);
1953+ ctx->b[index].major_params_changed = compare_decoding_params(ctx);
20181954
20191955 ctx->prev_decoding_params = ctx->cur_decoding_params;
20201956 ctx->prev_channel_params = ctx->cur_channel_params;
@@ -2027,12 +1963,9 @@ static void set_major_params(MLPEncodeContext *ctx) · 6 unmodified lines
20271963
20281964 static void analyze_sample_buffer(MLPEncodeContext *ctx)
20291965 {
2030− ChannelParams *seq_cp = ctx->seq_channel_params;
2031− DecodingParams *seq_dp = ctx->seq_decoding_params;
2032−
20331966 ctx->cur_restart_header = &ctx->restart_header;
2034− ctx->cur_decoding_params = seq_dp + 1;
2035− ctx->cur_channel_params = seq_cp + ctx->avctx->ch_layout.nb_channels;
1967+ ctx->cur_decoding_params = &ctx->b[1].decoding_params;
1968+ ctx->cur_channel_params = ctx->b[1].channel_params;
20361969
20371970 determine_quant_step_size(ctx);
20381971 generate_2_noise_channels(ctx);
@@ -2047,20 +1980,20 @@ static void analyze_sample_buffer(MLPEncodeContext *ctx) · 8 unmodified lines
20471980 * decoding_params[0] is for the filter state subblock.
20481981 */
20491982 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;
20511984 dp->blocksize = ctx->avctx->frame_size;
20521985 }
20531986 /* The official encoder seems to always encode a filter state subblock
20541987 * even if there are no filters. TODO check if it is possible to skip
20551988 * the filter state subblock for no filters.
20561989 */
2057− (seq_dp + 0)->blocksize = 8;
2058− (seq_dp + 1)->blocksize -= 8;
1990+ ctx->b[0].decoding_params.blocksize = 8;
1991+ ctx->b[1].decoding_params.blocksize -= 8;
20591992
20601993 for (unsigned int index = 0; index < ctx->number_of_subblocks; index++) {
2061− ctx->cur_decoding_params = seq_dp + index;
2062− ctx->cur_channel_params = seq_cp + index*(ctx->avctx->ch_layout.nb_channels);
2063− ctx->cur_best_offset = ctx->best_offset[index];
1994+ ctx->cur_decoding_params = &ctx->b[index].decoding_params;
1995+ ctx->cur_channel_params = ctx->b[index].channel_params;
1996+ ctx->cur_best_offset = ctx->b[index].best_offset;
20641997 determine_bits(ctx);
20651998 ctx->sample_buffer += ctx->cur_decoding_params->blocksize * ctx->num_channels;
20661999 }
@@ -2077,8 +2010,8 @@ static void process_major_frame(MLPEncodeContext *ctx) · 10 unmodified lines
20772010
20782011 ctx->cur_restart_header = &ctx->restart_header;
20792012
2080− ctx->cur_decoding_params = &ctx->major_decoding_params[1];
2081− ctx->cur_channel_params = ctx->major_channel_params[1];
2013+ ctx->cur_decoding_params = &ctx->b[1].major_decoding_params;
2014+ ctx->cur_channel_params = ctx->b[1].major_channel_params;
20822015
20832016 generate_2_noise_channels(ctx);
20842017 rematrix_channels (ctx);
@@ -2168,16 +2101,12 @@ input_and_return: · 83 unmodified lines
21682101 ctx->number_of_frames = ctx->next_major_number_of_frames;
21692102 ctx->number_of_subblocks = ctx->next_major_number_of_frames + 1;
21702103
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−
21752104 number_of_samples = avctx->frame_size * ctx->number_of_frames;
21762105 ctx->number_of_samples = number_of_samples;
21772106
2178− for (unsigned int index = 0; index < ctx->seq_size[seq_index]; index++) {
2179− clear_channel_params(ctx->seq_channel_params + index * channels, channels);
2180− default_decoding_params(ctx, ctx->seq_decoding_params + index);
2107+ for (unsigned int index = 0; index < ctx->b[seq_index].seq_size; index++) {
2108+ clear_channel_params(ctx->b[index].channel_params, channels);
2109+ default_decoding_params(ctx, &ctx->b[index].decoding_params);
21812110 }
21822111
21832112 input_to_sample_buffer(ctx);
@@ -2219,18 +2148,10 @@ static av_cold int mlp_encode_close(AVCodecContext *avctx) · 35 unmodified lines
22192148
22202149 ff_lpc_end(&ctx->lpc_ctx);
22212150
2222− av_freep(&ctx->lossless_check_data);
22232151 av_freep(&ctx->major_scratch_buffer);
22242152 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);
22292153 ff_af_queue_close(&ctx->afq);
22302154
2231− for (int i = 0; i < NUM_FILTERS; i++)
2232− av_freep(&ctx->filter_state_buffer[i]);
2233−
22342155 return 0;
22352156 }
22362157

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