1 /*
2  * gstvp8rangedecoder.c - VP8 range decoder interface
3  *
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.
9  */
10 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif
13 
14 #include "gstvp8rangedecoder.h"
15 #include "dboolhuff.h"
16 
17 #define BOOL_DECODER_CAST(rd) \
18   ((BOOL_DECODER *)(&(rd)->_gst_reserved[0]))
19 
20 gboolean
gst_vp8_range_decoder_init(GstVp8RangeDecoder * rd,const guchar * buf,guint buf_size)21 gst_vp8_range_decoder_init (GstVp8RangeDecoder * rd, const guchar * buf,
22     guint buf_size)
23 {
24   BOOL_DECODER *const bd = BOOL_DECODER_CAST (rd);
25 
26   g_return_val_if_fail (sizeof (rd->_gst_reserved) >= sizeof (*bd), FALSE);
27 
28   rd->buf = buf;
29   rd->buf_size = buf_size;
30   return vp8dx_start_decode (bd, buf, buf_size, NULL, NULL) == 0;
31 }
32 
33 gint
gst_vp8_range_decoder_read(GstVp8RangeDecoder * rd,guint8 prob)34 gst_vp8_range_decoder_read (GstVp8RangeDecoder * rd, guint8 prob)
35 {
36   return vp8dx_decode_bool (BOOL_DECODER_CAST (rd), prob);
37 }
38 
39 gint
gst_vp8_range_decoder_read_literal(GstVp8RangeDecoder * rd,gint bits)40 gst_vp8_range_decoder_read_literal (GstVp8RangeDecoder * rd, gint bits)
41 {
42   return vp8_decode_value (BOOL_DECODER_CAST (rd), bits);
43 }
44 
45 guint
gst_vp8_range_decoder_get_pos(GstVp8RangeDecoder * rd)46 gst_vp8_range_decoder_get_pos (GstVp8RangeDecoder * rd)
47 {
48   BOOL_DECODER *const bd = BOOL_DECODER_CAST (rd);
49 
50   return (bd->user_buffer - rd->buf) * 8 - (8 + bd->count);
51 }
52 
53 void
gst_vp8_range_decoder_get_state(GstVp8RangeDecoder * rd,GstVp8RangeDecoderState * state)54 gst_vp8_range_decoder_get_state (GstVp8RangeDecoder * rd,
55     GstVp8RangeDecoderState * state)
56 {
57   BOOL_DECODER *const bd = BOOL_DECODER_CAST (rd);
58 
59   if (bd->count < 0)
60     vp8dx_bool_decoder_fill (bd);
61 
62   state->range = bd->range;
63   state->value = (guint8) ((bd->value) >> (VP8_BD_VALUE_SIZE - 8));
64   state->count = (8 + bd->count) % 8;
65 }
66