Mercurial > libavcodec.hg
comparison rv40.c @ 8225:d133e597db2d libavcodec
RV40 loop filter
author | kostya |
---|---|
date | Sat, 29 Nov 2008 06:24:47 +0000 |
parents | de344498875e |
children | 45c3780b1a96 |
comparison
equal
deleted
inserted
replaced
8224:6771b3544991 | 8225:d133e597db2d |
---|---|
387 int alpha, int beta, int beta2, int chroma, int edge){ | 387 int alpha, int beta, int beta2, int chroma, int edge){ |
388 rv40_adaptive_loop_filter(src, stride, 1, dmode, lim_q1, lim_p1, | 388 rv40_adaptive_loop_filter(src, stride, 1, dmode, lim_q1, lim_p1, |
389 alpha, beta, beta2, chroma, edge); | 389 alpha, beta, beta2, chroma, edge); |
390 } | 390 } |
391 | 391 |
392 enum RV40BlockPos{ | |
393 POS_CUR, | |
394 POS_TOP, | |
395 POS_LEFT, | |
396 POS_BOTTOM, | |
397 }; | |
398 | |
399 #define MASK_CUR 0x0001 | |
400 #define MASK_RIGHT 0x0008 | |
401 #define MASK_BOTTOM 0x0010 | |
402 #define MASK_TOP 0x1000 | |
403 #define MASK_Y_TOP_ROW 0x000F | |
404 #define MASK_Y_LAST_ROW 0xF000 | |
405 #define MASK_Y_LEFT_COL 0x1111 | |
406 #define MASK_Y_RIGHT_COL 0x8888 | |
407 #define MASK_C_TOP_ROW 0x0003 | |
408 #define MASK_C_LAST_ROW 0x000C | |
409 #define MASK_C_LEFT_COL 0x0005 | |
410 #define MASK_C_RIGHT_COL 0x000A | |
411 | |
412 static const int neighbour_offs_x[4] = { 0, 0, -1, 0 }; | |
413 static const int neighbour_offs_y[4] = { 0, -1, 0, 1 }; | |
414 | |
415 /** | |
416 * RV40 loop filtering function | |
417 */ | |
418 static void rv40_loop_filter(RV34DecContext *r, int row) | |
419 { | |
420 MpegEncContext *s = &r->s; | |
421 int mb_pos, mb_x; | |
422 int i, j, k; | |
423 uint8_t *Y, *C; | |
424 int alpha, beta, betaY, betaC; | |
425 int q; | |
426 int mbtype[4]; ///< current macroblock and its neighbours types | |
427 /** | |
428 * flags indicating that macroblock can be filtered with strong filter | |
429 * it is set only for intra coded MB and MB with DCs coded separately | |
430 */ | |
431 int mb_strong[4]; | |
432 int clip[4]; ///< MB filter clipping value calculated from filtering strength | |
433 /** | |
434 * coded block patterns for luma part of current macroblock and its neighbours | |
435 * Format: | |
436 * LSB corresponds to the top left block, | |
437 * each nibble represents one row of subblocks. | |
438 */ | |
439 int cbp[4]; | |
440 /** | |
441 * coded block patterns for chroma part of current macroblock and its neighbours | |
442 * Format is the same as for luma with two subblocks in a row. | |
443 */ | |
444 int uvcbp[4][2]; | |
445 /** | |
446 * This mask represents the pattern of luma subblocks that should be filtered | |
447 * in addition to the coded ones because because they lie at the edge of | |
448 * 8x8 block with different enough motion vectors | |
449 */ | |
450 int mvmasks[4]; | |
451 | |
452 mb_pos = row * s->mb_stride; | |
453 for(mb_x = 0; mb_x < s->mb_width; mb_x++, mb_pos++){ | |
454 int mbtype = s->current_picture_ptr->mb_type[mb_pos]; | |
455 if(IS_INTRA(mbtype) || IS_SEPARATE_DC(mbtype)) | |
456 r->cbp_luma [mb_pos] = 0xFFFF; | |
457 if(IS_INTRA(mbtype)) | |
458 r->cbp_chroma[mb_pos] = 0xFF; | |
459 } | |
460 mb_pos = row * s->mb_stride; | |
461 for(mb_x = 0; mb_x < s->mb_width; mb_x++, mb_pos++){ | |
462 int y_h_deblock, y_v_deblock; | |
463 int c_v_deblock[2], c_h_deblock[2]; | |
464 int clip_left; | |
465 int avail[4]; | |
466 int y_to_deblock, c_to_deblock[2]; | |
467 | |
468 q = s->current_picture_ptr->qscale_table[mb_pos]; | |
469 alpha = rv40_alpha_tab[q]; | |
470 beta = rv40_beta_tab [q]; | |
471 betaY = betaC = beta * 3; | |
472 if(s->width * s->height <= 176*144) | |
473 betaY += beta; | |
474 | |
475 avail[0] = 1; | |
476 avail[1] = row; | |
477 avail[2] = mb_x; | |
478 avail[3] = row < s->mb_height - 1; | |
479 for(i = 0; i < 4; i++){ | |
480 if(avail[i]){ | |
481 int pos = mb_pos + neighbour_offs_x[i] + neighbour_offs_y[i]*s->mb_stride; | |
482 mvmasks[i] = r->deblock_coefs[pos]; | |
483 mbtype [i] = s->current_picture_ptr->mb_type[pos]; | |
484 cbp [i] = r->cbp_luma[pos]; | |
485 uvcbp[i][0] = r->cbp_chroma[pos] & 0xF; | |
486 uvcbp[i][1] = r->cbp_chroma[pos] >> 4; | |
487 }else{ | |
488 mvmasks[i] = 0; | |
489 mbtype [i] = mbtype[0]; | |
490 cbp [i] = 0; | |
491 uvcbp[i][0] = uvcbp[i][1] = 0; | |
492 } | |
493 mb_strong[i] = IS_INTRA(mbtype[i]) || IS_SEPARATE_DC(mbtype[i]); | |
494 clip[i] = rv40_filter_clip_tbl[mb_strong[i] + 1][q]; | |
495 } | |
496 y_to_deblock = cbp[POS_CUR] | |
497 | (cbp[POS_BOTTOM] << 16) | |
498 | mvmasks[POS_CUR] | |
499 | (mvmasks[POS_BOTTOM] << 16); | |
500 /* This pattern contains bits signalling that horizontal edges of | |
501 * the current block can be filtered. | |
502 * That happens when either of adjacent subblocks is coded or lies on | |
503 * the edge of 8x8 blocks with motion vectors differing by more than | |
504 * 3/4 pel in any component (any edge orientation for some reason). | |
505 */ | |
506 y_h_deblock = y_to_deblock | |
507 | ((cbp[POS_CUR] << 4) & ~MASK_Y_TOP_ROW) | |
508 | ((cbp[POS_TOP] & MASK_Y_LAST_ROW) >> 12); | |
509 /* This pattern contains bits signalling that vertical edges of | |
510 * the current block can be filtered. | |
511 * That happens when either of adjacent subblocks is coded or lies on | |
512 * the edge of 8x8 blocks with motion vectors differing by more than | |
513 * 3/4 pel in any component (any edge orientation for some reason). | |
514 */ | |
515 y_v_deblock = y_to_deblock | |
516 | ((cbp[POS_CUR] << 1) & ~MASK_Y_LEFT_COL) | |
517 | ((cbp[POS_LEFT] & MASK_Y_RIGHT_COL) >> 3); | |
518 if(!mb_x) | |
519 y_v_deblock &= ~MASK_Y_LEFT_COL; | |
520 if(!row) | |
521 y_h_deblock &= ~MASK_Y_TOP_ROW; | |
522 if(row == s->mb_height - 1 || (mb_strong[POS_CUR] || mb_strong[POS_BOTTOM])) | |
523 y_h_deblock &= ~(MASK_Y_TOP_ROW << 16); | |
524 /* Calculating chroma patterns is similar and easier since there is | |
525 * no motion vector pattern for them. | |
526 */ | |
527 for(i = 0; i < 2; i++){ | |
528 c_to_deblock[i] = (uvcbp[POS_BOTTOM][i] << 4) | uvcbp[POS_CUR][i]; | |
529 c_v_deblock[i] = c_to_deblock[i] | |
530 | ((uvcbp[POS_CUR] [i] << 1) & ~MASK_C_LEFT_COL) | |
531 | ((uvcbp[POS_LEFT][i] & MASK_C_RIGHT_COL) >> 1); | |
532 c_h_deblock[i] = c_to_deblock[i] | |
533 | ((uvcbp[POS_TOP][i] & MASK_C_LAST_ROW) >> 2) | |
534 | (uvcbp[POS_CUR][i] << 2); | |
535 if(!mb_x) | |
536 c_v_deblock[i] &= ~MASK_C_LEFT_COL; | |
537 if(!row) | |
538 c_h_deblock[i] &= ~MASK_C_TOP_ROW; | |
539 if(row == s->mb_height - 1 || mb_strong[POS_CUR] || mb_strong[POS_BOTTOM]) | |
540 c_h_deblock[i] &= ~(MASK_C_TOP_ROW << 4); | |
541 } | |
542 | |
543 for(j = 0; j < 16; j += 4){ | |
544 Y = s->current_picture_ptr->data[0] + mb_x*16 + (row*16 + j) * s->linesize; | |
545 for(i = 0; i < 4; i++, Y += 4){ | |
546 int ij = i + j; | |
547 int clip_cur = y_to_deblock & (MASK_CUR << ij) ? clip[POS_CUR] : 0; | |
548 int dither = j ? ij : i*4; | |
549 | |
550 // if bottom block is coded then we can filter its top edge | |
551 // (or bottom edge of this block, which is the same) | |
552 if(y_h_deblock & (MASK_BOTTOM << ij)){ | |
553 rv40_h_loop_filter(Y+4*s->linesize, s->linesize, dither, | |
554 y_to_deblock & (MASK_BOTTOM << ij) ? clip[POS_CUR] : 0, | |
555 clip_cur, | |
556 alpha, beta, betaY, 0, 0); | |
557 } | |
558 // filter left block edge in ordinary mode (with low filtering strength) | |
559 if(y_v_deblock & (MASK_CUR << ij) && (i || !(mb_strong[POS_CUR] || mb_strong[POS_LEFT]))){ | |
560 if(!i) | |
561 clip_left = (cbp[POS_LEFT] | mvmasks[POS_LEFT]) & (MASK_RIGHT << j) ? clip[POS_LEFT] : 0; | |
562 else | |
563 clip_left = y_to_deblock & (MASK_CUR << (ij-1)) ? clip[POS_CUR] : 0; | |
564 rv40_v_loop_filter(Y, s->linesize, dither, | |
565 clip_cur, | |
566 clip_left, | |
567 alpha, beta, betaY, 0, 0); | |
568 } | |
569 // filter top edge of the current macroblock when filtering strength is high | |
570 if(!j && y_h_deblock & (MASK_CUR << i) && (mb_strong[POS_CUR] || mb_strong[POS_TOP])){ | |
571 rv40_h_loop_filter(Y, s->linesize, dither, | |
572 clip_cur, | |
573 (cbp[POS_TOP] | mvmasks[POS_TOP]) & (MASK_TOP << i) ? clip[POS_TOP] : 0, | |
574 alpha, beta, betaY, 0, 1); | |
575 } | |
576 // filter left block edge in edge mode (with high filtering strength) | |
577 if(y_v_deblock & (MASK_CUR << ij) && !i && (mb_strong[POS_CUR] || mb_strong[POS_LEFT])){ | |
578 clip_left = (cbp[POS_LEFT] | mvmasks[POS_LEFT]) & (MASK_RIGHT << j) ? clip[POS_LEFT] : 0; | |
579 rv40_v_loop_filter(Y, s->linesize, dither, | |
580 clip_cur, | |
581 clip_left, | |
582 alpha, beta, betaY, 0, 1); | |
583 } | |
584 } | |
585 } | |
586 for(k = 0; k < 2; k++){ | |
587 for(j = 0; j < 2; j++){ | |
588 C = s->current_picture_ptr->data[k+1] + mb_x*8 + (row*8 + j*4) * s->uvlinesize; | |
589 for(i = 0; i < 2; i++, C += 4){ | |
590 int ij = i + j*2; | |
591 int clip_cur = c_to_deblock[k] & (MASK_CUR << ij) ? clip[POS_CUR] : 0; | |
592 if(c_h_deblock[k] & (MASK_CUR << (ij+2))){ | |
593 int clip_bot = c_to_deblock[k] & (MASK_CUR << (ij+2)) ? clip[POS_CUR] : 0; | |
594 rv40_h_loop_filter(C+4*s->uvlinesize, s->uvlinesize, i*8, | |
595 clip_bot, | |
596 clip_cur, | |
597 alpha, beta, betaC, 1, 0); | |
598 } | |
599 if((c_v_deblock[k] & (MASK_CUR << ij)) && (i || !(mb_strong[POS_CUR] || mb_strong[POS_LEFT]))){ | |
600 if(!i) | |
601 clip_left = uvcbp[POS_LEFT][k] & (MASK_CUR << (2*j+1)) ? clip[POS_LEFT] : 0; | |
602 else | |
603 clip_left = c_to_deblock[k] & (MASK_CUR << (ij-1)) ? clip[POS_CUR] : 0; | |
604 rv40_v_loop_filter(C, s->uvlinesize, j*8, | |
605 clip_cur, | |
606 clip_left, | |
607 alpha, beta, betaC, 1, 0); | |
608 } | |
609 if(!j && c_h_deblock[k] & (MASK_CUR << ij) && (mb_strong[POS_CUR] || mb_strong[POS_TOP])){ | |
610 int clip_top = uvcbp[POS_TOP][k] & (MASK_CUR << (ij+2)) ? clip[POS_TOP] : 0; | |
611 rv40_h_loop_filter(C, s->uvlinesize, i*8, | |
612 clip_cur, | |
613 clip_top, | |
614 alpha, beta, betaC, 1, 1); | |
615 } | |
616 if(c_v_deblock[k] & (MASK_CUR << ij) && !i && (mb_strong[POS_CUR] || mb_strong[POS_LEFT])){ | |
617 clip_left = uvcbp[POS_LEFT][k] & (MASK_CUR << (2*j+1)) ? clip[POS_LEFT] : 0; | |
618 rv40_v_loop_filter(C, s->uvlinesize, j*8, | |
619 clip_cur, | |
620 clip_left, | |
621 alpha, beta, betaC, 1, 1); | |
622 } | |
623 } | |
624 } | |
625 } | |
626 } | |
627 } | |
628 | |
392 /** | 629 /** |
393 * Initialize decoder. | 630 * Initialize decoder. |
394 */ | 631 */ |
395 static av_cold int rv40_decode_init(AVCodecContext *avctx) | 632 static av_cold int rv40_decode_init(AVCodecContext *avctx) |
396 { | 633 { |
401 if(!aic_top_vlc.bits) | 638 if(!aic_top_vlc.bits) |
402 rv40_init_tables(); | 639 rv40_init_tables(); |
403 r->parse_slice_header = rv40_parse_slice_header; | 640 r->parse_slice_header = rv40_parse_slice_header; |
404 r->decode_intra_types = rv40_decode_intra_types; | 641 r->decode_intra_types = rv40_decode_intra_types; |
405 r->decode_mb_info = rv40_decode_mb_info; | 642 r->decode_mb_info = rv40_decode_mb_info; |
643 r->loop_filter = rv40_loop_filter; | |
406 r->luma_dc_quant_i = rv40_luma_dc_quant[0]; | 644 r->luma_dc_quant_i = rv40_luma_dc_quant[0]; |
407 r->luma_dc_quant_p = rv40_luma_dc_quant[1]; | 645 r->luma_dc_quant_p = rv40_luma_dc_quant[1]; |
408 return 0; | 646 return 0; |
409 } | 647 } |
410 | 648 |