1 |
/****************************************************************************** |
/***************************************************************************** |
2 |
* * |
* |
3 |
* This file is part of XviD, a free MPEG-4 video encoder/decoder * |
* XVID MPEG-4 VIDEO CODEC |
4 |
* * |
* - Vector Length Coding tables - |
5 |
* XviD is an implementation of a part of one or more MPEG-4 Video tools * |
* |
6 |
* as specified in ISO/IEC 14496-2 standard. Those intending to use this * |
* Copyright(C) 2002 Michael Militzer <isibaar@xvid.org> |
7 |
* software module in hardware or software products are advised that its * |
* |
8 |
* use may infringe existing patents or copyrights, and any such use * |
* |
9 |
* would be at such party's own risk. The original developer of this * |
* This program is an implementation of a part of one or more MPEG-4 |
10 |
* software module and his/her company, and subsequent editors and their * |
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
11 |
* companies, will have no liability for use of this software or * |
* to use this software module in hardware or software products are |
12 |
* modifications or derivatives thereof. * |
* 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 |
* XviD is free software; you can redistribute it and/or modify it * |
* developer of this software module and his/her company, and subsequent |
15 |
* under the terms of the GNU General Public License as published by * |
* editors and their companies, will have no liability for use of this |
16 |
* the Free Software Foundation; either version 2 of the License, or * |
* software or modifications or derivatives thereof. |
17 |
* (at your option) any later version. * |
* |
18 |
* * |
* This program is free software; you can redistribute it and/or modify |
19 |
* XviD is distributed in the hope that it will be useful, but * |
* it under the terms of the GNU General Public License as published by |
20 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
* the Free Software Foundation; either version 2 of the License, or |
21 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
* (at your option) any later version. |
22 |
* GNU General Public License for more details. * |
* |
23 |
* * |
* This program is distributed in the hope that it will be useful, |
24 |
* You should have received a copy of the GNU General Public License * |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
25 |
* along with this program; if not, write to the Free Software * |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
26 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
* 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 |
* bitstream.c * |
* $Id: mbcoding.c,v 1.26 2002-09-08 14:43:04 edgomez Exp $ |
33 |
* * |
* |
34 |
* Copyright (C) 2001 - Peter Ross <pross@cs.rmit.edu.au> * |
****************************************************************************/ |
|
* * |
|
|
* For more information visit the XviD homepage: http://www.xvid.org * |
|
|
* * |
|
|
******************************************************************************/ |
|
|
|
|
|
/****************************************************************************** |
|
|
* * |
|
|
* Revision history: * |
|
|
* * |
|
|
* 14.04.2002 bframe encoding |
|
|
* 08.03.2002 initial version; isibaar * |
|
|
* * |
|
|
******************************************************************************/ |
|
|
|
|
|
|
|
35 |
|
|
36 |
#include <stdlib.h> |
#include <stdlib.h> |
37 |
#include "../portab.h" |
#include "../portab.h" |
45 |
#define ABS(X) (((X)>0)?(X):-(X)) |
#define ABS(X) (((X)>0)?(X):-(X)) |
46 |
#define CLIP(X,A) (X > A) ? (A) : (X) |
#define CLIP(X,A) (X > A) ? (A) : (X) |
47 |
|
|
48 |
VLC intra_table[65536]; |
/***************************************************************************** |
49 |
VLC inter_table[65536]; |
* Local data |
50 |
|
****************************************************************************/ |
51 |
VLC DCT3Dintra[4096]; |
|
52 |
VLC DCT3Dinter[4096]; |
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 |
|
* Functions |
60 |
|
****************************************************************************/ |
61 |
|
|
62 |
static int16_t clip_table[4096]; |
void |
63 |
|
init_vlc_tables(void) |
|
void init_vlc_tables(void) |
|
64 |
{ |
{ |
65 |
|
|
66 |
int32_t k, l, i, intra, last; |
int32_t k, l, i, intra, last; |
74 |
vlc[0] = intra_table; |
vlc[0] = intra_table; |
75 |
vlc[1] = inter_table; |
vlc[1] = inter_table; |
76 |
|
|
77 |
// initialize the clipping table |
/* |
78 |
for(i = -2048; i < 2048; i++) { |
* Generate encoding vlc lookup tables |
79 |
clip_table[i + 2048] = i; |
* the lookup table idea is taken from the excellent fame project |
80 |
if(i < -255) |
* by Vivien Chapellier |
81 |
clip_table[i + 2048] = -255; |
*/ |
|
if(i > 255) |
|
|
clip_table[i + 2048] = 255; |
|
|
} |
|
|
|
|
|
// generate encoding vlc lookup tables |
|
82 |
for(i = 0; i < 4; i++) { |
for(i = 0; i < 4; i++) { |
83 |
intra = i % 2; |
intra = i % 2; |
84 |
last = i / 2; |
last = i / 2; |
85 |
|
|
86 |
coeff_ptr = coeff_vlc[last + 2 * intra]; |
coeff_ptr = coeff_vlc[last + 2 * intra]; |
87 |
|
|
88 |
for(k = -255; k < 256; k++) { // level |
for (k = -2047; k < 2048; k++) { // level |
89 |
int8_t *max_level_ptr = max_level[last + 2 * intra]; |
int8_t *max_level_ptr = max_level[last + 2 * intra]; |
90 |
int8_t *max_run_ptr = max_run[last + 2 * intra]; |
int8_t *max_run_ptr = max_run[last + 2 * intra]; |
91 |
|
|
92 |
for(l = 0; l < 64; l++) { // run |
for(l = 0; l < 64; l++) { // run |
93 |
int32_t level = k; |
int32_t level = k; |
94 |
uint32_t run = l; |
ptr_t run = l; |
95 |
|
|
96 |
if((abs(level) <= max_level_ptr[run]) && |
if ((abs(level) <= max_level_ptr[run]) && (run <= (uint32_t) max_run_ptr[abs(level)])) { // level < max_level and run < max_run |
|
(run <= (uint32_t)max_run_ptr[abs(level)])) { // level < max_level and run < max_run |
|
97 |
|
|
98 |
vlc[intra]->code = 0; |
vlc[intra]->code = 0; |
99 |
vlc[intra]->len = 0; |
vlc[intra]->len = 0; |
100 |
goto loop_end; |
goto loop_end; |
101 |
} |
} else { |
|
else { |
|
102 |
if(level > 0) // correct level |
if(level > 0) // correct level |
103 |
level -= max_level_ptr[run]; |
level -= max_level_ptr[run]; |
104 |
else |
else |
129 |
run += max_run_ptr[abs(level)] + 1; |
run += max_run_ptr[abs(level)] + 1; |
130 |
} |
} |
131 |
|
|
132 |
vlc[intra]->code = (uint32_t) ((l << 14) | (0x1e + last) << 20) | |
vlc[intra]->code = |
133 |
(1 << 13) | ((k & 0xfff) << 1) | 1; |
(uint32_t) ((l << 14) | (0x1e + last) << 20) | (1 << 13) | |
134 |
|
((k & 0xfff) << 1) | 1; |
135 |
|
|
136 |
vlc[intra]->len = 30; |
vlc[intra]->len = 30; |
137 |
vlc[intra]++; |
vlc[intra]++; |
139 |
|
|
140 |
loop_end: |
loop_end: |
141 |
if(level != 0) { |
if(level != 0) { |
142 |
vlc[intra]->code = (vlc[intra]->code << (coeff_ptr[run][abs(level) - 1].len + 1)) | |
vlc[intra]->code = |
143 |
(coeff_ptr[run][abs(level) - 1].code << 1); |
(vlc[intra]-> |
144 |
vlc[intra]->len = (coeff_ptr[run][abs(level) - 1].len + 1) + vlc[intra]->len; |
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 |
|
|
151 |
if(level < 0) |
if(level < 0) |
152 |
vlc[intra]->code += 1; |
vlc[intra]->code += 1; |
161 |
if(i >= 512) { |
if(i >= 512) { |
162 |
*vlc1 = DCT3Dtab3[(i >> 5) - 16]; |
*vlc1 = DCT3Dtab3[(i >> 5) - 16]; |
163 |
*vlc2 = DCT3Dtab0[(i >> 5) - 16]; |
*vlc2 = DCT3Dtab0[(i >> 5) - 16]; |
164 |
} |
} else if (i >= 128) { |
|
else if(i >= 128) { |
|
165 |
*vlc1 = DCT3Dtab4[(i >> 2) - 32]; |
*vlc1 = DCT3Dtab4[(i >> 2) - 32]; |
166 |
*vlc2 = DCT3Dtab1[(i >> 2) - 32]; |
*vlc2 = DCT3Dtab1[(i >> 2) - 32]; |
167 |
} |
} else if (i >= 8) { |
|
else if(i >= 8) { |
|
168 |
*vlc1 = DCT3Dtab5[i - 8]; |
*vlc1 = DCT3Dtab5[i - 8]; |
169 |
*vlc2 = DCT3Dtab2[i - 8]; |
*vlc2 = DCT3Dtab2[i - 8]; |
170 |
} |
} else { |
|
else { |
|
171 |
*vlc1 = ERRtab[i]; |
*vlc1 = ERRtab[i]; |
172 |
*vlc2 = ERRtab[i]; |
*vlc2 = ERRtab[i]; |
173 |
} |
} |
180 |
|
|
181 |
} |
} |
182 |
|
|
183 |
static __inline void CodeVector(Bitstream *bs, |
static __inline void |
184 |
|
CodeVector(Bitstream * bs, |
185 |
int32_t value, |
int32_t value, |
186 |
int32_t f_code, |
int32_t f_code, |
187 |
Statistics *pStat) |
Statistics *pStat) |
200 |
pStat->iMvCount++; |
pStat->iMvCount++; |
201 |
|
|
202 |
if (value == 0) { |
if (value == 0) { |
203 |
BitstreamPutBits(bs, mb_motion_table[32].code, mb_motion_table[32].len); |
BitstreamPutBits(bs, mb_motion_table[32].code, |
204 |
|
mb_motion_table[32].len); |
205 |
} else { |
} else { |
206 |
uint16_t length, code, mv_res, sign; |
uint16_t length, code, mv_res, sign; |
207 |
|
|
226 |
code = -code; |
code = -code; |
227 |
|
|
228 |
code += 32; |
code += 32; |
229 |
BitstreamPutBits(bs, mb_motion_table[code].code, mb_motion_table[code].len); |
BitstreamPutBits(bs, mb_motion_table[code].code, |
230 |
|
mb_motion_table[code].len); |
231 |
|
|
232 |
if(f_code) |
if(f_code) |
233 |
BitstreamPutBits(bs, mv_res, f_code); |
BitstreamPutBits(bs, mv_res, f_code); |
236 |
} |
} |
237 |
|
|
238 |
|
|
239 |
static __inline void CodeCoeff(Bitstream *bs, |
static __inline void |
240 |
|
CodeCoeff(Bitstream * bs, |
241 |
const int16_t qcoeff[64], |
const int16_t qcoeff[64], |
242 |
VLC *table, |
VLC *table, |
243 |
const uint16_t *zigzag, |
const uint16_t *zigzag, |
251 |
j = intra; |
j = intra; |
252 |
last = intra; |
last = intra; |
253 |
|
|
254 |
while((v = qcoeff[zigzag[j]]) == 0) j++; |
while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) |
255 |
|
j++; |
256 |
|
|
257 |
do { |
do { |
258 |
// count zeroes |
vlc = table + 64 * 2047 + (v << 6) + j - last; |
|
vlc = table + 64*255 + (clip_table[2048+v] << 6) + j - last; |
|
259 |
last = ++j; |
last = ++j; |
260 |
while(j < 64 && (v = qcoeff[zigzag[j]]) == 0) j++; |
|
261 |
|
// count zeroes |
262 |
|
while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) |
263 |
|
j++; |
264 |
|
|
265 |
// write code |
// write code |
266 |
if(j != 64) { |
if(j != 64) { |
267 |
BitstreamPutBits(bs, vlc->code, vlc->len); |
BitstreamPutBits(bs, vlc->code, vlc->len); |
268 |
} else { |
} else { |
269 |
vlc += 64*511; |
vlc += 64 * 4095; |
270 |
BitstreamPutBits(bs, vlc->code, vlc->len); |
BitstreamPutBits(bs, vlc->code, vlc->len); |
271 |
break; |
break; |
272 |
} |
} |
275 |
} |
} |
276 |
|
|
277 |
|
|
278 |
static void CodeBlockIntra(const FRAMEINFO * frame, |
static void |
279 |
|
CodeBlockIntra(const FRAMEINFO * frame, |
280 |
const MACROBLOCK *pMB, |
const MACROBLOCK *pMB, |
281 |
int16_t qcoeff[6*64], |
int16_t qcoeff[6*64], |
282 |
Bitstream * bs, |
Bitstream * bs, |
290 |
// write mcbpc |
// write mcbpc |
291 |
if(frame->coding_type == I_VOP) { |
if(frame->coding_type == I_VOP) { |
292 |
mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2); |
mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2); |
293 |
BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, mcbpc_intra_tab[mcbpc].len); |
BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, |
294 |
} |
mcbpc_intra_tab[mcbpc].len); |
295 |
else { |
} else { |
296 |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
297 |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len); |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, |
298 |
|
mcbpc_inter_tab[mcbpc].len); |
299 |
} |
} |
300 |
|
|
301 |
// ac prediction flag |
// ac prediction flag |
312 |
BitstreamPutBits(bs, pMB->dquant, 2); |
BitstreamPutBits(bs, pMB->dquant, 2); |
313 |
|
|
314 |
// write interlacing |
// write interlacing |
315 |
if (frame->global_flags & XVID_INTERLACING) |
if (frame->global_flags & XVID_INTERLACING) { |
|
{ |
|
316 |
BitstreamPutBit(bs, pMB->field_dct); |
BitstreamPutBit(bs, pMB->field_dct); |
317 |
} |
} |
|
|
|
318 |
// code block coeffs |
// code block coeffs |
319 |
for(i = 0; i < 6; i++) |
for (i = 0; i < 6; i++) { |
|
{ |
|
320 |
if(i < 4) |
if(i < 4) |
321 |
BitstreamPutBits(bs, |
BitstreamPutBits(bs, dcy_tab[qcoeff[i * 64 + 0] + 255].code, |
|
dcy_tab[qcoeff[i*64 + 0] + 255].code, |
|
322 |
dcy_tab[qcoeff[i*64 + 0] + 255].len); |
dcy_tab[qcoeff[i*64 + 0] + 255].len); |
323 |
else |
else |
324 |
BitstreamPutBits(bs, |
BitstreamPutBits(bs, dcc_tab[qcoeff[i * 64 + 0] + 255].code, |
|
dcc_tab[qcoeff[i*64 + 0] + 255].code, |
|
325 |
dcc_tab[qcoeff[i*64 + 0] + 255].len); |
dcc_tab[qcoeff[i*64 + 0] + 255].len); |
326 |
|
|
327 |
if(pMB->cbp & (1 << (5 - i))) |
if (pMB->cbp & (1 << (5 - i))) { |
|
{ |
|
328 |
bits = BitstreamPos(bs); |
bits = BitstreamPos(bs); |
329 |
|
|
330 |
CodeCoeff(bs, |
CodeCoeff(bs, &qcoeff[i * 64], intra_table, |
331 |
&qcoeff[i*64], |
scan_tables[pMB->acpred_directions[i]], 1); |
|
intra_table, |
|
|
scan_tables[pMB->acpred_directions[i]], |
|
|
1); |
|
332 |
|
|
333 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
334 |
pStat->iTextBits += bits; |
pStat->iTextBits += bits; |
338 |
} |
} |
339 |
|
|
340 |
|
|
341 |
static void CodeBlockInter(const FRAMEINFO * frame, |
static void |
342 |
|
CodeBlockInter(const FRAMEINFO * frame, |
343 |
const MACROBLOCK *pMB, |
const MACROBLOCK *pMB, |
344 |
int16_t qcoeff[6*64], |
int16_t qcoeff[6*64], |
345 |
Bitstream * bs, |
Bitstream * bs, |
353 |
cbpy = 15 - (pMB->cbp >> 2); |
cbpy = 15 - (pMB->cbp >> 2); |
354 |
|
|
355 |
// write mcbpc |
// write mcbpc |
356 |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len); |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, |
357 |
|
mcbpc_inter_tab[mcbpc].len); |
358 |
|
|
359 |
// write cbpy |
// write cbpy |
360 |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
364 |
BitstreamPutBits(bs, pMB->dquant, 2); |
BitstreamPutBits(bs, pMB->dquant, 2); |
365 |
|
|
366 |
// interlacing |
// interlacing |
367 |
if (frame->global_flags & XVID_INTERLACING) |
if (frame->global_flags & XVID_INTERLACING) { |
368 |
{ |
if (pMB->cbp) { |
369 |
BitstreamPutBit(bs, pMB->field_dct); |
BitstreamPutBit(bs, pMB->field_dct); |
370 |
DEBUG1("codep: field_dct: ", pMB->field_dct); |
DEBUG1("codep: field_dct: ", pMB->field_dct); |
371 |
|
} |
372 |
|
|
373 |
// if inter block, write field ME flag |
// if inter block, write field ME flag |
374 |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
|
{ |
|
375 |
BitstreamPutBit(bs, pMB->field_pred); |
BitstreamPutBit(bs, pMB->field_pred); |
376 |
DEBUG1("codep: field_pred: ", pMB->field_pred); |
DEBUG1("codep: field_pred: ", pMB->field_pred); |
377 |
|
|
378 |
// write field prediction references |
// write field prediction references |
379 |
if (pMB->field_pred) |
if (pMB->field_pred) { |
|
{ |
|
380 |
BitstreamPutBit(bs, pMB->field_for_top); |
BitstreamPutBit(bs, pMB->field_for_top); |
381 |
BitstreamPutBit(bs, pMB->field_for_bot); |
BitstreamPutBit(bs, pMB->field_for_bot); |
382 |
} |
} |
383 |
} |
} |
384 |
} |
} |
|
|
|
385 |
// code motion vector(s) |
// code motion vector(s) |
386 |
for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) |
for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) { |
|
{ |
|
387 |
CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat); |
CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat); |
388 |
CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat); |
CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat); |
389 |
} |
} |
401 |
} |
} |
402 |
|
|
403 |
|
|
404 |
void MBCoding(const FRAMEINFO * frame, |
void |
405 |
|
MBCoding(const FRAMEINFO * frame, |
406 |
MACROBLOCK *pMB, |
MACROBLOCK *pMB, |
407 |
int16_t qcoeff[6*64], |
int16_t qcoeff[6*64], |
408 |
Bitstream * bs, |
Bitstream * bs, |
409 |
Statistics * pStat) |
Statistics * pStat) |
410 |
{ |
{ |
411 |
|
|
|
int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q); |
|
|
|
|
412 |
if(frame->coding_type == P_VOP) { |
if(frame->coding_type == P_VOP) { |
|
if(pMB->cbp == 0 && pMB->mode == MODE_INTER && |
|
|
pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0) |
|
|
{ |
|
|
BitstreamPutBit(bs, 1); // not_coded |
|
|
return; |
|
|
} |
|
|
else |
|
413 |
BitstreamPutBit(bs, 0); // coded |
BitstreamPutBit(bs, 0); // coded |
414 |
} |
} |
415 |
|
|
416 |
if(intra) |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) |
417 |
CodeBlockIntra(frame, pMB, qcoeff, bs, pStat); |
CodeBlockIntra(frame, pMB, qcoeff, bs, pStat); |
418 |
else |
else |
419 |
CodeBlockInter(frame, pMB, qcoeff, bs, pStat); |
CodeBlockInter(frame, pMB, qcoeff, bs, pStat); |
420 |
|
|
421 |
} |
} |
422 |
|
|
423 |
|
|
424 |
|
void |
425 |
|
MBSkip(Bitstream * bs) |
426 |
|
{ |
427 |
|
BitstreamPutBit(bs, 1); // not coded |
428 |
|
return; |
429 |
|
} |
430 |
|
|
431 |
|
|
432 |
/*************************************************************** |
/*************************************************************** |
433 |
* bframe encoding start |
* bframe encoding start |
434 |
***************************************************************/ |
***************************************************************/ |
441 |
3 0001b forward mc+q dbquant, mvdf |
3 0001b forward mc+q dbquant, mvdf |
442 |
*/ |
*/ |
443 |
|
|
444 |
void put_bvop_mbtype(Bitstream * bs, int value) |
void |
445 |
{ |
put_bvop_mbtype(Bitstream * bs, |
446 |
switch(value) |
int value) |
447 |
{ |
{ |
448 |
case 0 : BitstreamPutBit(bs, 1); |
switch (value) { |
449 |
|
case 0: |
450 |
|
BitstreamPutBit(bs, 1); |
451 |
return; |
return; |
452 |
|
|
453 |
case 1 : BitstreamPutBit(bs, 0); |
case 1: |
454 |
|
BitstreamPutBit(bs, 0); |
455 |
BitstreamPutBit(bs, 1); |
BitstreamPutBit(bs, 1); |
456 |
return; |
return; |
457 |
|
|
458 |
case 2 : BitstreamPutBit(bs, 0); |
case 2: |
459 |
|
BitstreamPutBit(bs, 0); |
460 |
BitstreamPutBit(bs, 0); |
BitstreamPutBit(bs, 0); |
461 |
BitstreamPutBit(bs, 1); |
BitstreamPutBit(bs, 1); |
462 |
return; |
return; |
463 |
|
|
464 |
case 3 : BitstreamPutBit(bs, 0); |
case 3: |
465 |
|
BitstreamPutBit(bs, 0); |
466 |
BitstreamPutBit(bs, 0); |
BitstreamPutBit(bs, 0); |
467 |
BitstreamPutBit(bs, 0); |
BitstreamPutBit(bs, 0); |
468 |
BitstreamPutBit(bs, 1); |
BitstreamPutBit(bs, 1); |
481 |
+2 11b |
+2 11b |
482 |
*/ |
*/ |
483 |
|
|
484 |
void put_bvop_dbquant(Bitstream *bs, int value) |
void |
485 |
|
put_bvop_dbquant(Bitstream * bs, |
486 |
|
int value) |
487 |
{ |
{ |
488 |
switch (value) |
switch (value) { |
489 |
{ |
case 0: |
490 |
case 0 : BitstreamPutBit(bs, 0); |
BitstreamPutBit(bs, 0); |
491 |
return; |
return; |
492 |
|
|
493 |
case -2 : BitstreamPutBit(bs, 1); |
case -2: |
494 |
|
BitstreamPutBit(bs, 1); |
495 |
BitstreamPutBit(bs, 0); |
BitstreamPutBit(bs, 0); |
496 |
return; |
return; |
497 |
|
|
498 |
case 2 : BitstreamPutBit(bs, 1); |
case 2: |
499 |
|
BitstreamPutBit(bs, 1); |
500 |
BitstreamPutBit(bs, 1); |
BitstreamPutBit(bs, 1); |
501 |
return; |
return; |
502 |
|
|
506 |
|
|
507 |
|
|
508 |
|
|
509 |
void MBCodingBVOP(const MACROBLOCK * mb, |
void |
510 |
|
MBCodingBVOP(const MACROBLOCK * mb, |
511 |
const int16_t qcoeff[6*64], |
const int16_t qcoeff[6*64], |
512 |
const int32_t fcode, |
const int32_t fcode, |
513 |
const int32_t bcode, |
const int32_t bcode, |
517 |
int i; |
int i; |
518 |
|
|
519 |
/* ------------------------------------------------------------------ |
/* ------------------------------------------------------------------ |
520 |
when a block is skipped it is decoded DIRECT(0,) |
when a block is skipped it is decoded DIRECT(0,0) |
521 |
hence are interpolated from forward & backward frames |
hence is interpolated from forward & backward frames |
522 |
------------------------------------------------------------------ */ |
------------------------------------------------------------------ */ |
523 |
|
|
524 |
if (mb->mode == 5) |
if (mb->mode == MODE_DIRECT_NONE_MV) { |
|
{ |
|
525 |
BitstreamPutBit(bs, 1); // skipped |
BitstreamPutBit(bs, 1); // skipped |
526 |
return; |
return; |
527 |
} |
} |
528 |
|
|
529 |
BitstreamPutBit(bs, 0); // not skipped |
BitstreamPutBit(bs, 0); // not skipped |
530 |
|
|
531 |
if (mb->cbp == 0) |
if (mb->cbp == 0) { |
|
{ |
|
532 |
BitstreamPutBit(bs, 1); // cbp == 0 |
BitstreamPutBit(bs, 1); // cbp == 0 |
533 |
} |
} else { |
|
else |
|
|
{ |
|
534 |
BitstreamPutBit(bs, 0); // cbp == xxx |
BitstreamPutBit(bs, 0); // cbp == xxx |
535 |
} |
} |
536 |
|
|
537 |
put_bvop_mbtype(bs, mb->mode); |
put_bvop_mbtype(bs, mb->mode); |
538 |
|
|
539 |
if (mb->cbp) |
if (mb->cbp) { |
|
{ |
|
540 |
BitstreamPutBits(bs, mb->cbp, 6); |
BitstreamPutBits(bs, mb->cbp, 6); |
541 |
} |
} |
542 |
|
|
543 |
if (mb->mode != MODE_DIRECT && mb->cbp != 0) |
if (mb->mode != MODE_DIRECT && mb->cbp != 0) { |
|
{ |
|
544 |
put_bvop_dbquant(bs, 0); // todo: mb->dquant = 0 |
put_bvop_dbquant(bs, 0); // todo: mb->dquant = 0 |
545 |
} |
} |
546 |
|
|
547 |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) { |
|
{ |
|
548 |
CodeVector(bs, mb->pmvs[0].x, fcode, pStat); |
CodeVector(bs, mb->pmvs[0].x, fcode, pStat); |
549 |
CodeVector(bs, mb->pmvs[0].y, fcode, pStat); |
CodeVector(bs, mb->pmvs[0].y, fcode, pStat); |
550 |
} |
} |
551 |
|
|
552 |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) { |
|
{ |
|
553 |
CodeVector(bs, mb->b_pmvs[0].x, bcode, pStat); |
CodeVector(bs, mb->b_pmvs[0].x, bcode, pStat); |
554 |
CodeVector(bs, mb->b_pmvs[0].y, bcode, pStat); |
CodeVector(bs, mb->b_pmvs[0].y, bcode, pStat); |
555 |
} |
} |
556 |
|
|
557 |
if (mb->mode == MODE_DIRECT) |
if (mb->mode == MODE_DIRECT) { |
558 |
{ |
CodeVector(bs, mb->deltamv.x, 1, pStat); /* fcode is always 1 for delta vector */ |
559 |
// TODO: direct |
CodeVector(bs, mb->deltamv.y, 1, pStat); /* prediction is always (0,0) */ |
560 |
} |
} |
561 |
|
|
562 |
for (i = 0; i < 6; i++) |
for (i = 0; i < 6; i++) { |
563 |
{ |
if (mb->cbp & (1 << (5 - i))) { |
|
if (mb->cbp & (1 << (5 - i))) |
|
|
{ |
|
564 |
CodeCoeff(bs, &qcoeff[i*64], inter_table, scan_tables[0], 0); |
CodeCoeff(bs, &qcoeff[i*64], inter_table, scan_tables[0], 0); |
565 |
} |
} |
566 |
} |
} |
572 |
* decoding stuff starts here * |
* decoding stuff starts here * |
573 |
***************************************************************/ |
***************************************************************/ |
574 |
|
|
575 |
int get_mcbpc_intra(Bitstream * bs) |
|
576 |
|
// for IVOP addbits == 0 |
577 |
|
// for PVOP addbits == fcode - 1 |
578 |
|
// for BVOP addbits == max(fcode,bcode) - 1 |
579 |
|
// returns true or false |
580 |
|
int |
581 |
|
check_resync_marker(Bitstream * bs, int addbits) |
582 |
|
{ |
583 |
|
uint32_t nbits; |
584 |
|
uint32_t code; |
585 |
|
uint32_t nbitsresyncmarker = NUMBITS_VP_RESYNC_MARKER + addbits; |
586 |
|
|
587 |
|
nbits = BitstreamNumBitsToByteAlign(bs); |
588 |
|
code = BitstreamShowBits(bs, nbits); |
589 |
|
|
590 |
|
if (code == (((uint32_t)1 << (nbits - 1)) - 1)) |
591 |
{ |
{ |
592 |
|
return BitstreamShowBitsFromByteAlign(bs, nbitsresyncmarker) == RESYNC_MARKER; |
593 |
|
} |
594 |
|
|
595 |
|
return 0; |
596 |
|
} |
597 |
|
|
598 |
|
|
|
uint32_t index; |
|
599 |
|
|
600 |
while((index = BitstreamShowBits(bs, 9)) == 1) |
int |
601 |
BitstreamSkip(bs, 9); |
get_mcbpc_intra(Bitstream * bs) |
602 |
|
{ |
603 |
|
|
604 |
|
uint32_t index; |
605 |
|
|
606 |
|
index = BitstreamShowBits(bs, 9); |
607 |
index >>= 3; |
index >>= 3; |
608 |
|
|
609 |
BitstreamSkip(bs, mcbpc_intra_table[index].len); |
BitstreamSkip(bs, mcbpc_intra_table[index].len); |
612 |
|
|
613 |
} |
} |
614 |
|
|
615 |
int get_mcbpc_inter(Bitstream * bs) |
int |
616 |
|
get_mcbpc_inter(Bitstream * bs) |
617 |
{ |
{ |
618 |
|
|
619 |
uint32_t index; |
uint32_t index; |
620 |
|
|
621 |
while((index = CLIP(BitstreamShowBits(bs, 9), 256)) == 1) |
index = CLIP(BitstreamShowBits(bs, 9), 256); |
|
BitstreamSkip(bs, 9); |
|
622 |
|
|
623 |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
624 |
|
|
626 |
|
|
627 |
} |
} |
628 |
|
|
629 |
int get_cbpy(Bitstream * bs, int intra) |
int |
630 |
|
get_cbpy(Bitstream * bs, |
631 |
|
int intra) |
632 |
{ |
{ |
633 |
|
|
634 |
int cbpy; |
int cbpy; |
644 |
|
|
645 |
} |
} |
646 |
|
|
647 |
int get_mv_data(Bitstream * bs) |
int |
648 |
|
get_mv_data(Bitstream * bs) |
649 |
{ |
{ |
650 |
|
|
651 |
uint32_t index; |
uint32_t index; |
655 |
|
|
656 |
index = BitstreamShowBits(bs, 12); |
index = BitstreamShowBits(bs, 12); |
657 |
|
|
658 |
if(index >= 512) |
if (index >= 512) { |
|
{ |
|
659 |
index = (index >> 8) - 2; |
index = (index >> 8) - 2; |
660 |
BitstreamSkip(bs, TMNMVtab0[index].len); |
BitstreamSkip(bs, TMNMVtab0[index].len); |
661 |
return TMNMVtab0[index].code; |
return TMNMVtab0[index].code; |
662 |
} |
} |
663 |
|
|
664 |
if(index >= 128) |
if (index >= 128) { |
|
{ |
|
665 |
index = (index >> 2) - 32; |
index = (index >> 2) - 32; |
666 |
BitstreamSkip(bs, TMNMVtab1[index].len); |
BitstreamSkip(bs, TMNMVtab1[index].len); |
667 |
return TMNMVtab1[index].code; |
return TMNMVtab1[index].code; |
674 |
|
|
675 |
} |
} |
676 |
|
|
677 |
int get_mv(Bitstream * bs, int fcode) |
int |
678 |
|
get_mv(Bitstream * bs, |
679 |
|
int fcode) |
680 |
{ |
{ |
681 |
|
|
682 |
int data; |
int data; |
696 |
|
|
697 |
} |
} |
698 |
|
|
699 |
int get_dc_dif(Bitstream * bs, uint32_t dc_size) |
int |
700 |
|
get_dc_dif(Bitstream * bs, |
701 |
|
uint32_t dc_size) |
702 |
{ |
{ |
703 |
|
|
704 |
int code = BitstreamGetBits(bs, dc_size); |
int code = BitstreamGetBits(bs, dc_size); |
711 |
|
|
712 |
} |
} |
713 |
|
|
714 |
int get_dc_size_lum(Bitstream * bs) |
int |
715 |
|
get_dc_size_lum(Bitstream * bs) |
716 |
{ |
{ |
717 |
|
|
718 |
int code, i; |
int code, i; |
719 |
|
|
720 |
code = BitstreamShowBits(bs, 11); |
code = BitstreamShowBits(bs, 11); |
721 |
|
|
722 |
for(i = 11; i > 3; i--) { |
for(i = 11; i > 3; i--) { |
733 |
} |
} |
734 |
|
|
735 |
|
|
736 |
int get_dc_size_chrom(Bitstream * bs) |
int |
737 |
|
get_dc_size_chrom(Bitstream * bs) |
738 |
{ |
{ |
739 |
|
|
740 |
uint32_t code, i; |
uint32_t code, i; |
741 |
|
|
742 |
code = BitstreamShowBits(bs, 12); |
code = BitstreamShowBits(bs, 12); |
743 |
|
|
744 |
for(i = 12; i > 2; i--) { |
for(i = 12; i > 2; i--) { |
753 |
|
|
754 |
} |
} |
755 |
|
|
756 |
void get_intra_block(Bitstream * bs, int16_t * block, int direction, int coeff) |
void |
757 |
|
get_intra_block(Bitstream * bs, |
758 |
|
int16_t * block, |
759 |
|
int direction, |
760 |
|
int coeff) |
761 |
{ |
{ |
762 |
|
|
763 |
const uint16_t * scan = scan_tables[ direction ]; |
const uint16_t * scan = scan_tables[ direction ]; |
765 |
int run; |
int run; |
766 |
int last; |
int last; |
767 |
|
|
768 |
do |
do { |
|
{ |
|
769 |
level = get_coeff(bs, &run, &last, 1, 0); |
level = get_coeff(bs, &run, &last, 1, 0); |
770 |
if (run == -1) |
if (run == -1) { |
|
{ |
|
771 |
DEBUG("fatal: invalid run"); |
DEBUG("fatal: invalid run"); |
772 |
break; |
break; |
773 |
} |
} |
774 |
coeff += run; |
coeff += run; |
775 |
block[ scan[coeff] ] = level; |
block[ scan[coeff] ] = level; |
776 |
if (level < -127 || level > 127) |
|
777 |
{ |
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level); |
778 |
|
//DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[coeff], level, BitstreamShowBits(bs, 32)); |
779 |
|
|
780 |
|
if (level < -127 || level > 127) { |
781 |
DEBUG1("warning: intra_overflow", level); |
DEBUG1("warning: intra_overflow", level); |
782 |
} |
} |
783 |
coeff++; |
coeff++; |
785 |
|
|
786 |
} |
} |
787 |
|
|
788 |
void get_inter_block(Bitstream * bs, int16_t * block) |
void |
789 |
|
get_inter_block(Bitstream * bs, |
790 |
|
int16_t * block) |
791 |
{ |
{ |
792 |
|
|
793 |
const uint16_t * scan = scan_tables[0]; |
const uint16_t * scan = scan_tables[0]; |
797 |
int last; |
int last; |
798 |
|
|
799 |
p = 0; |
p = 0; |
800 |
do |
do { |
|
{ |
|
801 |
level = get_coeff(bs, &run, &last, 0, 0); |
level = get_coeff(bs, &run, &last, 0, 0); |
802 |
if (run == -1) |
if (run == -1) { |
|
{ |
|
803 |
DEBUG("fatal: invalid run"); |
DEBUG("fatal: invalid run"); |
804 |
break; |
break; |
805 |
} |
} |
806 |
p += run; |
p += run; |
807 |
|
|
808 |
block[ scan[p] ] = level; |
block[ scan[p] ] = level; |
809 |
if (level < -127 || level > 127) |
|
810 |
{ |
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level); |
811 |
|
// DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[p], level, BitstreamShowBits(bs, 32)); |
812 |
|
|
813 |
|
if (level < -127 || level > 127) { |
814 |
DEBUG1("warning: inter_overflow", level); |
DEBUG1("warning: inter_overflow", level); |
815 |
} |
} |
816 |
p++; |
p++; |