| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Monkey's Audio lossless audio decoder | ||
| 3 | * Copyright (c) 2007 Benjamin Zores <ben@geexbox.org> | ||
| 4 | * based upon libdemac from Dave Chapman. | ||
| 5 | * | ||
| 6 | * This file is part of FFmpeg. | ||
| 7 | * | ||
| 8 | * FFmpeg is free software; you can redistribute it and/or | ||
| 9 | * modify it under the terms of the GNU Lesser General Public | ||
| 10 | * License as published by the Free Software Foundation; either | ||
| 11 | * version 2.1 of the License, or (at your option) any later version. | ||
| 12 | * | ||
| 13 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 16 | * Lesser General Public License for more details. | ||
| 17 | * | ||
| 18 | * You should have received a copy of the GNU Lesser General Public | ||
| 19 | * License along with FFmpeg; if not, write to the Free Software | ||
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 21 | */ | ||
| 22 | |||
| 23 | #include <inttypes.h> | ||
| 24 | |||
| 25 | #include "libavutil/attributes.h" | ||
| 26 | #include "libavutil/avassert.h" | ||
| 27 | #include "libavutil/channel_layout.h" | ||
| 28 | #include "libavutil/crc.h" | ||
| 29 | #include "libavutil/mem.h" | ||
| 30 | #include "libavutil/opt.h" | ||
| 31 | #include "lossless_audiodsp.h" | ||
| 32 | #include "avcodec.h" | ||
| 33 | #include "bswapdsp.h" | ||
| 34 | #include "bytestream.h" | ||
| 35 | #include "codec_internal.h" | ||
| 36 | #include "decode.h" | ||
| 37 | #include "get_bits.h" | ||
| 38 | #include "unary.h" | ||
| 39 | |||
| 40 | /** | ||
| 41 | * @file | ||
| 42 | * Monkey's Audio lossless audio decoder | ||
| 43 | */ | ||
| 44 | |||
| 45 | #define MAX_CHANNELS 2 | ||
| 46 | #define MAX_BYTESPERSAMPLE 3 | ||
| 47 | |||
| 48 | #define APE_FRAMECODE_MONO_SILENCE 1 | ||
| 49 | #define APE_FRAMECODE_STEREO_SILENCE 3 | ||
| 50 | #define APE_FRAMECODE_PSEUDO_STEREO 4 | ||
| 51 | |||
| 52 | #define HISTORY_SIZE 512 | ||
| 53 | #define PREDICTOR_ORDER 8 | ||
| 54 | /** Total size of all predictor histories */ | ||
| 55 | #define PREDICTOR_SIZE 50 | ||
| 56 | |||
| 57 | #define YDELAYA (18 + PREDICTOR_ORDER*4) | ||
| 58 | #define YDELAYB (18 + PREDICTOR_ORDER*3) | ||
| 59 | #define XDELAYA (18 + PREDICTOR_ORDER*2) | ||
| 60 | #define XDELAYB (18 + PREDICTOR_ORDER) | ||
| 61 | |||
| 62 | #define YADAPTCOEFFSA 18 | ||
| 63 | #define XADAPTCOEFFSA 14 | ||
| 64 | #define YADAPTCOEFFSB 10 | ||
| 65 | #define XADAPTCOEFFSB 5 | ||
| 66 | |||
| 67 | /** | ||
| 68 | * Possible compression levels | ||
| 69 | * @{ | ||
| 70 | */ | ||
| 71 | enum APECompressionLevel { | ||
| 72 | COMPRESSION_LEVEL_FAST = 1000, | ||
| 73 | COMPRESSION_LEVEL_NORMAL = 2000, | ||
| 74 | COMPRESSION_LEVEL_HIGH = 3000, | ||
| 75 | COMPRESSION_LEVEL_EXTRA_HIGH = 4000, | ||
| 76 | COMPRESSION_LEVEL_INSANE = 5000 | ||
| 77 | }; | ||
| 78 | /** @} */ | ||
| 79 | |||
| 80 | #define APE_FILTER_LEVELS 3 | ||
| 81 | |||
| 82 | /** Filter orders depending on compression level */ | ||
| 83 | static const uint16_t ape_filter_orders[5][APE_FILTER_LEVELS] = { | ||
| 84 | { 0, 0, 0 }, | ||
| 85 | { 16, 0, 0 }, | ||
| 86 | { 64, 0, 0 }, | ||
| 87 | { 32, 256, 0 }, | ||
| 88 | { 16, 256, 1280 } | ||
| 89 | }; | ||
| 90 | |||
| 91 | /** Filter fraction bits depending on compression level */ | ||
| 92 | static const uint8_t ape_filter_fracbits[5][APE_FILTER_LEVELS] = { | ||
| 93 | { 0, 0, 0 }, | ||
| 94 | { 11, 0, 0 }, | ||
| 95 | { 11, 0, 0 }, | ||
| 96 | { 10, 13, 0 }, | ||
| 97 | { 11, 13, 15 } | ||
| 98 | }; | ||
| 99 | |||
| 100 | |||
| 101 | /** Filters applied to the decoded data */ | ||
| 102 | typedef struct APEFilter { | ||
| 103 | int16_t *coeffs; ///< actual coefficients used in filtering | ||
| 104 | int16_t *adaptcoeffs; ///< adaptive filter coefficients used for correcting of actual filter coefficients | ||
| 105 | int16_t *historybuffer; ///< filter memory | ||
| 106 | int16_t *delay; ///< filtered values | ||
| 107 | |||
| 108 | uint32_t avg; | ||
| 109 | } APEFilter; | ||
| 110 | |||
| 111 | typedef struct APERice { | ||
| 112 | uint32_t k; | ||
| 113 | uint32_t ksum; | ||
| 114 | } APERice; | ||
| 115 | |||
| 116 | typedef struct APERangecoder { | ||
| 117 | uint32_t low; ///< low end of interval | ||
| 118 | uint32_t range; ///< length of interval | ||
| 119 | uint32_t help; ///< bytes_to_follow resp. intermediate value | ||
| 120 | unsigned int buffer; ///< buffer for input/output | ||
| 121 | } APERangecoder; | ||
| 122 | |||
| 123 | /** Filter histories */ | ||
| 124 | typedef struct APEPredictor { | ||
| 125 | int32_t *buf; | ||
| 126 | |||
| 127 | int32_t lastA[2]; | ||
| 128 | |||
| 129 | int32_t filterA[2]; | ||
| 130 | int32_t filterB[2]; | ||
| 131 | |||
| 132 | uint32_t coeffsA[2][4]; ///< adaption coefficients | ||
| 133 | uint32_t coeffsB[2][5]; ///< adaption coefficients | ||
| 134 | int32_t historybuffer[HISTORY_SIZE + PREDICTOR_SIZE]; | ||
| 135 | |||
| 136 | unsigned int sample_pos; | ||
| 137 | } APEPredictor; | ||
| 138 | |||
| 139 | typedef struct APEPredictor64 { | ||
| 140 | int64_t *buf; | ||
| 141 | |||
| 142 | int64_t lastA[2]; | ||
| 143 | |||
| 144 | int64_t filterA[2]; | ||
| 145 | int64_t filterB[2]; | ||
| 146 | |||
| 147 | uint64_t coeffsA[2][4]; ///< adaption coefficients | ||
| 148 | uint64_t coeffsB[2][5]; ///< adaption coefficients | ||
| 149 | int64_t historybuffer[HISTORY_SIZE + PREDICTOR_SIZE]; | ||
| 150 | } APEPredictor64; | ||
| 151 | |||
| 152 | /** Decoder context */ | ||
| 153 | typedef struct APEContext { | ||
| 154 | AVClass *class; ///< class for AVOptions | ||
| 155 | AVCodecContext *avctx; | ||
| 156 | BswapDSPContext bdsp; | ||
| 157 | LLAudDSPContext adsp; | ||
| 158 | int channels; | ||
| 159 | int samples; ///< samples left to decode in current frame | ||
| 160 | int bps; | ||
| 161 | |||
| 162 | int fileversion; ///< codec version, very important in decoding process | ||
| 163 | int compression_level; ///< compression levels | ||
| 164 | int fset; ///< which filter set to use (calculated from compression level) | ||
| 165 | int flags; ///< global decoder flags | ||
| 166 | |||
| 167 | uint32_t CRC; ///< signalled frame CRC | ||
| 168 | uint32_t CRC_state; ///< accumulated CRC | ||
| 169 | int frameflags; ///< frame flags | ||
| 170 | APEPredictor predictor; ///< predictor used for final reconstruction | ||
| 171 | APEPredictor64 predictor64; ///< 64bit predictor used for final reconstruction | ||
| 172 | |||
| 173 | int32_t *decoded_buffer; | ||
| 174 | int decoded_size; | ||
| 175 | int32_t *decoded[MAX_CHANNELS]; ///< decoded data for each channel | ||
| 176 | int32_t *interim_buffer; | ||
| 177 | int interim_size; | ||
| 178 | int32_t *interim[MAX_CHANNELS]; ///< decoded data for each channel | ||
| 179 | int blocks_per_loop; ///< maximum number of samples to decode for each call | ||
| 180 | |||
| 181 | int16_t* filterbuf[APE_FILTER_LEVELS]; ///< filter memory | ||
| 182 | |||
| 183 | APERangecoder rc; ///< rangecoder used to decode actual values | ||
| 184 | APERice riceX; ///< rice code parameters for the second channel | ||
| 185 | APERice riceY; ///< rice code parameters for the first channel | ||
| 186 | APEFilter filters[APE_FILTER_LEVELS][2]; ///< filters used for reconstruction | ||
| 187 | GetBitContext gb; | ||
| 188 | |||
| 189 | uint8_t *data; ///< current frame data | ||
| 190 | uint8_t *data_end; ///< frame data end | ||
| 191 | int data_size; ///< frame data allocated size | ||
| 192 | const uint8_t *ptr; ///< current position in frame data | ||
| 193 | |||
| 194 | int error; | ||
| 195 | int interim_mode; | ||
| 196 | |||
| 197 | void (*entropy_decode_mono)(struct APEContext *ctx, int blockstodecode); | ||
| 198 | void (*entropy_decode_stereo)(struct APEContext *ctx, int blockstodecode); | ||
| 199 | void (*predictor_decode_mono)(struct APEContext *ctx, int count); | ||
| 200 | void (*predictor_decode_stereo)(struct APEContext *ctx, int count); | ||
| 201 | } APEContext; | ||
| 202 | |||
| 203 | static void ape_apply_filters(APEContext *ctx, int32_t *decoded0, | ||
| 204 | int32_t *decoded1, int count); | ||
| 205 | |||
| 206 | static void entropy_decode_mono_0000(APEContext *ctx, int blockstodecode); | ||
| 207 | static void entropy_decode_stereo_0000(APEContext *ctx, int blockstodecode); | ||
| 208 | static void entropy_decode_mono_3860(APEContext *ctx, int blockstodecode); | ||
| 209 | static void entropy_decode_stereo_3860(APEContext *ctx, int blockstodecode); | ||
| 210 | static void entropy_decode_mono_3900(APEContext *ctx, int blockstodecode); | ||
| 211 | static void entropy_decode_stereo_3900(APEContext *ctx, int blockstodecode); | ||
| 212 | static void entropy_decode_stereo_3930(APEContext *ctx, int blockstodecode); | ||
| 213 | static void entropy_decode_mono_3990(APEContext *ctx, int blockstodecode); | ||
| 214 | static void entropy_decode_stereo_3990(APEContext *ctx, int blockstodecode); | ||
| 215 | |||
| 216 | static void predictor_decode_mono_3800(APEContext *ctx, int count); | ||
| 217 | static void predictor_decode_stereo_3800(APEContext *ctx, int count); | ||
| 218 | static void predictor_decode_mono_3930(APEContext *ctx, int count); | ||
| 219 | static void predictor_decode_stereo_3930(APEContext *ctx, int count); | ||
| 220 | static void predictor_decode_mono_3950(APEContext *ctx, int count); | ||
| 221 | static void predictor_decode_stereo_3950(APEContext *ctx, int count); | ||
| 222 | |||
| 223 | 29 | static av_cold int ape_decode_close(AVCodecContext *avctx) | |
| 224 | { | ||
| 225 | 29 | APEContext *s = avctx->priv_data; | |
| 226 | int i; | ||
| 227 | |||
| 228 |
2/2✓ Branch 0 taken 87 times.
✓ Branch 1 taken 29 times.
|
116 | for (i = 0; i < APE_FILTER_LEVELS; i++) |
| 229 | 87 | av_freep(&s->filterbuf[i]); | |
| 230 | |||
| 231 | 29 | av_freep(&s->decoded_buffer); | |
| 232 | 29 | av_freep(&s->interim_buffer); | |
| 233 | 29 | av_freep(&s->data); | |
| 234 | 29 | s->decoded_size = s->data_size = 0; | |
| 235 | |||
| 236 | 29 | return 0; | |
| 237 | } | ||
| 238 | |||
| 239 | 29 | static av_cold int ape_decode_init(AVCodecContext *avctx) | |
| 240 | { | ||
| 241 | 29 | APEContext *s = avctx->priv_data; | |
| 242 | 29 | int channels = avctx->ch_layout.nb_channels; | |
| 243 | int i; | ||
| 244 | |||
| 245 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
|
29 | if (avctx->extradata_size != 6) { |
| 246 | ✗ | av_log(avctx, AV_LOG_ERROR, "Incorrect extradata\n"); | |
| 247 | ✗ | return AVERROR(EINVAL); | |
| 248 | } | ||
| 249 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
|
29 | if (channels > 2) { |
| 250 | ✗ | av_log(avctx, AV_LOG_ERROR, "Only mono and stereo is supported\n"); | |
| 251 | ✗ | return AVERROR(EINVAL); | |
| 252 | } | ||
| 253 | 29 | avctx->bits_per_raw_sample = | |
| 254 | 29 | s->bps = avctx->bits_per_coded_sample; | |
| 255 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
29 | switch (s->bps) { |
| 256 | ✗ | case 8: | |
| 257 | ✗ | avctx->sample_fmt = AV_SAMPLE_FMT_U8P; | |
| 258 | ✗ | s->interim_mode = 0; | |
| 259 | ✗ | break; | |
| 260 | 27 | case 16: | |
| 261 | 27 | avctx->sample_fmt = AV_SAMPLE_FMT_S16P; | |
| 262 | 27 | s->interim_mode = 0; | |
| 263 | 27 | break; | |
| 264 | 2 | case 24: | |
| 265 | 2 | avctx->sample_fmt = AV_SAMPLE_FMT_S32P; | |
| 266 | 2 | s->interim_mode = -1; | |
| 267 | 2 | break; | |
| 268 | ✗ | default: | |
| 269 | ✗ | avpriv_request_sample(avctx, | |
| 270 | "%d bits per coded sample", s->bps); | ||
| 271 | ✗ | return AVERROR_PATCHWELCOME; | |
| 272 | } | ||
| 273 | 29 | s->avctx = avctx; | |
| 274 | 29 | s->channels = channels; | |
| 275 | 29 | s->fileversion = AV_RL16(avctx->extradata); | |
| 276 | 29 | s->compression_level = AV_RL16(avctx->extradata + 2); | |
| 277 | 29 | s->flags = AV_RL16(avctx->extradata + 4); | |
| 278 | |||
| 279 | 29 | av_log(avctx, AV_LOG_VERBOSE, "Compression Level: %d - Flags: %d\n", | |
| 280 | s->compression_level, s->flags); | ||
| 281 |
2/4✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
|
29 | if (s->compression_level % 1000 || s->compression_level > COMPRESSION_LEVEL_INSANE || |
| 282 |
1/2✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 | !s->compression_level || |
| 283 |
3/4✓ Branch 0 taken 20 times.
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
|
29 | (s->fileversion < 3930 && s->compression_level == COMPRESSION_LEVEL_INSANE)) { |
| 284 | ✗ | av_log(avctx, AV_LOG_ERROR, "Incorrect compression level %d\n", | |
| 285 | s->compression_level); | ||
| 286 | ✗ | return AVERROR_INVALIDDATA; | |
| 287 | } | ||
| 288 | 29 | s->fset = s->compression_level / 1000 - 1; | |
| 289 |
1/2✓ Branch 0 taken 70 times.
✗ Branch 1 not taken.
|
70 | for (i = 0; i < APE_FILTER_LEVELS; i++) { |
| 290 |
2/2✓ Branch 0 taken 29 times.
✓ Branch 1 taken 41 times.
|
70 | if (!ape_filter_orders[s->fset][i]) |
| 291 | 29 | break; | |
| 292 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
|
41 | if (!(s->filterbuf[i] = av_malloc((ape_filter_orders[s->fset][i] * 3 + HISTORY_SIZE) * 4))) |
| 293 | ✗ | return AVERROR(ENOMEM); | |
| 294 | } | ||
| 295 | |||
| 296 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 25 times.
|
29 | if (s->fileversion < 3860) { |
| 297 | 4 | s->entropy_decode_mono = entropy_decode_mono_0000; | |
| 298 | 4 | s->entropy_decode_stereo = entropy_decode_stereo_0000; | |
| 299 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 17 times.
|
25 | } else if (s->fileversion < 3900) { |
| 300 | 8 | s->entropy_decode_mono = entropy_decode_mono_3860; | |
| 301 | 8 | s->entropy_decode_stereo = entropy_decode_stereo_3860; | |
| 302 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 9 times.
|
17 | } else if (s->fileversion < 3930) { |
| 303 | 8 | s->entropy_decode_mono = entropy_decode_mono_3900; | |
| 304 | 8 | s->entropy_decode_stereo = entropy_decode_stereo_3900; | |
| 305 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5 times.
|
9 | } else if (s->fileversion < 3990) { |
| 306 | 4 | s->entropy_decode_mono = entropy_decode_mono_3900; | |
| 307 | 4 | s->entropy_decode_stereo = entropy_decode_stereo_3930; | |
| 308 | } else { | ||
| 309 | 5 | s->entropy_decode_mono = entropy_decode_mono_3990; | |
| 310 | 5 | s->entropy_decode_stereo = entropy_decode_stereo_3990; | |
| 311 | } | ||
| 312 | |||
| 313 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 9 times.
|
29 | if (s->fileversion < 3930) { |
| 314 | 20 | s->predictor_decode_mono = predictor_decode_mono_3800; | |
| 315 | 20 | s->predictor_decode_stereo = predictor_decode_stereo_3800; | |
| 316 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5 times.
|
9 | } else if (s->fileversion < 3950) { |
| 317 | 4 | s->predictor_decode_mono = predictor_decode_mono_3930; | |
| 318 | 4 | s->predictor_decode_stereo = predictor_decode_stereo_3930; | |
| 319 | } else { | ||
| 320 | 5 | s->predictor_decode_mono = predictor_decode_mono_3950; | |
| 321 | 5 | s->predictor_decode_stereo = predictor_decode_stereo_3950; | |
| 322 | } | ||
| 323 | |||
| 324 | 29 | ff_bswapdsp_init(&s->bdsp); | |
| 325 | 29 | ff_llauddsp_init(&s->adsp); | |
| 326 | 29 | av_channel_layout_uninit(&avctx->ch_layout); | |
| 327 | 29 | avctx->ch_layout = (channels == 2) ? (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO | |
| 328 |
1/2✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 | : (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO; |
| 329 | |||
| 330 | 29 | return 0; | |
| 331 | } | ||
| 332 | |||
| 333 | /** | ||
| 334 | * @name APE range decoding functions | ||
| 335 | * @{ | ||
| 336 | */ | ||
| 337 | |||
| 338 | #define CODE_BITS 32 | ||
| 339 | #define TOP_VALUE ((unsigned int)1 << (CODE_BITS-1)) | ||
| 340 | #define SHIFT_BITS (CODE_BITS - 9) | ||
| 341 | #define EXTRA_BITS ((CODE_BITS-2) % 8 + 1) | ||
| 342 | #define BOTTOM_VALUE (TOP_VALUE >> 8) | ||
| 343 | |||
| 344 | /** Start the decoder */ | ||
| 345 | 35 | static inline void range_start_decoding(APEContext *ctx) | |
| 346 | { | ||
| 347 | 35 | ctx->rc.buffer = bytestream_get_byte(&ctx->ptr); | |
| 348 | 35 | ctx->rc.low = ctx->rc.buffer >> (8 - EXTRA_BITS); | |
| 349 | 35 | ctx->rc.range = (uint32_t) 1 << EXTRA_BITS; | |
| 350 | 35 | } | |
| 351 | |||
| 352 | /** Perform normalization */ | ||
| 353 | 6060175 | static inline void range_dec_normalize(APEContext *ctx) | |
| 354 | { | ||
| 355 |
2/2✓ Branch 0 taken 3145504 times.
✓ Branch 1 taken 6060175 times.
|
9205679 | while (ctx->rc.range <= BOTTOM_VALUE) { |
| 356 | 3145504 | ctx->rc.buffer <<= 8; | |
| 357 |
2/2✓ Branch 0 taken 2774866 times.
✓ Branch 1 taken 370638 times.
|
3145504 | if(ctx->ptr < ctx->data_end) { |
| 358 | 2774866 | ctx->rc.buffer += *ctx->ptr; | |
| 359 | 2774866 | ctx->ptr++; | |
| 360 | } else { | ||
| 361 | 370638 | ctx->error = 1; | |
| 362 | } | ||
| 363 | 3145504 | ctx->rc.low = (ctx->rc.low << 8) | ((ctx->rc.buffer >> 1) & 0xFF); | |
| 364 | 3145504 | ctx->rc.range <<= 8; | |
| 365 | } | ||
| 366 | 6060175 | } | |
| 367 | |||
| 368 | /** | ||
| 369 | * Calculate cumulative frequency for next symbol. Does NO update! | ||
| 370 | * @param ctx decoder context | ||
| 371 | * @param tot_f is the total frequency or (code_value)1<<shift | ||
| 372 | * @return the cumulative frequency | ||
| 373 | */ | ||
| 374 | 945271 | static inline int range_decode_culfreq(APEContext *ctx, int tot_f) | |
| 375 | { | ||
| 376 | 945271 | range_dec_normalize(ctx); | |
| 377 | 945271 | ctx->rc.help = ctx->rc.range / tot_f; | |
| 378 | 945271 | return ctx->rc.low / ctx->rc.help; | |
| 379 | } | ||
| 380 | |||
| 381 | /** | ||
| 382 | * Decode value with given size in bits | ||
| 383 | * @param ctx decoder context | ||
| 384 | * @param shift number of bits to decode | ||
| 385 | */ | ||
| 386 | 5114892 | static inline int range_decode_culshift(APEContext *ctx, int shift) | |
| 387 | { | ||
| 388 | 5114892 | range_dec_normalize(ctx); | |
| 389 | 5114892 | ctx->rc.help = ctx->rc.range >> shift; | |
| 390 | 5114892 | return ctx->rc.low / ctx->rc.help; | |
| 391 | } | ||
| 392 | |||
| 393 | |||
| 394 | /** | ||
| 395 | * Update decoding state | ||
| 396 | * @param ctx decoder context | ||
| 397 | * @param sy_f the interval length (frequency of the symbol) | ||
| 398 | * @param lt_f the lower end (frequency sum of < symbols) | ||
| 399 | */ | ||
| 400 | 6060163 | static inline void range_decode_update(APEContext *ctx, int sy_f, int lt_f) | |
| 401 | { | ||
| 402 | 6060163 | ctx->rc.low -= ctx->rc.help * lt_f; | |
| 403 | 6060163 | ctx->rc.range = ctx->rc.help * sy_f; | |
| 404 | 6060163 | } | |
| 405 | |||
| 406 | /** Decode n bits (n <= 16) without modelling */ | ||
| 407 | 2101260 | static inline int range_decode_bits(APEContext *ctx, int n) | |
| 408 | { | ||
| 409 | 2101260 | int sym = range_decode_culshift(ctx, n); | |
| 410 | 2101260 | range_decode_update(ctx, 1, sym); | |
| 411 | 2101260 | return sym; | |
| 412 | } | ||
| 413 | |||
| 414 | |||
| 415 | #define MODEL_ELEMENTS 64 | ||
| 416 | |||
| 417 | /** | ||
| 418 | * Fixed probabilities for symbols in Monkey Audio version 3.97 | ||
| 419 | */ | ||
| 420 | static const uint16_t counts_3970[22] = { | ||
| 421 | 0, 14824, 28224, 39348, 47855, 53994, 58171, 60926, | ||
| 422 | 62682, 63786, 64463, 64878, 65126, 65276, 65365, 65419, | ||
| 423 | 65450, 65469, 65480, 65487, 65491, 65493, | ||
| 424 | }; | ||
| 425 | |||
| 426 | /** | ||
| 427 | * Probability ranges for symbols in Monkey Audio version 3.97 | ||
| 428 | */ | ||
| 429 | static const uint16_t counts_diff_3970[21] = { | ||
| 430 | 14824, 13400, 11124, 8507, 6139, 4177, 2755, 1756, | ||
| 431 | 1104, 677, 415, 248, 150, 89, 54, 31, | ||
| 432 | 19, 11, 7, 4, 2, | ||
| 433 | }; | ||
| 434 | |||
| 435 | /** | ||
| 436 | * Fixed probabilities for symbols in Monkey Audio version 3.98 | ||
| 437 | */ | ||
| 438 | static const uint16_t counts_3980[22] = { | ||
| 439 | 0, 19578, 36160, 48417, 56323, 60899, 63265, 64435, | ||
| 440 | 64971, 65232, 65351, 65416, 65447, 65466, 65476, 65482, | ||
| 441 | 65485, 65488, 65490, 65491, 65492, 65493, | ||
| 442 | }; | ||
| 443 | |||
| 444 | /** | ||
| 445 | * Probability ranges for symbols in Monkey Audio version 3.98 | ||
| 446 | */ | ||
| 447 | static const uint16_t counts_diff_3980[21] = { | ||
| 448 | 19578, 16582, 12257, 7906, 4576, 2366, 1170, 536, | ||
| 449 | 261, 119, 65, 31, 19, 10, 6, 3, | ||
| 450 | 3, 2, 1, 1, 1, | ||
| 451 | }; | ||
| 452 | |||
| 453 | /** | ||
| 454 | * Decode symbol | ||
| 455 | * @param ctx decoder context | ||
| 456 | * @param counts probability range start position | ||
| 457 | * @param counts_diff probability range widths | ||
| 458 | */ | ||
| 459 | 3013632 | static inline int range_get_symbol(APEContext *ctx, | |
| 460 | const uint16_t counts[], | ||
| 461 | const uint16_t counts_diff[]) | ||
| 462 | { | ||
| 463 | int symbol, cf; | ||
| 464 | |||
| 465 | 3013632 | cf = range_decode_culshift(ctx, 16); | |
| 466 | |||
| 467 |
2/2✓ Branch 0 taken 284 times.
✓ Branch 1 taken 3013348 times.
|
3013632 | if(cf > 65492){ |
| 468 | 284 | symbol= cf - 65535 + 63; | |
| 469 | 284 | range_decode_update(ctx, 1, cf); | |
| 470 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 277 times.
|
284 | if(cf > 65535) |
| 471 | 7 | ctx->error=1; | |
| 472 | 284 | return symbol; | |
| 473 | } | ||
| 474 | /* figure out the symbol inefficiently; a binary search would be much better */ | ||
| 475 |
2/2✓ Branch 0 taken 5520748 times.
✓ Branch 1 taken 3013348 times.
|
8534096 | for (symbol = 0; counts[symbol + 1] <= cf; symbol++); |
| 476 | |||
| 477 | 3013348 | range_decode_update(ctx, counts_diff[symbol], counts[symbol]); | |
| 478 | |||
| 479 | 3013348 | return symbol; | |
| 480 | } | ||
| 481 | /** @} */ // group rangecoder | ||
| 482 | |||
| 483 | 3013632 | static inline void update_rice(APERice *rice, unsigned int x) | |
| 484 | { | ||
| 485 |
2/2✓ Branch 0 taken 2585021 times.
✓ Branch 1 taken 428611 times.
|
3013632 | int lim = rice->k ? (1 << (rice->k + 4)) : 0; |
| 486 | 3013632 | rice->ksum += ((x + 1) / 2) - ((rice->ksum + 16) >> 5); | |
| 487 | |||
| 488 |
2/2✓ Branch 0 taken 29711 times.
✓ Branch 1 taken 2983921 times.
|
3013632 | if (rice->ksum < lim) |
| 489 | 29711 | rice->k--; | |
| 490 |
3/4✓ Branch 0 taken 29539 times.
✓ Branch 1 taken 2954382 times.
✓ Branch 2 taken 29539 times.
✗ Branch 3 not taken.
|
2983921 | else if (rice->ksum >= (1 << (rice->k + 5)) && rice->k < 24) |
| 491 | 29539 | rice->k++; | |
| 492 | 3013632 | } | |
| 493 | |||
| 494 | 482165 | static inline int get_rice_ook(GetBitContext *gb, int k) | |
| 495 | { | ||
| 496 | unsigned int x; | ||
| 497 | |||
| 498 | 482165 | x = get_unary(gb, 1, get_bits_left(gb)); | |
| 499 | |||
| 500 |
2/2✓ Branch 0 taken 482106 times.
✓ Branch 1 taken 59 times.
|
482165 | if (k) |
| 501 | 482106 | x = (x << k) | get_bits(gb, k); | |
| 502 | |||
| 503 | 482165 | return x; | |
| 504 | } | ||
| 505 | |||
| 506 | 1253376 | static inline int ape_decode_value_3860(APEContext *ctx, GetBitContext *gb, | |
| 507 | APERice *rice) | ||
| 508 | { | ||
| 509 | unsigned int x, overflow; | ||
| 510 | |||
| 511 | 1253376 | overflow = get_unary(gb, 1, get_bits_left(gb)); | |
| 512 | |||
| 513 |
2/2✓ Branch 0 taken 626688 times.
✓ Branch 1 taken 626688 times.
|
1253376 | if (ctx->fileversion > 3880) { |
| 514 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 626688 times.
|
626689 | while (overflow >= 16) { |
| 515 | 1 | overflow -= 16; | |
| 516 | 1 | rice->k += 4; | |
| 517 | } | ||
| 518 | } | ||
| 519 | |||
| 520 |
2/2✓ Branch 0 taken 285870 times.
✓ Branch 1 taken 967506 times.
|
1253376 | if (!rice->k) |
| 521 | 285870 | x = overflow; | |
| 522 |
1/2✓ Branch 0 taken 967506 times.
✗ Branch 1 not taken.
|
967506 | else if(rice->k <= MIN_CACHE_BITS) { |
| 523 | 967506 | x = (overflow << rice->k) + get_bits(gb, rice->k); | |
| 524 | } else { | ||
| 525 | ✗ | av_log(ctx->avctx, AV_LOG_ERROR, "Too many bits: %"PRIu32"\n", rice->k); | |
| 526 | ✗ | ctx->error = 1; | |
| 527 | ✗ | return AVERROR_INVALIDDATA; | |
| 528 | } | ||
| 529 | 1253376 | rice->ksum += x - (rice->ksum + 8 >> 4); | |
| 530 |
4/4✓ Branch 0 taken 967506 times.
✓ Branch 1 taken 285870 times.
✓ Branch 2 taken 24811 times.
✓ Branch 3 taken 1228565 times.
|
1253376 | if (rice->ksum < (rice->k ? 1 << (rice->k + 4) : 0)) |
| 531 | 24811 | rice->k--; | |
| 532 |
3/4✓ Branch 0 taken 24603 times.
✓ Branch 1 taken 1203962 times.
✓ Branch 2 taken 24603 times.
✗ Branch 3 not taken.
|
1228565 | else if (rice->ksum >= (1 << (rice->k + 5)) && rice->k < 24) |
| 533 | 24603 | rice->k++; | |
| 534 | |||
| 535 | /* Convert to signed */ | ||
| 536 | 1253376 | return ((x >> 1) ^ ((x & 1) - 1)) + 1; | |
| 537 | } | ||
| 538 | |||
| 539 | 2101248 | static inline int ape_decode_value_3900(APEContext *ctx, APERice *rice) | |
| 540 | { | ||
| 541 | unsigned int x, overflow; | ||
| 542 | int tmpk; | ||
| 543 | |||
| 544 | 2101248 | overflow = range_get_symbol(ctx, counts_3970, counts_diff_3970); | |
| 545 | |||
| 546 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2101248 times.
|
2101248 | if (overflow == (MODEL_ELEMENTS - 1)) { |
| 547 | ✗ | tmpk = range_decode_bits(ctx, 5); | |
| 548 | ✗ | overflow = 0; | |
| 549 | } else | ||
| 550 |
2/2✓ Branch 0 taken 1679116 times.
✓ Branch 1 taken 422132 times.
|
2101248 | tmpk = (rice->k < 1) ? 0 : rice->k - 1; |
| 551 | |||
| 552 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 2101248 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
2101248 | if (tmpk <= 16 || ctx->fileversion < 3910) { |
| 553 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2101248 times.
|
2101248 | if (tmpk > 23) { |
| 554 | ✗ | av_log(ctx->avctx, AV_LOG_ERROR, "Too many bits: %d\n", tmpk); | |
| 555 | ✗ | return AVERROR_INVALIDDATA; | |
| 556 | } | ||
| 557 | 2101248 | x = range_decode_bits(ctx, tmpk); | |
| 558 | ✗ | } else if (tmpk <= 31) { | |
| 559 | ✗ | x = range_decode_bits(ctx, 16); | |
| 560 | ✗ | x |= (range_decode_bits(ctx, tmpk - 16) << 16); | |
| 561 | } else { | ||
| 562 | ✗ | av_log(ctx->avctx, AV_LOG_ERROR, "Too many bits: %d\n", tmpk); | |
| 563 | ✗ | return AVERROR_INVALIDDATA; | |
| 564 | } | ||
| 565 | 2101248 | x += overflow << tmpk; | |
| 566 | |||
| 567 | 2101248 | update_rice(rice, x); | |
| 568 | |||
| 569 | /* Convert to signed */ | ||
| 570 | 2101248 | return ((x >> 1) ^ ((x & 1) - 1)) + 1; | |
| 571 | } | ||
| 572 | |||
| 573 | 912384 | static inline int ape_decode_value_3990(APEContext *ctx, APERice *rice) | |
| 574 | { | ||
| 575 | unsigned int x, overflow, pivot; | ||
| 576 | int base; | ||
| 577 | |||
| 578 | 912384 | pivot = FFMAX(rice->ksum >> 5, 1); | |
| 579 | |||
| 580 | 912384 | overflow = range_get_symbol(ctx, counts_3980, counts_diff_3980); | |
| 581 | |||
| 582 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 912378 times.
|
912384 | if (overflow == (MODEL_ELEMENTS - 1)) { |
| 583 | 6 | overflow = (unsigned)range_decode_bits(ctx, 16) << 16; | |
| 584 | 6 | overflow |= range_decode_bits(ctx, 16); | |
| 585 | } | ||
| 586 | |||
| 587 |
2/2✓ Branch 0 taken 879497 times.
✓ Branch 1 taken 32887 times.
|
912384 | if (pivot < 0x10000) { |
| 588 | 879497 | base = range_decode_culfreq(ctx, pivot); | |
| 589 | 879497 | range_decode_update(ctx, 1, base); | |
| 590 | } else { | ||
| 591 | 32887 | int base_hi = pivot, base_lo; | |
| 592 | 32887 | int bbits = 0; | |
| 593 | |||
| 594 |
2/2✓ Branch 0 taken 214168 times.
✓ Branch 1 taken 32887 times.
|
247055 | while (base_hi & ~0xFFFF) { |
| 595 | 214168 | base_hi >>= 1; | |
| 596 | 214168 | bbits++; | |
| 597 | } | ||
| 598 | 32887 | base_hi = range_decode_culfreq(ctx, base_hi + 1); | |
| 599 | 32887 | range_decode_update(ctx, 1, base_hi); | |
| 600 | 32887 | base_lo = range_decode_culfreq(ctx, 1 << bbits); | |
| 601 | 32887 | range_decode_update(ctx, 1, base_lo); | |
| 602 | |||
| 603 | 32887 | base = (base_hi << bbits) + base_lo; | |
| 604 | } | ||
| 605 | |||
| 606 | 912384 | x = base + overflow * pivot; | |
| 607 | |||
| 608 | 912384 | update_rice(rice, x); | |
| 609 | |||
| 610 | /* Convert to signed */ | ||
| 611 | 912384 | return ((x >> 1) ^ ((x & 1) - 1)) + 1; | |
| 612 | } | ||
| 613 | |||
| 614 | 1586 | static int get_k(int ksum) | |
| 615 | { | ||
| 616 | 1586 | return av_log2(ksum) + !!ksum; | |
| 617 | } | ||
| 618 | |||
| 619 | 26 | static void decode_array_0000(APEContext *ctx, GetBitContext *gb, | |
| 620 | int32_t *out, APERice *rice, int blockstodecode) | ||
| 621 | { | ||
| 622 | int i; | ||
| 623 | unsigned ksummax, ksummin; | ||
| 624 | |||
| 625 | 26 | rice->ksum = 0; | |
| 626 |
2/2✓ Branch 0 taken 130 times.
✓ Branch 1 taken 26 times.
|
156 | for (i = 0; i < FFMIN(blockstodecode, 5); i++) { |
| 627 | 130 | out[i] = get_rice_ook(&ctx->gb, 10); | |
| 628 | 130 | rice->ksum += out[i]; | |
| 629 | } | ||
| 630 | |||
| 631 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
|
26 | if (blockstodecode <= 5) |
| 632 | ✗ | goto end; | |
| 633 | |||
| 634 | 26 | rice->k = get_k(rice->ksum / 10); | |
| 635 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
|
26 | if (rice->k >= 24) |
| 636 | ✗ | return; | |
| 637 |
2/2✓ Branch 0 taken 1534 times.
✓ Branch 1 taken 26 times.
|
1560 | for (; i < FFMIN(blockstodecode, 64); i++) { |
| 638 | 1534 | out[i] = get_rice_ook(&ctx->gb, rice->k); | |
| 639 | 1534 | rice->ksum += out[i]; | |
| 640 | 1534 | rice->k = get_k(rice->ksum / ((i + 1) * 2)); | |
| 641 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1534 times.
|
1534 | if (rice->k >= 24) |
| 642 | ✗ | return; | |
| 643 | } | ||
| 644 | |||
| 645 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
|
26 | if (blockstodecode <= 64) |
| 646 | ✗ | goto end; | |
| 647 | |||
| 648 | 26 | rice->k = get_k(rice->ksum >> 7); | |
| 649 | 26 | ksummax = 1 << rice->k + 7; | |
| 650 |
2/2✓ Branch 0 taken 25 times.
✓ Branch 1 taken 1 times.
|
26 | ksummin = rice->k ? (1 << rice->k + 6) : 0; |
| 651 |
2/2✓ Branch 0 taken 480503 times.
✓ Branch 1 taken 24 times.
|
480527 | for (; i < blockstodecode; i++) { |
| 652 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 480501 times.
|
480503 | if (get_bits_left(&ctx->gb) < 1) { |
| 653 | 2 | ctx->error = 1; | |
| 654 | 2 | return; | |
| 655 | } | ||
| 656 | 480501 | out[i] = get_rice_ook(&ctx->gb, rice->k); | |
| 657 | 480501 | rice->ksum += out[i] - (unsigned)out[i - 64]; | |
| 658 |
2/2✓ Branch 0 taken 4040 times.
✓ Branch 1 taken 480501 times.
|
484541 | while (rice->ksum < ksummin) { |
| 659 | 4040 | rice->k--; | |
| 660 |
1/2✓ Branch 0 taken 4040 times.
✗ Branch 1 not taken.
|
4040 | ksummin = rice->k ? ksummin >> 1 : 0; |
| 661 | 4040 | ksummax >>= 1; | |
| 662 | } | ||
| 663 |
2/2✓ Branch 0 taken 4029 times.
✓ Branch 1 taken 480501 times.
|
484530 | while (rice->ksum >= ksummax) { |
| 664 | 4029 | rice->k++; | |
| 665 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4029 times.
|
4029 | if (rice->k > 24) |
| 666 | ✗ | return; | |
| 667 | 4029 | ksummax <<= 1; | |
| 668 |
1/2✓ Branch 0 taken 4029 times.
✗ Branch 1 not taken.
|
4029 | ksummin = ksummin ? ksummin << 1 : 128; |
| 669 | } | ||
| 670 | } | ||
| 671 | |||
| 672 | 24 | end: | |
| 673 |
2/2✓ Branch 0 taken 479232 times.
✓ Branch 1 taken 24 times.
|
479256 | for (i = 0; i < blockstodecode; i++) |
| 674 | 479232 | out[i] = ((out[i] >> 1) ^ ((out[i] & 1) - 1)) + 1; | |
| 675 | } | ||
| 676 | |||
| 677 | ✗ | static void entropy_decode_mono_0000(APEContext *ctx, int blockstodecode) | |
| 678 | { | ||
| 679 | ✗ | decode_array_0000(ctx, &ctx->gb, ctx->decoded[0], &ctx->riceY, | |
| 680 | blockstodecode); | ||
| 681 | ✗ | } | |
| 682 | |||
| 683 | 13 | static void entropy_decode_stereo_0000(APEContext *ctx, int blockstodecode) | |
| 684 | { | ||
| 685 | 13 | decode_array_0000(ctx, &ctx->gb, ctx->decoded[0], &ctx->riceY, | |
| 686 | blockstodecode); | ||
| 687 | 13 | decode_array_0000(ctx, &ctx->gb, ctx->decoded[1], &ctx->riceX, | |
| 688 | blockstodecode); | ||
| 689 | 13 | } | |
| 690 | |||
| 691 | ✗ | static void entropy_decode_mono_3860(APEContext *ctx, int blockstodecode) | |
| 692 | { | ||
| 693 | ✗ | int32_t *decoded0 = ctx->decoded[0]; | |
| 694 | |||
| 695 | ✗ | while (blockstodecode--) | |
| 696 | ✗ | *decoded0++ = ape_decode_value_3860(ctx, &ctx->gb, &ctx->riceY); | |
| 697 | ✗ | } | |
| 698 | |||
| 699 | 26 | static void entropy_decode_stereo_3860(APEContext *ctx, int blockstodecode) | |
| 700 | { | ||
| 701 | 26 | int32_t *decoded0 = ctx->decoded[0]; | |
| 702 | 26 | int32_t *decoded1 = ctx->decoded[1]; | |
| 703 | 26 | int blocks = blockstodecode; | |
| 704 | |||
| 705 |
2/2✓ Branch 0 taken 626688 times.
✓ Branch 1 taken 26 times.
|
626714 | while (blockstodecode--) |
| 706 | 626688 | *decoded0++ = ape_decode_value_3860(ctx, &ctx->gb, &ctx->riceY); | |
| 707 |
2/2✓ Branch 0 taken 626688 times.
✓ Branch 1 taken 26 times.
|
626714 | while (blocks--) |
| 708 | 626688 | *decoded1++ = ape_decode_value_3860(ctx, &ctx->gb, &ctx->riceX); | |
| 709 | 26 | } | |
| 710 | |||
| 711 | ✗ | static void entropy_decode_mono_3900(APEContext *ctx, int blockstodecode) | |
| 712 | { | ||
| 713 | ✗ | int32_t *decoded0 = ctx->decoded[0]; | |
| 714 | |||
| 715 | ✗ | while (blockstodecode--) | |
| 716 | ✗ | *decoded0++ = ape_decode_value_3900(ctx, &ctx->riceY); | |
| 717 | ✗ | } | |
| 718 | |||
| 719 | 12 | static void entropy_decode_stereo_3900(APEContext *ctx, int blockstodecode) | |
| 720 | { | ||
| 721 | 12 | int32_t *decoded0 = ctx->decoded[0]; | |
| 722 | 12 | int32_t *decoded1 = ctx->decoded[1]; | |
| 723 | 12 | int blocks = blockstodecode; | |
| 724 | |||
| 725 |
2/2✓ Branch 0 taken 884736 times.
✓ Branch 1 taken 12 times.
|
884748 | while (blockstodecode--) |
| 726 | 884736 | *decoded0++ = ape_decode_value_3900(ctx, &ctx->riceY); | |
| 727 | 12 | range_dec_normalize(ctx); | |
| 728 | // because of some implementation peculiarities we need to backpedal here | ||
| 729 | 12 | ctx->ptr -= 1; | |
| 730 | 12 | range_start_decoding(ctx); | |
| 731 |
2/2✓ Branch 0 taken 884736 times.
✓ Branch 1 taken 12 times.
|
884748 | while (blocks--) |
| 732 | 884736 | *decoded1++ = ape_decode_value_3900(ctx, &ctx->riceX); | |
| 733 | 12 | } | |
| 734 | |||
| 735 | 36 | static void entropy_decode_stereo_3930(APEContext *ctx, int blockstodecode) | |
| 736 | { | ||
| 737 | 36 | int32_t *decoded0 = ctx->decoded[0]; | |
| 738 | 36 | int32_t *decoded1 = ctx->decoded[1]; | |
| 739 | |||
| 740 |
2/2✓ Branch 0 taken 165888 times.
✓ Branch 1 taken 36 times.
|
165924 | while (blockstodecode--) { |
| 741 | 165888 | *decoded0++ = ape_decode_value_3900(ctx, &ctx->riceY); | |
| 742 | 165888 | *decoded1++ = ape_decode_value_3900(ctx, &ctx->riceX); | |
| 743 | } | ||
| 744 | 36 | } | |
| 745 | |||
| 746 | ✗ | static void entropy_decode_mono_3990(APEContext *ctx, int blockstodecode) | |
| 747 | { | ||
| 748 | ✗ | int32_t *decoded0 = ctx->decoded[0]; | |
| 749 | |||
| 750 | ✗ | while (blockstodecode--) | |
| 751 | ✗ | *decoded0++ = ape_decode_value_3990(ctx, &ctx->riceY); | |
| 752 | ✗ | } | |
| 753 | |||
| 754 | 99 | static void entropy_decode_stereo_3990(APEContext *ctx, int blockstodecode) | |
| 755 | { | ||
| 756 | 99 | int32_t *decoded0 = ctx->decoded[0]; | |
| 757 | 99 | int32_t *decoded1 = ctx->decoded[1]; | |
| 758 | |||
| 759 |
2/2✓ Branch 0 taken 456192 times.
✓ Branch 1 taken 99 times.
|
456291 | while (blockstodecode--) { |
| 760 | 456192 | *decoded0++ = ape_decode_value_3990(ctx, &ctx->riceY); | |
| 761 | 456192 | *decoded1++ = ape_decode_value_3990(ctx, &ctx->riceX); | |
| 762 | } | ||
| 763 | 99 | } | |
| 764 | |||
| 765 | 62 | static int init_entropy_decoder(APEContext *ctx) | |
| 766 | { | ||
| 767 | /* Read the CRC */ | ||
| 768 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 39 times.
|
62 | if (ctx->fileversion >= 3900) { |
| 769 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
|
23 | if (ctx->data_end - ctx->ptr < 6) |
| 770 | ✗ | return AVERROR_INVALIDDATA; | |
| 771 | 23 | ctx->CRC = bytestream_get_be32(&ctx->ptr); | |
| 772 | } else { | ||
| 773 | 39 | ctx->CRC = get_bits_long(&ctx->gb, 32); | |
| 774 | } | ||
| 775 | |||
| 776 | /* Read the frame flags if they exist */ | ||
| 777 | 62 | ctx->frameflags = 0; | |
| 778 | 62 | ctx->CRC_state = UINT32_MAX; | |
| 779 |
3/4✓ Branch 0 taken 49 times.
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 49 times.
|
62 | if ((ctx->fileversion > 3820) && (ctx->CRC & 0x80000000)) { |
| 780 | ✗ | ctx->CRC &= ~0x80000000; | |
| 781 | |||
| 782 | ✗ | if (ctx->data_end - ctx->ptr < 6) | |
| 783 | ✗ | return AVERROR_INVALIDDATA; | |
| 784 | ✗ | ctx->frameflags = bytestream_get_be32(&ctx->ptr); | |
| 785 | } | ||
| 786 | |||
| 787 | /* Initialize the rice structs */ | ||
| 788 | 62 | ctx->riceX.k = 10; | |
| 789 | 62 | ctx->riceX.ksum = (1 << ctx->riceX.k) * 16; | |
| 790 | 62 | ctx->riceY.k = 10; | |
| 791 | 62 | ctx->riceY.ksum = (1 << ctx->riceY.k) * 16; | |
| 792 | |||
| 793 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 39 times.
|
62 | if (ctx->fileversion >= 3900) { |
| 794 | /* The first 8 bits of input are ignored. */ | ||
| 795 | 23 | ctx->ptr++; | |
| 796 | |||
| 797 | 23 | range_start_decoding(ctx); | |
| 798 | } | ||
| 799 | |||
| 800 | 62 | return 0; | |
| 801 | } | ||
| 802 | |||
| 803 | static const int32_t initial_coeffs_fast_3320[1] = { | ||
| 804 | 375, | ||
| 805 | }; | ||
| 806 | |||
| 807 | static const int32_t initial_coeffs_a_3800[3] = { | ||
| 808 | 64, 115, 64, | ||
| 809 | }; | ||
| 810 | |||
| 811 | static const int32_t initial_coeffs_b_3800[2] = { | ||
| 812 | 740, 0 | ||
| 813 | }; | ||
| 814 | |||
| 815 | static const int32_t initial_coeffs_3930[4] = { | ||
| 816 | 360, 317, -109, 98 | ||
| 817 | }; | ||
| 818 | |||
| 819 | static const int64_t initial_coeffs_3930_64bit[4] = { | ||
| 820 | 360, 317, -109, 98 | ||
| 821 | }; | ||
| 822 | |||
| 823 | 62 | static void init_predictor_decoder(APEContext *ctx) | |
| 824 | { | ||
| 825 | 62 | APEPredictor *p = &ctx->predictor; | |
| 826 | 62 | APEPredictor64 *p64 = &ctx->predictor64; | |
| 827 | |||
| 828 | /* Zero the history buffers */ | ||
| 829 | 62 | memset(p->historybuffer, 0, PREDICTOR_SIZE * sizeof(*p->historybuffer)); | |
| 830 | 62 | memset(p64->historybuffer, 0, PREDICTOR_SIZE * sizeof(*p64->historybuffer)); | |
| 831 | 62 | p->buf = p->historybuffer; | |
| 832 | 62 | p64->buf = p64->historybuffer; | |
| 833 | |||
| 834 | /* Initialize and zero the coefficients */ | ||
| 835 |
2/2✓ Branch 0 taken 51 times.
✓ Branch 1 taken 11 times.
|
62 | if (ctx->fileversion < 3930) { |
| 836 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
|
51 | if (ctx->compression_level == COMPRESSION_LEVEL_FAST) { |
| 837 | ✗ | memcpy(p->coeffsA[0], initial_coeffs_fast_3320, | |
| 838 | sizeof(initial_coeffs_fast_3320)); | ||
| 839 | ✗ | memcpy(p->coeffsA[1], initial_coeffs_fast_3320, | |
| 840 | sizeof(initial_coeffs_fast_3320)); | ||
| 841 | } else { | ||
| 842 | 51 | memcpy(p->coeffsA[0], initial_coeffs_a_3800, | |
| 843 | sizeof(initial_coeffs_a_3800)); | ||
| 844 | 51 | memcpy(p->coeffsA[1], initial_coeffs_a_3800, | |
| 845 | sizeof(initial_coeffs_a_3800)); | ||
| 846 | } | ||
| 847 | } else { | ||
| 848 | 11 | memcpy(p->coeffsA[0], initial_coeffs_3930, sizeof(initial_coeffs_3930)); | |
| 849 | 11 | memcpy(p->coeffsA[1], initial_coeffs_3930, sizeof(initial_coeffs_3930)); | |
| 850 | 11 | memcpy(p64->coeffsA[0], initial_coeffs_3930_64bit, sizeof(initial_coeffs_3930_64bit)); | |
| 851 | 11 | memcpy(p64->coeffsA[1], initial_coeffs_3930_64bit, sizeof(initial_coeffs_3930_64bit)); | |
| 852 | } | ||
| 853 | 62 | memset(p->coeffsB, 0, sizeof(p->coeffsB)); | |
| 854 | 62 | memset(p64->coeffsB, 0, sizeof(p64->coeffsB)); | |
| 855 |
2/2✓ Branch 0 taken 51 times.
✓ Branch 1 taken 11 times.
|
62 | if (ctx->fileversion < 3930) { |
| 856 | 51 | memcpy(p->coeffsB[0], initial_coeffs_b_3800, | |
| 857 | sizeof(initial_coeffs_b_3800)); | ||
| 858 | 51 | memcpy(p->coeffsB[1], initial_coeffs_b_3800, | |
| 859 | sizeof(initial_coeffs_b_3800)); | ||
| 860 | } | ||
| 861 | |||
| 862 | 62 | p->filterA[0] = p->filterA[1] = 0; | |
| 863 | 62 | p->filterB[0] = p->filterB[1] = 0; | |
| 864 | 62 | p->lastA[0] = p->lastA[1] = 0; | |
| 865 | |||
| 866 | 62 | p64->filterA[0] = p64->filterA[1] = 0; | |
| 867 | 62 | p64->filterB[0] = p64->filterB[1] = 0; | |
| 868 | 62 | p64->lastA[0] = p64->lastA[1] = 0; | |
| 869 | |||
| 870 | 62 | p->sample_pos = 0; | |
| 871 | 62 | } | |
| 872 | |||
| 873 | /** Get inverse sign of integer (-1 for positive, 1 for negative and 0 for zero) */ | ||
| 874 | 16129995 | static inline int APESIGN(int32_t x) { | |
| 875 | 16129995 | return (x < 0) - (x > 0); | |
| 876 | } | ||
| 877 | |||
| 878 | ✗ | static av_always_inline int filter_fast_3320(APEPredictor *p, | |
| 879 | const int decoded, const int filter, | ||
| 880 | const int delayA) | ||
| 881 | { | ||
| 882 | int32_t predictionA; | ||
| 883 | |||
| 884 | ✗ | p->buf[delayA] = p->lastA[filter]; | |
| 885 | ✗ | if (p->sample_pos < 3) { | |
| 886 | ✗ | p->lastA[filter] = decoded; | |
| 887 | ✗ | p->filterA[filter] = decoded; | |
| 888 | ✗ | return decoded; | |
| 889 | } | ||
| 890 | |||
| 891 | ✗ | predictionA = p->buf[delayA] * 2U - p->buf[delayA - 1]; | |
| 892 | ✗ | p->lastA[filter] = decoded + (unsigned)((int32_t)(predictionA * p->coeffsA[filter][0]) >> 9); | |
| 893 | |||
| 894 | ✗ | if ((decoded ^ predictionA) > 0) | |
| 895 | ✗ | p->coeffsA[filter][0]++; | |
| 896 | else | ||
| 897 | ✗ | p->coeffsA[filter][0]--; | |
| 898 | |||
| 899 | ✗ | p->filterA[filter] += (unsigned)p->lastA[filter]; | |
| 900 | |||
| 901 | ✗ | return p->filterA[filter]; | |
| 902 | } | ||
| 903 | |||
| 904 | 2912256 | static av_always_inline int filter_3800(APEPredictor *p, | |
| 905 | const unsigned decoded, const int filter, | ||
| 906 | const int delayA, const int delayB, | ||
| 907 | const int start, const int shift) | ||
| 908 | { | ||
| 909 | int32_t predictionA, predictionB, sign; | ||
| 910 | int32_t d0, d1, d2, d3, d4; | ||
| 911 | |||
| 912 | 2912256 | p->buf[delayA] = p->lastA[filter]; | |
| 913 | 2912256 | p->buf[delayB] = p->filterB[filter]; | |
| 914 |
2/2✓ Branch 0 taken 5904 times.
✓ Branch 1 taken 2906352 times.
|
2912256 | if (p->sample_pos < start) { |
| 915 | 5904 | predictionA = decoded + p->filterA[filter]; | |
| 916 | 5904 | p->lastA[filter] = decoded; | |
| 917 | 5904 | p->filterB[filter] = decoded; | |
| 918 | 5904 | p->filterA[filter] = predictionA; | |
| 919 | 5904 | return predictionA; | |
| 920 | } | ||
| 921 | 2906352 | d2 = p->buf[delayA]; | |
| 922 | 2906352 | d1 = (p->buf[delayA] - (unsigned)p->buf[delayA - 1]) * 2; | |
| 923 | 2906352 | d0 = p->buf[delayA] + ((p->buf[delayA - 2] - (unsigned)p->buf[delayA - 1]) * 8); | |
| 924 | 2906352 | d3 = p->buf[delayB] * 2U - p->buf[delayB - 1]; | |
| 925 | 2906352 | d4 = p->buf[delayB]; | |
| 926 | |||
| 927 | 2906352 | predictionA = d0 * p->coeffsA[filter][0] + | |
| 928 | 2906352 | d1 * p->coeffsA[filter][1] + | |
| 929 | 2906352 | d2 * p->coeffsA[filter][2]; | |
| 930 | |||
| 931 | 2906352 | sign = APESIGN(decoded); | |
| 932 | 2906352 | p->coeffsA[filter][0] += (((d0 >> 30) & 2) - 1) * sign; | |
| 933 | 2906352 | p->coeffsA[filter][1] += (((d1 >> 28) & 8) - 4) * sign; | |
| 934 | 2906352 | p->coeffsA[filter][2] += (((d2 >> 28) & 8) - 4) * sign; | |
| 935 | |||
| 936 | 2906352 | predictionB = d3 * p->coeffsB[filter][0] - | |
| 937 | 2906352 | d4 * p->coeffsB[filter][1]; | |
| 938 | 2906352 | p->lastA[filter] = decoded + (predictionA >> 11); | |
| 939 | 2906352 | sign = APESIGN(p->lastA[filter]); | |
| 940 | 2906352 | p->coeffsB[filter][0] += (((d3 >> 29) & 4) - 2) * sign; | |
| 941 | 2906352 | p->coeffsB[filter][1] -= (((d4 >> 30) & 2) - 1) * sign; | |
| 942 | |||
| 943 | 2906352 | p->filterB[filter] = p->lastA[filter] + (unsigned)(predictionB >> shift); | |
| 944 | 2906352 | p->filterA[filter] = p->filterB[filter] + (unsigned)((int)(p->filterA[filter] * 31U) >> 5); | |
| 945 | |||
| 946 | 2906352 | return p->filterA[filter]; | |
| 947 | } | ||
| 948 | |||
| 949 | 24 | static void long_filter_high_3800(int32_t *buffer, int order, int shift, int length) | |
| 950 | { | ||
| 951 | int i, j; | ||
| 952 | int32_t dotprod, sign; | ||
| 953 | 24 | int32_t coeffs[256], delay[256+256], *delayp = delay; | |
| 954 | |||
| 955 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | if (order >= length) |
| 956 | ✗ | return; | |
| 957 | |||
| 958 | 24 | memset(coeffs, 0, order * sizeof(*coeffs)); | |
| 959 |
2/2✓ Branch 0 taken 5632 times.
✓ Branch 1 taken 24 times.
|
5656 | for (i = 0; i < order; i++) |
| 960 | 5632 | delay[i] = buffer[i]; | |
| 961 |
2/2✓ Branch 0 taken 1763840 times.
✓ Branch 1 taken 24 times.
|
1763864 | for (i = order; i < length; i++) { |
| 962 | 1763840 | dotprod = 0; | |
| 963 | 1763840 | sign = APESIGN(buffer[i]); | |
| 964 |
2/2✓ Branch 0 taken 727881 times.
✓ Branch 1 taken 1035959 times.
|
1763840 | if (sign == 1) { |
| 965 |
2/2✓ Branch 0 taken 167741056 times.
✓ Branch 1 taken 727881 times.
|
168468937 | for (j = 0; j < order; j++) { |
| 966 | 167741056 | dotprod += delayp[j] * (unsigned)coeffs[j]; | |
| 967 | 167741056 | coeffs[j] += (delayp[j] >> 31) | 1; | |
| 968 | } | ||
| 969 |
2/2✓ Branch 0 taken 739616 times.
✓ Branch 1 taken 296343 times.
|
1035959 | } else if (sign == -1) { |
| 970 |
2/2✓ Branch 0 taken 170526720 times.
✓ Branch 1 taken 739616 times.
|
171266336 | for (j = 0; j < order; j++) { |
| 971 | 170526720 | dotprod += delayp[j] * (unsigned)coeffs[j]; | |
| 972 | 170526720 | coeffs[j] -= (delayp[j] >> 31) | 1; | |
| 973 | } | ||
| 974 | } else { | ||
| 975 |
2/2✓ Branch 0 taken 75592064 times.
✓ Branch 1 taken 296343 times.
|
75888407 | for (j = 0; j < order; j++) { |
| 976 | 75592064 | dotprod += delayp[j] * (unsigned)coeffs[j]; | |
| 977 | } | ||
| 978 | } | ||
| 979 | 1763840 | buffer[i] -= (unsigned)(dotprod >> shift); | |
| 980 | 1763840 | delayp ++; | |
| 981 | 1763840 | delayp[order - 1] = buffer[i]; | |
| 982 |
2/2✓ Branch 0 taken 6888 times.
✓ Branch 1 taken 1756952 times.
|
1763840 | if (delayp - delay == 256) { |
| 983 | 6888 | memcpy(delay, delayp, sizeof(*delay)*256); | |
| 984 | 6888 | delayp = delay; | |
| 985 | } | ||
| 986 | } | ||
| 987 | } | ||
| 988 | |||
| 989 | 20 | static void long_filter_ehigh_3830(int32_t *buffer, int length) | |
| 990 | { | ||
| 991 | int i, j; | ||
| 992 | int32_t dotprod, sign; | ||
| 993 | 20 | int32_t delay[8] = { 0 }; | |
| 994 | 20 | uint32_t coeffs[8] = { 0 }; | |
| 995 | |||
| 996 |
2/2✓ Branch 0 taken 1469440 times.
✓ Branch 1 taken 20 times.
|
1469460 | for (i = 0; i < length; i++) { |
| 997 | 1469440 | dotprod = 0; | |
| 998 | 1469440 | sign = APESIGN(buffer[i]); | |
| 999 |
2/2✓ Branch 0 taken 11755520 times.
✓ Branch 1 taken 1469440 times.
|
13224960 | for (j = 7; j >= 0; j--) { |
| 1000 | 11755520 | dotprod += delay[j] * coeffs[j]; | |
| 1001 | 11755520 | coeffs[j] += ((delay[j] >> 31) | 1) * sign; | |
| 1002 | } | ||
| 1003 |
2/2✓ Branch 0 taken 10286080 times.
✓ Branch 1 taken 1469440 times.
|
11755520 | for (j = 7; j > 0; j--) |
| 1004 | 10286080 | delay[j] = delay[j - 1]; | |
| 1005 | 1469440 | delay[0] = buffer[i]; | |
| 1006 | 1469440 | buffer[i] -= (unsigned)(dotprod >> 9); | |
| 1007 | } | ||
| 1008 | 20 | } | |
| 1009 | |||
| 1010 | 46 | static void predictor_decode_stereo_3800(APEContext *ctx, int count) | |
| 1011 | { | ||
| 1012 | 46 | APEPredictor *p = &ctx->predictor; | |
| 1013 | 46 | int32_t *decoded0 = ctx->decoded[0]; | |
| 1014 | 46 | int32_t *decoded1 = ctx->decoded[1]; | |
| 1015 | 46 | int start = 4, shift = 10; | |
| 1016 | |||
| 1017 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
|
46 | if (ctx->compression_level == COMPRESSION_LEVEL_HIGH) { |
| 1018 | ✗ | start = 16; | |
| 1019 | ✗ | long_filter_high_3800(decoded0, 16, 9, count); | |
| 1020 | ✗ | long_filter_high_3800(decoded1, 16, 9, count); | |
| 1021 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 34 times.
|
46 | } else if (ctx->compression_level == COMPRESSION_LEVEL_EXTRA_HIGH) { |
| 1022 | 12 | int order = 128, shift2 = 11; | |
| 1023 | |||
| 1024 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 2 times.
|
12 | if (ctx->fileversion >= 3830) { |
| 1025 | 10 | order <<= 1; | |
| 1026 | 10 | shift++; | |
| 1027 | 10 | shift2++; | |
| 1028 | 10 | long_filter_ehigh_3830(decoded0 + order, count - order); | |
| 1029 | 10 | long_filter_ehigh_3830(decoded1 + order, count - order); | |
| 1030 | } | ||
| 1031 | 12 | start = order; | |
| 1032 | 12 | long_filter_high_3800(decoded0, order, shift2, count); | |
| 1033 | 12 | long_filter_high_3800(decoded1, order, shift2, count); | |
| 1034 | } | ||
| 1035 | |||
| 1036 |
2/2✓ Branch 0 taken 1456128 times.
✓ Branch 1 taken 46 times.
|
1456174 | while (count--) { |
| 1037 | 1456128 | int X = *decoded0, Y = *decoded1; | |
| 1038 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1456128 times.
|
1456128 | if (ctx->compression_level == COMPRESSION_LEVEL_FAST) { |
| 1039 | ✗ | *decoded0 = filter_fast_3320(p, Y, 0, YDELAYA); | |
| 1040 | ✗ | decoded0++; | |
| 1041 | ✗ | *decoded1 = filter_fast_3320(p, X, 1, XDELAYA); | |
| 1042 | ✗ | decoded1++; | |
| 1043 | } else { | ||
| 1044 | 1456128 | *decoded0 = filter_3800(p, Y, 0, YDELAYA, YDELAYB, | |
| 1045 | start, shift); | ||
| 1046 | 1456128 | decoded0++; | |
| 1047 | 1456128 | *decoded1 = filter_3800(p, X, 1, XDELAYA, XDELAYB, | |
| 1048 | start, shift); | ||
| 1049 | 1456128 | decoded1++; | |
| 1050 | } | ||
| 1051 | |||
| 1052 | /* Combined */ | ||
| 1053 | 1456128 | p->buf++; | |
| 1054 | 1456128 | p->sample_pos++; | |
| 1055 | |||
| 1056 | /* Have we filled the history buffer? */ | ||
| 1057 |
2/2✓ Branch 0 taken 2844 times.
✓ Branch 1 taken 1453284 times.
|
1456128 | if (p->buf == p->historybuffer + HISTORY_SIZE) { |
| 1058 | 2844 | memmove(p->historybuffer, p->buf, | |
| 1059 | PREDICTOR_SIZE * sizeof(*p->historybuffer)); | ||
| 1060 | 2844 | p->buf = p->historybuffer; | |
| 1061 | } | ||
| 1062 | } | ||
| 1063 | 46 | } | |
| 1064 | |||
| 1065 | ✗ | static void predictor_decode_mono_3800(APEContext *ctx, int count) | |
| 1066 | { | ||
| 1067 | ✗ | APEPredictor *p = &ctx->predictor; | |
| 1068 | ✗ | int32_t *decoded0 = ctx->decoded[0]; | |
| 1069 | ✗ | int start = 4, shift = 10; | |
| 1070 | |||
| 1071 | ✗ | if (ctx->compression_level == COMPRESSION_LEVEL_HIGH) { | |
| 1072 | ✗ | start = 16; | |
| 1073 | ✗ | long_filter_high_3800(decoded0, 16, 9, count); | |
| 1074 | ✗ | } else if (ctx->compression_level == COMPRESSION_LEVEL_EXTRA_HIGH) { | |
| 1075 | ✗ | int order = 128, shift2 = 11; | |
| 1076 | |||
| 1077 | ✗ | if (ctx->fileversion >= 3830) { | |
| 1078 | ✗ | order <<= 1; | |
| 1079 | ✗ | shift++; | |
| 1080 | ✗ | shift2++; | |
| 1081 | ✗ | long_filter_ehigh_3830(decoded0 + order, count - order); | |
| 1082 | } | ||
| 1083 | ✗ | start = order; | |
| 1084 | ✗ | long_filter_high_3800(decoded0, order, shift2, count); | |
| 1085 | } | ||
| 1086 | |||
| 1087 | ✗ | while (count--) { | |
| 1088 | ✗ | if (ctx->compression_level == COMPRESSION_LEVEL_FAST) { | |
| 1089 | ✗ | *decoded0 = filter_fast_3320(p, *decoded0, 0, YDELAYA); | |
| 1090 | ✗ | decoded0++; | |
| 1091 | } else { | ||
| 1092 | ✗ | *decoded0 = filter_3800(p, *decoded0, 0, YDELAYA, YDELAYB, | |
| 1093 | start, shift); | ||
| 1094 | ✗ | decoded0++; | |
| 1095 | } | ||
| 1096 | |||
| 1097 | /* Combined */ | ||
| 1098 | ✗ | p->buf++; | |
| 1099 | ✗ | p->sample_pos++; | |
| 1100 | |||
| 1101 | /* Have we filled the history buffer? */ | ||
| 1102 | ✗ | if (p->buf == p->historybuffer + HISTORY_SIZE) { | |
| 1103 | ✗ | memmove(p->historybuffer, p->buf, | |
| 1104 | PREDICTOR_SIZE * sizeof(*p->historybuffer)); | ||
| 1105 | ✗ | p->buf = p->historybuffer; | |
| 1106 | } | ||
| 1107 | } | ||
| 1108 | ✗ | } | |
| 1109 | |||
| 1110 | 331776 | static av_always_inline int predictor_update_3930(APEPredictor *p, | |
| 1111 | const int decoded, const int filter, | ||
| 1112 | const int delayA) | ||
| 1113 | { | ||
| 1114 | int32_t predictionA, sign; | ||
| 1115 | uint32_t d0, d1, d2, d3; | ||
| 1116 | |||
| 1117 | 331776 | p->buf[delayA] = p->lastA[filter]; | |
| 1118 | 331776 | d0 = p->buf[delayA ]; | |
| 1119 | 331776 | d1 = p->buf[delayA ] - (unsigned)p->buf[delayA - 1]; | |
| 1120 | 331776 | d2 = p->buf[delayA - 1] - (unsigned)p->buf[delayA - 2]; | |
| 1121 | 331776 | d3 = p->buf[delayA - 2] - (unsigned)p->buf[delayA - 3]; | |
| 1122 | |||
| 1123 | 331776 | predictionA = d0 * p->coeffsA[filter][0] + | |
| 1124 | 331776 | d1 * p->coeffsA[filter][1] + | |
| 1125 | 331776 | d2 * p->coeffsA[filter][2] + | |
| 1126 | 331776 | d3 * p->coeffsA[filter][3]; | |
| 1127 | |||
| 1128 | 331776 | p->lastA[filter] = decoded + (predictionA >> 9); | |
| 1129 | 331776 | p->filterA[filter] = p->lastA[filter] + ((int)(p->filterA[filter] * 31U) >> 5); | |
| 1130 | |||
| 1131 | 331776 | sign = APESIGN(decoded); | |
| 1132 | 331776 | p->coeffsA[filter][0] += (((int32_t)d0 < 0) * 2 - 1) * sign; | |
| 1133 | 331776 | p->coeffsA[filter][1] += (((int32_t)d1 < 0) * 2 - 1) * sign; | |
| 1134 | 331776 | p->coeffsA[filter][2] += (((int32_t)d2 < 0) * 2 - 1) * sign; | |
| 1135 | 331776 | p->coeffsA[filter][3] += (((int32_t)d3 < 0) * 2 - 1) * sign; | |
| 1136 | |||
| 1137 | 331776 | return p->filterA[filter]; | |
| 1138 | } | ||
| 1139 | |||
| 1140 | 36 | static void predictor_decode_stereo_3930(APEContext *ctx, int count) | |
| 1141 | { | ||
| 1142 | 36 | APEPredictor *p = &ctx->predictor; | |
| 1143 | 36 | int32_t *decoded0 = ctx->decoded[0]; | |
| 1144 | 36 | int32_t *decoded1 = ctx->decoded[1]; | |
| 1145 | |||
| 1146 | 36 | ape_apply_filters(ctx, ctx->decoded[0], ctx->decoded[1], count); | |
| 1147 | |||
| 1148 |
2/2✓ Branch 0 taken 165888 times.
✓ Branch 1 taken 36 times.
|
165924 | while (count--) { |
| 1149 | /* Predictor Y */ | ||
| 1150 | 165888 | int Y = *decoded1, X = *decoded0; | |
| 1151 | 165888 | *decoded0 = predictor_update_3930(p, Y, 0, YDELAYA); | |
| 1152 | 165888 | decoded0++; | |
| 1153 | 165888 | *decoded1 = predictor_update_3930(p, X, 1, XDELAYA); | |
| 1154 | 165888 | decoded1++; | |
| 1155 | |||
| 1156 | /* Combined */ | ||
| 1157 | 165888 | p->buf++; | |
| 1158 | |||
| 1159 | /* Have we filled the history buffer? */ | ||
| 1160 |
2/2✓ Branch 0 taken 324 times.
✓ Branch 1 taken 165564 times.
|
165888 | if (p->buf == p->historybuffer + HISTORY_SIZE) { |
| 1161 | 324 | memmove(p->historybuffer, p->buf, | |
| 1162 | PREDICTOR_SIZE * sizeof(*p->historybuffer)); | ||
| 1163 | 324 | p->buf = p->historybuffer; | |
| 1164 | } | ||
| 1165 | } | ||
| 1166 | 36 | } | |
| 1167 | |||
| 1168 | ✗ | static void predictor_decode_mono_3930(APEContext *ctx, int count) | |
| 1169 | { | ||
| 1170 | ✗ | APEPredictor *p = &ctx->predictor; | |
| 1171 | ✗ | int32_t *decoded0 = ctx->decoded[0]; | |
| 1172 | |||
| 1173 | ✗ | ape_apply_filters(ctx, ctx->decoded[0], NULL, count); | |
| 1174 | |||
| 1175 | ✗ | while (count--) { | |
| 1176 | ✗ | *decoded0 = predictor_update_3930(p, *decoded0, 0, YDELAYA); | |
| 1177 | ✗ | decoded0++; | |
| 1178 | |||
| 1179 | ✗ | p->buf++; | |
| 1180 | |||
| 1181 | /* Have we filled the history buffer? */ | ||
| 1182 | ✗ | if (p->buf == p->historybuffer + HISTORY_SIZE) { | |
| 1183 | ✗ | memmove(p->historybuffer, p->buf, | |
| 1184 | PREDICTOR_SIZE * sizeof(*p->historybuffer)); | ||
| 1185 | ✗ | p->buf = p->historybuffer; | |
| 1186 | } | ||
| 1187 | } | ||
| 1188 | ✗ | } | |
| 1189 | |||
| 1190 | 893958 | static av_always_inline int predictor_update_filter(APEPredictor64 *p, | |
| 1191 | const int decoded, const int filter, | ||
| 1192 | const int delayA, const int delayB, | ||
| 1193 | const int adaptA, const int adaptB, | ||
| 1194 | int interim_mode) | ||
| 1195 | { | ||
| 1196 | int64_t predictionA, predictionB; | ||
| 1197 | int32_t sign; | ||
| 1198 | |||
| 1199 | 893958 | p->buf[delayA] = p->lastA[filter]; | |
| 1200 | 893958 | p->buf[adaptA] = APESIGN(p->buf[delayA]); | |
| 1201 | 893958 | p->buf[delayA - 1] = p->buf[delayA] - (uint64_t)p->buf[delayA - 1]; | |
| 1202 | 893958 | p->buf[adaptA - 1] = APESIGN(p->buf[delayA - 1]); | |
| 1203 | |||
| 1204 | 893958 | predictionA = p->buf[delayA ] * p->coeffsA[filter][0] + | |
| 1205 | 893958 | p->buf[delayA - 1] * p->coeffsA[filter][1] + | |
| 1206 | 893958 | p->buf[delayA - 2] * p->coeffsA[filter][2] + | |
| 1207 | 893958 | p->buf[delayA - 3] * p->coeffsA[filter][3]; | |
| 1208 | |||
| 1209 | /* Apply a scaled first-order filter compression */ | ||
| 1210 | 893958 | p->buf[delayB] = p->filterA[filter ^ 1] - ((int64_t)(p->filterB[filter] * 31ULL) >> 5); | |
| 1211 | 893958 | p->buf[adaptB] = APESIGN(p->buf[delayB]); | |
| 1212 | 893958 | p->buf[delayB - 1] = p->buf[delayB] - (uint64_t)p->buf[delayB - 1]; | |
| 1213 | 893958 | p->buf[adaptB - 1] = APESIGN(p->buf[delayB - 1]); | |
| 1214 | 893958 | p->filterB[filter] = p->filterA[filter ^ 1]; | |
| 1215 | |||
| 1216 | 893958 | predictionB = p->buf[delayB ] * p->coeffsB[filter][0] + | |
| 1217 | 893958 | p->buf[delayB - 1] * p->coeffsB[filter][1] + | |
| 1218 | 893958 | p->buf[delayB - 2] * p->coeffsB[filter][2] + | |
| 1219 | 893958 | p->buf[delayB - 3] * p->coeffsB[filter][3] + | |
| 1220 | 893958 | p->buf[delayB - 4] * p->coeffsB[filter][4]; | |
| 1221 | |||
| 1222 |
2/2✓ Branch 0 taken 866310 times.
✓ Branch 1 taken 27648 times.
|
893958 | if (interim_mode < 1) { |
| 1223 | 866310 | predictionA = (int32_t)predictionA; | |
| 1224 | 866310 | predictionB = (int32_t)predictionB; | |
| 1225 | 866310 | p->lastA[filter] = (int32_t)(decoded + (unsigned)((int32_t)(predictionA + (predictionB >> 1)) >> 10)); | |
| 1226 | } else { | ||
| 1227 | 27648 | p->lastA[filter] = decoded + ((int64_t)((uint64_t)predictionA + (predictionB >> 1)) >> 10); | |
| 1228 | } | ||
| 1229 | 893958 | p->filterA[filter] = p->lastA[filter] + ((int64_t)(p->filterA[filter] * 31ULL) >> 5); | |
| 1230 | |||
| 1231 | 893958 | sign = APESIGN(decoded); | |
| 1232 | 893958 | p->coeffsA[filter][0] += p->buf[adaptA ] * sign; | |
| 1233 | 893958 | p->coeffsA[filter][1] += p->buf[adaptA - 1] * sign; | |
| 1234 | 893958 | p->coeffsA[filter][2] += p->buf[adaptA - 2] * sign; | |
| 1235 | 893958 | p->coeffsA[filter][3] += p->buf[adaptA - 3] * sign; | |
| 1236 | 893958 | p->coeffsB[filter][0] += p->buf[adaptB ] * sign; | |
| 1237 | 893958 | p->coeffsB[filter][1] += p->buf[adaptB - 1] * sign; | |
| 1238 | 893958 | p->coeffsB[filter][2] += p->buf[adaptB - 2] * sign; | |
| 1239 | 893958 | p->coeffsB[filter][3] += p->buf[adaptB - 3] * sign; | |
| 1240 | 893958 | p->coeffsB[filter][4] += p->buf[adaptB - 4] * sign; | |
| 1241 | |||
| 1242 | 893958 | return p->filterA[filter]; | |
| 1243 | } | ||
| 1244 | |||
| 1245 | 97 | static void predictor_decode_stereo_3950(APEContext *ctx, int count) | |
| 1246 | { | ||
| 1247 | 97 | APEPredictor64 *p_default = &ctx->predictor64; | |
| 1248 | APEPredictor64 p_interim; | ||
| 1249 | 97 | int lcount = count; | |
| 1250 | 97 | int num_passes = 1; | |
| 1251 | |||
| 1252 | 97 | ape_apply_filters(ctx, ctx->decoded[0], ctx->decoded[1], count); | |
| 1253 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 96 times.
|
97 | if (ctx->interim_mode == -1) { |
| 1254 | 1 | p_interim = *p_default; | |
| 1255 | 1 | num_passes ++; | |
| 1256 | 1 | memcpy(ctx->interim[0], ctx->decoded[0], sizeof(*ctx->interim[0])*count); | |
| 1257 | 1 | memcpy(ctx->interim[1], ctx->decoded[1], sizeof(*ctx->interim[1])*count); | |
| 1258 | } | ||
| 1259 | |||
| 1260 |
2/2✓ Branch 0 taken 98 times.
✓ Branch 1 taken 97 times.
|
195 | for (int pass = 0; pass < num_passes; pass++) { |
| 1261 | int32_t *decoded0, *decoded1; | ||
| 1262 |
3/4✓ Branch 0 taken 95 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 95 times.
|
98 | int interim_mode = ctx->interim_mode > 0 || pass; |
| 1263 | APEPredictor64 *p; | ||
| 1264 | |||
| 1265 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 97 times.
|
98 | if (pass) { |
| 1266 | 1 | p = &p_interim; | |
| 1267 | 1 | decoded0 = ctx->interim[0]; | |
| 1268 | 1 | decoded1 = ctx->interim[1]; | |
| 1269 | } else { | ||
| 1270 | 97 | p = p_default; | |
| 1271 | 97 | decoded0 = ctx->decoded[0]; | |
| 1272 | 97 | decoded1 = ctx->decoded[1]; | |
| 1273 | } | ||
| 1274 | 98 | p->buf = p->historybuffer; | |
| 1275 | |||
| 1276 | 98 | count = lcount; | |
| 1277 |
2/2✓ Branch 0 taken 446979 times.
✓ Branch 1 taken 97 times.
|
447076 | while (count--) { |
| 1278 | /* Predictor Y */ | ||
| 1279 | 446979 | int32_t a0 = predictor_update_filter(p, *decoded0, 0, YDELAYA, YDELAYB, | |
| 1280 | YADAPTCOEFFSA, YADAPTCOEFFSB, | ||
| 1281 | interim_mode); | ||
| 1282 | 446979 | int32_t a1 = predictor_update_filter(p, *decoded1, 1, XDELAYA, XDELAYB, | |
| 1283 | XADAPTCOEFFSA, XADAPTCOEFFSB, | ||
| 1284 | interim_mode); | ||
| 1285 | 446979 | *decoded0++ = a0; | |
| 1286 | 446979 | *decoded1++ = a1; | |
| 1287 |
2/2✓ Branch 0 taken 4611 times.
✓ Branch 1 taken 442368 times.
|
446979 | if (num_passes > 1) { |
| 1288 | 4611 | int32_t left = a1 - (unsigned)(a0 / 2); | |
| 1289 | 4611 | int32_t right = left + (unsigned)a0; | |
| 1290 | |||
| 1291 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4610 times.
|
4611 | if (FFMIN(FFNABS(left), FFNABS(right)) < -(1<<23)) { |
| 1292 | 1 | ctx->interim_mode = !interim_mode; | |
| 1293 | 1 | av_log(ctx->avctx, AV_LOG_VERBOSE, "Interim mode: %d\n", ctx->interim_mode); | |
| 1294 | 1 | break; | |
| 1295 | } | ||
| 1296 | } | ||
| 1297 | |||
| 1298 | /* Combined */ | ||
| 1299 | 446978 | p->buf++; | |
| 1300 | |||
| 1301 | /* Have we filled the history buffer? */ | ||
| 1302 |
2/2✓ Branch 0 taken 873 times.
✓ Branch 1 taken 446105 times.
|
446978 | if (p->buf == p->historybuffer + HISTORY_SIZE) { |
| 1303 | 873 | memmove(p->historybuffer, p->buf, | |
| 1304 | PREDICTOR_SIZE * sizeof(*p->historybuffer)); | ||
| 1305 | 873 | p->buf = p->historybuffer; | |
| 1306 | } | ||
| 1307 | } | ||
| 1308 | } | ||
| 1309 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 96 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
97 | if (num_passes > 1 && ctx->interim_mode > 0) { |
| 1310 | 1 | memcpy(ctx->decoded[0], ctx->interim[0], sizeof(*ctx->interim[0])*lcount); | |
| 1311 | 1 | memcpy(ctx->decoded[1], ctx->interim[1], sizeof(*ctx->interim[1])*lcount); | |
| 1312 | 1 | *p_default = p_interim; | |
| 1313 | 1 | p_default->buf = p_default->historybuffer; | |
| 1314 | } | ||
| 1315 | 97 | } | |
| 1316 | |||
| 1317 | ✗ | static void predictor_decode_mono_3950(APEContext *ctx, int count) | |
| 1318 | { | ||
| 1319 | ✗ | APEPredictor64 *p = &ctx->predictor64; | |
| 1320 | ✗ | int32_t *decoded0 = ctx->decoded[0]; | |
| 1321 | int32_t predictionA, currentA, A, sign; | ||
| 1322 | |||
| 1323 | ✗ | ape_apply_filters(ctx, ctx->decoded[0], NULL, count); | |
| 1324 | |||
| 1325 | ✗ | currentA = p->lastA[0]; | |
| 1326 | |||
| 1327 | ✗ | while (count--) { | |
| 1328 | ✗ | A = *decoded0; | |
| 1329 | |||
| 1330 | ✗ | p->buf[YDELAYA] = currentA; | |
| 1331 | ✗ | p->buf[YDELAYA - 1] = p->buf[YDELAYA] - (uint64_t)p->buf[YDELAYA - 1]; | |
| 1332 | |||
| 1333 | ✗ | predictionA = p->buf[YDELAYA ] * p->coeffsA[0][0] + | |
| 1334 | ✗ | p->buf[YDELAYA - 1] * p->coeffsA[0][1] + | |
| 1335 | ✗ | p->buf[YDELAYA - 2] * p->coeffsA[0][2] + | |
| 1336 | ✗ | p->buf[YDELAYA - 3] * p->coeffsA[0][3]; | |
| 1337 | |||
| 1338 | ✗ | currentA = A + (uint64_t)(predictionA >> 10); | |
| 1339 | |||
| 1340 | ✗ | p->buf[YADAPTCOEFFSA] = APESIGN(p->buf[YDELAYA ]); | |
| 1341 | ✗ | p->buf[YADAPTCOEFFSA - 1] = APESIGN(p->buf[YDELAYA - 1]); | |
| 1342 | |||
| 1343 | ✗ | sign = APESIGN(A); | |
| 1344 | ✗ | p->coeffsA[0][0] += p->buf[YADAPTCOEFFSA ] * sign; | |
| 1345 | ✗ | p->coeffsA[0][1] += p->buf[YADAPTCOEFFSA - 1] * sign; | |
| 1346 | ✗ | p->coeffsA[0][2] += p->buf[YADAPTCOEFFSA - 2] * sign; | |
| 1347 | ✗ | p->coeffsA[0][3] += p->buf[YADAPTCOEFFSA - 3] * sign; | |
| 1348 | |||
| 1349 | ✗ | p->buf++; | |
| 1350 | |||
| 1351 | /* Have we filled the history buffer? */ | ||
| 1352 | ✗ | if (p->buf == p->historybuffer + HISTORY_SIZE) { | |
| 1353 | ✗ | memmove(p->historybuffer, p->buf, | |
| 1354 | PREDICTOR_SIZE * sizeof(*p->historybuffer)); | ||
| 1355 | ✗ | p->buf = p->historybuffer; | |
| 1356 | } | ||
| 1357 | |||
| 1358 | ✗ | p->filterA[0] = currentA + (uint64_t)((int64_t)(p->filterA[0] * 31U) >> 5); | |
| 1359 | ✗ | *(decoded0++) = p->filterA[0]; | |
| 1360 | } | ||
| 1361 | |||
| 1362 | ✗ | p->lastA[0] = currentA; | |
| 1363 | ✗ | } | |
| 1364 | |||
| 1365 | 158 | static void do_init_filter(APEFilter *f, int16_t *buf, int order) | |
| 1366 | { | ||
| 1367 | 158 | f->coeffs = buf; | |
| 1368 | 158 | f->historybuffer = buf + order; | |
| 1369 | 158 | f->delay = f->historybuffer + order * 2; | |
| 1370 | 158 | f->adaptcoeffs = f->historybuffer + order; | |
| 1371 | |||
| 1372 | 158 | memset(f->historybuffer, 0, (order * 2) * sizeof(*f->historybuffer)); | |
| 1373 | 158 | memset(f->coeffs, 0, order * sizeof(*f->coeffs)); | |
| 1374 | 158 | f->avg = 0; | |
| 1375 | 158 | } | |
| 1376 | |||
| 1377 | 79 | static void init_filter(APEContext *ctx, APEFilter *f, int16_t *buf, int order) | |
| 1378 | { | ||
| 1379 | 79 | do_init_filter(&f[0], buf, order); | |
| 1380 | 79 | do_init_filter(&f[1], buf + order * 3 + HISTORY_SIZE, order); | |
| 1381 | 79 | } | |
| 1382 | |||
| 1383 | 302 | static void do_apply_filter(APEContext *ctx, int version, APEFilter *f, | |
| 1384 | int32_t *data, int count, int order, int fracbits) | ||
| 1385 | { | ||
| 1386 | int res; | ||
| 1387 | unsigned absres; | ||
| 1388 | |||
| 1389 |
2/2✓ Branch 0 taken 1391616 times.
✓ Branch 1 taken 302 times.
|
1391918 | while (count--) { |
| 1390 | /* round fixedpoint scalar product */ | ||
| 1391 | 1391616 | res = ctx->adsp.scalarproduct_and_madd_int16(f->coeffs, | |
| 1392 | 1391616 | f->delay - order, | |
| 1393 | 1391616 | f->adaptcoeffs - order, | |
| 1394 | order, APESIGN(*data)); | ||
| 1395 | 1391616 | res = (int64_t)(res + (1LL << (fracbits - 1))) >> fracbits; | |
| 1396 | 1391616 | res += (unsigned)*data; | |
| 1397 | 1391616 | *data++ = res; | |
| 1398 | |||
| 1399 | /* Update the output history */ | ||
| 1400 | 1391616 | *f->delay++ = av_clip_int16(res); | |
| 1401 | |||
| 1402 |
2/2✓ Branch 0 taken 497664 times.
✓ Branch 1 taken 893952 times.
|
1391616 | if (version < 3980) { |
| 1403 | /* Version ??? to < 3.98 files (untested) */ | ||
| 1404 |
2/2✓ Branch 0 taken 492164 times.
✓ Branch 1 taken 5500 times.
|
497664 | f->adaptcoeffs[0] = (res == 0) ? 0 : ((res >> 28) & 8) - 4; |
| 1405 | 497664 | f->adaptcoeffs[-4] >>= 1; | |
| 1406 | 497664 | f->adaptcoeffs[-8] >>= 1; | |
| 1407 | } else { | ||
| 1408 | /* Version 3.98 and later files */ | ||
| 1409 | |||
| 1410 | /* Update the adaption coefficients */ | ||
| 1411 |
2/2✓ Branch 0 taken 444730 times.
✓ Branch 1 taken 449222 times.
|
893952 | absres = FFABSU(res); |
| 1412 |
2/2✓ Branch 0 taken 890829 times.
✓ Branch 1 taken 3123 times.
|
893952 | if (absres) |
| 1413 | 890829 | *f->adaptcoeffs = APESIGN(res) * | |
| 1414 | 890829 | (8 << ((absres > f->avg * 3LL) + (absres > (f->avg + f->avg / 3)))); | |
| 1415 | /* equivalent to the following code | ||
| 1416 | if (absres <= f->avg * 4 / 3) | ||
| 1417 | *f->adaptcoeffs = APESIGN(res) * 8; | ||
| 1418 | else if (absres <= f->avg * 3) | ||
| 1419 | *f->adaptcoeffs = APESIGN(res) * 16; | ||
| 1420 | else | ||
| 1421 | *f->adaptcoeffs = APESIGN(res) * 32; | ||
| 1422 | */ | ||
| 1423 | else | ||
| 1424 | 3123 | *f->adaptcoeffs = 0; | |
| 1425 | |||
| 1426 | 893952 | f->avg += (int)(absres - (unsigned)f->avg) / 16; | |
| 1427 | |||
| 1428 | 893952 | f->adaptcoeffs[-1] >>= 1; | |
| 1429 | 893952 | f->adaptcoeffs[-2] >>= 1; | |
| 1430 | 893952 | f->adaptcoeffs[-8] >>= 1; | |
| 1431 | } | ||
| 1432 | |||
| 1433 | 1391616 | f->adaptcoeffs++; | |
| 1434 | |||
| 1435 | /* Have we filled the history buffer? */ | ||
| 1436 |
2/2✓ Branch 0 taken 2718 times.
✓ Branch 1 taken 1388898 times.
|
1391616 | if (f->delay == f->historybuffer + HISTORY_SIZE + (order * 2)) { |
| 1437 | 2718 | memmove(f->historybuffer, f->delay - (order * 2), | |
| 1438 | 2718 | (order * 2) * sizeof(*f->historybuffer)); | |
| 1439 | 2718 | f->delay = f->historybuffer + order * 2; | |
| 1440 | 2718 | f->adaptcoeffs = f->historybuffer + order; | |
| 1441 | } | ||
| 1442 | } | ||
| 1443 | 302 | } | |
| 1444 | |||
| 1445 | 151 | static void apply_filter(APEContext *ctx, APEFilter *f, | |
| 1446 | int32_t *data0, int32_t *data1, | ||
| 1447 | int count, int order, int fracbits) | ||
| 1448 | { | ||
| 1449 | 151 | do_apply_filter(ctx, ctx->fileversion, &f[0], data0, count, order, fracbits); | |
| 1450 |
1/2✓ Branch 0 taken 151 times.
✗ Branch 1 not taken.
|
151 | if (data1) |
| 1451 | 151 | do_apply_filter(ctx, ctx->fileversion, &f[1], data1, count, order, fracbits); | |
| 1452 | 151 | } | |
| 1453 | |||
| 1454 | 133 | static void ape_apply_filters(APEContext *ctx, int32_t *decoded0, | |
| 1455 | int32_t *decoded1, int count) | ||
| 1456 | { | ||
| 1457 | int i; | ||
| 1458 | |||
| 1459 |
1/2✓ Branch 0 taken 284 times.
✗ Branch 1 not taken.
|
284 | for (i = 0; i < APE_FILTER_LEVELS; i++) { |
| 1460 |
2/2✓ Branch 0 taken 133 times.
✓ Branch 1 taken 151 times.
|
284 | if (!ape_filter_orders[ctx->fset][i]) |
| 1461 | 133 | break; | |
| 1462 | 151 | apply_filter(ctx, ctx->filters[i], decoded0, decoded1, count, | |
| 1463 | 151 | ape_filter_orders[ctx->fset][i], | |
| 1464 | 151 | ape_filter_fracbits[ctx->fset][i]); | |
| 1465 | } | ||
| 1466 | 133 | } | |
| 1467 | |||
| 1468 | 62 | static int init_frame_decoder(APEContext *ctx) | |
| 1469 | { | ||
| 1470 | int i, ret; | ||
| 1471 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 62 times.
|
62 | if ((ret = init_entropy_decoder(ctx)) < 0) |
| 1472 | ✗ | return ret; | |
| 1473 | 62 | init_predictor_decoder(ctx); | |
| 1474 | |||
| 1475 |
1/2✓ Branch 0 taken 141 times.
✗ Branch 1 not taken.
|
141 | for (i = 0; i < APE_FILTER_LEVELS; i++) { |
| 1476 |
2/2✓ Branch 0 taken 62 times.
✓ Branch 1 taken 79 times.
|
141 | if (!ape_filter_orders[ctx->fset][i]) |
| 1477 | 62 | break; | |
| 1478 | 79 | init_filter(ctx, ctx->filters[i], ctx->filterbuf[i], | |
| 1479 | 79 | ape_filter_orders[ctx->fset][i]); | |
| 1480 | } | ||
| 1481 | 62 | return 0; | |
| 1482 | } | ||
| 1483 | |||
| 1484 | ✗ | static void ape_unpack_mono(APEContext *ctx, int count) | |
| 1485 | { | ||
| 1486 | ✗ | if (ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) { | |
| 1487 | /* We are pure silence, so we're done. */ | ||
| 1488 | ✗ | av_log(ctx->avctx, AV_LOG_DEBUG, "pure silence mono\n"); | |
| 1489 | ✗ | return; | |
| 1490 | } | ||
| 1491 | |||
| 1492 | ✗ | ctx->entropy_decode_mono(ctx, count); | |
| 1493 | ✗ | if (ctx->error) | |
| 1494 | ✗ | return; | |
| 1495 | |||
| 1496 | /* Now apply the predictor decoding */ | ||
| 1497 | ✗ | ctx->predictor_decode_mono(ctx, count); | |
| 1498 | |||
| 1499 | /* Pseudo-stereo - just copy left channel to right channel */ | ||
| 1500 | ✗ | if (ctx->channels == 2) { | |
| 1501 | ✗ | memcpy(ctx->decoded[1], ctx->decoded[0], count * sizeof(*ctx->decoded[1])); | |
| 1502 | } | ||
| 1503 | } | ||
| 1504 | |||
| 1505 | 186 | static void ape_unpack_stereo(APEContext *ctx, int count) | |
| 1506 | { | ||
| 1507 | unsigned left, right; | ||
| 1508 | 186 | int32_t *decoded0 = ctx->decoded[0]; | |
| 1509 | 186 | int32_t *decoded1 = ctx->decoded[1]; | |
| 1510 | |||
| 1511 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 186 times.
|
186 | if ((ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) == APE_FRAMECODE_STEREO_SILENCE) { |
| 1512 | /* We are pure silence, so we're done. */ | ||
| 1513 | ✗ | av_log(ctx->avctx, AV_LOG_DEBUG, "pure silence stereo\n"); | |
| 1514 | ✗ | return; | |
| 1515 | } | ||
| 1516 | |||
| 1517 | 186 | ctx->entropy_decode_stereo(ctx, count); | |
| 1518 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 179 times.
|
186 | if (ctx->error) |
| 1519 | 7 | return; | |
| 1520 | |||
| 1521 | /* Now apply the predictor decoding */ | ||
| 1522 | 179 | ctx->predictor_decode_stereo(ctx, count); | |
| 1523 | |||
| 1524 | /* Decorrelate and scale to output depth */ | ||
| 1525 |
2/2✓ Branch 0 taken 2068992 times.
✓ Branch 1 taken 179 times.
|
2069171 | while (count--) { |
| 1526 | 2068992 | left = *decoded1 - (unsigned)(*decoded0 / 2); | |
| 1527 | 2068992 | right = left + *decoded0; | |
| 1528 | |||
| 1529 | 2068992 | *(decoded0++) = left; | |
| 1530 | 2068992 | *(decoded1++) = right; | |
| 1531 | } | ||
| 1532 | } | ||
| 1533 | |||
| 1534 | 193 | static int ape_decode_frame(AVCodecContext *avctx, AVFrame *frame, | |
| 1535 | int *got_frame_ptr, AVPacket *avpkt) | ||
| 1536 | { | ||
| 1537 | 193 | const uint8_t *buf = avpkt->data; | |
| 1538 | 193 | APEContext *s = avctx->priv_data; | |
| 1539 | uint8_t *sample8; | ||
| 1540 | int16_t *sample16; | ||
| 1541 | int32_t *sample24; | ||
| 1542 | int i, ch, ret; | ||
| 1543 | int blockstodecode; | ||
| 1544 | uint64_t decoded_buffer_size; | ||
| 1545 | |||
| 1546 | /* this should never be negative, but bad things will happen if it is, so | ||
| 1547 | check it just to make sure. */ | ||
| 1548 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 193 times.
|
193 | av_assert0(s->samples >= 0); |
| 1549 | |||
| 1550 |
2/2✓ Branch 0 taken 69 times.
✓ Branch 1 taken 124 times.
|
193 | if(!s->samples){ |
| 1551 | uint32_t nblocks, offset; | ||
| 1552 | int buf_size; | ||
| 1553 | |||
| 1554 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 62 times.
|
69 | if (!avpkt->size) { |
| 1555 | 7 | *got_frame_ptr = 0; | |
| 1556 | 7 | return 0; | |
| 1557 | } | ||
| 1558 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
|
62 | if (avpkt->size < 8) { |
| 1559 | ✗ | av_log(avctx, AV_LOG_ERROR, "Packet is too small\n"); | |
| 1560 | ✗ | return AVERROR_INVALIDDATA; | |
| 1561 | } | ||
| 1562 | 62 | buf_size = avpkt->size & ~3; | |
| 1563 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 61 times.
|
62 | if (buf_size != avpkt->size) { |
| 1564 | 1 | av_log(avctx, AV_LOG_WARNING, "packet size is not a multiple of 4. " | |
| 1565 | "extra bytes at the end will be skipped.\n"); | ||
| 1566 | } | ||
| 1567 |
2/2✓ Branch 0 taken 55 times.
✓ Branch 1 taken 7 times.
|
62 | if (s->fileversion < 3950) // previous versions overread two bytes |
| 1568 | 55 | buf_size += 2; | |
| 1569 | 62 | av_fast_padded_malloc(&s->data, &s->data_size, buf_size); | |
| 1570 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 62 times.
|
62 | if (!s->data) |
| 1571 | ✗ | return AVERROR(ENOMEM); | |
| 1572 | 62 | s->bdsp.bswap_buf((uint32_t *) s->data, (const uint32_t *) buf, | |
| 1573 | buf_size >> 2); | ||
| 1574 | 62 | memset(s->data + (buf_size & ~3), 0, buf_size & 3); | |
| 1575 | 62 | s->ptr = s->data; | |
| 1576 | 62 | s->data_end = s->data + buf_size; | |
| 1577 | |||
| 1578 | 62 | nblocks = bytestream_get_be32(&s->ptr); | |
| 1579 | 62 | offset = bytestream_get_be32(&s->ptr); | |
| 1580 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 39 times.
|
62 | if (s->fileversion >= 3900) { |
| 1581 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
|
23 | if (offset > 3) { |
| 1582 | ✗ | av_log(avctx, AV_LOG_ERROR, "Incorrect offset passed\n"); | |
| 1583 | ✗ | av_freep(&s->data); | |
| 1584 | ✗ | s->data_size = 0; | |
| 1585 | ✗ | return AVERROR_INVALIDDATA; | |
| 1586 | } | ||
| 1587 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
|
23 | if (s->data_end - s->ptr < offset) { |
| 1588 | ✗ | av_log(avctx, AV_LOG_ERROR, "Packet is too small\n"); | |
| 1589 | ✗ | return AVERROR_INVALIDDATA; | |
| 1590 | } | ||
| 1591 | 23 | s->ptr += offset; | |
| 1592 | } else { | ||
| 1593 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
|
39 | if ((ret = init_get_bits8(&s->gb, s->ptr, s->data_end - s->ptr)) < 0) |
| 1594 | ✗ | return ret; | |
| 1595 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 13 times.
|
39 | if (s->fileversion > 3800) |
| 1596 | 26 | skip_bits_long(&s->gb, offset * 8); | |
| 1597 | else | ||
| 1598 | 13 | skip_bits_long(&s->gb, offset); | |
| 1599 | } | ||
| 1600 | |||
| 1601 |
2/4✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 62 times.
|
62 | if (!nblocks || nblocks > INT_MAX / 2 / sizeof(*s->decoded_buffer) - 8) { |
| 1602 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid sample count: %"PRIu32".\n", | |
| 1603 | nblocks); | ||
| 1604 | ✗ | return AVERROR_INVALIDDATA; | |
| 1605 | } | ||
| 1606 | |||
| 1607 | /* Initialize the frame decoder */ | ||
| 1608 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 62 times.
|
62 | if (init_frame_decoder(s) < 0) { |
| 1609 | ✗ | av_log(avctx, AV_LOG_ERROR, "Error reading frame header\n"); | |
| 1610 | ✗ | return AVERROR_INVALIDDATA; | |
| 1611 | } | ||
| 1612 | 62 | s->samples = nblocks; | |
| 1613 | } | ||
| 1614 | |||
| 1615 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 186 times.
|
186 | if (!s->data) { |
| 1616 | ✗ | *got_frame_ptr = 0; | |
| 1617 | ✗ | return avpkt->size; | |
| 1618 | } | ||
| 1619 | |||
| 1620 | 186 | blockstodecode = FFMIN(s->blocks_per_loop, s->samples); | |
| 1621 | // for old files coefficients were not interleaved, | ||
| 1622 | // so we need to decode all of them at once | ||
| 1623 |
2/2✓ Branch 0 taken 51 times.
✓ Branch 1 taken 135 times.
|
186 | if (s->fileversion < 3930) |
| 1624 | 51 | blockstodecode = s->samples; | |
| 1625 | |||
| 1626 | /* reallocate decoded sample buffer if needed */ | ||
| 1627 | 186 | decoded_buffer_size = 2LL * FFALIGN(blockstodecode, 8) * sizeof(*s->decoded_buffer); | |
| 1628 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 186 times.
|
186 | av_assert0(decoded_buffer_size <= INT_MAX); |
| 1629 | |||
| 1630 | /* get output buffer */ | ||
| 1631 | 186 | frame->nb_samples = blockstodecode; | |
| 1632 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 186 times.
|
186 | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) { |
| 1633 | ✗ | s->samples=0; | |
| 1634 | ✗ | return ret; | |
| 1635 | } | ||
| 1636 | |||
| 1637 | 186 | av_fast_malloc(&s->decoded_buffer, &s->decoded_size, decoded_buffer_size); | |
| 1638 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 186 times.
|
186 | if (!s->decoded_buffer) |
| 1639 | ✗ | return AVERROR(ENOMEM); | |
| 1640 | 186 | memset(s->decoded_buffer, 0, decoded_buffer_size); | |
| 1641 | 186 | s->decoded[0] = s->decoded_buffer; | |
| 1642 | 186 | s->decoded[1] = s->decoded_buffer + FFALIGN(blockstodecode, 8); | |
| 1643 | |||
| 1644 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 185 times.
|
186 | if (s->interim_mode < 0) { |
| 1645 | 1 | av_fast_malloc(&s->interim_buffer, &s->interim_size, decoded_buffer_size); | |
| 1646 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!s->interim_buffer) |
| 1647 | ✗ | return AVERROR(ENOMEM); | |
| 1648 | 1 | memset(s->interim_buffer, 0, decoded_buffer_size); | |
| 1649 | 1 | s->interim[0] = s->interim_buffer; | |
| 1650 | 1 | s->interim[1] = s->interim_buffer + FFALIGN(blockstodecode, 8); | |
| 1651 | } else { | ||
| 1652 | 185 | av_freep(&s->interim_buffer); | |
| 1653 | 185 | s->interim_size = 0; | |
| 1654 | 185 | memset(s->interim, 0, sizeof(s->interim)); | |
| 1655 | } | ||
| 1656 | |||
| 1657 | 186 | s->error=0; | |
| 1658 | |||
| 1659 |
2/4✓ Branch 0 taken 186 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 186 times.
|
186 | if ((s->channels == 1) || (s->frameflags & APE_FRAMECODE_PSEUDO_STEREO)) |
| 1660 | ✗ | ape_unpack_mono(s, blockstodecode); | |
| 1661 | else | ||
| 1662 | 186 | ape_unpack_stereo(s, blockstodecode); | |
| 1663 | |||
| 1664 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 179 times.
|
186 | if (s->error) { |
| 1665 | 7 | s->samples=0; | |
| 1666 | 7 | av_log(avctx, AV_LOG_ERROR, "Error decoding frame\n"); | |
| 1667 | 7 | return AVERROR_INVALIDDATA; | |
| 1668 | } | ||
| 1669 | |||
| 1670 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 176 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
179 | switch (s->bps) { |
| 1671 | ✗ | case 8: | |
| 1672 | ✗ | for (ch = 0; ch < s->channels; ch++) { | |
| 1673 | ✗ | sample8 = (uint8_t *)frame->data[ch]; | |
| 1674 | ✗ | for (i = 0; i < blockstodecode; i++) | |
| 1675 | ✗ | *sample8++ = (s->decoded[ch][i] + 0x80U) & 0xff; | |
| 1676 | } | ||
| 1677 | ✗ | break; | |
| 1678 | 176 | case 16: | |
| 1679 |
2/2✓ Branch 0 taken 352 times.
✓ Branch 1 taken 176 times.
|
528 | for (ch = 0; ch < s->channels; ch++) { |
| 1680 | 352 | sample16 = (int16_t *)frame->data[ch]; | |
| 1681 |
2/2✓ Branch 0 taken 4110336 times.
✓ Branch 1 taken 352 times.
|
4110688 | for (i = 0; i < blockstodecode; i++) |
| 1682 | 4110336 | *sample16++ = s->decoded[ch][i]; | |
| 1683 | } | ||
| 1684 | 176 | break; | |
| 1685 | 3 | case 24: | |
| 1686 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
|
9 | for (ch = 0; ch < s->channels; ch++) { |
| 1687 | 6 | sample24 = (int32_t *)frame->data[ch]; | |
| 1688 |
2/2✓ Branch 0 taken 27648 times.
✓ Branch 1 taken 6 times.
|
27654 | for (i = 0; i < blockstodecode; i++) |
| 1689 | 27648 | *sample24++ = s->decoded[ch][i] * 256U; | |
| 1690 | } | ||
| 1691 | 3 | break; | |
| 1692 | } | ||
| 1693 | |||
| 1694 | 179 | s->samples -= blockstodecode; | |
| 1695 | |||
| 1696 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 179 times.
|
179 | if (avctx->err_recognition & AV_EF_CRCCHECK && |
| 1697 | ✗ | s->fileversion >= 3900) { | |
| 1698 | ✗ | uint32_t crc = s->CRC_state; | |
| 1699 | ✗ | const AVCRC *crc_tab = av_crc_get_table(AV_CRC_32_IEEE_LE); | |
| 1700 | ✗ | int stride = s->bps == 24 ? 4 : (s->bps>>3); | |
| 1701 | ✗ | int offset = s->bps == 24; | |
| 1702 | ✗ | int bytes = s->bps >> 3; | |
| 1703 | |||
| 1704 | ✗ | for (i = 0; i < blockstodecode; i++) { | |
| 1705 | ✗ | for (ch = 0; ch < s->channels; ch++) { | |
| 1706 | #if HAVE_BIGENDIAN | ||
| 1707 | uint8_t *smp_native = frame->data[ch] + i*stride; | ||
| 1708 | uint8_t smp[4]; | ||
| 1709 | for(int j = 0; j<stride; j++) | ||
| 1710 | smp[j] = smp_native[stride-j-1]; | ||
| 1711 | #else | ||
| 1712 | ✗ | uint8_t *smp = frame->data[ch] + i*stride; | |
| 1713 | #endif | ||
| 1714 | ✗ | crc = av_crc(crc_tab, crc, smp+offset, bytes); | |
| 1715 | } | ||
| 1716 | } | ||
| 1717 | |||
| 1718 | ✗ | if (!s->samples && (~crc >> 1) ^ s->CRC) { | |
| 1719 | ✗ | av_log(avctx, AV_LOG_ERROR, "CRC mismatch! Previously decoded " | |
| 1720 | "frames may have been affected as well.\n"); | ||
| 1721 | ✗ | if (avctx->err_recognition & AV_EF_EXPLODE) | |
| 1722 | ✗ | return AVERROR_INVALIDDATA; | |
| 1723 | } | ||
| 1724 | |||
| 1725 | ✗ | s->CRC_state = crc; | |
| 1726 | } | ||
| 1727 | |||
| 1728 | 179 | *got_frame_ptr = 1; | |
| 1729 | |||
| 1730 |
2/2✓ Branch 0 taken 53 times.
✓ Branch 1 taken 126 times.
|
179 | return !s->samples ? avpkt->size : 0; |
| 1731 | } | ||
| 1732 | |||
| 1733 | ✗ | static av_cold void ape_flush(AVCodecContext *avctx) | |
| 1734 | { | ||
| 1735 | ✗ | APEContext *s = avctx->priv_data; | |
| 1736 | ✗ | s->samples= 0; | |
| 1737 | ✗ | } | |
| 1738 | |||
| 1739 | #define OFFSET(x) offsetof(APEContext, x) | ||
| 1740 | #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM) | ||
| 1741 | static const AVOption options[] = { | ||
| 1742 | { "max_samples", "maximum number of samples decoded per call", OFFSET(blocks_per_loop), AV_OPT_TYPE_INT, { .i64 = 4608 }, 1, INT_MAX, PAR, .unit = "max_samples" }, | ||
| 1743 | { "all", "no maximum. decode all samples for each packet at once", 0, AV_OPT_TYPE_CONST, { .i64 = INT_MAX }, INT_MIN, INT_MAX, PAR, .unit = "max_samples" }, | ||
| 1744 | { NULL}, | ||
| 1745 | }; | ||
| 1746 | |||
| 1747 | static const AVClass ape_decoder_class = { | ||
| 1748 | .class_name = "APE decoder", | ||
| 1749 | .item_name = av_default_item_name, | ||
| 1750 | .option = options, | ||
| 1751 | .version = LIBAVUTIL_VERSION_INT, | ||
| 1752 | }; | ||
| 1753 | |||
| 1754 | const FFCodec ff_ape_decoder = { | ||
| 1755 | .p.name = "ape", | ||
| 1756 | CODEC_LONG_NAME("Monkey's Audio"), | ||
| 1757 | .p.type = AVMEDIA_TYPE_AUDIO, | ||
| 1758 | .p.id = AV_CODEC_ID_APE, | ||
| 1759 | .priv_data_size = sizeof(APEContext), | ||
| 1760 | .init = ape_decode_init, | ||
| 1761 | .close = ape_decode_close, | ||
| 1762 | FF_CODEC_DECODE_CB(ape_decode_frame), | ||
| 1763 | .p.capabilities = AV_CODEC_CAP_DELAY | | ||
| 1764 | AV_CODEC_CAP_DR1, | ||
| 1765 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
| 1766 | .flush = ape_flush, | ||
| 1767 | CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_U8P, AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P), | ||
| 1768 | .p.priv_class = &ape_decoder_class, | ||
| 1769 | }; | ||
| 1770 |