2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
16 #include "vpx_ports/config.h"
17 #include "vpx_ports/mem.h"
18 #include "vpx/vpx_integer.h"
20 typedef size_t VP8_BD_VALUE
;
22 # define VP8_BD_VALUE_SIZE ((int)sizeof(VP8_BD_VALUE)*CHAR_BIT)
23 /*This is meant to be a large, positive constant that can still be efficiently
24 loaded as an immediate (on platforms like ARM, for example).
25 Even relatively modest values like 100 would work fine.*/
26 # define VP8_LOTS_OF_BITS (0x40000000)
30 struct vp8_dboolhuff_rtcd_vtable
;
34 const unsigned char *user_buffer_end
;
35 const unsigned char *user_buffer
;
39 #if CONFIG_RUNTIME_CPU_DETECT
40 struct vp8_dboolhuff_rtcd_vtable
*rtcd
;
44 #define prototype_dbool_start(sym) int sym(BOOL_DECODER *br, \
45 const unsigned char *source, unsigned int source_sz)
46 #define prototype_dbool_fill(sym) void sym(BOOL_DECODER *br)
47 #define prototype_dbool_debool(sym) int sym(BOOL_DECODER *br, int probability)
48 #define prototype_dbool_devalue(sym) int sym(BOOL_DECODER *br, int bits)
51 #include "arm/dboolhuff_arm.h"
54 #ifndef vp8_dbool_start
55 #define vp8_dbool_start vp8dx_start_decode_c
58 #ifndef vp8_dbool_fill
59 #define vp8_dbool_fill vp8dx_bool_decoder_fill_c
62 #ifndef vp8_dbool_debool
63 #define vp8_dbool_debool vp8dx_decode_bool_c
66 #ifndef vp8_dbool_devalue
67 #define vp8_dbool_devalue vp8dx_decode_value_c
70 extern prototype_dbool_start(vp8_dbool_start
);
71 extern prototype_dbool_fill(vp8_dbool_fill
);
72 extern prototype_dbool_debool(vp8_dbool_debool
);
73 extern prototype_dbool_devalue(vp8_dbool_devalue
);
75 typedef prototype_dbool_start((*vp8_dbool_start_fn_t
));
76 typedef prototype_dbool_fill((*vp8_dbool_fill_fn_t
));
77 typedef prototype_dbool_debool((*vp8_dbool_debool_fn_t
));
78 typedef prototype_dbool_devalue((*vp8_dbool_devalue_fn_t
));
80 typedef struct vp8_dboolhuff_rtcd_vtable
{
81 vp8_dbool_start_fn_t start
;
82 vp8_dbool_fill_fn_t fill
;
83 vp8_dbool_debool_fn_t debool
;
84 vp8_dbool_devalue_fn_t devalue
;
85 } vp8_dboolhuff_rtcd_vtable_t
;
87 /* There are no processor-specific versions of these
88 * functions right now. Disable RTCD to avoid using
89 * function pointers which gives a speed boost
91 /*#ifdef ENABLE_RUNTIME_CPU_DETECT
92 #define DBOOLHUFF_INVOKE(ctx,fn) (ctx)->fn
93 #define IF_RTCD(x) (x)
95 #define DBOOLHUFF_INVOKE(ctx,fn) vp8_dbool_##fn
96 #define IF_RTCD(x) NULL
99 DECLARE_ALIGNED(16, extern const unsigned char, vp8dx_bitreader_norm
[256]);
101 /* wrapper functions to hide RTCD. static means inline means hopefully no
104 static int vp8dx_start_decode(BOOL_DECODER
*br
,
105 struct vp8_dboolhuff_rtcd_vtable
*rtcd
,
106 const unsigned char *source
, unsigned int source_sz
) {
107 #if CONFIG_RUNTIME_CPU_DETECT
110 return DBOOLHUFF_INVOKE(rtcd
, start
)(br
, source
, source_sz
);
112 static void vp8dx_bool_decoder_fill(BOOL_DECODER
*br
) {
113 DBOOLHUFF_INVOKE(br
->rtcd
, fill
)(br
);
116 /*The refill loop is used in several places, so define it in a macro to make
117 sure they're all consistent.
118 An inline function would be cleaner, but has a significant penalty, because
119 multiple BOOL_DECODER fields must be modified, and the compiler is not smart
120 enough to eliminate the stores to those fields and the subsequent reloads
121 from them when inlining the function.*/
122 #define VP8DX_BOOL_DECODER_FILL(_count,_value,_bufptr,_bufend) \
126 for(shift = VP8_BD_VALUE_SIZE - 8 - ((_count) + 8); shift >= 0; ) \
128 if((_bufptr) >= (_bufend)) { \
129 (_count) = VP8_LOTS_OF_BITS; \
133 (_value) |= (VP8_BD_VALUE)*(_bufptr)++ << shift; \
140 static int vp8dx_decode_bool(BOOL_DECODER
*br
, int probability
) {
142 * Until optimized versions of this function are available, we
143 * keep the implementation in the header to allow inlining.
145 *return DBOOLHUFF_INVOKE(br->rtcd, debool)(br, probability);
147 unsigned int bit
= 0;
150 VP8_BD_VALUE bigsplit
;
158 split
= 1 + (((range
- 1) * probability
) >> 8);
159 bigsplit
= (VP8_BD_VALUE
)split
<< (VP8_BD_VALUE_SIZE
- 8);
163 if (value
>= bigsplit
)
165 range
= br
->range
- split
;
166 value
= value
- bigsplit
;
178 register unsigned int shift
= vp8dx_bitreader_norm
[range
];
187 vp8dx_bool_decoder_fill(br
);
191 static int vp8_decode_value(BOOL_DECODER
*br
, int bits
)
194 * Until optimized versions of this function are available, we
195 * keep the implementation in the header to allow inlining.
197 *return DBOOLHUFF_INVOKE(br->rtcd, devalue)(br, bits);
202 for (bit
= bits
- 1; bit
>= 0; bit
--)
204 z
|= (vp8dx_decode_bool(br
, 0x80) << bit
);