History

avcodec/apedec: Implement interim mode detection

Michael Niedermayer committed 3 years ago

Commit 86c092a1 parent 7995e171 file · +84 −22Browse files

Unified · Split

libavcodec/apedec.c+84−22
@@ -171,6 +171,9 @@ typedef struct APEContext { · 170 unmodified lines
171171 int32_t *decoded_buffer;
172172 int decoded_size;
173173 int32_t *decoded[MAX_CHANNELS]; ///< decoded data for each channel
174+ int32_t *interim_buffer;
175+ int interim_size;
176+ int32_t *interim[MAX_CHANNELS]; ///< decoded data for each channel
174177 int blocks_per_loop; ///< maximum number of samples to decode for each call
175178
176179 int16_t* filterbuf[APE_FILTER_LEVELS]; ///< filter memory
@@ -187,6 +190,7 @@ typedef struct APEContext { · 10 unmodified lines
187190 const uint8_t *ptr; ///< current position in frame data
188191
189192 int error;
193+ int interim_mode;
190194
191195 void (*entropy_decode_mono)(struct APEContext *ctx, int blockstodecode);
192196 void (*entropy_decode_stereo)(struct APEContext *ctx, int blockstodecode);
@@ -223,6 +227,7 @@ static av_cold int ape_decode_close(AVCodecContext *avctx) · 30 unmodified lines
223227 av_freep(&s->filterbuf[i]);
224228
225229 av_freep(&s->decoded_buffer);
230+ av_freep(&s->interim_buffer);
226231 av_freep(&s->data);
227232 s->decoded_size = s->data_size = 0;
228233
@@ -248,12 +253,15 @@ static av_cold int ape_decode_init(AVCodecContext *avctx) · 19 unmodified lines
248253 switch (s->bps) {
249254 case 8:
250255 avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
256+ s->interim_mode = 0;
251257 break;
252258 case 16:
253259 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
260+ s->interim_mode = 0;
254261 break;
255262 case 24:
256263 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
264+ s->interim_mode = -1;
257265 break;
258266 default:
259267 avpriv_request_sample(avctx,
@@ -1181,7 +1189,7 @@ static av_always_inline int predictor_update_filter(APEPredictor64 *p, · 921 unmodified lines
11811189 const int decoded, const int filter,
11821190 const int delayA, const int delayB,
11831191 const int adaptA, const int adaptB,
1184− int compression_level)
1192+ int interim_mode)
11851193 {
11861194 int64_t predictionA, predictionB;
11871195 int32_t sign;
@@ -1209,7 +1217,7 @@ static av_always_inline int predictor_update_filter(APEPredictor64 *p, · 21 unmodified lines
12091217 p->buf[delayB - 3] * p->coeffsB[filter][3] +
12101218 p->buf[delayB - 4] * p->coeffsB[filter][4];
12111219
1212− if (compression_level < COMPRESSION_LEVEL_INSANE) {
1220+ if (interim_mode < 1) {
12131221 predictionA = (int32_t)predictionA;
12141222 predictionB = (int32_t)predictionB;
12151223 p->lastA[filter] = decoded + ((int32_t)(predictionA + (predictionB >> 1)) >> 10);
@@ -1234,33 +1242,74 @@ static av_always_inline int predictor_update_filter(APEPredictor64 *p, · 18 unmodified lines
12341242
12351243 static void predictor_decode_stereo_3950(APEContext *ctx, int count)
12361244 {
1237− APEPredictor64 *p = &ctx->predictor64;
1238− int32_t *decoded0 = ctx->decoded[0];
1239− int32_t *decoded1 = ctx->decoded[1];
1245+ APEPredictor64 *p_default = &ctx->predictor64;
1246+ APEPredictor64 p_interim;
1247+ int lcount = count;
1248+ int num_passes = 1;
12401249
12411250 ape_apply_filters(ctx, ctx->decoded[0], ctx->decoded[1], count);
1251+ if (ctx->interim_mode == -1) {
1252+ p_interim = *p_default;
1253+ num_passes ++;
1254+ memcpy(ctx->interim[0], ctx->decoded[0], sizeof(*ctx->interim[0])*count);
1255+ memcpy(ctx->interim[1], ctx->decoded[1], sizeof(*ctx->interim[1])*count);
1256+ }
12421257
1243− while (count--) {
1244− /* Predictor Y */
1245− *decoded0 = predictor_update_filter(p, *decoded0, 0, YDELAYA, YDELAYB,
1246− YADAPTCOEFFSA, YADAPTCOEFFSB,
1247− ctx->compression_level);
1248− decoded0++;
1249− *decoded1 = predictor_update_filter(p, *decoded1, 1, XDELAYA, XDELAYB,
1250− XADAPTCOEFFSA, XADAPTCOEFFSB,
1251− ctx->compression_level);
1252− decoded1++;
1258+ for (int pass = 0; pass < num_passes; pass++) {
1259+ int32_t *decoded0, *decoded1;
1260+ int interim_mode = ctx->interim_mode > 0 || pass;
1261+ APEPredictor64 *p;
12531262
1254− /* Combined */
1255− p->buf++;
1263+ if (pass) {
1264+ p = &p_interim;
1265+ decoded0 = ctx->interim[0];
1266+ decoded1 = ctx->interim[1];
1267+ } else {
1268+ p = p_default;
1269+ decoded0 = ctx->decoded[0];
1270+ decoded1 = ctx->decoded[1];
1271+ }
1272+ p->buf = p->historybuffer;
1273+
1274+ count = lcount;
1275+ while (count--) {
1276+ /* Predictor Y */
1277+ int32_t a0 = predictor_update_filter(p, *decoded0, 0, YDELAYA, YDELAYB,
1278+ YADAPTCOEFFSA, YADAPTCOEFFSB,
1279+ interim_mode);
1280+ int32_t a1 = predictor_update_filter(p, *decoded1, 1, XDELAYA, XDELAYB,
1281+ XADAPTCOEFFSA, XADAPTCOEFFSB,
1282+ interim_mode);
1283+ *decoded0++ = a0;
1284+ *decoded1++ = a1;
1285+ if (num_passes > 1) {
1286+ int32_t left = a1 - (unsigned)(a0 / 2);
1287+ int32_t right = left + a0;
1288+
1289+ if (FFMAX(FFABS(left), FFABS(right)) > (1<<23)) {
1290+ ctx->interim_mode = !interim_mode;
1291+ av_log(ctx->avctx, AV_LOG_VERBOSE, "Interim mode: %d\n", ctx->interim_mode);
1292+ break;
1293+ }
1294+ }
12561295
1257− /* Have we filled the history buffer? */
1258− if (p->buf == p->historybuffer + HISTORY_SIZE) {
1259− memmove(p->historybuffer, p->buf,
1260− PREDICTOR_SIZE * sizeof(*p->historybuffer));
1261− p->buf = p->historybuffer;
1296+ /* Combined */
1297+ p->buf++;
1298+
1299+ /* Have we filled the history buffer? */
1300+ if (p->buf == p->historybuffer + HISTORY_SIZE) {
1301+ memmove(p->historybuffer, p->buf,
1302+ PREDICTOR_SIZE * sizeof(*p->historybuffer));
1303+ p->buf = p->historybuffer;
1304+ }
12621305 }
12631306 }
1307+ if (num_passes > 1 && ctx->interim_mode > 0) {
1308+ memcpy(ctx->decoded[0], ctx->interim[0], sizeof(*ctx->interim[0])*lcount);
1309+ memcpy(ctx->decoded[1], ctx->interim[1], sizeof(*ctx->interim[1])*lcount);
1310+ *p_default = p_interim;
1311+ p_default->buf = p_default->historybuffer;
1312+ }
12641313 }
12651314
12661315 static void predictor_decode_mono_3950(APEContext *ctx, int count)
@@ -1590,6 +1639,19 @@ static int ape_decode_frame(AVCodecContext *avctx, AVFrame *frame, · 323 unmodified lines
15901639 s->decoded[0] = s->decoded_buffer;
15911640 s->decoded[1] = s->decoded_buffer + FFALIGN(blockstodecode, 8);
15921641
1642+ if (s->interim_mode < 0) {
1643+ av_fast_malloc(&s->interim_buffer, &s->interim_size, decoded_buffer_size);
1644+ if (!s->interim_buffer)
1645+ return AVERROR(ENOMEM);
1646+ memset(s->interim_buffer, 0, decoded_buffer_size);
1647+ s->interim[0] = s->interim_buffer;
1648+ s->interim[1] = s->interim_buffer + FFALIGN(blockstodecode, 8);
1649+ } else {
1650+ av_freep(&s->interim_buffer);
1651+ s->interim_size = 0;
1652+ memset(s->interim, 0, sizeof(s->interim));
1653+ }
1654+
15931655 s->error=0;
15941656
15951657 if ((s->channels == 1) || (s->frameflags & APE_FRAMECODE_PSEUDO_STEREO))

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