18 |
|
|
19 |
void create_vlc_tables(void) |
void create_vlc_tables(void) |
20 |
{ |
{ |
21 |
|
|
22 |
int32_t k, l, i, intra, last; |
int32_t k, l, i, intra, last; |
23 |
VLC *vlc[2]; |
VLC *vlc[2]; |
24 |
VLC **coeff_ptr; |
VLC **coeff_ptr; |
57 |
char *max_run_ptr = max_run[last + (intra << 1)]; |
char *max_run_ptr = max_run[last + (intra << 1)]; |
58 |
|
|
59 |
for(l = 0; l < 64; l++) { // run |
for(l = 0; l < 64; l++) { // run |
60 |
int32_t level = k, run = l; |
int32_t level = k; |
61 |
|
uint32_t run = l; |
62 |
|
|
63 |
if(abs(level) <= max_level_ptr[run] && run <= max_run_ptr[abs(level)]) { |
if(abs(level) <= max_level_ptr[run] && run <= max_run_ptr[abs(level)]) { |
64 |
|
|
139 |
} |
} |
140 |
} |
} |
141 |
} |
} |
142 |
|
|
143 |
intra_table += 64*255; // center vlc tables |
intra_table += 64*255; // center vlc tables |
144 |
inter_table += 64*255; // center vlc tables |
inter_table += 64*255; // center vlc tables |
145 |
|
|
186 |
|
|
187 |
} |
} |
188 |
|
|
189 |
static __inline void CodeVector(Bitstream *bs, int16_t value, int16_t f_code, Statistics *pStat) |
static __inline void CodeVector(Bitstream *bs, |
190 |
|
int16_t value, |
191 |
|
int16_t f_code, |
192 |
|
Statistics *pStat) |
193 |
{ |
{ |
194 |
|
|
195 |
const int scale_factor = 1 << (f_code - 1); |
const int scale_factor = 1 << (f_code - 1); |
196 |
const int cmp = scale_factor << 5; |
const int cmp = scale_factor << 5; |
197 |
|
|
204 |
pStat->iMvSum += value * value; |
pStat->iMvSum += value * value; |
205 |
pStat->iMvCount++; |
pStat->iMvCount++; |
206 |
|
|
207 |
if (value == 0) |
if (value == 0) { |
208 |
BitstreamPutBits(bs, mb_motion_table[32].code, mb_motion_table[32].len); |
BitstreamPutBits(bs, mb_motion_table[32].code, mb_motion_table[32].len); |
209 |
else { |
} else { |
210 |
uint16_t length, code, mv_res, sign; |
uint16_t length, code, mv_res, sign; |
211 |
|
|
212 |
length = 16 << f_code; |
length = 16 << f_code; |
235 |
if(f_code) |
if(f_code) |
236 |
BitstreamPutBits(bs, mv_res, f_code); |
BitstreamPutBits(bs, mv_res, f_code); |
237 |
} |
} |
238 |
|
|
239 |
} |
} |
240 |
|
|
241 |
|
|
242 |
static __inline void CodeCoeff(Bitstream *bs, int16_t qcoeff[64], VLC *table, |
static __inline void CodeCoeff(Bitstream *bs, |
243 |
const uint16_t *zigzag, uint16_t intra) { |
int16_t qcoeff[64], |
244 |
|
VLC *table, |
245 |
|
const uint16_t *zigzag, |
246 |
|
uint16_t intra) |
247 |
|
{ |
248 |
|
|
249 |
uint32_t j, last; |
uint32_t j, last; |
250 |
short v; |
short v; |
251 |
VLC *vlc; |
VLC *vlc; |
270 |
break; |
break; |
271 |
} |
} |
272 |
} while(1); |
} while(1); |
273 |
|
|
274 |
} |
} |
275 |
|
|
276 |
|
|
277 |
static void CodeBlockIntra(const MBParam * pParam, const MACROBLOCK *pMB, |
static void CodeBlockIntra(const MBParam * pParam, |
278 |
int16_t qcoeff[][64], Bitstream * bs, Statistics * pStat) |
const MACROBLOCK *pMB, |
279 |
|
int16_t qcoeff[6*64], |
280 |
|
Bitstream * bs, |
281 |
|
Statistics * pStat) |
282 |
{ |
{ |
283 |
|
|
284 |
uint32_t i, mcbpc, cbpy, bits; |
uint32_t i, mcbpc, cbpy, bits; |
285 |
|
|
|
mcbpc = pMB->cbp & 3; |
|
286 |
cbpy = pMB->cbp >> 2; |
cbpy = pMB->cbp >> 2; |
287 |
|
|
288 |
// write mcbpc |
// write mcbpc |
289 |
if(pParam->coding_type == I_VOP) |
if(pParam->coding_type == I_VOP) { |
290 |
BitstreamPutBits(bs, mcbpc_I[mcbpc].code, mcbpc_I[mcbpc].len); |
mcbpc = ((pMB->mode >> 1) & 3) | ((pMB->cbp & 3) << 2); |
291 |
else |
BitstreamPutBits(bs, mcbpc_intra_tab[mcbpc].code, mcbpc_intra_tab[mcbpc].len); |
292 |
BitstreamPutBits(bs, mcbpc_P_intra[mcbpc].code, mcbpc_P_intra[mcbpc].len); |
} |
293 |
|
else { |
294 |
|
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
295 |
|
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len); |
296 |
|
} |
297 |
|
|
298 |
// ac prediction flag |
// ac prediction flag |
299 |
if(pMB->acpred_directions[0]) |
if(pMB->acpred_directions[0]) |
308 |
if(pMB->mode == MODE_INTRA_Q) |
if(pMB->mode == MODE_INTRA_Q) |
309 |
BitstreamPutBits(bs, pMB->dquant, 2); |
BitstreamPutBits(bs, pMB->dquant, 2); |
310 |
|
|
311 |
|
// write interlacing |
312 |
|
if (pParam->global_flags & XVID_INTERLACING) |
313 |
|
{ |
314 |
|
BitstreamPutBit(bs, pMB->field_dct); |
315 |
|
} |
316 |
|
|
317 |
// code block coeffs |
// code block coeffs |
318 |
for(i = 0; i < 6; i++) |
for(i = 0; i < 6; i++) |
319 |
{ |
{ |
320 |
if(i < 4) |
if(i < 4) |
321 |
BitstreamPutBits(bs, dcy_tab[qcoeff[i][0] + 255].code, |
BitstreamPutBits(bs, |
322 |
dcy_tab[qcoeff[i][0] + 255].len); |
dcy_tab[qcoeff[i*64 + 0] + 255].code, |
323 |
else |
dcy_tab[qcoeff[i*64 + 0] + 255].len); |
324 |
BitstreamPutBits(bs, dcc_tab[qcoeff[i][0] + 255].code, |
else |
325 |
dcc_tab[qcoeff[i][0] + 255].len); |
BitstreamPutBits(bs, |
326 |
|
dcc_tab[qcoeff[i*64 + 0] + 255].code, |
327 |
|
dcc_tab[qcoeff[i*64 + 0] + 255].len); |
328 |
|
|
329 |
if(pMB->cbp & (1 << (5 - i))) |
if(pMB->cbp & (1 << (5 - i))) |
330 |
{ |
{ |
331 |
bits = BitstreamPos(bs); |
bits = BitstreamPos(bs); |
332 |
|
|
333 |
CodeCoeff(bs, qcoeff[i], intra_table, scan_tables[pMB->acpred_directions[i]], 1); |
CodeCoeff(bs, |
334 |
|
&qcoeff[i*64], |
335 |
|
intra_table, |
336 |
|
scan_tables[pMB->acpred_directions[i]], |
337 |
|
1); |
338 |
|
|
339 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
340 |
pStat->iTextBits += bits; |
pStat->iTextBits += bits; |
341 |
} |
} |
342 |
} |
} |
343 |
|
|
344 |
} |
} |
345 |
|
|
346 |
|
|
347 |
static void CodeBlockInter(const MBParam * pParam, const MACROBLOCK *pMB, |
static void CodeBlockInter(const MBParam * pParam, |
348 |
int16_t qcoeff[][64], Bitstream * bs, Statistics * pStat) |
const MACROBLOCK *pMB, |
349 |
|
int16_t qcoeff[6*64], |
350 |
|
Bitstream * bs, |
351 |
|
Statistics * pStat) |
352 |
{ |
{ |
353 |
|
|
354 |
int32_t i; |
int32_t i; |
355 |
uint32_t bits, mcbpc, cbpy; |
uint32_t bits, mcbpc, cbpy; |
356 |
|
|
357 |
mcbpc = pMB->cbp & 3; |
mcbpc = (pMB->mode & 7) | ((pMB->cbp & 3) << 3); |
358 |
cbpy = 15 - (pMB->cbp >> 2); |
cbpy = 15 - (pMB->cbp >> 2); |
359 |
|
|
360 |
// write mcbpc |
// write mcbpc |
361 |
if(pMB->mode == MODE_INTER4V) |
BitstreamPutBits(bs, mcbpc_inter_tab[mcbpc].code, mcbpc_inter_tab[mcbpc].len); |
|
BitstreamPutBits(bs, mcbpc_P_inter4v[mcbpc].code, mcbpc_P_inter4v[mcbpc].len); |
|
|
else |
|
|
BitstreamPutBits(bs, mcbpc_P_inter[mcbpc].code, mcbpc_P_inter[mcbpc].len); |
|
362 |
|
|
363 |
// write cbpy |
// write cbpy |
364 |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
BitstreamPutBits(bs, cbpy_tab[cbpy].code, cbpy_tab[cbpy].len); |
367 |
if(pMB->mode == MODE_INTER_Q) |
if(pMB->mode == MODE_INTER_Q) |
368 |
BitstreamPutBits(bs, pMB->dquant, 2); |
BitstreamPutBits(bs, pMB->dquant, 2); |
369 |
|
|
370 |
|
// interlacing |
371 |
|
if (pParam->global_flags & XVID_INTERLACING) |
372 |
|
{ |
373 |
|
BitstreamPutBit(bs, pMB->field_dct); |
374 |
|
DEBUG1("codep: field_dct: ", pMB->field_dct); |
375 |
|
|
376 |
|
// if inter block, write field ME flag |
377 |
|
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) |
378 |
|
{ |
379 |
|
BitstreamPutBit(bs, pMB->field_pred); |
380 |
|
DEBUG1("codep: field_pred: ", pMB->field_pred); |
381 |
|
|
382 |
|
// write field prediction references |
383 |
|
if (pMB->field_pred) |
384 |
|
{ |
385 |
|
BitstreamPutBit(bs, pMB->field_for_top); |
386 |
|
BitstreamPutBit(bs, pMB->field_for_bot); |
387 |
|
} |
388 |
|
} |
389 |
|
} |
390 |
|
|
391 |
// code motion vector(s) |
// code motion vector(s) |
392 |
for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) |
for(i = 0; i < (pMB->mode == MODE_INTER4V ? 4 : 1); i++) |
393 |
{ |
{ |
400 |
// code block coeffs |
// code block coeffs |
401 |
for(i = 0; i < 6; i++) |
for(i = 0; i < 6; i++) |
402 |
if(pMB->cbp & (1 << (5 - i))) |
if(pMB->cbp & (1 << (5 - i))) |
403 |
CodeCoeff(bs, qcoeff[i], inter_table, scan_tables[0], 0); |
CodeCoeff(bs, &qcoeff[i*64], inter_table, scan_tables[0], 0); |
404 |
|
|
405 |
bits = BitstreamPos(bs) - bits; |
bits = BitstreamPos(bs) - bits; |
406 |
pStat->iTextBits += bits; |
pStat->iTextBits += bits; |
407 |
|
|
408 |
} |
} |
409 |
|
|
410 |
|
|
411 |
void MBCoding(const MBParam * pParam, MACROBLOCK *pMB, |
void MBCoding(const MBParam * pParam, |
412 |
int16_t qcoeff[][64], |
MACROBLOCK *pMB, |
413 |
Bitstream * bs, Statistics * pStat) |
int16_t qcoeff[6*64], |
414 |
|
Bitstream * bs, |
415 |
|
Statistics * pStat) |
416 |
{ |
{ |
417 |
|
|
418 |
int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q); |
int intra = (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q); |
419 |
|
|
420 |
if(pParam->coding_type == P_VOP) { |
if(pParam->coding_type == P_VOP) { |
432 |
CodeBlockIntra(pParam, pMB, qcoeff, bs, pStat); |
CodeBlockIntra(pParam, pMB, qcoeff, bs, pStat); |
433 |
else |
else |
434 |
CodeBlockInter(pParam, pMB, qcoeff, bs, pStat); |
CodeBlockInter(pParam, pMB, qcoeff, bs, pStat); |
435 |
|
|
436 |
} |
} |
437 |
|
|
438 |
|
|
442 |
|
|
443 |
int get_mcbpc_intra(Bitstream * bs) |
int get_mcbpc_intra(Bitstream * bs) |
444 |
{ |
{ |
445 |
|
|
446 |
uint32_t index; |
uint32_t index; |
447 |
|
|
448 |
while((index = BitstreamShowBits(bs, 9)) == 1) |
while((index = BitstreamShowBits(bs, 9)) == 1) |
451 |
index >>= 3; |
index >>= 3; |
452 |
|
|
453 |
BitstreamSkip(bs, mcbpc_intra_table[index].len); |
BitstreamSkip(bs, mcbpc_intra_table[index].len); |
454 |
|
|
455 |
return mcbpc_intra_table[index].code; |
return mcbpc_intra_table[index].code; |
456 |
|
|
457 |
} |
} |
458 |
|
|
459 |
int get_mcbpc_inter(Bitstream * bs) |
int get_mcbpc_inter(Bitstream * bs) |
460 |
{ |
{ |
461 |
|
|
462 |
uint32_t index; |
uint32_t index; |
463 |
|
|
464 |
while((index = CLIP(BitstreamShowBits(bs, 9), 256)) == 1) |
while((index = CLIP(BitstreamShowBits(bs, 9), 256)) == 1) |
465 |
BitstreamSkip(bs, 9); |
BitstreamSkip(bs, 9); |
466 |
|
|
467 |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
BitstreamSkip(bs, mcbpc_inter_table[index].len); |
468 |
|
|
469 |
return mcbpc_inter_table[index].code; |
return mcbpc_inter_table[index].code; |
470 |
|
|
471 |
} |
} |
472 |
|
|
473 |
int get_cbpy(Bitstream * bs, int intra) |
int get_cbpy(Bitstream * bs, int intra) |
474 |
{ |
{ |
475 |
|
|
476 |
int cbpy; |
int cbpy; |
477 |
uint32_t index = BitstreamShowBits(bs, 6); |
uint32_t index = BitstreamShowBits(bs, 6); |
478 |
|
|
483 |
cbpy = 15 - cbpy; |
cbpy = 15 - cbpy; |
484 |
|
|
485 |
return cbpy; |
return cbpy; |
486 |
|
|
487 |
} |
} |
488 |
|
|
489 |
int get_mv_data(Bitstream * bs) |
int get_mv_data(Bitstream * bs) |
490 |
{ |
{ |
491 |
|
|
492 |
uint32_t index; |
uint32_t index; |
493 |
|
|
494 |
if(BitstreamGetBit(bs)) |
if(BitstreamGetBit(bs)) |
514 |
|
|
515 |
BitstreamSkip(bs, TMNMVtab2[index].len); |
BitstreamSkip(bs, TMNMVtab2[index].len); |
516 |
return TMNMVtab2[index].code; |
return TMNMVtab2[index].code; |
517 |
|
|
518 |
} |
} |
519 |
|
|
520 |
int get_mv(Bitstream * bs, int fcode) |
int get_mv(Bitstream * bs, int fcode) |
521 |
{ |
{ |
522 |
|
|
523 |
int data; |
int data; |
524 |
int res; |
int res; |
525 |
int mv; |
int mv; |
534 |
mv = ((ABS(data) - 1) * scale_fac) + res + 1; |
mv = ((ABS(data) - 1) * scale_fac) + res + 1; |
535 |
|
|
536 |
return data < 0 ? -mv : mv; |
return data < 0 ? -mv : mv; |
537 |
|
|
538 |
} |
} |
539 |
|
|
540 |
int get_dc_dif(Bitstream * bs, uint32_t dc_size) |
int get_dc_dif(Bitstream * bs, uint32_t dc_size) |
541 |
{ |
{ |
542 |
|
|
543 |
int code = BitstreamGetBits(bs, dc_size); |
int code = BitstreamGetBits(bs, dc_size); |
544 |
int msb = code >> (dc_size - 1); |
int msb = code >> (dc_size - 1); |
545 |
|
|
547 |
return (-1 * (code^((1 << dc_size) - 1))); |
return (-1 * (code^((1 << dc_size) - 1))); |
548 |
|
|
549 |
return code; |
return code; |
550 |
|
|
551 |
} |
} |
552 |
|
|
553 |
int get_dc_size_lum(Bitstream * bs) |
int get_dc_size_lum(Bitstream * bs) |
554 |
{ |
{ |
555 |
|
|
556 |
int code, i; |
int code, i; |
557 |
code = BitstreamShowBits(bs, 11); |
code = BitstreamShowBits(bs, 11); |
558 |
|
|
566 |
|
|
567 |
BitstreamSkip(bs, dc_lum_tab[code].len); |
BitstreamSkip(bs, dc_lum_tab[code].len); |
568 |
return dc_lum_tab[code].code; |
return dc_lum_tab[code].code; |
569 |
|
|
570 |
} |
} |
571 |
|
|
572 |
|
|
573 |
int get_dc_size_chrom(Bitstream * bs) |
int get_dc_size_chrom(Bitstream * bs) |
574 |
{ |
{ |
575 |
|
|
576 |
uint32_t code, i; |
uint32_t code, i; |
577 |
code = BitstreamShowBits(bs, 12); |
code = BitstreamShowBits(bs, 12); |
578 |
|
|
585 |
} |
} |
586 |
|
|
587 |
return 3 - BitstreamGetBits(bs, 2); |
return 3 - BitstreamGetBits(bs, 2); |
588 |
|
|
589 |
} |
} |
590 |
|
|
591 |
int get_coeff(Bitstream * bs, int *run, int *last, int intra, int short_video_header) |
int get_coeff(Bitstream * bs, int *run, int *last, int intra, int short_video_header) |
592 |
{ |
{ |
593 |
|
|
594 |
uint32_t mode; |
uint32_t mode; |
595 |
const VLC *tab; |
const VLC *tab; |
596 |
int32_t level; |
int32_t level; |
678 |
error: |
error: |
679 |
*run = VLC_ERROR; |
*run = VLC_ERROR; |
680 |
return 0; |
return 0; |
681 |
|
|
682 |
} |
} |
683 |
|
|
684 |
|
|
685 |
void get_intra_block(Bitstream * bs, int16_t * block, int direction, int coeff) |
void get_intra_block(Bitstream * bs, int16_t * block, int direction, int coeff) |
686 |
{ |
{ |
687 |
|
|
688 |
const uint16_t * scan = scan_tables[ direction ]; |
const uint16_t * scan = scan_tables[ direction ]; |
689 |
int level; |
int level; |
690 |
int run; |
int run; |
706 |
} |
} |
707 |
coeff++; |
coeff++; |
708 |
} while (!last); |
} while (!last); |
709 |
|
|
710 |
} |
} |
711 |
|
|
712 |
void get_inter_block(Bitstream * bs, int16_t * block) |
void get_inter_block(Bitstream * bs, int16_t * block) |
713 |
{ |
{ |
714 |
|
|
715 |
const uint16_t * scan = scan_tables[0]; |
const uint16_t * scan = scan_tables[0]; |
716 |
int p; |
int p; |
717 |
int level; |
int level; |
735 |
} |
} |
736 |
p++; |
p++; |
737 |
} while (!last); |
} while (!last); |
738 |
|
|
739 |
} |
} |