1 /**************************************************************************
3 * Copyright 2011 Christian König.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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:
32 * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
33 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
35 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
36 * See http://libmpeg2.sourceforge.net/ for updates.
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.
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.
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
55 #include <pipe/p_video_state.h>
58 #include "vl_mpeg12_bitstream.h"
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)))
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)))
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
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)
81 /* picture structure */
83 #define BOTTOM_FIELD 2
84 #define FRAME_PICTURE 3
86 /* picture coding type (mpeg2 header) */
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
136 static const MBtab MB_I
[] = {
137 {INTRA
|QUANT
, 2}, {INTRA
, 1}
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}
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}
180 static const MVtab MV_4
[] = {
181 { 3, 6}, { 2, 4}, { 1, 3}, { 1, 3}, { 0, 2}, { 0, 2}, { 0, 2}, { 0, 2}
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}
193 static const DMVtab DMV_2
[] = {
194 { 0, 1}, { 0, 1}, { 1, 2}, {-1, 2}
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}
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}
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}
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}
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}
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}
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}
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}
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}
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}
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}
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}
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}
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}
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}
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}
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
461 get_macroblock_modes(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
)
463 int macroblock_modes
;
466 switch (picture
->picture_coding_type
) {
469 tab
= MB_I
+ vl_vlc_ubits(&bs
->vlc
, 1);
470 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
471 macroblock_modes
= tab
->modes
;
473 return macroblock_modes
;
477 tab
= MB_P
+ vl_vlc_ubits(&bs
->vlc
, 5);
478 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
479 macroblock_modes
= tab
->modes
;
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);
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
;
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);
496 return macroblock_modes
;
501 tab
= MB_B
+ vl_vlc_ubits(&bs
->vlc
, 6);
502 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
503 macroblock_modes
= tab
->modes
;
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);
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);
516 return macroblock_modes
;
520 vl_vlc_dumpbits(&bs
->vlc
, 1);
521 return MACROBLOCK_INTRA
;
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
)
531 enum pipe_mpeg12_dct_type dct_type
= PIPE_MPEG12_DCT_TYPE_FRAME
;
533 if ((picture
->picture_structure
== FRAME_PICTURE
) &&
534 (!picture
->frame_pred_frame_dct
) &&
535 (macroblock_modes
& (MACROBLOCK_INTRA
| MACROBLOCK_PATTERN
))) {
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);
544 get_quantizer_scale(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
)
546 int quantizer_scale_code
;
548 quantizer_scale_code
= vl_vlc_ubits(&bs
->vlc
, 5);
549 vl_vlc_dumpbits(&bs
->vlc
, 5);
551 if (picture
->q_scale_type
)
552 return non_linear_quantizer_scale
[quantizer_scale_code
];
554 return quantizer_scale_code
<< 1;
558 get_motion_delta(struct vl_mpg12_bs
*bs
, unsigned f_code
)
564 if (bs
->vlc
.buf
& 0x80000000) {
565 vl_vlc_dumpbits(&bs
->vlc
, 1);
567 } else if (bs
->vlc
.buf
>= 0x0c000000) {
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
;
574 sign
= vl_vlc_sbits(&bs
->vlc
, 1);
578 delta
+= vl_vlc_ubits(&bs
->vlc
, f_code
);
579 bs
->vlc
.buf
<<= f_code
;
581 return (delta
^ sign
) - sign
;
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
;
590 sign
= vl_vlc_sbits(&bs
->vlc
, 1);
594 vl_vlc_needbits(&bs
->vlc
);
595 delta
+= vl_vlc_ubits(&bs
->vlc
, f_code
);
596 vl_vlc_dumpbits(&bs
->vlc
, f_code
);
599 return (delta
^ sign
) - sign
;
604 bound_motion_vector(int vec
, unsigned f_code
)
610 limit
= 16 << f_code
;
612 if ((unsigned int)(vec
+ limit
) < 2 * limit
)
615 sign
= ((int32_t)vec
) >> 31;
616 return vec
- ((2 * limit
) ^ sign
) + sign
;
619 return ((int32_t)vec
<< (28 - f_code
)) >> (28 - f_code
);
624 get_dmv(struct vl_mpg12_bs
*bs
)
628 tab
= DMV_2
+ vl_vlc_ubits(&bs
->vlc
, 2);
629 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
634 get_coded_block_pattern(struct vl_mpg12_bs
*bs
)
638 vl_vlc_needbits(&bs
->vlc
);
640 if (bs
->vlc
.buf
>= 0x20000000) {
642 tab
= CBP_7
+ (vl_vlc_ubits(&bs
->vlc
, 7) - 16);
643 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
648 tab
= CBP_9
+ vl_vlc_ubits(&bs
->vlc
, 9);
649 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
655 get_luma_dc_dct_diff(struct vl_mpg12_bs
*bs
)
661 if (bs
->vlc
.buf
< 0xf8000000) {
662 tab
= DC_lum_5
+ vl_vlc_ubits(&bs
->vlc
, 5);
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
;
671 vl_vlc_dumpbits(&bs
->vlc
, 3);
675 tab
= DC_long
+ (vl_vlc_ubits(&bs
->vlc
, 9) - 0x1e0);
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
);
686 get_chroma_dc_dct_diff(struct vl_mpg12_bs
*bs
)
692 if (bs
->vlc
.buf
< 0xf8000000) {
693 tab
= DC_chrom_5
+ vl_vlc_ubits(&bs
->vlc
, 5);
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
;
702 vl_vlc_dumpbits(&bs
->vlc
, 2);
706 tab
= DC_long
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 0x3e0);
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
);
717 get_intra_block_B14(struct vl_mpg12_bs
*bs
, int quantizer_scale
, short *dest
)
724 vl_vlc_needbits(&bs
->vlc
);
727 if (bs
->vlc
.buf
>= 0x28000000) {
729 tab
= DCT_B14AC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
733 break; /* end of block */
736 bs
->vlc
.buf
<<= tab
->len
;
737 bs
->vlc
.bits
+= tab
->len
+ 1;
738 val
= tab
->level
* quantizer_scale
;
740 val
= (val
^ vl_vlc_sbits(&bs
->vlc
, 1)) - vl_vlc_sbits(&bs
->vlc
, 1);
745 vl_vlc_needbits(&bs
->vlc
);
749 } else if (bs
->vlc
.buf
>= 0x04000000) {
751 tab
= DCT_B14_8
+ (vl_vlc_ubits(&bs
->vlc
, 8) - 4);
759 i
+= UBITS(bs
->vlc
.buf
<< 6, 6) - 64;
761 break; /* illegal, check needed to avoid buffer overflow */
763 vl_vlc_dumpbits(&bs
->vlc
, 12);
764 vl_vlc_needbits(&bs
->vlc
);
765 val
= vl_vlc_sbits(&bs
->vlc
, 12) * quantizer_scale
;
769 vl_vlc_dumpbits(&bs
->vlc
, 12);
770 vl_vlc_needbits(&bs
->vlc
);
774 } else if (bs
->vlc
.buf
>= 0x02000000) {
775 tab
= DCT_B14_10
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 8);
779 } else if (bs
->vlc
.buf
>= 0x00800000) {
780 tab
= DCT_13
+ (vl_vlc_ubits(&bs
->vlc
, 13) - 16);
784 } else if (bs
->vlc
.buf
>= 0x00200000) {
785 tab
= DCT_15
+ (vl_vlc_ubits(&bs
->vlc
, 15) - 16);
790 tab
= DCT_16
+ vl_vlc_ubits(&bs
->vlc
, 16);
792 vl_vlc_getword(&bs
->vlc
, bs
->vlc
.bits
+ 16);
797 break; /* illegal, check needed to avoid buffer overflow */
800 vl_vlc_dumpbits(&bs
->vlc
, 2); /* dump end of block code */
804 get_intra_block_B15(struct vl_mpg12_bs
*bs
, int quantizer_scale
, short *dest
)
811 vl_vlc_needbits(&bs
->vlc
);
814 if (bs
->vlc
.buf
>= 0x04000000) {
816 tab
= DCT_B15_8
+ (vl_vlc_ubits(&bs
->vlc
, 8) - 4);
822 bs
->vlc
.buf
<<= tab
->len
;
823 bs
->vlc
.bits
+= tab
->len
+ 1;
824 val
= tab
->level
* quantizer_scale
;
826 val
= (val
^ vl_vlc_sbits(&bs
->vlc
, 1)) - vl_vlc_sbits(&bs
->vlc
, 1);
831 vl_vlc_needbits(&bs
->vlc
);
837 /* end of block. I commented out this code because if we */
838 /* dont exit here we will still exit at the later test :) */
840 /* if (i >= 128) break; */ /* end of block */
844 i
+= UBITS(bs
->vlc
.buf
<< 6, 6) - 64;
846 break; /* illegal, check against buffer overflow */
848 vl_vlc_dumpbits(&bs
->vlc
, 12);
849 vl_vlc_needbits(&bs
->vlc
);
850 val
= vl_vlc_sbits(&bs
->vlc
, 12) * quantizer_scale
;
854 vl_vlc_dumpbits(&bs
->vlc
, 12);
855 vl_vlc_needbits(&bs
->vlc
);
860 } else if (bs
->vlc
.buf
>= 0x02000000) {
861 tab
= DCT_B15_10
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 8);
865 } else if (bs
->vlc
.buf
>= 0x00800000) {
866 tab
= DCT_13
+ (vl_vlc_ubits(&bs
->vlc
, 13) - 16);
870 } else if (bs
->vlc
.buf
>= 0x00200000) {
871 tab
= DCT_15
+ (vl_vlc_ubits(&bs
->vlc
, 15) - 16);
876 tab
= DCT_16
+ vl_vlc_ubits(&bs
->vlc
, 16);
878 vl_vlc_getword(&bs
->vlc
, bs
->vlc
.bits
+ 16);
883 break; /* illegal, check needed to avoid buffer overflow */
886 vl_vlc_dumpbits(&bs
->vlc
, 4); /* dump end of block code */
890 get_non_intra_block(struct vl_mpg12_bs
*bs
, int quantizer_scale
, short *dest
)
897 vl_vlc_needbits(&bs
->vlc
);
898 if (bs
->vlc
.buf
>= 0x28000000) {
899 tab
= DCT_B14DC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
905 if (bs
->vlc
.buf
>= 0x28000000) {
907 tab
= DCT_B14AC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
912 break; /* end of block */
915 bs
->vlc
.buf
<<= tab
->len
;
916 bs
->vlc
.bits
+= tab
->len
+ 1;
917 val
= ((2*tab
->level
+1) * quantizer_scale
) >> 1;
919 val
= (val
^ vl_vlc_sbits(&bs
->vlc
, 1)) - vl_vlc_sbits(&bs
->vlc
, 1);
924 vl_vlc_needbits(&bs
->vlc
);
931 if (bs
->vlc
.buf
>= 0x04000000) {
933 tab
= DCT_B14_8
+ (vl_vlc_ubits(&bs
->vlc
, 8) - 4);
941 i
+= UBITS(bs
->vlc
.buf
<< 6, 6) - 64;
943 break; /* illegal, check needed to avoid buffer overflow */
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;
952 vl_vlc_dumpbits(&bs
->vlc
, 12);
953 vl_vlc_needbits(&bs
->vlc
);
957 } else if (bs
->vlc
.buf
>= 0x02000000) {
958 tab
= DCT_B14_10
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 8);
962 } else if (bs
->vlc
.buf
>= 0x00800000) {
963 tab
= DCT_13
+ (vl_vlc_ubits(&bs
->vlc
, 13) - 16);
967 } else if (bs
->vlc
.buf
>= 0x00200000) {
968 tab
= DCT_15
+ (vl_vlc_ubits(&bs
->vlc
, 15) - 16);
973 tab
= DCT_16
+ vl_vlc_ubits(&bs
->vlc
, 16);
975 vl_vlc_getword(&bs
->vlc
, bs
->vlc
.bits
+ 16);
980 break; /* illegal, check needed to avoid buffer overflow */
982 vl_vlc_dumpbits(&bs
->vlc
, 2); /* dump end of block code */
986 get_mpeg1_intra_block(struct vl_mpg12_bs
*bs
, int quantizer_scale
, short *dest
)
993 vl_vlc_needbits(&bs
->vlc
);
996 if (bs
->vlc
.buf
>= 0x28000000) {
998 tab
= DCT_B14AC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
1002 break; /* end of block */
1005 bs
->vlc
.buf
<<= tab
->len
;
1006 bs
->vlc
.bits
+= tab
->len
+ 1;
1007 val
= tab
->level
* quantizer_scale
;
1010 val
= (val
- 1) | 1;
1012 /* if (bitstream_get (1)) val = -val; */
1013 val
= (val
^ vl_vlc_sbits(&bs
->vlc
, 1)) - vl_vlc_sbits(&bs
->vlc
, 1);
1018 vl_vlc_needbits(&bs
->vlc
);
1022 } else if (bs
->vlc
.buf
>= 0x04000000) {
1024 tab
= DCT_B14_8
+ (vl_vlc_ubits(&bs
->vlc
, 8) - 4);
1032 i
+= UBITS(bs
->vlc
.buf
<< 6, 6) - 64;
1034 break; /* illegal, check needed to avoid buffer overflow */
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
;
1043 val
= val
* quantizer_scale
;
1046 val
= (val
+ ~SBITS (val
, 1)) | 1;
1050 vl_vlc_dumpbits(&bs
->vlc
, 8);
1051 vl_vlc_needbits(&bs
->vlc
);
1055 } else if (bs
->vlc
.buf
>= 0x02000000) {
1056 tab
= DCT_B14_10
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 8);
1060 } else if (bs
->vlc
.buf
>= 0x00800000) {
1061 tab
= DCT_13
+ (vl_vlc_ubits(&bs
->vlc
, 13) - 16);
1065 } else if (bs
->vlc
.buf
>= 0x00200000) {
1066 tab
= DCT_15
+ (vl_vlc_ubits(&bs
->vlc
, 15) - 16);
1071 tab
= DCT_16
+ vl_vlc_ubits(&bs
->vlc
, 16);
1073 vl_vlc_getword(&bs
->vlc
, bs
->vlc
.bits
+ 16);
1078 break; /* illegal, check needed to avoid buffer overflow */
1080 vl_vlc_dumpbits(&bs
->vlc
, 2); /* dump end of block code */
1084 get_mpeg1_non_intra_block(struct vl_mpg12_bs
*bs
, int quantizer_scale
, short *dest
)
1091 vl_vlc_needbits(&bs
->vlc
);
1092 if (bs
->vlc
.buf
>= 0x28000000) {
1093 tab
= DCT_B14DC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
1099 if (bs
->vlc
.buf
>= 0x28000000) {
1101 tab
= DCT_B14AC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
1106 break; /* end of block */
1109 bs
->vlc
.buf
<<= tab
->len
;
1110 bs
->vlc
.bits
+= tab
->len
+ 1;
1111 val
= ((2*tab
->level
+1) * quantizer_scale
) >> 1;
1114 val
= (val
- 1) | 1;
1116 /* if (bitstream_get (1)) val = -val; */
1117 val
= (val
^ vl_vlc_sbits(&bs
->vlc
, 1)) - vl_vlc_sbits(&bs
->vlc
, 1);
1122 vl_vlc_needbits(&bs
->vlc
);
1129 if (bs
->vlc
.buf
>= 0x04000000) {
1131 tab
= DCT_B14_8
+ (vl_vlc_ubits(&bs
->vlc
, 8) - 4);
1139 i
+= UBITS(bs
->vlc
.buf
<< 6, 6) - 64;
1141 break; /* illegal, check needed to avoid buffer overflow */
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
;
1150 val
= 2 * (val
+ SBITS (val
, 1)) + 1;
1151 val
= (val
* quantizer_scale
) / 2;
1154 val
= (val
+ ~SBITS (val
, 1)) | 1;
1158 vl_vlc_dumpbits(&bs
->vlc
, 8);
1159 vl_vlc_needbits(&bs
->vlc
);
1163 } else if (bs
->vlc
.buf
>= 0x02000000) {
1164 tab
= DCT_B14_10
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 8);
1168 } else if (bs
->vlc
.buf
>= 0x00800000) {
1169 tab
= DCT_13
+ (vl_vlc_ubits(&bs
->vlc
, 13) - 16);
1173 } else if (bs
->vlc
.buf
>= 0x00200000) {
1174 tab
= DCT_15
+ (vl_vlc_ubits(&bs
->vlc
, 15) - 16);
1179 tab
= DCT_16
+ vl_vlc_ubits(&bs
->vlc
, 16);
1181 vl_vlc_getword(&bs
->vlc
, bs
->vlc
.bits
+ 16);
1186 break; /* illegal, check needed to avoid buffer overflow */
1188 vl_vlc_dumpbits(&bs
->vlc
, 2); /* dump end of block code */
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])
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
;
1202 vl_vlc_needbits(&bs
->vlc
);
1204 /* Get the intra DC coefficient and inverse quantize it */
1206 dc_dct_pred
[0] += get_luma_dc_dct_diff(bs
);
1208 dc_dct_pred
[cc
] += get_chroma_dc_dct_diff(bs
);
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
);
1218 get_intra_block_B14(bs
, quantizer_scale
, dest
);
1220 memcpy(bs
->ycbcr_buffer
[cc
], dest
, sizeof(int16_t) * 64);
1222 bs
->num_ycbcr_blocks
[cc
]++;
1223 bs
->ycbcr_stream
[cc
]++;
1224 bs
->ycbcr_buffer
[cc
] += 64;
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
)
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
;
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
);
1242 get_non_intra_block(bs
, quantizer_scale
, dest
);
1244 memcpy(bs
->ycbcr_buffer
[cc
], dest
, sizeof(int16_t) * 64);
1246 bs
->num_ycbcr_blocks
[cc
]++;
1247 bs
->ycbcr_stream
[cc
]++;
1248 bs
->ycbcr_buffer
[cc
] += 64;
1252 motion_mp1(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1254 int motion_x
, motion_y
;
1256 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
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
;
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
;
1270 motion_fr_frame(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1272 int motion_x
, motion_y
;
1274 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
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
;
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
;
1288 motion_fr_field(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1290 int motion_x
, motion_y
;
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);
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
;
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;
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);
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
;
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;
1322 motion_fr_dmv(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1324 int motion_x
, motion_y
;
1326 // TODO Implement dmv
1327 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
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
;
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;
1340 /* like motion_frame, but parsing without actual motion compensation */
1342 motion_fr_conceal(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1346 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
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
;
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
;
1358 vl_vlc_dumpbits(&bs
->vlc
, 1); /* remove marker_bit */
1362 motion_fi_field(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1364 int motion_x
, motion_y
;
1366 vl_vlc_needbits(&bs
->vlc
);
1369 //vl_vlc_ubits(&bs->vlc, 1);
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);
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
;
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
;
1386 motion_fi_16x8(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1388 int motion_x
, motion_y
;
1390 vl_vlc_needbits(&bs
->vlc
);
1393 //vl_vlc_ubits(&bs->vlc, 1);
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);
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
;
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
;
1408 vl_vlc_needbits(&bs
->vlc
);
1410 //vl_vlc_ubits(&bs->vlc, 1);
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);
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
;
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
;
1427 motion_fi_dmv(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1429 int motion_x
, motion_y
;
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
;
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
;
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
;
1447 motion_fi_conceal(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1451 vl_vlc_needbits(&bs
->vlc
);
1452 vl_vlc_dumpbits(&bs
->vlc
, 1); /* remove field_select */
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
;
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
;
1463 vl_vlc_dumpbits(&bs
->vlc
, 1); /* remove marker_bit */
1466 #define MOTION_CALL(routine, macroblock_modes) \
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); \
1475 store_motionvectors(struct vl_mpg12_bs
*bs
, unsigned *mv_pos
,
1476 struct pipe_motionvector
*mv_fwd
,
1477 struct pipe_motionvector
*mv_bwd
)
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
;
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
;
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
)
1498 vl_vlc_need32bits(&bs
->vlc
);
1499 while(bs
->vlc
.buf
< 0x101 || bs
->vlc
.buf
> 0x1AF) {
1500 if(!vl_vlc_getbyte(&bs
->vlc
))
1503 *y
= (bs
->vlc
.buf
& 0xFF) - 1;
1504 vl_vlc_restart(&bs
->vlc
);
1506 *quantizer_scale
= get_quantizer_scale(bs
, picture
);
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
);
1514 /* decode initial macroblock address increment */
1517 if (bs
->vlc
.buf
>= 0x08000000) {
1518 mba
= MBA_5
+ (vl_vlc_ubits(&bs
->vlc
, 6) - 2);
1520 } else if (bs
->vlc
.buf
>= 0x01800000) {
1521 mba
= MBA_11
+ (vl_vlc_ubits(&bs
->vlc
, 12) - 24);
1523 } else switch (vl_vlc_ubits(&bs
->vlc
, 12)) {
1524 case 8: /* macroblock_escape */
1526 vl_vlc_dumpbits(&bs
->vlc
, 11);
1527 vl_vlc_needbits(&bs
->vlc
);
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
);
1534 default: /* error */
1538 vl_vlc_dumpbits(&bs
->vlc
, mba
->len
+ 1);
1541 while (*x
>= bs
->width
) {
1545 if (*y
> bs
->height
)
1548 *mv_pos
= *x
+ *y
* bs
->width
;
1554 decode_slice(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
*picture
)
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
;
1560 /* predictor for DC coefficients in intra blocks */
1561 int dc_dct_pred
[3] = { 0, 0, 0 };
1562 int quantizer_scale
;
1564 unsigned x
, y
, mv_pos
;
1566 switch(picture
->picture_structure
) {
1568 default_field_select
= PIPE_VIDEO_TOP_FIELD
;
1572 default_field_select
= PIPE_VIDEO_BOTTOM_FIELD
;
1576 default_field_select
= PIPE_VIDEO_FRAME
;
1580 if (!slice_init(bs
, picture
, &quantizer_scale
, &x
, &y
, &mv_pos
))
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
;
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
;
1590 int macroblock_modes
;
1594 vl_vlc_needbits(&bs
->vlc
);
1596 macroblock_modes
= get_macroblock_modes(bs
, picture
);
1597 dct_type
= get_dct_type(bs
, picture
, macroblock_modes
);
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
;
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
;
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
;
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
;
1621 /* maybe integrate MACROBLOCK_QUANT test into get_macroblock_modes ? */
1622 if (macroblock_modes
& MACROBLOCK_QUANT
)
1623 quantizer_scale
= get_quantizer_scale(bs
, picture
);
1625 if (macroblock_modes
& MACROBLOCK_INTRA
) {
1627 if (picture
->concealment_motion_vectors
) {
1628 if (picture
->picture_structure
== FRAME_PICTURE
)
1629 motion_fr_conceal(bs
, picture
->f_code
[0], &mv_fwd
);
1631 motion_fi_conceal(bs
, picture
->f_code
[0], &mv_fwd
);
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;
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
;
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
);
1648 if (picture
->picture_coding_type
== D_TYPE
) {
1649 vl_vlc_needbits(&bs
->vlc
);
1650 vl_vlc_dumpbits(&bs
->vlc
, 1);
1654 if (picture
->picture_structure
== FRAME_PICTURE
)
1655 switch (macroblock_modes
& MOTION_TYPE_MASK
) {
1657 if (picture
->base
.profile
== PIPE_VIDEO_PROFILE_MPEG1
) {
1658 MOTION_CALL(motion_mp1
, macroblock_modes
);
1660 MOTION_CALL(motion_fr_frame
, macroblock_modes
);
1665 MOTION_CALL (motion_fr_field
, macroblock_modes
);
1669 MOTION_CALL (motion_fr_dmv
, MACROBLOCK_MOTION_FORWARD
);
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;
1679 switch (macroblock_modes
& MOTION_TYPE_MASK
) {
1681 MOTION_CALL (motion_fi_field
, macroblock_modes
);
1685 MOTION_CALL (motion_fi_16x8
, macroblock_modes
);
1689 MOTION_CALL (motion_fi_dmv
, MACROBLOCK_MOTION_FORWARD
);
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;
1699 if (macroblock_modes
& MACROBLOCK_PATTERN
) {
1700 int coded_block_pattern
= get_coded_block_pattern(bs
);
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
1717 dc_dct_pred
[0] = dc_dct_pred
[1] = dc_dct_pred
[2] = 0;
1720 store_motionvectors(bs
, &mv_pos
, &mv_fwd
, &mv_bwd
);
1721 if (++x
>= bs
->width
) {
1723 if (y
>= bs
->height
)
1728 vl_vlc_needbits(&bs
->vlc
);
1731 if (bs
->vlc
.buf
>= 0x10000000) {
1732 mba
= MBA_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 2);
1734 } else if (bs
->vlc
.buf
>= 0x03000000) {
1735 mba
= MBA_11
+ (vl_vlc_ubits(&bs
->vlc
, 11) - 24);
1737 } else switch (vl_vlc_ubits(&bs
->vlc
, 11)) {
1738 case 8: /* macroblock_escape */
1741 case 15: /* macroblock_stuffing (MPEG1 only) */
1742 vl_vlc_dumpbits(&bs
->vlc
, 11);
1743 vl_vlc_needbits(&bs
->vlc
);
1745 default: /* end of slice, or error */
1749 vl_vlc_dumpbits(&bs
->vlc
, mba
->len
);
1750 mba_inc
+= mba
->mba
;
1752 //TODO conversion to signed format signed format
1753 dc_dct_pred
[0] = dc_dct_pred
[1] = dc_dct_pred
[2] = 0;
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
;
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
;
1765 store_motionvectors(bs
, &mv_pos
, &mv_fwd
, &mv_bwd
);
1766 } while (--mba_inc
);
1768 while (x
>= bs
->width
) {
1770 if (y
>= bs
->height
)
1778 vl_mpg12_bs_init(struct vl_mpg12_bs
*bs
, unsigned width
, unsigned height
)
1782 memset(bs
, 0, sizeof(struct vl_mpg12_bs
));
1785 bs
->height
= height
;
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
])
1795 assert(ycbcr_stream
&& ycbcr_buffer
);
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
];
1802 for (i
= 0; i
< VL_MAX_REF_FRAMES
; ++i
)
1803 bs
->mv_stream
[i
] = mv_stream
[i
];
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
;
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
;
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])
1828 assert(num_ycbcr_blocks
);
1829 assert(buffer
&& num_bytes
);
1831 bs
->num_ycbcr_blocks
= num_ycbcr_blocks
;
1833 vl_vlc_init(&bs
->vlc
, buffer
, num_bytes
);
1835 while(decode_slice(bs
, picture
));