1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
|
/*****************************************************************************
* Copyright (C) 2013-2020 MulticoreWare, Inc
*
* Authors: Deepthi Nandakumar <deepthi@multicorewareinc.com>
* Min Chen <chenm003@163.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
*
* This program is also available under a commercial proprietary license.
* For more information, contact us at license @ x265.com.
*****************************************************************************/
#include "common.h"
#include "slice.h"
#include "framedata.h"
#include "picyuv.h"
#include "predict.h"
#include "primitives.h"
using namespace X265_NS;
#if _MSC_VER
#pragma warning(disable: 4127) // conditional expression is constant
#endif
PredictionUnit::PredictionUnit(const CUData& cu, const CUGeom& cuGeom, int puIdx)
{
/* address of CTU */
ctuAddr = cu.m_cuAddr;
/* offset of CU */
cuAbsPartIdx = cuGeom.absPartIdx;
/* offset and dimensions of PU */
cu.getPartIndexAndSize(puIdx, puAbsPartIdx, width, height);
}
namespace
{
inline pixel weightBidir(int w0, int16_t P0, int w1, int16_t P1, int round, int shift, int offset)
{
return x265_clip((w0 * (P0 + IF_INTERNAL_OFFS) + w1 * (P1 + IF_INTERNAL_OFFS) + round + (offset * (1 << (shift - 1)))) >> shift);
}
}
Predict::Predict()
{
}
Predict::~Predict()
{
m_predShortYuv[0].destroy();
m_predShortYuv[1].destroy();
}
bool Predict::allocBuffers(int csp)
{
m_csp = csp;
m_hChromaShift = CHROMA_H_SHIFT(csp);
m_vChromaShift = CHROMA_V_SHIFT(csp);
return m_predShortYuv[0].create(MAX_CU_SIZE, csp) && m_predShortYuv[1].create(MAX_CU_SIZE, csp);
}
void Predict::motionCompensation(const CUData& cu, const PredictionUnit& pu, Yuv& predYuv, bool bLuma, bool bChroma)
{
int refIdx0 = cu.m_refIdx[0][pu.puAbsPartIdx];
int refIdx1 = cu.m_refIdx[1][pu.puAbsPartIdx];
if (cu.m_slice->isInterP())
{
/* P Slice */
WeightValues wv0[3];
X265_CHECK(refIdx0 >= 0, "invalid P refidx\n");
X265_CHECK(refIdx0 < cu.m_slice->m_numRefIdx[0], "P refidx out of range\n");
const WeightParam *wp0 = cu.m_slice->m_weightPredTable[0][refIdx0];
MV mv0 = cu.m_mv[0][pu.puAbsPartIdx];
cu.clipMv(mv0);
if (cu.m_slice->m_pps->bUseWeightPred && wp0->wtPresent)
{
for (int plane = 0; plane < (bChroma ? 3 : 1); plane++)
{
wv0[plane].w = wp0[plane].inputWeight;
wv0[plane].offset = wp0[plane].inputOffset * (1 << (X265_DEPTH - 8));
wv0[plane].shift = wp0[plane].log2WeightDenom;
wv0[plane].round = wp0[plane].log2WeightDenom >= 1 ? 1 << (wp0[plane].log2WeightDenom - 1) : 0;
}
ShortYuv& shortYuv = m_predShortYuv[0];
if (bLuma)
predInterLumaShort(pu, shortYuv, *cu.m_slice->m_refReconPicList[0][refIdx0], mv0);
if (bChroma)
predInterChromaShort(pu, shortYuv, *cu.m_slice->m_refReconPicList[0][refIdx0], mv0);
addWeightUni(pu, predYuv, shortYuv, wv0, bLuma, bChroma);
}
else
{
#if ENABLE_SCC_EXT
if (cu.m_slice->m_param->bEnableSCC && refIdx0 == (cu.m_slice->m_numRefIdx[0] - 1))
{
if (bLuma)
predInterLumaPixel(pu, predYuv, *cu.m_slice->m_refFrameList[0][refIdx0]->m_reconPic[1], mv0);
if (bChroma)
predInterChromaPixel(pu, predYuv, *cu.m_slice->m_refFrameList[0][refIdx0]->m_reconPic[1], mv0);
}
else
#endif
{
if (bLuma)
predInterLumaPixel(pu, predYuv, *cu.m_slice->m_refReconPicList[0][refIdx0], mv0);
if (bChroma)
predInterChromaPixel(pu, predYuv, *cu.m_slice->m_refReconPicList[0][refIdx0], mv0);
}
}
}
else
{
/* B Slice */
WeightValues wv0[3], wv1[3];
const WeightParam *pwp0, *pwp1;
X265_CHECK(refIdx0 < cu.m_slice->m_numRefIdx[0], "bidir refidx0 out of range\n");
X265_CHECK(refIdx1 < cu.m_slice->m_numRefIdx[1], "bidir refidx1 out of range\n");
if (cu.m_slice->m_pps->bUseWeightedBiPred)
{
pwp0 = refIdx0 >= 0 ? cu.m_slice->m_weightPredTable[0][refIdx0] : NULL;
pwp1 = refIdx1 >= 0 ? cu.m_slice->m_weightPredTable[1][refIdx1] : NULL;
if (pwp0 && pwp1 && (pwp0->wtPresent || pwp1->wtPresent))
{
/* biprediction weighting */
for (int plane = 0; plane < (bChroma ? 3 : 1); plane++)
{
wv0[plane].w = pwp0[plane].inputWeight;
wv0[plane].o = pwp0[plane].inputOffset * (1 << (X265_DEPTH - 8));
wv0[plane].shift = pwp0[plane].log2WeightDenom;
wv0[plane].round = 1 << pwp0[plane].log2WeightDenom;
wv1[plane].w = pwp1[plane].inputWeight;
wv1[plane].o = pwp1[plane].inputOffset * (1 << (X265_DEPTH - 8));
wv1[plane].shift = wv0[plane].shift;
wv1[plane].round = wv0[plane].round;
}
}
else
{
/* uniprediction weighting, always outputs to wv0 */
const WeightParam* pwp = (refIdx0 >= 0) ? pwp0 : pwp1;
for (int plane = 0; plane < (bChroma ? 3 : 1); plane++)
{
wv0[plane].w = pwp[plane].inputWeight;
wv0[plane].offset = pwp[plane].inputOffset * (1 << (X265_DEPTH - 8));
wv0[plane].shift = pwp[plane].log2WeightDenom;
wv0[plane].round = pwp[plane].log2WeightDenom >= 1 ? 1 << (pwp[plane].log2WeightDenom - 1) : 0;
}
}
}
else
pwp0 = pwp1 = NULL;
if (refIdx0 >= 0 && refIdx1 >= 0)
{
MV mv0 = cu.m_mv[0][pu.puAbsPartIdx];
MV mv1 = cu.m_mv[1][pu.puAbsPartIdx];
cu.clipMv(mv0);
cu.clipMv(mv1);
if (bLuma)
{
#if ENABLE_SCC_EXT
if (cu.m_slice->m_param->bEnableSCC && refIdx0 == (cu.m_slice->m_numRefIdx[0] - 1))
predInterLumaShort(pu, m_predShortYuv[0], *cu.m_slice->m_refFrameList[0][refIdx0]->m_reconPic[1], mv0);
else
#endif
predInterLumaShort(pu, m_predShortYuv[0], *cu.m_slice->m_refReconPicList[0][refIdx0], mv0);
predInterLumaShort(pu, m_predShortYuv[1], *cu.m_slice->m_refReconPicList[1][refIdx1], mv1);
}
if (bChroma)
{
#if ENABLE_SCC_EXT
if (cu.m_slice->m_param->bEnableSCC && refIdx0 == (cu.m_slice->m_numRefIdx[0] - 1))
predInterChromaShort(pu, m_predShortYuv[0], *cu.m_slice->m_refFrameList[0][refIdx0]->m_reconPic[1], mv0);
else
#endif
predInterChromaShort(pu, m_predShortYuv[0], *cu.m_slice->m_refReconPicList[0][refIdx0], mv0);
predInterChromaShort(pu, m_predShortYuv[1], *cu.m_slice->m_refReconPicList[1][refIdx1], mv1);
}
if (pwp0 && pwp1 && (pwp0->wtPresent || pwp1->wtPresent))
addWeightBi(pu, predYuv, m_predShortYuv[0], m_predShortYuv[1], wv0, wv1, bLuma, bChroma);
else
predYuv.addAvg(m_predShortYuv[0], m_predShortYuv[1], pu.puAbsPartIdx, pu.width, pu.height, bLuma, bChroma);
}
else if (refIdx0 >= 0)
{
MV mv0 = cu.m_mv[0][pu.puAbsPartIdx];
cu.clipMv(mv0);
if (pwp0 && pwp0->wtPresent)
{
ShortYuv& shortYuv = m_predShortYuv[0];
if (bLuma)
predInterLumaShort(pu, shortYuv, *cu.m_slice->m_refReconPicList[0][refIdx0], mv0);
if (bChroma)
predInterChromaShort(pu, shortYuv, *cu.m_slice->m_refReconPicList[0][refIdx0], mv0);
addWeightUni(pu, predYuv, shortYuv, wv0, bLuma, bChroma);
}
else
{
#if ENABLE_SCC_EXT
if (cu.m_slice->m_param->bEnableSCC && refIdx0 == (cu.m_slice->m_numRefIdx[0] - 1))
{
if (bLuma)
predInterLumaPixel(pu, predYuv, *cu.m_slice->m_refFrameList[0][refIdx0]->m_reconPic[1], mv0);
if (bChroma)
predInterChromaPixel(pu, predYuv, *cu.m_slice->m_refFrameList[0][refIdx0]->m_reconPic[1], mv0);
}
else
#endif
{
if (bLuma)
predInterLumaPixel(pu, predYuv, *cu.m_slice->m_refReconPicList[0][refIdx0], mv0);
if (bChroma)
predInterChromaPixel(pu, predYuv, *cu.m_slice->m_refReconPicList[0][refIdx0], mv0);
}
}
}
else
{
MV mv1 = cu.m_mv[1][pu.puAbsPartIdx];
cu.clipMv(mv1);
/* uniprediction to L1 */
X265_CHECK(refIdx1 >= 0, "refidx1 was not positive\n");
if (pwp1 && pwp1->wtPresent)
{
ShortYuv& shortYuv = m_predShortYuv[0];
if (bLuma)
predInterLumaShort(pu, shortYuv, *cu.m_slice->m_refReconPicList[1][refIdx1], mv1);
if (bChroma)
predInterChromaShort(pu, shortYuv, *cu.m_slice->m_refReconPicList[1][refIdx1], mv1);
addWeightUni(pu, predYuv, shortYuv, wv0, bLuma, bChroma);
}
else
{
if (bLuma)
predInterLumaPixel(pu, predYuv, *cu.m_slice->m_refReconPicList[1][refIdx1], mv1);
if (bChroma)
predInterChromaPixel(pu, predYuv, *cu.m_slice->m_refReconPicList[1][refIdx1], mv1);
}
}
}
}
void Predict::predInterLumaPixel(const PredictionUnit& pu, Yuv& dstYuv, const PicYuv& refPic, const MV& mv) const
{
pixel* dst = dstYuv.getLumaAddr(pu.puAbsPartIdx);
intptr_t dstStride = dstYuv.m_size;
intptr_t srcStride = refPic.m_stride;
intptr_t srcOffset = (mv.x >> 2) + (mv.y >> 2) * srcStride;
int partEnum = partitionFromSizes(pu.width, pu.height);
const pixel* src = refPic.getLumaAddr(pu.ctuAddr, pu.cuAbsPartIdx + pu.puAbsPartIdx) + srcOffset;
int xFrac = mv.x & 3;
int yFrac = mv.y & 3;
if (!(yFrac | xFrac))
primitives.pu[partEnum].copy_pp(dst, dstStride, src, srcStride);
else if (!yFrac)
primitives.pu[partEnum].luma_hpp(src, srcStride, dst, dstStride, xFrac);
else if (!xFrac)
primitives.pu[partEnum].luma_vpp(src, srcStride, dst, dstStride, yFrac);
else
primitives.pu[partEnum].luma_hvpp(src, srcStride, dst, dstStride, xFrac, yFrac);
}
void Predict::predInterLumaShort(const PredictionUnit& pu, ShortYuv& dstSYuv, const PicYuv& refPic, const MV& mv) const
{
int16_t* dst = dstSYuv.getLumaAddr(pu.puAbsPartIdx);
intptr_t dstStride = dstSYuv.m_size;
intptr_t srcStride = refPic.m_stride;
intptr_t srcOffset = (mv.x >> 2) + (mv.y >> 2) * srcStride;
const pixel* src = refPic.getLumaAddr(pu.ctuAddr, pu.cuAbsPartIdx + pu.puAbsPartIdx) + srcOffset;
int partEnum = partitionFromSizes(pu.width, pu.height);
X265_CHECK((pu.width % 4) + (pu.height % 4) == 0, "width or height not divisible by 4\n");
X265_CHECK(dstStride == MAX_CU_SIZE, "stride expected to be max cu size\n");
int xFrac = mv.x & 3;
int yFrac = mv.y & 3;
if (!(yFrac | xFrac))
{
bool srcbufferAlignCheck = (refPic.m_cuOffsetY[pu.ctuAddr] + refPic.m_buOffsetY[pu.cuAbsPartIdx + pu.puAbsPartIdx] + srcOffset) % 64 == 0;
bool dstbufferAlignCheck = (dstSYuv.getAddrOffset(pu.puAbsPartIdx, dstSYuv.m_size) % 64) == 0;
primitives.pu[partEnum].convert_p2s[srcStride % 64 == 0 && dstStride % 64 == 0 && srcbufferAlignCheck && dstbufferAlignCheck](src, srcStride, dst, dstStride);
}
else if (!yFrac)
primitives.pu[partEnum].luma_hps(src, srcStride, dst, dstStride, xFrac, 0);
else if (!xFrac)
primitives.pu[partEnum].luma_vps(src, srcStride, dst, dstStride, yFrac);
else
{
ALIGN_VAR_32(int16_t, immed[MAX_CU_SIZE * (MAX_CU_SIZE + NTAPS_LUMA - 1)]);
int immedStride = pu.width;
int halfFilterSize = NTAPS_LUMA >> 1;
primitives.pu[partEnum].luma_hps(src, srcStride, immed, immedStride, xFrac, 1);
primitives.pu[partEnum].luma_vss(immed + (halfFilterSize - 1) * immedStride, immedStride, dst, dstStride, yFrac);
}
}
void Predict::predInterChromaPixel(const PredictionUnit& pu, Yuv& dstYuv, const PicYuv& refPic, const MV& mv) const
{
intptr_t dstStride = dstYuv.m_csize;
intptr_t refStride = refPic.m_strideC;
int mvx = mv.x << (1 - m_hChromaShift);
int mvy = mv.y << (1 - m_vChromaShift);
intptr_t refOffset = (mvx >> 3) + (mvy >> 3) * refStride;
const pixel* refCb = refPic.getCbAddr(pu.ctuAddr, pu.cuAbsPartIdx + pu.puAbsPartIdx) + refOffset;
const pixel* refCr = refPic.getCrAddr(pu.ctuAddr, pu.cuAbsPartIdx + pu.puAbsPartIdx) + refOffset;
pixel* dstCb = dstYuv.getCbAddr(pu.puAbsPartIdx);
pixel* dstCr = dstYuv.getCrAddr(pu.puAbsPartIdx);
int partEnum = partitionFromSizes(pu.width, pu.height);
int xFrac = mvx & 7;
int yFrac = mvy & 7;
if (!(yFrac | xFrac))
{
primitives.chroma[m_csp].pu[partEnum].copy_pp(dstCb, dstStride, refCb, refStride);
primitives.chroma[m_csp].pu[partEnum].copy_pp(dstCr, dstStride, refCr, refStride);
}
else if (!yFrac)
{
primitives.chroma[m_csp].pu[partEnum].filter_hpp(refCb, refStride, dstCb, dstStride, xFrac);
primitives.chroma[m_csp].pu[partEnum].filter_hpp(refCr, refStride, dstCr, dstStride, xFrac);
}
else if (!xFrac)
{
primitives.chroma[m_csp].pu[partEnum].filter_vpp(refCb, refStride, dstCb, dstStride, yFrac);
primitives.chroma[m_csp].pu[partEnum].filter_vpp(refCr, refStride, dstCr, dstStride, yFrac);
}
else
{
ALIGN_VAR_32(int16_t, immed[MAX_CU_SIZE * (MAX_CU_SIZE + NTAPS_CHROMA - 1)]);
int immedStride = pu.width >> m_hChromaShift;
int halfFilterSize = NTAPS_CHROMA >> 1;
primitives.chroma[m_csp].pu[partEnum].filter_hps(refCb, refStride, immed, immedStride, xFrac, 1);
primitives.chroma[m_csp].pu[partEnum].filter_vsp(immed + (halfFilterSize - 1) * immedStride, immedStride, dstCb, dstStride, yFrac);
primitives.chroma[m_csp].pu[partEnum].filter_hps(refCr, refStride, immed, immedStride, xFrac, 1);
primitives.chroma[m_csp].pu[partEnum].filter_vsp(immed + (halfFilterSize - 1) * immedStride, immedStride, dstCr, dstStride, yFrac);
}
}
void Predict::predInterChromaShort(const PredictionUnit& pu, ShortYuv& dstSYuv, const PicYuv& refPic, const MV& mv) const
{
intptr_t dstStride = dstSYuv.m_csize;
intptr_t refStride = refPic.m_strideC;
int mvx = mv.x << (1 - m_hChromaShift);
int mvy = mv.y << (1 - m_vChromaShift);
intptr_t refOffset = (mvx >> 3) + (mvy >> 3) * refStride;
const pixel* refCb = refPic.getCbAddr(pu.ctuAddr, pu.cuAbsPartIdx + pu.puAbsPartIdx) + refOffset;
const pixel* refCr = refPic.getCrAddr(pu.ctuAddr, pu.cuAbsPartIdx + pu.puAbsPartIdx) + refOffset;
int16_t* dstCb = dstSYuv.getCbAddr(pu.puAbsPartIdx);
int16_t* dstCr = dstSYuv.getCrAddr(pu.puAbsPartIdx);
int partEnum = partitionFromSizes(pu.width, pu.height);
uint32_t cxWidth = pu.width >> m_hChromaShift;
X265_CHECK(((cxWidth | (pu.height >> m_vChromaShift)) % 2) == 0, "chroma block size expected to be multiple of 2\n");
int xFrac = mvx & 7;
int yFrac = mvy & 7;
if (!(yFrac | xFrac))
{
bool srcbufferAlignCheckC = (refPic.m_cuOffsetC[pu.ctuAddr] + refPic.m_buOffsetC[pu.cuAbsPartIdx + pu.puAbsPartIdx] + refOffset) % 64 == 0;
bool dstbufferAlignCheckC = dstSYuv.getChromaAddrOffset(pu.puAbsPartIdx) % 64 == 0;
primitives.chroma[m_csp].pu[partEnum].p2s[refStride % 64 == 0 && dstStride % 64 == 0 && srcbufferAlignCheckC && dstbufferAlignCheckC](refCb, refStride, dstCb, dstStride);
primitives.chroma[m_csp].pu[partEnum].p2s[refStride % 64 == 0 && dstStride % 64 == 0 && srcbufferAlignCheckC && dstbufferAlignCheckC](refCr, refStride, dstCr, dstStride);
}
else if (!yFrac)
{
primitives.chroma[m_csp].pu[partEnum].filter_hps(refCb, refStride, dstCb, dstStride, xFrac, 0);
primitives.chroma[m_csp].pu[partEnum].filter_hps(refCr, refStride, dstCr, dstStride, xFrac, 0);
}
else if (!xFrac)
{
primitives.chroma[m_csp].pu[partEnum].filter_vps(refCb, refStride, dstCb, dstStride, yFrac);
primitives.chroma[m_csp].pu[partEnum].filter_vps(refCr, refStride, dstCr, dstStride, yFrac);
}
else
{
ALIGN_VAR_32(int16_t, immed[MAX_CU_SIZE * (MAX_CU_SIZE + NTAPS_CHROMA - 1)]);
int immedStride = cxWidth;
int halfFilterSize = NTAPS_CHROMA >> 1;
primitives.chroma[m_csp].pu[partEnum].filter_hps(refCb, refStride, immed, immedStride, xFrac, 1);
primitives.chroma[m_csp].pu[partEnum].filter_vss(immed + (halfFilterSize - 1) * immedStride, immedStride, dstCb, dstStride, yFrac);
primitives.chroma[m_csp].pu[partEnum].filter_hps(refCr, refStride, immed, immedStride, xFrac, 1);
primitives.chroma[m_csp].pu[partEnum].filter_vss(immed + (halfFilterSize - 1) * immedStride, immedStride, dstCr, dstStride, yFrac);
}
}
/* weighted averaging for bi-pred */
void Predict::addWeightBi(const PredictionUnit& pu, Yuv& predYuv, const ShortYuv& srcYuv0, const ShortYuv& srcYuv1, const WeightValues wp0[3], const WeightValues wp1[3], bool bLuma, bool bChroma) const
{
int x, y;
int w0, w1, offset, shiftNum, shift, round;
uint32_t src0Stride, src1Stride, dststride;
if (bLuma)
{
pixel* dstY = predYuv.getLumaAddr(pu.puAbsPartIdx);
const int16_t* srcY0 = srcYuv0.getLumaAddr(pu.puAbsPartIdx);
const int16_t* srcY1 = srcYuv1.getLumaAddr(pu.puAbsPartIdx);
// Luma
w0 = wp0[0].w;
offset = wp0[0].o + wp1[0].o;
shiftNum = IF_INTERNAL_PREC - X265_DEPTH;
shift = wp0[0].shift + shiftNum + 1;
round = shift ? (1 << (shift - 1)) : 0;
w1 = wp1[0].w;
src0Stride = srcYuv0.m_size;
src1Stride = srcYuv1.m_size;
dststride = predYuv.m_size;
// TODO: can we use weight_sp here?
for (y = pu.height - 1; y >= 0; y--)
{
for (x = pu.width - 1; x >= 0; )
{
// note: luma min width is 4
dstY[x] = weightBidir(w0, srcY0[x], w1, srcY1[x], round, shift, offset);
x--;
dstY[x] = weightBidir(w0, srcY0[x], w1, srcY1[x], round, shift, offset);
x--;
dstY[x] = weightBidir(w0, srcY0[x], w1, srcY1[x], round, shift, offset);
x--;
dstY[x] = weightBidir(w0, srcY0[x], w1, srcY1[x], round, shift, offset);
x--;
}
srcY0 += src0Stride;
srcY1 += src1Stride;
dstY += dststride;
}
}
if (bChroma)
{
pixel* dstU = predYuv.getCbAddr(pu.puAbsPartIdx);
pixel* dstV = predYuv.getCrAddr(pu.puAbsPartIdx);
const int16_t* srcU0 = srcYuv0.getCbAddr(pu.puAbsPartIdx);
const int16_t* srcV0 = srcYuv0.getCrAddr(pu.puAbsPartIdx);
const int16_t* srcU1 = srcYuv1.getCbAddr(pu.puAbsPartIdx);
const int16_t* srcV1 = srcYuv1.getCrAddr(pu.puAbsPartIdx);
// Chroma U
w0 = wp0[1].w;
offset = wp0[1].o + wp1[1].o;
shiftNum = IF_INTERNAL_PREC - X265_DEPTH;
shift = wp0[1].shift + shiftNum + 1;
round = shift ? (1 << (shift - 1)) : 0;
w1 = wp1[1].w;
src0Stride = srcYuv0.m_csize;
src1Stride = srcYuv1.m_csize;
dststride = predYuv.m_csize;
uint32_t cwidth = pu.width >> srcYuv0.m_hChromaShift;
uint32_t cheight = pu.height >> srcYuv0.m_vChromaShift;
// TODO: can we use weight_sp here?
for (y = cheight - 1; y >= 0; y--)
{
for (x = cwidth - 1; x >= 0;)
{
// note: chroma min width is 2
dstU[x] = weightBidir(w0, srcU0[x], w1, srcU1[x], round, shift, offset);
x--;
dstU[x] = weightBidir(w0, srcU0[x], w1, srcU1[x], round, shift, offset);
x--;
}
srcU0 += src0Stride;
srcU1 += src1Stride;
dstU += dststride;
}
// Chroma V
w0 = wp0[2].w;
offset = wp0[2].o + wp1[2].o;
shift = wp0[2].shift + shiftNum + 1;
round = shift ? (1 << (shift - 1)) : 0;
w1 = wp1[2].w;
for (y = cheight - 1; y >= 0; y--)
{
for (x = cwidth - 1; x >= 0;)
{
// note: chroma min width is 2
dstV[x] = weightBidir(w0, srcV0[x], w1, srcV1[x], round, shift, offset);
x--;
dstV[x] = weightBidir(w0, srcV0[x], w1, srcV1[x], round, shift, offset);
x--;
}
srcV0 += src0Stride;
srcV1 += src1Stride;
dstV += dststride;
}
}
}
/* weighted averaging for uni-pred */
void Predict::addWeightUni(const PredictionUnit& pu, Yuv& predYuv, const ShortYuv& srcYuv, const WeightValues wp[3], bool bLuma, bool bChroma) const
{
int w0, offset, shiftNum, shift, round;
uint32_t srcStride, dstStride;
if (bLuma)
{
pixel* dstY = predYuv.getLumaAddr(pu.puAbsPartIdx);
const int16_t* srcY0 = srcYuv.getLumaAddr(pu.puAbsPartIdx);
// Luma
w0 = wp[0].w;
offset = wp[0].offset;
shiftNum = IF_INTERNAL_PREC - X265_DEPTH;
shift = wp[0].shift + shiftNum;
round = shift ? (1 << (shift - 1)) : 0;
srcStride = srcYuv.m_size;
dstStride = predYuv.m_size;
primitives.weight_sp(srcY0, dstY, srcStride, dstStride, pu.width, pu.height, w0, round, shift, offset);
}
if (bChroma)
{
pixel* dstU = predYuv.getCbAddr(pu.puAbsPartIdx);
pixel* dstV = predYuv.getCrAddr(pu.puAbsPartIdx);
const int16_t* srcU0 = srcYuv.getCbAddr(pu.puAbsPartIdx);
const int16_t* srcV0 = srcYuv.getCrAddr(pu.puAbsPartIdx);
// Chroma U
w0 = wp[1].w;
offset = wp[1].offset;
shiftNum = IF_INTERNAL_PREC - X265_DEPTH;
shift = wp[1].shift + shiftNum;
round = shift ? (1 << (shift - 1)) : 0;
srcStride = srcYuv.m_csize;
dstStride = predYuv.m_csize;
uint32_t cwidth = pu.width >> srcYuv.m_hChromaShift;
uint32_t cheight = pu.height >> srcYuv.m_vChromaShift;
primitives.weight_sp(srcU0, dstU, srcStride, dstStride, cwidth, cheight, w0, round, shift, offset);
// Chroma V
w0 = wp[2].w;
offset = wp[2].offset;
shift = wp[2].shift + shiftNum;
round = shift ? (1 << (shift - 1)) : 0;
primitives.weight_sp(srcV0, dstV, srcStride, dstStride, cwidth, cheight, w0, round, shift, offset);
}
}
void Predict::predIntraLumaAng(uint32_t dirMode, pixel* dst, intptr_t stride, uint32_t log2TrSize)
{
int tuSize = 1 << log2TrSize;
int sizeIdx = log2TrSize - 2;
X265_CHECK(sizeIdx >= 0 && sizeIdx < 4, "intra block size is out of range\n");
int filter = !!(g_intraFilterFlags[dirMode] & tuSize);
bool bFilter = log2TrSize <= 4;
primitives.cu[sizeIdx].intra_pred[dirMode](dst, stride, intraNeighbourBuf[filter], dirMode, bFilter);
}
void Predict::predIntraChromaAng(uint32_t dirMode, pixel* dst, intptr_t stride, uint32_t log2TrSizeC)
{
int tuSize = 1 << log2TrSizeC;
int sizeIdx = log2TrSizeC - 2;
X265_CHECK(sizeIdx >= 0 && sizeIdx < 4, "intra block size is out of range\n");
int filter = !!(m_csp == X265_CSP_I444 && (g_intraFilterFlags[dirMode] & tuSize));
primitives.cu[sizeIdx].intra_pred[dirMode](dst, stride, intraNeighbourBuf[filter], dirMode, 0);
}
void Predict::initAdiPattern(const CUData& cu, const CUGeom& cuGeom, uint32_t puAbsPartIdx, const IntraNeighbors& intraNeighbors, int dirMode)
{
int tuSize = 1 << intraNeighbors.log2TrSize;
int tuSize2 = tuSize << 1;
PicYuv* reconPic = cu.m_encData->m_reconPic[0];
pixel* adiOrigin = reconPic->getLumaAddr(cu.m_cuAddr, cuGeom.absPartIdx + puAbsPartIdx);
intptr_t picStride = reconPic->m_stride;
fillReferenceSamples(adiOrigin, picStride, intraNeighbors, intraNeighbourBuf[0]);
pixel* refBuf = intraNeighbourBuf[0];
pixel* fltBuf = intraNeighbourBuf[1];
pixel topLeft = refBuf[0], topLast = refBuf[tuSize2], leftLast = refBuf[tuSize2 + tuSize2];
if (dirMode == ALL_IDX ? (8 | 16 | 32) & tuSize : g_intraFilterFlags[dirMode] & tuSize)
{
// generate filtered intra prediction samples
if (cu.m_slice->m_sps->bUseStrongIntraSmoothing && tuSize == 32)
{
const int threshold = 1 << (X265_DEPTH - 5);
pixel topMiddle = refBuf[32], leftMiddle = refBuf[tuSize2 + 32];
if (abs(topLeft + topLast - (topMiddle << 1)) < threshold &&
abs(topLeft + leftLast - (leftMiddle << 1)) < threshold)
{
// "strong" bilinear interpolation
const int shift = 5 + 1;
int init = (topLeft << shift) + tuSize;
int deltaL, deltaR;
deltaL = leftLast - topLeft; deltaR = topLast - topLeft;
fltBuf[0] = topLeft;
for (int i = 1; i < tuSize2; i++)
{
fltBuf[i + tuSize2] = (pixel)((init + deltaL * i) >> shift); // Left Filtering
fltBuf[i] = (pixel)((init + deltaR * i) >> shift); // Above Filtering
}
fltBuf[tuSize2] = topLast;
fltBuf[tuSize2 + tuSize2] = leftLast;
return;
}
}
primitives.cu[intraNeighbors.log2TrSize - 2].intra_filter(refBuf, fltBuf);
}
}
void Predict::initAdiPatternChroma(const CUData& cu, const CUGeom& cuGeom, uint32_t puAbsPartIdx, const IntraNeighbors& intraNeighbors, uint32_t chromaId)
{
PicYuv* reconPic = cu.m_encData->m_reconPic[0];
const pixel* adiOrigin = reconPic->getChromaAddr(chromaId, cu.m_cuAddr, cuGeom.absPartIdx + puAbsPartIdx);
intptr_t picStride = reconPic->m_strideC;
fillReferenceSamples(adiOrigin, picStride, intraNeighbors, intraNeighbourBuf[0]);
if (m_csp == X265_CSP_I444)
primitives.cu[intraNeighbors.log2TrSize - 2].intra_filter(intraNeighbourBuf[0], intraNeighbourBuf[1]);
}
void Predict::initIntraNeighbors(const CUData& cu, uint32_t absPartIdx, uint32_t tuDepth, bool isLuma, IntraNeighbors *intraNeighbors)
{
uint32_t log2TrSize = cu.m_log2CUSize[0] - tuDepth;
int log2UnitWidth = LOG2_UNIT_SIZE;
int log2UnitHeight = LOG2_UNIT_SIZE;
if (!isLuma)
{
log2TrSize -= cu.m_hChromaShift;
log2UnitWidth -= cu.m_hChromaShift;
log2UnitHeight -= cu.m_vChromaShift;
}
int numIntraNeighbor;
bool* bNeighborFlags = intraNeighbors->bNeighborFlags;
uint32_t tuSize = 1 << log2TrSize;
int tuWidthInUnits = tuSize >> log2UnitWidth;
int tuHeightInUnits = tuSize >> log2UnitHeight;
int aboveUnits = tuWidthInUnits << 1;
int leftUnits = tuHeightInUnits << 1;
uint32_t partIdxLT = cu.m_absIdxInCTU + absPartIdx;
uint32_t partIdxRT = g_rasterToZscan[g_zscanToRaster[partIdxLT] + tuWidthInUnits - 1];
uint32_t partIdxLB = g_rasterToZscan[g_zscanToRaster[partIdxLT] + ((tuHeightInUnits - 1) << LOG2_RASTER_SIZE)];
if (cu.m_slice->isIntra() || !cu.m_slice->m_pps->bConstrainedIntraPred)
{
bNeighborFlags[leftUnits] = isAboveLeftAvailable<false>(cu, partIdxLT);
numIntraNeighbor = (int)(bNeighborFlags[leftUnits]);
numIntraNeighbor += isAboveAvailable<false>(cu, partIdxLT, partIdxRT, bNeighborFlags + leftUnits + 1);
numIntraNeighbor += isAboveRightAvailable<false>(cu, partIdxRT, bNeighborFlags + leftUnits + 1 + tuWidthInUnits, tuWidthInUnits);
numIntraNeighbor += isLeftAvailable<false>(cu, partIdxLT, partIdxLB, bNeighborFlags + leftUnits - 1);
numIntraNeighbor += isBelowLeftAvailable<false>(cu, partIdxLB, bNeighborFlags + tuHeightInUnits - 1, tuHeightInUnits);
}
else
{
bNeighborFlags[leftUnits] = isAboveLeftAvailable<true>(cu, partIdxLT);
numIntraNeighbor = (int)(bNeighborFlags[leftUnits]);
numIntraNeighbor += isAboveAvailable<true>(cu, partIdxLT, partIdxRT, bNeighborFlags + leftUnits + 1);
numIntraNeighbor += isAboveRightAvailable<true>(cu, partIdxRT, bNeighborFlags + leftUnits + 1 + tuWidthInUnits, tuWidthInUnits);
numIntraNeighbor += isLeftAvailable<true>(cu, partIdxLT, partIdxLB, bNeighborFlags + leftUnits - 1);
numIntraNeighbor += isBelowLeftAvailable<true>(cu, partIdxLB, bNeighborFlags + tuHeightInUnits - 1, tuHeightInUnits);
}
intraNeighbors->numIntraNeighbor = numIntraNeighbor;
intraNeighbors->totalUnits = aboveUnits + leftUnits + 1;
intraNeighbors->aboveUnits = aboveUnits;
intraNeighbors->leftUnits = leftUnits;
intraNeighbors->unitWidth = 1 << log2UnitWidth;
intraNeighbors->unitHeight = 1 << log2UnitHeight;
intraNeighbors->log2TrSize = log2TrSize;
}
void Predict::fillReferenceSamples(const pixel* adiOrigin, intptr_t picStride, const IntraNeighbors& intraNeighbors, pixel dst[258])
{
const pixel dcValue = (pixel)(1 << (X265_DEPTH - 1));
int numIntraNeighbor = intraNeighbors.numIntraNeighbor;
int totalUnits = intraNeighbors.totalUnits;
uint32_t tuSize = 1 << intraNeighbors.log2TrSize;
uint32_t refSize = tuSize * 2 + 1;
// Nothing is available, perform DC prediction.
if (numIntraNeighbor == 0)
{
// Fill top border with DC value
for (uint32_t i = 0; i < refSize; i++)
dst[i] = dcValue;
// Fill left border with DC value
for (uint32_t i = 0; i < refSize - 1; i++)
dst[i + refSize] = dcValue;
}
else if (numIntraNeighbor == totalUnits)
{
// Fill top border with rec. samples
const pixel* adiTemp = adiOrigin - picStride - 1;
memcpy(dst, adiTemp, refSize * sizeof(pixel));
// Fill left border with rec. samples
adiTemp = adiOrigin - 1;
for (uint32_t i = 0; i < refSize - 1; i++)
{
dst[i + refSize] = adiTemp[0];
adiTemp += picStride;
}
}
else // reference samples are partially available
{
const bool *bNeighborFlags = intraNeighbors.bNeighborFlags;
const bool *pNeighborFlags;
int aboveUnits = intraNeighbors.aboveUnits;
int leftUnits = intraNeighbors.leftUnits;
int unitWidth = intraNeighbors.unitWidth;
int unitHeight = intraNeighbors.unitHeight;
int totalSamples = (leftUnits * unitHeight) + ((aboveUnits + 1) * unitWidth);
pixel adiLineBuffer[5 * MAX_CU_SIZE];
pixel *adi;
// Initialize
for (int i = 0; i < totalSamples; i++)
adiLineBuffer[i] = dcValue;
// Fill top-left sample
const pixel* adiTemp = adiOrigin - picStride - 1;
adi = adiLineBuffer + (leftUnits * unitHeight);
pNeighborFlags = bNeighborFlags + leftUnits;
if (*pNeighborFlags)
{
pixel topLeftVal = adiTemp[0];
for (int i = 0; i < unitWidth; i++)
adi[i] = topLeftVal;
}
// Fill left & below-left samples
adiTemp += picStride;
adi--;
// NOTE: over copy here, but reduce condition operators
for (int j = 0; j < leftUnits * unitHeight; j++)
{
adi[-j] = adiTemp[j * picStride];
}
// Fill above & above-right samples
adiTemp = adiOrigin - picStride;
adi = adiLineBuffer + (leftUnits * unitHeight) + unitWidth;
// NOTE: over copy here, but reduce condition operators
memcpy(adi, adiTemp, aboveUnits * unitWidth * sizeof(*adiTemp));
// Pad reference samples when necessary
int curr = 0;
int next = 1;
adi = adiLineBuffer;
int pAdiLineTopRowOffset = leftUnits * (unitHeight - unitWidth);
if (!bNeighborFlags[0])
{
// very bottom unit of bottom-left; at least one unit will be valid.
while (next < totalUnits && !bNeighborFlags[next])
next++;
pixel* pAdiLineNext = adiLineBuffer + ((next < leftUnits) ? (next * unitHeight) : (pAdiLineTopRowOffset + (next * unitWidth)));
const pixel refSample = *pAdiLineNext;
// Pad unavailable samples with new value
int nextOrTop = X265_MIN(next, leftUnits);
// fill left column
#if HIGH_BIT_DEPTH
while (curr < nextOrTop)
{
for (int i = 0; i < unitHeight; i++)
adi[i] = refSample;
adi += unitHeight;
curr++;
}
// fill top row
while (curr < next)
{
for (int i = 0; i < unitWidth; i++)
adi[i] = refSample;
adi += unitWidth;
curr++;
}
#else
X265_CHECK(curr <= nextOrTop, "curr must be less than or equal to nextOrTop\n");
if (curr < nextOrTop)
{
const int fillSize = unitHeight * (nextOrTop - curr);
memset(adi, refSample, fillSize * sizeof(pixel));
curr = nextOrTop;
adi += fillSize;
}
if (curr < next)
{
const int fillSize = unitWidth * (next - curr);
memset(adi, refSample, fillSize * sizeof(pixel));
curr = next;
adi += fillSize;
}
#endif
}
// pad all other reference samples.
while (curr < totalUnits)
{
if (!bNeighborFlags[curr]) // samples not available
{
int numSamplesInCurrUnit = (curr >= leftUnits) ? unitWidth : unitHeight;
const pixel refSample = *(adi - 1);
for (int i = 0; i < numSamplesInCurrUnit; i++)
adi[i] = refSample;
adi += numSamplesInCurrUnit;
curr++;
}
else
{
adi += (curr >= leftUnits) ? unitWidth : unitHeight;
curr++;
}
}
// Copy processed samples
adi = adiLineBuffer + refSize + unitWidth - 2;
memcpy(dst, adi, refSize * sizeof(pixel));
adi = adiLineBuffer + refSize - 1;
for (int i = 0; i < (int)refSize - 1; i++)
dst[i + refSize] = adi[-(i + 1)];
}
}
template<bool cip>
bool Predict::isAboveLeftAvailable(const CUData& cu, uint32_t partIdxLT)
{
uint32_t partAboveLeft;
const CUData* cuAboveLeft = cu.getPUAboveLeft(partAboveLeft, partIdxLT);
return cuAboveLeft && (!cip || cuAboveLeft->isIntra(partAboveLeft));
}
template<bool cip>
int Predict::isAboveAvailable(const CUData& cu, uint32_t partIdxLT, uint32_t partIdxRT, bool* bValidFlags)
{
const uint32_t rasterPartBegin = g_zscanToRaster[partIdxLT];
const uint32_t rasterPartEnd = g_zscanToRaster[partIdxRT];
const uint32_t idxStep = 1;
int numIntra = 0;
for (uint32_t rasterPart = rasterPartBegin; rasterPart <= rasterPartEnd; rasterPart += idxStep, bValidFlags++)
{
uint32_t partAbove;
const CUData* cuAbove = cu.getPUAbove(partAbove, g_rasterToZscan[rasterPart]);
if (cuAbove && (!cip || cuAbove->isIntra(partAbove)))
{
numIntra++;
*bValidFlags = true;
}
else
*bValidFlags = false;
}
return numIntra;
}
template<bool cip>
int Predict::isLeftAvailable(const CUData& cu, uint32_t partIdxLT, uint32_t partIdxLB, bool* bValidFlags)
{
const uint32_t rasterPartBegin = g_zscanToRaster[partIdxLT];
const uint32_t rasterPartEnd = g_zscanToRaster[partIdxLB];
const uint32_t idxStep = RASTER_SIZE;
int numIntra = 0;
for (uint32_t rasterPart = rasterPartBegin; rasterPart <= rasterPartEnd; rasterPart += idxStep, bValidFlags--) // opposite direction
{
uint32_t partLeft;
const CUData* cuLeft = cu.getPULeft(partLeft, g_rasterToZscan[rasterPart]);
if (cuLeft && (!cip || cuLeft->isIntra(partLeft)))
{
numIntra++;
*bValidFlags = true;
}
else
*bValidFlags = false;
}
return numIntra;
}
template<bool cip>
int Predict::isAboveRightAvailable(const CUData& cu, uint32_t partIdxRT, bool* bValidFlags, uint32_t numUnits)
{
int numIntra = 0;
for (uint32_t offset = 1; offset <= numUnits; offset++, bValidFlags++)
{
uint32_t partAboveRight;
const CUData* cuAboveRight = cu.getPUAboveRightAdi(partAboveRight, partIdxRT, offset);
if (cuAboveRight && (!cip || cuAboveRight->isIntra(partAboveRight)))
{
numIntra++;
*bValidFlags = true;
}
else
*bValidFlags = false;
}
return numIntra;
}
template<bool cip>
int Predict::isBelowLeftAvailable(const CUData& cu, uint32_t partIdxLB, bool* bValidFlags, uint32_t numUnits)
{
int numIntra = 0;
for (uint32_t offset = 1; offset <= numUnits; offset++, bValidFlags--) // opposite direction
{
uint32_t partBelowLeft;
const CUData* cuBelowLeft = cu.getPUBelowLeftAdi(partBelowLeft, partIdxLB, offset);
if (cuBelowLeft && (!cip || cuBelowLeft->isIntra(partBelowLeft)))
{
numIntra++;
*bValidFlags = true;
}
else
*bValidFlags = false;
}
return numIntra;
}
|