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