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_compiler.h>
56 #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 /* macroblock modes */
68 #define MACROBLOCK_INTRA 1
69 #define MACROBLOCK_PATTERN 2
70 #define MACROBLOCK_MOTION_BACKWARD 4
71 #define MACROBLOCK_MOTION_FORWARD 8
72 #define MACROBLOCK_QUANT 16
75 #define MOTION_TYPE_MASK (3*64)
76 #define MOTION_TYPE_BASE 64
77 #define MC_FIELD (1*64)
78 #define MC_FRAME (2*64)
79 #define MC_16X8 (2*64)
82 /* picture structure */
84 #define BOTTOM_FIELD 2
85 #define FRAME_PICTURE 3
87 /* picture coding type (mpeg2 header) */
129 #define INTRA MACROBLOCK_INTRA
130 #define QUANT MACROBLOCK_QUANT
131 #define MC MACROBLOCK_MOTION_FORWARD
132 #define CODED MACROBLOCK_PATTERN
133 #define FWD MACROBLOCK_MOTION_FORWARD
134 #define BWD MACROBLOCK_MOTION_BACKWARD
135 #define INTER MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD
137 static const MBtab MB_I
[] = {
138 {INTRA
|QUANT
, 2}, {INTRA
, 1}
141 static const MBtab MB_P
[] = {
142 {INTRA
|QUANT
, 6}, {CODED
|QUANT
, 5}, {MC
|CODED
|QUANT
, 5}, {INTRA
, 5},
143 {MC
, 3}, {MC
, 3}, {MC
, 3}, {MC
, 3},
144 {CODED
, 2}, {CODED
, 2}, {CODED
, 2}, {CODED
, 2},
145 {CODED
, 2}, {CODED
, 2}, {CODED
, 2}, {CODED
, 2},
146 {MC
|CODED
, 1}, {MC
|CODED
, 1}, {MC
|CODED
, 1}, {MC
|CODED
, 1},
147 {MC
|CODED
, 1}, {MC
|CODED
, 1}, {MC
|CODED
, 1}, {MC
|CODED
, 1},
148 {MC
|CODED
, 1}, {MC
|CODED
, 1}, {MC
|CODED
, 1}, {MC
|CODED
, 1},
149 {MC
|CODED
, 1}, {MC
|CODED
, 1}, {MC
|CODED
, 1}, {MC
|CODED
, 1}
152 static const MBtab MB_B
[] = {
153 {0, 0}, {INTRA
|QUANT
, 6},
154 {BWD
|CODED
|QUANT
, 6}, {FWD
|CODED
|QUANT
, 6},
155 {INTER
|CODED
|QUANT
, 5}, {INTER
|CODED
|QUANT
, 5},
156 {INTRA
, 5}, {INTRA
, 5},
157 {FWD
, 4}, {FWD
, 4}, {FWD
, 4}, {FWD
, 4},
158 {FWD
|CODED
, 4}, {FWD
|CODED
, 4}, {FWD
|CODED
, 4}, {FWD
|CODED
, 4},
159 {BWD
, 3}, {BWD
, 3}, {BWD
, 3}, {BWD
, 3},
160 {BWD
, 3}, {BWD
, 3}, {BWD
, 3}, {BWD
, 3},
161 {BWD
|CODED
, 3}, {BWD
|CODED
, 3}, {BWD
|CODED
, 3}, {BWD
|CODED
, 3},
162 {BWD
|CODED
, 3}, {BWD
|CODED
, 3}, {BWD
|CODED
, 3}, {BWD
|CODED
, 3},
163 {INTER
, 2}, {INTER
, 2}, {INTER
, 2}, {INTER
, 2},
164 {INTER
, 2}, {INTER
, 2}, {INTER
, 2}, {INTER
, 2},
165 {INTER
, 2}, {INTER
, 2}, {INTER
, 2}, {INTER
, 2},
166 {INTER
, 2}, {INTER
, 2}, {INTER
, 2}, {INTER
, 2},
167 {INTER
|CODED
, 2}, {INTER
|CODED
, 2}, {INTER
|CODED
, 2}, {INTER
|CODED
, 2},
168 {INTER
|CODED
, 2}, {INTER
|CODED
, 2}, {INTER
|CODED
, 2}, {INTER
|CODED
, 2},
169 {INTER
|CODED
, 2}, {INTER
|CODED
, 2}, {INTER
|CODED
, 2}, {INTER
|CODED
, 2},
170 {INTER
|CODED
, 2}, {INTER
|CODED
, 2}, {INTER
|CODED
, 2}, {INTER
|CODED
, 2}
181 static const MVtab MV_4
[] = {
182 { 3, 6}, { 2, 4}, { 1, 3}, { 1, 3}, { 0, 2}, { 0, 2}, { 0, 2}, { 0, 2}
185 static const MVtab MV_10
[] = {
186 { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10},
187 { 0,10}, { 0,10}, { 0,10}, { 0,10}, {15,10}, {14,10}, {13,10}, {12,10},
188 {11,10}, {10,10}, { 9, 9}, { 9, 9}, { 8, 9}, { 8, 9}, { 7, 9}, { 7, 9},
189 { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7},
190 { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7},
191 { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}
194 static const DMVtab DMV_2
[] = {
195 { 0, 1}, { 0, 1}, { 1, 2}, {-1, 2}
198 static const CBPtab CBP_7
[] = {
199 {0x22, 7}, {0x12, 7}, {0x0a, 7}, {0x06, 7},
200 {0x21, 7}, {0x11, 7}, {0x09, 7}, {0x05, 7},
201 {0x3f, 6}, {0x3f, 6}, {0x03, 6}, {0x03, 6},
202 {0x24, 6}, {0x24, 6}, {0x18, 6}, {0x18, 6},
203 {0x3e, 5}, {0x3e, 5}, {0x3e, 5}, {0x3e, 5},
204 {0x02, 5}, {0x02, 5}, {0x02, 5}, {0x02, 5},
205 {0x3d, 5}, {0x3d, 5}, {0x3d, 5}, {0x3d, 5},
206 {0x01, 5}, {0x01, 5}, {0x01, 5}, {0x01, 5},
207 {0x38, 5}, {0x38, 5}, {0x38, 5}, {0x38, 5},
208 {0x34, 5}, {0x34, 5}, {0x34, 5}, {0x34, 5},
209 {0x2c, 5}, {0x2c, 5}, {0x2c, 5}, {0x2c, 5},
210 {0x1c, 5}, {0x1c, 5}, {0x1c, 5}, {0x1c, 5},
211 {0x28, 5}, {0x28, 5}, {0x28, 5}, {0x28, 5},
212 {0x14, 5}, {0x14, 5}, {0x14, 5}, {0x14, 5},
213 {0x30, 5}, {0x30, 5}, {0x30, 5}, {0x30, 5},
214 {0x0c, 5}, {0x0c, 5}, {0x0c, 5}, {0x0c, 5},
215 {0x20, 4}, {0x20, 4}, {0x20, 4}, {0x20, 4},
216 {0x20, 4}, {0x20, 4}, {0x20, 4}, {0x20, 4},
217 {0x10, 4}, {0x10, 4}, {0x10, 4}, {0x10, 4},
218 {0x10, 4}, {0x10, 4}, {0x10, 4}, {0x10, 4},
219 {0x08, 4}, {0x08, 4}, {0x08, 4}, {0x08, 4},
220 {0x08, 4}, {0x08, 4}, {0x08, 4}, {0x08, 4},
221 {0x04, 4}, {0x04, 4}, {0x04, 4}, {0x04, 4},
222 {0x04, 4}, {0x04, 4}, {0x04, 4}, {0x04, 4},
223 {0x3c, 3}, {0x3c, 3}, {0x3c, 3}, {0x3c, 3},
224 {0x3c, 3}, {0x3c, 3}, {0x3c, 3}, {0x3c, 3},
225 {0x3c, 3}, {0x3c, 3}, {0x3c, 3}, {0x3c, 3},
226 {0x3c, 3}, {0x3c, 3}, {0x3c, 3}, {0x3c, 3}
229 static const CBPtab CBP_9
[] = {
230 {0, 0}, {0x00, 9}, {0x27, 9}, {0x1b, 9},
231 {0x3b, 9}, {0x37, 9}, {0x2f, 9}, {0x1f, 9},
232 {0x3a, 8}, {0x3a, 8}, {0x36, 8}, {0x36, 8},
233 {0x2e, 8}, {0x2e, 8}, {0x1e, 8}, {0x1e, 8},
234 {0x39, 8}, {0x39, 8}, {0x35, 8}, {0x35, 8},
235 {0x2d, 8}, {0x2d, 8}, {0x1d, 8}, {0x1d, 8},
236 {0x26, 8}, {0x26, 8}, {0x1a, 8}, {0x1a, 8},
237 {0x25, 8}, {0x25, 8}, {0x19, 8}, {0x19, 8},
238 {0x2b, 8}, {0x2b, 8}, {0x17, 8}, {0x17, 8},
239 {0x33, 8}, {0x33, 8}, {0x0f, 8}, {0x0f, 8},
240 {0x2a, 8}, {0x2a, 8}, {0x16, 8}, {0x16, 8},
241 {0x32, 8}, {0x32, 8}, {0x0e, 8}, {0x0e, 8},
242 {0x29, 8}, {0x29, 8}, {0x15, 8}, {0x15, 8},
243 {0x31, 8}, {0x31, 8}, {0x0d, 8}, {0x0d, 8},
244 {0x23, 8}, {0x23, 8}, {0x13, 8}, {0x13, 8},
245 {0x0b, 8}, {0x0b, 8}, {0x07, 8}, {0x07, 8}
248 static const DCtab DC_lum_5
[] = {
249 {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
250 {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
251 {0, 3}, {0, 3}, {0, 3}, {0, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3},
252 {4, 3}, {4, 3}, {4, 3}, {4, 3}, {5, 4}, {5, 4}, {6, 5}
255 static const DCtab DC_chrom_5
[] = {
256 {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2},
257 {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
258 {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
259 {3, 3}, {3, 3}, {3, 3}, {3, 3}, {4, 4}, {4, 4}, {5, 5}
262 static const DCtab DC_long
[] = {
263 {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, { 6, 5}, { 6, 5},
264 {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, { 6, 5}, { 6, 5},
265 {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, { 7, 6}, { 7, 6},
266 {8, 7}, {8, 7}, {8, 7}, {8, 7}, {9, 8}, {9, 8}, {10, 9}, {11, 9}
269 static const DCTtab DCT_16
[] = {
270 {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0},
271 {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0},
272 {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0},
273 {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0},
274 { 2,18, 0}, { 2,17, 0}, { 2,16, 0}, { 2,15, 0},
275 { 7, 3, 0}, { 17, 2, 0}, { 16, 2, 0}, { 15, 2, 0},
276 { 14, 2, 0}, { 13, 2, 0}, { 12, 2, 0}, { 32, 1, 0},
277 { 31, 1, 0}, { 30, 1, 0}, { 29, 1, 0}, { 28, 1, 0}
280 static const DCTtab DCT_15
[] = {
281 { 1,40,15}, { 1,39,15}, { 1,38,15}, { 1,37,15},
282 { 1,36,15}, { 1,35,15}, { 1,34,15}, { 1,33,15},
283 { 1,32,15}, { 2,14,15}, { 2,13,15}, { 2,12,15},
284 { 2,11,15}, { 2,10,15}, { 2, 9,15}, { 2, 8,15},
285 { 1,31,14}, { 1,31,14}, { 1,30,14}, { 1,30,14},
286 { 1,29,14}, { 1,29,14}, { 1,28,14}, { 1,28,14},
287 { 1,27,14}, { 1,27,14}, { 1,26,14}, { 1,26,14},
288 { 1,25,14}, { 1,25,14}, { 1,24,14}, { 1,24,14},
289 { 1,23,14}, { 1,23,14}, { 1,22,14}, { 1,22,14},
290 { 1,21,14}, { 1,21,14}, { 1,20,14}, { 1,20,14},
291 { 1,19,14}, { 1,19,14}, { 1,18,14}, { 1,18,14},
292 { 1,17,14}, { 1,17,14}, { 1,16,14}, { 1,16,14}
295 static const DCTtab DCT_13
[] = {
296 { 11, 2,13}, { 10, 2,13}, { 6, 3,13}, { 4, 4,13},
297 { 3, 5,13}, { 2, 7,13}, { 2, 6,13}, { 1,15,13},
298 { 1,14,13}, { 1,13,13}, { 1,12,13}, { 27, 1,13},
299 { 26, 1,13}, { 25, 1,13}, { 24, 1,13}, { 23, 1,13},
300 { 1,11,12}, { 1,11,12}, { 9, 2,12}, { 9, 2,12},
301 { 5, 3,12}, { 5, 3,12}, { 1,10,12}, { 1,10,12},
302 { 3, 4,12}, { 3, 4,12}, { 8, 2,12}, { 8, 2,12},
303 { 22, 1,12}, { 22, 1,12}, { 21, 1,12}, { 21, 1,12},
304 { 1, 9,12}, { 1, 9,12}, { 20, 1,12}, { 20, 1,12},
305 { 19, 1,12}, { 19, 1,12}, { 2, 5,12}, { 2, 5,12},
306 { 4, 3,12}, { 4, 3,12}, { 1, 8,12}, { 1, 8,12},
307 { 7, 2,12}, { 7, 2,12}, { 18, 1,12}, { 18, 1,12}
310 static const DCTtab DCT_B14_10
[] = {
311 { 17, 1,10}, { 6, 2,10}, { 1, 7,10}, { 3, 3,10},
312 { 2, 4,10}, { 16, 1,10}, { 15, 1,10}, { 5, 2,10}
315 static const DCTtab DCT_B14_8
[] = {
316 { 65, 0, 6}, { 65, 0, 6}, { 65, 0, 6}, { 65, 0, 6},
317 { 3, 2, 7}, { 3, 2, 7}, { 10, 1, 7}, { 10, 1, 7},
318 { 1, 4, 7}, { 1, 4, 7}, { 9, 1, 7}, { 9, 1, 7},
319 { 8, 1, 6}, { 8, 1, 6}, { 8, 1, 6}, { 8, 1, 6},
320 { 7, 1, 6}, { 7, 1, 6}, { 7, 1, 6}, { 7, 1, 6},
321 { 2, 2, 6}, { 2, 2, 6}, { 2, 2, 6}, { 2, 2, 6},
322 { 6, 1, 6}, { 6, 1, 6}, { 6, 1, 6}, { 6, 1, 6},
323 { 14, 1, 8}, { 1, 6, 8}, { 13, 1, 8}, { 12, 1, 8},
324 { 4, 2, 8}, { 2, 3, 8}, { 1, 5, 8}, { 11, 1, 8}
327 static const DCTtab DCT_B14AC_5
[] = {
328 { 1, 3, 5}, { 5, 1, 5}, { 4, 1, 5},
329 { 1, 2, 4}, { 1, 2, 4}, { 3, 1, 4}, { 3, 1, 4},
330 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
331 {129, 0, 2}, {129, 0, 2}, {129, 0, 2}, {129, 0, 2},
332 {129, 0, 2}, {129, 0, 2}, {129, 0, 2}, {129, 0, 2},
333 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
334 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}
337 static const DCTtab DCT_B14DC_5
[] = {
338 { 1, 3, 5}, { 5, 1, 5}, { 4, 1, 5},
339 { 1, 2, 4}, { 1, 2, 4}, { 3, 1, 4}, { 3, 1, 4},
340 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
341 { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1},
342 { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1},
343 { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1},
344 { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}, { 1, 1, 1}
347 static const DCTtab DCT_B15_10
[] = {
348 { 6, 2, 9}, { 6, 2, 9}, { 15, 1, 9}, { 15, 1, 9},
349 { 3, 4,10}, { 17, 1,10}, { 16, 1, 9}, { 16, 1, 9}
352 static const DCTtab DCT_B15_8
[] = {
353 { 65, 0, 6}, { 65, 0, 6}, { 65, 0, 6}, { 65, 0, 6},
354 { 8, 1, 7}, { 8, 1, 7}, { 9, 1, 7}, { 9, 1, 7},
355 { 7, 1, 7}, { 7, 1, 7}, { 3, 2, 7}, { 3, 2, 7},
356 { 1, 7, 6}, { 1, 7, 6}, { 1, 7, 6}, { 1, 7, 6},
357 { 1, 6, 6}, { 1, 6, 6}, { 1, 6, 6}, { 1, 6, 6},
358 { 5, 1, 6}, { 5, 1, 6}, { 5, 1, 6}, { 5, 1, 6},
359 { 6, 1, 6}, { 6, 1, 6}, { 6, 1, 6}, { 6, 1, 6},
360 { 2, 5, 8}, { 12, 1, 8}, { 1,11, 8}, { 1,10, 8},
361 { 14, 1, 8}, { 13, 1, 8}, { 4, 2, 8}, { 2, 4, 8},
362 { 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5},
363 { 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5}, { 3, 1, 5},
364 { 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5},
365 { 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5}, { 2, 2, 5},
366 { 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5},
367 { 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5}, { 4, 1, 5},
368 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
369 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
370 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
371 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
372 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
373 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
374 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
375 { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3}, { 2, 1, 3},
376 {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4},
377 {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4},
378 {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4},
379 {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4},
380 { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4},
381 { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4},
382 { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4},
383 { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4}, { 1, 3, 4},
384 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
385 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
386 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
387 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
388 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
389 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
390 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
391 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
392 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
393 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
394 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
395 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
396 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
397 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
398 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
399 { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2}, { 1, 1, 2},
400 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
401 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
402 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
403 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
404 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
405 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
406 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
407 { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3}, { 1, 2, 3},
408 { 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5},
409 { 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5}, { 1, 4, 5},
410 { 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5},
411 { 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5}, { 1, 5, 5},
412 { 10, 1, 7}, { 10, 1, 7}, { 2, 3, 7}, { 2, 3, 7},
413 { 11, 1, 7}, { 11, 1, 7}, { 1, 8, 7}, { 1, 8, 7},
414 { 1, 9, 7}, { 1, 9, 7}, { 1,12, 8}, { 1,13, 8},
415 { 3, 3, 8}, { 5, 2, 8}, { 1,14, 8}, { 1,15, 8}
418 static const MBAtab MBA_5
[] = {
419 {6, 5}, {5, 5}, {4, 4}, {4, 4}, {3, 4}, {3, 4},
420 {2, 3}, {2, 3}, {2, 3}, {2, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3},
421 {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1},
422 {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}
425 static const MBAtab MBA_11
[] = {
426 {32, 11}, {31, 11}, {30, 11}, {29, 11},
427 {28, 11}, {27, 11}, {26, 11}, {25, 11},
428 {24, 11}, {23, 11}, {22, 11}, {21, 11},
429 {20, 10}, {20, 10}, {19, 10}, {19, 10},
430 {18, 10}, {18, 10}, {17, 10}, {17, 10},
431 {16, 10}, {16, 10}, {15, 10}, {15, 10},
432 {14, 8}, {14, 8}, {14, 8}, {14, 8},
433 {14, 8}, {14, 8}, {14, 8}, {14, 8},
434 {13, 8}, {13, 8}, {13, 8}, {13, 8},
435 {13, 8}, {13, 8}, {13, 8}, {13, 8},
436 {12, 8}, {12, 8}, {12, 8}, {12, 8},
437 {12, 8}, {12, 8}, {12, 8}, {12, 8},
438 {11, 8}, {11, 8}, {11, 8}, {11, 8},
439 {11, 8}, {11, 8}, {11, 8}, {11, 8},
440 {10, 8}, {10, 8}, {10, 8}, {10, 8},
441 {10, 8}, {10, 8}, {10, 8}, {10, 8},
442 { 9, 8}, { 9, 8}, { 9, 8}, { 9, 8},
443 { 9, 8}, { 9, 8}, { 9, 8}, { 9, 8},
444 { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7},
445 { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7},
446 { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7},
447 { 8, 7}, { 8, 7}, { 8, 7}, { 8, 7},
448 { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7},
449 { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7},
450 { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7},
451 { 7, 7}, { 7, 7}, { 7, 7}, { 7, 7}
454 static const int non_linear_quantizer_scale
[] = {
455 0, 1, 2, 3, 4, 5, 6, 7,
456 8, 10, 12, 14, 16, 18, 20, 22,
457 24, 28, 32, 36, 40, 44, 48, 52,
458 56, 64, 72, 80, 88, 96, 104, 112
462 get_macroblock_modes(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
)
464 int macroblock_modes
;
467 switch (picture
->picture_coding_type
) {
470 tab
= MB_I
+ vl_vlc_ubits(&bs
->vlc
, 1);
471 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
472 macroblock_modes
= tab
->modes
;
474 return macroblock_modes
;
478 tab
= MB_P
+ vl_vlc_ubits(&bs
->vlc
, 5);
479 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
480 macroblock_modes
= tab
->modes
;
482 if (picture
->picture_structure
!= FRAME_PICTURE
) {
483 if (macroblock_modes
& MACROBLOCK_MOTION_FORWARD
) {
484 macroblock_modes
|= vl_vlc_ubits(&bs
->vlc
, 2) * MOTION_TYPE_BASE
;
485 vl_vlc_dumpbits(&bs
->vlc
, 2);
487 return macroblock_modes
;
488 } else if (picture
->frame_pred_frame_dct
) {
489 if (macroblock_modes
& MACROBLOCK_MOTION_FORWARD
)
490 macroblock_modes
|= MC_FRAME
;
491 return macroblock_modes
;
493 if (macroblock_modes
& MACROBLOCK_MOTION_FORWARD
) {
494 macroblock_modes
|= vl_vlc_ubits(&bs
->vlc
, 2) * MOTION_TYPE_BASE
;
495 vl_vlc_dumpbits(&bs
->vlc
, 2);
497 return macroblock_modes
;
502 tab
= MB_B
+ vl_vlc_ubits(&bs
->vlc
, 6);
503 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
504 macroblock_modes
= tab
->modes
;
506 if (picture
->picture_structure
!= FRAME_PICTURE
) {
507 if (! (macroblock_modes
& MACROBLOCK_INTRA
)) {
508 macroblock_modes
|= vl_vlc_ubits(&bs
->vlc
, 2) * MOTION_TYPE_BASE
;
509 vl_vlc_dumpbits(&bs
->vlc
, 2);
511 } else if (picture
->frame_pred_frame_dct
) {
512 macroblock_modes
|= MC_FRAME
;
513 } else 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 return macroblock_modes
;
521 vl_vlc_dumpbits(&bs
->vlc
, 1);
522 return MACROBLOCK_INTRA
;
529 static INLINE
enum pipe_mpeg12_dct_type
530 get_dct_type(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
, int macroblock_modes
)
532 enum pipe_mpeg12_dct_type dct_type
= PIPE_MPEG12_DCT_TYPE_FRAME
;
534 if ((picture
->picture_structure
== FRAME_PICTURE
) &&
535 (!picture
->frame_pred_frame_dct
) &&
536 (macroblock_modes
& (MACROBLOCK_INTRA
| MACROBLOCK_PATTERN
))) {
538 dct_type
= vl_vlc_ubits(&bs
->vlc
, 1) ? PIPE_MPEG12_DCT_TYPE_FIELD
: PIPE_MPEG12_DCT_TYPE_FRAME
;
539 vl_vlc_dumpbits(&bs
->vlc
, 1);
545 get_quantizer_scale(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
)
547 int quantizer_scale_code
;
549 quantizer_scale_code
= vl_vlc_ubits(&bs
->vlc
, 5);
550 vl_vlc_dumpbits(&bs
->vlc
, 5);
552 if (picture
->q_scale_type
)
553 return non_linear_quantizer_scale
[quantizer_scale_code
];
555 return quantizer_scale_code
<< 1;
559 get_motion_delta(struct vl_mpg12_bs
*bs
, unsigned f_code
)
565 if (bs
->vlc
.buf
& 0x80000000) {
566 vl_vlc_dumpbits(&bs
->vlc
, 1);
568 } else if (bs
->vlc
.buf
>= 0x0c000000) {
570 tab
= MV_4
+ vl_vlc_ubits(&bs
->vlc
, 4);
571 delta
= (tab
->delta
<< f_code
) + 1;
572 bs
->vlc
.bits
+= tab
->len
+ f_code
+ 1;
573 bs
->vlc
.buf
<<= tab
->len
;
575 sign
= vl_vlc_sbits(&bs
->vlc
, 1);
579 delta
+= vl_vlc_ubits(&bs
->vlc
, f_code
);
580 bs
->vlc
.buf
<<= f_code
;
582 return (delta
^ sign
) - sign
;
586 tab
= MV_10
+ vl_vlc_ubits(&bs
->vlc
, 10);
587 delta
= (tab
->delta
<< f_code
) + 1;
588 bs
->vlc
.bits
+= tab
->len
+ 1;
589 bs
->vlc
.buf
<<= tab
->len
;
591 sign
= vl_vlc_sbits(&bs
->vlc
, 1);
595 vl_vlc_needbits(&bs
->vlc
);
596 delta
+= vl_vlc_ubits(&bs
->vlc
, f_code
);
597 vl_vlc_dumpbits(&bs
->vlc
, f_code
);
600 return (delta
^ sign
) - sign
;
605 bound_motion_vector(int vec
, unsigned f_code
)
611 limit
= 16 << f_code
;
613 if ((unsigned int)(vec
+ limit
) < 2 * limit
)
616 sign
= ((int32_t)vec
) >> 31;
617 return vec
- ((2 * limit
) ^ sign
) + sign
;
620 return ((int32_t)vec
<< (28 - f_code
)) >> (28 - f_code
);
625 get_dmv(struct vl_mpg12_bs
*bs
)
629 tab
= DMV_2
+ vl_vlc_ubits(&bs
->vlc
, 2);
630 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
635 get_coded_block_pattern(struct vl_mpg12_bs
*bs
)
639 vl_vlc_needbits(&bs
->vlc
);
641 if (bs
->vlc
.buf
>= 0x20000000) {
643 tab
= CBP_7
+ (vl_vlc_ubits(&bs
->vlc
, 7) - 16);
644 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
649 tab
= CBP_9
+ vl_vlc_ubits(&bs
->vlc
, 9);
650 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
656 get_luma_dc_dct_diff(struct vl_mpg12_bs
*bs
)
662 if (bs
->vlc
.buf
< 0xf8000000) {
663 tab
= DC_lum_5
+ vl_vlc_ubits(&bs
->vlc
, 5);
666 bs
->vlc
.bits
+= tab
->len
+ size
;
667 bs
->vlc
.buf
<<= tab
->len
;
668 dc_diff
= vl_vlc_ubits(&bs
->vlc
, size
) - UBITS (SBITS (~bs
->vlc
.buf
, 1), size
);
669 bs
->vlc
.buf
<<= size
;
672 vl_vlc_dumpbits(&bs
->vlc
, 3);
676 tab
= DC_long
+ (vl_vlc_ubits(&bs
->vlc
, 9) - 0x1e0);
678 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
);
679 vl_vlc_needbits(&bs
->vlc
);
680 dc_diff
= vl_vlc_ubits(&bs
->vlc
, size
) - UBITS (SBITS (~bs
->vlc
.buf
, 1), size
);
681 vl_vlc_dumpbits(&bs
->vlc
, size
);
687 get_chroma_dc_dct_diff(struct vl_mpg12_bs
*bs
)
693 if (bs
->vlc
.buf
< 0xf8000000) {
694 tab
= DC_chrom_5
+ vl_vlc_ubits(&bs
->vlc
, 5);
697 bs
->vlc
.bits
+= tab
->len
+ size
;
698 bs
->vlc
.buf
<<= tab
->len
;
699 dc_diff
= vl_vlc_ubits(&bs
->vlc
, size
) - UBITS (SBITS (~bs
->vlc
.buf
, 1), size
);
700 bs
->vlc
.buf
<<= size
;
703 vl_vlc_dumpbits(&bs
->vlc
, 2);
707 tab
= DC_long
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 0x3e0);
709 vl_vlc_dumpbits(&bs
->vlc
, tab
->len
+ 1);
710 vl_vlc_needbits(&bs
->vlc
);
711 dc_diff
= vl_vlc_ubits(&bs
->vlc
, size
) - UBITS (SBITS (~bs
->vlc
.buf
, 1), size
);
712 vl_vlc_dumpbits(&bs
->vlc
, size
);
718 get_intra_block_B14(struct vl_mpg12_bs
*bs
, int quantizer_scale
, short *dest
)
725 vl_vlc_needbits(&bs
->vlc
);
728 if (bs
->vlc
.buf
>= 0x28000000) {
730 tab
= DCT_B14AC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
734 break; /* end of block */
737 bs
->vlc
.buf
<<= tab
->len
;
738 bs
->vlc
.bits
+= tab
->len
+ 1;
739 val
= tab
->level
* quantizer_scale
;
741 val
= (val
^ vl_vlc_sbits(&bs
->vlc
, 1)) - vl_vlc_sbits(&bs
->vlc
, 1);
746 vl_vlc_needbits(&bs
->vlc
);
750 } else if (bs
->vlc
.buf
>= 0x04000000) {
752 tab
= DCT_B14_8
+ (vl_vlc_ubits(&bs
->vlc
, 8) - 4);
760 i
+= UBITS(bs
->vlc
.buf
<< 6, 6) - 64;
762 break; /* illegal, check needed to avoid buffer overflow */
764 vl_vlc_dumpbits(&bs
->vlc
, 12);
765 vl_vlc_needbits(&bs
->vlc
);
766 val
= vl_vlc_sbits(&bs
->vlc
, 12) * quantizer_scale
;
770 vl_vlc_dumpbits(&bs
->vlc
, 12);
771 vl_vlc_needbits(&bs
->vlc
);
775 } else if (bs
->vlc
.buf
>= 0x02000000) {
776 tab
= DCT_B14_10
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 8);
780 } else if (bs
->vlc
.buf
>= 0x00800000) {
781 tab
= DCT_13
+ (vl_vlc_ubits(&bs
->vlc
, 13) - 16);
785 } else if (bs
->vlc
.buf
>= 0x00200000) {
786 tab
= DCT_15
+ (vl_vlc_ubits(&bs
->vlc
, 15) - 16);
791 tab
= DCT_16
+ vl_vlc_ubits(&bs
->vlc
, 16);
793 vl_vlc_getword(&bs
->vlc
, bs
->vlc
.bits
+ 16);
798 break; /* illegal, check needed to avoid buffer overflow */
801 vl_vlc_dumpbits(&bs
->vlc
, 2); /* dump end of block code */
805 get_intra_block_B15(struct vl_mpg12_bs
*bs
, int quantizer_scale
, short *dest
)
812 vl_vlc_needbits(&bs
->vlc
);
815 if (bs
->vlc
.buf
>= 0x04000000) {
817 tab
= DCT_B15_8
+ (vl_vlc_ubits(&bs
->vlc
, 8) - 4);
823 bs
->vlc
.buf
<<= tab
->len
;
824 bs
->vlc
.bits
+= tab
->len
+ 1;
825 val
= tab
->level
* quantizer_scale
;
827 val
= (val
^ vl_vlc_sbits(&bs
->vlc
, 1)) - vl_vlc_sbits(&bs
->vlc
, 1);
832 vl_vlc_needbits(&bs
->vlc
);
838 /* end of block. I commented out this code because if we */
839 /* dont exit here we will still exit at the later test :) */
841 /* if (i >= 128) break; */ /* end of block */
845 i
+= UBITS(bs
->vlc
.buf
<< 6, 6) - 64;
847 break; /* illegal, check against buffer overflow */
849 vl_vlc_dumpbits(&bs
->vlc
, 12);
850 vl_vlc_needbits(&bs
->vlc
);
851 val
= vl_vlc_sbits(&bs
->vlc
, 12) * quantizer_scale
;
855 vl_vlc_dumpbits(&bs
->vlc
, 12);
856 vl_vlc_needbits(&bs
->vlc
);
861 } else if (bs
->vlc
.buf
>= 0x02000000) {
862 tab
= DCT_B15_10
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 8);
866 } else if (bs
->vlc
.buf
>= 0x00800000) {
867 tab
= DCT_13
+ (vl_vlc_ubits(&bs
->vlc
, 13) - 16);
871 } else if (bs
->vlc
.buf
>= 0x00200000) {
872 tab
= DCT_15
+ (vl_vlc_ubits(&bs
->vlc
, 15) - 16);
877 tab
= DCT_16
+ vl_vlc_ubits(&bs
->vlc
, 16);
879 vl_vlc_getword(&bs
->vlc
, bs
->vlc
.bits
+ 16);
884 break; /* illegal, check needed to avoid buffer overflow */
887 vl_vlc_dumpbits(&bs
->vlc
, 4); /* dump end of block code */
891 get_non_intra_block(struct vl_mpg12_bs
*bs
, int quantizer_scale
, short *dest
)
898 vl_vlc_needbits(&bs
->vlc
);
899 if (bs
->vlc
.buf
>= 0x28000000) {
900 tab
= DCT_B14DC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
906 if (bs
->vlc
.buf
>= 0x28000000) {
908 tab
= DCT_B14AC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
913 break; /* end of block */
916 bs
->vlc
.buf
<<= tab
->len
;
917 bs
->vlc
.bits
+= tab
->len
+ 1;
918 val
= ((2*tab
->level
+1) * quantizer_scale
) >> 1;
920 val
= (val
^ vl_vlc_sbits(&bs
->vlc
, 1)) - vl_vlc_sbits(&bs
->vlc
, 1);
925 vl_vlc_needbits(&bs
->vlc
);
932 if (bs
->vlc
.buf
>= 0x04000000) {
934 tab
= DCT_B14_8
+ (vl_vlc_ubits(&bs
->vlc
, 8) - 4);
942 i
+= UBITS(bs
->vlc
.buf
<< 6, 6) - 64;
944 break; /* illegal, check needed to avoid buffer overflow */
946 vl_vlc_dumpbits(&bs
->vlc
, 12);
947 vl_vlc_needbits(&bs
->vlc
);
948 val
= 2 * (vl_vlc_sbits(&bs
->vlc
, 12) + vl_vlc_sbits(&bs
->vlc
, 1)) + 1;
949 val
= (val
* quantizer_scale
) / 2;
953 vl_vlc_dumpbits(&bs
->vlc
, 12);
954 vl_vlc_needbits(&bs
->vlc
);
958 } else if (bs
->vlc
.buf
>= 0x02000000) {
959 tab
= DCT_B14_10
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 8);
963 } else if (bs
->vlc
.buf
>= 0x00800000) {
964 tab
= DCT_13
+ (vl_vlc_ubits(&bs
->vlc
, 13) - 16);
968 } else if (bs
->vlc
.buf
>= 0x00200000) {
969 tab
= DCT_15
+ (vl_vlc_ubits(&bs
->vlc
, 15) - 16);
974 tab
= DCT_16
+ vl_vlc_ubits(&bs
->vlc
, 16);
976 vl_vlc_getword(&bs
->vlc
, bs
->vlc
.bits
+ 16);
981 break; /* illegal, check needed to avoid buffer overflow */
983 vl_vlc_dumpbits(&bs
->vlc
, 2); /* dump end of block code */
987 get_mpeg1_intra_block(struct vl_mpg12_bs
*bs
, int quantizer_scale
, short *dest
)
994 vl_vlc_needbits(&bs
->vlc
);
997 if (bs
->vlc
.buf
>= 0x28000000) {
999 tab
= DCT_B14AC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
1003 break; /* end of block */
1006 bs
->vlc
.buf
<<= tab
->len
;
1007 bs
->vlc
.bits
+= tab
->len
+ 1;
1008 val
= tab
->level
* quantizer_scale
;
1011 val
= (val
- 1) | 1;
1013 /* if (bitstream_get (1)) val = -val; */
1014 val
= (val
^ vl_vlc_sbits(&bs
->vlc
, 1)) - vl_vlc_sbits(&bs
->vlc
, 1);
1019 vl_vlc_needbits(&bs
->vlc
);
1023 } else if (bs
->vlc
.buf
>= 0x04000000) {
1025 tab
= DCT_B14_8
+ (vl_vlc_ubits(&bs
->vlc
, 8) - 4);
1033 i
+= UBITS(bs
->vlc
.buf
<< 6, 6) - 64;
1035 break; /* illegal, check needed to avoid buffer overflow */
1037 vl_vlc_dumpbits(&bs
->vlc
, 12);
1038 vl_vlc_needbits(&bs
->vlc
);
1039 val
= vl_vlc_sbits(&bs
->vlc
, 8);
1040 if (! (val
& 0x7f)) {
1041 vl_vlc_dumpbits(&bs
->vlc
, 8);
1042 val
= vl_vlc_ubits(&bs
->vlc
, 8) + 2 * val
;
1044 val
= val
* quantizer_scale
;
1047 val
= (val
+ ~SBITS (val
, 1)) | 1;
1051 vl_vlc_dumpbits(&bs
->vlc
, 8);
1052 vl_vlc_needbits(&bs
->vlc
);
1056 } else if (bs
->vlc
.buf
>= 0x02000000) {
1057 tab
= DCT_B14_10
+ (vl_vlc_ubits(&bs
->vlc
, 10) - 8);
1061 } else if (bs
->vlc
.buf
>= 0x00800000) {
1062 tab
= DCT_13
+ (vl_vlc_ubits(&bs
->vlc
, 13) - 16);
1066 } else if (bs
->vlc
.buf
>= 0x00200000) {
1067 tab
= DCT_15
+ (vl_vlc_ubits(&bs
->vlc
, 15) - 16);
1072 tab
= DCT_16
+ vl_vlc_ubits(&bs
->vlc
, 16);
1074 vl_vlc_getword(&bs
->vlc
, bs
->vlc
.bits
+ 16);
1079 break; /* illegal, check needed to avoid buffer overflow */
1081 vl_vlc_dumpbits(&bs
->vlc
, 2); /* dump end of block code */
1085 get_mpeg1_non_intra_block(struct vl_mpg12_bs
*bs
, int quantizer_scale
, short *dest
)
1092 vl_vlc_needbits(&bs
->vlc
);
1093 if (bs
->vlc
.buf
>= 0x28000000) {
1094 tab
= DCT_B14DC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
1100 if (bs
->vlc
.buf
>= 0x28000000) {
1102 tab
= DCT_B14AC_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 5);
1107 break; /* end of block */
1110 bs
->vlc
.buf
<<= tab
->len
;
1111 bs
->vlc
.bits
+= tab
->len
+ 1;
1112 val
= ((2*tab
->level
+1) * quantizer_scale
) >> 1;
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);
1123 vl_vlc_needbits(&bs
->vlc
);
1130 if (bs
->vlc
.buf
>= 0x04000000) {
1132 tab
= DCT_B14_8
+ (vl_vlc_ubits(&bs
->vlc
, 8) - 4);
1140 i
+= UBITS(bs
->vlc
.buf
<< 6, 6) - 64;
1142 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
= 2 * (val
+ SBITS (val
, 1)) + 1;
1152 val
= (val
* quantizer_scale
) / 2;
1155 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 slice_intra_DCT(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
, int cc
,
1194 unsigned x
, unsigned y
, enum pipe_mpeg12_dct_type coding
, int quantizer_scale
, int dc_dct_pred
[3])
1198 bs
->ycbcr_stream
[cc
]->x
= x
;
1199 bs
->ycbcr_stream
[cc
]->y
= y
;
1200 bs
->ycbcr_stream
[cc
]->intra
= 1;
1201 bs
->ycbcr_stream
[cc
]->coding
= coding
;
1203 vl_vlc_needbits(&bs
->vlc
);
1205 /* Get the intra DC coefficient and inverse quantize it */
1207 dc_dct_pred
[0] += get_luma_dc_dct_diff(bs
);
1209 dc_dct_pred
[cc
] += get_chroma_dc_dct_diff(bs
);
1211 memset(dest
, 0, sizeof(int16_t) * 64);
1212 dest
[0] = dc_dct_pred
[cc
];
1213 if (picture
->base
.profile
== PIPE_VIDEO_PROFILE_MPEG1
) {
1214 if (picture
->picture_coding_type
!= D_TYPE
)
1215 get_mpeg1_intra_block(bs
, quantizer_scale
, dest
);
1216 } else if (picture
->intra_vlc_format
)
1217 get_intra_block_B15(bs
, quantizer_scale
, dest
);
1219 get_intra_block_B14(bs
, quantizer_scale
, dest
);
1221 memcpy(bs
->ycbcr_buffer
[cc
], dest
, sizeof(int16_t) * 64);
1223 bs
->num_ycbcr_blocks
[cc
]++;
1224 bs
->ycbcr_stream
[cc
]++;
1225 bs
->ycbcr_buffer
[cc
] += 64;
1229 slice_non_intra_DCT(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
, int cc
,
1230 unsigned x
, unsigned y
, enum pipe_mpeg12_dct_type coding
, int quantizer_scale
)
1234 bs
->ycbcr_stream
[cc
]->x
= x
;
1235 bs
->ycbcr_stream
[cc
]->y
= y
;
1236 bs
->ycbcr_stream
[cc
]->intra
= 0;
1237 bs
->ycbcr_stream
[cc
]->coding
= coding
;
1239 memset(dest
, 0, sizeof(int16_t) * 64);
1240 if (picture
->base
.profile
== PIPE_VIDEO_PROFILE_MPEG1
)
1241 get_mpeg1_non_intra_block(bs
, quantizer_scale
, dest
);
1243 get_non_intra_block(bs
, quantizer_scale
, dest
);
1245 memcpy(bs
->ycbcr_buffer
[cc
], dest
, sizeof(int16_t) * 64);
1247 bs
->num_ycbcr_blocks
[cc
]++;
1248 bs
->ycbcr_stream
[cc
]++;
1249 bs
->ycbcr_buffer
[cc
] += 64;
1253 motion_mp1(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct vl_motionvector
*mv
)
1255 int motion_x
, motion_y
;
1257 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1259 vl_vlc_needbits(&bs
->vlc
);
1260 motion_x
= (mv
->top
.x
+ (get_motion_delta(bs
, f_code
[0]) << f_code
[1]));
1261 motion_x
= bound_motion_vector (motion_x
, f_code
[0] + f_code
[1]);
1262 mv
->top
.x
= mv
->bottom
.x
= motion_x
;
1264 vl_vlc_needbits(&bs
->vlc
);
1265 motion_y
= (mv
->top
.y
+ (get_motion_delta(bs
, f_code
[0]) << f_code
[1]));
1266 motion_y
= bound_motion_vector (motion_y
, f_code
[0] + f_code
[1]);
1267 mv
->top
.y
= mv
->bottom
.y
= motion_y
;
1271 motion_fr_frame(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct vl_motionvector
*mv
)
1273 int motion_x
, motion_y
;
1275 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1277 vl_vlc_needbits(&bs
->vlc
);
1278 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1279 motion_x
= bound_motion_vector(motion_x
, f_code
[0]);
1280 mv
->top
.x
= mv
->bottom
.x
= motion_x
;
1282 vl_vlc_needbits(&bs
->vlc
);
1283 motion_y
= mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]);
1284 motion_y
= bound_motion_vector(motion_y
, f_code
[1]);
1285 mv
->top
.y
= mv
->bottom
.y
= motion_y
;
1289 motion_fr_field(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct vl_motionvector
*mv
)
1291 int motion_x
, motion_y
;
1293 vl_vlc_needbits(&bs
->vlc
);
1294 mv
->top
.field_select
= vl_vlc_ubits(&bs
->vlc
, 1) ?
1295 PIPE_VIDEO_BOTTOM_FIELD
: PIPE_VIDEO_TOP_FIELD
;
1296 vl_vlc_dumpbits(&bs
->vlc
, 1);
1298 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1299 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1300 mv
->top
.x
= motion_x
;
1302 vl_vlc_needbits(&bs
->vlc
);
1303 motion_y
= (mv
->top
.y
>> 1) + get_motion_delta(bs
, f_code
[1]);
1304 /* motion_y = bound_motion_vector (motion_y, f_code[1]); */
1305 mv
->top
.y
= motion_y
<< 1;
1307 vl_vlc_needbits(&bs
->vlc
);
1308 mv
->bottom
.field_select
= vl_vlc_ubits(&bs
->vlc
, 1) ?
1309 PIPE_VIDEO_BOTTOM_FIELD
: PIPE_VIDEO_TOP_FIELD
;
1310 vl_vlc_dumpbits(&bs
->vlc
, 1);
1312 motion_x
= mv
->bottom
.x
+ get_motion_delta(bs
, f_code
[0]);
1313 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1314 mv
->bottom
.x
= motion_x
;
1316 vl_vlc_needbits(&bs
->vlc
);
1317 motion_y
= (mv
->bottom
.y
>> 1) + get_motion_delta(bs
, f_code
[1]);
1318 /* motion_y = bound_motion_vector (motion_y, f_code[1]); */
1319 mv
->bottom
.y
= motion_y
<< 1;
1323 motion_fr_dmv(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct vl_motionvector
*mv
)
1325 int motion_x
, motion_y
;
1327 // TODO Implement dmv
1328 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1330 vl_vlc_needbits(&bs
->vlc
);
1331 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1332 motion_x
= bound_motion_vector(motion_x
, f_code
[0]);
1333 mv
->top
.x
= mv
->bottom
.x
= motion_x
;
1335 vl_vlc_needbits(&bs
->vlc
);
1336 motion_y
= (mv
->top
.y
>> 1) + get_motion_delta(bs
, f_code
[1]);
1337 /* motion_y = bound_motion_vector (motion_y, f_code[1]); */
1338 mv
->top
.y
= mv
->bottom
.y
= motion_y
<< 1;
1341 /* like motion_frame, but parsing without actual motion compensation */
1343 motion_fr_conceal(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct vl_motionvector
*mv
)
1347 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1349 vl_vlc_needbits(&bs
->vlc
);
1350 tmp
= (mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]));
1351 tmp
= bound_motion_vector (tmp
, f_code
[0]);
1352 mv
->top
.x
= mv
->bottom
.x
= tmp
;
1354 vl_vlc_needbits(&bs
->vlc
);
1355 tmp
= (mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]));
1356 tmp
= bound_motion_vector (tmp
, f_code
[1]);
1357 mv
->top
.y
= mv
->bottom
.y
= tmp
;
1359 vl_vlc_dumpbits(&bs
->vlc
, 1); /* remove marker_bit */
1363 motion_fi_field(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct vl_motionvector
*mv
)
1365 int motion_x
, motion_y
;
1367 vl_vlc_needbits(&bs
->vlc
);
1370 //vl_vlc_ubits(&bs->vlc, 1);
1372 // TODO field select may need to do something here for bob (weave ok)
1373 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1374 vl_vlc_dumpbits(&bs
->vlc
, 1);
1376 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1377 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1378 mv
->top
.x
= mv
->bottom
.x
= motion_x
;
1380 vl_vlc_needbits(&bs
->vlc
);
1381 motion_y
= mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]);
1382 motion_y
= bound_motion_vector (motion_y
, f_code
[1]);
1383 mv
->top
.y
= mv
->bottom
.y
= motion_y
;
1387 motion_fi_16x8(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct vl_motionvector
*mv
)
1389 int motion_x
, motion_y
;
1391 vl_vlc_needbits(&bs
->vlc
);
1394 //vl_vlc_ubits(&bs->vlc, 1);
1396 // TODO field select may need to do something here bob (weave ok)
1397 mv
->top
.field_select
= PIPE_VIDEO_FRAME
;
1398 vl_vlc_dumpbits(&bs
->vlc
, 1);
1400 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1401 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1402 mv
->top
.x
= motion_x
;
1404 vl_vlc_needbits(&bs
->vlc
);
1405 motion_y
= mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]);
1406 motion_y
= bound_motion_vector (motion_y
, f_code
[1]);
1407 mv
->top
.y
= motion_y
;
1409 vl_vlc_needbits(&bs
->vlc
);
1411 //vl_vlc_ubits(&bs->vlc, 1);
1413 // TODO field select may need to do something here for bob (weave ok)
1414 mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1415 vl_vlc_dumpbits(&bs
->vlc
, 1);
1417 motion_x
= mv
->bottom
.x
+ get_motion_delta(bs
, f_code
[0]);
1418 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1419 mv
->bottom
.x
= motion_x
;
1421 vl_vlc_needbits(&bs
->vlc
);
1422 motion_y
= mv
->bottom
.y
+ get_motion_delta(bs
, f_code
[1]);
1423 motion_y
= bound_motion_vector (motion_y
, f_code
[1]);
1424 mv
->bottom
.y
= motion_y
;
1428 motion_fi_dmv(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct vl_motionvector
*mv
)
1430 int motion_x
, motion_y
;
1432 // TODO field select may need to do something here for bob (weave ok)
1433 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1435 vl_vlc_needbits(&bs
->vlc
);
1436 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1437 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1438 mv
->top
.x
= mv
->bottom
.x
= motion_x
;
1440 vl_vlc_needbits(&bs
->vlc
);
1441 motion_y
= mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]);
1442 motion_y
= bound_motion_vector (motion_y
, f_code
[1]);
1443 mv
->top
.y
= mv
->bottom
.y
= motion_y
;
1448 motion_fi_conceal(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct vl_motionvector
*mv
)
1452 vl_vlc_needbits(&bs
->vlc
);
1453 vl_vlc_dumpbits(&bs
->vlc
, 1); /* remove field_select */
1455 tmp
= (mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]));
1456 tmp
= bound_motion_vector(tmp
, f_code
[0]);
1457 mv
->top
.x
= mv
->bottom
.x
= tmp
;
1459 vl_vlc_needbits(&bs
->vlc
);
1460 tmp
= (mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]));
1461 tmp
= bound_motion_vector(tmp
, f_code
[1]);
1462 mv
->top
.y
= mv
->bottom
.y
= tmp
;
1464 vl_vlc_dumpbits(&bs
->vlc
, 1); /* remove marker_bit */
1467 #define MOTION_CALL(routine, macroblock_modes) \
1469 if ((macroblock_modes) & MACROBLOCK_MOTION_FORWARD) \
1470 routine(bs, picture->f_code[0], &mv_fwd); \
1471 if ((macroblock_modes) & MACROBLOCK_MOTION_BACKWARD) \
1472 routine(bs, picture->f_code[1], &mv_bwd); \
1476 store_motionvectors(struct vl_mpg12_bs
*bs
, unsigned *mv_pos
,
1477 struct vl_motionvector
*mv_fwd
,
1478 struct vl_motionvector
*mv_bwd
)
1480 bs
->mv_stream
[0][*mv_pos
].top
= mv_fwd
->top
;
1481 bs
->mv_stream
[0][*mv_pos
].bottom
=
1482 mv_fwd
->top
.field_select
== PIPE_VIDEO_FRAME
?
1483 mv_fwd
->top
: mv_fwd
->bottom
;
1485 bs
->mv_stream
[1][*mv_pos
].top
= mv_bwd
->top
;
1486 bs
->mv_stream
[1][*mv_pos
].bottom
=
1487 mv_bwd
->top
.field_select
== PIPE_VIDEO_FRAME
?
1488 mv_bwd
->top
: mv_bwd
->bottom
;
1494 slice_init(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
,
1495 int *quantizer_scale
, unsigned *x
, unsigned *y
, unsigned *mv_pos
)
1499 vl_vlc_need32bits(&bs
->vlc
);
1500 while(bs
->vlc
.buf
< 0x101 || bs
->vlc
.buf
> 0x1AF) {
1501 if(!vl_vlc_getbyte(&bs
->vlc
))
1504 *y
= (bs
->vlc
.buf
& 0xFF) - 1;
1505 vl_vlc_restart(&bs
->vlc
);
1507 *quantizer_scale
= get_quantizer_scale(bs
, picture
);
1509 /* ignore intra_slice and all the extra data */
1510 while (bs
->vlc
.buf
& 0x80000000) {
1511 vl_vlc_dumpbits(&bs
->vlc
, 9);
1512 vl_vlc_needbits(&bs
->vlc
);
1515 /* decode initial macroblock address increment */
1518 if (bs
->vlc
.buf
>= 0x08000000) {
1519 mba
= MBA_5
+ (vl_vlc_ubits(&bs
->vlc
, 6) - 2);
1521 } else if (bs
->vlc
.buf
>= 0x01800000) {
1522 mba
= MBA_11
+ (vl_vlc_ubits(&bs
->vlc
, 12) - 24);
1524 } else switch (vl_vlc_ubits(&bs
->vlc
, 12)) {
1525 case 8: /* macroblock_escape */
1527 vl_vlc_dumpbits(&bs
->vlc
, 11);
1528 vl_vlc_needbits(&bs
->vlc
);
1530 case 15: /* macroblock_stuffing (MPEG1 only) */
1531 bs
->vlc
.buf
&= 0xfffff;
1532 vl_vlc_dumpbits(&bs
->vlc
, 11);
1533 vl_vlc_needbits(&bs
->vlc
);
1535 default: /* error */
1539 vl_vlc_dumpbits(&bs
->vlc
, mba
->len
+ 1);
1542 while (*x
>= bs
->width
) {
1546 if (*y
> bs
->height
)
1549 *mv_pos
= *x
+ *y
* bs
->width
;
1555 decode_slice(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
*picture
)
1557 enum vl_field_select default_field_select
;
1558 struct vl_motionvector mv_fwd
, mv_bwd
;
1559 enum pipe_mpeg12_dct_type dct_type
;
1561 /* predictor for DC coefficients in intra blocks */
1562 int dc_dct_pred
[3] = { 0, 0, 0 };
1563 int quantizer_scale
;
1565 unsigned x
, y
, mv_pos
;
1567 switch(picture
->picture_structure
) {
1569 default_field_select
= PIPE_VIDEO_TOP_FIELD
;
1573 default_field_select
= PIPE_VIDEO_BOTTOM_FIELD
;
1577 default_field_select
= PIPE_VIDEO_FRAME
;
1581 if (!slice_init(bs
, picture
, &quantizer_scale
, &x
, &y
, &mv_pos
))
1584 mv_fwd
.top
.x
= mv_fwd
.top
.y
= mv_fwd
.bottom
.x
= mv_fwd
.bottom
.y
= 0;
1585 mv_fwd
.top
.field_select
= mv_fwd
.bottom
.field_select
= default_field_select
;
1587 mv_bwd
.top
.x
= mv_bwd
.top
.y
= mv_bwd
.bottom
.x
= mv_bwd
.bottom
.y
= 0;
1588 mv_bwd
.top
.field_select
= mv_bwd
.bottom
.field_select
= default_field_select
;
1591 int macroblock_modes
;
1595 vl_vlc_needbits(&bs
->vlc
);
1597 macroblock_modes
= get_macroblock_modes(bs
, picture
);
1598 dct_type
= get_dct_type(bs
, picture
, macroblock_modes
);
1600 switch(macroblock_modes
& (MACROBLOCK_MOTION_FORWARD
|MACROBLOCK_MOTION_BACKWARD
)) {
1601 case (MACROBLOCK_MOTION_FORWARD
|MACROBLOCK_MOTION_BACKWARD
):
1602 mv_fwd
.top
.weight
= mv_fwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_HALF
;
1603 mv_bwd
.top
.weight
= mv_bwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_HALF
;
1607 mv_fwd
.top
.field_select
= mv_fwd
.bottom
.field_select
= default_field_select
;
1608 mv_bwd
.top
.field_select
= mv_bwd
.bottom
.field_select
= default_field_select
;
1611 case MACROBLOCK_MOTION_FORWARD
:
1612 mv_fwd
.top
.weight
= mv_fwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MAX
;
1613 mv_bwd
.top
.weight
= mv_bwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1616 case MACROBLOCK_MOTION_BACKWARD
:
1617 mv_fwd
.top
.weight
= mv_fwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1618 mv_bwd
.top
.weight
= mv_bwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MAX
;
1622 /* maybe integrate MACROBLOCK_QUANT test into get_macroblock_modes ? */
1623 if (macroblock_modes
& MACROBLOCK_QUANT
)
1624 quantizer_scale
= get_quantizer_scale(bs
, picture
);
1626 if (macroblock_modes
& MACROBLOCK_INTRA
) {
1628 if (picture
->concealment_motion_vectors
) {
1629 if (picture
->picture_structure
== FRAME_PICTURE
)
1630 motion_fr_conceal(bs
, picture
->f_code
[0], &mv_fwd
);
1632 motion_fi_conceal(bs
, picture
->f_code
[0], &mv_fwd
);
1635 mv_fwd
.top
.x
= mv_fwd
.top
.y
= mv_fwd
.bottom
.x
= mv_fwd
.bottom
.y
= 0;
1636 mv_bwd
.top
.x
= mv_bwd
.top
.y
= mv_bwd
.bottom
.x
= mv_bwd
.bottom
.y
= 0;
1638 mv_fwd
.top
.weight
= mv_fwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1639 mv_bwd
.top
.weight
= mv_bwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1641 // unravaled loop of 6 block(i) calls in macroblock()
1642 slice_intra_DCT(bs
, picture
, 0, x
*2+0, y
*2+0, dct_type
, quantizer_scale
, dc_dct_pred
);
1643 slice_intra_DCT(bs
, picture
, 0, x
*2+1, y
*2+0, dct_type
, quantizer_scale
, dc_dct_pred
);
1644 slice_intra_DCT(bs
, picture
, 0, x
*2+0, y
*2+1, dct_type
, quantizer_scale
, dc_dct_pred
);
1645 slice_intra_DCT(bs
, picture
, 0, x
*2+1, y
*2+1, dct_type
, quantizer_scale
, dc_dct_pred
);
1646 slice_intra_DCT(bs
, picture
, 1, x
, y
, PIPE_MPEG12_DCT_TYPE_FRAME
, quantizer_scale
, dc_dct_pred
);
1647 slice_intra_DCT(bs
, picture
, 2, x
, y
, PIPE_MPEG12_DCT_TYPE_FRAME
, quantizer_scale
, dc_dct_pred
);
1649 if (picture
->picture_coding_type
== D_TYPE
) {
1650 vl_vlc_needbits(&bs
->vlc
);
1651 vl_vlc_dumpbits(&bs
->vlc
, 1);
1655 if (picture
->picture_structure
== FRAME_PICTURE
)
1656 switch (macroblock_modes
& MOTION_TYPE_MASK
) {
1658 if (picture
->base
.profile
== PIPE_VIDEO_PROFILE_MPEG1
) {
1659 MOTION_CALL(motion_mp1
, macroblock_modes
);
1661 MOTION_CALL(motion_fr_frame
, macroblock_modes
);
1666 MOTION_CALL (motion_fr_field
, macroblock_modes
);
1670 MOTION_CALL (motion_fr_dmv
, MACROBLOCK_MOTION_FORWARD
);
1674 /* non-intra mb without forward mv in a P picture */
1675 mv_fwd
.top
.x
= mv_fwd
.top
.y
= mv_fwd
.bottom
.x
= mv_fwd
.bottom
.y
= 0;
1676 mv_bwd
.top
.x
= mv_bwd
.top
.y
= mv_bwd
.bottom
.x
= mv_bwd
.bottom
.y
= 0;
1680 switch (macroblock_modes
& MOTION_TYPE_MASK
) {
1682 MOTION_CALL (motion_fi_field
, macroblock_modes
);
1686 MOTION_CALL (motion_fi_16x8
, macroblock_modes
);
1690 MOTION_CALL (motion_fi_dmv
, MACROBLOCK_MOTION_FORWARD
);
1694 /* non-intra mb without forward mv in a P picture */
1695 mv_fwd
.top
.x
= mv_fwd
.top
.y
= mv_fwd
.bottom
.x
= mv_fwd
.bottom
.y
= 0;
1696 mv_bwd
.top
.x
= mv_bwd
.top
.y
= mv_bwd
.bottom
.x
= mv_bwd
.bottom
.y
= 0;
1700 if (macroblock_modes
& MACROBLOCK_PATTERN
) {
1701 int coded_block_pattern
= get_coded_block_pattern(bs
);
1703 // TODO optimize not fully used for idct accel only mc.
1704 if (coded_block_pattern
& 0x20)
1705 slice_non_intra_DCT(bs
, picture
, 0, x
*2+0, y
*2+0, dct_type
, quantizer_scale
); // cc0 luma 0
1706 if (coded_block_pattern
& 0x10)
1707 slice_non_intra_DCT(bs
, picture
, 0, x
*2+1, y
*2+0, dct_type
, quantizer_scale
); // cc0 luma 1
1708 if (coded_block_pattern
& 0x08)
1709 slice_non_intra_DCT(bs
, picture
, 0, x
*2+0, y
*2+1, dct_type
, quantizer_scale
); // cc0 luma 2
1710 if (coded_block_pattern
& 0x04)
1711 slice_non_intra_DCT(bs
, picture
, 0, x
*2+1, y
*2+1, dct_type
, quantizer_scale
); // cc0 luma 3
1712 if (coded_block_pattern
& 0x2)
1713 slice_non_intra_DCT(bs
, picture
, 1, x
, y
, PIPE_MPEG12_DCT_TYPE_FRAME
, quantizer_scale
); // cc1 croma
1714 if (coded_block_pattern
& 0x1)
1715 slice_non_intra_DCT(bs
, picture
, 2, x
, y
, PIPE_MPEG12_DCT_TYPE_FRAME
, quantizer_scale
); // cc2 croma
1718 dc_dct_pred
[0] = dc_dct_pred
[1] = dc_dct_pred
[2] = 0;
1721 store_motionvectors(bs
, &mv_pos
, &mv_fwd
, &mv_bwd
);
1722 if (++x
>= bs
->width
) {
1724 if (y
>= bs
->height
)
1729 vl_vlc_needbits(&bs
->vlc
);
1732 if (bs
->vlc
.buf
>= 0x10000000) {
1733 mba
= MBA_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 2);
1735 } else if (bs
->vlc
.buf
>= 0x03000000) {
1736 mba
= MBA_11
+ (vl_vlc_ubits(&bs
->vlc
, 11) - 24);
1738 } else switch (vl_vlc_ubits(&bs
->vlc
, 11)) {
1739 case 8: /* macroblock_escape */
1742 case 15: /* macroblock_stuffing (MPEG1 only) */
1743 vl_vlc_dumpbits(&bs
->vlc
, 11);
1744 vl_vlc_needbits(&bs
->vlc
);
1746 default: /* end of slice, or error */
1750 vl_vlc_dumpbits(&bs
->vlc
, mba
->len
);
1751 mba_inc
+= mba
->mba
;
1753 //TODO conversion to signed format signed format
1754 dc_dct_pred
[0] = dc_dct_pred
[1] = dc_dct_pred
[2] = 0;
1756 mv_fwd
.top
.field_select
= mv_fwd
.bottom
.field_select
= default_field_select
;
1757 mv_bwd
.top
.field_select
= mv_bwd
.bottom
.field_select
= default_field_select
;
1759 if (picture
->picture_coding_type
== P_TYPE
) {
1760 mv_fwd
.top
.x
= mv_fwd
.top
.y
= mv_fwd
.bottom
.x
= mv_fwd
.bottom
.y
= 0;
1761 mv_fwd
.top
.weight
= mv_fwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MAX
;
1766 store_motionvectors(bs
, &mv_pos
, &mv_fwd
, &mv_bwd
);
1767 } while (--mba_inc
);
1769 while (x
>= bs
->width
) {
1771 if (y
>= bs
->height
)
1779 vl_mpg12_bs_init(struct vl_mpg12_bs
*bs
, unsigned width
, unsigned height
)
1783 memset(bs
, 0, sizeof(struct vl_mpg12_bs
));
1786 bs
->height
= height
;
1790 vl_mpg12_bs_set_buffers(struct vl_mpg12_bs
*bs
, struct vl_ycbcr_block
*ycbcr_stream
[VL_MAX_PLANES
],
1791 short *ycbcr_buffer
[VL_MAX_PLANES
], struct vl_motionvector
*mv_stream
[VL_MAX_REF_FRAMES
])
1796 assert(ycbcr_stream
&& ycbcr_buffer
);
1799 for (i
= 0; i
< VL_MAX_PLANES
; ++i
) {
1800 bs
->ycbcr_stream
[i
] = ycbcr_stream
[i
];
1801 bs
->ycbcr_buffer
[i
] = ycbcr_buffer
[i
];
1803 for (i
= 0; i
< VL_MAX_REF_FRAMES
; ++i
)
1804 bs
->mv_stream
[i
] = mv_stream
[i
];
1807 for (i
= 0; i
< bs
->width
*bs
->height
; ++i
) {
1808 bs
->mv_stream
[0][i
].top
.x
= bs
->mv_stream
[0][i
].top
.y
= 0;
1809 bs
->mv_stream
[0][i
].top
.field_select
= PIPE_VIDEO_FRAME
;
1810 bs
->mv_stream
[0][i
].top
.weight
= PIPE_VIDEO_MV_WEIGHT_MAX
;
1811 bs
->mv_stream
[0][i
].bottom
.x
= bs
->mv_stream
[0][i
].bottom
.y
= 0;
1812 bs
->mv_stream
[0][i
].bottom
.field_select
= PIPE_VIDEO_FRAME
;
1813 bs
->mv_stream
[0][i
].bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MAX
;
1815 bs
->mv_stream
[1][i
].top
.x
= bs
->mv_stream
[1][i
].top
.y
= 0;
1816 bs
->mv_stream
[1][i
].top
.field_select
= PIPE_VIDEO_FRAME
;
1817 bs
->mv_stream
[1][i
].top
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1818 bs
->mv_stream
[1][i
].bottom
.x
= bs
->mv_stream
[1][i
].bottom
.y
= 0;
1819 bs
->mv_stream
[1][i
].bottom
.field_select
= PIPE_VIDEO_FRAME
;
1820 bs
->mv_stream
[1][i
].bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1825 vl_mpg12_bs_decode(struct vl_mpg12_bs
*bs
, unsigned num_bytes
, const void *buffer
,
1826 struct pipe_mpeg12_picture_desc
*picture
, unsigned num_ycbcr_blocks
[3])
1829 assert(num_ycbcr_blocks
);
1830 assert(buffer
&& num_bytes
);
1832 bs
->num_ycbcr_blocks
= num_ycbcr_blocks
;
1834 vl_vlc_init(&bs
->vlc
, buffer
, num_bytes
);
1836 while(decode_slice(bs
, picture
));