msac.h (4059B)
1 /* 2 * Copyright © 2018, VideoLAN and dav1d authors 3 * Copyright © 2018, Two Orioles, LLC 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright notice, this 10 * list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright notice, 13 * this list of conditions and the following disclaimer in the documentation 14 * and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #ifndef DAV1D_SRC_MSAC_H 29 #define DAV1D_SRC_MSAC_H 30 31 #include <stdint.h> 32 #include <stdlib.h> 33 34 #include "common/intops.h" 35 36 typedef size_t ec_win; 37 38 typedef struct MsacContext { 39 const uint8_t *buf_pos; 40 const uint8_t *buf_end; 41 ec_win dif; 42 unsigned rng; 43 int cnt; 44 int allow_update_cdf; 45 46 #if ARCH_X86_64 && HAVE_ASM 47 unsigned (*symbol_adapt16)(struct MsacContext *s, uint16_t *cdf, size_t n_symbols); 48 #endif 49 } MsacContext; 50 51 #if HAVE_ASM 52 #if ARCH_AARCH64 || ARCH_ARM 53 #include "src/arm/msac.h" 54 #elif ARCH_LOONGARCH64 55 #include "src/loongarch/msac.h" 56 #elif ARCH_X86 57 #include "src/x86/msac.h" 58 #endif 59 #endif 60 61 void dav1d_msac_init(MsacContext *s, const uint8_t *data, size_t sz, 62 int disable_cdf_update_flag); 63 unsigned dav1d_msac_decode_symbol_adapt_c(MsacContext *s, uint16_t *cdf, 64 size_t n_symbols); 65 unsigned dav1d_msac_decode_bool_adapt_c(MsacContext *s, uint16_t *cdf); 66 unsigned dav1d_msac_decode_bool_equi_c(MsacContext *s); 67 unsigned dav1d_msac_decode_bool_c(MsacContext *s, unsigned f); 68 unsigned dav1d_msac_decode_hi_tok_c(MsacContext *s, uint16_t *cdf); 69 int dav1d_msac_decode_subexp(MsacContext *s, int ref, int n, unsigned k); 70 71 /* Supported n_symbols ranges: adapt4: 1-3, adapt8: 1-7, adapt16: 3-15 */ 72 #ifndef dav1d_msac_decode_symbol_adapt4 73 #define dav1d_msac_decode_symbol_adapt4 dav1d_msac_decode_symbol_adapt_c 74 #endif 75 #ifndef dav1d_msac_decode_symbol_adapt8 76 #define dav1d_msac_decode_symbol_adapt8 dav1d_msac_decode_symbol_adapt_c 77 #endif 78 #ifndef dav1d_msac_decode_symbol_adapt16 79 #define dav1d_msac_decode_symbol_adapt16 dav1d_msac_decode_symbol_adapt_c 80 #endif 81 #ifndef dav1d_msac_decode_bool_adapt 82 #define dav1d_msac_decode_bool_adapt dav1d_msac_decode_bool_adapt_c 83 #endif 84 #ifndef dav1d_msac_decode_bool_equi 85 #define dav1d_msac_decode_bool_equi dav1d_msac_decode_bool_equi_c 86 #endif 87 #ifndef dav1d_msac_decode_bool 88 #define dav1d_msac_decode_bool dav1d_msac_decode_bool_c 89 #endif 90 #ifndef dav1d_msac_decode_hi_tok 91 #define dav1d_msac_decode_hi_tok dav1d_msac_decode_hi_tok_c 92 #endif 93 94 static inline unsigned dav1d_msac_decode_bools(MsacContext *const s, unsigned n) { 95 unsigned v = 0; 96 while (n--) 97 v = (v << 1) | dav1d_msac_decode_bool_equi(s); 98 return v; 99 } 100 101 static inline int dav1d_msac_decode_uniform(MsacContext *const s, const unsigned n) { 102 assert(n > 0); 103 const int l = ulog2(n) + 1; 104 assert(l > 1); 105 const unsigned m = (1 << l) - n; 106 const unsigned v = dav1d_msac_decode_bools(s, l - 1); 107 return v < m ? v : (v << 1) - m + dav1d_msac_decode_bool_equi(s); 108 } 109 110 #endif /* DAV1D_SRC_MSAC_H */