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 const int non_linear_quantizer_scale
[] = {
484 0, 1, 2, 3, 4, 5, 6, 7,
485 8, 10, 12, 14, 16, 18, 20, 22,
486 24, 28, 32, 36, 40, 44, 48, 52,
487 56, 64, 72, 80, 88, 96, 104, 112
491 get_macroblock_modes(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
)
493 int macroblock_modes
;
496 switch (picture
->picture_coding_type
) {
499 tab
= MB_I
+ vl_vlc_ubits(&bs
->vlc
, 1);
500 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
501 macroblock_modes
= tab
->modes
;
503 if ((!(picture
->frame_pred_frame_dct
)) && (picture
->picture_structure
== FRAME_PICTURE
)) {
504 macroblock_modes
|= vl_vlc_ubits(&bs
->vlc
, 1) * DCT_TYPE_INTERLACED
;
505 vl_vlc_dumpbits(&bs
->vlc
, 1);
508 return macroblock_modes
;
512 tab
= MB_P
+ vl_vlc_ubits(&bs
->vlc
, 5);
513 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
514 macroblock_modes
= tab
->modes
;
516 if (picture
->picture_structure
!= FRAME_PICTURE
) {
517 if (macroblock_modes
& MACROBLOCK_MOTION_FORWARD
) {
518 macroblock_modes
|= vl_vlc_ubits(&bs
->vlc
, 2) * MOTION_TYPE_BASE
;
519 vl_vlc_dumpbits(&bs
->vlc
, 2);
521 return macroblock_modes
;
522 } else if (picture
->frame_pred_frame_dct
) {
523 if (macroblock_modes
& MACROBLOCK_MOTION_FORWARD
)
524 macroblock_modes
|= MC_FRAME
;
525 return macroblock_modes
;
527 if (macroblock_modes
& MACROBLOCK_MOTION_FORWARD
) {
528 macroblock_modes
|= vl_vlc_ubits(&bs
->vlc
, 2) * MOTION_TYPE_BASE
;
529 vl_vlc_dumpbits(&bs
->vlc
, 2);
531 if (macroblock_modes
& (MACROBLOCK_INTRA
| MACROBLOCK_PATTERN
)) {
532 macroblock_modes
|= vl_vlc_ubits(&bs
->vlc
, 1) * DCT_TYPE_INTERLACED
;
533 vl_vlc_dumpbits(&bs
->vlc
, 1);
535 return macroblock_modes
;
540 tab
= MB_B
+ vl_vlc_ubits(&bs
->vlc
, 6);
541 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
542 macroblock_modes
= tab
->modes
;
544 if (picture
->picture_structure
!= FRAME_PICTURE
) {
545 if (! (macroblock_modes
& MACROBLOCK_INTRA
)) {
546 macroblock_modes
|= vl_vlc_ubits(&bs
->vlc
, 2) * MOTION_TYPE_BASE
;
547 vl_vlc_dumpbits(&bs
->vlc
, 2);
549 return macroblock_modes
;
550 } else if (picture
->frame_pred_frame_dct
) {
551 /* if (! (macroblock_modes & MACROBLOCK_INTRA)) */
552 macroblock_modes
|= MC_FRAME
;
553 return macroblock_modes
;
555 if (macroblock_modes
& MACROBLOCK_INTRA
)
557 macroblock_modes
|= vl_vlc_ubits(&bs
->vlc
, 2) * MOTION_TYPE_BASE
;
558 vl_vlc_dumpbits(&bs
->vlc
, 2);
559 if (macroblock_modes
& (MACROBLOCK_INTRA
| MACROBLOCK_PATTERN
)) {
561 macroblock_modes
|= vl_vlc_ubits(&bs
->vlc
, 1) * DCT_TYPE_INTERLACED
;
562 vl_vlc_dumpbits(&bs
->vlc
, 1);
564 return macroblock_modes
;
569 vl_vlc_dumpbits(&bs
->vlc
, 1);
570 return MACROBLOCK_INTRA
;
578 get_quantizer_scale(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
)
580 int quantizer_scale_code
;
582 quantizer_scale_code
= vl_vlc_ubits(&bs
->vlc
, 5);
583 vl_vlc_dumpbits(&bs
->vlc
, 5);
585 if (picture
->q_scale_type
)
586 return non_linear_quantizer_scale
[quantizer_scale_code
];
588 return quantizer_scale_code
<< 1;
592 get_motion_delta(struct vl_mpg12_bs
*bs
, unsigned f_code
)
598 if (bs
->vlc
.buf
& 0x80000000) {
599 vl_vlc_dumpbits(&bs
->vlc
, 1);
601 } else if (bs
->vlc
.buf
>= 0x0c000000) {
603 tab
= MV_4
+ vl_vlc_ubits(&bs
->vlc
, 4);
604 delta
= (tab
->delta
<< f_code
) + 1;
605 bs
->vlc
.bits
+= tab
->len
+ f_code
+ 1;
606 bs
->vlc
.buf
<<= tab
->len
;
608 sign
= vl_vlc_sbits(&bs
->vlc
, 1);
612 delta
+= vl_vlc_ubits(&bs
->vlc
, f_code
);
613 bs
->vlc
.buf
<<= f_code
;
615 return (delta
^ sign
) - sign
;
619 tab
= MV_10
+ vl_vlc_ubits(&bs
->vlc
, 10);
620 delta
= (tab
->delta
<< f_code
) + 1;
621 bs
->vlc
.bits
+= tab
->len
+ 1;
622 bs
->vlc
.buf
<<= tab
->len
;
624 sign
= vl_vlc_sbits(&bs
->vlc
, 1);
628 vl_vlc_needbits(&bs
->vlc
);
629 delta
+= vl_vlc_ubits(&bs
->vlc
, f_code
);
630 vl_vlc_dumpbits(&bs
->vlc
, f_code
);
633 return (delta
^ sign
) - sign
;
638 bound_motion_vector(int vec
, unsigned f_code
)
644 limit
= 16 << f_code
;
646 if ((unsigned int)(vec
+ limit
) < 2 * limit
)
649 sign
= ((int32_t)vec
) >> 31;
650 return vec
- ((2 * limit
) ^ sign
) + sign
;
653 return ((int32_t)vec
<< (28 - f_code
)) >> (28 - f_code
);
658 get_dmv(struct vl_mpg12_bs
*bs
)
662 tab
= DMV_2
+ vl_vlc_ubits(&bs
->vlc
, 2);
663 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
668 get_coded_block_pattern(struct vl_mpg12_bs
*bs
)
672 vl_vlc_needbits(&bs
->vlc
);
674 if (bs
->vlc
.buf
>= 0x20000000) {
676 tab
= CBP_7
+ (vl_vlc_ubits(&bs
->vlc
, 7) - 16);
677 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
682 tab
= CBP_9
+ vl_vlc_ubits(&bs
->vlc
, 9);
683 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
689 get_luma_dc_dct_diff(struct vl_mpg12_bs
*bs
)
695 if (bs
->vlc
.buf
< 0xf8000000) {
696 tab
= DC_lum_5
+ vl_vlc_ubits(&bs
->vlc
, 5);
699 bs
->vlc
.bits
+= tab
->len
+ size
;
700 bs
->vlc
.buf
<<= tab
->len
;
701 dc_diff
= vl_vlc_ubits(&bs
->vlc
, size
) - UBITS (SBITS (~bs
->vlc
.buf
, 1), size
);
702 bs
->vlc
.buf
<<= size
;
705 vl_vlc_dumpbits(&bs
->vlc
, 3);
709 tab
= DC_long
+ (vl_vlc_ubits(&bs
->vlc
, 9) - 0x1e0);
711 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
712 vl_vlc_needbits(&bs
->vlc
);
713 dc_diff
= vl_vlc_ubits(&bs
->vlc
, size
) - UBITS (SBITS (~bs
->vlc
.buf
, 1), size
);
714 vl_vlc_dumpbits(&bs
->vlc
, size
);
720 get_chroma_dc_dct_diff(struct vl_mpg12_bs
*bs
)
726 if (bs
->vlc
.buf
< 0xf8000000) {
727 tab
= DC_chrom_5
+ vl_vlc_ubits(&bs
->vlc
, 5);
730 bs
->vlc
.bits
+= tab
->len
+ size
;
731 bs
->vlc
.buf
<<= tab
->len
;
732 dc_diff
= vl_vlc_ubits(&bs
->vlc
, size
) - UBITS (SBITS (~bs
->vlc
.buf
, 1), size
);
733 bs
->vlc
.buf
<<= size
;
736 vl_vlc_dumpbits(&bs
->vlc
, 2);
740 tab
= DC_long
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 0x3e0);
742 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
+ 1);
743 vl_vlc_needbits(&bs
->vlc
);
744 dc_diff
= vl_vlc_ubits(&bs
->vlc
, size
) - UBITS (SBITS (~bs
->vlc
.buf
, 1), size
);
745 vl_vlc_dumpbits(&bs
->vlc
, size
);
751 get_intra_block_B14(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
, short *dest
)
755 uint8_t *quant_matrix
= picture
->intra_quantizer_matrix
;
756 int quantizer_scale
= picture
->quantizer_scale
;
760 if (!picture
->alternate_scan
) {
761 scan
= mpeg2_scan_norm_orig
;
763 scan
= mpeg2_scan_alt_orig
;
769 vl_vlc_needbits(&bs
->vlc
);
772 if (bs
->vlc
.buf
>= 0x28000000) {
774 tab
= DCT_B14AC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
778 break; /* end of block */
783 bs
->vlc
.buf
<<= tab
->len
;
784 bs
->vlc
.bits
+= tab
->len
+ 1;
785 val
= (tab
->level
* quantizer_scale
* quant_matrix
[j
]) >> 4;
787 /* if (bitstream_get (1)) val = -val; */
788 val
= (val
^ vl_vlc_sbits(&bs
->vlc
, 1)) - vl_vlc_sbits(&bs
->vlc
, 1);
795 vl_vlc_needbits(&bs
->vlc
);
799 } else if (bs
->vlc
.buf
>= 0x04000000) {
801 tab
= DCT_B14_8
+ (vl_vlc_ubits(&bs
->vlc
, 8) - 4);
809 i
+= UBITS(bs
->vlc
.buf
<< 6, 6) - 64;
811 break; /* illegal, check needed to avoid buffer overflow */
815 vl_vlc_dumpbits(&bs
->vlc
, 12);
816 vl_vlc_needbits(&bs
->vlc
);
817 val
= (vl_vlc_sbits(&bs
->vlc
, 12) * quantizer_scale
* quant_matrix
[j
]) / 16;
823 vl_vlc_dumpbits(&bs
->vlc
, 12);
824 vl_vlc_needbits(&bs
->vlc
);
828 } else if (bs
->vlc
.buf
>= 0x02000000) {
829 tab
= DCT_B14_10
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 8);
833 } else if (bs
->vlc
.buf
>= 0x00800000) {
834 tab
= DCT_13
+ (vl_vlc_ubits(&bs
->vlc
, 13) - 16);
838 } else if (bs
->vlc
.buf
>= 0x00200000) {
839 tab
= DCT_15
+ (vl_vlc_ubits(&bs
->vlc
, 15) - 16);
844 tab
= DCT_16
+ vl_vlc_ubits(&bs
->vlc
, 16);
846 vl_vlc_getword(&bs
->vlc
, bs
->vlc
.bits
+ 16);
851 break; /* illegal, check needed to avoid buffer overflow */
854 dest
[63] ^= mismatch
& 1;
855 vl_vlc_dumpbits(&bs
->vlc
, 2); /* dump end of block code */
859 get_intra_block_B15(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
, short *dest
)
863 uint8_t *quant_matrix
= picture
->intra_quantizer_matrix
;
864 int quantizer_scale
= picture
->quantizer_scale
;
868 if (!picture
->alternate_scan
) {
869 scan
= mpeg2_scan_norm_orig
;
871 scan
= mpeg2_scan_alt_orig
;
877 vl_vlc_needbits(&bs
->vlc
);
880 if (bs
->vlc
.buf
>= 0x04000000) {
882 tab
= DCT_B15_8
+ (vl_vlc_ubits(&bs
->vlc
, 8) - 4);
889 bs
->vlc
.buf
<<= tab
->len
;
890 bs
->vlc
.bits
+= tab
->len
+ 1;
891 val
= (tab
->level
* quantizer_scale
* quant_matrix
[j
]) >> 4;
893 /* if (bitstream_get (1)) val = -val; */
894 val
= (val
^ vl_vlc_sbits(&bs
->vlc
, 1)) - vl_vlc_sbits(&bs
->vlc
, 1);
901 vl_vlc_needbits(&bs
->vlc
);
907 /* end of block. I commented out this code because if we */
908 /* dont exit here we will still exit at the later test :) */
910 /* if (i >= 128) break; */ /* end of block */
914 i
+= UBITS(bs
->vlc
.buf
<< 6, 6) - 64;
916 break; /* illegal, check against buffer overflow */
920 vl_vlc_dumpbits(&bs
->vlc
, 12);
921 vl_vlc_needbits(&bs
->vlc
);
922 val
= (vl_vlc_sbits(&bs
->vlc
, 12) * quantizer_scale
* quant_matrix
[j
]) / 16;
928 vl_vlc_dumpbits(&bs
->vlc
, 12);
929 vl_vlc_needbits(&bs
->vlc
);
934 } else if (bs
->vlc
.buf
>= 0x02000000) {
935 tab
= DCT_B15_10
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 8);
939 } else if (bs
->vlc
.buf
>= 0x00800000) {
940 tab
= DCT_13
+ (vl_vlc_ubits(&bs
->vlc
, 13) - 16);
944 } else if (bs
->vlc
.buf
>= 0x00200000) {
945 tab
= DCT_15
+ (vl_vlc_ubits(&bs
->vlc
, 15) - 16);
950 tab
= DCT_16
+ vl_vlc_ubits(&bs
->vlc
, 16);
952 vl_vlc_getword(&bs
->vlc
, bs
->vlc
.bits
+ 16);
957 break; /* illegal, check needed to avoid buffer overflow */
960 dest
[63] ^= mismatch
& 1;
961 vl_vlc_dumpbits(&bs
->vlc
, 4); /* dump end of block code */
965 get_non_intra_block(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
, short *dest
)
969 uint8_t *quant_matrix
= picture
->non_intra_quantizer_matrix
;
970 int quantizer_scale
= picture
->quantizer_scale
;
977 if (!picture
->alternate_scan
) {
978 scan
= mpeg2_scan_norm_orig
;
980 scan
= mpeg2_scan_alt_orig
;
983 vl_vlc_needbits(&bs
->vlc
);
984 if (bs
->vlc
.buf
>= 0x28000000) {
985 tab
= DCT_B14DC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
991 if (bs
->vlc
.buf
>= 0x28000000) {
993 tab
= DCT_B14AC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
998 break; /* end of block */
1002 bs
->vlc
.buf
<<= tab
->len
;
1003 bs
->vlc
.bits
+= tab
->len
+ 1;
1004 val
= ((2*tab
->level
+1) * quantizer_scale
* quant_matrix
[j
]) >> 5;
1006 /* if (bitstream_get (1)) val = -val; */
1007 val
= (val
^ vl_vlc_sbits(&bs
->vlc
, 1)) - vl_vlc_sbits(&bs
->vlc
, 1);
1014 vl_vlc_needbits(&bs
->vlc
);
1021 if (bs
->vlc
.buf
>= 0x04000000) {
1023 tab
= DCT_B14_8
+ (vl_vlc_ubits(&bs
->vlc
, 8) - 4);
1031 i
+= UBITS(bs
->vlc
.buf
<< 6, 6) - 64;
1033 break; /* illegal, check needed to avoid buffer overflow */
1037 vl_vlc_dumpbits(&bs
->vlc
, 12);
1038 vl_vlc_needbits(&bs
->vlc
);
1039 val
= 2 * (vl_vlc_sbits(&bs
->vlc
, 12) + vl_vlc_sbits(&bs
->vlc
, 1)) + 1;
1040 val
= (val
* quantizer_scale
* quant_matrix
[j
]) / 32;
1046 vl_vlc_dumpbits(&bs
->vlc
, 12);
1047 vl_vlc_needbits(&bs
->vlc
);
1051 } else if (bs
->vlc
.buf
>= 0x02000000) {
1052 tab
= DCT_B14_10
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 8);
1056 } else if (bs
->vlc
.buf
>= 0x00800000) {
1057 tab
= DCT_13
+ (vl_vlc_ubits(&bs
->vlc
, 13) - 16);
1061 } else if (bs
->vlc
.buf
>= 0x00200000) {
1062 tab
= DCT_15
+ (vl_vlc_ubits(&bs
->vlc
, 15) - 16);
1067 tab
= DCT_16
+ vl_vlc_ubits(&bs
->vlc
, 16);
1069 vl_vlc_getword(&bs
->vlc
, bs
->vlc
.bits
+ 16);
1074 break; /* illegal, check needed to avoid buffer overflow */
1076 dest
[63] ^= mismatch
& 1;
1077 vl_vlc_dumpbits(&bs
->vlc
, 2); /* dump end of block code */
1081 get_mpeg1_intra_block(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
, short *dest
)
1084 const uint8_t *scan
;
1085 uint8_t *quant_matrix
= picture
->intra_quantizer_matrix
;
1086 int quantizer_scale
= picture
->quantizer_scale
;
1091 if (!picture
->alternate_scan
) {
1092 scan
= mpeg2_scan_norm_orig
;
1094 scan
= mpeg2_scan_alt_orig
;
1097 vl_vlc_needbits(&bs
->vlc
);
1100 if (bs
->vlc
.buf
>= 0x28000000) {
1102 tab
= DCT_B14AC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
1106 break; /* end of block */
1110 bs
->vlc
.buf
<<= tab
->len
;
1111 bs
->vlc
.bits
+= tab
->len
+ 1;
1112 val
= (tab
->level
* quantizer_scale
* quant_matrix
[j
]) >> 4;
1115 val
= (val
- 1) | 1;
1117 /* if (bitstream_get (1)) val = -val; */
1118 val
= (val
^ vl_vlc_sbits(&bs
->vlc
, 1)) - vl_vlc_sbits(&bs
->vlc
, 1);
1124 vl_vlc_needbits(&bs
->vlc
);
1128 } else if (bs
->vlc
.buf
>= 0x04000000) {
1130 tab
= DCT_B14_8
+ (vl_vlc_ubits(&bs
->vlc
, 8) - 4);
1138 i
+= UBITS(bs
->vlc
.buf
<< 6, 6) - 64;
1140 break; /* illegal, check needed to avoid buffer overflow */
1144 vl_vlc_dumpbits(&bs
->vlc
, 12);
1145 vl_vlc_needbits(&bs
->vlc
);
1146 val
= vl_vlc_sbits(&bs
->vlc
, 8);
1147 if (! (val
& 0x7f)) {
1148 vl_vlc_dumpbits(&bs
->vlc
, 8);
1149 val
= vl_vlc_ubits(&bs
->vlc
, 8) + 2 * val
;
1151 val
= (val
* quantizer_scale
* quant_matrix
[j
]) / 16;
1154 val
= (val
+ ~SBITS (val
, 1)) | 1;
1159 vl_vlc_dumpbits(&bs
->vlc
, 8);
1160 vl_vlc_needbits(&bs
->vlc
);
1164 } else if (bs
->vlc
.buf
>= 0x02000000) {
1165 tab
= DCT_B14_10
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 8);
1169 } else if (bs
->vlc
.buf
>= 0x00800000) {
1170 tab
= DCT_13
+ (vl_vlc_ubits(&bs
->vlc
, 13) - 16);
1174 } else if (bs
->vlc
.buf
>= 0x00200000) {
1175 tab
= DCT_15
+ (vl_vlc_ubits(&bs
->vlc
, 15) - 16);
1180 tab
= DCT_16
+ vl_vlc_ubits(&bs
->vlc
, 16);
1182 vl_vlc_getword(&bs
->vlc
, bs
->vlc
.bits
+ 16);
1187 break; /* illegal, check needed to avoid buffer overflow */
1189 vl_vlc_dumpbits(&bs
->vlc
, 2); /* dump end of block code */
1193 get_mpeg1_non_intra_block(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
, short *dest
)
1196 const uint8_t * scan
;
1197 uint8_t *quant_matrix
= picture
->non_intra_quantizer_matrix
;
1198 int quantizer_scale
= picture
->quantizer_scale
;
1203 if (!picture
->alternate_scan
) {
1204 scan
= mpeg2_scan_norm_orig
;
1206 scan
= mpeg2_scan_alt_orig
;
1209 vl_vlc_needbits(&bs
->vlc
);
1210 if (bs
->vlc
.buf
>= 0x28000000) {
1211 tab
= DCT_B14DC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
1217 if (bs
->vlc
.buf
>= 0x28000000) {
1219 tab
= DCT_B14AC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
1224 break; /* end of block */
1228 bs
->vlc
.buf
<<= tab
->len
;
1229 bs
->vlc
.bits
+= tab
->len
+ 1;
1230 val
= ((2*tab
->level
+1) * quantizer_scale
* quant_matrix
[j
]) >> 5;
1233 val
= (val
- 1) | 1;
1235 /* if (bitstream_get (1)) val = -val; */
1236 val
= (val
^ vl_vlc_sbits(&bs
->vlc
, 1)) - vl_vlc_sbits(&bs
->vlc
, 1);
1242 vl_vlc_needbits(&bs
->vlc
);
1249 if (bs
->vlc
.buf
>= 0x04000000) {
1251 tab
= DCT_B14_8
+ (vl_vlc_ubits(&bs
->vlc
, 8) - 4);
1259 i
+= UBITS(bs
->vlc
.buf
<< 6, 6) - 64;
1261 break; /* illegal, check needed to avoid buffer overflow */
1265 vl_vlc_dumpbits(&bs
->vlc
, 12);
1266 vl_vlc_needbits(&bs
->vlc
);
1267 val
= vl_vlc_sbits(&bs
->vlc
, 8);
1268 if (! (val
& 0x7f)) {
1269 vl_vlc_dumpbits(&bs
->vlc
, 8);
1270 val
= vl_vlc_ubits(&bs
->vlc
, 8) + 2 * val
;
1272 val
= 2 * (val
+ SBITS (val
, 1)) + 1;
1273 val
= (val
* quantizer_scale
* quant_matrix
[j
]) / 32;
1276 val
= (val
+ ~SBITS (val
, 1)) | 1;
1281 vl_vlc_dumpbits(&bs
->vlc
, 8);
1282 vl_vlc_needbits(&bs
->vlc
);
1286 } else if (bs
->vlc
.buf
>= 0x02000000) {
1287 tab
= DCT_B14_10
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 8);
1291 } else if (bs
->vlc
.buf
>= 0x00800000) {
1292 tab
= DCT_13
+ (vl_vlc_ubits(&bs
->vlc
, 13) - 16);
1296 } else if (bs
->vlc
.buf
>= 0x00200000) {
1297 tab
= DCT_15
+ (vl_vlc_ubits(&bs
->vlc
, 15) - 16);
1302 tab
= DCT_16
+ vl_vlc_ubits(&bs
->vlc
, 16);
1304 vl_vlc_getword(&bs
->vlc
, bs
->vlc
.bits
+ 16);
1309 break; /* illegal, check needed to avoid buffer overflow */
1311 vl_vlc_dumpbits(&bs
->vlc
, 2); /* dump end of block code */
1315 slice_intra_DCT(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
, int cc
,
1316 unsigned x
, unsigned y
, enum pipe_mpeg12_dct_type coding
, int dc_dct_pred
[3])
1318 short *dest
= bs
->ycbcr_buffer
[cc
];
1320 bs
->ycbcr_stream
[cc
]->x
= x
;
1321 bs
->ycbcr_stream
[cc
]->y
= y
;
1322 bs
->ycbcr_stream
[cc
]->intra
= PIPE_MPEG12_DCT_INTRA
;
1323 bs
->ycbcr_stream
[cc
]->coding
= coding
;
1325 vl_vlc_needbits(&bs
->vlc
);
1327 /* Get the intra DC coefficient and inverse quantize it */
1329 dc_dct_pred
[0] += get_luma_dc_dct_diff(bs
);
1331 dc_dct_pred
[cc
] += get_chroma_dc_dct_diff(bs
);
1333 memset(dest
, 0, sizeof(int16_t) * 64);
1334 dest
[0] = dc_dct_pred
[cc
] << (3 - picture
->intra_dc_precision
);
1335 if (picture
->mpeg1
) {
1336 if (picture
->picture_coding_type
!= D_TYPE
)
1337 get_mpeg1_intra_block(bs
, picture
, dest
);
1338 } else if (picture
->intra_vlc_format
)
1339 get_intra_block_B15(bs
, picture
, dest
);
1341 get_intra_block_B14(bs
, picture
, dest
);
1343 bs
->num_ycbcr_blocks
[cc
]++;
1344 bs
->ycbcr_stream
[cc
]++;
1345 bs
->ycbcr_buffer
[cc
] += 64;
1349 slice_non_intra_DCT(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
, int cc
,
1350 unsigned x
, unsigned y
, enum pipe_mpeg12_dct_type coding
)
1352 short *dest
= bs
->ycbcr_buffer
[cc
];
1354 bs
->ycbcr_stream
[cc
]->x
= x
;
1355 bs
->ycbcr_stream
[cc
]->y
= y
;
1356 bs
->ycbcr_stream
[cc
]->intra
= PIPE_MPEG12_DCT_DELTA
;
1357 bs
->ycbcr_stream
[cc
]->coding
= coding
;
1359 memset(dest
, 0, sizeof(int16_t) * 64);
1361 get_mpeg1_non_intra_block(bs
, picture
, dest
);
1363 get_non_intra_block(bs
, picture
, dest
);
1365 bs
->num_ycbcr_blocks
[cc
]++;
1366 bs
->ycbcr_stream
[cc
]++;
1367 bs
->ycbcr_buffer
[cc
] += 64;
1371 motion_mp1(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1373 int motion_x
, motion_y
;
1375 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1377 vl_vlc_needbits(&bs
->vlc
);
1378 motion_x
= (mv
->top
.x
+ (get_motion_delta(bs
, f_code
[0]) << f_code
[1]));
1379 motion_x
= bound_motion_vector (motion_x
, f_code
[0] + f_code
[1]);
1380 mv
->top
.x
= mv
->bottom
.x
= motion_x
;
1382 vl_vlc_needbits(&bs
->vlc
);
1383 motion_y
= (mv
->top
.y
+ (get_motion_delta(bs
, f_code
[0]) << f_code
[1]));
1384 motion_y
= bound_motion_vector (motion_y
, f_code
[0] + f_code
[1]);
1385 mv
->top
.y
= mv
->bottom
.y
= motion_y
;
1389 motion_fr_frame(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1391 int motion_x
, motion_y
;
1393 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1395 vl_vlc_needbits(&bs
->vlc
);
1396 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1397 motion_x
= bound_motion_vector(motion_x
, f_code
[0]);
1398 mv
->top
.x
= mv
->bottom
.x
= motion_x
;
1400 vl_vlc_needbits(&bs
->vlc
);
1401 motion_y
= mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]);
1402 motion_y
= bound_motion_vector(motion_y
, f_code
[1]);
1403 mv
->top
.y
= mv
->bottom
.y
= motion_y
;
1407 motion_fr_field(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1409 int motion_x
, motion_y
;
1411 vl_vlc_needbits(&bs
->vlc
);
1412 mv
->top
.field_select
= vl_vlc_ubits(&bs
->vlc
, 1) ?
1413 PIPE_VIDEO_BOTTOM_FIELD
: PIPE_VIDEO_TOP_FIELD
;
1414 vl_vlc_dumpbits(&bs
->vlc
, 1);
1416 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1417 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1418 mv
->top
.x
= motion_x
;
1420 vl_vlc_needbits(&bs
->vlc
);
1421 motion_y
= (mv
->top
.y
>> 1) + get_motion_delta(bs
, f_code
[1]);
1422 /* motion_y = bound_motion_vector (motion_y, f_code[1]); */
1423 mv
->top
.y
= motion_y
<< 1;
1425 vl_vlc_needbits(&bs
->vlc
);
1426 mv
->bottom
.field_select
= vl_vlc_ubits(&bs
->vlc
, 1) ?
1427 PIPE_VIDEO_BOTTOM_FIELD
: PIPE_VIDEO_TOP_FIELD
;
1428 vl_vlc_dumpbits(&bs
->vlc
, 1);
1430 motion_x
= mv
->bottom
.x
+ get_motion_delta(bs
, f_code
[0]);
1431 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1432 mv
->bottom
.x
= motion_x
;
1434 vl_vlc_needbits(&bs
->vlc
);
1435 motion_y
= (mv
->bottom
.y
>> 1) + get_motion_delta(bs
, f_code
[1]);
1436 /* motion_y = bound_motion_vector (motion_y, f_code[1]); */
1437 mv
->bottom
.y
= motion_y
<< 1;
1441 motion_fr_dmv(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1443 int motion_x
, motion_y
;
1445 // TODO Implement dmv
1446 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1448 vl_vlc_needbits(&bs
->vlc
);
1449 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1450 motion_x
= bound_motion_vector(motion_x
, f_code
[0]);
1451 mv
->top
.x
= mv
->bottom
.x
= motion_x
;
1453 vl_vlc_needbits(&bs
->vlc
);
1454 motion_y
= (mv
->top
.y
>> 1) + get_motion_delta(bs
, f_code
[1]);
1455 /* motion_y = bound_motion_vector (motion_y, f_code[1]); */
1456 mv
->top
.y
= mv
->bottom
.y
= motion_y
<< 1;
1459 /* like motion_frame, but parsing without actual motion compensation */
1461 motion_fr_conceal(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1465 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1467 vl_vlc_needbits(&bs
->vlc
);
1468 tmp
= (mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]));
1469 tmp
= bound_motion_vector (tmp
, f_code
[0]);
1470 mv
->top
.x
= mv
->bottom
.x
= tmp
;
1472 vl_vlc_needbits(&bs
->vlc
);
1473 tmp
= (mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]));
1474 tmp
= bound_motion_vector (tmp
, f_code
[1]);
1475 mv
->top
.y
= mv
->bottom
.y
= tmp
;
1477 vl_vlc_dumpbits(&bs
->vlc
, 1); /* remove marker_bit */
1481 motion_fi_field(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1483 int motion_x
, motion_y
;
1485 vl_vlc_needbits(&bs
->vlc
);
1488 //vl_vlc_ubits(&bs->vlc, 1);
1490 // TODO field select may need to do something here for bob (weave ok)
1491 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1492 vl_vlc_dumpbits(&bs
->vlc
, 1);
1494 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1495 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1496 mv
->top
.x
= mv
->bottom
.x
= motion_x
;
1498 vl_vlc_needbits(&bs
->vlc
);
1499 motion_y
= mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]);
1500 motion_y
= bound_motion_vector (motion_y
, f_code
[1]);
1501 mv
->top
.y
= mv
->bottom
.y
= motion_y
;
1505 motion_fi_16x8(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1507 int motion_x
, motion_y
;
1509 vl_vlc_needbits(&bs
->vlc
);
1512 //vl_vlc_ubits(&bs->vlc, 1);
1514 // TODO field select may need to do something here bob (weave ok)
1515 mv
->top
.field_select
= PIPE_VIDEO_FRAME
;
1516 vl_vlc_dumpbits(&bs
->vlc
, 1);
1518 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1519 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1520 mv
->top
.x
= motion_x
;
1522 vl_vlc_needbits(&bs
->vlc
);
1523 motion_y
= mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]);
1524 motion_y
= bound_motion_vector (motion_y
, f_code
[1]);
1525 mv
->top
.y
= motion_y
;
1527 vl_vlc_needbits(&bs
->vlc
);
1529 //vl_vlc_ubits(&bs->vlc, 1);
1531 // TODO field select may need to do something here for bob (weave ok)
1532 mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1533 vl_vlc_dumpbits(&bs
->vlc
, 1);
1535 motion_x
= mv
->bottom
.x
+ get_motion_delta(bs
, f_code
[0]);
1536 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1537 mv
->bottom
.x
= motion_x
;
1539 vl_vlc_needbits(&bs
->vlc
);
1540 motion_y
= mv
->bottom
.y
+ get_motion_delta(bs
, f_code
[1]);
1541 motion_y
= bound_motion_vector (motion_y
, f_code
[1]);
1542 mv
->bottom
.y
= motion_y
;
1546 motion_fi_dmv(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1548 int motion_x
, motion_y
;
1550 // TODO field select may need to do something here for bob (weave ok)
1551 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1553 vl_vlc_needbits(&bs
->vlc
);
1554 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1555 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1556 mv
->top
.x
= mv
->bottom
.x
= motion_x
;
1558 vl_vlc_needbits(&bs
->vlc
);
1559 motion_y
= mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]);
1560 motion_y
= bound_motion_vector (motion_y
, f_code
[1]);
1561 mv
->top
.y
= mv
->bottom
.y
= motion_y
;
1566 motion_fi_conceal(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1570 vl_vlc_needbits(&bs
->vlc
);
1571 vl_vlc_dumpbits(&bs
->vlc
, 1); /* remove field_select */
1573 tmp
= (mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]));
1574 tmp
= bound_motion_vector(tmp
, f_code
[0]);
1575 mv
->top
.x
= mv
->bottom
.x
= tmp
;
1577 vl_vlc_needbits(&bs
->vlc
);
1578 tmp
= (mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]));
1579 tmp
= bound_motion_vector(tmp
, f_code
[1]);
1580 mv
->top
.y
= mv
->bottom
.y
= tmp
;
1582 vl_vlc_dumpbits(&bs
->vlc
, 1); /* remove marker_bit */
1585 #define MOTION_CALL(routine, macroblock_modes) \
1587 if ((macroblock_modes) & MACROBLOCK_MOTION_FORWARD) \
1588 routine(bs, picture->f_code[0], &mv_fwd); \
1589 if ((macroblock_modes) & MACROBLOCK_MOTION_BACKWARD) \
1590 routine(bs, picture->f_code[1], &mv_bwd); \
1593 #define NEXT_MACROBLOCK \
1595 bs->mv_stream[0][x+y*bs->width] = mv_fwd; \
1596 bs->mv_stream[1][x+y*bs->width] = mv_bwd; \
1598 if (x == bs->width) { \
1600 if (y >= bs->height) \
1607 slice_init(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
, int *x
, int *y
)
1611 vl_vlc_need32bits(&bs
->vlc
);
1612 while(bs
->vlc
.buf
< 0x101 || bs
->vlc
.buf
> 0x1AF) {
1613 if(!vl_vlc_getbyte(&bs
->vlc
))
1616 *y
= (bs
->vlc
.buf
& 0xFF) - 1;
1617 vl_vlc_restart(&bs
->vlc
);
1619 picture
->quantizer_scale
= get_quantizer_scale(bs
, picture
);
1621 /* ignore intra_slice and all the extra data */
1622 while (bs
->vlc
.buf
& 0x80000000) {
1623 vl_vlc_dumpbits(&bs
->vlc
, 9);
1624 vl_vlc_needbits(&bs
->vlc
);
1627 /* decode initial macroblock address increment */
1630 if (bs
->vlc
.buf
>= 0x08000000) {
1631 mba
= MBA_5
+ (vl_vlc_ubits(&bs
->vlc
, 6) - 2);
1633 } else if (bs
->vlc
.buf
>= 0x01800000) {
1634 mba
= MBA_11
+ (vl_vlc_ubits(&bs
->vlc
, 12) - 24);
1636 } else switch (vl_vlc_ubits(&bs
->vlc
, 12)) {
1637 case 8: /* macroblock_escape */
1639 vl_vlc_dumpbits(&bs
->vlc
, 11);
1640 vl_vlc_needbits(&bs
->vlc
);
1642 case 15: /* macroblock_stuffing (MPEG1 only) */
1643 bs
->vlc
.buf
&= 0xfffff;
1644 vl_vlc_dumpbits(&bs
->vlc
, 11);
1645 vl_vlc_needbits(&bs
->vlc
);
1647 default: /* error */
1651 vl_vlc_dumpbits(&bs
->vlc
, mba
->len
+ 1);
1654 while (*x
>= bs
->width
) {
1658 if (*y
> bs
->height
)
1665 decode_slice(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
*picture
)
1667 struct pipe_motionvector mv_fwd
, mv_bwd
;
1668 enum pipe_mpeg12_dct_type dct_type
;
1670 /* predictor for DC coefficients in intra blocks */
1671 int dc_dct_pred
[3] = { 0, 0, 0 };
1675 if (!slice_init(bs
, picture
, &x
, &y
))
1678 mv_fwd
.top
.x
= mv_fwd
.top
.y
= mv_fwd
.bottom
.x
= mv_fwd
.bottom
.y
= 0;
1679 mv_fwd
.top
.field_select
= mv_fwd
.bottom
.field_select
= PIPE_VIDEO_FRAME
;
1681 mv_bwd
.top
.x
= mv_bwd
.top
.y
= mv_bwd
.bottom
.x
= mv_bwd
.bottom
.y
= 0;
1682 mv_bwd
.top
.field_select
= mv_bwd
.bottom
.field_select
= PIPE_VIDEO_FRAME
;
1685 int macroblock_modes
;
1689 vl_vlc_needbits(&bs
->vlc
);
1691 macroblock_modes
= get_macroblock_modes(bs
, picture
); //macroblock_modes()
1692 dct_type
= macroblock_modes
& DCT_TYPE_INTERLACED
?
1693 PIPE_MPEG12_DCT_TYPE_FIELD
: PIPE_MPEG12_DCT_TYPE_FRAME
;
1695 switch(macroblock_modes
& (MACROBLOCK_MOTION_FORWARD
|MACROBLOCK_MOTION_BACKWARD
)) {
1696 case (MACROBLOCK_MOTION_FORWARD
|MACROBLOCK_MOTION_BACKWARD
):
1697 mv_fwd
.top
.weight
= mv_fwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_HALF
;
1698 mv_bwd
.top
.weight
= mv_bwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_HALF
;
1702 case MACROBLOCK_MOTION_FORWARD
:
1703 mv_fwd
.top
.weight
= mv_fwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MAX
;
1704 mv_bwd
.top
.weight
= mv_bwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1707 case MACROBLOCK_MOTION_BACKWARD
:
1708 mv_fwd
.top
.weight
= mv_fwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1709 mv_bwd
.top
.weight
= mv_bwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MAX
;
1713 /* maybe integrate MACROBLOCK_QUANT test into get_macroblock_modes ? */
1714 if (macroblock_modes
& MACROBLOCK_QUANT
)
1715 picture
->quantizer_scale
= get_quantizer_scale(bs
, picture
);
1717 if (macroblock_modes
& MACROBLOCK_INTRA
) {
1719 if (picture
->concealment_motion_vectors
) {
1720 if (picture
->picture_structure
== FRAME_PICTURE
)
1721 motion_fr_conceal(bs
, picture
->f_code
[0], &mv_fwd
);
1723 motion_fi_conceal(bs
, picture
->f_code
[0], &mv_fwd
);
1726 mv_fwd
.top
.x
= mv_fwd
.top
.y
= mv_fwd
.bottom
.x
= mv_fwd
.bottom
.y
= 0;
1727 mv_bwd
.top
.x
= mv_bwd
.top
.y
= mv_bwd
.bottom
.x
= mv_bwd
.bottom
.y
= 0;
1729 mv_fwd
.top
.weight
= mv_fwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1730 mv_bwd
.top
.weight
= mv_bwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1732 // unravaled loop of 6 block(i) calls in macroblock()
1733 slice_intra_DCT(bs
, picture
, 0, x
*2+0, y
*2+0, dct_type
, dc_dct_pred
);
1734 slice_intra_DCT(bs
, picture
, 0, x
*2+1, y
*2+0, dct_type
, dc_dct_pred
);
1735 slice_intra_DCT(bs
, picture
, 0, x
*2+0, y
*2+1, dct_type
, dc_dct_pred
);
1736 slice_intra_DCT(bs
, picture
, 0, x
*2+1, y
*2+1, dct_type
, dc_dct_pred
);
1737 slice_intra_DCT(bs
, picture
, 1, x
, y
, dct_type
, dc_dct_pred
);
1738 slice_intra_DCT(bs
, picture
, 2, x
, y
, dct_type
, dc_dct_pred
);
1740 if (picture
->picture_coding_type
== D_TYPE
) {
1741 vl_vlc_needbits(&bs
->vlc
);
1742 vl_vlc_dumpbits(&bs
->vlc
, 1);
1746 if (picture
->picture_structure
== FRAME_PICTURE
)
1747 switch (macroblock_modes
& MOTION_TYPE_MASK
) {
1749 if (picture
->mpeg1
) {
1750 MOTION_CALL(motion_mp1
, macroblock_modes
);
1752 MOTION_CALL(motion_fr_frame
, macroblock_modes
);
1757 MOTION_CALL (motion_fr_field
, macroblock_modes
);
1761 MOTION_CALL (motion_fr_dmv
, MACROBLOCK_MOTION_FORWARD
);
1765 /* non-intra mb without forward mv in a P picture */
1766 mv_fwd
.top
.x
= mv_fwd
.top
.y
= mv_fwd
.bottom
.x
= mv_fwd
.bottom
.y
= 0;
1767 mv_bwd
.top
.x
= mv_bwd
.top
.y
= mv_bwd
.bottom
.x
= mv_bwd
.bottom
.y
= 0;
1771 switch (macroblock_modes
& MOTION_TYPE_MASK
) {
1773 MOTION_CALL (motion_fi_field
, macroblock_modes
);
1777 MOTION_CALL (motion_fi_16x8
, macroblock_modes
);
1781 MOTION_CALL (motion_fi_dmv
, MACROBLOCK_MOTION_FORWARD
);
1785 /* non-intra mb without forward mv in a P picture */
1786 mv_fwd
.top
.x
= mv_fwd
.top
.y
= mv_fwd
.bottom
.x
= mv_fwd
.bottom
.y
= 0;
1787 mv_bwd
.top
.x
= mv_bwd
.top
.y
= mv_bwd
.bottom
.x
= mv_bwd
.bottom
.y
= 0;
1791 if (macroblock_modes
& MACROBLOCK_PATTERN
) {
1792 int coded_block_pattern
= get_coded_block_pattern(bs
);
1794 // TODO optimize not fully used for idct accel only mc.
1795 if (coded_block_pattern
& 0x20)
1796 slice_non_intra_DCT(bs
, picture
, 0, x
*2+0, y
*2+0, dct_type
); // cc0 luma 0
1797 if (coded_block_pattern
& 0x10)
1798 slice_non_intra_DCT(bs
, picture
, 0, x
*2+1, y
*2+0, dct_type
); // cc0 luma 1
1799 if (coded_block_pattern
& 0x08)
1800 slice_non_intra_DCT(bs
, picture
, 0, x
*2+0, y
*2+1, dct_type
); // cc0 luma 2
1801 if (coded_block_pattern
& 0x04)
1802 slice_non_intra_DCT(bs
, picture
, 0, x
*2+1, y
*2+1, dct_type
); // cc0 luma 3
1803 if (coded_block_pattern
& 0x2)
1804 slice_non_intra_DCT(bs
, picture
, 1, x
, y
, dct_type
); // cc1 croma
1805 if (coded_block_pattern
& 0x1)
1806 slice_non_intra_DCT(bs
, picture
, 2, x
, y
, dct_type
); // cc2 croma
1809 dc_dct_pred
[0] = dc_dct_pred
[1] = dc_dct_pred
[2] = 0;
1814 vl_vlc_needbits(&bs
->vlc
);
1817 if (bs
->vlc
.buf
>= 0x10000000) {
1818 mba
= MBA_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 2);
1820 } else if (bs
->vlc
.buf
>= 0x03000000) {
1821 mba
= MBA_11
+ (vl_vlc_ubits(&bs
->vlc
, 11) - 24);
1823 } else switch (vl_vlc_ubits(&bs
->vlc
, 11)) {
1824 case 8: /* macroblock_escape */
1827 case 15: /* macroblock_stuffing (MPEG1 only) */
1828 vl_vlc_dumpbits(&bs
->vlc
, 11);
1829 vl_vlc_needbits(&bs
->vlc
);
1831 default: /* end of slice, or error */
1835 vl_vlc_dumpbits(&bs
->vlc
, mba
->len
);
1836 mba_inc
+= mba
->mba
;
1838 //TODO conversion to signed format signed format
1839 dc_dct_pred
[0] = dc_dct_pred
[1] = dc_dct_pred
[2] = 0;
1841 switch(picture
->picture_structure
) {
1843 mv_fwd
.top
.field_select
= mv_fwd
.bottom
.field_select
= PIPE_VIDEO_FRAME
;
1844 mv_bwd
.top
.field_select
= mv_bwd
.bottom
.field_select
= PIPE_VIDEO_FRAME
;
1848 mv_fwd
.top
.field_select
= mv_fwd
.bottom
.field_select
= PIPE_VIDEO_TOP_FIELD
;
1849 mv_bwd
.top
.field_select
= mv_bwd
.bottom
.field_select
= PIPE_VIDEO_TOP_FIELD
;
1853 mv_fwd
.top
.field_select
= mv_fwd
.bottom
.field_select
= PIPE_VIDEO_BOTTOM_FIELD
;
1854 mv_bwd
.top
.field_select
= mv_bwd
.bottom
.field_select
= PIPE_VIDEO_BOTTOM_FIELD
;
1858 if (picture
->picture_coding_type
== P_TYPE
) {
1859 mv_fwd
.top
.x
= mv_fwd
.top
.y
= mv_fwd
.bottom
.x
= mv_fwd
.bottom
.y
= 0;
1860 mv_fwd
.top
.weight
= mv_fwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MAX
;
1864 } while (--mba_inc
);
1870 vl_mpg12_bs_init(struct vl_mpg12_bs
*bs
, unsigned width
, unsigned height
)
1874 memset(bs
, 0, sizeof(struct vl_mpg12_bs
));
1877 bs
->height
= height
;
1881 vl_mpg12_bs_set_buffers(struct vl_mpg12_bs
*bs
, struct pipe_ycbcr_block
*ycbcr_stream
[VL_MAX_PLANES
],
1882 short *ycbcr_buffer
[VL_MAX_PLANES
], struct pipe_motionvector
*mv_stream
[VL_MAX_REF_FRAMES
])
1887 assert(ycbcr_stream
&& ycbcr_buffer
);
1890 for (i
= 0; i
< VL_MAX_PLANES
; ++i
) {
1891 bs
->ycbcr_stream
[i
] = ycbcr_stream
[i
];
1892 bs
->ycbcr_buffer
[i
] = ycbcr_buffer
[i
];
1894 for (i
= 0; i
< VL_MAX_REF_FRAMES
; ++i
)
1895 bs
->mv_stream
[i
] = mv_stream
[i
];
1898 for (i
= 0; i
< bs
->width
*bs
->height
; ++i
) {
1899 bs
->mv_stream
[0][i
].top
.x
= bs
->mv_stream
[0][i
].top
.y
= 0;
1900 bs
->mv_stream
[0][i
].top
.field_select
= PIPE_VIDEO_FRAME
;
1901 bs
->mv_stream
[0][i
].top
.weight
= PIPE_VIDEO_MV_WEIGHT_MAX
;
1902 bs
->mv_stream
[0][i
].bottom
.x
= bs
->mv_stream
[0][i
].bottom
.y
= 0;
1903 bs
->mv_stream
[0][i
].bottom
.field_select
= PIPE_VIDEO_FRAME
;
1904 bs
->mv_stream
[0][i
].bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MAX
;
1906 bs
->mv_stream
[1][i
].top
.x
= bs
->mv_stream
[1][i
].top
.y
= 0;
1907 bs
->mv_stream
[1][i
].top
.field_select
= PIPE_VIDEO_FRAME
;
1908 bs
->mv_stream
[1][i
].top
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1909 bs
->mv_stream
[1][i
].bottom
.x
= bs
->mv_stream
[1][i
].bottom
.y
= 0;
1910 bs
->mv_stream
[1][i
].bottom
.field_select
= PIPE_VIDEO_FRAME
;
1911 bs
->mv_stream
[1][i
].bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1916 vl_mpg12_bs_decode(struct vl_mpg12_bs
*bs
, unsigned num_bytes
, const void *buffer
,
1917 struct pipe_mpeg12_picture_desc
*picture
, unsigned num_ycbcr_blocks
[3])
1920 assert(num_ycbcr_blocks
);
1921 assert(buffer
&& num_bytes
);
1923 bs
->num_ycbcr_blocks
= num_ycbcr_blocks
;
1925 vl_vlc_init(&bs
->vlc
, buffer
, num_bytes
);
1927 while(decode_slice(bs
, picture
));