vl/rbsp: fix another three byte not detected
[mesa.git] / src / gallium / auxiliary / vl / vl_rbsp.h
1 /**************************************************************************
2 *
3 * Copyright 2013 Advanced Micro Devices, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Christian König <christian.koenig@amd.com>
31 *
32 */
33
34 /*
35 * Functions for reading the raw byte sequence payload of H.264
36 */
37
38 #ifndef vl_rbsp_h
39 #define vl_rbsp_h
40
41 #include "vl/vl_vlc.h"
42
43 struct vl_rbsp {
44 struct vl_vlc nal;
45 unsigned escaped;
46 };
47
48 /**
49 * Initialize the RBSP object
50 */
51 static inline void vl_rbsp_init(struct vl_rbsp *rbsp, struct vl_vlc *nal, unsigned num_bits)
52 {
53 unsigned valid, bits_left = vl_vlc_bits_left(nal);
54 int i;
55
56 /* copy the position */
57 rbsp->nal = *nal;
58
59 rbsp->escaped = 16;
60
61 /* search for the end of the NAL unit */
62 while (vl_vlc_search_byte(nal, num_bits, 0x00)) {
63 if (vl_vlc_peekbits(nal, 24) == 0x000001 ||
64 vl_vlc_peekbits(nal, 32) == 0x00000001) {
65 vl_vlc_limit(&rbsp->nal, bits_left - vl_vlc_bits_left(nal));
66 break;
67 }
68 vl_vlc_eatbits(nal, 8);
69 }
70
71 valid = vl_vlc_valid_bits(&rbsp->nal);
72 /* search for the emulation prevention three byte */
73 for (i = 24; i <= valid; i += 8) {
74 if ((vl_vlc_peekbits(&rbsp->nal, i) & 0xffffff) == 0x3) {
75 vl_vlc_removebits(&rbsp->nal, i - 8, 8);
76 i += 8;
77 }
78 }
79 }
80
81 /**
82 * Make at least 16 more bits available
83 */
84 static inline void vl_rbsp_fillbits(struct vl_rbsp *rbsp)
85 {
86 unsigned valid = vl_vlc_valid_bits(&rbsp->nal);
87 unsigned i, bits;
88
89 /* abort if we still have enough bits */
90 if (valid >= 32)
91 return;
92
93 vl_vlc_fillbits(&rbsp->nal);
94
95 /* abort if we have less than 24 bits left in this nal */
96 if (vl_vlc_bits_left(&rbsp->nal) < 24)
97 return;
98
99 /* check that we have enough bits left from the last fillbits */
100 assert(valid >= rbsp->escaped);
101
102 /* handle the already escaped bits */
103 valid -= rbsp->escaped;
104
105 /* search for the emulation prevention three byte */
106 rbsp->escaped = 16;
107 bits = vl_vlc_valid_bits(&rbsp->nal);
108 for (i = valid + 24; i <= bits; i += 8) {
109 if ((vl_vlc_peekbits(&rbsp->nal, i) & 0xffffff) == 0x3) {
110 vl_vlc_removebits(&rbsp->nal, i - 8, 8);
111 rbsp->escaped = bits - i;
112 bits -= 8;
113 i += 8;
114 }
115 }
116 }
117
118 /**
119 * Return an unsigned integer from the first n bits
120 */
121 static inline unsigned vl_rbsp_u(struct vl_rbsp *rbsp, unsigned n)
122 {
123 if (n == 0)
124 return 0;
125
126 vl_rbsp_fillbits(rbsp);
127 return vl_vlc_get_uimsbf(&rbsp->nal, n);
128 }
129
130 /**
131 * Return an unsigned exponential Golomb encoded integer
132 */
133 static inline unsigned vl_rbsp_ue(struct vl_rbsp *rbsp)
134 {
135 unsigned bits = 0;
136
137 vl_rbsp_fillbits(rbsp);
138 while (!vl_vlc_get_uimsbf(&rbsp->nal, 1))
139 ++bits;
140
141 return (1 << bits) - 1 + vl_rbsp_u(rbsp, bits);
142 }
143
144 /**
145 * Return an signed exponential Golomb encoded integer
146 */
147 static inline signed vl_rbsp_se(struct vl_rbsp *rbsp)
148 {
149 signed codeNum = vl_rbsp_ue(rbsp);
150 if (codeNum & 1)
151 return (codeNum + 1) >> 1;
152 else
153 return -(codeNum >> 1);
154 }
155
156 /**
157 * Are more data available in the RBSP ?
158 */
159 static inline bool vl_rbsp_more_data(struct vl_rbsp *rbsp)
160 {
161 unsigned bits, value;
162
163 if (vl_vlc_bits_left(&rbsp->nal) > 8)
164 return TRUE;
165
166 bits = vl_vlc_valid_bits(&rbsp->nal);
167 value = vl_vlc_peekbits(&rbsp->nal, bits);
168 if (value == 0 || value == (1 << (bits - 1)))
169 return FALSE;
170
171 return TRUE;
172 }
173
174 #endif /* vl_rbsp_h */