Parent Directory
|
Revision Log
Revision 1.30 - (view) (download)
1 : | edgomez | 1.26 | /***************************************************************************** |
2 : | * | ||
3 : | * XVID MPEG-4 VIDEO CODEC | ||
4 : | edgomez | 1.27 | * - Macro Block coding functions - |
5 : | edgomez | 1.26 | * |
6 : | * Copyright(C) 2002 Michael Militzer <isibaar@xvid.org> | ||
7 : | * | ||
8 : | * | ||
9 : | * This program is an implementation of a part of one or more MPEG-4 | ||
10 : | * Video tools as specified in ISO/IEC 14496-2 standard. Those intending | ||
11 : | * to use this software module in hardware or software products are | ||
12 : | * advised that its use may infringe existing patents or copyrights, and | ||
13 : | * any such use would be at such party's own risk. The original | ||
14 : | * developer of this software module and his/her company, and subsequent | ||
15 : | * editors and their companies, will have no liability for use of this | ||
16 : | * software or modifications or derivatives thereof. | ||
17 : | * | ||
18 : | * This program is free software; you can redistribute it and/or modify | ||
19 : | * it under the terms of the GNU General Public License as published by | ||
20 : | * the Free Software Foundation; either version 2 of the License, or | ||
21 : | * (at your option) any later version. | ||
22 : | * | ||
23 : | * This program is distributed in the hope that it will be useful, | ||
24 : | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
25 : | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
26 : | * GNU General Public License for more details. | ||
27 : | * | ||
28 : | * You should have received a copy of the GNU General Public License | ||
29 : | * along with this program; if not, write to the Free Software | ||
30 : | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
31 : | * | ||
32 : | edgomez | 1.30 | * $Id: mbcoding.c,v 1.29 2002/09/10 21:29:03 edgomez Exp $ |
33 : | edgomez | 1.26 | * |
34 : | ****************************************************************************/ | ||
35 : | suxen_drol | 1.11 | |
36 : | Isibaar | 1.8 | #include <stdlib.h> |
37 : | Isibaar | 1.1 | #include "../portab.h" |
38 : | #include "bitstream.h" | ||
39 : | #include "zigzag.h" | ||
40 : | #include "vlc_codes.h" | ||
41 : | Isibaar | 1.8 | #include "mbcoding.h" |
42 : | Isibaar | 1.1 | |
43 : | #include "../utils/mbfunctions.h" | ||
44 : | |||
45 : | #define ABS(X) (((X)>0)?(X):-(X)) | ||
46 : | #define CLIP(X,A) (X > A) ? (A) : (X) | ||
47 : | |||
48 : | edgomez | 1.26 | /***************************************************************************** |
49 : | * Local data | ||
50 : | ****************************************************************************/ | ||
51 : | |||
52 : | static VLC intra_table[524032]; | ||
53 : | static VLC inter_table[524032]; | ||
54 : | |||
55 : | static VLC DCT3Dintra[4096]; | ||
56 : | static VLC DCT3Dinter[4096]; | ||
57 : | |||
58 : | /***************************************************************************** | ||
59 : | edgomez | 1.27 | * Vector Length Coding Initialization |
60 : | edgomez | 1.26 | ****************************************************************************/ |
61 : | Isibaar | 1.1 | |
62 : | edgomez | 1.17 | void |
63 : | init_vlc_tables(void) | ||
64 : | Isibaar | 1.1 | { |
65 : | edgomez | 1.7 | |
66 : | Isibaar | 1.1 | int32_t k, l, i, intra, last; |
67 : | VLC *vlc[2]; | ||
68 : | edgomez | 1.29 | VLC const **coeff_ptr; |
69 : | Isibaar | 1.1 | VLC *vlc1, *vlc2; |
70 : | |||
71 : | vlc1 = DCT3Dintra; | ||
72 : | vlc2 = DCT3Dinter; | ||
73 : | edgomez | 1.17 | |
74 : | Isibaar | 1.8 | vlc[0] = intra_table; |
75 : | vlc[1] = inter_table; | ||
76 : | edgomez | 1.17 | |
77 : | edgomez | 1.26 | /* |
78 : | * Generate encoding vlc lookup tables | ||
79 : | * the lookup table idea is taken from the excellent fame project | ||
80 : | * by Vivien Chapellier | ||
81 : | */ | ||
82 : | edgomez | 1.17 | for (i = 0; i < 4; i++) { |
83 : | Isibaar | 1.1 | intra = i % 2; |
84 : | Isibaar | 1.8 | last = i / 2; |
85 : | Isibaar | 1.1 | |
86 : | Isibaar | 1.8 | coeff_ptr = coeff_vlc[last + 2 * intra]; |
87 : | edgomez | 1.17 | |
88 : | for (k = -2047; k < 2048; k++) { // level | ||
89 : | edgomez | 1.29 | int8_t const *max_level_ptr = max_level[last + 2 * intra]; |
90 : | int8_t const *max_run_ptr = max_run[last + 2 * intra]; | ||
91 : | edgomez | 1.17 | |
92 : | for (l = 0; l < 64; l++) { // run | ||
93 : | Isibaar | 1.5 | int32_t level = k; |
94 : | Isibaar | 1.18 | ptr_t run = l; |
95 : | edgomez | 1.17 | |
96 : | if ((abs(level) <= max_level_ptr[run]) && (run <= (uint32_t) max_run_ptr[abs(level)])) { // level < max_level and run < max_run | ||
97 : | |||
98 : | vlc[intra]->code = 0; | ||
99 : | vlc[intra]->len = 0; | ||
100 : | goto loop_end; | ||
101 : | } else { | ||
102 : | if (level > 0) // correct level | ||
103 : | Isibaar | 1.1 | level -= max_level_ptr[run]; |
104 : | else | ||
105 : | level += max_level_ptr[run]; | ||
106 : | Isibaar | 1.5 | |
107 : | edgomez | 1.17 | if ((abs(level) <= max_level_ptr[run]) && |
108 : | (run <= (uint32_t) max_run_ptr[abs(level)])) { | ||
109 : | |||
110 : | Isibaar | 1.8 | vlc[intra]->code = 0x06; |
111 : | vlc[intra]->len = 8; | ||
112 : | goto loop_end; | ||
113 : | } | ||
114 : | |||
115 : | edgomez | 1.17 | if (level > 0) // still here? |
116 : | level += max_level_ptr[run]; // restore level | ||
117 : | Isibaar | 1.8 | else |
118 : | level -= max_level_ptr[run]; | ||
119 : | |||
120 : | edgomez | 1.17 | run -= max_run_ptr[abs(level)] + 1; // and change run |
121 : | |||
122 : | if ((abs(level) <= max_level_ptr[run]) && | ||
123 : | (run <= (uint32_t) max_run_ptr[abs(level)])) { | ||
124 : | Isibaar | 1.8 | |
125 : | vlc[intra]->code = 0x0e; | ||
126 : | vlc[intra]->len = 9; | ||
127 : | goto loop_end; | ||
128 : | Isibaar | 1.1 | } |
129 : | Isibaar | 1.8 | run += max_run_ptr[abs(level)] + 1; |
130 : | Isibaar | 1.1 | } |
131 : | Isibaar | 1.8 | |
132 : | edgomez | 1.17 | vlc[intra]->code = |
133 : | (uint32_t) ((l << 14) | (0x1e + last) << 20) | (1 << 13) | | ||
134 : | ((k & 0xfff) << 1) | 1; | ||
135 : | Isibaar | 1.8 | |
136 : | vlc[intra]->len = 30; | ||
137 : | vlc[intra]++; | ||
138 : | continue; | ||
139 : | |||
140 : | edgomez | 1.17 | loop_end: |
141 : | if (level != 0) { | ||
142 : | vlc[intra]->code = | ||
143 : | (vlc[intra]-> | ||
144 : | code << (coeff_ptr[run][abs(level) - 1].len + | ||
145 : | 1)) | (coeff_ptr[run][abs(level) - | ||
146 : | 1].code << 1); | ||
147 : | vlc[intra]->len = | ||
148 : | (coeff_ptr[run][abs(level) - 1].len + 1) + | ||
149 : | vlc[intra]->len; | ||
150 : | Isibaar | 1.8 | |
151 : | edgomez | 1.17 | if (level < 0) |
152 : | Isibaar | 1.8 | vlc[intra]->code += 1; |
153 : | } | ||
154 : | |||
155 : | Isibaar | 1.1 | vlc[intra]++; |
156 : | } | ||
157 : | } | ||
158 : | } | ||
159 : | edgomez | 1.7 | |
160 : | edgomez | 1.17 | for (i = 0; i < 4096; i++) { |
161 : | if (i >= 512) { | ||
162 : | Isibaar | 1.1 | *vlc1 = DCT3Dtab3[(i >> 5) - 16]; |
163 : | *vlc2 = DCT3Dtab0[(i >> 5) - 16]; | ||
164 : | edgomez | 1.17 | } else if (i >= 128) { |
165 : | Isibaar | 1.1 | *vlc1 = DCT3Dtab4[(i >> 2) - 32]; |
166 : | *vlc2 = DCT3Dtab1[(i >> 2) - 32]; | ||
167 : | edgomez | 1.17 | } else if (i >= 8) { |
168 : | Isibaar | 1.1 | *vlc1 = DCT3Dtab5[i - 8]; |
169 : | *vlc2 = DCT3Dtab2[i - 8]; | ||
170 : | edgomez | 1.17 | } else { |
171 : | Isibaar | 1.1 | *vlc1 = ERRtab[i]; |
172 : | *vlc2 = ERRtab[i]; | ||
173 : | } | ||
174 : | |||
175 : | vlc1++; | ||
176 : | vlc2++; | ||
177 : | } | ||
178 : | DCT3D[0] = DCT3Dinter; | ||
179 : | DCT3D[1] = DCT3Dintra; | ||
180 : | |||
181 : | } | ||
182 : | |||
183 : | edgomez | 1.27 | /***************************************************************************** |
184 : | * Local inlined functions for MB coding | ||
185 : | ****************************************************************************/ | ||
186 : | |||
187 : | edgomez | 1.17 | static __inline void |
188 : | CodeVector(Bitstream * bs, | ||
189 : | int32_t value, | ||
190 : | int32_t f_code, | ||
191 : | Statistics * pStat) | ||
192 : | Isibaar | 1.1 | { |
193 : | edgomez | 1.7 | |
194 : | Isibaar | 1.1 | const int scale_factor = 1 << (f_code - 1); |
195 : | const int cmp = scale_factor << 5; | ||
196 : | |||
197 : | edgomez | 1.17 | if (value < (-1 * cmp)) |
198 : | Isibaar | 1.1 | value += 64 * scale_factor; |
199 : | edgomez | 1.17 | |
200 : | if (value > (cmp - 1)) | ||
201 : | Isibaar | 1.1 | value -= 64 * scale_factor; |
202 : | |||
203 : | edgomez | 1.7 | pStat->iMvSum += value * value; |
204 : | pStat->iMvCount++; | ||
205 : | Isibaar | 1.1 | |
206 : | edgomez | 1.7 | if (value == 0) { |
207 : | edgomez | 1.17 | BitstreamPutBits(bs, mb_motion_table[32].code, |
208 : | mb_motion_table[32].len); | ||
209 : | edgomez | 1.7 | } else { |
210 : | Isibaar | 1.1 | uint16_t length, code, mv_res, sign; |
211 : | edgomez | 1.17 | |
212 : | Isibaar | 1.1 | length = 16 << f_code; |
213 : | f_code--; | ||
214 : | edgomez | 1.17 | |
215 : | Isibaar | 1.1 | sign = (value < 0); |
216 : | |||
217 : | edgomez | 1.17 | if (value >= length) |
218 : | Isibaar | 1.1 | value -= 2 * length; |
219 : | edgomez | 1.17 | else if (value < -length) |
220 : | Isibaar | 1.1 | value += 2 * length; |
221 : | |||
222 : | edgomez | 1.17 | if (sign) |
223 : | Isibaar | 1.1 | value = -value; |
224 : | |||
225 : | value--; | ||
226 : | mv_res = value & ((1 << f_code) - 1); | ||
227 : | code = ((value - mv_res) >> f_code) + 1; | ||
228 : | |||
229 : | edgomez | 1.17 | if (sign) |
230 : | Isibaar | 1.1 | code = -code; |
231 : | |||
232 : | code += 32; | ||
233 : | edgomez | 1.17 | BitstreamPutBits(bs, mb_motion_table[code].code, |
234 : | mb_motion_table[code].len); | ||
235 : | |||
236 : | if (f_code) | ||
237 : | Isibaar | 1.1 | BitstreamPutBits(bs, mv_res, f_code); |
238 : | edgomez | 1.7 | } |
239 : | |||
240 : | Isibaar | 1.1 | } |
241 : | |||
242 : | edgomez | 1.17 | static __inline void |
243 : | CodeCoeff(Bitstream * bs, | ||
244 : | const int16_t qcoeff[64], | ||
245 : | VLC * table, | ||
246 : | const uint16_t * zigzag, | ||
247 : | uint16_t intra) | ||
248 : | edgomez | 1.7 | { |
249 : | |||
250 : | Isibaar | 1.1 | uint32_t j, last; |
251 : | short v; | ||
252 : | VLC *vlc; | ||
253 : | edgomez | 1.17 | |
254 : | Isibaar | 1.1 | j = intra; |
255 : | Isibaar | 1.10 | last = intra; |
256 : | Isibaar | 1.1 | |
257 : | edgomez | 1.17 | while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) |
258 : | j++; | ||
259 : | |||
260 : | Isibaar | 1.1 | do { |
261 : | Isibaar | 1.15 | vlc = table + 64 * 2047 + (v << 6) + j - last; |
262 : | last = ++j; | ||
263 : | |||
264 : | Isibaar | 1.1 | // count zeroes |
265 : | edgomez | 1.17 | while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) |
266 : | j++; | ||
267 : | |||
268 : | Isibaar | 1.1 | // write code |
269 : | edgomez | 1.17 | if (j != 64) { |
270 : | Isibaar | 1.1 | BitstreamPutBits(bs, vlc->code, vlc->len); |
271 : | } else { | ||
272 : | Isibaar | 1.15 | vlc += 64 * 4095; |
273 : | Isibaar | 1.1 | BitstreamPutBits(bs, vlc->code, vlc->len); |
274 : | break; | ||
275 : | } | ||
276 : | edgomez | 1.17 | } while (1); |
277 : | edgomez | 1.7 | |
278 : | Isibaar | 1.1 | } |
279 : | |||
280 : | edgomez | 1.27 | /***************************************************************************** |
281 : | * Local functions | ||
282 : | ****************************************************************************/ | ||
283 : | Isibaar | 1.1 | |
284 : | edgomez | 1.17 | static void |
285 : | CodeBlockIntra(const FRAMEINFO * frame, | ||
286 : | const MACROBLOCK * pMB, | ||
287 : | int16_t qcoeff[6 * 64], | ||
288 : | edgomez | 1.7 | Bitstream * bs, |
289 : | Statistics * pStat) | ||
290 : | Isibaar | 1.1 | { |
291 : | edgomez | 1.7 | |
292 : | Isibaar | 1.1 | uint32_t i, mcbpc, cbpy, bits; |
293 : | |||
294 : | cbpy = pMB->cbp >> 2; | ||
295 : | |||
296 : | edgomez | 1.7 | // write mcbpc |
297 : | edgomez | 1.17 | if (frame->coding_type == I_VOP) { |
298 : | edgomez | 1.7 | mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2); |
299 : | edgomez | 1.17 | BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, |
300 : | mcbpc_intra_tab[mcbpc].len); | ||
301 : | } else { | ||
302 : | edgomez | 1.7 | mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
303 : | edgomez | 1.17 | BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, |
304 : | mcbpc_inter_tab[mcbpc].len); | ||
305 : | Isibaar | 1.4 | } |
306 : | Isibaar | 1.1 | |
307 : | // ac prediction flag | ||
308 : | edgomez | 1.17 | if (pMB->acpred_directions[0]) |
309 : | edgomez | 1.7 | BitstreamPutBits(bs, 1, 1); |
310 : | Isibaar | 1.1 | else |
311 : | edgomez | 1.7 | BitstreamPutBits(bs, 0, 1); |
312 : | Isibaar | 1.1 | |
313 : | edgomez | 1.7 | // write cbpy |
314 : | edgomez | 1.17 | BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
315 : | Isibaar | 1.1 | |
316 : | // write dquant | ||
317 : | edgomez | 1.17 | if (pMB->mode == MODE_INTRA_Q) |
318 : | Isibaar | 1.1 | BitstreamPutBits(bs, pMB->dquant, 2); |
319 : | |||
320 : | h | 1.6 | // write interlacing |
321 : | edgomez | 1.17 | if (frame->global_flags & XVID_INTERLACING) { |
322 : | h | 1.6 | BitstreamPutBit(bs, pMB->field_dct); |
323 : | } | ||
324 : | Isibaar | 1.1 | // code block coeffs |
325 : | edgomez | 1.17 | for (i = 0; i < 6; i++) { |
326 : | if (i < 4) | ||
327 : | BitstreamPutBits(bs, dcy_tab[qcoeff[i * 64 + 0] + 255].code, | ||
328 : | dcy_tab[qcoeff[i * 64 + 0] + 255].len); | ||
329 : | Isibaar | 1.1 | else |
330 : | edgomez | 1.17 | BitstreamPutBits(bs, dcc_tab[qcoeff[i * 64 + 0] + 255].code, |
331 : | dcc_tab[qcoeff[i * 64 + 0] + 255].len); | ||
332 : | |||
333 : | if (pMB->cbp & (1 << (5 - i))) { | ||
334 : | Isibaar | 1.1 | bits = BitstreamPos(bs); |
335 : | |||
336 : | edgomez | 1.17 | CodeCoeff(bs, &qcoeff[i * 64], intra_table, |
337 : | scan_tables[pMB->acpred_directions[i]], 1); | ||
338 : | Isibaar | 1.1 | |
339 : | bits = BitstreamPos(bs) - bits; | ||
340 : | pStat->iTextBits += bits; | ||
341 : | } | ||
342 : | } | ||
343 : | edgomez | 1.7 | |
344 : | Isibaar | 1.1 | } |
345 : | |||
346 : | |||
347 : | edgomez | 1.17 | static void |
348 : | CodeBlockInter(const FRAMEINFO * frame, | ||
349 : | const MACROBLOCK * pMB, | ||
350 : | int16_t qcoeff[6 * 64], | ||
351 : | edgomez | 1.7 | Bitstream * bs, |
352 : | Statistics * pStat) | ||
353 : | Isibaar | 1.1 | { |
354 : | edgomez | 1.7 | |
355 : | Isibaar | 1.1 | int32_t i; |
356 : | uint32_t bits, mcbpc, cbpy; | ||
357 : | |||
358 : | edgomez | 1.7 | mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
359 : | Isibaar | 1.1 | cbpy = 15 - (pMB->cbp >> 2); |
360 : | |||
361 : | // write mcbpc | ||
362 : | edgomez | 1.17 | BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, |
363 : | mcbpc_inter_tab[mcbpc].len); | ||
364 : | Isibaar | 1.1 | |
365 : | // write cbpy | ||
366 : | BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); | ||
367 : | |||
368 : | // write dquant | ||
369 : | edgomez | 1.17 | if (pMB->mode == MODE_INTER_Q) |
370 : | Isibaar | 1.1 | BitstreamPutBits(bs, pMB->dquant, 2); |
371 : | edgomez | 1.17 | |
372 : | h | 1.6 | // interlacing |
373 : | edgomez | 1.17 | if (frame->global_flags & XVID_INTERLACING) { |
374 : | h | 1.25 | if (pMB->cbp) { |
375 : | BitstreamPutBit(bs, pMB->field_dct); | ||
376 : | DEBUG1("codep: field_dct: ", pMB->field_dct); | ||
377 : | } | ||
378 : | h | 1.6 | |
379 : | // if inter block, write field ME flag | ||
380 : | edgomez | 1.17 | if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
381 : | h | 1.6 | BitstreamPutBit(bs, pMB->field_pred); |
382 : | DEBUG1("codep: field_pred: ", pMB->field_pred); | ||
383 : | |||
384 : | // write field prediction references | ||
385 : | edgomez | 1.17 | if (pMB->field_pred) { |
386 : | h | 1.6 | BitstreamPutBit(bs, pMB->field_for_top); |
387 : | BitstreamPutBit(bs, pMB->field_for_bot); | ||
388 : | } | ||
389 : | } | ||
390 : | } | ||
391 : | Isibaar | 1.1 | // code motion vector(s) |
392 : | edgomez | 1.17 | for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) { |
393 : | suxen_drol | 1.13 | CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat); |
394 : | CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat); | ||
395 : | Isibaar | 1.1 | } |
396 : | |||
397 : | bits = BitstreamPos(bs); | ||
398 : | edgomez | 1.17 | |
399 : | Isibaar | 1.1 | // code block coeffs |
400 : | edgomez | 1.17 | for (i = 0; i < 6; i++) |
401 : | if (pMB->cbp & (1 << (5 - i))) | ||
402 : | CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[0], 0); | ||
403 : | Isibaar | 1.1 | |
404 : | bits = BitstreamPos(bs) - bits; | ||
405 : | pStat->iTextBits += bits; | ||
406 : | edgomez | 1.7 | |
407 : | Isibaar | 1.1 | } |
408 : | |||
409 : | edgomez | 1.27 | /***************************************************************************** |
410 : | * Macro Block bitstream encoding functions | ||
411 : | ****************************************************************************/ | ||
412 : | Isibaar | 1.1 | |
413 : | edgomez | 1.17 | void |
414 : | MBCoding(const FRAMEINFO * frame, | ||
415 : | MACROBLOCK * pMB, | ||
416 : | int16_t qcoeff[6 * 64], | ||
417 : | Bitstream * bs, | ||
418 : | Statistics * pStat) | ||
419 : | Isibaar | 1.1 | { |
420 : | edgomez | 1.7 | |
421 : | edgomez | 1.17 | if (frame->coding_type == P_VOP) { |
422 : | BitstreamPutBit(bs, 0); // coded | ||
423 : | Isibaar | 1.1 | } |
424 : | |||
425 : | chl | 1.22 | if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) |
426 : | suxen_drol | 1.13 | CodeBlockIntra(frame, pMB, qcoeff, bs, pStat); |
427 : | Isibaar | 1.1 | else |
428 : | suxen_drol | 1.13 | CodeBlockInter(frame, pMB, qcoeff, bs, pStat); |
429 : | edgomez | 1.7 | |
430 : | Isibaar | 1.1 | } |
431 : | chl | 1.24 | |
432 : | |||
433 : | void | ||
434 : | MBSkip(Bitstream * bs) | ||
435 : | { | ||
436 : | BitstreamPutBit(bs, 1); // not coded | ||
437 : | return; | ||
438 : | } | ||
439 : | Isibaar | 1.1 | |
440 : | edgomez | 1.27 | /***************************************************************************** |
441 : | * decoding stuff starts here | ||
442 : | ****************************************************************************/ | ||
443 : | Isibaar | 1.1 | |
444 : | edgomez | 1.27 | /* |
445 : | * For IVOP addbits == 0 | ||
446 : | * For PVOP addbits == fcode - 1 | ||
447 : | * For BVOP addbits == max(fcode,bcode) - 1 | ||
448 : | * returns true or false | ||
449 : | */ | ||
450 : | suxen_drol | 1.19 | |
451 : | int | ||
452 : | check_resync_marker(Bitstream * bs, int addbits) | ||
453 : | { | ||
454 : | uint32_t nbits; | ||
455 : | uint32_t code; | ||
456 : | uint32_t nbitsresyncmarker = NUMBITS_VP_RESYNC_MARKER + addbits; | ||
457 : | |||
458 : | nbits = BitstreamNumBitsToByteAlign(bs); | ||
459 : | code = BitstreamShowBits(bs, nbits); | ||
460 : | |||
461 : | if (code == (((uint32_t)1 << (nbits - 1)) - 1)) | ||
462 : | { | ||
463 : | return BitstreamShowBitsFromByteAlign(bs, nbitsresyncmarker) == RESYNC_MARKER; | ||
464 : | } | ||
465 : | |||
466 : | return 0; | ||
467 : | } | ||
468 : | |||
469 : | |||
470 : | |||
471 : | edgomez | 1.17 | int |
472 : | get_mcbpc_intra(Bitstream * bs) | ||
473 : | Isibaar | 1.1 | { |
474 : | edgomez | 1.7 | |
475 : | Isibaar | 1.1 | uint32_t index; |
476 : | edgomez | 1.17 | |
477 : | suxen_drol | 1.19 | index = BitstreamShowBits(bs, 9); |
478 : | Isibaar | 1.1 | index >>= 3; |
479 : | |||
480 : | BitstreamSkip(bs, mcbpc_intra_table[index].len); | ||
481 : | edgomez | 1.7 | |
482 : | Isibaar | 1.1 | return mcbpc_intra_table[index].code; |
483 : | edgomez | 1.7 | |
484 : | Isibaar | 1.1 | } |
485 : | |||
486 : | edgomez | 1.17 | int |
487 : | get_mcbpc_inter(Bitstream * bs) | ||
488 : | Isibaar | 1.1 | { |
489 : | edgomez | 1.7 | |
490 : | Isibaar | 1.1 | uint32_t index; |
491 : | suxen_drol | 1.19 | |
492 : | index = CLIP(BitstreamShowBits(bs, 9), 256); | ||
493 : | Isibaar | 1.1 | |
494 : | edgomez | 1.17 | BitstreamSkip(bs, mcbpc_inter_table[index].len); |
495 : | edgomez | 1.7 | |
496 : | Isibaar | 1.1 | return mcbpc_inter_table[index].code; |
497 : | edgomez | 1.7 | |
498 : | Isibaar | 1.1 | } |
499 : | |||
500 : | edgomez | 1.17 | int |
501 : | get_cbpy(Bitstream * bs, | ||
502 : | int intra) | ||
503 : | Isibaar | 1.1 | { |
504 : | edgomez | 1.7 | |
505 : | Isibaar | 1.1 | int cbpy; |
506 : | uint32_t index = BitstreamShowBits(bs, 6); | ||
507 : | |||
508 : | BitstreamSkip(bs, cbpy_table[index].len); | ||
509 : | cbpy = cbpy_table[index].code; | ||
510 : | |||
511 : | edgomez | 1.17 | if (!intra) |
512 : | Isibaar | 1.1 | cbpy = 15 - cbpy; |
513 : | |||
514 : | return cbpy; | ||
515 : | edgomez | 1.7 | |
516 : | Isibaar | 1.1 | } |
517 : | |||
518 : | edgomez | 1.17 | int |
519 : | get_mv_data(Bitstream * bs) | ||
520 : | Isibaar | 1.1 | { |
521 : | edgomez | 1.7 | |
522 : | Isibaar | 1.1 | uint32_t index; |
523 : | |||
524 : | edgomez | 1.17 | if (BitstreamGetBit(bs)) |
525 : | Isibaar | 1.1 | return 0; |
526 : | edgomez | 1.17 | |
527 : | Isibaar | 1.1 | index = BitstreamShowBits(bs, 12); |
528 : | |||
529 : | edgomez | 1.17 | if (index >= 512) { |
530 : | Isibaar | 1.1 | index = (index >> 8) - 2; |
531 : | BitstreamSkip(bs, TMNMVtab0[index].len); | ||
532 : | return TMNMVtab0[index].code; | ||
533 : | } | ||
534 : | edgomez | 1.17 | |
535 : | if (index >= 128) { | ||
536 : | Isibaar | 1.1 | index = (index >> 2) - 32; |
537 : | BitstreamSkip(bs, TMNMVtab1[index].len); | ||
538 : | return TMNMVtab1[index].code; | ||
539 : | } | ||
540 : | |||
541 : | edgomez | 1.17 | index -= 4; |
542 : | Isibaar | 1.1 | |
543 : | BitstreamSkip(bs, TMNMVtab2[index].len); | ||
544 : | return TMNMVtab2[index].code; | ||
545 : | edgomez | 1.7 | |
546 : | Isibaar | 1.1 | } |
547 : | |||
548 : | edgomez | 1.17 | int |
549 : | get_mv(Bitstream * bs, | ||
550 : | int fcode) | ||
551 : | Isibaar | 1.1 | { |
552 : | edgomez | 1.7 | |
553 : | Isibaar | 1.1 | int data; |
554 : | int res; | ||
555 : | int mv; | ||
556 : | int scale_fac = 1 << (fcode - 1); | ||
557 : | |||
558 : | data = get_mv_data(bs); | ||
559 : | edgomez | 1.17 | |
560 : | if (scale_fac == 1 || data == 0) | ||
561 : | Isibaar | 1.1 | return data; |
562 : | |||
563 : | res = BitstreamGetBits(bs, fcode - 1); | ||
564 : | mv = ((ABS(data) - 1) * scale_fac) + res + 1; | ||
565 : | edgomez | 1.17 | |
566 : | Isibaar | 1.1 | return data < 0 ? -mv : mv; |
567 : | edgomez | 1.7 | |
568 : | Isibaar | 1.1 | } |
569 : | |||
570 : | edgomez | 1.17 | int |
571 : | get_dc_dif(Bitstream * bs, | ||
572 : | uint32_t dc_size) | ||
573 : | Isibaar | 1.1 | { |
574 : | edgomez | 1.7 | |
575 : | Isibaar | 1.1 | int code = BitstreamGetBits(bs, dc_size); |
576 : | int msb = code >> (dc_size - 1); | ||
577 : | |||
578 : | edgomez | 1.17 | if (msb == 0) |
579 : | return (-1 * (code ^ ((1 << dc_size) - 1))); | ||
580 : | Isibaar | 1.1 | |
581 : | return code; | ||
582 : | edgomez | 1.7 | |
583 : | Isibaar | 1.1 | } |
584 : | |||
585 : | edgomez | 1.17 | int |
586 : | get_dc_size_lum(Bitstream * bs) | ||
587 : | Isibaar | 1.1 | { |
588 : | edgomez | 1.7 | |
589 : | Isibaar | 1.1 | int code, i; |
590 : | edgomez | 1.17 | |
591 : | Isibaar | 1.1 | code = BitstreamShowBits(bs, 11); |
592 : | |||
593 : | edgomez | 1.17 | for (i = 11; i > 3; i--) { |
594 : | if (code == 1) { | ||
595 : | Isibaar | 1.1 | BitstreamSkip(bs, i); |
596 : | return i + 1; | ||
597 : | } | ||
598 : | code >>= 1; | ||
599 : | } | ||
600 : | |||
601 : | BitstreamSkip(bs, dc_lum_tab[code].len); | ||
602 : | return dc_lum_tab[code].code; | ||
603 : | edgomez | 1.7 | |
604 : | Isibaar | 1.1 | } |
605 : | |||
606 : | |||
607 : | edgomez | 1.17 | int |
608 : | get_dc_size_chrom(Bitstream * bs) | ||
609 : | Isibaar | 1.1 | { |
610 : | edgomez | 1.7 | |
611 : | Isibaar | 1.1 | uint32_t code, i; |
612 : | edgomez | 1.17 | |
613 : | Isibaar | 1.1 | code = BitstreamShowBits(bs, 12); |
614 : | |||
615 : | edgomez | 1.17 | for (i = 12; i > 2; i--) { |
616 : | if (code == 1) { | ||
617 : | Isibaar | 1.1 | BitstreamSkip(bs, i); |
618 : | return i; | ||
619 : | } | ||
620 : | code >>= 1; | ||
621 : | } | ||
622 : | |||
623 : | return 3 - BitstreamGetBits(bs, 2); | ||
624 : | edgomez | 1.7 | |
625 : | Isibaar | 1.1 | } |
626 : | edgomez | 1.27 | |
627 : | /***************************************************************************** | ||
628 : | * Local inlined function to "decode" written vlc codes | ||
629 : | ****************************************************************************/ | ||
630 : | |||
631 : | static __inline int | ||
632 : | get_coeff(Bitstream * bs, | ||
633 : | int *run, | ||
634 : | int *last, | ||
635 : | int intra, | ||
636 : | int short_video_header) | ||
637 : | { | ||
638 : | |||
639 : | uint32_t mode; | ||
640 : | const VLC *tab; | ||
641 : | int32_t level; | ||
642 : | |||
643 : | if (short_video_header) // inter-VLCs will be used for both intra and inter blocks | ||
644 : | intra = 0; | ||
645 : | |||
646 : | tab = &DCT3D[intra][BitstreamShowBits(bs, 12)]; | ||
647 : | |||
648 : | if (tab->code == -1) | ||
649 : | goto error; | ||
650 : | |||
651 : | BitstreamSkip(bs, tab->len); | ||
652 : | |||
653 : | if (tab->code != ESCAPE) { | ||
654 : | if (!intra) { | ||
655 : | *run = (tab->code >> 4) & 255; | ||
656 : | level = tab->code & 15; | ||
657 : | *last = (tab->code >> 12) & 1; | ||
658 : | } else { | ||
659 : | *run = (tab->code >> 8) & 255; | ||
660 : | level = tab->code & 255; | ||
661 : | *last = (tab->code >> 16) & 1; | ||
662 : | } | ||
663 : | return BitstreamGetBit(bs) ? -level : level; | ||
664 : | } | ||
665 : | |||
666 : | if (short_video_header) { | ||
667 : | // escape mode 4 - H.263 type, only used if short_video_header = 1 | ||
668 : | *last = BitstreamGetBit(bs); | ||
669 : | *run = BitstreamGetBits(bs, 6); | ||
670 : | level = BitstreamGetBits(bs, 8); | ||
671 : | |||
672 : | if (level == 0 || level == 128) | ||
673 : | DEBUG1("Illegal LEVEL for ESCAPE mode 4:", level); | ||
674 : | |||
675 : | return (level >= 128 ? -(256 - level) : level); | ||
676 : | } | ||
677 : | |||
678 : | mode = BitstreamShowBits(bs, 2); | ||
679 : | |||
680 : | if (mode < 3) { | ||
681 : | BitstreamSkip(bs, (mode == 2) ? 2 : 1); | ||
682 : | |||
683 : | tab = &DCT3D[intra][BitstreamShowBits(bs, 12)]; | ||
684 : | if (tab->code == -1) | ||
685 : | goto error; | ||
686 : | |||
687 : | BitstreamSkip(bs, tab->len); | ||
688 : | |||
689 : | if (!intra) { | ||
690 : | *run = (tab->code >> 4) & 255; | ||
691 : | level = tab->code & 15; | ||
692 : | *last = (tab->code >> 12) & 1; | ||
693 : | } else { | ||
694 : | *run = (tab->code >> 8) & 255; | ||
695 : | level = tab->code & 255; | ||
696 : | *last = (tab->code >> 16) & 1; | ||
697 : | } | ||
698 : | |||
699 : | if (mode < 2) // first escape mode, level is offset | ||
700 : | level += max_level[*last + (!intra << 1)][*run]; // need to add back the max level | ||
701 : | else if (mode == 2) // second escape mode, run is offset | ||
702 : | *run += max_run[*last + (!intra << 1)][level] + 1; | ||
703 : | |||
704 : | return BitstreamGetBit(bs) ? -level : level; | ||
705 : | } | ||
706 : | // third escape mode - fixed length codes | ||
707 : | BitstreamSkip(bs, 2); | ||
708 : | *last = BitstreamGetBits(bs, 1); | ||
709 : | *run = BitstreamGetBits(bs, 6); | ||
710 : | BitstreamSkip(bs, 1); // marker | ||
711 : | level = BitstreamGetBits(bs, 12); | ||
712 : | BitstreamSkip(bs, 1); // marker | ||
713 : | |||
714 : | return (level & 0x800) ? (level | (-1 ^ 0xfff)) : level; | ||
715 : | |||
716 : | error: | ||
717 : | *run = VLC_ERROR; | ||
718 : | return 0; | ||
719 : | |||
720 : | } | ||
721 : | |||
722 : | /***************************************************************************** | ||
723 : | * MB reading functions | ||
724 : | ****************************************************************************/ | ||
725 : | Isibaar | 1.1 | |
726 : | edgomez | 1.17 | void |
727 : | get_intra_block(Bitstream * bs, | ||
728 : | int16_t * block, | ||
729 : | int direction, | ||
730 : | int coeff) | ||
731 : | Isibaar | 1.1 | { |
732 : | edgomez | 1.7 | |
733 : | edgomez | 1.17 | const uint16_t *scan = scan_tables[direction]; |
734 : | Isibaar | 1.1 | int level; |
735 : | int run; | ||
736 : | int last; | ||
737 : | |||
738 : | edgomez | 1.17 | do { |
739 : | Isibaar | 1.1 | level = get_coeff(bs, &run, &last, 1, 0); |
740 : | edgomez | 1.17 | if (run == -1) { |
741 : | Isibaar | 1.1 | DEBUG("fatal: invalid run"); |
742 : | break; | ||
743 : | } | ||
744 : | coeff += run; | ||
745 : | edgomez | 1.17 | block[scan[coeff]] = level; |
746 : | suxen_drol | 1.20 | |
747 : | DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level); | ||
748 : | //DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[coeff], level, BitstreamShowBits(bs, 32)); | ||
749 : | |||
750 : | edgomez | 1.17 | if (level < -127 || level > 127) { |
751 : | Isibaar | 1.1 | DEBUG1("warning: intra_overflow", level); |
752 : | } | ||
753 : | coeff++; | ||
754 : | } while (!last); | ||
755 : | edgomez | 1.7 | |
756 : | Isibaar | 1.1 | } |
757 : | |||
758 : | edgomez | 1.17 | void |
759 : | get_inter_block(Bitstream * bs, | ||
760 : | int16_t * block) | ||
761 : | Isibaar | 1.1 | { |
762 : | edgomez | 1.7 | |
763 : | edgomez | 1.17 | const uint16_t *scan = scan_tables[0]; |
764 : | Isibaar | 1.1 | int p; |
765 : | int level; | ||
766 : | int run; | ||
767 : | int last; | ||
768 : | |||
769 : | p = 0; | ||
770 : | edgomez | 1.17 | do { |
771 : | Isibaar | 1.1 | level = get_coeff(bs, &run, &last, 0, 0); |
772 : | edgomez | 1.17 | if (run == -1) { |
773 : | Isibaar | 1.1 | DEBUG("fatal: invalid run"); |
774 : | break; | ||
775 : | } | ||
776 : | p += run; | ||
777 : | chenm001 | 1.16 | |
778 : | edgomez | 1.17 | block[scan[p]] = level; |
779 : | suxen_drol | 1.20 | |
780 : | DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level); | ||
781 : | // DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[p], level, BitstreamShowBits(bs, 32)); | ||
782 : | |||
783 : | edgomez | 1.17 | if (level < -127 || level > 127) { |
784 : | Isibaar | 1.1 | DEBUG1("warning: inter_overflow", level); |
785 : | } | ||
786 : | p++; | ||
787 : | } while (!last); | ||
788 : | edgomez | 1.7 | |
789 : | Isibaar | 1.1 | } |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |