1 /*****************************************************************************
2 * vpar_blocks.c : blocks parsing
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
6 * Authors: Christophe Massiot <massiot@via.ecp.fr>
7 * Jean-Marc Dressler <polux@via.ecp.fr>
8 * Stéphane Borel <stef@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
30 #include <sys/types.h> /* on BSD, uio.h needs types.h */
31 #include <sys/uio.h> /* "input.h" */
32 #include <string.h> /* memset */
41 #include "debug.h" /* XXX?? temporaire, requis par netlist.h */
43 #include "stream_control.h"
44 #include "input_ext-dec.h"
47 #include "video_output.h"
49 #include "vdec_idct.h"
50 #include "video_decoder.h"
51 #include "vdec_motion.h"
53 #include "vpar_blocks.h"
54 #include "vpar_headers.h"
55 #include "vpar_synchro.h"
56 #include "video_parser.h"
57 #include "video_fifo.h"
60 * Welcome to vpar_blocks.c ! Here's where the heavy processor-critical parsing
61 * task is done. This file is divided in several parts :
62 * - Initialization of the lookup tables
63 * - Decoding of coded blocks
64 * - Decoding of motion vectors
65 * - Decoding of the other macroblock structures
66 * - Picture data parsing management (slices and error handling)
67 * It's a pretty long file. Good luck and have a nice day.
72 * Initialization tables
75 /* Table for coded_block_pattern resolution */
76 static lookup_t pl_coded_pattern[512] =
77 { {MB_ERROR, 0}, {0, 9}, {39, 9}, {27, 9}, {59, 9}, {55, 9}, {47, 9}, {31, 9},
78 {58, 8}, {58, 8}, {54, 8}, {54, 8}, {46, 8}, {46, 8}, {30, 8}, {30, 8},
79 {57, 8}, {57, 8}, {53, 8}, {53, 8}, {45, 8}, {45, 8}, {29, 8}, {29, 8},
80 {38, 8}, {38, 8}, {26, 8}, {26, 8}, {37, 8}, {37, 8}, {25, 8}, {25, 8},
81 {43, 8}, {43, 8}, {23, 8}, {23, 8}, {51, 8}, {51, 8}, {15, 8}, {15, 8},
82 {42, 8}, {42, 8}, {22, 8}, {22, 8}, {50, 8}, {50, 8}, {14, 8}, {14, 8},
83 {41, 8}, {41, 8}, {21, 8}, {21, 8}, {49, 8}, {49, 8}, {13, 8}, {13, 8},
84 {35, 8}, {35, 8}, {19, 8}, {19, 8}, {11, 8}, {11, 8}, {7, 8}, {7, 8},
85 {34, 7}, {34, 7}, {34, 7}, {34, 7}, {18, 7}, {18, 7}, {18, 7}, {18, 7},
86 {10, 7}, {10, 7}, {10, 7}, {10, 7}, {6, 7}, {6, 7}, {6, 7}, {6, 7},
87 {33, 7}, {33, 7}, {33, 7}, {33, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7},
88 {9, 7}, {9, 7}, {9, 7}, {9, 7}, {5, 7}, {5, 7}, {5, 7}, {5, 7},
89 {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6},
90 {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6},
91 {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6},
92 {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6},
93 {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
94 {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
95 {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5},
96 {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5},
97 {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5},
98 {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5},
99 {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5},
100 {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5},
101 {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5},
102 {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5},
103 {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5},
104 {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5},
105 {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5},
106 {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5},
107 {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5},
108 {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5},
109 {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5},
110 {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5},
111 {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5},
112 {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5},
113 {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5},
114 {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5},
115 {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5},
116 {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5},
117 {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
118 {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
119 {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
120 {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4},
121 {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
122 {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
123 {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
124 {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4},
125 {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
126 {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
127 {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
128 {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
129 {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
130 {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
131 {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
132 {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
133 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
134 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
135 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
136 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
137 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
138 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
139 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3},
140 {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3} };
142 /* Tables for dc DCT coefficients
143 * Tables are cut in two parts to reduce memory occupation
146 /* Table B-12, dct_dc_size_luminance, codes 00XXX ... 11110 */
148 static lookup_t pl_dct_dc_lum_init_table_1[32] =
149 { {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
150 {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
151 {0, 3}, {0, 3}, {0, 3}, {0, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3},
152 {4, 3}, {4, 3}, {4, 3}, {4, 3}, {5, 4}, {5, 4}, {6, 5}, {MB_ERROR, 0}
155 static lookup_t ppl_dct_dc_init_table_1[2][32] =
156 { { {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
157 {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
158 {0, 3}, {0, 3}, {0, 3}, {0, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3},
159 {4, 3}, {4, 3}, {4, 3}, {4, 3}, {5, 4}, {5, 4}, {6, 5}, {MB_ERROR, 0}},
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}
167 /* Table B-12, dct_dc_size_luminance, codes 111110xxx ... 111111111 */
169 static lookup_t pl_dct_dc_lum_init_table_2[32] =
170 { {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6},
171 {8, 7}, {8, 7}, {8, 7}, {8, 7}, {9, 8}, {9, 8}, {10,9}, {11,9},
172 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
173 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
174 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
175 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}
178 static lookup_t ppl_dct_dc_init_table_2[2][32] =
179 { { {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6}, {7, 6},
180 {8, 7}, {8, 7}, {8, 7}, {8, 7}, {9, 8}, {9, 8}, {10,9}, {11,9},
181 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
182 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
183 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0},
184 {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}, {MB_ERROR, 0}},
185 { {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
186 {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
187 {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7},
188 {8, 8}, {8, 8}, {8, 8}, {8, 8}, {9, 9}, {9, 9}, {10,10}, {11,10}
192 /* Table B-13, dct_dc_size_chrominance, codes 00xxx ... 11110 */
193 static lookup_t pl_dct_dc_chrom_init_table_1[32] =
194 { {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2},
195 {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2},
196 {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2},
197 {3, 3}, {3, 3}, {3, 3}, {3, 3}, {4, 4}, {4, 4}, {5, 5}, {MB_ERROR, 0}
201 /* Table B-13, dct_dc_size_chrominance, codes 111110xxxx ... 1111111111 */
202 static lookup_t pl_dct_dc_chrom_init_table_2[32] =
203 { {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
204 {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6},
205 {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7}, {7, 7},
206 {8, 8}, {8, 8}, {8, 8}, {8, 8}, {9, 9}, {9, 9}, {10,10}, {11,10}
210 /* Tables for ac DCT coefficients. There are cut in many parts to save space */
211 /* Table B-14, DCT coefficients table zero,
212 * codes 0100 ... 1xxx (used for first (DC) coefficient)
214 static dct_lookup_t pl_DCT_tab_dc[12] =
216 {0,2,4}, {2,1,4}, {1,1,3}, {1,1,3},
217 {0,1,1}, {0,1,1}, {0,1,1}, {0,1,1},
218 {0,1,1}, {0,1,1}, {0,1,1}, {0,1,1}
221 /* Table B-14, DCT coefficients table zero,
222 * codes 0100 ... 1xxx (used for all other coefficients)
224 static dct_lookup_t pl_DCT_tab_ac[12] =
226 {0,2,4}, {2,1,4}, {1,1,3}, {1,1,3},
227 {DCT_EOB,0,2}, {DCT_EOB,0,2}, {DCT_EOB,0,2}, {DCT_EOB,0,2}, /* EOB */
228 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2}
231 /* Table B-14, DCT coefficients table zero,
232 * codes 000001xx ... 00111xxx
234 static dct_lookup_t pl_DCT_tab0[60] =
236 {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6}, {DCT_ESCAPE,0,6},
238 {2,2,7}, {2,2,7}, {9,1,7}, {9,1,7},
239 {0,4,7}, {0,4,7}, {8,1,7}, {8,1,7},
240 {7,1,6}, {7,1,6}, {7,1,6}, {7,1,6},
241 {6,1,6}, {6,1,6}, {6,1,6}, {6,1,6},
242 {1,2,6}, {1,2,6}, {1,2,6}, {1,2,6},
243 {5,1,6}, {5,1,6}, {5,1,6}, {5,1,6},
244 {13,1,8}, {0,6,8}, {12,1,8}, {11,1,8},
245 {3,2,8}, {1,3,8}, {0,5,8}, {10,1,8},
246 {0,3,5}, {0,3,5}, {0,3,5}, {0,3,5},
247 {0,3,5}, {0,3,5}, {0,3,5}, {0,3,5},
248 {4,1,5}, {4,1,5}, {4,1,5}, {4,1,5},
249 {4,1,5}, {4,1,5}, {4,1,5}, {4,1,5},
250 {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
251 {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5}
254 /* Table B-15, DCT coefficients table one,
255 * codes 000001xx ... 11111111
257 static dct_lookup_t pl_DCT_tab0a[252] =
259 {65,0,6}, {65,0,6}, {65,0,6}, {65,0,6}, /* Escape */
260 {7,1,7}, {7,1,7}, {8,1,7}, {8,1,7},
261 {6,1,7}, {6,1,7}, {2,2,7}, {2,2,7},
262 {0,7,6}, {0,7,6}, {0,7,6}, {0,7,6},
263 {0,6,6}, {0,6,6}, {0,6,6}, {0,6,6},
264 {4,1,6}, {4,1,6}, {4,1,6}, {4,1,6},
265 {5,1,6}, {5,1,6}, {5,1,6}, {5,1,6},
266 {1,5,8}, {11,1,8}, {0,11,8}, {0,10,8},
267 {13,1,8}, {12,1,8}, {3,2,8}, {1,4,8},
268 {2,1,5}, {2,1,5}, {2,1,5}, {2,1,5},
269 {2,1,5}, {2,1,5}, {2,1,5}, {2,1,5},
270 {1,2,5}, {1,2,5}, {1,2,5}, {1,2,5},
271 {1,2,5}, {1,2,5}, {1,2,5}, {1,2,5},
272 {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
273 {3,1,5}, {3,1,5}, {3,1,5}, {3,1,5},
274 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
275 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
276 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
277 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
278 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
279 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
280 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
281 {1,1,3}, {1,1,3}, {1,1,3}, {1,1,3},
282 {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4}, /* EOB */
283 {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
284 {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
285 {64,0,4}, {64,0,4}, {64,0,4}, {64,0,4},
286 {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
287 {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
288 {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
289 {0,3,4}, {0,3,4}, {0,3,4}, {0,3,4},
290 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
291 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
292 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
293 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
294 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
295 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
296 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
297 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
298 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
299 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
300 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
301 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
302 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
303 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
304 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
305 {0,1,2}, {0,1,2}, {0,1,2}, {0,1,2},
306 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
307 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
308 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
309 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
310 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
311 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
312 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
313 {0,2,3}, {0,2,3}, {0,2,3}, {0,2,3},
314 {0,4,5}, {0,4,5}, {0,4,5}, {0,4,5},
315 {0,4,5}, {0,4,5}, {0,4,5}, {0,4,5},
316 {0,5,5}, {0,5,5}, {0,5,5}, {0,5,5},
317 {0,5,5}, {0,5,5}, {0,5,5}, {0,5,5},
318 {9,1,7}, {9,1,7}, {1,3,7}, {1,3,7},
319 {10,1,7}, {10,1,7}, {0,8,7}, {0,8,7},
320 {0,9,7}, {0,9,7}, {0,12,8}, {0,13,8},
321 {2,3,8}, {4,2,8}, {0,14,8}, {0,15,8}
324 /* Table B-14, DCT coefficients table zero,
325 * codes 0000001000 ... 0000001111
327 static dct_lookup_t pl_DCT_tab1[8] =
329 {16,1,10}, {5,2,10}, {0,7,10}, {2,3,10},
330 {1,4,10}, {15,1,10}, {14,1,10}, {4,2,10}
333 /* Table B-15, DCT coefficients table one,
334 * codes 000000100x ... 000000111x
336 static dct_lookup_t pl_DCT_tab1a[8] =
338 {5,2,9}, {5,2,9}, {14,1,9}, {14,1,9},
339 {2,4,10}, {16,1,10}, {15,1,9}, {15,1,9}
342 /* Table B-14/15, DCT coefficients table zero / one,
343 * codes 000000010000 ... 000000011111
345 static dct_lookup_t pl_DCT_tab2[16] =
347 {0,11,12}, {8,2,12}, {4,3,12}, {0,10,12},
348 {2,4,12}, {7,2,12}, {21,1,12}, {20,1,12},
349 {0,9,12}, {19,1,12}, {18,1,12}, {1,5,12},
350 {3,3,12}, {0,8,12}, {6,2,12}, {17,1,12}
353 /* Table B-14/15, DCT coefficients table zero / one,
354 * codes 0000000010000 ... 0000000011111
356 static dct_lookup_t pl_DCT_tab3[16] =
358 {10,2,13}, {9,2,13}, {5,3,13}, {3,4,13},
359 {2,5,13}, {1,7,13}, {1,6,13}, {0,15,13},
360 {0,14,13}, {0,13,13}, {0,12,13}, {26,1,13},
361 {25,1,13}, {24,1,13}, {23,1,13}, {22,1,13}
364 /* Table B-14/15, DCT coefficients table zero / one,
365 * codes 00000000010000 ... 00000000011111
367 static dct_lookup_t pl_DCT_tab4[16] =
369 {0,31,14}, {0,30,14}, {0,29,14}, {0,28,14},
370 {0,27,14}, {0,26,14}, {0,25,14}, {0,24,14},
371 {0,23,14}, {0,22,14}, {0,21,14}, {0,20,14},
372 {0,19,14}, {0,18,14}, {0,17,14}, {0,16,14}
375 /* Table B-14/15, DCT coefficients table zero / one,
376 * codes 000000000010000 ... 000000000011111
378 static dct_lookup_t pl_DCT_tab5[16] =
380 {0,40,15}, {0,39,15}, {0,38,15}, {0,37,15},
381 {0,36,15}, {0,35,15}, {0,34,15}, {0,33,15},
382 {0,32,15}, {1,14,15}, {1,13,15}, {1,12,15},
383 {1,11,15}, {1,10,15}, {1,9,15}, {1,8,15}
386 /* Table B-14/15, DCT coefficients table zero / one,
387 * codes 0000000000010000 ... 0000000000011111
389 static dct_lookup_t pl_DCT_tab6[16] =
391 {1,18,16}, {1,17,16}, {1,16,16}, {1,15,16},
392 {6,3,16}, {16,2,16}, {15,2,16}, {14,2,16},
393 {13,2,16}, {12,2,16}, {11,2,16}, {31,1,16},
394 {30,1,16}, {29,1,16}, {28,1,16}, {27,1,16}
397 /* Pointers on tables of dct coefficients */
398 static dct_lookup_t * ppl_dct_tab1[2] = { pl_DCT_tab_ac, pl_DCT_tab0a };
400 static dct_lookup_t * ppl_dct_tab2[2] = { pl_DCT_tab_ac, pl_DCT_tab_dc };
403 /* Lookup Table for the chromatic component */
404 static int pi_cc_index[12] = { 0, 0, 0, 0, 1, 2, 1, 2, 1, 2 };
408 * Initialization of lookup tables
411 /*****************************************************************************
412 * vpar_InitCrop : Initialize the crop table for saturation
413 * (ISO/IEC 13818-2 section 7.4.3)
414 *****************************************************************************/
415 #if defined(MPEG2_COMPLIANT) && !defined(VDEC_DFT)
416 void vpar_InitCrop( vpar_thread_t * p_vpar )
420 p_vpar->pi_crop = p_vpar->pi_crop_buf + 4096;
422 for( i_dummy = -4096; i_dummy < -2048; i_dummy++ )
424 p_vpar->pi_crop[i_dummy] = -2048;
426 for( ; i_dummy < 2047; i_dummy++ )
428 p_vpar->pi_crop[i_dummy] = i_dummy;
430 for( ; i_dummy < 4095; i_dummy++ )
432 p_vpar->pi_crop[i_dummy] = 2047;
437 /*****************************************************************************
438 * vpar_InitMbAddrInc : Initialize the lookup table for mb_addr_inc
439 *****************************************************************************/
441 /* Function for filling up the lookup table for mb_addr_inc */
442 static void FillMbAddrIncTable( vpar_thread_t * p_vpar,
443 int i_start, int i_end, int i_step,
444 int * pi_value, int i_length )
447 for( i_pos = i_start ; i_pos < i_end ; i_pos += i_step )
449 for( i_offset = 0 ; i_offset < i_step ; i_offset ++ )
451 p_vpar->pl_mb_addr_inc[i_pos + i_offset].i_value = * pi_value;
452 p_vpar->pl_mb_addr_inc[i_pos + i_offset].i_length = i_length;
458 /* Function that initialize the table using the last one */
459 void vpar_InitMbAddrInc( vpar_thread_t * p_vpar )
464 for( i_dummy = 0 ; i_dummy < 8 ; i_dummy++ )
466 p_vpar->pl_mb_addr_inc[i_dummy].i_value = MB_ERROR;
467 p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
470 p_vpar->pl_mb_addr_inc[8].i_value = MB_ADDRINC_ESCAPE;
471 p_vpar->pl_mb_addr_inc[8].i_length = 11;
473 for( i_dummy = 9 ; i_dummy < 15 ; i_dummy ++ )
475 p_vpar->pl_mb_addr_inc[i_dummy].i_value = MB_ERROR;
476 p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
479 p_vpar->pl_mb_addr_inc[15].i_value = MB_ADDRINC_STUFFING;
480 p_vpar->pl_mb_addr_inc[15].i_length = 11;
482 for( i_dummy = 16; i_dummy < 24; i_dummy++ )
484 p_vpar->pl_mb_addr_inc[i_dummy].i_value = MB_ERROR;
485 p_vpar->pl_mb_addr_inc[i_dummy].i_length = 0;
490 FillMbAddrIncTable( p_vpar, 24, 36, 1, &i_value, 11 );
491 FillMbAddrIncTable( p_vpar, 36, 48, 2, &i_value, 10 );
492 FillMbAddrIncTable( p_vpar, 48, 96, 8, &i_value, 8 );
493 FillMbAddrIncTable( p_vpar, 96, 128, 16, &i_value, 7 );
494 FillMbAddrIncTable( p_vpar, 128, 256, 64, &i_value, 5 );
495 FillMbAddrIncTable( p_vpar, 256, 512, 128, &i_value, 4 );
496 FillMbAddrIncTable( p_vpar, 512, 1024, 256, &i_value, 3 );
497 FillMbAddrIncTable( p_vpar, 1024, 2048, 1024, &i_value, 1 );
500 /*****************************************************************************
501 * vpar_Init*MBType : Initialize lookup table for the Macroblock type
502 *****************************************************************************/
504 /* Fonction for filling up the tables */
505 static void FillMBType( vpar_thread_t * p_vpar,
514 for( i_dummy = i_start ; i_dummy < i_end ; i_dummy++ )
516 p_vpar->ppl_mb_type[i_mb_type][i_dummy].i_value = i_value;
517 p_vpar->ppl_mb_type[i_mb_type][i_dummy].i_length = i_length;
521 /* Fonction that fills the table for P MB_Type */
522 void vpar_InitPMBType( vpar_thread_t * p_vpar )
524 FillMBType( p_vpar, 0, 32, 64, MB_MOTION_FORWARD|MB_PATTERN, 1 );
525 FillMBType( p_vpar, 0, 16, 32, MB_PATTERN, 2 );
526 FillMBType( p_vpar, 0, 8, 16, MB_MOTION_FORWARD, 3 );
527 FillMBType( p_vpar, 0, 6, 8, MB_INTRA, 5 );
528 FillMBType( p_vpar, 0, 4, 6, MB_QUANT|MB_MOTION_FORWARD|MB_PATTERN, 5 );
529 FillMBType( p_vpar, 0, 2, 4, MB_QUANT|MB_PATTERN, 5 );
530 p_vpar->ppl_mb_type[0][1].i_value = MB_QUANT|MB_INTRA;
531 p_vpar->ppl_mb_type[0][1].i_length = 6;
532 p_vpar->ppl_mb_type[0][0].i_value = MB_ERROR;
533 p_vpar->ppl_mb_type[0][0].i_length = 0;
536 /* Fonction that fills the table for B MB_Type */
537 void vpar_InitBMBType( vpar_thread_t * p_vpar )
539 FillMBType( p_vpar, 1, 48, 64, MB_MOTION_FORWARD
540 |MB_MOTION_BACKWARD|MB_PATTERN, 2 );
541 FillMBType( p_vpar, 1, 32, 48, MB_MOTION_FORWARD|MB_MOTION_BACKWARD, 2 );
542 FillMBType( p_vpar, 1, 24, 32, MB_MOTION_BACKWARD|MB_PATTERN, 3 );
543 FillMBType( p_vpar, 1, 16, 24, MB_MOTION_BACKWARD, 3 );
544 FillMBType( p_vpar, 1, 12, 16, MB_MOTION_FORWARD|MB_PATTERN, 4 );
545 FillMBType( p_vpar, 1, 8, 12, MB_MOTION_FORWARD, 4 );
546 FillMBType( p_vpar, 1, 6, 8, MB_INTRA, 5 );
547 FillMBType( p_vpar, 1, 4, 6, MB_QUANT|MB_MOTION_FORWARD
548 |MB_MOTION_BACKWARD|MB_PATTERN, 5 );
549 p_vpar->ppl_mb_type[1][3].i_value = MB_QUANT|MB_MOTION_FORWARD|MB_PATTERN;
550 p_vpar->ppl_mb_type[1][3].i_length = 6;
551 p_vpar->ppl_mb_type[1][2].i_value = MB_QUANT|MB_MOTION_BACKWARD|MB_PATTERN;
552 p_vpar->ppl_mb_type[1][2].i_length = 6;
553 p_vpar->ppl_mb_type[1][1].i_value = MB_QUANT|MB_INTRA;
554 p_vpar->ppl_mb_type[1][1].i_length = 6;
555 p_vpar->ppl_mb_type[1][0].i_value =MB_ERROR;
556 p_vpar->ppl_mb_type[1][0].i_length = 0;
560 /*****************************************************************************
561 * vpar_InitDCTTables : Initialize tables giving the length of the dct
562 * coefficient from the vlc code
563 *****************************************************************************/
565 /* First fonction for filling the table */
566 static void FillDCTTable( dct_lookup_t * p_tab_dest, dct_lookup_t * p_tab_src,
567 int i_step, int i_nb_elem, int i_offset )
569 int i_dummy, i_dummy2;
571 for( i_dummy=0 ; i_dummy < i_nb_elem ; i_dummy++ )
573 for( i_dummy2=0 ; i_dummy2 < i_step ; i_dummy2++ )
575 p_tab_dest[(i_dummy+i_offset)*i_step+i_dummy2] = p_tab_src[i_dummy];
581 /* Fonction that actually fills the table or create the pointers */
582 void vpar_InitDCTTables( vpar_thread_t * p_vpar )
584 /* Tables are cut in two parts to reduce memory occupation */
585 p_vpar->pppl_dct_dc_size[0][0] = pl_dct_dc_lum_init_table_1;
586 p_vpar->pppl_dct_dc_size[0][1] = pl_dct_dc_lum_init_table_2;
587 p_vpar->pppl_dct_dc_size[1][0] = pl_dct_dc_chrom_init_table_1;
588 p_vpar->pppl_dct_dc_size[1][1] = pl_dct_dc_chrom_init_table_2;
590 /* XXX?? MB_ERROR is replaced by 0 because if we use -1 we
591 * can block in DecodeMPEG2Intra and others */
592 memset( p_vpar->ppl_dct_coef[0], 0, 16 );
593 memset( p_vpar->ppl_dct_coef[1], 0, 16 );
595 /* For table B14 & B15, we have a pointer to tables */
596 /* We fill the table thanks to the fonction defined above */
597 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab0, 256, 60, 4 );
598 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab1, 64, 8, 8 );
599 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab2, 16, 16, 16 );
600 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab3, 8, 16, 16 );
601 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab4, 4, 16, 16 );
602 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab5, 2, 16, 16 );
603 FillDCTTable( p_vpar->ppl_dct_coef[0], pl_DCT_tab6, 1, 16, 16 );
605 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab0a, 256, 60, 4 );
606 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab1a, 64, 8, 8 );
607 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab2, 16, 16, 16 );
608 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab3, 8, 16, 16 );
609 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab4, 4, 16, 16 );
610 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab5, 2, 16, 16 );
611 FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab6, 1, 16, 16 );
619 /*****************************************************************************
620 * DecodeMPEG1NonIntra : decode MPEG-1 non-intra blocks
621 *****************************************************************************/
622 static __inline__ void DecodeMPEG1NonIntra( vpar_thread_t * p_vpar,
623 macroblock_t * p_mb, int i_b,
624 int i_chroma_format )
639 /* Give the chromatic component (0, 1, 2) */
640 i_cc = pi_cc_index[i_b];
642 /* Determine whether it is luminance or not (chrominance) */
643 b_chroma = ( i_cc + 1 ) >> 1;
645 /* There should be no D picture in non-intra blocks */
646 if( p_vpar->picture.i_coding_type == D_CODING_TYPE )
647 intf_ErrMsg("vpar error : D-picture in non intra block\n");
649 /* Decoding of the AC coefficients */
655 for( i_parse = 0; !p_vpar->p_fifo->b_die; i_parse++ )
657 i_code = ShowBits( &p_vpar->bit_stream, 16 );
658 if( i_code >= 16384 )
660 b_dc = (i_parse == 0);
661 i_run = ppl_dct_tab2[b_dc][(i_code>>12)-4].i_run;
662 i_level = ppl_dct_tab2[b_dc][(i_code>>12)-4].i_level;
663 i_length = ppl_dct_tab2[b_dc][(i_code>>12)-4].i_length;
667 i_run = p_vpar->ppl_dct_coef[0][i_code].i_run;
668 i_length = p_vpar->ppl_dct_coef[0][i_code].i_length;
669 i_level = p_vpar->ppl_dct_coef[0][i_code].i_level;
672 RemoveBits( &p_vpar->bit_stream, i_length );
677 i_run = GetBits( &p_vpar->bit_stream, 6 );
678 i_level = GetBits( &p_vpar->bit_stream, 8 );
680 i_level = GetBits( &p_vpar->bit_stream, 8 );
681 else if (i_level == 128)
682 i_level = GetBits( &p_vpar->bit_stream, 8 ) - 256;
683 else if (i_level > 128)
686 if( (b_sign = i_level < 0) )
692 /* The MMX IDCT has a precision problem with non-intra
694 p_mb->ppi_blocks[i_b][0] += 4;
698 p_mb->pf_idct[i_b] = vdec_SparseIDCT;
699 p_mb->pi_sparse_pos[i_b] = i_coef;
703 p_mb->pf_idct[i_b] = vdec_IDCT;
709 b_sign = GetBits( &p_vpar->bit_stream, 1 );
720 i_pos = pi_scan[p_vpar->picture.b_alternate_scan][i_parse];
721 i_level = ( ((i_level << 1) + 1) * p_vpar->mb.i_quantizer_scale
722 * p_vpar->sequence.nonintra_quant.pi_matrix[i_pos] ) >> 4;
724 /* Mismatch control */
725 if( i_level ) /* Should always be true */
727 /* Equivalent to : if( (val & 1) == 0 ) val = val - 1; */
728 i_level = (i_level - 1) | 1;
731 p_mb->ppi_blocks[i_b][i_pos] = b_sign ? -i_level : i_level;
734 intf_ErrMsg("vpar error: DCT coeff (non-intra) is out of bounds\n");
735 p_vpar->picture.b_error = 1;
738 /*****************************************************************************
739 * DecodeMPEG1Intra : decode MPEG-1 intra blocks
740 *****************************************************************************/
741 static __inline__ void DecodeMPEG1Intra( vpar_thread_t * p_vpar,
742 macroblock_t * p_mb, int i_b ,
743 int i_chroma_format )
759 /* Give the chromatic component (0, 1, 2) */
760 i_cc = pi_cc_index[i_b];
762 /* Determine whether it is luminance or not (chrominance) */
763 b_chroma = ( i_cc + 1 ) >> 1;
766 i_code = ShowBits(&p_vpar->bit_stream, 5);
770 i_dct_dc_size = ppl_dct_dc_init_table_1[b_chroma][i_code].i_value;
771 i_length = ppl_dct_dc_init_table_1[b_chroma][i_code].i_length;
772 RemoveBits( &p_vpar->bit_stream, i_length);
776 i_code = ShowBits(&p_vpar->bit_stream, (9+b_chroma)) - (0x1f0 * (b_chroma + 1));
777 i_dct_dc_size = ppl_dct_dc_init_table_2[b_chroma][i_code].i_value;
778 i_length = ppl_dct_dc_init_table_2[b_chroma][i_code].i_length;
779 RemoveBits( &p_vpar->bit_stream, i_length);
782 if (i_dct_dc_size == 0)
786 i_dct_dc_diff = GetBits( &p_vpar->bit_stream, i_dct_dc_size);
787 if ((i_dct_dc_diff & (1<<(i_dct_dc_size-1))) == 0)
788 i_dct_dc_diff -= (1<<i_dct_dc_size) - 1;
791 /* Read the actual code with the good length */
792 p_vpar->mb.pi_dc_dct_pred[i_cc] += i_dct_dc_diff;
794 p_mb->ppi_blocks[i_b][0] = p_vpar->mb.pi_dc_dct_pred[i_cc] << 3;
796 i_nc = ( p_vpar->mb.pi_dc_dct_pred[i_cc] != 0 );
799 if( p_vpar->picture.i_coding_type == D_CODING_TYPE )
801 /* Remove end_of_macroblock (always 1, prevents startcode emulation)
802 * ISO/IEC 11172-2 section 2.4.2.7 and 2.4.3.6 */
803 RemoveBits( &p_vpar->bit_stream, 1 );
804 /* D pictures do not have AC coefficients */
809 /* Decoding of the AC coefficients */
813 for( i_parse = 1; !p_vpar->p_fifo->b_die/*i_parse < 64*/; i_parse++ )
815 i_code = ShowBits( &p_vpar->bit_stream, 16 );
816 /* We use 2 main tables for the coefficients */
817 if( i_code >= 16384 )
819 i_run = ppl_dct_tab1[0][(i_code>>12)-4].i_run;
820 i_level = ppl_dct_tab1[0][(i_code>>12)-4].i_level;
821 i_length = ppl_dct_tab1[0][(i_code>>12)-4].i_length;
825 i_run = p_vpar->ppl_dct_coef[0][i_code].i_run;
826 i_length = p_vpar->ppl_dct_coef[0][i_code].i_length;
827 i_level = p_vpar->ppl_dct_coef[0][i_code].i_level;
830 RemoveBits( &p_vpar->bit_stream, i_length );
835 i_run = GetBits( &p_vpar->bit_stream, 6 );
836 i_level = GetBits( &p_vpar->bit_stream, 8 );
838 i_level = GetBits( &p_vpar->bit_stream, 8 );
839 else if (i_level == 128)
840 i_level = GetBits( &p_vpar->bit_stream, 8 ) - 256;
841 else if (i_level > 128)
843 if( (b_sign = i_level < 0) )
849 p_mb->pf_idct[i_b] = vdec_SparseIDCT;
850 p_mb->pi_sparse_pos[i_b] = i_coef;
854 p_mb->pf_idct[i_b] = vdec_IDCT;
860 b_sign = GetBits( &p_vpar->bit_stream, 1 );
863 /* Prepare the next block */
870 /* We have an error in the stream */
874 /* Determine the position of the block in the frame */
875 i_pos = pi_scan[p_vpar->picture.b_alternate_scan][i_parse];
876 i_level = ( i_level *
877 p_vpar->mb.i_quantizer_scale *
878 p_vpar->sequence.intra_quant.pi_matrix[i_pos] ) >> 3;
880 /* Mismatch control */
881 if( i_level ) /* Should always be true */
883 /* Equivalent to : if( (val & 1) == 0 ) val = val - 1; */
884 i_level = (i_level - 1) | 1;
887 p_mb->ppi_blocks[i_b][i_pos] = b_sign ? -i_level : i_level;
890 intf_ErrMsg("vpar error: DCT coeff (intra) is out of bounds\n");
891 p_vpar->picture.b_error = 1;
894 /*****************************************************************************
895 * DecodeMPEG2NonIntra : decode MPEG-2 non-intra blocks
896 *****************************************************************************/
897 static __inline__ void DecodeMPEG2NonIntra( vpar_thread_t * p_vpar,
898 macroblock_t * p_mb, int i_b,
899 int i_chroma_format )
915 /* Give the chromatic component (0, 1, 2) */
916 i_cc = pi_cc_index[i_b];
918 /* Determine whether it is luminance or not (chrominance) */
919 b_chroma = ( i_cc + 1 ) >> 1;
921 /* Give a pointer to the quantization matrices for intra blocks */
922 if( (i_chroma_format == CHROMA_420) || (!b_chroma) )
924 pi_quant = p_vpar->sequence.nonintra_quant.pi_matrix;
928 pi_quant = p_vpar->sequence.chroma_nonintra_quant.pi_matrix;
931 /* Decoding of the AC coefficients */
935 for( i_parse = 0; !p_vpar->p_fifo->b_die; i_parse++ )
937 i_code = ShowBits( &p_vpar->bit_stream, 16 );
938 if( i_code >= 16384 )
940 b_dc = (i_parse == 0);
941 i_run = ppl_dct_tab2[b_dc][(i_code>>12)-4].i_run;
942 i_level = ppl_dct_tab2[b_dc][(i_code>>12)-4].i_level;
943 i_length = ppl_dct_tab2[b_dc][(i_code>>12)-4].i_length;
947 i_run = p_vpar->ppl_dct_coef[0][i_code].i_run;
948 i_length = p_vpar->ppl_dct_coef[0][i_code].i_length;
949 i_level = p_vpar->ppl_dct_coef[0][i_code].i_level;
953 RemoveBits( &p_vpar->bit_stream, i_length );
958 i_run = GetBits( &p_vpar->bit_stream, 6 );
959 i_level = GetBits( &p_vpar->bit_stream, 12 );
960 i_level = (b_sign = ( i_level > 2047 )) ? 4096 - i_level
965 /* The MMX IDCT has a precision problem with non-intra
967 p_mb->ppi_blocks[i_b][0] += 4;
971 p_mb->pf_idct[i_b] = vdec_SparseIDCT;
972 p_mb->pi_sparse_pos[i_b] = i_coef;
976 p_mb->pf_idct[i_b] = vdec_IDCT;
982 b_sign = GetBits( &p_vpar->bit_stream, 1 );
993 i_pos = pi_scan[p_vpar->picture.b_alternate_scan][i_parse];
994 i_level = ( ((i_level << 1) + 1) * p_vpar->mb.i_quantizer_scale
995 * pi_quant[i_pos] ) >> 5;
996 p_mb->ppi_blocks[i_b][i_pos] = b_sign ? -i_level : i_level;
999 intf_ErrMsg("vpar error: DCT coeff (non-intra) is out of bounds\n");
1000 p_vpar->picture.b_error = 1;
1003 /*****************************************************************************
1004 * DecodeMPEG2Intra : decode MPEG-2 intra blocks
1005 *****************************************************************************/
1006 static __inline__ void DecodeMPEG2Intra( vpar_thread_t * p_vpar,
1007 macroblock_t * p_mb, int i_b,
1008 int i_chroma_format )
1021 boolean_t b_vlc_intra;
1026 /* Give the chromatic component (0, 1, 2) */
1027 i_cc = pi_cc_index[i_b];
1029 /* Determine whether it is luminance or not (chrominance) */
1030 b_chroma = ( i_cc + 1 ) >> 1;
1032 /* Give a pointer to the quantization matrices for intra blocks */
1033 if( (i_chroma_format == CHROMA_420) || (!b_chroma) )
1035 pi_quant = p_vpar->sequence.intra_quant.pi_matrix;
1039 pi_quant = p_vpar->sequence.chroma_intra_quant.pi_matrix;
1043 i_code = ShowBits(&p_vpar->bit_stream, 5);
1047 i_dct_dc_size = ppl_dct_dc_init_table_1[b_chroma][i_code].i_value;
1048 i_length = ppl_dct_dc_init_table_1[b_chroma][i_code].i_length;
1049 RemoveBits( &p_vpar->bit_stream, i_length);
1053 i_code = ShowBits(&p_vpar->bit_stream, (9+b_chroma)) - (0x1f0 * (b_chroma + 1));
1054 i_dct_dc_size = ppl_dct_dc_init_table_2[b_chroma][i_code].i_value;
1055 i_length = ppl_dct_dc_init_table_2[b_chroma][i_code].i_length;
1056 RemoveBits( &p_vpar->bit_stream, i_length);
1059 if (i_dct_dc_size == 0)
1063 i_dct_dc_diff = GetBits( &p_vpar->bit_stream, i_dct_dc_size);
1064 if ((i_dct_dc_diff & (1<<(i_dct_dc_size-1))) == 0)
1065 i_dct_dc_diff -= (1<<i_dct_dc_size) - 1;
1068 /* Read the actual code with the good length */
1069 p_vpar->mb.pi_dc_dct_pred[i_cc] += i_dct_dc_diff;
1071 p_mb->ppi_blocks[i_b][0] = ( p_vpar->mb.pi_dc_dct_pred[i_cc] <<
1072 ( 3 - p_vpar->picture.i_intra_dc_precision ) );
1074 i_nc = ( p_vpar->mb.pi_dc_dct_pred[i_cc] != 0 );
1077 /* Decoding of the AC coefficients */
1080 b_vlc_intra = p_vpar->picture.b_intra_vlc_format;
1081 for( i_parse = 1; !p_vpar->p_fifo->b_die/*i_parse < 64*/; i_parse++ )
1083 i_code = ShowBits( &p_vpar->bit_stream, 16 );
1084 /* We use 2 main tables for the coefficients */
1085 if( i_code >= 16384 )
1087 i_run = ppl_dct_tab1[b_vlc_intra][(i_code>>(12-(4*b_vlc_intra)))-4].i_run;
1088 i_level = ppl_dct_tab1[b_vlc_intra][(i_code>>(12-(4*b_vlc_intra)))-4].i_level;
1089 i_length = ppl_dct_tab1[b_vlc_intra][(i_code>>(12-(4*b_vlc_intra)))-4].i_length;
1093 i_run = p_vpar->ppl_dct_coef[b_vlc_intra][i_code].i_run;
1094 i_length = p_vpar->ppl_dct_coef[b_vlc_intra][i_code].i_length;
1095 i_level = p_vpar->ppl_dct_coef[b_vlc_intra][i_code].i_level;
1098 RemoveBits( &p_vpar->bit_stream, i_length );
1103 i_run = GetBits( &p_vpar->bit_stream, 6 );
1104 i_level = GetBits( &p_vpar->bit_stream, 12 );
1105 i_level = (b_sign = ( i_level > 2047 )) ? 4096 - i_level
1111 p_mb->pf_idct[i_b] = vdec_SparseIDCT;
1112 p_mb->pi_sparse_pos[i_b] = i_coef;
1116 p_mb->pf_idct[i_b] = vdec_IDCT;
1122 b_sign = GetBits( &p_vpar->bit_stream, 1 );
1125 /* Prepare the next block */
1132 /* We have an error in the stream */
1136 /* Determine the position of the block in the frame */
1137 i_pos = pi_scan[p_vpar->picture.b_alternate_scan][i_parse];
1138 i_level = ( i_level *
1139 p_vpar->mb.i_quantizer_scale *
1140 pi_quant[i_pos] ) >> 4;
1141 p_mb->ppi_blocks[i_b][i_pos] = b_sign ? -i_level : i_level;
1144 intf_ErrMsg("vpar error: DCT coeff (intra) is out of bounds\n");
1145 p_vpar->picture.b_error = 1;
1153 /****************************************************************************
1154 * MotionCode : Parse the next motion code
1155 ****************************************************************************/
1156 static __inline__ int MotionCode( vpar_thread_t * p_vpar )
1159 static lookup_t pl_mv_tab0[8] =
1160 { {-1,0}, {3,3}, {2,2}, {2,2}, {1,1}, {1,1}, {1,1}, {1,1} };
1161 /* Table B-10, motion_code, codes 0000011 ... 000011x */
1162 static lookup_t pl_mv_tab1[8] =
1163 { {-1,0}, {-1,0}, {-1,0}, {7,6}, {6,6}, {5,6}, {4,5}, {4,5} };
1164 /* Table B-10, motion_code, codes 0000001100 ... 000001011x */
1165 static lookup_t pl_mv_tab2[12] = {
1166 {16,9}, {15,9}, {14,9}, {13,9},
1167 {12,9}, {11,9}, {10,8}, {10,8},
1168 {9,8}, {9,8}, {8,8}, {8,8} };
1170 if( GetBits( &p_vpar->bit_stream, 1 ) )
1174 if( (i_code = ShowBits( &p_vpar->bit_stream, 9) ) >= 64 )
1177 RemoveBits( &p_vpar->bit_stream, pl_mv_tab0[i_code].i_length );
1178 return( GetBits( &p_vpar->bit_stream, 1 ) ?
1179 -pl_mv_tab0[i_code].i_value : pl_mv_tab0[i_code].i_value );
1185 RemoveBits( &p_vpar->bit_stream, pl_mv_tab1[i_code].i_length );
1186 return( GetBits( &p_vpar->bit_stream, 1 ) ?
1187 -pl_mv_tab1[i_code].i_value : pl_mv_tab1[i_code].i_value );
1190 if( (i_code -= 12) < 0 )
1192 p_vpar->picture.b_error = 1;
1193 intf_DbgMsg( "vpar debug: Invalid motion_vector code\n" );
1197 RemoveBits( &p_vpar->bit_stream, pl_mv_tab2[i_code].i_length );
1198 return( GetBits( &p_vpar->bit_stream, 1 ) ?
1199 -pl_mv_tab2[i_code].i_value : pl_mv_tab2[i_code].i_value );
1202 /****************************************************************************
1203 * DecodeMotionVector : Decode a motion_vector
1204 ****************************************************************************/
1205 static __inline__ void DecodeMotionVector( int * pi_prediction, int i_r_size,
1206 int i_motion_code, int i_motion_residual, int i_full_pel )
1208 int i_limit, i_vector;
1210 /* ISO/IEC 13818-1 section 7.6.3.1 */
1211 i_limit = 16 << i_r_size;
1212 i_vector = *pi_prediction >> i_full_pel;
1214 if( i_motion_code > 0 )
1216 i_vector += ((i_motion_code-1) << i_r_size) + i_motion_residual + 1;
1217 if( i_vector >= i_limit )
1218 i_vector -= i_limit + i_limit;
1220 else if( i_motion_code < 0 )
1222 i_vector -= ((-i_motion_code-1) << i_r_size) + i_motion_residual + 1;
1223 if( i_vector < -i_limit )
1224 i_vector += i_limit + i_limit;
1226 *pi_prediction = i_vector << i_full_pel;
1229 /****************************************************************************
1230 * MotionVector : Parse the next motion_vector field
1231 ****************************************************************************/
1232 static __inline__ void MotionVector( vpar_thread_t * p_vpar,
1233 macroblock_t * p_mb, int i_r,
1234 int i_s, int i_full_pel, int i_structure,
1235 int i_h_r_size, int i_v_r_size )
1237 int i_motion_code, i_motion_residual;
1238 int pi_dm_vector[2];
1240 i_motion_code = MotionCode( p_vpar );
1241 i_motion_residual = (i_h_r_size != 0 && i_motion_code != 0) ?
1242 GetBits( &p_vpar->bit_stream, i_h_r_size) : 0;
1243 DecodeMotionVector( &p_vpar->mb.pppi_pmv[i_r][i_s][0], i_h_r_size,
1244 i_motion_code, i_motion_residual, i_full_pel );
1245 p_mb->pppi_motion_vectors[i_r][i_s][0] = p_vpar->mb.pppi_pmv[i_r][i_s][0];
1247 if( p_vpar->mb.b_dmv )
1249 if( GetBits(&p_vpar->bit_stream, 1) )
1251 pi_dm_vector[0] = GetBits( &p_vpar->bit_stream, 1 ) ? -1 : 1;
1255 pi_dm_vector[0] = 0;
1259 i_motion_code = MotionCode( p_vpar );
1260 i_motion_residual = (i_v_r_size != 0 && i_motion_code != 0) ?
1261 GetBits( &p_vpar->bit_stream, i_v_r_size) : 0;
1264 if( (p_vpar->mb.i_mv_format == MOTION_FIELD)
1265 && (i_structure == FRAME_STRUCTURE) )
1267 p_vpar->mb.pppi_pmv[i_r][i_s][1] >>= 1;
1270 DecodeMotionVector( &p_vpar->mb.pppi_pmv[i_r][i_s][1], i_v_r_size,
1271 i_motion_code, i_motion_residual, i_full_pel );
1273 if( (p_vpar->mb.i_mv_format == MOTION_FIELD)
1274 && (i_structure == FRAME_STRUCTURE) )
1275 p_vpar->mb.pppi_pmv[i_r][i_s][1] <<= 1;
1277 p_mb->pppi_motion_vectors[i_r][i_s][1] = p_vpar->mb.pppi_pmv[i_r][i_s][1];
1279 if( p_vpar->mb.b_dmv )
1281 if( GetBits(&p_vpar->bit_stream, 1) )
1283 pi_dm_vector[1] = GetBits( &p_vpar->bit_stream, 1 ) ? -1 : 1;
1287 pi_dm_vector[1] = 0;
1290 /* Dual Prime Arithmetic (ISO/IEC 13818-2 section 7.6.3.6). */
1292 #define i_mv_x p_mb->pppi_motion_vectors[0][0][0]
1293 if( i_structure == FRAME_STRUCTURE )
1295 #define i_mv_y (p_mb->pppi_motion_vectors[0][0][1] << 1)
1296 if( p_vpar->picture.b_top_field_first )
1298 /* vector for prediction of top field from bottom field */
1299 p_mb->ppi_dmv[0][0] = ((i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1300 p_mb->ppi_dmv[0][1] = ((i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] - 1;
1302 /* vector for prediction of bottom field from top field */
1303 p_mb->ppi_dmv[1][0] = ((3*i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1304 p_mb->ppi_dmv[1][1] = ((3*i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] + 1;
1308 /* vector for prediction of top field from bottom field */
1309 p_mb->ppi_dmv[0][0] = ((3*i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1310 p_mb->ppi_dmv[0][1] = ((3*i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] - 1;
1312 /* vector for prediction of bottom field from top field */
1313 p_mb->ppi_dmv[1][0] = ((i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1314 p_mb->ppi_dmv[1][1] = ((i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] + 1;
1320 #define i_mv_y p_mb->pppi_motion_vectors[0][0][1]
1321 /* vector for prediction from field of opposite 'parity' */
1322 p_mb->ppi_dmv[0][0] = ((i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1323 p_mb->ppi_dmv[0][1] = ((i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1];
1325 /* correct for vertical field shift */
1326 if( p_vpar->picture.i_structure == TOP_FIELD )
1327 p_mb->ppi_dmv[0][1]--;
1329 p_mb->ppi_dmv[0][1]++;
1336 /*****************************************************************************
1337 * DecodeMVMPEG1 : Parse the next MPEG-1 motion vectors
1338 *****************************************************************************/
1339 static void DecodeMVMPEG1( vpar_thread_t * p_vpar,
1340 macroblock_t * p_mb, int i_s, int i_structure )
1342 int i_r_size = i_s ? p_vpar->picture.i_backward_f_code - 1 :
1343 p_vpar->picture.i_forward_f_code - 1;
1344 MotionVector( p_vpar, p_mb, 0, i_s,
1345 p_vpar->picture.pb_full_pel_vector[i_s], FRAME_STRUCTURE,
1346 i_r_size, i_r_size );
1349 /*****************************************************************************
1350 * DecodeMVMPEG2 : Parse the next MPEG-2 motion_vectors field
1351 *****************************************************************************/
1352 static void DecodeMVMPEG2( vpar_thread_t * p_vpar,
1353 macroblock_t * p_mb, int i_s, int i_structure )
1355 if( p_vpar->mb.i_mv_count == 1 )
1357 if( p_vpar->mb.i_mv_format == MOTION_FIELD && !p_vpar->mb.b_dmv )
1359 p_mb->ppi_field_select[0][i_s] = p_mb->ppi_field_select[1][i_s]
1360 = GetBits( &p_vpar->bit_stream, 1 );
1362 MotionVector( p_vpar, p_mb, 0, i_s, 0, i_structure,
1363 p_vpar->picture.ppi_f_code[i_s][0] - 1,
1364 p_vpar->picture.ppi_f_code[i_s][1] - 1 );
1365 p_vpar->mb.pppi_pmv[1][i_s][0] = p_vpar->mb.pppi_pmv[0][i_s][0];
1366 p_vpar->mb.pppi_pmv[1][i_s][1] = p_vpar->mb.pppi_pmv[0][i_s][1];
1367 p_mb->pppi_motion_vectors[1][i_s][0] = p_vpar->mb.pppi_pmv[0][i_s][0];
1368 p_mb->pppi_motion_vectors[1][i_s][1] = p_vpar->mb.pppi_pmv[0][i_s][1];
1372 p_mb->ppi_field_select[0][i_s] = GetBits( &p_vpar->bit_stream, 1 );
1373 MotionVector( p_vpar, p_mb, 0, i_s, 0, i_structure,
1374 p_vpar->picture.ppi_f_code[i_s][0] - 1,
1375 p_vpar->picture.ppi_f_code[i_s][1] - 1 );
1376 p_mb->ppi_field_select[1][i_s] = GetBits( &p_vpar->bit_stream, 1 );
1377 MotionVector( p_vpar, p_mb, 1, i_s, 0, i_structure,
1378 p_vpar->picture.ppi_f_code[i_s][0] - 1,
1379 p_vpar->picture.ppi_f_code[i_s][1] - 1 );
1385 * Macroblock information structures
1388 /*****************************************************************************
1389 * MacroblockAddressIncrement : Get the macroblock_address_increment field
1390 *****************************************************************************/
1391 static int MacroblockAddressIncrement( vpar_thread_t * p_vpar )
1394 /* Index in the lookup table mb_addr_inc */
1395 int i_index = ShowBits( &p_vpar->bit_stream, 11 );
1397 /* Test the presence of the escape character */
1398 while( i_index == 8 )
1400 RemoveBits( &p_vpar->bit_stream, 11 );
1402 i_index = ShowBits( &p_vpar->bit_stream, 11 );
1405 /* Affect the value from the lookup table */
1406 i_addr_inc += p_vpar->pl_mb_addr_inc[i_index].i_value;
1408 /* Dump the good number of bits */
1409 RemoveBits( &p_vpar->bit_stream, p_vpar->pl_mb_addr_inc[i_index].i_length );
1414 /*****************************************************************************
1415 * IMBType : macroblock_type in I pictures
1416 *****************************************************************************/
1417 static __inline__ int IMBType( vpar_thread_t * p_vpar )
1419 /* Take two bits for testing */
1420 int i_type = ShowBits( &p_vpar->bit_stream, 2 );
1422 /* Lookup table for macroblock_type */
1423 static lookup_t pl_mb_Itype[4] = { {MB_ERROR, 0},
1424 {MB_QUANT|MB_INTRA, 2},
1427 /* Dump the good number of bits */
1428 RemoveBits( &p_vpar->bit_stream, pl_mb_Itype[i_type].i_length );
1429 return pl_mb_Itype[i_type].i_value;
1432 /*****************************************************************************
1433 * PMBType : macroblock_type in P pictures
1434 *****************************************************************************/
1435 static __inline__ int PMBType( vpar_thread_t * p_vpar )
1437 /* Testing on 6 bits */
1438 int i_type = ShowBits( &p_vpar->bit_stream, 6 );
1440 /* Dump the good number of bits */
1441 RemoveBits( &p_vpar->bit_stream, p_vpar->ppl_mb_type[0][i_type].i_length );
1442 /* return the value from the lookup table for P type */
1443 return p_vpar->ppl_mb_type[0][i_type].i_value;
1446 /*****************************************************************************
1447 * BMBType : macroblock_type in B pictures
1448 *****************************************************************************/
1449 static __inline__ int BMBType( vpar_thread_t * p_vpar )
1451 /* Testing on 6 bits */
1452 int i_type = ShowBits( &p_vpar->bit_stream, 6 );
1454 /* Dump the good number of bits */
1455 RemoveBits( &p_vpar->bit_stream, p_vpar->ppl_mb_type[1][i_type].i_length );
1457 /* return the value from the lookup table for B type */
1458 return p_vpar->ppl_mb_type[1][i_type].i_value;
1461 /*****************************************************************************
1462 * DMBType : macroblock_type in D pictures
1463 *****************************************************************************/
1464 static __inline__ int DMBType( vpar_thread_t * p_vpar )
1466 return GetBits( &p_vpar->bit_stream, 1 );
1469 /*****************************************************************************
1470 * CodedPattern420 : coded_block_pattern with 4:2:0 chroma
1471 *****************************************************************************/
1472 static __inline__ int CodedPattern420( vpar_thread_t * p_vpar )
1474 /* Take the max 9 bits length vlc code for testing */
1475 int i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1477 /* Trash the good number of bits read in the lookup table */
1478 RemoveBits( &p_vpar->bit_stream, pl_coded_pattern[i_vlc].i_length );
1480 /* return the value from the vlc table */
1481 return pl_coded_pattern[i_vlc].i_value;
1484 /*****************************************************************************
1485 * CodedPattern422 : coded_block_pattern with 4:2:2 chroma
1486 *****************************************************************************/
1487 static __inline__ int CodedPattern422( vpar_thread_t * p_vpar )
1489 int i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1491 RemoveBits( &p_vpar->bit_stream, pl_coded_pattern[i_vlc].i_length );
1493 /* Supplementary 2 bits long code for 4:2:2 format */
1494 return pl_coded_pattern[i_vlc].i_value |
1495 (GetBits( &p_vpar->bit_stream, 2 ) << 6);
1498 /*****************************************************************************
1499 * CodedPattern444 : coded_block_pattern with 4:4:4 chroma
1500 *****************************************************************************/
1501 static __inline__ int CodedPattern444( vpar_thread_t * p_vpar )
1503 int i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1505 RemoveBits( &p_vpar->bit_stream, pl_coded_pattern[i_vlc].i_length );
1507 return pl_coded_pattern[i_vlc].i_value |
1508 (GetBits( &p_vpar->bit_stream, 6 ) << 6);
1511 /*****************************************************************************
1512 * InitMacroblock : Initialize macroblock values
1513 *****************************************************************************/
1514 static __inline__ void InitMacroblock( vpar_thread_t * p_vpar,
1515 macroblock_t * p_mb, int i_coding_type,
1516 int i_chroma_format,
1518 boolean_t b_second_field )
1520 p_mb->i_chroma_nb_blocks = 1 << i_chroma_format;
1521 p_mb->p_picture = p_vpar->picture.p_picture;
1523 if( i_coding_type == B_CODING_TYPE )
1524 p_mb->p_backward = p_vpar->sequence.p_backward;
1526 p_mb->p_backward = NULL;
1527 if( (i_coding_type == P_CODING_TYPE) || (i_coding_type == B_CODING_TYPE) )
1528 p_mb->p_forward = p_vpar->sequence.p_forward;
1530 p_mb->p_forward = NULL;
1532 p_mb->i_l_x = p_vpar->mb.i_l_x;
1533 p_mb->i_c_x = p_vpar->mb.i_c_x;
1534 p_mb->i_motion_l_y = p_vpar->mb.i_l_y;
1535 p_mb->i_motion_c_y = p_vpar->mb.i_c_y;
1536 if( (p_mb->b_motion_field = (i_structure == BOTTOM_FIELD)) )
1538 p_mb->i_motion_l_y--;
1539 p_mb->i_motion_c_y--;
1541 p_mb->i_addb_l_stride = (p_mb->i_l_stride = p_vpar->picture.i_l_stride) - 8;
1542 p_mb->i_addb_c_stride = (p_mb->i_c_stride = p_vpar->picture.i_c_stride) - 8;
1543 p_mb->b_P_second = ( b_second_field && i_coding_type == P_CODING_TYPE );
1546 /*****************************************************************************
1547 * UpdateContext : Update the p_vpar contextual values
1548 *****************************************************************************/
1549 static __inline__ void UpdateContext( vpar_thread_t * p_vpar, int i_structure )
1551 /* Update macroblock real position. */
1552 p_vpar->mb.i_l_x += 16;
1553 p_vpar->mb.i_l_y += (p_vpar->mb.i_l_x / p_vpar->sequence.i_width)
1554 * (2 - (i_structure == FRAME_STRUCTURE)) * 16;
1555 p_vpar->mb.i_l_x %= p_vpar->sequence.i_width;
1557 p_vpar->mb.i_c_x += p_vpar->sequence.i_chroma_mb_width;
1558 p_vpar->mb.i_c_y += (p_vpar->mb.i_c_x / p_vpar->sequence.i_chroma_width)
1559 * (2 - (i_structure == FRAME_STRUCTURE))
1560 * p_vpar->sequence.i_chroma_mb_height;
1561 p_vpar->mb.i_c_x %= p_vpar->sequence.i_chroma_width;
1564 /*****************************************************************************
1565 * SkippedMacroblock : Generate a skipped macroblock with NULL motion vector
1566 *****************************************************************************/
1567 static __inline__ void SkippedMacroblock( vpar_thread_t * p_vpar, int i_mb,
1568 int i_mb_base, int i_coding_type,
1569 int i_chroma_format,
1571 boolean_t b_second_field )
1573 macroblock_t * p_mb;
1575 static f_motion_t pf_motion_skipped[4][4] =
1577 {NULL, NULL, NULL, NULL},
1578 {NULL, vdec_MotionFieldField420, vdec_MotionFieldField420,
1579 vdec_MotionFrameFrame420},
1580 {NULL, vdec_MotionFieldField422, vdec_MotionFieldField422,
1581 vdec_MotionFrameFrame422},
1582 {NULL, vdec_MotionFieldField444, vdec_MotionFieldField444,
1583 vdec_MotionFrameFrame444},
1586 if( i_coding_type == I_CODING_TYPE )
1588 intf_DbgMsg("vpar error: skipped macroblock in I-picture\n");
1589 p_vpar->picture.b_error = 1;
1593 if( (p_mb = vpar_NewMacroblock( &p_vpar->vfifo )) == NULL )
1599 p_vpar->picture.pp_mb[i_mb_base + i_mb] = p_mb;
1602 InitMacroblock( p_vpar, p_mb, i_coding_type, i_chroma_format,
1603 i_structure, b_second_field );
1605 /* Motion type is picture structure. */
1606 p_mb->pf_motion = pf_motion_skipped[i_chroma_format]
1608 p_mb->i_coded_block_pattern = 0;
1610 /* Motion direction and motion vectors depend on the coding type. */
1611 if( i_coding_type == B_CODING_TYPE )
1614 p_mb->i_mb_type = p_vpar->mb.i_motion_dir;
1615 for( i = 0; i < 2; i++ )
1616 for( j = 0; j < 2; j++ )
1617 for( k = 0; k < 2; k++ )
1618 p_mb->pppi_motion_vectors[i][j][k] = p_vpar->mb.pppi_pmv[i][j][k];
1620 else if( i_coding_type == P_CODING_TYPE )
1622 p_mb->i_mb_type = MB_MOTION_FORWARD;
1623 memset( p_mb->pppi_motion_vectors, 0, 8*sizeof(int) );
1626 /* Set the field we use for motion compensation */
1627 p_mb->ppi_field_select[0][0] = p_mb->ppi_field_select[0][1]
1628 = ( i_structure == BOTTOM_FIELD );
1630 UpdateContext( p_vpar, i_structure );
1633 /* Decode the macroblock NOW ! */
1634 vpar_DecodeMacroblock ( &p_vpar->vfifo, p_mb );
1638 /*****************************************************************************
1639 * MacroblockModes : Get the macroblock_modes structure
1640 *****************************************************************************/
1641 static __inline__ void MacroblockModes( vpar_thread_t * p_vpar,
1642 macroblock_t * p_mb,
1643 int i_chroma_format,
1647 static int ppi_mv_count[2][4] = { {0, 1, 2, 1}, {0, 2, 1, 1} };
1648 static int ppi_mv_format[2][4] = { {0, 1, 1, 1}, {0, 1, 2, 1} };
1650 /* Get macroblock_type. */
1651 switch( i_coding_type )
1654 p_mb->i_mb_type = PMBType( p_vpar );
1657 p_mb->i_mb_type = BMBType( p_vpar );
1660 p_mb->i_mb_type = IMBType( p_vpar );
1663 p_mb->i_mb_type = DMBType( p_vpar );
1666 if( i_coding_type == B_CODING_TYPE )
1668 /* We need to remember the motion direction of the last macroblock
1669 * before a skipped macroblock (ISO/IEC 13818-2 7.6.6) */
1670 p_vpar->mb.i_motion_dir = p_mb->i_mb_type
1671 & (MB_MOTION_FORWARD | MB_MOTION_BACKWARD);
1674 /* SCALABILITY : warning, we don't know if spatial_temporal_weight_code
1675 * has to be dropped, take care if you use scalable streams. */
1676 /* RemoveBits( &p_vpar->bit_stream, 2 ); */
1678 if( (i_coding_type == P_CODING_TYPE || i_coding_type == B_CODING_TYPE)
1679 && (p_mb->i_mb_type & (MB_MOTION_FORWARD | MB_MOTION_BACKWARD)) )
1681 if( !(i_structure == FRAME_STRUCTURE
1682 && p_vpar->picture.b_frame_pred_frame_dct) )
1684 p_vpar->mb.i_motion_type = GetBits( &p_vpar->bit_stream, 2 );
1688 p_vpar->mb.i_motion_type = MOTION_FRAME;
1692 p_vpar->mb.i_mv_count = ppi_mv_count[i_structure == FRAME_STRUCTURE]
1693 [p_vpar->mb.i_motion_type];
1694 p_vpar->mb.i_mv_format = ppi_mv_format[i_structure == FRAME_STRUCTURE]
1695 [p_vpar->mb.i_motion_type];
1696 p_vpar->mb.b_dmv = p_vpar->mb.i_motion_type == MOTION_DMV;
1699 p_vpar->mb.b_dct_type = 0;
1700 if( (i_structure == FRAME_STRUCTURE) &&
1701 (!p_vpar->picture.b_frame_pred_frame_dct) &&
1702 (p_mb->i_mb_type & (MB_PATTERN|MB_INTRA)) )
1704 if( (p_vpar->mb.b_dct_type = GetBits( &p_vpar->bit_stream, 1 )) )
1706 /* The DCT is coded on fields. Jump one line between each
1708 p_mb->i_addb_l_stride <<= 1;
1709 p_mb->i_addb_l_stride += 8;
1710 /* With CHROMA_420, the DCT is necessarily frame-coded. */
1711 if( i_chroma_format != CHROMA_420 )
1713 p_mb->i_addb_c_stride <<= 1;
1714 p_mb->i_addb_c_stride += 8;
1720 /*****************************************************************************
1721 * ParseMacroblock : Parse the next macroblock
1722 *****************************************************************************/
1723 #define PARSEERROR \
1724 if( p_vpar->picture.b_error ) \
1726 /* Mark this block as skipped (better than green blocks), and \
1727 * go to the next slice. */ \
1728 (*pi_mb_address)--; \
1729 vpar_DestroyMacroblock( &p_vpar->vfifo, p_mb ); \
1733 #define PARSEBLOCKS( MPEG1FUNC, MPEG2FUNC ) \
1735 i_mask = 1 << (3 + (1 << i_chroma_format)); \
1738 p_data1 = p_mb->p_picture->p_y \
1739 + p_mb->i_l_x + p_vpar->mb.i_l_y*(p_vpar->sequence.i_width); \
1741 for( i_b = 0 ; i_b < 4 ; i_b++, i_mask >>= 1 ) \
1743 if( p_mb->i_coded_block_pattern & i_mask ) \
1745 memset( p_mb->ppi_blocks[i_b], 0, 64*sizeof(dctelem_t) ); \
1747 MPEG2FUNC( p_vpar, p_mb, i_b, i_chroma_format ); \
1749 MPEG1FUNC( p_vpar, p_mb, i_b, i_chroma_format ); \
1751 /* Calculate block coordinates. */ \
1752 p_mb->p_data[i_b] = p_data1 \
1753 + pi_y[p_vpar->mb.b_dct_type][i_b] \
1754 * p_vpar->picture.i_l_stride \
1762 p_data1 = p_mb->p_picture->p_u \
1764 + p_vpar->mb.i_c_y \
1765 * (p_vpar->sequence.i_chroma_width); \
1766 p_data2 = p_mb->p_picture->p_v \
1768 + p_vpar->mb.i_c_y \
1769 * (p_vpar->sequence.i_chroma_width); \
1771 for( i_b = 4; i_b < 4 + (1 << i_chroma_format); \
1772 i_b++, i_mask >>= 1 ) \
1774 yuv_data_t * pp_data[2] = {p_data1, p_data2}; \
1776 if( p_mb->i_coded_block_pattern & i_mask ) \
1778 memset( p_mb->ppi_blocks[i_b], 0, 64*sizeof(dctelem_t) ); \
1780 MPEG2FUNC( p_vpar, p_mb, i_b, i_chroma_format ); \
1782 MPEG1FUNC( p_vpar, p_mb, i_b, i_chroma_format ); \
1784 /* Calculate block coordinates. */ \
1785 p_mb->p_data[i_b] = pp_data[i_b & 1] \
1786 + pi_y[p_vpar->mb.b_dct_type][i_b] \
1787 * p_vpar->picture.i_c_stride \
1795 static __inline__ void ParseMacroblock(
1796 vpar_thread_t * p_vpar,
1797 int * pi_mb_address, /* previous address to be
1798 * used for mb_addr_incr */
1799 int i_mb_previous, /* actual previous mb */
1800 int i_mb_base, /* non-zero if field structure */
1801 /* The following parameters are explicit in
1802 * optimized routines : */
1803 boolean_t b_mpeg2, /* you know what ? */
1804 int i_coding_type, /* I, P, B or D */
1805 int i_chroma_format, /* 4:2:0, 4:2:2 or 4:4:4 */
1806 int i_structure, /* T(OP), B(OTTOM) or F(RAME) */
1807 boolean_t b_second_field ) /* second field of a
1810 static f_motion_t pppf_motion[4][2][4] =
1812 { {NULL, NULL, NULL, NULL},
1813 {NULL, NULL, NULL, NULL}
1815 { {NULL, vdec_MotionFieldField420, vdec_MotionField16x8420,
1816 vdec_MotionFieldDMV420},
1817 {NULL, vdec_MotionFrameField420, vdec_MotionFrameFrame420,
1818 vdec_MotionFrameDMV420}
1820 { {NULL, vdec_MotionFieldField422, vdec_MotionField16x8422,
1821 vdec_MotionFieldDMV422},
1822 {NULL, vdec_MotionFrameField422, vdec_MotionFrameFrame422,
1823 vdec_MotionFrameDMV422}
1825 { {NULL, vdec_MotionFieldField444, vdec_MotionField16x8444,
1826 vdec_MotionFieldDMV444},
1827 {NULL, vdec_MotionFrameField444, vdec_MotionFrameFrame444,
1828 vdec_MotionFrameDMV444}
1831 static int pi_x[12] = {0,8,0,8,0,0,0,0,8,8,8,8};
1832 static int pi_y[2][12] = { {0,0,8,8,0,0,8,8,0,0,8,8},
1833 {0,0,1,1,0,0,1,1,0,0,1,1} };
1834 int i_mb, i_b, i_mask;
1836 macroblock_t * p_mb;
1837 yuv_data_t * p_data1;
1838 yuv_data_t * p_data2;
1840 i_inc = MacroblockAddressIncrement( p_vpar );
1841 *pi_mb_address += i_inc;
1845 intf_ErrMsg( "vpar error: bad address increment (%d)\n", i_inc );
1846 p_vpar->picture.b_error = 1;
1850 if( *pi_mb_address - i_mb_previous - 1 )
1852 /* Skipped macroblock (ISO/IEC 13818-2 7.6.6). */
1854 /* Reset DC predictors (7.2.1). */
1855 p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
1856 = p_vpar->mb.pi_dc_dct_pred[2]
1857 = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
1859 if( i_coding_type == P_CODING_TYPE )
1861 /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
1862 memset( p_vpar->mb.pppi_pmv, 0, 8*sizeof(int) );
1865 for( i_mb = i_mb_previous + 1; i_mb < *pi_mb_address; i_mb++ )
1867 SkippedMacroblock( p_vpar, i_mb, i_mb_base, i_coding_type,
1868 i_chroma_format, i_structure, b_second_field );
1872 /* Get a macroblock structure. */
1873 if( (p_mb = vpar_NewMacroblock( &p_vpar->vfifo )) == NULL )
1879 p_vpar->picture.pp_mb[i_mb_base + *pi_mb_address] = p_mb;
1882 InitMacroblock( p_vpar, p_mb, i_coding_type, i_chroma_format,
1883 i_structure, b_second_field );
1885 /* Parse off macroblock_modes structure. */
1886 MacroblockModes( p_vpar, p_mb, i_chroma_format, i_coding_type,
1889 if( p_mb->i_mb_type & MB_QUANT )
1891 LoadQuantizerScale( p_vpar );
1894 if( (i_coding_type == P_CODING_TYPE || i_coding_type == B_CODING_TYPE)
1895 && (p_mb->i_mb_type & MB_MOTION_FORWARD) )
1898 DecodeMVMPEG2( p_vpar, p_mb, 0, i_structure );
1900 DecodeMVMPEG1( p_vpar, p_mb, 0, i_structure );
1904 if( (i_coding_type == B_CODING_TYPE)
1905 && (p_mb->i_mb_type & MB_MOTION_BACKWARD) )
1908 DecodeMVMPEG2( p_vpar, p_mb, 1, i_structure );
1910 DecodeMVMPEG1( p_vpar, p_mb, 1, i_structure );
1914 if( i_coding_type == P_CODING_TYPE
1915 && !(p_mb->i_mb_type & (MB_MOTION_FORWARD|MB_INTRA)) )
1917 /* Special No-MC macroblock in P pictures (7.6.3.5). */
1918 p_mb->i_mb_type |= MB_MOTION_FORWARD;
1919 memset( p_vpar->mb.pppi_pmv, 0, 8*sizeof(int) );
1920 memset( p_mb->pppi_motion_vectors, 0, 8*sizeof(int) );
1921 p_vpar->mb.i_motion_type = 1 + (i_structure == FRAME_STRUCTURE);
1922 p_mb->ppi_field_select[0][0] = (i_structure == BOTTOM_FIELD);
1925 if( (i_coding_type != I_CODING_TYPE) && !(p_mb->i_mb_type & MB_INTRA) )
1927 /* Reset DC predictors (7.2.1). */
1928 p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
1929 = p_vpar->mb.pi_dc_dct_pred[2]
1930 = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
1932 /* Motion function pointer. */
1933 p_mb->pf_motion = pppf_motion[i_chroma_format]
1934 [i_structure == FRAME_STRUCTURE]
1935 [p_vpar->mb.i_motion_type];
1937 if( p_mb->i_mb_type & MB_PATTERN )
1939 switch( i_chroma_format )
1942 p_mb->i_coded_block_pattern = CodedPattern420( p_vpar );
1945 p_mb->i_coded_block_pattern = CodedPattern422( p_vpar );
1948 p_mb->i_coded_block_pattern = CodedPattern444( p_vpar );
1953 p_mb->i_coded_block_pattern = 0;
1957 * Effectively decode blocks.
1959 PARSEBLOCKS( DecodeMPEG1NonIntra, DecodeMPEG2NonIntra )
1963 if( !p_vpar->picture.b_concealment_mv )
1965 /* Reset MV predictors. */
1966 memset( p_vpar->mb.pppi_pmv, 0, 8*sizeof(int) );
1971 DecodeMVMPEG2( p_vpar, p_mb, 0, i_structure );
1973 DecodeMVMPEG1( p_vpar, p_mb, 0, i_structure );
1974 RemoveBits( &p_vpar->bit_stream, 1 );
1977 if( p_mb->i_mb_type & MB_PATTERN )
1979 switch( i_chroma_format )
1982 p_mb->i_coded_block_pattern = CodedPattern420( p_vpar );
1985 p_mb->i_coded_block_pattern = CodedPattern422( p_vpar );
1988 p_mb->i_coded_block_pattern = CodedPattern444( p_vpar );
1993 p_mb->i_coded_block_pattern =
1994 (1 << (4 + (1 << i_chroma_format))) - 1;
1998 * Effectively decode blocks.
2000 PARSEBLOCKS( DecodeMPEG1Intra, DecodeMPEG2Intra )
2003 if( !p_vpar->picture.b_error )
2005 UpdateContext( p_vpar, i_structure );
2007 /* Decode the macroblock NOW ! */
2008 vpar_DecodeMacroblock ( &p_vpar->vfifo, p_mb );
2013 /* Mark this block as skipped (better than green blocks), and go
2014 * to the next slice. */
2016 vpar_DestroyMacroblock( &p_vpar->vfifo, p_mb );
2021 * Picture data parsing management
2024 /*****************************************************************************
2025 * ParseMacroblockVWXYZ : Parse the next macroblock ; specific functions
2026 *****************************************************************************
2029 * X = chroma format ?
2031 * Z = second field ?
2032 *****************************************************************************/
2033 void ParseMacroblockGENERIC( vpar_thread_t * p_vpar, int * pi_mb_address,
2034 int i_mb_previous, int i_mb_base,
2035 boolean_t b_mpeg2, int i_coding_type,
2036 int i_chroma_format, int i_structure,
2037 boolean_t b_second_field )
2039 ParseMacroblock( p_vpar, pi_mb_address, i_mb_previous, i_mb_base,
2040 b_mpeg2, i_coding_type, i_chroma_format,
2041 i_structure, b_second_field );
2044 #if (VPAR_OPTIM_LEVEL > 0)
2045 /* Optimizations for frame pictures */
2046 void ParseMacroblock2I420F0( vpar_thread_t * p_vpar, int * pi_mb_address, int i_mb_previous, int i_mb_base,
2047 boolean_t b_mpeg2, int i_coding_type,
2048 int i_chroma_format, int i_structure,
2049 boolean_t b_second_field )
2051 ParseMacroblock( p_vpar, pi_mb_address, i_mb_previous, i_mb_base,
2052 1, I_CODING_TYPE, CHROMA_420,
2053 FRAME_STRUCTURE, 0 );
2056 void ParseMacroblock2P420F0( vpar_thread_t * p_vpar, int * pi_mb_address, int i_mb_previous, int i_mb_base,
2057 boolean_t b_mpeg2, int i_coding_type,
2058 int i_chroma_format, int i_structure,
2059 boolean_t b_second_field )
2061 ParseMacroblock( p_vpar, pi_mb_address, i_mb_previous, i_mb_base,
2062 1, P_CODING_TYPE, CHROMA_420,
2063 FRAME_STRUCTURE, 0 );
2066 void ParseMacroblock2B420F0( vpar_thread_t * p_vpar, int * pi_mb_address, int i_mb_previous, int i_mb_base,
2067 boolean_t b_mpeg2, int i_coding_type,
2068 int i_chroma_format, int i_structure,
2069 boolean_t b_second_field )
2071 ParseMacroblock( p_vpar, pi_mb_address, i_mb_previous, i_mb_base,
2072 1, B_CODING_TYPE, CHROMA_420,
2073 FRAME_STRUCTURE, 0 );
2077 #if (VPAR_OPTIM_LEVEL > 1)
2078 /* Optimizations for field pictures */
2079 void ParseMacroblock2I420TZ( vpar_thread_t * p_vpar, int * pi_mb_address, int i_mb_previous, int i_mb_base,
2080 boolean_t b_mpeg2, int i_coding_type,
2081 int i_chroma_format, int i_structure,
2082 boolean_t b_second_field )
2084 ParseMacroblock( p_vpar, pi_mb_address, i_mb_previous, i_mb_base,
2085 1, I_CODING_TYPE, CHROMA_420,
2086 TOP_FIELD, (p_vpar->picture.i_structure !=
2087 p_vpar->picture.i_current_structure) );
2090 void ParseMacroblock2P420TZ( vpar_thread_t * p_vpar, int * pi_mb_address, int i_mb_previous, int i_mb_base,
2091 boolean_t b_mpeg2, int i_coding_type,
2092 int i_chroma_format, int i_structure,
2093 boolean_t b_second_field )
2095 ParseMacroblock( p_vpar, pi_mb_address, i_mb_previous, i_mb_base,
2096 1, P_CODING_TYPE, CHROMA_420,
2097 TOP_FIELD, (p_vpar->picture.i_structure !=
2098 p_vpar->picture.i_current_structure) );
2101 void ParseMacroblock2B420TZ( vpar_thread_t * p_vpar, int * pi_mb_address, int i_mb_previous, int i_mb_base,
2102 boolean_t b_mpeg2, int i_coding_type,
2103 int i_chroma_format, int i_structure,
2104 boolean_t b_second_field )
2106 ParseMacroblock( p_vpar, pi_mb_address, i_mb_previous, i_mb_base,
2107 1, B_CODING_TYPE, CHROMA_420,
2108 TOP_FIELD, (p_vpar->picture.i_structure !=
2109 p_vpar->picture.i_current_structure) );
2112 void ParseMacroblock2I420BZ( vpar_thread_t * p_vpar, int * pi_mb_address, int i_mb_previous, int i_mb_base,
2113 boolean_t b_mpeg2, int i_coding_type,
2114 int i_chroma_format, int i_structure,
2115 boolean_t b_second_field )
2117 ParseMacroblock( p_vpar, pi_mb_address, i_mb_previous, i_mb_base,
2118 1, I_CODING_TYPE, CHROMA_420,
2119 BOTTOM_FIELD, (p_vpar->picture.i_structure !=
2120 p_vpar->picture.i_current_structure) );
2123 void ParseMacroblock2P420BZ( vpar_thread_t * p_vpar, int * pi_mb_address, int i_mb_previous, int i_mb_base,
2124 boolean_t b_mpeg2, int i_coding_type,
2125 int i_chroma_format, int i_structure,
2126 boolean_t b_second_field )
2128 ParseMacroblock( p_vpar, pi_mb_address, i_mb_previous, i_mb_base,
2129 1, P_CODING_TYPE, CHROMA_420,
2130 BOTTOM_FIELD, (p_vpar->picture.i_structure !=
2131 p_vpar->picture.i_current_structure) );
2134 void ParseMacroblock2B420BZ( vpar_thread_t * p_vpar, int * pi_mb_address, int i_mb_previous, int i_mb_base,
2135 boolean_t b_mpeg2, int i_coding_type,
2136 int i_chroma_format, int i_structure,
2137 boolean_t b_second_field )
2139 ParseMacroblock( p_vpar, pi_mb_address, i_mb_previous, i_mb_base,
2140 1, B_CODING_TYPE, CHROMA_420,
2141 BOTTOM_FIELD, (p_vpar->picture.i_structure !=
2142 p_vpar->picture.i_current_structure) );
2146 /*****************************************************************************
2147 * SliceHeader : Parse the next slice structure
2148 *****************************************************************************/
2149 typedef void (*f_parse_mb_t)( vpar_thread_t * p_vpar, int * pi_mb_address, int i_mb_previous, int i_mb_base,
2150 boolean_t b_mpeg2, int i_coding_type,
2151 int i_chroma_format, int i_structure,
2152 boolean_t b_second_field );
2154 static __inline__ void SliceHeader( vpar_thread_t * p_vpar,
2155 int * pi_mb_address, int i_mb_base,
2156 u32 i_vert_code, boolean_t b_high,
2157 boolean_t b_dp_scalable,
2158 boolean_t b_mpeg2, int i_coding_type,
2159 int i_chroma_format, int i_structure,
2160 boolean_t b_second_field )
2162 static f_parse_mb_t ppf_parse_mb[4][4] =
2165 NULL, NULL, NULL, NULL
2169 #if (VPAR_OPTIM_LEVEL > 1)
2170 NULL, ParseMacroblock2I420TZ, ParseMacroblock2P420TZ,
2171 ParseMacroblock2B420TZ
2173 NULL, ParseMacroblockGENERIC, ParseMacroblockGENERIC,
2174 ParseMacroblockGENERIC
2179 #if (VPAR_OPTIM_LEVEL > 1)
2180 NULL, ParseMacroblock2I420BZ, ParseMacroblock2P420BZ,
2181 ParseMacroblock2B420BZ
2183 NULL, ParseMacroblockGENERIC, ParseMacroblockGENERIC,
2184 ParseMacroblockGENERIC
2189 #if (VPAR_OPTIM_LEVEL > 0)
2190 NULL, ParseMacroblock2I420F0, ParseMacroblock2P420F0,
2191 ParseMacroblock2B420F0
2193 NULL, ParseMacroblockGENERIC, ParseMacroblockGENERIC,
2194 ParseMacroblockGENERIC
2199 int i_mb_address_save = *pi_mb_address;
2201 p_vpar->picture.b_error = 0;
2205 /* Picture with more than 2800 lines. */
2206 i_vert_code += GetBits( &p_vpar->bit_stream, 3 ) << 7;
2210 /* DATA_PARTITIONING scalability. */
2211 RemoveBits( &p_vpar->bit_stream, 7 ); /* priority_breakpoint */
2214 LoadQuantizerScale( p_vpar );
2216 if( GetBits( &p_vpar->bit_stream, 1 ) )
2218 /* intra_slice, slice_id */
2219 RemoveBits( &p_vpar->bit_stream, 8 );
2220 /* extra_information_slice */
2221 while( GetBits( &p_vpar->bit_stream, 1 ) )
2223 RemoveBits( &p_vpar->bit_stream, 8 );
2226 *pi_mb_address = (i_vert_code - 1)*p_vpar->sequence.i_mb_width;
2228 if( *pi_mb_address < i_mb_address_save )
2230 intf_ErrMsg( "vpar error: slices do not follow, maybe a PES has been trashed\n" );
2231 p_vpar->picture.b_error = 1;
2235 /* Reset DC coefficients predictors (ISO/IEC 13818-2 7.2.1). */
2236 p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
2237 = p_vpar->mb.pi_dc_dct_pred[2]
2238 = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
2240 /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
2241 memset( p_vpar->mb.pppi_pmv, 0, 8*sizeof(int) );
2245 if( i_mb_address_save >= p_vpar->sequence.i_mb_size )
2247 p_vpar->picture.b_error = 1;
2251 if( p_vpar->sequence.i_chroma_format != CHROMA_420
2252 || !p_vpar->sequence.b_mpeg2 || p_vpar->sequence.i_height > 2800
2253 || p_vpar->sequence.i_scalable_mode == SC_DP )
2255 /* Weird stream. Use the slower generic function. */
2256 ParseMacroblockGENERIC( p_vpar, pi_mb_address, i_mb_address_save,
2257 i_mb_base, b_mpeg2, i_coding_type,
2258 i_chroma_format, i_structure,
2263 /* Try to find an optimized function. */
2264 if( ppf_parse_mb[i_structure]
2265 [p_vpar->picture.i_coding_type] == NULL )
2267 intf_ErrMsg( "vpar error: bad ppf_parse_mb function pointer (struct:%d, coding type:%d)\n",
2268 i_structure, i_coding_type );
2272 ppf_parse_mb[i_structure][i_coding_type]
2273 ( p_vpar, pi_mb_address, i_mb_address_save,
2274 i_mb_base, b_mpeg2, i_coding_type,
2275 i_chroma_format, i_structure,
2280 i_mb_address_save = *pi_mb_address;
2281 if( p_vpar->picture.b_error )
2286 while( ShowBits( &p_vpar->bit_stream, 23 )
2287 && !p_vpar->p_fifo->b_die );
2288 NextStartCode( &p_vpar->bit_stream );
2291 /*****************************************************************************
2292 * PictureData : Parse off all macroblocks (ISO/IEC 13818-2 6.2.3.7)
2293 *****************************************************************************/
2294 void vpar_PictureData( vpar_thread_t * p_vpar, int i_mb_base )
2296 int i_mb_address = 0;
2299 NextStartCode( &p_vpar->bit_stream );
2300 while( ((p_vpar->picture.i_coding_type != I_CODING_TYPE
2301 && p_vpar->picture.i_coding_type != D_CODING_TYPE)
2302 || !p_vpar->picture.b_error)
2303 && i_mb_address < (p_vpar->sequence.i_mb_size
2304 >> (p_vpar->picture.i_structure != FRAME_STRUCTURE))
2305 && !p_vpar->p_fifo->b_die )
2307 if( ((i_dummy = ShowBits( &p_vpar->bit_stream, 32 ))
2308 < SLICE_START_CODE_MIN) ||
2309 (i_dummy > SLICE_START_CODE_MAX) )
2311 intf_DbgMsg("vpar debug: premature end of picture\n");
2312 p_vpar->picture.b_error = 1;
2315 RemoveBits32( &p_vpar->bit_stream );
2317 /* Decode slice data. */
2318 SliceHeader( p_vpar, &i_mb_address, i_mb_base, i_dummy & 255,
2319 (p_vpar->sequence.i_height > 2800),
2320 (p_vpar->sequence.i_scalable_mode == SC_DP),
2321 p_vpar->sequence.b_mpeg2, p_vpar->picture.i_coding_type,
2322 p_vpar->sequence.i_chroma_format,
2323 p_vpar->picture.i_structure,
2324 (p_vpar->picture.i_structure !=
2325 p_vpar->picture.i_current_structure) );
2328 /* Try to recover from error. If we missed less than half the
2329 * number of macroblocks of the picture, mark the missed ones
2331 if( (p_vpar->picture.i_coding_type == P_CODING_TYPE
2332 || p_vpar->picture.i_coding_type == B_CODING_TYPE)
2333 && p_vpar->picture.b_error &&
2334 ( (i_mb_address-i_mb_base) > (p_vpar->sequence.i_mb_size >> 1)
2335 || (p_vpar->picture.i_structure != FRAME_STRUCTURE
2336 && (i_mb_address-i_mb_base) > (p_vpar->sequence.i_mb_size >> 2) ) ) )
2340 p_vpar->picture.b_error = 0;
2341 for( i_mb = i_mb_address + 1;
2342 i_mb < (p_vpar->sequence.i_mb_size
2343 << (p_vpar->picture.i_structure != FRAME_STRUCTURE));
2346 SkippedMacroblock( p_vpar, i_mb, i_mb_base,
2347 p_vpar->picture.i_coding_type,
2348 p_vpar->sequence.i_chroma_format,
2349 p_vpar->picture.i_structure,
2350 (p_vpar->picture.i_structure !=
2351 p_vpar->picture.i_current_structure) );