41 |
* * |
* * |
42 |
* Revision history: * |
* Revision history: * |
43 |
* * |
* * |
44 |
|
* 28.10.2002 GMC support - gruel * |
45 |
|
* 28.06.2002 added check_resync_marker() * |
46 |
* 14.04.2002 bframe encoding * |
* 14.04.2002 bframe encoding * |
47 |
* 08.03.2002 initial version; isibaar * |
* 08.03.2002 initial version; isibaar * |
48 |
* * |
* * |
52 |
|
|
53 |
#include <stdlib.h> |
#include <stdlib.h> |
54 |
#include "../portab.h" |
#include "../portab.h" |
55 |
|
#include "../global.h" |
56 |
#include "bitstream.h" |
#include "bitstream.h" |
57 |
#include "zigzag.h" |
#include "zigzag.h" |
58 |
#include "vlc_codes.h" |
#include "vlc_codes.h" |
60 |
|
|
61 |
#include "../utils/mbfunctions.h" |
#include "../utils/mbfunctions.h" |
62 |
|
|
63 |
#define ABS(X) (((X)>0)?(X):-(X)) |
VLC intra_table[4*2048*64]; |
64 |
#define CLIP(X,A) (X > A) ? (A) : (X) |
VLC inter_table[4*2048*64]; |
|
|
|
|
VLC intra_table[524032]; |
|
|
VLC inter_table[524032]; |
|
65 |
|
|
66 |
VLC DCT3Dintra[4096]; |
VLC DCT3Dintra[4096]; |
67 |
VLC DCT3Dinter[4096]; |
VLC DCT3Dinter[4096]; |
68 |
|
|
69 |
void init_vlc_tables(void) |
/* not really MB related, but VLCs are only available here */ |
70 |
|
void bs_put_spritetrajectory(Bitstream * bs, const int val) |
71 |
|
{ |
72 |
|
const int code = sprite_trajectory_code[val+16384].code; |
73 |
|
const int len = sprite_trajectory_code[val+16384].len; |
74 |
|
const int code2 = sprite_trajectory_len[len].code; |
75 |
|
const int len2 = sprite_trajectory_len[len].len; |
76 |
|
|
77 |
|
// printf("GMC=%d Code/Len = %d / %d ",val, code,len); |
78 |
|
// printf("Code2 / Len2 = %d / %d \n",code2,len2); |
79 |
|
|
80 |
|
BitstreamPutBits(bs, code2, len2); |
81 |
|
if (len) BitstreamPutBits(bs, code, len); |
82 |
|
} |
83 |
|
|
84 |
|
int bs_get_spritetrajectory(Bitstream * bs) |
85 |
|
{ |
86 |
|
int i; |
87 |
|
for (i = 0; i < 12; i++) |
88 |
|
{ |
89 |
|
if (BitstreamShowBits(bs, sprite_trajectory_len[i].len) == sprite_trajectory_len[i].code) |
90 |
|
{ |
91 |
|
BitstreamSkip(bs, sprite_trajectory_len[i].len); |
92 |
|
return i; |
93 |
|
} |
94 |
|
} |
95 |
|
return -1; |
96 |
|
} |
97 |
|
|
98 |
|
void |
99 |
|
init_vlc_tables(void) |
100 |
{ |
{ |
101 |
|
|
102 |
int32_t k, l, i, intra, last; |
int32_t k, l, i, intra, last; |
124 |
|
|
125 |
for(l = 0; l < 64; l++) { // run |
for(l = 0; l < 64; l++) { // run |
126 |
int32_t level = k; |
int32_t level = k; |
127 |
uint32_t run = l; |
ptr_t run = l; |
128 |
|
|
129 |
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 |
|
130 |
|
|
131 |
vlc[intra]->code = 0; |
vlc[intra]->code = 0; |
132 |
vlc[intra]->len = 0; |
vlc[intra]->len = 0; |
133 |
goto loop_end; |
goto loop_end; |
134 |
} |
} else { |
|
else { |
|
135 |
if(level > 0) // correct level |
if(level > 0) // correct level |
136 |
level -= max_level_ptr[run]; |
level -= max_level_ptr[run]; |
137 |
else |
else |
162 |
run += max_run_ptr[abs(level)] + 1; |
run += max_run_ptr[abs(level)] + 1; |
163 |
} |
} |
164 |
|
|
165 |
vlc[intra]->code = (uint32_t) ((l << 14) | (0x1e + last) << 20) | |
vlc[intra]->code = |
166 |
(1 << 13) | ((k & 0xfff) << 1) | 1; |
(uint32_t) ((l << 14) | (0x1e + last) << 20) | (1 << 13) | |
167 |
|
((k & 0xfff) << 1) | 1; |
168 |
|
|
169 |
vlc[intra]->len = 30; |
vlc[intra]->len = 30; |
170 |
vlc[intra]++; |
vlc[intra]++; |
172 |
|
|
173 |
loop_end: |
loop_end: |
174 |
if(level != 0) { |
if(level != 0) { |
175 |
vlc[intra]->code = (vlc[intra]->code << (coeff_ptr[run][abs(level) - 1].len + 1)) | |
vlc[intra]->code = |
176 |
(coeff_ptr[run][abs(level) - 1].code << 1); |
(vlc[intra]-> |
177 |
vlc[intra]->len = (coeff_ptr[run][abs(level) - 1].len + 1) + vlc[intra]->len; |
code << (coeff_ptr[run][abs(level) - 1].len + |
178 |
|
1)) | (coeff_ptr[run][abs(level) - |
179 |
|
1].code << 1); |
180 |
|
vlc[intra]->len = |
181 |
|
(coeff_ptr[run][abs(level) - 1].len + 1) + |
182 |
|
vlc[intra]->len; |
183 |
|
|
184 |
if(level < 0) |
if(level < 0) |
185 |
vlc[intra]->code += 1; |
vlc[intra]->code += 1; |
194 |
if(i >= 512) { |
if(i >= 512) { |
195 |
*vlc1 = DCT3Dtab3[(i >> 5) - 16]; |
*vlc1 = DCT3Dtab3[(i >> 5) - 16]; |
196 |
*vlc2 = DCT3Dtab0[(i >> 5) - 16]; |
*vlc2 = DCT3Dtab0[(i >> 5) - 16]; |
197 |
} |
} else if (i >= 128) { |
|
else if(i >= 128) { |
|
198 |
*vlc1 = DCT3Dtab4[(i >> 2) - 32]; |
*vlc1 = DCT3Dtab4[(i >> 2) - 32]; |
199 |
*vlc2 = DCT3Dtab1[(i >> 2) - 32]; |
*vlc2 = DCT3Dtab1[(i >> 2) - 32]; |
200 |
} |
} else if (i >= 8) { |
|
else if(i >= 8) { |
|
201 |
*vlc1 = DCT3Dtab5[i - 8]; |
*vlc1 = DCT3Dtab5[i - 8]; |
202 |
*vlc2 = DCT3Dtab2[i - 8]; |
*vlc2 = DCT3Dtab2[i - 8]; |
203 |
} |
} else { |
|
else { |
|
204 |
*vlc1 = ERRtab[i]; |
*vlc1 = ERRtab[i]; |
205 |
*vlc2 = ERRtab[i]; |
*vlc2 = ERRtab[i]; |
206 |
} |
} |
211 |
DCT3D[0] = DCT3Dinter; |
DCT3D[0] = DCT3Dinter; |
212 |
DCT3D[1] = DCT3Dintra; |
DCT3D[1] = DCT3Dintra; |
213 |
|
|
214 |
|
|
215 |
|
/* init sprite_trajectory tables */ |
216 |
|
/* even if GMC is not specified (it might be used later...) */ |
217 |
|
|
218 |
|
sprite_trajectory_code[0+16384].code = 0; |
219 |
|
sprite_trajectory_code[0+16384].len = 0; |
220 |
|
for (k=0;k<14;k++) |
221 |
|
{ |
222 |
|
int limit = (1<<k); |
223 |
|
|
224 |
|
for (i=-(2*limit-1); i<= -limit; i++) |
225 |
|
{ |
226 |
|
sprite_trajectory_code[i+16384].code = (2*limit-1)+i; |
227 |
|
sprite_trajectory_code[i+16384].len = k+1; |
228 |
} |
} |
229 |
|
|
230 |
static __inline void CodeVector(Bitstream *bs, |
for (i=limit; i<= 2*limit-1; i++) |
231 |
|
{ |
232 |
|
sprite_trajectory_code[i+16384].code = i; |
233 |
|
sprite_trajectory_code[i+16384].len = k+1; |
234 |
|
} |
235 |
|
} |
236 |
|
} |
237 |
|
|
238 |
|
static __inline void |
239 |
|
CodeVector(Bitstream * bs, |
240 |
int32_t value, |
int32_t value, |
241 |
int32_t f_code, |
int32_t f_code, |
242 |
Statistics *pStat) |
Statistics *pStat) |
255 |
pStat->iMvCount++; |
pStat->iMvCount++; |
256 |
|
|
257 |
if (value == 0) { |
if (value == 0) { |
258 |
BitstreamPutBits(bs, mb_motion_table[32].code, mb_motion_table[32].len); |
BitstreamPutBits(bs, mb_motion_table[32].code, |
259 |
|
mb_motion_table[32].len); |
260 |
} else { |
} else { |
261 |
uint16_t length, code, mv_res, sign; |
uint16_t length, code, mv_res, sign; |
262 |
|
|
281 |
code = -code; |
code = -code; |
282 |
|
|
283 |
code += 32; |
code += 32; |
284 |
BitstreamPutBits(bs, mb_motion_table[code].code, mb_motion_table[code].len); |
BitstreamPutBits(bs, mb_motion_table[code].code, |
285 |
|
mb_motion_table[code].len); |
286 |
|
|
287 |
if(f_code) |
if(f_code) |
288 |
BitstreamPutBits(bs, mv_res, f_code); |
BitstreamPutBits(bs, mv_res, f_code); |
291 |
} |
} |
292 |
|
|
293 |
|
|
294 |
static __inline void CodeCoeff(Bitstream *bs, |
static __inline void |
295 |
|
CodeCoeff(Bitstream * bs, |
296 |
const int16_t qcoeff[64], |
const int16_t qcoeff[64], |
297 |
VLC *table, |
VLC *table, |
298 |
const uint16_t *zigzag, |
const uint16_t *zigzag, |
306 |
j = intra; |
j = intra; |
307 |
last = intra; |
last = intra; |
308 |
|
|
309 |
while(j < 64 && (v = qcoeff[zigzag[j]]) == 0) j++; |
while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) |
310 |
|
j++; |
311 |
|
|
312 |
do { |
do { |
313 |
vlc = table + 64 * 2047 + (v << 6) + j - last; |
vlc = table + 64 * 2047 + (v << 6) + j - last; |
314 |
last = ++j; |
last = ++j; |
315 |
|
|
316 |
// count zeroes |
// count zeroes |
317 |
while(j < 64 && (v = qcoeff[zigzag[j]]) == 0) j++; |
while (j < 64 && (v = qcoeff[zigzag[j]]) == 0) |
318 |
|
j++; |
319 |
|
|
320 |
// write code |
// write code |
321 |
if(j != 64) { |
if(j != 64) { |
330 |
} |
} |
331 |
|
|
332 |
|
|
333 |
static void CodeBlockIntra(const FRAMEINFO * frame, |
static __inline void |
334 |
|
CodeBlockIntra(const FRAMEINFO * const frame, |
335 |
const MACROBLOCK *pMB, |
const MACROBLOCK *pMB, |
336 |
int16_t qcoeff[6*64], |
int16_t qcoeff[6*64], |
337 |
Bitstream * bs, |
Bitstream * bs, |
345 |
// write mcbpc |
// write mcbpc |
346 |
if(frame->coding_type == I_VOP) { |
if(frame->coding_type == I_VOP) { |
347 |
mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2); |
mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2); |
348 |
BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, mcbpc_intra_tab[mcbpc].len); |
BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, |
349 |
} |
mcbpc_intra_tab[mcbpc].len); |
350 |
else { |
} else { |
351 |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
352 |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len); |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, |
353 |
|
mcbpc_inter_tab[mcbpc].len); |
354 |
} |
} |
355 |
|
|
356 |
// ac prediction flag |
// ac prediction flag |
367 |
BitstreamPutBits(bs, pMB->dquant, 2); |
BitstreamPutBits(bs, pMB->dquant, 2); |
368 |
|
|
369 |
// write interlacing |
// write interlacing |
370 |
if (frame->global_flags & XVID_INTERLACING) |
if (frame->global_flags & XVID_INTERLACING) { |
|
{ |
|
371 |
BitstreamPutBit(bs, pMB->field_dct); |
BitstreamPutBit(bs, pMB->field_dct); |
372 |
} |
} |
|
|
|
373 |
// code block coeffs |
// code block coeffs |
374 |
for(i = 0; i < 6; i++) |
for (i = 0; i < 6; i++) { |
|
{ |
|
375 |
if(i < 4) |
if(i < 4) |
376 |
BitstreamPutBits(bs, |
BitstreamPutBits(bs, dcy_tab[qcoeff[i * 64 + 0] + 255].code, |
|
dcy_tab[qcoeff[i*64 + 0] + 255].code, |
|
377 |
dcy_tab[qcoeff[i*64 + 0] + 255].len); |
dcy_tab[qcoeff[i*64 + 0] + 255].len); |
378 |
else |
else |
379 |
BitstreamPutBits(bs, |
BitstreamPutBits(bs, dcc_tab[qcoeff[i * 64 + 0] + 255].code, |
|
dcc_tab[qcoeff[i*64 + 0] + 255].code, |
|
380 |
dcc_tab[qcoeff[i*64 + 0] + 255].len); |
dcc_tab[qcoeff[i*64 + 0] + 255].len); |
381 |
|
|
382 |
if(pMB->cbp & (1 << (5 - i))) |
if (pMB->cbp & (1 << (5 - i))) { |
383 |
{ |
const uint16_t *scan_table = |
384 |
|
frame->global_flags & XVID_ALTERNATESCAN ? |
385 |
|
scan_tables[2] : scan_tables[pMB->acpred_directions[i]]; |
386 |
|
|
387 |
bits = BitstreamPos(bs); |
bits = BitstreamPos(bs); |
388 |
|
|
389 |
CodeCoeff(bs, |
CodeCoeff(bs, &qcoeff[i * 64], intra_table, scan_table, 1); |
|
&qcoeff[i*64], |
|
|
intra_table, |
|
|
scan_tables[pMB->acpred_directions[i]], |
|
|
1); |
|
390 |
|
|
391 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
392 |
pStat->iTextBits += bits; |
pStat->iTextBits += bits; |
396 |
} |
} |
397 |
|
|
398 |
|
|
399 |
static void CodeBlockInter(const FRAMEINFO * frame, |
static void |
400 |
|
CodeBlockInter(const FRAMEINFO * const frame, |
401 |
const MACROBLOCK *pMB, |
const MACROBLOCK *pMB, |
402 |
int16_t qcoeff[6*64], |
int16_t qcoeff[6*64], |
403 |
Bitstream * bs, |
Bitstream * bs, |
406 |
|
|
407 |
int32_t i; |
int32_t i; |
408 |
uint32_t bits, mcbpc, cbpy; |
uint32_t bits, mcbpc, cbpy; |
409 |
|
int mcsel=0; |
410 |
|
|
411 |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
412 |
cbpy = 15 - (pMB->cbp >> 2); |
cbpy = 15 - (pMB->cbp >> 2); |
413 |
|
|
414 |
// write mcbpc |
// write mcbpc |
415 |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len); |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, |
416 |
|
mcbpc_inter_tab[mcbpc].len); |
417 |
|
|
418 |
|
if ( (frame->coding_type == S_VOP) && (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) ) |
419 |
|
{ |
420 |
|
if (frame->quarterpel) { |
421 |
|
if ( (pMB->qmvs[0].x == frame->GMC_MV.x) && (pMB->qmvs[0].y == frame->GMC_MV.y) ) |
422 |
|
mcsel=1; |
423 |
|
} else { |
424 |
|
if ( (pMB->mvs[0].x == frame->GMC_MV.x) && (pMB->mvs[0].y == frame->GMC_MV.y) ) |
425 |
|
mcsel=1; |
426 |
|
} |
427 |
|
BitstreamPutBit(bs, mcsel); // mcsel: '0'=local motion, '1'=GMC |
428 |
|
} |
429 |
|
|
430 |
// write cbpy |
// write cbpy |
431 |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
435 |
BitstreamPutBits(bs, pMB->dquant, 2); |
BitstreamPutBits(bs, pMB->dquant, 2); |
436 |
|
|
437 |
// interlacing |
// interlacing |
438 |
if (frame->global_flags & XVID_INTERLACING) |
if (frame->global_flags & XVID_INTERLACING) { |
439 |
{ |
if (pMB->cbp) { |
440 |
BitstreamPutBit(bs, pMB->field_dct); |
BitstreamPutBit(bs, pMB->field_dct); |
441 |
DEBUG1("codep: field_dct: ", pMB->field_dct); |
DPRINTF(DPRINTF_MB,"codep: field_dct: %i", pMB->field_dct); |
442 |
|
} |
443 |
|
|
444 |
// if inter block, write field ME flag |
// if inter block, write field ME flag |
445 |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
|
{ |
|
446 |
BitstreamPutBit(bs, pMB->field_pred); |
BitstreamPutBit(bs, pMB->field_pred); |
447 |
DEBUG1("codep: field_pred: ", pMB->field_pred); |
DPRINTF(DPRINTF_MB,"codep: field_pred: %i", pMB->field_pred); |
448 |
|
|
449 |
// write field prediction references |
// write field prediction references |
450 |
if (pMB->field_pred) |
if (pMB->field_pred) { |
|
{ |
|
451 |
BitstreamPutBit(bs, pMB->field_for_top); |
BitstreamPutBit(bs, pMB->field_for_top); |
452 |
BitstreamPutBit(bs, pMB->field_for_bot); |
BitstreamPutBit(bs, pMB->field_for_bot); |
453 |
} |
} |
454 |
} |
} |
455 |
} |
} |
456 |
|
// code motion vector(s) if motion is local |
457 |
// code motion vector(s) |
if (mcsel==0) |
458 |
for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) |
for (i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) { |
|
{ |
|
459 |
CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat); |
CodeVector(bs, pMB->pmvs[i].x, frame->fcode, pStat); |
460 |
CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat); |
CodeVector(bs, pMB->pmvs[i].y, frame->fcode, pStat); |
461 |
} |
} |
465 |
// code block coeffs |
// code block coeffs |
466 |
for(i = 0; i < 6; i++) |
for(i = 0; i < 6; i++) |
467 |
if(pMB->cbp & (1 << (5 - i))) |
if(pMB->cbp & (1 << (5 - i))) |
468 |
CodeCoeff(bs, &qcoeff[i*64], inter_table, scan_tables[0], 0); |
{ |
469 |
|
const uint16_t *scan_table = |
470 |
|
frame->global_flags & XVID_ALTERNATESCAN ? |
471 |
|
scan_tables[2] : scan_tables[0]; |
472 |
|
|
473 |
|
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_table, 0); |
474 |
|
} |
475 |
|
|
476 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
477 |
pStat->iTextBits += bits; |
pStat->iTextBits += bits; |
|
|
|
478 |
} |
} |
479 |
|
|
480 |
|
|
481 |
void MBCoding(const FRAMEINFO * frame, |
void |
482 |
|
MBCoding(const FRAMEINFO * const frame, |
483 |
MACROBLOCK *pMB, |
MACROBLOCK *pMB, |
484 |
int16_t qcoeff[6*64], |
int16_t qcoeff[6*64], |
485 |
Bitstream * bs, |
Bitstream * bs, |
486 |
Statistics * pStat) |
Statistics * pStat) |
487 |
{ |
{ |
488 |
|
if (frame->coding_type != I_VOP) |
489 |
|
BitstreamPutBit(bs, 0); // not_coded |
490 |
|
|
491 |
int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q); |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) |
|
|
|
|
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 |
|
|
BitstreamPutBit(bs, 0); // coded |
|
|
} |
|
|
|
|
|
if(intra) |
|
492 |
CodeBlockIntra(frame, pMB, qcoeff, bs, pStat); |
CodeBlockIntra(frame, pMB, qcoeff, bs, pStat); |
493 |
else |
else |
494 |
CodeBlockInter(frame, pMB, qcoeff, bs, pStat); |
CodeBlockInter(frame, pMB, qcoeff, bs, pStat); |
495 |
|
|
496 |
} |
} |
497 |
|
|
498 |
|
/* |
499 |
|
// moved to mbcoding.h so that in can be 'static __inline' |
500 |
|
void |
501 |
|
MBSkip(Bitstream * bs) |
502 |
|
{ |
503 |
|
BitstreamPutBit(bs, 1); // not coded |
504 |
|
} |
505 |
|
*/ |
506 |
|
|
507 |
/*************************************************************** |
/*************************************************************** |
508 |
* bframe encoding start |
* bframe encoding start |
509 |
***************************************************************/ |
***************************************************************/ |
516 |
3 0001b forward mc+q dbquant, mvdf |
3 0001b forward mc+q dbquant, mvdf |
517 |
*/ |
*/ |
518 |
|
|
519 |
void put_bvop_mbtype(Bitstream * bs, int value) |
static __inline void |
520 |
|
put_bvop_mbtype(Bitstream * bs, |
521 |
|
int value) |
522 |
{ |
{ |
523 |
switch(value) |
switch (value) { |
524 |
{ |
case MODE_FORWARD: |
|
case 0 : BitstreamPutBit(bs, 1); |
|
|
return; |
|
|
|
|
|
case 1 : BitstreamPutBit(bs, 0); |
|
|
BitstreamPutBit(bs, 1); |
|
|
return; |
|
|
|
|
|
case 2 : BitstreamPutBit(bs, 0); |
|
525 |
BitstreamPutBit(bs, 0); |
BitstreamPutBit(bs, 0); |
526 |
BitstreamPutBit(bs, 1); |
case MODE_BACKWARD: |
|
return; |
|
|
|
|
|
case 3 : BitstreamPutBit(bs, 0); |
|
527 |
BitstreamPutBit(bs, 0); |
BitstreamPutBit(bs, 0); |
528 |
|
case MODE_INTERPOLATE: |
529 |
BitstreamPutBit(bs, 0); |
BitstreamPutBit(bs, 0); |
530 |
|
case MODE_DIRECT: |
531 |
BitstreamPutBit(bs, 1); |
BitstreamPutBit(bs, 1); |
532 |
return; |
default: |
533 |
|
break; |
|
default : ; // invalid! |
|
|
|
|
534 |
} |
} |
|
|
|
535 |
} |
} |
536 |
|
|
537 |
/* |
/* |
541 |
+2 11b |
+2 11b |
542 |
*/ |
*/ |
543 |
|
|
544 |
void put_bvop_dbquant(Bitstream *bs, int value) |
static __inline void |
545 |
|
put_bvop_dbquant(Bitstream * bs, |
546 |
|
int value) |
547 |
{ |
{ |
548 |
switch (value) |
switch (value) { |
549 |
{ |
case 0: |
550 |
case 0 : BitstreamPutBit(bs, 0); |
BitstreamPutBit(bs, 0); |
551 |
return; |
return; |
552 |
|
|
553 |
case -2 : BitstreamPutBit(bs, 1); |
case -2: |
554 |
|
BitstreamPutBit(bs, 1); |
555 |
BitstreamPutBit(bs, 0); |
BitstreamPutBit(bs, 0); |
556 |
return; |
return; |
557 |
|
|
558 |
case 2 : BitstreamPutBit(bs, 1); |
case 2: |
559 |
|
BitstreamPutBit(bs, 1); |
560 |
BitstreamPutBit(bs, 1); |
BitstreamPutBit(bs, 1); |
561 |
return; |
return; |
562 |
|
|
566 |
|
|
567 |
|
|
568 |
|
|
569 |
void MBCodingBVOP(const MACROBLOCK * mb, |
void |
570 |
|
MBCodingBVOP(const MACROBLOCK * mb, |
571 |
const int16_t qcoeff[6*64], |
const int16_t qcoeff[6*64], |
572 |
const int32_t fcode, |
const int32_t fcode, |
573 |
const int32_t bcode, |
const int32_t bcode, |
574 |
Bitstream * bs, |
Bitstream * bs, |
575 |
Statistics * pStat) |
Statistics * pStat, |
576 |
|
int direction) |
577 |
{ |
{ |
578 |
int i; |
int vcode = fcode; |
579 |
|
unsigned int i; |
580 |
|
|
581 |
/* ------------------------------------------------------------------ |
/* ------------------------------------------------------------------ |
582 |
when a block is skipped it is decoded DIRECT(0,) |
when a block is skipped it is decoded DIRECT(0,0) |
583 |
hence are interpolated from forward & backward frames |
hence is interpolated from forward & backward frames |
584 |
------------------------------------------------------------------ */ |
------------------------------------------------------------------ */ |
585 |
|
|
586 |
if (mb->mode == 5) |
if (mb->mode == MODE_DIRECT_NONE_MV) { |
|
{ |
|
587 |
BitstreamPutBit(bs, 1); // skipped |
BitstreamPutBit(bs, 1); // skipped |
588 |
return; |
return; |
589 |
} |
} |
590 |
|
|
591 |
BitstreamPutBit(bs, 0); // not skipped |
BitstreamPutBit(bs, 0); // not skipped |
592 |
|
|
593 |
if (mb->cbp == 0) |
if (mb->cbp == 0) { |
|
{ |
|
594 |
BitstreamPutBit(bs, 1); // cbp == 0 |
BitstreamPutBit(bs, 1); // cbp == 0 |
595 |
} |
} else { |
|
else |
|
|
{ |
|
596 |
BitstreamPutBit(bs, 0); // cbp == xxx |
BitstreamPutBit(bs, 0); // cbp == xxx |
597 |
} |
} |
598 |
|
|
599 |
put_bvop_mbtype(bs, mb->mode); |
put_bvop_mbtype(bs, mb->mode); |
600 |
|
|
601 |
if (mb->cbp) |
if (mb->cbp) { |
|
{ |
|
602 |
BitstreamPutBits(bs, mb->cbp, 6); |
BitstreamPutBits(bs, mb->cbp, 6); |
603 |
} |
} |
604 |
|
|
605 |
if (mb->mode != MODE_DIRECT && mb->cbp != 0) |
if (mb->mode != MODE_DIRECT && mb->cbp != 0) { |
|
{ |
|
606 |
put_bvop_dbquant(bs, 0); // todo: mb->dquant = 0 |
put_bvop_dbquant(bs, 0); // todo: mb->dquant = 0 |
607 |
} |
} |
608 |
|
|
609 |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) |
switch (mb->mode) { |
610 |
{ |
case MODE_INTERPOLATE: |
611 |
CodeVector(bs, mb->pmvs[0].x, fcode, pStat); |
CodeVector(bs, mb->pmvs[1].x, vcode, pStat); //forward vector of interpolate mode |
612 |
CodeVector(bs, mb->pmvs[0].y, fcode, pStat); |
CodeVector(bs, mb->pmvs[1].y, vcode, pStat); |
613 |
} |
case MODE_BACKWARD: |
614 |
|
vcode = bcode; |
615 |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) |
case MODE_FORWARD: |
616 |
{ |
CodeVector(bs, mb->pmvs[0].x, vcode, pStat); |
617 |
CodeVector(bs, mb->b_pmvs[0].x, bcode, pStat); |
CodeVector(bs, mb->pmvs[0].y, vcode, pStat); |
618 |
CodeVector(bs, mb->b_pmvs[0].y, bcode, pStat); |
break; |
619 |
} |
case MODE_DIRECT: |
620 |
|
CodeVector(bs, mb->pmvs[3].x, 1, pStat); // fcode is always 1 for delta vector |
621 |
if (mb->mode == MODE_DIRECT) |
CodeVector(bs, mb->pmvs[3].y, 1, pStat); // prediction is always (0,0) |
622 |
{ |
default: break; |
|
// TODO: direct |
|
623 |
} |
} |
624 |
|
|
625 |
for (i = 0; i < 6; i++) |
for (i = 0; i < 6; i++) { |
626 |
{ |
if (mb->cbp & (1 << (5 - i))) { |
627 |
if (mb->cbp & (1 << (5 - i))) |
CodeCoeff(bs, &qcoeff[i * 64], inter_table, scan_tables[direction], 0); |
|
{ |
|
|
CodeCoeff(bs, &qcoeff[i*64], inter_table, scan_tables[0], 0); |
|
628 |
} |
} |
629 |
} |
} |
630 |
} |
} |
635 |
* decoding stuff starts here * |
* decoding stuff starts here * |
636 |
***************************************************************/ |
***************************************************************/ |
637 |
|
|
638 |
int get_mcbpc_intra(Bitstream * bs) |
|
639 |
|
// for IVOP addbits == 0 |
640 |
|
// for PVOP addbits == fcode - 1 |
641 |
|
// for BVOP addbits == max(fcode,bcode) - 1 |
642 |
|
// returns true or false |
643 |
|
int |
644 |
|
check_resync_marker(Bitstream * bs, int addbits) |
645 |
|
{ |
646 |
|
uint32_t nbits; |
647 |
|
uint32_t code; |
648 |
|
uint32_t nbitsresyncmarker = NUMBITS_VP_RESYNC_MARKER + addbits; |
649 |
|
|
650 |
|
nbits = BitstreamNumBitsToByteAlign(bs); |
651 |
|
code = BitstreamShowBits(bs, nbits); |
652 |
|
|
653 |
|
if (code == (((uint32_t)1 << (nbits - 1)) - 1)) |
654 |
{ |
{ |
655 |
|
return BitstreamShowBitsFromByteAlign(bs, nbitsresyncmarker) == RESYNC_MARKER; |
656 |
|
} |
657 |
|
|
658 |
uint32_t index; |
return 0; |
659 |
|
} |
660 |
|
|
|
while((index = BitstreamShowBits(bs, 9)) == 1) |
|
|
BitstreamSkip(bs, 9); |
|
661 |
|
|
662 |
|
|
663 |
|
int |
664 |
|
get_mcbpc_intra(Bitstream * bs) |
665 |
|
{ |
666 |
|
|
667 |
|
uint32_t index; |
668 |
|
|
669 |
|
index = BitstreamShowBits(bs, 9); |
670 |
index >>= 3; |
index >>= 3; |
671 |
|
|
672 |
BitstreamSkip(bs, mcbpc_intra_table[index].len); |
BitstreamSkip(bs, mcbpc_intra_table[index].len); |
675 |
|
|
676 |
} |
} |
677 |
|
|
678 |
int get_mcbpc_inter(Bitstream * bs) |
int |
679 |
|
get_mcbpc_inter(Bitstream * bs) |
680 |
{ |
{ |
681 |
|
|
682 |
uint32_t index; |
uint32_t index; |
683 |
|
|
684 |
while((index = CLIP(BitstreamShowBits(bs, 9), 256)) == 1) |
index = MIN(BitstreamShowBits(bs, 9), 256); |
|
BitstreamSkip(bs, 9); |
|
685 |
|
|
686 |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
687 |
|
|
689 |
|
|
690 |
} |
} |
691 |
|
|
692 |
int get_cbpy(Bitstream * bs, int intra) |
int |
693 |
|
get_cbpy(Bitstream * bs, |
694 |
|
int intra) |
695 |
{ |
{ |
696 |
|
|
697 |
int cbpy; |
int cbpy; |
707 |
|
|
708 |
} |
} |
709 |
|
|
710 |
int get_mv_data(Bitstream * bs) |
static __inline int |
711 |
|
get_mv_data(Bitstream * bs) |
712 |
{ |
{ |
713 |
|
|
714 |
uint32_t index; |
uint32_t index; |
718 |
|
|
719 |
index = BitstreamShowBits(bs, 12); |
index = BitstreamShowBits(bs, 12); |
720 |
|
|
721 |
if(index >= 512) |
if (index >= 512) { |
|
{ |
|
722 |
index = (index >> 8) - 2; |
index = (index >> 8) - 2; |
723 |
BitstreamSkip(bs, TMNMVtab0[index].len); |
BitstreamSkip(bs, TMNMVtab0[index].len); |
724 |
return TMNMVtab0[index].code; |
return TMNMVtab0[index].code; |
725 |
} |
} |
726 |
|
|
727 |
if(index >= 128) |
if (index >= 128) { |
|
{ |
|
728 |
index = (index >> 2) - 32; |
index = (index >> 2) - 32; |
729 |
BitstreamSkip(bs, TMNMVtab1[index].len); |
BitstreamSkip(bs, TMNMVtab1[index].len); |
730 |
return TMNMVtab1[index].code; |
return TMNMVtab1[index].code; |
737 |
|
|
738 |
} |
} |
739 |
|
|
740 |
int get_mv(Bitstream * bs, int fcode) |
int |
741 |
|
get_mv(Bitstream * bs, |
742 |
|
int fcode) |
743 |
{ |
{ |
744 |
|
|
745 |
int data; |
int data; |
759 |
|
|
760 |
} |
} |
761 |
|
|
762 |
int get_dc_dif(Bitstream * bs, uint32_t dc_size) |
int |
763 |
|
get_dc_dif(Bitstream * bs, |
764 |
|
uint32_t dc_size) |
765 |
{ |
{ |
766 |
|
|
767 |
int code = BitstreamGetBits(bs, dc_size); |
int code = BitstreamGetBits(bs, dc_size); |
774 |
|
|
775 |
} |
} |
776 |
|
|
777 |
int get_dc_size_lum(Bitstream * bs) |
int |
778 |
|
get_dc_size_lum(Bitstream * bs) |
779 |
{ |
{ |
780 |
|
|
781 |
int code, i; |
int code, i; |
782 |
|
|
783 |
code = BitstreamShowBits(bs, 11); |
code = BitstreamShowBits(bs, 11); |
784 |
|
|
785 |
for(i = 11; i > 3; i--) { |
for(i = 11; i > 3; i--) { |
796 |
} |
} |
797 |
|
|
798 |
|
|
799 |
int get_dc_size_chrom(Bitstream * bs) |
int |
800 |
|
get_dc_size_chrom(Bitstream * bs) |
801 |
{ |
{ |
802 |
|
|
803 |
uint32_t code, i; |
uint32_t code, i; |
804 |
|
|
805 |
code = BitstreamShowBits(bs, 12); |
code = BitstreamShowBits(bs, 12); |
806 |
|
|
807 |
for(i = 12; i > 2; i--) { |
for(i = 12; i > 2; i--) { |
816 |
|
|
817 |
} |
} |
818 |
|
|
819 |
void get_intra_block(Bitstream * bs, int16_t * block, int direction, int coeff) |
void |
820 |
|
get_intra_block(Bitstream * bs, |
821 |
|
int16_t * block, |
822 |
|
int direction, |
823 |
|
int coeff) |
824 |
{ |
{ |
825 |
|
|
826 |
const uint16_t * scan = scan_tables[ direction ]; |
const uint16_t * scan = scan_tables[ direction ]; |
827 |
int level; |
int level, run, last; |
|
int run; |
|
|
int last; |
|
828 |
|
|
829 |
do |
do { |
|
{ |
|
830 |
level = get_coeff(bs, &run, &last, 1, 0); |
level = get_coeff(bs, &run, &last, 1, 0); |
831 |
if (run == -1) |
if (run == -1) { |
832 |
{ |
DPRINTF(DPRINTF_ERROR,"fatal: invalid run"); |
|
DEBUG("fatal: invalid run"); |
|
833 |
break; |
break; |
834 |
} |
} |
835 |
coeff += run; |
coeff += run; |
836 |
block[ scan[coeff] ] = level; |
block[ scan[coeff] ] = level; |
837 |
if (level < -127 || level > 127) |
|
838 |
{ |
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[coeff], level); |
839 |
DEBUG1("warning: intra_overflow", level); |
//DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[coeff], level, BitstreamShowBits(bs, 32)); |
840 |
|
|
841 |
|
if (level < -2047 || level > 2047) { |
842 |
|
DPRINTF(DPRINTF_ERROR,"warning: intra_overflow %i", level); |
843 |
} |
} |
844 |
coeff++; |
coeff++; |
845 |
} while (!last); |
} while (!last); |
846 |
|
|
847 |
} |
} |
848 |
|
|
849 |
void get_inter_block(Bitstream * bs, int16_t * block) |
void |
850 |
|
get_inter_block(Bitstream * bs, |
851 |
|
int16_t * block, |
852 |
|
int direction) |
853 |
{ |
{ |
854 |
|
|
855 |
const uint16_t * scan = scan_tables[0]; |
const uint16_t *scan = scan_tables[direction]; |
856 |
int p; |
int p; |
857 |
int level; |
int level; |
858 |
int run; |
int run; |
859 |
int last; |
int last; |
860 |
|
|
861 |
p = 0; |
p = 0; |
862 |
do |
do { |
|
{ |
|
863 |
level = get_coeff(bs, &run, &last, 0, 0); |
level = get_coeff(bs, &run, &last, 0, 0); |
864 |
if (run == -1) |
if (run == -1) { |
865 |
{ |
DPRINTF(DPRINTF_ERROR,"fatal: invalid run"); |
|
DEBUG("fatal: invalid run"); |
|
866 |
break; |
break; |
867 |
} |
} |
868 |
p += run; |
p += run; |
869 |
|
|
870 |
block[ scan[p] ] = level; |
block[ scan[p] ] = level; |
871 |
if (level < -127 || level > 127) |
|
872 |
{ |
DPRINTF(DPRINTF_COEFF,"block[%i] %i", scan[p], level); |
873 |
DEBUG1("warning: inter_overflow", level); |
// DPRINTF(DPRINTF_COEFF,"block[%i] %i %08x", scan[p], level, BitstreamShowBits(bs, 32)); |
874 |
|
|
875 |
|
if (level < -2047 || level > 2047) { |
876 |
|
DPRINTF(DPRINTF_ERROR,"warning: inter overflow %i", level); |
877 |
} |
} |
878 |
p++; |
p++; |
879 |
} while (!last); |
} while (!last); |