1 /*****************************************************************************
2 * vpar_blocks.c : blocks parsing
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: vpar_blocks.c,v 1.5 2001/07/27 09:17:38 massiot Exp $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Jean-Marc Dressler <polux@via.ecp.fr>
9 * Stéphane Borel <stef@via.ecp.fr>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
31 #include <string.h> /* memset */
40 #include "stream_control.h"
41 #include "input_ext-dec.h"
44 #include "video_output.h"
46 #include "vdec_ext-plugins.h"
47 #include "vpar_pool.h"
48 #include "video_parser.h"
51 * Welcome to vpar_blocks.c ! Here's where the heavy processor-critical parsing
52 * task is done. This file is divided in several parts :
53 * - Initialization of the lookup tables
54 * - Decoding of coded blocks
55 * - Decoding of motion vectors
56 * - Decoding of the other macroblock structures
57 * - Picture data parsing management (slices and error handling)
58 * It's a pretty long file. Good luck and have a nice day.
63 * Initialization tables
66 /* Table for coded_block_pattern resolution */
67 static lookup_t pl_coded_pattern[512] =
68 { {MB_ERROR, 0}, {0, 9}, {39, 9}, {27, 9}, {59, 9}, {55, 9}, {47, 9}, {31, 9},
69 {58, 8}, {58, 8}, {54, 8}, {54, 8}, {46, 8}, {46, 8}, {30, 8}, {30, 8},
70 {57, 8}, {57, 8}, {53, 8}, {53, 8}, {45, 8}, {45, 8}, {29, 8}, {29, 8},
71 {38, 8}, {38, 8}, {26, 8}, {26, 8}, {37, 8}, {37, 8}, {25, 8}, {25, 8},
72 {43, 8}, {43, 8}, {23, 8}, {23, 8}, {51, 8}, {51, 8}, {15, 8}, {15, 8},
73 {42, 8}, {42, 8}, {22, 8}, {22, 8}, {50, 8}, {50, 8}, {14, 8}, {14, 8},
74 {41, 8}, {41, 8}, {21, 8}, {21, 8}, {49, 8}, {49, 8}, {13, 8}, {13, 8},
75 {35, 8}, {35, 8}, {19, 8}, {19, 8}, {11, 8}, {11, 8}, {7, 8}, {7, 8},
76 {34, 7}, {34, 7}, {34, 7}, {34, 7}, {18, 7}, {18, 7}, {18, 7}, {18, 7},
77 {10, 7}, {10, 7}, {10, 7}, {10, 7}, {6, 7}, {6, 7}, {6, 7}, {6, 7},
78 {33, 7}, {33, 7}, {33, 7}, {33, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7},
79 {9, 7}, {9, 7}, {9, 7}, {9, 7}, {5, 7}, {5, 7}, {5, 7}, {5, 7},
80 {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6},
81 {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6},
82 {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6},
83 {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6},
84 {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
85 {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
86 {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5},
87 {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5},
88 {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5},
89 {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5},
90 {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5},
91 {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5},
92 {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5},
93 {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5},
94 {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5},
95 {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5},
96 {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5},
97 {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5},
98 {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5},
99 {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5},
100 {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5},
101 {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5},
102 {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5},
103 {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5},
104 {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5},
105 {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5},
106 {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5},
107 {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5},
108 {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
109 {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
110 {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
111 {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
112 {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
113 {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
114 {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
115 {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
116 {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
117 {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
118 {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
119 {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
120 {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
121 {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
122 {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
123 {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
124 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
125 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
126 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
127 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
128 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
129 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
130 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
131 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3} };
133 /* Tables for dc DCT coefficients
134 * Tables are cut in two parts to reduce memory occupation
137 /* Table B-12, dct_dc_size_luminance, codes 00XXX ... 11110 */
139 static lookup_t pl_dct_dc_lum_init_table_1[32] =
140 { {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
141 {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
142 {0, 3}, {0, 3}, {0, 3}, {0, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3},
143 {4, 3}, {4, 3}, {4, 3}, {4, 3}, {5, 4}, {5, 4}, {6, 5}, {MB_ERROR, 0}
146 static lookup_t ppl_dct_dc_init_table_1[2][32] =
147 { { {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
148 {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
149 {0, 3}, {0, 3}, {0, 3}, {0, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3},
150 {4, 3}, {4, 3}, {4, 3}, {4, 3}, {5, 4}, {5, 4}, {6, 5}, {MB_ERROR, 0}},
151 { {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2},
152 {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
153 {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
154 {3, 3}, {3, 3}, {3, 3}, {3, 3}, {4, 4}, {4, 4}, {5, 5}, {MB_ERROR, 0}
158 /* Table B-12, dct_dc_size_luminance, codes 111110xxx ... 111111111 */
160 static lookup_t pl_dct_dc_lum_init_table_2[32] =
161 { {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6},
162 {8, 7}, {8, 7}, {8, 7}, {8, 7}, {9, 8}, {9, 8}, {10,9}, {11,9},
163 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
164 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
165 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
166 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}
169 static lookup_t ppl_dct_dc_init_table_2[2][32] =
170 { { {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6},
171 {8, 7}, {8, 7}, {8, 7}, {8, 7}, {9, 8}, {9, 8}, {10,9}, {11,9},
172 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
173 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
174 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
175 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}},
176 { {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
177 {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
178 {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7},
179 {8, 8}, {8, 8}, {8, 8}, {8, 8}, {9, 9}, {9, 9}, {10,10}, {11,10}
183 /* Table B-13, dct_dc_size_chrominance, codes 00xxx ... 11110 */
184 static lookup_t pl_dct_dc_chrom_init_table_1[32] =
185 { {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2},
186 {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
187 {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
188 {3, 3}, {3, 3}, {3, 3}, {3, 3}, {4, 4}, {4, 4}, {5, 5}, {MB_ERROR, 0}
192 /* Table B-13, dct_dc_size_chrominance, codes 111110xxxx ... 1111111111 */
193 static lookup_t pl_dct_dc_chrom_init_table_2[32] =
194 { {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
195 {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
196 {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7},
197 {8, 8}, {8, 8}, {8, 8}, {8, 8}, {9, 9}, {9, 9}, {10,10}, {11,10}
201 /* Tables for ac DCT coefficients. There are cut in many parts to
203 /* Table B-14, DCT coefficients table zero,
204 * codes 0100 ... 1xxx (used for first (DC) coefficient)
206 static dct_lookup_t pl_DCT_tab_dc[12] =
208 {0,2,4}, {2,1,4}, {1,1,3}, {1,1,3},
209 {0,1,1}, {0,1,1}, {0,1,1}, {0,1,1},
210 {0,1,1}, {0,1,1}, {0,1,1}, {0,1,1}
213 /* Table B-14, DCT coefficients table zero,
214 * codes 0100 ... 1xxx (used for all other coefficients)
216 static dct_lookup_t pl_DCT_tab_ac[12] =
218 {0,2,4}, {2,1,4}, {1,1,3}, {1,1,3},
219 {DCT_EOB,0,2}, {DCT_EOB,0,2}, {DCT_EOB,0,2}, {DCT_EOB,0,2}, /* EOB */
220 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2}
223 /* Table B-14, DCT coefficients table zero,
224 * codes 000001xx ... 00111xxx
226 static dct_lookup_t pl_DCT_tab0[60] =
228 {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6},
230 {2,2,7}, {2,2,7}, {9,1,7}, {9,1,7},
231 {0,4,7}, {0,4,7}, {8,1,7}, {8,1,7},
232 {7,1,6}, {7,1,6}, {7,1,6}, {7,1,6},
233 {6,1,6}, {6,1,6}, {6,1,6}, {6,1,6},
234 {1,2,6}, {1,2,6}, {1,2,6}, {1,2,6},
235 {5,1,6}, {5,1,6}, {5,1,6}, {5,1,6},
236 {13,1,8}, {0,6,8}, {12,1,8}, {11,1,8},
237 {3,2,8}, {1,3,8}, {0,5,8}, {10,1,8},
238 {0,3,5}, {0,3,5}, {0,3,5}, {0,3,5},
239 {0,3,5}, {0,3,5}, {0,3,5}, {0,3,5},
240 {4,1,5}, {4,1,5}, {4,1,5}, {4,1,5},
241 {4,1,5}, {4,1,5}, {4,1,5}, {4,1,5},
242 {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
243 {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5}
246 /* Table B-15, DCT coefficients table one,
247 * codes 000001xx ... 11111111
249 static dct_lookup_t pl_DCT_tab0a[252] =
251 {65,0,6}, {65,0,6}, {65,0,6}, {65,0,6}, /* Escape */
252 {7,1,7}, {7,1,7}, {8,1,7}, {8,1,7},
253 {6,1,7}, {6,1,7}, {2,2,7}, {2,2,7},
254 {0,7,6}, {0,7,6}, {0,7,6}, {0,7,6},
255 {0,6,6}, {0,6,6}, {0,6,6}, {0,6,6},
256 {4,1,6}, {4,1,6}, {4,1,6}, {4,1,6},
257 {5,1,6}, {5,1,6}, {5,1,6}, {5,1,6},
258 {1,5,8}, {11,1,8}, {0,11,8}, {0,10,8},
259 {13,1,8}, {12,1,8}, {3,2,8}, {1,4,8},
260 {2,1,5}, {2,1,5}, {2,1,5}, {2,1,5},
261 {2,1,5}, {2,1,5}, {2,1,5}, {2,1,5},
262 {1,2,5}, {1,2,5}, {1,2,5}, {1,2,5},
263 {1,2,5}, {1,2,5}, {1,2,5}, {1,2,5},
264 {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
265 {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
266 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
267 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
268 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
269 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
270 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
271 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
272 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
273 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
274 {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4}, /* EOB */
275 {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
276 {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
277 {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
278 {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
279 {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
280 {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
281 {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
282 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
283 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
284 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
285 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
286 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
287 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
288 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
289 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
290 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
291 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
292 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
293 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
294 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
295 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
296 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
297 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
298 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
299 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
300 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
301 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
302 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
303 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
304 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
305 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
306 {0,4,5}, {0,4,5}, {0,4,5}, {0,4,5},
307 {0,4,5}, {0,4,5}, {0,4,5}, {0,4,5},
308 {0,5,5}, {0,5,5}, {0,5,5}, {0,5,5},
309 {0,5,5}, {0,5,5}, {0,5,5}, {0,5,5},
310 {9,1,7}, {9,1,7}, {1,3,7}, {1,3,7},
311 {10,1,7}, {10,1,7}, {0,8,7}, {0,8,7},
312 {0,9,7}, {0,9,7}, {0,12,8}, {0,13,8},
313 {2,3,8}, {4,2,8}, {0,14,8}, {0,15,8}
316 /* Table B-14, DCT coefficients table zero,
317 * codes 0000001000 ... 0000001111
319 static dct_lookup_t pl_DCT_tab1[8] =
321 {16,1,10}, {5,2,10}, {0,7,10}, {2,3,10},
322 {1,4,10}, {15,1,10}, {14,1,10}, {4,2,10}
325 /* Table B-15, DCT coefficients table one,
326 * codes 000000100x ... 000000111x
328 static dct_lookup_t pl_DCT_tab1a[8] =
330 {5,2,9}, {5,2,9}, {14,1,9}, {14,1,9},
331 {2,4,10}, {16,1,10}, {15,1,9}, {15,1,9}
334 /* Table B-14/15, DCT coefficients table zero / one,
335 * codes 000000010000 ... 000000011111
337 static dct_lookup_t pl_DCT_tab2[16] =
339 {0,11,12}, {8,2,12}, {4,3,12}, {0,10,12},
340 {2,4,12}, {7,2,12}, {21,1,12}, {20,1,12},
341 {0,9,12}, {19,1,12}, {18,1,12}, {1,5,12},
342 {3,3,12}, {0,8,12}, {6,2,12}, {17,1,12}
345 /* Table B-14/15, DCT coefficients table zero / one,
346 * codes 0000000010000 ... 0000000011111
348 static dct_lookup_t pl_DCT_tab3[16] =
350 {10,2,13}, {9,2,13}, {5,3,13}, {3,4,13},
351 {2,5,13}, {1,7,13}, {1,6,13}, {0,15,13},
352 {0,14,13}, {0,13,13}, {0,12,13}, {26,1,13},
353 {25,1,13}, {24,1,13}, {23,1,13}, {22,1,13}
356 /* Table B-14/15, DCT coefficients table zero / one,
357 * codes 00000000010000 ... 00000000011111
359 static dct_lookup_t pl_DCT_tab4[16] =
361 {0,31,14}, {0,30,14}, {0,29,14}, {0,28,14},
362 {0,27,14}, {0,26,14}, {0,25,14}, {0,24,14},
363 {0,23,14}, {0,22,14}, {0,21,14}, {0,20,14},
364 {0,19,14}, {0,18,14}, {0,17,14}, {0,16,14}
367 /* Table B-14/15, DCT coefficients table zero / one,
368 * codes 000000000010000 ... 000000000011111
370 static dct_lookup_t pl_DCT_tab5[16] =
372 {0,40,15}, {0,39,15}, {0,38,15}, {0,37,15},
373 {0,36,15}, {0,35,15}, {0,34,15}, {0,33,15},
374 {0,32,15}, {1,14,15}, {1,13,15}, {1,12,15},
375 {1,11,15}, {1,10,15}, {1,9,15}, {1,8,15}
378 /* Table B-14/15, DCT coefficients table zero / one,
379 * codes 0000000000010000 ... 0000000000011111
381 static dct_lookup_t pl_DCT_tab6[16] =
383 {1,18,16}, {1,17,16}, {1,16,16}, {1,15,16},
384 {6,3,16}, {16,2,16}, {15,2,16}, {14,2,16},
385 {13,2,16}, {12,2,16}, {11,2,16}, {31,1,16},
386 {30,1,16}, {29,1,16}, {28,1,16}, {27,1,16}
389 /* Pointers on tables of dct coefficients */
390 static dct_lookup_t * ppl_dct_tab1[2] = { pl_DCT_tab_ac, pl_DCT_tab0a };
392 static dct_lookup_t * ppl_dct_tab2[2] = { pl_DCT_tab_ac, pl_DCT_tab_dc };
394 /* Replacement for memset( p_table, 0, 8*sizeof( int ) ); */
395 static __inline__ void bzero8int( void *p_table )
397 ((int*)p_table)[0] = ((int*)p_table)[1] =
398 ((int*)p_table)[2] = ((int*)p_table)[3] =
399 ((int*)p_table)[4] = ((int*)p_table)[5] =
400 ((int*)p_table)[6] = ((int*)p_table)[7] = 0;
404 * Initialization of lookup tables
407 /*****************************************************************************
408 * vpar_InitMbAddrInc : Initialize the lookup table for mb_addr_inc
409 *****************************************************************************/
411 /* Function for filling up the lookup table for mb_addr_inc */
412 static void FillMbAddrIncTable( vpar_thread_t * p_vpar,
413 int i_start, int i_end, int i_step,
414 int * pi_value, int i_length )
417 for( i_pos = i_start ; i_pos < i_end ; i_pos += i_step )
419 for( i_offset = 0 ; i_offset < i_step ; i_offset ++ )
421 p_vpar->pl_mb_addr_inc[i_pos + i_offset].i_value = * pi_value;
422 p_vpar->pl_mb_addr_inc[i_pos + i_offset].i_length = i_length;
428 /* Function that initialize the table using the last one */
429 void vpar_InitMbAddrInc( vpar_thread_t * p_vpar )
434 for( i_dummy = 0 ; i_dummy < 8 ; i_dummy++ )
436 p_vpar->pl_mb_addr_inc[i_dummy].i_value = MB_ERROR;
437 p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
440 p_vpar->pl_mb_addr_inc[8].i_value = MB_ADDRINC_ESCAPE;
441 p_vpar->pl_mb_addr_inc[8].i_length = 11;
443 for( i_dummy = 9 ; i_dummy < 15 ; i_dummy ++ )
445 p_vpar->pl_mb_addr_inc[i_dummy].i_value = MB_ERROR;
446 p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
449 p_vpar->pl_mb_addr_inc[15].i_value = MB_ADDRINC_STUFFING;
450 p_vpar->pl_mb_addr_inc[15].i_length = 11;
452 for( i_dummy = 16; i_dummy < 24; i_dummy++ )
454 p_vpar->pl_mb_addr_inc[i_dummy].i_value = MB_ERROR;
455 p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
460 FillMbAddrIncTable( p_vpar, 24, 36, 1, &i_value, 11 );
461 FillMbAddrIncTable( p_vpar, 36, 48, 2, &i_value, 10 );
462 FillMbAddrIncTable( p_vpar, 48, 96, 8, &i_value, 8 );
463 FillMbAddrIncTable( p_vpar, 96, 128, 16, &i_value, 7 );
464 FillMbAddrIncTable( p_vpar, 128, 256, 64, &i_value, 5 );
465 FillMbAddrIncTable( p_vpar, 256, 512, 128, &i_value, 4 );
466 FillMbAddrIncTable( p_vpar, 512, 1024, 256, &i_value, 3 );
467 FillMbAddrIncTable( p_vpar, 1024, 2048, 1024, &i_value, 1 );
470 /*****************************************************************************
471 * vpar_Init*MBType : Initialize lookup table for the Macroblock type
472 *****************************************************************************/
474 /* Fonction for filling up the tables */
475 static void FillMBType( vpar_thread_t * p_vpar,
484 for( i_dummy = i_start ; i_dummy < i_end ; i_dummy++ )
486 p_vpar->ppl_mb_type[i_mb_type][i_dummy].i_value = i_value;
487 p_vpar->ppl_mb_type[i_mb_type][i_dummy].i_length = i_length;
491 /* Fonction that fills the table for P MB_Type */
492 void vpar_InitPMBType( vpar_thread_t * p_vpar )
494 FillMBType( p_vpar, 0, 32, 64, MB_MOTION_FORWARD|MB_PATTERN, 1 );
495 FillMBType( p_vpar, 0, 16, 32, MB_PATTERN, 2 );
496 FillMBType( p_vpar, 0, 8, 16, MB_MOTION_FORWARD, 3 );
497 FillMBType( p_vpar, 0, 6, 8, MB_INTRA, 5 );
498 FillMBType( p_vpar, 0, 4, 6, MB_QUANT|MB_MOTION_FORWARD|MB_PATTERN, 5 );
499 FillMBType( p_vpar, 0, 2, 4, MB_QUANT|MB_PATTERN, 5 );
500 p_vpar->ppl_mb_type[0][1].i_value = MB_QUANT|MB_INTRA;
501 p_vpar->ppl_mb_type[0][1].i_length = 6;
502 p_vpar->ppl_mb_type[0][0].i_value = MB_ERROR;
503 p_vpar->ppl_mb_type[0][0].i_length = 0;
506 /* Fonction that fills the table for B MB_Type */
507 void vpar_InitBMBType( vpar_thread_t * p_vpar )
509 FillMBType( p_vpar, 1, 48, 64, MB_MOTION_FORWARD
510 |MB_MOTION_BACKWARD|MB_PATTERN, 2 );
511 FillMBType( p_vpar, 1, 32, 48, MB_MOTION_FORWARD|MB_MOTION_BACKWARD, 2 );
512 FillMBType( p_vpar, 1, 24, 32, MB_MOTION_BACKWARD|MB_PATTERN, 3 );
513 FillMBType( p_vpar, 1, 16, 24, MB_MOTION_BACKWARD, 3 );
514 FillMBType( p_vpar, 1, 12, 16, MB_MOTION_FORWARD|MB_PATTERN, 4 );
515 FillMBType( p_vpar, 1, 8, 12, MB_MOTION_FORWARD, 4 );
516 FillMBType( p_vpar, 1, 6, 8, MB_INTRA, 5 );
517 FillMBType( p_vpar, 1, 4, 6, MB_QUANT|MB_MOTION_FORWARD
518 |MB_MOTION_BACKWARD|MB_PATTERN, 5 );
519 p_vpar->ppl_mb_type[1][3].i_value = MB_QUANT|MB_MOTION_FORWARD|MB_PATTERN;
520 p_vpar->ppl_mb_type[1][3].i_length = 6;
521 p_vpar->ppl_mb_type[1][2].i_value = MB_QUANT|MB_MOTION_BACKWARD|MB_PATTERN;
522 p_vpar->ppl_mb_type[1][2].i_length = 6;
523 p_vpar->ppl_mb_type[1][1].i_value = MB_QUANT|MB_INTRA;
524 p_vpar->ppl_mb_type[1][1].i_length = 6;
525 p_vpar->ppl_mb_type[1][0].i_value =MB_ERROR;
526 p_vpar->ppl_mb_type[1][0].i_length = 0;
530 /*****************************************************************************
531 * vpar_InitDCTTables : Initialize tables giving the length of the dct
532 * coefficient from the vlc code
533 *****************************************************************************/
535 /* First fonction for filling the table */
536 static void FillDCTTable( dct_lookup_t * p_tab_dest, dct_lookup_t * p_tab_src,
537 int i_step, int i_nb_elem, int i_offset )
539 int i_dummy, i_dummy2;
541 for( i_dummy=0 ; i_dummy < i_nb_elem ; i_dummy++ )
543 for( i_dummy2=0 ; i_dummy2 < i_step ; i_dummy2++ )
545 p_tab_dest[(i_dummy+i_offset)*i_step+i_dummy2] = p_tab_src[i_dummy];
551 /* Fonction that actually fills the table or create the pointers */
552 void vpar_InitDCTTables( vpar_thread_t * p_vpar )
554 /* Tables are cut in two parts to reduce memory occupation */
555 p_vpar->pppl_dct_dc_size[0][0] = pl_dct_dc_lum_init_table_1;
556 p_vpar->pppl_dct_dc_size[0][1] = pl_dct_dc_lum_init_table_2;
557 p_vpar->pppl_dct_dc_size[1][0] = pl_dct_dc_chrom_init_table_1;
558 p_vpar->pppl_dct_dc_size[1][1] = pl_dct_dc_chrom_init_table_2;
560 /* XXX?? MB_ERROR is replaced by 0 because if we use -1 we
561 * can block in DecodeMPEG2Intra and others */
562 memset( p_vpar->ppl_dct_coef[0], 0, 16 );
563 memset( p_vpar->ppl_dct_coef[1], 0, 16 );
565 /* For table B14 & B15, we have a pointer to tables */
566 /* We fill the table thanks to the fonction defined above */
567 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab0, 256, 60, 4 );
568 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab1, 64, 8, 8 );
569 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab2, 16, 16, 16 );
570 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab3, 8, 16, 16 );
571 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab4, 4, 16, 16 );
572 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab5, 2, 16, 16 );
573 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab6, 1, 16, 16 );
575 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab0a, 256, 60, 4 );
576 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab1a, 64, 8, 8 );
577 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab2, 16, 16, 16 );
578 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab3, 8, 16, 16 );
579 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab4, 4, 16, 16 );
580 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab5, 2, 16, 16 );
581 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab6, 1, 16, 16 );
584 /*****************************************************************************
585 * vpar_InitScanTable : Initialize scan table
586 *****************************************************************************/
587 void vpar_InitScanTable( vpar_thread_t * p_vpar )
591 memcpy( p_vpar->ppi_scan, pi_scan, sizeof(pi_scan) );
592 p_vpar->pf_norm_scan( p_vpar->ppi_scan );
594 /* If scan table has changed, we must change the quantization matrices. */
595 for( i = 0; i < 64; i++ )
597 p_vpar->pi_default_intra_quant[ p_vpar->ppi_scan[0][i] ] =
598 pi_default_intra_quant[ pi_scan[0][i] ];
599 p_vpar->pi_default_nonintra_quant[ p_vpar->ppi_scan[0][i] ] =
600 pi_default_nonintra_quant[ pi_scan[0][i] ];
609 #define SATURATE(val) \
612 else if( val < -2048 ) \
615 /*****************************************************************************
616 * DecodeMPEG1NonIntra : decode MPEG-1 non-intra blocks
617 *****************************************************************************/
618 static __inline__ void DecodeMPEG1NonIntra( vpar_thread_t * p_vpar,
619 macroblock_t * p_mb, int i_b,
620 boolean_t b_chroma, int i_cc )
631 dct_lookup_t * p_tab;
633 /* Decoding of the AC coefficients */
639 for( i_parse = 0; !p_vpar->p_fifo->b_die; i_parse++ )
641 i_code = ShowBits( &p_vpar->bit_stream, 16 );
642 if( i_code >= 16384 )
644 p_tab = &ppl_dct_tab2[(i_parse == 0)][(i_code>>12)-4];
648 p_tab = &p_vpar->ppl_dct_coef[0][i_code];
650 i_run = p_tab->i_run;
651 i_level = p_tab->i_level;
652 i_length = p_tab->i_length;
654 RemoveBits( &p_vpar->bit_stream, i_length );
659 i_run = GetBits( &p_vpar->bit_stream, 6 );
660 i_level = GetBits( &p_vpar->bit_stream, 8 );
662 i_level = GetBits( &p_vpar->bit_stream, 8 );
663 else if (i_level == 128)
664 i_level = GetBits( &p_vpar->bit_stream, 8 ) - 256;
665 else if (i_level > 128)
668 if( (b_sign = i_level < 0) )
675 p_mb->pf_idct[i_b] = p_vpar->pf_sparse_idct;
676 p_mb->pi_sparse_pos[i_b] = i_coef;
680 p_mb->pf_idct[i_b] = p_vpar->pf_idct;
686 b_sign = GetBits( &p_vpar->bit_stream, 1 );
697 i_pos = p_vpar->ppi_scan[p_vpar->picture.b_alternate_scan][i_parse];
698 i_level = ( ((i_level << 1) + 1) * p_vpar->mb.i_quantizer_scale
699 * p_vpar->sequence.nonintra_quant.pi_matrix[i_pos] ) >> 4;
701 /* Mismatch control */
702 /* Equivalent to : if( (val & 1) == 0 ) val = val - 1; */
703 i_level = (i_level - 1) | 1;
705 i_level = b_sign ? -i_level : i_level;
709 p_mb->ppi_blocks[i_b][i_pos] = i_level;
712 intf_WarnMsg( 2, "DCT coeff (non-intra) is out of bounds" );
713 p_vpar->picture.b_error = 1;
716 /*****************************************************************************
717 * DecodeMPEG1Intra : decode MPEG-1 intra blocks
718 *****************************************************************************/
719 static __inline__ void DecodeMPEG1Intra( vpar_thread_t * p_vpar,
720 macroblock_t * p_mb, int i_b,
721 boolean_t b_chroma, int i_cc )
734 dct_lookup_t * p_tab;
736 u8 * pi_quant = p_vpar->sequence.intra_quant.pi_matrix;
739 i_code = ShowBits(&p_vpar->bit_stream, 5);
743 p_lookup = &ppl_dct_dc_init_table_1[b_chroma][i_code];
747 i_code = ShowBits(&p_vpar->bit_stream, (9+b_chroma)) - (0x1f0 * (b_chroma + 1));
748 p_lookup = &ppl_dct_dc_init_table_2[b_chroma][i_code];
750 i_dct_dc_size = p_lookup->i_value;
751 i_length = p_lookup->i_length;
752 RemoveBits( &p_vpar->bit_stream, i_length );
754 if (i_dct_dc_size == 0)
758 i_dct_dc_diff = GetBits( &p_vpar->bit_stream, i_dct_dc_size);
759 if ((i_dct_dc_diff & (1<<(i_dct_dc_size-1))) == 0)
760 i_dct_dc_diff -= (1<<i_dct_dc_size) - 1;
763 /* Read the actual code with the good length */
764 p_vpar->mb.pi_dc_dct_pred[i_cc] += i_dct_dc_diff;
766 p_mb->ppi_blocks[i_b][0] = p_vpar->mb.pi_dc_dct_pred[i_cc] << 3;
768 i_nc = ( p_vpar->mb.pi_dc_dct_pred[i_cc] != 0 );
770 if( p_vpar->picture.i_coding_type == D_CODING_TYPE )
772 /* Remove end_of_macroblock (always 1, prevents startcode emulation)
773 * ISO/IEC 11172-2 section 2.4.2.7 and 2.4.3.6 */
774 RemoveBits( &p_vpar->bit_stream, 1 );
775 /* D pictures do not have AC coefficients */
780 /* Decoding of the AC coefficients */
784 for( i_parse = 1; !p_vpar->p_fifo->b_die; i_parse++ )
786 i_code = ShowBits( &p_vpar->bit_stream, 16 );
787 /* We use 2 main tables for the coefficients */
788 if( i_code >= 16384 )
790 p_tab = &ppl_dct_tab1[0][(i_code>>12)-4];
794 p_tab = &p_vpar->ppl_dct_coef[0][i_code];
796 i_run = p_tab->i_run;
797 i_length = p_tab->i_length;
798 i_level = p_tab->i_level;
800 RemoveBits( &p_vpar->bit_stream, i_length );
805 i_run = GetBits( &p_vpar->bit_stream, 6 );
806 i_level = GetBits( &p_vpar->bit_stream, 8 );
808 i_level = GetBits( &p_vpar->bit_stream, 8 );
809 else if (i_level == 128)
810 i_level = GetBits( &p_vpar->bit_stream, 8 ) - 256;
811 else if (i_level > 128)
813 if( (b_sign = i_level < 0) )
819 p_mb->pf_idct[i_b] = p_vpar->pf_sparse_idct;
820 p_mb->pi_sparse_pos[i_b] = i_coef;
824 p_mb->pf_idct[i_b] = p_vpar->pf_idct;
830 b_sign = GetBits( &p_vpar->bit_stream, 1 );
833 /* Prepare the next block */
840 /* We have an error in the stream */
844 /* Determine the position of the block in the frame */
845 i_pos = p_vpar->ppi_scan[p_vpar->picture.b_alternate_scan][i_parse];
846 i_level = ( i_level *
847 p_vpar->mb.i_quantizer_scale * pi_quant[i_pos] ) >> 3;
849 /* Mismatch control */
850 /* Equivalent to : if( (val & 1) == 0 ) val = val - 1; */
851 i_level = (i_level - 1) | 1;
853 i_level = b_sign ? -i_level : i_level;
857 p_mb->ppi_blocks[i_b][i_pos] = i_level;
860 intf_WarnMsg( 2, "DCT coeff (intra) is out of bounds" );
861 p_vpar->picture.b_error = 1;
864 /*****************************************************************************
865 * DecodeMPEG2NonIntra : decode MPEG-2 non-intra blocks
866 *****************************************************************************/
867 static __inline__ void DecodeMPEG2NonIntra( vpar_thread_t * p_vpar,
868 macroblock_t * p_mb, int i_b,
869 boolean_t b_chroma, int i_cc )
882 dct_lookup_t * p_tab;
883 /* FIXME : if you want to enable other types of chroma, replace this
884 * by p_vpar->sequence.i_chroma_format. */
885 int i_chroma_format = CHROMA_420;
887 /* Give a pointer to the quantization matrices for intra blocks */
888 if( (i_chroma_format == CHROMA_420) || (!b_chroma) )
890 pi_quant = p_vpar->sequence.nonintra_quant.pi_matrix;
894 pi_quant = p_vpar->sequence.chroma_nonintra_quant.pi_matrix;
897 /* Decoding of the AC coefficients */
901 for( i_parse = 0; !p_vpar->p_fifo->b_die; i_parse++ )
903 i_code = ShowBits( &p_vpar->bit_stream, 16 );
904 if( i_code >= 16384 )
906 p_tab = &ppl_dct_tab2[(i_parse == 0)][(i_code>>12)-4];
910 p_tab = &p_vpar->ppl_dct_coef[0][i_code];
912 i_run = p_tab->i_run;
913 i_level = p_tab->i_level;
914 i_length = p_tab->i_length;
916 RemoveBits( &p_vpar->bit_stream, i_length );
921 i_run = GetBits( &p_vpar->bit_stream, 6 );
922 i_level = GetBits( &p_vpar->bit_stream, 12 );
923 i_level = (b_sign = ( i_level > 2047 )) ? 4096 - i_level
929 p_mb->pf_idct[i_b] = p_vpar->pf_sparse_idct;
930 p_mb->pi_sparse_pos[i_b] = i_coef;
934 p_mb->pf_idct[i_b] = p_vpar->pf_idct;
937 /* Mismatch control */
938 p_mb->ppi_blocks[i_b][63] ^= i_mismatch & 1;
943 b_sign = GetBits( &p_vpar->bit_stream, 1 );
954 i_pos = p_vpar->ppi_scan[p_vpar->picture.b_alternate_scan][i_parse];
955 i_level = ( ((i_level << 1) + 1) * p_vpar->mb.i_quantizer_scale
956 * pi_quant[i_pos] ) >> 5;
958 /* Could be replaced by (i_level ^ sign) - sign */
959 i_level = b_sign ? -i_level : i_level;
963 p_mb->ppi_blocks[i_b][i_pos] = i_level;
965 /* Mismatch control */
966 i_mismatch ^= i_level;
969 intf_WarnMsg( 2, "DCT coeff (non-intra) is out of bounds" );
970 p_vpar->picture.b_error = 1;
973 /*****************************************************************************
974 * DecodeMPEG2Intra : decode MPEG-2 intra blocks
975 *****************************************************************************/
976 static __inline__ void DecodeMPEG2Intra( vpar_thread_t * p_vpar,
977 macroblock_t * p_mb, int i_b,
978 boolean_t b_chroma, int i_cc )
991 boolean_t b_vlc_intra;
994 dct_lookup_t * p_tab;
996 /* FIXME : if you want to enable other types of chroma, replace this
997 * by p_vpar->sequence.i_chroma_format. */
998 int i_chroma_format = CHROMA_420;
1000 /* Give a pointer to the quantization matrices for intra blocks */
1001 if( (i_chroma_format == CHROMA_420) || (!b_chroma) )
1003 pi_quant = p_vpar->sequence.intra_quant.pi_matrix;
1007 pi_quant = p_vpar->sequence.chroma_intra_quant.pi_matrix;
1011 i_code = ShowBits( &p_vpar->bit_stream, 5 );
1015 p_lookup = &ppl_dct_dc_init_table_1[b_chroma][i_code];
1019 i_code = ShowBits(&p_vpar->bit_stream, (9+b_chroma)) - (0x1f0 * (b_chroma + 1));
1020 p_lookup = &ppl_dct_dc_init_table_2[b_chroma][i_code];
1022 i_dct_dc_size = p_lookup->i_value;
1023 i_length = p_lookup->i_length;
1024 RemoveBits( &p_vpar->bit_stream, i_length );
1026 if (i_dct_dc_size == 0)
1030 i_dct_dc_diff = GetBits( &p_vpar->bit_stream, i_dct_dc_size);
1031 if ((i_dct_dc_diff & (1<<(i_dct_dc_size-1))) == 0)
1032 i_dct_dc_diff -= (1<<i_dct_dc_size) - 1;
1035 /* Read the actual code with the good length */
1036 p_vpar->mb.pi_dc_dct_pred[i_cc] += i_dct_dc_diff;
1038 p_mb->ppi_blocks[i_b][0] = ( p_vpar->mb.pi_dc_dct_pred[i_cc] <<
1039 ( 3 - p_vpar->picture.i_intra_dc_precision ) );
1041 i_nc = ( p_vpar->mb.pi_dc_dct_pred[i_cc] != 0 );
1043 /* Decoding of the AC coefficients */
1046 b_vlc_intra = p_vpar->picture.b_intra_vlc_format;
1047 for( i_parse = 1; !p_vpar->p_fifo->b_die; i_parse++ )
1049 i_code = ShowBits( &p_vpar->bit_stream, 16 );
1050 /* We use 2 main tables for the coefficients */
1051 if( i_code >= 16384 )
1053 p_tab = &ppl_dct_tab1[b_vlc_intra][(i_code>>(12-(4*b_vlc_intra)))-4];
1057 p_tab = &p_vpar->ppl_dct_coef[b_vlc_intra][i_code];
1059 i_run = p_tab->i_run;
1060 i_level = p_tab->i_level;
1061 i_length = p_tab->i_length;
1063 RemoveBits( &p_vpar->bit_stream, i_length );
1068 i_run = GetBits( &p_vpar->bit_stream, 6 );
1069 i_level = GetBits( &p_vpar->bit_stream, 12 );
1070 i_level = (b_sign = ( i_level > 2047 )) ? 4096 - i_level
1076 p_mb->pf_idct[i_b] = p_vpar->pf_sparse_idct;
1077 p_mb->pi_sparse_pos[i_b] = i_coef;
1081 p_mb->pf_idct[i_b] = p_vpar->pf_idct;
1084 /* Mismatch control */
1085 p_mb->ppi_blocks[i_b][63] ^= i_mismatch & 1;
1090 b_sign = GetBits( &p_vpar->bit_stream, 1 );
1093 /* Prepare the next block */
1100 /* We have an error in the stream */
1104 /* Determine the position of the block in the frame */
1105 i_pos = p_vpar->ppi_scan[p_vpar->picture.b_alternate_scan][i_parse];
1106 i_level = ( i_level *
1107 p_vpar->mb.i_quantizer_scale *
1108 pi_quant[i_pos] ) >> 4;
1110 i_level = b_sign ? -i_level : i_level;
1112 SATURATE( i_level );
1114 p_mb->ppi_blocks[i_b][i_pos] = i_level;
1116 /* Mismatch control */
1117 i_mismatch ^= i_level;
1120 intf_WarnMsg( 2, "DCT coeff (intra) is out of bounds" );
1121 p_vpar->picture.b_error = 1;
1124 /*****************************************************************************
1125 * Decode*Blocks : decode blocks
1126 *****************************************************************************/
1127 static int pi_x[12] = {0,8,0,8,0,0,0,0,8,8,8,8};
1128 static int pi_y[2][12] = { {0,0,8,8,0,0,8,8,0,0,8,8},
1129 {0,0,1,1,0,0,1,1,0,0,1,1} };
1130 #define PARSEERROR \
1131 if( p_vpar->picture.b_error ) \
1136 #define DECODEBLOCKS( MBFUNC, BLOCKFUNC ) \
1137 static void MBFUNC( vpar_thread_t * p_vpar, macroblock_t * p_mb ) \
1139 /* FIXME : if you want to enable other types of chroma, replace this\
1140 * by p_vpar->sequence.i_chroma_format. */ \
1141 int i_chroma_format = CHROMA_420; \
1143 yuv_data_t * p_data1; \
1144 yuv_data_t * p_data2; \
1146 i_mask = 1 << (3 + (1 << i_chroma_format)); \
1149 p_data1 = p_mb->p_picture->p_y \
1150 + p_mb->i_l_x + p_vpar->mb.i_l_y * (p_vpar->sequence.i_width); \
1152 for( i_b = 0 ; i_b < 4 ; i_b++, i_mask >>= 1 ) \
1154 if( p_mb->i_coded_block_pattern & i_mask ) \
1156 memset( p_mb->ppi_blocks[i_b], 0, 64*sizeof(dctelem_t) ); \
1157 BLOCKFUNC( p_vpar, p_mb, i_b, 0, 0 ); \
1159 /* Calculate block coordinates. */ \
1160 p_mb->p_data[i_b] = p_data1 \
1161 + pi_y[p_vpar->mb.b_dct_type][i_b] \
1162 * p_vpar->picture.i_l_stride \
1170 p_data1 = p_mb->p_picture->p_u \
1172 + p_vpar->mb.i_c_y \
1173 * (p_vpar->sequence.i_chroma_width); \
1174 p_data2 = p_mb->p_picture->p_v \
1176 + p_vpar->mb.i_c_y \
1177 * (p_vpar->sequence.i_chroma_width); \
1179 for( i_b = 4; i_b < 4 + (1 << i_chroma_format); \
1180 i_b++, i_mask >>= 1 ) \
1182 yuv_data_t * pp_data[2] = {p_data1, p_data2}; \
1184 if( p_mb->i_coded_block_pattern & i_mask ) \
1186 memset( p_mb->ppi_blocks[i_b], 0, 64*sizeof(dctelem_t) ); \
1187 BLOCKFUNC( p_vpar, p_mb, i_b, 1, 1 + ((i_b - 4) & 1) ); \
1189 /* Calculate block coordinates. */ \
1190 p_mb->p_data[i_b] = pp_data[i_b & 1] \
1191 + pi_y[p_vpar->mb.b_dct_type][i_b] \
1192 * p_vpar->picture.i_c_stride \
1200 DECODEBLOCKS( DecodeMPEG1NonIntraMB, DecodeMPEG1NonIntra );
1201 DECODEBLOCKS( DecodeMPEG1IntraMB, DecodeMPEG1Intra );
1202 DECODEBLOCKS( DecodeMPEG2NonIntraMB, DecodeMPEG2NonIntra );
1203 DECODEBLOCKS( DecodeMPEG2IntraMB, DecodeMPEG2Intra );
1212 /****************************************************************************
1213 * MotionCode : Parse the next motion code
1214 ****************************************************************************/
1215 static __inline__ int MotionCode( vpar_thread_t * p_vpar )
1218 static lookup_t pl_mv_tab0[8] =
1219 { {-1,0}, {3,3}, {2,2}, {2,2}, {1,1}, {1,1}, {1,1}, {1,1} };
1220 /* Table B-10, motion_code, codes 0000011 ... 000011x */
1221 static lookup_t pl_mv_tab1[8] =
1222 { {-1,0}, {-1,0}, {-1,0}, {7,6}, {6,6}, {5,6}, {4,5}, {4,5} };
1223 /* Table B-10, motion_code, codes 0000001100 ... 000001011x */
1224 static lookup_t pl_mv_tab2[12] = {
1225 {16,9}, {15,9}, {14,9}, {13,9},
1226 {12,9}, {11,9}, {10,8}, {10,8},
1227 {9,8}, {9,8}, {8,8}, {8,8} };
1229 if( GetBits( &p_vpar->bit_stream, 1 ) )
1233 if( (i_code = ShowBits( &p_vpar->bit_stream, 9) ) >= 64 )
1236 RemoveBits( &p_vpar->bit_stream, pl_mv_tab0[i_code].i_length );
1237 return( GetBits( &p_vpar->bit_stream, 1 ) ?
1238 -pl_mv_tab0[i_code].i_value : pl_mv_tab0[i_code].i_value );
1244 RemoveBits( &p_vpar->bit_stream, pl_mv_tab1[i_code].i_length );
1245 return( GetBits( &p_vpar->bit_stream, 1 ) ?
1246 -pl_mv_tab1[i_code].i_value : pl_mv_tab1[i_code].i_value );
1249 if( (i_code -= 12) < 0 )
1251 p_vpar->picture.b_error = 1;
1252 intf_WarnMsg( 2, "Invalid motion_vector code" );
1256 RemoveBits( &p_vpar->bit_stream, pl_mv_tab2[i_code].i_length );
1257 return( GetBits( &p_vpar->bit_stream, 1 ) ?
1258 -pl_mv_tab2[i_code].i_value : pl_mv_tab2[i_code].i_value );
1261 /****************************************************************************
1262 * DecodeMotionVector : Decode a motion_vector
1263 ****************************************************************************/
1264 static __inline__ void DecodeMotionVector( int * pi_prediction, int i_r_size,
1265 int i_motion_code, int i_motion_residual, int i_full_pel )
1267 int i_limit, i_vector;
1269 /* ISO/IEC 13818-1 section 7.6.3.1 */
1270 i_limit = 16 << i_r_size;
1271 i_vector = *pi_prediction >> i_full_pel;
1273 if( i_motion_code > 0 )
1275 i_vector += ((i_motion_code-1) << i_r_size) + i_motion_residual + 1;
1276 if( i_vector >= i_limit )
1277 i_vector -= i_limit + i_limit;
1279 else if( i_motion_code < 0 )
1281 i_vector -= ((-i_motion_code-1) << i_r_size) + i_motion_residual + 1;
1282 if( i_vector < -i_limit )
1283 i_vector += i_limit + i_limit;
1285 *pi_prediction = i_vector << i_full_pel;
1288 /****************************************************************************
1289 * MotionVector : Parse the next motion_vector field
1290 ****************************************************************************/
1291 static __inline__ void MotionVector( vpar_thread_t * p_vpar,
1292 macroblock_t * p_mb, int i_r,
1293 int i_s, int i_full_pel, int i_structure,
1294 int i_h_r_size, int i_v_r_size )
1296 int i_motion_code, i_motion_residual;
1297 int pi_dm_vector[2];
1299 i_motion_code = MotionCode( p_vpar );
1300 i_motion_residual = (i_h_r_size != 0 && i_motion_code != 0) ?
1301 GetBits( &p_vpar->bit_stream, i_h_r_size) : 0;
1302 DecodeMotionVector( &p_vpar->mb.pppi_pmv[i_r][i_s][0], i_h_r_size,
1303 i_motion_code, i_motion_residual, i_full_pel );
1304 p_mb->pppi_motion_vectors[i_r][i_s][0] = p_vpar->mb.pppi_pmv[i_r][i_s][0];
1306 if( p_vpar->mb.b_dmv )
1308 if( GetBits(&p_vpar->bit_stream, 1) )
1310 pi_dm_vector[0] = GetBits( &p_vpar->bit_stream, 1 ) ? -1 : 1;
1314 pi_dm_vector[0] = 0;
1318 i_motion_code = MotionCode( p_vpar );
1319 i_motion_residual = (i_v_r_size != 0 && i_motion_code != 0) ?
1320 GetBits( &p_vpar->bit_stream, i_v_r_size) : 0;
1323 if( (p_vpar->mb.i_mv_format == MOTION_FIELD)
1324 && (i_structure == FRAME_STRUCTURE) )
1326 p_vpar->mb.pppi_pmv[i_r][i_s][1] >>= 1;
1329 DecodeMotionVector( &p_vpar->mb.pppi_pmv[i_r][i_s][1], i_v_r_size,
1330 i_motion_code, i_motion_residual, i_full_pel );
1332 if( (p_vpar->mb.i_mv_format == MOTION_FIELD)
1333 && (i_structure == FRAME_STRUCTURE) )
1334 p_vpar->mb.pppi_pmv[i_r][i_s][1] <<= 1;
1336 p_mb->pppi_motion_vectors[i_r][i_s][1] = p_vpar->mb.pppi_pmv[i_r][i_s][1];
1338 if( p_vpar->mb.b_dmv )
1340 if( GetBits(&p_vpar->bit_stream, 1) )
1342 pi_dm_vector[1] = GetBits( &p_vpar->bit_stream, 1 ) ? -1 : 1;
1346 pi_dm_vector[1] = 0;
1349 /* Dual Prime Arithmetic (ISO/IEC 13818-2 section 7.6.3.6). */
1351 //intf_Msg( "Your stream uses Dual Prime Arithmetic. Please send a mail"
1352 // " to massiot@via.ecp.fr for debugging purposes. Thank you." );
1354 #define i_mv_x p_mb->pppi_motion_vectors[0][0][0]
1355 if( i_structure == FRAME_STRUCTURE )
1357 #define i_mv_y (p_mb->pppi_motion_vectors[0][0][1] << 1)
1358 if( p_vpar->picture.b_top_field_first )
1360 /* vector for prediction of top field from bottom field */
1361 p_mb->ppi_dmv[0][0] = ((i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1362 p_mb->ppi_dmv[0][1] = ((i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] - 1;
1364 /* vector for prediction of bottom field from top field */
1365 p_mb->ppi_dmv[1][0] = ((3*i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1366 p_mb->ppi_dmv[1][1] = ((3*i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] + 1;
1370 /* vector for prediction of top field from bottom field */
1371 p_mb->ppi_dmv[0][0] = ((3*i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1372 p_mb->ppi_dmv[0][1] = ((3*i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] - 1;
1374 /* vector for prediction of bottom field from top field */
1375 p_mb->ppi_dmv[1][0] = ((i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1376 p_mb->ppi_dmv[1][1] = ((i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] + 1;
1382 #define i_mv_y p_mb->pppi_motion_vectors[0][0][1]
1383 /* vector for prediction from field of opposite 'parity' */
1384 p_mb->ppi_dmv[0][0] = ((i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1385 p_mb->ppi_dmv[0][1] = ((i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1];
1387 /* correct for vertical field shift */
1388 if( i_structure == TOP_FIELD )
1389 p_mb->ppi_dmv[0][1]--;
1391 p_mb->ppi_dmv[0][1]++;
1398 /*****************************************************************************
1399 * DecodeMVMPEG1 : Parse the next MPEG-1 motion vectors
1400 *****************************************************************************/
1401 static void DecodeMVMPEG1( vpar_thread_t * p_vpar,
1402 macroblock_t * p_mb, int i_s, int i_structure )
1404 int i_r_size = i_s ? p_vpar->picture.i_backward_f_code - 1 :
1405 p_vpar->picture.i_forward_f_code - 1;
1406 MotionVector( p_vpar, p_mb, 0, i_s,
1407 p_vpar->picture.pb_full_pel_vector[i_s], FRAME_STRUCTURE,
1408 i_r_size, i_r_size );
1411 /*****************************************************************************
1412 * DecodeMVMPEG2 : Parse the next MPEG-2 motion_vectors field
1413 *****************************************************************************/
1414 static void DecodeMVMPEG2( vpar_thread_t * p_vpar,
1415 macroblock_t * p_mb, int i_s, int i_structure )
1417 if( p_vpar->mb.i_mv_count == 1 )
1419 if( p_vpar->mb.i_mv_format == MOTION_FIELD && !p_vpar->mb.b_dmv )
1421 p_mb->ppi_field_select[0][i_s] = p_mb->ppi_field_select[1][i_s]
1422 = GetBits( &p_vpar->bit_stream, 1 );
1424 MotionVector( p_vpar, p_mb, 0, i_s, 0, i_structure,
1425 p_vpar->picture.ppi_f_code[i_s][0] - 1,
1426 p_vpar->picture.ppi_f_code[i_s][1] - 1 );
1427 p_vpar->mb.pppi_pmv[1][i_s][0] = p_vpar->mb.pppi_pmv[0][i_s][0];
1428 p_vpar->mb.pppi_pmv[1][i_s][1] = p_vpar->mb.pppi_pmv[0][i_s][1];
1429 p_mb->pppi_motion_vectors[1][i_s][0] = p_vpar->mb.pppi_pmv[0][i_s][0];
1430 p_mb->pppi_motion_vectors[1][i_s][1] = p_vpar->mb.pppi_pmv[0][i_s][1];
1434 p_mb->ppi_field_select[0][i_s] = GetBits( &p_vpar->bit_stream, 1 );
1435 MotionVector( p_vpar, p_mb, 0, i_s, 0, i_structure,
1436 p_vpar->picture.ppi_f_code[i_s][0] - 1,
1437 p_vpar->picture.ppi_f_code[i_s][1] - 1 );
1438 p_mb->ppi_field_select[1][i_s] = GetBits( &p_vpar->bit_stream, 1 );
1439 MotionVector( p_vpar, p_mb, 1, i_s, 0, i_structure,
1440 p_vpar->picture.ppi_f_code[i_s][0] - 1,
1441 p_vpar->picture.ppi_f_code[i_s][1] - 1 );
1447 * Macroblock information structures
1450 /*****************************************************************************
1451 * MacroblockAddressIncrement : Get the macroblock_address_increment field
1452 *****************************************************************************/
1453 static __inline__ int MacroblockAddressIncrement( vpar_thread_t * p_vpar )
1456 /* Index in the lookup table mb_addr_inc */
1457 int i_index = ShowBits( &p_vpar->bit_stream, 11 );
1459 /* Test the presence of the escape character */
1460 while( i_index == 8 )
1462 RemoveBits( &p_vpar->bit_stream, 11 );
1464 i_index = ShowBits( &p_vpar->bit_stream, 11 );
1467 /* Affect the value from the lookup table */
1468 i_addr_inc += p_vpar->pl_mb_addr_inc[i_index].i_value;
1470 /* Dump the good number of bits */
1471 RemoveBits( &p_vpar->bit_stream, p_vpar->pl_mb_addr_inc[i_index].i_length );
1476 /*****************************************************************************
1477 * IMBType : macroblock_type in I pictures
1478 *****************************************************************************/
1479 static __inline__ int IMBType( vpar_thread_t * p_vpar )
1481 /* Take two bits for testing */
1482 int i_type = ShowBits( &p_vpar->bit_stream, 2 );
1484 /* Lookup table for macroblock_type */
1485 static lookup_t pl_mb_Itype[4] = { {MB_ERROR, 0},
1486 {MB_QUANT|MB_INTRA, 2},
1489 /* Dump the good number of bits */
1490 RemoveBits( &p_vpar->bit_stream, pl_mb_Itype[i_type].i_length );
1491 return pl_mb_Itype[i_type].i_value;
1494 /*****************************************************************************
1495 * PMBType : macroblock_type in P pictures
1496 *****************************************************************************/
1497 static __inline__ int PMBType( vpar_thread_t * p_vpar )
1499 /* Testing on 6 bits */
1500 int i_type = ShowBits( &p_vpar->bit_stream, 6 );
1502 /* Dump the good number of bits */
1503 RemoveBits( &p_vpar->bit_stream, p_vpar->ppl_mb_type[0][i_type].i_length );
1504 /* return the value from the lookup table for P type */
1505 return p_vpar->ppl_mb_type[0][i_type].i_value;
1508 /*****************************************************************************
1509 * BMBType : macroblock_type in B pictures
1510 *****************************************************************************/
1511 static __inline__ int BMBType( vpar_thread_t * p_vpar )
1513 /* Testing on 6 bits */
1514 int i_type = ShowBits( &p_vpar->bit_stream, 6 );
1516 /* Dump the good number of bits */
1517 RemoveBits( &p_vpar->bit_stream, p_vpar->ppl_mb_type[1][i_type].i_length );
1519 /* return the value from the lookup table for B type */
1520 return p_vpar->ppl_mb_type[1][i_type].i_value;
1523 /*****************************************************************************
1524 * DMBType : macroblock_type in D pictures
1525 *****************************************************************************/
1526 static __inline__ int DMBType( vpar_thread_t * p_vpar )
1528 return GetBits( &p_vpar->bit_stream, 1 );
1531 /*****************************************************************************
1532 * CodedPattern420 : coded_block_pattern with 4:2:0 chroma
1533 *****************************************************************************/
1534 static __inline__ int CodedPattern420( vpar_thread_t * p_vpar )
1536 /* Take the max 9 bits length vlc code for testing */
1537 int i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1539 /* Trash the good number of bits read in the lookup table */
1540 RemoveBits( &p_vpar->bit_stream, pl_coded_pattern[i_vlc].i_length );
1542 /* return the value from the vlc table */
1543 return pl_coded_pattern[i_vlc].i_value;
1546 /*****************************************************************************
1547 * CodedPattern422 : coded_block_pattern with 4:2:2 chroma
1548 *****************************************************************************/
1549 static __inline__ int CodedPattern422( vpar_thread_t * p_vpar )
1551 int i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1553 RemoveBits( &p_vpar->bit_stream, pl_coded_pattern[i_vlc].i_length );
1555 /* Supplementary 2 bits long code for 4:2:2 format */
1556 return pl_coded_pattern[i_vlc].i_value |
1557 (GetBits( &p_vpar->bit_stream, 2 ) << 6);
1560 /*****************************************************************************
1561 * CodedPattern444 : coded_block_pattern with 4:4:4 chroma
1562 *****************************************************************************/
1563 static __inline__ int CodedPattern444( vpar_thread_t * p_vpar )
1565 int i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1567 RemoveBits( &p_vpar->bit_stream, pl_coded_pattern[i_vlc].i_length );
1569 return pl_coded_pattern[i_vlc].i_value |
1570 (GetBits( &p_vpar->bit_stream, 6 ) << 6);
1573 /*****************************************************************************
1574 * InitMacroblock : Initialize macroblock values
1575 *****************************************************************************/
1576 static __inline__ void InitMacroblock( vpar_thread_t * p_vpar,
1577 macroblock_t * p_mb, int i_coding_type,
1580 int i_chroma_format = CHROMA_420; /* FIXME : MP@ML */
1582 p_mb->i_chroma_nb_blocks = 1 << i_chroma_format;
1583 p_mb->p_picture = p_vpar->picture.p_picture;
1585 if( i_coding_type == B_CODING_TYPE )
1586 p_mb->p_backward = p_vpar->sequence.p_backward;
1588 p_mb->p_backward = NULL;
1590 if( (i_coding_type == P_CODING_TYPE) || (i_coding_type == B_CODING_TYPE) )
1591 p_mb->p_forward = p_vpar->sequence.p_forward;
1593 p_mb->p_forward = NULL;
1595 p_mb->i_l_x = p_vpar->mb.i_l_x;
1596 p_mb->i_c_x = p_vpar->mb.i_c_x;
1597 p_mb->i_motion_l_y = p_vpar->mb.i_l_y;
1598 p_mb->i_motion_c_y = p_vpar->mb.i_c_y;
1599 if( (p_mb->b_motion_field = (i_structure == BOTTOM_FIELD)) )
1601 p_mb->i_motion_l_y--;
1602 p_mb->i_motion_c_y--;
1604 p_mb->i_addb_l_stride = (p_mb->i_l_stride = p_vpar->picture.i_l_stride) - 8;
1605 p_mb->i_addb_c_stride = (p_mb->i_c_stride = p_vpar->picture.i_c_stride) - 8;
1606 p_mb->b_P_second = ( (i_structure != p_vpar->picture.i_current_structure)
1607 && i_coding_type == P_CODING_TYPE );
1610 /*****************************************************************************
1611 * UpdateContext : Update the p_vpar contextual values
1612 *****************************************************************************/
1613 static __inline__ void UpdateContext( vpar_thread_t * p_vpar, int i_structure )
1615 /* Update macroblock real position. */
1616 p_vpar->mb.i_l_x += 16;
1617 p_vpar->mb.i_l_y += (p_vpar->mb.i_l_x / p_vpar->sequence.i_width)
1618 * (2 - (i_structure == FRAME_STRUCTURE)) * 16;
1619 p_vpar->mb.i_l_x %= p_vpar->sequence.i_width;
1621 p_vpar->mb.i_c_x += p_vpar->sequence.i_chroma_mb_width;
1622 p_vpar->mb.i_c_y += (p_vpar->mb.i_c_x / p_vpar->sequence.i_chroma_width)
1623 * (2 - (i_structure == FRAME_STRUCTURE))
1624 * p_vpar->sequence.i_chroma_mb_height;
1625 p_vpar->mb.i_c_x %= p_vpar->sequence.i_chroma_width;
1628 /*****************************************************************************
1629 * SkippedMacroblock : Generate a skipped macroblock with NULL motion vector
1630 *****************************************************************************/
1631 static __inline__ void SkippedMacroblock( vpar_thread_t * p_vpar, int i_mb,
1632 int i_mb_base, int i_coding_type,
1635 macroblock_t * p_mb;
1636 int i_chroma_format = CHROMA_420; /* FIXME : MP@ML */
1638 if( i_coding_type == I_CODING_TYPE )
1640 intf_WarnMsg( 2, "skipped macroblock in I-picture" );
1641 p_vpar->picture.b_error = 1;
1645 p_mb = p_vpar->pool.pf_new_mb( &p_vpar->pool );
1647 InitMacroblock( p_vpar, p_mb, i_coding_type, i_structure );
1649 /* Motion type is picture structure. */
1650 p_mb->pf_motion = p_vpar->ppf_motion_skipped[i_chroma_format]
1652 p_mb->i_coded_block_pattern = 0;
1654 /* Motion direction and motion vectors depend on the coding type. */
1655 if( i_coding_type == B_CODING_TYPE )
1657 p_mb->i_mb_type = p_vpar->mb.i_motion_dir;
1658 memcpy( p_mb->pppi_motion_vectors, p_vpar->mb.pppi_pmv,
1659 sizeof( p_vpar->mb.pppi_pmv ) );
1663 p_mb->i_mb_type = MB_MOTION_FORWARD;
1664 bzero8int( p_mb->pppi_motion_vectors );
1667 /* Set the field we use for motion compensation */
1668 p_mb->ppi_field_select[0][0] = p_mb->ppi_field_select[0][1]
1669 = ( i_structure == BOTTOM_FIELD );
1671 UpdateContext( p_vpar, i_structure );
1673 /* Decode the macroblock NOW ! */
1674 p_vpar->pool.pf_decode_mb( &p_vpar->pool, p_mb );
1677 /*****************************************************************************
1678 * MacroblockModes : Get the macroblock_modes structure
1679 *****************************************************************************/
1680 static __inline__ void MacroblockModes( vpar_thread_t * p_vpar,
1681 macroblock_t * p_mb,
1685 static int ppi_mv_count[2][4] = { {0, 1, 2, 1}, {0, 2, 1, 1} };
1686 static int ppi_mv_format[2][4] = { {0, 1, 1, 1}, {0, 1, 2, 1} };
1688 /* Get macroblock_type. */
1689 switch( i_coding_type )
1692 p_mb->i_mb_type = PMBType( p_vpar );
1695 p_mb->i_mb_type = BMBType( p_vpar );
1698 p_mb->i_mb_type = IMBType( p_vpar );
1701 p_mb->i_mb_type = DMBType( p_vpar );
1704 if( i_coding_type == B_CODING_TYPE )
1706 /* We need to remember the motion direction of the last macroblock
1707 * before a skipped macroblock (ISO/IEC 13818-2 7.6.6) */
1708 p_vpar->mb.i_motion_dir = p_mb->i_mb_type
1709 & (MB_MOTION_FORWARD | MB_MOTION_BACKWARD);
1712 /* SCALABILITY : warning, we don't know if spatial_temporal_weight_code
1713 * has to be dropped, take care if you use scalable streams. */
1714 /* RemoveBits( &p_vpar->bit_stream, 2 ); */
1716 if( (i_coding_type == P_CODING_TYPE || i_coding_type == B_CODING_TYPE)
1717 && (p_mb->i_mb_type & (MB_MOTION_FORWARD | MB_MOTION_BACKWARD)) )
1719 if( !(i_structure == FRAME_STRUCTURE
1720 && p_vpar->picture.b_frame_pred_frame_dct) )
1722 p_vpar->mb.i_motion_type = GetBits( &p_vpar->bit_stream, 2 );
1726 p_vpar->mb.i_motion_type = MOTION_FRAME;
1730 p_vpar->mb.i_mv_count = ppi_mv_count[i_structure == FRAME_STRUCTURE]
1731 [p_vpar->mb.i_motion_type];
1732 p_vpar->mb.i_mv_format = ppi_mv_format[i_structure == FRAME_STRUCTURE]
1733 [p_vpar->mb.i_motion_type];
1734 p_vpar->mb.b_dmv = p_vpar->mb.i_motion_type == MOTION_DMV;
1737 p_vpar->mb.b_dct_type = 0;
1738 if( (i_structure == FRAME_STRUCTURE) &&
1739 (!p_vpar->picture.b_frame_pred_frame_dct) &&
1740 (p_mb->i_mb_type & (MB_PATTERN|MB_INTRA)) )
1742 if( (p_vpar->mb.b_dct_type = GetBits( &p_vpar->bit_stream, 1 )) )
1744 /* The DCT is coded on fields. Jump one line between each
1746 p_mb->i_addb_l_stride <<= 1;
1747 p_mb->i_addb_l_stride += 8;
1750 /* With CHROMA_420, the DCT is necessarily frame-coded. */
1751 if( i_chroma_format != CHROMA_420 )
1753 p_mb->i_addb_c_stride <<= 1;
1754 p_mb->i_addb_c_stride += 8;
1761 /*****************************************************************************
1762 * ParseMacroblock : Parse the next macroblock
1763 *****************************************************************************/
1764 #define PARSEERROR \
1765 if( p_vpar->picture.b_error ) \
1767 /* Go to the next slice. */ \
1768 p_vpar->pool.pf_free_mb( &p_vpar->pool, p_mb ); \
1772 static __inline__ void ParseMacroblock(
1773 vpar_thread_t * p_vpar,
1774 int * pi_mb_address, /* previous address to be
1775 * used for mb_addr_incr */
1776 int i_mb_previous, /* actual previous mb */
1777 int i_mb_base, /* non-zero if field structure */
1778 /* The following parameters are explicit in
1779 * optimized routines : */
1780 boolean_t b_mpeg2, /* you know what ? */
1781 int i_coding_type, /* I, P, B or D */
1782 int i_structure ) /* T(OP), B(OTTOM) or F(RAME) */
1786 int i_chroma_format = CHROMA_420; /* FIXME : MP@ML */
1787 macroblock_t * p_mb;
1789 i_inc = MacroblockAddressIncrement( p_vpar );
1790 *pi_mb_address += i_inc;
1794 intf_WarnMsg( 2, "Bad address increment (%d)", i_inc );
1795 p_vpar->picture.b_error = 1;
1799 if( *pi_mb_address - i_mb_previous - 1 )
1801 /* Skipped macroblock (ISO/IEC 13818-2 7.6.6). */
1803 /* Reset DC predictors (7.2.1). */
1804 p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
1805 = p_vpar->mb.pi_dc_dct_pred[2]
1806 = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
1808 if( i_coding_type == P_CODING_TYPE )
1810 /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
1811 bzero8int( p_vpar->mb.pppi_pmv );
1814 for( i_mb = i_mb_previous + 1; i_mb < *pi_mb_address; i_mb++ )
1816 SkippedMacroblock( p_vpar, i_mb, i_mb_base, i_coding_type,
1821 /* Get a macroblock structure. */
1822 p_mb = p_vpar->pool.pf_new_mb( &p_vpar->pool );
1824 InitMacroblock( p_vpar, p_mb, i_coding_type, i_structure );
1826 /* Parse off macroblock_modes structure. */
1827 MacroblockModes( p_vpar, p_mb, i_coding_type, i_structure );
1829 if( p_mb->i_mb_type & MB_QUANT )
1831 LoadQuantizerScale( p_vpar );
1834 if( (i_coding_type == P_CODING_TYPE || i_coding_type == B_CODING_TYPE)
1835 && (p_mb->i_mb_type & MB_MOTION_FORWARD) )
1838 DecodeMVMPEG2( p_vpar, p_mb, 0, i_structure );
1840 DecodeMVMPEG1( p_vpar, p_mb, 0, i_structure );
1844 if( (i_coding_type == B_CODING_TYPE)
1845 && (p_mb->i_mb_type & MB_MOTION_BACKWARD) )
1848 DecodeMVMPEG2( p_vpar, p_mb, 1, i_structure );
1850 DecodeMVMPEG1( p_vpar, p_mb, 1, i_structure );
1854 if( i_coding_type == P_CODING_TYPE
1855 && !(p_mb->i_mb_type & (MB_MOTION_FORWARD|MB_INTRA)) )
1857 /* Special No-MC macroblock in P pictures (7.6.3.5). */
1858 p_mb->i_mb_type |= MB_MOTION_FORWARD;
1859 bzero8int( p_vpar->mb.pppi_pmv );
1860 bzero8int( p_mb->pppi_motion_vectors );
1861 p_vpar->mb.i_motion_type = 1 + (i_structure == FRAME_STRUCTURE);
1862 p_mb->ppi_field_select[0][0] = (i_structure == BOTTOM_FIELD);
1865 if( (i_coding_type != I_CODING_TYPE) && !(p_mb->i_mb_type & MB_INTRA) )
1867 /* Reset DC predictors (7.2.1). */
1868 p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
1869 = p_vpar->mb.pi_dc_dct_pred[2]
1870 = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
1872 /* Motion function pointer. */
1873 p_mb->pf_motion = p_vpar->pppf_motion[i_chroma_format]
1874 [i_structure == FRAME_STRUCTURE]
1875 [p_vpar->mb.i_motion_type];
1877 if( p_mb->i_mb_type & MB_PATTERN )
1879 switch( i_chroma_format )
1882 p_mb->i_coded_block_pattern = CodedPattern420( p_vpar );
1885 p_mb->i_coded_block_pattern = CodedPattern422( p_vpar );
1888 p_mb->i_coded_block_pattern = CodedPattern444( p_vpar );
1893 p_mb->i_coded_block_pattern = 0;
1897 * Effectively decode blocks.
1900 DecodeMPEG2NonIntraMB( p_vpar, p_mb );
1902 DecodeMPEG1NonIntraMB( p_vpar, p_mb );
1907 if( !p_vpar->picture.b_concealment_mv )
1909 /* Reset MV predictors. */
1910 bzero8int( p_vpar->mb.pppi_pmv );
1915 DecodeMVMPEG2( p_vpar, p_mb, 0, i_structure );
1917 DecodeMVMPEG1( p_vpar, p_mb, 0, i_structure );
1918 RemoveBits( &p_vpar->bit_stream, 1 );
1921 if( p_mb->i_mb_type & MB_PATTERN )
1923 switch( i_chroma_format )
1926 p_mb->i_coded_block_pattern = CodedPattern420( p_vpar );
1929 p_mb->i_coded_block_pattern = CodedPattern422( p_vpar );
1932 p_mb->i_coded_block_pattern = CodedPattern444( p_vpar );
1937 p_mb->i_coded_block_pattern =
1938 (1 << (4 + (1 << i_chroma_format))) - 1;
1942 * Effectively decode blocks.
1946 DecodeMPEG2IntraMB( p_vpar, p_mb );
1950 DecodeMPEG1IntraMB( p_vpar, p_mb );
1955 if( !p_vpar->picture.b_error )
1957 UpdateContext( p_vpar, i_structure );
1959 /* Decode the macroblock NOW ! */
1960 p_vpar->pool.pf_decode_mb( &p_vpar->pool, p_mb );
1964 /* Go to the next slice. */
1965 p_vpar->pool.pf_free_mb( &p_vpar->pool, p_mb );
1973 * Picture data parsing management
1976 /*****************************************************************************
1977 * SliceHeader : Parse the next slice structure
1978 *****************************************************************************/
1979 static __inline__ void SliceHeader( vpar_thread_t * p_vpar,
1980 int * pi_mb_address, int i_mb_base,
1981 u32 i_vert_code, boolean_t b_mpeg2,
1982 int i_coding_type, int i_structure )
1984 int i_mb_address_save = *pi_mb_address;
1987 /* FIXME : MP@ML doesn't support this. */
1990 /* Picture with more than 2800 lines. */
1991 i_vert_code += GetBits( &p_vpar->bit_stream, 3 ) << 7;
1995 /* DATA_PARTITIONING scalability. */
1996 RemoveBits( &p_vpar->bit_stream, 7 ); /* priority_breakpoint */
2000 LoadQuantizerScale( p_vpar );
2002 if( GetBits( &p_vpar->bit_stream, 1 ) )
2004 /* intra_slice, slice_id */
2005 RemoveBits( &p_vpar->bit_stream, 8 );
2006 /* extra_information_slice */
2007 while( GetBits( &p_vpar->bit_stream, 1 ) )
2009 RemoveBits( &p_vpar->bit_stream, 8 );
2012 *pi_mb_address = (i_vert_code - 1) * p_vpar->sequence.i_mb_width;
2014 if( *pi_mb_address < i_mb_address_save )
2016 intf_WarnMsg( 2, "Slices do not follow" );
2017 p_vpar->picture.b_error = 1;
2021 /* Reset DC coefficients predictors (ISO/IEC 13818-2 7.2.1). */
2022 p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
2023 = p_vpar->mb.pi_dc_dct_pred[2]
2024 = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
2026 /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
2027 bzero8int( p_vpar->mb.pppi_pmv );
2031 ParseMacroblock( p_vpar, pi_mb_address, i_mb_address_save,
2032 i_mb_base, b_mpeg2, i_coding_type, i_structure );
2034 i_mb_address_save = *pi_mb_address;
2035 if( p_vpar->picture.b_error )
2040 while( ShowBits( &p_vpar->bit_stream, 23 ) && !p_vpar->p_fifo->b_die );
2041 NextStartCode( &p_vpar->bit_stream );
2044 /*****************************************************************************
2045 * PictureData : Parse off all macroblocks (ISO/IEC 13818-2 6.2.3.7)
2046 *****************************************************************************/
2047 static __inline__ void vpar_PictureData( vpar_thread_t * p_vpar,
2048 int i_mb_base, boolean_t b_mpeg2,
2049 int i_coding_type, int i_structure )
2051 int i_mb_address = 0;
2054 NextStartCode( &p_vpar->bit_stream );
2055 while( ((i_coding_type != I_CODING_TYPE && i_coding_type != D_CODING_TYPE)
2056 || !p_vpar->picture.b_error)
2057 && i_mb_address < (p_vpar->sequence.i_mb_size
2058 >> (i_structure != FRAME_STRUCTURE))
2059 && !p_vpar->picture.b_error
2060 && !p_vpar->p_fifo->b_die )
2062 if( ((i_dummy = ShowBits( &p_vpar->bit_stream, 32 ))
2063 < SLICE_START_CODE_MIN) ||
2064 (i_dummy > SLICE_START_CODE_MAX) )
2066 intf_WarnMsg( 2, "Premature end of picture" );
2067 p_vpar->picture.b_error = 1;
2070 RemoveBits32( &p_vpar->bit_stream );
2072 /* Decode slice data. */
2073 SliceHeader( p_vpar, &i_mb_address, i_mb_base, i_dummy & 255,
2074 b_mpeg2, i_coding_type, i_structure );
2079 /* Try to recover from error. If we missed less than half the
2080 * number of macroblocks of the picture, mark the missed ones
2082 if( (i_coding_type == P_CODING_TYPE || i_coding_type == B_CODING_TYPE)
2083 && !p_vpar->sequence.b_expect_discontinuity
2084 && p_vpar->picture.b_error &&
2085 ( (i_mb_address-i_mb_base) > (p_vpar->sequence.i_mb_size >> 1)
2086 || (i_structure != FRAME_STRUCTURE
2087 && (i_mb_address-i_mb_base) >
2088 (p_vpar->sequence.i_mb_size >> 2) ) ) )
2092 p_vpar->picture.b_error = 0;
2093 for( i_mb = i_mb_address + 1;
2094 i_mb < (p_vpar->sequence.i_mb_size
2095 << (i_structure != FRAME_STRUCTURE));
2098 SkippedMacroblock( p_vpar, i_mb, i_mb_base,
2106 #define DECLARE_PICD( FUNCNAME, B_MPEG2, I_CODING_TYPE, I_STRUCTURE ) \
2107 void FUNCNAME( vpar_thread_t * p_vpar, int i_mb_base ) \
2109 vpar_PictureData( p_vpar, i_mb_base, B_MPEG2, I_CODING_TYPE, \
2113 DECLARE_PICD( vpar_PictureDataGENERIC, p_vpar->sequence.b_mpeg2,
2114 p_vpar->picture.i_coding_type, p_vpar->picture.i_structure );
2115 #if (VPAR_OPTIM_LEVEL > 0)
2116 DECLARE_PICD( vpar_PictureData1I, 0, I_CODING_TYPE, FRAME_STRUCTURE );
2117 DECLARE_PICD( vpar_PictureData1P, 0, P_CODING_TYPE, FRAME_STRUCTURE );
2118 DECLARE_PICD( vpar_PictureData1B, 0, B_CODING_TYPE, FRAME_STRUCTURE );
2119 DECLARE_PICD( vpar_PictureData1D, 0, D_CODING_TYPE, FRAME_STRUCTURE );
2120 DECLARE_PICD( vpar_PictureData2IF, 1, I_CODING_TYPE, FRAME_STRUCTURE );
2121 DECLARE_PICD( vpar_PictureData2PF, 1, P_CODING_TYPE, FRAME_STRUCTURE );
2122 DECLARE_PICD( vpar_PictureData2BF, 1, B_CODING_TYPE, FRAME_STRUCTURE );
2124 #if (VPAR_OPTIM_LEVEL > 1)
2125 DECLARE_PICD( vpar_PictureData2IT, 1, I_CODING_TYPE, TOP_FIELD );
2126 DECLARE_PICD( vpar_PictureData2PT, 1, P_CODING_TYPE, TOP_FIELD );
2127 DECLARE_PICD( vpar_PictureData2BT, 1, B_CODING_TYPE, TOP_FIELD );
2128 DECLARE_PICD( vpar_PictureData2IB, 1, I_CODING_TYPE, BOTTOM_FIELD );
2129 DECLARE_PICD( vpar_PictureData2PB, 1, P_CODING_TYPE, BOTTOM_FIELD );
2130 DECLARE_PICD( vpar_PictureData2BB, 1, B_CODING_TYPE, BOTTOM_FIELD );