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
171 int32_t *decoded_buffer;171 int32_t *decoded_buffer;
172 int decoded_size;172 int decoded_size;
173 int32_t *decoded[MAX_CHANNELS]; ///< decoded data for each channel173 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
174 int blocks_per_loop; ///< maximum number of samples to decode for each call177 int blocks_per_loop; ///< maximum number of samples to decode for each call
175 178
176 int16_t* filterbuf[APE_FILTER_LEVELS]; ///< filter memory179 int16_t* filterbuf[APE_FILTER_LEVELS]; ///< filter memory
@@ -187,6 +190,7 @@ typedef struct APEContext { · 10 unmodified lines
187 const uint8_t *ptr; ///< current position in frame data190 const uint8_t *ptr; ///< current position in frame data
188 191
189 int error;192 int error;
193 int interim_mode;
190 194
191 void (*entropy_decode_mono)(struct APEContext *ctx, int blockstodecode);195 void (*entropy_decode_mono)(struct APEContext *ctx, int blockstodecode);
192 void (*entropy_decode_stereo)(struct APEContext *ctx, int blockstodecode);196 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
223 av_freep(&s->filterbuf[i]);227 av_freep(&s->filterbuf[i]);
224 228
225 av_freep(&s->decoded_buffer);229 av_freep(&s->decoded_buffer);
230 av_freep(&s->interim_buffer);
226 av_freep(&s->data);231 av_freep(&s->data);
227 s->decoded_size = s->data_size = 0;232 s->decoded_size = s->data_size = 0;
228 233
@@ -248,12 +253,15 @@ static av_cold int ape_decode_init(AVCodecContext *avctx) · 19 unmodified lines
248 switch (s->bps) {253 switch (s->bps) {
249 case 8:254 case 8:
250 avctx->sample_fmt = AV_SAMPLE_FMT_U8P;255 avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
256 s->interim_mode = 0;
251 break;257 break;
252 case 16:258 case 16:
253 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;259 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
260 s->interim_mode = 0;
254 break;261 break;
255 case 24:262 case 24:
256 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;263 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
264 s->interim_mode = -1;
257 break;265 break;
258 default:266 default:
259 avpriv_request_sample(avctx,267 avpriv_request_sample(avctx,
@@ -1181,7 +1189,7 @@ static av_always_inline int predictor_update_filter(APEPredictor64 *p, · 921 unmodified lines
1181 const int decoded, const int filter,1189 const int decoded, const int filter,
1182 const int delayA, const int delayB,1190 const int delayA, const int delayB,
1183 const int adaptA, const int adaptB,1191 const int adaptA, const int adaptB,
1184 int compression_level)1192 int interim_mode)
1185{1193{
1186 int64_t predictionA, predictionB;1194 int64_t predictionA, predictionB;
1187 int32_t sign;1195 int32_t sign;
@@ -1209,7 +1217,7 @@ static av_always_inline int predictor_update_filter(APEPredictor64 *p, · 21 unmodified lines
1209 p->buf[delayB - 3] * p->coeffsB[filter][3] +1217 p->buf[delayB - 3] * p->coeffsB[filter][3] +
1210 p->buf[delayB - 4] * p->coeffsB[filter][4];1218 p->buf[delayB - 4] * p->coeffsB[filter][4];
1211 1219
1212 if (compression_level < COMPRESSION_LEVEL_INSANE) {1220 if (interim_mode < 1) {
1213 predictionA = (int32_t)predictionA;1221 predictionA = (int32_t)predictionA;
1214 predictionB = (int32_t)predictionB;1222 predictionB = (int32_t)predictionB;
1215 p->lastA[filter] = decoded + ((int32_t)(predictionA + (predictionB >> 1)) >> 10);1223 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
1234 1242
1235static void predictor_decode_stereo_3950(APEContext *ctx, int count)1243static void predictor_decode_stereo_3950(APEContext *ctx, int count)
1236{1244{
1237 APEPredictor64 *p = &ctx->predictor64;1245 APEPredictor64 *p_default = &ctx->predictor64;
1238 int32_t *decoded0 = ctx->decoded[0];1246 APEPredictor64 p_interim;
1239 int32_t *decoded1 = ctx->decoded[1];1247 int lcount = count;
1248 int num_passes = 1;
1240 1249
1241 ape_apply_filters(ctx, ctx->decoded[0], ctx->decoded[1], count);1250 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 }
1242 1257
1243 while (count--) {1258 for (int pass = 0; pass < num_passes; pass++) {
1244 /* Predictor Y */1259 int32_t *decoded0, *decoded1;
1245 *decoded0 = predictor_update_filter(p, *decoded0, 0, YDELAYA, YDELAYB,1260 int interim_mode = ctx->interim_mode > 0 || pass;
1246 YADAPTCOEFFSA, YADAPTCOEFFSB,1261 APEPredictor64 *p;
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++;
1253 1262
1254 /* Combined */1263 if (pass) {
1255 p->buf++;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 }
1256 1295
1257 /* Have we filled the history buffer? */1296 /* Combined */
1258 if (p->buf == p->historybuffer + HISTORY_SIZE) {1297 p->buf++;
1259 memmove(p->historybuffer, p->buf,1298
1260 PREDICTOR_SIZE * sizeof(*p->historybuffer));1299 /* Have we filled the history buffer? */
1261 p->buf = p->historybuffer;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 }
1262 }1305 }
1263 }1306 }
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 }
1264}1313}
1265 1314
1266static void predictor_decode_mono_3950(APEContext *ctx, int count)1315static 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
1590 s->decoded[0] = s->decoded_buffer;1639 s->decoded[0] = s->decoded_buffer;
1591 s->decoded[1] = s->decoded_buffer + FFALIGN(blockstodecode, 8);1640 s->decoded[1] = s->decoded_buffer + FFALIGN(blockstodecode, 8);
1592 1641
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
1593 s->error=0;1655 s->error=0;
1594 1656
1595 if ((s->channels == 1) || (s->frameflags & APE_FRAMECODE_PSEUDO_STEREO))1657 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