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