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_init_table[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 * Structure to store the tables B14 & B15
163 * Is constructed from the tables below
165 dct_lookup_t ppl_dct_coef[2][16384];
168 /* Tables for ac DCT coefficients. There are cut in many parts to save space */
169 /* Table B-14, DCT coefficients table zero,
170 * codes 0100 ... 1xxx (used for first (DC) coefficient)
172 static dct_lookup_t pl_DCT_tab_dc[12] =
174 {0,2,4}, {2,1,4}, {1,1,3}, {1,1,3},
175 {0,1,1}, {0,1,1}, {0,1,1}, {0,1,1},
176 {0,1,1}, {0,1,1}, {0,1,1}, {0,1,1}
179 /* Table B-14, DCT coefficients table zero,
180 * codes 0100 ... 1xxx (used for all other coefficients)
182 static dct_lookup_t pl_DCT_tab_ac[12] =
184 {0,2,4}, {2,1,4}, {1,1,3}, {1,1,3},
185 {DCT_EOB,0,2}, {DCT_EOB,0,2}, {DCT_EOB,0,2}, {DCT_EOB,0,2}, /* EOB */
186 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2}
189 /* Table B-14, DCT coefficients table zero,
190 * codes 000001xx ... 00111xxx
192 static dct_lookup_t pl_DCT_tab0[60] =
194 {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6},
196 {2,2,7}, {2,2,7}, {9,1,7}, {9,1,7},
197 {0,4,7}, {0,4,7}, {8,1,7}, {8,1,7},
198 {7,1,6}, {7,1,6}, {7,1,6}, {7,1,6},
199 {6,1,6}, {6,1,6}, {6,1,6}, {6,1,6},
200 {1,2,6}, {1,2,6}, {1,2,6}, {1,2,6},
201 {5,1,6}, {5,1,6}, {5,1,6}, {5,1,6},
202 {13,1,8}, {0,6,8}, {12,1,8}, {11,1,8},
203 {3,2,8}, {1,3,8}, {0,5,8}, {10,1,8},
204 {0,3,5}, {0,3,5}, {0,3,5}, {0,3,5},
205 {0,3,5}, {0,3,5}, {0,3,5}, {0,3,5},
206 {4,1,5}, {4,1,5}, {4,1,5}, {4,1,5},
207 {4,1,5}, {4,1,5}, {4,1,5}, {4,1,5},
208 {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
209 {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5}
212 /* Table B-15, DCT coefficients table one,
213 * codes 000001xx ... 11111111
215 static dct_lookup_t pl_DCT_tab0a[252] =
217 {65,0,6}, {65,0,6}, {65,0,6}, {65,0,6}, /* Escape */
218 {7,1,7}, {7,1,7}, {8,1,7}, {8,1,7},
219 {6,1,7}, {6,1,7}, {2,2,7}, {2,2,7},
220 {0,7,6}, {0,7,6}, {0,7,6}, {0,7,6},
221 {0,6,6}, {0,6,6}, {0,6,6}, {0,6,6},
222 {4,1,6}, {4,1,6}, {4,1,6}, {4,1,6},
223 {5,1,6}, {5,1,6}, {5,1,6}, {5,1,6},
224 {1,5,8}, {11,1,8}, {0,11,8}, {0,10,8},
225 {13,1,8}, {12,1,8}, {3,2,8}, {1,4,8},
226 {2,1,5}, {2,1,5}, {2,1,5}, {2,1,5},
227 {2,1,5}, {2,1,5}, {2,1,5}, {2,1,5},
228 {1,2,5}, {1,2,5}, {1,2,5}, {1,2,5},
229 {1,2,5}, {1,2,5}, {1,2,5}, {1,2,5},
230 {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
231 {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
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 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
235 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
236 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
237 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
238 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
239 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
240 {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4}, /* EOB */
241 {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
242 {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
243 {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
244 {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
245 {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
246 {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
247 {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
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,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
259 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
260 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
261 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
262 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
263 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
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,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
267 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
268 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
269 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
270 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
271 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
272 {0,4,5}, {0,4,5}, {0,4,5}, {0,4,5},
273 {0,4,5}, {0,4,5}, {0,4,5}, {0,4,5},
274 {0,5,5}, {0,5,5}, {0,5,5}, {0,5,5},
275 {0,5,5}, {0,5,5}, {0,5,5}, {0,5,5},
276 {9,1,7}, {9,1,7}, {1,3,7}, {1,3,7},
277 {10,1,7}, {10,1,7}, {0,8,7}, {0,8,7},
278 {0,9,7}, {0,9,7}, {0,12,8}, {0,13,8},
279 {2,3,8}, {4,2,8}, {0,14,8}, {0,15,8}
282 /* Table B-14, DCT coefficients table zero,
283 * codes 0000001000 ... 0000001111
285 static dct_lookup_t pl_DCT_tab1[8] =
287 {16,1,10}, {5,2,10}, {0,7,10}, {2,3,10},
288 {1,4,10}, {15,1,10}, {14,1,10}, {4,2,10}
291 /* Table B-15, DCT coefficients table one,
292 * codes 000000100x ... 000000111x
294 static dct_lookup_t pl_DCT_tab1a[8] =
296 {5,2,9}, {5,2,9}, {14,1,9}, {14,1,9},
297 {2,4,10}, {16,1,10}, {15,1,9}, {15,1,9}
300 /* Table B-14/15, DCT coefficients table zero / one,
301 * codes 000000010000 ... 000000011111
303 static dct_lookup_t pl_DCT_tab2[16] =
305 {0,11,12}, {8,2,12}, {4,3,12}, {0,10,12},
306 {2,4,12}, {7,2,12}, {21,1,12}, {20,1,12},
307 {0,9,12}, {19,1,12}, {18,1,12}, {1,5,12},
308 {3,3,12}, {0,8,12}, {6,2,12}, {17,1,12}
311 /* Table B-14/15, DCT coefficients table zero / one,
312 * codes 0000000010000 ... 0000000011111
314 static dct_lookup_t pl_DCT_tab3[16] =
316 {10,2,13}, {9,2,13}, {5,3,13}, {3,4,13},
317 {2,5,13}, {1,7,13}, {1,6,13}, {0,15,13},
318 {0,14,13}, {0,13,13}, {0,12,13}, {26,1,13},
319 {25,1,13}, {24,1,13}, {23,1,13}, {22,1,13}
322 /* Table B-14/15, DCT coefficients table zero / one,
323 * codes 00000000010000 ... 00000000011111
325 static dct_lookup_t pl_DCT_tab4[16] =
327 {0,31,14}, {0,30,14}, {0,29,14}, {0,28,14},
328 {0,27,14}, {0,26,14}, {0,25,14}, {0,24,14},
329 {0,23,14}, {0,22,14}, {0,21,14}, {0,20,14},
330 {0,19,14}, {0,18,14}, {0,17,14}, {0,16,14}
333 /* Table B-14/15, DCT coefficients table zero / one,
334 * codes 000000000010000 ... 000000000011111
336 static dct_lookup_t pl_DCT_tab5[16] =
338 {0,40,15}, {0,39,15}, {0,38,15}, {0,37,15},
339 {0,36,15}, {0,35,15}, {0,34,15}, {0,33,15},
340 {0,32,15}, {1,14,15}, {1,13,15}, {1,12,15},
341 {1,11,15}, {1,10,15}, {1,9,15}, {1,8,15}
344 /* Table B-14/15, DCT coefficients table zero / one,
345 * codes 0000000000010000 ... 0000000000011111
347 static dct_lookup_t pl_DCT_tab6[16] =
349 {1,18,16}, {1,17,16}, {1,16,16}, {1,15,16},
350 {6,3,16}, {16,2,16}, {15,2,16}, {14,2,16},
351 {13,2,16}, {12,2,16}, {11,2,16}, {31,1,16},
352 {30,1,16}, {29,1,16}, {28,1,16}, {27,1,16}
357 * Initialization of lookup tables
360 /*****************************************************************************
361 * vpar_InitCrop : Initialize the crop table for saturation
362 * (ISO/IEC 13818-2 section 7.4.3)
363 *****************************************************************************/
364 #if defined(MPEG2_COMPLIANT) && !defined(VDEC_DFT)
365 void vpar_InitCrop( vpar_thread_t * p_vpar )
369 p_vpar->pi_crop = p_vpar->pi_crop_buf + 4096;
371 for( i_dummy = -4096; i_dummy < -2048; i_dummy++ )
373 p_vpar->pi_crop[i_dummy] = -2048;
375 for( ; i_dummy < 2047; i_dummy++ )
377 p_vpar->pi_crop[i_dummy] = i_dummy;
379 for( ; i_dummy < 4095; i_dummy++ )
381 p_vpar->pi_crop[i_dummy] = 2047;
386 /*****************************************************************************
387 * vpar_InitMbAddrInc : Initialize the lookup table for mb_addr_inc
388 *****************************************************************************/
390 /* Function for filling up the lookup table for mb_addr_inc */
391 static void __inline__ FillMbAddrIncTable( vpar_thread_t * p_vpar,
392 int i_start, int i_end, int i_step,
393 int * pi_value, int i_length )
396 for( i_pos = i_start ; i_pos < i_end ; i_pos += i_step )
398 for( i_offset = 0 ; i_offset < i_step ; i_offset ++ )
400 p_vpar->pl_mb_addr_inc[i_pos + i_offset].i_value = * pi_value;
401 p_vpar->pl_mb_addr_inc[i_pos + i_offset].i_length = i_length;
407 /* Function that initialize the table using the last one */
408 void vpar_InitMbAddrInc( vpar_thread_t * p_vpar )
413 for( i_dummy = 0 ; i_dummy < 8 ; i_dummy++ )
415 p_vpar->pl_mb_addr_inc[i_dummy].i_value = MB_ERROR;
416 p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
419 p_vpar->pl_mb_addr_inc[8].i_value = MB_ADDRINC_ESCAPE;
420 p_vpar->pl_mb_addr_inc[8].i_length = 11;
422 for( i_dummy = 9 ; i_dummy < 15 ; i_dummy ++ )
424 p_vpar->pl_mb_addr_inc[i_dummy].i_value = MB_ERROR;
425 p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
428 p_vpar->pl_mb_addr_inc[15].i_value = MB_ADDRINC_STUFFING;
429 p_vpar->pl_mb_addr_inc[15].i_length = 11;
431 for( i_dummy = 16; i_dummy < 24; 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;
439 FillMbAddrIncTable( p_vpar, 24, 36, 1, &i_value, 11 );
440 FillMbAddrIncTable( p_vpar, 36, 48, 2, &i_value, 10 );
441 FillMbAddrIncTable( p_vpar, 48, 96, 8, &i_value, 8 );
442 FillMbAddrIncTable( p_vpar, 96, 128, 16, &i_value, 7 );
443 FillMbAddrIncTable( p_vpar, 128, 256, 64, &i_value, 5 );
444 FillMbAddrIncTable( p_vpar, 256, 512, 128, &i_value, 4 );
445 FillMbAddrIncTable( p_vpar, 512, 1024, 256, &i_value, 3 );
446 FillMbAddrIncTable( p_vpar, 1024, 2048, 1024, &i_value, 1 );
449 /*****************************************************************************
450 * vpar_Init*MBType : Initialize lookup table for the Macroblock type
451 *****************************************************************************/
453 /* Fonction for filling up the tables */
454 static void __inline__ FillMBType( vpar_thread_t * p_vpar,
463 for( i_dummy = i_start ; i_dummy < i_end ; i_dummy++ )
465 p_vpar->ppl_mb_type[i_mb_type][i_dummy].i_value = i_value;
466 p_vpar->ppl_mb_type[i_mb_type][i_dummy].i_length = i_length;
470 /* Fonction that fills the table for P MB_Type */
471 void vpar_InitPMBType( vpar_thread_t * p_vpar )
473 FillMBType( p_vpar, 0, 32, 64, MB_MOTION_FORWARD|MB_PATTERN, 1 );
474 FillMBType( p_vpar, 0, 16, 32, MB_PATTERN, 2 );
475 FillMBType( p_vpar, 0, 8, 16, MB_MOTION_FORWARD, 3 );
476 FillMBType( p_vpar, 0, 6, 8, MB_INTRA, 5 );
477 FillMBType( p_vpar, 0, 4, 6, MB_QUANT|MB_MOTION_FORWARD|MB_PATTERN, 5 );
478 FillMBType( p_vpar, 0, 2, 4, MB_QUANT|MB_PATTERN, 5 );
479 p_vpar->ppl_mb_type[0][1].i_value = MB_QUANT|MB_INTRA;
480 p_vpar->ppl_mb_type[0][1].i_length = 6;
481 p_vpar->ppl_mb_type[0][0].i_value = MB_ERROR;
482 p_vpar->ppl_mb_type[0][0].i_length = 0;
485 /* Fonction that fills the table for B MB_Type */
486 void vpar_InitBMBType( vpar_thread_t * p_vpar )
488 FillMBType( p_vpar, 1, 48, 64, MB_MOTION_FORWARD
489 |MB_MOTION_BACKWARD|MB_PATTERN, 2 );
490 FillMBType( p_vpar, 1, 32, 48, MB_MOTION_FORWARD|MB_MOTION_BACKWARD, 2 );
491 FillMBType( p_vpar, 1, 24, 32, MB_MOTION_BACKWARD|MB_PATTERN, 3 );
492 FillMBType( p_vpar, 1, 16, 24, MB_MOTION_BACKWARD, 3 );
493 FillMBType( p_vpar, 1, 12, 16, MB_MOTION_FORWARD|MB_PATTERN, 4 );
494 FillMBType( p_vpar, 1, 8, 12, MB_MOTION_FORWARD, 4 );
495 FillMBType( p_vpar, 1, 6, 8, MB_INTRA, 5 );
496 FillMBType( p_vpar, 1, 4, 6, MB_QUANT|MB_MOTION_FORWARD
497 |MB_MOTION_BACKWARD|MB_PATTERN, 5 );
498 p_vpar->ppl_mb_type[1][3].i_value = MB_QUANT|MB_MOTION_FORWARD|MB_PATTERN;
499 p_vpar->ppl_mb_type[1][3].i_length = 6;
500 p_vpar->ppl_mb_type[1][2].i_value = MB_QUANT|MB_MOTION_BACKWARD|MB_PATTERN;
501 p_vpar->ppl_mb_type[1][2].i_length = 6;
502 p_vpar->ppl_mb_type[1][1].i_value = MB_QUANT|MB_INTRA;
503 p_vpar->ppl_mb_type[1][1].i_length = 6;
504 p_vpar->ppl_mb_type[1][0].i_value =MB_ERROR;
505 p_vpar->ppl_mb_type[1][0].i_length = 0;
508 /*****************************************************************************
509 * vpar_InitCodedPattern : Initialize the lookup table for decoding
510 * coded block pattern
511 *****************************************************************************/
512 void vpar_InitCodedPattern( vpar_thread_t * p_vpar )
514 p_vpar->pl_coded_pattern = (lookup_t*) pl_coded_pattern_init_table;
517 /*****************************************************************************
518 * vpar_InitDCTTables : Initialize tables giving the length of the dct
519 * coefficient from the vlc code
520 *****************************************************************************/
522 /* First fonction for filling the table */
523 static void __inline__ FillDCTTable( dct_lookup_t * p_tab_dest, dct_lookup_t * p_tab_src,
524 int i_step, int i_nb_elem, int i_offset )
526 int i_dummy, i_dummy2;
528 for( i_dummy=0 ; i_dummy < i_nb_elem ; i_dummy++ )
530 for( i_dummy2=0 ; i_dummy2 < i_step ; i_dummy2++ )
532 p_tab_dest[(i_dummy+i_offset)*i_step+i_dummy2] = p_tab_src[i_dummy];
538 /* Fonction that actually fills the table or create the pointers */
539 void vpar_InitDCTTables( vpar_thread_t * p_vpar )
541 /* Tables are cut in two parts to reduce memory occupation */
542 p_vpar->pppl_dct_dc_size[0][0] = pl_dct_dc_lum_init_table_1;
543 p_vpar->pppl_dct_dc_size[0][1] = pl_dct_dc_lum_init_table_2;
544 p_vpar->pppl_dct_dc_size[1][0] = pl_dct_dc_chrom_init_table_1;
545 p_vpar->pppl_dct_dc_size[1][1] = pl_dct_dc_chrom_init_table_2;
547 memset( ppl_dct_coef[0], MB_ERROR, 16 );
548 memset( ppl_dct_coef[1], MB_ERROR, 16 );
550 /* For table B14 & B15, we have a pointer to tables */
551 /* We fill the table thanks to the fonction defined above */
552 FillDCTTable( ppl_dct_coef[0], pl_DCT_tab0, 256, 60, 4 );
553 FillDCTTable( ppl_dct_coef[0], pl_DCT_tab1, 64, 8, 8 );
554 FillDCTTable( ppl_dct_coef[0], pl_DCT_tab2, 16, 16, 16 );
555 FillDCTTable( ppl_dct_coef[0], pl_DCT_tab3, 8, 16, 16 );
556 FillDCTTable( ppl_dct_coef[0], pl_DCT_tab4, 4, 16, 16 );
557 FillDCTTable( ppl_dct_coef[0], pl_DCT_tab5, 2, 16, 16 );
558 FillDCTTable( ppl_dct_coef[0], pl_DCT_tab6, 1, 16, 16 );
560 FillDCTTable( ppl_dct_coef[1], pl_DCT_tab0a, 256, 60, 4 );
561 FillDCTTable( ppl_dct_coef[1], pl_DCT_tab1a, 64, 8, 8 );
562 FillDCTTable( ppl_dct_coef[1], pl_DCT_tab2, 16, 16, 16 );
563 FillDCTTable( ppl_dct_coef[1], pl_DCT_tab3, 8, 16, 16 );
564 FillDCTTable( ppl_dct_coef[1], pl_DCT_tab4, 4, 16, 16 );
565 FillDCTTable( ppl_dct_coef[1], pl_DCT_tab5, 2, 16, 16 );
566 FillDCTTable( ppl_dct_coef[1], pl_DCT_tab6, 1, 16, 16 );
574 /*****************************************************************************
575 * DecodeMPEG1NonIntra : decode MPEG-1 non-intra blocks
576 *****************************************************************************/
577 static __inline__ void DecodeMPEG1NonIntra( vpar_thread_t * p_vpar,
578 macroblock_t * p_mb, int i_b,
579 int i_chroma_format )
582 if( p_vpar->picture.i_coding_type == D_CODING_TYPE )
584 /* Remove end_of_macroblock (always 1, prevents startcode emulation)
585 * ISO/IEC 11172-2 section 2.4.2.7 and 2.4.3.6 */
586 RemoveBits( &p_vpar->bit_stream, 1 );
590 /*****************************************************************************
591 * DecodeMPEG1Intra : decode MPEG-1 intra blocks
592 *****************************************************************************/
593 static __inline__ void DecodeMPEG1Intra( vpar_thread_t * p_vpar,
594 macroblock_t * p_mb, int i_b ,
595 int i_chroma_format )
598 if( p_vpar->picture.i_coding_type == D_CODING_TYPE )
600 /* Remove end_of_macroblock (always 1, prevents startcode emulation)
601 * ISO/IEC 11172-2 section 2.4.2.7 and 2.4.3.6 */
602 RemoveBits( &p_vpar->bit_stream, 1 );
606 /*****************************************************************************
607 * DecodeMPEG2NonIntra : decode MPEG-2 non-intra blocks
608 *****************************************************************************/
609 static __inline__ void DecodeMPEG2NonIntra( vpar_thread_t * p_vpar,
610 macroblock_t * p_mb, int i_b,
611 int i_chroma_format )
626 /* Lookup Table for the chromatic component */
627 static int pi_cc_index[12] = { 0, 0, 0, 0, 1, 2, 1, 2, 1, 2 };
629 i_cc = pi_cc_index[i_b];
631 /* Determine whether it is luminance or not (chrominance) */
632 i_type = ( i_cc + 1 ) >> 1;
634 /* Give a pointer to the quantization matrices for intra blocks */
635 if( (i_chroma_format == CHROMA_420) || (!i_type) )
637 pi_quant = p_vpar->sequence.nonintra_quant.pi_matrix;
641 pi_quant = p_vpar->sequence.chroma_nonintra_quant.pi_matrix;
644 /* Decoding of the AC coefficients */
648 for( i_parse = 0; ; i_parse++ )
650 i_code = ShowBits( &p_vpar->bit_stream, 16 );
651 if( i_code >= 16384 )
655 i_run = pl_DCT_tab_dc[(i_code>>12)-4].i_run;
656 i_level = pl_DCT_tab_dc[(i_code>>12)-4].i_level;
657 i_length = pl_DCT_tab_dc[(i_code>>12)-4].i_length;
661 i_run = pl_DCT_tab_ac[(i_code>>12)-4].i_run;
662 i_level = pl_DCT_tab_ac[(i_code>>12)-4].i_level;
663 i_length = pl_DCT_tab_ac[(i_code>>12)-4].i_length;
666 else if( i_code >= 1024 )
668 i_run = pl_DCT_tab0[(i_code>>8)-4].i_run;
669 i_length = pl_DCT_tab0[(i_code>>8)-4].i_length;
670 i_level = pl_DCT_tab0[(i_code>>8)-4].i_level;
674 i_run = ppl_dct_coef[0][i_code].i_run;
675 i_length = ppl_dct_coef[0][i_code].i_length;
676 i_level = ppl_dct_coef[0][i_code].i_level;
680 RemoveBits( &p_vpar->bit_stream, i_length );
685 i_run = GetBits( &p_vpar->bit_stream, 6 );
686 i_level = GetBits( &p_vpar->bit_stream, 12 );
687 i_level = (b_sign = ( i_level > 2047 )) ? 4096 - i_level
693 p_mb->pf_idct[i_b] = vdec_SparseIDCT;
694 p_mb->pi_sparse_pos[i_b] = i_coef;
698 p_mb->pf_idct[i_b] = vdec_IDCT;
704 b_sign = GetBits( &p_vpar->bit_stream, 1 );
715 i_pos = pi_scan[p_vpar->picture.b_alternate_scan][i_parse];
716 i_level = ( ((i_level << 1) + 1) * p_vpar->mb.i_quantizer_scale
717 * pi_quant[i_pos] ) >> 5;
718 p_mb->ppi_blocks[i_b][i_pos] = b_sign ? -i_level : i_level;
721 intf_ErrMsg("vpar error: DCT coeff (non-intra) is out of bounds\n");
722 p_vpar->picture.b_error = 1;
725 /*****************************************************************************
726 * DecodeMPEG2Intra : decode MPEG-2 intra blocks
727 *****************************************************************************/
728 static __inline__ void DecodeMPEG2Intra( vpar_thread_t * p_vpar,
729 macroblock_t * p_mb, int i_b,
730 int i_chroma_format )
744 boolean_t b_vlc_intra;
748 /* Lookup Table for the chromatic component */
749 static int pi_cc_index[12] = { 0, 0, 0, 0, 1, 2, 1, 2, 1, 2 };
750 i_cc = pi_cc_index[i_b];
752 /* Determine whether it is luminance or not (chrominance) */
753 i_type = ( i_cc + 1 ) >> 1;
755 /* Give a pointer to the quantization matrices for intra blocks */
756 if( (i_chroma_format == CHROMA_420) || (!i_type) )
758 pi_quant = p_vpar->sequence.intra_quant.pi_matrix;
762 pi_quant = p_vpar->sequence.chroma_intra_quant.pi_matrix;
766 /* Decoding of the DC intra coefficient */
767 /* The nb of bits to parse depends on i_type */
768 i_code = ShowBits( &p_vpar->bit_stream, 9 + i_type );
770 /* To reduce memory occupation, there are two lookup tables
771 * See InitDCT above */
772 i_code5 = i_code >> (4+i_type);
774 /* Shall we lookup in the first or in the second table ? */
775 i_select = ( i_code5 == 31 );
776 /* Offset value for looking in the second table */
777 i_offset = 0x1f0 + ( i_type * 0x1f0 );
778 i_pos = ( i_code5 * ( ! i_select ) ) + ( ( i_code - i_offset ) * i_select );
779 i_dct_dc_size = p_vpar->pppl_dct_dc_size[i_type][i_select][i_pos].i_value;
782 if( !i_type/*i_b < 4*/ )
785 i_code = ShowBits(&p_vpar->bit_stream, 5);
788 i_dct_dc_size = pl_dct_dc_lum_init_table_1[i_code].i_value;
789 i_length = pl_dct_dc_lum_init_table_1[i_code].i_length;
790 RemoveBits( &p_vpar->bit_stream, i_length);
794 i_code = ShowBits(&p_vpar->bit_stream, 9) - 0x1f0;
795 i_dct_dc_size = pl_dct_dc_lum_init_table_2[i_code].i_value;
796 i_length = pl_dct_dc_lum_init_table_2[i_code].i_length;
797 RemoveBits( &p_vpar->bit_stream, i_length);
803 i_code = ShowBits(&p_vpar->bit_stream, 5);
807 i_dct_dc_size = pl_dct_dc_chrom_init_table_1[i_code].i_value;
808 i_length = pl_dct_dc_chrom_init_table_1[i_code].i_length;
809 RemoveBits(&p_vpar->bit_stream, i_length);
813 i_code = ShowBits(&p_vpar->bit_stream, 10) - 0x3e0;
814 i_dct_dc_size = pl_dct_dc_chrom_init_table_2[i_code].i_value;
815 i_length = pl_dct_dc_chrom_init_table_2[i_code].i_length;
816 RemoveBits( &p_vpar->bit_stream, i_length);
819 if (i_dct_dc_size==0)
823 i_dct_dc_diff = GetBits( &p_vpar->bit_stream, i_dct_dc_size);
824 if ((i_dct_dc_diff & (1<<(i_dct_dc_size-1)))==0)
825 i_dct_dc_diff-= (1<<i_dct_dc_size) - 1;
828 /* Dump the variable length code */
829 //RemoveBits( &p_vpar->bit_stream,
830 // p_vpar->pppl_dct_dc_size[i_type][i_select][i_pos].i_length );
832 /* Read the actual code with the good length */
833 p_vpar->mb.pi_dc_dct_pred[i_cc] += i_dct_dc_diff;
835 p_mb->ppi_blocks[i_b][0] = ( p_vpar->mb.pi_dc_dct_pred[i_cc] <<
836 ( 3 - p_vpar->picture.i_intra_dc_precision ) );
837 i_nc = ( p_vpar->mb.pi_dc_dct_pred[i_cc] != 0 );
839 /* Decoding of the AC coefficients */
842 b_vlc_intra = p_vpar->picture.b_intra_vlc_format;
843 for( i_parse = 1; /*i_parse < 64*/; i_parse++ )
845 i_code = ShowBits( &p_vpar->bit_stream, 16 );
846 if( i_code >= 16384 )
850 i_run = pl_DCT_tab0a[(i_code>>8)-4].i_run;
851 i_level = pl_DCT_tab0a[(i_code>>8)-4].i_level;
852 i_length = pl_DCT_tab0a[(i_code>>8)-4].i_length;
853 //fprintf( stderr, "**********> %d, %d, %d *******\n", i_run, i_level, (i_code>>8)-4 );
857 i_run = pl_DCT_tab_ac[(i_code>>12)-4].i_run;
858 i_level = pl_DCT_tab_ac[(i_code>>12)-4].i_level;
859 i_length = pl_DCT_tab_ac[(i_code>>12)-4].i_length;
864 i_run = ppl_dct_coef[b_vlc_intra][i_code].i_run;
865 i_length = ppl_dct_coef[b_vlc_intra][i_code].i_length;
866 i_level = ppl_dct_coef[b_vlc_intra][i_code].i_level;
872 int intra_vlc_format = b_vlc_intra;
875 if (code>=16384 && !intra_vlc_format)
876 tab = pl_DCT_tab_ac[(code>>12)-4];
879 if (intra_vlc_format)
880 tab = pl_DCT_tab0a[(code>>8)-4];
882 tab = pl_DCT_tab0[(code>>8)-4];
886 if (intra_vlc_format)
887 tab = pl_DCT_tab1a[(code>>6)-8];
889 tab = pl_DCT_tab1[(code>>6)-8];
892 tab = pl_DCT_tab2[(code>>4)-16];
894 tab = pl_DCT_tab3[(code>>3)-16];
896 tab = pl_DCT_tab4[(code>>2)-16];
898 tab = pl_DCT_tab5[(code>>1)-16];
900 tab = pl_DCT_tab6[code-16];
903 fprintf( stderr, "invalid Huffman code in Decode_MPEG2_Intra_Block()\n");
906 if( (i_run != tab.i_run) || (i_length != tab.i_length) || (i_level != tab.i_level) )
908 fprintf( stderr, "ET M....... !!!\n" );
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 /*p_mb->ppi_blocks[i_b][i_parse] = ( b_sign = ( i_level > 2047 ) )
925 ? ( -4096 + i_level )
927 i_level = (b_sign = ( i_level > 2047 )) ? 4096 - i_level
933 p_mb->pf_idct[i_b] = vdec_SparseIDCT;
934 p_mb->pi_sparse_pos[i_b] = i_coef;
938 p_mb->pf_idct[i_b] = vdec_IDCT;
944 b_sign = GetBits( &p_vpar->bit_stream, 1 );
956 i_pos = pi_scan[p_vpar->picture.b_alternate_scan][i_parse];
957 i_level = ( i_level *
958 p_vpar->mb.i_quantizer_scale *
959 pi_quant[i_pos] ) >> 4;
960 p_mb->ppi_blocks[i_b][i_pos] = b_sign ? -i_level : i_level;
963 intf_ErrMsg("vpar error: DCT coeff (intra) is out of bounds\n");
964 p_vpar->picture.b_error = 1;
972 /****************************************************************************
973 * MotionCode : Parse the next motion code
974 ****************************************************************************/
975 static __inline__ int MotionCode( vpar_thread_t * p_vpar )
978 static lookup_t pl_mv_tab0[8] =
979 { {-1,0}, {3,3}, {2,2}, {2,2}, {1,1}, {1,1}, {1,1}, {1,1} };
980 /* Table B-10, motion_code, codes 0000011 ... 000011x */
981 static lookup_t pl_mv_tab1[8] =
982 { {-1,0}, {-1,0}, {-1,0}, {7,6}, {6,6}, {5,6}, {4,5}, {4,5} };
983 /* Table B-10, motion_code, codes 0000001100 ... 000001011x */
984 static lookup_t pl_mv_tab2[12] = {
985 {16,9}, {15,9}, {14,9}, {13,9},
986 {12,9}, {11,9}, {10,8}, {10,8},
987 {9,8}, {9,8}, {8,8}, {8,8} };
989 if( GetBits( &p_vpar->bit_stream, 1 ) )
993 if( (i_code = ShowBits( &p_vpar->bit_stream, 9) ) >= 64 )
996 RemoveBits( &p_vpar->bit_stream, pl_mv_tab0[i_code].i_length );
997 return( GetBits( &p_vpar->bit_stream, 1 ) ?
998 -pl_mv_tab0[i_code].i_value : pl_mv_tab0[i_code].i_value );
1004 RemoveBits( &p_vpar->bit_stream, pl_mv_tab1[i_code].i_length );
1005 return( GetBits( &p_vpar->bit_stream, 1 ) ?
1006 -pl_mv_tab1[i_code].i_value : pl_mv_tab1[i_code].i_value );
1009 if( (i_code -= 12) < 0 )
1011 p_vpar->picture.b_error = 1;
1012 intf_DbgMsg( "vpar debug: Invalid motion_vector code\n" );
1016 RemoveBits( &p_vpar->bit_stream, pl_mv_tab2[i_code].i_length );
1017 return( GetBits( &p_vpar->bit_stream, 1 ) ?
1018 -pl_mv_tab2[i_code].i_value : pl_mv_tab2[i_code].i_value );
1021 /****************************************************************************
1022 * DecodeMotionVector : Decode a motion_vector
1023 ****************************************************************************/
1024 static __inline__ void DecodeMotionVector( int * pi_prediction, int i_r_size,
1025 int i_motion_code, int i_motion_residual, int i_full_pel )
1027 int i_limit, i_vector;
1029 /* ISO/IEC 13818-1 section 7.6.3.1 */
1030 i_limit = 16 << i_r_size;
1031 i_vector = *pi_prediction >> i_full_pel;
1033 if( i_motion_code > 0 )
1035 i_vector += ((i_motion_code-1) << i_r_size) + i_motion_residual + 1;
1036 if( i_vector >= i_limit )
1037 i_vector -= i_limit + i_limit;
1039 else if( i_motion_code < 0 )
1041 i_vector -= ((-i_motion_code-1) << i_r_size) + i_motion_residual + 1;
1042 if( i_vector < -i_limit )
1043 i_vector += i_limit + i_limit;
1045 *pi_prediction = i_vector << i_full_pel;
1048 /****************************************************************************
1049 * MotionVector : Parse the next motion_vector field
1050 ****************************************************************************/
1051 static __inline__ void MotionVector( vpar_thread_t * p_vpar,
1052 macroblock_t * p_mb, int i_r,
1053 int i_s, int i_full_pel, int i_structure )
1055 int i_motion_code, i_motion_residual;
1057 int pi_dm_vector[2];
1059 i_r_size = p_vpar->picture.ppi_f_code[i_s][0] - 1;
1060 i_motion_code = MotionCode( p_vpar );
1061 i_motion_residual = (i_r_size != 0 && i_motion_code != 0) ?
1062 GetBits( &p_vpar->bit_stream, i_r_size) : 0;
1063 DecodeMotionVector( &p_vpar->mb.pppi_pmv[i_r][i_s][0], i_r_size,
1064 i_motion_code, i_motion_residual, i_full_pel );
1065 p_mb->pppi_motion_vectors[i_r][i_s][0] = p_vpar->mb.pppi_pmv[i_r][i_s][0];
1067 if( p_vpar->mb.b_dmv )
1069 if( GetBits(&p_vpar->bit_stream, 1) )
1071 pi_dm_vector[0] = GetBits( &p_vpar->bit_stream, 1 ) ? -1 : 1;
1075 pi_dm_vector[0] = 0;
1079 i_r_size = p_vpar->picture.ppi_f_code[i_s][1]-1;
1080 i_motion_code = MotionCode( p_vpar );
1081 i_motion_residual = (i_r_size != 0 && i_motion_code != 0) ?
1082 GetBits( &p_vpar->bit_stream, i_r_size) : 0;
1085 if( (p_vpar->mb.i_mv_format == MOTION_FIELD)
1086 && (i_structure == FRAME_STRUCTURE) )
1088 p_vpar->mb.pppi_pmv[i_r][i_s][1] >>= 1;
1091 DecodeMotionVector( &p_vpar->mb.pppi_pmv[i_r][i_s][1], i_r_size,
1092 i_motion_code, i_motion_residual, i_full_pel );
1094 if( (p_vpar->mb.i_mv_format == MOTION_FIELD)
1095 && (i_structure == FRAME_STRUCTURE) )
1096 p_vpar->mb.pppi_pmv[i_r][i_s][1] <<= 1;
1098 p_mb->pppi_motion_vectors[i_r][i_s][1] = p_vpar->mb.pppi_pmv[i_r][i_s][1];
1100 if( p_vpar->mb.b_dmv )
1102 if( GetBits(&p_vpar->bit_stream, 1) )
1104 pi_dm_vector[1] = GetBits( &p_vpar->bit_stream, 1 ) ? -1 : 1;
1108 pi_dm_vector[1] = 0;
1111 /* Dual Prime Arithmetic (ISO/IEC 13818-2 section 7.6.3.6). */
1113 #define i_mv_x p_mb->pppi_motion_vectors[0][0][0]
1114 if( i_structure == FRAME_STRUCTURE )
1116 #define i_mv_y (p_mb->pppi_motion_vectors[0][0][1] << 1)
1117 if( p_vpar->picture.b_top_field_first )
1119 /* vector for prediction of top field from bottom field */
1120 p_mb->ppi_dmv[0][0] = ((i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1121 p_mb->ppi_dmv[0][1] = ((i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] - 1;
1123 /* vector for prediction of bottom field from top field */
1124 p_mb->ppi_dmv[1][0] = ((3*i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1125 p_mb->ppi_dmv[1][1] = ((3*i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] + 1;
1129 /* vector for prediction of top field from bottom field */
1130 p_mb->ppi_dmv[0][0] = ((3*i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1131 p_mb->ppi_dmv[0][1] = ((3*i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] - 1;
1133 /* vector for prediction of bottom field from top field */
1134 p_mb->ppi_dmv[1][0] = ((i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1135 p_mb->ppi_dmv[1][1] = ((i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] + 1;
1141 #define i_mv_y p_mb->pppi_motion_vectors[0][0][1]
1142 /* vector for prediction from field of opposite 'parity' */
1143 p_mb->ppi_dmv[0][0] = ((i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1144 p_mb->ppi_dmv[0][1] = ((i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1];
1146 /* correct for vertical field shift */
1147 if( p_vpar->picture.i_structure == TOP_FIELD )
1148 p_mb->ppi_dmv[0][1]--;
1150 p_mb->ppi_dmv[0][1]++;
1157 /*****************************************************************************
1158 * DecodeMVMPEG1 : Parse the next MPEG-1 motion vectors
1159 *****************************************************************************/
1160 static __inline__ void DecodeMVMPEG1( vpar_thread_t * p_vpar,
1161 macroblock_t * p_mb, int i_s, int i_structure )
1163 MotionVector( p_vpar, p_mb, 0, i_s,
1164 p_vpar->picture.pb_full_pel_vector[i_s], i_structure );
1167 /*****************************************************************************
1168 * DecodeMVMPEG2 : Parse the next MPEG-2 motion_vectors field
1169 *****************************************************************************/
1170 static __inline__ void DecodeMVMPEG2( vpar_thread_t * p_vpar,
1171 macroblock_t * p_mb, int i_s, int i_structure )
1173 if( p_vpar->mb.i_mv_count == 1 )
1175 if( p_vpar->mb.i_mv_format == MOTION_FIELD && !p_vpar->mb.b_dmv )
1177 p_mb->ppi_field_select[0][i_s] = p_mb->ppi_field_select[1][i_s]
1178 = GetBits( &p_vpar->bit_stream, 1 );
1180 MotionVector( p_vpar, p_mb, 0, i_s, 0, i_structure );
1181 p_vpar->mb.pppi_pmv[1][i_s][0] = p_vpar->mb.pppi_pmv[0][i_s][0];
1182 p_vpar->mb.pppi_pmv[1][i_s][1] = p_vpar->mb.pppi_pmv[0][i_s][1];
1183 p_mb->pppi_motion_vectors[1][i_s][0] = p_vpar->mb.pppi_pmv[0][i_s][0];
1184 p_mb->pppi_motion_vectors[1][i_s][1] = p_vpar->mb.pppi_pmv[0][i_s][1];
1188 p_mb->ppi_field_select[0][i_s] = GetBits( &p_vpar->bit_stream, 1 );
1189 MotionVector( p_vpar, p_mb, 0, i_s, 0, i_structure );
1190 p_mb->ppi_field_select[1][i_s] = GetBits( &p_vpar->bit_stream, 1 );
1191 MotionVector( p_vpar, p_mb, 1, i_s, 0, i_structure );
1197 * Macroblock information structures
1200 /*****************************************************************************
1201 * MacroblockAddressIncrement : Get the macroblock_address_increment field
1202 *****************************************************************************/
1203 static __inline__ int MacroblockAddressIncrement( vpar_thread_t * p_vpar )
1206 /* Index in the lookup table mb_addr_inc */
1207 int i_index = ShowBits( &p_vpar->bit_stream, 11 );
1209 /* Test the presence of the escape character */
1210 while( i_index == 8 )
1212 RemoveBits( &p_vpar->bit_stream, 11 );
1214 i_index = ShowBits( &p_vpar->bit_stream, 11 );
1217 /* Affect the value from the lookup table */
1218 i_addr_inc += p_vpar->pl_mb_addr_inc[i_index].i_value;
1220 /* Dump the good number of bits */
1221 RemoveBits( &p_vpar->bit_stream, p_vpar->pl_mb_addr_inc[i_index].i_length );
1226 /*****************************************************************************
1227 * IMBType : macroblock_type in I pictures
1228 *****************************************************************************/
1229 static __inline__ int IMBType( vpar_thread_t * p_vpar )
1231 /* Take two bits for testing */
1232 int i_type = ShowBits( &p_vpar->bit_stream, 2 );
1234 /* Lookup table for macroblock_type */
1235 static lookup_t pl_mb_Itype[4] = { {MB_ERROR, 0},
1236 {MB_QUANT|MB_INTRA, 2},
1239 /* Dump the good number of bits */
1240 RemoveBits( &p_vpar->bit_stream, pl_mb_Itype[i_type].i_length );
1241 return pl_mb_Itype[i_type].i_value;
1244 /*****************************************************************************
1245 * PMBType : macroblock_type in P pictures
1246 *****************************************************************************/
1247 static __inline__ int PMBType( vpar_thread_t * p_vpar )
1249 /* Testing on 6 bits */
1250 int i_type = ShowBits( &p_vpar->bit_stream, 6 );
1252 /* Dump the good number of bits */
1253 RemoveBits( &p_vpar->bit_stream, p_vpar->ppl_mb_type[0][i_type].i_length );
1254 /* return the value from the lookup table for P type */
1255 return p_vpar->ppl_mb_type[0][i_type].i_value;
1258 /*****************************************************************************
1259 * BMBType : macroblock_type in B pictures
1260 *****************************************************************************/
1261 static __inline__ int BMBType( vpar_thread_t * p_vpar )
1263 /* Testing on 6 bits */
1264 int i_type = ShowBits( &p_vpar->bit_stream, 6 );
1266 /* Dump the good number of bits */
1267 RemoveBits( &p_vpar->bit_stream, p_vpar->ppl_mb_type[1][i_type].i_length );
1269 /* return the value from the lookup table for B type */
1270 return p_vpar->ppl_mb_type[1][i_type].i_value;
1273 /*****************************************************************************
1274 * DMBType : macroblock_type in D pictures
1275 *****************************************************************************/
1276 static __inline__ int DMBType( vpar_thread_t * p_vpar )
1278 return GetBits( &p_vpar->bit_stream, 1 );
1281 /*****************************************************************************
1282 * CodedPattern420 : coded_block_pattern with 4:2:0 chroma
1283 *****************************************************************************/
1284 static __inline__ int CodedPattern420( vpar_thread_t * p_vpar )
1286 /* Take the max 9 bits length vlc code for testing */
1287 int i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1289 /* Trash the good number of bits read in the lookup table */
1290 RemoveBits( &p_vpar->bit_stream, p_vpar->pl_coded_pattern[i_vlc].i_length );
1292 /* return the value from the vlc table */
1293 return p_vpar->pl_coded_pattern[i_vlc].i_value;
1296 /*****************************************************************************
1297 * CodedPattern422 : coded_block_pattern with 4:2:2 chroma
1298 *****************************************************************************/
1299 static __inline__ int CodedPattern422( vpar_thread_t * p_vpar )
1301 int i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1303 RemoveBits( &p_vpar->bit_stream, p_vpar->pl_coded_pattern[i_vlc].i_length );
1305 /* Supplementary 2 bits long code for 4:2:2 format */
1306 return p_vpar->pl_coded_pattern[i_vlc].i_value |
1307 (GetBits( &p_vpar->bit_stream, 2 ) << 6);
1310 /*****************************************************************************
1311 * CodedPattern444 : coded_block_pattern with 4:4:4 chroma
1312 *****************************************************************************/
1313 static __inline__ int CodedPattern444( vpar_thread_t * p_vpar )
1315 int i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1317 RemoveBits( &p_vpar->bit_stream, p_vpar->pl_coded_pattern[i_vlc].i_length );
1319 return p_vpar->pl_coded_pattern[i_vlc].i_value |
1320 (GetBits( &p_vpar->bit_stream, 6 ) << 6);
1323 /*****************************************************************************
1324 * InitMacroblock : Initialize macroblock values
1325 *****************************************************************************/
1326 static __inline__ void InitMacroblock( vpar_thread_t * p_vpar,
1327 macroblock_t * p_mb, int i_coding_type,
1328 int i_chroma_format,
1330 boolean_t b_second_field )
1332 p_mb->i_chroma_nb_blocks = 1 << i_chroma_format;
1333 p_mb->p_picture = p_vpar->picture.p_picture;
1335 if( i_coding_type == B_CODING_TYPE )
1336 p_mb->p_backward = p_vpar->sequence.p_backward;
1338 p_mb->p_backward = NULL;
1339 if( (i_coding_type == P_CODING_TYPE) || (i_coding_type == B_CODING_TYPE) )
1340 p_mb->p_forward = p_vpar->sequence.p_forward;
1342 p_mb->p_forward = NULL;
1344 p_mb->i_l_x = p_vpar->mb.i_l_x;
1345 p_mb->i_c_x = p_vpar->mb.i_c_x;
1346 p_mb->i_motion_l_y = p_vpar->mb.i_l_y;
1347 p_mb->i_motion_c_y = p_vpar->mb.i_c_y;
1348 if( (p_mb->b_motion_field = (i_structure == BOTTOM_FIELD)) )
1350 p_mb->i_motion_l_y--;
1351 p_mb->i_motion_c_y--;
1353 p_mb->i_addb_l_stride = (p_mb->i_l_stride = p_vpar->picture.i_l_stride) - 8;
1354 p_mb->i_addb_c_stride = (p_mb->i_c_stride = p_vpar->picture.i_c_stride) - 8;
1355 p_mb->b_P_second = ( b_second_field && i_coding_type == P_CODING_TYPE );
1358 /*****************************************************************************
1359 * UpdateContext : Update the p_vpar contextual values
1360 *****************************************************************************/
1361 static __inline__ void UpdateContext( vpar_thread_t * p_vpar, int i_structure )
1363 /* Update macroblock real position. */
1364 p_vpar->mb.i_l_x += 16;
1365 p_vpar->mb.i_l_y += (p_vpar->mb.i_l_x / p_vpar->sequence.i_width)
1366 * (2 - (i_structure == FRAME_STRUCTURE)) * 16;
1367 p_vpar->mb.i_l_x %= p_vpar->sequence.i_width;
1369 p_vpar->mb.i_c_x += p_vpar->sequence.i_chroma_mb_width;
1370 p_vpar->mb.i_c_y += (p_vpar->mb.i_c_x / p_vpar->sequence.i_chroma_width)
1371 * (2 - (i_structure == FRAME_STRUCTURE))
1372 * p_vpar->sequence.i_chroma_mb_height;
1373 p_vpar->mb.i_c_x %= p_vpar->sequence.i_chroma_width;
1376 /*****************************************************************************
1377 * SkippedMacroblock : Generate a skipped macroblock with NULL motion vector
1378 *****************************************************************************/
1379 static __inline__ void SkippedMacroblock( vpar_thread_t * p_vpar, int i_mb,
1380 int i_mb_base, int i_coding_type,
1381 int i_chroma_format,
1383 boolean_t b_second_field )
1385 macroblock_t * p_mb;
1387 static f_motion_t pf_motion_skipped[4][4] =
1389 {NULL, NULL, NULL, NULL},
1390 {NULL, vdec_MotionFieldField420, vdec_MotionFieldField420,
1391 vdec_MotionFrameFrame420},
1392 {NULL, vdec_MotionFieldField422, vdec_MotionFieldField422,
1393 vdec_MotionFrameFrame422},
1394 {NULL, vdec_MotionFieldField444, vdec_MotionFieldField444,
1395 vdec_MotionFrameFrame444},
1398 if( (p_mb = vpar_NewMacroblock( &p_vpar->vfifo )) == NULL )
1404 p_vpar->picture.pp_mb[i_mb_base + i_mb] = p_mb;
1407 InitMacroblock( p_vpar, p_mb, i_coding_type, i_chroma_format,
1408 i_structure, b_second_field );
1410 /* Motion type is picture structure. */
1411 p_mb->pf_motion = pf_motion_skipped[i_chroma_format]
1413 p_mb->i_mb_type = MB_MOTION_FORWARD;
1414 p_mb->i_coded_block_pattern = 0;
1415 memset( p_mb->pppi_motion_vectors, 0, 8*sizeof(int) );
1417 /* Set the field we use for motion compensation */
1418 p_mb->ppi_field_select[0][0] = p_mb->ppi_field_select[0][1]
1419 = ( i_structure == BOTTOM_FIELD );
1421 UpdateContext( p_vpar, i_structure );
1424 /* Decode the macroblock NOW ! */
1425 vdec_DecodeMacroblock( p_vpar->pp_vdec[0], p_mb );
1429 /*****************************************************************************
1430 * MacroblockModes : Get the macroblock_modes structure
1431 *****************************************************************************/
1432 static __inline__ void MacroblockModes( vpar_thread_t * p_vpar,
1433 macroblock_t * p_mb,
1434 int i_chroma_format,
1438 static int ppi_mv_count[2][4] = { {0, 1, 2, 1}, {0, 2, 1, 1} };
1439 static int ppi_mv_format[2][4] = { {0, 1, 1, 1}, {0, 1, 2, 1} };
1441 /* Get macroblock_type. */
1442 switch( i_coding_type )
1445 p_mb->i_mb_type = PMBType( p_vpar );
1448 p_mb->i_mb_type = BMBType( p_vpar );
1451 p_mb->i_mb_type = IMBType( p_vpar );
1454 p_mb->i_mb_type = DMBType( p_vpar );
1457 /* SCALABILITY : warning, we don't know if spatial_temporal_weight_code
1458 * has to be dropped, take care if you use scalable streams. */
1459 /* RemoveBits( &p_vpar->bit_stream, 2 ); */
1461 if( (i_coding_type == P_CODING_TYPE || i_coding_type == B_CODING_TYPE)
1462 && (p_mb->i_mb_type & (MB_MOTION_FORWARD | MB_MOTION_BACKWARD)) )
1464 if( !(i_structure == FRAME_STRUCTURE
1465 && p_vpar->picture.b_frame_pred_frame_dct) )
1467 p_vpar->mb.i_motion_type = GetBits( &p_vpar->bit_stream, 2 );
1471 p_vpar->mb.i_motion_type = MOTION_FRAME;
1475 p_vpar->mb.i_mv_count = ppi_mv_count[i_structure == FRAME_STRUCTURE]
1476 [p_vpar->mb.i_motion_type];
1477 p_vpar->mb.i_mv_format = ppi_mv_format[i_structure == FRAME_STRUCTURE]
1478 [p_vpar->mb.i_motion_type];
1479 p_vpar->mb.b_dmv = p_vpar->mb.i_motion_type == MOTION_DMV;
1482 p_vpar->mb.b_dct_type = 0;
1483 if( (i_structure == FRAME_STRUCTURE) &&
1484 (!p_vpar->picture.b_frame_pred_frame_dct) &&
1485 (p_mb->i_mb_type & (MB_PATTERN|MB_INTRA)) )
1487 if( (p_vpar->mb.b_dct_type = GetBits( &p_vpar->bit_stream, 1 )) )
1489 /* The DCT is coded on fields. Jump one line between each
1491 p_mb->i_addb_l_stride <<= 1;
1492 p_mb->i_addb_l_stride += 8;
1493 /* With CHROMA_420, the DCT is necessarily frame-coded. */
1494 if( i_chroma_format != CHROMA_420 )
1496 p_mb->i_addb_c_stride <<= 1;
1497 p_mb->i_addb_c_stride += 8;
1503 /*****************************************************************************
1504 * ParseMacroblock : Parse the next macroblock
1505 *****************************************************************************/
1506 #define PARSEBLOCKS( MPEG1FUNC, MPEG2FUNC ) \
1508 i_mask = 1 << (3 + (1 << i_chroma_format)); \
1511 p_data1 = p_mb->p_picture->p_y \
1512 + p_mb->i_l_x + p_vpar->mb.i_l_y*(p_vpar->sequence.i_width); \
1514 for( i_b = 0 ; i_b < 4 ; i_b++, i_mask >>= 1 ) \
1516 if( p_mb->i_coded_block_pattern & i_mask ) \
1518 memset( p_mb->ppi_blocks[i_b], 0, 64*sizeof(dctelem_t) ); \
1520 MPEG2FUNC( p_vpar, p_mb, i_b, i_chroma_format ); \
1522 MPEG1FUNC( p_vpar, p_mb, i_b, i_chroma_format ); \
1524 /* Calculate block coordinates. */ \
1525 p_mb->p_data[i_b] = p_data1 \
1526 + pi_y[p_vpar->mb.b_dct_type][i_b] \
1527 * p_vpar->sequence.i_width \
1532 if( p_vpar->picture.b_error ) \
1534 /* Mark this block as skipped (better than green blocks), and \
1535 * go to the next slice. */ \
1536 (*pi_mb_address)--; \
1537 vpar_DestroyMacroblock( &p_vpar->vfifo, p_mb ); \
1542 p_data1 = p_mb->p_picture->p_u \
1544 + p_vpar->mb.i_c_y \
1545 * (p_vpar->sequence.i_chroma_width); \
1546 p_data2 = p_mb->p_picture->p_v \
1548 + p_vpar->mb.i_c_y \
1549 * (p_vpar->sequence.i_chroma_width); \
1551 for( i_b = 4; i_b < 4 + (1 << i_chroma_format); \
1552 i_b++, i_mask >>= 1 ) \
1554 yuv_data_t * pp_data[2] = {p_data1, p_data2}; \
1556 if( p_mb->i_coded_block_pattern & i_mask ) \
1558 memset( p_mb->ppi_blocks[i_b], 0, 64*sizeof(dctelem_t) ); \
1560 MPEG2FUNC( p_vpar, p_mb, i_b, i_chroma_format ); \
1562 MPEG1FUNC( p_vpar, p_mb, i_b, i_chroma_format ); \
1564 /* Calculate block coordinates. */ \
1565 p_mb->p_data[i_b] = pp_data[i_b & 1] \
1566 + pi_y[p_vpar->mb.b_dct_type][i_b] \
1567 * p_vpar->sequence.i_chroma_width \
1573 static __inline__ void ParseMacroblock(
1574 vpar_thread_t * p_vpar,
1575 int * pi_mb_address, /* previous address to be
1576 * used for mb_addr_incr */
1577 int i_mb_previous, /* actual previous mb */
1578 int i_mb_base, /* non-zero if field structure */
1579 /* The following parameters are explicit in
1580 * optimized routines : */
1581 boolean_t b_mpeg2, /* you know what ? */
1582 int i_coding_type, /* I, P, B or D */
1583 int i_chroma_format, /* 4:2:0, 4:2:2 or 4:4:4 */
1584 int i_structure, /* T(OP), B(OTTOM) or F(RAME) */
1585 boolean_t b_second_field ) /* second field of a
1588 static f_motion_t pppf_motion[4][2][4] =
1590 { {NULL, NULL, NULL, NULL},
1591 {NULL, NULL, NULL, NULL}
1593 { {NULL, vdec_MotionFieldField420, vdec_MotionField16x8420,
1594 vdec_MotionFieldDMV420},
1595 {NULL, vdec_MotionFrameField420, vdec_MotionFrameFrame420,
1596 vdec_MotionFrameDMV420}
1598 { {NULL, vdec_MotionFieldField422, vdec_MotionField16x8422,
1599 vdec_MotionFieldDMV422},
1600 {NULL, vdec_MotionFrameField422, vdec_MotionFrameFrame422,
1601 vdec_MotionFrameDMV422}
1603 { {NULL, vdec_MotionFieldField444, vdec_MotionField16x8444,
1604 vdec_MotionFieldDMV444},
1605 {NULL, vdec_MotionFrameField444, vdec_MotionFrameFrame444,
1606 vdec_MotionFrameDMV444}
1609 static int pi_x[12] = {0,8,0,8,0,0,0,0,8,8,8,8};
1610 static int pi_y[2][12] = { {0,0,8,8,0,0,8,8,0,0,8,8},
1611 {0,0,1,1,0,0,1,1,0,0,1,1} };
1613 int i_mb, i_b, i_mask;
1614 macroblock_t * p_mb;
1615 yuv_data_t * p_data1;
1616 yuv_data_t * p_data2;
1618 *pi_mb_address += MacroblockAddressIncrement( p_vpar );
1620 if( *pi_mb_address - i_mb_previous - 1 )
1622 /* Skipped macroblock (ISO/IEC 13818-2 7.6.6). */
1624 /* Reset DC predictors (7.2.1). */
1625 p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
1626 = p_vpar->mb.pi_dc_dct_pred[2]
1627 = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
1629 if( i_coding_type == P_CODING_TYPE )
1631 /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
1632 memset( p_vpar->mb.pppi_pmv, 0, 8*sizeof(int) );
1635 for( i_mb = i_mb_previous + 1; i_mb < *pi_mb_address; i_mb++ )
1637 SkippedMacroblock( p_vpar, i_mb, i_mb_base, i_coding_type,
1638 i_chroma_format, i_structure, b_second_field );
1642 /* Get a macroblock structure. */
1643 if( (p_mb = vpar_NewMacroblock( &p_vpar->vfifo )) == NULL )
1649 p_vpar->picture.pp_mb[i_mb_base + *pi_mb_address] = p_mb;
1652 InitMacroblock( p_vpar, p_mb, i_coding_type, i_chroma_format,
1653 i_structure, b_second_field );
1655 /* Parse off macroblock_modes structure. */
1656 MacroblockModes( p_vpar, p_mb, i_chroma_format, i_coding_type,
1659 if( p_mb->i_mb_type & MB_QUANT )
1661 LoadQuantizerScale( p_vpar );
1664 if( (i_coding_type == P_CODING_TYPE || i_coding_type == B_CODING_TYPE)
1665 && (p_mb->i_mb_type & MB_MOTION_FORWARD) )
1668 DecodeMVMPEG2( p_vpar, p_mb, 0, i_structure );
1670 DecodeMVMPEG1( p_vpar, p_mb, 0, i_structure );
1673 if( (i_coding_type == B_CODING_TYPE)
1674 && (p_mb->i_mb_type & MB_MOTION_BACKWARD) )
1677 DecodeMVMPEG2( p_vpar, p_mb, 1, i_structure );
1679 DecodeMVMPEG1( p_vpar, p_mb, 1, i_structure );
1682 if( i_coding_type == P_CODING_TYPE
1683 && !(p_mb->i_mb_type & (MB_MOTION_FORWARD|MB_INTRA)) )
1685 /* Special No-MC macroblock in P pictures (7.6.3.5). */
1686 p_mb->i_mb_type |= MB_MOTION_FORWARD;
1687 memset( p_vpar->mb.pppi_pmv, 0, 8*sizeof(int) );
1688 memset( p_mb->pppi_motion_vectors, 0, 8*sizeof(int) );
1689 p_vpar->mb.i_motion_type = 1 + (i_structure == FRAME_STRUCTURE);
1690 p_mb->ppi_field_select[0][0] = (i_structure == BOTTOM_FIELD);
1693 if( (i_coding_type != I_CODING_TYPE) && !(p_mb->i_mb_type & MB_INTRA) )
1695 /* Reset DC predictors (7.2.1). */
1696 p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
1697 = p_vpar->mb.pi_dc_dct_pred[2]
1698 = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
1700 /* Motion function pointer. */
1701 p_mb->pf_motion = pppf_motion[i_chroma_format]
1702 [i_structure == FRAME_STRUCTURE]
1703 [p_vpar->mb.i_motion_type];
1705 if( p_mb->i_mb_type & MB_PATTERN )
1707 switch( i_chroma_format )
1710 p_mb->i_coded_block_pattern = CodedPattern420( p_vpar );
1713 p_mb->i_coded_block_pattern = CodedPattern422( p_vpar );
1716 p_mb->i_coded_block_pattern = CodedPattern444( p_vpar );
1721 p_mb->i_coded_block_pattern = 0;
1725 * Effectively decode blocks.
1727 PARSEBLOCKS( DecodeMPEG1NonIntra, DecodeMPEG2NonIntra )
1731 if( !p_vpar->picture.b_concealment_mv )
1733 /* Reset MV predictors. */
1734 memset( p_vpar->mb.pppi_pmv, 0, 8*sizeof(int) );
1739 DecodeMVMPEG2( p_vpar, p_mb, 0, i_structure );
1741 DecodeMVMPEG1( p_vpar, p_mb, 0, i_structure );
1742 RemoveBits( &p_vpar->bit_stream, 1 );
1745 if( p_mb->i_mb_type & MB_PATTERN )
1747 switch( i_chroma_format )
1750 p_mb->i_coded_block_pattern = CodedPattern420( p_vpar );
1753 p_mb->i_coded_block_pattern = CodedPattern422( p_vpar );
1756 p_mb->i_coded_block_pattern = CodedPattern444( p_vpar );
1761 p_mb->i_coded_block_pattern =
1762 (1 << (4 + (1 << i_chroma_format))) - 1;
1766 * Effectively decode blocks.
1768 PARSEBLOCKS( DecodeMPEG1Intra, DecodeMPEG2Intra )
1771 if( !p_vpar->picture.b_error )
1773 UpdateContext( p_vpar, i_structure );
1775 /* Decode the macroblock NOW ! */
1776 vdec_DecodeMacroblock( p_vpar->pp_vdec[0], p_mb );
1781 /* Mark this block as skipped (better than green blocks), and go
1782 * to the next slice. */
1784 vpar_DestroyMacroblock( &p_vpar->vfifo, p_mb );
1789 * Picture data parsing management
1792 /*****************************************************************************
1793 * SliceHeader : Parse the next slice structure
1794 *****************************************************************************/
1795 static __inline__ void SliceHeader( vpar_thread_t * p_vpar,
1796 int * pi_mb_address, int i_mb_base,
1797 u32 i_vert_code, boolean_t b_high,
1798 boolean_t b_dp_scalable,
1799 boolean_t b_mpeg2, int i_coding_type,
1800 int i_chroma_format, int i_structure,
1801 boolean_t b_second_field )
1803 int i_mb_address_save = *pi_mb_address;
1805 p_vpar->picture.b_error = 0;
1809 /* Picture with more than 2800 lines. */
1810 i_vert_code += GetBits( &p_vpar->bit_stream, 3 ) << 7;
1814 /* DATA_PARTITIONING scalability. */
1815 RemoveBits( &p_vpar->bit_stream, 7 ); /* priority_breakpoint */
1818 LoadQuantizerScale( p_vpar );
1820 if( GetBits( &p_vpar->bit_stream, 1 ) )
1822 /* intra_slice, slice_id */
1823 RemoveBits( &p_vpar->bit_stream, 8 );
1824 /* extra_information_slice */
1825 while( GetBits( &p_vpar->bit_stream, 1 ) )
1827 RemoveBits( &p_vpar->bit_stream, 8 );
1830 *pi_mb_address = (i_vert_code - 1)*p_vpar->sequence.i_mb_width;
1832 /* Reset DC coefficients predictors (ISO/IEC 13818-2 7.2.1). */
1833 p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
1834 = p_vpar->mb.pi_dc_dct_pred[2]
1835 = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
1837 /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
1838 memset( p_vpar->mb.pppi_pmv, 0, 8*sizeof(int) );
1842 ParseMacroblock( p_vpar, pi_mb_address, i_mb_address_save,
1843 i_mb_base, b_mpeg2, i_coding_type,
1844 i_chroma_format, i_structure,
1846 i_mb_address_save = *pi_mb_address;
1848 while( ShowBits( &p_vpar->bit_stream, 23 ) && !p_vpar->picture.b_error
1849 && !p_vpar->b_die );
1850 NextStartCode( p_vpar );
1853 /*****************************************************************************
1854 * PictureData : Parse off all macroblocks (ISO/IEC 13818-2 6.2.3.7)
1855 *****************************************************************************/
1856 static __inline__ void PictureData( vpar_thread_t * p_vpar, int i_mb_base,
1857 boolean_t b_high, boolean_t b_dp_scalable,
1858 boolean_t b_mpeg2, int i_coding_type,
1859 int i_chroma_format, int i_structure,
1860 boolean_t b_second_field )
1862 int i_mb_address = 0;
1865 NextStartCode( p_vpar );
1866 while( (i_coding_type != I_CODING_TYPE || i_coding_type != D_CODING_TYPE
1867 || !p_vpar->picture.b_error)
1868 && i_mb_address+i_mb_base < p_vpar->sequence.i_mb_size
1871 if( ((i_dummy = ShowBits( &p_vpar->bit_stream, 32 ))
1872 < SLICE_START_CODE_MIN) ||
1873 (i_dummy > SLICE_START_CODE_MAX) )
1875 intf_DbgMsg("vpar debug: premature end of picture\n");
1876 p_vpar->picture.b_error = 1;
1879 RemoveBits32( &p_vpar->bit_stream );
1881 /* Decode slice data. */
1882 SliceHeader( p_vpar, &i_mb_address, i_mb_base, i_dummy & 255,
1883 b_high, b_dp_scalable, b_mpeg2, i_coding_type,
1884 i_chroma_format, i_structure, b_second_field );
1887 /* Try to recover from error. If we missed less than half the
1888 * number of macroblocks of the picture, mark the missed ones
1890 if( (i_coding_type == P_CODING_TYPE || i_coding_type == B_CODING_TYPE)
1891 && p_vpar->picture.b_error &&
1892 ( (i_mb_address-i_mb_base) > (p_vpar->sequence.i_mb_size >> 1)
1893 || (i_structure != FRAME_STRUCTURE
1894 && (i_mb_address-i_mb_base) > (p_vpar->sequence.i_mb_size >> 2) ) ) )
1898 p_vpar->picture.b_error = 0;
1899 for( i_mb = i_mb_address + 1;
1900 i_mb < (p_vpar->sequence.i_mb_size
1901 << (i_structure != FRAME_STRUCTURE));
1904 SkippedMacroblock( p_vpar, i_mb, i_mb_base, i_coding_type,
1905 i_chroma_format, i_structure, b_second_field );
1910 /*****************************************************************************
1911 * vpar_PictureDataVWXYZ : Parse the next macroblock ; specific functions
1912 *****************************************************************************
1915 * X = chroma format ?
1917 * Z = second field ?
1918 *****************************************************************************/
1919 void vpar_PictureDataGENERIC( vpar_thread_t * p_vpar, int i_mb_base )
1921 PictureData( p_vpar, i_mb_base, (p_vpar->sequence.i_height > 2800),
1922 (p_vpar->sequence.i_scalable_mode == SC_DP),
1923 p_vpar->sequence.b_mpeg2, p_vpar->picture.i_coding_type,
1924 p_vpar->sequence.i_chroma_format,
1925 p_vpar->picture.i_structure,
1926 (p_vpar->picture.i_structure !=
1927 p_vpar->picture.i_current_structure) );
1930 #if (VPAR_OPTIM_LEVEL > 0)
1931 /* Optimizations for frame pictures */
1932 void vpar_PictureData2I420F0( vpar_thread_t * p_vpar, int i_mb_base )
1934 PictureData( p_vpar, 0, 0, 0, 1, I_CODING_TYPE, CHROMA_420,
1935 FRAME_STRUCTURE, 0 );
1938 void vpar_PictureData2P420F0( vpar_thread_t * p_vpar, int i_mb_base )
1940 PictureData( p_vpar, 0, 0, 0, 1, P_CODING_TYPE, CHROMA_420,
1941 FRAME_STRUCTURE, 0 );
1944 void vpar_PictureData2B420F0( vpar_thread_t * p_vpar, int i_mb_base )
1946 PictureData( p_vpar, 0, 0, 0, 1, B_CODING_TYPE, CHROMA_420,
1947 FRAME_STRUCTURE, 0 );
1951 #if (VPAR_OPTIM_LEVEL > 1)
1952 /* Optimizations for field pictures */
1953 void vpar_PictureData2I420TZ( vpar_thread_t * p_vpar, int i_mb_base )
1955 PictureData( p_vpar, i_mb_base, 0, 0, 1, I_CODING_TYPE, CHROMA_420,
1956 TOP_FIELD, (p_vpar->picture.i_structure !=
1957 p_vpar->picture.i_current_structure) );
1960 void vpar_PictureData2P420TZ( vpar_thread_t * p_vpar, int i_mb_base )
1962 PictureData( p_vpar, i_mb_base, 0, 0, 1, P_CODING_TYPE, CHROMA_420,
1963 TOP_FIELD, (p_vpar->picture.i_structure !=
1964 p_vpar->picture.i_current_structure) );
1967 void vpar_PictureData2B420TZ( vpar_thread_t * p_vpar, int i_mb_base )
1969 PictureData( p_vpar, i_mb_base, 0, 0, 1, B_CODING_TYPE, CHROMA_420,
1970 TOP_FIELD, (p_vpar->picture.i_structure !=
1971 p_vpar->picture.i_current_structure) );
1974 void vpar_PictureData2I420BZ( vpar_thread_t * p_vpar, int i_mb_base )
1976 PictureData( p_vpar, i_mb_base, 0, 0, 1, I_CODING_TYPE, CHROMA_420,
1977 BOTTOM_FIELD, (p_vpar->picture.i_structure !=
1978 p_vpar->picture.i_current_structure) );
1981 void vpar_PictureData2P420BZ( vpar_thread_t * p_vpar, int i_mb_base )
1983 PictureData( p_vpar, i_mb_base, 0, 0, 1, P_CODING_TYPE, CHROMA_420,
1984 BOTTOM_FIELD, (p_vpar->picture.i_structure !=
1985 p_vpar->picture.i_current_structure) );
1988 void vpar_PictureData2B420BZ( vpar_thread_t * p_vpar, int i_mb_base )
1990 PictureData( p_vpar, i_mb_base, 0, 0, 1, B_CODING_TYPE, CHROMA_420,
1991 BOTTOM_FIELD, (p_vpar->picture.i_structure !=
1992 p_vpar->picture.i_current_structure) );