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