[g3dvl] move zscan into shaders
[mesa.git] / src / gallium / auxiliary / vl / vl_mpeg12_bitstream.c
1 /**************************************************************************
2 *
3 * Copyright 2011 Christian König.
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 TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS 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 * This file is based uppon slice_xvmc.c and vlc.h from the xine project,
30 * which in turn is based on mpeg2dec. The following is the original copyright:
31 *
32 * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
33 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
34 *
35 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
36 * See http://libmpeg2.sourceforge.net/ for updates.
37 *
38 * mpeg2dec is free software; you can redistribute it and/or modify
39 * it under the terms of the GNU General Public License as published by
40 * the Free Software Foundation; either version 2 of the License, or
41 * (at your option) any later version.
42 *
43 * mpeg2dec is distributed in the hope that it will be useful,
44 * but WITHOUT ANY WARRANTY; without even the implied warranty of
45 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46 * GNU General Public License for more details.
47 *
48 * You should have received a copy of the GNU General Public License
49 * along with this program; if not, write to the Free Software
50 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
51 */
52
53 #include <stdint.h>
54
55 #include <pipe/p_video_state.h>
56
57 #include "vl_vlc.h"
58 #include "vl_zscan.h"
59 #include "vl_mpeg12_bitstream.h"
60
61 /* take num bits from the high part of bit_buf and zero extend them */
62 #define UBITS(buf,num) (((uint32_t)(buf)) >> (32 - (num)))
63
64 /* take num bits from the high part of bit_buf and sign extend them */
65 #define SBITS(buf,num) (((int32_t)(buf)) >> (32 - (num)))
66
67 #define SATURATE(val) \
68 do { \
69 if ((uint32_t)(val + 2048) > 4095) \
70 val = (val > 0) ? 2047 : -2048; \
71 } while (0)
72
73 /* macroblock modes */
74 #define MACROBLOCK_INTRA 1
75 #define MACROBLOCK_PATTERN 2
76 #define MACROBLOCK_MOTION_BACKWARD 4
77 #define MACROBLOCK_MOTION_FORWARD 8
78 #define MACROBLOCK_QUANT 16
79 #define DCT_TYPE_INTERLACED 32
80
81 /* motion_type */
82 #define MOTION_TYPE_MASK (3*64)
83 #define MOTION_TYPE_BASE 64
84 #define MC_FIELD (1*64)
85 #define MC_FRAME (2*64)
86 #define MC_16X8 (2*64)
87 #define MC_DMV (3*64)
88
89 /* picture structure */
90 #define TOP_FIELD 1
91 #define BOTTOM_FIELD 2
92 #define FRAME_PICTURE 3
93
94 /* picture coding type (mpeg2 header) */
95 #define I_TYPE 1
96 #define P_TYPE 2
97 #define B_TYPE 3
98 #define D_TYPE 4
99
100 typedef struct {
101 uint8_t modes;
102 uint8_t len;
103 } MBtab;
104
105 typedef struct {
106 uint8_t delta;
107 uint8_t len;
108 } MVtab;
109
110 typedef struct {
111 int8_t dmv;
112 uint8_t len;
113 } DMVtab;
114
115 typedef struct {
116 uint8_t cbp;
117 uint8_t len;
118 } CBPtab;
119
120 typedef struct {
121 uint8_t size;
122 uint8_t len;
123 } DCtab;
124
125 typedef struct {
126 uint8_t run;
127 uint8_t level;
128 uint8_t len;
129 } DCTtab;
130
131 typedef struct {
132 uint8_t mba;
133 uint8_t len;
134 } MBAtab;
135
136 #define INTRA MACROBLOCK_INTRA
137 #define QUANT MACROBLOCK_QUANT
138 #define MC MACROBLOCK_MOTION_FORWARD
139 #define CODED MACROBLOCK_PATTERN
140 #define FWD MACROBLOCK_MOTION_FORWARD
141 #define BWD MACROBLOCK_MOTION_BACKWARD
142 #define INTER MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD
143
144 static const MBtab MB_I [] = {
145 {INTRA|QUANT, 2}, {INTRA, 1}
146 };
147
148 static const MBtab MB_P [] = {
149 {INTRA|QUANT, 6}, {CODED|QUANT, 5}, {MC|CODED|QUANT, 5}, {INTRA, 5},
150 {MC, 3}, {MC, 3}, {MC, 3}, {MC, 3},
151 {CODED, 2}, {CODED, 2}, {CODED, 2}, {CODED, 2},
152 {CODED, 2}, {CODED, 2}, {CODED, 2}, {CODED, 2},
153 {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1},
154 {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1},
155 {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1},
156 {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}, {MC|CODED, 1}
157 };
158
159 static const MBtab MB_B [] = {
160 {0, 0}, {INTRA|QUANT, 6},
161 {BWD|CODED|QUANT, 6}, {FWD|CODED|QUANT, 6},
162 {INTER|CODED|QUANT, 5}, {INTER|CODED|QUANT, 5},
163 {INTRA, 5}, {INTRA, 5},
164 {FWD, 4}, {FWD, 4}, {FWD, 4}, {FWD, 4},
165 {FWD|CODED, 4}, {FWD|CODED, 4}, {FWD|CODED, 4}, {FWD|CODED, 4},
166 {BWD, 3}, {BWD, 3}, {BWD, 3}, {BWD, 3},
167 {BWD, 3}, {BWD, 3}, {BWD, 3}, {BWD, 3},
168 {BWD|CODED, 3}, {BWD|CODED, 3}, {BWD|CODED, 3}, {BWD|CODED, 3},
169 {BWD|CODED, 3}, {BWD|CODED, 3}, {BWD|CODED, 3}, {BWD|CODED, 3},
170 {INTER, 2}, {INTER, 2}, {INTER, 2}, {INTER, 2},
171 {INTER, 2}, {INTER, 2}, {INTER, 2}, {INTER, 2},
172 {INTER, 2}, {INTER, 2}, {INTER, 2}, {INTER, 2},
173 {INTER, 2}, {INTER, 2}, {INTER, 2}, {INTER, 2},
174 {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2},
175 {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2},
176 {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2},
177 {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}
178 };
179
180 #undef INTRA
181 #undef QUANT
182 #undef MC
183 #undef CODED
184 #undef FWD
185 #undef BWD
186 #undef INTER
187
188 static const MVtab MV_4 [] = {
189 { 3, 6}, { 2, 4}, { 1, 3}, { 1, 3}, { 0, 2}, { 0, 2}, { 0, 2}, { 0, 2}
190 };
191
192 static const MVtab MV_10 [] = {
193 { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10},
194 { 0,10}, { 0,10}, { 0,10}, { 0,10}, {15,10}, {14,10}, {13,10}, {12,10},
195 {11,10}, {10,10}, { 9, 9}, { 9, 9}, { 8, 9}, { 8, 9}, { 7, 9}, { 7, 9},
196 { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7},
197 { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7},
198 { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}
199 };
200
201 static const DMVtab DMV_2 [] = {
202 { 0, 1}, { 0, 1}, { 1, 2}, {-1, 2}
203 };
204
205 static const CBPtab CBP_7 [] = {
206 {0x22, 7}, {0x12, 7}, {0x0a, 7}, {0x06, 7},
207 {0x21, 7}, {0x11, 7}, {0x09, 7}, {0x05, 7},
208 {0x3f, 6}, {0x3f, 6}, {0x03, 6}, {0x03, 6},
209 {0x24, 6}, {0x24, 6}, {0x18, 6}, {0x18, 6},
210 {0x3e, 5}, {0x3e, 5}, {0x3e, 5}, {0x3e, 5},
211 {0x02, 5}, {0x02, 5}, {0x02, 5}, {0x02, 5},
212 {0x3d, 5}, {0x3d, 5}, {0x3d, 5}, {0x3d, 5},
213 {0x01, 5}, {0x01, 5}, {0x01, 5}, {0x01, 5},
214 {0x38, 5}, {0x38, 5}, {0x38, 5}, {0x38, 5},
215 {0x34, 5}, {0x34, 5}, {0x34, 5}, {0x34, 5},
216 {0x2c, 5}, {0x2c, 5}, {0x2c, 5}, {0x2c, 5},
217 {0x1c, 5}, {0x1c, 5}, {0x1c, 5}, {0x1c, 5},
218 {0x28, 5}, {0x28, 5}, {0x28, 5}, {0x28, 5},
219 {0x14, 5}, {0x14, 5}, {0x14, 5}, {0x14, 5},
220 {0x30, 5}, {0x30, 5}, {0x30, 5}, {0x30, 5},
221 {0x0c, 5}, {0x0c, 5}, {0x0c, 5}, {0x0c, 5},
222 {0x20, 4}, {0x20, 4}, {0x20, 4}, {0x20, 4},
223 {0x20, 4}, {0x20, 4}, {0x20, 4}, {0x20, 4},
224 {0x10, 4}, {0x10, 4}, {0x10, 4}, {0x10, 4},
225 {0x10, 4}, {0x10, 4}, {0x10, 4}, {0x10, 4},
226 {0x08, 4}, {0x08, 4}, {0x08, 4}, {0x08, 4},
227 {0x08, 4}, {0x08, 4}, {0x08, 4}, {0x08, 4},
228 {0x04, 4}, {0x04, 4}, {0x04, 4}, {0x04, 4},
229 {0x04, 4}, {0x04, 4}, {0x04, 4}, {0x04, 4},
230 {0x3c, 3}, {0x3c, 3}, {0x3c, 3}, {0x3c, 3},
231 {0x3c, 3}, {0x3c, 3}, {0x3c, 3}, {0x3c, 3},
232 {0x3c, 3}, {0x3c, 3}, {0x3c, 3}, {0x3c, 3},
233 {0x3c, 3}, {0x3c, 3}, {0x3c, 3}, {0x3c, 3}
234 };
235
236 static const CBPtab CBP_9 [] = {
237 {0, 0}, {0x00, 9}, {0x27, 9}, {0x1b, 9},
238 {0x3b, 9}, {0x37, 9}, {0x2f, 9}, {0x1f, 9},
239 {0x3a, 8}, {0x3a, 8}, {0x36, 8}, {0x36, 8},
240 {0x2e, 8}, {0x2e, 8}, {0x1e, 8}, {0x1e, 8},
241 {0x39, 8}, {0x39, 8}, {0x35, 8}, {0x35, 8},
242 {0x2d, 8}, {0x2d, 8}, {0x1d, 8}, {0x1d, 8},
243 {0x26, 8}, {0x26, 8}, {0x1a, 8}, {0x1a, 8},
244 {0x25, 8}, {0x25, 8}, {0x19, 8}, {0x19, 8},
245 {0x2b, 8}, {0x2b, 8}, {0x17, 8}, {0x17, 8},
246 {0x33, 8}, {0x33, 8}, {0x0f, 8}, {0x0f, 8},
247 {0x2a, 8}, {0x2a, 8}, {0x16, 8}, {0x16, 8},
248 {0x32, 8}, {0x32, 8}, {0x0e, 8}, {0x0e, 8},
249 {0x29, 8}, {0x29, 8}, {0x15, 8}, {0x15, 8},
250 {0x31, 8}, {0x31, 8}, {0x0d, 8}, {0x0d, 8},
251 {0x23, 8}, {0x23, 8}, {0x13, 8}, {0x13, 8},
252 {0x0b, 8}, {0x0b, 8}, {0x07, 8}, {0x07, 8}
253 };
254
255 static const DCtab DC_lum_5 [] = {
256 {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
257 {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
258 {0, 3}, {0, 3}, {0, 3}, {0, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3},
259 {4, 3}, {4, 3}, {4, 3}, {4, 3}, {5, 4}, {5, 4}, {6, 5}
260 };
261
262 static const DCtab DC_chrom_5 [] = {
263 {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2},
264 {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
265 {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
266 {3, 3}, {3, 3}, {3, 3}, {3, 3}, {4, 4}, {4, 4}, {5, 5}
267 };
268
269 static const DCtab DC_long [] = {
270 {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, { 6, 5}, { 6, 5},
271 {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, { 6, 5}, { 6, 5},
272 {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, { 7, 6}, { 7, 6},
273 {8, 7}, {8, 7}, {8, 7}, {8, 7}, {9, 8}, {9, 8}, {10, 9}, {11, 9}
274 };
275
276 static const DCTtab DCT_16 [] = {
277 {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0},
278 {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0},
279 {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0},
280 {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0},
281 { 2,18, 0}, { 2,17, 0}, { 2,16, 0}, { 2,15, 0},
282 { 7, 3, 0}, { 17, 2, 0}, { 16, 2, 0}, { 15, 2, 0},
283 { 14, 2, 0}, { 13, 2, 0}, { 12, 2, 0}, { 32, 1, 0},
284 { 31, 1, 0}, { 30, 1, 0}, { 29, 1, 0}, { 28, 1, 0}
285 };
286
287 static const DCTtab DCT_15 [] = {
288 { 1,40,15}, { 1,39,15}, { 1,38,15}, { 1,37,15},
289 { 1,36,15}, { 1,35,15}, { 1,34,15}, { 1,33,15},
290 { 1,32,15}, { 2,14,15}, { 2,13,15}, { 2,12,15},
291 { 2,11,15}, { 2,10,15}, { 2, 9,15}, { 2, 8,15},
292 { 1,31,14}, { 1,31,14}, { 1,30,14}, { 1,30,14},
293 { 1,29,14}, { 1,29,14}, { 1,28,14}, { 1,28,14},
294 { 1,27,14}, { 1,27,14}, { 1,26,14}, { 1,26,14},
295 { 1,25,14}, { 1,25,14}, { 1,24,14}, { 1,24,14},
296 { 1,23,14}, { 1,23,14}, { 1,22,14}, { 1,22,14},
297 { 1,21,14}, { 1,21,14}, { 1,20,14}, { 1,20,14},
298 { 1,19,14}, { 1,19,14}, { 1,18,14}, { 1,18,14},
299 { 1,17,14}, { 1,17,14}, { 1,16,14}, { 1,16,14}
300 };
301
302 static const DCTtab DCT_13 [] = {
303 { 11, 2,13}, { 10, 2,13}, { 6, 3,13}, { 4, 4,13},
304 { 3, 5,13}, { 2, 7,13}, { 2, 6,13}, { 1,15,13},
305 { 1,14,13}, { 1,13,13}, { 1,12,13}, { 27, 1,13},
306 { 26, 1,13}, { 25, 1,13}, { 24, 1,13}, { 23, 1,13},
307 { 1,11,12}, { 1,11,12}, { 9, 2,12}, { 9, 2,12},
308 { 5, 3,12}, { 5, 3,12}, { 1,10,12}, { 1,10,12},
309 { 3, 4,12}, { 3, 4,12}, { 8, 2,12}, { 8, 2,12},
310 { 22, 1,12}, { 22, 1,12}, { 21, 1,12}, { 21, 1,12},
311 { 1, 9,12}, { 1, 9,12}, { 20, 1,12}, { 20, 1,12},
312 { 19, 1,12}, { 19, 1,12}, { 2, 5,12}, { 2, 5,12},
313 { 4, 3,12}, { 4, 3,12}, { 1, 8,12}, { 1, 8,12},
314 { 7, 2,12}, { 7, 2,12}, { 18, 1,12}, { 18, 1,12}
315 };
316
317 static const DCTtab DCT_B14_10 [] = {
318 { 17, 1,10}, { 6, 2,10}, { 1, 7,10}, { 3, 3,10},
319 { 2, 4,10}, { 16, 1,10}, { 15, 1,10}, { 5, 2,10}
320 };
321
322 static const DCTtab DCT_B14_8 [] = {
323 { 65, 0, 6}, { 65, 0, 6}, { 65, 0, 6}, { 65, 0, 6},
324 { 3, 2, 7}, { 3, 2, 7}, { 10, 1, 7}, { 10, 1, 7},
325 { 1, 4, 7}, { 1, 4, 7}, { 9, 1, 7}, { 9, 1, 7},
326 { 8, 1, 6}, { 8, 1, 6}, { 8, 1, 6}, { 8, 1, 6},
327 { 7, 1, 6}, { 7, 1, 6}, { 7, 1, 6}, { 7, 1, 6},
328 { 2, 2, 6}, { 2, 2, 6}, { 2, 2, 6}, { 2, 2, 6},
329 { 6, 1, 6}, { 6, 1, 6}, { 6, 1, 6}, { 6, 1, 6},
330 { 14, 1, 8}, { 1, 6, 8}, { 13, 1, 8}, { 12, 1, 8},
331 { 4, 2, 8}, { 2, 3, 8}, { 1, 5, 8}, { 11, 1, 8}
332 };
333
334 static const DCTtab DCT_B14AC_5 [] = {
335 { 1, 3, 5}, { 5, 1, 5}, { 4, 1, 5},
336 { 1, 2, 4}, { 1, 2, 4}, { 3, 1, 4}, { 3, 1, 4},
337 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
338 {129, 0, 2}, {129, 0, 2}, {129, 0, 2}, {129, 0, 2},
339 {129, 0, 2}, {129, 0, 2}, {129, 0, 2}, {129, 0, 2},
340 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
341 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}
342 };
343
344 static const DCTtab DCT_B14DC_5 [] = {
345 { 1, 3, 5}, { 5, 1, 5}, { 4, 1, 5},
346 { 1, 2, 4}, { 1, 2, 4}, { 3, 1, 4}, { 3, 1, 4},
347 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
348 { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1},
349 { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1},
350 { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1},
351 { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}
352 };
353
354 static const DCTtab DCT_B15_10 [] = {
355 { 6, 2, 9}, { 6, 2, 9}, { 15, 1, 9}, { 15, 1, 9},
356 { 3, 4,10}, { 17, 1,10}, { 16, 1, 9}, { 16, 1, 9}
357 };
358
359 static const DCTtab DCT_B15_8 [] = {
360 { 65, 0, 6}, { 65, 0, 6}, { 65, 0, 6}, { 65, 0, 6},
361 { 8, 1, 7}, { 8, 1, 7}, { 9, 1, 7}, { 9, 1, 7},
362 { 7, 1, 7}, { 7, 1, 7}, { 3, 2, 7}, { 3, 2, 7},
363 { 1, 7, 6}, { 1, 7, 6}, { 1, 7, 6}, { 1, 7, 6},
364 { 1, 6, 6}, { 1, 6, 6}, { 1, 6, 6}, { 1, 6, 6},
365 { 5, 1, 6}, { 5, 1, 6}, { 5, 1, 6}, { 5, 1, 6},
366 { 6, 1, 6}, { 6, 1, 6}, { 6, 1, 6}, { 6, 1, 6},
367 { 2, 5, 8}, { 12, 1, 8}, { 1,11, 8}, { 1,10, 8},
368 { 14, 1, 8}, { 13, 1, 8}, { 4, 2, 8}, { 2, 4, 8},
369 { 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5},
370 { 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5},
371 { 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5},
372 { 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5},
373 { 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5},
374 { 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5},
375 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
376 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
377 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
378 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
379 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
380 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
381 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
382 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
383 {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4},
384 {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4},
385 {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4},
386 {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4},
387 { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4},
388 { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4},
389 { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4},
390 { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4},
391 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
392 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
393 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
394 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
395 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
396 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
397 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
398 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
399 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
400 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
401 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
402 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
403 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
404 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
405 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
406 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
407 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
408 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
409 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
410 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
411 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
412 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
413 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
414 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
415 { 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5},
416 { 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5},
417 { 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5},
418 { 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5},
419 { 10, 1, 7}, { 10, 1, 7}, { 2, 3, 7}, { 2, 3, 7},
420 { 11, 1, 7}, { 11, 1, 7}, { 1, 8, 7}, { 1, 8, 7},
421 { 1, 9, 7}, { 1, 9, 7}, { 1,12, 8}, { 1,13, 8},
422 { 3, 3, 8}, { 5, 2, 8}, { 1,14, 8}, { 1,15, 8}
423 };
424
425 static const MBAtab MBA_5 [] = {
426 {6, 5}, {5, 5}, {4, 4}, {4, 4}, {3, 4}, {3, 4},
427 {2, 3}, {2, 3}, {2, 3}, {2, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3},
428 {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1},
429 {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}
430 };
431
432 static const MBAtab MBA_11 [] = {
433 {32, 11}, {31, 11}, {30, 11}, {29, 11},
434 {28, 11}, {27, 11}, {26, 11}, {25, 11},
435 {24, 11}, {23, 11}, {22, 11}, {21, 11},
436 {20, 10}, {20, 10}, {19, 10}, {19, 10},
437 {18, 10}, {18, 10}, {17, 10}, {17, 10},
438 {16, 10}, {16, 10}, {15, 10}, {15, 10},
439 {14, 8}, {14, 8}, {14, 8}, {14, 8},
440 {14, 8}, {14, 8}, {14, 8}, {14, 8},
441 {13, 8}, {13, 8}, {13, 8}, {13, 8},
442 {13, 8}, {13, 8}, {13, 8}, {13, 8},
443 {12, 8}, {12, 8}, {12, 8}, {12, 8},
444 {12, 8}, {12, 8}, {12, 8}, {12, 8},
445 {11, 8}, {11, 8}, {11, 8}, {11, 8},
446 {11, 8}, {11, 8}, {11, 8}, {11, 8},
447 {10, 8}, {10, 8}, {10, 8}, {10, 8},
448 {10, 8}, {10, 8}, {10, 8}, {10, 8},
449 { 9, 8}, { 9, 8}, { 9, 8}, { 9, 8},
450 { 9, 8}, { 9, 8}, { 9, 8}, { 9, 8},
451 { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7},
452 { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7},
453 { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7},
454 { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7},
455 { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7},
456 { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7},
457 { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7},
458 { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7}
459 };
460
461 static const int non_linear_quantizer_scale[] = {
462 0, 1, 2, 3, 4, 5, 6, 7,
463 8, 10, 12, 14, 16, 18, 20, 22,
464 24, 28, 32, 36, 40, 44, 48, 52,
465 56, 64, 72, 80, 88, 96, 104, 112
466 };
467
468 static inline int
469 get_macroblock_modes(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture)
470 {
471 int macroblock_modes;
472 const MBtab * tab;
473
474 switch (picture->picture_coding_type) {
475 case I_TYPE:
476
477 tab = MB_I + vl_vlc_ubits(&bs->vlc, 1);
478 vl_vlc_dumpbits(&bs->vlc, tab->len);
479 macroblock_modes = tab->modes;
480
481 if ((!(picture->frame_pred_frame_dct)) && (picture->picture_structure == FRAME_PICTURE)) {
482 macroblock_modes |= vl_vlc_ubits(&bs->vlc, 1) * DCT_TYPE_INTERLACED;
483 vl_vlc_dumpbits(&bs->vlc, 1);
484 }
485
486 return macroblock_modes;
487
488 case P_TYPE:
489
490 tab = MB_P + vl_vlc_ubits(&bs->vlc, 5);
491 vl_vlc_dumpbits(&bs->vlc, tab->len);
492 macroblock_modes = tab->modes;
493
494 if (picture->picture_structure != FRAME_PICTURE) {
495 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD) {
496 macroblock_modes |= vl_vlc_ubits(&bs->vlc, 2) * MOTION_TYPE_BASE;
497 vl_vlc_dumpbits(&bs->vlc, 2);
498 }
499 return macroblock_modes;
500 } else if (picture->frame_pred_frame_dct) {
501 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
502 macroblock_modes |= MC_FRAME;
503 return macroblock_modes;
504 } else {
505 if (macroblock_modes & MACROBLOCK_MOTION_FORWARD) {
506 macroblock_modes |= vl_vlc_ubits(&bs->vlc, 2) * MOTION_TYPE_BASE;
507 vl_vlc_dumpbits(&bs->vlc, 2);
508 }
509 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN)) {
510 macroblock_modes |= vl_vlc_ubits(&bs->vlc, 1) * DCT_TYPE_INTERLACED;
511 vl_vlc_dumpbits(&bs->vlc, 1);
512 }
513 return macroblock_modes;
514 }
515
516 case B_TYPE:
517
518 tab = MB_B + vl_vlc_ubits(&bs->vlc, 6);
519 vl_vlc_dumpbits(&bs->vlc, tab->len);
520 macroblock_modes = tab->modes;
521
522 if (picture->picture_structure != FRAME_PICTURE) {
523 if (! (macroblock_modes & MACROBLOCK_INTRA)) {
524 macroblock_modes |= vl_vlc_ubits(&bs->vlc, 2) * MOTION_TYPE_BASE;
525 vl_vlc_dumpbits(&bs->vlc, 2);
526 }
527 return macroblock_modes;
528 } else if (picture->frame_pred_frame_dct) {
529 /* if (! (macroblock_modes & MACROBLOCK_INTRA)) */
530 macroblock_modes |= MC_FRAME;
531 return macroblock_modes;
532 } else {
533 if (macroblock_modes & MACROBLOCK_INTRA)
534 goto intra;
535 macroblock_modes |= vl_vlc_ubits(&bs->vlc, 2) * MOTION_TYPE_BASE;
536 vl_vlc_dumpbits(&bs->vlc, 2);
537 if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN)) {
538 intra:
539 macroblock_modes |= vl_vlc_ubits(&bs->vlc, 1) * DCT_TYPE_INTERLACED;
540 vl_vlc_dumpbits(&bs->vlc, 1);
541 }
542 return macroblock_modes;
543 }
544
545 case D_TYPE:
546
547 vl_vlc_dumpbits(&bs->vlc, 1);
548 return MACROBLOCK_INTRA;
549
550 default:
551 return 0;
552 }
553 }
554
555 static inline int
556 get_quantizer_scale(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture)
557 {
558 int quantizer_scale_code;
559
560 quantizer_scale_code = vl_vlc_ubits(&bs->vlc, 5);
561 vl_vlc_dumpbits(&bs->vlc, 5);
562
563 if (picture->q_scale_type)
564 return non_linear_quantizer_scale[quantizer_scale_code];
565 else
566 return quantizer_scale_code << 1;
567 }
568
569 static inline int
570 get_motion_delta(struct vl_mpg12_bs *bs, unsigned f_code)
571 {
572 int delta;
573 int sign;
574 const MVtab * tab;
575
576 if (bs->vlc.buf & 0x80000000) {
577 vl_vlc_dumpbits(&bs->vlc, 1);
578 return 0;
579 } else if (bs->vlc.buf >= 0x0c000000) {
580
581 tab = MV_4 + vl_vlc_ubits(&bs->vlc, 4);
582 delta = (tab->delta << f_code) + 1;
583 bs->vlc.bits += tab->len + f_code + 1;
584 bs->vlc.buf <<= tab->len;
585
586 sign = vl_vlc_sbits(&bs->vlc, 1);
587 bs->vlc.buf <<= 1;
588
589 if (f_code)
590 delta += vl_vlc_ubits(&bs->vlc, f_code);
591 bs->vlc.buf <<= f_code;
592
593 return (delta ^ sign) - sign;
594
595 } else {
596
597 tab = MV_10 + vl_vlc_ubits(&bs->vlc, 10);
598 delta = (tab->delta << f_code) + 1;
599 bs->vlc.bits += tab->len + 1;
600 bs->vlc.buf <<= tab->len;
601
602 sign = vl_vlc_sbits(&bs->vlc, 1);
603 bs->vlc.buf <<= 1;
604
605 if (f_code) {
606 vl_vlc_needbits(&bs->vlc);
607 delta += vl_vlc_ubits(&bs->vlc, f_code);
608 vl_vlc_dumpbits(&bs->vlc, f_code);
609 }
610
611 return (delta ^ sign) - sign;
612 }
613 }
614
615 static inline int
616 bound_motion_vector(int vec, unsigned f_code)
617 {
618 #if 1
619 unsigned int limit;
620 int sign;
621
622 limit = 16 << f_code;
623
624 if ((unsigned int)(vec + limit) < 2 * limit)
625 return vec;
626 else {
627 sign = ((int32_t)vec) >> 31;
628 return vec - ((2 * limit) ^ sign) + sign;
629 }
630 #else
631 return ((int32_t)vec << (28 - f_code)) >> (28 - f_code);
632 #endif
633 }
634
635 static inline int
636 get_dmv(struct vl_mpg12_bs *bs)
637 {
638 const DMVtab * tab;
639
640 tab = DMV_2 + vl_vlc_ubits(&bs->vlc, 2);
641 vl_vlc_dumpbits(&bs->vlc, tab->len);
642 return tab->dmv;
643 }
644
645 static inline int
646 get_coded_block_pattern(struct vl_mpg12_bs *bs)
647 {
648 const CBPtab * tab;
649
650 vl_vlc_needbits(&bs->vlc);
651
652 if (bs->vlc.buf >= 0x20000000) {
653
654 tab = CBP_7 + (vl_vlc_ubits(&bs->vlc, 7) - 16);
655 vl_vlc_dumpbits(&bs->vlc, tab->len);
656 return tab->cbp;
657
658 } else {
659
660 tab = CBP_9 + vl_vlc_ubits(&bs->vlc, 9);
661 vl_vlc_dumpbits(&bs->vlc, tab->len);
662 return tab->cbp;
663 }
664 }
665
666 static inline int
667 get_luma_dc_dct_diff(struct vl_mpg12_bs *bs)
668 {
669 const DCtab * tab;
670 int size;
671 int dc_diff;
672
673 if (bs->vlc.buf < 0xf8000000) {
674 tab = DC_lum_5 + vl_vlc_ubits(&bs->vlc, 5);
675 size = tab->size;
676 if (size) {
677 bs->vlc.bits += tab->len + size;
678 bs->vlc.buf <<= tab->len;
679 dc_diff = vl_vlc_ubits(&bs->vlc, size) - UBITS (SBITS (~bs->vlc.buf, 1), size);
680 bs->vlc.buf <<= size;
681 return dc_diff;
682 } else {
683 vl_vlc_dumpbits(&bs->vlc, 3);
684 return 0;
685 }
686 } else {
687 tab = DC_long + (vl_vlc_ubits(&bs->vlc, 9) - 0x1e0);
688 size = tab->size;
689 vl_vlc_dumpbits(&bs->vlc, tab->len);
690 vl_vlc_needbits(&bs->vlc);
691 dc_diff = vl_vlc_ubits(&bs->vlc, size) - UBITS (SBITS (~bs->vlc.buf, 1), size);
692 vl_vlc_dumpbits(&bs->vlc, size);
693 return dc_diff;
694 }
695 }
696
697 static inline int
698 get_chroma_dc_dct_diff(struct vl_mpg12_bs *bs)
699 {
700 const DCtab * tab;
701 int size;
702 int dc_diff;
703
704 if (bs->vlc.buf < 0xf8000000) {
705 tab = DC_chrom_5 + vl_vlc_ubits(&bs->vlc, 5);
706 size = tab->size;
707 if (size) {
708 bs->vlc.bits += tab->len + size;
709 bs->vlc.buf <<= tab->len;
710 dc_diff = vl_vlc_ubits(&bs->vlc, size) - UBITS (SBITS (~bs->vlc.buf, 1), size);
711 bs->vlc.buf <<= size;
712 return dc_diff;
713 } else {
714 vl_vlc_dumpbits(&bs->vlc, 2);
715 return 0;
716 }
717 } else {
718 tab = DC_long + (vl_vlc_ubits(&bs->vlc, 10) - 0x3e0);
719 size = tab->size;
720 vl_vlc_dumpbits(&bs->vlc, tab->len + 1);
721 vl_vlc_needbits(&bs->vlc);
722 dc_diff = vl_vlc_ubits(&bs->vlc, size) - UBITS (SBITS (~bs->vlc.buf, 1), size);
723 vl_vlc_dumpbits(&bs->vlc, size);
724 return dc_diff;
725 }
726 }
727
728 static inline void
729 get_intra_block_B14(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture,
730 const int scan[64], int quantizer_scale, short *dest)
731 {
732 int i, j, val;
733 uint8_t *quant_matrix = picture->intra_quantizer_matrix;
734 int mismatch;
735 const DCTtab *tab;
736
737 i = 0;
738 mismatch = ~dest[0];
739
740 vl_vlc_needbits(&bs->vlc);
741
742 while (1) {
743 if (bs->vlc.buf >= 0x28000000) {
744
745 tab = DCT_B14AC_5 + (vl_vlc_ubits(&bs->vlc, 5) - 5);
746
747 i += tab->run;
748 if (i >= 64)
749 break; /* end of block */
750
751 normal_code:
752 j = scan[i];
753
754 bs->vlc.buf <<= tab->len;
755 bs->vlc.bits += tab->len + 1;
756 val = (tab->level * quantizer_scale * quant_matrix[j]) >> 4;
757
758 /* if (bitstream_get (1)) val = -val; */
759 val = (val ^ vl_vlc_sbits(&bs->vlc, 1)) - vl_vlc_sbits(&bs->vlc, 1);
760
761 SATURATE (val);
762 dest[i] = val;
763 mismatch ^= val;
764
765 bs->vlc.buf <<= 1;
766 vl_vlc_needbits(&bs->vlc);
767
768 continue;
769
770 } else if (bs->vlc.buf >= 0x04000000) {
771
772 tab = DCT_B14_8 + (vl_vlc_ubits(&bs->vlc, 8) - 4);
773
774 i += tab->run;
775 if (i < 64)
776 goto normal_code;
777
778 /* escape code */
779
780 i += UBITS(bs->vlc.buf << 6, 6) - 64;
781 if (i >= 64)
782 break; /* illegal, check needed to avoid buffer overflow */
783
784 j = scan[i];
785
786 vl_vlc_dumpbits(&bs->vlc, 12);
787 vl_vlc_needbits(&bs->vlc);
788 val = (vl_vlc_sbits(&bs->vlc, 12) * quantizer_scale * quant_matrix[j]) / 16;
789
790 SATURATE (val);
791 dest[i] = val;
792 mismatch ^= val;
793
794 vl_vlc_dumpbits(&bs->vlc, 12);
795 vl_vlc_needbits(&bs->vlc);
796
797 continue;
798
799 } else if (bs->vlc.buf >= 0x02000000) {
800 tab = DCT_B14_10 + (vl_vlc_ubits(&bs->vlc, 10) - 8);
801 i += tab->run;
802 if (i < 64)
803 goto normal_code;
804 } else if (bs->vlc.buf >= 0x00800000) {
805 tab = DCT_13 + (vl_vlc_ubits(&bs->vlc, 13) - 16);
806 i += tab->run;
807 if (i < 64)
808 goto normal_code;
809 } else if (bs->vlc.buf >= 0x00200000) {
810 tab = DCT_15 + (vl_vlc_ubits(&bs->vlc, 15) - 16);
811 i += tab->run;
812 if (i < 64)
813 goto normal_code;
814 } else {
815 tab = DCT_16 + vl_vlc_ubits(&bs->vlc, 16);
816 bs->vlc.buf <<= 16;
817 vl_vlc_getword(&bs->vlc, bs->vlc.bits + 16);
818 i += tab->run;
819 if (i < 64)
820 goto normal_code;
821 }
822 break; /* illegal, check needed to avoid buffer overflow */
823 }
824
825 dest[63] ^= mismatch & 1;
826 vl_vlc_dumpbits(&bs->vlc, 2); /* dump end of block code */
827 }
828
829 static inline void
830 get_intra_block_B15(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture,
831 const int scan[64], int quantizer_scale, short *dest)
832 {
833 int i, j, val;
834 uint8_t *quant_matrix = picture->intra_quantizer_matrix;
835 int mismatch;
836 const DCTtab * tab;
837
838 i = 0;
839 mismatch = ~dest[0];
840
841 vl_vlc_needbits(&bs->vlc);
842
843 while (1) {
844 if (bs->vlc.buf >= 0x04000000) {
845
846 tab = DCT_B15_8 + (vl_vlc_ubits(&bs->vlc, 8) - 4);
847
848 i += tab->run;
849 if (i < 64) {
850
851 normal_code:
852 j = scan[i];
853 bs->vlc.buf <<= tab->len;
854 bs->vlc.bits += tab->len + 1;
855 val = (tab->level * quantizer_scale * quant_matrix[j]) >> 4;
856
857 /* if (bitstream_get (1)) val = -val; */
858 val = (val ^ vl_vlc_sbits(&bs->vlc, 1)) - vl_vlc_sbits(&bs->vlc, 1);
859
860 SATURATE (val);
861 dest[i] = val;
862 mismatch ^= val;
863
864 bs->vlc.buf <<= 1;
865 vl_vlc_needbits(&bs->vlc);
866
867 continue;
868
869 } else {
870
871 /* end of block. I commented out this code because if we */
872 /* dont exit here we will still exit at the later test :) */
873
874 /* if (i >= 128) break; */ /* end of block */
875
876 /* escape code */
877
878 i += UBITS(bs->vlc.buf << 6, 6) - 64;
879 if (i >= 64)
880 break; /* illegal, check against buffer overflow */
881
882 j = scan[i];
883
884 vl_vlc_dumpbits(&bs->vlc, 12);
885 vl_vlc_needbits(&bs->vlc);
886 val = (vl_vlc_sbits(&bs->vlc, 12) * quantizer_scale * quant_matrix[j]) / 16;
887
888 SATURATE (val);
889 dest[i] = val;
890 mismatch ^= val;
891
892 vl_vlc_dumpbits(&bs->vlc, 12);
893 vl_vlc_needbits(&bs->vlc);
894
895 continue;
896
897 }
898 } else if (bs->vlc.buf >= 0x02000000) {
899 tab = DCT_B15_10 + (vl_vlc_ubits(&bs->vlc, 10) - 8);
900 i += tab->run;
901 if (i < 64)
902 goto normal_code;
903 } else if (bs->vlc.buf >= 0x00800000) {
904 tab = DCT_13 + (vl_vlc_ubits(&bs->vlc, 13) - 16);
905 i += tab->run;
906 if (i < 64)
907 goto normal_code;
908 } else if (bs->vlc.buf >= 0x00200000) {
909 tab = DCT_15 + (vl_vlc_ubits(&bs->vlc, 15) - 16);
910 i += tab->run;
911 if (i < 64)
912 goto normal_code;
913 } else {
914 tab = DCT_16 + vl_vlc_ubits(&bs->vlc, 16);
915 bs->vlc.buf <<= 16;
916 vl_vlc_getword(&bs->vlc, bs->vlc.bits + 16);
917 i += tab->run;
918 if (i < 64)
919 goto normal_code;
920 }
921 break; /* illegal, check needed to avoid buffer overflow */
922 }
923
924 dest[63] ^= mismatch & 1;
925 vl_vlc_dumpbits(&bs->vlc, 4); /* dump end of block code */
926 }
927
928 static inline void
929 get_non_intra_block(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture,
930 const int scan[64], int quantizer_scale, short *dest)
931 {
932 int i, j, val;
933 uint8_t *quant_matrix = picture->non_intra_quantizer_matrix;
934 int mismatch;
935 const DCTtab *tab;
936
937 i = -1;
938 mismatch = 1;
939
940 vl_vlc_needbits(&bs->vlc);
941 if (bs->vlc.buf >= 0x28000000) {
942 tab = DCT_B14DC_5 + (vl_vlc_ubits(&bs->vlc, 5) - 5);
943 goto entry_1;
944 } else
945 goto entry_2;
946
947 while (1) {
948 if (bs->vlc.buf >= 0x28000000) {
949
950 tab = DCT_B14AC_5 + (vl_vlc_ubits(&bs->vlc, 5) - 5);
951
952 entry_1:
953 i += tab->run;
954 if (i >= 64)
955 break; /* end of block */
956
957 normal_code:
958 j = scan[i];
959 bs->vlc.buf <<= tab->len;
960 bs->vlc.bits += tab->len + 1;
961 val = ((2*tab->level+1) * quantizer_scale * quant_matrix[j]) >> 5;
962
963 /* if (bitstream_get (1)) val = -val; */
964 val = (val ^ vl_vlc_sbits(&bs->vlc, 1)) - vl_vlc_sbits(&bs->vlc, 1);
965
966 SATURATE (val);
967 dest[i] = val;
968 mismatch ^= val;
969
970 bs->vlc.buf <<= 1;
971 vl_vlc_needbits(&bs->vlc);
972
973 continue;
974
975 }
976
977 entry_2:
978 if (bs->vlc.buf >= 0x04000000) {
979
980 tab = DCT_B14_8 + (vl_vlc_ubits(&bs->vlc, 8) - 4);
981
982 i += tab->run;
983 if (i < 64)
984 goto normal_code;
985
986 /* escape code */
987
988 i += UBITS(bs->vlc.buf << 6, 6) - 64;
989 if (i >= 64)
990 break; /* illegal, check needed to avoid buffer overflow */
991
992 j = scan[i];
993
994 vl_vlc_dumpbits(&bs->vlc, 12);
995 vl_vlc_needbits(&bs->vlc);
996 val = 2 * (vl_vlc_sbits(&bs->vlc, 12) + vl_vlc_sbits(&bs->vlc, 1)) + 1;
997 val = (val * quantizer_scale * quant_matrix[j]) / 32;
998
999 SATURATE (val);
1000 dest[i] = val;
1001 mismatch ^= val;
1002
1003 vl_vlc_dumpbits(&bs->vlc, 12);
1004 vl_vlc_needbits(&bs->vlc);
1005
1006 continue;
1007
1008 } else if (bs->vlc.buf >= 0x02000000) {
1009 tab = DCT_B14_10 + (vl_vlc_ubits(&bs->vlc, 10) - 8);
1010 i += tab->run;
1011 if (i < 64)
1012 goto normal_code;
1013 } else if (bs->vlc.buf >= 0x00800000) {
1014 tab = DCT_13 + (vl_vlc_ubits(&bs->vlc, 13) - 16);
1015 i += tab->run;
1016 if (i < 64)
1017 goto normal_code;
1018 } else if (bs->vlc.buf >= 0x00200000) {
1019 tab = DCT_15 + (vl_vlc_ubits(&bs->vlc, 15) - 16);
1020 i += tab->run;
1021 if (i < 64)
1022 goto normal_code;
1023 } else {
1024 tab = DCT_16 + vl_vlc_ubits(&bs->vlc, 16);
1025 bs->vlc.buf <<= 16;
1026 vl_vlc_getword(&bs->vlc, bs->vlc.bits + 16);
1027 i += tab->run;
1028 if (i < 64)
1029 goto normal_code;
1030 }
1031 break; /* illegal, check needed to avoid buffer overflow */
1032 }
1033 dest[63] ^= mismatch & 1;
1034 vl_vlc_dumpbits(&bs->vlc, 2); /* dump end of block code */
1035 }
1036
1037 static inline void
1038 get_mpeg1_intra_block(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture,
1039 const int scan[64], int quantizer_scale, short *dest)
1040 {
1041 int i, j, val;
1042 uint8_t *quant_matrix = picture->intra_quantizer_matrix;
1043 const DCTtab * tab;
1044
1045 i = 0;
1046
1047 vl_vlc_needbits(&bs->vlc);
1048
1049 while (1) {
1050 if (bs->vlc.buf >= 0x28000000) {
1051
1052 tab = DCT_B14AC_5 + (vl_vlc_ubits(&bs->vlc, 5) - 5);
1053
1054 i += tab->run;
1055 if (i >= 64)
1056 break; /* end of block */
1057
1058 normal_code:
1059 j = scan[i];
1060 bs->vlc.buf <<= tab->len;
1061 bs->vlc.bits += tab->len + 1;
1062 val = (tab->level * quantizer_scale * quant_matrix[j]) >> 4;
1063
1064 /* oddification */
1065 val = (val - 1) | 1;
1066
1067 /* if (bitstream_get (1)) val = -val; */
1068 val = (val ^ vl_vlc_sbits(&bs->vlc, 1)) - vl_vlc_sbits(&bs->vlc, 1);
1069
1070 SATURATE (val);
1071 dest[i] = val;
1072
1073 bs->vlc.buf <<= 1;
1074 vl_vlc_needbits(&bs->vlc);
1075
1076 continue;
1077
1078 } else if (bs->vlc.buf >= 0x04000000) {
1079
1080 tab = DCT_B14_8 + (vl_vlc_ubits(&bs->vlc, 8) - 4);
1081
1082 i += tab->run;
1083 if (i < 64)
1084 goto normal_code;
1085
1086 /* escape code */
1087
1088 i += UBITS(bs->vlc.buf << 6, 6) - 64;
1089 if (i >= 64)
1090 break; /* illegal, check needed to avoid buffer overflow */
1091
1092 j = scan[i];
1093
1094 vl_vlc_dumpbits(&bs->vlc, 12);
1095 vl_vlc_needbits(&bs->vlc);
1096 val = vl_vlc_sbits(&bs->vlc, 8);
1097 if (! (val & 0x7f)) {
1098 vl_vlc_dumpbits(&bs->vlc, 8);
1099 val = vl_vlc_ubits(&bs->vlc, 8) + 2 * val;
1100 }
1101 val = (val * quantizer_scale * quant_matrix[j]) / 16;
1102
1103 /* oddification */
1104 val = (val + ~SBITS (val, 1)) | 1;
1105
1106 SATURATE (val);
1107 dest[i] = val;
1108
1109 vl_vlc_dumpbits(&bs->vlc, 8);
1110 vl_vlc_needbits(&bs->vlc);
1111
1112 continue;
1113
1114 } else if (bs->vlc.buf >= 0x02000000) {
1115 tab = DCT_B14_10 + (vl_vlc_ubits(&bs->vlc, 10) - 8);
1116 i += tab->run;
1117 if (i < 64)
1118 goto normal_code;
1119 } else if (bs->vlc.buf >= 0x00800000) {
1120 tab = DCT_13 + (vl_vlc_ubits(&bs->vlc, 13) - 16);
1121 i += tab->run;
1122 if (i < 64)
1123 goto normal_code;
1124 } else if (bs->vlc.buf >= 0x00200000) {
1125 tab = DCT_15 + (vl_vlc_ubits(&bs->vlc, 15) - 16);
1126 i += tab->run;
1127 if (i < 64)
1128 goto normal_code;
1129 } else {
1130 tab = DCT_16 + vl_vlc_ubits(&bs->vlc, 16);
1131 bs->vlc.buf <<= 16;
1132 vl_vlc_getword(&bs->vlc, bs->vlc.bits + 16);
1133 i += tab->run;
1134 if (i < 64)
1135 goto normal_code;
1136 }
1137 break; /* illegal, check needed to avoid buffer overflow */
1138 }
1139 vl_vlc_dumpbits(&bs->vlc, 2); /* dump end of block code */
1140 }
1141
1142 static inline void
1143 get_mpeg1_non_intra_block(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture,
1144 const int scan[64], int quantizer_scale, short *dest)
1145 {
1146 int i, j, val;
1147 uint8_t *quant_matrix = picture->non_intra_quantizer_matrix;
1148 const DCTtab * tab;
1149
1150 i = -1;
1151
1152 vl_vlc_needbits(&bs->vlc);
1153 if (bs->vlc.buf >= 0x28000000) {
1154 tab = DCT_B14DC_5 + (vl_vlc_ubits(&bs->vlc, 5) - 5);
1155 goto entry_1;
1156 } else
1157 goto entry_2;
1158
1159 while (1) {
1160 if (bs->vlc.buf >= 0x28000000) {
1161
1162 tab = DCT_B14AC_5 + (vl_vlc_ubits(&bs->vlc, 5) - 5);
1163
1164 entry_1:
1165 i += tab->run;
1166 if (i >= 64)
1167 break; /* end of block */
1168
1169 normal_code:
1170 j = scan[i];
1171 bs->vlc.buf <<= tab->len;
1172 bs->vlc.bits += tab->len + 1;
1173 val = ((2*tab->level+1) * quantizer_scale * quant_matrix[j]) >> 5;
1174
1175 /* oddification */
1176 val = (val - 1) | 1;
1177
1178 /* if (bitstream_get (1)) val = -val; */
1179 val = (val ^ vl_vlc_sbits(&bs->vlc, 1)) - vl_vlc_sbits(&bs->vlc, 1);
1180
1181 SATURATE (val);
1182 dest[i] = val;
1183
1184 bs->vlc.buf <<= 1;
1185 vl_vlc_needbits(&bs->vlc);
1186
1187 continue;
1188
1189 }
1190
1191 entry_2:
1192 if (bs->vlc.buf >= 0x04000000) {
1193
1194 tab = DCT_B14_8 + (vl_vlc_ubits(&bs->vlc, 8) - 4);
1195
1196 i += tab->run;
1197 if (i < 64)
1198 goto normal_code;
1199
1200 /* escape code */
1201
1202 i += UBITS(bs->vlc.buf << 6, 6) - 64;
1203 if (i >= 64)
1204 break; /* illegal, check needed to avoid buffer overflow */
1205
1206 j = scan[i];
1207
1208 vl_vlc_dumpbits(&bs->vlc, 12);
1209 vl_vlc_needbits(&bs->vlc);
1210 val = vl_vlc_sbits(&bs->vlc, 8);
1211 if (! (val & 0x7f)) {
1212 vl_vlc_dumpbits(&bs->vlc, 8);
1213 val = vl_vlc_ubits(&bs->vlc, 8) + 2 * val;
1214 }
1215 val = 2 * (val + SBITS (val, 1)) + 1;
1216 val = (val * quantizer_scale * quant_matrix[j]) / 32;
1217
1218 /* oddification */
1219 val = (val + ~SBITS (val, 1)) | 1;
1220
1221 SATURATE (val);
1222 dest[i] = val;
1223
1224 vl_vlc_dumpbits(&bs->vlc, 8);
1225 vl_vlc_needbits(&bs->vlc);
1226
1227 continue;
1228
1229 } else if (bs->vlc.buf >= 0x02000000) {
1230 tab = DCT_B14_10 + (vl_vlc_ubits(&bs->vlc, 10) - 8);
1231 i += tab->run;
1232 if (i < 64)
1233 goto normal_code;
1234 } else if (bs->vlc.buf >= 0x00800000) {
1235 tab = DCT_13 + (vl_vlc_ubits(&bs->vlc, 13) - 16);
1236 i += tab->run;
1237 if (i < 64)
1238 goto normal_code;
1239 } else if (bs->vlc.buf >= 0x00200000) {
1240 tab = DCT_15 + (vl_vlc_ubits(&bs->vlc, 15) - 16);
1241 i += tab->run;
1242 if (i < 64)
1243 goto normal_code;
1244 } else {
1245 tab = DCT_16 + vl_vlc_ubits(&bs->vlc, 16);
1246 bs->vlc.buf <<= 16;
1247 vl_vlc_getword(&bs->vlc, bs->vlc.bits + 16);
1248 i += tab->run;
1249 if (i < 64)
1250 goto normal_code;
1251 }
1252 break; /* illegal, check needed to avoid buffer overflow */
1253 }
1254 vl_vlc_dumpbits(&bs->vlc, 2); /* dump end of block code */
1255 }
1256
1257 static inline void
1258 slice_intra_DCT(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture, const int scan[64], int cc,
1259 unsigned x, unsigned y, enum pipe_mpeg12_dct_type coding, int quantizer_scale, int dc_dct_pred[3])
1260 {
1261 short *dest = bs->ycbcr_buffer[cc];
1262
1263 bs->ycbcr_stream[cc]->x = x;
1264 bs->ycbcr_stream[cc]->y = y;
1265 bs->ycbcr_stream[cc]->intra = PIPE_MPEG12_DCT_INTRA;
1266 bs->ycbcr_stream[cc]->coding = coding;
1267
1268 vl_vlc_needbits(&bs->vlc);
1269
1270 /* Get the intra DC coefficient and inverse quantize it */
1271 if (cc == 0)
1272 dc_dct_pred[0] += get_luma_dc_dct_diff(bs);
1273 else
1274 dc_dct_pred[cc] += get_chroma_dc_dct_diff(bs);
1275
1276 memset(dest, 0, sizeof(int16_t) * 64);
1277 dest[0] = dc_dct_pred[cc] << (3 - picture->intra_dc_precision);
1278 if (picture->mpeg1) {
1279 if (picture->picture_coding_type != D_TYPE)
1280 get_mpeg1_intra_block(bs, picture, scan, quantizer_scale, dest);
1281 } else if (picture->intra_vlc_format)
1282 get_intra_block_B15(bs, picture, scan, quantizer_scale, dest);
1283 else
1284 get_intra_block_B14(bs, picture, scan, quantizer_scale, dest);
1285
1286 bs->num_ycbcr_blocks[cc]++;
1287 bs->ycbcr_stream[cc]++;
1288 bs->ycbcr_buffer[cc] += 64;
1289 }
1290
1291 static inline void
1292 slice_non_intra_DCT(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture, const int scan[64], int cc,
1293 unsigned x, unsigned y, int quantizer_scale, enum pipe_mpeg12_dct_type coding)
1294 {
1295 short *dest = bs->ycbcr_buffer[cc];
1296
1297 bs->ycbcr_stream[cc]->x = x;
1298 bs->ycbcr_stream[cc]->y = y;
1299 bs->ycbcr_stream[cc]->intra = PIPE_MPEG12_DCT_DELTA;
1300 bs->ycbcr_stream[cc]->coding = coding;
1301
1302 memset(dest, 0, sizeof(int16_t) * 64);
1303 if (picture->mpeg1)
1304 get_mpeg1_non_intra_block(bs, picture, scan, quantizer_scale, dest);
1305 else
1306 get_non_intra_block(bs, picture, scan, quantizer_scale, dest);
1307
1308 bs->num_ycbcr_blocks[cc]++;
1309 bs->ycbcr_stream[cc]++;
1310 bs->ycbcr_buffer[cc] += 64;
1311 }
1312
1313 static inline void
1314 motion_mp1(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv)
1315 {
1316 int motion_x, motion_y;
1317
1318 mv->top.field_select = mv->bottom.field_select = PIPE_VIDEO_FRAME;
1319
1320 vl_vlc_needbits(&bs->vlc);
1321 motion_x = (mv->top.x + (get_motion_delta(bs, f_code[0]) << f_code[1]));
1322 motion_x = bound_motion_vector (motion_x, f_code[0] + f_code[1]);
1323 mv->top.x = mv->bottom.x = motion_x;
1324
1325 vl_vlc_needbits(&bs->vlc);
1326 motion_y = (mv->top.y + (get_motion_delta(bs, f_code[0]) << f_code[1]));
1327 motion_y = bound_motion_vector (motion_y, f_code[0] + f_code[1]);
1328 mv->top.y = mv->bottom.y = motion_y;
1329 }
1330
1331 static inline void
1332 motion_fr_frame(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv)
1333 {
1334 int motion_x, motion_y;
1335
1336 mv->top.field_select = mv->bottom.field_select = PIPE_VIDEO_FRAME;
1337
1338 vl_vlc_needbits(&bs->vlc);
1339 motion_x = mv->top.x + get_motion_delta(bs, f_code[0]);
1340 motion_x = bound_motion_vector(motion_x, f_code[0]);
1341 mv->top.x = mv->bottom.x = motion_x;
1342
1343 vl_vlc_needbits(&bs->vlc);
1344 motion_y = mv->top.y + get_motion_delta(bs, f_code[1]);
1345 motion_y = bound_motion_vector(motion_y, f_code[1]);
1346 mv->top.y = mv->bottom.y = motion_y;
1347 }
1348
1349 static inline void
1350 motion_fr_field(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv)
1351 {
1352 int motion_x, motion_y;
1353
1354 vl_vlc_needbits(&bs->vlc);
1355 mv->top.field_select = vl_vlc_ubits(&bs->vlc, 1) ?
1356 PIPE_VIDEO_BOTTOM_FIELD : PIPE_VIDEO_TOP_FIELD;
1357 vl_vlc_dumpbits(&bs->vlc, 1);
1358
1359 motion_x = mv->top.x + get_motion_delta(bs, f_code[0]);
1360 motion_x = bound_motion_vector (motion_x, f_code[0]);
1361 mv->top.x = motion_x;
1362
1363 vl_vlc_needbits(&bs->vlc);
1364 motion_y = (mv->top.y >> 1) + get_motion_delta(bs, f_code[1]);
1365 /* motion_y = bound_motion_vector (motion_y, f_code[1]); */
1366 mv->top.y = motion_y << 1;
1367
1368 vl_vlc_needbits(&bs->vlc);
1369 mv->bottom.field_select = vl_vlc_ubits(&bs->vlc, 1) ?
1370 PIPE_VIDEO_BOTTOM_FIELD : PIPE_VIDEO_TOP_FIELD;
1371 vl_vlc_dumpbits(&bs->vlc, 1);
1372
1373 motion_x = mv->bottom.x + get_motion_delta(bs, f_code[0]);
1374 motion_x = bound_motion_vector (motion_x, f_code[0]);
1375 mv->bottom.x = motion_x;
1376
1377 vl_vlc_needbits(&bs->vlc);
1378 motion_y = (mv->bottom.y >> 1) + get_motion_delta(bs, f_code[1]);
1379 /* motion_y = bound_motion_vector (motion_y, f_code[1]); */
1380 mv->bottom.y = motion_y << 1;
1381 }
1382
1383 static inline void
1384 motion_fr_dmv(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv)
1385 {
1386 int motion_x, motion_y;
1387
1388 // TODO Implement dmv
1389 mv->top.field_select = mv->bottom.field_select = PIPE_VIDEO_FRAME;
1390
1391 vl_vlc_needbits(&bs->vlc);
1392 motion_x = mv->top.x + get_motion_delta(bs, f_code[0]);
1393 motion_x = bound_motion_vector(motion_x, f_code[0]);
1394 mv->top.x = mv->bottom.x = motion_x;
1395
1396 vl_vlc_needbits(&bs->vlc);
1397 motion_y = (mv->top.y >> 1) + get_motion_delta(bs, f_code[1]);
1398 /* motion_y = bound_motion_vector (motion_y, f_code[1]); */
1399 mv->top.y = mv->bottom.y = motion_y << 1;
1400 }
1401
1402 /* like motion_frame, but parsing without actual motion compensation */
1403 static inline void
1404 motion_fr_conceal(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv)
1405 {
1406 int tmp;
1407
1408 mv->top.field_select = mv->bottom.field_select = PIPE_VIDEO_FRAME;
1409
1410 vl_vlc_needbits(&bs->vlc);
1411 tmp = (mv->top.x + get_motion_delta(bs, f_code[0]));
1412 tmp = bound_motion_vector (tmp, f_code[0]);
1413 mv->top.x = mv->bottom.x = tmp;
1414
1415 vl_vlc_needbits(&bs->vlc);
1416 tmp = (mv->top.y + get_motion_delta(bs, f_code[1]));
1417 tmp = bound_motion_vector (tmp, f_code[1]);
1418 mv->top.y = mv->bottom.y = tmp;
1419
1420 vl_vlc_dumpbits(&bs->vlc, 1); /* remove marker_bit */
1421 }
1422
1423 static inline void
1424 motion_fi_field(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv)
1425 {
1426 int motion_x, motion_y;
1427
1428 vl_vlc_needbits(&bs->vlc);
1429
1430 // ref_field
1431 //vl_vlc_ubits(&bs->vlc, 1);
1432
1433 // TODO field select may need to do something here for bob (weave ok)
1434 mv->top.field_select = mv->bottom.field_select = PIPE_VIDEO_FRAME;
1435 vl_vlc_dumpbits(&bs->vlc, 1);
1436
1437 motion_x = mv->top.x + get_motion_delta(bs, f_code[0]);
1438 motion_x = bound_motion_vector (motion_x, f_code[0]);
1439 mv->top.x = mv->bottom.x = motion_x;
1440
1441 vl_vlc_needbits(&bs->vlc);
1442 motion_y = mv->top.y + get_motion_delta(bs, f_code[1]);
1443 motion_y = bound_motion_vector (motion_y, f_code[1]);
1444 mv->top.y = mv->bottom.y = motion_y;
1445 }
1446
1447 static inline void
1448 motion_fi_16x8(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv)
1449 {
1450 int motion_x, motion_y;
1451
1452 vl_vlc_needbits(&bs->vlc);
1453
1454 // ref_field
1455 //vl_vlc_ubits(&bs->vlc, 1);
1456
1457 // TODO field select may need to do something here bob (weave ok)
1458 mv->top.field_select = PIPE_VIDEO_FRAME;
1459 vl_vlc_dumpbits(&bs->vlc, 1);
1460
1461 motion_x = mv->top.x + get_motion_delta(bs, f_code[0]);
1462 motion_x = bound_motion_vector (motion_x, f_code[0]);
1463 mv->top.x = motion_x;
1464
1465 vl_vlc_needbits(&bs->vlc);
1466 motion_y = mv->top.y + get_motion_delta(bs, f_code[1]);
1467 motion_y = bound_motion_vector (motion_y, f_code[1]);
1468 mv->top.y = motion_y;
1469
1470 vl_vlc_needbits(&bs->vlc);
1471 // ref_field
1472 //vl_vlc_ubits(&bs->vlc, 1);
1473
1474 // TODO field select may need to do something here for bob (weave ok)
1475 mv->bottom.field_select = PIPE_VIDEO_FRAME;
1476 vl_vlc_dumpbits(&bs->vlc, 1);
1477
1478 motion_x = mv->bottom.x + get_motion_delta(bs, f_code[0]);
1479 motion_x = bound_motion_vector (motion_x, f_code[0]);
1480 mv->bottom.x = motion_x;
1481
1482 vl_vlc_needbits(&bs->vlc);
1483 motion_y = mv->bottom.y + get_motion_delta(bs, f_code[1]);
1484 motion_y = bound_motion_vector (motion_y, f_code[1]);
1485 mv->bottom.y = motion_y;
1486 }
1487
1488 static inline void
1489 motion_fi_dmv(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv)
1490 {
1491 int motion_x, motion_y;
1492
1493 // TODO field select may need to do something here for bob (weave ok)
1494 mv->top.field_select = mv->bottom.field_select = PIPE_VIDEO_FRAME;
1495
1496 vl_vlc_needbits(&bs->vlc);
1497 motion_x = mv->top.x + get_motion_delta(bs, f_code[0]);
1498 motion_x = bound_motion_vector (motion_x, f_code[0]);
1499 mv->top.x = mv->bottom.x = motion_x;
1500
1501 vl_vlc_needbits(&bs->vlc);
1502 motion_y = mv->top.y + get_motion_delta(bs, f_code[1]);
1503 motion_y = bound_motion_vector (motion_y, f_code[1]);
1504 mv->top.y = mv->bottom.y = motion_y;
1505 }
1506
1507
1508 static inline void
1509 motion_fi_conceal(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv)
1510 {
1511 int tmp;
1512
1513 vl_vlc_needbits(&bs->vlc);
1514 vl_vlc_dumpbits(&bs->vlc, 1); /* remove field_select */
1515
1516 tmp = (mv->top.x + get_motion_delta(bs, f_code[0]));
1517 tmp = bound_motion_vector(tmp, f_code[0]);
1518 mv->top.x = mv->bottom.x = tmp;
1519
1520 vl_vlc_needbits(&bs->vlc);
1521 tmp = (mv->top.y + get_motion_delta(bs, f_code[1]));
1522 tmp = bound_motion_vector(tmp, f_code[1]);
1523 mv->top.y = mv->bottom.y = tmp;
1524
1525 vl_vlc_dumpbits(&bs->vlc, 1); /* remove marker_bit */
1526 }
1527
1528 #define MOTION_CALL(routine, macroblock_modes) \
1529 do { \
1530 if ((macroblock_modes) & MACROBLOCK_MOTION_FORWARD) \
1531 routine(bs, picture->f_code[0], &mv_fwd); \
1532 if ((macroblock_modes) & MACROBLOCK_MOTION_BACKWARD) \
1533 routine(bs, picture->f_code[1], &mv_bwd); \
1534 } while (0)
1535
1536 #define NEXT_MACROBLOCK \
1537 do { \
1538 bs->mv_stream[0][x+y*bs->width] = mv_fwd; \
1539 bs->mv_stream[1][x+y*bs->width] = mv_bwd; \
1540 ++x; \
1541 if (x == bs->width) { \
1542 ++y; \
1543 if (y >= bs->height) \
1544 return false; \
1545 x = 0; \
1546 } \
1547 } while (0)
1548
1549 static inline bool
1550 slice_init(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture,
1551 int *quantizer_scale, int *x, int *y)
1552 {
1553 const MBAtab * mba;
1554
1555 vl_vlc_need32bits(&bs->vlc);
1556 while(bs->vlc.buf < 0x101 || bs->vlc.buf > 0x1AF) {
1557 if(!vl_vlc_getbyte(&bs->vlc))
1558 return false;
1559 }
1560 *y = (bs->vlc.buf & 0xFF) - 1;
1561 vl_vlc_restart(&bs->vlc);
1562
1563 *quantizer_scale = get_quantizer_scale(bs, picture);
1564
1565 /* ignore intra_slice and all the extra data */
1566 while (bs->vlc.buf & 0x80000000) {
1567 vl_vlc_dumpbits(&bs->vlc, 9);
1568 vl_vlc_needbits(&bs->vlc);
1569 }
1570
1571 /* decode initial macroblock address increment */
1572 *x = 0;
1573 while (1) {
1574 if (bs->vlc.buf >= 0x08000000) {
1575 mba = MBA_5 + (vl_vlc_ubits(&bs->vlc, 6) - 2);
1576 break;
1577 } else if (bs->vlc.buf >= 0x01800000) {
1578 mba = MBA_11 + (vl_vlc_ubits(&bs->vlc, 12) - 24);
1579 break;
1580 } else switch (vl_vlc_ubits(&bs->vlc, 12)) {
1581 case 8: /* macroblock_escape */
1582 *x += 33;
1583 vl_vlc_dumpbits(&bs->vlc, 11);
1584 vl_vlc_needbits(&bs->vlc);
1585 continue;
1586 case 15: /* macroblock_stuffing (MPEG1 only) */
1587 bs->vlc.buf &= 0xfffff;
1588 vl_vlc_dumpbits(&bs->vlc, 11);
1589 vl_vlc_needbits(&bs->vlc);
1590 continue;
1591 default: /* error */
1592 return false;
1593 }
1594 }
1595 vl_vlc_dumpbits(&bs->vlc, mba->len + 1);
1596 *x += mba->mba;
1597
1598 while (*x >= bs->width) {
1599 *x -= bs->width;
1600 (*y)++;
1601 }
1602 if (*y > bs->height)
1603 return false;
1604
1605 return true;
1606 }
1607
1608 static inline bool
1609 decode_slice(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc *picture, const int scan[64])
1610 {
1611 struct pipe_motionvector mv_fwd, mv_bwd;
1612 enum pipe_mpeg12_dct_type dct_type;
1613
1614 /* predictor for DC coefficients in intra blocks */
1615 int dc_dct_pred[3] = { 0, 0, 0 };
1616 int quantizer_scale;
1617
1618 int x, y;
1619
1620 if (!slice_init(bs, picture, &quantizer_scale, &x, &y))
1621 return false;
1622
1623 mv_fwd.top.x = mv_fwd.top.y = mv_fwd.bottom.x = mv_fwd.bottom.y = 0;
1624 mv_fwd.top.field_select = mv_fwd.bottom.field_select = PIPE_VIDEO_FRAME;
1625
1626 mv_bwd.top.x = mv_bwd.top.y = mv_bwd.bottom.x = mv_bwd.bottom.y = 0;
1627 mv_bwd.top.field_select = mv_bwd.bottom.field_select = PIPE_VIDEO_FRAME;
1628
1629 while (1) {
1630 int macroblock_modes;
1631 int mba_inc;
1632 const MBAtab * mba;
1633
1634 vl_vlc_needbits(&bs->vlc);
1635
1636 macroblock_modes = get_macroblock_modes(bs, picture); //macroblock_modes()
1637 dct_type = macroblock_modes & DCT_TYPE_INTERLACED ?
1638 PIPE_MPEG12_DCT_TYPE_FIELD : PIPE_MPEG12_DCT_TYPE_FRAME;
1639
1640 switch(macroblock_modes & (MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD)) {
1641 case (MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD):
1642 mv_fwd.top.weight = mv_fwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_HALF;
1643 mv_bwd.top.weight = mv_bwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_HALF;
1644 break;
1645
1646 default:
1647 case MACROBLOCK_MOTION_FORWARD:
1648 mv_fwd.top.weight = mv_fwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MAX;
1649 mv_bwd.top.weight = mv_bwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MIN;
1650 break;
1651
1652 case MACROBLOCK_MOTION_BACKWARD:
1653 mv_fwd.top.weight = mv_fwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MIN;
1654 mv_bwd.top.weight = mv_bwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MAX;
1655 break;
1656 }
1657
1658 /* maybe integrate MACROBLOCK_QUANT test into get_macroblock_modes ? */
1659 if (macroblock_modes & MACROBLOCK_QUANT)
1660 quantizer_scale = get_quantizer_scale(bs, picture);
1661
1662 if (macroblock_modes & MACROBLOCK_INTRA) {
1663
1664 if (picture->concealment_motion_vectors) {
1665 if (picture->picture_structure == FRAME_PICTURE)
1666 motion_fr_conceal(bs, picture->f_code[0], &mv_fwd);
1667 else
1668 motion_fi_conceal(bs, picture->f_code[0], &mv_fwd);
1669
1670 } else {
1671 mv_fwd.top.x = mv_fwd.top.y = mv_fwd.bottom.x = mv_fwd.bottom.y = 0;
1672 mv_bwd.top.x = mv_bwd.top.y = mv_bwd.bottom.x = mv_bwd.bottom.y = 0;
1673 }
1674 mv_fwd.top.weight = mv_fwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MIN;
1675 mv_bwd.top.weight = mv_bwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MIN;
1676
1677 // unravaled loop of 6 block(i) calls in macroblock()
1678 slice_intra_DCT(bs, picture, scan, 0, x*2+0, y*2+0, dct_type, quantizer_scale, dc_dct_pred);
1679 slice_intra_DCT(bs, picture, scan, 0, x*2+1, y*2+0, dct_type, quantizer_scale, dc_dct_pred);
1680 slice_intra_DCT(bs, picture, scan, 0, x*2+0, y*2+1, dct_type, quantizer_scale, dc_dct_pred);
1681 slice_intra_DCT(bs, picture, scan, 0, x*2+1, y*2+1, dct_type, quantizer_scale, dc_dct_pred);
1682 slice_intra_DCT(bs, picture, scan, 1, x, y, dct_type, quantizer_scale, dc_dct_pred);
1683 slice_intra_DCT(bs, picture, scan, 2, x, y, dct_type, quantizer_scale, dc_dct_pred);
1684
1685 if (picture->picture_coding_type == D_TYPE) {
1686 vl_vlc_needbits(&bs->vlc);
1687 vl_vlc_dumpbits(&bs->vlc, 1);
1688 }
1689
1690 } else {
1691 if (picture->picture_structure == FRAME_PICTURE)
1692 switch (macroblock_modes & MOTION_TYPE_MASK) {
1693 case MC_FRAME:
1694 if (picture->mpeg1) {
1695 MOTION_CALL(motion_mp1, macroblock_modes);
1696 } else {
1697 MOTION_CALL(motion_fr_frame, macroblock_modes);
1698 }
1699 break;
1700
1701 case MC_FIELD:
1702 MOTION_CALL (motion_fr_field, macroblock_modes);
1703 break;
1704
1705 case MC_DMV:
1706 MOTION_CALL (motion_fr_dmv, MACROBLOCK_MOTION_FORWARD);
1707 break;
1708
1709 case 0:
1710 /* non-intra mb without forward mv in a P picture */
1711 mv_fwd.top.x = mv_fwd.top.y = mv_fwd.bottom.x = mv_fwd.bottom.y = 0;
1712 mv_bwd.top.x = mv_bwd.top.y = mv_bwd.bottom.x = mv_bwd.bottom.y = 0;
1713 break;
1714 }
1715 else
1716 switch (macroblock_modes & MOTION_TYPE_MASK) {
1717 case MC_FIELD:
1718 MOTION_CALL (motion_fi_field, macroblock_modes);
1719 break;
1720
1721 case MC_16X8:
1722 MOTION_CALL (motion_fi_16x8, macroblock_modes);
1723 break;
1724
1725 case MC_DMV:
1726 MOTION_CALL (motion_fi_dmv, MACROBLOCK_MOTION_FORWARD);
1727 break;
1728
1729 case 0:
1730 /* non-intra mb without forward mv in a P picture */
1731 mv_fwd.top.x = mv_fwd.top.y = mv_fwd.bottom.x = mv_fwd.bottom.y = 0;
1732 mv_bwd.top.x = mv_bwd.top.y = mv_bwd.bottom.x = mv_bwd.bottom.y = 0;
1733 break;
1734 }
1735
1736 if (macroblock_modes & MACROBLOCK_PATTERN) {
1737 int coded_block_pattern = get_coded_block_pattern(bs);
1738
1739 // TODO optimize not fully used for idct accel only mc.
1740 if (coded_block_pattern & 0x20)
1741 slice_non_intra_DCT(bs, picture, scan, 0, x*2+0, y*2+0, quantizer_scale, dct_type); // cc0 luma 0
1742 if (coded_block_pattern & 0x10)
1743 slice_non_intra_DCT(bs, picture, scan, 0, x*2+1, y*2+0, quantizer_scale, dct_type); // cc0 luma 1
1744 if (coded_block_pattern & 0x08)
1745 slice_non_intra_DCT(bs, picture, scan, 0, x*2+0, y*2+1, quantizer_scale, dct_type); // cc0 luma 2
1746 if (coded_block_pattern & 0x04)
1747 slice_non_intra_DCT(bs, picture, scan, 0, x*2+1, y*2+1, quantizer_scale, dct_type); // cc0 luma 3
1748 if (coded_block_pattern & 0x2)
1749 slice_non_intra_DCT(bs, picture, scan, 1, x, y, quantizer_scale, dct_type); // cc1 croma
1750 if (coded_block_pattern & 0x1)
1751 slice_non_intra_DCT(bs, picture, scan, 2, x, y, quantizer_scale, dct_type); // cc2 croma
1752 }
1753
1754 dc_dct_pred[0] = dc_dct_pred[1] = dc_dct_pred[2] = 0;
1755 }
1756
1757 NEXT_MACROBLOCK;
1758
1759 vl_vlc_needbits(&bs->vlc);
1760 mba_inc = 0;
1761 while (1) {
1762 if (bs->vlc.buf >= 0x10000000) {
1763 mba = MBA_5 + (vl_vlc_ubits(&bs->vlc, 5) - 2);
1764 break;
1765 } else if (bs->vlc.buf >= 0x03000000) {
1766 mba = MBA_11 + (vl_vlc_ubits(&bs->vlc, 11) - 24);
1767 break;
1768 } else switch (vl_vlc_ubits(&bs->vlc, 11)) {
1769 case 8: /* macroblock_escape */
1770 mba_inc += 33;
1771 /* pass through */
1772 case 15: /* macroblock_stuffing (MPEG1 only) */
1773 vl_vlc_dumpbits(&bs->vlc, 11);
1774 vl_vlc_needbits(&bs->vlc);
1775 continue;
1776 default: /* end of slice, or error */
1777 return true;
1778 }
1779 }
1780 vl_vlc_dumpbits(&bs->vlc, mba->len);
1781 mba_inc += mba->mba;
1782 if (mba_inc) {
1783 //TODO conversion to signed format signed format
1784 dc_dct_pred[0] = dc_dct_pred[1] = dc_dct_pred[2] = 0;
1785
1786 switch(picture->picture_structure) {
1787 case FRAME_PICTURE:
1788 mv_fwd.top.field_select = mv_fwd.bottom.field_select = PIPE_VIDEO_FRAME;
1789 mv_bwd.top.field_select = mv_bwd.bottom.field_select = PIPE_VIDEO_FRAME;
1790 break;
1791
1792 case TOP_FIELD:
1793 mv_fwd.top.field_select = mv_fwd.bottom.field_select = PIPE_VIDEO_TOP_FIELD;
1794 mv_bwd.top.field_select = mv_bwd.bottom.field_select = PIPE_VIDEO_TOP_FIELD;
1795 break;
1796
1797 case BOTTOM_FIELD:
1798 mv_fwd.top.field_select = mv_fwd.bottom.field_select = PIPE_VIDEO_BOTTOM_FIELD;
1799 mv_bwd.top.field_select = mv_bwd.bottom.field_select = PIPE_VIDEO_BOTTOM_FIELD;
1800 break;
1801 }
1802
1803 if (picture->picture_coding_type == P_TYPE) {
1804 mv_fwd.top.x = mv_fwd.top.y = mv_fwd.bottom.x = mv_fwd.bottom.y = 0;
1805 mv_fwd.top.weight = mv_fwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MAX;
1806 }
1807 do {
1808 NEXT_MACROBLOCK;
1809 } while (--mba_inc);
1810 }
1811 }
1812 }
1813
1814 void
1815 vl_mpg12_bs_init(struct vl_mpg12_bs *bs, unsigned width, unsigned height)
1816 {
1817 assert(bs);
1818
1819 memset(bs, 0, sizeof(struct vl_mpg12_bs));
1820
1821 bs->width = width;
1822 bs->height = height;
1823 }
1824
1825 void
1826 vl_mpg12_bs_set_buffers(struct vl_mpg12_bs *bs, struct pipe_ycbcr_block *ycbcr_stream[VL_MAX_PLANES],
1827 short *ycbcr_buffer[VL_MAX_PLANES], struct pipe_motionvector *mv_stream[VL_MAX_REF_FRAMES])
1828 {
1829 unsigned i;
1830
1831 assert(bs);
1832 assert(ycbcr_stream && ycbcr_buffer);
1833 assert(mv_stream);
1834
1835 for (i = 0; i < VL_MAX_PLANES; ++i) {
1836 bs->ycbcr_stream[i] = ycbcr_stream[i];
1837 bs->ycbcr_buffer[i] = ycbcr_buffer[i];
1838 }
1839 for (i = 0; i < VL_MAX_REF_FRAMES; ++i)
1840 bs->mv_stream[i] = mv_stream[i];
1841
1842 // TODO
1843 for (i = 0; i < bs->width*bs->height; ++i) {
1844 bs->mv_stream[0][i].top.x = bs->mv_stream[0][i].top.y = 0;
1845 bs->mv_stream[0][i].top.field_select = PIPE_VIDEO_FRAME;
1846 bs->mv_stream[0][i].top.weight = PIPE_VIDEO_MV_WEIGHT_MAX;
1847 bs->mv_stream[0][i].bottom.x = bs->mv_stream[0][i].bottom.y = 0;
1848 bs->mv_stream[0][i].bottom.field_select = PIPE_VIDEO_FRAME;
1849 bs->mv_stream[0][i].bottom.weight = PIPE_VIDEO_MV_WEIGHT_MAX;
1850
1851 bs->mv_stream[1][i].top.x = bs->mv_stream[1][i].top.y = 0;
1852 bs->mv_stream[1][i].top.field_select = PIPE_VIDEO_FRAME;
1853 bs->mv_stream[1][i].top.weight = PIPE_VIDEO_MV_WEIGHT_MIN;
1854 bs->mv_stream[1][i].bottom.x = bs->mv_stream[1][i].bottom.y = 0;
1855 bs->mv_stream[1][i].bottom.field_select = PIPE_VIDEO_FRAME;
1856 bs->mv_stream[1][i].bottom.weight = PIPE_VIDEO_MV_WEIGHT_MIN;
1857 }
1858 }
1859
1860 void
1861 vl_mpg12_bs_decode(struct vl_mpg12_bs *bs, unsigned num_bytes, const void *buffer,
1862 struct pipe_mpeg12_picture_desc *picture, unsigned num_ycbcr_blocks[3])
1863 {
1864 const int *scan;
1865
1866 assert(bs);
1867 assert(num_ycbcr_blocks);
1868 assert(buffer && num_bytes);
1869
1870 bs->num_ycbcr_blocks = num_ycbcr_blocks;
1871
1872 vl_vlc_init(&bs->vlc, buffer, num_bytes);
1873
1874 scan = picture->alternate_scan ? vl_zscan_alternate : vl_zscan_normal;
1875
1876 while(decode_slice(bs, picture, scan));
1877 }