vl_mpeg12_bitstream.c revision e3789105fe3a289338821a53da499857aa924637
1/**************************************************************************
2 *
3 * Copyright 2011 Christian König.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28/**
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:
31 *
32 * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
33 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
34 *
35 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
36 * See http://libmpeg2.sourceforge.net/ for updates.
37 *
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.
42 *
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.
47 *
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
51 */
52
53#include <stdint.h>
54
55#include <pipe/p_video_state.h>
56
57#include "vl_vlc.h"
58#include "vl_mpeg12_bitstream.h"
59
60/* take num bits from the high part of bit_buf and zero extend them */
61#define UBITS(buf,num) (((uint32_t)(buf)) >> (32 - (num)))
62
63/* take num bits from the high part of bit_buf and sign extend them */
64#define SBITS(buf,num) (((int32_t)(buf)) >> (32 - (num)))
65
66#define SATURATE(val)			\
67do {					\
68   if ((uint32_t)(val + 2048) > 4095)	\
69      val = (val > 0) ? 2047 : -2048;	\
70} while (0)
71
72/* macroblock modes */
73#define MACROBLOCK_INTRA 1
74#define MACROBLOCK_PATTERN 2
75#define MACROBLOCK_MOTION_BACKWARD 4
76#define MACROBLOCK_MOTION_FORWARD 8
77#define MACROBLOCK_QUANT 16
78#define DCT_TYPE_INTERLACED 32
79
80/* motion_type */
81#define MOTION_TYPE_MASK (3*64)
82#define MOTION_TYPE_BASE 64
83#define MC_FIELD (1*64)
84#define MC_FRAME (2*64)
85#define MC_16X8 (2*64)
86#define MC_DMV (3*64)
87
88/* picture structure */
89#define TOP_FIELD     1
90#define BOTTOM_FIELD  2
91#define FRAME_PICTURE 3
92
93/* picture coding type (mpeg2 header) */
94#define I_TYPE 1
95#define P_TYPE 2
96#define B_TYPE 3
97#define D_TYPE 4
98
99typedef struct {
100   uint8_t modes;
101   uint8_t len;
102} MBtab;
103
104typedef struct {
105   uint8_t delta;
106   uint8_t len;
107} MVtab;
108
109typedef struct {
110   int8_t dmv;
111   uint8_t len;
112} DMVtab;
113
114typedef struct {
115   uint8_t cbp;
116   uint8_t len;
117} CBPtab;
118
119typedef struct {
120   uint8_t size;
121   uint8_t len;
122} DCtab;
123
124typedef struct {
125   uint8_t run;
126   uint8_t level;
127   uint8_t len;
128} DCTtab;
129
130typedef struct {
131   uint8_t mba;
132   uint8_t len;
133} MBAtab;
134
135#define INTRA MACROBLOCK_INTRA
136#define QUANT MACROBLOCK_QUANT
137#define MC MACROBLOCK_MOTION_FORWARD
138#define CODED MACROBLOCK_PATTERN
139#define FWD MACROBLOCK_MOTION_FORWARD
140#define BWD MACROBLOCK_MOTION_BACKWARD
141#define INTER MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD
142
143static const MBtab MB_I [] = {
144   {INTRA|QUANT, 2}, {INTRA, 1}
145};
146
147static const MBtab MB_P [] = {
148   {INTRA|QUANT, 6}, {CODED|QUANT, 5}, {MC|CODED|QUANT, 5}, {INTRA,    5},
149   {MC,          3}, {MC,          3}, {MC,             3}, {MC,       3},
150   {CODED,       2}, {CODED,       2}, {CODED,          2}, {CODED,    2},
151   {CODED,       2}, {CODED,       2}, {CODED,          2}, {CODED,    2},
152   {MC|CODED,    1}, {MC|CODED,    1}, {MC|CODED,       1}, {MC|CODED, 1},
153   {MC|CODED,    1}, {MC|CODED,    1}, {MC|CODED,       1}, {MC|CODED, 1},
154   {MC|CODED,    1}, {MC|CODED,    1}, {MC|CODED,       1}, {MC|CODED, 1},
155   {MC|CODED,    1}, {MC|CODED,    1}, {MC|CODED,       1}, {MC|CODED, 1}
156};
157
158static const MBtab MB_B [] = {
159   {0,                 0}, {INTRA|QUANT,       6},
160   {BWD|CODED|QUANT,   6}, {FWD|CODED|QUANT,   6},
161   {INTER|CODED|QUANT, 5}, {INTER|CODED|QUANT, 5},
162                                     {INTRA,       5}, {INTRA,       5},
163   {FWD,         4}, {FWD,         4}, {FWD,         4}, {FWD,         4},
164   {FWD|CODED,   4}, {FWD|CODED,   4}, {FWD|CODED,   4}, {FWD|CODED,   4},
165   {BWD,         3}, {BWD,         3}, {BWD,         3}, {BWD,         3},
166   {BWD,         3}, {BWD,         3}, {BWD,         3}, {BWD,         3},
167   {BWD|CODED,   3}, {BWD|CODED,   3}, {BWD|CODED,   3}, {BWD|CODED,   3},
168   {BWD|CODED,   3}, {BWD|CODED,   3}, {BWD|CODED,   3}, {BWD|CODED,   3},
169   {INTER,       2}, {INTER,       2}, {INTER,       2}, {INTER,       2},
170   {INTER,       2}, {INTER,       2}, {INTER,       2}, {INTER,       2},
171   {INTER,       2}, {INTER,       2}, {INTER,       2}, {INTER,       2},
172   {INTER,       2}, {INTER,       2}, {INTER,       2}, {INTER,       2},
173   {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2},
174   {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2},
175   {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2},
176   {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}, {INTER|CODED, 2}
177};
178
179#undef INTRA
180#undef QUANT
181#undef MC
182#undef CODED
183#undef FWD
184#undef BWD
185#undef INTER
186
187static const MVtab MV_4 [] = {
188   { 3, 6}, { 2, 4}, { 1, 3}, { 1, 3}, { 0, 2}, { 0, 2}, { 0, 2}, { 0, 2}
189};
190
191static const MVtab MV_10 [] = {
192   { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10}, { 0,10},
193   { 0,10}, { 0,10}, { 0,10}, { 0,10}, {15,10}, {14,10}, {13,10}, {12,10},
194   {11,10}, {10,10}, { 9, 9}, { 9, 9}, { 8, 9}, { 8, 9}, { 7, 9}, { 7, 9},
195   { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7}, { 6, 7},
196   { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7}, { 5, 7},
197   { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}, { 4, 7}
198};
199
200static const DMVtab DMV_2 [] = {
201   { 0, 1}, { 0, 1}, { 1, 2}, {-1, 2}
202};
203
204static const CBPtab CBP_7 [] = {
205   {0x22, 7}, {0x12, 7}, {0x0a, 7}, {0x06, 7},
206   {0x21, 7}, {0x11, 7}, {0x09, 7}, {0x05, 7},
207   {0x3f, 6}, {0x3f, 6}, {0x03, 6}, {0x03, 6},
208   {0x24, 6}, {0x24, 6}, {0x18, 6}, {0x18, 6},
209   {0x3e, 5}, {0x3e, 5}, {0x3e, 5}, {0x3e, 5},
210   {0x02, 5}, {0x02, 5}, {0x02, 5}, {0x02, 5},
211   {0x3d, 5}, {0x3d, 5}, {0x3d, 5}, {0x3d, 5},
212   {0x01, 5}, {0x01, 5}, {0x01, 5}, {0x01, 5},
213   {0x38, 5}, {0x38, 5}, {0x38, 5}, {0x38, 5},
214   {0x34, 5}, {0x34, 5}, {0x34, 5}, {0x34, 5},
215   {0x2c, 5}, {0x2c, 5}, {0x2c, 5}, {0x2c, 5},
216   {0x1c, 5}, {0x1c, 5}, {0x1c, 5}, {0x1c, 5},
217   {0x28, 5}, {0x28, 5}, {0x28, 5}, {0x28, 5},
218   {0x14, 5}, {0x14, 5}, {0x14, 5}, {0x14, 5},
219   {0x30, 5}, {0x30, 5}, {0x30, 5}, {0x30, 5},
220   {0x0c, 5}, {0x0c, 5}, {0x0c, 5}, {0x0c, 5},
221   {0x20, 4}, {0x20, 4}, {0x20, 4}, {0x20, 4},
222   {0x20, 4}, {0x20, 4}, {0x20, 4}, {0x20, 4},
223   {0x10, 4}, {0x10, 4}, {0x10, 4}, {0x10, 4},
224   {0x10, 4}, {0x10, 4}, {0x10, 4}, {0x10, 4},
225   {0x08, 4}, {0x08, 4}, {0x08, 4}, {0x08, 4},
226   {0x08, 4}, {0x08, 4}, {0x08, 4}, {0x08, 4},
227   {0x04, 4}, {0x04, 4}, {0x04, 4}, {0x04, 4},
228   {0x04, 4}, {0x04, 4}, {0x04, 4}, {0x04, 4},
229   {0x3c, 3}, {0x3c, 3}, {0x3c, 3}, {0x3c, 3},
230   {0x3c, 3}, {0x3c, 3}, {0x3c, 3}, {0x3c, 3},
231   {0x3c, 3}, {0x3c, 3}, {0x3c, 3}, {0x3c, 3},
232   {0x3c, 3}, {0x3c, 3}, {0x3c, 3}, {0x3c, 3}
233};
234
235static const CBPtab CBP_9 [] = {
236   {0,    0}, {0x00, 9}, {0x27, 9}, {0x1b, 9},
237   {0x3b, 9}, {0x37, 9}, {0x2f, 9}, {0x1f, 9},
238   {0x3a, 8}, {0x3a, 8}, {0x36, 8}, {0x36, 8},
239   {0x2e, 8}, {0x2e, 8}, {0x1e, 8}, {0x1e, 8},
240   {0x39, 8}, {0x39, 8}, {0x35, 8}, {0x35, 8},
241   {0x2d, 8}, {0x2d, 8}, {0x1d, 8}, {0x1d, 8},
242   {0x26, 8}, {0x26, 8}, {0x1a, 8}, {0x1a, 8},
243   {0x25, 8}, {0x25, 8}, {0x19, 8}, {0x19, 8},
244   {0x2b, 8}, {0x2b, 8}, {0x17, 8}, {0x17, 8},
245   {0x33, 8}, {0x33, 8}, {0x0f, 8}, {0x0f, 8},
246   {0x2a, 8}, {0x2a, 8}, {0x16, 8}, {0x16, 8},
247   {0x32, 8}, {0x32, 8}, {0x0e, 8}, {0x0e, 8},
248   {0x29, 8}, {0x29, 8}, {0x15, 8}, {0x15, 8},
249   {0x31, 8}, {0x31, 8}, {0x0d, 8}, {0x0d, 8},
250   {0x23, 8}, {0x23, 8}, {0x13, 8}, {0x13, 8},
251   {0x0b, 8}, {0x0b, 8}, {0x07, 8}, {0x07, 8}
252};
253
254static const DCtab DC_lum_5 [] = {
255   {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
256   {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
257   {0, 3}, {0, 3}, {0, 3}, {0, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3},
258   {4, 3}, {4, 3}, {4, 3}, {4, 3}, {5, 4}, {5, 4}, {6, 5}
259};
260
261static const DCtab DC_chrom_5 [] = {
262   {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2},
263   {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
264   {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
265   {3, 3}, {3, 3}, {3, 3}, {3, 3}, {4, 4}, {4, 4}, {5, 5}
266};
267
268static const DCtab DC_long [] = {
269   {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, { 6, 5}, { 6, 5},
270   {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, {6, 5}, { 6, 5}, { 6, 5},
271   {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, { 7, 6}, { 7, 6},
272   {8, 7}, {8, 7}, {8, 7}, {8, 7}, {9, 8}, {9, 8}, {10, 9}, {11, 9}
273};
274
275static const DCTtab DCT_16 [] = {
276   {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0},
277   {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0},
278   {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0},
279   {129, 0, 0}, {129, 0, 0}, {129, 0, 0}, {129, 0, 0},
280   {  2,18, 0}, {  2,17, 0}, {  2,16, 0}, {  2,15, 0},
281   {  7, 3, 0}, { 17, 2, 0}, { 16, 2, 0}, { 15, 2, 0},
282   { 14, 2, 0}, { 13, 2, 0}, { 12, 2, 0}, { 32, 1, 0},
283   { 31, 1, 0}, { 30, 1, 0}, { 29, 1, 0}, { 28, 1, 0}
284};
285
286static const DCTtab DCT_15 [] = {
287   {  1,40,15}, {  1,39,15}, {  1,38,15}, {  1,37,15},
288   {  1,36,15}, {  1,35,15}, {  1,34,15}, {  1,33,15},
289   {  1,32,15}, {  2,14,15}, {  2,13,15}, {  2,12,15},
290   {  2,11,15}, {  2,10,15}, {  2, 9,15}, {  2, 8,15},
291   {  1,31,14}, {  1,31,14}, {  1,30,14}, {  1,30,14},
292   {  1,29,14}, {  1,29,14}, {  1,28,14}, {  1,28,14},
293   {  1,27,14}, {  1,27,14}, {  1,26,14}, {  1,26,14},
294   {  1,25,14}, {  1,25,14}, {  1,24,14}, {  1,24,14},
295   {  1,23,14}, {  1,23,14}, {  1,22,14}, {  1,22,14},
296   {  1,21,14}, {  1,21,14}, {  1,20,14}, {  1,20,14},
297   {  1,19,14}, {  1,19,14}, {  1,18,14}, {  1,18,14},
298   {  1,17,14}, {  1,17,14}, {  1,16,14}, {  1,16,14}
299};
300
301static const DCTtab DCT_13 [] = {
302   { 11, 2,13}, { 10, 2,13}, {  6, 3,13}, {  4, 4,13},
303   {  3, 5,13}, {  2, 7,13}, {  2, 6,13}, {  1,15,13},
304   {  1,14,13}, {  1,13,13}, {  1,12,13}, { 27, 1,13},
305   { 26, 1,13}, { 25, 1,13}, { 24, 1,13}, { 23, 1,13},
306   {  1,11,12}, {  1,11,12}, {  9, 2,12}, {  9, 2,12},
307   {  5, 3,12}, {  5, 3,12}, {  1,10,12}, {  1,10,12},
308   {  3, 4,12}, {  3, 4,12}, {  8, 2,12}, {  8, 2,12},
309   { 22, 1,12}, { 22, 1,12}, { 21, 1,12}, { 21, 1,12},
310   {  1, 9,12}, {  1, 9,12}, { 20, 1,12}, { 20, 1,12},
311   { 19, 1,12}, { 19, 1,12}, {  2, 5,12}, {  2, 5,12},
312   {  4, 3,12}, {  4, 3,12}, {  1, 8,12}, {  1, 8,12},
313   {  7, 2,12}, {  7, 2,12}, { 18, 1,12}, { 18, 1,12}
314};
315
316static const DCTtab DCT_B14_10 [] = {
317   { 17, 1,10}, {  6, 2,10}, {  1, 7,10}, {  3, 3,10},
318   {  2, 4,10}, { 16, 1,10}, { 15, 1,10}, {  5, 2,10}
319};
320
321static const DCTtab DCT_B14_8 [] = {
322   { 65, 0, 6}, { 65, 0, 6}, { 65, 0, 6}, { 65, 0, 6},
323   {  3, 2, 7}, {  3, 2, 7}, { 10, 1, 7}, { 10, 1, 7},
324   {  1, 4, 7}, {  1, 4, 7}, {  9, 1, 7}, {  9, 1, 7},
325   {  8, 1, 6}, {  8, 1, 6}, {  8, 1, 6}, {  8, 1, 6},
326   {  7, 1, 6}, {  7, 1, 6}, {  7, 1, 6}, {  7, 1, 6},
327   {  2, 2, 6}, {  2, 2, 6}, {  2, 2, 6}, {  2, 2, 6},
328   {  6, 1, 6}, {  6, 1, 6}, {  6, 1, 6}, {  6, 1, 6},
329   { 14, 1, 8}, {  1, 6, 8}, { 13, 1, 8}, { 12, 1, 8},
330   {  4, 2, 8}, {  2, 3, 8}, {  1, 5, 8}, { 11, 1, 8}
331};
332
333static const DCTtab DCT_B14AC_5 [] = {
334                {  1, 3, 5}, {  5, 1, 5}, {  4, 1, 5},
335   {  1, 2, 4}, {  1, 2, 4}, {  3, 1, 4}, {  3, 1, 4},
336   {  2, 1, 3}, {  2, 1, 3}, {  2, 1, 3}, {  2, 1, 3},
337   {129, 0, 2}, {129, 0, 2}, {129, 0, 2}, {129, 0, 2},
338   {129, 0, 2}, {129, 0, 2}, {129, 0, 2}, {129, 0, 2},
339   {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2},
340   {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2}
341};
342
343static const DCTtab DCT_B14DC_5 [] = {
344                {  1, 3, 5}, {  5, 1, 5}, {  4, 1, 5},
345   {  1, 2, 4}, {  1, 2, 4}, {  3, 1, 4}, {  3, 1, 4},
346   {  2, 1, 3}, {  2, 1, 3}, {  2, 1, 3}, {  2, 1, 3},
347   {  1, 1, 1}, {  1, 1, 1}, {  1, 1, 1}, {  1, 1, 1},
348   {  1, 1, 1}, {  1, 1, 1}, {  1, 1, 1}, {  1, 1, 1},
349   {  1, 1, 1}, {  1, 1, 1}, {  1, 1, 1}, {  1, 1, 1},
350   {  1, 1, 1}, {  1, 1, 1}, {  1, 1, 1}, {  1, 1, 1}
351};
352
353static const DCTtab DCT_B15_10 [] = {
354   {  6, 2, 9}, {  6, 2, 9}, { 15, 1, 9}, { 15, 1, 9},
355   {  3, 4,10}, { 17, 1,10}, { 16, 1, 9}, { 16, 1, 9}
356};
357
358static const DCTtab DCT_B15_8 [] = {
359   { 65, 0, 6}, { 65, 0, 6}, { 65, 0, 6}, { 65, 0, 6},
360   {  8, 1, 7}, {  8, 1, 7}, {  9, 1, 7}, {  9, 1, 7},
361   {  7, 1, 7}, {  7, 1, 7}, {  3, 2, 7}, {  3, 2, 7},
362   {  1, 7, 6}, {  1, 7, 6}, {  1, 7, 6}, {  1, 7, 6},
363   {  1, 6, 6}, {  1, 6, 6}, {  1, 6, 6}, {  1, 6, 6},
364   {  5, 1, 6}, {  5, 1, 6}, {  5, 1, 6}, {  5, 1, 6},
365   {  6, 1, 6}, {  6, 1, 6}, {  6, 1, 6}, {  6, 1, 6},
366   {  2, 5, 8}, { 12, 1, 8}, {  1,11, 8}, {  1,10, 8},
367   { 14, 1, 8}, { 13, 1, 8}, {  4, 2, 8}, {  2, 4, 8},
368   {  3, 1, 5}, {  3, 1, 5}, {  3, 1, 5}, {  3, 1, 5},
369   {  3, 1, 5}, {  3, 1, 5}, {  3, 1, 5}, {  3, 1, 5},
370   {  2, 2, 5}, {  2, 2, 5}, {  2, 2, 5}, {  2, 2, 5},
371   {  2, 2, 5}, {  2, 2, 5}, {  2, 2, 5}, {  2, 2, 5},
372   {  4, 1, 5}, {  4, 1, 5}, {  4, 1, 5}, {  4, 1, 5},
373   {  4, 1, 5}, {  4, 1, 5}, {  4, 1, 5}, {  4, 1, 5},
374   {  2, 1, 3}, {  2, 1, 3}, {  2, 1, 3}, {  2, 1, 3},
375   {  2, 1, 3}, {  2, 1, 3}, {  2, 1, 3}, {  2, 1, 3},
376   {  2, 1, 3}, {  2, 1, 3}, {  2, 1, 3}, {  2, 1, 3},
377   {  2, 1, 3}, {  2, 1, 3}, {  2, 1, 3}, {  2, 1, 3},
378   {  2, 1, 3}, {  2, 1, 3}, {  2, 1, 3}, {  2, 1, 3},
379   {  2, 1, 3}, {  2, 1, 3}, {  2, 1, 3}, {  2, 1, 3},
380   {  2, 1, 3}, {  2, 1, 3}, {  2, 1, 3}, {  2, 1, 3},
381   {  2, 1, 3}, {  2, 1, 3}, {  2, 1, 3}, {  2, 1, 3},
382   {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4},
383   {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4},
384   {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4},
385   {129, 0, 4}, {129, 0, 4}, {129, 0, 4}, {129, 0, 4},
386   {  1, 3, 4}, {  1, 3, 4}, {  1, 3, 4}, {  1, 3, 4},
387   {  1, 3, 4}, {  1, 3, 4}, {  1, 3, 4}, {  1, 3, 4},
388   {  1, 3, 4}, {  1, 3, 4}, {  1, 3, 4}, {  1, 3, 4},
389   {  1, 3, 4}, {  1, 3, 4}, {  1, 3, 4}, {  1, 3, 4},
390   {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2},
391   {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2},
392   {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2},
393   {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2},
394   {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2},
395   {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2},
396   {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2},
397   {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2},
398   {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2},
399   {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2},
400   {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2},
401   {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2},
402   {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2},
403   {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2},
404   {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2},
405   {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2}, {  1, 1, 2},
406   {  1, 2, 3}, {  1, 2, 3}, {  1, 2, 3}, {  1, 2, 3},
407   {  1, 2, 3}, {  1, 2, 3}, {  1, 2, 3}, {  1, 2, 3},
408   {  1, 2, 3}, {  1, 2, 3}, {  1, 2, 3}, {  1, 2, 3},
409   {  1, 2, 3}, {  1, 2, 3}, {  1, 2, 3}, {  1, 2, 3},
410   {  1, 2, 3}, {  1, 2, 3}, {  1, 2, 3}, {  1, 2, 3},
411   {  1, 2, 3}, {  1, 2, 3}, {  1, 2, 3}, {  1, 2, 3},
412   {  1, 2, 3}, {  1, 2, 3}, {  1, 2, 3}, {  1, 2, 3},
413   {  1, 2, 3}, {  1, 2, 3}, {  1, 2, 3}, {  1, 2, 3},
414   {  1, 4, 5}, {  1, 4, 5}, {  1, 4, 5}, {  1, 4, 5},
415   {  1, 4, 5}, {  1, 4, 5}, {  1, 4, 5}, {  1, 4, 5},
416   {  1, 5, 5}, {  1, 5, 5}, {  1, 5, 5}, {  1, 5, 5},
417   {  1, 5, 5}, {  1, 5, 5}, {  1, 5, 5}, {  1, 5, 5},
418   { 10, 1, 7}, { 10, 1, 7}, {  2, 3, 7}, {  2, 3, 7},
419   { 11, 1, 7}, { 11, 1, 7}, {  1, 8, 7}, {  1, 8, 7},
420   {  1, 9, 7}, {  1, 9, 7}, {  1,12, 8}, {  1,13, 8},
421   {  3, 3, 8}, {  5, 2, 8}, {  1,14, 8}, {  1,15, 8}
422};
423
424static const MBAtab MBA_5 [] = {
425                   {6, 5}, {5, 5}, {4, 4}, {4, 4}, {3, 4}, {3, 4},
426   {2, 3}, {2, 3}, {2, 3}, {2, 3}, {1, 3}, {1, 3}, {1, 3}, {1, 3},
427   {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1},
428   {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}
429};
430
431static const MBAtab MBA_11 [] = {
432   {32, 11}, {31, 11}, {30, 11}, {29, 11},
433   {28, 11}, {27, 11}, {26, 11}, {25, 11},
434   {24, 11}, {23, 11}, {22, 11}, {21, 11},
435   {20, 10}, {20, 10}, {19, 10}, {19, 10},
436   {18, 10}, {18, 10}, {17, 10}, {17, 10},
437   {16, 10}, {16, 10}, {15, 10}, {15, 10},
438   {14,  8}, {14,  8}, {14,  8}, {14,  8},
439   {14,  8}, {14,  8}, {14,  8}, {14,  8},
440   {13,  8}, {13,  8}, {13,  8}, {13,  8},
441   {13,  8}, {13,  8}, {13,  8}, {13,  8},
442   {12,  8}, {12,  8}, {12,  8}, {12,  8},
443   {12,  8}, {12,  8}, {12,  8}, {12,  8},
444   {11,  8}, {11,  8}, {11,  8}, {11,  8},
445   {11,  8}, {11,  8}, {11,  8}, {11,  8},
446   {10,  8}, {10,  8}, {10,  8}, {10,  8},
447   {10,  8}, {10,  8}, {10,  8}, {10,  8},
448   { 9,  8}, { 9,  8}, { 9,  8}, { 9,  8},
449   { 9,  8}, { 9,  8}, { 9,  8}, { 9,  8},
450   { 8,  7}, { 8,  7}, { 8,  7}, { 8,  7},
451   { 8,  7}, { 8,  7}, { 8,  7}, { 8,  7},
452   { 8,  7}, { 8,  7}, { 8,  7}, { 8,  7},
453   { 8,  7}, { 8,  7}, { 8,  7}, { 8,  7},
454   { 7,  7}, { 7,  7}, { 7,  7}, { 7,  7},
455   { 7,  7}, { 7,  7}, { 7,  7}, { 7,  7},
456   { 7,  7}, { 7,  7}, { 7,  7}, { 7,  7},
457   { 7,  7}, { 7,  7}, { 7,  7}, { 7,  7}
458};
459
460/* original (non-patched) scan tables */
461static const uint8_t mpeg2_scan_norm_orig[64] =
462{
463   /* Zig-Zag scan pattern */
464    0, 1, 8,16, 9, 2, 3,10,
465   17,24,32,25,18,11, 4, 5,
466   12,19,26,33,40,48,41,34,
467   27,20,13, 6, 7,14,21,28,
468   35,42,49,56,57,50,43,36,
469   29,22,15,23,30,37,44,51,
470   58,59,52,45,38,31,39,46,
471   53,60,61,54,47,55,62,63
472};
473
474static const uint8_t mpeg2_scan_alt_orig[64] =
475{
476   /* Alternate scan pattern */
477   0,8,16,24,1,9,2,10,17,25,32,40,48,56,57,49,
478   41,33,26,18,3,11,4,12,19,27,34,42,50,58,35,43,
479   51,59,20,28,5,13,6,14,21,29,36,44,52,60,37,45,
480   53,61,22,30,7,15,23,31,38,46,54,62,39,47,55,63
481};
482
483static const int non_linear_quantizer_scale[] = {
484   0,  1,  2,  3,  4,  5,   6,   7,
485   8, 10, 12, 14, 16, 18,  20,  22,
486   24, 28, 32, 36, 40, 44,  48,  52,
487   56, 64, 72, 80, 88, 96, 104, 112
488};
489
490static inline int
491get_macroblock_modes(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture)
492{
493   int macroblock_modes;
494   const MBtab * tab;
495
496   switch (picture->picture_coding_type) {
497   case I_TYPE:
498
499      tab = MB_I + vl_vlc_ubits(&bs->vlc, 1);
500      vl_vlc_dumpbits(&bs->vlc, tab->len);
501      macroblock_modes = tab->modes;
502
503      if ((!(picture->frame_pred_frame_dct)) && (picture->picture_structure == FRAME_PICTURE)) {
504         macroblock_modes |= vl_vlc_ubits(&bs->vlc, 1) * DCT_TYPE_INTERLACED;
505         vl_vlc_dumpbits(&bs->vlc, 1);
506      }
507
508      return macroblock_modes;
509
510   case P_TYPE:
511
512      tab = MB_P + vl_vlc_ubits(&bs->vlc, 5);
513      vl_vlc_dumpbits(&bs->vlc, tab->len);
514      macroblock_modes = tab->modes;
515
516      if (picture->picture_structure != FRAME_PICTURE) {
517         if (macroblock_modes & MACROBLOCK_MOTION_FORWARD) {
518            macroblock_modes |= vl_vlc_ubits(&bs->vlc, 2) * MOTION_TYPE_BASE;
519            vl_vlc_dumpbits(&bs->vlc, 2);
520          }
521          return macroblock_modes;
522      } else if (picture->frame_pred_frame_dct) {
523          if (macroblock_modes & MACROBLOCK_MOTION_FORWARD)
524            macroblock_modes |= MC_FRAME;
525          return macroblock_modes;
526      } else {
527          if (macroblock_modes & MACROBLOCK_MOTION_FORWARD) {
528            macroblock_modes |= vl_vlc_ubits(&bs->vlc, 2) * MOTION_TYPE_BASE;
529            vl_vlc_dumpbits(&bs->vlc, 2);
530          }
531          if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN)) {
532            macroblock_modes |= vl_vlc_ubits(&bs->vlc, 1) * DCT_TYPE_INTERLACED;
533            vl_vlc_dumpbits(&bs->vlc, 1);
534          }
535          return macroblock_modes;
536      }
537
538   case B_TYPE:
539
540      tab = MB_B + vl_vlc_ubits(&bs->vlc, 6);
541      vl_vlc_dumpbits(&bs->vlc, tab->len);
542      macroblock_modes = tab->modes;
543
544      if (picture->picture_structure != FRAME_PICTURE) {
545          if (! (macroblock_modes & MACROBLOCK_INTRA)) {
546            macroblock_modes |= vl_vlc_ubits(&bs->vlc, 2) * MOTION_TYPE_BASE;
547            vl_vlc_dumpbits(&bs->vlc, 2);
548          }
549          return macroblock_modes;
550      } else if (picture->frame_pred_frame_dct) {
551          /* if (! (macroblock_modes & MACROBLOCK_INTRA)) */
552          macroblock_modes |= MC_FRAME;
553          return macroblock_modes;
554      } else {
555          if (macroblock_modes & MACROBLOCK_INTRA)
556            goto intra;
557          macroblock_modes |= vl_vlc_ubits(&bs->vlc, 2) * MOTION_TYPE_BASE;
558          vl_vlc_dumpbits(&bs->vlc, 2);
559          if (macroblock_modes & (MACROBLOCK_INTRA | MACROBLOCK_PATTERN)) {
560          intra:
561            macroblock_modes |= vl_vlc_ubits(&bs->vlc, 1) * DCT_TYPE_INTERLACED;
562            vl_vlc_dumpbits(&bs->vlc, 1);
563          }
564          return macroblock_modes;
565      }
566
567   case D_TYPE:
568
569      vl_vlc_dumpbits(&bs->vlc, 1);
570      return MACROBLOCK_INTRA;
571
572   default:
573      return 0;
574   }
575}
576
577static inline int
578get_quantizer_scale(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture)
579{
580   int quantizer_scale_code;
581
582   quantizer_scale_code = vl_vlc_ubits(&bs->vlc, 5);
583   vl_vlc_dumpbits(&bs->vlc, 5);
584
585   if (picture->q_scale_type)
586      return non_linear_quantizer_scale[quantizer_scale_code];
587   else
588      return quantizer_scale_code << 1;
589}
590
591static inline int
592get_motion_delta(struct vl_mpg12_bs *bs, unsigned f_code)
593{
594   int delta;
595   int sign;
596   const MVtab * tab;
597
598   if (bs->vlc.buf & 0x80000000) {
599      vl_vlc_dumpbits(&bs->vlc, 1);
600      return 0;
601   } else if (bs->vlc.buf >= 0x0c000000) {
602
603      tab = MV_4 + vl_vlc_ubits(&bs->vlc, 4);
604      delta = (tab->delta << f_code) + 1;
605      bs->vlc.bits += tab->len + f_code + 1;
606      bs->vlc.buf <<= tab->len;
607
608      sign = vl_vlc_sbits(&bs->vlc, 1);
609      bs->vlc.buf <<= 1;
610
611      if (f_code)
612         delta += vl_vlc_ubits(&bs->vlc, f_code);
613      bs->vlc.buf <<= f_code;
614
615      return (delta ^ sign) - sign;
616
617   } else {
618
619      tab = MV_10 + vl_vlc_ubits(&bs->vlc, 10);
620      delta = (tab->delta << f_code) + 1;
621      bs->vlc.bits += tab->len + 1;
622      bs->vlc.buf <<= tab->len;
623
624      sign = vl_vlc_sbits(&bs->vlc, 1);
625      bs->vlc.buf <<= 1;
626
627      if (f_code) {
628         vl_vlc_needbits(&bs->vlc);
629         delta += vl_vlc_ubits(&bs->vlc, f_code);
630         vl_vlc_dumpbits(&bs->vlc, f_code);
631      }
632
633      return (delta ^ sign) - sign;
634   }
635}
636
637static inline int
638bound_motion_vector(int vec, unsigned f_code)
639{
640#if 1
641   unsigned int limit;
642   int sign;
643
644   limit = 16 << f_code;
645
646   if ((unsigned int)(vec + limit) < 2 * limit)
647      return vec;
648   else {
649      sign = ((int32_t)vec) >> 31;
650      return vec - ((2 * limit) ^ sign) + sign;
651   }
652#else
653   return ((int32_t)vec << (28 - f_code)) >> (28 - f_code);
654#endif
655}
656
657static inline int
658get_dmv(struct vl_mpg12_bs *bs)
659{
660   const DMVtab * tab;
661
662   tab = DMV_2 + vl_vlc_ubits(&bs->vlc, 2);
663   vl_vlc_dumpbits(&bs->vlc, tab->len);
664   return tab->dmv;
665}
666
667static inline int
668get_coded_block_pattern(struct vl_mpg12_bs *bs)
669{
670   const CBPtab * tab;
671
672   vl_vlc_needbits(&bs->vlc);
673
674   if (bs->vlc.buf >= 0x20000000) {
675
676      tab = CBP_7 + (vl_vlc_ubits(&bs->vlc, 7) - 16);
677      vl_vlc_dumpbits(&bs->vlc, tab->len);
678      return tab->cbp;
679
680   } else {
681
682      tab = CBP_9 + vl_vlc_ubits(&bs->vlc, 9);
683      vl_vlc_dumpbits(&bs->vlc, tab->len);
684      return tab->cbp;
685   }
686}
687
688static inline int
689get_luma_dc_dct_diff(struct vl_mpg12_bs *bs)
690{
691   const DCtab * tab;
692   int size;
693   int dc_diff;
694
695   if (bs->vlc.buf < 0xf8000000) {
696      tab = DC_lum_5 + vl_vlc_ubits(&bs->vlc, 5);
697      size = tab->size;
698      if (size) {
699         bs->vlc.bits += tab->len + size;
700         bs->vlc.buf <<= tab->len;
701         dc_diff = vl_vlc_ubits(&bs->vlc, size) - UBITS (SBITS (~bs->vlc.buf, 1), size);
702         bs->vlc.buf <<= size;
703         return dc_diff;
704      } else {
705         vl_vlc_dumpbits(&bs->vlc, 3);
706         return 0;
707      }
708   } else {
709      tab = DC_long + (vl_vlc_ubits(&bs->vlc, 9) - 0x1e0);
710      size = tab->size;
711      vl_vlc_dumpbits(&bs->vlc, tab->len);
712      vl_vlc_needbits(&bs->vlc);
713      dc_diff = vl_vlc_ubits(&bs->vlc, size) - UBITS (SBITS (~bs->vlc.buf, 1), size);
714      vl_vlc_dumpbits(&bs->vlc, size);
715      return dc_diff;
716   }
717}
718
719static inline int
720get_chroma_dc_dct_diff(struct vl_mpg12_bs *bs)
721{
722   const DCtab * tab;
723   int size;
724   int dc_diff;
725
726   if (bs->vlc.buf < 0xf8000000) {
727      tab = DC_chrom_5 + vl_vlc_ubits(&bs->vlc, 5);
728      size = tab->size;
729      if (size) {
730         bs->vlc.bits += tab->len + size;
731         bs->vlc.buf <<= tab->len;
732         dc_diff = vl_vlc_ubits(&bs->vlc, size) - UBITS (SBITS (~bs->vlc.buf, 1), size);
733         bs->vlc.buf <<= size;
734         return dc_diff;
735      } else {
736         vl_vlc_dumpbits(&bs->vlc, 2);
737         return 0;
738      }
739   } else {
740      tab = DC_long + (vl_vlc_ubits(&bs->vlc, 10) - 0x3e0);
741      size = tab->size;
742      vl_vlc_dumpbits(&bs->vlc, tab->len + 1);
743      vl_vlc_needbits(&bs->vlc);
744      dc_diff = vl_vlc_ubits(&bs->vlc, size) - UBITS (SBITS (~bs->vlc.buf, 1), size);
745      vl_vlc_dumpbits(&bs->vlc, size);
746      return dc_diff;
747   }
748}
749
750static inline void
751get_intra_block_B14(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture, short *dest)
752{
753   int i, j, val;
754   const uint8_t *scan;
755   uint8_t *quant_matrix = picture->intra_quantizer_matrix;
756   int quantizer_scale = picture->quantizer_scale;
757   int mismatch;
758   const DCTtab *tab;
759
760   if (!picture->alternate_scan) {
761      scan =  mpeg2_scan_norm_orig;
762   } else {
763      scan = mpeg2_scan_alt_orig;
764   }
765
766   i = 0;
767   mismatch = ~dest[0];
768
769   vl_vlc_needbits(&bs->vlc);
770
771   while (1) {
772      if (bs->vlc.buf >= 0x28000000) {
773
774         tab = DCT_B14AC_5 + (vl_vlc_ubits(&bs->vlc, 5) - 5);
775
776         i += tab->run;
777         if (i >= 64)
778            break;	/* end of block */
779
780      normal_code:
781         j = scan[i];
782
783         bs->vlc.buf <<= tab->len;
784         bs->vlc.bits += tab->len + 1;
785         val = (tab->level * quantizer_scale * quant_matrix[j]) >> 4;
786
787         /* if (bitstream_get (1)) val = -val; */
788         val = (val ^ vl_vlc_sbits(&bs->vlc, 1)) - vl_vlc_sbits(&bs->vlc, 1);
789
790         SATURATE (val);
791         dest[j] = val;
792         mismatch ^= val;
793
794         bs->vlc.buf <<= 1;
795         vl_vlc_needbits(&bs->vlc);
796
797         continue;
798
799      } else if (bs->vlc.buf >= 0x04000000) {
800
801         tab = DCT_B14_8 + (vl_vlc_ubits(&bs->vlc, 8) - 4);
802
803         i += tab->run;
804         if (i < 64)
805            goto normal_code;
806
807         /* escape code */
808
809         i += UBITS(bs->vlc.buf << 6, 6) - 64;
810         if (i >= 64)
811            break;	/* illegal, check needed to avoid buffer overflow */
812
813         j = scan[i];
814
815         vl_vlc_dumpbits(&bs->vlc, 12);
816         vl_vlc_needbits(&bs->vlc);
817         val = (vl_vlc_sbits(&bs->vlc, 12) * quantizer_scale * quant_matrix[j]) / 16;
818
819         SATURATE (val);
820         dest[j] = val;
821         mismatch ^= val;
822
823         vl_vlc_dumpbits(&bs->vlc, 12);
824         vl_vlc_needbits(&bs->vlc);
825
826         continue;
827
828      } else if (bs->vlc.buf >= 0x02000000) {
829         tab = DCT_B14_10 + (vl_vlc_ubits(&bs->vlc, 10) - 8);
830         i += tab->run;
831         if (i < 64)
832            goto normal_code;
833      } else if (bs->vlc.buf >= 0x00800000) {
834         tab = DCT_13 + (vl_vlc_ubits(&bs->vlc, 13) - 16);
835         i += tab->run;
836         if (i < 64)
837            goto normal_code;
838      } else if (bs->vlc.buf >= 0x00200000) {
839         tab = DCT_15 + (vl_vlc_ubits(&bs->vlc, 15) - 16);
840         i += tab->run;
841         if (i < 64)
842            goto normal_code;
843      } else {
844         tab = DCT_16 + vl_vlc_ubits(&bs->vlc, 16);
845         bs->vlc.buf <<= 16;
846         vl_vlc_getword(&bs->vlc, bs->vlc.bits + 16);
847         i += tab->run;
848         if (i < 64)
849            goto normal_code;
850      }
851      break;	/* illegal, check needed to avoid buffer overflow */
852   }
853
854   dest[63] ^= mismatch & 1;
855   vl_vlc_dumpbits(&bs->vlc, 2);	/* dump end of block code */
856}
857
858static inline void
859get_intra_block_B15(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture, short *dest)
860{
861   int i, j, val;
862   const uint8_t *scan;
863   uint8_t *quant_matrix = picture->intra_quantizer_matrix;
864   int quantizer_scale = picture->quantizer_scale;
865   int mismatch;
866   const DCTtab * tab;
867
868   if (!picture->alternate_scan) {
869      scan =  mpeg2_scan_norm_orig;
870   } else {
871      scan = mpeg2_scan_alt_orig;
872   }
873
874   i = 0;
875   mismatch = ~dest[0];
876
877   vl_vlc_needbits(&bs->vlc);
878
879   while (1) {
880      if (bs->vlc.buf >= 0x04000000) {
881
882         tab = DCT_B15_8 + (vl_vlc_ubits(&bs->vlc, 8) - 4);
883
884         i += tab->run;
885         if (i < 64) {
886
887         normal_code:
888            j = scan[i];
889            bs->vlc.buf <<= tab->len;
890            bs->vlc.bits += tab->len + 1;
891            val = (tab->level * quantizer_scale * quant_matrix[j]) >> 4;
892
893            /* if (bitstream_get (1)) val = -val; */
894            val = (val ^ vl_vlc_sbits(&bs->vlc, 1)) - vl_vlc_sbits(&bs->vlc, 1);
895
896            SATURATE (val);
897            dest[j] = val;
898            mismatch ^= val;
899
900            bs->vlc.buf <<= 1;
901            vl_vlc_needbits(&bs->vlc);
902
903            continue;
904
905         } else {
906
907            /* end of block. I commented out this code because if we */
908            /* dont exit here we will still exit at the later test :) */
909
910            /* if (i >= 128) break;	*/	/* end of block */
911
912            /* escape code */
913
914            i += UBITS(bs->vlc.buf << 6, 6) - 64;
915            if (i >= 64)
916                break;	/* illegal, check against buffer overflow */
917
918            j = scan[i];
919
920            vl_vlc_dumpbits(&bs->vlc, 12);
921            vl_vlc_needbits(&bs->vlc);
922            val = (vl_vlc_sbits(&bs->vlc, 12) * quantizer_scale * quant_matrix[j]) / 16;
923
924            SATURATE (val);
925            dest[j] = val;
926            mismatch ^= val;
927
928            vl_vlc_dumpbits(&bs->vlc, 12);
929            vl_vlc_needbits(&bs->vlc);
930
931            continue;
932
933          }
934      } else if (bs->vlc.buf >= 0x02000000) {
935         tab = DCT_B15_10 + (vl_vlc_ubits(&bs->vlc, 10) - 8);
936         i += tab->run;
937         if (i < 64)
938            goto normal_code;
939      } else if (bs->vlc.buf >= 0x00800000) {
940         tab = DCT_13 + (vl_vlc_ubits(&bs->vlc, 13) - 16);
941         i += tab->run;
942         if (i < 64)
943            goto normal_code;
944      } else if (bs->vlc.buf >= 0x00200000) {
945         tab = DCT_15 + (vl_vlc_ubits(&bs->vlc, 15) - 16);
946         i += tab->run;
947         if (i < 64)
948            goto normal_code;
949      } else {
950         tab = DCT_16 + vl_vlc_ubits(&bs->vlc, 16);
951         bs->vlc.buf <<= 16;
952         vl_vlc_getword(&bs->vlc, bs->vlc.bits + 16);
953         i += tab->run;
954         if (i < 64)
955            goto normal_code;
956      }
957      break;	/* illegal, check needed to avoid buffer overflow */
958   }
959
960   dest[63] ^= mismatch & 1;
961   vl_vlc_dumpbits(&bs->vlc, 4);	/* dump end of block code */
962}
963
964static inline void
965get_non_intra_block(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture, short *dest)
966{
967   int i, j, val;
968   const uint8_t *scan;
969   uint8_t *quant_matrix = picture->non_intra_quantizer_matrix;
970   int quantizer_scale = picture->quantizer_scale;
971   int mismatch;
972   const DCTtab *tab;
973
974   i = -1;
975   mismatch = 1;
976
977   if (!picture->alternate_scan) {
978      scan =  mpeg2_scan_norm_orig;
979   } else {
980      scan = mpeg2_scan_alt_orig;
981   }
982
983   vl_vlc_needbits(&bs->vlc);
984   if (bs->vlc.buf >= 0x28000000) {
985      tab = DCT_B14DC_5 + (vl_vlc_ubits(&bs->vlc, 5) - 5);
986      goto entry_1;
987   } else
988      goto entry_2;
989
990   while (1) {
991      if (bs->vlc.buf >= 0x28000000) {
992
993         tab = DCT_B14AC_5 + (vl_vlc_ubits(&bs->vlc, 5) - 5);
994
995      entry_1:
996         i += tab->run;
997         if (i >= 64)
998            break;	/* end of block */
999
1000      normal_code:
1001         j = scan[i];
1002         bs->vlc.buf <<= tab->len;
1003         bs->vlc.bits += tab->len + 1;
1004         val = ((2*tab->level+1) * quantizer_scale * quant_matrix[j]) >> 5;
1005
1006         /* if (bitstream_get (1)) val = -val; */
1007         val = (val ^ vl_vlc_sbits(&bs->vlc, 1)) - vl_vlc_sbits(&bs->vlc, 1);
1008
1009         SATURATE (val);
1010         dest[j] = val;
1011         mismatch ^= val;
1012
1013         bs->vlc.buf <<= 1;
1014         vl_vlc_needbits(&bs->vlc);
1015
1016         continue;
1017
1018      }
1019
1020   entry_2:
1021      if (bs->vlc.buf >= 0x04000000) {
1022
1023         tab = DCT_B14_8 + (vl_vlc_ubits(&bs->vlc, 8) - 4);
1024
1025         i += tab->run;
1026         if (i < 64)
1027            goto normal_code;
1028
1029         /* escape code */
1030
1031         i += UBITS(bs->vlc.buf << 6, 6) - 64;
1032         if (i >= 64)
1033            break;	/* illegal, check needed to avoid buffer overflow */
1034
1035         j = scan[i];
1036
1037         vl_vlc_dumpbits(&bs->vlc, 12);
1038         vl_vlc_needbits(&bs->vlc);
1039         val = 2 * (vl_vlc_sbits(&bs->vlc, 12) + vl_vlc_sbits(&bs->vlc, 1)) + 1;
1040         val = (val * quantizer_scale * quant_matrix[j]) / 32;
1041
1042         SATURATE (val);
1043         dest[j] = val;
1044         mismatch ^= val;
1045
1046         vl_vlc_dumpbits(&bs->vlc, 12);
1047         vl_vlc_needbits(&bs->vlc);
1048
1049         continue;
1050
1051      } else if (bs->vlc.buf >= 0x02000000) {
1052         tab = DCT_B14_10 + (vl_vlc_ubits(&bs->vlc, 10) - 8);
1053         i += tab->run;
1054         if (i < 64)
1055            goto normal_code;
1056      } else if (bs->vlc.buf >= 0x00800000) {
1057         tab = DCT_13 + (vl_vlc_ubits(&bs->vlc, 13) - 16);
1058         i += tab->run;
1059         if (i < 64)
1060            goto normal_code;
1061      } else if (bs->vlc.buf >= 0x00200000) {
1062         tab = DCT_15 + (vl_vlc_ubits(&bs->vlc, 15) - 16);
1063         i += tab->run;
1064         if (i < 64)
1065            goto normal_code;
1066      } else {
1067         tab = DCT_16 + vl_vlc_ubits(&bs->vlc, 16);
1068         bs->vlc.buf <<= 16;
1069         vl_vlc_getword(&bs->vlc, bs->vlc.bits + 16);
1070         i += tab->run;
1071         if (i < 64)
1072            goto normal_code;
1073      }
1074      break;	/* illegal, check needed to avoid buffer overflow */
1075   }
1076   dest[63] ^= mismatch & 1;
1077   vl_vlc_dumpbits(&bs->vlc, 2);	/* dump end of block code */
1078}
1079
1080static inline void
1081get_mpeg1_intra_block(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture, short *dest)
1082{
1083   int i, j, val;
1084   const uint8_t *scan;
1085   uint8_t *quant_matrix = picture->intra_quantizer_matrix;
1086   int quantizer_scale = picture->quantizer_scale;
1087   const DCTtab * tab;
1088
1089   i = 0;
1090
1091   if (!picture->alternate_scan) {
1092      scan =  mpeg2_scan_norm_orig;
1093   } else {
1094      scan = mpeg2_scan_alt_orig;
1095   }
1096
1097   vl_vlc_needbits(&bs->vlc);
1098
1099   while (1) {
1100      if (bs->vlc.buf >= 0x28000000) {
1101
1102         tab = DCT_B14AC_5 + (vl_vlc_ubits(&bs->vlc, 5) - 5);
1103
1104         i += tab->run;
1105         if (i >= 64)
1106            break;	/* end of block */
1107
1108      normal_code:
1109         j = scan[i];
1110         bs->vlc.buf <<= tab->len;
1111         bs->vlc.bits += tab->len + 1;
1112         val = (tab->level * quantizer_scale * quant_matrix[j]) >> 4;
1113
1114         /* oddification */
1115         val = (val - 1) | 1;
1116
1117         /* if (bitstream_get (1)) val = -val; */
1118         val = (val ^ vl_vlc_sbits(&bs->vlc, 1)) - vl_vlc_sbits(&bs->vlc, 1);
1119
1120         SATURATE (val);
1121         dest[j] = val;
1122
1123         bs->vlc.buf <<= 1;
1124         vl_vlc_needbits(&bs->vlc);
1125
1126         continue;
1127
1128      } else if (bs->vlc.buf >= 0x04000000) {
1129
1130         tab = DCT_B14_8 + (vl_vlc_ubits(&bs->vlc, 8) - 4);
1131
1132         i += tab->run;
1133         if (i < 64)
1134            goto normal_code;
1135
1136         /* escape code */
1137
1138         i += UBITS(bs->vlc.buf << 6, 6) - 64;
1139         if (i >= 64)
1140            break;	/* illegal, check needed to avoid buffer overflow */
1141
1142         j = scan[i];
1143
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;
1150         }
1151         val = (val * quantizer_scale * quant_matrix[j]) / 16;
1152
1153         /* oddification */
1154         val = (val + ~SBITS (val, 1)) | 1;
1155
1156         SATURATE (val);
1157         dest[j] = val;
1158
1159         vl_vlc_dumpbits(&bs->vlc, 8);
1160         vl_vlc_needbits(&bs->vlc);
1161
1162         continue;
1163
1164      } else if (bs->vlc.buf >= 0x02000000) {
1165         tab = DCT_B14_10 + (vl_vlc_ubits(&bs->vlc, 10) - 8);
1166         i += tab->run;
1167         if (i < 64)
1168            goto normal_code;
1169      } else if (bs->vlc.buf >= 0x00800000) {
1170         tab = DCT_13 + (vl_vlc_ubits(&bs->vlc, 13) - 16);
1171         i += tab->run;
1172         if (i < 64)
1173            goto normal_code;
1174      } else if (bs->vlc.buf >= 0x00200000) {
1175         tab = DCT_15 + (vl_vlc_ubits(&bs->vlc, 15) - 16);
1176         i += tab->run;
1177         if (i < 64)
1178            goto normal_code;
1179      } else {
1180         tab = DCT_16 + vl_vlc_ubits(&bs->vlc, 16);
1181         bs->vlc.buf <<= 16;
1182         vl_vlc_getword(&bs->vlc, bs->vlc.bits + 16);
1183         i += tab->run;
1184         if (i < 64)
1185            goto normal_code;
1186      }
1187      break;	/* illegal, check needed to avoid buffer overflow */
1188   }
1189   vl_vlc_dumpbits(&bs->vlc, 2);	/* dump end of block code */
1190}
1191
1192static inline void
1193get_mpeg1_non_intra_block(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture, short *dest)
1194{
1195   int i, j, val;
1196   const uint8_t * scan;
1197   uint8_t *quant_matrix = picture->non_intra_quantizer_matrix;
1198   int quantizer_scale = picture->quantizer_scale;
1199   const DCTtab * tab;
1200
1201   i = -1;
1202
1203   if (!picture->alternate_scan) {
1204      scan =  mpeg2_scan_norm_orig;
1205   } else {
1206      scan = mpeg2_scan_alt_orig;
1207   }
1208
1209   vl_vlc_needbits(&bs->vlc);
1210   if (bs->vlc.buf >= 0x28000000) {
1211      tab = DCT_B14DC_5 + (vl_vlc_ubits(&bs->vlc, 5) - 5);
1212      goto entry_1;
1213   } else
1214      goto entry_2;
1215
1216   while (1) {
1217      if (bs->vlc.buf >= 0x28000000) {
1218
1219         tab = DCT_B14AC_5 + (vl_vlc_ubits(&bs->vlc, 5) - 5);
1220
1221      entry_1:
1222         i += tab->run;
1223         if (i >= 64)
1224            break;	/* end of block */
1225
1226      normal_code:
1227         j = scan[i];
1228         bs->vlc.buf <<= tab->len;
1229         bs->vlc.bits += tab->len + 1;
1230         val = ((2*tab->level+1) * quantizer_scale * quant_matrix[j]) >> 5;
1231
1232         /* oddification */
1233         val = (val - 1) | 1;
1234
1235         /* if (bitstream_get (1)) val = -val; */
1236         val = (val ^ vl_vlc_sbits(&bs->vlc, 1)) - vl_vlc_sbits(&bs->vlc, 1);
1237
1238         SATURATE (val);
1239         dest[j] = val;
1240
1241         bs->vlc.buf <<= 1;
1242         vl_vlc_needbits(&bs->vlc);
1243
1244         continue;
1245
1246      }
1247
1248   entry_2:
1249      if (bs->vlc.buf >= 0x04000000) {
1250
1251         tab = DCT_B14_8 + (vl_vlc_ubits(&bs->vlc, 8) - 4);
1252
1253         i += tab->run;
1254         if (i < 64)
1255            goto normal_code;
1256
1257         /* escape code */
1258
1259         i += UBITS(bs->vlc.buf << 6, 6) - 64;
1260         if (i >= 64)
1261            break;	/* illegal, check needed to avoid buffer overflow */
1262
1263         j = scan[i];
1264
1265         vl_vlc_dumpbits(&bs->vlc, 12);
1266         vl_vlc_needbits(&bs->vlc);
1267         val = vl_vlc_sbits(&bs->vlc, 8);
1268         if (! (val & 0x7f)) {
1269            vl_vlc_dumpbits(&bs->vlc, 8);
1270            val = vl_vlc_ubits(&bs->vlc, 8) + 2 * val;
1271         }
1272         val = 2 * (val + SBITS (val, 1)) + 1;
1273         val = (val * quantizer_scale * quant_matrix[j]) / 32;
1274
1275         /* oddification */
1276         val = (val + ~SBITS (val, 1)) | 1;
1277
1278         SATURATE (val);
1279         dest[j] = val;
1280
1281         vl_vlc_dumpbits(&bs->vlc, 8);
1282         vl_vlc_needbits(&bs->vlc);
1283
1284         continue;
1285
1286      } else if (bs->vlc.buf >= 0x02000000) {
1287         tab = DCT_B14_10 + (vl_vlc_ubits(&bs->vlc, 10) - 8);
1288         i += tab->run;
1289         if (i < 64)
1290            goto normal_code;
1291      } else if (bs->vlc.buf >= 0x00800000) {
1292         tab = DCT_13 + (vl_vlc_ubits(&bs->vlc, 13) - 16);
1293         i += tab->run;
1294         if (i < 64)
1295            goto normal_code;
1296      } else if (bs->vlc.buf >= 0x00200000) {
1297         tab = DCT_15 + (vl_vlc_ubits(&bs->vlc, 15) - 16);
1298         i += tab->run;
1299         if (i < 64)
1300            goto normal_code;
1301      } else {
1302         tab = DCT_16 + vl_vlc_ubits(&bs->vlc, 16);
1303         bs->vlc.buf <<= 16;
1304         vl_vlc_getword(&bs->vlc, bs->vlc.bits + 16);
1305         i += tab->run;
1306         if (i < 64)
1307            goto normal_code;
1308      }
1309      break;	/* illegal, check needed to avoid buffer overflow */
1310   }
1311   vl_vlc_dumpbits(&bs->vlc, 2);	/* dump end of block code */
1312}
1313
1314static inline void
1315slice_intra_DCT(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture, int cc,
1316                unsigned x, unsigned y, enum pipe_mpeg12_dct_type coding)
1317{
1318   short *dest = bs->ycbcr_buffer[cc];
1319
1320   bs->ycbcr_stream[cc]->x = x;
1321   bs->ycbcr_stream[cc]->y = y;
1322   bs->ycbcr_stream[cc]->intra = PIPE_MPEG12_DCT_INTRA;
1323   bs->ycbcr_stream[cc]->coding = coding;
1324
1325   vl_vlc_needbits(&bs->vlc);
1326
1327   /* Get the intra DC coefficient and inverse quantize it */
1328   if (cc == 0)
1329      picture->dc_dct_pred[0] += get_luma_dc_dct_diff(bs);
1330   else
1331      picture->dc_dct_pred[cc] += get_chroma_dc_dct_diff(bs);
1332
1333   memset(dest, 0, sizeof(int16_t) * 64);
1334   dest[0] = picture->dc_dct_pred[cc] << (3 - picture->intra_dc_precision);
1335   if (picture->mpeg1) {
1336      if (picture->picture_coding_type != D_TYPE)
1337          get_mpeg1_intra_block(bs, picture, dest);
1338   } else if (picture->intra_vlc_format)
1339      get_intra_block_B15(bs, picture, dest);
1340   else
1341      get_intra_block_B14(bs, picture, dest);
1342
1343   bs->num_ycbcr_blocks[cc]++;
1344   bs->ycbcr_stream[cc]++;
1345   bs->ycbcr_buffer[cc] += 64;
1346}
1347
1348static inline void
1349slice_non_intra_DCT(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture, int cc,
1350                    unsigned x, unsigned y, enum pipe_mpeg12_dct_type coding)
1351{
1352   short *dest = bs->ycbcr_buffer[cc];
1353
1354   bs->ycbcr_stream[cc]->x = x;
1355   bs->ycbcr_stream[cc]->y = y;
1356   bs->ycbcr_stream[cc]->intra = PIPE_MPEG12_DCT_DELTA;
1357   bs->ycbcr_stream[cc]->coding = coding;
1358
1359   memset(dest, 0, sizeof(int16_t) * 64);
1360   if (picture->mpeg1)
1361      get_mpeg1_non_intra_block(bs, picture, dest);
1362   else
1363      get_non_intra_block(bs, picture, dest);
1364
1365   bs->num_ycbcr_blocks[cc]++;
1366   bs->ycbcr_stream[cc]++;
1367   bs->ycbcr_buffer[cc] += 64;
1368}
1369
1370static inline void
1371motion_mp1(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv)
1372{
1373   int motion_x, motion_y;
1374
1375   mv->top.field_select = mv->bottom.field_select = PIPE_VIDEO_FRAME;
1376
1377   vl_vlc_needbits(&bs->vlc);
1378   motion_x = (mv->top.x + (get_motion_delta(bs, f_code[0]) << f_code[1]));
1379   motion_x = bound_motion_vector (motion_x, f_code[0] + f_code[1]);
1380   mv->top.x = mv->bottom.x = motion_x;
1381
1382   vl_vlc_needbits(&bs->vlc);
1383   motion_y = (mv->top.y + (get_motion_delta(bs, f_code[0]) << f_code[1]));
1384   motion_y = bound_motion_vector (motion_y, f_code[0] + f_code[1]);
1385   mv->top.y = mv->bottom.y = motion_y;
1386}
1387
1388static inline void
1389motion_fr_frame(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv)
1390{
1391   int motion_x, motion_y;
1392
1393   mv->top.field_select = mv->bottom.field_select = PIPE_VIDEO_FRAME;
1394
1395   vl_vlc_needbits(&bs->vlc);
1396   motion_x = mv->top.x + get_motion_delta(bs, f_code[0]);
1397   motion_x = bound_motion_vector(motion_x, f_code[0]);
1398   mv->top.x = mv->bottom.x = motion_x;
1399
1400   vl_vlc_needbits(&bs->vlc);
1401   motion_y = mv->top.y + get_motion_delta(bs, f_code[1]);
1402   motion_y = bound_motion_vector(motion_y, f_code[1]);
1403   mv->top.y = mv->bottom.y = motion_y;
1404}
1405
1406static inline void
1407motion_fr_field(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv)
1408{
1409   int motion_x, motion_y;
1410
1411   vl_vlc_needbits(&bs->vlc);
1412   mv->top.field_select = vl_vlc_ubits(&bs->vlc, 1) ?
1413      PIPE_VIDEO_BOTTOM_FIELD : PIPE_VIDEO_TOP_FIELD;
1414   vl_vlc_dumpbits(&bs->vlc, 1);
1415
1416   motion_x = mv->top.x + get_motion_delta(bs, f_code[0]);
1417   motion_x = bound_motion_vector (motion_x, f_code[0]);
1418   mv->top.x = motion_x;
1419
1420   vl_vlc_needbits(&bs->vlc);
1421   motion_y = (mv->top.y >> 1) + get_motion_delta(bs, f_code[1]);
1422   /* motion_y = bound_motion_vector (motion_y, f_code[1]); */
1423   mv->top.y = motion_y << 1;
1424
1425   vl_vlc_needbits(&bs->vlc);
1426   mv->bottom.field_select = vl_vlc_ubits(&bs->vlc, 1) ?
1427      PIPE_VIDEO_BOTTOM_FIELD : PIPE_VIDEO_TOP_FIELD;
1428   vl_vlc_dumpbits(&bs->vlc, 1);
1429
1430   motion_x = mv->bottom.x + get_motion_delta(bs, f_code[0]);
1431   motion_x = bound_motion_vector (motion_x, f_code[0]);
1432   mv->bottom.x = motion_x;
1433
1434   vl_vlc_needbits(&bs->vlc);
1435   motion_y = (mv->bottom.y >> 1) + get_motion_delta(bs, f_code[1]);
1436   /* motion_y = bound_motion_vector (motion_y, f_code[1]); */
1437   mv->bottom.y = motion_y << 1;
1438}
1439
1440static inline void
1441motion_fr_dmv(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv)
1442{
1443   int motion_x, motion_y;
1444
1445   // TODO Implement dmv
1446   mv->top.field_select = mv->bottom.field_select = PIPE_VIDEO_FRAME;
1447
1448   vl_vlc_needbits(&bs->vlc);
1449   motion_x = mv->top.x + get_motion_delta(bs, f_code[0]);
1450   motion_x = bound_motion_vector(motion_x, f_code[0]);
1451   mv->top.x = mv->bottom.x = motion_x;
1452
1453   vl_vlc_needbits(&bs->vlc);
1454   motion_y = (mv->top.y >> 1) + get_motion_delta(bs, f_code[1]);
1455   /* motion_y = bound_motion_vector (motion_y, f_code[1]); */
1456   mv->top.y = mv->bottom.y = motion_y << 1;
1457}
1458
1459/* like motion_frame, but parsing without actual motion compensation */
1460static inline void
1461motion_fr_conceal(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv)
1462{
1463   int tmp;
1464
1465   mv->top.field_select = mv->bottom.field_select = PIPE_VIDEO_FRAME;
1466
1467   vl_vlc_needbits(&bs->vlc);
1468   tmp = (mv->top.x + get_motion_delta(bs, f_code[0]));
1469   tmp = bound_motion_vector (tmp, f_code[0]);
1470   mv->top.x = mv->bottom.x = tmp;
1471
1472   vl_vlc_needbits(&bs->vlc);
1473   tmp = (mv->top.y + get_motion_delta(bs, f_code[1]));
1474   tmp = bound_motion_vector (tmp, f_code[1]);
1475   mv->top.y = mv->bottom.y = tmp;
1476
1477   vl_vlc_dumpbits(&bs->vlc, 1); /* remove marker_bit */
1478}
1479
1480static inline void
1481motion_fi_field(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv)
1482{
1483   int motion_x, motion_y;
1484
1485   vl_vlc_needbits(&bs->vlc);
1486
1487   // ref_field
1488   //vl_vlc_ubits(&bs->vlc, 1);
1489
1490   // TODO field select may need to do something here for bob (weave ok)
1491   mv->top.field_select = mv->bottom.field_select = PIPE_VIDEO_FRAME;
1492   vl_vlc_dumpbits(&bs->vlc, 1);
1493
1494   motion_x = mv->top.x + get_motion_delta(bs, f_code[0]);
1495   motion_x = bound_motion_vector (motion_x, f_code[0]);
1496   mv->top.x = mv->bottom.x = motion_x;
1497
1498   vl_vlc_needbits(&bs->vlc);
1499   motion_y = mv->top.y + get_motion_delta(bs, f_code[1]);
1500   motion_y = bound_motion_vector (motion_y, f_code[1]);
1501   mv->top.y = mv->bottom.y = motion_y;
1502}
1503
1504static inline void
1505motion_fi_16x8(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv)
1506{
1507   int motion_x, motion_y;
1508
1509   vl_vlc_needbits(&bs->vlc);
1510
1511   // ref_field
1512   //vl_vlc_ubits(&bs->vlc, 1);
1513
1514   // TODO field select may need to do something here bob  (weave ok)
1515   mv->top.field_select = PIPE_VIDEO_FRAME;
1516   vl_vlc_dumpbits(&bs->vlc, 1);
1517
1518   motion_x = mv->top.x + get_motion_delta(bs, f_code[0]);
1519   motion_x = bound_motion_vector (motion_x, f_code[0]);
1520   mv->top.x = motion_x;
1521
1522   vl_vlc_needbits(&bs->vlc);
1523   motion_y = mv->top.y + get_motion_delta(bs, f_code[1]);
1524   motion_y = bound_motion_vector (motion_y, f_code[1]);
1525   mv->top.y = motion_y;
1526
1527   vl_vlc_needbits(&bs->vlc);
1528   // ref_field
1529   //vl_vlc_ubits(&bs->vlc, 1);
1530
1531   // TODO field select may need to do something here for bob (weave ok)
1532   mv->bottom.field_select = PIPE_VIDEO_FRAME;
1533   vl_vlc_dumpbits(&bs->vlc, 1);
1534
1535   motion_x = mv->bottom.x + get_motion_delta(bs, f_code[0]);
1536   motion_x = bound_motion_vector (motion_x, f_code[0]);
1537   mv->bottom.x = motion_x;
1538
1539   vl_vlc_needbits(&bs->vlc);
1540   motion_y = mv->bottom.y + get_motion_delta(bs, f_code[1]);
1541   motion_y = bound_motion_vector (motion_y, f_code[1]);
1542   mv->bottom.y = motion_y;
1543}
1544
1545static inline void
1546motion_fi_dmv(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv)
1547{
1548   int motion_x, motion_y;
1549
1550   // TODO field select may need to do something here for bob  (weave ok)
1551   mv->top.field_select = mv->bottom.field_select = PIPE_VIDEO_FRAME;
1552
1553   vl_vlc_needbits(&bs->vlc);
1554   motion_x = mv->top.x + get_motion_delta(bs, f_code[0]);
1555   motion_x = bound_motion_vector (motion_x, f_code[0]);
1556   mv->top.x = mv->bottom.x = motion_x;
1557
1558   vl_vlc_needbits(&bs->vlc);
1559   motion_y = mv->top.y + get_motion_delta(bs, f_code[1]);
1560   motion_y = bound_motion_vector (motion_y, f_code[1]);
1561   mv->top.y = mv->bottom.y = motion_y;
1562}
1563
1564
1565static inline void
1566motion_fi_conceal(struct vl_mpg12_bs *bs, unsigned f_code[2], struct pipe_motionvector *mv)
1567{
1568   int tmp;
1569
1570   vl_vlc_needbits(&bs->vlc);
1571   vl_vlc_dumpbits(&bs->vlc, 1); /* remove field_select */
1572
1573   tmp = (mv->top.x + get_motion_delta(bs, f_code[0]));
1574   tmp = bound_motion_vector(tmp, f_code[0]);
1575   mv->top.x = mv->bottom.x = tmp;
1576
1577   vl_vlc_needbits(&bs->vlc);
1578   tmp = (mv->top.y + get_motion_delta(bs, f_code[1]));
1579   tmp = bound_motion_vector(tmp, f_code[1]);
1580   mv->top.y = mv->bottom.y = tmp;
1581
1582   vl_vlc_dumpbits(&bs->vlc, 1); /* remove marker_bit */
1583}
1584
1585#define MOTION_CALL(routine, macroblock_modes)		\
1586do {							\
1587   if ((macroblock_modes) & MACROBLOCK_MOTION_FORWARD)  \
1588      routine(bs, picture->f_code[0], &mv_fwd);         \
1589   if ((macroblock_modes) & MACROBLOCK_MOTION_BACKWARD)	\
1590      routine(bs, picture->f_code[1], &mv_bwd);         \
1591} while (0)
1592
1593#define NEXT_MACROBLOCK		                \
1594do {				                \
1595   bs->mv_stream[0][x+y*bs->width] = mv_fwd;    \
1596   bs->mv_stream[1][x+y*bs->width] = mv_bwd;    \
1597   ++x;				                \
1598   if (x == bs->width) {	                \
1599      ++y;                                      \
1600      if (y >= bs->height)                      \
1601         return false;                          \
1602      x = 0;                                    \
1603   }                                            \
1604} while (0)
1605
1606static inline bool
1607slice_init(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc * picture, int *x, int *y)
1608{
1609   const MBAtab * mba;
1610
1611   vl_vlc_need32bits(&bs->vlc);
1612   while(bs->vlc.buf < 0x101 || bs->vlc.buf > 0x1AF) {
1613      if(!vl_vlc_getbyte(&bs->vlc))
1614         return false;
1615   }
1616   *y = (bs->vlc.buf & 0xFF) - 1;
1617   vl_vlc_restart(&bs->vlc);
1618
1619   //TODO conversion to signed format signed format
1620   picture->dc_dct_pred[0] = picture->dc_dct_pred[1] = picture->dc_dct_pred[2] = 0;
1621
1622   picture->quantizer_scale = get_quantizer_scale(bs, picture);
1623
1624   /* ignore intra_slice and all the extra data */
1625   while (bs->vlc.buf & 0x80000000) {
1626      vl_vlc_dumpbits(&bs->vlc, 9);
1627      vl_vlc_needbits(&bs->vlc);
1628   }
1629
1630   /* decode initial macroblock address increment */
1631   *x = 0;
1632   while (1) {
1633      if (bs->vlc.buf >= 0x08000000) {
1634          mba = MBA_5 + (vl_vlc_ubits(&bs->vlc, 6) - 2);
1635          break;
1636      } else if (bs->vlc.buf >= 0x01800000) {
1637          mba = MBA_11 + (vl_vlc_ubits(&bs->vlc, 12) - 24);
1638          break;
1639      } else switch (vl_vlc_ubits(&bs->vlc, 12)) {
1640      case 8:		/* macroblock_escape */
1641          *x += 33;
1642          vl_vlc_dumpbits(&bs->vlc, 11);
1643          vl_vlc_needbits(&bs->vlc);
1644          continue;
1645      case 15:	/* macroblock_stuffing (MPEG1 only) */
1646          bs->vlc.buf &= 0xfffff;
1647          vl_vlc_dumpbits(&bs->vlc, 11);
1648          vl_vlc_needbits(&bs->vlc);
1649          continue;
1650      default:	/* error */
1651          return false;
1652      }
1653   }
1654   vl_vlc_dumpbits(&bs->vlc, mba->len + 1);
1655   *x += mba->mba;
1656
1657   while (*x >= bs->width) {
1658      *x -= bs->width;
1659      (*y)++;
1660   }
1661   if (*y > bs->height)
1662      return false;
1663
1664   return true;
1665}
1666
1667static inline bool
1668decode_slice(struct vl_mpg12_bs *bs, struct pipe_mpeg12_picture_desc *picture)
1669{
1670   struct pipe_motionvector mv_fwd, mv_bwd;
1671   enum pipe_mpeg12_dct_type dct_type;
1672   int x, y;
1673
1674   if (!slice_init(bs, picture, &x, &y))
1675      return false;
1676
1677   mv_fwd.top.x = mv_fwd.top.y = mv_fwd.bottom.x = mv_fwd.bottom.y = 0;
1678   mv_fwd.top.field_select = mv_fwd.bottom.field_select = PIPE_VIDEO_FRAME;
1679
1680   mv_bwd.top.x = mv_bwd.top.y = mv_bwd.bottom.x = mv_bwd.bottom.y = 0;
1681   mv_bwd.top.field_select = mv_bwd.bottom.field_select = PIPE_VIDEO_FRAME;
1682
1683   while (1) {
1684      int macroblock_modes;
1685      int mba_inc;
1686      const MBAtab * mba;
1687
1688      vl_vlc_needbits(&bs->vlc);
1689
1690      macroblock_modes = get_macroblock_modes(bs, picture); //macroblock_modes()
1691      dct_type = macroblock_modes & DCT_TYPE_INTERLACED ?
1692         PIPE_MPEG12_DCT_TYPE_FIELD : PIPE_MPEG12_DCT_TYPE_FRAME;
1693
1694      switch(macroblock_modes & (MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD)) {
1695      case (MACROBLOCK_MOTION_FORWARD|MACROBLOCK_MOTION_BACKWARD):
1696         mv_fwd.top.weight = mv_fwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_HALF;
1697         mv_bwd.top.weight = mv_bwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_HALF;
1698         break;
1699
1700      default:
1701      case MACROBLOCK_MOTION_FORWARD:
1702         mv_fwd.top.weight = mv_fwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MAX;
1703         mv_bwd.top.weight = mv_bwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MIN;
1704         break;
1705
1706      case MACROBLOCK_MOTION_BACKWARD:
1707         mv_fwd.top.weight = mv_fwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MIN;
1708         mv_bwd.top.weight = mv_bwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MAX;
1709         break;
1710      }
1711
1712      /* maybe integrate MACROBLOCK_QUANT test into get_macroblock_modes ? */
1713      if (macroblock_modes & MACROBLOCK_QUANT)
1714         picture->quantizer_scale = get_quantizer_scale(bs, picture);
1715
1716      if (macroblock_modes & MACROBLOCK_INTRA) {
1717
1718         if (picture->concealment_motion_vectors) {
1719            if (picture->picture_structure == FRAME_PICTURE)
1720               motion_fr_conceal(bs, picture->f_code[0], &mv_fwd);
1721            else
1722               motion_fi_conceal(bs, picture->f_code[0], &mv_fwd);
1723
1724         } else {
1725            mv_fwd.top.x = mv_fwd.top.y = mv_fwd.bottom.x = mv_fwd.bottom.y = 0;
1726            mv_bwd.top.x = mv_bwd.top.y = mv_bwd.bottom.x = mv_bwd.bottom.y = 0;
1727         }
1728         mv_fwd.top.weight = mv_fwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MIN;
1729         mv_bwd.top.weight = mv_bwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MIN;
1730
1731         // unravaled loop of 6 block(i) calls in macroblock()
1732         slice_intra_DCT(bs, picture, 0, x*2+0, y*2+0, dct_type);
1733         slice_intra_DCT(bs, picture, 0, x*2+1, y*2+0, dct_type);
1734         slice_intra_DCT(bs, picture, 0, x*2+0, y*2+1, dct_type);
1735         slice_intra_DCT(bs, picture, 0, x*2+1, y*2+1, dct_type);
1736         slice_intra_DCT(bs, picture, 1, x, y, dct_type);
1737         slice_intra_DCT(bs, picture, 2, x, y, dct_type);
1738
1739         if (picture->picture_coding_type == D_TYPE) {
1740            vl_vlc_needbits(&bs->vlc);
1741            vl_vlc_dumpbits(&bs->vlc, 1);
1742         }
1743
1744      } else {
1745         if (picture->picture_structure == FRAME_PICTURE)
1746            switch (macroblock_modes & MOTION_TYPE_MASK) {
1747            case MC_FRAME:
1748               if (picture->mpeg1) {
1749                  MOTION_CALL(motion_mp1, macroblock_modes);
1750               } else {
1751                  MOTION_CALL(motion_fr_frame, macroblock_modes);
1752               }
1753               break;
1754
1755            case MC_FIELD:
1756               MOTION_CALL (motion_fr_field, macroblock_modes);
1757               break;
1758
1759            case MC_DMV:
1760               MOTION_CALL (motion_fr_dmv, MACROBLOCK_MOTION_FORWARD);
1761               break;
1762
1763            case 0:
1764               /* non-intra mb without forward mv in a P picture */
1765               mv_fwd.top.x = mv_fwd.top.y = mv_fwd.bottom.x = mv_fwd.bottom.y = 0;
1766               mv_bwd.top.x = mv_bwd.top.y = mv_bwd.bottom.x = mv_bwd.bottom.y = 0;
1767               break;
1768            }
1769         else
1770            switch (macroblock_modes & MOTION_TYPE_MASK) {
1771            case MC_FIELD:
1772               MOTION_CALL (motion_fi_field, macroblock_modes);
1773               break;
1774
1775            case MC_16X8:
1776               MOTION_CALL (motion_fi_16x8, macroblock_modes);
1777               break;
1778
1779            case MC_DMV:
1780               MOTION_CALL (motion_fi_dmv, MACROBLOCK_MOTION_FORWARD);
1781               break;
1782
1783            case 0:
1784               /* non-intra mb without forward mv in a P picture */
1785               mv_fwd.top.x = mv_fwd.top.y = mv_fwd.bottom.x = mv_fwd.bottom.y = 0;
1786               mv_bwd.top.x = mv_bwd.top.y = mv_bwd.bottom.x = mv_bwd.bottom.y = 0;
1787               break;
1788            }
1789
1790         if (macroblock_modes & MACROBLOCK_PATTERN) {
1791            int coded_block_pattern = get_coded_block_pattern(bs);
1792
1793            // TODO  optimize not fully used for idct accel only mc.
1794            if (coded_block_pattern & 0x20)
1795               slice_non_intra_DCT(bs, picture, 0, x*2+0, y*2+0, dct_type); // cc0  luma 0
1796            if (coded_block_pattern & 0x10)
1797               slice_non_intra_DCT(bs, picture, 0, x*2+1, y*2+0, dct_type); // cc0 luma 1
1798            if (coded_block_pattern & 0x08)
1799               slice_non_intra_DCT(bs, picture, 0, x*2+0, y*2+1, dct_type); // cc0 luma 2
1800            if (coded_block_pattern & 0x04)
1801               slice_non_intra_DCT(bs, picture, 0, x*2+1, y*2+1, dct_type); // cc0 luma 3
1802            if (coded_block_pattern & 0x2)
1803               slice_non_intra_DCT(bs, picture, 1, x, y, dct_type); // cc1 croma
1804            if (coded_block_pattern & 0x1)
1805               slice_non_intra_DCT(bs, picture, 2, x, y, dct_type); // cc2 croma
1806         }
1807
1808         picture->dc_dct_pred[0] = picture->dc_dct_pred[1] = picture->dc_dct_pred[2] = 0;
1809      }
1810
1811      NEXT_MACROBLOCK;
1812
1813      vl_vlc_needbits(&bs->vlc);
1814      mba_inc = 0;
1815      while (1) {
1816         if (bs->vlc.buf >= 0x10000000) {
1817            mba = MBA_5 + (vl_vlc_ubits(&bs->vlc, 5) - 2);
1818            break;
1819         } else if (bs->vlc.buf >= 0x03000000) {
1820            mba = MBA_11 + (vl_vlc_ubits(&bs->vlc, 11) - 24);
1821            break;
1822         } else switch (vl_vlc_ubits(&bs->vlc, 11)) {
1823         case 8:		/* macroblock_escape */
1824            mba_inc += 33;
1825            /* pass through */
1826         case 15:	/* macroblock_stuffing (MPEG1 only) */
1827            vl_vlc_dumpbits(&bs->vlc, 11);
1828            vl_vlc_needbits(&bs->vlc);
1829            continue;
1830         default:	/* end of slice, or error */
1831            return true;
1832         }
1833      }
1834      vl_vlc_dumpbits(&bs->vlc, mba->len);
1835      mba_inc += mba->mba;
1836      if (mba_inc) {
1837         //TODO  conversion to signed format signed format
1838         picture->dc_dct_pred[0] = picture->dc_dct_pred[1] = picture->dc_dct_pred[2] = 0;
1839
1840         switch(picture->picture_structure) {
1841         case FRAME_PICTURE:
1842            mv_fwd.top.field_select = mv_fwd.bottom.field_select = PIPE_VIDEO_FRAME;
1843            mv_bwd.top.field_select = mv_bwd.bottom.field_select = PIPE_VIDEO_FRAME;
1844            break;
1845
1846         case TOP_FIELD:
1847            mv_fwd.top.field_select = mv_fwd.bottom.field_select = PIPE_VIDEO_TOP_FIELD;
1848            mv_bwd.top.field_select = mv_bwd.bottom.field_select = PIPE_VIDEO_TOP_FIELD;
1849            break;
1850
1851         case BOTTOM_FIELD:
1852            mv_fwd.top.field_select = mv_fwd.bottom.field_select = PIPE_VIDEO_BOTTOM_FIELD;
1853            mv_bwd.top.field_select = mv_bwd.bottom.field_select = PIPE_VIDEO_BOTTOM_FIELD;
1854            break;
1855         }
1856
1857         if (picture->picture_coding_type == P_TYPE) {
1858            mv_fwd.top.x = mv_fwd.top.y = mv_fwd.bottom.x = mv_fwd.bottom.y = 0;
1859            mv_fwd.top.weight = mv_fwd.bottom.weight = PIPE_VIDEO_MV_WEIGHT_MAX;
1860         }
1861         do {
1862            NEXT_MACROBLOCK;
1863         } while (--mba_inc);
1864      }
1865   }
1866}
1867
1868void
1869vl_mpg12_bs_init(struct vl_mpg12_bs *bs, unsigned width, unsigned height)
1870{
1871   assert(bs);
1872
1873   memset(bs, 0, sizeof(struct vl_mpg12_bs));
1874
1875   bs->width = width;
1876   bs->height = height;
1877}
1878
1879void
1880vl_mpg12_bs_set_buffers(struct vl_mpg12_bs *bs, struct pipe_ycbcr_block *ycbcr_stream[VL_MAX_PLANES],
1881                        short *ycbcr_buffer[VL_MAX_PLANES], struct pipe_motionvector *mv_stream[VL_MAX_REF_FRAMES])
1882{
1883   unsigned i;
1884
1885   assert(bs);
1886   assert(ycbcr_stream && ycbcr_buffer);
1887   assert(mv_stream);
1888
1889   for (i = 0; i < VL_MAX_PLANES; ++i) {
1890      bs->ycbcr_stream[i] = ycbcr_stream[i];
1891      bs->ycbcr_buffer[i] = ycbcr_buffer[i];
1892   }
1893   for (i = 0; i < VL_MAX_REF_FRAMES; ++i)
1894      bs->mv_stream[i] = mv_stream[i];
1895
1896   // TODO
1897   for (i = 0; i < bs->width*bs->height; ++i) {
1898      bs->mv_stream[0][i].top.x = bs->mv_stream[0][i].top.y = 0;
1899      bs->mv_stream[0][i].top.field_select = PIPE_VIDEO_FRAME;
1900      bs->mv_stream[0][i].top.weight = PIPE_VIDEO_MV_WEIGHT_MAX;
1901      bs->mv_stream[0][i].bottom.x = bs->mv_stream[0][i].bottom.y = 0;
1902      bs->mv_stream[0][i].bottom.field_select = PIPE_VIDEO_FRAME;
1903      bs->mv_stream[0][i].bottom.weight = PIPE_VIDEO_MV_WEIGHT_MAX;
1904
1905      bs->mv_stream[1][i].top.x = bs->mv_stream[1][i].top.y = 0;
1906      bs->mv_stream[1][i].top.field_select = PIPE_VIDEO_FRAME;
1907      bs->mv_stream[1][i].top.weight = PIPE_VIDEO_MV_WEIGHT_MIN;
1908      bs->mv_stream[1][i].bottom.x = bs->mv_stream[1][i].bottom.y = 0;
1909      bs->mv_stream[1][i].bottom.field_select = PIPE_VIDEO_FRAME;
1910      bs->mv_stream[1][i].bottom.weight = PIPE_VIDEO_MV_WEIGHT_MIN;
1911   }
1912}
1913
1914void
1915vl_mpg12_bs_decode(struct vl_mpg12_bs *bs, unsigned num_bytes, const void *buffer,
1916                   struct pipe_mpeg12_picture_desc *picture, unsigned num_ycbcr_blocks[3])
1917{
1918   assert(bs);
1919   assert(num_ycbcr_blocks);
1920   assert(buffer && num_bytes);
1921
1922   bs->num_ycbcr_blocks = num_ycbcr_blocks;
1923
1924   vl_vlc_init(&bs->vlc, buffer, num_bytes);
1925
1926   while(decode_slice(bs, picture));
1927}
1928