1 /*****************************************************************************
2 * vpar_blocks.c : blocks parsing
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: vpar_blocks.c,v 1.75 2001/02/11 01:15:12 sam 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 "video_decoder.h"
47 #include "vdec_motion.h"
48 #include "../video_decoder/vdec_idct.h"
50 #include "vpar_blocks.h"
51 #include "../video_decoder/vpar_headers.h"
52 #include "../video_decoder/vpar_synchro.h"
53 #include "../video_decoder/video_parser.h"
54 #include "../video_decoder/video_fifo.h"
57 * Welcome to vpar_blocks.c ! Here's where the heavy processor-critical parsing
58 * task is done. This file is divided in several parts :
59 * - Initialization of the lookup tables
60 * - Decoding of coded blocks
61 * - Decoding of motion vectors
62 * - Decoding of the other macroblock structures
63 * - Picture data parsing management (slices and error handling)
64 * It's a pretty long file. Good luck and have a nice day.
69 * Initialization tables
72 /* Table for coded_block_pattern resolution */
73 static lookup_t pl_coded_pattern[512] =
74 { {MB_ERROR, 0}, {0, 9}, {39, 9}, {27, 9}, {59, 9}, {55, 9}, {47, 9}, {31, 9},
75 {58, 8}, {58, 8}, {54, 8}, {54, 8}, {46, 8}, {46, 8}, {30, 8}, {30, 8},
76 {57, 8}, {57, 8}, {53, 8}, {53, 8}, {45, 8}, {45, 8}, {29, 8}, {29, 8},
77 {38, 8}, {38, 8}, {26, 8}, {26, 8}, {37, 8}, {37, 8}, {25, 8}, {25, 8},
78 {43, 8}, {43, 8}, {23, 8}, {23, 8}, {51, 8}, {51, 8}, {15, 8}, {15, 8},
79 {42, 8}, {42, 8}, {22, 8}, {22, 8}, {50, 8}, {50, 8}, {14, 8}, {14, 8},
80 {41, 8}, {41, 8}, {21, 8}, {21, 8}, {49, 8}, {49, 8}, {13, 8}, {13, 8},
81 {35, 8}, {35, 8}, {19, 8}, {19, 8}, {11, 8}, {11, 8}, {7, 8}, {7, 8},
82 {34, 7}, {34, 7}, {34, 7}, {34, 7}, {18, 7}, {18, 7}, {18, 7}, {18, 7},
83 {10, 7}, {10, 7}, {10, 7}, {10, 7}, {6, 7}, {6, 7}, {6, 7}, {6, 7},
84 {33, 7}, {33, 7}, {33, 7}, {33, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7},
85 {9, 7}, {9, 7}, {9, 7}, {9, 7}, {5, 7}, {5, 7}, {5, 7}, {5, 7},
86 {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6},
87 {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6},
88 {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6},
89 {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6},
90 {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
91 {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
92 {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5},
93 {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5},
94 {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5},
95 {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5},
96 {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5},
97 {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5},
98 {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5},
99 {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5},
100 {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5},
101 {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5},
102 {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5},
103 {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5},
104 {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5},
105 {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5},
106 {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5},
107 {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5},
108 {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5},
109 {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5},
110 {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5},
111 {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5},
112 {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5},
113 {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5},
114 {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
115 {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
116 {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
117 {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
118 {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
119 {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
120 {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
121 {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
122 {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
123 {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
124 {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
125 {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
126 {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
127 {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
128 {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
129 {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
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},
132 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
133 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
134 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
135 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
136 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
137 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3} };
139 /* Tables for dc DCT coefficients
140 * Tables are cut in two parts to reduce memory occupation
143 /* Table B-12, dct_dc_size_luminance, codes 00XXX ... 11110 */
145 static lookup_t pl_dct_dc_lum_init_table_1[32] =
146 { {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
147 {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
148 {0, 3}, {0, 3}, {0, 3}, {0, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3},
149 {4, 3}, {4, 3}, {4, 3}, {4, 3}, {5, 4}, {5, 4}, {6, 5}, {MB_ERROR, 0}
152 static lookup_t ppl_dct_dc_init_table_1[2][32] =
153 { { {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
154 {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
155 {0, 3}, {0, 3}, {0, 3}, {0, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3},
156 {4, 3}, {4, 3}, {4, 3}, {4, 3}, {5, 4}, {5, 4}, {6, 5}, {MB_ERROR, 0}},
157 { {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2},
158 {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
159 {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
160 {3, 3}, {3, 3}, {3, 3}, {3, 3}, {4, 4}, {4, 4}, {5, 5}, {MB_ERROR, 0}
164 /* Table B-12, dct_dc_size_luminance, codes 111110xxx ... 111111111 */
166 static lookup_t pl_dct_dc_lum_init_table_2[32] =
167 { {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6},
168 {8, 7}, {8, 7}, {8, 7}, {8, 7}, {9, 8}, {9, 8}, {10,9}, {11,9},
169 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
170 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
171 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
172 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}
175 static lookup_t ppl_dct_dc_init_table_2[2][32] =
176 { { {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6},
177 {8, 7}, {8, 7}, {8, 7}, {8, 7}, {9, 8}, {9, 8}, {10,9}, {11,9},
178 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
179 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
180 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
181 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}},
182 { {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
183 {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
184 {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7},
185 {8, 8}, {8, 8}, {8, 8}, {8, 8}, {9, 9}, {9, 9}, {10,10}, {11,10}
189 /* Table B-13, dct_dc_size_chrominance, codes 00xxx ... 11110 */
190 static lookup_t pl_dct_dc_chrom_init_table_1[32] =
191 { {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2},
192 {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
193 {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
194 {3, 3}, {3, 3}, {3, 3}, {3, 3}, {4, 4}, {4, 4}, {5, 5}, {MB_ERROR, 0}
198 /* Table B-13, dct_dc_size_chrominance, codes 111110xxxx ... 1111111111 */
199 static lookup_t pl_dct_dc_chrom_init_table_2[32] =
200 { {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
201 {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
202 {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7},
203 {8, 8}, {8, 8}, {8, 8}, {8, 8}, {9, 9}, {9, 9}, {10,10}, {11,10}
207 /* Tables for ac DCT coefficients. There are cut in many parts to save space */
208 /* Table B-14, DCT coefficients table zero,
209 * codes 0100 ... 1xxx (used for first (DC) coefficient)
211 static dct_lookup_t pl_DCT_tab_dc[12] =
213 {0,2,4}, {2,1,4}, {1,1,3}, {1,1,3},
214 {0,1,1}, {0,1,1}, {0,1,1}, {0,1,1},
215 {0,1,1}, {0,1,1}, {0,1,1}, {0,1,1}
218 /* Table B-14, DCT coefficients table zero,
219 * codes 0100 ... 1xxx (used for all other coefficients)
221 static dct_lookup_t pl_DCT_tab_ac[12] =
223 {0,2,4}, {2,1,4}, {1,1,3}, {1,1,3},
224 {DCT_EOB,0,2}, {DCT_EOB,0,2}, {DCT_EOB,0,2}, {DCT_EOB,0,2}, /* EOB */
225 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2}
228 /* Table B-14, DCT coefficients table zero,
229 * codes 000001xx ... 00111xxx
231 static dct_lookup_t pl_DCT_tab0[60] =
233 {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6},
235 {2,2,7}, {2,2,7}, {9,1,7}, {9,1,7},
236 {0,4,7}, {0,4,7}, {8,1,7}, {8,1,7},
237 {7,1,6}, {7,1,6}, {7,1,6}, {7,1,6},
238 {6,1,6}, {6,1,6}, {6,1,6}, {6,1,6},
239 {1,2,6}, {1,2,6}, {1,2,6}, {1,2,6},
240 {5,1,6}, {5,1,6}, {5,1,6}, {5,1,6},
241 {13,1,8}, {0,6,8}, {12,1,8}, {11,1,8},
242 {3,2,8}, {1,3,8}, {0,5,8}, {10,1,8},
243 {0,3,5}, {0,3,5}, {0,3,5}, {0,3,5},
244 {0,3,5}, {0,3,5}, {0,3,5}, {0,3,5},
245 {4,1,5}, {4,1,5}, {4,1,5}, {4,1,5},
246 {4,1,5}, {4,1,5}, {4,1,5}, {4,1,5},
247 {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
248 {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5}
251 /* Table B-15, DCT coefficients table one,
252 * codes 000001xx ... 11111111
254 static dct_lookup_t pl_DCT_tab0a[252] =
256 {65,0,6}, {65,0,6}, {65,0,6}, {65,0,6}, /* Escape */
257 {7,1,7}, {7,1,7}, {8,1,7}, {8,1,7},
258 {6,1,7}, {6,1,7}, {2,2,7}, {2,2,7},
259 {0,7,6}, {0,7,6}, {0,7,6}, {0,7,6},
260 {0,6,6}, {0,6,6}, {0,6,6}, {0,6,6},
261 {4,1,6}, {4,1,6}, {4,1,6}, {4,1,6},
262 {5,1,6}, {5,1,6}, {5,1,6}, {5,1,6},
263 {1,5,8}, {11,1,8}, {0,11,8}, {0,10,8},
264 {13,1,8}, {12,1,8}, {3,2,8}, {1,4,8},
265 {2,1,5}, {2,1,5}, {2,1,5}, {2,1,5},
266 {2,1,5}, {2,1,5}, {2,1,5}, {2,1,5},
267 {1,2,5}, {1,2,5}, {1,2,5}, {1,2,5},
268 {1,2,5}, {1,2,5}, {1,2,5}, {1,2,5},
269 {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
270 {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
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 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
275 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
276 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
277 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
278 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
279 {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4}, /* EOB */
280 {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
281 {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
282 {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
283 {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
284 {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
285 {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
286 {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
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,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
299 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
300 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
301 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
302 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
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,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
307 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
308 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
309 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
310 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
311 {0,4,5}, {0,4,5}, {0,4,5}, {0,4,5},
312 {0,4,5}, {0,4,5}, {0,4,5}, {0,4,5},
313 {0,5,5}, {0,5,5}, {0,5,5}, {0,5,5},
314 {0,5,5}, {0,5,5}, {0,5,5}, {0,5,5},
315 {9,1,7}, {9,1,7}, {1,3,7}, {1,3,7},
316 {10,1,7}, {10,1,7}, {0,8,7}, {0,8,7},
317 {0,9,7}, {0,9,7}, {0,12,8}, {0,13,8},
318 {2,3,8}, {4,2,8}, {0,14,8}, {0,15,8}
321 /* Table B-14, DCT coefficients table zero,
322 * codes 0000001000 ... 0000001111
324 static dct_lookup_t pl_DCT_tab1[8] =
326 {16,1,10}, {5,2,10}, {0,7,10}, {2,3,10},
327 {1,4,10}, {15,1,10}, {14,1,10}, {4,2,10}
330 /* Table B-15, DCT coefficients table one,
331 * codes 000000100x ... 000000111x
333 static dct_lookup_t pl_DCT_tab1a[8] =
335 {5,2,9}, {5,2,9}, {14,1,9}, {14,1,9},
336 {2,4,10}, {16,1,10}, {15,1,9}, {15,1,9}
339 /* Table B-14/15, DCT coefficients table zero / one,
340 * codes 000000010000 ... 000000011111
342 static dct_lookup_t pl_DCT_tab2[16] =
344 {0,11,12}, {8,2,12}, {4,3,12}, {0,10,12},
345 {2,4,12}, {7,2,12}, {21,1,12}, {20,1,12},
346 {0,9,12}, {19,1,12}, {18,1,12}, {1,5,12},
347 {3,3,12}, {0,8,12}, {6,2,12}, {17,1,12}
350 /* Table B-14/15, DCT coefficients table zero / one,
351 * codes 0000000010000 ... 0000000011111
353 static dct_lookup_t pl_DCT_tab3[16] =
355 {10,2,13}, {9,2,13}, {5,3,13}, {3,4,13},
356 {2,5,13}, {1,7,13}, {1,6,13}, {0,15,13},
357 {0,14,13}, {0,13,13}, {0,12,13}, {26,1,13},
358 {25,1,13}, {24,1,13}, {23,1,13}, {22,1,13}
361 /* Table B-14/15, DCT coefficients table zero / one,
362 * codes 00000000010000 ... 00000000011111
364 static dct_lookup_t pl_DCT_tab4[16] =
366 {0,31,14}, {0,30,14}, {0,29,14}, {0,28,14},
367 {0,27,14}, {0,26,14}, {0,25,14}, {0,24,14},
368 {0,23,14}, {0,22,14}, {0,21,14}, {0,20,14},
369 {0,19,14}, {0,18,14}, {0,17,14}, {0,16,14}
372 /* Table B-14/15, DCT coefficients table zero / one,
373 * codes 000000000010000 ... 000000000011111
375 static dct_lookup_t pl_DCT_tab5[16] =
377 {0,40,15}, {0,39,15}, {0,38,15}, {0,37,15},
378 {0,36,15}, {0,35,15}, {0,34,15}, {0,33,15},
379 {0,32,15}, {1,14,15}, {1,13,15}, {1,12,15},
380 {1,11,15}, {1,10,15}, {1,9,15}, {1,8,15}
383 /* Table B-14/15, DCT coefficients table zero / one,
384 * codes 0000000000010000 ... 0000000000011111
386 static dct_lookup_t pl_DCT_tab6[16] =
388 {1,18,16}, {1,17,16}, {1,16,16}, {1,15,16},
389 {6,3,16}, {16,2,16}, {15,2,16}, {14,2,16},
390 {13,2,16}, {12,2,16}, {11,2,16}, {31,1,16},
391 {30,1,16}, {29,1,16}, {28,1,16}, {27,1,16}
394 /* Pointers on tables of dct coefficients */
395 static dct_lookup_t * ppl_dct_tab1[2] = { pl_DCT_tab_ac, pl_DCT_tab0a };
397 static dct_lookup_t * ppl_dct_tab2[2] = { pl_DCT_tab_ac, pl_DCT_tab_dc };
401 * Initialization of lookup tables
404 /*****************************************************************************
405 * vpar_InitMbAddrInc : Initialize the lookup table for mb_addr_inc
406 *****************************************************************************/
408 /* Function for filling up the lookup table for mb_addr_inc */
409 static void FillMbAddrIncTable( vpar_thread_t * p_vpar,
410 int i_start, int i_end, int i_step,
411 int * pi_value, int i_length )
414 for( i_pos = i_start ; i_pos < i_end ; i_pos += i_step )
416 for( i_offset = 0 ; i_offset < i_step ; i_offset ++ )
418 p_vpar->pl_mb_addr_inc[i_pos + i_offset].i_value = * pi_value;
419 p_vpar->pl_mb_addr_inc[i_pos + i_offset].i_length = i_length;
425 /* Function that initialize the table using the last one */
426 void vpar_InitMbAddrInc( vpar_thread_t * p_vpar )
431 for( i_dummy = 0 ; i_dummy < 8 ; i_dummy++ )
433 p_vpar->pl_mb_addr_inc[i_dummy].i_value = MB_ERROR;
434 p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
437 p_vpar->pl_mb_addr_inc[8].i_value = MB_ADDRINC_ESCAPE;
438 p_vpar->pl_mb_addr_inc[8].i_length = 11;
440 for( i_dummy = 9 ; i_dummy < 15 ; i_dummy ++ )
442 p_vpar->pl_mb_addr_inc[i_dummy].i_value = MB_ERROR;
443 p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
446 p_vpar->pl_mb_addr_inc[15].i_value = MB_ADDRINC_STUFFING;
447 p_vpar->pl_mb_addr_inc[15].i_length = 11;
449 for( i_dummy = 16; i_dummy < 24; i_dummy++ )
451 p_vpar->pl_mb_addr_inc[i_dummy].i_value = MB_ERROR;
452 p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
457 FillMbAddrIncTable( p_vpar, 24, 36, 1, &i_value, 11 );
458 FillMbAddrIncTable( p_vpar, 36, 48, 2, &i_value, 10 );
459 FillMbAddrIncTable( p_vpar, 48, 96, 8, &i_value, 8 );
460 FillMbAddrIncTable( p_vpar, 96, 128, 16, &i_value, 7 );
461 FillMbAddrIncTable( p_vpar, 128, 256, 64, &i_value, 5 );
462 FillMbAddrIncTable( p_vpar, 256, 512, 128, &i_value, 4 );
463 FillMbAddrIncTable( p_vpar, 512, 1024, 256, &i_value, 3 );
464 FillMbAddrIncTable( p_vpar, 1024, 2048, 1024, &i_value, 1 );
467 /*****************************************************************************
468 * vpar_Init*MBType : Initialize lookup table for the Macroblock type
469 *****************************************************************************/
471 /* Fonction for filling up the tables */
472 static void FillMBType( vpar_thread_t * p_vpar,
481 for( i_dummy = i_start ; i_dummy < i_end ; i_dummy++ )
483 p_vpar->ppl_mb_type[i_mb_type][i_dummy].i_value = i_value;
484 p_vpar->ppl_mb_type[i_mb_type][i_dummy].i_length = i_length;
488 /* Fonction that fills the table for P MB_Type */
489 void vpar_InitPMBType( vpar_thread_t * p_vpar )
491 FillMBType( p_vpar, 0, 32, 64, MB_MOTION_FORWARD|MB_PATTERN, 1 );
492 FillMBType( p_vpar, 0, 16, 32, MB_PATTERN, 2 );
493 FillMBType( p_vpar, 0, 8, 16, MB_MOTION_FORWARD, 3 );
494 FillMBType( p_vpar, 0, 6, 8, MB_INTRA, 5 );
495 FillMBType( p_vpar, 0, 4, 6, MB_QUANT|MB_MOTION_FORWARD|MB_PATTERN, 5 );
496 FillMBType( p_vpar, 0, 2, 4, MB_QUANT|MB_PATTERN, 5 );
497 p_vpar->ppl_mb_type[0][1].i_value = MB_QUANT|MB_INTRA;
498 p_vpar->ppl_mb_type[0][1].i_length = 6;
499 p_vpar->ppl_mb_type[0][0].i_value = MB_ERROR;
500 p_vpar->ppl_mb_type[0][0].i_length = 0;
503 /* Fonction that fills the table for B MB_Type */
504 void vpar_InitBMBType( vpar_thread_t * p_vpar )
506 FillMBType( p_vpar, 1, 48, 64, MB_MOTION_FORWARD
507 |MB_MOTION_BACKWARD|MB_PATTERN, 2 );
508 FillMBType( p_vpar, 1, 32, 48, MB_MOTION_FORWARD|MB_MOTION_BACKWARD, 2 );
509 FillMBType( p_vpar, 1, 24, 32, MB_MOTION_BACKWARD|MB_PATTERN, 3 );
510 FillMBType( p_vpar, 1, 16, 24, MB_MOTION_BACKWARD, 3 );
511 FillMBType( p_vpar, 1, 12, 16, MB_MOTION_FORWARD|MB_PATTERN, 4 );
512 FillMBType( p_vpar, 1, 8, 12, MB_MOTION_FORWARD, 4 );
513 FillMBType( p_vpar, 1, 6, 8, MB_INTRA, 5 );
514 FillMBType( p_vpar, 1, 4, 6, MB_QUANT|MB_MOTION_FORWARD
515 |MB_MOTION_BACKWARD|MB_PATTERN, 5 );
516 p_vpar->ppl_mb_type[1][3].i_value = MB_QUANT|MB_MOTION_FORWARD|MB_PATTERN;
517 p_vpar->ppl_mb_type[1][3].i_length = 6;
518 p_vpar->ppl_mb_type[1][2].i_value = MB_QUANT|MB_MOTION_BACKWARD|MB_PATTERN;
519 p_vpar->ppl_mb_type[1][2].i_length = 6;
520 p_vpar->ppl_mb_type[1][1].i_value = MB_QUANT|MB_INTRA;
521 p_vpar->ppl_mb_type[1][1].i_length = 6;
522 p_vpar->ppl_mb_type[1][0].i_value =MB_ERROR;
523 p_vpar->ppl_mb_type[1][0].i_length = 0;
527 /*****************************************************************************
528 * vpar_InitDCTTables : Initialize tables giving the length of the dct
529 * coefficient from the vlc code
530 *****************************************************************************/
532 /* First fonction for filling the table */
533 static void FillDCTTable( dct_lookup_t * p_tab_dest, dct_lookup_t * p_tab_src,
534 int i_step, int i_nb_elem, int i_offset )
536 int i_dummy, i_dummy2;
538 for( i_dummy=0 ; i_dummy < i_nb_elem ; i_dummy++ )
540 for( i_dummy2=0 ; i_dummy2 < i_step ; i_dummy2++ )
542 p_tab_dest[(i_dummy+i_offset)*i_step+i_dummy2] = p_tab_src[i_dummy];
548 /* Fonction that actually fills the table or create the pointers */
549 void vpar_InitDCTTables( vpar_thread_t * p_vpar )
551 /* Tables are cut in two parts to reduce memory occupation */
552 p_vpar->pppl_dct_dc_size[0][0] = pl_dct_dc_lum_init_table_1;
553 p_vpar->pppl_dct_dc_size[0][1] = pl_dct_dc_lum_init_table_2;
554 p_vpar->pppl_dct_dc_size[1][0] = pl_dct_dc_chrom_init_table_1;
555 p_vpar->pppl_dct_dc_size[1][1] = pl_dct_dc_chrom_init_table_2;
557 /* XXX?? MB_ERROR is replaced by 0 because if we use -1 we
558 * can block in DecodeMPEG2Intra and others */
559 memset( p_vpar->ppl_dct_coef[0], 0, 16 );
560 memset( p_vpar->ppl_dct_coef[1], 0, 16 );
562 /* For table B14 & B15, we have a pointer to tables */
563 /* We fill the table thanks to the fonction defined above */
564 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab0, 256, 60, 4 );
565 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab1, 64, 8, 8 );
566 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab2, 16, 16, 16 );
567 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab3, 8, 16, 16 );
568 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab4, 4, 16, 16 );
569 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab5, 2, 16, 16 );
570 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab6, 1, 16, 16 );
572 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab0a, 256, 60, 4 );
573 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab1a, 64, 8, 8 );
574 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab2, 16, 16, 16 );
575 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab3, 8, 16, 16 );
576 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab4, 4, 16, 16 );
577 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab5, 2, 16, 16 );
578 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab6, 1, 16, 16 );
581 /*****************************************************************************
582 * vpar_InitScanTable : Initialize scan table
583 *****************************************************************************/
584 void vpar_InitScanTable( vpar_thread_t * p_vpar )
588 memcpy( p_vpar->ppi_scan, pi_scan, sizeof(pi_scan) );
589 p_vpar->pf_norm_scan( p_vpar->ppi_scan );
591 /* If scan table has changed, we must change the quantization matrices. */
592 for( i = 0; i < 64; i++ )
594 p_vpar->pi_default_intra_quant[ p_vpar->ppi_scan[0][i] ] =
595 pi_default_intra_quant[ pi_scan[0][i] ];
596 p_vpar->pi_default_nonintra_quant[ p_vpar->ppi_scan[0][i] ] =
597 pi_default_nonintra_quant[ pi_scan[0][i] ];
606 #define SATURATE(val) \
609 else if( val < -2048 ) \
612 /*****************************************************************************
613 * DecodeMPEG1NonIntra : decode MPEG-1 non-intra blocks
614 *****************************************************************************/
615 static __inline__ void DecodeMPEG1NonIntra( vpar_thread_t * p_vpar,
616 macroblock_t * p_mb, int i_b,
617 boolean_t b_chroma, int i_cc )
628 dct_lookup_t * p_tab;
630 /* There should be no D picture in non-intra blocks */
631 if( p_vpar->picture.i_coding_type == D_CODING_TYPE )
632 intf_ErrMsg("vpar error : D-picture in non intra block");
634 /* Decoding of the AC coefficients */
640 for( i_parse = 0; !p_vpar->p_fifo->b_die; i_parse++ )
642 i_code = ShowBits( &p_vpar->bit_stream, 16 );
643 if( i_code >= 16384 )
645 p_tab = &ppl_dct_tab2[(i_parse == 0)][(i_code>>12)-4];
649 p_tab = &p_vpar->ppl_dct_coef[0][i_code];
651 i_run = p_tab->i_run;
652 i_level = p_tab->i_level;
653 i_length = p_tab->i_length;
655 RemoveBits( &p_vpar->bit_stream, i_length );
660 i_run = GetBits( &p_vpar->bit_stream, 6 );
661 i_level = GetBits( &p_vpar->bit_stream, 8 );
663 i_level = GetBits( &p_vpar->bit_stream, 8 );
664 else if (i_level == 128)
665 i_level = GetBits( &p_vpar->bit_stream, 8 ) - 256;
666 else if (i_level > 128)
669 if( (b_sign = i_level < 0) )
676 p_mb->pf_idct[i_b] = p_vpar->pf_sparse_idct;
677 p_mb->pi_sparse_pos[i_b] = i_coef;
681 p_mb->pf_idct[i_b] = p_vpar->pf_idct;
687 b_sign = GetBits( &p_vpar->bit_stream, 1 );
698 i_pos = p_vpar->ppi_scan[p_vpar->picture.b_alternate_scan][i_parse];
699 i_level = ( ((i_level << 1) + 1) * p_vpar->mb.i_quantizer_scale
700 * p_vpar->sequence.nonintra_quant.pi_matrix[i_pos] ) >> 4;
702 /* Mismatch control */
703 /* Equivalent to : if( (val & 1) == 0 ) val = val - 1; */
704 i_level = (i_level - 1) | 1;
706 i_level = b_sign ? -i_level : i_level;
710 p_mb->ppi_blocks[i_b][i_pos] = i_level;
713 intf_ErrMsg("vpar error: DCT coeff (non-intra) is out of bounds");
714 p_vpar->picture.b_error = 1;
717 /*****************************************************************************
718 * DecodeMPEG1Intra : decode MPEG-1 intra blocks
719 *****************************************************************************/
720 static __inline__ void DecodeMPEG1Intra( vpar_thread_t * p_vpar,
721 macroblock_t * p_mb, int i_b,
722 boolean_t b_chroma, int i_cc )
735 dct_lookup_t * p_tab;
737 u8 * pi_quant = p_vpar->sequence.intra_quant.pi_matrix;
740 i_code = ShowBits(&p_vpar->bit_stream, 5);
744 p_lookup = &ppl_dct_dc_init_table_1[b_chroma][i_code];
748 i_code = ShowBits(&p_vpar->bit_stream, (9+b_chroma)) - (0x1f0 * (b_chroma + 1));
749 p_lookup = &ppl_dct_dc_init_table_2[b_chroma][i_code];
751 i_dct_dc_size = p_lookup->i_value;
752 i_length = p_lookup->i_length;
753 RemoveBits( &p_vpar->bit_stream, i_length );
755 if (i_dct_dc_size == 0)
759 i_dct_dc_diff = GetBits( &p_vpar->bit_stream, i_dct_dc_size);
760 if ((i_dct_dc_diff & (1<<(i_dct_dc_size-1))) == 0)
761 i_dct_dc_diff -= (1<<i_dct_dc_size) - 1;
764 /* Read the actual code with the good length */
765 p_vpar->mb.pi_dc_dct_pred[i_cc] += i_dct_dc_diff;
767 p_mb->ppi_blocks[i_b][0] = p_vpar->mb.pi_dc_dct_pred[i_cc] << 3;
769 i_nc = ( p_vpar->mb.pi_dc_dct_pred[i_cc] != 0 );
771 if( p_vpar->picture.i_coding_type == D_CODING_TYPE )
773 /* Remove end_of_macroblock (always 1, prevents startcode emulation)
774 * ISO/IEC 11172-2 section 2.4.2.7 and 2.4.3.6 */
775 RemoveBits( &p_vpar->bit_stream, 1 );
776 /* D pictures do not have AC coefficients */
781 /* Decoding of the AC coefficients */
785 for( i_parse = 1; !p_vpar->p_fifo->b_die; i_parse++ )
787 i_code = ShowBits( &p_vpar->bit_stream, 16 );
788 /* We use 2 main tables for the coefficients */
789 if( i_code >= 16384 )
791 p_tab = &ppl_dct_tab1[0][(i_code>>12)-4];
795 p_tab = &p_vpar->ppl_dct_coef[0][i_code];
797 i_run = p_tab->i_run;
798 i_length = p_tab->i_length;
799 i_level = p_tab->i_level;
801 RemoveBits( &p_vpar->bit_stream, i_length );
806 i_run = GetBits( &p_vpar->bit_stream, 6 );
807 i_level = GetBits( &p_vpar->bit_stream, 8 );
809 i_level = GetBits( &p_vpar->bit_stream, 8 );
810 else if (i_level == 128)
811 i_level = GetBits( &p_vpar->bit_stream, 8 ) - 256;
812 else if (i_level > 128)
814 if( (b_sign = i_level < 0) )
820 p_mb->pf_idct[i_b] = p_vpar->pf_sparse_idct;
821 p_mb->pi_sparse_pos[i_b] = i_coef;
825 p_mb->pf_idct[i_b] = p_vpar->pf_idct;
831 b_sign = GetBits( &p_vpar->bit_stream, 1 );
834 /* Prepare the next block */
841 /* We have an error in the stream */
845 /* Determine the position of the block in the frame */
846 i_pos = p_vpar->ppi_scan[p_vpar->picture.b_alternate_scan][i_parse];
847 i_level = ( i_level *
848 p_vpar->mb.i_quantizer_scale * pi_quant[i_pos] ) >> 3;
850 /* Mismatch control */
851 /* Equivalent to : if( (val & 1) == 0 ) val = val - 1; */
852 i_level = (i_level - 1) | 1;
854 i_level = b_sign ? -i_level : i_level;
858 p_mb->ppi_blocks[i_b][i_pos] = i_level;
861 intf_ErrMsg("vpar error: DCT coeff (intra) is out of bounds");
862 p_vpar->picture.b_error = 1;
865 /*****************************************************************************
866 * DecodeMPEG2NonIntra : decode MPEG-2 non-intra blocks
867 *****************************************************************************/
868 static __inline__ void DecodeMPEG2NonIntra( vpar_thread_t * p_vpar,
869 macroblock_t * p_mb, int i_b,
870 boolean_t b_chroma, int i_cc )
883 dct_lookup_t * p_tab;
884 /* FIXME : if you want to enable other types of chroma, replace this
885 * by p_vpar->sequence.i_chroma_format. */
886 int i_chroma_format = CHROMA_420;
888 /* Give a pointer to the quantization matrices for intra blocks */
889 if( (i_chroma_format == CHROMA_420) || (!b_chroma) )
891 pi_quant = p_vpar->sequence.nonintra_quant.pi_matrix;
895 pi_quant = p_vpar->sequence.chroma_nonintra_quant.pi_matrix;
898 /* Decoding of the AC coefficients */
902 for( i_parse = 0; !p_vpar->p_fifo->b_die; i_parse++ )
904 i_code = ShowBits( &p_vpar->bit_stream, 16 );
905 if( i_code >= 16384 )
907 p_tab = &ppl_dct_tab2[(i_parse == 0)][(i_code>>12)-4];
911 p_tab = &p_vpar->ppl_dct_coef[0][i_code];
913 i_run = p_tab->i_run;
914 i_level = p_tab->i_level;
915 i_length = p_tab->i_length;
917 RemoveBits( &p_vpar->bit_stream, i_length );
922 i_run = GetBits( &p_vpar->bit_stream, 6 );
923 i_level = GetBits( &p_vpar->bit_stream, 12 );
924 i_level = (b_sign = ( i_level > 2047 )) ? 4096 - i_level
930 p_mb->pf_idct[i_b] = p_vpar->pf_sparse_idct;
931 p_mb->pi_sparse_pos[i_b] = i_coef;
935 p_mb->pf_idct[i_b] = p_vpar->pf_idct;
938 /* Mismatch control */
939 p_mb->ppi_blocks[i_b][63] ^= i_mismatch & 1;
944 b_sign = GetBits( &p_vpar->bit_stream, 1 );
955 i_pos = p_vpar->ppi_scan[p_vpar->picture.b_alternate_scan][i_parse];
956 i_level = ( ((i_level << 1) + 1) * p_vpar->mb.i_quantizer_scale
957 * pi_quant[i_pos] ) >> 5;
959 /* Could be replaced by (i_level ^ sign) - sign */
960 i_level = b_sign ? -i_level : i_level;
964 p_mb->ppi_blocks[i_b][i_pos] = i_level;
966 /* Mismatch control */
967 i_mismatch ^= i_level;
970 intf_ErrMsg("vpar error: DCT coeff (non-intra) is out of bounds");
971 p_vpar->picture.b_error = 1;
974 /*****************************************************************************
975 * DecodeMPEG2Intra : decode MPEG-2 intra blocks
976 *****************************************************************************/
977 static __inline__ void DecodeMPEG2Intra( vpar_thread_t * p_vpar,
978 macroblock_t * p_mb, int i_b,
979 boolean_t b_chroma, int i_cc )
992 boolean_t b_vlc_intra;
995 dct_lookup_t * p_tab;
997 /* FIXME : if you want to enable other types of chroma, replace this
998 * by p_vpar->sequence.i_chroma_format. */
999 int i_chroma_format = CHROMA_420;
1001 /* Give a pointer to the quantization matrices for intra blocks */
1002 if( (i_chroma_format == CHROMA_420) || (!b_chroma) )
1004 pi_quant = p_vpar->sequence.intra_quant.pi_matrix;
1008 pi_quant = p_vpar->sequence.chroma_intra_quant.pi_matrix;
1012 i_code = ShowBits( &p_vpar->bit_stream, 5 );
1016 p_lookup = &ppl_dct_dc_init_table_1[b_chroma][i_code];
1020 i_code = ShowBits(&p_vpar->bit_stream, (9+b_chroma)) - (0x1f0 * (b_chroma + 1));
1021 p_lookup = &ppl_dct_dc_init_table_2[b_chroma][i_code];
1023 i_dct_dc_size = p_lookup->i_value;
1024 i_length = p_lookup->i_length;
1025 RemoveBits( &p_vpar->bit_stream, i_length );
1027 if (i_dct_dc_size == 0)
1031 i_dct_dc_diff = GetBits( &p_vpar->bit_stream, i_dct_dc_size);
1032 if ((i_dct_dc_diff & (1<<(i_dct_dc_size-1))) == 0)
1033 i_dct_dc_diff -= (1<<i_dct_dc_size) - 1;
1036 /* Read the actual code with the good length */
1037 p_vpar->mb.pi_dc_dct_pred[i_cc] += i_dct_dc_diff;
1039 p_mb->ppi_blocks[i_b][0] = ( p_vpar->mb.pi_dc_dct_pred[i_cc] <<
1040 ( 3 - p_vpar->picture.i_intra_dc_precision ) );
1042 i_nc = ( p_vpar->mb.pi_dc_dct_pred[i_cc] != 0 );
1044 /* Decoding of the AC coefficients */
1047 b_vlc_intra = p_vpar->picture.b_intra_vlc_format;
1048 for( i_parse = 1; !p_vpar->p_fifo->b_die; i_parse++ )
1050 i_code = ShowBits( &p_vpar->bit_stream, 16 );
1051 /* We use 2 main tables for the coefficients */
1052 if( i_code >= 16384 )
1054 p_tab = &ppl_dct_tab1[b_vlc_intra][(i_code>>(12-(4*b_vlc_intra)))-4];
1058 p_tab = &p_vpar->ppl_dct_coef[b_vlc_intra][i_code];
1060 i_run = p_tab->i_run;
1061 i_level = p_tab->i_level;
1062 i_length = p_tab->i_length;
1064 RemoveBits( &p_vpar->bit_stream, i_length );
1069 i_run = GetBits( &p_vpar->bit_stream, 6 );
1070 i_level = GetBits( &p_vpar->bit_stream, 12 );
1071 i_level = (b_sign = ( i_level > 2047 )) ? 4096 - i_level
1077 p_mb->pf_idct[i_b] = p_vpar->pf_sparse_idct;
1078 p_mb->pi_sparse_pos[i_b] = i_coef;
1082 p_mb->pf_idct[i_b] = p_vpar->pf_idct;
1085 /* Mismatch control */
1086 p_mb->ppi_blocks[i_b][63] ^= i_mismatch & 1;
1091 b_sign = GetBits( &p_vpar->bit_stream, 1 );
1094 /* Prepare the next block */
1101 /* We have an error in the stream */
1105 /* Determine the position of the block in the frame */
1106 i_pos = p_vpar->ppi_scan[p_vpar->picture.b_alternate_scan][i_parse];
1107 i_level = ( i_level *
1108 p_vpar->mb.i_quantizer_scale *
1109 pi_quant[i_pos] ) >> 4;
1111 i_level = b_sign ? -i_level : i_level;
1113 SATURATE( i_level );
1115 p_mb->ppi_blocks[i_b][i_pos] = i_level;
1117 /* Mismatch control */
1118 i_mismatch ^= i_level;
1121 intf_ErrMsg("vpar error: DCT coeff (intra) is out of bounds");
1122 p_vpar->picture.b_error = 1;
1125 /*****************************************************************************
1126 * Decode*Blocks : decode blocks
1127 *****************************************************************************/
1128 static int pi_x[12] = {0,8,0,8,0,0,0,0,8,8,8,8};
1129 static int pi_y[2][12] = { {0,0,8,8,0,0,8,8,0,0,8,8},
1130 {0,0,1,1,0,0,1,1,0,0,1,1} };
1131 #define PARSEERROR \
1132 if( p_vpar->picture.b_error ) \
1137 #define DECODEBLOCKS( MBFUNC, BLOCKFUNC ) \
1138 static void MBFUNC( vpar_thread_t * p_vpar, macroblock_t * p_mb ) \
1140 /* FIXME : if you want to enable other types of chroma, replace this\
1141 * by p_vpar->sequence.i_chroma_format. */ \
1142 int i_chroma_format = CHROMA_420; \
1144 yuv_data_t * p_data1; \
1145 yuv_data_t * p_data2; \
1147 i_mask = 1 << (3 + (1 << i_chroma_format)); \
1150 p_data1 = p_mb->p_picture->p_y \
1151 + p_mb->i_l_x + p_vpar->mb.i_l_y * (p_vpar->sequence.i_width); \
1153 for( i_b = 0 ; i_b < 4 ; i_b++, i_mask >>= 1 ) \
1155 if( p_mb->i_coded_block_pattern & i_mask ) \
1157 memset( p_mb->ppi_blocks[i_b], 0, 64*sizeof(dctelem_t) ); \
1158 BLOCKFUNC( p_vpar, p_mb, i_b, 0, 0 ); \
1160 /* Calculate block coordinates. */ \
1161 p_mb->p_data[i_b] = p_data1 \
1162 + pi_y[p_vpar->mb.b_dct_type][i_b] \
1163 * p_vpar->picture.i_l_stride \
1171 p_data1 = p_mb->p_picture->p_u \
1173 + p_vpar->mb.i_c_y \
1174 * (p_vpar->sequence.i_chroma_width); \
1175 p_data2 = p_mb->p_picture->p_v \
1177 + p_vpar->mb.i_c_y \
1178 * (p_vpar->sequence.i_chroma_width); \
1180 for( i_b = 4; i_b < 4 + (1 << i_chroma_format); \
1181 i_b++, i_mask >>= 1 ) \
1183 yuv_data_t * pp_data[2] = {p_data1, p_data2}; \
1185 if( p_mb->i_coded_block_pattern & i_mask ) \
1187 memset( p_mb->ppi_blocks[i_b], 0, 64*sizeof(dctelem_t) ); \
1188 BLOCKFUNC( p_vpar, p_mb, i_b, 1, 1 + ((i_b - 4) & 1) ); \
1190 /* Calculate block coordinates. */ \
1191 p_mb->p_data[i_b] = pp_data[i_b & 1] \
1192 + pi_y[p_vpar->mb.b_dct_type][i_b] \
1193 * p_vpar->picture.i_c_stride \
1201 DECODEBLOCKS( DecodeMPEG1NonIntraMB, DecodeMPEG1NonIntra );
1202 DECODEBLOCKS( DecodeMPEG1IntraMB, DecodeMPEG1Intra );
1203 DECODEBLOCKS( DecodeMPEG2NonIntraMB, DecodeMPEG2NonIntra );
1204 DECODEBLOCKS( DecodeMPEG2IntraMB, DecodeMPEG2Intra );
1213 /****************************************************************************
1214 * MotionCode : Parse the next motion code
1215 ****************************************************************************/
1216 static __inline__ int MotionCode( vpar_thread_t * p_vpar )
1219 static lookup_t pl_mv_tab0[8] =
1220 { {-1,0}, {3,3}, {2,2}, {2,2}, {1,1}, {1,1}, {1,1}, {1,1} };
1221 /* Table B-10, motion_code, codes 0000011 ... 000011x */
1222 static lookup_t pl_mv_tab1[8] =
1223 { {-1,0}, {-1,0}, {-1,0}, {7,6}, {6,6}, {5,6}, {4,5}, {4,5} };
1224 /* Table B-10, motion_code, codes 0000001100 ... 000001011x */
1225 static lookup_t pl_mv_tab2[12] = {
1226 {16,9}, {15,9}, {14,9}, {13,9},
1227 {12,9}, {11,9}, {10,8}, {10,8},
1228 {9,8}, {9,8}, {8,8}, {8,8} };
1230 if( GetBits( &p_vpar->bit_stream, 1 ) )
1234 if( (i_code = ShowBits( &p_vpar->bit_stream, 9) ) >= 64 )
1237 RemoveBits( &p_vpar->bit_stream, pl_mv_tab0[i_code].i_length );
1238 return( GetBits( &p_vpar->bit_stream, 1 ) ?
1239 -pl_mv_tab0[i_code].i_value : pl_mv_tab0[i_code].i_value );
1245 RemoveBits( &p_vpar->bit_stream, pl_mv_tab1[i_code].i_length );
1246 return( GetBits( &p_vpar->bit_stream, 1 ) ?
1247 -pl_mv_tab1[i_code].i_value : pl_mv_tab1[i_code].i_value );
1250 if( (i_code -= 12) < 0 )
1252 p_vpar->picture.b_error = 1;
1253 intf_ErrMsg( "vpar error: Invalid motion_vector code" );
1257 RemoveBits( &p_vpar->bit_stream, pl_mv_tab2[i_code].i_length );
1258 return( GetBits( &p_vpar->bit_stream, 1 ) ?
1259 -pl_mv_tab2[i_code].i_value : pl_mv_tab2[i_code].i_value );
1262 /****************************************************************************
1263 * DecodeMotionVector : Decode a motion_vector
1264 ****************************************************************************/
1265 static __inline__ void DecodeMotionVector( int * pi_prediction, int i_r_size,
1266 int i_motion_code, int i_motion_residual, int i_full_pel )
1268 int i_limit, i_vector;
1270 /* ISO/IEC 13818-1 section 7.6.3.1 */
1271 i_limit = 16 << i_r_size;
1272 i_vector = *pi_prediction >> i_full_pel;
1274 if( i_motion_code > 0 )
1276 i_vector += ((i_motion_code-1) << i_r_size) + i_motion_residual + 1;
1277 if( i_vector >= i_limit )
1278 i_vector -= i_limit + i_limit;
1280 else if( i_motion_code < 0 )
1282 i_vector -= ((-i_motion_code-1) << i_r_size) + i_motion_residual + 1;
1283 if( i_vector < -i_limit )
1284 i_vector += i_limit + i_limit;
1286 *pi_prediction = i_vector << i_full_pel;
1289 /****************************************************************************
1290 * MotionVector : Parse the next motion_vector field
1291 ****************************************************************************/
1292 static __inline__ void MotionVector( vpar_thread_t * p_vpar,
1293 macroblock_t * p_mb, int i_r,
1294 int i_s, int i_full_pel, int i_structure,
1295 int i_h_r_size, int i_v_r_size )
1297 int i_motion_code, i_motion_residual;
1298 int pi_dm_vector[2];
1300 i_motion_code = MotionCode( p_vpar );
1301 i_motion_residual = (i_h_r_size != 0 && i_motion_code != 0) ?
1302 GetBits( &p_vpar->bit_stream, i_h_r_size) : 0;
1303 DecodeMotionVector( &p_vpar->mb.pppi_pmv[i_r][i_s][0], i_h_r_size,
1304 i_motion_code, i_motion_residual, i_full_pel );
1305 p_mb->pppi_motion_vectors[i_r][i_s][0] = p_vpar->mb.pppi_pmv[i_r][i_s][0];
1307 if( p_vpar->mb.b_dmv )
1309 if( GetBits(&p_vpar->bit_stream, 1) )
1311 pi_dm_vector[0] = GetBits( &p_vpar->bit_stream, 1 ) ? -1 : 1;
1315 pi_dm_vector[0] = 0;
1319 i_motion_code = MotionCode( p_vpar );
1320 i_motion_residual = (i_v_r_size != 0 && i_motion_code != 0) ?
1321 GetBits( &p_vpar->bit_stream, i_v_r_size) : 0;
1324 if( (p_vpar->mb.i_mv_format == MOTION_FIELD)
1325 && (i_structure == FRAME_STRUCTURE) )
1327 p_vpar->mb.pppi_pmv[i_r][i_s][1] >>= 1;
1330 DecodeMotionVector( &p_vpar->mb.pppi_pmv[i_r][i_s][1], i_v_r_size,
1331 i_motion_code, i_motion_residual, i_full_pel );
1333 if( (p_vpar->mb.i_mv_format == MOTION_FIELD)
1334 && (i_structure == FRAME_STRUCTURE) )
1335 p_vpar->mb.pppi_pmv[i_r][i_s][1] <<= 1;
1337 p_mb->pppi_motion_vectors[i_r][i_s][1] = p_vpar->mb.pppi_pmv[i_r][i_s][1];
1339 if( p_vpar->mb.b_dmv )
1341 if( GetBits(&p_vpar->bit_stream, 1) )
1343 pi_dm_vector[1] = GetBits( &p_vpar->bit_stream, 1 ) ? -1 : 1;
1347 pi_dm_vector[1] = 0;
1350 /* Dual Prime Arithmetic (ISO/IEC 13818-2 section 7.6.3.6). */
1352 #define i_mv_x p_mb->pppi_motion_vectors[0][0][0]
1353 if( i_structure == FRAME_STRUCTURE )
1355 #define i_mv_y (p_mb->pppi_motion_vectors[0][0][1] << 1)
1356 if( p_vpar->picture.b_top_field_first )
1358 /* vector for prediction of top field from bottom field */
1359 p_mb->ppi_dmv[0][0] = ((i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1360 p_mb->ppi_dmv[0][1] = ((i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] - 1;
1362 /* vector for prediction of bottom field from top field */
1363 p_mb->ppi_dmv[1][0] = ((3*i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1364 p_mb->ppi_dmv[1][1] = ((3*i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] + 1;
1368 /* vector for prediction of top field from bottom field */
1369 p_mb->ppi_dmv[0][0] = ((3*i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1370 p_mb->ppi_dmv[0][1] = ((3*i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] - 1;
1372 /* vector for prediction of bottom field from top field */
1373 p_mb->ppi_dmv[1][0] = ((i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1374 p_mb->ppi_dmv[1][1] = ((i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] + 1;
1380 #define i_mv_y p_mb->pppi_motion_vectors[0][0][1]
1381 /* vector for prediction from field of opposite 'parity' */
1382 p_mb->ppi_dmv[0][0] = ((i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1383 p_mb->ppi_dmv[0][1] = ((i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1];
1385 /* correct for vertical field shift */
1386 if( i_structure == TOP_FIELD )
1387 p_mb->ppi_dmv[0][1]--;
1389 p_mb->ppi_dmv[0][1]++;
1396 /*****************************************************************************
1397 * DecodeMVMPEG1 : Parse the next MPEG-1 motion vectors
1398 *****************************************************************************/
1399 static void DecodeMVMPEG1( vpar_thread_t * p_vpar,
1400 macroblock_t * p_mb, int i_s, int i_structure )
1402 int i_r_size = i_s ? p_vpar->picture.i_backward_f_code - 1 :
1403 p_vpar->picture.i_forward_f_code - 1;
1404 MotionVector( p_vpar, p_mb, 0, i_s,
1405 p_vpar->picture.pb_full_pel_vector[i_s], FRAME_STRUCTURE,
1406 i_r_size, i_r_size );
1409 /*****************************************************************************
1410 * DecodeMVMPEG2 : Parse the next MPEG-2 motion_vectors field
1411 *****************************************************************************/
1412 static void DecodeMVMPEG2( vpar_thread_t * p_vpar,
1413 macroblock_t * p_mb, int i_s, int i_structure )
1415 if( p_vpar->mb.i_mv_count == 1 )
1417 if( p_vpar->mb.i_mv_format == MOTION_FIELD && !p_vpar->mb.b_dmv )
1419 p_mb->ppi_field_select[0][i_s] = p_mb->ppi_field_select[1][i_s]
1420 = GetBits( &p_vpar->bit_stream, 1 );
1422 MotionVector( p_vpar, p_mb, 0, i_s, 0, i_structure,
1423 p_vpar->picture.ppi_f_code[i_s][0] - 1,
1424 p_vpar->picture.ppi_f_code[i_s][1] - 1 );
1425 p_vpar->mb.pppi_pmv[1][i_s][0] = p_vpar->mb.pppi_pmv[0][i_s][0];
1426 p_vpar->mb.pppi_pmv[1][i_s][1] = p_vpar->mb.pppi_pmv[0][i_s][1];
1427 p_mb->pppi_motion_vectors[1][i_s][0] = p_vpar->mb.pppi_pmv[0][i_s][0];
1428 p_mb->pppi_motion_vectors[1][i_s][1] = p_vpar->mb.pppi_pmv[0][i_s][1];
1432 p_mb->ppi_field_select[0][i_s] = GetBits( &p_vpar->bit_stream, 1 );
1433 MotionVector( p_vpar, p_mb, 0, i_s, 0, i_structure,
1434 p_vpar->picture.ppi_f_code[i_s][0] - 1,
1435 p_vpar->picture.ppi_f_code[i_s][1] - 1 );
1436 p_mb->ppi_field_select[1][i_s] = GetBits( &p_vpar->bit_stream, 1 );
1437 MotionVector( p_vpar, p_mb, 1, i_s, 0, i_structure,
1438 p_vpar->picture.ppi_f_code[i_s][0] - 1,
1439 p_vpar->picture.ppi_f_code[i_s][1] - 1 );
1445 * Macroblock information structures
1448 /*****************************************************************************
1449 * MacroblockAddressIncrement : Get the macroblock_address_increment field
1450 *****************************************************************************/
1451 static __inline__ int MacroblockAddressIncrement( vpar_thread_t * p_vpar )
1454 /* Index in the lookup table mb_addr_inc */
1455 int i_index = ShowBits( &p_vpar->bit_stream, 11 );
1457 /* Test the presence of the escape character */
1458 while( i_index == 8 )
1460 RemoveBits( &p_vpar->bit_stream, 11 );
1462 i_index = ShowBits( &p_vpar->bit_stream, 11 );
1465 /* Affect the value from the lookup table */
1466 i_addr_inc += p_vpar->pl_mb_addr_inc[i_index].i_value;
1468 /* Dump the good number of bits */
1469 RemoveBits( &p_vpar->bit_stream, p_vpar->pl_mb_addr_inc[i_index].i_length );
1474 /*****************************************************************************
1475 * IMBType : macroblock_type in I pictures
1476 *****************************************************************************/
1477 static __inline__ int IMBType( vpar_thread_t * p_vpar )
1479 /* Take two bits for testing */
1480 int i_type = ShowBits( &p_vpar->bit_stream, 2 );
1482 /* Lookup table for macroblock_type */
1483 static lookup_t pl_mb_Itype[4] = { {MB_ERROR, 0},
1484 {MB_QUANT|MB_INTRA, 2},
1487 /* Dump the good number of bits */
1488 RemoveBits( &p_vpar->bit_stream, pl_mb_Itype[i_type].i_length );
1489 return pl_mb_Itype[i_type].i_value;
1492 /*****************************************************************************
1493 * PMBType : macroblock_type in P pictures
1494 *****************************************************************************/
1495 static __inline__ int PMBType( vpar_thread_t * p_vpar )
1497 /* Testing on 6 bits */
1498 int i_type = ShowBits( &p_vpar->bit_stream, 6 );
1500 /* Dump the good number of bits */
1501 RemoveBits( &p_vpar->bit_stream, p_vpar->ppl_mb_type[0][i_type].i_length );
1502 /* return the value from the lookup table for P type */
1503 return p_vpar->ppl_mb_type[0][i_type].i_value;
1506 /*****************************************************************************
1507 * BMBType : macroblock_type in B pictures
1508 *****************************************************************************/
1509 static __inline__ int BMBType( vpar_thread_t * p_vpar )
1511 /* Testing on 6 bits */
1512 int i_type = ShowBits( &p_vpar->bit_stream, 6 );
1514 /* Dump the good number of bits */
1515 RemoveBits( &p_vpar->bit_stream, p_vpar->ppl_mb_type[1][i_type].i_length );
1517 /* return the value from the lookup table for B type */
1518 return p_vpar->ppl_mb_type[1][i_type].i_value;
1521 /*****************************************************************************
1522 * DMBType : macroblock_type in D pictures
1523 *****************************************************************************/
1524 static __inline__ int DMBType( vpar_thread_t * p_vpar )
1526 return GetBits( &p_vpar->bit_stream, 1 );
1529 /*****************************************************************************
1530 * CodedPattern420 : coded_block_pattern with 4:2:0 chroma
1531 *****************************************************************************/
1532 static __inline__ int CodedPattern420( vpar_thread_t * p_vpar )
1534 /* Take the max 9 bits length vlc code for testing */
1535 int i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1537 /* Trash the good number of bits read in the lookup table */
1538 RemoveBits( &p_vpar->bit_stream, pl_coded_pattern[i_vlc].i_length );
1540 /* return the value from the vlc table */
1541 return pl_coded_pattern[i_vlc].i_value;
1544 /*****************************************************************************
1545 * CodedPattern422 : coded_block_pattern with 4:2:2 chroma
1546 *****************************************************************************/
1547 static __inline__ int CodedPattern422( vpar_thread_t * p_vpar )
1549 int i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1551 RemoveBits( &p_vpar->bit_stream, pl_coded_pattern[i_vlc].i_length );
1553 /* Supplementary 2 bits long code for 4:2:2 format */
1554 return pl_coded_pattern[i_vlc].i_value |
1555 (GetBits( &p_vpar->bit_stream, 2 ) << 6);
1558 /*****************************************************************************
1559 * CodedPattern444 : coded_block_pattern with 4:4:4 chroma
1560 *****************************************************************************/
1561 static __inline__ int CodedPattern444( vpar_thread_t * p_vpar )
1563 int i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1565 RemoveBits( &p_vpar->bit_stream, pl_coded_pattern[i_vlc].i_length );
1567 return pl_coded_pattern[i_vlc].i_value |
1568 (GetBits( &p_vpar->bit_stream, 6 ) << 6);
1571 /*****************************************************************************
1572 * InitMacroblock : Initialize macroblock values
1573 *****************************************************************************/
1574 static __inline__ void InitMacroblock( vpar_thread_t * p_vpar,
1575 macroblock_t * p_mb, int i_coding_type,
1578 int i_chroma_format = CHROMA_420; /* FIXME : MP@ML */
1580 p_mb->i_chroma_nb_blocks = 1 << i_chroma_format;
1581 p_mb->p_picture = p_vpar->picture.p_picture;
1583 if( i_coding_type == B_CODING_TYPE )
1584 p_mb->p_backward = p_vpar->sequence.p_backward;
1586 p_mb->p_backward = NULL;
1587 if( (i_coding_type == P_CODING_TYPE) || (i_coding_type == B_CODING_TYPE) )
1588 p_mb->p_forward = p_vpar->sequence.p_forward;
1590 p_mb->p_forward = NULL;
1592 p_mb->i_l_x = p_vpar->mb.i_l_x;
1593 p_mb->i_c_x = p_vpar->mb.i_c_x;
1594 p_mb->i_motion_l_y = p_vpar->mb.i_l_y;
1595 p_mb->i_motion_c_y = p_vpar->mb.i_c_y;
1596 if( (p_mb->b_motion_field = (i_structure == BOTTOM_FIELD)) )
1598 p_mb->i_motion_l_y--;
1599 p_mb->i_motion_c_y--;
1601 p_mb->i_addb_l_stride = (p_mb->i_l_stride = p_vpar->picture.i_l_stride) - 8;
1602 p_mb->i_addb_c_stride = (p_mb->i_c_stride = p_vpar->picture.i_c_stride) - 8;
1603 p_mb->b_P_second = ( (i_structure != p_vpar->picture.i_current_structure)
1604 && i_coding_type == P_CODING_TYPE );
1607 /*****************************************************************************
1608 * UpdateContext : Update the p_vpar contextual values
1609 *****************************************************************************/
1610 static __inline__ void UpdateContext( vpar_thread_t * p_vpar, int i_structure )
1612 /* Update macroblock real position. */
1613 p_vpar->mb.i_l_x += 16;
1614 p_vpar->mb.i_l_y += (p_vpar->mb.i_l_x / p_vpar->sequence.i_width)
1615 * (2 - (i_structure == FRAME_STRUCTURE)) * 16;
1616 p_vpar->mb.i_l_x %= p_vpar->sequence.i_width;
1618 p_vpar->mb.i_c_x += p_vpar->sequence.i_chroma_mb_width;
1619 p_vpar->mb.i_c_y += (p_vpar->mb.i_c_x / p_vpar->sequence.i_chroma_width)
1620 * (2 - (i_structure == FRAME_STRUCTURE))
1621 * p_vpar->sequence.i_chroma_mb_height;
1622 p_vpar->mb.i_c_x %= p_vpar->sequence.i_chroma_width;
1625 /*****************************************************************************
1626 * SkippedMacroblock : Generate a skipped macroblock with NULL motion vector
1627 *****************************************************************************/
1628 static __inline__ void SkippedMacroblock( vpar_thread_t * p_vpar, int i_mb,
1629 int i_mb_base, int i_coding_type,
1632 macroblock_t * p_mb;
1633 int i_chroma_format = CHROMA_420; /* FIXME : MP@ML */
1635 if( i_coding_type == I_CODING_TYPE )
1637 intf_ErrMsg("vpar error: skipped macroblock in I-picture");
1638 p_vpar->picture.b_error = 1;
1642 if( (p_mb = vpar_NewMacroblock( &p_vpar->vfifo )) == NULL )
1648 p_vpar->picture.pp_mb[i_mb_base + i_mb] = p_mb;
1651 InitMacroblock( p_vpar, p_mb, i_coding_type, i_structure );
1653 /* Motion type is picture structure. */
1654 p_mb->pf_motion = p_vpar->ppf_motion_skipped[i_chroma_format]
1656 p_mb->i_coded_block_pattern = 0;
1658 /* Motion direction and motion vectors depend on the coding type. */
1659 if( i_coding_type == B_CODING_TYPE )
1661 p_mb->i_mb_type = p_vpar->mb.i_motion_dir;
1662 memcpy( p_mb->pppi_motion_vectors, p_vpar->mb.pppi_pmv,
1663 sizeof( p_vpar->mb.pppi_pmv ) );
1667 p_mb->i_mb_type = MB_MOTION_FORWARD;
1668 memset( p_mb->pppi_motion_vectors, 0, 8*sizeof(int) );
1671 /* Set the field we use for motion compensation */
1672 p_mb->ppi_field_select[0][0] = p_mb->ppi_field_select[0][1]
1673 = ( i_structure == BOTTOM_FIELD );
1675 UpdateContext( p_vpar, i_structure );
1678 /* Decode the macroblock NOW ! */
1679 vpar_DecodeMacroblock ( &p_vpar->vfifo, p_mb );
1683 /*****************************************************************************
1684 * MacroblockModes : Get the macroblock_modes structure
1685 *****************************************************************************/
1686 static __inline__ void MacroblockModes( vpar_thread_t * p_vpar,
1687 macroblock_t * p_mb,
1691 static int ppi_mv_count[2][4] = { {0, 1, 2, 1}, {0, 2, 1, 1} };
1692 static int ppi_mv_format[2][4] = { {0, 1, 1, 1}, {0, 1, 2, 1} };
1694 /* Get macroblock_type. */
1695 switch( i_coding_type )
1698 p_mb->i_mb_type = PMBType( p_vpar );
1701 p_mb->i_mb_type = BMBType( p_vpar );
1704 p_mb->i_mb_type = IMBType( p_vpar );
1707 p_mb->i_mb_type = DMBType( p_vpar );
1710 if( i_coding_type == B_CODING_TYPE )
1712 /* We need to remember the motion direction of the last macroblock
1713 * before a skipped macroblock (ISO/IEC 13818-2 7.6.6) */
1714 p_vpar->mb.i_motion_dir = p_mb->i_mb_type
1715 & (MB_MOTION_FORWARD | MB_MOTION_BACKWARD);
1718 /* SCALABILITY : warning, we don't know if spatial_temporal_weight_code
1719 * has to be dropped, take care if you use scalable streams. */
1720 /* RemoveBits( &p_vpar->bit_stream, 2 ); */
1722 if( (i_coding_type == P_CODING_TYPE || i_coding_type == B_CODING_TYPE)
1723 && (p_mb->i_mb_type & (MB_MOTION_FORWARD | MB_MOTION_BACKWARD)) )
1725 if( !(i_structure == FRAME_STRUCTURE
1726 && p_vpar->picture.b_frame_pred_frame_dct) )
1728 p_vpar->mb.i_motion_type = GetBits( &p_vpar->bit_stream, 2 );
1732 p_vpar->mb.i_motion_type = MOTION_FRAME;
1736 p_vpar->mb.i_mv_count = ppi_mv_count[i_structure == FRAME_STRUCTURE]
1737 [p_vpar->mb.i_motion_type];
1738 p_vpar->mb.i_mv_format = ppi_mv_format[i_structure == FRAME_STRUCTURE]
1739 [p_vpar->mb.i_motion_type];
1740 p_vpar->mb.b_dmv = p_vpar->mb.i_motion_type == MOTION_DMV;
1743 p_vpar->mb.b_dct_type = 0;
1744 if( (i_structure == FRAME_STRUCTURE) &&
1745 (!p_vpar->picture.b_frame_pred_frame_dct) &&
1746 (p_mb->i_mb_type & (MB_PATTERN|MB_INTRA)) )
1748 if( (p_vpar->mb.b_dct_type = GetBits( &p_vpar->bit_stream, 1 )) )
1750 /* The DCT is coded on fields. Jump one line between each
1752 p_mb->i_addb_l_stride <<= 1;
1753 p_mb->i_addb_l_stride += 8;
1756 /* With CHROMA_420, the DCT is necessarily frame-coded. */
1757 if( i_chroma_format != CHROMA_420 )
1759 p_mb->i_addb_c_stride <<= 1;
1760 p_mb->i_addb_c_stride += 8;
1767 /*****************************************************************************
1768 * ParseMacroblock : Parse the next macroblock
1769 *****************************************************************************/
1770 #define PARSEERROR \
1771 if( p_vpar->picture.b_error ) \
1773 /* Mark this block as skipped (better than green blocks), and \
1774 * go to the next slice. */ \
1775 (*pi_mb_address)--; \
1776 vpar_DestroyMacroblock( &p_vpar->vfifo, p_mb ); \
1780 static __inline__ void ParseMacroblock(
1781 vpar_thread_t * p_vpar,
1782 int * pi_mb_address, /* previous address to be
1783 * used for mb_addr_incr */
1784 int i_mb_previous, /* actual previous mb */
1785 int i_mb_base, /* non-zero if field structure */
1786 /* The following parameters are explicit in
1787 * optimized routines : */
1788 boolean_t b_mpeg2, /* you know what ? */
1789 int i_coding_type, /* I, P, B or D */
1790 int i_structure ) /* T(OP), B(OTTOM) or F(RAME) */
1794 int i_chroma_format = CHROMA_420; /* FIXME : MP@ML */
1795 macroblock_t * p_mb;
1797 i_inc = MacroblockAddressIncrement( p_vpar );
1798 *pi_mb_address += i_inc;
1802 intf_ErrMsg( "vpar error: bad address increment (%d)", i_inc );
1803 p_vpar->picture.b_error = 1;
1807 if( *pi_mb_address - i_mb_previous - 1 )
1809 /* Skipped macroblock (ISO/IEC 13818-2 7.6.6). */
1811 /* Reset DC predictors (7.2.1). */
1812 p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
1813 = p_vpar->mb.pi_dc_dct_pred[2]
1814 = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
1816 if( i_coding_type == P_CODING_TYPE )
1818 /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
1819 memset( p_vpar->mb.pppi_pmv, 0, 8*sizeof(int) );
1822 for( i_mb = i_mb_previous + 1; i_mb < *pi_mb_address; i_mb++ )
1824 SkippedMacroblock( p_vpar, i_mb, i_mb_base, i_coding_type,
1829 /* Get a macroblock structure. */
1830 if( (p_mb = vpar_NewMacroblock( &p_vpar->vfifo )) == NULL )
1836 p_vpar->picture.pp_mb[i_mb_base + *pi_mb_address] = p_mb;
1839 InitMacroblock( p_vpar, p_mb, i_coding_type, i_structure );
1841 /* Parse off macroblock_modes structure. */
1842 MacroblockModes( p_vpar, p_mb, i_coding_type, i_structure );
1844 if( p_mb->i_mb_type & MB_QUANT )
1846 LoadQuantizerScale( p_vpar );
1849 if( (i_coding_type == P_CODING_TYPE || i_coding_type == B_CODING_TYPE)
1850 && (p_mb->i_mb_type & MB_MOTION_FORWARD) )
1853 DecodeMVMPEG2( p_vpar, p_mb, 0, i_structure );
1855 DecodeMVMPEG1( p_vpar, p_mb, 0, i_structure );
1859 if( (i_coding_type == B_CODING_TYPE)
1860 && (p_mb->i_mb_type & MB_MOTION_BACKWARD) )
1863 DecodeMVMPEG2( p_vpar, p_mb, 1, i_structure );
1865 DecodeMVMPEG1( p_vpar, p_mb, 1, i_structure );
1869 if( i_coding_type == P_CODING_TYPE
1870 && !(p_mb->i_mb_type & (MB_MOTION_FORWARD|MB_INTRA)) )
1872 /* Special No-MC macroblock in P pictures (7.6.3.5). */
1873 p_mb->i_mb_type |= MB_MOTION_FORWARD;
1874 memset( p_vpar->mb.pppi_pmv, 0, 8*sizeof(int) );
1875 memset( p_mb->pppi_motion_vectors, 0, 8*sizeof(int) );
1876 p_vpar->mb.i_motion_type = 1 + (i_structure == FRAME_STRUCTURE);
1877 p_mb->ppi_field_select[0][0] = (i_structure == BOTTOM_FIELD);
1880 if( (i_coding_type != I_CODING_TYPE) && !(p_mb->i_mb_type & MB_INTRA) )
1882 /* Reset DC predictors (7.2.1). */
1883 p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
1884 = p_vpar->mb.pi_dc_dct_pred[2]
1885 = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
1887 /* Motion function pointer. */
1888 p_mb->pf_motion = p_vpar->pppf_motion[i_chroma_format]
1889 [i_structure == FRAME_STRUCTURE]
1890 [p_vpar->mb.i_motion_type];
1892 if( p_mb->i_mb_type & MB_PATTERN )
1894 switch( i_chroma_format )
1897 p_mb->i_coded_block_pattern = CodedPattern420( p_vpar );
1900 p_mb->i_coded_block_pattern = CodedPattern422( p_vpar );
1903 p_mb->i_coded_block_pattern = CodedPattern444( p_vpar );
1908 p_mb->i_coded_block_pattern = 0;
1912 * Effectively decode blocks.
1915 DecodeMPEG2NonIntraMB( p_vpar, p_mb );
1917 DecodeMPEG1NonIntraMB( p_vpar, p_mb );
1922 if( !p_vpar->picture.b_concealment_mv )
1924 /* Reset MV predictors. */
1925 memset( p_vpar->mb.pppi_pmv, 0, 8*sizeof(int) );
1930 DecodeMVMPEG2( p_vpar, p_mb, 0, i_structure );
1932 DecodeMVMPEG1( p_vpar, p_mb, 0, i_structure );
1933 RemoveBits( &p_vpar->bit_stream, 1 );
1936 if( p_mb->i_mb_type & MB_PATTERN )
1938 switch( i_chroma_format )
1941 p_mb->i_coded_block_pattern = CodedPattern420( p_vpar );
1944 p_mb->i_coded_block_pattern = CodedPattern422( p_vpar );
1947 p_mb->i_coded_block_pattern = CodedPattern444( p_vpar );
1952 p_mb->i_coded_block_pattern =
1953 (1 << (4 + (1 << i_chroma_format))) - 1;
1957 * Effectively decode blocks.
1960 DecodeMPEG2IntraMB( p_vpar, p_mb );
1962 DecodeMPEG1IntraMB( p_vpar, p_mb );
1966 if( !p_vpar->picture.b_error )
1968 UpdateContext( p_vpar, i_structure );
1970 /* Decode the macroblock NOW ! */
1971 vpar_DecodeMacroblock ( &p_vpar->vfifo, p_mb );
1976 /* Mark this block as skipped (better than green blocks), and go
1977 * to the next slice. */
1979 vpar_DestroyMacroblock( &p_vpar->vfifo, p_mb );
1987 * Picture data parsing management
1990 /*****************************************************************************
1991 * SliceHeader : Parse the next slice structure
1992 *****************************************************************************/
1993 static __inline__ void SliceHeader( vpar_thread_t * p_vpar,
1994 int * pi_mb_address, int i_mb_base,
1995 u32 i_vert_code, boolean_t b_mpeg2,
1996 int i_coding_type, int i_structure )
1998 int i_mb_address_save = *pi_mb_address;
2000 p_vpar->picture.b_error = 0;
2003 /* FIXME : MP@ML doesn't support this. */
2006 /* Picture with more than 2800 lines. */
2007 i_vert_code += GetBits( &p_vpar->bit_stream, 3 ) << 7;
2011 /* DATA_PARTITIONING scalability. */
2012 RemoveBits( &p_vpar->bit_stream, 7 ); /* priority_breakpoint */
2016 LoadQuantizerScale( p_vpar );
2018 if( GetBits( &p_vpar->bit_stream, 1 ) )
2020 /* intra_slice, slice_id */
2021 RemoveBits( &p_vpar->bit_stream, 8 );
2022 /* extra_information_slice */
2023 while( GetBits( &p_vpar->bit_stream, 1 ) )
2025 RemoveBits( &p_vpar->bit_stream, 8 );
2028 *pi_mb_address = (i_vert_code - 1) * p_vpar->sequence.i_mb_width;
2030 if( *pi_mb_address < i_mb_address_save )
2032 intf_ErrMsg( "vpar error: slices do not follow, maybe a PES has been trashed" );
2033 p_vpar->picture.b_error = 1;
2037 /* Reset DC coefficients predictors (ISO/IEC 13818-2 7.2.1). */
2038 p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
2039 = p_vpar->mb.pi_dc_dct_pred[2]
2040 = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
2042 /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
2043 memset( p_vpar->mb.pppi_pmv, 0, 8*sizeof(int) );
2047 ParseMacroblock( p_vpar, pi_mb_address, i_mb_address_save,
2048 i_mb_base, b_mpeg2, i_coding_type, i_structure );
2050 i_mb_address_save = *pi_mb_address;
2051 if( p_vpar->picture.b_error )
2056 while( ShowBits( &p_vpar->bit_stream, 23 ) && !p_vpar->p_fifo->b_die );
2057 NextStartCode( &p_vpar->bit_stream );
2060 /*****************************************************************************
2061 * PictureData : Parse off all macroblocks (ISO/IEC 13818-2 6.2.3.7)
2062 *****************************************************************************/
2063 static __inline__ void vpar_PictureData( vpar_thread_t * p_vpar,
2064 int i_mb_base, boolean_t b_mpeg2,
2065 int i_coding_type, int i_structure )
2067 int i_mb_address = 0;
2070 NextStartCode( &p_vpar->bit_stream );
2071 while( ((i_coding_type != I_CODING_TYPE && i_coding_type != D_CODING_TYPE)
2072 || !p_vpar->picture.b_error)
2073 && i_mb_address < (p_vpar->sequence.i_mb_size
2074 >> (i_structure != FRAME_STRUCTURE))
2075 && !p_vpar->p_fifo->b_die )
2077 if( ((i_dummy = ShowBits( &p_vpar->bit_stream, 32 ))
2078 < SLICE_START_CODE_MIN) ||
2079 (i_dummy > SLICE_START_CODE_MAX) )
2081 intf_ErrMsg("vpar error: premature end of picture");
2082 p_vpar->picture.b_error = 1;
2085 RemoveBits32( &p_vpar->bit_stream );
2087 /* Decode slice data. */
2088 SliceHeader( p_vpar, &i_mb_address, i_mb_base, i_dummy & 255,
2089 b_mpeg2, i_coding_type, i_structure );
2092 /* Try to recover from error. If we missed less than half the
2093 * number of macroblocks of the picture, mark the missed ones
2095 if( (i_coding_type == P_CODING_TYPE || i_coding_type == B_CODING_TYPE)
2096 && p_vpar->picture.b_error &&
2097 ( (i_mb_address-i_mb_base) > (p_vpar->sequence.i_mb_size >> 1)
2098 || (i_structure != FRAME_STRUCTURE
2099 && (i_mb_address-i_mb_base) >
2100 (p_vpar->sequence.i_mb_size >> 2) ) ) )
2104 p_vpar->picture.b_error = 0;
2105 for( i_mb = i_mb_address + 1;
2106 i_mb < (p_vpar->sequence.i_mb_size
2107 << (i_structure != FRAME_STRUCTURE));
2110 SkippedMacroblock( p_vpar, i_mb, i_mb_base,
2117 #define DECLARE_PICD( FUNCNAME, B_MPEG2, I_CODING_TYPE, I_STRUCTURE ) \
2118 void FUNCNAME( vpar_thread_t * p_vpar, int i_mb_base ) \
2120 vpar_PictureData( p_vpar, i_mb_base, B_MPEG2, I_CODING_TYPE, \
2124 DECLARE_PICD( vpar_PictureDataGENERIC, p_vpar->sequence.b_mpeg2,
2125 p_vpar->picture.i_coding_type, p_vpar->picture.i_structure );
2126 #if (VPAR_OPTIM_LEVEL > 0)
2127 DECLARE_PICD( vpar_PictureData1I, 0, I_CODING_TYPE, FRAME_STRUCTURE );
2128 DECLARE_PICD( vpar_PictureData1P, 0, P_CODING_TYPE, FRAME_STRUCTURE );
2129 DECLARE_PICD( vpar_PictureData1B, 0, B_CODING_TYPE, FRAME_STRUCTURE );
2130 DECLARE_PICD( vpar_PictureData1D, 0, D_CODING_TYPE, FRAME_STRUCTURE );
2131 DECLARE_PICD( vpar_PictureData2IF, 1, I_CODING_TYPE, FRAME_STRUCTURE );
2132 DECLARE_PICD( vpar_PictureData2PF, 1, P_CODING_TYPE, FRAME_STRUCTURE );
2133 DECLARE_PICD( vpar_PictureData2BF, 1, B_CODING_TYPE, FRAME_STRUCTURE );
2135 #if (VPAR_OPTIM_LEVEL > 1)
2136 DECLARE_PICD( vpar_PictureData2IT, 1, I_CODING_TYPE, TOP_FIELD );
2137 DECLARE_PICD( vpar_PictureData2PT, 1, P_CODING_TYPE, TOP_FIELD );
2138 DECLARE_PICD( vpar_PictureData2BT, 1, B_CODING_TYPE, TOP_FIELD );
2139 DECLARE_PICD( vpar_PictureData2IB, 1, I_CODING_TYPE, BOTTOM_FIELD );
2140 DECLARE_PICD( vpar_PictureData2PB, 1, P_CODING_TYPE, BOTTOM_FIELD );
2141 DECLARE_PICD( vpar_PictureData2BB, 1, B_CODING_TYPE, BOTTOM_FIELD );