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>
59 #include "vl_mpeg12_bitstream.h"
61 /* take num bits from the high part of bit_buf and zero extend them */
62 #define UBITS(buf,num) (((uint32_t)(buf)) >> (32 - (num)))
64 /* take num bits from the high part of bit_buf and sign extend them */
65 #define SBITS(buf,num) (((int32_t)(buf)) >> (32 - (num)))
67 #define SATURATE(val) \
69 if ((uint32_t)(val + 2048) > 4095) \
70 val = (val > 0) ? 2047 : -2048; \
73 /* macroblock modes */
74 #define MACROBLOCK_INTRA 1
75 #define MACROBLOCK_PATTERN 2
76 #define MACROBLOCK_MOTION_BACKWARD 4
77 #define MACROBLOCK_MOTION_FORWARD 8
78 #define MACROBLOCK_QUANT 16
79 #define DCT_TYPE_INTERLACED 32
82 #define MOTION_TYPE_MASK (3*64)
83 #define MOTION_TYPE_BASE 64
84 #define MC_FIELD (1*64)
85 #define MC_FRAME (2*64)
86 #define MC_16X8 (2*64)
89 /* picture structure */
91 #define BOTTOM_FIELD 2
92 #define FRAME_PICTURE 3
94 /* picture coding type (mpeg2 header) */
136 #define INTRA MACROBLOCK_INTRA
137 #define QUANT MACROBLOCK_QUANT
138 #define MC MACROBLOCK_MOTION_FORWARD
139 #define CODED MACROBLOCK_PATTERN
140 #define FWD MACROBLOCK_MOTION_FORWARD
141 #define BWD MACROBLOCK_MOTION_BACKWARD
142 #define INTER MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD
144 static const MBtab MB_I
[] = {
145 {INTRA
|QUANT
, 2}, {INTRA
, 1}
148 static const MBtab MB_P
[] = {
149 {INTRA
|QUANT
, 6}, {CODED
|QUANT
, 5}, {MC
|CODED
|QUANT
, 5}, {INTRA
, 5},
150 {MC
, 3}, {MC
, 3}, {MC
, 3}, {MC
, 3},
151 {CODED
, 2}, {CODED
, 2}, {CODED
, 2}, {CODED
, 2},
152 {CODED
, 2}, {CODED
, 2}, {CODED
, 2}, {CODED
, 2},
153 {MC
|CODED
, 1}, {MC
|CODED
, 1}, {MC
|CODED
, 1}, {MC
|CODED
, 1},
154 {MC
|CODED
, 1}, {MC
|CODED
, 1}, {MC
|CODED
, 1}, {MC
|CODED
, 1},
155 {MC
|CODED
, 1}, {MC
|CODED
, 1}, {MC
|CODED
, 1}, {MC
|CODED
, 1},
156 {MC
|CODED
, 1}, {MC
|CODED
, 1}, {MC
|CODED
, 1}, {MC
|CODED
, 1}
159 static const MBtab MB_B
[] = {
160 {0, 0}, {INTRA
|QUANT
, 6},
161 {BWD
|CODED
|QUANT
, 6}, {FWD
|CODED
|QUANT
, 6},
162 {INTER
|CODED
|QUANT
, 5}, {INTER
|CODED
|QUANT
, 5},
163 {INTRA
, 5}, {INTRA
, 5},
164 {FWD
, 4}, {FWD
, 4}, {FWD
, 4}, {FWD
, 4},
165 {FWD
|CODED
, 4}, {FWD
|CODED
, 4}, {FWD
|CODED
, 4}, {FWD
|CODED
, 4},
166 {BWD
, 3}, {BWD
, 3}, {BWD
, 3}, {BWD
, 3},
167 {BWD
, 3}, {BWD
, 3}, {BWD
, 3}, {BWD
, 3},
168 {BWD
|CODED
, 3}, {BWD
|CODED
, 3}, {BWD
|CODED
, 3}, {BWD
|CODED
, 3},
169 {BWD
|CODED
, 3}, {BWD
|CODED
, 3}, {BWD
|CODED
, 3}, {BWD
|CODED
, 3},
170 {INTER
, 2}, {INTER
, 2}, {INTER
, 2}, {INTER
, 2},
171 {INTER
, 2}, {INTER
, 2}, {INTER
, 2}, {INTER
, 2},
172 {INTER
, 2}, {INTER
, 2}, {INTER
, 2}, {INTER
, 2},
173 {INTER
, 2}, {INTER
, 2}, {INTER
, 2}, {INTER
, 2},
174 {INTER
|CODED
, 2}, {INTER
|CODED
, 2}, {INTER
|CODED
, 2}, {INTER
|CODED
, 2},
175 {INTER
|CODED
, 2}, {INTER
|CODED
, 2}, {INTER
|CODED
, 2}, {INTER
|CODED
, 2},
176 {INTER
|CODED
, 2}, {INTER
|CODED
, 2}, {INTER
|CODED
, 2}, {INTER
|CODED
, 2},
177 {INTER
|CODED
, 2}, {INTER
|CODED
, 2}, {INTER
|CODED
, 2}, {INTER
|CODED
, 2}
188 static const MVtab MV_4
[] = {
189 { 3, 6}, { 2, 4}, { 1, 3}, { 1, 3}, { 0, 2}, { 0, 2}, { 0, 2}, { 0, 2}
192 static const MVtab MV_10
[] = {
193 { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10},
194 { 0,10}, { 0,10}, { 0,10}, { 0,10}, {15,10}, {14,10}, {13,10}, {12,10},
195 {11,10}, {10,10}, { 9, 9}, { 9, 9}, { 8, 9}, { 8, 9}, { 7, 9}, { 7, 9},
196 { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7},
197 { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7},
198 { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}
201 static const DMVtab DMV_2
[] = {
202 { 0, 1}, { 0, 1}, { 1, 2}, {-1, 2}
205 static const CBPtab CBP_7
[] = {
206 {0x22, 7}, {0x12, 7}, {0x0a, 7}, {0x06, 7},
207 {0x21, 7}, {0x11, 7}, {0x09, 7}, {0x05, 7},
208 {0x3f, 6}, {0x3f, 6}, {0x03, 6}, {0x03, 6},
209 {0x24, 6}, {0x24, 6}, {0x18, 6}, {0x18, 6},
210 {0x3e, 5}, {0x3e, 5}, {0x3e, 5}, {0x3e, 5},
211 {0x02, 5}, {0x02, 5}, {0x02, 5}, {0x02, 5},
212 {0x3d, 5}, {0x3d, 5}, {0x3d, 5}, {0x3d, 5},
213 {0x01, 5}, {0x01, 5}, {0x01, 5}, {0x01, 5},
214 {0x38, 5}, {0x38, 5}, {0x38, 5}, {0x38, 5},
215 {0x34, 5}, {0x34, 5}, {0x34, 5}, {0x34, 5},
216 {0x2c, 5}, {0x2c, 5}, {0x2c, 5}, {0x2c, 5},
217 {0x1c, 5}, {0x1c, 5}, {0x1c, 5}, {0x1c, 5},
218 {0x28, 5}, {0x28, 5}, {0x28, 5}, {0x28, 5},
219 {0x14, 5}, {0x14, 5}, {0x14, 5}, {0x14, 5},
220 {0x30, 5}, {0x30, 5}, {0x30, 5}, {0x30, 5},
221 {0x0c, 5}, {0x0c, 5}, {0x0c, 5}, {0x0c, 5},
222 {0x20, 4}, {0x20, 4}, {0x20, 4}, {0x20, 4},
223 {0x20, 4}, {0x20, 4}, {0x20, 4}, {0x20, 4},
224 {0x10, 4}, {0x10, 4}, {0x10, 4}, {0x10, 4},
225 {0x10, 4}, {0x10, 4}, {0x10, 4}, {0x10, 4},
226 {0x08, 4}, {0x08, 4}, {0x08, 4}, {0x08, 4},
227 {0x08, 4}, {0x08, 4}, {0x08, 4}, {0x08, 4},
228 {0x04, 4}, {0x04, 4}, {0x04, 4}, {0x04, 4},
229 {0x04, 4}, {0x04, 4}, {0x04, 4}, {0x04, 4},
230 {0x3c, 3}, {0x3c, 3}, {0x3c, 3}, {0x3c, 3},
231 {0x3c, 3}, {0x3c, 3}, {0x3c, 3}, {0x3c, 3},
232 {0x3c, 3}, {0x3c, 3}, {0x3c, 3}, {0x3c, 3},
233 {0x3c, 3}, {0x3c, 3}, {0x3c, 3}, {0x3c, 3}
236 static const CBPtab CBP_9
[] = {
237 {0, 0}, {0x00, 9}, {0x27, 9}, {0x1b, 9},
238 {0x3b, 9}, {0x37, 9}, {0x2f, 9}, {0x1f, 9},
239 {0x3a, 8}, {0x3a, 8}, {0x36, 8}, {0x36, 8},
240 {0x2e, 8}, {0x2e, 8}, {0x1e, 8}, {0x1e, 8},
241 {0x39, 8}, {0x39, 8}, {0x35, 8}, {0x35, 8},
242 {0x2d, 8}, {0x2d, 8}, {0x1d, 8}, {0x1d, 8},
243 {0x26, 8}, {0x26, 8}, {0x1a, 8}, {0x1a, 8},
244 {0x25, 8}, {0x25, 8}, {0x19, 8}, {0x19, 8},
245 {0x2b, 8}, {0x2b, 8}, {0x17, 8}, {0x17, 8},
246 {0x33, 8}, {0x33, 8}, {0x0f, 8}, {0x0f, 8},
247 {0x2a, 8}, {0x2a, 8}, {0x16, 8}, {0x16, 8},
248 {0x32, 8}, {0x32, 8}, {0x0e, 8}, {0x0e, 8},
249 {0x29, 8}, {0x29, 8}, {0x15, 8}, {0x15, 8},
250 {0x31, 8}, {0x31, 8}, {0x0d, 8}, {0x0d, 8},
251 {0x23, 8}, {0x23, 8}, {0x13, 8}, {0x13, 8},
252 {0x0b, 8}, {0x0b, 8}, {0x07, 8}, {0x07, 8}
255 static const DCtab DC_lum_5
[] = {
256 {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
257 {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
258 {0, 3}, {0, 3}, {0, 3}, {0, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3},
259 {4, 3}, {4, 3}, {4, 3}, {4, 3}, {5, 4}, {5, 4}, {6, 5}
262 static const DCtab DC_chrom_5
[] = {
263 {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2},
264 {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
265 {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
266 {3, 3}, {3, 3}, {3, 3}, {3, 3}, {4, 4}, {4, 4}, {5, 5}
269 static const DCtab DC_long
[] = {
270 {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, { 6, 5}, { 6, 5},
271 {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, { 6, 5}, { 6, 5},
272 {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, { 7, 6}, { 7, 6},
273 {8, 7}, {8, 7}, {8, 7}, {8, 7}, {9, 8}, {9, 8}, {10, 9}, {11, 9}
276 static const DCTtab DCT_16
[] = {
277 {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0},
278 {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0},
279 {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0},
280 {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0},
281 { 2,18, 0}, { 2,17, 0}, { 2,16, 0}, { 2,15, 0},
282 { 7, 3, 0}, { 17, 2, 0}, { 16, 2, 0}, { 15, 2, 0},
283 { 14, 2, 0}, { 13, 2, 0}, { 12, 2, 0}, { 32, 1, 0},
284 { 31, 1, 0}, { 30, 1, 0}, { 29, 1, 0}, { 28, 1, 0}
287 static const DCTtab DCT_15
[] = {
288 { 1,40,15}, { 1,39,15}, { 1,38,15}, { 1,37,15},
289 { 1,36,15}, { 1,35,15}, { 1,34,15}, { 1,33,15},
290 { 1,32,15}, { 2,14,15}, { 2,13,15}, { 2,12,15},
291 { 2,11,15}, { 2,10,15}, { 2, 9,15}, { 2, 8,15},
292 { 1,31,14}, { 1,31,14}, { 1,30,14}, { 1,30,14},
293 { 1,29,14}, { 1,29,14}, { 1,28,14}, { 1,28,14},
294 { 1,27,14}, { 1,27,14}, { 1,26,14}, { 1,26,14},
295 { 1,25,14}, { 1,25,14}, { 1,24,14}, { 1,24,14},
296 { 1,23,14}, { 1,23,14}, { 1,22,14}, { 1,22,14},
297 { 1,21,14}, { 1,21,14}, { 1,20,14}, { 1,20,14},
298 { 1,19,14}, { 1,19,14}, { 1,18,14}, { 1,18,14},
299 { 1,17,14}, { 1,17,14}, { 1,16,14}, { 1,16,14}
302 static const DCTtab DCT_13
[] = {
303 { 11, 2,13}, { 10, 2,13}, { 6, 3,13}, { 4, 4,13},
304 { 3, 5,13}, { 2, 7,13}, { 2, 6,13}, { 1,15,13},
305 { 1,14,13}, { 1,13,13}, { 1,12,13}, { 27, 1,13},
306 { 26, 1,13}, { 25, 1,13}, { 24, 1,13}, { 23, 1,13},
307 { 1,11,12}, { 1,11,12}, { 9, 2,12}, { 9, 2,12},
308 { 5, 3,12}, { 5, 3,12}, { 1,10,12}, { 1,10,12},
309 { 3, 4,12}, { 3, 4,12}, { 8, 2,12}, { 8, 2,12},
310 { 22, 1,12}, { 22, 1,12}, { 21, 1,12}, { 21, 1,12},
311 { 1, 9,12}, { 1, 9,12}, { 20, 1,12}, { 20, 1,12},
312 { 19, 1,12}, { 19, 1,12}, { 2, 5,12}, { 2, 5,12},
313 { 4, 3,12}, { 4, 3,12}, { 1, 8,12}, { 1, 8,12},
314 { 7, 2,12}, { 7, 2,12}, { 18, 1,12}, { 18, 1,12}
317 static const DCTtab DCT_B14_10
[] = {
318 { 17, 1,10}, { 6, 2,10}, { 1, 7,10}, { 3, 3,10},
319 { 2, 4,10}, { 16, 1,10}, { 15, 1,10}, { 5, 2,10}
322 static const DCTtab DCT_B14_8
[] = {
323 { 65, 0, 6}, { 65, 0, 6}, { 65, 0, 6}, { 65, 0, 6},
324 { 3, 2, 7}, { 3, 2, 7}, { 10, 1, 7}, { 10, 1, 7},
325 { 1, 4, 7}, { 1, 4, 7}, { 9, 1, 7}, { 9, 1, 7},
326 { 8, 1, 6}, { 8, 1, 6}, { 8, 1, 6}, { 8, 1, 6},
327 { 7, 1, 6}, { 7, 1, 6}, { 7, 1, 6}, { 7, 1, 6},
328 { 2, 2, 6}, { 2, 2, 6}, { 2, 2, 6}, { 2, 2, 6},
329 { 6, 1, 6}, { 6, 1, 6}, { 6, 1, 6}, { 6, 1, 6},
330 { 14, 1, 8}, { 1, 6, 8}, { 13, 1, 8}, { 12, 1, 8},
331 { 4, 2, 8}, { 2, 3, 8}, { 1, 5, 8}, { 11, 1, 8}
334 static const DCTtab DCT_B14AC_5
[] = {
335 { 1, 3, 5}, { 5, 1, 5}, { 4, 1, 5},
336 { 1, 2, 4}, { 1, 2, 4}, { 3, 1, 4}, { 3, 1, 4},
337 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
338 {129, 0, 2}, {129, 0, 2}, {129, 0, 2}, {129, 0, 2},
339 {129, 0, 2}, {129, 0, 2}, {129, 0, 2}, {129, 0, 2},
340 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
341 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}
344 static const DCTtab DCT_B14DC_5
[] = {
345 { 1, 3, 5}, { 5, 1, 5}, { 4, 1, 5},
346 { 1, 2, 4}, { 1, 2, 4}, { 3, 1, 4}, { 3, 1, 4},
347 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
348 { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1},
349 { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1},
350 { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1},
351 { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}
354 static const DCTtab DCT_B15_10
[] = {
355 { 6, 2, 9}, { 6, 2, 9}, { 15, 1, 9}, { 15, 1, 9},
356 { 3, 4,10}, { 17, 1,10}, { 16, 1, 9}, { 16, 1, 9}
359 static const DCTtab DCT_B15_8
[] = {
360 { 65, 0, 6}, { 65, 0, 6}, { 65, 0, 6}, { 65, 0, 6},
361 { 8, 1, 7}, { 8, 1, 7}, { 9, 1, 7}, { 9, 1, 7},
362 { 7, 1, 7}, { 7, 1, 7}, { 3, 2, 7}, { 3, 2, 7},
363 { 1, 7, 6}, { 1, 7, 6}, { 1, 7, 6}, { 1, 7, 6},
364 { 1, 6, 6}, { 1, 6, 6}, { 1, 6, 6}, { 1, 6, 6},
365 { 5, 1, 6}, { 5, 1, 6}, { 5, 1, 6}, { 5, 1, 6},
366 { 6, 1, 6}, { 6, 1, 6}, { 6, 1, 6}, { 6, 1, 6},
367 { 2, 5, 8}, { 12, 1, 8}, { 1,11, 8}, { 1,10, 8},
368 { 14, 1, 8}, { 13, 1, 8}, { 4, 2, 8}, { 2, 4, 8},
369 { 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5},
370 { 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5},
371 { 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5},
372 { 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5},
373 { 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5},
374 { 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5},
375 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
376 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
377 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
378 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
379 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
380 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
381 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
382 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
383 {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4},
384 {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4},
385 {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4},
386 {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4},
387 { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4},
388 { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4},
389 { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4},
390 { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4},
391 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
392 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
393 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
394 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
395 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
396 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
397 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
398 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
399 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
400 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
401 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
402 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
403 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
404 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
405 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
406 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
407 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
408 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
409 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
410 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
411 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
412 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
413 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
414 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
415 { 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5},
416 { 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5},
417 { 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5},
418 { 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5},
419 { 10, 1, 7}, { 10, 1, 7}, { 2, 3, 7}, { 2, 3, 7},
420 { 11, 1, 7}, { 11, 1, 7}, { 1, 8, 7}, { 1, 8, 7},
421 { 1, 9, 7}, { 1, 9, 7}, { 1,12, 8}, { 1,13, 8},
422 { 3, 3, 8}, { 5, 2, 8}, { 1,14, 8}, { 1,15, 8}
425 static const MBAtab MBA_5
[] = {
426 {6, 5}, {5, 5}, {4, 4}, {4, 4}, {3, 4}, {3, 4},
427 {2, 3}, {2, 3}, {2, 3}, {2, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3},
428 {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1},
429 {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}
432 static const MBAtab MBA_11
[] = {
433 {32, 11}, {31, 11}, {30, 11}, {29, 11},
434 {28, 11}, {27, 11}, {26, 11}, {25, 11},
435 {24, 11}, {23, 11}, {22, 11}, {21, 11},
436 {20, 10}, {20, 10}, {19, 10}, {19, 10},
437 {18, 10}, {18, 10}, {17, 10}, {17, 10},
438 {16, 10}, {16, 10}, {15, 10}, {15, 10},
439 {14, 8}, {14, 8}, {14, 8}, {14, 8},
440 {14, 8}, {14, 8}, {14, 8}, {14, 8},
441 {13, 8}, {13, 8}, {13, 8}, {13, 8},
442 {13, 8}, {13, 8}, {13, 8}, {13, 8},
443 {12, 8}, {12, 8}, {12, 8}, {12, 8},
444 {12, 8}, {12, 8}, {12, 8}, {12, 8},
445 {11, 8}, {11, 8}, {11, 8}, {11, 8},
446 {11, 8}, {11, 8}, {11, 8}, {11, 8},
447 {10, 8}, {10, 8}, {10, 8}, {10, 8},
448 {10, 8}, {10, 8}, {10, 8}, {10, 8},
449 { 9, 8}, { 9, 8}, { 9, 8}, { 9, 8},
450 { 9, 8}, { 9, 8}, { 9, 8}, { 9, 8},
451 { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7},
452 { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7},
453 { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7},
454 { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7},
455 { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7},
456 { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7},
457 { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7},
458 { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7}
461 static const int non_linear_quantizer_scale
[] = {
462 0, 1, 2, 3, 4, 5, 6, 7,
463 8, 10, 12, 14, 16, 18, 20, 22,
464 24, 28, 32, 36, 40, 44, 48, 52,
465 56, 64, 72, 80, 88, 96, 104, 112
469 get_macroblock_modes(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
)
471 int macroblock_modes
;
474 switch (picture
->picture_coding_type
) {
477 tab
= MB_I
+ vl_vlc_ubits(&bs
->vlc
, 1);
478 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
479 macroblock_modes
= tab
->modes
;
481 if ((!(picture
->frame_pred_frame_dct
)) && (picture
->picture_structure
== FRAME_PICTURE
)) {
482 macroblock_modes
|= vl_vlc_ubits(&bs
->vlc
, 1) * DCT_TYPE_INTERLACED
;
483 vl_vlc_dumpbits(&bs
->vlc
, 1);
486 return macroblock_modes
;
490 tab
= MB_P
+ vl_vlc_ubits(&bs
->vlc
, 5);
491 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
492 macroblock_modes
= tab
->modes
;
494 if (picture
->picture_structure
!= FRAME_PICTURE
) {
495 if (macroblock_modes
& MACROBLOCK_MOTION_FORWARD
) {
496 macroblock_modes
|= vl_vlc_ubits(&bs
->vlc
, 2) * MOTION_TYPE_BASE
;
497 vl_vlc_dumpbits(&bs
->vlc
, 2);
499 return macroblock_modes
;
500 } else if (picture
->frame_pred_frame_dct
) {
501 if (macroblock_modes
& MACROBLOCK_MOTION_FORWARD
)
502 macroblock_modes
|= MC_FRAME
;
503 return macroblock_modes
;
505 if (macroblock_modes
& MACROBLOCK_MOTION_FORWARD
) {
506 macroblock_modes
|= vl_vlc_ubits(&bs
->vlc
, 2) * MOTION_TYPE_BASE
;
507 vl_vlc_dumpbits(&bs
->vlc
, 2);
509 if (macroblock_modes
& (MACROBLOCK_INTRA
| MACROBLOCK_PATTERN
)) {
510 macroblock_modes
|= vl_vlc_ubits(&bs
->vlc
, 1) * DCT_TYPE_INTERLACED
;
511 vl_vlc_dumpbits(&bs
->vlc
, 1);
513 return macroblock_modes
;
518 tab
= MB_B
+ vl_vlc_ubits(&bs
->vlc
, 6);
519 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
520 macroblock_modes
= tab
->modes
;
522 if (picture
->picture_structure
!= FRAME_PICTURE
) {
523 if (! (macroblock_modes
& MACROBLOCK_INTRA
)) {
524 macroblock_modes
|= vl_vlc_ubits(&bs
->vlc
, 2) * MOTION_TYPE_BASE
;
525 vl_vlc_dumpbits(&bs
->vlc
, 2);
527 return macroblock_modes
;
528 } else if (picture
->frame_pred_frame_dct
) {
529 /* if (! (macroblock_modes & MACROBLOCK_INTRA)) */
530 macroblock_modes
|= MC_FRAME
;
531 return macroblock_modes
;
533 if (macroblock_modes
& MACROBLOCK_INTRA
)
535 macroblock_modes
|= vl_vlc_ubits(&bs
->vlc
, 2) * MOTION_TYPE_BASE
;
536 vl_vlc_dumpbits(&bs
->vlc
, 2);
537 if (macroblock_modes
& (MACROBLOCK_INTRA
| MACROBLOCK_PATTERN
)) {
539 macroblock_modes
|= vl_vlc_ubits(&bs
->vlc
, 1) * DCT_TYPE_INTERLACED
;
540 vl_vlc_dumpbits(&bs
->vlc
, 1);
542 return macroblock_modes
;
547 vl_vlc_dumpbits(&bs
->vlc
, 1);
548 return MACROBLOCK_INTRA
;
556 get_quantizer_scale(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
)
558 int quantizer_scale_code
;
560 quantizer_scale_code
= vl_vlc_ubits(&bs
->vlc
, 5);
561 vl_vlc_dumpbits(&bs
->vlc
, 5);
563 if (picture
->q_scale_type
)
564 return non_linear_quantizer_scale
[quantizer_scale_code
];
566 return quantizer_scale_code
<< 1;
570 get_motion_delta(struct vl_mpg12_bs
*bs
, unsigned f_code
)
576 if (bs
->vlc
.buf
& 0x80000000) {
577 vl_vlc_dumpbits(&bs
->vlc
, 1);
579 } else if (bs
->vlc
.buf
>= 0x0c000000) {
581 tab
= MV_4
+ vl_vlc_ubits(&bs
->vlc
, 4);
582 delta
= (tab
->delta
<< f_code
) + 1;
583 bs
->vlc
.bits
+= tab
->len
+ f_code
+ 1;
584 bs
->vlc
.buf
<<= tab
->len
;
586 sign
= vl_vlc_sbits(&bs
->vlc
, 1);
590 delta
+= vl_vlc_ubits(&bs
->vlc
, f_code
);
591 bs
->vlc
.buf
<<= f_code
;
593 return (delta
^ sign
) - sign
;
597 tab
= MV_10
+ vl_vlc_ubits(&bs
->vlc
, 10);
598 delta
= (tab
->delta
<< f_code
) + 1;
599 bs
->vlc
.bits
+= tab
->len
+ 1;
600 bs
->vlc
.buf
<<= tab
->len
;
602 sign
= vl_vlc_sbits(&bs
->vlc
, 1);
606 vl_vlc_needbits(&bs
->vlc
);
607 delta
+= vl_vlc_ubits(&bs
->vlc
, f_code
);
608 vl_vlc_dumpbits(&bs
->vlc
, f_code
);
611 return (delta
^ sign
) - sign
;
616 bound_motion_vector(int vec
, unsigned f_code
)
622 limit
= 16 << f_code
;
624 if ((unsigned int)(vec
+ limit
) < 2 * limit
)
627 sign
= ((int32_t)vec
) >> 31;
628 return vec
- ((2 * limit
) ^ sign
) + sign
;
631 return ((int32_t)vec
<< (28 - f_code
)) >> (28 - f_code
);
636 get_dmv(struct vl_mpg12_bs
*bs
)
640 tab
= DMV_2
+ vl_vlc_ubits(&bs
->vlc
, 2);
641 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
646 get_coded_block_pattern(struct vl_mpg12_bs
*bs
)
650 vl_vlc_needbits(&bs
->vlc
);
652 if (bs
->vlc
.buf
>= 0x20000000) {
654 tab
= CBP_7
+ (vl_vlc_ubits(&bs
->vlc
, 7) - 16);
655 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
660 tab
= CBP_9
+ vl_vlc_ubits(&bs
->vlc
, 9);
661 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
667 get_luma_dc_dct_diff(struct vl_mpg12_bs
*bs
)
673 if (bs
->vlc
.buf
< 0xf8000000) {
674 tab
= DC_lum_5
+ vl_vlc_ubits(&bs
->vlc
, 5);
677 bs
->vlc
.bits
+= tab
->len
+ size
;
678 bs
->vlc
.buf
<<= tab
->len
;
679 dc_diff
= vl_vlc_ubits(&bs
->vlc
, size
) - UBITS (SBITS (~bs
->vlc
.buf
, 1), size
);
680 bs
->vlc
.buf
<<= size
;
683 vl_vlc_dumpbits(&bs
->vlc
, 3);
687 tab
= DC_long
+ (vl_vlc_ubits(&bs
->vlc
, 9) - 0x1e0);
689 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
690 vl_vlc_needbits(&bs
->vlc
);
691 dc_diff
= vl_vlc_ubits(&bs
->vlc
, size
) - UBITS (SBITS (~bs
->vlc
.buf
, 1), size
);
692 vl_vlc_dumpbits(&bs
->vlc
, size
);
698 get_chroma_dc_dct_diff(struct vl_mpg12_bs
*bs
)
704 if (bs
->vlc
.buf
< 0xf8000000) {
705 tab
= DC_chrom_5
+ vl_vlc_ubits(&bs
->vlc
, 5);
708 bs
->vlc
.bits
+= tab
->len
+ size
;
709 bs
->vlc
.buf
<<= tab
->len
;
710 dc_diff
= vl_vlc_ubits(&bs
->vlc
, size
) - UBITS (SBITS (~bs
->vlc
.buf
, 1), size
);
711 bs
->vlc
.buf
<<= size
;
714 vl_vlc_dumpbits(&bs
->vlc
, 2);
718 tab
= DC_long
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 0x3e0);
720 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
+ 1);
721 vl_vlc_needbits(&bs
->vlc
);
722 dc_diff
= vl_vlc_ubits(&bs
->vlc
, size
) - UBITS (SBITS (~bs
->vlc
.buf
, 1), size
);
723 vl_vlc_dumpbits(&bs
->vlc
, size
);
729 get_intra_block_B14(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
,
730 const int scan
[64], int quantizer_scale
, short *dest
)
733 uint8_t *quant_matrix
= picture
->intra_quantizer_matrix
;
740 vl_vlc_needbits(&bs
->vlc
);
743 if (bs
->vlc
.buf
>= 0x28000000) {
745 tab
= DCT_B14AC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
749 break; /* end of block */
754 bs
->vlc
.buf
<<= tab
->len
;
755 bs
->vlc
.bits
+= tab
->len
+ 1;
756 val
= (tab
->level
* quantizer_scale
* quant_matrix
[j
]) >> 4;
758 /* if (bitstream_get (1)) val = -val; */
759 val
= (val
^ vl_vlc_sbits(&bs
->vlc
, 1)) - vl_vlc_sbits(&bs
->vlc
, 1);
766 vl_vlc_needbits(&bs
->vlc
);
770 } else if (bs
->vlc
.buf
>= 0x04000000) {
772 tab
= DCT_B14_8
+ (vl_vlc_ubits(&bs
->vlc
, 8) - 4);
780 i
+= UBITS(bs
->vlc
.buf
<< 6, 6) - 64;
782 break; /* illegal, check needed to avoid buffer overflow */
786 vl_vlc_dumpbits(&bs
->vlc
, 12);
787 vl_vlc_needbits(&bs
->vlc
);
788 val
= (vl_vlc_sbits(&bs
->vlc
, 12) * quantizer_scale
* quant_matrix
[j
]) / 16;
794 vl_vlc_dumpbits(&bs
->vlc
, 12);
795 vl_vlc_needbits(&bs
->vlc
);
799 } else if (bs
->vlc
.buf
>= 0x02000000) {
800 tab
= DCT_B14_10
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 8);
804 } else if (bs
->vlc
.buf
>= 0x00800000) {
805 tab
= DCT_13
+ (vl_vlc_ubits(&bs
->vlc
, 13) - 16);
809 } else if (bs
->vlc
.buf
>= 0x00200000) {
810 tab
= DCT_15
+ (vl_vlc_ubits(&bs
->vlc
, 15) - 16);
815 tab
= DCT_16
+ vl_vlc_ubits(&bs
->vlc
, 16);
817 vl_vlc_getword(&bs
->vlc
, bs
->vlc
.bits
+ 16);
822 break; /* illegal, check needed to avoid buffer overflow */
825 dest
[63] ^= mismatch
& 1;
826 vl_vlc_dumpbits(&bs
->vlc
, 2); /* dump end of block code */
830 get_intra_block_B15(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
,
831 const int scan
[64], int quantizer_scale
, short *dest
)
834 uint8_t *quant_matrix
= picture
->intra_quantizer_matrix
;
841 vl_vlc_needbits(&bs
->vlc
);
844 if (bs
->vlc
.buf
>= 0x04000000) {
846 tab
= DCT_B15_8
+ (vl_vlc_ubits(&bs
->vlc
, 8) - 4);
853 bs
->vlc
.buf
<<= tab
->len
;
854 bs
->vlc
.bits
+= tab
->len
+ 1;
855 val
= (tab
->level
* quantizer_scale
* quant_matrix
[j
]) >> 4;
857 /* if (bitstream_get (1)) val = -val; */
858 val
= (val
^ vl_vlc_sbits(&bs
->vlc
, 1)) - vl_vlc_sbits(&bs
->vlc
, 1);
865 vl_vlc_needbits(&bs
->vlc
);
871 /* end of block. I commented out this code because if we */
872 /* dont exit here we will still exit at the later test :) */
874 /* if (i >= 128) break; */ /* end of block */
878 i
+= UBITS(bs
->vlc
.buf
<< 6, 6) - 64;
880 break; /* illegal, check against buffer overflow */
884 vl_vlc_dumpbits(&bs
->vlc
, 12);
885 vl_vlc_needbits(&bs
->vlc
);
886 val
= (vl_vlc_sbits(&bs
->vlc
, 12) * quantizer_scale
* quant_matrix
[j
]) / 16;
892 vl_vlc_dumpbits(&bs
->vlc
, 12);
893 vl_vlc_needbits(&bs
->vlc
);
898 } else if (bs
->vlc
.buf
>= 0x02000000) {
899 tab
= DCT_B15_10
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 8);
903 } else if (bs
->vlc
.buf
>= 0x00800000) {
904 tab
= DCT_13
+ (vl_vlc_ubits(&bs
->vlc
, 13) - 16);
908 } else if (bs
->vlc
.buf
>= 0x00200000) {
909 tab
= DCT_15
+ (vl_vlc_ubits(&bs
->vlc
, 15) - 16);
914 tab
= DCT_16
+ vl_vlc_ubits(&bs
->vlc
, 16);
916 vl_vlc_getword(&bs
->vlc
, bs
->vlc
.bits
+ 16);
921 break; /* illegal, check needed to avoid buffer overflow */
924 dest
[63] ^= mismatch
& 1;
925 vl_vlc_dumpbits(&bs
->vlc
, 4); /* dump end of block code */
929 get_non_intra_block(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
,
930 const int scan
[64], int quantizer_scale
, short *dest
)
933 uint8_t *quant_matrix
= picture
->non_intra_quantizer_matrix
;
940 vl_vlc_needbits(&bs
->vlc
);
941 if (bs
->vlc
.buf
>= 0x28000000) {
942 tab
= DCT_B14DC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
948 if (bs
->vlc
.buf
>= 0x28000000) {
950 tab
= DCT_B14AC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
955 break; /* end of block */
959 bs
->vlc
.buf
<<= tab
->len
;
960 bs
->vlc
.bits
+= tab
->len
+ 1;
961 val
= ((2*tab
->level
+1) * quantizer_scale
* quant_matrix
[j
]) >> 5;
963 /* if (bitstream_get (1)) val = -val; */
964 val
= (val
^ vl_vlc_sbits(&bs
->vlc
, 1)) - vl_vlc_sbits(&bs
->vlc
, 1);
971 vl_vlc_needbits(&bs
->vlc
);
978 if (bs
->vlc
.buf
>= 0x04000000) {
980 tab
= DCT_B14_8
+ (vl_vlc_ubits(&bs
->vlc
, 8) - 4);
988 i
+= UBITS(bs
->vlc
.buf
<< 6, 6) - 64;
990 break; /* illegal, check needed to avoid buffer overflow */
994 vl_vlc_dumpbits(&bs
->vlc
, 12);
995 vl_vlc_needbits(&bs
->vlc
);
996 val
= 2 * (vl_vlc_sbits(&bs
->vlc
, 12) + vl_vlc_sbits(&bs
->vlc
, 1)) + 1;
997 val
= (val
* quantizer_scale
* quant_matrix
[j
]) / 32;
1003 vl_vlc_dumpbits(&bs
->vlc
, 12);
1004 vl_vlc_needbits(&bs
->vlc
);
1008 } else if (bs
->vlc
.buf
>= 0x02000000) {
1009 tab
= DCT_B14_10
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 8);
1013 } else if (bs
->vlc
.buf
>= 0x00800000) {
1014 tab
= DCT_13
+ (vl_vlc_ubits(&bs
->vlc
, 13) - 16);
1018 } else if (bs
->vlc
.buf
>= 0x00200000) {
1019 tab
= DCT_15
+ (vl_vlc_ubits(&bs
->vlc
, 15) - 16);
1024 tab
= DCT_16
+ vl_vlc_ubits(&bs
->vlc
, 16);
1026 vl_vlc_getword(&bs
->vlc
, bs
->vlc
.bits
+ 16);
1031 break; /* illegal, check needed to avoid buffer overflow */
1033 dest
[63] ^= mismatch
& 1;
1034 vl_vlc_dumpbits(&bs
->vlc
, 2); /* dump end of block code */
1038 get_mpeg1_intra_block(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
,
1039 const int scan
[64], int quantizer_scale
, short *dest
)
1042 uint8_t *quant_matrix
= picture
->intra_quantizer_matrix
;
1047 vl_vlc_needbits(&bs
->vlc
);
1050 if (bs
->vlc
.buf
>= 0x28000000) {
1052 tab
= DCT_B14AC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
1056 break; /* end of block */
1060 bs
->vlc
.buf
<<= tab
->len
;
1061 bs
->vlc
.bits
+= tab
->len
+ 1;
1062 val
= (tab
->level
* quantizer_scale
* quant_matrix
[j
]) >> 4;
1065 val
= (val
- 1) | 1;
1067 /* if (bitstream_get (1)) val = -val; */
1068 val
= (val
^ vl_vlc_sbits(&bs
->vlc
, 1)) - vl_vlc_sbits(&bs
->vlc
, 1);
1074 vl_vlc_needbits(&bs
->vlc
);
1078 } else if (bs
->vlc
.buf
>= 0x04000000) {
1080 tab
= DCT_B14_8
+ (vl_vlc_ubits(&bs
->vlc
, 8) - 4);
1088 i
+= UBITS(bs
->vlc
.buf
<< 6, 6) - 64;
1090 break; /* illegal, check needed to avoid buffer overflow */
1094 vl_vlc_dumpbits(&bs
->vlc
, 12);
1095 vl_vlc_needbits(&bs
->vlc
);
1096 val
= vl_vlc_sbits(&bs
->vlc
, 8);
1097 if (! (val
& 0x7f)) {
1098 vl_vlc_dumpbits(&bs
->vlc
, 8);
1099 val
= vl_vlc_ubits(&bs
->vlc
, 8) + 2 * val
;
1101 val
= (val
* quantizer_scale
* quant_matrix
[j
]) / 16;
1104 val
= (val
+ ~SBITS (val
, 1)) | 1;
1109 vl_vlc_dumpbits(&bs
->vlc
, 8);
1110 vl_vlc_needbits(&bs
->vlc
);
1114 } else if (bs
->vlc
.buf
>= 0x02000000) {
1115 tab
= DCT_B14_10
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 8);
1119 } else if (bs
->vlc
.buf
>= 0x00800000) {
1120 tab
= DCT_13
+ (vl_vlc_ubits(&bs
->vlc
, 13) - 16);
1124 } else if (bs
->vlc
.buf
>= 0x00200000) {
1125 tab
= DCT_15
+ (vl_vlc_ubits(&bs
->vlc
, 15) - 16);
1130 tab
= DCT_16
+ vl_vlc_ubits(&bs
->vlc
, 16);
1132 vl_vlc_getword(&bs
->vlc
, bs
->vlc
.bits
+ 16);
1137 break; /* illegal, check needed to avoid buffer overflow */
1139 vl_vlc_dumpbits(&bs
->vlc
, 2); /* dump end of block code */
1143 get_mpeg1_non_intra_block(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
,
1144 const int scan
[64], int quantizer_scale
, short *dest
)
1147 uint8_t *quant_matrix
= picture
->non_intra_quantizer_matrix
;
1152 vl_vlc_needbits(&bs
->vlc
);
1153 if (bs
->vlc
.buf
>= 0x28000000) {
1154 tab
= DCT_B14DC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
1160 if (bs
->vlc
.buf
>= 0x28000000) {
1162 tab
= DCT_B14AC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
1167 break; /* end of block */
1171 bs
->vlc
.buf
<<= tab
->len
;
1172 bs
->vlc
.bits
+= tab
->len
+ 1;
1173 val
= ((2*tab
->level
+1) * quantizer_scale
* quant_matrix
[j
]) >> 5;
1176 val
= (val
- 1) | 1;
1178 /* if (bitstream_get (1)) val = -val; */
1179 val
= (val
^ vl_vlc_sbits(&bs
->vlc
, 1)) - vl_vlc_sbits(&bs
->vlc
, 1);
1185 vl_vlc_needbits(&bs
->vlc
);
1192 if (bs
->vlc
.buf
>= 0x04000000) {
1194 tab
= DCT_B14_8
+ (vl_vlc_ubits(&bs
->vlc
, 8) - 4);
1202 i
+= UBITS(bs
->vlc
.buf
<< 6, 6) - 64;
1204 break; /* illegal, check needed to avoid buffer overflow */
1208 vl_vlc_dumpbits(&bs
->vlc
, 12);
1209 vl_vlc_needbits(&bs
->vlc
);
1210 val
= vl_vlc_sbits(&bs
->vlc
, 8);
1211 if (! (val
& 0x7f)) {
1212 vl_vlc_dumpbits(&bs
->vlc
, 8);
1213 val
= vl_vlc_ubits(&bs
->vlc
, 8) + 2 * val
;
1215 val
= 2 * (val
+ SBITS (val
, 1)) + 1;
1216 val
= (val
* quantizer_scale
* quant_matrix
[j
]) / 32;
1219 val
= (val
+ ~SBITS (val
, 1)) | 1;
1224 vl_vlc_dumpbits(&bs
->vlc
, 8);
1225 vl_vlc_needbits(&bs
->vlc
);
1229 } else if (bs
->vlc
.buf
>= 0x02000000) {
1230 tab
= DCT_B14_10
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 8);
1234 } else if (bs
->vlc
.buf
>= 0x00800000) {
1235 tab
= DCT_13
+ (vl_vlc_ubits(&bs
->vlc
, 13) - 16);
1239 } else if (bs
->vlc
.buf
>= 0x00200000) {
1240 tab
= DCT_15
+ (vl_vlc_ubits(&bs
->vlc
, 15) - 16);
1245 tab
= DCT_16
+ vl_vlc_ubits(&bs
->vlc
, 16);
1247 vl_vlc_getword(&bs
->vlc
, bs
->vlc
.bits
+ 16);
1252 break; /* illegal, check needed to avoid buffer overflow */
1254 vl_vlc_dumpbits(&bs
->vlc
, 2); /* dump end of block code */
1258 slice_intra_DCT(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
, const int scan
[64], int cc
,
1259 unsigned x
, unsigned y
, enum pipe_mpeg12_dct_type coding
, int quantizer_scale
, int dc_dct_pred
[3])
1261 short *dest
= bs
->ycbcr_buffer
[cc
];
1263 bs
->ycbcr_stream
[cc
]->x
= x
;
1264 bs
->ycbcr_stream
[cc
]->y
= y
;
1265 bs
->ycbcr_stream
[cc
]->intra
= PIPE_MPEG12_DCT_INTRA
;
1266 bs
->ycbcr_stream
[cc
]->coding
= coding
;
1268 vl_vlc_needbits(&bs
->vlc
);
1270 /* Get the intra DC coefficient and inverse quantize it */
1272 dc_dct_pred
[0] += get_luma_dc_dct_diff(bs
);
1274 dc_dct_pred
[cc
] += get_chroma_dc_dct_diff(bs
);
1276 memset(dest
, 0, sizeof(int16_t) * 64);
1277 dest
[0] = dc_dct_pred
[cc
] << (3 - picture
->intra_dc_precision
);
1278 if (picture
->mpeg1
) {
1279 if (picture
->picture_coding_type
!= D_TYPE
)
1280 get_mpeg1_intra_block(bs
, picture
, scan
, quantizer_scale
, dest
);
1281 } else if (picture
->intra_vlc_format
)
1282 get_intra_block_B15(bs
, picture
, scan
, quantizer_scale
, dest
);
1284 get_intra_block_B14(bs
, picture
, scan
, quantizer_scale
, dest
);
1286 bs
->num_ycbcr_blocks
[cc
]++;
1287 bs
->ycbcr_stream
[cc
]++;
1288 bs
->ycbcr_buffer
[cc
] += 64;
1292 slice_non_intra_DCT(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
, const int scan
[64], int cc
,
1293 unsigned x
, unsigned y
, int quantizer_scale
, enum pipe_mpeg12_dct_type coding
)
1295 short *dest
= bs
->ycbcr_buffer
[cc
];
1297 bs
->ycbcr_stream
[cc
]->x
= x
;
1298 bs
->ycbcr_stream
[cc
]->y
= y
;
1299 bs
->ycbcr_stream
[cc
]->intra
= PIPE_MPEG12_DCT_DELTA
;
1300 bs
->ycbcr_stream
[cc
]->coding
= coding
;
1302 memset(dest
, 0, sizeof(int16_t) * 64);
1304 get_mpeg1_non_intra_block(bs
, picture
, scan
, quantizer_scale
, dest
);
1306 get_non_intra_block(bs
, picture
, scan
, quantizer_scale
, dest
);
1308 bs
->num_ycbcr_blocks
[cc
]++;
1309 bs
->ycbcr_stream
[cc
]++;
1310 bs
->ycbcr_buffer
[cc
] += 64;
1314 motion_mp1(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1316 int motion_x
, motion_y
;
1318 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1320 vl_vlc_needbits(&bs
->vlc
);
1321 motion_x
= (mv
->top
.x
+ (get_motion_delta(bs
, f_code
[0]) << f_code
[1]));
1322 motion_x
= bound_motion_vector (motion_x
, f_code
[0] + f_code
[1]);
1323 mv
->top
.x
= mv
->bottom
.x
= motion_x
;
1325 vl_vlc_needbits(&bs
->vlc
);
1326 motion_y
= (mv
->top
.y
+ (get_motion_delta(bs
, f_code
[0]) << f_code
[1]));
1327 motion_y
= bound_motion_vector (motion_y
, f_code
[0] + f_code
[1]);
1328 mv
->top
.y
= mv
->bottom
.y
= motion_y
;
1332 motion_fr_frame(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1334 int motion_x
, motion_y
;
1336 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1338 vl_vlc_needbits(&bs
->vlc
);
1339 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1340 motion_x
= bound_motion_vector(motion_x
, f_code
[0]);
1341 mv
->top
.x
= mv
->bottom
.x
= motion_x
;
1343 vl_vlc_needbits(&bs
->vlc
);
1344 motion_y
= mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]);
1345 motion_y
= bound_motion_vector(motion_y
, f_code
[1]);
1346 mv
->top
.y
= mv
->bottom
.y
= motion_y
;
1350 motion_fr_field(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1352 int motion_x
, motion_y
;
1354 vl_vlc_needbits(&bs
->vlc
);
1355 mv
->top
.field_select
= vl_vlc_ubits(&bs
->vlc
, 1) ?
1356 PIPE_VIDEO_BOTTOM_FIELD
: PIPE_VIDEO_TOP_FIELD
;
1357 vl_vlc_dumpbits(&bs
->vlc
, 1);
1359 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1360 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1361 mv
->top
.x
= motion_x
;
1363 vl_vlc_needbits(&bs
->vlc
);
1364 motion_y
= (mv
->top
.y
>> 1) + get_motion_delta(bs
, f_code
[1]);
1365 /* motion_y = bound_motion_vector (motion_y, f_code[1]); */
1366 mv
->top
.y
= motion_y
<< 1;
1368 vl_vlc_needbits(&bs
->vlc
);
1369 mv
->bottom
.field_select
= vl_vlc_ubits(&bs
->vlc
, 1) ?
1370 PIPE_VIDEO_BOTTOM_FIELD
: PIPE_VIDEO_TOP_FIELD
;
1371 vl_vlc_dumpbits(&bs
->vlc
, 1);
1373 motion_x
= mv
->bottom
.x
+ get_motion_delta(bs
, f_code
[0]);
1374 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1375 mv
->bottom
.x
= motion_x
;
1377 vl_vlc_needbits(&bs
->vlc
);
1378 motion_y
= (mv
->bottom
.y
>> 1) + get_motion_delta(bs
, f_code
[1]);
1379 /* motion_y = bound_motion_vector (motion_y, f_code[1]); */
1380 mv
->bottom
.y
= motion_y
<< 1;
1384 motion_fr_dmv(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1386 int motion_x
, motion_y
;
1388 // TODO Implement dmv
1389 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1391 vl_vlc_needbits(&bs
->vlc
);
1392 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1393 motion_x
= bound_motion_vector(motion_x
, f_code
[0]);
1394 mv
->top
.x
= mv
->bottom
.x
= motion_x
;
1396 vl_vlc_needbits(&bs
->vlc
);
1397 motion_y
= (mv
->top
.y
>> 1) + get_motion_delta(bs
, f_code
[1]);
1398 /* motion_y = bound_motion_vector (motion_y, f_code[1]); */
1399 mv
->top
.y
= mv
->bottom
.y
= motion_y
<< 1;
1402 /* like motion_frame, but parsing without actual motion compensation */
1404 motion_fr_conceal(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1408 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1410 vl_vlc_needbits(&bs
->vlc
);
1411 tmp
= (mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]));
1412 tmp
= bound_motion_vector (tmp
, f_code
[0]);
1413 mv
->top
.x
= mv
->bottom
.x
= tmp
;
1415 vl_vlc_needbits(&bs
->vlc
);
1416 tmp
= (mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]));
1417 tmp
= bound_motion_vector (tmp
, f_code
[1]);
1418 mv
->top
.y
= mv
->bottom
.y
= tmp
;
1420 vl_vlc_dumpbits(&bs
->vlc
, 1); /* remove marker_bit */
1424 motion_fi_field(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1426 int motion_x
, motion_y
;
1428 vl_vlc_needbits(&bs
->vlc
);
1431 //vl_vlc_ubits(&bs->vlc, 1);
1433 // TODO field select may need to do something here for bob (weave ok)
1434 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1435 vl_vlc_dumpbits(&bs
->vlc
, 1);
1437 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1438 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1439 mv
->top
.x
= mv
->bottom
.x
= motion_x
;
1441 vl_vlc_needbits(&bs
->vlc
);
1442 motion_y
= mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]);
1443 motion_y
= bound_motion_vector (motion_y
, f_code
[1]);
1444 mv
->top
.y
= mv
->bottom
.y
= motion_y
;
1448 motion_fi_16x8(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1450 int motion_x
, motion_y
;
1452 vl_vlc_needbits(&bs
->vlc
);
1455 //vl_vlc_ubits(&bs->vlc, 1);
1457 // TODO field select may need to do something here bob (weave ok)
1458 mv
->top
.field_select
= PIPE_VIDEO_FRAME
;
1459 vl_vlc_dumpbits(&bs
->vlc
, 1);
1461 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1462 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1463 mv
->top
.x
= motion_x
;
1465 vl_vlc_needbits(&bs
->vlc
);
1466 motion_y
= mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]);
1467 motion_y
= bound_motion_vector (motion_y
, f_code
[1]);
1468 mv
->top
.y
= motion_y
;
1470 vl_vlc_needbits(&bs
->vlc
);
1472 //vl_vlc_ubits(&bs->vlc, 1);
1474 // TODO field select may need to do something here for bob (weave ok)
1475 mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1476 vl_vlc_dumpbits(&bs
->vlc
, 1);
1478 motion_x
= mv
->bottom
.x
+ get_motion_delta(bs
, f_code
[0]);
1479 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1480 mv
->bottom
.x
= motion_x
;
1482 vl_vlc_needbits(&bs
->vlc
);
1483 motion_y
= mv
->bottom
.y
+ get_motion_delta(bs
, f_code
[1]);
1484 motion_y
= bound_motion_vector (motion_y
, f_code
[1]);
1485 mv
->bottom
.y
= motion_y
;
1489 motion_fi_dmv(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1491 int motion_x
, motion_y
;
1493 // TODO field select may need to do something here for bob (weave ok)
1494 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1496 vl_vlc_needbits(&bs
->vlc
);
1497 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1498 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1499 mv
->top
.x
= mv
->bottom
.x
= motion_x
;
1501 vl_vlc_needbits(&bs
->vlc
);
1502 motion_y
= mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]);
1503 motion_y
= bound_motion_vector (motion_y
, f_code
[1]);
1504 mv
->top
.y
= mv
->bottom
.y
= motion_y
;
1509 motion_fi_conceal(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1513 vl_vlc_needbits(&bs
->vlc
);
1514 vl_vlc_dumpbits(&bs
->vlc
, 1); /* remove field_select */
1516 tmp
= (mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]));
1517 tmp
= bound_motion_vector(tmp
, f_code
[0]);
1518 mv
->top
.x
= mv
->bottom
.x
= tmp
;
1520 vl_vlc_needbits(&bs
->vlc
);
1521 tmp
= (mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]));
1522 tmp
= bound_motion_vector(tmp
, f_code
[1]);
1523 mv
->top
.y
= mv
->bottom
.y
= tmp
;
1525 vl_vlc_dumpbits(&bs
->vlc
, 1); /* remove marker_bit */
1528 #define MOTION_CALL(routine, macroblock_modes) \
1530 if ((macroblock_modes) & MACROBLOCK_MOTION_FORWARD) \
1531 routine(bs, picture->f_code[0], &mv_fwd); \
1532 if ((macroblock_modes) & MACROBLOCK_MOTION_BACKWARD) \
1533 routine(bs, picture->f_code[1], &mv_bwd); \
1536 #define NEXT_MACROBLOCK \
1538 bs->mv_stream[0][x+y*bs->width] = mv_fwd; \
1539 bs->mv_stream[1][x+y*bs->width] = mv_bwd; \
1541 if (x == bs->width) { \
1543 if (y >= bs->height) \
1550 slice_init(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
,
1551 int *quantizer_scale
, int *x
, int *y
)
1555 vl_vlc_need32bits(&bs
->vlc
);
1556 while(bs
->vlc
.buf
< 0x101 || bs
->vlc
.buf
> 0x1AF) {
1557 if(!vl_vlc_getbyte(&bs
->vlc
))
1560 *y
= (bs
->vlc
.buf
& 0xFF) - 1;
1561 vl_vlc_restart(&bs
->vlc
);
1563 *quantizer_scale
= get_quantizer_scale(bs
, picture
);
1565 /* ignore intra_slice and all the extra data */
1566 while (bs
->vlc
.buf
& 0x80000000) {
1567 vl_vlc_dumpbits(&bs
->vlc
, 9);
1568 vl_vlc_needbits(&bs
->vlc
);
1571 /* decode initial macroblock address increment */
1574 if (bs
->vlc
.buf
>= 0x08000000) {
1575 mba
= MBA_5
+ (vl_vlc_ubits(&bs
->vlc
, 6) - 2);
1577 } else if (bs
->vlc
.buf
>= 0x01800000) {
1578 mba
= MBA_11
+ (vl_vlc_ubits(&bs
->vlc
, 12) - 24);
1580 } else switch (vl_vlc_ubits(&bs
->vlc
, 12)) {
1581 case 8: /* macroblock_escape */
1583 vl_vlc_dumpbits(&bs
->vlc
, 11);
1584 vl_vlc_needbits(&bs
->vlc
);
1586 case 15: /* macroblock_stuffing (MPEG1 only) */
1587 bs
->vlc
.buf
&= 0xfffff;
1588 vl_vlc_dumpbits(&bs
->vlc
, 11);
1589 vl_vlc_needbits(&bs
->vlc
);
1591 default: /* error */
1595 vl_vlc_dumpbits(&bs
->vlc
, mba
->len
+ 1);
1598 while (*x
>= bs
->width
) {
1602 if (*y
> bs
->height
)
1609 decode_slice(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
*picture
, const int scan
[64])
1611 struct pipe_motionvector mv_fwd
, mv_bwd
;
1612 enum pipe_mpeg12_dct_type dct_type
;
1614 /* predictor for DC coefficients in intra blocks */
1615 int dc_dct_pred
[3] = { 0, 0, 0 };
1616 int quantizer_scale
;
1620 if (!slice_init(bs
, picture
, &quantizer_scale
, &x
, &y
))
1623 mv_fwd
.top
.x
= mv_fwd
.top
.y
= mv_fwd
.bottom
.x
= mv_fwd
.bottom
.y
= 0;
1624 mv_fwd
.top
.field_select
= mv_fwd
.bottom
.field_select
= PIPE_VIDEO_FRAME
;
1626 mv_bwd
.top
.x
= mv_bwd
.top
.y
= mv_bwd
.bottom
.x
= mv_bwd
.bottom
.y
= 0;
1627 mv_bwd
.top
.field_select
= mv_bwd
.bottom
.field_select
= PIPE_VIDEO_FRAME
;
1630 int macroblock_modes
;
1634 vl_vlc_needbits(&bs
->vlc
);
1636 macroblock_modes
= get_macroblock_modes(bs
, picture
); //macroblock_modes()
1637 dct_type
= macroblock_modes
& DCT_TYPE_INTERLACED
?
1638 PIPE_MPEG12_DCT_TYPE_FIELD
: PIPE_MPEG12_DCT_TYPE_FRAME
;
1640 switch(macroblock_modes
& (MACROBLOCK_MOTION_FORWARD
|MACROBLOCK_MOTION_BACKWARD
)) {
1641 case (MACROBLOCK_MOTION_FORWARD
|MACROBLOCK_MOTION_BACKWARD
):
1642 mv_fwd
.top
.weight
= mv_fwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_HALF
;
1643 mv_bwd
.top
.weight
= mv_bwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_HALF
;
1647 case MACROBLOCK_MOTION_FORWARD
:
1648 mv_fwd
.top
.weight
= mv_fwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MAX
;
1649 mv_bwd
.top
.weight
= mv_bwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1652 case MACROBLOCK_MOTION_BACKWARD
:
1653 mv_fwd
.top
.weight
= mv_fwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1654 mv_bwd
.top
.weight
= mv_bwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MAX
;
1658 /* maybe integrate MACROBLOCK_QUANT test into get_macroblock_modes ? */
1659 if (macroblock_modes
& MACROBLOCK_QUANT
)
1660 quantizer_scale
= get_quantizer_scale(bs
, picture
);
1662 if (macroblock_modes
& MACROBLOCK_INTRA
) {
1664 if (picture
->concealment_motion_vectors
) {
1665 if (picture
->picture_structure
== FRAME_PICTURE
)
1666 motion_fr_conceal(bs
, picture
->f_code
[0], &mv_fwd
);
1668 motion_fi_conceal(bs
, picture
->f_code
[0], &mv_fwd
);
1671 mv_fwd
.top
.x
= mv_fwd
.top
.y
= mv_fwd
.bottom
.x
= mv_fwd
.bottom
.y
= 0;
1672 mv_bwd
.top
.x
= mv_bwd
.top
.y
= mv_bwd
.bottom
.x
= mv_bwd
.bottom
.y
= 0;
1674 mv_fwd
.top
.weight
= mv_fwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1675 mv_bwd
.top
.weight
= mv_bwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1677 // unravaled loop of 6 block(i) calls in macroblock()
1678 slice_intra_DCT(bs
, picture
, scan
, 0, x
*2+0, y
*2+0, dct_type
, quantizer_scale
, dc_dct_pred
);
1679 slice_intra_DCT(bs
, picture
, scan
, 0, x
*2+1, y
*2+0, dct_type
, quantizer_scale
, dc_dct_pred
);
1680 slice_intra_DCT(bs
, picture
, scan
, 0, x
*2+0, y
*2+1, dct_type
, quantizer_scale
, dc_dct_pred
);
1681 slice_intra_DCT(bs
, picture
, scan
, 0, x
*2+1, y
*2+1, dct_type
, quantizer_scale
, dc_dct_pred
);
1682 slice_intra_DCT(bs
, picture
, scan
, 1, x
, y
, dct_type
, quantizer_scale
, dc_dct_pred
);
1683 slice_intra_DCT(bs
, picture
, scan
, 2, x
, y
, dct_type
, quantizer_scale
, dc_dct_pred
);
1685 if (picture
->picture_coding_type
== D_TYPE
) {
1686 vl_vlc_needbits(&bs
->vlc
);
1687 vl_vlc_dumpbits(&bs
->vlc
, 1);
1691 if (picture
->picture_structure
== FRAME_PICTURE
)
1692 switch (macroblock_modes
& MOTION_TYPE_MASK
) {
1694 if (picture
->mpeg1
) {
1695 MOTION_CALL(motion_mp1
, macroblock_modes
);
1697 MOTION_CALL(motion_fr_frame
, macroblock_modes
);
1702 MOTION_CALL (motion_fr_field
, macroblock_modes
);
1706 MOTION_CALL (motion_fr_dmv
, MACROBLOCK_MOTION_FORWARD
);
1710 /* non-intra mb without forward mv in a P picture */
1711 mv_fwd
.top
.x
= mv_fwd
.top
.y
= mv_fwd
.bottom
.x
= mv_fwd
.bottom
.y
= 0;
1712 mv_bwd
.top
.x
= mv_bwd
.top
.y
= mv_bwd
.bottom
.x
= mv_bwd
.bottom
.y
= 0;
1716 switch (macroblock_modes
& MOTION_TYPE_MASK
) {
1718 MOTION_CALL (motion_fi_field
, macroblock_modes
);
1722 MOTION_CALL (motion_fi_16x8
, macroblock_modes
);
1726 MOTION_CALL (motion_fi_dmv
, MACROBLOCK_MOTION_FORWARD
);
1730 /* non-intra mb without forward mv in a P picture */
1731 mv_fwd
.top
.x
= mv_fwd
.top
.y
= mv_fwd
.bottom
.x
= mv_fwd
.bottom
.y
= 0;
1732 mv_bwd
.top
.x
= mv_bwd
.top
.y
= mv_bwd
.bottom
.x
= mv_bwd
.bottom
.y
= 0;
1736 if (macroblock_modes
& MACROBLOCK_PATTERN
) {
1737 int coded_block_pattern
= get_coded_block_pattern(bs
);
1739 // TODO optimize not fully used for idct accel only mc.
1740 if (coded_block_pattern
& 0x20)
1741 slice_non_intra_DCT(bs
, picture
, scan
, 0, x
*2+0, y
*2+0, quantizer_scale
, dct_type
); // cc0 luma 0
1742 if (coded_block_pattern
& 0x10)
1743 slice_non_intra_DCT(bs
, picture
, scan
, 0, x
*2+1, y
*2+0, quantizer_scale
, dct_type
); // cc0 luma 1
1744 if (coded_block_pattern
& 0x08)
1745 slice_non_intra_DCT(bs
, picture
, scan
, 0, x
*2+0, y
*2+1, quantizer_scale
, dct_type
); // cc0 luma 2
1746 if (coded_block_pattern
& 0x04)
1747 slice_non_intra_DCT(bs
, picture
, scan
, 0, x
*2+1, y
*2+1, quantizer_scale
, dct_type
); // cc0 luma 3
1748 if (coded_block_pattern
& 0x2)
1749 slice_non_intra_DCT(bs
, picture
, scan
, 1, x
, y
, quantizer_scale
, dct_type
); // cc1 croma
1750 if (coded_block_pattern
& 0x1)
1751 slice_non_intra_DCT(bs
, picture
, scan
, 2, x
, y
, quantizer_scale
, dct_type
); // cc2 croma
1754 dc_dct_pred
[0] = dc_dct_pred
[1] = dc_dct_pred
[2] = 0;
1759 vl_vlc_needbits(&bs
->vlc
);
1762 if (bs
->vlc
.buf
>= 0x10000000) {
1763 mba
= MBA_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 2);
1765 } else if (bs
->vlc
.buf
>= 0x03000000) {
1766 mba
= MBA_11
+ (vl_vlc_ubits(&bs
->vlc
, 11) - 24);
1768 } else switch (vl_vlc_ubits(&bs
->vlc
, 11)) {
1769 case 8: /* macroblock_escape */
1772 case 15: /* macroblock_stuffing (MPEG1 only) */
1773 vl_vlc_dumpbits(&bs
->vlc
, 11);
1774 vl_vlc_needbits(&bs
->vlc
);
1776 default: /* end of slice, or error */
1780 vl_vlc_dumpbits(&bs
->vlc
, mba
->len
);
1781 mba_inc
+= mba
->mba
;
1783 //TODO conversion to signed format signed format
1784 dc_dct_pred
[0] = dc_dct_pred
[1] = dc_dct_pred
[2] = 0;
1786 switch(picture
->picture_structure
) {
1788 mv_fwd
.top
.field_select
= mv_fwd
.bottom
.field_select
= PIPE_VIDEO_FRAME
;
1789 mv_bwd
.top
.field_select
= mv_bwd
.bottom
.field_select
= PIPE_VIDEO_FRAME
;
1793 mv_fwd
.top
.field_select
= mv_fwd
.bottom
.field_select
= PIPE_VIDEO_TOP_FIELD
;
1794 mv_bwd
.top
.field_select
= mv_bwd
.bottom
.field_select
= PIPE_VIDEO_TOP_FIELD
;
1798 mv_fwd
.top
.field_select
= mv_fwd
.bottom
.field_select
= PIPE_VIDEO_BOTTOM_FIELD
;
1799 mv_bwd
.top
.field_select
= mv_bwd
.bottom
.field_select
= PIPE_VIDEO_BOTTOM_FIELD
;
1803 if (picture
->picture_coding_type
== P_TYPE
) {
1804 mv_fwd
.top
.x
= mv_fwd
.top
.y
= mv_fwd
.bottom
.x
= mv_fwd
.bottom
.y
= 0;
1805 mv_fwd
.top
.weight
= mv_fwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MAX
;
1809 } while (--mba_inc
);
1815 vl_mpg12_bs_init(struct vl_mpg12_bs
*bs
, unsigned width
, unsigned height
)
1819 memset(bs
, 0, sizeof(struct vl_mpg12_bs
));
1822 bs
->height
= height
;
1826 vl_mpg12_bs_set_buffers(struct vl_mpg12_bs
*bs
, struct pipe_ycbcr_block
*ycbcr_stream
[VL_MAX_PLANES
],
1827 short *ycbcr_buffer
[VL_MAX_PLANES
], struct pipe_motionvector
*mv_stream
[VL_MAX_REF_FRAMES
])
1832 assert(ycbcr_stream
&& ycbcr_buffer
);
1835 for (i
= 0; i
< VL_MAX_PLANES
; ++i
) {
1836 bs
->ycbcr_stream
[i
] = ycbcr_stream
[i
];
1837 bs
->ycbcr_buffer
[i
] = ycbcr_buffer
[i
];
1839 for (i
= 0; i
< VL_MAX_REF_FRAMES
; ++i
)
1840 bs
->mv_stream
[i
] = mv_stream
[i
];
1843 for (i
= 0; i
< bs
->width
*bs
->height
; ++i
) {
1844 bs
->mv_stream
[0][i
].top
.x
= bs
->mv_stream
[0][i
].top
.y
= 0;
1845 bs
->mv_stream
[0][i
].top
.field_select
= PIPE_VIDEO_FRAME
;
1846 bs
->mv_stream
[0][i
].top
.weight
= PIPE_VIDEO_MV_WEIGHT_MAX
;
1847 bs
->mv_stream
[0][i
].bottom
.x
= bs
->mv_stream
[0][i
].bottom
.y
= 0;
1848 bs
->mv_stream
[0][i
].bottom
.field_select
= PIPE_VIDEO_FRAME
;
1849 bs
->mv_stream
[0][i
].bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MAX
;
1851 bs
->mv_stream
[1][i
].top
.x
= bs
->mv_stream
[1][i
].top
.y
= 0;
1852 bs
->mv_stream
[1][i
].top
.field_select
= PIPE_VIDEO_FRAME
;
1853 bs
->mv_stream
[1][i
].top
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1854 bs
->mv_stream
[1][i
].bottom
.x
= bs
->mv_stream
[1][i
].bottom
.y
= 0;
1855 bs
->mv_stream
[1][i
].bottom
.field_select
= PIPE_VIDEO_FRAME
;
1856 bs
->mv_stream
[1][i
].bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1861 vl_mpg12_bs_decode(struct vl_mpg12_bs
*bs
, unsigned num_bytes
, const void *buffer
,
1862 struct pipe_mpeg12_picture_desc
*picture
, unsigned num_ycbcr_blocks
[3])
1867 assert(num_ycbcr_blocks
);
1868 assert(buffer
&& num_bytes
);
1870 bs
->num_ycbcr_blocks
= num_ycbcr_blocks
;
1872 vl_vlc_init(&bs
->vlc
, buffer
, num_bytes
);
1874 scan
= picture
->alternate_scan
? vl_zscan_alternate
: vl_zscan_normal
;
1876 while(decode_slice(bs
, picture
, scan
));