1 /*****************************************************************************
2 * vpar_blocks.c : blocks parsing
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
29 #include <sys/types.h> /* on BSD, uio.h needs types.h */
30 #include <sys/uio.h> /* "input.h" */
39 #include "debug.h" /* XXX?? temporaire, requis par netlist.h */
42 #include "input_netlist.h"
43 #include "decoder_fifo.h"
45 #include "video_output.h"
47 #include "vdec_idct.h"
48 #include "video_decoder.h"
49 #include "vdec_motion.h"
51 #include "vpar_blocks.h"
52 #include "vpar_headers.h"
53 #include "vpar_synchro.h"
54 #include "video_parser.h"
55 #include "video_fifo.h"
57 static int i_count = 0;
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 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->b_die; i_parse++ )
657 i_code = ShowBits( &p_vpar->bit_stream, 16 );
658 if( i_code >= 16384 )
660 b_dc = !((i_parse + 62)/63);
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)
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;
723 p_mb->ppi_blocks[i_b][i_pos] = b_sign ? -i_level : i_level;
726 intf_ErrMsg("vpar error: DCT coeff (non-intra) is out of bounds\n");
727 p_vpar->picture.b_error = 1;
730 /*****************************************************************************
731 * DecodeMPEG1Intra : decode MPEG-1 intra blocks
732 *****************************************************************************/
733 static void DecodeMPEG1Intra( vpar_thread_t * p_vpar,
734 macroblock_t * p_mb, int i_b ,
735 int i_chroma_format )
751 /* Give the chromatic component (0, 1, 2) */
752 i_cc = pi_cc_index[i_b];
754 /* Determine whether it is luminance or not (chrominance) */
755 b_chroma = ( i_cc + 1 ) >> 1;
758 i_code = ShowBits(&p_vpar->bit_stream, 5);
762 i_dct_dc_size = ppl_dct_dc_init_table_1[b_chroma][i_code].i_value;
763 i_length = ppl_dct_dc_init_table_1[b_chroma][i_code].i_length;
764 RemoveBits( &p_vpar->bit_stream, i_length);
768 i_code = ShowBits(&p_vpar->bit_stream, (9+b_chroma)) - (0x1f0 * (b_chroma + 1));
769 i_dct_dc_size = ppl_dct_dc_init_table_2[b_chroma][i_code].i_value;
770 i_length = ppl_dct_dc_init_table_2[b_chroma][i_code].i_length;
771 RemoveBits( &p_vpar->bit_stream, i_length);
774 if (i_dct_dc_size == 0)
778 i_dct_dc_diff = GetBits( &p_vpar->bit_stream, i_dct_dc_size);
779 if ((i_dct_dc_diff & (1<<(i_dct_dc_size-1))) == 0)
780 i_dct_dc_diff -= (1<<i_dct_dc_size) - 1;
783 /* Read the actual code with the good length */
784 p_vpar->mb.pi_dc_dct_pred[i_cc] += i_dct_dc_diff;
786 p_mb->ppi_blocks[i_b][0] = p_vpar->mb.pi_dc_dct_pred[i_cc];
788 i_nc = ( p_vpar->mb.pi_dc_dct_pred[i_cc] != 0 );
791 if( p_vpar->picture.i_coding_type == D_CODING_TYPE )
793 /* Remove end_of_macroblock (always 1, prevents startcode emulation)
794 * ISO/IEC 11172-2 section 2.4.2.7 and 2.4.3.6 */
795 RemoveBits( &p_vpar->bit_stream, 1 );
796 /* D pictures do not have AC coefficients */
801 /* Decoding of the AC coefficients */
806 for( i_parse = 1; !p_vpar->b_die/*i_parse < 64*/; i_parse++ )
808 i_code = ShowBits( &p_vpar->bit_stream, 16 );
809 /* We use 2 main tables for the coefficients */
810 if( i_code >= 16384 )
812 i_run = ppl_dct_tab1[0][(i_code>>12)-4].i_run;
813 i_level = ppl_dct_tab1[0][(i_code>>12)-4].i_level;
814 i_length = ppl_dct_tab1[0][(i_code>>12)-4].i_length;
818 i_run = p_vpar->ppl_dct_coef[0][i_code].i_run;
819 i_length = p_vpar->ppl_dct_coef[0][i_code].i_length;
820 i_level = p_vpar->ppl_dct_coef[0][i_code].i_level;
823 RemoveBits( &p_vpar->bit_stream, i_length );
828 i_run = GetBits( &p_vpar->bit_stream, 6 );
829 i_level = GetBits( &p_vpar->bit_stream, 8 );
831 i_level = GetBits( &p_vpar->bit_stream, 8 );
832 else if (i_level == 128)
833 i_level = GetBits( &p_vpar->bit_stream, 8 ) - 256;
834 else if (i_level > 128)
840 p_mb->pf_idct[i_b] = vdec_SparseIDCT;
841 p_mb->pi_sparse_pos[i_b] = i_coef;
845 p_mb->pf_idct[i_b] = vdec_IDCT;
851 b_sign = GetBits( &p_vpar->bit_stream, 1 );
854 /* Prepare the next block */
861 /* We have an error in the stream */
865 /* Determine the position of the block in the frame */
866 i_pos = pi_scan[p_vpar->picture.b_alternate_scan][i_parse];
867 i_level = ( i_level *
868 p_vpar->mb.i_quantizer_scale *
869 p_vpar->sequence.intra_quant.pi_matrix[i_pos] ) >> 3;
870 p_mb->ppi_blocks[i_b][i_pos] = b_sign ? -i_level : i_level;
873 intf_ErrMsg("vpar error: DCT coeff (intra) is out of bounds\n");
874 p_vpar->picture.b_error = 1;
877 /*****************************************************************************
878 * DecodeMPEG2NonIntra : decode MPEG-2 non-intra blocks
879 *****************************************************************************/
880 static void DecodeMPEG2NonIntra( vpar_thread_t * p_vpar,
881 macroblock_t * p_mb, int i_b,
882 int i_chroma_format )
898 /* Give the chromatic component (0, 1, 2) */
899 i_cc = pi_cc_index[i_b];
901 /* Determine whether it is luminance or not (chrominance) */
902 b_chroma = ( i_cc + 1 ) >> 1;
904 /* Give a pointer to the quantization matrices for intra blocks */
905 if( (i_chroma_format == CHROMA_420) || (!b_chroma) )
907 pi_quant = p_vpar->sequence.nonintra_quant.pi_matrix;
911 pi_quant = p_vpar->sequence.chroma_nonintra_quant.pi_matrix;
914 /* Decoding of the AC coefficients */
918 for( i_parse = 0; !p_vpar->b_die; i_parse++ )
920 i_code = ShowBits( &p_vpar->bit_stream, 16 );
921 if( i_code >= 16384 )
923 b_dc = (i_parse == 0);
924 i_run = ppl_dct_tab2[b_dc][(i_code>>12)-4].i_run;
925 i_level = ppl_dct_tab2[b_dc][(i_code>>12)-4].i_level;
926 i_length = ppl_dct_tab2[b_dc][(i_code>>12)-4].i_length;
930 i_run = p_vpar->ppl_dct_coef[0][i_code].i_run;
931 i_length = p_vpar->ppl_dct_coef[0][i_code].i_length;
932 i_level = p_vpar->ppl_dct_coef[0][i_code].i_level;
936 RemoveBits( &p_vpar->bit_stream, i_length );
941 i_run = GetBits( &p_vpar->bit_stream, 6 );
942 i_level = GetBits( &p_vpar->bit_stream, 12 );
943 i_level = (b_sign = ( i_level > 2047 )) ? 4096 - i_level
948 /* The MMX IDCT has a precision problem with non-intra
950 p_mb->ppi_blocks[i_b][0] += 4;
954 p_mb->pf_idct[i_b] = vdec_SparseIDCT;
955 p_mb->pi_sparse_pos[i_b] = i_coef;
959 p_mb->pf_idct[i_b] = vdec_IDCT;
965 b_sign = GetBits( &p_vpar->bit_stream, 1 );
976 i_pos = pi_scan[p_vpar->picture.b_alternate_scan][i_parse];
977 i_level = ( ((i_level << 1) + 1) * p_vpar->mb.i_quantizer_scale
978 * pi_quant[i_pos] ) >> 5;
979 p_mb->ppi_blocks[i_b][i_pos] = b_sign ? -i_level : i_level;
982 intf_ErrMsg("vpar error: DCT coeff (non-intra) is out of bounds\n");
983 p_vpar->picture.b_error = 1;
986 /*****************************************************************************
987 * DecodeMPEG2Intra : decode MPEG-2 intra blocks
988 *****************************************************************************/
989 static void DecodeMPEG2Intra( vpar_thread_t * p_vpar,
990 macroblock_t * p_mb, int i_b,
991 int i_chroma_format )
1004 boolean_t b_vlc_intra;
1009 /* Give the chromatic component (0, 1, 2) */
1010 i_cc = pi_cc_index[i_b];
1012 /* Determine whether it is luminance or not (chrominance) */
1013 b_chroma = ( i_cc + 1 ) >> 1;
1015 /* Give a pointer to the quantization matrices for intra blocks */
1016 if( (i_chroma_format == CHROMA_420) || (!b_chroma) )
1018 pi_quant = p_vpar->sequence.intra_quant.pi_matrix;
1022 pi_quant = p_vpar->sequence.chroma_intra_quant.pi_matrix;
1026 i_code = ShowBits(&p_vpar->bit_stream, 5);
1030 i_dct_dc_size = ppl_dct_dc_init_table_1[b_chroma][i_code].i_value;
1031 i_length = ppl_dct_dc_init_table_1[b_chroma][i_code].i_length;
1032 RemoveBits( &p_vpar->bit_stream, i_length);
1036 i_code = ShowBits(&p_vpar->bit_stream, (9+b_chroma)) - (0x1f0 * (b_chroma + 1));
1037 i_dct_dc_size = ppl_dct_dc_init_table_2[b_chroma][i_code].i_value;
1038 i_length = ppl_dct_dc_init_table_2[b_chroma][i_code].i_length;
1039 RemoveBits( &p_vpar->bit_stream, i_length);
1042 if (i_dct_dc_size == 0)
1046 i_dct_dc_diff = GetBits( &p_vpar->bit_stream, i_dct_dc_size);
1047 if ((i_dct_dc_diff & (1<<(i_dct_dc_size-1))) == 0)
1048 i_dct_dc_diff -= (1<<i_dct_dc_size) - 1;
1051 /* Read the actual code with the good length */
1052 p_vpar->mb.pi_dc_dct_pred[i_cc] += i_dct_dc_diff;
1054 p_mb->ppi_blocks[i_b][0] = ( p_vpar->mb.pi_dc_dct_pred[i_cc] <<
1055 ( 3 - p_vpar->picture.i_intra_dc_precision ) );
1057 i_nc = ( p_vpar->mb.pi_dc_dct_pred[i_cc] != 0 );
1060 /* Decoding of the AC coefficients */
1063 b_vlc_intra = p_vpar->picture.b_intra_vlc_format;
1064 for( i_parse = 1; !p_vpar->b_die/*i_parse < 64*/; i_parse++ )
1066 i_code = ShowBits( &p_vpar->bit_stream, 16 );
1067 /* We use 2 main tables for the coefficients */
1068 if( i_code >= 16384 )
1070 i_run = ppl_dct_tab1[b_vlc_intra][(i_code>>(12-(4*b_vlc_intra)))-4].i_run;
1071 i_level = ppl_dct_tab1[b_vlc_intra][(i_code>>(12-(4*b_vlc_intra)))-4].i_level;
1072 i_length = ppl_dct_tab1[b_vlc_intra][(i_code>>(12-(4*b_vlc_intra)))-4].i_length;
1076 i_run = p_vpar->ppl_dct_coef[b_vlc_intra][i_code].i_run;
1077 i_length = p_vpar->ppl_dct_coef[b_vlc_intra][i_code].i_length;
1078 i_level = p_vpar->ppl_dct_coef[b_vlc_intra][i_code].i_level;
1081 RemoveBits( &p_vpar->bit_stream, i_length );
1086 i_run = GetBits( &p_vpar->bit_stream, 6 );
1087 i_level = GetBits( &p_vpar->bit_stream, 12 );
1088 i_level = (b_sign = ( i_level > 2047 )) ? 4096 - i_level
1094 p_mb->pf_idct[i_b] = vdec_SparseIDCT;
1095 p_mb->pi_sparse_pos[i_b] = i_coef;
1099 p_mb->pf_idct[i_b] = vdec_IDCT;
1105 b_sign = GetBits( &p_vpar->bit_stream, 1 );
1108 /* Prepare the next block */
1115 /* We have an error in the stream */
1119 /* Determine the position of the block in the frame */
1120 i_pos = pi_scan[p_vpar->picture.b_alternate_scan][i_parse];
1121 i_level = ( i_level *
1122 p_vpar->mb.i_quantizer_scale *
1123 pi_quant[i_pos] ) >> 4;
1124 p_mb->ppi_blocks[i_b][i_pos] = b_sign ? -i_level : i_level;
1127 intf_ErrMsg("vpar error: DCT coeff (intra) is out of bounds\n");
1128 p_vpar->picture.b_error = 1;
1136 /****************************************************************************
1137 * MotionCode : Parse the next motion code
1138 ****************************************************************************/
1139 static int MotionCode( vpar_thread_t * p_vpar )
1142 static lookup_t pl_mv_tab0[8] =
1143 { {-1,0}, {3,3}, {2,2}, {2,2}, {1,1}, {1,1}, {1,1}, {1,1} };
1144 /* Table B-10, motion_code, codes 0000011 ... 000011x */
1145 static lookup_t pl_mv_tab1[8] =
1146 { {-1,0}, {-1,0}, {-1,0}, {7,6}, {6,6}, {5,6}, {4,5}, {4,5} };
1147 /* Table B-10, motion_code, codes 0000001100 ... 000001011x */
1148 static lookup_t pl_mv_tab2[12] = {
1149 {16,9}, {15,9}, {14,9}, {13,9},
1150 {12,9}, {11,9}, {10,8}, {10,8},
1151 {9,8}, {9,8}, {8,8}, {8,8} };
1153 if( GetBits( &p_vpar->bit_stream, 1 ) )
1157 if( (i_code = ShowBits( &p_vpar->bit_stream, 9) ) >= 64 )
1160 RemoveBits( &p_vpar->bit_stream, pl_mv_tab0[i_code].i_length );
1161 return( GetBits( &p_vpar->bit_stream, 1 ) ?
1162 -pl_mv_tab0[i_code].i_value : pl_mv_tab0[i_code].i_value );
1168 RemoveBits( &p_vpar->bit_stream, pl_mv_tab1[i_code].i_length );
1169 return( GetBits( &p_vpar->bit_stream, 1 ) ?
1170 -pl_mv_tab1[i_code].i_value : pl_mv_tab1[i_code].i_value );
1173 if( (i_code -= 12) < 0 )
1175 p_vpar->picture.b_error = 1;
1176 intf_DbgMsg( "vpar debug: Invalid motion_vector code\n" );
1180 RemoveBits( &p_vpar->bit_stream, pl_mv_tab2[i_code].i_length );
1181 return( GetBits( &p_vpar->bit_stream, 1 ) ?
1182 -pl_mv_tab2[i_code].i_value : pl_mv_tab2[i_code].i_value );
1185 /****************************************************************************
1186 * DecodeMotionVector : Decode a motion_vector
1187 ****************************************************************************/
1188 static void DecodeMotionVector( int * pi_prediction, int i_r_size,
1189 int i_motion_code, int i_motion_residual, int i_full_pel )
1191 int i_limit, i_vector;
1193 /* ISO/IEC 13818-1 section 7.6.3.1 */
1194 i_limit = 16 << i_r_size;
1195 i_vector = *pi_prediction >> i_full_pel;
1197 if( i_motion_code > 0 )
1199 i_vector += ((i_motion_code-1) << i_r_size) + i_motion_residual + 1;
1200 if( i_vector >= i_limit )
1201 i_vector -= i_limit + i_limit;
1203 else if( i_motion_code < 0 )
1205 i_vector -= ((-i_motion_code-1) << i_r_size) + i_motion_residual + 1;
1206 if( i_vector < -i_limit )
1207 i_vector += i_limit + i_limit;
1209 *pi_prediction = i_vector << i_full_pel;
1212 /****************************************************************************
1213 * MotionVector : Parse the next motion_vector field
1214 ****************************************************************************/
1215 static void MotionVector( vpar_thread_t * p_vpar,
1216 macroblock_t * p_mb, int i_r,
1217 int i_s, int i_full_pel, int i_structure )
1219 int i_motion_code, i_motion_residual;
1221 int pi_dm_vector[2];
1223 i_r_size = p_vpar->picture.ppi_f_code[i_s][0] - 1;
1224 i_motion_code = MotionCode( p_vpar );
1225 i_motion_residual = (i_r_size != 0 && i_motion_code != 0) ?
1226 GetBits( &p_vpar->bit_stream, i_r_size) : 0;
1227 DecodeMotionVector( &p_vpar->mb.pppi_pmv[i_r][i_s][0], i_r_size,
1228 i_motion_code, i_motion_residual, i_full_pel );
1229 p_mb->pppi_motion_vectors[i_r][i_s][0] = p_vpar->mb.pppi_pmv[i_r][i_s][0];
1231 if( p_vpar->mb.b_dmv )
1233 if( GetBits(&p_vpar->bit_stream, 1) )
1235 pi_dm_vector[0] = GetBits( &p_vpar->bit_stream, 1 ) ? -1 : 1;
1239 pi_dm_vector[0] = 0;
1243 i_r_size = p_vpar->picture.ppi_f_code[i_s][1]-1;
1244 i_motion_code = MotionCode( p_vpar );
1245 i_motion_residual = (i_r_size != 0 && i_motion_code != 0) ?
1246 GetBits( &p_vpar->bit_stream, i_r_size) : 0;
1249 if( (p_vpar->mb.i_mv_format == MOTION_FIELD)
1250 && (i_structure == FRAME_STRUCTURE) )
1252 p_vpar->mb.pppi_pmv[i_r][i_s][1] >>= 1;
1255 DecodeMotionVector( &p_vpar->mb.pppi_pmv[i_r][i_s][1], i_r_size,
1256 i_motion_code, i_motion_residual, i_full_pel );
1258 if( (p_vpar->mb.i_mv_format == MOTION_FIELD)
1259 && (i_structure == FRAME_STRUCTURE) )
1260 p_vpar->mb.pppi_pmv[i_r][i_s][1] <<= 1;
1262 p_mb->pppi_motion_vectors[i_r][i_s][1] = p_vpar->mb.pppi_pmv[i_r][i_s][1];
1264 if( p_vpar->mb.b_dmv )
1266 if( GetBits(&p_vpar->bit_stream, 1) )
1268 pi_dm_vector[1] = GetBits( &p_vpar->bit_stream, 1 ) ? -1 : 1;
1272 pi_dm_vector[1] = 0;
1275 /* Dual Prime Arithmetic (ISO/IEC 13818-2 section 7.6.3.6). */
1277 #define i_mv_x p_mb->pppi_motion_vectors[0][0][0]
1278 if( i_structure == FRAME_STRUCTURE )
1280 #define i_mv_y (p_mb->pppi_motion_vectors[0][0][1] << 1)
1281 if( p_vpar->picture.b_top_field_first )
1283 /* vector for prediction of top field from bottom field */
1284 p_mb->ppi_dmv[0][0] = ((i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1285 p_mb->ppi_dmv[0][1] = ((i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] - 1;
1287 /* vector for prediction of bottom field from top field */
1288 p_mb->ppi_dmv[1][0] = ((3*i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1289 p_mb->ppi_dmv[1][1] = ((3*i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] + 1;
1293 /* vector for prediction of top field from bottom field */
1294 p_mb->ppi_dmv[0][0] = ((3*i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1295 p_mb->ppi_dmv[0][1] = ((3*i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] - 1;
1297 /* vector for prediction of bottom field from top field */
1298 p_mb->ppi_dmv[1][0] = ((i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1299 p_mb->ppi_dmv[1][1] = ((i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1] + 1;
1305 #define i_mv_y p_mb->pppi_motion_vectors[0][0][1]
1306 /* vector for prediction from field of opposite 'parity' */
1307 p_mb->ppi_dmv[0][0] = ((i_mv_x + (i_mv_x > 0)) >> 1) + pi_dm_vector[0];
1308 p_mb->ppi_dmv[0][1] = ((i_mv_y + (i_mv_y > 0)) >> 1) + pi_dm_vector[1];
1310 /* correct for vertical field shift */
1311 if( p_vpar->picture.i_structure == TOP_FIELD )
1312 p_mb->ppi_dmv[0][1]--;
1314 p_mb->ppi_dmv[0][1]++;
1321 /*****************************************************************************
1322 * DecodeMVMPEG1 : Parse the next MPEG-1 motion vectors
1323 *****************************************************************************/
1324 static void DecodeMVMPEG1( vpar_thread_t * p_vpar,
1325 macroblock_t * p_mb, int i_s, int i_structure )
1327 MotionVector( p_vpar, p_mb, 0, i_s,
1328 p_vpar->picture.pb_full_pel_vector[i_s], i_structure );
1331 /*****************************************************************************
1332 * DecodeMVMPEG2 : Parse the next MPEG-2 motion_vectors field
1333 *****************************************************************************/
1334 static void DecodeMVMPEG2( vpar_thread_t * p_vpar,
1335 macroblock_t * p_mb, int i_s, int i_structure )
1337 if( p_vpar->mb.i_mv_count == 1 )
1339 if( p_vpar->mb.i_mv_format == MOTION_FIELD && !p_vpar->mb.b_dmv )
1341 p_mb->ppi_field_select[0][i_s] = p_mb->ppi_field_select[1][i_s]
1342 = GetBits( &p_vpar->bit_stream, 1 );
1344 MotionVector( p_vpar, p_mb, 0, i_s, 0, i_structure );
1345 p_vpar->mb.pppi_pmv[1][i_s][0] = p_vpar->mb.pppi_pmv[0][i_s][0];
1346 p_vpar->mb.pppi_pmv[1][i_s][1] = p_vpar->mb.pppi_pmv[0][i_s][1];
1347 p_mb->pppi_motion_vectors[1][i_s][0] = p_vpar->mb.pppi_pmv[0][i_s][0];
1348 p_mb->pppi_motion_vectors[1][i_s][1] = p_vpar->mb.pppi_pmv[0][i_s][1];
1352 p_mb->ppi_field_select[0][i_s] = GetBits( &p_vpar->bit_stream, 1 );
1353 MotionVector( p_vpar, p_mb, 0, i_s, 0, i_structure );
1354 p_mb->ppi_field_select[1][i_s] = GetBits( &p_vpar->bit_stream, 1 );
1355 MotionVector( p_vpar, p_mb, 1, i_s, 0, i_structure );
1361 * Macroblock information structures
1364 /*****************************************************************************
1365 * MacroblockAddressIncrement : Get the macroblock_address_increment field
1366 *****************************************************************************/
1367 static int MacroblockAddressIncrement( vpar_thread_t * p_vpar )
1370 /* Index in the lookup table mb_addr_inc */
1371 int i_index = ShowBits( &p_vpar->bit_stream, 11 );
1373 /* Test the presence of the escape character */
1374 while( i_index == 8 )
1376 RemoveBits( &p_vpar->bit_stream, 11 );
1378 i_index = ShowBits( &p_vpar->bit_stream, 11 );
1381 /* Affect the value from the lookup table */
1382 i_addr_inc += p_vpar->pl_mb_addr_inc[i_index].i_value;
1384 /* Dump the good number of bits */
1385 RemoveBits( &p_vpar->bit_stream, p_vpar->pl_mb_addr_inc[i_index].i_length );
1390 /*****************************************************************************
1391 * IMBType : macroblock_type in I pictures
1392 *****************************************************************************/
1393 static int IMBType( vpar_thread_t * p_vpar )
1395 /* Take two bits for testing */
1396 int i_type = ShowBits( &p_vpar->bit_stream, 2 );
1398 /* Lookup table for macroblock_type */
1399 static lookup_t pl_mb_Itype[4] = { {MB_ERROR, 0},
1400 {MB_QUANT|MB_INTRA, 2},
1403 /* Dump the good number of bits */
1404 RemoveBits( &p_vpar->bit_stream, pl_mb_Itype[i_type].i_length );
1405 return pl_mb_Itype[i_type].i_value;
1408 /*****************************************************************************
1409 * PMBType : macroblock_type in P pictures
1410 *****************************************************************************/
1411 static int PMBType( vpar_thread_t * p_vpar )
1413 /* Testing on 6 bits */
1414 int i_type = ShowBits( &p_vpar->bit_stream, 6 );
1416 /* Dump the good number of bits */
1417 RemoveBits( &p_vpar->bit_stream, p_vpar->ppl_mb_type[0][i_type].i_length );
1418 /* return the value from the lookup table for P type */
1419 return p_vpar->ppl_mb_type[0][i_type].i_value;
1422 /*****************************************************************************
1423 * BMBType : macroblock_type in B pictures
1424 *****************************************************************************/
1425 static int BMBType( vpar_thread_t * p_vpar )
1427 /* Testing on 6 bits */
1428 int i_type = ShowBits( &p_vpar->bit_stream, 6 );
1430 /* Dump the good number of bits */
1431 RemoveBits( &p_vpar->bit_stream, p_vpar->ppl_mb_type[1][i_type].i_length );
1433 /* return the value from the lookup table for B type */
1434 return p_vpar->ppl_mb_type[1][i_type].i_value;
1437 /*****************************************************************************
1438 * DMBType : macroblock_type in D pictures
1439 *****************************************************************************/
1440 static int DMBType( vpar_thread_t * p_vpar )
1442 return GetBits( &p_vpar->bit_stream, 1 );
1445 /*****************************************************************************
1446 * CodedPattern420 : coded_block_pattern with 4:2:0 chroma
1447 *****************************************************************************/
1448 static int CodedPattern420( vpar_thread_t * p_vpar )
1450 /* Take the max 9 bits length vlc code for testing */
1451 int i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1453 /* Trash the good number of bits read in the lookup table */
1454 RemoveBits( &p_vpar->bit_stream, pl_coded_pattern[i_vlc].i_length );
1456 /* return the value from the vlc table */
1457 return pl_coded_pattern[i_vlc].i_value;
1460 /*****************************************************************************
1461 * CodedPattern422 : coded_block_pattern with 4:2:2 chroma
1462 *****************************************************************************/
1463 static int CodedPattern422( vpar_thread_t * p_vpar )
1465 int i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1467 RemoveBits( &p_vpar->bit_stream, pl_coded_pattern[i_vlc].i_length );
1469 /* Supplementary 2 bits long code for 4:2:2 format */
1470 return pl_coded_pattern[i_vlc].i_value |
1471 (GetBits( &p_vpar->bit_stream, 2 ) << 6);
1474 /*****************************************************************************
1475 * CodedPattern444 : coded_block_pattern with 4:4:4 chroma
1476 *****************************************************************************/
1477 static int CodedPattern444( vpar_thread_t * p_vpar )
1479 int i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
1481 RemoveBits( &p_vpar->bit_stream, pl_coded_pattern[i_vlc].i_length );
1483 return pl_coded_pattern[i_vlc].i_value |
1484 (GetBits( &p_vpar->bit_stream, 6 ) << 6);
1487 /*****************************************************************************
1488 * InitMacroblock : Initialize macroblock values
1489 *****************************************************************************/
1490 static void InitMacroblock( vpar_thread_t * p_vpar,
1491 macroblock_t * p_mb, int i_coding_type,
1492 int i_chroma_format,
1494 boolean_t b_second_field )
1496 p_mb->i_chroma_nb_blocks = 1 << i_chroma_format;
1497 p_mb->p_picture = p_vpar->picture.p_picture;
1499 if( i_coding_type == B_CODING_TYPE )
1500 p_mb->p_backward = p_vpar->sequence.p_backward;
1502 p_mb->p_backward = NULL;
1503 if( (i_coding_type == P_CODING_TYPE) || (i_coding_type == B_CODING_TYPE) )
1504 p_mb->p_forward = p_vpar->sequence.p_forward;
1506 p_mb->p_forward = NULL;
1508 p_mb->i_l_x = p_vpar->mb.i_l_x;
1509 p_mb->i_c_x = p_vpar->mb.i_c_x;
1510 p_mb->i_motion_l_y = p_vpar->mb.i_l_y;
1511 p_mb->i_motion_c_y = p_vpar->mb.i_c_y;
1512 if( (p_mb->b_motion_field = (i_structure == BOTTOM_FIELD)) )
1514 p_mb->i_motion_l_y--;
1515 p_mb->i_motion_c_y--;
1517 p_mb->i_addb_l_stride = (p_mb->i_l_stride = p_vpar->picture.i_l_stride) - 8;
1518 p_mb->i_addb_c_stride = (p_mb->i_c_stride = p_vpar->picture.i_c_stride) - 8;
1519 p_mb->b_P_second = ( b_second_field && i_coding_type == P_CODING_TYPE );
1522 /*****************************************************************************
1523 * UpdateContext : Update the p_vpar contextual values
1524 *****************************************************************************/
1525 static void UpdateContext( vpar_thread_t * p_vpar, int i_structure )
1527 /* Update macroblock real position. */
1528 p_vpar->mb.i_l_x += 16;
1529 p_vpar->mb.i_l_y += (p_vpar->mb.i_l_x / p_vpar->sequence.i_width)
1530 * (2 - (i_structure == FRAME_STRUCTURE)) * 16;
1531 p_vpar->mb.i_l_x %= p_vpar->sequence.i_width;
1533 p_vpar->mb.i_c_x += p_vpar->sequence.i_chroma_mb_width;
1534 p_vpar->mb.i_c_y += (p_vpar->mb.i_c_x / p_vpar->sequence.i_chroma_width)
1535 * (2 - (i_structure == FRAME_STRUCTURE))
1536 * p_vpar->sequence.i_chroma_mb_height;
1537 p_vpar->mb.i_c_x %= p_vpar->sequence.i_chroma_width;
1540 /*****************************************************************************
1541 * SkippedMacroblock : Generate a skipped macroblock with NULL motion vector
1542 *****************************************************************************/
1543 static void SkippedMacroblock( vpar_thread_t * p_vpar, int i_mb,
1544 int i_mb_base, int i_coding_type,
1545 int i_chroma_format,
1547 boolean_t b_second_field )
1549 macroblock_t * p_mb;
1551 static f_motion_t pf_motion_skipped[4][4] =
1553 {NULL, NULL, NULL, NULL},
1554 {NULL, vdec_MotionFieldField420, vdec_MotionFieldField420,
1555 vdec_MotionFrameFrame420},
1556 {NULL, vdec_MotionFieldField422, vdec_MotionFieldField422,
1557 vdec_MotionFrameFrame422},
1558 {NULL, vdec_MotionFieldField444, vdec_MotionFieldField444,
1559 vdec_MotionFrameFrame444},
1562 if( i_coding_type == I_CODING_TYPE )
1564 intf_DbgMsg("vpar error: skipped macroblock in I-picture\n");
1565 p_vpar->picture.b_error = 1;
1569 if( (p_mb = vpar_NewMacroblock( &p_vpar->vfifo )) == NULL )
1575 p_vpar->picture.pp_mb[i_mb_base + i_mb] = p_mb;
1578 InitMacroblock( p_vpar, p_mb, i_coding_type, i_chroma_format,
1579 i_structure, b_second_field );
1581 /* Motion type is picture structure. */
1582 p_mb->pf_motion = pf_motion_skipped[i_chroma_format]
1584 p_mb->i_mb_type = MB_MOTION_FORWARD;
1585 p_mb->i_coded_block_pattern = 0;
1586 memset( p_mb->pppi_motion_vectors, 0, 8*sizeof(int) );
1588 /* Set the field we use for motion compensation */
1589 p_mb->ppi_field_select[0][0] = p_mb->ppi_field_select[0][1]
1590 = ( i_structure == BOTTOM_FIELD );
1592 UpdateContext( p_vpar, i_structure );
1595 /* Decode the macroblock NOW ! */
1596 vdec_DecodeMacroblock( p_vpar->pp_vdec[0], p_mb );
1600 /*****************************************************************************
1601 * MacroblockModes : Get the macroblock_modes structure
1602 *****************************************************************************/
1603 static void MacroblockModes( vpar_thread_t * p_vpar,
1604 macroblock_t * p_mb,
1605 int i_chroma_format,
1609 static int ppi_mv_count[2][4] = { {0, 1, 2, 1}, {0, 2, 1, 1} };
1610 static int ppi_mv_format[2][4] = { {0, 1, 1, 1}, {0, 1, 2, 1} };
1612 /* Get macroblock_type. */
1613 switch( i_coding_type )
1616 p_mb->i_mb_type = PMBType( p_vpar );
1619 p_mb->i_mb_type = BMBType( p_vpar );
1622 p_mb->i_mb_type = IMBType( p_vpar );
1625 p_mb->i_mb_type = DMBType( p_vpar );
1628 /* SCALABILITY : warning, we don't know if spatial_temporal_weight_code
1629 * has to be dropped, take care if you use scalable streams. */
1630 /* RemoveBits( &p_vpar->bit_stream, 2 ); */
1632 if( (i_coding_type == P_CODING_TYPE || i_coding_type == B_CODING_TYPE)
1633 && (p_mb->i_mb_type & (MB_MOTION_FORWARD | MB_MOTION_BACKWARD)) )
1635 if( !(i_structure == FRAME_STRUCTURE
1636 && p_vpar->picture.b_frame_pred_frame_dct) )
1638 p_vpar->mb.i_motion_type = GetBits( &p_vpar->bit_stream, 2 );
1642 p_vpar->mb.i_motion_type = MOTION_FRAME;
1646 p_vpar->mb.i_mv_count = ppi_mv_count[i_structure == FRAME_STRUCTURE]
1647 [p_vpar->mb.i_motion_type];
1648 p_vpar->mb.i_mv_format = ppi_mv_format[i_structure == FRAME_STRUCTURE]
1649 [p_vpar->mb.i_motion_type];
1650 p_vpar->mb.b_dmv = p_vpar->mb.i_motion_type == MOTION_DMV;
1653 p_vpar->mb.b_dct_type = 0;
1654 if( (i_structure == FRAME_STRUCTURE) &&
1655 (!p_vpar->picture.b_frame_pred_frame_dct) &&
1656 (p_mb->i_mb_type & (MB_PATTERN|MB_INTRA)) )
1658 if( (p_vpar->mb.b_dct_type = GetBits( &p_vpar->bit_stream, 1 )) )
1660 /* The DCT is coded on fields. Jump one line between each
1662 p_mb->i_addb_l_stride <<= 1;
1663 p_mb->i_addb_l_stride += 8;
1664 /* With CHROMA_420, the DCT is necessarily frame-coded. */
1665 if( i_chroma_format != CHROMA_420 )
1667 p_mb->i_addb_c_stride <<= 1;
1668 p_mb->i_addb_c_stride += 8;
1674 /*****************************************************************************
1675 * ParseMacroblock : Parse the next macroblock
1676 *****************************************************************************/
1677 #define PARSEERROR \
1678 if( p_vpar->picture.b_error ) \
1680 /* Mark this block as skipped (better than green blocks), and \
1681 * go to the next slice. */ \
1682 (*pi_mb_address)--; \
1683 vpar_DestroyMacroblock( &p_vpar->vfifo, p_mb ); \
1688 #define PARSEBLOCKS( MPEG1FUNC, MPEG2FUNC ) \
1690 i_mask = 1 << (3 + (1 << i_chroma_format)); \
1693 p_data1 = p_mb->p_picture->p_y \
1694 + p_mb->i_l_x + p_vpar->mb.i_l_y*(p_vpar->sequence.i_width); \
1696 for( i_b = 0 ; i_b < 4 ; i_b++, i_mask >>= 1 ) \
1698 if( p_mb->i_coded_block_pattern & i_mask ) \
1700 memset( p_mb->ppi_blocks[i_b], 0, 64*sizeof(dctelem_t) ); \
1702 MPEG2FUNC( p_vpar, p_mb, i_b, i_chroma_format ); \
1704 MPEG1FUNC( p_vpar, p_mb, i_b, i_chroma_format ); \
1706 /* Calculate block coordinates. */ \
1707 p_mb->p_data[i_b] = p_data1 \
1708 + pi_y[p_vpar->mb.b_dct_type][i_b] \
1709 * p_vpar->sequence.i_width \
1717 p_data1 = p_mb->p_picture->p_u \
1719 + p_vpar->mb.i_c_y \
1720 * (p_vpar->sequence.i_chroma_width); \
1721 p_data2 = p_mb->p_picture->p_v \
1723 + p_vpar->mb.i_c_y \
1724 * (p_vpar->sequence.i_chroma_width); \
1726 for( i_b = 4; i_b < 4 + (1 << i_chroma_format); \
1727 i_b++, i_mask >>= 1 ) \
1729 yuv_data_t * pp_data[2] = {p_data1, p_data2}; \
1731 if( p_mb->i_coded_block_pattern & i_mask ) \
1733 memset( p_mb->ppi_blocks[i_b], 0, 64*sizeof(dctelem_t) ); \
1735 MPEG2FUNC( p_vpar, p_mb, i_b, i_chroma_format ); \
1737 MPEG1FUNC( p_vpar, p_mb, i_b, i_chroma_format ); \
1739 /* Calculate block coordinates. */ \
1740 p_mb->p_data[i_b] = pp_data[i_b & 1] \
1741 + pi_y[p_vpar->mb.b_dct_type][i_b] \
1742 * p_vpar->sequence.i_chroma_width \
1750 static void ParseMacroblock(
1751 vpar_thread_t * p_vpar,
1752 int * pi_mb_address, /* previous address to be
1753 * used for mb_addr_incr */
1754 int i_mb_previous, /* actual previous mb */
1755 int i_mb_base, /* non-zero if field structure */
1756 /* The following parameters are explicit in
1757 * optimized routines : */
1758 boolean_t b_mpeg2, /* you know what ? */
1759 int i_coding_type, /* I, P, B or D */
1760 int i_chroma_format, /* 4:2:0, 4:2:2 or 4:4:4 */
1761 int i_structure, /* T(OP), B(OTTOM) or F(RAME) */
1762 boolean_t b_second_field ) /* second field of a
1765 static f_motion_t pppf_motion[4][2][4] =
1767 { {NULL, NULL, NULL, NULL},
1768 {NULL, NULL, NULL, NULL}
1770 { {NULL, vdec_MotionFieldField420, vdec_MotionField16x8420,
1771 vdec_MotionFieldDMV420},
1772 {NULL, vdec_MotionFrameField420, vdec_MotionFrameFrame420,
1773 vdec_MotionFrameDMV420}
1775 { {NULL, vdec_MotionFieldField422, vdec_MotionField16x8422,
1776 vdec_MotionFieldDMV422},
1777 {NULL, vdec_MotionFrameField422, vdec_MotionFrameFrame422,
1778 vdec_MotionFrameDMV422}
1780 { {NULL, vdec_MotionFieldField444, vdec_MotionField16x8444,
1781 vdec_MotionFieldDMV444},
1782 {NULL, vdec_MotionFrameField444, vdec_MotionFrameFrame444,
1783 vdec_MotionFrameDMV444}
1786 static int pi_x[12] = {0,8,0,8,0,0,0,0,8,8,8,8};
1787 static int pi_y[2][12] = { {0,0,8,8,0,0,8,8,0,0,8,8},
1788 {0,0,1,1,0,0,1,1,0,0,1,1} };
1789 int i_mb, i_b, i_mask;
1791 macroblock_t * p_mb;
1792 yuv_data_t * p_data1;
1793 yuv_data_t * p_data2;
1797 i_inc = MacroblockAddressIncrement( p_vpar );
1798 *pi_mb_address += i_inc;
1802 intf_ErrMsg( "vpar error: bad address increment (%d)\n", i_inc );
1803 p_vpar->picture.b_error = 1;
1807 if( *pi_mb_address - i_mb_previous - 1 )
1809 /* Skipped macroblock (ISO/IEC 13818-2 7.6.6). */
1811 /* Reset DC predictors (7.2.1). */
1812 p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
1813 = p_vpar->mb.pi_dc_dct_pred[2]
1814 = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
1816 if( i_coding_type == P_CODING_TYPE )
1818 /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
1819 memset( p_vpar->mb.pppi_pmv, 0, 8*sizeof(int) );
1822 for( i_mb = i_mb_previous + 1; i_mb < *pi_mb_address; i_mb++ )
1824 SkippedMacroblock( p_vpar, i_mb, i_mb_base, i_coding_type,
1825 i_chroma_format, i_structure, b_second_field );
1829 /* Get a macroblock structure. */
1830 if( (p_mb = vpar_NewMacroblock( &p_vpar->vfifo )) == NULL )
1836 p_vpar->picture.pp_mb[i_mb_base + *pi_mb_address] = p_mb;
1839 InitMacroblock( p_vpar, p_mb, i_coding_type, i_chroma_format,
1840 i_structure, b_second_field );
1842 /* Parse off macroblock_modes structure. */
1843 MacroblockModes( p_vpar, p_mb, i_chroma_format, i_coding_type,
1846 if( p_mb->i_mb_type & MB_QUANT )
1848 LoadQuantizerScale( p_vpar );
1851 if( (i_coding_type == P_CODING_TYPE || i_coding_type == B_CODING_TYPE)
1852 && (p_mb->i_mb_type & MB_MOTION_FORWARD) )
1855 DecodeMVMPEG2( p_vpar, p_mb, 0, i_structure );
1857 DecodeMVMPEG1( p_vpar, p_mb, 0, i_structure );
1861 if( (i_coding_type == B_CODING_TYPE)
1862 && (p_mb->i_mb_type & MB_MOTION_BACKWARD) )
1865 DecodeMVMPEG2( p_vpar, p_mb, 1, i_structure );
1867 DecodeMVMPEG1( p_vpar, p_mb, 1, i_structure );
1871 if( i_coding_type == P_CODING_TYPE
1872 && !(p_mb->i_mb_type & (MB_MOTION_FORWARD|MB_INTRA)) )
1874 /* Special No-MC macroblock in P pictures (7.6.3.5). */
1875 p_mb->i_mb_type |= MB_MOTION_FORWARD;
1876 memset( p_vpar->mb.pppi_pmv, 0, 8*sizeof(int) );
1877 memset( p_mb->pppi_motion_vectors, 0, 8*sizeof(int) );
1878 p_vpar->mb.i_motion_type = 1 + (i_structure == FRAME_STRUCTURE);
1879 p_mb->ppi_field_select[0][0] = (i_structure == BOTTOM_FIELD);
1882 if( (i_coding_type != I_CODING_TYPE) && !(p_mb->i_mb_type & MB_INTRA) )
1884 /* Reset DC predictors (7.2.1). */
1885 p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
1886 = p_vpar->mb.pi_dc_dct_pred[2]
1887 = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
1889 /* Motion function pointer. */
1890 p_mb->pf_motion = pppf_motion[i_chroma_format]
1891 [i_structure == FRAME_STRUCTURE]
1892 [p_vpar->mb.i_motion_type];
1894 if( p_mb->i_mb_type & MB_PATTERN )
1896 switch( i_chroma_format )
1899 p_mb->i_coded_block_pattern = CodedPattern420( p_vpar );
1902 p_mb->i_coded_block_pattern = CodedPattern422( p_vpar );
1905 p_mb->i_coded_block_pattern = CodedPattern444( p_vpar );
1910 p_mb->i_coded_block_pattern = 0;
1914 * Effectively decode blocks.
1916 PARSEBLOCKS( DecodeMPEG1NonIntra, DecodeMPEG2NonIntra )
1920 if( !p_vpar->picture.b_concealment_mv )
1922 /* Reset MV predictors. */
1923 memset( p_vpar->mb.pppi_pmv, 0, 8*sizeof(int) );
1928 DecodeMVMPEG2( p_vpar, p_mb, 0, i_structure );
1930 DecodeMVMPEG1( p_vpar, p_mb, 0, i_structure );
1931 RemoveBits( &p_vpar->bit_stream, 1 );
1934 if( p_mb->i_mb_type & MB_PATTERN )
1936 switch( i_chroma_format )
1939 p_mb->i_coded_block_pattern = CodedPattern420( p_vpar );
1942 p_mb->i_coded_block_pattern = CodedPattern422( p_vpar );
1945 p_mb->i_coded_block_pattern = CodedPattern444( p_vpar );
1950 p_mb->i_coded_block_pattern =
1951 (1 << (4 + (1 << i_chroma_format))) - 1;
1955 * Effectively decode blocks.
1957 PARSEBLOCKS( DecodeMPEG1Intra, DecodeMPEG2Intra )
1960 if( !p_vpar->picture.b_error )
1962 UpdateContext( p_vpar, i_structure );
1964 /* Decode the macroblock NOW ! */
1965 vdec_DecodeMacroblock( p_vpar->pp_vdec[0], p_mb );
1970 /* Mark this block as skipped (better than green blocks), and go
1971 * to the next slice. */
1973 vpar_DestroyMacroblock( &p_vpar->vfifo, p_mb );
1978 * Picture data parsing management
1981 /*****************************************************************************
1982 * SliceHeader : Parse the next slice structure
1983 *****************************************************************************/
1984 static void SliceHeader( vpar_thread_t * p_vpar,
1985 int * pi_mb_address, int i_mb_base,
1986 u32 i_vert_code, boolean_t b_high,
1987 boolean_t b_dp_scalable,
1988 boolean_t b_mpeg2, int i_coding_type,
1989 int i_chroma_format, int i_structure,
1990 boolean_t b_second_field )
1992 int i_mb_address_save = *pi_mb_address;
1994 p_vpar->picture.b_error = 0;
1998 /* Picture with more than 2800 lines. */
1999 i_vert_code += GetBits( &p_vpar->bit_stream, 3 ) << 7;
2003 /* DATA_PARTITIONING scalability. */
2004 RemoveBits( &p_vpar->bit_stream, 7 ); /* priority_breakpoint */
2007 LoadQuantizerScale( p_vpar );
2009 if( GetBits( &p_vpar->bit_stream, 1 ) )
2011 /* intra_slice, slice_id */
2012 RemoveBits( &p_vpar->bit_stream, 8 );
2013 /* extra_information_slice */
2014 while( GetBits( &p_vpar->bit_stream, 1 ) )
2016 RemoveBits( &p_vpar->bit_stream, 8 );
2019 *pi_mb_address = (i_vert_code - 1)*p_vpar->sequence.i_mb_width;
2021 if( *pi_mb_address < i_mb_address_save )
2023 intf_ErrMsg( "vpar error: slices do not follow, maybe a PES has been trashed\n" );
2024 p_vpar->picture.b_error = 1;
2028 /* Reset DC coefficients predictors (ISO/IEC 13818-2 7.2.1). */
2029 p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
2030 = p_vpar->mb.pi_dc_dct_pred[2]
2031 = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
2033 /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
2034 memset( p_vpar->mb.pppi_pmv, 0, 8*sizeof(int) );
2038 if( i_mb_address_save >= p_vpar->sequence.i_mb_size )
2040 p_vpar->picture.b_error = 1;
2043 ParseMacroblock( p_vpar, pi_mb_address, i_mb_address_save,
2044 i_mb_base, b_mpeg2, i_coding_type,
2045 i_chroma_format, i_structure,
2047 i_mb_address_save = *pi_mb_address;
2048 if( p_vpar->picture.b_error )
2053 while( ShowBits( &p_vpar->bit_stream, 23 ) && !p_vpar->b_die );
2054 NextStartCode( p_vpar );
2057 /*****************************************************************************
2058 * PictureData : Parse off all macroblocks (ISO/IEC 13818-2 6.2.3.7)
2059 *****************************************************************************/
2060 static void PictureData( vpar_thread_t * p_vpar, int i_mb_base,
2061 boolean_t b_high, boolean_t b_dp_scalable,
2062 boolean_t b_mpeg2, int i_coding_type,
2063 int i_chroma_format, int i_structure,
2064 boolean_t b_second_field )
2066 int i_mb_address = 0;
2069 NextStartCode( p_vpar );
2070 while( ((i_coding_type != I_CODING_TYPE && i_coding_type != D_CODING_TYPE)
2071 || !p_vpar->picture.b_error)
2072 && i_mb_address+i_mb_base < p_vpar->sequence.i_mb_size
2075 if( ((i_dummy = ShowBits( &p_vpar->bit_stream, 32 ))
2076 < SLICE_START_CODE_MIN) ||
2077 (i_dummy > SLICE_START_CODE_MAX) )
2079 intf_DbgMsg("vpar debug: premature end of picture\n");
2080 p_vpar->picture.b_error = 1;
2083 RemoveBits32( &p_vpar->bit_stream );
2085 /* Decode slice data. */
2086 SliceHeader( p_vpar, &i_mb_address, i_mb_base, i_dummy & 255,
2087 b_high, b_dp_scalable, b_mpeg2, i_coding_type,
2088 i_chroma_format, i_structure, b_second_field );
2091 /* Try to recover from error. If we missed less than half the
2092 * number of macroblocks of the picture, mark the missed ones
2094 if( (i_coding_type == P_CODING_TYPE || i_coding_type == B_CODING_TYPE)
2095 && p_vpar->picture.b_error &&
2096 ( (i_mb_address-i_mb_base) > (p_vpar->sequence.i_mb_size >> 1)
2097 || (i_structure != FRAME_STRUCTURE
2098 && (i_mb_address-i_mb_base) > (p_vpar->sequence.i_mb_size >> 2) ) ) )
2102 p_vpar->picture.b_error = 0;
2103 for( i_mb = i_mb_address + 1;
2104 i_mb < (p_vpar->sequence.i_mb_size
2105 << (i_structure != FRAME_STRUCTURE));
2108 SkippedMacroblock( p_vpar, i_mb, i_mb_base, i_coding_type,
2109 i_chroma_format, i_structure, b_second_field );
2114 /*****************************************************************************
2115 * vpar_PictureDataVWXYZ : Parse the next macroblock ; specific functions
2116 *****************************************************************************
2119 * X = chroma format ?
2121 * Z = second field ?
2122 *****************************************************************************/
2123 void vpar_PictureDataGENERIC( vpar_thread_t * p_vpar, int i_mb_base )
2125 PictureData( p_vpar, i_mb_base, (p_vpar->sequence.i_height > 2800),
2126 (p_vpar->sequence.i_scalable_mode == SC_DP),
2127 p_vpar->sequence.b_mpeg2, p_vpar->picture.i_coding_type,
2128 p_vpar->sequence.i_chroma_format,
2129 p_vpar->picture.i_structure,
2130 (p_vpar->picture.i_structure !=
2131 p_vpar->picture.i_current_structure) );
2134 #if (VPAR_OPTIM_LEVEL > 0)
2135 /* Optimizations for frame pictures */
2136 void vpar_PictureData2I420F0( vpar_thread_t * p_vpar, int i_mb_base )
2138 PictureData( p_vpar, 0, 0, 0, 1, I_CODING_TYPE, CHROMA_420,
2139 FRAME_STRUCTURE, 0 );
2142 void vpar_PictureData2P420F0( vpar_thread_t * p_vpar, int i_mb_base )
2144 PictureData( p_vpar, 0, 0, 0, 1, P_CODING_TYPE, CHROMA_420,
2145 FRAME_STRUCTURE, 0 );
2148 void vpar_PictureData2B420F0( vpar_thread_t * p_vpar, int i_mb_base )
2150 PictureData( p_vpar, 0, 0, 0, 1, B_CODING_TYPE, CHROMA_420,
2151 FRAME_STRUCTURE, 0 );
2155 #if (VPAR_OPTIM_LEVEL > 1)
2156 /* Optimizations for field pictures */
2157 void vpar_PictureData2I420TZ( vpar_thread_t * p_vpar, int i_mb_base )
2159 PictureData( p_vpar, i_mb_base, 0, 0, 1, I_CODING_TYPE, CHROMA_420,
2160 TOP_FIELD, (p_vpar->picture.i_structure !=
2161 p_vpar->picture.i_current_structure) );
2164 void vpar_PictureData2P420TZ( vpar_thread_t * p_vpar, int i_mb_base )
2166 PictureData( p_vpar, i_mb_base, 0, 0, 1, P_CODING_TYPE, CHROMA_420,
2167 TOP_FIELD, (p_vpar->picture.i_structure !=
2168 p_vpar->picture.i_current_structure) );
2171 void vpar_PictureData2B420TZ( vpar_thread_t * p_vpar, int i_mb_base )
2173 PictureData( p_vpar, i_mb_base, 0, 0, 1, B_CODING_TYPE, CHROMA_420,
2174 TOP_FIELD, (p_vpar->picture.i_structure !=
2175 p_vpar->picture.i_current_structure) );
2178 void vpar_PictureData2I420BZ( vpar_thread_t * p_vpar, int i_mb_base )
2180 PictureData( p_vpar, i_mb_base, 0, 0, 1, I_CODING_TYPE, CHROMA_420,
2181 BOTTOM_FIELD, (p_vpar->picture.i_structure !=
2182 p_vpar->picture.i_current_structure) );
2185 void vpar_PictureData2P420BZ( vpar_thread_t * p_vpar, int i_mb_base )
2187 PictureData( p_vpar, i_mb_base, 0, 0, 1, P_CODING_TYPE, CHROMA_420,
2188 BOTTOM_FIELD, (p_vpar->picture.i_structure !=
2189 p_vpar->picture.i_current_structure) );
2192 void vpar_PictureData2B420BZ( vpar_thread_t * p_vpar, int i_mb_base )
2194 PictureData( p_vpar, i_mb_base, 0, 0, 1, B_CODING_TYPE, CHROMA_420,
2195 BOTTOM_FIELD, (p_vpar->picture.i_structure !=
2196 p_vpar->picture.i_current_structure) );