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