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 #define SATURATE(val) \
68 if ((uint32_t)(val + 2048) > 4095) \
69 val = (val > 0) ? 2047 : -2048; \
72 /* macroblock modes */
73 #define MACROBLOCK_INTRA 1
74 #define MACROBLOCK_PATTERN 2
75 #define MACROBLOCK_MOTION_BACKWARD 4
76 #define MACROBLOCK_MOTION_FORWARD 8
77 #define MACROBLOCK_QUANT 16
78 #define DCT_TYPE_INTERLACED 32
81 #define MOTION_TYPE_MASK (3*64)
82 #define MOTION_TYPE_BASE 64
83 #define MC_FIELD (1*64)
84 #define MC_FRAME (2*64)
85 #define MC_16X8 (2*64)
88 /* picture structure */
90 #define BOTTOM_FIELD 2
91 #define FRAME_PICTURE 3
93 /* picture coding type (mpeg2 header) */
135 #define INTRA MACROBLOCK_INTRA
136 #define QUANT MACROBLOCK_QUANT
137 #define MC MACROBLOCK_MOTION_FORWARD
138 #define CODED MACROBLOCK_PATTERN
139 #define FWD MACROBLOCK_MOTION_FORWARD
140 #define BWD MACROBLOCK_MOTION_BACKWARD
141 #define INTER MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD
143 static const MBtab MB_I
[] = {
144 {INTRA
|QUANT
, 2}, {INTRA
, 1}
147 static const MBtab MB_P
[] = {
148 {INTRA
|QUANT
, 6}, {CODED
|QUANT
, 5}, {MC
|CODED
|QUANT
, 5}, {INTRA
, 5},
149 {MC
, 3}, {MC
, 3}, {MC
, 3}, {MC
, 3},
150 {CODED
, 2}, {CODED
, 2}, {CODED
, 2}, {CODED
, 2},
151 {CODED
, 2}, {CODED
, 2}, {CODED
, 2}, {CODED
, 2},
152 {MC
|CODED
, 1}, {MC
|CODED
, 1}, {MC
|CODED
, 1}, {MC
|CODED
, 1},
153 {MC
|CODED
, 1}, {MC
|CODED
, 1}, {MC
|CODED
, 1}, {MC
|CODED
, 1},
154 {MC
|CODED
, 1}, {MC
|CODED
, 1}, {MC
|CODED
, 1}, {MC
|CODED
, 1},
155 {MC
|CODED
, 1}, {MC
|CODED
, 1}, {MC
|CODED
, 1}, {MC
|CODED
, 1}
158 static const MBtab MB_B
[] = {
159 {0, 0}, {INTRA
|QUANT
, 6},
160 {BWD
|CODED
|QUANT
, 6}, {FWD
|CODED
|QUANT
, 6},
161 {INTER
|CODED
|QUANT
, 5}, {INTER
|CODED
|QUANT
, 5},
162 {INTRA
, 5}, {INTRA
, 5},
163 {FWD
, 4}, {FWD
, 4}, {FWD
, 4}, {FWD
, 4},
164 {FWD
|CODED
, 4}, {FWD
|CODED
, 4}, {FWD
|CODED
, 4}, {FWD
|CODED
, 4},
165 {BWD
, 3}, {BWD
, 3}, {BWD
, 3}, {BWD
, 3},
166 {BWD
, 3}, {BWD
, 3}, {BWD
, 3}, {BWD
, 3},
167 {BWD
|CODED
, 3}, {BWD
|CODED
, 3}, {BWD
|CODED
, 3}, {BWD
|CODED
, 3},
168 {BWD
|CODED
, 3}, {BWD
|CODED
, 3}, {BWD
|CODED
, 3}, {BWD
|CODED
, 3},
169 {INTER
, 2}, {INTER
, 2}, {INTER
, 2}, {INTER
, 2},
170 {INTER
, 2}, {INTER
, 2}, {INTER
, 2}, {INTER
, 2},
171 {INTER
, 2}, {INTER
, 2}, {INTER
, 2}, {INTER
, 2},
172 {INTER
, 2}, {INTER
, 2}, {INTER
, 2}, {INTER
, 2},
173 {INTER
|CODED
, 2}, {INTER
|CODED
, 2}, {INTER
|CODED
, 2}, {INTER
|CODED
, 2},
174 {INTER
|CODED
, 2}, {INTER
|CODED
, 2}, {INTER
|CODED
, 2}, {INTER
|CODED
, 2},
175 {INTER
|CODED
, 2}, {INTER
|CODED
, 2}, {INTER
|CODED
, 2}, {INTER
|CODED
, 2},
176 {INTER
|CODED
, 2}, {INTER
|CODED
, 2}, {INTER
|CODED
, 2}, {INTER
|CODED
, 2}
187 static const MVtab MV_4
[] = {
188 { 3, 6}, { 2, 4}, { 1, 3}, { 1, 3}, { 0, 2}, { 0, 2}, { 0, 2}, { 0, 2}
191 static const MVtab MV_10
[] = {
192 { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10},
193 { 0,10}, { 0,10}, { 0,10}, { 0,10}, {15,10}, {14,10}, {13,10}, {12,10},
194 {11,10}, {10,10}, { 9, 9}, { 9, 9}, { 8, 9}, { 8, 9}, { 7, 9}, { 7, 9},
195 { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7},
196 { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7},
197 { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}
200 static const DMVtab DMV_2
[] = {
201 { 0, 1}, { 0, 1}, { 1, 2}, {-1, 2}
204 static const CBPtab CBP_7
[] = {
205 {0x22, 7}, {0x12, 7}, {0x0a, 7}, {0x06, 7},
206 {0x21, 7}, {0x11, 7}, {0x09, 7}, {0x05, 7},
207 {0x3f, 6}, {0x3f, 6}, {0x03, 6}, {0x03, 6},
208 {0x24, 6}, {0x24, 6}, {0x18, 6}, {0x18, 6},
209 {0x3e, 5}, {0x3e, 5}, {0x3e, 5}, {0x3e, 5},
210 {0x02, 5}, {0x02, 5}, {0x02, 5}, {0x02, 5},
211 {0x3d, 5}, {0x3d, 5}, {0x3d, 5}, {0x3d, 5},
212 {0x01, 5}, {0x01, 5}, {0x01, 5}, {0x01, 5},
213 {0x38, 5}, {0x38, 5}, {0x38, 5}, {0x38, 5},
214 {0x34, 5}, {0x34, 5}, {0x34, 5}, {0x34, 5},
215 {0x2c, 5}, {0x2c, 5}, {0x2c, 5}, {0x2c, 5},
216 {0x1c, 5}, {0x1c, 5}, {0x1c, 5}, {0x1c, 5},
217 {0x28, 5}, {0x28, 5}, {0x28, 5}, {0x28, 5},
218 {0x14, 5}, {0x14, 5}, {0x14, 5}, {0x14, 5},
219 {0x30, 5}, {0x30, 5}, {0x30, 5}, {0x30, 5},
220 {0x0c, 5}, {0x0c, 5}, {0x0c, 5}, {0x0c, 5},
221 {0x20, 4}, {0x20, 4}, {0x20, 4}, {0x20, 4},
222 {0x20, 4}, {0x20, 4}, {0x20, 4}, {0x20, 4},
223 {0x10, 4}, {0x10, 4}, {0x10, 4}, {0x10, 4},
224 {0x10, 4}, {0x10, 4}, {0x10, 4}, {0x10, 4},
225 {0x08, 4}, {0x08, 4}, {0x08, 4}, {0x08, 4},
226 {0x08, 4}, {0x08, 4}, {0x08, 4}, {0x08, 4},
227 {0x04, 4}, {0x04, 4}, {0x04, 4}, {0x04, 4},
228 {0x04, 4}, {0x04, 4}, {0x04, 4}, {0x04, 4},
229 {0x3c, 3}, {0x3c, 3}, {0x3c, 3}, {0x3c, 3},
230 {0x3c, 3}, {0x3c, 3}, {0x3c, 3}, {0x3c, 3},
231 {0x3c, 3}, {0x3c, 3}, {0x3c, 3}, {0x3c, 3},
232 {0x3c, 3}, {0x3c, 3}, {0x3c, 3}, {0x3c, 3}
235 static const CBPtab CBP_9
[] = {
236 {0, 0}, {0x00, 9}, {0x27, 9}, {0x1b, 9},
237 {0x3b, 9}, {0x37, 9}, {0x2f, 9}, {0x1f, 9},
238 {0x3a, 8}, {0x3a, 8}, {0x36, 8}, {0x36, 8},
239 {0x2e, 8}, {0x2e, 8}, {0x1e, 8}, {0x1e, 8},
240 {0x39, 8}, {0x39, 8}, {0x35, 8}, {0x35, 8},
241 {0x2d, 8}, {0x2d, 8}, {0x1d, 8}, {0x1d, 8},
242 {0x26, 8}, {0x26, 8}, {0x1a, 8}, {0x1a, 8},
243 {0x25, 8}, {0x25, 8}, {0x19, 8}, {0x19, 8},
244 {0x2b, 8}, {0x2b, 8}, {0x17, 8}, {0x17, 8},
245 {0x33, 8}, {0x33, 8}, {0x0f, 8}, {0x0f, 8},
246 {0x2a, 8}, {0x2a, 8}, {0x16, 8}, {0x16, 8},
247 {0x32, 8}, {0x32, 8}, {0x0e, 8}, {0x0e, 8},
248 {0x29, 8}, {0x29, 8}, {0x15, 8}, {0x15, 8},
249 {0x31, 8}, {0x31, 8}, {0x0d, 8}, {0x0d, 8},
250 {0x23, 8}, {0x23, 8}, {0x13, 8}, {0x13, 8},
251 {0x0b, 8}, {0x0b, 8}, {0x07, 8}, {0x07, 8}
254 static const DCtab DC_lum_5
[] = {
255 {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
256 {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
257 {0, 3}, {0, 3}, {0, 3}, {0, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3},
258 {4, 3}, {4, 3}, {4, 3}, {4, 3}, {5, 4}, {5, 4}, {6, 5}
261 static const DCtab DC_chrom_5
[] = {
262 {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2},
263 {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
264 {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
265 {3, 3}, {3, 3}, {3, 3}, {3, 3}, {4, 4}, {4, 4}, {5, 5}
268 static const DCtab DC_long
[] = {
269 {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, { 6, 5}, { 6, 5},
270 {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, { 6, 5}, { 6, 5},
271 {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, { 7, 6}, { 7, 6},
272 {8, 7}, {8, 7}, {8, 7}, {8, 7}, {9, 8}, {9, 8}, {10, 9}, {11, 9}
275 static const DCTtab DCT_16
[] = {
276 {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0},
277 {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0},
278 {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0},
279 {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0},
280 { 2,18, 0}, { 2,17, 0}, { 2,16, 0}, { 2,15, 0},
281 { 7, 3, 0}, { 17, 2, 0}, { 16, 2, 0}, { 15, 2, 0},
282 { 14, 2, 0}, { 13, 2, 0}, { 12, 2, 0}, { 32, 1, 0},
283 { 31, 1, 0}, { 30, 1, 0}, { 29, 1, 0}, { 28, 1, 0}
286 static const DCTtab DCT_15
[] = {
287 { 1,40,15}, { 1,39,15}, { 1,38,15}, { 1,37,15},
288 { 1,36,15}, { 1,35,15}, { 1,34,15}, { 1,33,15},
289 { 1,32,15}, { 2,14,15}, { 2,13,15}, { 2,12,15},
290 { 2,11,15}, { 2,10,15}, { 2, 9,15}, { 2, 8,15},
291 { 1,31,14}, { 1,31,14}, { 1,30,14}, { 1,30,14},
292 { 1,29,14}, { 1,29,14}, { 1,28,14}, { 1,28,14},
293 { 1,27,14}, { 1,27,14}, { 1,26,14}, { 1,26,14},
294 { 1,25,14}, { 1,25,14}, { 1,24,14}, { 1,24,14},
295 { 1,23,14}, { 1,23,14}, { 1,22,14}, { 1,22,14},
296 { 1,21,14}, { 1,21,14}, { 1,20,14}, { 1,20,14},
297 { 1,19,14}, { 1,19,14}, { 1,18,14}, { 1,18,14},
298 { 1,17,14}, { 1,17,14}, { 1,16,14}, { 1,16,14}
301 static const DCTtab DCT_13
[] = {
302 { 11, 2,13}, { 10, 2,13}, { 6, 3,13}, { 4, 4,13},
303 { 3, 5,13}, { 2, 7,13}, { 2, 6,13}, { 1,15,13},
304 { 1,14,13}, { 1,13,13}, { 1,12,13}, { 27, 1,13},
305 { 26, 1,13}, { 25, 1,13}, { 24, 1,13}, { 23, 1,13},
306 { 1,11,12}, { 1,11,12}, { 9, 2,12}, { 9, 2,12},
307 { 5, 3,12}, { 5, 3,12}, { 1,10,12}, { 1,10,12},
308 { 3, 4,12}, { 3, 4,12}, { 8, 2,12}, { 8, 2,12},
309 { 22, 1,12}, { 22, 1,12}, { 21, 1,12}, { 21, 1,12},
310 { 1, 9,12}, { 1, 9,12}, { 20, 1,12}, { 20, 1,12},
311 { 19, 1,12}, { 19, 1,12}, { 2, 5,12}, { 2, 5,12},
312 { 4, 3,12}, { 4, 3,12}, { 1, 8,12}, { 1, 8,12},
313 { 7, 2,12}, { 7, 2,12}, { 18, 1,12}, { 18, 1,12}
316 static const DCTtab DCT_B14_10
[] = {
317 { 17, 1,10}, { 6, 2,10}, { 1, 7,10}, { 3, 3,10},
318 { 2, 4,10}, { 16, 1,10}, { 15, 1,10}, { 5, 2,10}
321 static const DCTtab DCT_B14_8
[] = {
322 { 65, 0, 6}, { 65, 0, 6}, { 65, 0, 6}, { 65, 0, 6},
323 { 3, 2, 7}, { 3, 2, 7}, { 10, 1, 7}, { 10, 1, 7},
324 { 1, 4, 7}, { 1, 4, 7}, { 9, 1, 7}, { 9, 1, 7},
325 { 8, 1, 6}, { 8, 1, 6}, { 8, 1, 6}, { 8, 1, 6},
326 { 7, 1, 6}, { 7, 1, 6}, { 7, 1, 6}, { 7, 1, 6},
327 { 2, 2, 6}, { 2, 2, 6}, { 2, 2, 6}, { 2, 2, 6},
328 { 6, 1, 6}, { 6, 1, 6}, { 6, 1, 6}, { 6, 1, 6},
329 { 14, 1, 8}, { 1, 6, 8}, { 13, 1, 8}, { 12, 1, 8},
330 { 4, 2, 8}, { 2, 3, 8}, { 1, 5, 8}, { 11, 1, 8}
333 static const DCTtab DCT_B14AC_5
[] = {
334 { 1, 3, 5}, { 5, 1, 5}, { 4, 1, 5},
335 { 1, 2, 4}, { 1, 2, 4}, { 3, 1, 4}, { 3, 1, 4},
336 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
337 {129, 0, 2}, {129, 0, 2}, {129, 0, 2}, {129, 0, 2},
338 {129, 0, 2}, {129, 0, 2}, {129, 0, 2}, {129, 0, 2},
339 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
340 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}
343 static const DCTtab DCT_B14DC_5
[] = {
344 { 1, 3, 5}, { 5, 1, 5}, { 4, 1, 5},
345 { 1, 2, 4}, { 1, 2, 4}, { 3, 1, 4}, { 3, 1, 4},
346 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
347 { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1},
348 { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1},
349 { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1},
350 { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}
353 static const DCTtab DCT_B15_10
[] = {
354 { 6, 2, 9}, { 6, 2, 9}, { 15, 1, 9}, { 15, 1, 9},
355 { 3, 4,10}, { 17, 1,10}, { 16, 1, 9}, { 16, 1, 9}
358 static const DCTtab DCT_B15_8
[] = {
359 { 65, 0, 6}, { 65, 0, 6}, { 65, 0, 6}, { 65, 0, 6},
360 { 8, 1, 7}, { 8, 1, 7}, { 9, 1, 7}, { 9, 1, 7},
361 { 7, 1, 7}, { 7, 1, 7}, { 3, 2, 7}, { 3, 2, 7},
362 { 1, 7, 6}, { 1, 7, 6}, { 1, 7, 6}, { 1, 7, 6},
363 { 1, 6, 6}, { 1, 6, 6}, { 1, 6, 6}, { 1, 6, 6},
364 { 5, 1, 6}, { 5, 1, 6}, { 5, 1, 6}, { 5, 1, 6},
365 { 6, 1, 6}, { 6, 1, 6}, { 6, 1, 6}, { 6, 1, 6},
366 { 2, 5, 8}, { 12, 1, 8}, { 1,11, 8}, { 1,10, 8},
367 { 14, 1, 8}, { 13, 1, 8}, { 4, 2, 8}, { 2, 4, 8},
368 { 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5},
369 { 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5},
370 { 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5},
371 { 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5},
372 { 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5},
373 { 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5},
374 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
375 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
376 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
377 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
378 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
379 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
380 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
381 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
382 {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4},
383 {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4},
384 {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4},
385 {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4},
386 { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4},
387 { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4},
388 { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4},
389 { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4},
390 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
391 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
392 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
393 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
394 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
395 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
396 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
397 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
398 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
399 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
400 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
401 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
402 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
403 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
404 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
405 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
406 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
407 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
408 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
409 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
410 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
411 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
412 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
413 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
414 { 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5},
415 { 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5},
416 { 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5},
417 { 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5},
418 { 10, 1, 7}, { 10, 1, 7}, { 2, 3, 7}, { 2, 3, 7},
419 { 11, 1, 7}, { 11, 1, 7}, { 1, 8, 7}, { 1, 8, 7},
420 { 1, 9, 7}, { 1, 9, 7}, { 1,12, 8}, { 1,13, 8},
421 { 3, 3, 8}, { 5, 2, 8}, { 1,14, 8}, { 1,15, 8}
424 static const MBAtab MBA_5
[] = {
425 {6, 5}, {5, 5}, {4, 4}, {4, 4}, {3, 4}, {3, 4},
426 {2, 3}, {2, 3}, {2, 3}, {2, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3},
427 {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1},
428 {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}
431 static const MBAtab MBA_11
[] = {
432 {32, 11}, {31, 11}, {30, 11}, {29, 11},
433 {28, 11}, {27, 11}, {26, 11}, {25, 11},
434 {24, 11}, {23, 11}, {22, 11}, {21, 11},
435 {20, 10}, {20, 10}, {19, 10}, {19, 10},
436 {18, 10}, {18, 10}, {17, 10}, {17, 10},
437 {16, 10}, {16, 10}, {15, 10}, {15, 10},
438 {14, 8}, {14, 8}, {14, 8}, {14, 8},
439 {14, 8}, {14, 8}, {14, 8}, {14, 8},
440 {13, 8}, {13, 8}, {13, 8}, {13, 8},
441 {13, 8}, {13, 8}, {13, 8}, {13, 8},
442 {12, 8}, {12, 8}, {12, 8}, {12, 8},
443 {12, 8}, {12, 8}, {12, 8}, {12, 8},
444 {11, 8}, {11, 8}, {11, 8}, {11, 8},
445 {11, 8}, {11, 8}, {11, 8}, {11, 8},
446 {10, 8}, {10, 8}, {10, 8}, {10, 8},
447 {10, 8}, {10, 8}, {10, 8}, {10, 8},
448 { 9, 8}, { 9, 8}, { 9, 8}, { 9, 8},
449 { 9, 8}, { 9, 8}, { 9, 8}, { 9, 8},
450 { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7},
451 { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7},
452 { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7},
453 { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7},
454 { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7},
455 { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7},
456 { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7},
457 { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7}
460 /* original (non-patched) scan tables */
461 static const uint8_t mpeg2_scan_norm_orig
[64] =
463 /* Zig-Zag scan pattern */
464 0, 1, 8,16, 9, 2, 3,10,
465 17,24,32,25,18,11, 4, 5,
466 12,19,26,33,40,48,41,34,
467 27,20,13, 6, 7,14,21,28,
468 35,42,49,56,57,50,43,36,
469 29,22,15,23,30,37,44,51,
470 58,59,52,45,38,31,39,46,
471 53,60,61,54,47,55,62,63
474 static const uint8_t mpeg2_scan_alt_orig
[64] =
476 /* Alternate scan pattern */
477 0,8,16,24,1,9,2,10,17,25,32,40,48,56,57,49,
478 41,33,26,18,3,11,4,12,19,27,34,42,50,58,35,43,
479 51,59,20,28,5,13,6,14,21,29,36,44,52,60,37,45,
480 53,61,22,30,7,15,23,31,38,46,54,62,39,47,55,63
483 static uint8_t mpeg2_scan_alt_ptable
[64];
484 static uint8_t mpeg2_scan_norm_ptable
[64];
485 static uint8_t mpeg2_scan_orig_ptable
[64];
488 setup_scan_ptable( void )
491 for (i
=0; i
<64; ++i
) {
492 mpeg2_scan_norm_ptable
[mpeg2_scan_norm_orig
[i
]] = mpeg2_scan_norm_orig
[i
];
493 mpeg2_scan_alt_ptable
[mpeg2_scan_alt_orig
[i
]] = mpeg2_scan_alt_orig
[i
];
494 mpeg2_scan_orig_ptable
[i
] = i
;
498 static const int non_linear_quantizer_scale
[] = {
499 0, 1, 2, 3, 4, 5, 6, 7,
500 8, 10, 12, 14, 16, 18, 20, 22,
501 24, 28, 32, 36, 40, 44, 48, 52,
502 56, 64, 72, 80, 88, 96, 104, 112
506 get_macroblock_modes(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
)
508 int macroblock_modes
;
511 switch (picture
->picture_coding_type
) {
514 tab
= MB_I
+ vl_vlc_ubits(&bs
->vlc
, 1);
515 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
516 macroblock_modes
= tab
->modes
;
518 if ((!(picture
->frame_pred_frame_dct
)) && (picture
->picture_structure
== FRAME_PICTURE
)) {
519 macroblock_modes
|= vl_vlc_ubits(&bs
->vlc
, 1) * DCT_TYPE_INTERLACED
;
520 vl_vlc_dumpbits(&bs
->vlc
, 1);
523 return macroblock_modes
;
527 tab
= MB_P
+ vl_vlc_ubits(&bs
->vlc
, 5);
528 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
529 macroblock_modes
= tab
->modes
;
531 if (picture
->picture_structure
!= FRAME_PICTURE
) {
532 if (macroblock_modes
& MACROBLOCK_MOTION_FORWARD
) {
533 macroblock_modes
|= vl_vlc_ubits(&bs
->vlc
, 2) * MOTION_TYPE_BASE
;
534 vl_vlc_dumpbits(&bs
->vlc
, 2);
536 return macroblock_modes
;
537 } else if (picture
->frame_pred_frame_dct
) {
538 if (macroblock_modes
& MACROBLOCK_MOTION_FORWARD
)
539 macroblock_modes
|= MC_FRAME
;
540 return macroblock_modes
;
542 if (macroblock_modes
& MACROBLOCK_MOTION_FORWARD
) {
543 macroblock_modes
|= vl_vlc_ubits(&bs
->vlc
, 2) * MOTION_TYPE_BASE
;
544 vl_vlc_dumpbits(&bs
->vlc
, 2);
546 if (macroblock_modes
& (MACROBLOCK_INTRA
| MACROBLOCK_PATTERN
)) {
547 macroblock_modes
|= vl_vlc_ubits(&bs
->vlc
, 1) * DCT_TYPE_INTERLACED
;
548 vl_vlc_dumpbits(&bs
->vlc
, 1);
550 return macroblock_modes
;
555 tab
= MB_B
+ vl_vlc_ubits(&bs
->vlc
, 6);
556 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
557 macroblock_modes
= tab
->modes
;
559 if (picture
->picture_structure
!= FRAME_PICTURE
) {
560 if (! (macroblock_modes
& MACROBLOCK_INTRA
)) {
561 macroblock_modes
|= vl_vlc_ubits(&bs
->vlc
, 2) * MOTION_TYPE_BASE
;
562 vl_vlc_dumpbits(&bs
->vlc
, 2);
564 return macroblock_modes
;
565 } else if (picture
->frame_pred_frame_dct
) {
566 /* if (! (macroblock_modes & MACROBLOCK_INTRA)) */
567 macroblock_modes
|= MC_FRAME
;
568 return macroblock_modes
;
570 if (macroblock_modes
& MACROBLOCK_INTRA
)
572 macroblock_modes
|= vl_vlc_ubits(&bs
->vlc
, 2) * MOTION_TYPE_BASE
;
573 vl_vlc_dumpbits(&bs
->vlc
, 2);
574 if (macroblock_modes
& (MACROBLOCK_INTRA
| MACROBLOCK_PATTERN
)) {
576 macroblock_modes
|= vl_vlc_ubits(&bs
->vlc
, 1) * DCT_TYPE_INTERLACED
;
577 vl_vlc_dumpbits(&bs
->vlc
, 1);
579 return macroblock_modes
;
584 vl_vlc_dumpbits(&bs
->vlc
, 1);
585 return MACROBLOCK_INTRA
;
593 get_quantizer_scale(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
)
595 int quantizer_scale_code
;
597 quantizer_scale_code
= vl_vlc_ubits(&bs
->vlc
, 5);
598 vl_vlc_dumpbits(&bs
->vlc
, 5);
600 if (picture
->q_scale_type
)
601 return non_linear_quantizer_scale
[quantizer_scale_code
];
603 return quantizer_scale_code
<< 1;
607 get_motion_delta(struct vl_mpg12_bs
*bs
, unsigned f_code
)
613 if (bs
->vlc
.buf
& 0x80000000) {
614 vl_vlc_dumpbits(&bs
->vlc
, 1);
616 } else if (bs
->vlc
.buf
>= 0x0c000000) {
618 tab
= MV_4
+ vl_vlc_ubits(&bs
->vlc
, 4);
619 delta
= (tab
->delta
<< f_code
) + 1;
620 bs
->vlc
.bits
+= tab
->len
+ f_code
+ 1;
621 bs
->vlc
.buf
<<= tab
->len
;
623 sign
= vl_vlc_sbits(&bs
->vlc
, 1);
627 delta
+= vl_vlc_ubits(&bs
->vlc
, f_code
);
628 bs
->vlc
.buf
<<= f_code
;
630 return (delta
^ sign
) - sign
;
634 tab
= MV_10
+ vl_vlc_ubits(&bs
->vlc
, 10);
635 delta
= (tab
->delta
<< f_code
) + 1;
636 bs
->vlc
.bits
+= tab
->len
+ 1;
637 bs
->vlc
.buf
<<= tab
->len
;
639 sign
= vl_vlc_sbits(&bs
->vlc
, 1);
643 vl_vlc_needbits(&bs
->vlc
);
644 delta
+= vl_vlc_ubits(&bs
->vlc
, f_code
);
645 vl_vlc_dumpbits(&bs
->vlc
, f_code
);
648 return (delta
^ sign
) - sign
;
653 bound_motion_vector(int vec
, unsigned f_code
)
659 limit
= 16 << f_code
;
661 if ((unsigned int)(vec
+ limit
) < 2 * limit
)
664 sign
= ((int32_t)vec
) >> 31;
665 return vec
- ((2 * limit
) ^ sign
) + sign
;
668 return ((int32_t)vec
<< (28 - f_code
)) >> (28 - f_code
);
673 get_dmv(struct vl_mpg12_bs
*bs
)
677 tab
= DMV_2
+ vl_vlc_ubits(&bs
->vlc
, 2);
678 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
683 get_coded_block_pattern(struct vl_mpg12_bs
*bs
)
687 vl_vlc_needbits(&bs
->vlc
);
689 if (bs
->vlc
.buf
>= 0x20000000) {
691 tab
= CBP_7
+ (vl_vlc_ubits(&bs
->vlc
, 7) - 16);
692 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
697 tab
= CBP_9
+ vl_vlc_ubits(&bs
->vlc
, 9);
698 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
704 get_luma_dc_dct_diff(struct vl_mpg12_bs
*bs
)
710 if (bs
->vlc
.buf
< 0xf8000000) {
711 tab
= DC_lum_5
+ vl_vlc_ubits(&bs
->vlc
, 5);
714 bs
->vlc
.bits
+= tab
->len
+ size
;
715 bs
->vlc
.buf
<<= tab
->len
;
716 dc_diff
= vl_vlc_ubits(&bs
->vlc
, size
) - UBITS (SBITS (~bs
->vlc
.buf
, 1), size
);
717 bs
->vlc
.buf
<<= size
;
720 vl_vlc_dumpbits(&bs
->vlc
, 3);
724 tab
= DC_long
+ (vl_vlc_ubits(&bs
->vlc
, 9) - 0x1e0);
726 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
727 vl_vlc_needbits(&bs
->vlc
);
728 dc_diff
= vl_vlc_ubits(&bs
->vlc
, size
) - UBITS (SBITS (~bs
->vlc
.buf
, 1), size
);
729 vl_vlc_dumpbits(&bs
->vlc
, size
);
735 get_chroma_dc_dct_diff(struct vl_mpg12_bs
*bs
)
741 if (bs
->vlc
.buf
< 0xf8000000) {
742 tab
= DC_chrom_5
+ vl_vlc_ubits(&bs
->vlc
, 5);
745 bs
->vlc
.bits
+= tab
->len
+ size
;
746 bs
->vlc
.buf
<<= tab
->len
;
747 dc_diff
= vl_vlc_ubits(&bs
->vlc
, size
) - UBITS (SBITS (~bs
->vlc
.buf
, 1), size
);
748 bs
->vlc
.buf
<<= size
;
751 vl_vlc_dumpbits(&bs
->vlc
, 2);
755 tab
= DC_long
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 0x3e0);
757 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
+ 1);
758 vl_vlc_needbits(&bs
->vlc
);
759 dc_diff
= vl_vlc_ubits(&bs
->vlc
, size
) - UBITS (SBITS (~bs
->vlc
.buf
, 1), size
);
760 vl_vlc_dumpbits(&bs
->vlc
, size
);
766 get_intra_block_B14(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
, short *dest
)
770 uint8_t *scan_ptable
;
771 uint8_t *quant_matrix
= picture
->intra_quantizer_matrix
;
772 int quantizer_scale
= picture
->quantizer_scale
;
776 if (!picture
->alternate_scan
) {
777 scan
= mpeg2_scan_norm_orig
;
778 scan_ptable
= mpeg2_scan_norm_ptable
;
780 scan
= mpeg2_scan_alt_orig
;
781 scan_ptable
= mpeg2_scan_alt_ptable
;
787 vl_vlc_needbits(&bs
->vlc
);
790 if (bs
->vlc
.buf
>= 0x28000000) {
792 tab
= DCT_B14AC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
796 break; /* end of block */
799 l
= scan_ptable
[j
= scan
[i
]];
801 bs
->vlc
.buf
<<= tab
->len
;
802 bs
->vlc
.bits
+= tab
->len
+ 1;
803 val
= (tab
->level
* quantizer_scale
* quant_matrix
[l
]) >> 4;
805 /* if (bitstream_get (1)) val = -val; */
806 val
= (val
^ vl_vlc_sbits(&bs
->vlc
, 1)) - vl_vlc_sbits(&bs
->vlc
, 1);
813 vl_vlc_needbits(&bs
->vlc
);
817 } else if (bs
->vlc
.buf
>= 0x04000000) {
819 tab
= DCT_B14_8
+ (vl_vlc_ubits(&bs
->vlc
, 8) - 4);
827 i
+= UBITS(bs
->vlc
.buf
<< 6, 6) - 64;
829 break; /* illegal, check needed to avoid buffer overflow */
831 l
= scan_ptable
[j
= scan
[i
]];
833 vl_vlc_dumpbits(&bs
->vlc
, 12);
834 vl_vlc_needbits(&bs
->vlc
);
835 val
= (vl_vlc_sbits(&bs
->vlc
, 12) * quantizer_scale
* quant_matrix
[l
]) / 16;
841 vl_vlc_dumpbits(&bs
->vlc
, 12);
842 vl_vlc_needbits(&bs
->vlc
);
846 } else if (bs
->vlc
.buf
>= 0x02000000) {
847 tab
= DCT_B14_10
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 8);
851 } else if (bs
->vlc
.buf
>= 0x00800000) {
852 tab
= DCT_13
+ (vl_vlc_ubits(&bs
->vlc
, 13) - 16);
856 } else if (bs
->vlc
.buf
>= 0x00200000) {
857 tab
= DCT_15
+ (vl_vlc_ubits(&bs
->vlc
, 15) - 16);
862 tab
= DCT_16
+ vl_vlc_ubits(&bs
->vlc
, 16);
864 vl_vlc_getword(&bs
->vlc
, bs
->vlc
.bits
+ 16);
869 break; /* illegal, check needed to avoid buffer overflow */
872 dest
[63] ^= mismatch
& 1;
873 vl_vlc_dumpbits(&bs
->vlc
, 2); /* dump end of block code */
877 get_intra_block_B15(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
, short *dest
)
881 uint8_t *scan_ptable
;
882 uint8_t *quant_matrix
= picture
->intra_quantizer_matrix
;
883 int quantizer_scale
= picture
->quantizer_scale
;
887 if (!picture
->alternate_scan
) {
888 scan
= mpeg2_scan_norm_orig
;
889 scan_ptable
= mpeg2_scan_norm_ptable
;
891 scan
= mpeg2_scan_alt_orig
;
892 scan_ptable
= mpeg2_scan_alt_ptable
;
898 vl_vlc_needbits(&bs
->vlc
);
901 if (bs
->vlc
.buf
>= 0x04000000) {
903 tab
= DCT_B15_8
+ (vl_vlc_ubits(&bs
->vlc
, 8) - 4);
909 l
= scan_ptable
[j
= scan
[i
]];
910 bs
->vlc
.buf
<<= tab
->len
;
911 bs
->vlc
.bits
+= tab
->len
+ 1;
912 val
= (tab
->level
* quantizer_scale
* quant_matrix
[l
]) >> 4;
914 /* if (bitstream_get (1)) val = -val; */
915 val
= (val
^ vl_vlc_sbits(&bs
->vlc
, 1)) - vl_vlc_sbits(&bs
->vlc
, 1);
922 vl_vlc_needbits(&bs
->vlc
);
928 /* end of block. I commented out this code because if we */
929 /* dont exit here we will still exit at the later test :) */
931 /* if (i >= 128) break; */ /* end of block */
935 i
+= UBITS(bs
->vlc
.buf
<< 6, 6) - 64;
937 break; /* illegal, check against buffer overflow */
939 l
= scan_ptable
[j
= scan
[i
]];
941 vl_vlc_dumpbits(&bs
->vlc
, 12);
942 vl_vlc_needbits(&bs
->vlc
);
943 val
= (vl_vlc_sbits(&bs
->vlc
, 12) * quantizer_scale
* quant_matrix
[l
]) / 16;
949 vl_vlc_dumpbits(&bs
->vlc
, 12);
950 vl_vlc_needbits(&bs
->vlc
);
955 } else if (bs
->vlc
.buf
>= 0x02000000) {
956 tab
= DCT_B15_10
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 8);
960 } else if (bs
->vlc
.buf
>= 0x00800000) {
961 tab
= DCT_13
+ (vl_vlc_ubits(&bs
->vlc
, 13) - 16);
965 } else if (bs
->vlc
.buf
>= 0x00200000) {
966 tab
= DCT_15
+ (vl_vlc_ubits(&bs
->vlc
, 15) - 16);
971 tab
= DCT_16
+ vl_vlc_ubits(&bs
->vlc
, 16);
973 vl_vlc_getword(&bs
->vlc
, bs
->vlc
.bits
+ 16);
978 break; /* illegal, check needed to avoid buffer overflow */
981 dest
[63] ^= mismatch
& 1;
982 vl_vlc_dumpbits(&bs
->vlc
, 4); /* dump end of block code */
986 get_non_intra_block(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
, short *dest
)
990 uint8_t *scan_ptable
;
991 uint8_t *quant_matrix
= picture
->non_intra_quantizer_matrix
;
992 int quantizer_scale
= picture
->quantizer_scale
;
999 if (!picture
->alternate_scan
) {
1000 scan
= mpeg2_scan_norm_orig
;
1001 scan_ptable
= mpeg2_scan_norm_ptable
;
1003 scan
= mpeg2_scan_alt_orig
;
1004 scan_ptable
= mpeg2_scan_alt_ptable
;
1007 vl_vlc_needbits(&bs
->vlc
);
1008 if (bs
->vlc
.buf
>= 0x28000000) {
1009 tab
= DCT_B14DC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
1015 if (bs
->vlc
.buf
>= 0x28000000) {
1017 tab
= DCT_B14AC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
1022 break; /* end of block */
1025 l
= scan_ptable
[j
= scan
[i
]];
1026 bs
->vlc
.buf
<<= tab
->len
;
1027 bs
->vlc
.bits
+= tab
->len
+ 1;
1028 val
= ((2*tab
->level
+1) * quantizer_scale
* quant_matrix
[l
]) >> 5;
1030 /* if (bitstream_get (1)) val = -val; */
1031 val
= (val
^ vl_vlc_sbits(&bs
->vlc
, 1)) - vl_vlc_sbits(&bs
->vlc
, 1);
1038 vl_vlc_needbits(&bs
->vlc
);
1045 if (bs
->vlc
.buf
>= 0x04000000) {
1047 tab
= DCT_B14_8
+ (vl_vlc_ubits(&bs
->vlc
, 8) - 4);
1055 i
+= UBITS(bs
->vlc
.buf
<< 6, 6) - 64;
1057 break; /* illegal, check needed to avoid buffer overflow */
1059 l
= scan_ptable
[j
= scan
[i
]];
1061 vl_vlc_dumpbits(&bs
->vlc
, 12);
1062 vl_vlc_needbits(&bs
->vlc
);
1063 val
= 2 * (vl_vlc_sbits(&bs
->vlc
, 12) + vl_vlc_sbits(&bs
->vlc
, 1)) + 1;
1064 val
= (val
* quantizer_scale
* quant_matrix
[l
]) / 32;
1070 vl_vlc_dumpbits(&bs
->vlc
, 12);
1071 vl_vlc_needbits(&bs
->vlc
);
1075 } else if (bs
->vlc
.buf
>= 0x02000000) {
1076 tab
= DCT_B14_10
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 8);
1080 } else if (bs
->vlc
.buf
>= 0x00800000) {
1081 tab
= DCT_13
+ (vl_vlc_ubits(&bs
->vlc
, 13) - 16);
1085 } else if (bs
->vlc
.buf
>= 0x00200000) {
1086 tab
= DCT_15
+ (vl_vlc_ubits(&bs
->vlc
, 15) - 16);
1091 tab
= DCT_16
+ vl_vlc_ubits(&bs
->vlc
, 16);
1093 vl_vlc_getword(&bs
->vlc
, bs
->vlc
.bits
+ 16);
1098 break; /* illegal, check needed to avoid buffer overflow */
1100 dest
[63] ^= mismatch
& 1;
1101 vl_vlc_dumpbits(&bs
->vlc
, 2); /* dump end of block code */
1105 get_mpeg1_intra_block(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
, short *dest
)
1108 const uint8_t *scan
;
1109 uint8_t *scan_ptable
;
1110 uint8_t *quant_matrix
= picture
->intra_quantizer_matrix
;
1111 int quantizer_scale
= picture
->quantizer_scale
;
1116 if (!picture
->alternate_scan
) {
1117 scan
= mpeg2_scan_norm_orig
;
1118 scan_ptable
= mpeg2_scan_norm_ptable
;
1120 scan
= mpeg2_scan_alt_orig
;
1121 scan_ptable
= mpeg2_scan_alt_ptable
;
1124 vl_vlc_needbits(&bs
->vlc
);
1127 if (bs
->vlc
.buf
>= 0x28000000) {
1129 tab
= DCT_B14AC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
1133 break; /* end of block */
1136 l
= scan_ptable
[j
= scan
[i
]];
1137 bs
->vlc
.buf
<<= tab
->len
;
1138 bs
->vlc
.bits
+= tab
->len
+ 1;
1139 val
= (tab
->level
* quantizer_scale
* quant_matrix
[l
]) >> 4;
1142 val
= (val
- 1) | 1;
1144 /* if (bitstream_get (1)) val = -val; */
1145 val
= (val
^ vl_vlc_sbits(&bs
->vlc
, 1)) - vl_vlc_sbits(&bs
->vlc
, 1);
1151 vl_vlc_needbits(&bs
->vlc
);
1155 } else if (bs
->vlc
.buf
>= 0x04000000) {
1157 tab
= DCT_B14_8
+ (vl_vlc_ubits(&bs
->vlc
, 8) - 4);
1165 i
+= UBITS(bs
->vlc
.buf
<< 6, 6) - 64;
1167 break; /* illegal, check needed to avoid buffer overflow */
1169 l
= scan_ptable
[j
= scan
[i
]];
1171 vl_vlc_dumpbits(&bs
->vlc
, 12);
1172 vl_vlc_needbits(&bs
->vlc
);
1173 val
= vl_vlc_sbits(&bs
->vlc
, 8);
1174 if (! (val
& 0x7f)) {
1175 vl_vlc_dumpbits(&bs
->vlc
, 8);
1176 val
= vl_vlc_ubits(&bs
->vlc
, 8) + 2 * val
;
1178 val
= (val
* quantizer_scale
* quant_matrix
[l
]) / 16;
1181 val
= (val
+ ~SBITS (val
, 1)) | 1;
1186 vl_vlc_dumpbits(&bs
->vlc
, 8);
1187 vl_vlc_needbits(&bs
->vlc
);
1191 } else if (bs
->vlc
.buf
>= 0x02000000) {
1192 tab
= DCT_B14_10
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 8);
1196 } else if (bs
->vlc
.buf
>= 0x00800000) {
1197 tab
= DCT_13
+ (vl_vlc_ubits(&bs
->vlc
, 13) - 16);
1201 } else if (bs
->vlc
.buf
>= 0x00200000) {
1202 tab
= DCT_15
+ (vl_vlc_ubits(&bs
->vlc
, 15) - 16);
1207 tab
= DCT_16
+ vl_vlc_ubits(&bs
->vlc
, 16);
1209 vl_vlc_getword(&bs
->vlc
, bs
->vlc
.bits
+ 16);
1214 break; /* illegal, check needed to avoid buffer overflow */
1216 vl_vlc_dumpbits(&bs
->vlc
, 2); /* dump end of block code */
1220 get_mpeg1_non_intra_block(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
, short *dest
)
1223 const uint8_t * scan
;
1224 uint8_t *scan_ptable
;
1225 uint8_t *quant_matrix
= picture
->non_intra_quantizer_matrix
;
1226 int quantizer_scale
= picture
->quantizer_scale
;
1231 if (!picture
->alternate_scan
) {
1232 scan
= mpeg2_scan_norm_orig
;
1233 scan_ptable
= mpeg2_scan_norm_ptable
;
1235 scan
= mpeg2_scan_alt_orig
;
1236 scan_ptable
= mpeg2_scan_alt_ptable
;
1239 vl_vlc_needbits(&bs
->vlc
);
1240 if (bs
->vlc
.buf
>= 0x28000000) {
1241 tab
= DCT_B14DC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
1247 if (bs
->vlc
.buf
>= 0x28000000) {
1249 tab
= DCT_B14AC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
1254 break; /* end of block */
1257 l
= scan_ptable
[j
= scan
[i
]];
1258 bs
->vlc
.buf
<<= tab
->len
;
1259 bs
->vlc
.bits
+= tab
->len
+ 1;
1260 val
= ((2*tab
->level
+1) * quantizer_scale
* quant_matrix
[l
]) >> 5;
1263 val
= (val
- 1) | 1;
1265 /* if (bitstream_get (1)) val = -val; */
1266 val
= (val
^ vl_vlc_sbits(&bs
->vlc
, 1)) - vl_vlc_sbits(&bs
->vlc
, 1);
1272 vl_vlc_needbits(&bs
->vlc
);
1279 if (bs
->vlc
.buf
>= 0x04000000) {
1281 tab
= DCT_B14_8
+ (vl_vlc_ubits(&bs
->vlc
, 8) - 4);
1289 i
+= UBITS(bs
->vlc
.buf
<< 6, 6) - 64;
1291 break; /* illegal, check needed to avoid buffer overflow */
1293 l
= scan_ptable
[j
= scan
[i
]];
1295 vl_vlc_dumpbits(&bs
->vlc
, 12);
1296 vl_vlc_needbits(&bs
->vlc
);
1297 val
= vl_vlc_sbits(&bs
->vlc
, 8);
1298 if (! (val
& 0x7f)) {
1299 vl_vlc_dumpbits(&bs
->vlc
, 8);
1300 val
= vl_vlc_ubits(&bs
->vlc
, 8) + 2 * val
;
1302 val
= 2 * (val
+ SBITS (val
, 1)) + 1;
1303 val
= (val
* quantizer_scale
* quant_matrix
[l
]) / 32;
1306 val
= (val
+ ~SBITS (val
, 1)) | 1;
1311 vl_vlc_dumpbits(&bs
->vlc
, 8);
1312 vl_vlc_needbits(&bs
->vlc
);
1316 } else if (bs
->vlc
.buf
>= 0x02000000) {
1317 tab
= DCT_B14_10
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 8);
1321 } else if (bs
->vlc
.buf
>= 0x00800000) {
1322 tab
= DCT_13
+ (vl_vlc_ubits(&bs
->vlc
, 13) - 16);
1326 } else if (bs
->vlc
.buf
>= 0x00200000) {
1327 tab
= DCT_15
+ (vl_vlc_ubits(&bs
->vlc
, 15) - 16);
1332 tab
= DCT_16
+ vl_vlc_ubits(&bs
->vlc
, 16);
1334 vl_vlc_getword(&bs
->vlc
, bs
->vlc
.bits
+ 16);
1339 break; /* illegal, check needed to avoid buffer overflow */
1341 vl_vlc_dumpbits(&bs
->vlc
, 2); /* dump end of block code */
1345 slice_intra_DCT(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
, int cc
,
1346 unsigned x
, unsigned y
, enum pipe_mpeg12_dct_type coding
)
1348 short *dest
= bs
->ycbcr_buffer
[cc
];
1350 bs
->ycbcr_stream
[cc
]->x
= x
;
1351 bs
->ycbcr_stream
[cc
]->y
= y
;
1352 bs
->ycbcr_stream
[cc
]->intra
= PIPE_MPEG12_DCT_INTRA
;
1353 bs
->ycbcr_stream
[cc
]->coding
= coding
;
1355 vl_vlc_needbits(&bs
->vlc
);
1357 /* Get the intra DC coefficient and inverse quantize it */
1359 picture
->dc_dct_pred
[0] += get_luma_dc_dct_diff(bs
);
1361 picture
->dc_dct_pred
[cc
] += get_chroma_dc_dct_diff(bs
);
1363 memset(dest
, 0, sizeof(int16_t) * 64);
1364 dest
[0] = picture
->dc_dct_pred
[cc
] << (3 - picture
->intra_dc_precision
);
1365 if (picture
->mpeg1
) {
1366 if (picture
->picture_coding_type
!= D_TYPE
)
1367 get_mpeg1_intra_block(bs
, picture
, dest
);
1368 } else if (picture
->intra_vlc_format
)
1369 get_intra_block_B15(bs
, picture
, dest
);
1371 get_intra_block_B14(bs
, picture
, dest
);
1373 bs
->num_ycbcr_blocks
[cc
]++;
1374 bs
->ycbcr_stream
[cc
]++;
1375 bs
->ycbcr_buffer
[cc
] += 64;
1379 slice_non_intra_DCT(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
, int cc
,
1380 unsigned x
, unsigned y
, enum pipe_mpeg12_dct_type coding
)
1382 short *dest
= bs
->ycbcr_buffer
[cc
];
1384 bs
->ycbcr_stream
[cc
]->x
= x
;
1385 bs
->ycbcr_stream
[cc
]->y
= y
;
1386 bs
->ycbcr_stream
[cc
]->intra
= PIPE_MPEG12_DCT_DELTA
;
1387 bs
->ycbcr_stream
[cc
]->coding
= coding
;
1389 memset(dest
, 0, sizeof(int16_t) * 64);
1391 get_mpeg1_non_intra_block(bs
, picture
, dest
);
1393 get_non_intra_block(bs
, picture
, dest
);
1395 bs
->num_ycbcr_blocks
[cc
]++;
1396 bs
->ycbcr_stream
[cc
]++;
1397 bs
->ycbcr_buffer
[cc
] += 64;
1401 motion_mp1(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1403 int motion_x
, motion_y
;
1405 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1407 vl_vlc_needbits(&bs
->vlc
);
1408 motion_x
= (mv
->top
.x
+ (get_motion_delta(bs
, f_code
[0]) << f_code
[1]));
1409 motion_x
= bound_motion_vector (motion_x
, f_code
[0] + f_code
[1]);
1410 mv
->top
.x
= mv
->bottom
.x
= motion_x
;
1412 vl_vlc_needbits(&bs
->vlc
);
1413 motion_y
= (mv
->top
.y
+ (get_motion_delta(bs
, f_code
[0]) << f_code
[1]));
1414 motion_y
= bound_motion_vector (motion_y
, f_code
[0] + f_code
[1]);
1415 mv
->top
.y
= mv
->bottom
.y
= motion_y
;
1419 motion_fr_frame(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1421 int motion_x
, motion_y
;
1423 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1425 vl_vlc_needbits(&bs
->vlc
);
1426 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1427 motion_x
= bound_motion_vector(motion_x
, f_code
[0]);
1428 mv
->top
.x
= mv
->bottom
.x
= motion_x
;
1430 vl_vlc_needbits(&bs
->vlc
);
1431 motion_y
= mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]);
1432 motion_y
= bound_motion_vector(motion_y
, f_code
[1]);
1433 mv
->top
.y
= mv
->bottom
.y
= motion_y
;
1437 motion_fr_field(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1439 int motion_x
, motion_y
;
1441 vl_vlc_needbits(&bs
->vlc
);
1442 mv
->top
.field_select
= vl_vlc_ubits(&bs
->vlc
, 1) ?
1443 PIPE_VIDEO_BOTTOM_FIELD
: PIPE_VIDEO_TOP_FIELD
;
1444 vl_vlc_dumpbits(&bs
->vlc
, 1);
1446 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1447 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1448 mv
->top
.x
= motion_x
;
1450 vl_vlc_needbits(&bs
->vlc
);
1451 motion_y
= (mv
->top
.y
>> 1) + get_motion_delta(bs
, f_code
[1]);
1452 /* motion_y = bound_motion_vector (motion_y, f_code[1]); */
1453 mv
->top
.y
= motion_y
<< 1;
1455 vl_vlc_needbits(&bs
->vlc
);
1456 mv
->bottom
.field_select
= vl_vlc_ubits(&bs
->vlc
, 1) ?
1457 PIPE_VIDEO_BOTTOM_FIELD
: PIPE_VIDEO_TOP_FIELD
;
1458 vl_vlc_dumpbits(&bs
->vlc
, 1);
1460 motion_x
= mv
->bottom
.x
+ get_motion_delta(bs
, f_code
[0]);
1461 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1462 mv
->bottom
.x
= motion_x
;
1464 vl_vlc_needbits(&bs
->vlc
);
1465 motion_y
= (mv
->bottom
.y
>> 1) + get_motion_delta(bs
, f_code
[1]);
1466 /* motion_y = bound_motion_vector (motion_y, f_code[1]); */
1467 mv
->bottom
.y
= motion_y
<< 1;
1471 motion_fr_dmv(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1473 int motion_x
, motion_y
;
1475 // TODO Implement dmv
1476 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1478 vl_vlc_needbits(&bs
->vlc
);
1479 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1480 motion_x
= bound_motion_vector(motion_x
, f_code
[0]);
1481 mv
->top
.x
= mv
->bottom
.x
= motion_x
;
1483 vl_vlc_needbits(&bs
->vlc
);
1484 motion_y
= (mv
->top
.y
>> 1) + get_motion_delta(bs
, f_code
[1]);
1485 /* motion_y = bound_motion_vector (motion_y, f_code[1]); */
1486 mv
->top
.y
= mv
->bottom
.y
= motion_y
<< 1;
1489 /* like motion_frame, but parsing without actual motion compensation */
1491 motion_fr_conceal(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1495 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1497 vl_vlc_needbits(&bs
->vlc
);
1498 tmp
= (mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]));
1499 tmp
= bound_motion_vector (tmp
, f_code
[0]);
1500 mv
->top
.x
= mv
->bottom
.x
= tmp
;
1502 vl_vlc_needbits(&bs
->vlc
);
1503 tmp
= (mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]));
1504 tmp
= bound_motion_vector (tmp
, f_code
[1]);
1505 mv
->top
.y
= mv
->bottom
.y
= tmp
;
1507 vl_vlc_dumpbits(&bs
->vlc
, 1); /* remove marker_bit */
1511 motion_fi_field(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1513 int motion_x
, motion_y
;
1515 vl_vlc_needbits(&bs
->vlc
);
1518 //vl_vlc_ubits(&bs->vlc, 1);
1520 // TODO field select may need to do something here for bob (weave ok)
1521 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1522 vl_vlc_dumpbits(&bs
->vlc
, 1);
1524 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1525 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1526 mv
->top
.x
= mv
->bottom
.x
= motion_x
;
1528 vl_vlc_needbits(&bs
->vlc
);
1529 motion_y
= mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]);
1530 motion_y
= bound_motion_vector (motion_y
, f_code
[1]);
1531 mv
->top
.y
= mv
->bottom
.y
= motion_y
;
1535 motion_fi_16x8(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1537 int motion_x
, motion_y
;
1539 vl_vlc_needbits(&bs
->vlc
);
1542 //vl_vlc_ubits(&bs->vlc, 1);
1544 // TODO field select may need to do something here bob (weave ok)
1545 mv
->top
.field_select
= PIPE_VIDEO_FRAME
;
1546 vl_vlc_dumpbits(&bs
->vlc
, 1);
1548 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1549 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1550 mv
->top
.x
= motion_x
;
1552 vl_vlc_needbits(&bs
->vlc
);
1553 motion_y
= mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]);
1554 motion_y
= bound_motion_vector (motion_y
, f_code
[1]);
1555 mv
->top
.y
= motion_y
;
1557 vl_vlc_needbits(&bs
->vlc
);
1559 //vl_vlc_ubits(&bs->vlc, 1);
1561 // TODO field select may need to do something here for bob (weave ok)
1562 mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1563 vl_vlc_dumpbits(&bs
->vlc
, 1);
1565 motion_x
= mv
->bottom
.x
+ get_motion_delta(bs
, f_code
[0]);
1566 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1567 mv
->bottom
.x
= motion_x
;
1569 vl_vlc_needbits(&bs
->vlc
);
1570 motion_y
= mv
->bottom
.y
+ get_motion_delta(bs
, f_code
[1]);
1571 motion_y
= bound_motion_vector (motion_y
, f_code
[1]);
1572 mv
->bottom
.y
= motion_y
;
1576 motion_fi_dmv(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1578 int motion_x
, motion_y
;
1580 // TODO field select may need to do something here for bob (weave ok)
1581 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1583 vl_vlc_needbits(&bs
->vlc
);
1584 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1585 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1586 mv
->top
.x
= mv
->bottom
.x
= motion_x
;
1588 vl_vlc_needbits(&bs
->vlc
);
1589 motion_y
= mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]);
1590 motion_y
= bound_motion_vector (motion_y
, f_code
[1]);
1591 mv
->top
.y
= mv
->bottom
.y
= motion_y
;
1596 motion_fi_conceal(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1600 vl_vlc_needbits(&bs
->vlc
);
1601 vl_vlc_dumpbits(&bs
->vlc
, 1); /* remove field_select */
1603 tmp
= (mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]));
1604 tmp
= bound_motion_vector(tmp
, f_code
[0]);
1605 mv
->top
.x
= mv
->bottom
.x
= tmp
;
1607 vl_vlc_needbits(&bs
->vlc
);
1608 tmp
= (mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]));
1609 tmp
= bound_motion_vector(tmp
, f_code
[1]);
1610 mv
->top
.y
= mv
->bottom
.y
= tmp
;
1612 vl_vlc_dumpbits(&bs
->vlc
, 1); /* remove marker_bit */
1615 #define MOTION_CALL(routine, macroblock_modes) \
1617 if ((macroblock_modes) & MACROBLOCK_MOTION_FORWARD) \
1618 routine(bs, picture->f_code[0], &mv_fwd); \
1619 if ((macroblock_modes) & MACROBLOCK_MOTION_BACKWARD) \
1620 routine(bs, picture->f_code[1], &mv_bwd); \
1623 #define NEXT_MACROBLOCK \
1625 bs->mv_stream[0][x+y*bs->width/16] = mv_fwd; \
1626 bs->mv_stream[1][x+y*bs->width/16] = mv_bwd; \
1628 if (x == bs->width/16) { \
1630 if (y >= bs->height/16) \
1637 slice_init(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
, int *x
, int *y
)
1641 vl_vlc_need32bits(&bs
->vlc
);
1642 while(bs
->vlc
.buf
< 0x101 || bs
->vlc
.buf
> 0x1AF) {
1643 if(!vl_vlc_getbyte(&bs
->vlc
))
1646 *y
= ((bs
->vlc
.buf
& 0xFF) - 1) * 16;
1647 vl_vlc_restart(&bs
->vlc
);
1649 //TODO conversion to signed format signed format
1650 picture
->dc_dct_pred
[0] = picture
->dc_dct_pred
[1] = picture
->dc_dct_pred
[2] = 0;
1652 picture
->quantizer_scale
= get_quantizer_scale(bs
, picture
);
1654 /* ignore intra_slice and all the extra data */
1655 while (bs
->vlc
.buf
& 0x80000000) {
1656 vl_vlc_dumpbits(&bs
->vlc
, 9);
1657 vl_vlc_needbits(&bs
->vlc
);
1660 /* decode initial macroblock address increment */
1663 if (bs
->vlc
.buf
>= 0x08000000) {
1664 mba
= MBA_5
+ (vl_vlc_ubits(&bs
->vlc
, 6) - 2);
1666 } else if (bs
->vlc
.buf
>= 0x01800000) {
1667 mba
= MBA_11
+ (vl_vlc_ubits(&bs
->vlc
, 12) - 24);
1669 } else switch (vl_vlc_ubits(&bs
->vlc
, 12)) {
1670 case 8: /* macroblock_escape */
1672 vl_vlc_dumpbits(&bs
->vlc
, 11);
1673 vl_vlc_needbits(&bs
->vlc
);
1675 case 15: /* macroblock_stuffing (MPEG1 only) */
1676 bs
->vlc
.buf
&= 0xfffff;
1677 vl_vlc_dumpbits(&bs
->vlc
, 11);
1678 vl_vlc_needbits(&bs
->vlc
);
1680 default: /* error */
1684 vl_vlc_dumpbits(&bs
->vlc
, mba
->len
+ 1);
1685 *x
= (*x
+ mba
->mba
) << 4;
1687 while (*x
>= bs
->width
) {
1691 if (*y
> bs
->height
)
1698 decode_slice(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
*picture
)
1700 struct pipe_motionvector mv_fwd
, mv_bwd
;
1701 enum pipe_mpeg12_dct_type dct_type
;
1704 if (!slice_init(bs
, picture
, &x
, &y
))
1707 mv_fwd
.top
.x
= mv_fwd
.top
.y
= mv_fwd
.bottom
.x
= mv_fwd
.bottom
.y
= 0;
1708 mv_fwd
.top
.field_select
= mv_fwd
.bottom
.field_select
= PIPE_VIDEO_FRAME
;
1710 mv_bwd
.top
.x
= mv_bwd
.top
.y
= mv_bwd
.bottom
.x
= mv_bwd
.bottom
.y
= 0;
1711 mv_bwd
.top
.field_select
= mv_bwd
.bottom
.field_select
= PIPE_VIDEO_FRAME
;
1717 int macroblock_modes
;
1721 vl_vlc_needbits(&bs
->vlc
);
1723 macroblock_modes
= get_macroblock_modes(bs
, picture
); //macroblock_modes()
1724 dct_type
= macroblock_modes
& DCT_TYPE_INTERLACED
?
1725 PIPE_MPEG12_DCT_TYPE_FIELD
: PIPE_MPEG12_DCT_TYPE_FRAME
;
1727 switch(macroblock_modes
& (MACROBLOCK_MOTION_FORWARD
|MACROBLOCK_MOTION_BACKWARD
)) {
1728 case (MACROBLOCK_MOTION_FORWARD
|MACROBLOCK_MOTION_BACKWARD
):
1729 mv_fwd
.top
.weight
= mv_fwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_HALF
;
1730 mv_bwd
.top
.weight
= mv_bwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_HALF
;
1734 case MACROBLOCK_MOTION_FORWARD
:
1735 mv_fwd
.top
.weight
= mv_fwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MAX
;
1736 mv_bwd
.top
.weight
= mv_bwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1739 case MACROBLOCK_MOTION_BACKWARD
:
1740 mv_fwd
.top
.weight
= mv_fwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1741 mv_bwd
.top
.weight
= mv_bwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MAX
;
1745 /* maybe integrate MACROBLOCK_QUANT test into get_macroblock_modes ? */
1746 if (macroblock_modes
& MACROBLOCK_QUANT
)
1747 picture
->quantizer_scale
= get_quantizer_scale(bs
, picture
);
1749 if (macroblock_modes
& MACROBLOCK_INTRA
) {
1751 if (picture
->concealment_motion_vectors
) {
1752 if (picture
->picture_structure
== FRAME_PICTURE
)
1753 motion_fr_conceal(bs
, picture
->f_code
[0], &mv_fwd
);
1755 motion_fi_conceal(bs
, picture
->f_code
[0], &mv_fwd
);
1758 mv_fwd
.top
.x
= mv_fwd
.top
.y
= mv_fwd
.bottom
.x
= mv_fwd
.bottom
.y
= 0;
1759 mv_bwd
.top
.x
= mv_bwd
.top
.y
= mv_bwd
.bottom
.x
= mv_bwd
.bottom
.y
= 0;
1761 mv_fwd
.top
.weight
= mv_fwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1762 mv_bwd
.top
.weight
= mv_bwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1764 // unravaled loop of 6 block(i) calls in macroblock()
1765 slice_intra_DCT(bs
, picture
, 0, x
*2+0, y
*2+0, dct_type
);
1766 slice_intra_DCT(bs
, picture
, 0, x
*2+1, y
*2+0, dct_type
);
1767 slice_intra_DCT(bs
, picture
, 0, x
*2+0, y
*2+1, dct_type
);
1768 slice_intra_DCT(bs
, picture
, 0, x
*2+1, y
*2+1, dct_type
);
1769 slice_intra_DCT(bs
, picture
, 1, x
, y
, dct_type
);
1770 slice_intra_DCT(bs
, picture
, 2, x
, y
, dct_type
);
1772 if (picture
->picture_coding_type
== D_TYPE
) {
1773 vl_vlc_needbits(&bs
->vlc
);
1774 vl_vlc_dumpbits(&bs
->vlc
, 1);
1778 if (picture
->picture_structure
== FRAME_PICTURE
)
1779 switch (macroblock_modes
& MOTION_TYPE_MASK
) {
1781 if (picture
->mpeg1
) {
1782 MOTION_CALL(motion_mp1
, macroblock_modes
);
1784 MOTION_CALL(motion_fr_frame
, macroblock_modes
);
1789 MOTION_CALL (motion_fr_field
, macroblock_modes
);
1793 MOTION_CALL (motion_fr_dmv
, MACROBLOCK_MOTION_FORWARD
);
1797 /* non-intra mb without forward mv in a P picture */
1798 mv_fwd
.top
.x
= mv_fwd
.top
.y
= mv_fwd
.bottom
.x
= mv_fwd
.bottom
.y
= 0;
1799 mv_bwd
.top
.x
= mv_bwd
.top
.y
= mv_bwd
.bottom
.x
= mv_bwd
.bottom
.y
= 0;
1803 switch (macroblock_modes
& MOTION_TYPE_MASK
) {
1805 MOTION_CALL (motion_fi_field
, macroblock_modes
);
1809 MOTION_CALL (motion_fi_16x8
, macroblock_modes
);
1813 MOTION_CALL (motion_fi_dmv
, MACROBLOCK_MOTION_FORWARD
);
1817 /* non-intra mb without forward mv in a P picture */
1818 mv_fwd
.top
.x
= mv_fwd
.top
.y
= mv_fwd
.bottom
.x
= mv_fwd
.bottom
.y
= 0;
1819 mv_bwd
.top
.x
= mv_bwd
.top
.y
= mv_bwd
.bottom
.x
= mv_bwd
.bottom
.y
= 0;
1823 if (macroblock_modes
& MACROBLOCK_PATTERN
) {
1824 int coded_block_pattern
= get_coded_block_pattern(bs
);
1826 // TODO optimize not fully used for idct accel only mc.
1827 if (coded_block_pattern
& 0x20)
1828 slice_non_intra_DCT(bs
, picture
, 0, x
*2+0, y
*2+0, dct_type
); // cc0 luma 0
1829 if (coded_block_pattern
& 0x10)
1830 slice_non_intra_DCT(bs
, picture
, 0, x
*2+1, y
*2+0, dct_type
); // cc0 luma 1
1831 if (coded_block_pattern
& 0x08)
1832 slice_non_intra_DCT(bs
, picture
, 0, x
*2+0, y
*2+1, dct_type
); // cc0 luma 2
1833 if (coded_block_pattern
& 0x04)
1834 slice_non_intra_DCT(bs
, picture
, 0, x
*2+1, y
*2+1, dct_type
); // cc0 luma 3
1835 if (coded_block_pattern
& 0x2)
1836 slice_non_intra_DCT(bs
, picture
, 1, x
, y
, dct_type
); // cc1 croma
1837 if (coded_block_pattern
& 0x1)
1838 slice_non_intra_DCT(bs
, picture
, 2, x
, y
, dct_type
); // cc2 croma
1841 picture
->dc_dct_pred
[0] = picture
->dc_dct_pred
[1] = picture
->dc_dct_pred
[2] = 0;
1846 vl_vlc_needbits(&bs
->vlc
);
1849 if (bs
->vlc
.buf
>= 0x10000000) {
1850 mba
= MBA_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 2);
1852 } else if (bs
->vlc
.buf
>= 0x03000000) {
1853 mba
= MBA_11
+ (vl_vlc_ubits(&bs
->vlc
, 11) - 24);
1855 } else switch (vl_vlc_ubits(&bs
->vlc
, 11)) {
1856 case 8: /* macroblock_escape */
1859 case 15: /* macroblock_stuffing (MPEG1 only) */
1860 vl_vlc_dumpbits(&bs
->vlc
, 11);
1861 vl_vlc_needbits(&bs
->vlc
);
1863 default: /* end of slice, or error */
1867 vl_vlc_dumpbits(&bs
->vlc
, mba
->len
);
1868 mba_inc
+= mba
->mba
;
1870 //TODO conversion to signed format signed format
1871 picture
->dc_dct_pred
[0] = picture
->dc_dct_pred
[1] = picture
->dc_dct_pred
[2] = 0;
1873 switch(picture
->picture_structure
) {
1875 mv_fwd
.top
.field_select
= mv_fwd
.bottom
.field_select
= PIPE_VIDEO_FRAME
;
1876 mv_bwd
.top
.field_select
= mv_bwd
.bottom
.field_select
= PIPE_VIDEO_FRAME
;
1880 mv_fwd
.top
.field_select
= mv_fwd
.bottom
.field_select
= PIPE_VIDEO_TOP_FIELD
;
1881 mv_bwd
.top
.field_select
= mv_bwd
.bottom
.field_select
= PIPE_VIDEO_TOP_FIELD
;
1885 mv_fwd
.top
.field_select
= mv_fwd
.bottom
.field_select
= PIPE_VIDEO_BOTTOM_FIELD
;
1886 mv_bwd
.top
.field_select
= mv_bwd
.bottom
.field_select
= PIPE_VIDEO_BOTTOM_FIELD
;
1890 if (picture
->picture_coding_type
== P_TYPE
) {
1891 mv_fwd
.top
.x
= mv_fwd
.top
.y
= mv_fwd
.bottom
.x
= mv_fwd
.bottom
.y
= 0;
1892 mv_fwd
.top
.weight
= mv_fwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MAX
;
1896 } while (--mba_inc
);
1902 vl_mpg12_bs_init(struct vl_mpg12_bs
*bs
, unsigned width
, unsigned height
)
1906 memset(bs
, 0, sizeof(struct vl_mpg12_bs
));
1909 bs
->height
= height
;
1911 setup_scan_ptable();
1915 vl_mpg12_bs_set_buffers(struct vl_mpg12_bs
*bs
, struct pipe_ycbcr_block
*ycbcr_stream
[VL_MAX_PLANES
],
1916 short *ycbcr_buffer
[VL_MAX_PLANES
], struct pipe_motionvector
*mv_stream
[VL_MAX_REF_FRAMES
])
1921 assert(ycbcr_stream
&& ycbcr_buffer
);
1924 for (i
= 0; i
< VL_MAX_PLANES
; ++i
) {
1925 bs
->ycbcr_stream
[i
] = ycbcr_stream
[i
];
1926 bs
->ycbcr_buffer
[i
] = ycbcr_buffer
[i
];
1928 for (i
= 0; i
< VL_MAX_REF_FRAMES
; ++i
)
1929 bs
->mv_stream
[i
] = mv_stream
[i
];
1932 for (i
= 0; i
< bs
->width
/16*bs
->height
/16; ++i
) {
1933 bs
->mv_stream
[0][i
].top
.x
= bs
->mv_stream
[0][i
].top
.y
= 0;
1934 bs
->mv_stream
[0][i
].top
.field_select
= PIPE_VIDEO_FRAME
;
1935 bs
->mv_stream
[0][i
].top
.weight
= PIPE_VIDEO_MV_WEIGHT_MAX
;
1936 bs
->mv_stream
[0][i
].bottom
.x
= bs
->mv_stream
[0][i
].bottom
.y
= 0;
1937 bs
->mv_stream
[0][i
].bottom
.field_select
= PIPE_VIDEO_FRAME
;
1938 bs
->mv_stream
[0][i
].bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MAX
;
1940 bs
->mv_stream
[1][i
].top
.x
= bs
->mv_stream
[1][i
].top
.y
= 0;
1941 bs
->mv_stream
[1][i
].top
.field_select
= PIPE_VIDEO_FRAME
;
1942 bs
->mv_stream
[1][i
].top
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1943 bs
->mv_stream
[1][i
].bottom
.x
= bs
->mv_stream
[1][i
].bottom
.y
= 0;
1944 bs
->mv_stream
[1][i
].bottom
.field_select
= PIPE_VIDEO_FRAME
;
1945 bs
->mv_stream
[1][i
].bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1950 vl_mpg12_bs_decode(struct vl_mpg12_bs
*bs
, unsigned num_bytes
, const void *buffer
,
1951 struct pipe_mpeg12_picture_desc
*picture
, unsigned num_ycbcr_blocks
[3])
1954 assert(num_ycbcr_blocks
);
1955 assert(buffer
&& num_bytes
);
1957 bs
->num_ycbcr_blocks
= num_ycbcr_blocks
;
1959 vl_vlc_init(&bs
->vlc
, buffer
, num_bytes
);
1961 while(decode_slice(bs
, picture
));