1 /**************************************************************************
3 * Copyright 2011 Christian König.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
29 * This file is based uppon slice_xvmc.c and vlc.h from the xine project,
30 * which in turn is based on mpeg2dec. The following is the original copyright:
32 * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
33 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
35 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
36 * See http://libmpeg2.sourceforge.net/ for updates.
38 * mpeg2dec is free software; you can redistribute it and/or modify
39 * it under the terms of the GNU General Public License as published by
40 * the Free Software Foundation; either version 2 of the License, or
41 * (at your option) any later version.
43 * mpeg2dec is distributed in the hope that it will be useful,
44 * but WITHOUT ANY WARRANTY; without even the implied warranty of
45 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46 * GNU General Public License for more details.
48 * You should have received a copy of the GNU General Public License
49 * along with this program; if not, write to the Free Software
50 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
55 #include <pipe/p_video_state.h>
59 #include "vl_mpeg12_bitstream.h"
61 /* take num bits from the high part of bit_buf and zero extend them */
62 #define UBITS(buf,num) (((uint32_t)(buf)) >> (32 - (num)))
64 /* take num bits from the high part of bit_buf and sign extend them */
65 #define SBITS(buf,num) (((int32_t)(buf)) >> (32 - (num)))
67 #define SATURATE(val) \
69 if ((uint32_t)(val + 2048) > 4095) \
70 val = (val > 0) ? 2047 : -2048; \
73 /* macroblock modes */
74 #define MACROBLOCK_INTRA 1
75 #define MACROBLOCK_PATTERN 2
76 #define MACROBLOCK_MOTION_BACKWARD 4
77 #define MACROBLOCK_MOTION_FORWARD 8
78 #define MACROBLOCK_QUANT 16
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 static const int non_linear_quantizer_scale
[] = {
461 0, 1, 2, 3, 4, 5, 6, 7,
462 8, 10, 12, 14, 16, 18, 20, 22,
463 24, 28, 32, 36, 40, 44, 48, 52,
464 56, 64, 72, 80, 88, 96, 104, 112
468 get_macroblock_modes(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
)
470 int macroblock_modes
;
473 switch (picture
->picture_coding_type
) {
476 tab
= MB_I
+ vl_vlc_ubits(&bs
->vlc
, 1);
477 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
478 macroblock_modes
= tab
->modes
;
480 return macroblock_modes
;
484 tab
= MB_P
+ vl_vlc_ubits(&bs
->vlc
, 5);
485 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
486 macroblock_modes
= tab
->modes
;
488 if (picture
->picture_structure
!= FRAME_PICTURE
) {
489 if (macroblock_modes
& MACROBLOCK_MOTION_FORWARD
) {
490 macroblock_modes
|= vl_vlc_ubits(&bs
->vlc
, 2) * MOTION_TYPE_BASE
;
491 vl_vlc_dumpbits(&bs
->vlc
, 2);
493 return macroblock_modes
;
494 } else if (picture
->frame_pred_frame_dct
) {
495 if (macroblock_modes
& MACROBLOCK_MOTION_FORWARD
)
496 macroblock_modes
|= MC_FRAME
;
497 return macroblock_modes
;
499 if (macroblock_modes
& MACROBLOCK_MOTION_FORWARD
) {
500 macroblock_modes
|= vl_vlc_ubits(&bs
->vlc
, 2) * MOTION_TYPE_BASE
;
501 vl_vlc_dumpbits(&bs
->vlc
, 2);
503 return macroblock_modes
;
508 tab
= MB_B
+ vl_vlc_ubits(&bs
->vlc
, 6);
509 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
510 macroblock_modes
= tab
->modes
;
512 if (picture
->picture_structure
!= FRAME_PICTURE
) {
513 if (! (macroblock_modes
& MACROBLOCK_INTRA
)) {
514 macroblock_modes
|= vl_vlc_ubits(&bs
->vlc
, 2) * MOTION_TYPE_BASE
;
515 vl_vlc_dumpbits(&bs
->vlc
, 2);
517 } else if (picture
->frame_pred_frame_dct
) {
518 macroblock_modes
|= MC_FRAME
;
519 } else if (!(macroblock_modes
& MACROBLOCK_INTRA
)) {
520 macroblock_modes
|= vl_vlc_ubits(&bs
->vlc
, 2) * MOTION_TYPE_BASE
;
521 vl_vlc_dumpbits(&bs
->vlc
, 2);
523 return macroblock_modes
;
527 vl_vlc_dumpbits(&bs
->vlc
, 1);
528 return MACROBLOCK_INTRA
;
535 static inline enum pipe_mpeg12_dct_type
536 get_dct_type(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
, int macroblock_modes
)
538 enum pipe_mpeg12_dct_type dct_type
= PIPE_MPEG12_DCT_TYPE_FRAME
;
540 if ((picture
->picture_structure
== FRAME_PICTURE
) &&
541 (!picture
->frame_pred_frame_dct
) &&
542 (macroblock_modes
& (MACROBLOCK_INTRA
| MACROBLOCK_PATTERN
))) {
544 dct_type
= vl_vlc_ubits(&bs
->vlc
, 1) ? PIPE_MPEG12_DCT_TYPE_FIELD
: PIPE_MPEG12_DCT_TYPE_FRAME
;
545 vl_vlc_dumpbits(&bs
->vlc
, 1);
551 get_quantizer_scale(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
)
553 int quantizer_scale_code
;
555 quantizer_scale_code
= vl_vlc_ubits(&bs
->vlc
, 5);
556 vl_vlc_dumpbits(&bs
->vlc
, 5);
558 if (picture
->q_scale_type
)
559 return non_linear_quantizer_scale
[quantizer_scale_code
];
561 return quantizer_scale_code
<< 1;
565 get_motion_delta(struct vl_mpg12_bs
*bs
, unsigned f_code
)
571 if (bs
->vlc
.buf
& 0x80000000) {
572 vl_vlc_dumpbits(&bs
->vlc
, 1);
574 } else if (bs
->vlc
.buf
>= 0x0c000000) {
576 tab
= MV_4
+ vl_vlc_ubits(&bs
->vlc
, 4);
577 delta
= (tab
->delta
<< f_code
) + 1;
578 bs
->vlc
.bits
+= tab
->len
+ f_code
+ 1;
579 bs
->vlc
.buf
<<= tab
->len
;
581 sign
= vl_vlc_sbits(&bs
->vlc
, 1);
585 delta
+= vl_vlc_ubits(&bs
->vlc
, f_code
);
586 bs
->vlc
.buf
<<= f_code
;
588 return (delta
^ sign
) - sign
;
592 tab
= MV_10
+ vl_vlc_ubits(&bs
->vlc
, 10);
593 delta
= (tab
->delta
<< f_code
) + 1;
594 bs
->vlc
.bits
+= tab
->len
+ 1;
595 bs
->vlc
.buf
<<= tab
->len
;
597 sign
= vl_vlc_sbits(&bs
->vlc
, 1);
601 vl_vlc_needbits(&bs
->vlc
);
602 delta
+= vl_vlc_ubits(&bs
->vlc
, f_code
);
603 vl_vlc_dumpbits(&bs
->vlc
, f_code
);
606 return (delta
^ sign
) - sign
;
611 bound_motion_vector(int vec
, unsigned f_code
)
617 limit
= 16 << f_code
;
619 if ((unsigned int)(vec
+ limit
) < 2 * limit
)
622 sign
= ((int32_t)vec
) >> 31;
623 return vec
- ((2 * limit
) ^ sign
) + sign
;
626 return ((int32_t)vec
<< (28 - f_code
)) >> (28 - f_code
);
631 get_dmv(struct vl_mpg12_bs
*bs
)
635 tab
= DMV_2
+ vl_vlc_ubits(&bs
->vlc
, 2);
636 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
641 get_coded_block_pattern(struct vl_mpg12_bs
*bs
)
645 vl_vlc_needbits(&bs
->vlc
);
647 if (bs
->vlc
.buf
>= 0x20000000) {
649 tab
= CBP_7
+ (vl_vlc_ubits(&bs
->vlc
, 7) - 16);
650 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
655 tab
= CBP_9
+ vl_vlc_ubits(&bs
->vlc
, 9);
656 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
662 get_luma_dc_dct_diff(struct vl_mpg12_bs
*bs
)
668 if (bs
->vlc
.buf
< 0xf8000000) {
669 tab
= DC_lum_5
+ vl_vlc_ubits(&bs
->vlc
, 5);
672 bs
->vlc
.bits
+= tab
->len
+ size
;
673 bs
->vlc
.buf
<<= tab
->len
;
674 dc_diff
= vl_vlc_ubits(&bs
->vlc
, size
) - UBITS (SBITS (~bs
->vlc
.buf
, 1), size
);
675 bs
->vlc
.buf
<<= size
;
678 vl_vlc_dumpbits(&bs
->vlc
, 3);
682 tab
= DC_long
+ (vl_vlc_ubits(&bs
->vlc
, 9) - 0x1e0);
684 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
685 vl_vlc_needbits(&bs
->vlc
);
686 dc_diff
= vl_vlc_ubits(&bs
->vlc
, size
) - UBITS (SBITS (~bs
->vlc
.buf
, 1), size
);
687 vl_vlc_dumpbits(&bs
->vlc
, size
);
693 get_chroma_dc_dct_diff(struct vl_mpg12_bs
*bs
)
699 if (bs
->vlc
.buf
< 0xf8000000) {
700 tab
= DC_chrom_5
+ vl_vlc_ubits(&bs
->vlc
, 5);
703 bs
->vlc
.bits
+= tab
->len
+ size
;
704 bs
->vlc
.buf
<<= tab
->len
;
705 dc_diff
= vl_vlc_ubits(&bs
->vlc
, size
) - UBITS (SBITS (~bs
->vlc
.buf
, 1), size
);
706 bs
->vlc
.buf
<<= size
;
709 vl_vlc_dumpbits(&bs
->vlc
, 2);
713 tab
= DC_long
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 0x3e0);
715 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
+ 1);
716 vl_vlc_needbits(&bs
->vlc
);
717 dc_diff
= vl_vlc_ubits(&bs
->vlc
, size
) - UBITS (SBITS (~bs
->vlc
.buf
, 1), size
);
718 vl_vlc_dumpbits(&bs
->vlc
, size
);
724 get_intra_block_B14(struct vl_mpg12_bs
*bs
, const int quant_matrix
[64], int quantizer_scale
, short *dest
)
733 vl_vlc_needbits(&bs
->vlc
);
736 if (bs
->vlc
.buf
>= 0x28000000) {
738 tab
= DCT_B14AC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
742 break; /* end of block */
745 bs
->vlc
.buf
<<= tab
->len
;
746 bs
->vlc
.bits
+= tab
->len
+ 1;
747 val
= (tab
->level
* quantizer_scale
* quant_matrix
[i
]) >> 4;
749 /* if (bitstream_get (1)) val = -val; */
750 val
= (val
^ vl_vlc_sbits(&bs
->vlc
, 1)) - vl_vlc_sbits(&bs
->vlc
, 1);
757 vl_vlc_needbits(&bs
->vlc
);
761 } else if (bs
->vlc
.buf
>= 0x04000000) {
763 tab
= DCT_B14_8
+ (vl_vlc_ubits(&bs
->vlc
, 8) - 4);
771 i
+= UBITS(bs
->vlc
.buf
<< 6, 6) - 64;
773 break; /* illegal, check needed to avoid buffer overflow */
775 vl_vlc_dumpbits(&bs
->vlc
, 12);
776 vl_vlc_needbits(&bs
->vlc
);
777 val
= (vl_vlc_sbits(&bs
->vlc
, 12) * quantizer_scale
* quant_matrix
[i
]) / 16;
783 vl_vlc_dumpbits(&bs
->vlc
, 12);
784 vl_vlc_needbits(&bs
->vlc
);
788 } else if (bs
->vlc
.buf
>= 0x02000000) {
789 tab
= DCT_B14_10
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 8);
793 } else if (bs
->vlc
.buf
>= 0x00800000) {
794 tab
= DCT_13
+ (vl_vlc_ubits(&bs
->vlc
, 13) - 16);
798 } else if (bs
->vlc
.buf
>= 0x00200000) {
799 tab
= DCT_15
+ (vl_vlc_ubits(&bs
->vlc
, 15) - 16);
804 tab
= DCT_16
+ vl_vlc_ubits(&bs
->vlc
, 16);
806 vl_vlc_getword(&bs
->vlc
, bs
->vlc
.bits
+ 16);
811 break; /* illegal, check needed to avoid buffer overflow */
814 dest
[63] ^= mismatch
& 1;
815 vl_vlc_dumpbits(&bs
->vlc
, 2); /* dump end of block code */
819 get_intra_block_B15(struct vl_mpg12_bs
*bs
, const int quant_matrix
[64], int quantizer_scale
, short *dest
)
828 vl_vlc_needbits(&bs
->vlc
);
831 if (bs
->vlc
.buf
>= 0x04000000) {
833 tab
= DCT_B15_8
+ (vl_vlc_ubits(&bs
->vlc
, 8) - 4);
839 bs
->vlc
.buf
<<= tab
->len
;
840 bs
->vlc
.bits
+= tab
->len
+ 1;
841 val
= (tab
->level
* quantizer_scale
* quant_matrix
[i
]) >> 4;
843 /* if (bitstream_get (1)) val = -val; */
844 val
= (val
^ vl_vlc_sbits(&bs
->vlc
, 1)) - vl_vlc_sbits(&bs
->vlc
, 1);
851 vl_vlc_needbits(&bs
->vlc
);
857 /* end of block. I commented out this code because if we */
858 /* dont exit here we will still exit at the later test :) */
860 /* if (i >= 128) break; */ /* end of block */
864 i
+= UBITS(bs
->vlc
.buf
<< 6, 6) - 64;
866 break; /* illegal, check against buffer overflow */
868 vl_vlc_dumpbits(&bs
->vlc
, 12);
869 vl_vlc_needbits(&bs
->vlc
);
870 val
= (vl_vlc_sbits(&bs
->vlc
, 12) * quantizer_scale
* quant_matrix
[i
]) / 16;
876 vl_vlc_dumpbits(&bs
->vlc
, 12);
877 vl_vlc_needbits(&bs
->vlc
);
882 } else if (bs
->vlc
.buf
>= 0x02000000) {
883 tab
= DCT_B15_10
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 8);
887 } else if (bs
->vlc
.buf
>= 0x00800000) {
888 tab
= DCT_13
+ (vl_vlc_ubits(&bs
->vlc
, 13) - 16);
892 } else if (bs
->vlc
.buf
>= 0x00200000) {
893 tab
= DCT_15
+ (vl_vlc_ubits(&bs
->vlc
, 15) - 16);
898 tab
= DCT_16
+ vl_vlc_ubits(&bs
->vlc
, 16);
900 vl_vlc_getword(&bs
->vlc
, bs
->vlc
.bits
+ 16);
905 break; /* illegal, check needed to avoid buffer overflow */
908 dest
[63] ^= mismatch
& 1;
909 vl_vlc_dumpbits(&bs
->vlc
, 4); /* dump end of block code */
913 get_non_intra_block(struct vl_mpg12_bs
*bs
, const int quant_matrix
[64], int quantizer_scale
, short *dest
)
922 vl_vlc_needbits(&bs
->vlc
);
923 if (bs
->vlc
.buf
>= 0x28000000) {
924 tab
= DCT_B14DC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
930 if (bs
->vlc
.buf
>= 0x28000000) {
932 tab
= DCT_B14AC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
937 break; /* end of block */
940 bs
->vlc
.buf
<<= tab
->len
;
941 bs
->vlc
.bits
+= tab
->len
+ 1;
942 val
= ((2*tab
->level
+1) * quantizer_scale
* quant_matrix
[i
]) >> 5;
944 /* if (bitstream_get (1)) val = -val; */
945 val
= (val
^ vl_vlc_sbits(&bs
->vlc
, 1)) - vl_vlc_sbits(&bs
->vlc
, 1);
952 vl_vlc_needbits(&bs
->vlc
);
959 if (bs
->vlc
.buf
>= 0x04000000) {
961 tab
= DCT_B14_8
+ (vl_vlc_ubits(&bs
->vlc
, 8) - 4);
969 i
+= UBITS(bs
->vlc
.buf
<< 6, 6) - 64;
971 break; /* illegal, check needed to avoid buffer overflow */
973 vl_vlc_dumpbits(&bs
->vlc
, 12);
974 vl_vlc_needbits(&bs
->vlc
);
975 val
= 2 * (vl_vlc_sbits(&bs
->vlc
, 12) + vl_vlc_sbits(&bs
->vlc
, 1)) + 1;
976 val
= (val
* quantizer_scale
* quant_matrix
[i
]) / 32;
982 vl_vlc_dumpbits(&bs
->vlc
, 12);
983 vl_vlc_needbits(&bs
->vlc
);
987 } else if (bs
->vlc
.buf
>= 0x02000000) {
988 tab
= DCT_B14_10
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 8);
992 } else if (bs
->vlc
.buf
>= 0x00800000) {
993 tab
= DCT_13
+ (vl_vlc_ubits(&bs
->vlc
, 13) - 16);
997 } else if (bs
->vlc
.buf
>= 0x00200000) {
998 tab
= DCT_15
+ (vl_vlc_ubits(&bs
->vlc
, 15) - 16);
1003 tab
= DCT_16
+ vl_vlc_ubits(&bs
->vlc
, 16);
1005 vl_vlc_getword(&bs
->vlc
, bs
->vlc
.bits
+ 16);
1010 break; /* illegal, check needed to avoid buffer overflow */
1012 dest
[63] ^= mismatch
& 1;
1013 vl_vlc_dumpbits(&bs
->vlc
, 2); /* dump end of block code */
1017 get_mpeg1_intra_block(struct vl_mpg12_bs
*bs
, const int quant_matrix
[64], int quantizer_scale
, short *dest
)
1024 vl_vlc_needbits(&bs
->vlc
);
1027 if (bs
->vlc
.buf
>= 0x28000000) {
1029 tab
= DCT_B14AC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
1033 break; /* end of block */
1036 bs
->vlc
.buf
<<= tab
->len
;
1037 bs
->vlc
.bits
+= tab
->len
+ 1;
1038 val
= (tab
->level
* quantizer_scale
* quant_matrix
[i
]) >> 4;
1041 val
= (val
- 1) | 1;
1043 /* if (bitstream_get (1)) val = -val; */
1044 val
= (val
^ vl_vlc_sbits(&bs
->vlc
, 1)) - vl_vlc_sbits(&bs
->vlc
, 1);
1050 vl_vlc_needbits(&bs
->vlc
);
1054 } else if (bs
->vlc
.buf
>= 0x04000000) {
1056 tab
= DCT_B14_8
+ (vl_vlc_ubits(&bs
->vlc
, 8) - 4);
1064 i
+= UBITS(bs
->vlc
.buf
<< 6, 6) - 64;
1066 break; /* illegal, check needed to avoid buffer overflow */
1068 vl_vlc_dumpbits(&bs
->vlc
, 12);
1069 vl_vlc_needbits(&bs
->vlc
);
1070 val
= vl_vlc_sbits(&bs
->vlc
, 8);
1071 if (! (val
& 0x7f)) {
1072 vl_vlc_dumpbits(&bs
->vlc
, 8);
1073 val
= vl_vlc_ubits(&bs
->vlc
, 8) + 2 * val
;
1075 val
= (val
* quantizer_scale
* quant_matrix
[i
]) / 16;
1078 val
= (val
+ ~SBITS (val
, 1)) | 1;
1083 vl_vlc_dumpbits(&bs
->vlc
, 8);
1084 vl_vlc_needbits(&bs
->vlc
);
1088 } else if (bs
->vlc
.buf
>= 0x02000000) {
1089 tab
= DCT_B14_10
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 8);
1093 } else if (bs
->vlc
.buf
>= 0x00800000) {
1094 tab
= DCT_13
+ (vl_vlc_ubits(&bs
->vlc
, 13) - 16);
1098 } else if (bs
->vlc
.buf
>= 0x00200000) {
1099 tab
= DCT_15
+ (vl_vlc_ubits(&bs
->vlc
, 15) - 16);
1104 tab
= DCT_16
+ vl_vlc_ubits(&bs
->vlc
, 16);
1106 vl_vlc_getword(&bs
->vlc
, bs
->vlc
.bits
+ 16);
1111 break; /* illegal, check needed to avoid buffer overflow */
1113 vl_vlc_dumpbits(&bs
->vlc
, 2); /* dump end of block code */
1117 get_mpeg1_non_intra_block(struct vl_mpg12_bs
*bs
, const int quant_matrix
[64], int quantizer_scale
, short *dest
)
1124 vl_vlc_needbits(&bs
->vlc
);
1125 if (bs
->vlc
.buf
>= 0x28000000) {
1126 tab
= DCT_B14DC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
1132 if (bs
->vlc
.buf
>= 0x28000000) {
1134 tab
= DCT_B14AC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
1139 break; /* end of block */
1142 bs
->vlc
.buf
<<= tab
->len
;
1143 bs
->vlc
.bits
+= tab
->len
+ 1;
1144 val
= ((2*tab
->level
+1) * quantizer_scale
* quant_matrix
[i
]) >> 5;
1147 val
= (val
- 1) | 1;
1149 /* if (bitstream_get (1)) val = -val; */
1150 val
= (val
^ vl_vlc_sbits(&bs
->vlc
, 1)) - vl_vlc_sbits(&bs
->vlc
, 1);
1156 vl_vlc_needbits(&bs
->vlc
);
1163 if (bs
->vlc
.buf
>= 0x04000000) {
1165 tab
= DCT_B14_8
+ (vl_vlc_ubits(&bs
->vlc
, 8) - 4);
1173 i
+= UBITS(bs
->vlc
.buf
<< 6, 6) - 64;
1175 break; /* illegal, check needed to avoid buffer overflow */
1177 vl_vlc_dumpbits(&bs
->vlc
, 12);
1178 vl_vlc_needbits(&bs
->vlc
);
1179 val
= vl_vlc_sbits(&bs
->vlc
, 8);
1180 if (! (val
& 0x7f)) {
1181 vl_vlc_dumpbits(&bs
->vlc
, 8);
1182 val
= vl_vlc_ubits(&bs
->vlc
, 8) + 2 * val
;
1184 val
= 2 * (val
+ SBITS (val
, 1)) + 1;
1185 val
= (val
* quantizer_scale
* quant_matrix
[i
]) / 32;
1188 val
= (val
+ ~SBITS (val
, 1)) | 1;
1193 vl_vlc_dumpbits(&bs
->vlc
, 8);
1194 vl_vlc_needbits(&bs
->vlc
);
1198 } else if (bs
->vlc
.buf
>= 0x02000000) {
1199 tab
= DCT_B14_10
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 8);
1203 } else if (bs
->vlc
.buf
>= 0x00800000) {
1204 tab
= DCT_13
+ (vl_vlc_ubits(&bs
->vlc
, 13) - 16);
1208 } else if (bs
->vlc
.buf
>= 0x00200000) {
1209 tab
= DCT_15
+ (vl_vlc_ubits(&bs
->vlc
, 15) - 16);
1214 tab
= DCT_16
+ vl_vlc_ubits(&bs
->vlc
, 16);
1216 vl_vlc_getword(&bs
->vlc
, bs
->vlc
.bits
+ 16);
1221 break; /* illegal, check needed to avoid buffer overflow */
1223 vl_vlc_dumpbits(&bs
->vlc
, 2); /* dump end of block code */
1227 slice_intra_DCT(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
, const int quant_matrix
[64], int cc
,
1228 unsigned x
, unsigned y
, enum pipe_mpeg12_dct_type coding
, int quantizer_scale
, int dc_dct_pred
[3])
1232 bs
->ycbcr_stream
[cc
]->x
= x
;
1233 bs
->ycbcr_stream
[cc
]->y
= y
;
1234 bs
->ycbcr_stream
[cc
]->intra
= PIPE_MPEG12_DCT_INTRA
;
1235 bs
->ycbcr_stream
[cc
]->coding
= coding
;
1237 vl_vlc_needbits(&bs
->vlc
);
1239 /* Get the intra DC coefficient and inverse quantize it */
1241 dc_dct_pred
[0] += get_luma_dc_dct_diff(bs
);
1243 dc_dct_pred
[cc
] += get_chroma_dc_dct_diff(bs
);
1245 memset(dest
, 0, sizeof(int16_t) * 64);
1246 dest
[0] = dc_dct_pred
[cc
] << (3 - picture
->intra_dc_precision
);
1247 if (picture
->mpeg1
) {
1248 if (picture
->picture_coding_type
!= D_TYPE
)
1249 get_mpeg1_intra_block(bs
, quant_matrix
, quantizer_scale
, dest
);
1250 } else if (picture
->intra_vlc_format
)
1251 get_intra_block_B15(bs
, quant_matrix
, quantizer_scale
, dest
);
1253 get_intra_block_B14(bs
, quant_matrix
, quantizer_scale
, dest
);
1255 memcpy(bs
->ycbcr_buffer
[cc
], dest
, sizeof(int16_t) * 64);
1257 bs
->num_ycbcr_blocks
[cc
]++;
1258 bs
->ycbcr_stream
[cc
]++;
1259 bs
->ycbcr_buffer
[cc
] += 64;
1263 slice_non_intra_DCT(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
, const int quant_matrix
[64], int cc
,
1264 unsigned x
, unsigned y
, enum pipe_mpeg12_dct_type coding
, int quantizer_scale
)
1268 bs
->ycbcr_stream
[cc
]->x
= x
;
1269 bs
->ycbcr_stream
[cc
]->y
= y
;
1270 bs
->ycbcr_stream
[cc
]->intra
= PIPE_MPEG12_DCT_DELTA
;
1271 bs
->ycbcr_stream
[cc
]->coding
= coding
;
1273 memset(dest
, 0, sizeof(int16_t) * 64);
1275 get_mpeg1_non_intra_block(bs
, quant_matrix
, quantizer_scale
, dest
);
1277 get_non_intra_block(bs
, quant_matrix
, quantizer_scale
, dest
);
1279 memcpy(bs
->ycbcr_buffer
[cc
], dest
, sizeof(int16_t) * 64);
1281 bs
->num_ycbcr_blocks
[cc
]++;
1282 bs
->ycbcr_stream
[cc
]++;
1283 bs
->ycbcr_buffer
[cc
] += 64;
1287 motion_mp1(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1289 int motion_x
, motion_y
;
1291 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1293 vl_vlc_needbits(&bs
->vlc
);
1294 motion_x
= (mv
->top
.x
+ (get_motion_delta(bs
, f_code
[0]) << f_code
[1]));
1295 motion_x
= bound_motion_vector (motion_x
, f_code
[0] + f_code
[1]);
1296 mv
->top
.x
= mv
->bottom
.x
= motion_x
;
1298 vl_vlc_needbits(&bs
->vlc
);
1299 motion_y
= (mv
->top
.y
+ (get_motion_delta(bs
, f_code
[0]) << f_code
[1]));
1300 motion_y
= bound_motion_vector (motion_y
, f_code
[0] + f_code
[1]);
1301 mv
->top
.y
= mv
->bottom
.y
= motion_y
;
1305 motion_fr_frame(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1307 int motion_x
, motion_y
;
1309 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1311 vl_vlc_needbits(&bs
->vlc
);
1312 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1313 motion_x
= bound_motion_vector(motion_x
, f_code
[0]);
1314 mv
->top
.x
= mv
->bottom
.x
= motion_x
;
1316 vl_vlc_needbits(&bs
->vlc
);
1317 motion_y
= mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]);
1318 motion_y
= bound_motion_vector(motion_y
, f_code
[1]);
1319 mv
->top
.y
= mv
->bottom
.y
= motion_y
;
1323 motion_fr_field(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1325 int motion_x
, motion_y
;
1327 vl_vlc_needbits(&bs
->vlc
);
1328 mv
->top
.field_select
= vl_vlc_ubits(&bs
->vlc
, 1) ?
1329 PIPE_VIDEO_BOTTOM_FIELD
: PIPE_VIDEO_TOP_FIELD
;
1330 vl_vlc_dumpbits(&bs
->vlc
, 1);
1332 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1333 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1334 mv
->top
.x
= motion_x
;
1336 vl_vlc_needbits(&bs
->vlc
);
1337 motion_y
= (mv
->top
.y
>> 1) + get_motion_delta(bs
, f_code
[1]);
1338 /* motion_y = bound_motion_vector (motion_y, f_code[1]); */
1339 mv
->top
.y
= motion_y
<< 1;
1341 vl_vlc_needbits(&bs
->vlc
);
1342 mv
->bottom
.field_select
= vl_vlc_ubits(&bs
->vlc
, 1) ?
1343 PIPE_VIDEO_BOTTOM_FIELD
: PIPE_VIDEO_TOP_FIELD
;
1344 vl_vlc_dumpbits(&bs
->vlc
, 1);
1346 motion_x
= mv
->bottom
.x
+ get_motion_delta(bs
, f_code
[0]);
1347 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1348 mv
->bottom
.x
= motion_x
;
1350 vl_vlc_needbits(&bs
->vlc
);
1351 motion_y
= (mv
->bottom
.y
>> 1) + get_motion_delta(bs
, f_code
[1]);
1352 /* motion_y = bound_motion_vector (motion_y, f_code[1]); */
1353 mv
->bottom
.y
= motion_y
<< 1;
1357 motion_fr_dmv(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1359 int motion_x
, motion_y
;
1361 // TODO Implement dmv
1362 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1364 vl_vlc_needbits(&bs
->vlc
);
1365 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1366 motion_x
= bound_motion_vector(motion_x
, f_code
[0]);
1367 mv
->top
.x
= mv
->bottom
.x
= motion_x
;
1369 vl_vlc_needbits(&bs
->vlc
);
1370 motion_y
= (mv
->top
.y
>> 1) + get_motion_delta(bs
, f_code
[1]);
1371 /* motion_y = bound_motion_vector (motion_y, f_code[1]); */
1372 mv
->top
.y
= mv
->bottom
.y
= motion_y
<< 1;
1375 /* like motion_frame, but parsing without actual motion compensation */
1377 motion_fr_conceal(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1381 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1383 vl_vlc_needbits(&bs
->vlc
);
1384 tmp
= (mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]));
1385 tmp
= bound_motion_vector (tmp
, f_code
[0]);
1386 mv
->top
.x
= mv
->bottom
.x
= tmp
;
1388 vl_vlc_needbits(&bs
->vlc
);
1389 tmp
= (mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]));
1390 tmp
= bound_motion_vector (tmp
, f_code
[1]);
1391 mv
->top
.y
= mv
->bottom
.y
= tmp
;
1393 vl_vlc_dumpbits(&bs
->vlc
, 1); /* remove marker_bit */
1397 motion_fi_field(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1399 int motion_x
, motion_y
;
1401 vl_vlc_needbits(&bs
->vlc
);
1404 //vl_vlc_ubits(&bs->vlc, 1);
1406 // TODO field select may need to do something here for bob (weave ok)
1407 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1408 vl_vlc_dumpbits(&bs
->vlc
, 1);
1410 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1411 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1412 mv
->top
.x
= mv
->bottom
.x
= motion_x
;
1414 vl_vlc_needbits(&bs
->vlc
);
1415 motion_y
= mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]);
1416 motion_y
= bound_motion_vector (motion_y
, f_code
[1]);
1417 mv
->top
.y
= mv
->bottom
.y
= motion_y
;
1421 motion_fi_16x8(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1423 int motion_x
, motion_y
;
1425 vl_vlc_needbits(&bs
->vlc
);
1428 //vl_vlc_ubits(&bs->vlc, 1);
1430 // TODO field select may need to do something here bob (weave ok)
1431 mv
->top
.field_select
= PIPE_VIDEO_FRAME
;
1432 vl_vlc_dumpbits(&bs
->vlc
, 1);
1434 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1435 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1436 mv
->top
.x
= motion_x
;
1438 vl_vlc_needbits(&bs
->vlc
);
1439 motion_y
= mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]);
1440 motion_y
= bound_motion_vector (motion_y
, f_code
[1]);
1441 mv
->top
.y
= motion_y
;
1443 vl_vlc_needbits(&bs
->vlc
);
1445 //vl_vlc_ubits(&bs->vlc, 1);
1447 // TODO field select may need to do something here for bob (weave ok)
1448 mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1449 vl_vlc_dumpbits(&bs
->vlc
, 1);
1451 motion_x
= mv
->bottom
.x
+ get_motion_delta(bs
, f_code
[0]);
1452 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1453 mv
->bottom
.x
= motion_x
;
1455 vl_vlc_needbits(&bs
->vlc
);
1456 motion_y
= mv
->bottom
.y
+ get_motion_delta(bs
, f_code
[1]);
1457 motion_y
= bound_motion_vector (motion_y
, f_code
[1]);
1458 mv
->bottom
.y
= motion_y
;
1462 motion_fi_dmv(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1464 int motion_x
, motion_y
;
1466 // TODO field select may need to do something here for bob (weave ok)
1467 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1469 vl_vlc_needbits(&bs
->vlc
);
1470 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1471 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1472 mv
->top
.x
= mv
->bottom
.x
= motion_x
;
1474 vl_vlc_needbits(&bs
->vlc
);
1475 motion_y
= mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]);
1476 motion_y
= bound_motion_vector (motion_y
, f_code
[1]);
1477 mv
->top
.y
= mv
->bottom
.y
= motion_y
;
1482 motion_fi_conceal(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct pipe_motionvector
*mv
)
1486 vl_vlc_needbits(&bs
->vlc
);
1487 vl_vlc_dumpbits(&bs
->vlc
, 1); /* remove field_select */
1489 tmp
= (mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]));
1490 tmp
= bound_motion_vector(tmp
, f_code
[0]);
1491 mv
->top
.x
= mv
->bottom
.x
= tmp
;
1493 vl_vlc_needbits(&bs
->vlc
);
1494 tmp
= (mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]));
1495 tmp
= bound_motion_vector(tmp
, f_code
[1]);
1496 mv
->top
.y
= mv
->bottom
.y
= tmp
;
1498 vl_vlc_dumpbits(&bs
->vlc
, 1); /* remove marker_bit */
1501 #define MOTION_CALL(routine, macroblock_modes) \
1503 if ((macroblock_modes) & MACROBLOCK_MOTION_FORWARD) \
1504 routine(bs, picture->f_code[0], &mv_fwd); \
1505 if ((macroblock_modes) & MACROBLOCK_MOTION_BACKWARD) \
1506 routine(bs, picture->f_code[1], &mv_bwd); \
1509 #define NEXT_MACROBLOCK \
1512 if (x == bs->width) { \
1514 if (y >= bs->height) \
1521 store_motionvectors(struct vl_mpg12_bs
*bs
, int x
, int y
,
1522 struct pipe_motionvector
*mv_fwd
,
1523 struct pipe_motionvector
*mv_bwd
)
1525 bs
->mv_stream
[0][x
+y
*bs
->width
].top
= mv_fwd
->top
;
1526 bs
->mv_stream
[0][x
+y
*bs
->width
].bottom
=
1527 mv_fwd
->top
.field_select
== PIPE_VIDEO_FRAME
?
1528 mv_fwd
->top
: mv_fwd
->bottom
;
1530 bs
->mv_stream
[1][x
+y
*bs
->width
].top
= mv_bwd
->top
;
1531 bs
->mv_stream
[1][x
+y
*bs
->width
].bottom
=
1532 mv_bwd
->top
.field_select
== PIPE_VIDEO_FRAME
?
1533 mv_bwd
->top
: mv_bwd
->bottom
;
1537 slice_init(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
,
1538 int *quantizer_scale
, int *x
, int *y
)
1542 vl_vlc_need32bits(&bs
->vlc
);
1543 while(bs
->vlc
.buf
< 0x101 || bs
->vlc
.buf
> 0x1AF) {
1544 if(!vl_vlc_getbyte(&bs
->vlc
))
1547 *y
= (bs
->vlc
.buf
& 0xFF) - 1;
1548 vl_vlc_restart(&bs
->vlc
);
1550 *quantizer_scale
= get_quantizer_scale(bs
, picture
);
1552 /* ignore intra_slice and all the extra data */
1553 while (bs
->vlc
.buf
& 0x80000000) {
1554 vl_vlc_dumpbits(&bs
->vlc
, 9);
1555 vl_vlc_needbits(&bs
->vlc
);
1558 /* decode initial macroblock address increment */
1561 if (bs
->vlc
.buf
>= 0x08000000) {
1562 mba
= MBA_5
+ (vl_vlc_ubits(&bs
->vlc
, 6) - 2);
1564 } else if (bs
->vlc
.buf
>= 0x01800000) {
1565 mba
= MBA_11
+ (vl_vlc_ubits(&bs
->vlc
, 12) - 24);
1567 } else switch (vl_vlc_ubits(&bs
->vlc
, 12)) {
1568 case 8: /* macroblock_escape */
1570 vl_vlc_dumpbits(&bs
->vlc
, 11);
1571 vl_vlc_needbits(&bs
->vlc
);
1573 case 15: /* macroblock_stuffing (MPEG1 only) */
1574 bs
->vlc
.buf
&= 0xfffff;
1575 vl_vlc_dumpbits(&bs
->vlc
, 11);
1576 vl_vlc_needbits(&bs
->vlc
);
1578 default: /* error */
1582 vl_vlc_dumpbits(&bs
->vlc
, mba
->len
+ 1);
1585 while (*x
>= bs
->width
) {
1589 if (*y
> bs
->height
)
1596 decode_slice(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
*picture
,
1597 const int intra_quantizer_matrix
[64], const int non_intra_quantizer_matrix
[64])
1599 enum pipe_video_field_select default_field_select
;
1600 struct pipe_motionvector mv_fwd
, mv_bwd
;
1601 enum pipe_mpeg12_dct_type dct_type
;
1603 /* predictor for DC coefficients in intra blocks */
1604 int dc_dct_pred
[3] = { 0, 0, 0 };
1605 int quantizer_scale
;
1609 switch(picture
->picture_structure
) {
1611 default_field_select
= PIPE_VIDEO_TOP_FIELD
;
1615 default_field_select
= PIPE_VIDEO_BOTTOM_FIELD
;
1619 default_field_select
= PIPE_VIDEO_FRAME
;
1623 if (!slice_init(bs
, picture
, &quantizer_scale
, &x
, &y
))
1626 mv_fwd
.top
.x
= mv_fwd
.top
.y
= mv_fwd
.bottom
.x
= mv_fwd
.bottom
.y
= 0;
1627 mv_fwd
.top
.field_select
= mv_fwd
.bottom
.field_select
= default_field_select
;
1629 mv_bwd
.top
.x
= mv_bwd
.top
.y
= mv_bwd
.bottom
.x
= mv_bwd
.bottom
.y
= 0;
1630 mv_bwd
.top
.field_select
= mv_bwd
.bottom
.field_select
= default_field_select
;
1633 int macroblock_modes
;
1637 vl_vlc_needbits(&bs
->vlc
);
1639 macroblock_modes
= get_macroblock_modes(bs
, picture
);
1640 dct_type
= get_dct_type(bs
, picture
, macroblock_modes
);
1642 switch(macroblock_modes
& (MACROBLOCK_MOTION_FORWARD
|MACROBLOCK_MOTION_BACKWARD
)) {
1643 case (MACROBLOCK_MOTION_FORWARD
|MACROBLOCK_MOTION_BACKWARD
):
1644 mv_fwd
.top
.weight
= mv_fwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_HALF
;
1645 mv_bwd
.top
.weight
= mv_bwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_HALF
;
1649 mv_fwd
.top
.field_select
= mv_fwd
.bottom
.field_select
= default_field_select
;
1650 mv_bwd
.top
.field_select
= mv_bwd
.bottom
.field_select
= default_field_select
;
1653 case MACROBLOCK_MOTION_FORWARD
:
1654 mv_fwd
.top
.weight
= mv_fwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MAX
;
1655 mv_bwd
.top
.weight
= mv_bwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1658 case MACROBLOCK_MOTION_BACKWARD
:
1659 mv_fwd
.top
.weight
= mv_fwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1660 mv_bwd
.top
.weight
= mv_bwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MAX
;
1664 /* maybe integrate MACROBLOCK_QUANT test into get_macroblock_modes ? */
1665 if (macroblock_modes
& MACROBLOCK_QUANT
)
1666 quantizer_scale
= get_quantizer_scale(bs
, picture
);
1668 if (macroblock_modes
& MACROBLOCK_INTRA
) {
1670 if (picture
->concealment_motion_vectors
) {
1671 if (picture
->picture_structure
== FRAME_PICTURE
)
1672 motion_fr_conceal(bs
, picture
->f_code
[0], &mv_fwd
);
1674 motion_fi_conceal(bs
, picture
->f_code
[0], &mv_fwd
);
1677 mv_fwd
.top
.x
= mv_fwd
.top
.y
= mv_fwd
.bottom
.x
= mv_fwd
.bottom
.y
= 0;
1678 mv_bwd
.top
.x
= mv_bwd
.top
.y
= mv_bwd
.bottom
.x
= mv_bwd
.bottom
.y
= 0;
1680 mv_fwd
.top
.weight
= mv_fwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1681 mv_bwd
.top
.weight
= mv_bwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1683 // unravaled loop of 6 block(i) calls in macroblock()
1684 slice_intra_DCT(bs
, picture
, intra_quantizer_matrix
, 0, x
*2+0, y
*2+0, dct_type
, quantizer_scale
, dc_dct_pred
);
1685 slice_intra_DCT(bs
, picture
, intra_quantizer_matrix
, 0, x
*2+1, y
*2+0, dct_type
, quantizer_scale
, dc_dct_pred
);
1686 slice_intra_DCT(bs
, picture
, intra_quantizer_matrix
, 0, x
*2+0, y
*2+1, dct_type
, quantizer_scale
, dc_dct_pred
);
1687 slice_intra_DCT(bs
, picture
, intra_quantizer_matrix
, 0, x
*2+1, y
*2+1, dct_type
, quantizer_scale
, dc_dct_pred
);
1688 slice_intra_DCT(bs
, picture
, intra_quantizer_matrix
, 1, x
, y
, PIPE_MPEG12_DCT_TYPE_FRAME
, quantizer_scale
, dc_dct_pred
);
1689 slice_intra_DCT(bs
, picture
, intra_quantizer_matrix
, 2, x
, y
, PIPE_MPEG12_DCT_TYPE_FRAME
, quantizer_scale
, dc_dct_pred
);
1691 if (picture
->picture_coding_type
== D_TYPE
) {
1692 vl_vlc_needbits(&bs
->vlc
);
1693 vl_vlc_dumpbits(&bs
->vlc
, 1);
1697 if (picture
->picture_structure
== FRAME_PICTURE
)
1698 switch (macroblock_modes
& MOTION_TYPE_MASK
) {
1700 if (picture
->mpeg1
) {
1701 MOTION_CALL(motion_mp1
, macroblock_modes
);
1703 MOTION_CALL(motion_fr_frame
, macroblock_modes
);
1708 MOTION_CALL (motion_fr_field
, macroblock_modes
);
1712 MOTION_CALL (motion_fr_dmv
, MACROBLOCK_MOTION_FORWARD
);
1716 /* non-intra mb without forward mv in a P picture */
1717 mv_fwd
.top
.x
= mv_fwd
.top
.y
= mv_fwd
.bottom
.x
= mv_fwd
.bottom
.y
= 0;
1718 mv_bwd
.top
.x
= mv_bwd
.top
.y
= mv_bwd
.bottom
.x
= mv_bwd
.bottom
.y
= 0;
1722 switch (macroblock_modes
& MOTION_TYPE_MASK
) {
1724 MOTION_CALL (motion_fi_field
, macroblock_modes
);
1728 MOTION_CALL (motion_fi_16x8
, macroblock_modes
);
1732 MOTION_CALL (motion_fi_dmv
, MACROBLOCK_MOTION_FORWARD
);
1736 /* non-intra mb without forward mv in a P picture */
1737 mv_fwd
.top
.x
= mv_fwd
.top
.y
= mv_fwd
.bottom
.x
= mv_fwd
.bottom
.y
= 0;
1738 mv_bwd
.top
.x
= mv_bwd
.top
.y
= mv_bwd
.bottom
.x
= mv_bwd
.bottom
.y
= 0;
1742 if (macroblock_modes
& MACROBLOCK_PATTERN
) {
1743 int coded_block_pattern
= get_coded_block_pattern(bs
);
1745 // TODO optimize not fully used for idct accel only mc.
1746 if (coded_block_pattern
& 0x20)
1747 slice_non_intra_DCT(bs
, picture
, non_intra_quantizer_matrix
, 0, x
*2+0, y
*2+0, dct_type
, quantizer_scale
); // cc0 luma 0
1748 if (coded_block_pattern
& 0x10)
1749 slice_non_intra_DCT(bs
, picture
, non_intra_quantizer_matrix
, 0, x
*2+1, y
*2+0, dct_type
, quantizer_scale
); // cc0 luma 1
1750 if (coded_block_pattern
& 0x08)
1751 slice_non_intra_DCT(bs
, picture
, non_intra_quantizer_matrix
, 0, x
*2+0, y
*2+1, dct_type
, quantizer_scale
); // cc0 luma 2
1752 if (coded_block_pattern
& 0x04)
1753 slice_non_intra_DCT(bs
, picture
, non_intra_quantizer_matrix
, 0, x
*2+1, y
*2+1, dct_type
, quantizer_scale
); // cc0 luma 3
1754 if (coded_block_pattern
& 0x2)
1755 slice_non_intra_DCT(bs
, picture
, non_intra_quantizer_matrix
, 1, x
, y
, PIPE_MPEG12_DCT_TYPE_FRAME
, quantizer_scale
); // cc1 croma
1756 if (coded_block_pattern
& 0x1)
1757 slice_non_intra_DCT(bs
, picture
, non_intra_quantizer_matrix
, 2, x
, y
, PIPE_MPEG12_DCT_TYPE_FRAME
, quantizer_scale
); // cc2 croma
1760 dc_dct_pred
[0] = dc_dct_pred
[1] = dc_dct_pred
[2] = 0;
1763 store_motionvectors(bs
, x
, y
, &mv_fwd
, &mv_bwd
);
1766 vl_vlc_needbits(&bs
->vlc
);
1769 if (bs
->vlc
.buf
>= 0x10000000) {
1770 mba
= MBA_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 2);
1772 } else if (bs
->vlc
.buf
>= 0x03000000) {
1773 mba
= MBA_11
+ (vl_vlc_ubits(&bs
->vlc
, 11) - 24);
1775 } else switch (vl_vlc_ubits(&bs
->vlc
, 11)) {
1776 case 8: /* macroblock_escape */
1779 case 15: /* macroblock_stuffing (MPEG1 only) */
1780 vl_vlc_dumpbits(&bs
->vlc
, 11);
1781 vl_vlc_needbits(&bs
->vlc
);
1783 default: /* end of slice, or error */
1787 vl_vlc_dumpbits(&bs
->vlc
, mba
->len
);
1788 mba_inc
+= mba
->mba
;
1790 //TODO conversion to signed format signed format
1791 dc_dct_pred
[0] = dc_dct_pred
[1] = dc_dct_pred
[2] = 0;
1793 mv_fwd
.top
.field_select
= mv_fwd
.bottom
.field_select
= default_field_select
;
1794 mv_bwd
.top
.field_select
= mv_bwd
.bottom
.field_select
= default_field_select
;
1796 if (picture
->picture_coding_type
== P_TYPE
) {
1797 mv_fwd
.top
.x
= mv_fwd
.top
.y
= mv_fwd
.bottom
.x
= mv_fwd
.bottom
.y
= 0;
1798 mv_fwd
.top
.weight
= mv_fwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MAX
;
1802 store_motionvectors(bs
, x
, y
, &mv_fwd
, &mv_bwd
);
1804 } while (--mba_inc
);
1810 vl_mpg12_bs_init(struct vl_mpg12_bs
*bs
, unsigned width
, unsigned height
)
1814 memset(bs
, 0, sizeof(struct vl_mpg12_bs
));
1817 bs
->height
= height
;
1821 vl_mpg12_bs_set_buffers(struct vl_mpg12_bs
*bs
, struct pipe_ycbcr_block
*ycbcr_stream
[VL_MAX_PLANES
],
1822 short *ycbcr_buffer
[VL_MAX_PLANES
], struct pipe_motionvector
*mv_stream
[VL_MAX_REF_FRAMES
])
1827 assert(ycbcr_stream
&& ycbcr_buffer
);
1830 for (i
= 0; i
< VL_MAX_PLANES
; ++i
) {
1831 bs
->ycbcr_stream
[i
] = ycbcr_stream
[i
];
1832 bs
->ycbcr_buffer
[i
] = ycbcr_buffer
[i
];
1834 for (i
= 0; i
< VL_MAX_REF_FRAMES
; ++i
)
1835 bs
->mv_stream
[i
] = mv_stream
[i
];
1838 for (i
= 0; i
< bs
->width
*bs
->height
; ++i
) {
1839 bs
->mv_stream
[0][i
].top
.x
= bs
->mv_stream
[0][i
].top
.y
= 0;
1840 bs
->mv_stream
[0][i
].top
.field_select
= PIPE_VIDEO_FRAME
;
1841 bs
->mv_stream
[0][i
].top
.weight
= PIPE_VIDEO_MV_WEIGHT_MAX
;
1842 bs
->mv_stream
[0][i
].bottom
.x
= bs
->mv_stream
[0][i
].bottom
.y
= 0;
1843 bs
->mv_stream
[0][i
].bottom
.field_select
= PIPE_VIDEO_FRAME
;
1844 bs
->mv_stream
[0][i
].bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MAX
;
1846 bs
->mv_stream
[1][i
].top
.x
= bs
->mv_stream
[1][i
].top
.y
= 0;
1847 bs
->mv_stream
[1][i
].top
.field_select
= PIPE_VIDEO_FRAME
;
1848 bs
->mv_stream
[1][i
].top
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1849 bs
->mv_stream
[1][i
].bottom
.x
= bs
->mv_stream
[1][i
].bottom
.y
= 0;
1850 bs
->mv_stream
[1][i
].bottom
.field_select
= PIPE_VIDEO_FRAME
;
1851 bs
->mv_stream
[1][i
].bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1856 vl_mpg12_bs_decode(struct vl_mpg12_bs
*bs
, unsigned num_bytes
, const void *buffer
,
1857 struct pipe_mpeg12_picture_desc
*picture
, unsigned num_ycbcr_blocks
[3])
1859 int intra_quantizer_matrix
[64];
1860 int non_intra_quantizer_matrix
[64];
1866 assert(num_ycbcr_blocks
);
1867 assert(buffer
&& num_bytes
);
1869 bs
->num_ycbcr_blocks
= num_ycbcr_blocks
;
1871 vl_vlc_init(&bs
->vlc
, buffer
, num_bytes
);
1873 scan
= picture
->alternate_scan
? vl_zscan_alternate
: vl_zscan_normal
;
1874 for (i
= 0; i
< 64; ++i
) {
1875 intra_quantizer_matrix
[i
] = picture
->intra_quantizer_matrix
[scan
[i
]];
1876 non_intra_quantizer_matrix
[i
] = picture
->non_intra_quantizer_matrix
[scan
[i
]];
1879 while(decode_slice(bs
, picture
, intra_quantizer_matrix
, non_intra_quantizer_matrix
));