msac.h 2.98 KB
Newer Older
1
/*
Rostislav Pehlivanov's avatar
Rostislav Pehlivanov committed
2 3 4
 * Copyright © 2018, VideoLAN and dav1d authors
 * Copyright © 2018, Two Orioles, LLC
 * All rights reserved.
5
 *
Rostislav Pehlivanov's avatar
Rostislav Pehlivanov committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 27 28 29 30 31 32 33
 */

#ifndef __DAV1D_SRC_MSAC_H__
#define __DAV1D_SRC_MSAC_H__

#include <stdint.h>
#include <stdlib.h>

Rostislav Pehlivanov's avatar
Rostislav Pehlivanov committed
34 35 36
/* Using uint32_t should be faster on 32 bit systems, in theory, maybe */
typedef uint64_t ec_win;

37
typedef struct MsacContext {
Rostislav Pehlivanov's avatar
Rostislav Pehlivanov committed
38 39 40
    const uint8_t *buf_pos;
    const uint8_t *buf_end;
    ec_win dif;
41
    uint16_t rng;
Rostislav Pehlivanov's avatar
Rostislav Pehlivanov committed
42
    int cnt;
43 44
} MsacContext;

Rostislav Pehlivanov's avatar
Rostislav Pehlivanov committed
45 46 47
#define EC_PROB_SHIFT 6
#define EC_BOOL_EPROB 256

48
void msac_init(MsacContext *c, const uint8_t *data, size_t sz);
Rostislav Pehlivanov's avatar
Rostislav Pehlivanov committed
49
unsigned msac_decode_symbol(MsacContext *s, const uint16_t *cdf,
50
                            const unsigned n_symbols);
Rostislav Pehlivanov's avatar
Rostislav Pehlivanov committed
51
unsigned msac_decode_bool(MsacContext *s, unsigned f);
52
unsigned msac_decode_bools(MsacContext *c, unsigned l);
Rostislav Pehlivanov's avatar
Rostislav Pehlivanov committed
53
int msac_decode_subexp(MsacContext *c, int ref, int n, unsigned k);
54
int msac_decode_uniform(MsacContext *c, unsigned n);
55
void msac_update_cdf(uint16_t *cdf, unsigned val, unsigned n_symbols);
56 57 58 59 60 61

static inline unsigned msac_decode_symbol_adapt(MsacContext *const c,
                                                uint16_t *const cdf,
                                                const unsigned n_symbols)
{
    const unsigned val = msac_decode_symbol(c, cdf, n_symbols);
62
    msac_update_cdf(cdf, val, n_symbols);
63 64 65 66 67 68
    return val;
}

static inline unsigned msac_decode_bool_adapt(MsacContext *const c,
                                              uint16_t *const cdf)
{
Rostislav Pehlivanov's avatar
Rostislav Pehlivanov committed
69
    const unsigned bit = msac_decode_bool(c, *cdf >> EC_PROB_SHIFT);
70
    uint16_t bak_cdf[3] = { cdf[0], 0, cdf[1] };
71
    msac_update_cdf(bak_cdf, bit, 2);
72 73 74 75 76 77
    cdf[0] = bak_cdf[0];
    cdf[1] = bak_cdf[2];
    return bit;
}

#endif /* __DAV1D_SRC_MSAC_H__ */