1 /*****************************************************************************
2 * vpar_blocks.c : blocks parsing
4 *****************************************************************************/
6 /*****************************************************************************
8 *****************************************************************************/
19 #include "vlc_thread.h"
22 #include "debug.h" /* ?? temporaire, requis par netlist.h */
25 #include "input_netlist.h"
26 #include "decoder_fifo.h"
28 #include "video_output.h"
30 #include "vdec_idct.h"
31 #include "video_decoder.h"
32 #include "vdec_motion.h"
34 #include "vpar_blocks.h"
35 #include "vpar_headers.h"
36 #include "vpar_synchro.h"
37 #include "video_parser.h"
38 #include "video_fifo.h"
41 * Welcome to vpar_blocks.c ! Here's where the heavy processor-critical parsing
42 * task is done. This file is divided in several parts :
43 * - Initialization of the lookup tables
44 * - Decoding of coded blocks
45 * - Decoding of motion vectors
46 * - Decoding of the other macroblock structures
47 * - Picture data parsing management (slices and error handling)
48 * It's a pretty long file. Good luck and have a nice day.
53 * Initialization tables
56 /* Table for coded_block_pattern resolution */
57 static lookup_t pl_coded_pattern[512] =
58 { {MB_ERROR, 0}, {0, 9}, {39, 9}, {27, 9}, {59, 9}, {55, 9}, {47, 9}, {31, 9},
59 {58, 8}, {58, 8}, {54, 8}, {54, 8}, {46, 8}, {46, 8}, {30, 8}, {30, 8},
60 {57, 8}, {57, 8}, {53, 8}, {53, 8}, {45, 8}, {45, 8}, {29, 8}, {29, 8},
61 {38, 8}, {38, 8}, {26, 8}, {26, 8}, {37, 8}, {37, 8}, {25, 8}, {25, 8},
62 {43, 8}, {43, 8}, {23, 8}, {23, 8}, {51, 8}, {51, 8}, {15, 8}, {15, 8},
63 {42, 8}, {42, 8}, {22, 8}, {22, 8}, {50, 8}, {50, 8}, {14, 8}, {14, 8},
64 {41, 8}, {41, 8}, {21, 8}, {21, 8}, {49, 8}, {49, 8}, {13, 8}, {13, 8},
65 {35, 8}, {35, 8}, {19, 8}, {19, 8}, {11, 8}, {11, 8}, {7, 8}, {7, 8},
66 {34, 7}, {34, 7}, {34, 7}, {34, 7}, {18, 7}, {18, 7}, {18, 7}, {18, 7},
67 {10, 7}, {10, 7}, {10, 7}, {10, 7}, {6, 7}, {6, 7}, {6, 7}, {6, 7},
68 {33, 7}, {33, 7}, {33, 7}, {33, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7},
69 {9, 7}, {9, 7}, {9, 7}, {9, 7}, {5, 7}, {5, 7}, {5, 7}, {5, 7},
70 {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6},
71 {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6},
72 {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6},
73 {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6},
74 {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
75 {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
76 {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5},
77 {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5},
78 {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5},
79 {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5},
80 {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5},
81 {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5},
82 {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5},
83 {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5},
84 {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5},
85 {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5},
86 {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5},
87 {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5},
88 {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5},
89 {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5},
90 {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5},
91 {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5},
92 {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5},
93 {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5},
94 {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5},
95 {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5},
96 {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5},
97 {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5},
98 {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
99 {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
100 {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
101 {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
102 {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
103 {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
104 {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
105 {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
106 {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
107 {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
108 {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
109 {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
110 {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
111 {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
112 {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
113 {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
114 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
115 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
116 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
117 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
118 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
119 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
120 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
121 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3} };
123 /* Tables for dc DCT coefficients
124 * Tables are cut in two parts to reduce memory occupation
127 /* Table B-12, dct_dc_size_luminance, codes 00xxx ... 11110 */
128 static lookup_t pl_dct_dc_lum_init_table_1[32] =
129 { {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
130 {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
131 {0, 3}, {0, 3}, {0, 3}, {0, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3},
132 {4, 3}, {4, 3}, {4, 3}, {4, 3}, {5, 4}, {5, 4}, {6, 5}, {MB_ERROR, 0}
135 /* Table B-12, dct_dc_size_luminance, codes 111110xxx ... 111111111 */
136 static lookup_t pl_dct_dc_lum_init_table_2[32] =
137 { {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6},
138 {8, 7}, {8, 7}, {8, 7}, {8, 7}, {9, 8}, {9, 8}, {10,9}, {11,9},
139 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
140 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
141 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
142 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}
145 /* Table B-13, dct_dc_size_chrominance, codes 00xxx ... 11110 */
146 static lookup_t pl_dct_dc_chrom_init_table_1[32] =
147 { {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2},
148 {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
149 {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
150 {3, 3}, {3, 3}, {3, 3}, {3, 3}, {4, 4}, {4, 4}, {5, 5}, {MB_ERROR, 0}
153 /* Table B-13, dct_dc_size_chrominance, codes 111110xxxx ... 1111111111 */
154 static lookup_t pl_dct_dc_chrom_init_table_2[32] =
155 { {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
156 {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
157 {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7},
158 {8, 8}, {8, 8}, {8, 8}, {8, 8}, {9, 9}, {9, 9}, {10,10}, {11,10}
162 /* Tables for ac DCT coefficients. There are cut in many parts to save space */
163 /* Table B-14, DCT coefficients table zero,
164 * codes 0100 ... 1xxx (used for first (DC) coefficient)
166 static dct_lookup_t pl_DCT_tab_dc[12] =
168 {0,2,4}, {2,1,4}, {1,1,3}, {1,1,3},
169 {0,1,1}, {0,1,1}, {0,1,1}, {0,1,1},
170 {0,1,1}, {0,1,1}, {0,1,1}, {0,1,1}
173 /* Table B-14, DCT coefficients table zero,
174 * codes 0100 ... 1xxx (used for all other coefficients)
176 static dct_lookup_t pl_DCT_tab_ac[12] =
178 {0,2,4}, {2,1,4}, {1,1,3}, {1,1,3},
179 {DCT_EOB,0,2}, {DCT_EOB,0,2}, {DCT_EOB,0,2}, {DCT_EOB,0,2}, /* EOB */
180 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2}
183 /* Table B-14, DCT coefficients table zero,
184 * codes 000001xx ... 00111xxx
186 static dct_lookup_t pl_DCT_tab0[60] =
188 {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6},
190 {2,2,7}, {2,2,7}, {9,1,7}, {9,1,7},
191 {0,4,7}, {0,4,7}, {8,1,7}, {8,1,7},
192 {7,1,6}, {7,1,6}, {7,1,6}, {7,1,6},
193 {6,1,6}, {6,1,6}, {6,1,6}, {6,1,6},
194 {1,2,6}, {1,2,6}, {1,2,6}, {1,2,6},
195 {5,1,6}, {5,1,6}, {5,1,6}, {5,1,6},
196 {13,1,8}, {0,6,8}, {12,1,8}, {11,1,8},
197 {3,2,8}, {1,3,8}, {0,5,8}, {10,1,8},
198 {0,3,5}, {0,3,5}, {0,3,5}, {0,3,5},
199 {0,3,5}, {0,3,5}, {0,3,5}, {0,3,5},
200 {4,1,5}, {4,1,5}, {4,1,5}, {4,1,5},
201 {4,1,5}, {4,1,5}, {4,1,5}, {4,1,5},
202 {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
203 {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5}
206 /* Table B-15, DCT coefficients table one,
207 * codes 000001xx ... 11111111
209 static dct_lookup_t pl_DCT_tab0a[252] =
211 {65,0,6}, {65,0,6}, {65,0,6}, {65,0,6}, /* Escape */
212 {7,1,7}, {7,1,7}, {8,1,7}, {8,1,7},
213 {6,1,7}, {6,1,7}, {2,2,7}, {2,2,7},
214 {0,7,6}, {0,7,6}, {0,7,6}, {0,7,6},
215 {0,6,6}, {0,6,6}, {0,6,6}, {0,6,6},
216 {4,1,6}, {4,1,6}, {4,1,6}, {4,1,6},
217 {5,1,6}, {5,1,6}, {5,1,6}, {5,1,6},
218 {1,5,8}, {11,1,8}, {0,11,8}, {0,10,8},
219 {13,1,8}, {12,1,8}, {3,2,8}, {1,4,8},
220 {2,1,5}, {2,1,5}, {2,1,5}, {2,1,5},
221 {2,1,5}, {2,1,5}, {2,1,5}, {2,1,5},
222 {1,2,5}, {1,2,5}, {1,2,5}, {1,2,5},
223 {1,2,5}, {1,2,5}, {1,2,5}, {1,2,5},
224 {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
225 {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
226 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
227 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
228 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
229 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
230 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
231 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
232 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
233 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
234 {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4}, /* EOB */
235 {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
236 {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
237 {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
238 {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
239 {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
240 {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
241 {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
242 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
243 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
244 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
245 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
246 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
247 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
248 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
249 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
250 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
251 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
252 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
253 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
254 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
255 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
256 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
257 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
258 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
259 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
260 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
261 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
262 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
263 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
264 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
265 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
266 {0,4,5}, {0,4,5}, {0,4,5}, {0,4,5},
267 {0,4,5}, {0,4,5}, {0,4,5}, {0,4,5},
268 {0,5,5}, {0,5,5}, {0,5,5}, {0,5,5},
269 {0,5,5}, {0,5,5}, {0,5,5}, {0,5,5},
270 {9,1,7}, {9,1,7}, {1,3,7}, {1,3,7},
271 {10,1,7}, {10,1,7}, {0,8,7}, {0,8,7},
272 {0,9,7}, {0,9,7}, {0,12,8}, {0,13,8},
273 {2,3,8}, {4,2,8}, {0,14,8}, {0,15,8}
276 /* Table B-14, DCT coefficients table zero,
277 * codes 0000001000 ... 0000001111
279 static dct_lookup_t pl_DCT_tab1[8] =
281 {16,1,10}, {5,2,10}, {0,7,10}, {2,3,10},
282 {1,4,10}, {15,1,10}, {14,1,10}, {4,2,10}
285 /* Table B-15, DCT coefficients table one,
286 * codes 000000100x ... 000000111x
288 static dct_lookup_t pl_DCT_tab1a[8] =
290 {5,2,9}, {5,2,9}, {14,1,9}, {14,1,9},
291 {2,4,10}, {16,1,10}, {15,1,9}, {15,1,9}
294 /* Table B-14/15, DCT coefficients table zero / one,
295 * codes 000000010000 ... 000000011111
297 static dct_lookup_t pl_DCT_tab2[16] =
299 {0,11,12}, {8,2,12}, {4,3,12}, {0,10,12},
300 {2,4,12}, {7,2,12}, {21,1,12}, {20,1,12},
301 {0,9,12}, {19,1,12}, {18,1,12}, {1,5,12},
302 {3,3,12}, {0,8,12}, {6,2,12}, {17,1,12}
305 /* Table B-14/15, DCT coefficients table zero / one,
306 * codes 0000000010000 ... 0000000011111
308 static dct_lookup_t pl_DCT_tab3[16] =
310 {10,2,13}, {9,2,13}, {5,3,13}, {3,4,13},
311 {2,5,13}, {1,7,13}, {1,6,13}, {0,15,13},
312 {0,14,13}, {0,13,13}, {0,12,13}, {26,1,13},
313 {25,1,13}, {24,1,13}, {23,1,13}, {22,1,13}
316 /* Table B-14/15, DCT coefficients table zero / one,
317 * codes 00000000010000 ... 00000000011111
319 static dct_lookup_t pl_DCT_tab4[16] =
321 {0,31,14}, {0,30,14}, {0,29,14}, {0,28,14},
322 {0,27,14}, {0,26,14}, {0,25,14}, {0,24,14},
323 {0,23,14}, {0,22,14}, {0,21,14}, {0,20,14},
324 {0,19,14}, {0,18,14}, {0,17,14}, {0,16,14}
327 /* Table B-14/15, DCT coefficients table zero / one,
328 * codes 000000000010000 ... 000000000011111
330 static dct_lookup_t pl_DCT_tab5[16] =
332 {0,40,15}, {0,39,15}, {0,38,15}, {0,37,15},
333 {0,36,15}, {0,35,15}, {0,34,15}, {0,33,15},
334 {0,32,15}, {1,14,15}, {1,13,15}, {1,12,15},
335 {1,11,15}, {1,10,15}, {1,9,15}, {1,8,15}
338 /* Table B-14/15, DCT coefficients table zero / one,
339 * codes 0000000000010000 ... 0000000000011111
341 static dct_lookup_t pl_DCT_tab6[16] =
343 {1,18,16}, {1,17,16}, {1,16,16}, {1,15,16},
344 {6,3,16}, {16,2,16}, {15,2,16}, {14,2,16},
345 {13,2,16}, {12,2,16}, {11,2,16}, {31,1,16},
346 {30,1,16}, {29,1,16}, {28,1,16}, {27,1,16}
351 * Initialization of lookup tables
354 /*****************************************************************************
355 * vpar_InitCrop : Initialize the crop table for saturation
356 * (ISO/IEC 13818-2 section 7.4.3)
357 *****************************************************************************/
358 #if defined(MPEG2_COMPLIANT) && !defined(VDEC_DFT)
359 void vpar_InitCrop( vpar_thread_t * p_vpar )
363 p_vpar->pi_crop = p_vpar->pi_crop_buf + 4096;
365 for( i_dummy = -4096; i_dummy < -2048; i_dummy++ )
367 p_vpar->pi_crop[i_dummy] = -2048;
369 for( ; i_dummy < 2047; i_dummy++ )
371 p_vpar->pi_crop[i_dummy] = i_dummy;
373 for( ; i_dummy < 4095; i_dummy++ )
375 p_vpar->pi_crop[i_dummy] = 2047;
380 /*****************************************************************************
381 * vpar_InitMbAddrInc : Initialize the lookup table for mb_addr_inc
382 *****************************************************************************/
384 /* Function for filling up the lookup table for mb_addr_inc */
385 static void __inline__ FillMbAddrIncTable( vpar_thread_t * p_vpar,
386 int i_start, int i_end, int i_step,
387 int * pi_value, int i_length )
390 for( i_pos = i_start ; i_pos < i_end ; i_pos += i_step )
392 for( i_offset = 0 ; i_offset < i_step ; i_offset ++ )
394 p_vpar->pl_mb_addr_inc[i_pos + i_offset].i_value = * pi_value;
395 p_vpar->pl_mb_addr_inc[i_pos + i_offset].i_length = i_length;
401 /* Function that initialize the table using the last one */
402 void vpar_InitMbAddrInc( vpar_thread_t * p_vpar )
407 for( i_dummy = 0 ; i_dummy < 8 ; i_dummy++ )
409 p_vpar->pl_mb_addr_inc[i_dummy].i_value = MB_ERROR;
410 p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
413 p_vpar->pl_mb_addr_inc[8].i_value = MB_ADDRINC_ESCAPE;
414 p_vpar->pl_mb_addr_inc[8].i_length = 11;
416 for( i_dummy = 9 ; i_dummy < 15 ; i_dummy ++ )
418 p_vpar->pl_mb_addr_inc[i_dummy].i_value = MB_ERROR;
419 p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
422 p_vpar->pl_mb_addr_inc[15].i_value = MB_ADDRINC_STUFFING;
423 p_vpar->pl_mb_addr_inc[15].i_length = 11;
425 for( i_dummy = 16; i_dummy < 24; i_dummy++ )
427 p_vpar->pl_mb_addr_inc[i_dummy].i_value = MB_ERROR;
428 p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
433 FillMbAddrIncTable( p_vpar, 24, 36, 1, &i_value, 11 );
434 FillMbAddrIncTable( p_vpar, 36, 48, 2, &i_value, 10 );
435 FillMbAddrIncTable( p_vpar, 48, 96, 8, &i_value, 8 );
436 FillMbAddrIncTable( p_vpar, 96, 128, 16, &i_value, 7 );
437 FillMbAddrIncTable( p_vpar, 128, 256, 64, &i_value, 5 );
438 FillMbAddrIncTable( p_vpar, 256, 512, 128, &i_value, 4 );
439 FillMbAddrIncTable( p_vpar, 512, 1024, 256, &i_value, 3 );
440 FillMbAddrIncTable( p_vpar, 1024, 2048, 1024, &i_value, 1 );
443 /*****************************************************************************
444 * vpar_Init*MBType : Initialize lookup table for the Macroblock type
445 *****************************************************************************/
447 /* Fonction for filling up the tables */
448 static void __inline__ FillMBType( vpar_thread_t * p_vpar,
457 for( i_dummy = i_start ; i_dummy < i_end ; i_dummy++ )
459 p_vpar->ppl_mb_type[i_mb_type][i_dummy].i_value = i_value;
460 p_vpar->ppl_mb_type[i_mb_type][i_dummy].i_length = i_length;
464 /* Fonction that fills the table for P MB_Type */
465 void vpar_InitPMBType( vpar_thread_t * p_vpar )
467 FillMBType( p_vpar, 0, 32, 64, MB_MOTION_FORWARD|MB_PATTERN, 1 );
468 FillMBType( p_vpar, 0, 16, 32, MB_PATTERN, 2 );
469 FillMBType( p_vpar, 0, 8, 16, MB_MOTION_FORWARD, 3 );
470 FillMBType( p_vpar, 0, 6, 8, MB_INTRA, 5 );
471 FillMBType( p_vpar, 0, 4, 6, MB_QUANT|MB_MOTION_FORWARD|MB_PATTERN, 5 );
472 FillMBType( p_vpar, 0, 2, 4, MB_QUANT|MB_PATTERN, 5 );
473 p_vpar->ppl_mb_type[0][1].i_value = MB_QUANT|MB_INTRA;
474 p_vpar->ppl_mb_type[0][1].i_length = 6;
475 p_vpar->ppl_mb_type[0][0].i_value = MB_ERROR;
476 p_vpar->ppl_mb_type[0][0].i_length = 0;
479 /* Fonction that fills the table for B MB_Type */
480 void vpar_InitBMBType( vpar_thread_t * p_vpar )
482 FillMBType( p_vpar, 1, 48, 64, MB_MOTION_FORWARD
483 |MB_MOTION_BACKWARD|MB_PATTERN, 2 );
484 FillMBType( p_vpar, 1, 32, 48, MB_MOTION_FORWARD|MB_MOTION_BACKWARD, 2 );
485 FillMBType( p_vpar, 1, 24, 32, MB_MOTION_BACKWARD|MB_PATTERN, 3 );
486 FillMBType( p_vpar, 1, 16, 24, MB_MOTION_BACKWARD, 3 );
487 FillMBType( p_vpar, 1, 12, 16, MB_MOTION_FORWARD|MB_PATTERN, 4 );
488 FillMBType( p_vpar, 1, 8, 12, MB_MOTION_FORWARD, 4 );
489 FillMBType( p_vpar, 1, 6, 8, MB_INTRA, 5 );
490 FillMBType( p_vpar, 1, 4, 6, MB_QUANT|MB_MOTION_FORWARD
491 |MB_MOTION_BACKWARD|MB_PATTERN, 5 );
492 p_vpar->ppl_mb_type[1][3].i_value = MB_QUANT|MB_MOTION_FORWARD|MB_PATTERN;
493 p_vpar->ppl_mb_type[1][3].i_length = 6;
494 p_vpar->ppl_mb_type[1][2].i_value = MB_QUANT|MB_MOTION_BACKWARD|MB_PATTERN;
495 p_vpar->ppl_mb_type[1][2].i_length = 6;
496 p_vpar->ppl_mb_type[1][1].i_value = MB_QUANT|MB_INTRA;
497 p_vpar->ppl_mb_type[1][1].i_length = 6;
498 p_vpar->ppl_mb_type[1][0].i_value =MB_ERROR;
499 p_vpar->ppl_mb_type[1][0].i_length = 0;
503 /*****************************************************************************
504 * vpar_InitDCTTables : Initialize tables giving the length of the dct
505 * coefficient from the vlc code
506 *****************************************************************************/
508 /* First fonction for filling the table */
509 static void __inline__ FillDCTTable( dct_lookup_t * p_tab_dest, dct_lookup_t * p_tab_src,
510 int i_step, int i_nb_elem, int i_offset )
512 int i_dummy, i_dummy2;
514 for( i_dummy=0 ; i_dummy < i_nb_elem ; i_dummy++ )
516 for( i_dummy2=0 ; i_dummy2 < i_step ; i_dummy2++ )
518 p_tab_dest[(i_dummy+i_offset)*i_step+i_dummy2] = p_tab_src[i_dummy];
524 /* Fonction that actually fills the table or create the pointers */
525 void vpar_InitDCTTables( vpar_thread_t * p_vpar )
527 /* Tables are cut in two parts to reduce memory occupation */
528 p_vpar->pppl_dct_dc_size[0][0] = pl_dct_dc_lum_init_table_1;
529 p_vpar->pppl_dct_dc_size[0][1] = pl_dct_dc_lum_init_table_2;
530 p_vpar->pppl_dct_dc_size[1][0] = pl_dct_dc_chrom_init_table_1;
531 p_vpar->pppl_dct_dc_size[1][1] = pl_dct_dc_chrom_init_table_2;
533 memset( p_vpar->ppl_dct_coef[0], MB_ERROR, 16 );
534 memset( p_vpar->ppl_dct_coef[1], MB_ERROR, 16 );
536 /* For table B14 & B15, we have a pointer to tables */
537 /* We fill the table thanks to the fonction defined above */
538 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab0, 256, 60, 4 );
539 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab1, 64, 8, 8 );
540 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab2, 16, 16, 16 );
541 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab3, 8, 16, 16 );
542 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab4, 4, 16, 16 );
543 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab5, 2, 16, 16 );
544 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab6, 1, 16, 16 );
546 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab0a, 256, 60, 4 );
547 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab1a, 64, 8, 8 );
548 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab2, 16, 16, 16 );
549 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab3, 8, 16, 16 );
550 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab4, 4, 16, 16 );
551 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab5, 2, 16, 16 );
552 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab6, 1, 16, 16 );
560 /*****************************************************************************
561 * DecodeMPEG1NonIntra : decode MPEG-1 non-intra blocks
562 *****************************************************************************/
563 static __inline__ void DecodeMPEG1NonIntra( vpar_thread_t * p_vpar,
564 macroblock_t * p_mb, int i_b,
565 int i_chroma_format )
568 if( p_vpar->picture.i_coding_type == D_CODING_TYPE )
570 /* Remove end_of_macroblock (always 1, prevents startcode emulation)
571 * ISO/IEC 11172-2 section 2.4.2.7 and 2.4.3.6 */
572 RemoveBits( &p_vpar->bit_stream, 1 );
576 /*****************************************************************************
577 * DecodeMPEG1Intra : decode MPEG-1 intra blocks
578 *****************************************************************************/
579 static __inline__ void DecodeMPEG1Intra( vpar_thread_t * p_vpar,
580 macroblock_t * p_mb, int i_b ,
581 int i_chroma_format )
584 if( p_vpar->picture.i_coding_type == D_CODING_TYPE )
586 /* Remove end_of_macroblock (always 1, prevents startcode emulation)
587 * ISO/IEC 11172-2 section 2.4.2.7 and 2.4.3.6 */
588 RemoveBits( &p_vpar->bit_stream, 1 );
592 /*****************************************************************************
593 * DecodeMPEG2NonIntra : decode MPEG-2 non-intra blocks
594 *****************************************************************************/
595 static __inline__ void DecodeMPEG2NonIntra( vpar_thread_t * p_vpar,
596 macroblock_t * p_mb, int i_b,
597 int i_chroma_format )
612 /* Lookup Table for the chromatic component */
613 static int pi_cc_index[12] = { 0, 0, 0, 0, 1, 2, 1, 2, 1, 2 };
615 i_cc = pi_cc_index[i_b];
617 /* Determine whether it is luminance or not (chrominance) */
618 i_type = ( i_cc + 1 ) >> 1;
620 /* Give a pointer to the quantization matrices for intra blocks */
621 if( (i_chroma_format == CHROMA_420) || (!i_type) )
623 pi_quant = p_vpar->sequence.nonintra_quant.pi_matrix;
627 pi_quant = p_vpar->sequence.chroma_nonintra_quant.pi_matrix;
630 /* Decoding of the AC coefficients */
634 for( i_parse = 0; !p_vpar->b_die; i_parse++ )
636 i_code = ShowBits( &p_vpar->bit_stream, 16 );
637 if( i_code >= 16384 )
641 i_run = pl_DCT_tab_dc[(i_code>>12)-4].i_run;
642 i_level = pl_DCT_tab_dc[(i_code>>12)-4].i_level;
643 i_length = pl_DCT_tab_dc[(i_code>>12)-4].i_length;
647 i_run = pl_DCT_tab_ac[(i_code>>12)-4].i_run;
648 i_level = pl_DCT_tab_ac[(i_code>>12)-4].i_level;
649 i_length = pl_DCT_tab_ac[(i_code>>12)-4].i_length;
652 else if( i_code >= 1024 )
654 i_run = pl_DCT_tab0[(i_code>>8)-4].i_run;
655 i_length = pl_DCT_tab0[(i_code>>8)-4].i_length;
656 i_level = pl_DCT_tab0[(i_code>>8)-4].i_level;
660 i_run = p_vpar->ppl_dct_coef[0][i_code].i_run;
661 i_length = p_vpar->ppl_dct_coef[0][i_code].i_length;
662 i_level = p_vpar->ppl_dct_coef[0][i_code].i_level;
666 RemoveBits( &p_vpar->bit_stream, i_length );
671 i_run = GetBits( &p_vpar->bit_stream, 6 );
672 i_level = GetBits( &p_vpar->bit_stream, 12 );
673 i_level = (b_sign = ( i_level > 2047 )) ? 4096 - i_level
678 /* The MMX IDCT has a precision problem with non-intra
680 p_mb->ppi_blocks[i_b][0] += 4;
684 p_mb->pf_idct[i_b] = vdec_SparseIDCT;
685 p_mb->pi_sparse_pos[i_b] = i_coef;
689 p_mb->pf_idct[i_b] = vdec_IDCT;
695 b_sign = GetBits( &p_vpar->bit_stream, 1 );
706 i_pos = pi_scan[p_vpar->picture.b_alternate_scan][i_parse];
707 i_level = ( ((i_level << 1) + 1) * p_vpar->mb.i_quantizer_scale
708 * pi_quant[i_pos] ) >> 5;
709 p_mb->ppi_blocks[i_b][i_pos] = b_sign ? -i_level : i_level;
712 intf_ErrMsg("vpar error: DCT coeff (non-intra) is out of bounds\n");
713 p_vpar->picture.b_error = 1;
716 /*****************************************************************************
717 * DecodeMPEG2Intra : decode MPEG-2 intra blocks
718 *****************************************************************************/
719 static __inline__ void DecodeMPEG2Intra( vpar_thread_t * p_vpar,
720 macroblock_t * p_mb, int i_b,
721 int i_chroma_format )
735 boolean_t b_vlc_intra;
739 /* Lookup Table for the chromatic component */
740 static int pi_cc_index[12] = { 0, 0, 0, 0, 1, 2, 1, 2, 1, 2 };
741 i_cc = pi_cc_index[i_b];
743 /* Determine whether it is luminance or not (chrominance) */
744 i_type = ( i_cc + 1 ) >> 1;
746 /* Give a pointer to the quantization matrices for intra blocks */
747 if( (i_chroma_format == CHROMA_420) || (!i_type) )
749 pi_quant = p_vpar->sequence.intra_quant.pi_matrix;
753 pi_quant = p_vpar->sequence.chroma_intra_quant.pi_matrix;
757 /* Decoding of the DC intra coefficient */
758 /* The nb of bits to parse depends on i_type */
759 i_code = ShowBits( &p_vpar->bit_stream, 9 + i_type );
761 /* To reduce memory occupation, there are two lookup tables
762 * See InitDCT above */
763 i_code5 = i_code >> (4+i_type);
765 /* Shall we lookup in the first or in the second table ? */
766 i_select = ( i_code5 == 31 );
767 /* Offset value for looking in the second table */
768 i_offset = 0x1f0 + ( i_type * 0x1f0 );
769 i_pos = ( i_code5 * ( ! i_select ) ) + ( ( i_code - i_offset ) * i_select );
770 i_dct_dc_size = p_vpar->pppl_dct_dc_size[i_type][i_select][i_pos].i_value;
773 if( !i_type/*i_b < 4*/ )
776 i_code = ShowBits(&p_vpar->bit_stream, 5);
779 i_dct_dc_size = pl_dct_dc_lum_init_table_1[i_code].i_value;
780 i_length = pl_dct_dc_lum_init_table_1[i_code].i_length;
781 RemoveBits( &p_vpar->bit_stream, i_length);
785 i_code = ShowBits(&p_vpar->bit_stream, 9) - 0x1f0;
786 i_dct_dc_size = pl_dct_dc_lum_init_table_2[i_code].i_value;
787 i_length = pl_dct_dc_lum_init_table_2[i_code].i_length;
788 RemoveBits( &p_vpar->bit_stream, i_length);
794 i_code = ShowBits(&p_vpar->bit_stream, 5);
798 i_dct_dc_size = pl_dct_dc_chrom_init_table_1[i_code].i_value;
799 i_length = pl_dct_dc_chrom_init_table_1[i_code].i_length;
800 RemoveBits(&p_vpar->bit_stream, i_length);
804 i_code = ShowBits(&p_vpar->bit_stream, 10) - 0x3e0;
805 i_dct_dc_size = pl_dct_dc_chrom_init_table_2[i_code].i_value;
806 i_length = pl_dct_dc_chrom_init_table_2[i_code].i_length;
807 RemoveBits( &p_vpar->bit_stream, i_length);
810 if (i_dct_dc_size==0)
814 i_dct_dc_diff = GetBits( &p_vpar->bit_stream, i_dct_dc_size);
815 if ((i_dct_dc_diff & (1<<(i_dct_dc_size-1)))==0)
816 i_dct_dc_diff-= (1<<i_dct_dc_size) - 1;
819 /* Dump the variable length code */
820 //RemoveBits( &p_vpar->bit_stream,
821 // p_vpar->pppl_dct_dc_size[i_type][i_select][i_pos].i_length );
823 /* Read the actual code with the good length */
824 p_vpar->mb.pi_dc_dct_pred[i_cc] += i_dct_dc_diff;
826 p_mb->ppi_blocks[i_b][0] = ( p_vpar->mb.pi_dc_dct_pred[i_cc] <<
827 ( 3 - p_vpar->picture.i_intra_dc_precision ) );
829 i_nc = ( p_vpar->mb.pi_dc_dct_pred[i_cc] != 0 );
831 /* Decoding of the AC coefficients */
834 b_vlc_intra = p_vpar->picture.b_intra_vlc_format;
835 for( i_parse = 1; !p_vpar->b_die/*i_parse < 64*/; i_parse++ )
837 i_code = ShowBits( &p_vpar->bit_stream, 16 );
838 if( i_code >= 16384 )
842 i_run = pl_DCT_tab0a[(i_code>>8)-4].i_run;
843 i_level = pl_DCT_tab0a[(i_code>>8)-4].i_level;
844 i_length = pl_DCT_tab0a[(i_code>>8)-4].i_length;
845 //fprintf( stderr, "**********> %d, %d, %d *******\n", i_run, i_level, (i_code>>8)-4 );
849 i_run = pl_DCT_tab_ac[(i_code>>12)-4].i_run;
850 i_level = pl_DCT_tab_ac[(i_code>>12)-4].i_level;
851 i_length = pl_DCT_tab_ac[(i_code>>12)-4].i_length;
856 i_run = p_vpar->ppl_dct_coef[b_vlc_intra][i_code].i_run;
857 i_length = p_vpar->ppl_dct_coef[b_vlc_intra][i_code].i_length;
858 i_level = p_vpar->ppl_dct_coef[b_vlc_intra][i_code].i_level;
864 int intra_vlc_format = b_vlc_intra;
867 if (code>=16384 && !intra_vlc_format)
868 tab = pl_DCT_tab_ac[(code>>12)-4];
871 if (intra_vlc_format)
872 tab = pl_DCT_tab0a[(code>>8)-4];
874 tab = pl_DCT_tab0[(code>>8)-4];
878 if (intra_vlc_format)
879 tab = pl_DCT_tab1a[(code>>6)-8];
881 tab = pl_DCT_tab1[(code>>6)-8];
884 tab = pl_DCT_tab2[(code>>4)-16];
886 tab = pl_DCT_tab3[(code>>3)-16];
888 tab = pl_DCT_tab4[(code>>2)-16];
890 tab = pl_DCT_tab5[(code>>1)-16];
892 tab = pl_DCT_tab6[code-16];
895 fprintf( stderr, "invalid Huffman code in Decode_MPEG2_Intra_Block()\n");
898 if( (i_run != tab.i_run) || (i_length != tab.i_length) || (i_level != tab.i_level) )
900 fprintf( stderr, "ET M....... !!!\n" );
909 RemoveBits( &p_vpar->bit_stream, i_length );
914 i_run = GetBits( &p_vpar->bit_stream, 6 );
915 i_level = GetBits( &p_vpar->bit_stream, 12 );
916 /*p_mb->ppi_blocks[i_b][i_parse] = ( b_sign = ( i_level > 2047 ) )
917 ? ( -4096 + i_level )
919 i_level = (b_sign = ( i_level > 2047 )) ? 4096 - i_level
925 p_mb->pf_idct[i_b] = vdec_SparseIDCT;
926 p_mb->pi_sparse_pos[i_b] = i_coef;
930 p_mb->pf_idct[i_b] = vdec_IDCT;
936 b_sign = GetBits( &p_vpar->bit_stream, 1 );
948 i_pos = pi_scan[p_vpar->picture.b_alternate_scan][i_parse];
949 i_level = ( i_level *
950 p_vpar->mb.i_quantizer_scale *
951 pi_quant[i_pos] ) >> 4;
952 p_mb->ppi_blocks[i_b][i_pos] = b_sign ? -i_level : i_level;
955 intf_ErrMsg("vpar error: DCT coeff (intra) is out of bounds\n");
956 p_vpar->picture.b_error = 1;
964 /****************************************************************************
965 * MotionCode : Parse the next motion code
966 ****************************************************************************/
967 static __inline__ int MotionCode( vpar_thread_t * p_vpar )
970 static lookup_t pl_mv_tab0[8] =
971 { {-1,0}, {3,3}, {2,2}, {2,2}, {1,1}, {1,1}, {1,1}, {1,1} };
972 /* Table B-10, motion_code, codes 0000011 ... 000011x */
973 static lookup_t pl_mv_tab1[8] =
974 { {-1,0}, {-1,0}, {-1,0}, {7,6}, {6,6}, {5,6}, {4,5}, {4,5} };
975 /* Table B-10, motion_code, codes 0000001100 ... 000001011x */
976 static lookup_t pl_mv_tab2[12] = {
977 {16,9}, {15,9}, {14,9}, {13,9},
978 {12,9}, {11,9}, {10,8}, {10,8},
979 {9,8}, {9,8}, {8,8}, {8,8} };
981 if( GetBits( &p_vpar->bit_stream, 1 ) )
985 if( (i_code = ShowBits( &p_vpar->bit_stream, 9) ) >= 64 )
988 RemoveBits( &p_vpar->bit_stream, pl_mv_tab0[i_code].i_length );
989 return( GetBits( &p_vpar->bit_stream, 1 ) ?
990 -pl_mv_tab0[i_code].i_value : pl_mv_tab0[i_code].i_value );
996 RemoveBits( &p_vpar->bit_stream, pl_mv_tab1[i_code].i_length );
997 return( GetBits( &p_vpar->bit_stream, 1 ) ?
998 -pl_mv_tab1[i_code].i_value : pl_mv_tab1[i_code].i_value );
1001 if( (i_code -= 12) < 0 )
1003 p_vpar->picture.b_error = 1;
1004 intf_DbgMsg( "vpar debug: Invalid motion_vector code\n" );
1008 RemoveBits( &p_vpar->bit_stream, pl_mv_tab2[i_code].i_length );
1009 return( GetBits( &p_vpar->bit_stream, 1 ) ?
1010 -pl_mv_tab2[i_code].i_value : pl_mv_tab2[i_code].i_value );
1013 /****************************************************************************
1014 * DecodeMotionVector : Decode a motion_vector
1015 ****************************************************************************/
1016 static __inline__ void DecodeMotionVector( int * pi_prediction, int i_r_size,
1017 int i_motion_code, int i_motion_residual, int i_full_pel )
1019 int i_limit, i_vector;
1021 /* ISO/IEC 13818-1 section 7.6.3.1 */
1022 i_limit = 16 << i_r_size;
1023 i_vector = *pi_prediction >> i_full_pel;
1025 if( i_motion_code > 0 )
1027 i_vector += ((i_motion_code-1) << i_r_size) + i_motion_residual + 1;
1028 if( i_vector >= i_limit )
1029 i_vector -= i_limit + i_limit;
1031 else if( i_motion_code < 0 )
1033 i_vector -= ((-i_motion_code-1) << i_r_size) + i_motion_residual + 1;
1034 if( i_vector < -i_limit )
1035 i_vector += i_limit + i_limit;
1037 *pi_prediction = i_vector << i_full_pel;
1040 /****************************************************************************
1041 * MotionVector : Parse the next motion_vector field
1042 ****************************************************************************/
1043 static __inline__ void MotionVector( vpar_thread_t * p_vpar,
1044 macroblock_t * p_mb, int i_r,
1045 int i_s, int i_full_pel, int i_structure )
1047 int i_motion_code, i_motion_residual;
1049 int pi_dm_vector[2];
1051 i_r_size = p_vpar->picture.ppi_f_code[i_s][0] - 1;
1052 i_motion_code = MotionCode( p_vpar );
1053 i_motion_residual = (i_r_size != 0 && i_motion_code != 0) ?
1054 GetBits( &p_vpar->bit_stream, i_r_size) : 0;
1055 DecodeMotionVector( &p_vpar->mb.pppi_pmv[i_r][i_s][0], i_r_size,
1056 i_motion_code, i_motion_residual, i_full_pel );
1057 p_mb->pppi_motion_vectors[i_r][i_s][0] = p_vpar->mb.pppi_pmv[i_r][i_s][0];
1059 if( p_vpar->mb.b_dmv )
1061 if( GetBits(&p_vpar->bit_stream, 1) )
1063 pi_dm_vector[0] = GetBits( &p_vpar->bit_stream, 1 ) ? -1 : 1;
1067 pi_dm_vector[0] = 0;
1071 i_r_size = p_vpar->picture.ppi_f_code[i_s][1]-1;
1072 i_motion_code = MotionCode( p_vpar );
1073 i_motion_residual = (i_r_size != 0 && i_motion_code != 0) ?
1074 GetBits( &p_vpar->bit_stream, i_r_size) : 0;
1077 if( (p_vpar->mb.i_mv_format == MOTION_FIELD)
1078 && (i_structure == FRAME_STRUCTURE) )
1080 p_vpar->mb.pppi_pmv[i_r][i_s][1] >>= 1;
1083 DecodeMotionVector( &p_vpar->mb.pppi_pmv[i_r][i_s][1], i_r_size,
1084 i_motion_code, i_motion_residual, i_full_pel );
1086 if( (p_vpar->mb.i_mv_format == MOTION_FIELD)
1087 && (i_structure == FRAME_STRUCTURE) )
1088 p_vpar->mb.pppi_pmv[i_r][i_s][1] <<= 1;
1090 p_mb->pppi_motion_vectors[i_r][i_s][1] = p_vpar->mb.pppi_pmv[i_r][i_s][1];
1092 if( p_vpar->mb.b_dmv )
1094 if( GetBits(&p_vpar->bit_stream, 1) )
1096 pi_dm_vector[1] = GetBits( &p_vpar->bit_stream, 1 ) ? -1 : 1;
1100 pi_dm_vector[1] = 0;
1103 /* Dual Prime Arithmetic (ISO/IEC 13818-2 section 7.6.3.6). */
1105 #define i_mv_x p_mb->pppi_motion_vectors[0][0][0]
1106 if( i_structure == FRAME_STRUCTURE )
1108 #define i_mv_y (p_mb->pppi_motion_vectors[0][0][1] << 1)
1109 if( p_vpar->picture.b_top_field_first )
1111 /* vector for prediction of top field from bottom field */
1112 p_mb->ppi_dmv[0][0] = ((i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1113 p_mb->ppi_dmv[0][1] = ((i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] - 1;
1115 /* vector for prediction of bottom field from top field */
1116 p_mb->ppi_dmv[1][0] = ((3*i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1117 p_mb->ppi_dmv[1][1] = ((3*i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] + 1;
1121 /* vector for prediction of top field from bottom field */
1122 p_mb->ppi_dmv[0][0] = ((3*i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1123 p_mb->ppi_dmv[0][1] = ((3*i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] - 1;
1125 /* vector for prediction of bottom field from top field */
1126 p_mb->ppi_dmv[1][0] = ((i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1127 p_mb->ppi_dmv[1][1] = ((i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] + 1;
1133 #define i_mv_y p_mb->pppi_motion_vectors[0][0][1]
1134 /* vector for prediction from field of opposite 'parity' */
1135 p_mb->ppi_dmv[0][0] = ((i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1136 p_mb->ppi_dmv[0][1] = ((i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1];
1138 /* correct for vertical field shift */
1139 if( p_vpar->picture.i_structure == TOP_FIELD )
1140 p_mb->ppi_dmv[0][1]--;
1142 p_mb->ppi_dmv[0][1]++;
1149 /*****************************************************************************
1150 * DecodeMVMPEG1 : Parse the next MPEG-1 motion vectors
1151 *****************************************************************************/
1152 static __inline__ void DecodeMVMPEG1( vpar_thread_t * p_vpar,
1153 macroblock_t * p_mb, int i_s, int i_structure )
1155 MotionVector( p_vpar, p_mb, 0, i_s,
1156 p_vpar->picture.pb_full_pel_vector[i_s], i_structure );
1159 /*****************************************************************************
1160 * DecodeMVMPEG2 : Parse the next MPEG-2 motion_vectors field
1161 *****************************************************************************/
1162 static __inline__ void DecodeMVMPEG2( vpar_thread_t * p_vpar,
1163 macroblock_t * p_mb, int i_s, int i_structure )
1165 if( p_vpar->mb.i_mv_count == 1 )
1167 if( p_vpar->mb.i_mv_format == MOTION_FIELD && !p_vpar->mb.b_dmv )
1169 p_mb->ppi_field_select[0][i_s] = p_mb->ppi_field_select[1][i_s]
1170 = GetBits( &p_vpar->bit_stream, 1 );
1172 MotionVector( p_vpar, p_mb, 0, i_s, 0, i_structure );
1173 p_vpar->mb.pppi_pmv[1][i_s][0] = p_vpar->mb.pppi_pmv[0][i_s][0];
1174 p_vpar->mb.pppi_pmv[1][i_s][1] = p_vpar->mb.pppi_pmv[0][i_s][1];
1175 p_mb->pppi_motion_vectors[1][i_s][0] = p_vpar->mb.pppi_pmv[0][i_s][0];
1176 p_mb->pppi_motion_vectors[1][i_s][1] = p_vpar->mb.pppi_pmv[0][i_s][1];
1180 p_mb->ppi_field_select[0][i_s] = GetBits( &p_vpar->bit_stream, 1 );
1181 MotionVector( p_vpar, p_mb, 0, i_s, 0, i_structure );
1182 p_mb->ppi_field_select[1][i_s] = GetBits( &p_vpar->bit_stream, 1 );
1183 MotionVector( p_vpar, p_mb, 1, i_s, 0, i_structure );
1189 * Macroblock information structures
1192 /*****************************************************************************
1193 * MacroblockAddressIncrement : Get the macroblock_address_increment field
1194 *****************************************************************************/
1195 static __inline__ int MacroblockAddressIncrement( vpar_thread_t * p_vpar )
1198 /* Index in the lookup table mb_addr_inc */
1199 int i_index = ShowBits( &p_vpar->bit_stream, 11 );
1201 /* Test the presence of the escape character */
1202 while( i_index == 8 )
1204 RemoveBits( &p_vpar->bit_stream, 11 );
1206 i_index = ShowBits( &p_vpar->bit_stream, 11 );
1209 /* Affect the value from the lookup table */
1210 i_addr_inc += p_vpar->pl_mb_addr_inc[i_index].i_value;
1212 /* Dump the good number of bits */
1213 RemoveBits( &p_vpar->bit_stream, p_vpar->pl_mb_addr_inc[i_index].i_length );
1218 /*****************************************************************************
1219 * IMBType : macroblock_type in I pictures
1220 *****************************************************************************/
1221 static __inline__ int IMBType( vpar_thread_t * p_vpar )
1223 /* Take two bits for testing */
1224 int i_type = ShowBits( &p_vpar->bit_stream, 2 );
1226 /* Lookup table for macroblock_type */
1227 static lookup_t pl_mb_Itype[4] = { {MB_ERROR, 0},
1228 {MB_QUANT|MB_INTRA, 2},
1231 /* Dump the good number of bits */
1232 RemoveBits( &p_vpar->bit_stream, pl_mb_Itype[i_type].i_length );
1233 return pl_mb_Itype[i_type].i_value;
1236 /*****************************************************************************
1237 * PMBType : macroblock_type in P pictures
1238 *****************************************************************************/
1239 static __inline__ int PMBType( vpar_thread_t * p_vpar )
1241 /* Testing on 6 bits */
1242 int i_type = ShowBits( &p_vpar->bit_stream, 6 );
1244 /* Dump the good number of bits */
1245 RemoveBits( &p_vpar->bit_stream, p_vpar->ppl_mb_type[0][i_type].i_length );
1246 /* return the value from the lookup table for P type */
1247 return p_vpar->ppl_mb_type[0][i_type].i_value;
1250 /*****************************************************************************
1251 * BMBType : macroblock_type in B pictures
1252 *****************************************************************************/
1253 static __inline__ int BMBType( vpar_thread_t * p_vpar )
1255 /* Testing on 6 bits */
1256 int i_type = ShowBits( &p_vpar->bit_stream, 6 );
1258 /* Dump the good number of bits */
1259 RemoveBits( &p_vpar->bit_stream, p_vpar->ppl_mb_type[1][i_type].i_length );
1261 /* return the value from the lookup table for B type */
1262 return p_vpar->ppl_mb_type[1][i_type].i_value;
1265 /*****************************************************************************
1266 * DMBType : macroblock_type in D pictures
1267 *****************************************************************************/
1268 static __inline__ int DMBType( vpar_thread_t * p_vpar )
1270 return GetBits( &p_vpar->bit_stream, 1 );
1273 /*****************************************************************************
1274 * CodedPattern420 : coded_block_pattern with 4:2:0 chroma
1275 *****************************************************************************/
1276 static __inline__ int CodedPattern420( vpar_thread_t * p_vpar )
1278 /* Take the max 9 bits length vlc code for testing */
1279 int i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1281 /* Trash the good number of bits read in the lookup table */
1282 RemoveBits( &p_vpar->bit_stream, pl_coded_pattern[i_vlc].i_length );
1284 /* return the value from the vlc table */
1285 return pl_coded_pattern[i_vlc].i_value;
1288 /*****************************************************************************
1289 * CodedPattern422 : coded_block_pattern with 4:2:2 chroma
1290 *****************************************************************************/
1291 static __inline__ int CodedPattern422( vpar_thread_t * p_vpar )
1293 int i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1295 RemoveBits( &p_vpar->bit_stream, pl_coded_pattern[i_vlc].i_length );
1297 /* Supplementary 2 bits long code for 4:2:2 format */
1298 return pl_coded_pattern[i_vlc].i_value |
1299 (GetBits( &p_vpar->bit_stream, 2 ) << 6);
1302 /*****************************************************************************
1303 * CodedPattern444 : coded_block_pattern with 4:4:4 chroma
1304 *****************************************************************************/
1305 static __inline__ int CodedPattern444( vpar_thread_t * p_vpar )
1307 int i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1309 RemoveBits( &p_vpar->bit_stream, pl_coded_pattern[i_vlc].i_length );
1311 return pl_coded_pattern[i_vlc].i_value |
1312 (GetBits( &p_vpar->bit_stream, 6 ) << 6);
1315 /*****************************************************************************
1316 * InitMacroblock : Initialize macroblock values
1317 *****************************************************************************/
1318 static __inline__ void InitMacroblock( vpar_thread_t * p_vpar,
1319 macroblock_t * p_mb, int i_coding_type,
1320 int i_chroma_format,
1322 boolean_t b_second_field )
1324 p_mb->i_chroma_nb_blocks = 1 << i_chroma_format;
1325 p_mb->p_picture = p_vpar->picture.p_picture;
1327 if( i_coding_type == B_CODING_TYPE )
1328 p_mb->p_backward = p_vpar->sequence.p_backward;
1330 p_mb->p_backward = NULL;
1331 if( (i_coding_type == P_CODING_TYPE) || (i_coding_type == B_CODING_TYPE) )
1332 p_mb->p_forward = p_vpar->sequence.p_forward;
1334 p_mb->p_forward = NULL;
1336 p_mb->i_l_x = p_vpar->mb.i_l_x;
1337 p_mb->i_c_x = p_vpar->mb.i_c_x;
1338 p_mb->i_motion_l_y = p_vpar->mb.i_l_y;
1339 p_mb->i_motion_c_y = p_vpar->mb.i_c_y;
1340 if( (p_mb->b_motion_field = (i_structure == BOTTOM_FIELD)) )
1342 p_mb->i_motion_l_y--;
1343 p_mb->i_motion_c_y--;
1345 p_mb->i_addb_l_stride = (p_mb->i_l_stride = p_vpar->picture.i_l_stride) - 8;
1346 p_mb->i_addb_c_stride = (p_mb->i_c_stride = p_vpar->picture.i_c_stride) - 8;
1347 p_mb->b_P_second = ( b_second_field && i_coding_type == P_CODING_TYPE );
1350 /*****************************************************************************
1351 * UpdateContext : Update the p_vpar contextual values
1352 *****************************************************************************/
1353 static __inline__ void UpdateContext( vpar_thread_t * p_vpar, int i_structure )
1355 /* Update macroblock real position. */
1356 p_vpar->mb.i_l_x += 16;
1357 p_vpar->mb.i_l_y += (p_vpar->mb.i_l_x / p_vpar->sequence.i_width)
1358 * (2 - (i_structure == FRAME_STRUCTURE)) * 16;
1359 p_vpar->mb.i_l_x %= p_vpar->sequence.i_width;
1361 p_vpar->mb.i_c_x += p_vpar->sequence.i_chroma_mb_width;
1362 p_vpar->mb.i_c_y += (p_vpar->mb.i_c_x / p_vpar->sequence.i_chroma_width)
1363 * (2 - (i_structure == FRAME_STRUCTURE))
1364 * p_vpar->sequence.i_chroma_mb_height;
1365 p_vpar->mb.i_c_x %= p_vpar->sequence.i_chroma_width;
1368 /*****************************************************************************
1369 * SkippedMacroblock : Generate a skipped macroblock with NULL motion vector
1370 *****************************************************************************/
1371 static __inline__ void SkippedMacroblock( vpar_thread_t * p_vpar, int i_mb,
1372 int i_mb_base, int i_coding_type,
1373 int i_chroma_format,
1375 boolean_t b_second_field )
1377 macroblock_t * p_mb;
1379 static f_motion_t pf_motion_skipped[4][4] =
1381 {NULL, NULL, NULL, NULL},
1382 {NULL, vdec_MotionFieldField420, vdec_MotionFieldField420,
1383 vdec_MotionFrameFrame420},
1384 {NULL, vdec_MotionFieldField422, vdec_MotionFieldField422,
1385 vdec_MotionFrameFrame422},
1386 {NULL, vdec_MotionFieldField444, vdec_MotionFieldField444,
1387 vdec_MotionFrameFrame444},
1390 if( i_coding_type == I_CODING_TYPE )
1392 intf_DbgMsg("vpar error: skipped macroblock in I-picture\n");
1393 p_vpar->picture.b_error = 1;
1397 if( (p_mb = vpar_NewMacroblock( &p_vpar->vfifo )) == NULL )
1403 p_vpar->picture.pp_mb[i_mb_base + i_mb] = p_mb;
1406 InitMacroblock( p_vpar, p_mb, i_coding_type, i_chroma_format,
1407 i_structure, b_second_field );
1409 /* Motion type is picture structure. */
1410 p_mb->pf_motion = pf_motion_skipped[i_chroma_format]
1412 p_mb->i_mb_type = MB_MOTION_FORWARD;
1413 p_mb->i_coded_block_pattern = 0;
1414 memset( p_mb->pppi_motion_vectors, 0, 8*sizeof(int) );
1416 /* Set the field we use for motion compensation */
1417 p_mb->ppi_field_select[0][0] = p_mb->ppi_field_select[0][1]
1418 = ( i_structure == BOTTOM_FIELD );
1420 UpdateContext( p_vpar, i_structure );
1423 /* Decode the macroblock NOW ! */
1424 vdec_DecodeMacroblock( p_vpar->pp_vdec[0], p_mb );
1428 /*****************************************************************************
1429 * MacroblockModes : Get the macroblock_modes structure
1430 *****************************************************************************/
1431 static __inline__ void MacroblockModes( vpar_thread_t * p_vpar,
1432 macroblock_t * p_mb,
1433 int i_chroma_format,
1437 static int ppi_mv_count[2][4] = { {0, 1, 2, 1}, {0, 2, 1, 1} };
1438 static int ppi_mv_format[2][4] = { {0, 1, 1, 1}, {0, 1, 2, 1} };
1440 /* Get macroblock_type. */
1441 switch( i_coding_type )
1444 p_mb->i_mb_type = PMBType( p_vpar );
1447 p_mb->i_mb_type = BMBType( p_vpar );
1450 p_mb->i_mb_type = IMBType( p_vpar );
1453 p_mb->i_mb_type = DMBType( p_vpar );
1456 /* SCALABILITY : warning, we don't know if spatial_temporal_weight_code
1457 * has to be dropped, take care if you use scalable streams. */
1458 /* RemoveBits( &p_vpar->bit_stream, 2 ); */
1460 if( (i_coding_type == P_CODING_TYPE || i_coding_type == B_CODING_TYPE)
1461 && (p_mb->i_mb_type & (MB_MOTION_FORWARD | MB_MOTION_BACKWARD)) )
1463 if( !(i_structure == FRAME_STRUCTURE
1464 && p_vpar->picture.b_frame_pred_frame_dct) )
1466 p_vpar->mb.i_motion_type = GetBits( &p_vpar->bit_stream, 2 );
1470 p_vpar->mb.i_motion_type = MOTION_FRAME;
1474 p_vpar->mb.i_mv_count = ppi_mv_count[i_structure == FRAME_STRUCTURE]
1475 [p_vpar->mb.i_motion_type];
1476 p_vpar->mb.i_mv_format = ppi_mv_format[i_structure == FRAME_STRUCTURE]
1477 [p_vpar->mb.i_motion_type];
1478 p_vpar->mb.b_dmv = p_vpar->mb.i_motion_type == MOTION_DMV;
1481 p_vpar->mb.b_dct_type = 0;
1482 if( (i_structure == FRAME_STRUCTURE) &&
1483 (!p_vpar->picture.b_frame_pred_frame_dct) &&
1484 (p_mb->i_mb_type & (MB_PATTERN|MB_INTRA)) )
1486 if( (p_vpar->mb.b_dct_type = GetBits( &p_vpar->bit_stream, 1 )) )
1488 /* The DCT is coded on fields. Jump one line between each
1490 p_mb->i_addb_l_stride <<= 1;
1491 p_mb->i_addb_l_stride += 8;
1492 /* With CHROMA_420, the DCT is necessarily frame-coded. */
1493 if( i_chroma_format != CHROMA_420 )
1495 p_mb->i_addb_c_stride <<= 1;
1496 p_mb->i_addb_c_stride += 8;
1502 /*****************************************************************************
1503 * ParseMacroblock : Parse the next macroblock
1504 *****************************************************************************/
1505 #define PARSEERROR \
1506 if( p_vpar->picture.b_error ) \
1508 /* Mark this block as skipped (better than green blocks), and \
1509 * go to the next slice. */ \
1510 (*pi_mb_address)--; \
1511 vpar_DestroyMacroblock( &p_vpar->vfifo, p_mb ); \
1516 #define PARSEBLOCKS( MPEG1FUNC, MPEG2FUNC ) \
1518 i_mask = 1 << (3 + (1 << i_chroma_format)); \
1521 p_data1 = p_mb->p_picture->p_y \
1522 + p_mb->i_l_x + p_vpar->mb.i_l_y*(p_vpar->sequence.i_width); \
1524 for( i_b = 0 ; i_b < 4 ; i_b++, i_mask >>= 1 ) \
1526 if( p_mb->i_coded_block_pattern & i_mask ) \
1528 memset( p_mb->ppi_blocks[i_b], 0, 64*sizeof(dctelem_t) ); \
1530 MPEG2FUNC( p_vpar, p_mb, i_b, i_chroma_format ); \
1532 MPEG1FUNC( p_vpar, p_mb, i_b, i_chroma_format ); \
1534 /* Calculate block coordinates. */ \
1535 p_mb->p_data[i_b] = p_data1 \
1536 + pi_y[p_vpar->mb.b_dct_type][i_b] \
1537 * p_vpar->sequence.i_width \
1545 p_data1 = p_mb->p_picture->p_u \
1547 + p_vpar->mb.i_c_y \
1548 * (p_vpar->sequence.i_chroma_width); \
1549 p_data2 = p_mb->p_picture->p_v \
1551 + p_vpar->mb.i_c_y \
1552 * (p_vpar->sequence.i_chroma_width); \
1554 for( i_b = 4; i_b < 4 + (1 << i_chroma_format); \
1555 i_b++, i_mask >>= 1 ) \
1557 yuv_data_t * pp_data[2] = {p_data1, p_data2}; \
1559 if( p_mb->i_coded_block_pattern & i_mask ) \
1561 memset( p_mb->ppi_blocks[i_b], 0, 64*sizeof(dctelem_t) ); \
1563 MPEG2FUNC( p_vpar, p_mb, i_b, i_chroma_format ); \
1565 MPEG1FUNC( p_vpar, p_mb, i_b, i_chroma_format ); \
1567 /* Calculate block coordinates. */ \
1568 p_mb->p_data[i_b] = pp_data[i_b & 1] \
1569 + pi_y[p_vpar->mb.b_dct_type][i_b] \
1570 * p_vpar->sequence.i_chroma_width \
1578 static __inline__ void ParseMacroblock(
1579 vpar_thread_t * p_vpar,
1580 int * pi_mb_address, /* previous address to be
1581 * used for mb_addr_incr */
1582 int i_mb_previous, /* actual previous mb */
1583 int i_mb_base, /* non-zero if field structure */
1584 /* The following parameters are explicit in
1585 * optimized routines : */
1586 boolean_t b_mpeg2, /* you know what ? */
1587 int i_coding_type, /* I, P, B or D */
1588 int i_chroma_format, /* 4:2:0, 4:2:2 or 4:4:4 */
1589 int i_structure, /* T(OP), B(OTTOM) or F(RAME) */
1590 boolean_t b_second_field ) /* second field of a
1593 static f_motion_t pppf_motion[4][2][4] =
1595 { {NULL, NULL, NULL, NULL},
1596 {NULL, NULL, NULL, NULL}
1598 { {NULL, vdec_MotionFieldField420, vdec_MotionField16x8420,
1599 vdec_MotionFieldDMV420},
1600 {NULL, vdec_MotionFrameField420, vdec_MotionFrameFrame420,
1601 vdec_MotionFrameDMV420}
1603 { {NULL, vdec_MotionFieldField422, vdec_MotionField16x8422,
1604 vdec_MotionFieldDMV422},
1605 {NULL, vdec_MotionFrameField422, vdec_MotionFrameFrame422,
1606 vdec_MotionFrameDMV422}
1608 { {NULL, vdec_MotionFieldField444, vdec_MotionField16x8444,
1609 vdec_MotionFieldDMV444},
1610 {NULL, vdec_MotionFrameField444, vdec_MotionFrameFrame444,
1611 vdec_MotionFrameDMV444}
1614 static int pi_x[12] = {0,8,0,8,0,0,0,0,8,8,8,8};
1615 static int pi_y[2][12] = { {0,0,8,8,0,0,8,8,0,0,8,8},
1616 {0,0,1,1,0,0,1,1,0,0,1,1} };
1618 int i_mb, i_b, i_mask;
1619 macroblock_t * p_mb;
1620 yuv_data_t * p_data1;
1621 yuv_data_t * p_data2;
1623 *pi_mb_address += MacroblockAddressIncrement( p_vpar );
1625 if( *pi_mb_address - i_mb_previous - 1 )
1627 /* Skipped macroblock (ISO/IEC 13818-2 7.6.6). */
1629 /* Reset DC predictors (7.2.1). */
1630 p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
1631 = p_vpar->mb.pi_dc_dct_pred[2]
1632 = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
1634 if( i_coding_type == P_CODING_TYPE )
1636 /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
1637 memset( p_vpar->mb.pppi_pmv, 0, 8*sizeof(int) );
1640 for( i_mb = i_mb_previous + 1; i_mb < *pi_mb_address; i_mb++ )
1642 SkippedMacroblock( p_vpar, i_mb, i_mb_base, i_coding_type,
1643 i_chroma_format, i_structure, b_second_field );
1647 /* Get a macroblock structure. */
1648 if( (p_mb = vpar_NewMacroblock( &p_vpar->vfifo )) == NULL )
1654 p_vpar->picture.pp_mb[i_mb_base + *pi_mb_address] = p_mb;
1657 InitMacroblock( p_vpar, p_mb, i_coding_type, i_chroma_format,
1658 i_structure, b_second_field );
1660 /* Parse off macroblock_modes structure. */
1661 MacroblockModes( p_vpar, p_mb, i_chroma_format, i_coding_type,
1664 if( p_mb->i_mb_type & MB_QUANT )
1666 LoadQuantizerScale( p_vpar );
1669 if( (i_coding_type == P_CODING_TYPE || i_coding_type == B_CODING_TYPE)
1670 && (p_mb->i_mb_type & MB_MOTION_FORWARD) )
1673 DecodeMVMPEG2( p_vpar, p_mb, 0, i_structure );
1675 DecodeMVMPEG1( p_vpar, p_mb, 0, i_structure );
1679 if( (i_coding_type == B_CODING_TYPE)
1680 && (p_mb->i_mb_type & MB_MOTION_BACKWARD) )
1683 DecodeMVMPEG2( p_vpar, p_mb, 1, i_structure );
1685 DecodeMVMPEG1( p_vpar, p_mb, 1, i_structure );
1689 if( i_coding_type == P_CODING_TYPE
1690 && !(p_mb->i_mb_type & (MB_MOTION_FORWARD|MB_INTRA)) )
1692 /* Special No-MC macroblock in P pictures (7.6.3.5). */
1693 p_mb->i_mb_type |= MB_MOTION_FORWARD;
1694 memset( p_vpar->mb.pppi_pmv, 0, 8*sizeof(int) );
1695 memset( p_mb->pppi_motion_vectors, 0, 8*sizeof(int) );
1696 p_vpar->mb.i_motion_type = 1 + (i_structure == FRAME_STRUCTURE);
1697 p_mb->ppi_field_select[0][0] = (i_structure == BOTTOM_FIELD);
1700 if( (i_coding_type != I_CODING_TYPE) && !(p_mb->i_mb_type & MB_INTRA) )
1702 /* Reset DC predictors (7.2.1). */
1703 p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
1704 = p_vpar->mb.pi_dc_dct_pred[2]
1705 = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
1707 /* Motion function pointer. */
1708 p_mb->pf_motion = pppf_motion[i_chroma_format]
1709 [i_structure == FRAME_STRUCTURE]
1710 [p_vpar->mb.i_motion_type];
1712 if( p_mb->i_mb_type & MB_PATTERN )
1714 switch( i_chroma_format )
1717 p_mb->i_coded_block_pattern = CodedPattern420( p_vpar );
1720 p_mb->i_coded_block_pattern = CodedPattern422( p_vpar );
1723 p_mb->i_coded_block_pattern = CodedPattern444( p_vpar );
1728 p_mb->i_coded_block_pattern = 0;
1732 * Effectively decode blocks.
1734 PARSEBLOCKS( DecodeMPEG1NonIntra, DecodeMPEG2NonIntra )
1738 if( !p_vpar->picture.b_concealment_mv )
1740 /* Reset MV predictors. */
1741 memset( p_vpar->mb.pppi_pmv, 0, 8*sizeof(int) );
1746 DecodeMVMPEG2( p_vpar, p_mb, 0, i_structure );
1748 DecodeMVMPEG1( p_vpar, p_mb, 0, i_structure );
1749 RemoveBits( &p_vpar->bit_stream, 1 );
1752 if( p_mb->i_mb_type & MB_PATTERN )
1754 switch( i_chroma_format )
1757 p_mb->i_coded_block_pattern = CodedPattern420( p_vpar );
1760 p_mb->i_coded_block_pattern = CodedPattern422( p_vpar );
1763 p_mb->i_coded_block_pattern = CodedPattern444( p_vpar );
1768 p_mb->i_coded_block_pattern =
1769 (1 << (4 + (1 << i_chroma_format))) - 1;
1773 * Effectively decode blocks.
1775 PARSEBLOCKS( DecodeMPEG1Intra, DecodeMPEG2Intra )
1778 if( !p_vpar->picture.b_error )
1780 UpdateContext( p_vpar, i_structure );
1782 /* Decode the macroblock NOW ! */
1783 vdec_DecodeMacroblock( p_vpar->pp_vdec[0], p_mb );
1788 /* Mark this block as skipped (better than green blocks), and go
1789 * to the next slice. */
1791 vpar_DestroyMacroblock( &p_vpar->vfifo, p_mb );
1796 * Picture data parsing management
1799 /*****************************************************************************
1800 * SliceHeader : Parse the next slice structure
1801 *****************************************************************************/
1802 static __inline__ void SliceHeader( vpar_thread_t * p_vpar,
1803 int * pi_mb_address, int i_mb_base,
1804 u32 i_vert_code, boolean_t b_high,
1805 boolean_t b_dp_scalable,
1806 boolean_t b_mpeg2, int i_coding_type,
1807 int i_chroma_format, int i_structure,
1808 boolean_t b_second_field )
1810 int i_mb_address_save = *pi_mb_address;
1812 p_vpar->picture.b_error = 0;
1816 /* Picture with more than 2800 lines. */
1817 i_vert_code += GetBits( &p_vpar->bit_stream, 3 ) << 7;
1821 /* DATA_PARTITIONING scalability. */
1822 RemoveBits( &p_vpar->bit_stream, 7 ); /* priority_breakpoint */
1825 LoadQuantizerScale( p_vpar );
1827 if( GetBits( &p_vpar->bit_stream, 1 ) )
1829 /* intra_slice, slice_id */
1830 RemoveBits( &p_vpar->bit_stream, 8 );
1831 /* extra_information_slice */
1832 while( GetBits( &p_vpar->bit_stream, 1 ) )
1834 RemoveBits( &p_vpar->bit_stream, 8 );
1837 *pi_mb_address = (i_vert_code - 1)*p_vpar->sequence.i_mb_width;
1839 /* Reset DC coefficients predictors (ISO/IEC 13818-2 7.2.1). */
1840 p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
1841 = p_vpar->mb.pi_dc_dct_pred[2]
1842 = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
1844 /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
1845 memset( p_vpar->mb.pppi_pmv, 0, 8*sizeof(int) );
1849 ParseMacroblock( p_vpar, pi_mb_address, i_mb_address_save,
1850 i_mb_base, b_mpeg2, i_coding_type,
1851 i_chroma_format, i_structure,
1853 i_mb_address_save = *pi_mb_address;
1855 while( ShowBits( &p_vpar->bit_stream, 23 ) && !p_vpar->picture.b_error
1856 && !p_vpar->b_die );
1857 NextStartCode( p_vpar );
1860 /*****************************************************************************
1861 * PictureData : Parse off all macroblocks (ISO/IEC 13818-2 6.2.3.7)
1862 *****************************************************************************/
1863 static __inline__ void PictureData( vpar_thread_t * p_vpar, int i_mb_base,
1864 boolean_t b_high, boolean_t b_dp_scalable,
1865 boolean_t b_mpeg2, int i_coding_type,
1866 int i_chroma_format, int i_structure,
1867 boolean_t b_second_field )
1869 int i_mb_address = 0;
1872 NextStartCode( p_vpar );
1873 while( ((i_coding_type != I_CODING_TYPE && i_coding_type != D_CODING_TYPE)
1874 || !p_vpar->picture.b_error)
1875 && i_mb_address+i_mb_base < p_vpar->sequence.i_mb_size
1878 if( ((i_dummy = ShowBits( &p_vpar->bit_stream, 32 ))
1879 < SLICE_START_CODE_MIN) ||
1880 (i_dummy > SLICE_START_CODE_MAX) )
1882 intf_DbgMsg("vpar debug: premature end of picture\n");
1883 p_vpar->picture.b_error = 1;
1886 RemoveBits32( &p_vpar->bit_stream );
1888 /* Decode slice data. */
1889 SliceHeader( p_vpar, &i_mb_address, i_mb_base, i_dummy & 255,
1890 b_high, b_dp_scalable, b_mpeg2, i_coding_type,
1891 i_chroma_format, i_structure, b_second_field );
1894 /* Try to recover from error. If we missed less than half the
1895 * number of macroblocks of the picture, mark the missed ones
1897 if( (i_coding_type == P_CODING_TYPE || i_coding_type == B_CODING_TYPE)
1898 && p_vpar->picture.b_error &&
1899 ( (i_mb_address-i_mb_base) > (p_vpar->sequence.i_mb_size >> 1)
1900 || (i_structure != FRAME_STRUCTURE
1901 && (i_mb_address-i_mb_base) > (p_vpar->sequence.i_mb_size >> 2) ) ) )
1905 p_vpar->picture.b_error = 0;
1906 for( i_mb = i_mb_address + 1;
1907 i_mb < (p_vpar->sequence.i_mb_size
1908 << (i_structure != FRAME_STRUCTURE));
1911 SkippedMacroblock( p_vpar, i_mb, i_mb_base, i_coding_type,
1912 i_chroma_format, i_structure, b_second_field );
1917 /*****************************************************************************
1918 * vpar_PictureDataVWXYZ : Parse the next macroblock ; specific functions
1919 *****************************************************************************
1922 * X = chroma format ?
1924 * Z = second field ?
1925 *****************************************************************************/
1926 void vpar_PictureDataGENERIC( vpar_thread_t * p_vpar, int i_mb_base )
1928 PictureData( p_vpar, i_mb_base, (p_vpar->sequence.i_height > 2800),
1929 (p_vpar->sequence.i_scalable_mode == SC_DP),
1930 p_vpar->sequence.b_mpeg2, p_vpar->picture.i_coding_type,
1931 p_vpar->sequence.i_chroma_format,
1932 p_vpar->picture.i_structure,
1933 (p_vpar->picture.i_structure !=
1934 p_vpar->picture.i_current_structure) );
1937 #if (VPAR_OPTIM_LEVEL > 0)
1938 /* Optimizations for frame pictures */
1939 void vpar_PictureData2I420F0( vpar_thread_t * p_vpar, int i_mb_base )
1941 PictureData( p_vpar, 0, 0, 0, 1, I_CODING_TYPE, CHROMA_420,
1942 FRAME_STRUCTURE, 0 );
1945 void vpar_PictureData2P420F0( vpar_thread_t * p_vpar, int i_mb_base )
1947 PictureData( p_vpar, 0, 0, 0, 1, P_CODING_TYPE, CHROMA_420,
1948 FRAME_STRUCTURE, 0 );
1951 void vpar_PictureData2B420F0( vpar_thread_t * p_vpar, int i_mb_base )
1953 PictureData( p_vpar, 0, 0, 0, 1, B_CODING_TYPE, CHROMA_420,
1954 FRAME_STRUCTURE, 0 );
1958 #if (VPAR_OPTIM_LEVEL > 1)
1959 /* Optimizations for field pictures */
1960 void vpar_PictureData2I420TZ( vpar_thread_t * p_vpar, int i_mb_base )
1962 PictureData( p_vpar, i_mb_base, 0, 0, 1, I_CODING_TYPE, CHROMA_420,
1963 TOP_FIELD, (p_vpar->picture.i_structure !=
1964 p_vpar->picture.i_current_structure) );
1967 void vpar_PictureData2P420TZ( vpar_thread_t * p_vpar, int i_mb_base )
1969 PictureData( p_vpar, i_mb_base, 0, 0, 1, P_CODING_TYPE, CHROMA_420,
1970 TOP_FIELD, (p_vpar->picture.i_structure !=
1971 p_vpar->picture.i_current_structure) );
1974 void vpar_PictureData2B420TZ( vpar_thread_t * p_vpar, int i_mb_base )
1976 PictureData( p_vpar, i_mb_base, 0, 0, 1, B_CODING_TYPE, CHROMA_420,
1977 TOP_FIELD, (p_vpar->picture.i_structure !=
1978 p_vpar->picture.i_current_structure) );
1981 void vpar_PictureData2I420BZ( vpar_thread_t * p_vpar, int i_mb_base )
1983 PictureData( p_vpar, i_mb_base, 0, 0, 1, I_CODING_TYPE, CHROMA_420,
1984 BOTTOM_FIELD, (p_vpar->picture.i_structure !=
1985 p_vpar->picture.i_current_structure) );
1988 void vpar_PictureData2P420BZ( vpar_thread_t * p_vpar, int i_mb_base )
1990 PictureData( p_vpar, i_mb_base, 0, 0, 1, P_CODING_TYPE, CHROMA_420,
1991 BOTTOM_FIELD, (p_vpar->picture.i_structure !=
1992 p_vpar->picture.i_current_structure) );
1995 void vpar_PictureData2B420BZ( vpar_thread_t * p_vpar, int i_mb_base )
1997 PictureData( p_vpar, i_mb_base, 0, 0, 1, B_CODING_TYPE, CHROMA_420,
1998 BOTTOM_FIELD, (p_vpar->picture.i_structure !=
1999 p_vpar->picture.i_current_structure) );