2 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * Common VP5-VP9 range decoder stuff
26 #ifndef AVCODEC_VPX_RAC_H
27 #define AVCODEC_VPX_RAC_H
32 #include "libavutil/attributes.h"
33 #include "bytestream.h"
35 typedef struct VPXRangeCoder
{
37 int bits
; /* stored negated (i.e. negative "bits" is a positive number of
38 bits left) in order to eliminate a negate in cache refilling */
39 const uint8_t *buffer
;
41 unsigned int code_word
;
45 extern const uint8_t ff_vpx_norm_shift
[256];
46 int ff_vpx_init_range_decoder(VPXRangeCoder
*c
, const uint8_t *buf
, int buf_size
);
49 * returns 1 if the end of the stream has been reached, 0 otherwise.
51 static av_always_inline
int vpx_rac_is_end(VPXRangeCoder
*c
)
53 if (c
->end
<= c
->buffer
&& c
->bits
>= 0)
55 return c
->end_reached
> 10;
58 static av_always_inline
unsigned int vpx_rac_renorm(VPXRangeCoder
*c
)
60 int shift
= ff_vpx_norm_shift
[c
->high
];
62 unsigned int code_word
= c
->code_word
;
67 if(bits
>= 0 && c
->buffer
< c
->end
) {
68 code_word
|= bytestream_get_be16(&c
->buffer
) << bits
;
76 #include "arm/vpx_arith.h"
78 #include "x86/vpx_arith.h"
81 #ifndef vpx_rac_get_prob
82 #define vpx_rac_get_prob vpx_rac_get_prob
83 static av_always_inline
int vpx_rac_get_prob(VPXRangeCoder
*c
, uint8_t prob
)
85 unsigned int code_word
= vpx_rac_renorm(c
);
86 unsigned int low
= 1 + (((c
->high
- 1) * prob
) >> 8);
87 unsigned int low_shift
= low
<< 16;
88 int bit
= code_word
>= low_shift
;
90 c
->high
= bit
? c
->high
- low
: low
;
91 c
->code_word
= bit
? code_word
- low_shift
: code_word
;
97 #ifndef vpx_rac_get_prob_branchy
98 // branchy variant, to be used where there's a branch based on the bit decoded
99 static av_always_inline
int vpx_rac_get_prob_branchy(VPXRangeCoder
*c
, int prob
)
101 unsigned long code_word
= vpx_rac_renorm(c
);
102 unsigned low
= 1 + (((c
->high
- 1) * prob
) >> 8);
103 unsigned low_shift
= low
<< 16;
105 if (code_word
>= low_shift
) {
107 c
->code_word
= code_word
- low_shift
;
112 c
->code_word
= code_word
;
117 static av_always_inline
int vpx_rac_get(VPXRangeCoder
*c
)
119 unsigned int code_word
= vpx_rac_renorm(c
);
121 int low
= (c
->high
+ 1) >> 1;
122 unsigned int low_shift
= low
<< 16;
123 int bit
= code_word
>= low_shift
;
126 code_word
-= low_shift
;
131 c
->code_word
= code_word
;
135 #endif /* AVCODEC_VPX_RAC_H */