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
;
1202 bs
->ycbcr_stream
[cc
]->block_num
= bs
->block_num
++;
1204 vl_vlc_needbits(&bs
->vlc
);
1206 /* Get the intra DC coefficient and inverse quantize it */
1208 dc_dct_pred
[0] += get_luma_dc_dct_diff(bs
);
1210 dc_dct_pred
[cc
] += get_chroma_dc_dct_diff(bs
);
1212 memset(dest
, 0, sizeof(int16_t) * 64);
1213 dest
[0] = dc_dct_pred
[cc
];
1214 if (picture
->base
.profile
== PIPE_VIDEO_PROFILE_MPEG1
) {
1215 if (picture
->picture_coding_type
!= D_TYPE
)
1216 get_mpeg1_intra_block(bs
, quantizer_scale
, dest
);
1217 } else if (picture
->intra_vlc_format
)
1218 get_intra_block_B15(bs
, quantizer_scale
, dest
);
1220 get_intra_block_B14(bs
, quantizer_scale
, dest
);
1222 memcpy(bs
->ycbcr_buffer
, dest
, sizeof(int16_t) * 64);
1224 bs
->num_ycbcr_blocks
[cc
]++;
1225 bs
->ycbcr_stream
[cc
]++;
1226 bs
->ycbcr_buffer
+= 64;
1230 slice_non_intra_DCT(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
, int cc
,
1231 unsigned x
, unsigned y
, enum pipe_mpeg12_dct_type coding
, int quantizer_scale
)
1235 bs
->ycbcr_stream
[cc
]->x
= x
;
1236 bs
->ycbcr_stream
[cc
]->y
= y
;
1237 bs
->ycbcr_stream
[cc
]->intra
= 0;
1238 bs
->ycbcr_stream
[cc
]->coding
= coding
;
1239 bs
->ycbcr_stream
[cc
]->block_num
= bs
->block_num
++;
1241 memset(dest
, 0, sizeof(int16_t) * 64);
1242 if (picture
->base
.profile
== PIPE_VIDEO_PROFILE_MPEG1
)
1243 get_mpeg1_non_intra_block(bs
, quantizer_scale
, dest
);
1245 get_non_intra_block(bs
, quantizer_scale
, dest
);
1247 memcpy(bs
->ycbcr_buffer
, dest
, sizeof(int16_t) * 64);
1249 bs
->num_ycbcr_blocks
[cc
]++;
1250 bs
->ycbcr_stream
[cc
]++;
1251 bs
->ycbcr_buffer
+= 64;
1255 motion_mp1(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct vl_motionvector
*mv
)
1257 int motion_x
, motion_y
;
1259 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1261 vl_vlc_needbits(&bs
->vlc
);
1262 motion_x
= (mv
->top
.x
+ (get_motion_delta(bs
, f_code
[0]) << f_code
[1]));
1263 motion_x
= bound_motion_vector (motion_x
, f_code
[0] + f_code
[1]);
1264 mv
->top
.x
= mv
->bottom
.x
= motion_x
;
1266 vl_vlc_needbits(&bs
->vlc
);
1267 motion_y
= (mv
->top
.y
+ (get_motion_delta(bs
, f_code
[0]) << f_code
[1]));
1268 motion_y
= bound_motion_vector (motion_y
, f_code
[0] + f_code
[1]);
1269 mv
->top
.y
= mv
->bottom
.y
= motion_y
;
1273 motion_fr_frame(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct vl_motionvector
*mv
)
1275 int motion_x
, motion_y
;
1277 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1279 vl_vlc_needbits(&bs
->vlc
);
1280 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1281 motion_x
= bound_motion_vector(motion_x
, f_code
[0]);
1282 mv
->top
.x
= mv
->bottom
.x
= motion_x
;
1284 vl_vlc_needbits(&bs
->vlc
);
1285 motion_y
= mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]);
1286 motion_y
= bound_motion_vector(motion_y
, f_code
[1]);
1287 mv
->top
.y
= mv
->bottom
.y
= motion_y
;
1291 motion_fr_field(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct vl_motionvector
*mv
)
1293 int motion_x
, motion_y
;
1295 vl_vlc_needbits(&bs
->vlc
);
1296 mv
->top
.field_select
= vl_vlc_ubits(&bs
->vlc
, 1) ?
1297 PIPE_VIDEO_BOTTOM_FIELD
: PIPE_VIDEO_TOP_FIELD
;
1298 vl_vlc_dumpbits(&bs
->vlc
, 1);
1300 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1301 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1302 mv
->top
.x
= motion_x
;
1304 vl_vlc_needbits(&bs
->vlc
);
1305 motion_y
= (mv
->top
.y
>> 1) + get_motion_delta(bs
, f_code
[1]);
1306 /* motion_y = bound_motion_vector (motion_y, f_code[1]); */
1307 mv
->top
.y
= motion_y
<< 1;
1309 vl_vlc_needbits(&bs
->vlc
);
1310 mv
->bottom
.field_select
= vl_vlc_ubits(&bs
->vlc
, 1) ?
1311 PIPE_VIDEO_BOTTOM_FIELD
: PIPE_VIDEO_TOP_FIELD
;
1312 vl_vlc_dumpbits(&bs
->vlc
, 1);
1314 motion_x
= mv
->bottom
.x
+ get_motion_delta(bs
, f_code
[0]);
1315 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1316 mv
->bottom
.x
= motion_x
;
1318 vl_vlc_needbits(&bs
->vlc
);
1319 motion_y
= (mv
->bottom
.y
>> 1) + get_motion_delta(bs
, f_code
[1]);
1320 /* motion_y = bound_motion_vector (motion_y, f_code[1]); */
1321 mv
->bottom
.y
= motion_y
<< 1;
1325 motion_fr_dmv(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct vl_motionvector
*mv
)
1327 int motion_x
, motion_y
;
1329 // TODO Implement dmv
1330 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1332 vl_vlc_needbits(&bs
->vlc
);
1333 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1334 motion_x
= bound_motion_vector(motion_x
, f_code
[0]);
1335 mv
->top
.x
= mv
->bottom
.x
= motion_x
;
1337 vl_vlc_needbits(&bs
->vlc
);
1338 motion_y
= (mv
->top
.y
>> 1) + get_motion_delta(bs
, f_code
[1]);
1339 /* motion_y = bound_motion_vector (motion_y, f_code[1]); */
1340 mv
->top
.y
= mv
->bottom
.y
= motion_y
<< 1;
1343 /* like motion_frame, but parsing without actual motion compensation */
1345 motion_fr_conceal(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct vl_motionvector
*mv
)
1349 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1351 vl_vlc_needbits(&bs
->vlc
);
1352 tmp
= (mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]));
1353 tmp
= bound_motion_vector (tmp
, f_code
[0]);
1354 mv
->top
.x
= mv
->bottom
.x
= tmp
;
1356 vl_vlc_needbits(&bs
->vlc
);
1357 tmp
= (mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]));
1358 tmp
= bound_motion_vector (tmp
, f_code
[1]);
1359 mv
->top
.y
= mv
->bottom
.y
= tmp
;
1361 vl_vlc_dumpbits(&bs
->vlc
, 1); /* remove marker_bit */
1365 motion_fi_field(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct vl_motionvector
*mv
)
1367 int motion_x
, motion_y
;
1369 vl_vlc_needbits(&bs
->vlc
);
1372 //vl_vlc_ubits(&bs->vlc, 1);
1374 // TODO field select may need to do something here for bob (weave ok)
1375 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1376 vl_vlc_dumpbits(&bs
->vlc
, 1);
1378 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1379 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1380 mv
->top
.x
= mv
->bottom
.x
= motion_x
;
1382 vl_vlc_needbits(&bs
->vlc
);
1383 motion_y
= mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]);
1384 motion_y
= bound_motion_vector (motion_y
, f_code
[1]);
1385 mv
->top
.y
= mv
->bottom
.y
= motion_y
;
1389 motion_fi_16x8(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct vl_motionvector
*mv
)
1391 int motion_x
, motion_y
;
1393 vl_vlc_needbits(&bs
->vlc
);
1396 //vl_vlc_ubits(&bs->vlc, 1);
1398 // TODO field select may need to do something here bob (weave ok)
1399 mv
->top
.field_select
= PIPE_VIDEO_FRAME
;
1400 vl_vlc_dumpbits(&bs
->vlc
, 1);
1402 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1403 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1404 mv
->top
.x
= motion_x
;
1406 vl_vlc_needbits(&bs
->vlc
);
1407 motion_y
= mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]);
1408 motion_y
= bound_motion_vector (motion_y
, f_code
[1]);
1409 mv
->top
.y
= motion_y
;
1411 vl_vlc_needbits(&bs
->vlc
);
1413 //vl_vlc_ubits(&bs->vlc, 1);
1415 // TODO field select may need to do something here for bob (weave ok)
1416 mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1417 vl_vlc_dumpbits(&bs
->vlc
, 1);
1419 motion_x
= mv
->bottom
.x
+ get_motion_delta(bs
, f_code
[0]);
1420 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1421 mv
->bottom
.x
= motion_x
;
1423 vl_vlc_needbits(&bs
->vlc
);
1424 motion_y
= mv
->bottom
.y
+ get_motion_delta(bs
, f_code
[1]);
1425 motion_y
= bound_motion_vector (motion_y
, f_code
[1]);
1426 mv
->bottom
.y
= motion_y
;
1430 motion_fi_dmv(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct vl_motionvector
*mv
)
1432 int motion_x
, motion_y
;
1434 // TODO field select may need to do something here for bob (weave ok)
1435 mv
->top
.field_select
= mv
->bottom
.field_select
= PIPE_VIDEO_FRAME
;
1437 vl_vlc_needbits(&bs
->vlc
);
1438 motion_x
= mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]);
1439 motion_x
= bound_motion_vector (motion_x
, f_code
[0]);
1440 mv
->top
.x
= mv
->bottom
.x
= motion_x
;
1442 vl_vlc_needbits(&bs
->vlc
);
1443 motion_y
= mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]);
1444 motion_y
= bound_motion_vector (motion_y
, f_code
[1]);
1445 mv
->top
.y
= mv
->bottom
.y
= motion_y
;
1450 motion_fi_conceal(struct vl_mpg12_bs
*bs
, unsigned f_code
[2], struct vl_motionvector
*mv
)
1454 vl_vlc_needbits(&bs
->vlc
);
1455 vl_vlc_dumpbits(&bs
->vlc
, 1); /* remove field_select */
1457 tmp
= (mv
->top
.x
+ get_motion_delta(bs
, f_code
[0]));
1458 tmp
= bound_motion_vector(tmp
, f_code
[0]);
1459 mv
->top
.x
= mv
->bottom
.x
= tmp
;
1461 vl_vlc_needbits(&bs
->vlc
);
1462 tmp
= (mv
->top
.y
+ get_motion_delta(bs
, f_code
[1]));
1463 tmp
= bound_motion_vector(tmp
, f_code
[1]);
1464 mv
->top
.y
= mv
->bottom
.y
= tmp
;
1466 vl_vlc_dumpbits(&bs
->vlc
, 1); /* remove marker_bit */
1469 #define MOTION_CALL(routine, macroblock_modes) \
1471 if ((macroblock_modes) & MACROBLOCK_MOTION_FORWARD) \
1472 routine(bs, picture->f_code[0], &mv_fwd); \
1473 if ((macroblock_modes) & MACROBLOCK_MOTION_BACKWARD) \
1474 routine(bs, picture->f_code[1], &mv_bwd); \
1478 store_motionvectors(struct vl_mpg12_bs
*bs
, unsigned *mv_pos
,
1479 struct vl_motionvector
*mv_fwd
,
1480 struct vl_motionvector
*mv_bwd
)
1482 bs
->mv_stream
[0][*mv_pos
].top
= mv_fwd
->top
;
1483 bs
->mv_stream
[0][*mv_pos
].bottom
=
1484 mv_fwd
->top
.field_select
== PIPE_VIDEO_FRAME
?
1485 mv_fwd
->top
: mv_fwd
->bottom
;
1487 bs
->mv_stream
[1][*mv_pos
].top
= mv_bwd
->top
;
1488 bs
->mv_stream
[1][*mv_pos
].bottom
=
1489 mv_bwd
->top
.field_select
== PIPE_VIDEO_FRAME
?
1490 mv_bwd
->top
: mv_bwd
->bottom
;
1496 slice_init(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
* picture
,
1497 int *quantizer_scale
, unsigned *x
, unsigned *y
, unsigned *mv_pos
)
1501 vl_vlc_need32bits(&bs
->vlc
);
1502 while(bs
->vlc
.buf
< 0x101 || bs
->vlc
.buf
> 0x1AF) {
1503 if(!vl_vlc_getbyte(&bs
->vlc
))
1506 *y
= (bs
->vlc
.buf
& 0xFF) - 1;
1507 vl_vlc_restart(&bs
->vlc
);
1509 *quantizer_scale
= get_quantizer_scale(bs
, picture
);
1511 /* ignore intra_slice and all the extra data */
1512 while (bs
->vlc
.buf
& 0x80000000) {
1513 vl_vlc_dumpbits(&bs
->vlc
, 9);
1514 vl_vlc_needbits(&bs
->vlc
);
1517 /* decode initial macroblock address increment */
1520 if (bs
->vlc
.buf
>= 0x08000000) {
1521 mba
= MBA_5
+ (vl_vlc_ubits(&bs
->vlc
, 6) - 2);
1523 } else if (bs
->vlc
.buf
>= 0x01800000) {
1524 mba
= MBA_11
+ (vl_vlc_ubits(&bs
->vlc
, 12) - 24);
1526 } else switch (vl_vlc_ubits(&bs
->vlc
, 12)) {
1527 case 8: /* macroblock_escape */
1529 vl_vlc_dumpbits(&bs
->vlc
, 11);
1530 vl_vlc_needbits(&bs
->vlc
);
1532 case 15: /* macroblock_stuffing (MPEG1 only) */
1533 bs
->vlc
.buf
&= 0xfffff;
1534 vl_vlc_dumpbits(&bs
->vlc
, 11);
1535 vl_vlc_needbits(&bs
->vlc
);
1537 default: /* error */
1541 vl_vlc_dumpbits(&bs
->vlc
, mba
->len
+ 1);
1544 while (*x
>= bs
->width
) {
1548 if (*y
> bs
->height
)
1551 *mv_pos
= *x
+ *y
* bs
->width
;
1557 decode_slice(struct vl_mpg12_bs
*bs
, struct pipe_mpeg12_picture_desc
*picture
)
1559 enum vl_field_select default_field_select
;
1560 struct vl_motionvector mv_fwd
, mv_bwd
;
1561 enum pipe_mpeg12_dct_type dct_type
;
1563 /* predictor for DC coefficients in intra blocks */
1564 int dc_dct_pred
[3] = { 0, 0, 0 };
1565 int quantizer_scale
;
1567 unsigned x
, y
, mv_pos
;
1569 switch(picture
->picture_structure
) {
1571 default_field_select
= PIPE_VIDEO_TOP_FIELD
;
1575 default_field_select
= PIPE_VIDEO_BOTTOM_FIELD
;
1579 default_field_select
= PIPE_VIDEO_FRAME
;
1583 if (!slice_init(bs
, picture
, &quantizer_scale
, &x
, &y
, &mv_pos
))
1586 mv_fwd
.top
.x
= mv_fwd
.top
.y
= mv_fwd
.bottom
.x
= mv_fwd
.bottom
.y
= 0;
1587 mv_fwd
.top
.field_select
= mv_fwd
.bottom
.field_select
= default_field_select
;
1589 mv_bwd
.top
.x
= mv_bwd
.top
.y
= mv_bwd
.bottom
.x
= mv_bwd
.bottom
.y
= 0;
1590 mv_bwd
.top
.field_select
= mv_bwd
.bottom
.field_select
= default_field_select
;
1593 int macroblock_modes
;
1597 vl_vlc_needbits(&bs
->vlc
);
1599 macroblock_modes
= get_macroblock_modes(bs
, picture
);
1600 dct_type
= get_dct_type(bs
, picture
, macroblock_modes
);
1602 switch(macroblock_modes
& (MACROBLOCK_MOTION_FORWARD
|MACROBLOCK_MOTION_BACKWARD
)) {
1603 case (MACROBLOCK_MOTION_FORWARD
|MACROBLOCK_MOTION_BACKWARD
):
1604 mv_fwd
.top
.weight
= mv_fwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_HALF
;
1605 mv_bwd
.top
.weight
= mv_bwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_HALF
;
1609 mv_fwd
.top
.field_select
= mv_fwd
.bottom
.field_select
= default_field_select
;
1610 mv_bwd
.top
.field_select
= mv_bwd
.bottom
.field_select
= default_field_select
;
1613 case MACROBLOCK_MOTION_FORWARD
:
1614 mv_fwd
.top
.weight
= mv_fwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MAX
;
1615 mv_bwd
.top
.weight
= mv_bwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1618 case MACROBLOCK_MOTION_BACKWARD
:
1619 mv_fwd
.top
.weight
= mv_fwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1620 mv_bwd
.top
.weight
= mv_bwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MAX
;
1624 /* maybe integrate MACROBLOCK_QUANT test into get_macroblock_modes ? */
1625 if (macroblock_modes
& MACROBLOCK_QUANT
)
1626 quantizer_scale
= get_quantizer_scale(bs
, picture
);
1628 if (macroblock_modes
& MACROBLOCK_INTRA
) {
1630 if (picture
->concealment_motion_vectors
) {
1631 if (picture
->picture_structure
== FRAME_PICTURE
)
1632 motion_fr_conceal(bs
, picture
->f_code
[0], &mv_fwd
);
1634 motion_fi_conceal(bs
, picture
->f_code
[0], &mv_fwd
);
1637 mv_fwd
.top
.x
= mv_fwd
.top
.y
= mv_fwd
.bottom
.x
= mv_fwd
.bottom
.y
= 0;
1638 mv_bwd
.top
.x
= mv_bwd
.top
.y
= mv_bwd
.bottom
.x
= mv_bwd
.bottom
.y
= 0;
1640 mv_fwd
.top
.weight
= mv_fwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1641 mv_bwd
.top
.weight
= mv_bwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1643 // unravaled loop of 6 block(i) calls in macroblock()
1644 slice_intra_DCT(bs
, picture
, 0, x
*2+0, y
*2+0, dct_type
, quantizer_scale
, dc_dct_pred
);
1645 slice_intra_DCT(bs
, picture
, 0, x
*2+1, y
*2+0, dct_type
, quantizer_scale
, dc_dct_pred
);
1646 slice_intra_DCT(bs
, picture
, 0, x
*2+0, y
*2+1, dct_type
, quantizer_scale
, dc_dct_pred
);
1647 slice_intra_DCT(bs
, picture
, 0, x
*2+1, y
*2+1, dct_type
, quantizer_scale
, dc_dct_pred
);
1648 slice_intra_DCT(bs
, picture
, 1, x
, y
, PIPE_MPEG12_DCT_TYPE_FRAME
, quantizer_scale
, dc_dct_pred
);
1649 slice_intra_DCT(bs
, picture
, 2, x
, y
, PIPE_MPEG12_DCT_TYPE_FRAME
, quantizer_scale
, dc_dct_pred
);
1651 if (picture
->picture_coding_type
== D_TYPE
) {
1652 vl_vlc_needbits(&bs
->vlc
);
1653 vl_vlc_dumpbits(&bs
->vlc
, 1);
1657 if (picture
->picture_structure
== FRAME_PICTURE
)
1658 switch (macroblock_modes
& MOTION_TYPE_MASK
) {
1660 if (picture
->base
.profile
== PIPE_VIDEO_PROFILE_MPEG1
) {
1661 MOTION_CALL(motion_mp1
, macroblock_modes
);
1663 MOTION_CALL(motion_fr_frame
, macroblock_modes
);
1668 MOTION_CALL (motion_fr_field
, macroblock_modes
);
1672 MOTION_CALL (motion_fr_dmv
, MACROBLOCK_MOTION_FORWARD
);
1676 /* non-intra mb without forward mv in a P picture */
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;
1682 switch (macroblock_modes
& MOTION_TYPE_MASK
) {
1684 MOTION_CALL (motion_fi_field
, macroblock_modes
);
1688 MOTION_CALL (motion_fi_16x8
, macroblock_modes
);
1692 MOTION_CALL (motion_fi_dmv
, MACROBLOCK_MOTION_FORWARD
);
1696 /* non-intra mb without forward mv in a P picture */
1697 mv_fwd
.top
.x
= mv_fwd
.top
.y
= mv_fwd
.bottom
.x
= mv_fwd
.bottom
.y
= 0;
1698 mv_bwd
.top
.x
= mv_bwd
.top
.y
= mv_bwd
.bottom
.x
= mv_bwd
.bottom
.y
= 0;
1702 if (macroblock_modes
& MACROBLOCK_PATTERN
) {
1703 int coded_block_pattern
= get_coded_block_pattern(bs
);
1705 // TODO optimize not fully used for idct accel only mc.
1706 if (coded_block_pattern
& 0x20)
1707 slice_non_intra_DCT(bs
, picture
, 0, x
*2+0, y
*2+0, dct_type
, quantizer_scale
); // cc0 luma 0
1708 if (coded_block_pattern
& 0x10)
1709 slice_non_intra_DCT(bs
, picture
, 0, x
*2+1, y
*2+0, dct_type
, quantizer_scale
); // cc0 luma 1
1710 if (coded_block_pattern
& 0x08)
1711 slice_non_intra_DCT(bs
, picture
, 0, x
*2+0, y
*2+1, dct_type
, quantizer_scale
); // cc0 luma 2
1712 if (coded_block_pattern
& 0x04)
1713 slice_non_intra_DCT(bs
, picture
, 0, x
*2+1, y
*2+1, dct_type
, quantizer_scale
); // cc0 luma 3
1714 if (coded_block_pattern
& 0x2)
1715 slice_non_intra_DCT(bs
, picture
, 1, x
, y
, PIPE_MPEG12_DCT_TYPE_FRAME
, quantizer_scale
); // cc1 croma
1716 if (coded_block_pattern
& 0x1)
1717 slice_non_intra_DCT(bs
, picture
, 2, x
, y
, PIPE_MPEG12_DCT_TYPE_FRAME
, quantizer_scale
); // cc2 croma
1720 dc_dct_pred
[0] = dc_dct_pred
[1] = dc_dct_pred
[2] = 0;
1723 store_motionvectors(bs
, &mv_pos
, &mv_fwd
, &mv_bwd
);
1724 if (++x
>= bs
->width
) {
1726 if (y
>= bs
->height
)
1731 vl_vlc_needbits(&bs
->vlc
);
1734 if (bs
->vlc
.buf
>= 0x10000000) {
1735 mba
= MBA_5
+ (vl_vlc_ubits(&bs
->vlc
, 5) - 2);
1737 } else if (bs
->vlc
.buf
>= 0x03000000) {
1738 mba
= MBA_11
+ (vl_vlc_ubits(&bs
->vlc
, 11) - 24);
1740 } else switch (vl_vlc_ubits(&bs
->vlc
, 11)) {
1741 case 8: /* macroblock_escape */
1744 case 15: /* macroblock_stuffing (MPEG1 only) */
1745 vl_vlc_dumpbits(&bs
->vlc
, 11);
1746 vl_vlc_needbits(&bs
->vlc
);
1748 default: /* end of slice, or error */
1752 vl_vlc_dumpbits(&bs
->vlc
, mba
->len
);
1753 mba_inc
+= mba
->mba
;
1755 //TODO conversion to signed format signed format
1756 dc_dct_pred
[0] = dc_dct_pred
[1] = dc_dct_pred
[2] = 0;
1758 mv_fwd
.top
.field_select
= mv_fwd
.bottom
.field_select
= default_field_select
;
1759 mv_bwd
.top
.field_select
= mv_bwd
.bottom
.field_select
= default_field_select
;
1761 if (picture
->picture_coding_type
== P_TYPE
) {
1762 mv_fwd
.top
.x
= mv_fwd
.top
.y
= mv_fwd
.bottom
.x
= mv_fwd
.bottom
.y
= 0;
1763 mv_fwd
.top
.weight
= mv_fwd
.bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MAX
;
1768 store_motionvectors(bs
, &mv_pos
, &mv_fwd
, &mv_bwd
);
1769 } while (--mba_inc
);
1771 while (x
>= bs
->width
) {
1773 if (y
>= bs
->height
)
1781 vl_mpg12_bs_init(struct vl_mpg12_bs
*bs
, unsigned width
, unsigned height
)
1785 memset(bs
, 0, sizeof(struct vl_mpg12_bs
));
1788 bs
->height
= height
;
1792 vl_mpg12_bs_set_buffers(struct vl_mpg12_bs
*bs
, struct vl_ycbcr_block
*ycbcr_stream
[VL_MAX_PLANES
],
1793 short *ycbcr_buffer
, struct vl_motionvector
*mv_stream
[VL_MAX_REF_FRAMES
])
1798 assert(ycbcr_stream
&& ycbcr_buffer
);
1803 for (i
= 0; i
< VL_MAX_PLANES
; ++i
)
1804 bs
->ycbcr_stream
[i
] = ycbcr_stream
[i
];
1805 bs
->ycbcr_buffer
= ycbcr_buffer
;
1807 for (i
= 0; i
< VL_MAX_REF_FRAMES
; ++i
)
1808 bs
->mv_stream
[i
] = mv_stream
[i
];
1811 for (i
= 0; i
< bs
->width
*bs
->height
; ++i
) {
1812 bs
->mv_stream
[0][i
].top
.x
= bs
->mv_stream
[0][i
].top
.y
= 0;
1813 bs
->mv_stream
[0][i
].top
.field_select
= PIPE_VIDEO_FRAME
;
1814 bs
->mv_stream
[0][i
].top
.weight
= PIPE_VIDEO_MV_WEIGHT_MAX
;
1815 bs
->mv_stream
[0][i
].bottom
.x
= bs
->mv_stream
[0][i
].bottom
.y
= 0;
1816 bs
->mv_stream
[0][i
].bottom
.field_select
= PIPE_VIDEO_FRAME
;
1817 bs
->mv_stream
[0][i
].bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MAX
;
1819 bs
->mv_stream
[1][i
].top
.x
= bs
->mv_stream
[1][i
].top
.y
= 0;
1820 bs
->mv_stream
[1][i
].top
.field_select
= PIPE_VIDEO_FRAME
;
1821 bs
->mv_stream
[1][i
].top
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1822 bs
->mv_stream
[1][i
].bottom
.x
= bs
->mv_stream
[1][i
].bottom
.y
= 0;
1823 bs
->mv_stream
[1][i
].bottom
.field_select
= PIPE_VIDEO_FRAME
;
1824 bs
->mv_stream
[1][i
].bottom
.weight
= PIPE_VIDEO_MV_WEIGHT_MIN
;
1829 vl_mpg12_bs_decode(struct vl_mpg12_bs
*bs
, unsigned num_bytes
, const void *buffer
,
1830 struct pipe_mpeg12_picture_desc
*picture
, unsigned num_ycbcr_blocks
[3])
1833 assert(num_ycbcr_blocks
);
1834 assert(buffer
&& num_bytes
);
1836 bs
->num_ycbcr_blocks
= num_ycbcr_blocks
;
1838 vl_vlc_init(&bs
->vlc
, buffer
, num_bytes
);
1840 while(decode_slice(bs
, picture
));