History

avcodec/mlpenc: restructure code even more

Implement lsb_bypass for lossless rematrix.

Paul B Mahol committed 3 years ago

Commit 9adc5d81 parent 23619701 file · +317 −316Browse files

Unified · Split

libavcodec/mlpenc.c+317−316
@@ -38,6 +38,8 @@ · 37 unmodified lines
3838 #include "mlp.h"
3939 #include "lpc.h"
4040
41+#define MAX_NCHANNELS (MAX_CHANNELS + 2)
42+
4143 #define MIN_HEADER_INTERVAL 8
4244 #define MAX_HEADER_INTERVAL 128
4345
@@ -66,11 +68,13 @@ typedef struct MatrixParams { · 22 unmodified lines
6668 uint8_t count; ///< number of matrices to apply
6769
6870 uint8_t outch[MAX_MATRICES]; ///< output channel for each matrix
69− int32_t forco[MAX_MATRICES][MAX_CHANNELS+2]; ///< forward coefficients
70− int32_t coeff[MAX_MATRICES][MAX_CHANNELS+2]; ///< decoding coefficients
71+ int32_t forco[MAX_MATRICES][MAX_NCHANNELS]; ///< forward coefficients
72+ int32_t coeff[MAX_MATRICES][MAX_NCHANNELS]; ///< decoding coefficients
7173 uint8_t fbits[MAX_CHANNELS]; ///< fraction bits
7274
7375 int8_t shift[MAX_CHANNELS]; ///< Left shift to apply to decoded PCM values to get final 24-bit output.
76+ uint8_t lsb_bypass[MAX_MATRICES];
77+ int8_t bypassed_lsbs[MAX_MATRICES][MAX_BLOCKSIZE];
7478 } MatrixParams;
7579
7680 enum ParamFlags {
@@ -93,6 +97,7 @@ typedef struct DecodingParams { · 16 unmodified lines
9397 MatrixParams matrix_params;
9498
9599 uint8_t param_presence_flags; ///< Bitmask of which parameter sets are conveyed in a decoding parameter block.
100+ int32_t sample_buffer[MAX_NCHANNELS][MAX_BLOCKSIZE];
96101 } DecodingParams;
97102
98103 typedef struct BestOffset {
@@ -111,14 +116,15 @@ typedef struct BestOffset { · 12 unmodified lines
111116
112117 typedef struct MLPBlock {
113118 unsigned int seq_size;
119+ ChannelParams channel_params[MAX_CHANNELS];
114120 DecodingParams decoding_params;
115121 int32_t lossless_check_data; ///< Array with lossless_check_data for each access unit.
116122 unsigned int max_output_bits; ///< largest output bit-depth
117123 BestOffset best_offset[MAX_CHANNELS][NUM_CODEBOOKS];
118− ChannelParams channel_params[MAX_CHANNELS];
119124 ChannelParams major_channel_params[MAX_CHANNELS]; ///< ChannelParams to be written to bitstream.
120125 DecodingParams major_decoding_params; ///< DecodingParams to be written to bitstream.
121126 int major_params_changed; ///< params_changed to be written to bitstream.
127+ int32_t inout_buffer[MAX_NCHANNELS][MAX_BLOCKSIZE];
122128 } MLPBlock;
123129
124130 typedef struct MLPEncodeContext {
@@ -127,6 +133,7 @@ typedef struct MLPEncodeContext { · 2 unmodified lines
127133
128134 int max_restart_interval; ///< Max interval of access units in between two major frames.
129135 int min_restart_interval; ///< Min interval of access units in between two major frames.
136+ int cur_restart_interval;
130137 int lpc_coeff_precision;
131138 int lpc_type;
132139 int lpc_passes;
@@ -152,11 +159,6 @@ typedef struct MLPEncodeContext { · 19 unmodified lines
152159 int channel_occupancy;
153160 int summary_info;
154161
155− int32_t *inout_buffer; ///< Pointer to data currently being read from lavc or written to bitstream.
156− int32_t *major_inout_buffer; ///< Buffer with all in/out data for one entire major frame interval.
157− int32_t *write_buffer; ///< Pointer to data currently being written to bitstream.
158− int32_t *sample_buffer; ///< Pointer to current access unit samples.
159− int32_t *major_scratch_buffer; ///< Scratch buffer big enough to fit all data for one entire major frame interval.
160162 int32_t last_frames; ///< Signal last frames.
161163
162164 unsigned int major_number_of_frames;
@@ -167,8 +169,6 @@ typedef struct MLPEncodeContext { · 4 unmodified lines
167169
168170 unsigned int frame_index; ///< Index of current frame being encoded.
169171
170− unsigned int one_sample_buffer_size; ///< Number of samples*channel for one access unit.
171−
172172 unsigned int restart_intervals; ///< Number of possible major frame sizes.
173173
174174 uint16_t output_timing; ///< Timestamp of current access unit.
@@ -185,28 +185,21 @@ typedef struct MLPEncodeContext { · 10 unmodified lines
185185 RestartHeader restart_header;
186186
187187 MLPBlock b[MAX_HEADER_INTERVAL + 1];
188+ int32_t filter_state[NUM_FILTERS][MAX_HEADER_INTERVAL * MAX_BLOCKSIZE];
188189 int32_t lpc_sample_buffer[MAX_HEADER_INTERVAL * MAX_BLOCKSIZE];
189− int32_t filter_state_buffer[NUM_FILTERS][MAX_BLOCKSIZE * MAX_HEADER_INTERVAL];
190190
191191 unsigned int major_cur_subblock_index;
192192 unsigned int major_filter_state_subblock;
193193 unsigned int major_number_of_subblocks;
194194
195− BestOffset (*cur_best_offset)[NUM_CODEBOOKS];
196− ChannelParams *cur_channel_params;
197− DecodingParams *cur_decoding_params;
198195 RestartHeader *cur_restart_header;
199196
200197 AudioFrameQueue afq;
201198
202199 /* Analysis stage. */
203200 unsigned int number_of_frames;
204− unsigned int number_of_samples;
205201 unsigned int number_of_subblocks;
206202
207− const ChannelParams *prev_channel_params;
208− const DecodingParams *prev_decoding_params;
209−
210203 int shorten_by;
211204
212205 LPCContext lpc_ctx;
@@ -278,6 +271,9 @@ static int compare_matrix_params(MLPEncodeContext *ctx, const MatrixParams *prev · 65 unmodified lines
278271 if (prev->outch[mat] != mp->outch[mat])
279272 return 1;
280273
274+ if (prev->lsb_bypass[mat] != mp->lsb_bypass[mat])
275+ return 1;
276+
281277 for (unsigned int channel = 0; channel < ctx->num_channels; channel++)
282278 if (prev->coeff[mat][channel] != mp->coeff[mat][channel])
283279 return 1;
@@ -289,13 +285,14 @@ static int compare_matrix_params(MLPEncodeContext *ctx, const MatrixParams *prev · 5 unmodified lines
289285 /** Compares two DecodingParams and ChannelParams structures to decide if a
290286 * new decoding params header has to be written.
291287 */
292−static int compare_decoding_params(MLPEncodeContext *ctx)
288+static int compare_decoding_params(MLPEncodeContext *ctx,
289+ unsigned int index)
293290 {
294− const DecodingParams *prev = ctx->prev_decoding_params;
295− DecodingParams *dp = ctx->cur_decoding_params;
291+ const DecodingParams *prev = index ? &ctx->b[index-1].major_decoding_params : restart_decoding_params;
292+ DecodingParams *dp = &ctx->b[index].major_decoding_params;
296293 const MatrixParams *prev_mp = &prev->matrix_params;
297294 MatrixParams *mp = &dp->matrix_params;
298− RestartHeader *rh = ctx->cur_restart_header;
295+ RestartHeader *rh = ctx->cur_restart_header;
299296 int retval = 0;
300297
301298 if (prev->param_presence_flags != dp->param_presence_flags)
@@ -320,8 +317,8 @@ static int compare_decoding_params(MLPEncodeContext *ctx) · 18 unmodified lines
320317 }
321318
322319 for (unsigned int ch = rh->min_channel; ch <= rh->max_channel; ch++) {
323− const ChannelParams *prev_cp = &ctx->prev_channel_params[ch];
324− ChannelParams *cp = &ctx->cur_channel_params[ch];
320+ const ChannelParams *prev_cp = index ? &ctx->b[index-1].major_channel_params[ch] : &restart_channel_params[ch];
321+ ChannelParams *cp = &ctx->b[index].major_channel_params[ch];
325322
326323 if (!(retval & PARAM_FIR) &&
327324 compare_filter_params(prev_cp, cp, FIR))
@@ -374,8 +371,10 @@ static void copy_matrix_params(MatrixParams *dst, MatrixParams *src) · 46 unmodified lines
374371 dst->coeff[count][channel] = src->coeff[count][channel];
375372 }
376373
377− for (unsigned int count = 0; count < MAX_MATRICES; count++)
374+ for (unsigned int count = 0; count < MAX_MATRICES; count++) {
378375 dst->outch[count] = src->outch[count];
376+ dst->lsb_bypass[count] = src->lsb_bypass[count];
377+ }
379378 }
380379 }
381380
@@ -384,17 +383,17 @@ static void copy_restart_frame_params(MLPEncodeContext *ctx) · 2 unmodified lines
384383 for (unsigned int index = 0; index < ctx->number_of_subblocks; index++) {
385384 DecodingParams *dp = &ctx->b[index].decoding_params;
386385
387− copy_matrix_params(&dp->matrix_params, &ctx->cur_decoding_params->matrix_params);
386+ copy_matrix_params(&dp->matrix_params, &ctx->b[1].decoding_params.matrix_params);
388387
389388 for (unsigned int channel = 0; channel < ctx->avctx->ch_layout.nb_channels; channel++) {
390389 ChannelParams *cp = &ctx->b[index].channel_params[channel];
391390
392− dp->quant_step_size[channel] = ctx->cur_decoding_params->quant_step_size[channel];
393− dp->matrix_params.shift[channel] = ctx->cur_decoding_params->matrix_params.shift[channel];
391+ dp->quant_step_size[channel] = ctx->b[1].decoding_params.quant_step_size[channel];
392+ dp->matrix_params.shift[channel] = ctx->b[1].decoding_params.matrix_params.shift[channel];
394393
395394 if (index)
396395 for (unsigned int filter = 0; filter < NUM_FILTERS; filter++)
397− copy_filter_params(cp, &ctx->cur_channel_params[channel], filter);
396+ copy_filter_params(cp, &ctx->b[1].channel_params[channel], filter);
398397 }
399398 }
400399 }
@@ -405,10 +404,11 @@ static void clear_decoding_params(DecodingParams *decoding_params) · 4 unmodified lines
405404 DecodingParams *dp = decoding_params;
406405
407406 dp->param_presence_flags = 0xff;
408− dp->blocksize = 8;
407+ dp->blocksize = 0;
409408
410− memset(&dp->matrix_params , 0, sizeof(MatrixParams ));
409+ memset(&dp->matrix_params, 0, sizeof(dp->matrix_params ));
411410 memset(dp->quant_step_size, 0, sizeof(dp->quant_step_size));
411+ memset(dp->sample_buffer, 0, sizeof(dp->sample_buffer ));
412412 }
413413
414414 /** Clears a ChannelParams struct the way it should be after a restart header. */
@@ -427,12 +427,11 @@ static void clear_channel_params(ChannelParams *channel_params, int nb_channels) · 12 unmodified lines
427427 }
428428
429429 /** Sets default vales in our encoder for a DecodingParams struct. */
430−static void default_decoding_params(MLPEncodeContext *ctx, DecodingParams *decoding_params)
430+static void default_decoding_params(MLPEncodeContext *ctx, DecodingParams *dp)
431431 {
432− DecodingParams *dp = decoding_params;
433432 uint8_t param_presence_flags = 0;
434433
435− clear_decoding_params(decoding_params);
434+ clear_decoding_params(dp);
436435
437436 param_presence_flags |= PARAM_BLOCKSIZE;
438437 param_presence_flags |= PARAM_MATRIX;
@@ -480,7 +479,6 @@ static av_cold int mlp_encode_init(AVCodecContext *avctx) · 41 unmodified lines
480479 MLPEncodeContext *ctx = avctx->priv_data;
481480 RestartHeader *const rh = &ctx->restart_header;
482481 uint64_t channels_present;
483− size_t size;
484482 int ret;
485483
486484 ctx->avctx = avctx;
@@ -555,21 +553,10 @@ static av_cold int mlp_encode_init(AVCodecContext *avctx) · 68 unmodified lines
555553 ctx->input_timing = -avctx->frame_size;
556554
557555 ctx->num_channels = avctx->ch_layout.nb_channels + 2; /* +2 noise channels */
558− ctx->one_sample_buffer_size = avctx->frame_size
559− * ctx->num_channels;
560556
561− ctx->min_restart_interval = ctx->max_restart_interval;
557+ ctx->min_restart_interval = ctx->cur_restart_interval = ctx->max_restart_interval;
562558 ctx->restart_intervals = ctx->max_restart_interval / ctx->min_restart_interval;
563559
564− size = ctx->one_sample_buffer_size * ctx->max_restart_interval;
565− ctx->major_scratch_buffer = av_calloc(size, sizeof(*ctx->major_scratch_buffer));
566− if (!ctx->major_scratch_buffer)
567− return AVERROR(ENOMEM);
568−
569− ctx->major_inout_buffer = av_calloc(size, sizeof(*ctx->major_inout_buffer));
570− if (!ctx->major_inout_buffer)
571− return AVERROR(ENOMEM);
572−
573560 ctx->num_substreams = 1; // TODO: change this after adding multi-channel support for TrueHD
574561
575562 channels_present = av_channel_layout_subset(&avctx->ch_layout, ~(uint64_t)0);
@@ -643,9 +630,7 @@ static av_cold int mlp_encode_init(AVCodecContext *avctx) · 67 unmodified lines
643630 /* FIXME: this works for 1 and 2 channels, but check for more */
644631 rh->max_matrix_channel = rh->max_channel;
645632
646− ctx->number_of_samples = avctx->frame_size * ctx->max_restart_interval;
647−
648− if ((ret = ff_lpc_init(&ctx->lpc_ctx, ctx->number_of_samples,
633+ if ((ret = ff_lpc_init(&ctx->lpc_ctx, ctx->avctx->frame_size,
649634 MLP_MAX_LPC_ORDER, ctx->lpc_type)) < 0)
650635 return ret;
651636
@@ -779,9 +764,10 @@ static void write_restart_header(MLPEncodeContext *ctx, PutBitContext *pb) · 127 unmodified lines
779764 }
780765
781766 /** Writes matrix params for all primitive matrices to the bitstream. */
782−static void write_matrix_params(MLPEncodeContext *ctx, PutBitContext *pb)
767+static void write_matrix_params(MLPEncodeContext *ctx,
768+ DecodingParams *dp,
769+ PutBitContext *pb)
783770 {
784− DecodingParams *dp = ctx->cur_decoding_params;
785771 MatrixParams *mp = &dp->matrix_params;
786772
787773 put_bits(pb, 4, mp->count);
@@ -789,7 +775,7 @@ static void write_matrix_params(MLPEncodeContext *ctx, PutBitContext *pb) · 1 unmodified line
789775 for (unsigned int mat = 0; mat < mp->count; mat++) {
790776 put_bits(pb, 4, mp->outch[mat]); /* matrix_out_ch */
791777 put_bits(pb, 4, mp->fbits[mat]);
792− put_bits(pb, 1, 0 ); /* lsb_bypass */
778+ put_bits(pb, 1, mp->lsb_bypass[mat]);
793779
794780 for (unsigned int channel = 0; channel < ctx->num_channels; channel++) {
795781 int32_t coeff = mp->coeff[mat][channel];
@@ -808,15 +794,17 @@ static void write_matrix_params(MLPEncodeContext *ctx, PutBitContext *pb) · 12 unmodified lines
808794 }
809795
810796 /** Writes filter parameters for one filter to the bitstream. */
811−static void write_filter_params(MLPEncodeContext *ctx, PutBitContext *pb,
797+static void write_filter_params(MLPEncodeContext *ctx,
798+ ChannelParams *cp,
799+ PutBitContext *pb,
812800 unsigned int channel, unsigned int filter)
813801 {
814− FilterParams *fp = &ctx->cur_channel_params[channel].filter_params[filter];
802+ FilterParams *fp = &cp->filter_params[filter];
815803
816804 put_bits(pb, 4, fp->order);
817805
818806 if (fp->order > 0) {
819− int32_t *fcoeff = ctx->cur_channel_params[channel].coeff[filter];
807+ int32_t *fcoeff = cp->coeff[filter];
820808
821809 put_bits(pb, 4, fp->shift );
822810 put_bits(pb, 5, fp->coeff_bits );
@@ -835,10 +823,11 @@ static void write_filter_params(MLPEncodeContext *ctx, PutBitContext *pb, · 12 unmodified lines
835823 * usually at almost every frame.
836824 */
837825 static void write_decoding_params(MLPEncodeContext *ctx, PutBitContext *pb,
838− int params_changed)
826+ int params_changed,
827+ unsigned int subblock_index)
839828 {
840− DecodingParams *dp = ctx->cur_decoding_params;
841− RestartHeader *rh = ctx->cur_restart_header;
829+ DecodingParams *dp = &ctx->b[subblock_index].major_decoding_params;
830+ RestartHeader *rh = ctx->cur_restart_header;
842831 MatrixParams *mp = &dp->matrix_params;
843832
844833 if (dp->param_presence_flags != PARAMS_DEFAULT &&
@@ -861,7 +850,7 @@ static void write_decoding_params(MLPEncodeContext *ctx, PutBitContext *pb, · 16 unmodified lines
861850 if (dp->param_presence_flags & PARAM_MATRIX) {
862851 if (params_changed & PARAM_MATRIX) {
863852 put_bits(pb, 1, 1);
864− write_matrix_params(ctx, pb);
853+ write_matrix_params(ctx, dp, pb);
865854 } else {
866855 put_bits(pb, 1, 0);
867856 }
@@ -888,7 +877,7 @@ static void write_decoding_params(MLPEncodeContext *ctx, PutBitContext *pb, · 20 unmodified lines
888877 }
889878
890879 for (unsigned int ch = rh->min_channel; ch <= rh->max_channel; ch++) {
891− ChannelParams *cp = &ctx->cur_channel_params[ch];
880+ ChannelParams *cp = &ctx->b[subblock_index].major_channel_params[ch];
892881
893882 if (dp->param_presence_flags & 0xF) {
894883 put_bits(pb, 1, 1);
@@ -896,7 +885,7 @@ static void write_decoding_params(MLPEncodeContext *ctx, PutBitContext *pb, · 1 unmodified line
896885 if (dp->param_presence_flags & PARAM_FIR) {
897886 if (params_changed & PARAM_FIR) {
898887 put_bits(pb, 1, 1);
899− write_filter_params(ctx, pb, ch, FIR);
888+ write_filter_params(ctx, cp, pb, ch, FIR);
900889 } else {
901890 put_bits(pb, 1, 0);
902891 }
@@ -905,7 +894,7 @@ static void write_decoding_params(MLPEncodeContext *ctx, PutBitContext *pb, · 2 unmodified lines
905894 if (dp->param_presence_flags & PARAM_IIR) {
906895 if (params_changed & PARAM_IIR) {
907896 put_bits(pb, 1, 1);
908− write_filter_params(ctx, pb, ch, IIR);
897+ write_filter_params(ctx, cp, pb, ch, IIR);
909898 } else {
910899 put_bits(pb, 1, 0);
911900 }
@@ -934,17 +923,18 @@ static void write_decoding_params(MLPEncodeContext *ctx, PutBitContext *pb, · 22 unmodified lines
934923 /** Writes the residuals to the bitstream. That is, the VLC codes from the
935924 * codebooks (if any is used), and then the residual.
936925 */
937−static void write_block_data(MLPEncodeContext *ctx, PutBitContext *pb)
926+static void write_block_data(MLPEncodeContext *ctx, PutBitContext *pb,
927+ unsigned int subblock_index)
938928 {
939− DecodingParams *dp = ctx->cur_decoding_params;
940− RestartHeader *rh = ctx->cur_restart_header;
941− int32_t *sample_buffer = ctx->write_buffer;
929+ RestartHeader *rh = ctx->cur_restart_header;
930+ DecodingParams *dp = &ctx->b[subblock_index].major_decoding_params;
931+ MatrixParams *mp = &dp->matrix_params;
942932 int32_t sign_huff_offset[MAX_CHANNELS];
943933 int codebook_index [MAX_CHANNELS];
944934 int lsb_bits [MAX_CHANNELS];
945935
946936 for (unsigned int ch = rh->min_channel; ch <= rh->max_channel; ch++) {
947− ChannelParams *cp = &ctx->cur_channel_params[ch];
937+ ChannelParams *cp = &ctx->b[subblock_index].major_channel_params[ch];
948938 int sign_shift;
949939
950940 lsb_bits [ch] = cp->huff_lsbs - dp->quant_step_size[ch];
@@ -962,8 +952,17 @@ static void write_block_data(MLPEncodeContext *ctx, PutBitContext *pb) · 11 unmodified lines
962952 }
963953
964954 for (unsigned int i = 0; i < dp->blocksize; i++) {
955+ for (unsigned int mat = 0; mat < mp->count; mat++) {
956+ if (mp->lsb_bypass[mat]) {
957+ const int8_t *bypassed_lsbs = mp->bypassed_lsbs[mat];
958+
959+ put_bits(pb, 1, bypassed_lsbs[i]);
960+ }
961+ }
962+
965963 for (unsigned int ch = rh->min_channel; ch <= rh->max_channel; ch++) {
966− int32_t sample = *sample_buffer++ >> dp->quant_step_size[ch];
964+ int32_t *sample_buffer = dp->sample_buffer[ch];
965+ int32_t sample = sample_buffer[i] >> dp->quant_step_size[ch];
967966 sample -= sign_huff_offset[ch];
968967
969968 if (codebook_index[ch] >= 0) {
@@ -975,10 +974,7 @@ static void write_block_data(MLPEncodeContext *ctx, PutBitContext *pb) · 5 unmodified lines
975974
976975 put_bits(pb, lsb_bits[ch], sample);
977976 }
978− sample_buffer += 2; /* noise channels */
979977 }
980−
981− ctx->write_buffer = sample_buffer;
982978 }
983979
984980 /** Writes the substream data to the bitstream. */
@@ -989,7 +985,7 @@ static uint8_t *write_substr(MLPEncodeContext *ctx, uint8_t *buf, int buf_size, · 4 unmodified lines
989985 int32_t *lossless_check_data = &ctx->b[ctx->frame_index].lossless_check_data;
990986 unsigned int cur_subblock_index = ctx->major_cur_subblock_index;
991987 unsigned int num_subblocks = ctx->major_filter_state_subblock;
992− RestartHeader *rh = &ctx->restart_header;
988+ RestartHeader *rh = &ctx->restart_header;
993989 int substr_restart_frame = restart_frame;
994990 uint8_t parity, checksum;
995991 PutBitContext pb;
@@ -1001,12 +997,7 @@ static uint8_t *write_substr(MLPEncodeContext *ctx, uint8_t *buf, int buf_size, · 5 unmodified lines
1001997 init_put_bits(&pb, buf, buf_size);
1002998
1003999 for (unsigned int subblock = 0; subblock <= num_subblocks; subblock++) {
1004− unsigned int subblock_index;
1005−
1006− subblock_index = cur_subblock_index++;
1007−
1008− ctx->cur_decoding_params = &ctx->b[subblock_index].major_decoding_params;
1009− ctx->cur_channel_params = ctx->b[subblock_index].major_channel_params;
1000+ unsigned int subblock_index = cur_subblock_index++;
10101001
10111002 params_changed = ctx->b[subblock_index].major_params_changed;
10121003
@@ -1022,12 +1013,13 @@ static uint8_t *write_substr(MLPEncodeContext *ctx, uint8_t *buf, int buf_size, · 9 unmodified lines
10221013 put_bits(&pb, 1, 0);
10231014 }
10241015
1025− write_decoding_params(ctx, &pb, params_changed);
1016+ write_decoding_params(ctx, &pb, params_changed,
1017+ subblock_index);
10261018 } else {
10271019 put_bits(&pb, 1, 0);
10281020 }
10291021
1030− write_block_data(ctx, &pb);
1022+ write_block_data(ctx, &pb, subblock_index);
10311023
10321024 put_bits(&pb, 1, !substr_restart_frame);
10331025
@@ -1159,23 +1151,21 @@ static void input_data_internal(MLPEncodeContext *ctx, const uint8_t *samples, · 125 unmodified lines
11591151 const int32_t *samples_32 = (const int32_t *) samples;
11601152 const int16_t *samples_16 = (const int16_t *) samples;
11611153 RestartHeader *rh = &ctx->restart_header;
1162− int32_t *sample_buffer = ctx->inout_buffer;
11631154 int32_t temp_lossless_check_data = 0;
11641155 uint32_t bits = 0;
11651156
11661157 for (int i = 0; i < nb_samples; i++) {
1167− for (unsigned int channel = 0; channel <= rh->max_channel; channel++) {
1158+ for (unsigned int ch = 0; ch <= rh->max_channel; ch++) {
1159+ int32_t *sample_buffer = ctx->b[ctx->frame_index].inout_buffer[ch];
11681160 int32_t sample;
11691161
11701162 sample = is24 ? *samples_32++ >> 8 : *samples_16++ * 256;
11711163
11721164 bits = FFMAX(number_sbits(sample), bits);
11731165
1174− temp_lossless_check_data ^= (sample & 0x00ffffff) << channel;
1175− *sample_buffer++ = sample;
1166+ temp_lossless_check_data ^= (sample & 0x00ffffff) << ch;
1167+ sample_buffer[i] = sample;
11761168 }
1177−
1178− sample_buffer += 2; /* noise channels */
11791169 }
11801170
11811171 ctx->b[ctx->frame_index].max_output_bits = bits;
@@ -1191,17 +1181,25 @@ static void input_data(MLPEncodeContext *ctx, const void *samples, int nb_sample · 9 unmodified lines
11911181
11921182 static void input_to_sample_buffer(MLPEncodeContext *ctx)
11931183 {
1194− int32_t *sample_buffer = ctx->sample_buffer;
1195−
11961184 for (unsigned int index = 0; index < ctx->number_of_frames; index++) {
1197− unsigned int cur_index = (ctx->frame_index + index + 1) % ctx->max_restart_interval;
1198− int32_t *input_buffer = ctx->inout_buffer + cur_index * ctx->one_sample_buffer_size;
1199−
1200− for (unsigned int i = 0; i < ctx->avctx->frame_size; i++) {
1201− for (unsigned int channel = 0; channel < ctx->avctx->ch_layout.nb_channels; channel++)
1202− *sample_buffer++ = *input_buffer++;
1203− sample_buffer += 2; /* noise_channels */
1204− input_buffer += 2; /* noise_channels */
1185+ unsigned int cur_index = (ctx->frame_index + index + 1) % ctx->cur_restart_interval;
1186+ DecodingParams *dp = &ctx->b[index+1].decoding_params;
1187+
1188+ for (unsigned int ch = 0; ch < ctx->avctx->ch_layout.nb_channels; ch++) {
1189+ const int32_t *input_buffer = ctx->b[cur_index].inout_buffer[ch];
1190+ int32_t *sample_buffer = dp->sample_buffer[ch];
1191+ int off = 0;
1192+
1193+ if (dp->blocksize < ctx->avctx->frame_size) {
1194+ DecodingParams *dp = &ctx->b[index].decoding_params;
1195+ int32_t *sample_buffer = dp->sample_buffer[ch];
1196+ for (unsigned int i = 0; i < dp->blocksize; i++)
1197+ sample_buffer[i] = input_buffer[i];
1198+ off = dp->blocksize;
1199+ }
1200+
1201+ for (unsigned int i = 0; i < dp->blocksize; i++)
1202+ sample_buffer[i] = input_buffer[i + off];
12051203 }
12061204 }
12071205 }
@@ -1221,23 +1219,25 @@ static int number_trailing_zeroes(int32_t sample, unsigned int max, unsigned int · 13 unmodified lines
12211219 */
12221220 static void determine_quant_step_size(MLPEncodeContext *ctx)
12231221 {
1224− DecodingParams *dp = ctx->cur_decoding_params;
1225− RestartHeader *rh = ctx->cur_restart_header;
1226− MatrixParams *mp = &dp->matrix_params;
1227− int32_t *sample_buffer = ctx->sample_buffer;
1222+ RestartHeader *rh = ctx->cur_restart_header;
1223+ DecodingParams *dp1 = &ctx->b[1].decoding_params;
12281224 int32_t sample_mask[MAX_CHANNELS];
12291225
1230− memset(sample_mask, 0x00, sizeof(sample_mask));
1226+ memset(sample_mask, 0, sizeof(sample_mask));
12311227
1232− for (unsigned int i = 0; i < ctx->number_of_samples; i++) {
1233− for (unsigned int channel = 0; channel <= rh->max_channel; channel++)
1234− sample_mask[channel] |= *sample_buffer++;
1228+ for (unsigned int j = 0; j <= ctx->cur_restart_interval; j++) {
1229+ DecodingParams *dp = &ctx->b[j].decoding_params;
12351230
1236− sample_buffer += 2; /* noise channels */
1231+ for (unsigned int ch = 0; ch <= rh->max_channel; ch++) {
1232+ int32_t *sample_buffer = dp->sample_buffer[ch];
1233+
1234+ for (unsigned int i = 0; i < dp->blocksize; i++)
1235+ sample_mask[ch] |= sample_buffer[i];
1236+ }
12371237 }
12381238
12391239 for (unsigned int channel = 0; channel <= rh->max_channel; channel++)
1240− dp->quant_step_size[channel] = number_trailing_zeroes(sample_mask[channel], 15, 0) - mp->shift[channel];
1240+ dp1->quant_step_size[channel] = number_trailing_zeroes(sample_mask[channel], 15, 0);
12411241 }
12421242
12431243 /** Determines the smallest number of bits needed to encode the filter
@@ -1271,7 +1271,7 @@ static void set_filter_params(MLPEncodeContext *ctx, · 27 unmodified lines
12711271 unsigned int channel, unsigned int filter,
12721272 int clear_filter)
12731273 {
1274− ChannelParams *cp = &ctx->cur_channel_params[channel];
1274+ ChannelParams *cp = &ctx->b[1].channel_params[channel];
12751275 FilterParams *fp = &cp->filter_params[filter];
12761276
12771277 if (clear_filter) {
@@ -1280,21 +1280,26 @@ static void set_filter_params(MLPEncodeContext *ctx, · 2 unmodified lines
12801280 fp->order = 0;
12811281 } else if (filter == FIR) {
12821282 const int max_order = MAX_FIR_ORDER;
1283− int32_t *sample_buffer = ctx->sample_buffer + channel;
12841283 int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];
12851284 int32_t *lpc_samples = ctx->lpc_sample_buffer;
1286− int32_t *fcoeff = ctx->cur_channel_params[channel].coeff[filter];
1285+ int32_t *fcoeff = cp->coeff[filter];
12871286 int shift[MAX_LPC_ORDER];
12881287 int order;
12891288
1290− for (unsigned int i = 0; i < ctx->number_of_samples; i++) {
1291− *lpc_samples++ = *sample_buffer;
1292− sample_buffer += ctx->num_channels;
1289+ for (unsigned int j = 0; j <= ctx->cur_restart_interval; j++) {
1290+ DecodingParams *dp = &ctx->b[j].decoding_params;
1291+ int32_t *sample_buffer = dp->sample_buffer[channel];
1292+
1293+ for (unsigned int i = 0; i < dp->blocksize; i++)
1294+ lpc_samples[i] = sample_buffer[i];
1295+ lpc_samples += dp->blocksize;
12931296 }
12941297
12951298 order = ff_lpc_calc_coefs(&ctx->lpc_ctx, ctx->lpc_sample_buffer,
1296− ctx->number_of_samples, MLP_MIN_LPC_ORDER,
1297− max_order, ctx->lpc_coeff_precision, coefs, shift, ctx->lpc_type, ctx->lpc_passes,
1299+ lpc_samples - ctx->lpc_sample_buffer,
1300+ MLP_MIN_LPC_ORDER, max_order,
1301+ ctx->lpc_coeff_precision,
1302+ coefs, shift, ctx->lpc_type, ctx->lpc_passes,
12981303 ctx->prediction_order, MLP_MIN_LPC_SHIFT,
12991304 MLP_MAX_LPC_SHIFT, MLP_MIN_LPC_SHIFT);
13001305
@@ -1326,53 +1331,53 @@ static int estimate_coeff(MLPEncodeContext *ctx, · 25 unmodified lines
13261331 MatrixParams *mp,
13271332 unsigned int ch0, unsigned int ch1)
13281333 {
1329− const int number_of_samples = ctx->number_of_samples;
1330− const int num_channels = ctx->num_channels;
13311334 int first = 1, x, x0;
1332− const int32_t *ch[2];
13331335 int64_t g = 0, g0 = 0;
13341336
1335− ch[0] = ctx->sample_buffer + ch0;
1336− ch[1] = ctx->sample_buffer + ch1;
1337+ for (int j = 0; j <= ctx->cur_restart_interval; j++) {
1338+ DecodingParams *dp = &ctx->b[j].decoding_params;
1339+ const int32_t *ch[2];
13371340
1338− for (int n = 0, i = 0; n < number_of_samples; n++) {
1339− int64_t c0, c1;
1340− int32_t a0, a1;
1341+ ch[0] = dp->sample_buffer[ch0];
1342+ ch[1] = dp->sample_buffer[ch1];
13411343
1342− c0 = ch[0][i];
1343− c1 = ch[1][i];
1344+ for (int i = 0; i < dp->blocksize; i++) {
1345+ int64_t c0, c1;
1346+ int32_t a0, a1;
13441347
1345− if (!c0 && !c1)
1346− goto next;
1348+ c0 = ch[0][i];
1349+ c1 = ch[1][i];
13471350
1348− if (!c0 || !c1) {
1349− g0 = 0;
1350− break;
1351− }
1351+ if (!c0 && !c1)
1352+ continue;
13521353
1353− a0 = FFABS(c0);
1354− a1 = FFABS(c1);
1354+ if (!c0 || !c1) {
1355+ g0 = 0;
1356+ goto end;
1357+ }
13551358
1356− if (a0 >= a1) {
1357− g = (c1 * (1 << 14)) / c0;
1358− x = ch1;
1359− } else if (a0 < a1) {
1360− g = (c0 * (1 << 14)) / c1;
1361− x = ch0;
1362− }
1359+ a0 = FFABS(c0);
1360+ a1 = FFABS(c1);
13631361
1364− if (first) {
1365− g0 = g;
1366− x0 = x;
1367− first = 0;
1368− } else if (g != g0 || x != x0) {
1369− g0 = 0;
1370− break;
1371− }
1362+ if (a0 >= a1) {
1363+ g = (c1 * (1 << 14)) / c0;
1364+ x = ch1;
1365+ } else if (a0 < a1) {
1366+ g = (c0 * (1 << 14)) / c1;
1367+ x = ch0;
1368+ }
13721369
1373−next:
1374− i += num_channels;
1370+ if (first) {
1371+ g0 = g;
1372+ x0 = x;
1373+ first = 0;
1374+ } else if (g != g0 || x != x0) {
1375+ g0 = 0;
1376+ goto end;
1377+ }
1378+ }
13751379 }
1380+end:
13761381
13771382 if (g0) {
13781383 mp->outch[0] = (x0 == ch0) ? ch0 : ch1;
@@ -1383,9 +1388,6 @@ next: · 4 unmodified lines
13831388 mp->forco[0][ch0] = 0;
13841389 mp->forco[0][ch1] = 0;
13851390
1386− mp->coeff[0][num_channels - 2] = mp->coeff[0][num_channels - 1] = 0;
1387− mp->forco[0][num_channels - 2] = mp->forco[0][num_channels - 1] = 0;
1388−
13891391 return 1;
13901392 }
13911393
@@ -1395,9 +1397,10 @@ next: · 3 unmodified lines
13951397 /** Determines how many fractional bits are needed to encode matrix
13961398 * coefficients. Also shifts the coefficients to fit within 2.14 bits.
13971399 */
1398−static void code_matrix_coeffs(MLPEncodeContext *ctx, unsigned int mat)
1400+static void code_matrix_coeffs(MLPEncodeContext *ctx,
1401+ DecodingParams *dp,
1402+ unsigned int mat)
13991403 {
1400− DecodingParams *dp = ctx->cur_decoding_params;
14011404 MatrixParams *mp = &dp->matrix_params;
14021405 int32_t coeff_mask = 0;
14031406
@@ -1411,9 +1414,8 @@ static void code_matrix_coeffs(MLPEncodeContext *ctx, unsigned int mat) · 7 unmodified lines
14111414 /** Determines best coefficients to use for the lossless matrix. */
14121415 static void lossless_matrix_coeffs(MLPEncodeContext *ctx)
14131416 {
1414− DecodingParams *dp = ctx->cur_decoding_params;
1417+ DecodingParams *dp = &ctx->b[1].decoding_params;
14151418 MatrixParams *mp = &dp->matrix_params;
1416− unsigned int shift = 0;
14171419
14181420 mp->count = 0;
14191421 if (ctx->num_channels - 2 != 2)
@@ -1422,10 +1424,7 @@ static void lossless_matrix_coeffs(MLPEncodeContext *ctx) · 2 unmodified lines
14221424 mp->count = estimate_coeff(ctx, mp, 0, 1);
14231425
14241426 for (int mat = 0; mat < mp->count; mat++)
1425− code_matrix_coeffs(ctx, mat);
1426−
1427− for (unsigned int channel = 0; channel < ctx->num_channels; channel++)
1428− mp->shift[channel] = shift;
1427+ code_matrix_coeffs(ctx, dp, mat);
14291428 }
14301429
14311430 /** Min and max values that can be encoded with each codebook. The values for
@@ -1441,11 +1440,11 @@ static const int8_t codebook_extremes[3][2] = { · 9 unmodified lines
14411440 * codebooks and a specified offset.
14421441 */
14431442 static void no_codebook_bits_offset(MLPEncodeContext *ctx,
1443+ DecodingParams *dp,
14441444 unsigned int channel, int32_t offset,
14451445 int32_t min, int32_t max,
14461446 BestOffset *bo)
14471447 {
1448− DecodingParams *dp = ctx->cur_decoding_params;
14491448 int32_t unsign = 0;
14501449 int lsb_bits;
14511450
@@ -1470,11 +1469,11 @@ static void no_codebook_bits_offset(MLPEncodeContext *ctx, · 18 unmodified lines
14701469 * codebooks.
14711470 */
14721471 static void no_codebook_bits(MLPEncodeContext *ctx,
1472+ DecodingParams *dp,
14731473 unsigned int channel,
14741474 int32_t min, int32_t max,
14751475 BestOffset *bo)
14761476 {
1477− DecodingParams *dp = ctx->cur_decoding_params;
14781477 int32_t offset, unsign = 0;
14791478 uint8_t lsb_bits;
14801479
@@ -1507,14 +1506,14 @@ static void no_codebook_bits(MLPEncodeContext *ctx, · 26 unmodified lines
15071506 * given codebook and a given offset.
15081507 */
15091508 static inline void codebook_bits_offset(MLPEncodeContext *ctx,
1509+ DecodingParams *dp,
15101510 unsigned int channel, int codebook,
15111511 int32_t sample_min, int32_t sample_max,
15121512 int32_t offset, BestOffset *bo)
15131513 {
15141514 int32_t codebook_min = codebook_extremes[codebook][0];
15151515 int32_t codebook_max = codebook_extremes[codebook][1];
1516− int32_t *sample_buffer = ctx->sample_buffer + channel;
1517− DecodingParams *dp = ctx->cur_decoding_params;
1516+ int32_t *sample_buffer = dp->sample_buffer[channel];
15181517 int codebook_offset = 7 + (2 - codebook);
15191518 int32_t unsign_offset = offset;
15201519 uint32_t bitcount = 0;
@@ -1540,7 +1539,7 @@ static inline void codebook_bits_offset(MLPEncodeContext *ctx, · 19 unmodified lines
15401539 }
15411540
15421541 for (int i = 0; i < dp->blocksize; i++) {
1543− int32_t sample = *sample_buffer >> dp->quant_step_size[channel];
1542+ int32_t sample = sample_buffer[i] >> dp->quant_step_size[channel];
15441543 int temp_min, temp_max;
15451544
15461545 sample -= unsign_offset;
@@ -1556,8 +1555,6 @@ static inline void codebook_bits_offset(MLPEncodeContext *ctx, · 9 unmodified lines
15561555 sample >>= lsb_bits;
15571556
15581557 bitcount += ff_mlp_huffman_tables[codebook][sample + codebook_offset][1];
1559−
1560− sample_buffer += ctx->num_channels;
15611558 }
15621559
15631560 bo->offset = offset;
@@ -1571,6 +1568,7 @@ static inline void codebook_bits_offset(MLPEncodeContext *ctx, · 7 unmodified lines
15711568 * given codebook. Searches for the best offset to minimize the bits.
15721569 */
15731570 static inline void codebook_bits(MLPEncodeContext *ctx,
1571+ DecodingParams *dp,
15741572 unsigned int channel, int codebook,
15751573 int offset, int32_t min, int32_t max,
15761574 BestOffset *bo, int direction)
@@ -1585,7 +1583,7 @@ static inline void codebook_bits(MLPEncodeContext *ctx, · 8 unmodified lines
15851583 while (offset <= offset_max && offset >= offset_min) {
15861584 BestOffset temp_bo;
15871585
1588− codebook_bits_offset(ctx, channel, codebook,
1586+ codebook_bits_offset(ctx, dp, channel, codebook,
15891587 min, max, offset,
15901588 &temp_bo);
15911589
@@ -1612,56 +1610,57 @@ static inline void codebook_bits(MLPEncodeContext *ctx, · 20 unmodified lines
16121610 */
16131611 static void determine_bits(MLPEncodeContext *ctx)
16141612 {
1615− DecodingParams *dp = ctx->cur_decoding_params;
1616− RestartHeader *rh = ctx->cur_restart_header;
1617−
1618− for (unsigned int channel = 0; channel <= rh->max_channel; channel++) {
1619− ChannelParams *cp = &ctx->cur_channel_params[channel];
1620− int32_t *sample_buffer = ctx->sample_buffer + channel;
1621− int32_t min = INT32_MAX, max = INT32_MIN;
1622− int no_filters_used = !cp->filter_params[FIR].order;
1623− int average = 0;
1624− int offset = 0;
1625−
1626− /* Determine extremes and average. */
1627− for (int i = 0; i < dp->blocksize; i++) {
1628− int32_t sample = *sample_buffer >> dp->quant_step_size[channel];
1629− if (sample < min)
1630− min = sample;
1631− if (sample > max)
1632− max = sample;
1633− average += sample;
1634− sample_buffer += ctx->num_channels;
1635− }
1636− average /= dp->blocksize;
1613+ RestartHeader *rh = ctx->cur_restart_header;
1614+ for (unsigned int index = 0; index < ctx->number_of_subblocks; index++) {
1615+ DecodingParams *dp = &ctx->b[index].decoding_params;
16371616
1638− /* If filtering is used, we always set the offset to zero, otherwise
1639− * we search for the offset that minimizes the bitcount. */
1640− if (no_filters_used) {
1641− no_codebook_bits(ctx, channel, min, max, &ctx->cur_best_offset[channel][0]);
1642− offset = av_clip(average, HUFF_OFFSET_MIN, HUFF_OFFSET_MAX);
1643− } else {
1644− no_codebook_bits_offset(ctx, channel, offset, min, max, &ctx->cur_best_offset[channel][0]);
1645− }
1617+ for (unsigned int channel = 0; channel <= rh->max_channel; channel++) {
1618+ ChannelParams *cp = &ctx->b[index].channel_params[channel];
1619+ int32_t *sample_buffer = dp->sample_buffer[channel];
1620+ int32_t min = INT32_MAX, max = INT32_MIN;
1621+ int no_filters_used = !cp->filter_params[FIR].order;
1622+ int average = 0;
1623+ int offset = 0;
1624+
1625+ /* Determine extremes and average. */
1626+ for (int i = 0; i < dp->blocksize; i++) {
1627+ int32_t sample = sample_buffer[i] >> dp->quant_step_size[channel];
1628+ if (sample < min)
1629+ min = sample;
1630+ if (sample > max)
1631+ max = sample;
1632+ average += sample;
1633+ }
1634+ average /= dp->blocksize;
1635+
1636+ /* If filtering is used, we always set the offset to zero, otherwise
1637+ * we search for the offset that minimizes the bitcount. */
1638+ if (no_filters_used) {
1639+ no_codebook_bits(ctx, dp, channel, min, max, &ctx->b[index].best_offset[channel][0]);
1640+ offset = av_clip(average, HUFF_OFFSET_MIN, HUFF_OFFSET_MAX);
1641+ } else {
1642+ no_codebook_bits_offset(ctx, dp, channel, offset, min, max, &ctx->b[index].best_offset[channel][0]);
1643+ }
16461644
1647− for (int i = 1; i < NUM_CODEBOOKS; i++) {
1648− BestOffset temp_bo = { 0, UINT32_MAX, 0, 0, 0, };
1649− int32_t offset_max;
1645+ for (int i = 1; i < NUM_CODEBOOKS; i++) {
1646+ BestOffset temp_bo = { 0, UINT32_MAX, 0, 0, 0, };
1647+ int32_t offset_max;
16501648
1651− codebook_bits_offset(ctx, channel, i - 1,
1652− min, max, offset,
1653− &temp_bo);
1649+ codebook_bits_offset(ctx, dp, channel, i - 1,
1650+ min, max, offset,
1651+ &temp_bo);
16541652
1655− if (no_filters_used) {
1656− offset_max = temp_bo.max;
1653+ if (no_filters_used) {
1654+ offset_max = temp_bo.max;
16571655
1658− codebook_bits(ctx, channel, i - 1, temp_bo.min - 1,
1659− min, max, &temp_bo, 0);
1660− codebook_bits(ctx, channel, i - 1, offset_max + 1,
1661− min, max, &temp_bo, 1);
1662− }
1656+ codebook_bits(ctx, dp, channel, i - 1, temp_bo.min - 1,
1657+ min, max, &temp_bo, 0);
1658+ codebook_bits(ctx, dp, channel, i - 1, offset_max + 1,
1659+ min, max, &temp_bo, 1);
1660+ }
16631661
1664− ctx->cur_best_offset[channel][i] = temp_bo;
1662+ ctx->b[index].best_offset[channel][i] = temp_bo;
1663+ }
16651664 }
16661665 }
16671666 }
@@ -1682,34 +1681,40 @@ static void determine_bits(MLPEncodeContext *ctx) · 14 unmodified lines
16821681 */
16831682 static int apply_filter(MLPEncodeContext *ctx, unsigned int channel)
16841683 {
1685− ChannelParams *cp = &ctx->cur_channel_params[channel];
1684+ DecodingParams *dp = &ctx->b[1].decoding_params;
1685+ ChannelParams *cp = &ctx->b[1].channel_params[channel];
16861686 FilterParams *fp[NUM_FILTERS] = { &cp->filter_params[FIR],
16871687 &cp->filter_params[IIR], };
16881688 const uint8_t codebook = cp->codebook;
1689− int32_t mask = MSB_MASK(ctx->cur_decoding_params->quant_step_size[channel]);
1690− int32_t *sample_buffer = ctx->sample_buffer + channel;
1691− unsigned int number_of_samples = ctx->number_of_samples;
1689+ int32_t mask = MSB_MASK(dp->quant_step_size[channel]);
1690+ int32_t *sample_buffer = ctx->b[0].decoding_params.sample_buffer[channel];
16921691 unsigned int filter_shift = fp[FIR]->shift;
1692+ int32_t *filter_state[NUM_FILTERS] = { ctx->filter_state[FIR],
1693+ ctx->filter_state[IIR], };
1694+ int i, j = 1, k = 0;
16931695
1694− for (int i = 0; i < 8; i++) {
1695− ctx->filter_state_buffer[FIR][i] = *sample_buffer;
1696− ctx->filter_state_buffer[IIR][i] = *sample_buffer;
1697−
1698− sample_buffer += ctx->num_channels;
1696+ for (i = 0; i < 8; i++) {
1697+ filter_state[FIR][i] = sample_buffer[i];
1698+ filter_state[IIR][i] = sample_buffer[i];
16991699 }
17001700
1701− for (int i = 8; i < number_of_samples; i++) {
1702− int32_t sample = *sample_buffer;
1701+ while (1) {
1702+ int32_t *sample_buffer = ctx->b[j].decoding_params.sample_buffer[channel];
1703+ unsigned int blocksize = ctx->b[j].decoding_params.blocksize;
1704+ int32_t sample, residual;
17031705 int64_t accum = 0;
1704− int32_t residual;
1706+
1707+ if (!blocksize)
1708+ break;
17051709
17061710 for (int filter = 0; filter < NUM_FILTERS; filter++) {
17071711 int32_t *fcoeff = cp->coeff[filter];
17081712 for (unsigned int order = 0; order < fp[filter]->order; order++)
1709− accum += (int64_t)ctx->filter_state_buffer[filter][i - 1 - order] *
1710− fcoeff[order];
1713+ accum += (int64_t)filter_state[filter][i - 1 - order] *
1714+ fcoeff[order];
17111715 }
17121716
1717+ sample = sample_buffer[k];
17131718 accum >>= filter_shift;
17141719 residual = sample - (accum & mask);
17151720
@@ -1718,17 +1723,25 @@ static int apply_filter(MLPEncodeContext *ctx, unsigned int channel) · 2 unmodified lines
17181723 residual > SAMPLE_MAX(24)))
17191724 return -1;
17201725
1721− ctx->filter_state_buffer[FIR][i] = sample;
1722− ctx->filter_state_buffer[IIR][i] = residual;
1726+ filter_state[FIR][i] = sample;
1727+ filter_state[IIR][i] = residual;
17231728
1724− sample_buffer += ctx->num_channels;
1729+ i++;
1730+ k++;
1731+ if (k >= blocksize) {
1732+ k = 0;
1733+ j++;
1734+ if (j > ctx->cur_restart_interval)
1735+ break;
1736+ }
17251737 }
17261738
1727− sample_buffer = ctx->sample_buffer + channel;
1728− for (int i = 0; i < number_of_samples; i++) {
1729− *sample_buffer = ctx->filter_state_buffer[IIR][i];
1739+ for (int l = 0, j = 0; j <= ctx->cur_restart_interval; j++) {
1740+ int32_t *sample_buffer = ctx->b[j].decoding_params.sample_buffer[channel];
1741+ unsigned int blocksize = ctx->b[j].decoding_params.blocksize;
17301742
1731− sample_buffer += ctx->num_channels;
1743+ for (int i = 0; i < blocksize; i++, l++)
1744+ sample_buffer[i] = filter_state[IIR][l];
17321745 }
17331746
17341747 return 0;
@@ -1752,18 +1765,21 @@ static void apply_filters(MLPEncodeContext *ctx) · 17 unmodified lines
17521765 /** Generates two noise channels worth of data. */
17531766 static void generate_2_noise_channels(MLPEncodeContext *ctx)
17541767 {
1755− int32_t *sample_buffer = ctx->sample_buffer + ctx->num_channels - 2;
17561768 RestartHeader *rh = ctx->cur_restart_header;
17571769 uint32_t seed = rh->noisegen_seed;
17581770
1759− for (unsigned int i = 0; i < ctx->number_of_samples; i++) {
1760− uint16_t seed_shr7 = seed >> 7;
1761− *sample_buffer++ = ((int8_t)(seed >> 15)) * (1 << rh->noise_shift);
1762− *sample_buffer++ = ((int8_t) seed_shr7) * (1 << rh->noise_shift);
1771+ for (unsigned int j = 0; j <= ctx->cur_restart_interval; j++) {
1772+ DecodingParams *dp = &ctx->b[j].decoding_params;
1773+ int32_t *sample_buffer2 = dp->sample_buffer[ctx->num_channels-2];
1774+ int32_t *sample_buffer1 = dp->sample_buffer[ctx->num_channels-1];
17631775
1764− seed = (seed << 16) ^ seed_shr7 ^ (seed_shr7 << 5);
1776+ for (unsigned int i = 0; i < dp->blocksize; i++) {
1777+ uint16_t seed_shr7 = seed >> 7;
1778+ sample_buffer2[i] = ((int8_t)(seed >> 15)) * (1 << rh->noise_shift);
1779+ sample_buffer1[i] = ((int8_t) seed_shr7) * (1 << rh->noise_shift);
17651780
1766− sample_buffer += ctx->num_channels - 2;
1781+ seed = (seed << 16) ^ seed_shr7 ^ (seed_shr7 << 5);
1782+ }
17671783 }
17681784
17691785 rh->noisegen_seed = seed & ((1 << 24)-1);
@@ -1772,27 +1788,55 @@ static void generate_2_noise_channels(MLPEncodeContext *ctx) · 2 unmodified lines
17721788 /** Rematrixes all channels using chosen coefficients. */
17731789 static void rematrix_channels(MLPEncodeContext *ctx)
17741790 {
1775− DecodingParams *dp = ctx->cur_decoding_params;
1776− MatrixParams *mp = &dp->matrix_params;
1777− int32_t *sample_buffer = ctx->sample_buffer;
1791+ DecodingParams *dp1 = &ctx->b[1].decoding_params;
1792+ MatrixParams *mp1 = &dp1->matrix_params;
17781793 unsigned int maxchan = ctx->num_channels;
1794+ int32_t input_samples[MAX_NCHANNELS];
1795+ uint8_t lsb_bypass[MAX_MATRICES] = { 0 };
17791796
1780− for (unsigned int mat = 0; mat < mp->count; mat++) {
1781− unsigned int outch = mp->outch[mat];
1797+ for (unsigned int j = 0; j <= ctx->cur_restart_interval; j++) {
1798+ DecodingParams *dp = &ctx->b[j].decoding_params;
1799+ MatrixParams *mp = &dp->matrix_params;
17821800
1783− sample_buffer = ctx->sample_buffer;
1784− for (unsigned int i = 0; i < ctx->number_of_samples; i++) {
1785− int64_t accum = 0;
1801+ for (unsigned int i = 0; i < dp->blocksize; i++) {
1802+ for (unsigned int ch = 0; ch < maxchan; ch++)
1803+ input_samples[ch] = dp->sample_buffer[ch][i];
17861804
1787− for (unsigned int src_ch = 0; src_ch < maxchan; src_ch++) {
1788− int32_t sample = *(sample_buffer + src_ch);
1789− accum += (int64_t) sample * mp->forco[mat][src_ch];
1805+ for (unsigned int mat = 0; mat < mp1->count; mat++) {
1806+ unsigned int outch = mp1->outch[mat];
1807+ int64_t accum = 0;
1808+
1809+ for (unsigned int ch = 0; ch < maxchan; ch++) {
1810+ int32_t sample = input_samples[ch];
1811+
1812+ accum += (int64_t)sample * mp1->forco[mat][ch];
1813+ }
1814+
1815+ dp->sample_buffer[outch][i] = accum >> 14;
17901816 }
1791− sample_buffer[outch] = accum >> 14;
17921817
1793− sample_buffer += ctx->num_channels;
1818+ for (unsigned int mat = 0; mat < mp1->count; mat++) {
1819+ int8_t *bypassed_lsbs = mp->bypassed_lsbs[mat];
1820+ unsigned int outch = mp1->outch[mat];
1821+ int64_t accum = 0;
1822+ int8_t bit;
1823+
1824+ for (unsigned int ch = 0; ch < maxchan; ch++) {
1825+ int32_t sample = dp->sample_buffer[ch][i];
1826+
1827+ accum += (int64_t)sample * mp1->coeff[mat][ch];
1828+ }
1829+
1830+ bit = (accum >> 14) != input_samples[outch];
1831+
1832+ bypassed_lsbs[i] = bit;
1833+ lsb_bypass[mat] |= bit;
1834+ }
17941835 }
17951836 }
1837+
1838+ for (unsigned int mat = 0; mat < mp1->count; mat++)
1839+ mp1->lsb_bypass[mat] = lsb_bypass[mat];
17961840 }
17971841
17981842 /****************************************************************************
@@ -1945,18 +1989,8 @@ static void set_major_params(MLPEncodeContext *ctx) · 146 unmodified lines
19451989
19461990 ctx->cur_restart_header = &ctx->restart_header;
19471991
1948− ctx->prev_decoding_params = restart_decoding_params;
1949− ctx->prev_channel_params = restart_channel_params;
1950−
1951− for (unsigned int index = 0; index < MAX_HEADER_INTERVAL + 1; index++) {
1952− ctx->cur_decoding_params = &ctx->b[index].major_decoding_params;
1953− ctx->cur_channel_params = ctx->b[index].major_channel_params;
1954−
1955− ctx->b[index].major_params_changed = compare_decoding_params(ctx);
1956−
1957− ctx->prev_decoding_params = ctx->cur_decoding_params;
1958− ctx->prev_channel_params = ctx->cur_channel_params;
1959− }
1992+ for (unsigned int index = 0; index <= ctx->cur_restart_interval; index++)
1993+ ctx->b[index].major_params_changed = compare_decoding_params(ctx, index);
19601994
19611995 ctx->major_number_of_subblocks = ctx->number_of_subblocks;
19621996 ctx->major_filter_state_subblock = 1;
@@ -1966,23 +2000,12 @@ static void set_major_params(MLPEncodeContext *ctx) · 3 unmodified lines
19662000 static void analyze_sample_buffer(MLPEncodeContext *ctx)
19672001 {
19682002 ctx->cur_restart_header = &ctx->restart_header;
1969− ctx->cur_decoding_params = &ctx->b[1].decoding_params;
1970− ctx->cur_channel_params = ctx->b[1].channel_params;
1971−
1972− determine_quant_step_size(ctx);
1973− generate_2_noise_channels(ctx);
1974− lossless_matrix_coeffs (ctx);
1975− rematrix_channels (ctx);
1976− determine_filters (ctx);
1977− apply_filters (ctx);
1978−
1979− copy_restart_frame_params(ctx);
19802003
19812004 /* Copy frame_size from frames 0...max to decoding_params 1...max + 1
19822005 * decoding_params[0] is for the filter state subblock.
19832006 */
19842007 for (unsigned int index = 0; index < ctx->number_of_frames; index++) {
1985− DecodingParams *dp = &ctx->b[index + 1].decoding_params;
2008+ DecodingParams *dp = &ctx->b[index+1].decoding_params;
19862009 dp->blocksize = ctx->avctx->frame_size;
19872010 }
19882011 /* The official encoder seems to always encode a filter state subblock
@@ -1992,29 +2015,27 @@ static void analyze_sample_buffer(MLPEncodeContext *ctx) · 3 unmodified lines
19922015 ctx->b[0].decoding_params.blocksize = 8;
19932016 ctx->b[1].decoding_params.blocksize -= 8;
19942017
1995− for (unsigned int index = 0; index < ctx->number_of_subblocks; index++) {
1996− ctx->cur_decoding_params = &ctx->b[index].decoding_params;
1997− ctx->cur_channel_params = ctx->b[index].channel_params;
1998− ctx->cur_best_offset = ctx->b[index].best_offset;
1999− determine_bits(ctx);
2000− ctx->sample_buffer += ctx->cur_decoding_params->blocksize * ctx->num_channels;
2001− }
2018+ input_to_sample_buffer (ctx);
2019+ generate_2_noise_channels(ctx);
2020+ lossless_matrix_coeffs (ctx);
2021+ rematrix_channels (ctx);
2022+ determine_quant_step_size(ctx);
2023+ determine_filters (ctx);
2024+ apply_filters (ctx);
2025+
2026+ copy_restart_frame_params(ctx);
2027+
2028+ determine_bits(ctx);
20022029
20032030 set_best_codebook(ctx);
20042031 }
20052032
20062033 static void process_major_frame(MLPEncodeContext *ctx)
20072034 {
2008− ctx->sample_buffer = ctx->major_inout_buffer;
2009−
20102035 ctx->number_of_frames = ctx->major_number_of_frames;
2011− ctx->number_of_samples = ctx->major_frame_size;
20122036
20132037 ctx->cur_restart_header = &ctx->restart_header;
20142038
2015− ctx->cur_decoding_params = &ctx->b[1].major_decoding_params;
2016− ctx->cur_channel_params = ctx->b[1].major_channel_params;
2017−
20182039 generate_2_noise_channels(ctx);
20192040 rematrix_channels (ctx);
20202041
@@ -2049,17 +2070,9 @@ static int mlp_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, · 28 unmodified lines
20492070
20502071 data = frame ? frame->data[0] : NULL;
20512072
2052− ctx->frame_index = avctx->frame_num % ctx->max_restart_interval;
2073+ ctx->frame_index = avctx->frame_num % ctx->cur_restart_interval;
20532074
2054− ctx->inout_buffer = ctx->major_inout_buffer
2055− + ctx->frame_index * ctx->one_sample_buffer_size;
2056−
2057− ctx->sample_buffer = ctx->major_scratch_buffer
2058− + ctx->frame_index * ctx->one_sample_buffer_size;
2059−
2060− ctx->write_buffer = ctx->inout_buffer;
2061−
2062− if (avctx->frame_num < ctx->max_restart_interval) {
2075+ if (avctx->frame_num < ctx->cur_restart_interval) {
20632076 if (data)
20642077 goto input_and_return;
20652078 }
@@ -2069,13 +2082,10 @@ static int mlp_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, · 3 unmodified lines
20692082 if (restart_frame) {
20702083 avpkt->flags |= AV_PKT_FLAG_KEY;
20712084 set_major_params(ctx);
2072− if (ctx->min_restart_interval != ctx->max_restart_interval)
2085+ if (ctx->min_restart_interval != ctx->cur_restart_interval)
20732086 process_major_frame(ctx);
20742087 }
20752088
2076− if (ctx->min_restart_interval == ctx->max_restart_interval)
2077− ctx->write_buffer = ctx->sample_buffer;
2078−
20792089 bytes_written = write_access_unit(ctx, avpkt->data, avpkt->size, restart_frame);
20802090
20812091 ctx->output_timing += avctx->frame_size;
@@ -2097,27 +2107,21 @@ input_and_return: · 15 unmodified lines
20972107 for (unsigned int seq_index = 0; seq_index < ctx->restart_intervals; seq_index++) {
20982108 unsigned int number_of_samples;
20992109
2100− ctx->sample_buffer = ctx->major_scratch_buffer;
2101− ctx->inout_buffer = ctx->major_inout_buffer;
2102−
21032110 ctx->number_of_frames = ctx->next_major_number_of_frames;
21042111 ctx->number_of_subblocks = ctx->next_major_number_of_frames + 1;
21052112
21062113 number_of_samples = avctx->frame_size * ctx->number_of_frames;
2107− ctx->number_of_samples = number_of_samples;
21082114
21092115 for (unsigned int index = 0; index < ctx->b[seq_index].seq_size; index++) {
21102116 clear_channel_params(ctx->b[index].channel_params, channels);
21112117 default_decoding_params(ctx, &ctx->b[index].decoding_params);
21122118 }
21132119
2114− input_to_sample_buffer(ctx);
2115−
21162120 if (number_of_samples > 0)
21172121 analyze_sample_buffer(ctx);
21182122 }
21192123
2120− if (ctx->frame_index == (ctx->max_restart_interval - 1)) {
2124+ if (ctx->frame_index == (ctx->cur_restart_interval - 1)) {
21212125 ctx->major_frame_size = ctx->next_major_frame_size;
21222126 ctx->next_major_frame_size = 0;
21232127 ctx->major_number_of_frames = ctx->next_major_number_of_frames;
@@ -2125,7 +2129,7 @@ input_and_return: · 1 unmodified line
21252129 }
21262130 }
21272131
2128− if (!frame && ctx->last_frames < ctx->max_restart_interval - 1)
2132+ if (!frame && ctx->last_frames < ctx->cur_restart_interval - 1)
21292133 avctx->frame_num++;
21302134
21312135 if (bytes_written > 0) {
@@ -2149,9 +2153,6 @@ static av_cold int mlp_encode_close(AVCodecContext *avctx) · 17 unmodified lines
21492153 MLPEncodeContext *ctx = avctx->priv_data;
21502154
21512155 ff_lpc_end(&ctx->lpc_ctx);
2152−
2153− av_freep(&ctx->major_scratch_buffer);
2154− av_freep(&ctx->major_inout_buffer);
21552156 ff_af_queue_close(&ctx->afq);
21562157
21572158 return 0;

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