1 /*****************************************************************************
2 * vpar_blocks.c : blocks parsing
4 *****************************************************************************/
6 /*****************************************************************************
8 *****************************************************************************/
19 #include "vlc_thread.h"
22 #include "debug.h" /* XXX?? 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"
46 static int i_count = 0;
54 * Welcome to vpar_blocks.c ! Here's where the heavy processor-critical parsing
55 * task is done. This file is divided in several parts :
56 * - Initialization of the lookup tables
57 * - Decoding of coded blocks
58 * - Decoding of motion vectors
59 * - Decoding of the other macroblock structures
60 * - Picture data parsing management (slices and error handling)
61 * It's a pretty long file. Good luck and have a nice day.
66 * Initialization tables
69 /* Table for coded_block_pattern resolution */
70 static lookup_t pl_coded_pattern[512] =
71 { {MB_ERROR, 0}, {0, 9}, {39, 9}, {27, 9}, {59, 9}, {55, 9}, {47, 9}, {31, 9},
72 {58, 8}, {58, 8}, {54, 8}, {54, 8}, {46, 8}, {46, 8}, {30, 8}, {30, 8},
73 {57, 8}, {57, 8}, {53, 8}, {53, 8}, {45, 8}, {45, 8}, {29, 8}, {29, 8},
74 {38, 8}, {38, 8}, {26, 8}, {26, 8}, {37, 8}, {37, 8}, {25, 8}, {25, 8},
75 {43, 8}, {43, 8}, {23, 8}, {23, 8}, {51, 8}, {51, 8}, {15, 8}, {15, 8},
76 {42, 8}, {42, 8}, {22, 8}, {22, 8}, {50, 8}, {50, 8}, {14, 8}, {14, 8},
77 {41, 8}, {41, 8}, {21, 8}, {21, 8}, {49, 8}, {49, 8}, {13, 8}, {13, 8},
78 {35, 8}, {35, 8}, {19, 8}, {19, 8}, {11, 8}, {11, 8}, {7, 8}, {7, 8},
79 {34, 7}, {34, 7}, {34, 7}, {34, 7}, {18, 7}, {18, 7}, {18, 7}, {18, 7},
80 {10, 7}, {10, 7}, {10, 7}, {10, 7}, {6, 7}, {6, 7}, {6, 7}, {6, 7},
81 {33, 7}, {33, 7}, {33, 7}, {33, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7},
82 {9, 7}, {9, 7}, {9, 7}, {9, 7}, {5, 7}, {5, 7}, {5, 7}, {5, 7},
83 {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6},
84 {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6},
85 {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6},
86 {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6},
87 {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
88 {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
89 {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5},
90 {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5},
91 {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5},
92 {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5},
93 {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5},
94 {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5},
95 {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5},
96 {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5},
97 {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5},
98 {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5},
99 {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5},
100 {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5},
101 {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5},
102 {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5},
103 {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5},
104 {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5},
105 {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5},
106 {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5},
107 {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5},
108 {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5},
109 {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5},
110 {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5},
111 {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
112 {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
113 {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
114 {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
115 {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
116 {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
117 {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
118 {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
119 {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
120 {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
121 {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
122 {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
123 {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
124 {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
125 {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
126 {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
127 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
128 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
129 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
130 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
131 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
132 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
133 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
134 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3} };
136 /* Tables for dc DCT coefficients
137 * Tables are cut in two parts to reduce memory occupation
140 /* Table B-12, dct_dc_size_luminance, codes 00XXX ... 11110 */
141 static lookup_t pl_dct_dc_lum_init_table_1[32] =
142 { {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
143 {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
144 {0, 3}, {0, 3}, {0, 3}, {0, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3},
145 {4, 3}, {4, 3}, {4, 3}, {4, 3}, {5, 4}, {5, 4}, {6, 5}, {MB_ERROR, 0}
148 /* Table B-12, dct_dc_size_luminance, codes 111110xxx ... 111111111 */
149 static lookup_t pl_dct_dc_lum_init_table_2[32] =
150 { {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6},
151 {8, 7}, {8, 7}, {8, 7}, {8, 7}, {9, 8}, {9, 8}, {10,9}, {11,9},
152 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
153 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
154 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
155 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}
158 /* Table B-13, dct_dc_size_chrominance, codes 00xxx ... 11110 */
159 static lookup_t pl_dct_dc_chrom_init_table_1[32] =
160 { {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2},
161 {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
162 {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
163 {3, 3}, {3, 3}, {3, 3}, {3, 3}, {4, 4}, {4, 4}, {5, 5}, {MB_ERROR, 0}
166 /* Table B-13, dct_dc_size_chrominance, codes 111110xxxx ... 1111111111 */
167 static lookup_t pl_dct_dc_chrom_init_table_2[32] =
168 { {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
169 {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
170 {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7},
171 {8, 8}, {8, 8}, {8, 8}, {8, 8}, {9, 9}, {9, 9}, {10,10}, {11,10}
175 /* Tables for ac DCT coefficients. There are cut in many parts to save space */
176 /* Table B-14, DCT coefficients table zero,
177 * codes 0100 ... 1xxx (used for first (DC) coefficient)
179 static dct_lookup_t pl_DCT_tab_dc[12] =
181 {0,2,4}, {2,1,4}, {1,1,3}, {1,1,3},
182 {0,1,1}, {0,1,1}, {0,1,1}, {0,1,1},
183 {0,1,1}, {0,1,1}, {0,1,1}, {0,1,1}
186 /* Table B-14, DCT coefficients table zero,
187 * codes 0100 ... 1xxx (used for all other coefficients)
189 static dct_lookup_t pl_DCT_tab_ac[12] =
191 {0,2,4}, {2,1,4}, {1,1,3}, {1,1,3},
192 {DCT_EOB,0,2}, {DCT_EOB,0,2}, {DCT_EOB,0,2}, {DCT_EOB,0,2}, /* EOB */
193 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2}
196 /* Table B-14, DCT coefficients table zero,
197 * codes 000001xx ... 00111xxx
199 static dct_lookup_t pl_DCT_tab0[60] =
201 {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6},
203 {2,2,7}, {2,2,7}, {9,1,7}, {9,1,7},
204 {0,4,7}, {0,4,7}, {8,1,7}, {8,1,7},
205 {7,1,6}, {7,1,6}, {7,1,6}, {7,1,6},
206 {6,1,6}, {6,1,6}, {6,1,6}, {6,1,6},
207 {1,2,6}, {1,2,6}, {1,2,6}, {1,2,6},
208 {5,1,6}, {5,1,6}, {5,1,6}, {5,1,6},
209 {13,1,8}, {0,6,8}, {12,1,8}, {11,1,8},
210 {3,2,8}, {1,3,8}, {0,5,8}, {10,1,8},
211 {0,3,5}, {0,3,5}, {0,3,5}, {0,3,5},
212 {0,3,5}, {0,3,5}, {0,3,5}, {0,3,5},
213 {4,1,5}, {4,1,5}, {4,1,5}, {4,1,5},
214 {4,1,5}, {4,1,5}, {4,1,5}, {4,1,5},
215 {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
216 {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5}
219 /* Table B-15, DCT coefficients table one,
220 * codes 000001xx ... 11111111
222 static dct_lookup_t pl_DCT_tab0a[252] =
224 {65,0,6}, {65,0,6}, {65,0,6}, {65,0,6}, /* Escape */
225 {7,1,7}, {7,1,7}, {8,1,7}, {8,1,7},
226 {6,1,7}, {6,1,7}, {2,2,7}, {2,2,7},
227 {0,7,6}, {0,7,6}, {0,7,6}, {0,7,6},
228 {0,6,6}, {0,6,6}, {0,6,6}, {0,6,6},
229 {4,1,6}, {4,1,6}, {4,1,6}, {4,1,6},
230 {5,1,6}, {5,1,6}, {5,1,6}, {5,1,6},
231 {1,5,8}, {11,1,8}, {0,11,8}, {0,10,8},
232 {13,1,8}, {12,1,8}, {3,2,8}, {1,4,8},
233 {2,1,5}, {2,1,5}, {2,1,5}, {2,1,5},
234 {2,1,5}, {2,1,5}, {2,1,5}, {2,1,5},
235 {1,2,5}, {1,2,5}, {1,2,5}, {1,2,5},
236 {1,2,5}, {1,2,5}, {1,2,5}, {1,2,5},
237 {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
238 {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
239 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
240 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
241 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
242 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
243 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
244 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
245 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
246 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
247 {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4}, /* EOB */
248 {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
249 {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
250 {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
251 {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
252 {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
253 {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
254 {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
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,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
265 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
266 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
267 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
268 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
269 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
270 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
271 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
272 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
273 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
274 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
275 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
276 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
277 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
278 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
279 {0,4,5}, {0,4,5}, {0,4,5}, {0,4,5},
280 {0,4,5}, {0,4,5}, {0,4,5}, {0,4,5},
281 {0,5,5}, {0,5,5}, {0,5,5}, {0,5,5},
282 {0,5,5}, {0,5,5}, {0,5,5}, {0,5,5},
283 {9,1,7}, {9,1,7}, {1,3,7}, {1,3,7},
284 {10,1,7}, {10,1,7}, {0,8,7}, {0,8,7},
285 {0,9,7}, {0,9,7}, {0,12,8}, {0,13,8},
286 {2,3,8}, {4,2,8}, {0,14,8}, {0,15,8}
289 /* Table B-14, DCT coefficients table zero,
290 * codes 0000001000 ... 0000001111
292 static dct_lookup_t pl_DCT_tab1[8] =
294 {16,1,10}, {5,2,10}, {0,7,10}, {2,3,10},
295 {1,4,10}, {15,1,10}, {14,1,10}, {4,2,10}
298 /* Table B-15, DCT coefficients table one,
299 * codes 000000100x ... 000000111x
301 static dct_lookup_t pl_DCT_tab1a[8] =
303 {5,2,9}, {5,2,9}, {14,1,9}, {14,1,9},
304 {2,4,10}, {16,1,10}, {15,1,9}, {15,1,9}
307 /* Table B-14/15, DCT coefficients table zero / one,
308 * codes 000000010000 ... 000000011111
310 static dct_lookup_t pl_DCT_tab2[16] =
312 {0,11,12}, {8,2,12}, {4,3,12}, {0,10,12},
313 {2,4,12}, {7,2,12}, {21,1,12}, {20,1,12},
314 {0,9,12}, {19,1,12}, {18,1,12}, {1,5,12},
315 {3,3,12}, {0,8,12}, {6,2,12}, {17,1,12}
318 /* Table B-14/15, DCT coefficients table zero / one,
319 * codes 0000000010000 ... 0000000011111
321 static dct_lookup_t pl_DCT_tab3[16] =
323 {10,2,13}, {9,2,13}, {5,3,13}, {3,4,13},
324 {2,5,13}, {1,7,13}, {1,6,13}, {0,15,13},
325 {0,14,13}, {0,13,13}, {0,12,13}, {26,1,13},
326 {25,1,13}, {24,1,13}, {23,1,13}, {22,1,13}
329 /* Table B-14/15, DCT coefficients table zero / one,
330 * codes 00000000010000 ... 00000000011111
332 static dct_lookup_t pl_DCT_tab4[16] =
334 {0,31,14}, {0,30,14}, {0,29,14}, {0,28,14},
335 {0,27,14}, {0,26,14}, {0,25,14}, {0,24,14},
336 {0,23,14}, {0,22,14}, {0,21,14}, {0,20,14},
337 {0,19,14}, {0,18,14}, {0,17,14}, {0,16,14}
340 /* Table B-14/15, DCT coefficients table zero / one,
341 * codes 000000000010000 ... 000000000011111
343 static dct_lookup_t pl_DCT_tab5[16] =
345 {0,40,15}, {0,39,15}, {0,38,15}, {0,37,15},
346 {0,36,15}, {0,35,15}, {0,34,15}, {0,33,15},
347 {0,32,15}, {1,14,15}, {1,13,15}, {1,12,15},
348 {1,11,15}, {1,10,15}, {1,9,15}, {1,8,15}
351 /* Table B-14/15, DCT coefficients table zero / one,
352 * codes 0000000000010000 ... 0000000000011111
354 static dct_lookup_t pl_DCT_tab6[16] =
356 {1,18,16}, {1,17,16}, {1,16,16}, {1,15,16},
357 {6,3,16}, {16,2,16}, {15,2,16}, {14,2,16},
358 {13,2,16}, {12,2,16}, {11,2,16}, {31,1,16},
359 {30,1,16}, {29,1,16}, {28,1,16}, {27,1,16}
364 * Initialization of lookup tables
367 /*****************************************************************************
368 * vpar_InitCrop : Initialize the crop table for saturation
369 * (ISO/IEC 13818-2 section 7.4.3)
370 *****************************************************************************/
371 #if defined(MPEG2_COMPLIANT) && !defined(VDEC_DFT)
372 void vpar_InitCrop( vpar_thread_t * p_vpar )
376 p_vpar->pi_crop = p_vpar->pi_crop_buf + 4096;
378 for( i_dummy = -4096; i_dummy < -2048; i_dummy++ )
380 p_vpar->pi_crop[i_dummy] = -2048;
382 for( ; i_dummy < 2047; i_dummy++ )
384 p_vpar->pi_crop[i_dummy] = i_dummy;
386 for( ; i_dummy < 4095; i_dummy++ )
388 p_vpar->pi_crop[i_dummy] = 2047;
393 /*****************************************************************************
394 * vpar_InitMbAddrInc : Initialize the lookup table for mb_addr_inc
395 *****************************************************************************/
397 /* Function for filling up the lookup table for mb_addr_inc */
398 static void __inline__ FillMbAddrIncTable( vpar_thread_t * p_vpar,
399 int i_start, int i_end, int i_step,
400 int * pi_value, int i_length )
403 for( i_pos = i_start ; i_pos < i_end ; i_pos += i_step )
405 for( i_offset = 0 ; i_offset < i_step ; i_offset ++ )
407 p_vpar->pl_mb_addr_inc[i_pos + i_offset].i_value = * pi_value;
408 p_vpar->pl_mb_addr_inc[i_pos + i_offset].i_length = i_length;
414 /* Function that initialize the table using the last one */
415 void vpar_InitMbAddrInc( vpar_thread_t * p_vpar )
420 for( i_dummy = 0 ; i_dummy < 8 ; i_dummy++ )
422 p_vpar->pl_mb_addr_inc[i_dummy].i_value = MB_ERROR;
423 p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
426 p_vpar->pl_mb_addr_inc[8].i_value = MB_ADDRINC_ESCAPE;
427 p_vpar->pl_mb_addr_inc[8].i_length = 11;
429 for( i_dummy = 9 ; i_dummy < 15 ; i_dummy ++ )
431 p_vpar->pl_mb_addr_inc[i_dummy].i_value = MB_ERROR;
432 p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
435 p_vpar->pl_mb_addr_inc[15].i_value = MB_ADDRINC_STUFFING;
436 p_vpar->pl_mb_addr_inc[15].i_length = 11;
438 for( i_dummy = 16; i_dummy < 24; i_dummy++ )
440 p_vpar->pl_mb_addr_inc[i_dummy].i_value = MB_ERROR;
441 p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
446 FillMbAddrIncTable( p_vpar, 24, 36, 1, &i_value, 11 );
447 FillMbAddrIncTable( p_vpar, 36, 48, 2, &i_value, 10 );
448 FillMbAddrIncTable( p_vpar, 48, 96, 8, &i_value, 8 );
449 FillMbAddrIncTable( p_vpar, 96, 128, 16, &i_value, 7 );
450 FillMbAddrIncTable( p_vpar, 128, 256, 64, &i_value, 5 );
451 FillMbAddrIncTable( p_vpar, 256, 512, 128, &i_value, 4 );
452 FillMbAddrIncTable( p_vpar, 512, 1024, 256, &i_value, 3 );
453 FillMbAddrIncTable( p_vpar, 1024, 2048, 1024, &i_value, 1 );
456 /*****************************************************************************
457 * vpar_Init*MBType : Initialize lookup table for the Macroblock type
458 *****************************************************************************/
460 /* Fonction for filling up the tables */
461 static void __inline__ FillMBType( vpar_thread_t * p_vpar,
470 for( i_dummy = i_start ; i_dummy < i_end ; i_dummy++ )
472 p_vpar->ppl_mb_type[i_mb_type][i_dummy].i_value = i_value;
473 p_vpar->ppl_mb_type[i_mb_type][i_dummy].i_length = i_length;
477 /* Fonction that fills the table for P MB_Type */
478 void vpar_InitPMBType( vpar_thread_t * p_vpar )
480 FillMBType( p_vpar, 0, 32, 64, MB_MOTION_FORWARD|MB_PATTERN, 1 );
481 FillMBType( p_vpar, 0, 16, 32, MB_PATTERN, 2 );
482 FillMBType( p_vpar, 0, 8, 16, MB_MOTION_FORWARD, 3 );
483 FillMBType( p_vpar, 0, 6, 8, MB_INTRA, 5 );
484 FillMBType( p_vpar, 0, 4, 6, MB_QUANT|MB_MOTION_FORWARD|MB_PATTERN, 5 );
485 FillMBType( p_vpar, 0, 2, 4, MB_QUANT|MB_PATTERN, 5 );
486 p_vpar->ppl_mb_type[0][1].i_value = MB_QUANT|MB_INTRA;
487 p_vpar->ppl_mb_type[0][1].i_length = 6;
488 p_vpar->ppl_mb_type[0][0].i_value = MB_ERROR;
489 p_vpar->ppl_mb_type[0][0].i_length = 0;
492 /* Fonction that fills the table for B MB_Type */
493 void vpar_InitBMBType( vpar_thread_t * p_vpar )
495 FillMBType( p_vpar, 1, 48, 64, MB_MOTION_FORWARD
496 |MB_MOTION_BACKWARD|MB_PATTERN, 2 );
497 FillMBType( p_vpar, 1, 32, 48, MB_MOTION_FORWARD|MB_MOTION_BACKWARD, 2 );
498 FillMBType( p_vpar, 1, 24, 32, MB_MOTION_BACKWARD|MB_PATTERN, 3 );
499 FillMBType( p_vpar, 1, 16, 24, MB_MOTION_BACKWARD, 3 );
500 FillMBType( p_vpar, 1, 12, 16, MB_MOTION_FORWARD|MB_PATTERN, 4 );
501 FillMBType( p_vpar, 1, 8, 12, MB_MOTION_FORWARD, 4 );
502 FillMBType( p_vpar, 1, 6, 8, MB_INTRA, 5 );
503 FillMBType( p_vpar, 1, 4, 6, MB_QUANT|MB_MOTION_FORWARD
504 |MB_MOTION_BACKWARD|MB_PATTERN, 5 );
505 p_vpar->ppl_mb_type[1][3].i_value = MB_QUANT|MB_MOTION_FORWARD|MB_PATTERN;
506 p_vpar->ppl_mb_type[1][3].i_length = 6;
507 p_vpar->ppl_mb_type[1][2].i_value = MB_QUANT|MB_MOTION_BACKWARD|MB_PATTERN;
508 p_vpar->ppl_mb_type[1][2].i_length = 6;
509 p_vpar->ppl_mb_type[1][1].i_value = MB_QUANT|MB_INTRA;
510 p_vpar->ppl_mb_type[1][1].i_length = 6;
511 p_vpar->ppl_mb_type[1][0].i_value =MB_ERROR;
512 p_vpar->ppl_mb_type[1][0].i_length = 0;
516 /*****************************************************************************
517 * vpar_InitDCTTables : Initialize tables giving the length of the dct
518 * coefficient from the vlc code
519 *****************************************************************************/
521 /* First fonction for filling the table */
522 static void __inline__ FillDCTTable( dct_lookup_t * p_tab_dest, dct_lookup_t * p_tab_src,
523 int i_step, int i_nb_elem, int i_offset )
525 int i_dummy, i_dummy2;
527 for( i_dummy=0 ; i_dummy < i_nb_elem ; i_dummy++ )
529 for( i_dummy2=0 ; i_dummy2 < i_step ; i_dummy2++ )
531 p_tab_dest[(i_dummy+i_offset)*i_step+i_dummy2] = p_tab_src[i_dummy];
537 /* Fonction that actually fills the table or create the pointers */
538 void vpar_InitDCTTables( vpar_thread_t * p_vpar )
540 /* Tables are cut in two parts to reduce memory occupation */
541 p_vpar->pppl_dct_dc_size[0][0] = pl_dct_dc_lum_init_table_1;
542 p_vpar->pppl_dct_dc_size[0][1] = pl_dct_dc_lum_init_table_2;
543 p_vpar->pppl_dct_dc_size[1][0] = pl_dct_dc_chrom_init_table_1;
544 p_vpar->pppl_dct_dc_size[1][1] = pl_dct_dc_chrom_init_table_2;
546 /* XXX?? MB_ERROR is replaced by 0 because if we use -1 we
547 * can block in DecodeMPEG2Intra and others */
548 memset( p_vpar->ppl_dct_coef[0], 0, 16 );
549 memset( p_vpar->ppl_dct_coef[1], 0, 16 );
551 /* For table B14 & B15, we have a pointer to tables */
552 /* We fill the table thanks to the fonction defined above */
553 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab0, 256, 60, 4 );
554 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab1, 64, 8, 8 );
555 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab2, 16, 16, 16 );
556 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab3, 8, 16, 16 );
557 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab4, 4, 16, 16 );
558 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab5, 2, 16, 16 );
559 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab6, 1, 16, 16 );
561 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab0a, 256, 60, 4 );
562 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab1a, 64, 8, 8 );
563 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab2, 16, 16, 16 );
564 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab3, 8, 16, 16 );
565 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab4, 4, 16, 16 );
566 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab5, 2, 16, 16 );
567 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab6, 1, 16, 16 );
575 /*****************************************************************************
576 * DecodeMPEG1NonIntra : decode MPEG-1 non-intra blocks
577 *****************************************************************************/
578 static __inline__ void DecodeMPEG1NonIntra( vpar_thread_t * p_vpar,
579 macroblock_t * p_mb, int i_b,
580 int i_chroma_format )
583 if( p_vpar->picture.i_coding_type == D_CODING_TYPE )
585 /* Remove end_of_macroblock (always 1, prevents startcode emulation)
586 * ISO/IEC 11172-2 section 2.4.2.7 and 2.4.3.6 */
587 RemoveBits( &p_vpar->bit_stream, 1 );
591 /*****************************************************************************
592 * DecodeMPEG1Intra : decode MPEG-1 intra blocks
593 *****************************************************************************/
594 static __inline__ void DecodeMPEG1Intra( vpar_thread_t * p_vpar,
595 macroblock_t * p_mb, int i_b ,
596 int i_chroma_format )
599 if( p_vpar->picture.i_coding_type == D_CODING_TYPE )
601 /* Remove end_of_macroblock (always 1, prevents startcode emulation)
602 * ISO/IEC 11172-2 section 2.4.2.7 and 2.4.3.6 */
603 RemoveBits( &p_vpar->bit_stream, 1 );
607 /*****************************************************************************
608 * DecodeMPEG2NonIntra : decode MPEG-2 non-intra blocks
609 *****************************************************************************/
610 static __inline__ void DecodeMPEG2NonIntra( vpar_thread_t * p_vpar,
611 macroblock_t * p_mb, int i_b,
612 int i_chroma_format )
627 /* Lookup Table for the chromatic component */
628 static int pi_cc_index[12] = { 0, 0, 0, 0, 1, 2, 1, 2, 1, 2 };
630 i_cc = pi_cc_index[i_b];
632 /* Determine whether it is luminance or not (chrominance) */
633 i_type = ( i_cc + 1 ) >> 1;
635 /* Give a pointer to the quantization matrices for intra blocks */
636 if( (i_chroma_format == CHROMA_420) || (!i_type) )
638 pi_quant = p_vpar->sequence.nonintra_quant.pi_matrix;
642 pi_quant = p_vpar->sequence.chroma_nonintra_quant.pi_matrix;
645 /* Decoding of the AC coefficients */
649 for( i_parse = 0; !p_vpar->b_die; i_parse++ )
651 i_code = ShowBits( &p_vpar->bit_stream, 16 );
652 if( i_code >= 16384 )
656 i_run = pl_DCT_tab_dc[(i_code>>12)-4].i_run;
657 i_level = pl_DCT_tab_dc[(i_code>>12)-4].i_level;
658 i_length = pl_DCT_tab_dc[(i_code>>12)-4].i_length;
662 i_run = pl_DCT_tab_ac[(i_code>>12)-4].i_run;
663 i_level = pl_DCT_tab_ac[(i_code>>12)-4].i_level;
664 i_length = pl_DCT_tab_ac[(i_code>>12)-4].i_length;
667 else if( i_code >= 1024 )
669 i_run = pl_DCT_tab0[(i_code>>8)-4].i_run;
670 i_length = pl_DCT_tab0[(i_code>>8)-4].i_length;
671 i_level = pl_DCT_tab0[(i_code>>8)-4].i_level;
675 i_run = p_vpar->ppl_dct_coef[0][i_code].i_run;
676 i_length = p_vpar->ppl_dct_coef[0][i_code].i_length;
677 i_level = p_vpar->ppl_dct_coef[0][i_code].i_level;
681 RemoveBits( &p_vpar->bit_stream, i_length );
686 i_run = GetBits( &p_vpar->bit_stream, 6 );
687 i_level = GetBits( &p_vpar->bit_stream, 12 );
688 i_level = (b_sign = ( i_level > 2047 )) ? 4096 - i_level
693 /* The MMX IDCT has a precision problem with non-intra
695 p_mb->ppi_blocks[i_b][0] += 4;
699 p_mb->pf_idct[i_b] = vdec_SparseIDCT;
700 p_mb->pi_sparse_pos[i_b] = i_coef;
704 p_mb->pf_idct[i_b] = vdec_IDCT;
710 b_sign = GetBits( &p_vpar->bit_stream, 1 );
721 i_pos = pi_scan[p_vpar->picture.b_alternate_scan][i_parse];
722 i_level = ( ((i_level << 1) + 1) * p_vpar->mb.i_quantizer_scale
723 * pi_quant[i_pos] ) >> 5;
724 p_mb->ppi_blocks[i_b][i_pos] = b_sign ? -i_level : i_level;
727 intf_ErrMsg("vpar error: DCT coeff (non-intra) is out of bounds\n");
728 p_vpar->picture.b_error = 1;
731 /*****************************************************************************
732 * DecodeMPEG2Intra : decode MPEG-2 intra blocks
733 *****************************************************************************/
734 static __inline__ void DecodeMPEG2Intra( vpar_thread_t * p_vpar,
735 macroblock_t * p_mb, int i_b,
736 int i_chroma_format )
750 boolean_t b_vlc_intra;
754 /* Lookup Table for the chromatic component */
755 static int pi_cc_index[12] = { 0, 0, 0, 0, 1, 2, 1, 2, 1, 2 };
756 i_cc = pi_cc_index[i_b];
758 /* Determine whether it is luminance or not (chrominance) */
759 i_type = ( i_cc + 1 ) >> 1;
761 /* Give a pointer to the quantization matrices for intra blocks */
762 if( (i_chroma_format == CHROMA_420) || (!i_type) )
764 pi_quant = p_vpar->sequence.intra_quant.pi_matrix;
768 pi_quant = p_vpar->sequence.chroma_intra_quant.pi_matrix;
772 /* Decoding of the DC intra coefficient */
773 /* The nb of bits to parse depends on i_type */
774 i_code = ShowBits( &p_vpar->bit_stream, 9 + i_type );
776 /* To reduce memory occupation, there are two lookup tables
777 * See InitDCT above */
778 i_code5 = i_code >> (4+i_type);
780 /* Shall we lookup in the first or in the second table ? */
781 i_select = ( i_code5 == 31 );
782 /* Offset value for looking in the second table */
783 i_offset = 0x1f0 + ( i_type * 0x1f0 );
784 i_pos = ( i_code5 * ( ! i_select ) ) + ( ( i_code - i_offset ) * i_select );
785 i_dct_dc_size = p_vpar->pppl_dct_dc_size[i_type][i_select][i_pos].i_value;
788 if( !i_type/*i_b < 4*/ )
791 i_code = ShowBits(&p_vpar->bit_stream, 5);
794 i_dct_dc_size = pl_dct_dc_lum_init_table_1[i_code].i_value;
795 i_length = pl_dct_dc_lum_init_table_1[i_code].i_length;
796 RemoveBits( &p_vpar->bit_stream, i_length);
800 i_code = ShowBits(&p_vpar->bit_stream, 9) - 0x1f0;
801 i_dct_dc_size = pl_dct_dc_lum_init_table_2[i_code].i_value;
802 i_length = pl_dct_dc_lum_init_table_2[i_code].i_length;
803 RemoveBits( &p_vpar->bit_stream, i_length);
809 i_code = ShowBits(&p_vpar->bit_stream, 5);
813 i_dct_dc_size = pl_dct_dc_chrom_init_table_1[i_code].i_value;
814 i_length = pl_dct_dc_chrom_init_table_1[i_code].i_length;
815 RemoveBits(&p_vpar->bit_stream, i_length);
819 i_code = ShowBits(&p_vpar->bit_stream, 10) - 0x3e0;
820 i_dct_dc_size = pl_dct_dc_chrom_init_table_2[i_code].i_value;
821 i_length = pl_dct_dc_chrom_init_table_2[i_code].i_length;
822 RemoveBits( &p_vpar->bit_stream, i_length);
825 if (i_dct_dc_size==0)
829 i_dct_dc_diff = GetBits( &p_vpar->bit_stream, i_dct_dc_size);
830 if ((i_dct_dc_diff & (1<<(i_dct_dc_size-1)))==0)
831 i_dct_dc_diff-= (1<<i_dct_dc_size) - 1;
834 /* Dump the variable length code */
835 //RemoveBits( &p_vpar->bit_stream,
836 // p_vpar->pppl_dct_dc_size[i_type][i_select][i_pos].i_length );
838 /* Read the actual code with the good length */
839 p_vpar->mb.pi_dc_dct_pred[i_cc] += i_dct_dc_diff;
841 p_mb->ppi_blocks[i_b][0] = ( p_vpar->mb.pi_dc_dct_pred[i_cc] <<
842 ( 3 - p_vpar->picture.i_intra_dc_precision ) );
844 i_nc = ( p_vpar->mb.pi_dc_dct_pred[i_cc] != 0 );
846 /* Decoding of the AC coefficients */
849 b_vlc_intra = p_vpar->picture.b_intra_vlc_format;
850 for( i_parse = 1; !p_vpar->b_die/*i_parse < 64*/; i_parse++ )
852 i_code = ShowBits( &p_vpar->bit_stream, 16 );
853 if( i_code >= 16384 )
857 i_run = pl_DCT_tab0a[(i_code>>8)-4].i_run;
858 i_level = pl_DCT_tab0a[(i_code>>8)-4].i_level;
859 i_length = pl_DCT_tab0a[(i_code>>8)-4].i_length;
860 //fprintf( stderr, "**********> %d, %d, %d *******\n", i_run, i_level, (i_code>>8)-4 );
864 i_run = pl_DCT_tab_ac[(i_code>>12)-4].i_run;
865 i_level = pl_DCT_tab_ac[(i_code>>12)-4].i_level;
866 i_length = pl_DCT_tab_ac[(i_code>>12)-4].i_length;
871 i_run = p_vpar->ppl_dct_coef[b_vlc_intra][i_code].i_run;
872 i_length = p_vpar->ppl_dct_coef[b_vlc_intra][i_code].i_length;
873 i_level = p_vpar->ppl_dct_coef[b_vlc_intra][i_code].i_level;
879 int intra_vlc_format = b_vlc_intra;
882 if (code>=16384 && !intra_vlc_format)
883 tab = pl_DCT_tab_ac[(code>>12)-4];
886 if (intra_vlc_format)
887 tab = pl_DCT_tab0a[(code>>8)-4];
889 tab = pl_DCT_tab0[(code>>8)-4];
893 if (intra_vlc_format)
894 tab = pl_DCT_tab1a[(code>>6)-8];
896 tab = pl_DCT_tab1[(code>>6)-8];
899 tab = pl_DCT_tab2[(code>>4)-16];
901 tab = pl_DCT_tab3[(code>>3)-16];
903 tab = pl_DCT_tab4[(code>>2)-16];
905 tab = pl_DCT_tab5[(code>>1)-16];
907 tab = pl_DCT_tab6[code-16];
910 fprintf( stderr, "invalid Huffman code in Decode_MPEG2_Intra_Block()\n");
913 if( (i_run != tab.i_run) || (i_length != tab.i_length) || (i_level != tab.i_level) )
915 fprintf( stderr, "ET M....... !!!\n" );
924 RemoveBits( &p_vpar->bit_stream, i_length );
929 i_run = GetBits( &p_vpar->bit_stream, 6 );
930 i_level = GetBits( &p_vpar->bit_stream, 12 );
931 /*p_mb->ppi_blocks[i_b][i_parse] = ( b_sign = ( i_level > 2047 ) )
932 ? ( -4096 + i_level )
934 i_level = (b_sign = ( i_level > 2047 )) ? 4096 - i_level
940 p_mb->pf_idct[i_b] = vdec_SparseIDCT;
941 p_mb->pi_sparse_pos[i_b] = i_coef;
945 p_mb->pf_idct[i_b] = vdec_IDCT;
951 b_sign = GetBits( &p_vpar->bit_stream, 1 );
963 i_pos = pi_scan[p_vpar->picture.b_alternate_scan][i_parse];
964 i_level = ( i_level *
965 p_vpar->mb.i_quantizer_scale *
966 pi_quant[i_pos] ) >> 4;
967 p_mb->ppi_blocks[i_b][i_pos] = b_sign ? -i_level : i_level;
970 intf_ErrMsg("vpar error: DCT coeff (intra) is out of bounds\n");
971 p_vpar->picture.b_error = 1;
979 /****************************************************************************
980 * MotionCode : Parse the next motion code
981 ****************************************************************************/
982 static __inline__ int MotionCode( vpar_thread_t * p_vpar )
985 static lookup_t pl_mv_tab0[8] =
986 { {-1,0}, {3,3}, {2,2}, {2,2}, {1,1}, {1,1}, {1,1}, {1,1} };
987 /* Table B-10, motion_code, codes 0000011 ... 000011x */
988 static lookup_t pl_mv_tab1[8] =
989 { {-1,0}, {-1,0}, {-1,0}, {7,6}, {6,6}, {5,6}, {4,5}, {4,5} };
990 /* Table B-10, motion_code, codes 0000001100 ... 000001011x */
991 static lookup_t pl_mv_tab2[12] = {
992 {16,9}, {15,9}, {14,9}, {13,9},
993 {12,9}, {11,9}, {10,8}, {10,8},
994 {9,8}, {9,8}, {8,8}, {8,8} };
996 if( GetBits( &p_vpar->bit_stream, 1 ) )
1000 if( (i_code = ShowBits( &p_vpar->bit_stream, 9) ) >= 64 )
1003 RemoveBits( &p_vpar->bit_stream, pl_mv_tab0[i_code].i_length );
1004 return( GetBits( &p_vpar->bit_stream, 1 ) ?
1005 -pl_mv_tab0[i_code].i_value : pl_mv_tab0[i_code].i_value );
1011 RemoveBits( &p_vpar->bit_stream, pl_mv_tab1[i_code].i_length );
1012 return( GetBits( &p_vpar->bit_stream, 1 ) ?
1013 -pl_mv_tab1[i_code].i_value : pl_mv_tab1[i_code].i_value );
1016 if( (i_code -= 12) < 0 )
1018 p_vpar->picture.b_error = 1;
1019 intf_DbgMsg( "vpar debug: Invalid motion_vector code\n" );
1023 RemoveBits( &p_vpar->bit_stream, pl_mv_tab2[i_code].i_length );
1024 return( GetBits( &p_vpar->bit_stream, 1 ) ?
1025 -pl_mv_tab2[i_code].i_value : pl_mv_tab2[i_code].i_value );
1028 /****************************************************************************
1029 * DecodeMotionVector : Decode a motion_vector
1030 ****************************************************************************/
1031 static __inline__ void DecodeMotionVector( int * pi_prediction, int i_r_size,
1032 int i_motion_code, int i_motion_residual, int i_full_pel )
1034 int i_limit, i_vector;
1036 /* ISO/IEC 13818-1 section 7.6.3.1 */
1037 i_limit = 16 << i_r_size;
1038 i_vector = *pi_prediction >> i_full_pel;
1040 if( i_motion_code > 0 )
1042 i_vector += ((i_motion_code-1) << i_r_size) + i_motion_residual + 1;
1043 if( i_vector >= i_limit )
1044 i_vector -= i_limit + i_limit;
1046 else if( i_motion_code < 0 )
1048 i_vector -= ((-i_motion_code-1) << i_r_size) + i_motion_residual + 1;
1049 if( i_vector < -i_limit )
1050 i_vector += i_limit + i_limit;
1052 *pi_prediction = i_vector << i_full_pel;
1055 /****************************************************************************
1056 * MotionVector : Parse the next motion_vector field
1057 ****************************************************************************/
1058 static __inline__ void MotionVector( vpar_thread_t * p_vpar,
1059 macroblock_t * p_mb, int i_r,
1060 int i_s, int i_full_pel, int i_structure )
1062 int i_motion_code, i_motion_residual;
1064 int pi_dm_vector[2];
1066 i_r_size = p_vpar->picture.ppi_f_code[i_s][0] - 1;
1067 i_motion_code = MotionCode( p_vpar );
1068 i_motion_residual = (i_r_size != 0 && i_motion_code != 0) ?
1069 GetBits( &p_vpar->bit_stream, i_r_size) : 0;
1070 DecodeMotionVector( &p_vpar->mb.pppi_pmv[i_r][i_s][0], i_r_size,
1071 i_motion_code, i_motion_residual, i_full_pel );
1072 p_mb->pppi_motion_vectors[i_r][i_s][0] = p_vpar->mb.pppi_pmv[i_r][i_s][0];
1074 if( p_vpar->mb.b_dmv )
1076 if( GetBits(&p_vpar->bit_stream, 1) )
1078 pi_dm_vector[0] = GetBits( &p_vpar->bit_stream, 1 ) ? -1 : 1;
1082 pi_dm_vector[0] = 0;
1086 i_r_size = p_vpar->picture.ppi_f_code[i_s][1]-1;
1087 i_motion_code = MotionCode( p_vpar );
1088 i_motion_residual = (i_r_size != 0 && i_motion_code != 0) ?
1089 GetBits( &p_vpar->bit_stream, i_r_size) : 0;
1092 if( (p_vpar->mb.i_mv_format == MOTION_FIELD)
1093 && (i_structure == FRAME_STRUCTURE) )
1095 p_vpar->mb.pppi_pmv[i_r][i_s][1] >>= 1;
1098 DecodeMotionVector( &p_vpar->mb.pppi_pmv[i_r][i_s][1], i_r_size,
1099 i_motion_code, i_motion_residual, i_full_pel );
1101 if( (p_vpar->mb.i_mv_format == MOTION_FIELD)
1102 && (i_structure == FRAME_STRUCTURE) )
1103 p_vpar->mb.pppi_pmv[i_r][i_s][1] <<= 1;
1105 p_mb->pppi_motion_vectors[i_r][i_s][1] = p_vpar->mb.pppi_pmv[i_r][i_s][1];
1107 if( p_vpar->mb.b_dmv )
1109 if( GetBits(&p_vpar->bit_stream, 1) )
1111 pi_dm_vector[1] = GetBits( &p_vpar->bit_stream, 1 ) ? -1 : 1;
1115 pi_dm_vector[1] = 0;
1118 /* Dual Prime Arithmetic (ISO/IEC 13818-2 section 7.6.3.6). */
1120 #define i_mv_x p_mb->pppi_motion_vectors[0][0][0]
1121 if( i_structure == FRAME_STRUCTURE )
1123 #define i_mv_y (p_mb->pppi_motion_vectors[0][0][1] << 1)
1124 if( p_vpar->picture.b_top_field_first )
1126 /* vector for prediction of top field from bottom field */
1127 p_mb->ppi_dmv[0][0] = ((i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1128 p_mb->ppi_dmv[0][1] = ((i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] - 1;
1130 /* vector for prediction of bottom field from top field */
1131 p_mb->ppi_dmv[1][0] = ((3*i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1132 p_mb->ppi_dmv[1][1] = ((3*i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] + 1;
1136 /* vector for prediction of top field from bottom field */
1137 p_mb->ppi_dmv[0][0] = ((3*i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1138 p_mb->ppi_dmv[0][1] = ((3*i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] - 1;
1140 /* vector for prediction of bottom field from top field */
1141 p_mb->ppi_dmv[1][0] = ((i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1142 p_mb->ppi_dmv[1][1] = ((i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] + 1;
1148 #define i_mv_y p_mb->pppi_motion_vectors[0][0][1]
1149 /* vector for prediction from field of opposite 'parity' */
1150 p_mb->ppi_dmv[0][0] = ((i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1151 p_mb->ppi_dmv[0][1] = ((i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1];
1153 /* correct for vertical field shift */
1154 if( p_vpar->picture.i_structure == TOP_FIELD )
1155 p_mb->ppi_dmv[0][1]--;
1157 p_mb->ppi_dmv[0][1]++;
1164 /*****************************************************************************
1165 * DecodeMVMPEG1 : Parse the next MPEG-1 motion vectors
1166 *****************************************************************************/
1167 static __inline__ void DecodeMVMPEG1( vpar_thread_t * p_vpar,
1168 macroblock_t * p_mb, int i_s, int i_structure )
1170 MotionVector( p_vpar, p_mb, 0, i_s,
1171 p_vpar->picture.pb_full_pel_vector[i_s], i_structure );
1174 /*****************************************************************************
1175 * DecodeMVMPEG2 : Parse the next MPEG-2 motion_vectors field
1176 *****************************************************************************/
1177 static __inline__ void DecodeMVMPEG2( vpar_thread_t * p_vpar,
1178 macroblock_t * p_mb, int i_s, int i_structure )
1180 if( p_vpar->mb.i_mv_count == 1 )
1182 if( p_vpar->mb.i_mv_format == MOTION_FIELD && !p_vpar->mb.b_dmv )
1184 p_mb->ppi_field_select[0][i_s] = p_mb->ppi_field_select[1][i_s]
1185 = GetBits( &p_vpar->bit_stream, 1 );
1187 MotionVector( p_vpar, p_mb, 0, i_s, 0, i_structure );
1188 p_vpar->mb.pppi_pmv[1][i_s][0] = p_vpar->mb.pppi_pmv[0][i_s][0];
1189 p_vpar->mb.pppi_pmv[1][i_s][1] = p_vpar->mb.pppi_pmv[0][i_s][1];
1190 p_mb->pppi_motion_vectors[1][i_s][0] = p_vpar->mb.pppi_pmv[0][i_s][0];
1191 p_mb->pppi_motion_vectors[1][i_s][1] = p_vpar->mb.pppi_pmv[0][i_s][1];
1195 p_mb->ppi_field_select[0][i_s] = GetBits( &p_vpar->bit_stream, 1 );
1196 MotionVector( p_vpar, p_mb, 0, i_s, 0, i_structure );
1197 p_mb->ppi_field_select[1][i_s] = GetBits( &p_vpar->bit_stream, 1 );
1198 MotionVector( p_vpar, p_mb, 1, i_s, 0, i_structure );
1204 * Macroblock information structures
1207 /*****************************************************************************
1208 * MacroblockAddressIncrement : Get the macroblock_address_increment field
1209 *****************************************************************************/
1210 static __inline__ int MacroblockAddressIncrement( vpar_thread_t * p_vpar )
1213 /* Index in the lookup table mb_addr_inc */
1214 int i_index = ShowBits( &p_vpar->bit_stream, 11 );
1216 /* Test the presence of the escape character */
1217 while( i_index == 8 )
1219 RemoveBits( &p_vpar->bit_stream, 11 );
1221 i_index = ShowBits( &p_vpar->bit_stream, 11 );
1224 /* Affect the value from the lookup table */
1225 i_addr_inc += p_vpar->pl_mb_addr_inc[i_index].i_value;
1227 /* Dump the good number of bits */
1228 RemoveBits( &p_vpar->bit_stream, p_vpar->pl_mb_addr_inc[i_index].i_length );
1233 /*****************************************************************************
1234 * IMBType : macroblock_type in I pictures
1235 *****************************************************************************/
1236 static __inline__ int IMBType( vpar_thread_t * p_vpar )
1238 /* Take two bits for testing */
1239 int i_type = ShowBits( &p_vpar->bit_stream, 2 );
1241 /* Lookup table for macroblock_type */
1242 static lookup_t pl_mb_Itype[4] = { {MB_ERROR, 0},
1243 {MB_QUANT|MB_INTRA, 2},
1246 /* Dump the good number of bits */
1247 RemoveBits( &p_vpar->bit_stream, pl_mb_Itype[i_type].i_length );
1248 return pl_mb_Itype[i_type].i_value;
1251 /*****************************************************************************
1252 * PMBType : macroblock_type in P pictures
1253 *****************************************************************************/
1254 static __inline__ int PMBType( vpar_thread_t * p_vpar )
1256 /* Testing on 6 bits */
1257 int i_type = ShowBits( &p_vpar->bit_stream, 6 );
1259 /* Dump the good number of bits */
1260 RemoveBits( &p_vpar->bit_stream, p_vpar->ppl_mb_type[0][i_type].i_length );
1261 /* return the value from the lookup table for P type */
1262 return p_vpar->ppl_mb_type[0][i_type].i_value;
1265 /*****************************************************************************
1266 * BMBType : macroblock_type in B pictures
1267 *****************************************************************************/
1268 static __inline__ int BMBType( vpar_thread_t * p_vpar )
1270 /* Testing on 6 bits */
1271 int i_type = ShowBits( &p_vpar->bit_stream, 6 );
1273 /* Dump the good number of bits */
1274 RemoveBits( &p_vpar->bit_stream, p_vpar->ppl_mb_type[1][i_type].i_length );
1276 /* return the value from the lookup table for B type */
1277 return p_vpar->ppl_mb_type[1][i_type].i_value;
1280 /*****************************************************************************
1281 * DMBType : macroblock_type in D pictures
1282 *****************************************************************************/
1283 static __inline__ int DMBType( vpar_thread_t * p_vpar )
1285 return GetBits( &p_vpar->bit_stream, 1 );
1288 /*****************************************************************************
1289 * CodedPattern420 : coded_block_pattern with 4:2:0 chroma
1290 *****************************************************************************/
1291 static __inline__ int CodedPattern420( vpar_thread_t * p_vpar )
1293 /* Take the max 9 bits length vlc code for testing */
1294 int i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1296 /* Trash the good number of bits read in the lookup table */
1297 RemoveBits( &p_vpar->bit_stream, pl_coded_pattern[i_vlc].i_length );
1299 /* return the value from the vlc table */
1300 return pl_coded_pattern[i_vlc].i_value;
1303 /*****************************************************************************
1304 * CodedPattern422 : coded_block_pattern with 4:2:2 chroma
1305 *****************************************************************************/
1306 static __inline__ int CodedPattern422( vpar_thread_t * p_vpar )
1308 int i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1310 RemoveBits( &p_vpar->bit_stream, pl_coded_pattern[i_vlc].i_length );
1312 /* Supplementary 2 bits long code for 4:2:2 format */
1313 return pl_coded_pattern[i_vlc].i_value |
1314 (GetBits( &p_vpar->bit_stream, 2 ) << 6);
1317 /*****************************************************************************
1318 * CodedPattern444 : coded_block_pattern with 4:4:4 chroma
1319 *****************************************************************************/
1320 static __inline__ int CodedPattern444( vpar_thread_t * p_vpar )
1322 int i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1324 RemoveBits( &p_vpar->bit_stream, pl_coded_pattern[i_vlc].i_length );
1326 return pl_coded_pattern[i_vlc].i_value |
1327 (GetBits( &p_vpar->bit_stream, 6 ) << 6);
1330 /*****************************************************************************
1331 * InitMacroblock : Initialize macroblock values
1332 *****************************************************************************/
1333 static __inline__ void InitMacroblock( vpar_thread_t * p_vpar,
1334 macroblock_t * p_mb, int i_coding_type,
1335 int i_chroma_format,
1337 boolean_t b_second_field )
1339 p_mb->i_chroma_nb_blocks = 1 << i_chroma_format;
1340 p_mb->p_picture = p_vpar->picture.p_picture;
1342 if( i_coding_type == B_CODING_TYPE )
1343 p_mb->p_backward = p_vpar->sequence.p_backward;
1345 p_mb->p_backward = NULL;
1346 if( (i_coding_type == P_CODING_TYPE) || (i_coding_type == B_CODING_TYPE) )
1347 p_mb->p_forward = p_vpar->sequence.p_forward;
1349 p_mb->p_forward = NULL;
1351 p_mb->i_l_x = p_vpar->mb.i_l_x;
1352 p_mb->i_c_x = p_vpar->mb.i_c_x;
1353 p_mb->i_motion_l_y = p_vpar->mb.i_l_y;
1354 p_mb->i_motion_c_y = p_vpar->mb.i_c_y;
1355 if( (p_mb->b_motion_field = (i_structure == BOTTOM_FIELD)) )
1357 p_mb->i_motion_l_y--;
1358 p_mb->i_motion_c_y--;
1360 p_mb->i_addb_l_stride = (p_mb->i_l_stride = p_vpar->picture.i_l_stride) - 8;
1361 p_mb->i_addb_c_stride = (p_mb->i_c_stride = p_vpar->picture.i_c_stride) - 8;
1362 p_mb->b_P_second = ( b_second_field && i_coding_type == P_CODING_TYPE );
1365 /*****************************************************************************
1366 * UpdateContext : Update the p_vpar contextual values
1367 *****************************************************************************/
1368 static __inline__ void UpdateContext( vpar_thread_t * p_vpar, int i_structure )
1370 /* Update macroblock real position. */
1371 p_vpar->mb.i_l_x += 16;
1372 p_vpar->mb.i_l_y += (p_vpar->mb.i_l_x / p_vpar->sequence.i_width)
1373 * (2 - (i_structure == FRAME_STRUCTURE)) * 16;
1374 p_vpar->mb.i_l_x %= p_vpar->sequence.i_width;
1376 p_vpar->mb.i_c_x += p_vpar->sequence.i_chroma_mb_width;
1377 p_vpar->mb.i_c_y += (p_vpar->mb.i_c_x / p_vpar->sequence.i_chroma_width)
1378 * (2 - (i_structure == FRAME_STRUCTURE))
1379 * p_vpar->sequence.i_chroma_mb_height;
1380 p_vpar->mb.i_c_x %= p_vpar->sequence.i_chroma_width;
1383 /*****************************************************************************
1384 * SkippedMacroblock : Generate a skipped macroblock with NULL motion vector
1385 *****************************************************************************/
1386 static __inline__ void SkippedMacroblock( vpar_thread_t * p_vpar, int i_mb,
1387 int i_mb_base, int i_coding_type,
1388 int i_chroma_format,
1390 boolean_t b_second_field )
1392 macroblock_t * p_mb;
1394 static f_motion_t pf_motion_skipped[4][4] =
1396 {NULL, NULL, NULL, NULL},
1397 {NULL, vdec_MotionFieldField420, vdec_MotionFieldField420,
1398 vdec_MotionFrameFrame420},
1399 {NULL, vdec_MotionFieldField422, vdec_MotionFieldField422,
1400 vdec_MotionFrameFrame422},
1401 {NULL, vdec_MotionFieldField444, vdec_MotionFieldField444,
1402 vdec_MotionFrameFrame444},
1405 if( i_coding_type == I_CODING_TYPE )
1407 intf_DbgMsg("vpar error: skipped macroblock in I-picture\n");
1408 p_vpar->picture.b_error = 1;
1412 if( (p_mb = vpar_NewMacroblock( &p_vpar->vfifo )) == NULL )
1418 p_vpar->picture.pp_mb[i_mb_base + i_mb] = p_mb;
1421 InitMacroblock( p_vpar, p_mb, i_coding_type, i_chroma_format,
1422 i_structure, b_second_field );
1424 /* Motion type is picture structure. */
1425 p_mb->pf_motion = pf_motion_skipped[i_chroma_format]
1427 p_mb->i_mb_type = MB_MOTION_FORWARD;
1428 p_mb->i_coded_block_pattern = 0;
1429 memset( p_mb->pppi_motion_vectors, 0, 8*sizeof(int) );
1431 /* Set the field we use for motion compensation */
1432 p_mb->ppi_field_select[0][0] = p_mb->ppi_field_select[0][1]
1433 = ( i_structure == BOTTOM_FIELD );
1435 UpdateContext( p_vpar, i_structure );
1438 /* Decode the macroblock NOW ! */
1439 vdec_DecodeMacroblock( p_vpar->pp_vdec[0], p_mb );
1443 /*****************************************************************************
1444 * MacroblockModes : Get the macroblock_modes structure
1445 *****************************************************************************/
1446 static __inline__ void MacroblockModes( vpar_thread_t * p_vpar,
1447 macroblock_t * p_mb,
1448 int i_chroma_format,
1452 static int ppi_mv_count[2][4] = { {0, 1, 2, 1}, {0, 2, 1, 1} };
1453 static int ppi_mv_format[2][4] = { {0, 1, 1, 1}, {0, 1, 2, 1} };
1455 /* Get macroblock_type. */
1456 switch( i_coding_type )
1459 p_mb->i_mb_type = PMBType( p_vpar );
1462 p_mb->i_mb_type = BMBType( p_vpar );
1465 p_mb->i_mb_type = IMBType( p_vpar );
1468 p_mb->i_mb_type = DMBType( p_vpar );
1471 /* SCALABILITY : warning, we don't know if spatial_temporal_weight_code
1472 * has to be dropped, take care if you use scalable streams. */
1473 /* RemoveBits( &p_vpar->bit_stream, 2 ); */
1475 if( (i_coding_type == P_CODING_TYPE || i_coding_type == B_CODING_TYPE)
1476 && (p_mb->i_mb_type & (MB_MOTION_FORWARD | MB_MOTION_BACKWARD)) )
1478 if( !(i_structure == FRAME_STRUCTURE
1479 && p_vpar->picture.b_frame_pred_frame_dct) )
1481 p_vpar->mb.i_motion_type = GetBits( &p_vpar->bit_stream, 2 );
1485 p_vpar->mb.i_motion_type = MOTION_FRAME;
1489 p_vpar->mb.i_mv_count = ppi_mv_count[i_structure == FRAME_STRUCTURE]
1490 [p_vpar->mb.i_motion_type];
1491 p_vpar->mb.i_mv_format = ppi_mv_format[i_structure == FRAME_STRUCTURE]
1492 [p_vpar->mb.i_motion_type];
1493 p_vpar->mb.b_dmv = p_vpar->mb.i_motion_type == MOTION_DMV;
1496 p_vpar->mb.b_dct_type = 0;
1497 if( (i_structure == FRAME_STRUCTURE) &&
1498 (!p_vpar->picture.b_frame_pred_frame_dct) &&
1499 (p_mb->i_mb_type & (MB_PATTERN|MB_INTRA)) )
1501 if( (p_vpar->mb.b_dct_type = GetBits( &p_vpar->bit_stream, 1 )) )
1503 /* The DCT is coded on fields. Jump one line between each
1505 p_mb->i_addb_l_stride <<= 1;
1506 p_mb->i_addb_l_stride += 8;
1507 /* With CHROMA_420, the DCT is necessarily frame-coded. */
1508 if( i_chroma_format != CHROMA_420 )
1510 p_mb->i_addb_c_stride <<= 1;
1511 p_mb->i_addb_c_stride += 8;
1517 /*****************************************************************************
1518 * ParseMacroblock : Parse the next macroblock
1519 *****************************************************************************/
1520 #define PARSEERROR \
1521 if( p_vpar->picture.b_error ) \
1523 /* Mark this block as skipped (better than green blocks), and \
1524 * go to the next slice. */ \
1525 (*pi_mb_address)--; \
1526 vpar_DestroyMacroblock( &p_vpar->vfifo, p_mb ); \
1531 #define PARSEBLOCKS( MPEG1FUNC, MPEG2FUNC ) \
1533 i_mask = 1 << (3 + (1 << i_chroma_format)); \
1536 p_data1 = p_mb->p_picture->p_y \
1537 + p_mb->i_l_x + p_vpar->mb.i_l_y*(p_vpar->sequence.i_width); \
1539 for( i_b = 0 ; i_b < 4 ; i_b++, i_mask >>= 1 ) \
1541 if( p_mb->i_coded_block_pattern & i_mask ) \
1543 memset( p_mb->ppi_blocks[i_b], 0, 64*sizeof(dctelem_t) ); \
1545 MPEG2FUNC( p_vpar, p_mb, i_b, i_chroma_format ); \
1547 MPEG1FUNC( p_vpar, p_mb, i_b, i_chroma_format ); \
1549 /* Calculate block coordinates. */ \
1550 p_mb->p_data[i_b] = p_data1 \
1551 + pi_y[p_vpar->mb.b_dct_type][i_b] \
1552 * p_vpar->sequence.i_width \
1560 p_data1 = p_mb->p_picture->p_u \
1562 + p_vpar->mb.i_c_y \
1563 * (p_vpar->sequence.i_chroma_width); \
1564 p_data2 = p_mb->p_picture->p_v \
1566 + p_vpar->mb.i_c_y \
1567 * (p_vpar->sequence.i_chroma_width); \
1569 for( i_b = 4; i_b < 4 + (1 << i_chroma_format); \
1570 i_b++, i_mask >>= 1 ) \
1572 yuv_data_t * pp_data[2] = {p_data1, p_data2}; \
1574 if( p_mb->i_coded_block_pattern & i_mask ) \
1576 memset( p_mb->ppi_blocks[i_b], 0, 64*sizeof(dctelem_t) ); \
1578 MPEG2FUNC( p_vpar, p_mb, i_b, i_chroma_format ); \
1580 MPEG1FUNC( p_vpar, p_mb, i_b, i_chroma_format ); \
1582 /* Calculate block coordinates. */ \
1583 p_mb->p_data[i_b] = pp_data[i_b & 1] \
1584 + pi_y[p_vpar->mb.b_dct_type][i_b] \
1585 * p_vpar->sequence.i_chroma_width \
1593 static __inline__ void ParseMacroblock(
1594 vpar_thread_t * p_vpar,
1595 int * pi_mb_address, /* previous address to be
1596 * used for mb_addr_incr */
1597 int i_mb_previous, /* actual previous mb */
1598 int i_mb_base, /* non-zero if field structure */
1599 /* The following parameters are explicit in
1600 * optimized routines : */
1601 boolean_t b_mpeg2, /* you know what ? */
1602 int i_coding_type, /* I, P, B or D */
1603 int i_chroma_format, /* 4:2:0, 4:2:2 or 4:4:4 */
1604 int i_structure, /* T(OP), B(OTTOM) or F(RAME) */
1605 boolean_t b_second_field ) /* second field of a
1608 static f_motion_t pppf_motion[4][2][4] =
1610 { {NULL, NULL, NULL, NULL},
1611 {NULL, NULL, NULL, NULL}
1613 { {NULL, vdec_MotionFieldField420, vdec_MotionField16x8420,
1614 vdec_MotionFieldDMV420},
1615 {NULL, vdec_MotionFrameField420, vdec_MotionFrameFrame420,
1616 vdec_MotionFrameDMV420}
1618 { {NULL, vdec_MotionFieldField422, vdec_MotionField16x8422,
1619 vdec_MotionFieldDMV422},
1620 {NULL, vdec_MotionFrameField422, vdec_MotionFrameFrame422,
1621 vdec_MotionFrameDMV422}
1623 { {NULL, vdec_MotionFieldField444, vdec_MotionField16x8444,
1624 vdec_MotionFieldDMV444},
1625 {NULL, vdec_MotionFrameField444, vdec_MotionFrameFrame444,
1626 vdec_MotionFrameDMV444}
1629 static int pi_x[12] = {0,8,0,8,0,0,0,0,8,8,8,8};
1630 static int pi_y[2][12] = { {0,0,8,8,0,0,8,8,0,0,8,8},
1631 {0,0,1,1,0,0,1,1,0,0,1,1} };
1632 int i_mb, i_b, i_mask;
1634 macroblock_t * p_mb;
1635 yuv_data_t * p_data1;
1636 yuv_data_t * p_data2;
1640 i_inc = MacroblockAddressIncrement( p_vpar );
1641 *pi_mb_address += i_inc;
1645 fprintf( stderr, "vpar error: bad address increment (%d)\n", i_inc );
1646 p_vpar->picture.b_error = 1;
1650 if( *pi_mb_address - i_mb_previous - 1 )
1652 /* Skipped macroblock (ISO/IEC 13818-2 7.6.6). */
1654 /* Reset DC predictors (7.2.1). */
1655 p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
1656 = p_vpar->mb.pi_dc_dct_pred[2]
1657 = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
1659 if( i_coding_type == P_CODING_TYPE )
1661 /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
1662 memset( p_vpar->mb.pppi_pmv, 0, 8*sizeof(int) );
1665 for( i_mb = i_mb_previous + 1; i_mb < *pi_mb_address; i_mb++ )
1667 SkippedMacroblock( p_vpar, i_mb, i_mb_base, i_coding_type,
1668 i_chroma_format, i_structure, b_second_field );
1672 /* Get a macroblock structure. */
1673 if( (p_mb = vpar_NewMacroblock( &p_vpar->vfifo )) == NULL )
1679 p_vpar->picture.pp_mb[i_mb_base + *pi_mb_address] = p_mb;
1682 InitMacroblock( p_vpar, p_mb, i_coding_type, i_chroma_format,
1683 i_structure, b_second_field );
1685 /* Parse off macroblock_modes structure. */
1686 MacroblockModes( p_vpar, p_mb, i_chroma_format, i_coding_type,
1689 if( p_mb->i_mb_type & MB_QUANT )
1691 LoadQuantizerScale( p_vpar );
1694 if( (i_coding_type == P_CODING_TYPE || i_coding_type == B_CODING_TYPE)
1695 && (p_mb->i_mb_type & MB_MOTION_FORWARD) )
1698 DecodeMVMPEG2( p_vpar, p_mb, 0, i_structure );
1700 DecodeMVMPEG1( p_vpar, p_mb, 0, i_structure );
1704 if( (i_coding_type == B_CODING_TYPE)
1705 && (p_mb->i_mb_type & MB_MOTION_BACKWARD) )
1708 DecodeMVMPEG2( p_vpar, p_mb, 1, i_structure );
1710 DecodeMVMPEG1( p_vpar, p_mb, 1, i_structure );
1714 if( i_coding_type == P_CODING_TYPE
1715 && !(p_mb->i_mb_type & (MB_MOTION_FORWARD|MB_INTRA)) )
1717 /* Special No-MC macroblock in P pictures (7.6.3.5). */
1718 p_mb->i_mb_type |= MB_MOTION_FORWARD;
1719 memset( p_vpar->mb.pppi_pmv, 0, 8*sizeof(int) );
1720 memset( p_mb->pppi_motion_vectors, 0, 8*sizeof(int) );
1721 p_vpar->mb.i_motion_type = 1 + (i_structure == FRAME_STRUCTURE);
1722 p_mb->ppi_field_select[0][0] = (i_structure == BOTTOM_FIELD);
1725 if( (i_coding_type != I_CODING_TYPE) && !(p_mb->i_mb_type & MB_INTRA) )
1727 /* Reset DC predictors (7.2.1). */
1728 p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
1729 = p_vpar->mb.pi_dc_dct_pred[2]
1730 = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
1732 /* Motion function pointer. */
1733 p_mb->pf_motion = pppf_motion[i_chroma_format]
1734 [i_structure == FRAME_STRUCTURE]
1735 [p_vpar->mb.i_motion_type];
1737 if( p_mb->i_mb_type & MB_PATTERN )
1739 switch( i_chroma_format )
1742 p_mb->i_coded_block_pattern = CodedPattern420( p_vpar );
1745 p_mb->i_coded_block_pattern = CodedPattern422( p_vpar );
1748 p_mb->i_coded_block_pattern = CodedPattern444( p_vpar );
1753 p_mb->i_coded_block_pattern = 0;
1757 * Effectively decode blocks.
1759 PARSEBLOCKS( DecodeMPEG1NonIntra, DecodeMPEG2NonIntra )
1763 if( !p_vpar->picture.b_concealment_mv )
1765 /* Reset MV predictors. */
1766 memset( p_vpar->mb.pppi_pmv, 0, 8*sizeof(int) );
1771 DecodeMVMPEG2( p_vpar, p_mb, 0, i_structure );
1773 DecodeMVMPEG1( p_vpar, p_mb, 0, i_structure );
1774 RemoveBits( &p_vpar->bit_stream, 1 );
1777 if( p_mb->i_mb_type & MB_PATTERN )
1779 switch( i_chroma_format )
1782 p_mb->i_coded_block_pattern = CodedPattern420( p_vpar );
1785 p_mb->i_coded_block_pattern = CodedPattern422( p_vpar );
1788 p_mb->i_coded_block_pattern = CodedPattern444( p_vpar );
1793 p_mb->i_coded_block_pattern =
1794 (1 << (4 + (1 << i_chroma_format))) - 1;
1798 * Effectively decode blocks.
1800 PARSEBLOCKS( DecodeMPEG1Intra, DecodeMPEG2Intra )
1803 if( !p_vpar->picture.b_error )
1805 UpdateContext( p_vpar, i_structure );
1807 /* Decode the macroblock NOW ! */
1808 vdec_DecodeMacroblock( p_vpar->pp_vdec[0], p_mb );
1813 /* Mark this block as skipped (better than green blocks), and go
1814 * to the next slice. */
1816 vpar_DestroyMacroblock( &p_vpar->vfifo, p_mb );
1821 * Picture data parsing management
1824 /*****************************************************************************
1825 * SliceHeader : Parse the next slice structure
1826 *****************************************************************************/
1827 static __inline__ void SliceHeader( vpar_thread_t * p_vpar,
1828 int * pi_mb_address, int i_mb_base,
1829 u32 i_vert_code, boolean_t b_high,
1830 boolean_t b_dp_scalable,
1831 boolean_t b_mpeg2, int i_coding_type,
1832 int i_chroma_format, int i_structure,
1833 boolean_t b_second_field )
1835 int i_mb_address_save = *pi_mb_address;
1837 p_vpar->picture.b_error = 0;
1841 /* Picture with more than 2800 lines. */
1842 i_vert_code += GetBits( &p_vpar->bit_stream, 3 ) << 7;
1846 /* DATA_PARTITIONING scalability. */
1847 RemoveBits( &p_vpar->bit_stream, 7 ); /* priority_breakpoint */
1850 LoadQuantizerScale( p_vpar );
1852 if( GetBits( &p_vpar->bit_stream, 1 ) )
1854 /* intra_slice, slice_id */
1855 RemoveBits( &p_vpar->bit_stream, 8 );
1856 /* extra_information_slice */
1857 while( GetBits( &p_vpar->bit_stream, 1 ) )
1859 RemoveBits( &p_vpar->bit_stream, 8 );
1862 *pi_mb_address = (i_vert_code - 1)*p_vpar->sequence.i_mb_width;
1864 if( *pi_mb_address < i_mb_address_save )
1866 fprintf( stderr, "vpar error: slices do not follow, maybe a PES has been trashed\n" );
1867 p_vpar->picture.b_error = 1;
1871 /* Reset DC coefficients predictors (ISO/IEC 13818-2 7.2.1). */
1872 p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
1873 = p_vpar->mb.pi_dc_dct_pred[2]
1874 = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
1876 /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
1877 memset( p_vpar->mb.pppi_pmv, 0, 8*sizeof(int) );
1881 if( i_mb_address_save >= p_vpar->sequence.i_mb_size )
1883 p_vpar->picture.b_error = 1;
1886 ParseMacroblock( p_vpar, pi_mb_address, i_mb_address_save,
1887 i_mb_base, b_mpeg2, i_coding_type,
1888 i_chroma_format, i_structure,
1890 i_mb_address_save = *pi_mb_address;
1891 if( p_vpar->picture.b_error )
1896 while( ShowBits( &p_vpar->bit_stream, 23 ) && !p_vpar->b_die );
1897 NextStartCode( p_vpar );
1900 /*****************************************************************************
1901 * PictureData : Parse off all macroblocks (ISO/IEC 13818-2 6.2.3.7)
1902 *****************************************************************************/
1903 static __inline__ void PictureData( vpar_thread_t * p_vpar, int i_mb_base,
1904 boolean_t b_high, boolean_t b_dp_scalable,
1905 boolean_t b_mpeg2, int i_coding_type,
1906 int i_chroma_format, int i_structure,
1907 boolean_t b_second_field )
1909 int i_mb_address = 0;
1912 NextStartCode( p_vpar );
1913 while( ((i_coding_type != I_CODING_TYPE && i_coding_type != D_CODING_TYPE)
1914 || !p_vpar->picture.b_error)
1915 && i_mb_address+i_mb_base < p_vpar->sequence.i_mb_size
1918 if( ((i_dummy = ShowBits( &p_vpar->bit_stream, 32 ))
1919 < SLICE_START_CODE_MIN) ||
1920 (i_dummy > SLICE_START_CODE_MAX) )
1922 intf_DbgMsg("vpar debug: premature end of picture\n");
1923 p_vpar->picture.b_error = 1;
1926 RemoveBits32( &p_vpar->bit_stream );
1928 /* Decode slice data. */
1929 SliceHeader( p_vpar, &i_mb_address, i_mb_base, i_dummy & 255,
1930 b_high, b_dp_scalable, b_mpeg2, i_coding_type,
1931 i_chroma_format, i_structure, b_second_field );
1934 /* Try to recover from error. If we missed less than half the
1935 * number of macroblocks of the picture, mark the missed ones
1937 if( (i_coding_type == P_CODING_TYPE || i_coding_type == B_CODING_TYPE)
1938 && p_vpar->picture.b_error &&
1939 ( (i_mb_address-i_mb_base) > (p_vpar->sequence.i_mb_size >> 1)
1940 || (i_structure != FRAME_STRUCTURE
1941 && (i_mb_address-i_mb_base) > (p_vpar->sequence.i_mb_size >> 2) ) ) )
1945 p_vpar->picture.b_error = 0;
1946 for( i_mb = i_mb_address + 1;
1947 i_mb < (p_vpar->sequence.i_mb_size
1948 << (i_structure != FRAME_STRUCTURE));
1951 SkippedMacroblock( p_vpar, i_mb, i_mb_base, i_coding_type,
1952 i_chroma_format, i_structure, b_second_field );
1957 /*****************************************************************************
1958 * vpar_PictureDataVWXYZ : Parse the next macroblock ; specific functions
1959 *****************************************************************************
1962 * X = chroma format ?
1964 * Z = second field ?
1965 *****************************************************************************/
1966 void vpar_PictureDataGENERIC( vpar_thread_t * p_vpar, int i_mb_base )
1968 PictureData( p_vpar, i_mb_base, (p_vpar->sequence.i_height > 2800),
1969 (p_vpar->sequence.i_scalable_mode == SC_DP),
1970 p_vpar->sequence.b_mpeg2, p_vpar->picture.i_coding_type,
1971 p_vpar->sequence.i_chroma_format,
1972 p_vpar->picture.i_structure,
1973 (p_vpar->picture.i_structure !=
1974 p_vpar->picture.i_current_structure) );
1977 #if (VPAR_OPTIM_LEVEL > 0)
1978 /* Optimizations for frame pictures */
1979 void vpar_PictureData2I420F0( vpar_thread_t * p_vpar, int i_mb_base )
1981 PictureData( p_vpar, 0, 0, 0, 1, I_CODING_TYPE, CHROMA_420,
1982 FRAME_STRUCTURE, 0 );
1985 void vpar_PictureData2P420F0( vpar_thread_t * p_vpar, int i_mb_base )
1987 PictureData( p_vpar, 0, 0, 0, 1, P_CODING_TYPE, CHROMA_420,
1988 FRAME_STRUCTURE, 0 );
1991 void vpar_PictureData2B420F0( vpar_thread_t * p_vpar, int i_mb_base )
1993 PictureData( p_vpar, 0, 0, 0, 1, B_CODING_TYPE, CHROMA_420,
1994 FRAME_STRUCTURE, 0 );
1998 #if (VPAR_OPTIM_LEVEL > 1)
1999 /* Optimizations for field pictures */
2000 void vpar_PictureData2I420TZ( vpar_thread_t * p_vpar, int i_mb_base )
2002 PictureData( p_vpar, i_mb_base, 0, 0, 1, I_CODING_TYPE, CHROMA_420,
2003 TOP_FIELD, (p_vpar->picture.i_structure !=
2004 p_vpar->picture.i_current_structure) );
2007 void vpar_PictureData2P420TZ( vpar_thread_t * p_vpar, int i_mb_base )
2009 PictureData( p_vpar, i_mb_base, 0, 0, 1, P_CODING_TYPE, CHROMA_420,
2010 TOP_FIELD, (p_vpar->picture.i_structure !=
2011 p_vpar->picture.i_current_structure) );
2014 void vpar_PictureData2B420TZ( vpar_thread_t * p_vpar, int i_mb_base )
2016 PictureData( p_vpar, i_mb_base, 0, 0, 1, B_CODING_TYPE, CHROMA_420,
2017 TOP_FIELD, (p_vpar->picture.i_structure !=
2018 p_vpar->picture.i_current_structure) );
2021 void vpar_PictureData2I420BZ( vpar_thread_t * p_vpar, int i_mb_base )
2023 PictureData( p_vpar, i_mb_base, 0, 0, 1, I_CODING_TYPE, CHROMA_420,
2024 BOTTOM_FIELD, (p_vpar->picture.i_structure !=
2025 p_vpar->picture.i_current_structure) );
2028 void vpar_PictureData2P420BZ( vpar_thread_t * p_vpar, int i_mb_base )
2030 PictureData( p_vpar, i_mb_base, 0, 0, 1, P_CODING_TYPE, CHROMA_420,
2031 BOTTOM_FIELD, (p_vpar->picture.i_structure !=
2032 p_vpar->picture.i_current_structure) );
2035 void vpar_PictureData2B420BZ( vpar_thread_t * p_vpar, int i_mb_base )
2037 PictureData( p_vpar, i_mb_base, 0, 0, 1, B_CODING_TYPE, CHROMA_420,
2038 BOTTOM_FIELD, (p_vpar->picture.i_structure !=
2039 p_vpar->picture.i_current_structure) );