akcolorconvert.cpp 36.7 KB
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 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
/* Webcamoid, webcam capture application.
 * Copyright (C) 2023  Gonzalo Exequiel Pedone
 *
 * Webcamoid 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 3 of the License, or
 * (at your option) any later version.
 *
 * Webcamoid 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 Webcamoid. If not, see <http://www.gnu.org/licenses/>.
 *
 * Web-Site: http://webcamoid.github.io/
 */

#include <QtDebug>
#include <QQmlEngine>
#include <limits>

#include "akcolorconvert.h"
#include "akvideoformatspec.h"

class AkColorConvertPrivate
{
    public:
        AkColorConvert *self {nullptr};
        AkColorConvert::YuvColorSpace m_yuvColorSpace {AkColorConvert::YuvColorSpace_ITUR_BT601};
        AkColorConvert::YuvColorSpaceType m_yuvColorSpaceType {AkColorConvert::YuvColorSpaceType_StudioSwing};

        explicit AkColorConvertPrivate(AkColorConvert *self);
        void rbConstants(AkColorConvert::YuvColorSpace colorSpace,
                         qint64 &kr,
                         qint64 &kb,
                         qint64 &div) const;
        qint64 roundedDiv(qint64 num, qint64 den) const;
        qint64 nearestPowOf2(qint64 value) const;
        void limitsY(int bits,
                     AkColorConvert::YuvColorSpaceType type,
                     qint64 &minY,
                     qint64 &maxY) const;
        void limitsUV(int bits,
                      AkColorConvert::YuvColorSpaceType type,
                      qint64 &minUV,
                      qint64 &maxUV) const;
        void loadAbc2xyzMatrix(int abits,
                               int bbits,
                               int cbits,
                               int xbits,
                               int ybits,
                               int zbits);
        void loadRgb2yuvMatrix(AkColorConvert::YuvColorSpace YuvColorSpace,
                               AkColorConvert::YuvColorSpaceType yuvColorSpaceType,
                               int rbits,
                               int gbits,
                               int bbits,
                               int ybits,
                               int ubits,
                               int vbits);
        void loadYuv2rgbMatrix(AkColorConvert::YuvColorSpace YuvColorSpace,
                               AkColorConvert::YuvColorSpaceType yuvColorSpaceType,
                               int ybits,
                               int ubits,
                               int vbits,
                               int rbits,
                               int gbits,
                               int bbits);
        void loadRgb2grayMatrix(AkColorConvert::YuvColorSpace YuvColorSpace,
                                int rbits,
                                int gbits,
                                int bbits,
                                int graybits);
        void loadGray2rgbMatrix(int graybits,
                                int rbits,
                                int gbits,
                                int bbits);
        void loadYuv2grayMatrix(AkColorConvert::YuvColorSpaceType yuvColorSpaceType,
                                int ybits,
                                int ubits,
                                int vbits,
                                int graybits);
        void loadGray2yuvMatrix(AkColorConvert::YuvColorSpaceType yuvColorSpaceType,
                                int graybits,
                                int ybits,
                                int ubits,
                                int vbits);
        void loadAlphaRgbMatrix(int alphaBits);
        void loadAlphaYuvMatrix(AkColorConvert::YuvColorSpaceType yuvColorSpaceType,
                                int alphaBits,
                                int ybits,
                                int ubits,
                                int vbits);
        void loadAlphaGrayMatrix(int alphaBits, int graybits);
};

AkColorConvert::AkColorConvert(QObject *parent):
    QObject(parent)
{
    this->d = new AkColorConvertPrivate(this);
}

AkColorConvert::AkColorConvert(YuvColorSpace yuvColorSpace,
                               YuvColorSpaceType yuvColorSpaceType,
                               QObject *parent):
    QObject(parent)
{
    this->d = new AkColorConvertPrivate(this);
    this->d->m_yuvColorSpace = yuvColorSpace;
    this->d->m_yuvColorSpaceType = yuvColorSpaceType;
}

AkColorConvert::AkColorConvert(YuvColorSpaceType yuvColorSpaceType,
                               QObject *parent):
    QObject(parent)
{
    this->d = new AkColorConvertPrivate(this);
    this->d->m_yuvColorSpaceType = yuvColorSpaceType;
}

AkColorConvert::AkColorConvert(const AkColorConvert &other):
    QObject()
{
    this->d = new AkColorConvertPrivate(this);
    this->d->m_yuvColorSpace = other.d->m_yuvColorSpace;
    this->d->m_yuvColorSpaceType = other.d->m_yuvColorSpaceType;
    this->m00 = other.m00; this->m01 = other.m01; this->m02 = other.m02; this->m03 = other.m03;
    this->m10 = other.m10; this->m11 = other.m11; this->m12 = other.m12; this->m13 = other.m13;
    this->m20 = other.m20; this->m21 = other.m21; this->m22 = other.m22; this->m23 = other.m23;
    this->a00 = other.a00; this->a01 = other.a01; this->a02 = other.a02;
    this->a10 = other.a10; this->a11 = other.a11; this->a12 = other.a12;
    this->a20 = other.a20; this->a21 = other.a21; this->a22 = other.a22;
    this->xmin = other.xmin; this->xmax = other.xmax;
    this->ymin = other.ymin; this->ymax = other.ymax;
    this->zmin = other.zmin; this->zmax = other.zmax;
    this->colorShift = other.colorShift;
    this->alphaShift = other.alphaShift;
}

AkColorConvert::~AkColorConvert()
{
    delete this->d;
}

AkColorConvert &AkColorConvert::operator =(const AkColorConvert &other)
{
    if (this != &other) {
        this->d->m_yuvColorSpace = other.d->m_yuvColorSpace;
        this->d->m_yuvColorSpaceType = other.d->m_yuvColorSpaceType;
        this->m00 = other.m00; this->m01 = other.m01; this->m02 = other.m02; this->m03 = other.m03;
        this->m10 = other.m10; this->m11 = other.m11; this->m12 = other.m12; this->m13 = other.m13;
        this->m20 = other.m20; this->m21 = other.m21; this->m22 = other.m22; this->m23 = other.m23;
        this->a00 = other.a00; this->a01 = other.a01; this->a02 = other.a02;
        this->a10 = other.a10; this->a11 = other.a11; this->a12 = other.a12;
        this->a20 = other.a20; this->a21 = other.a21; this->a22 = other.a22;
        this->xmin = other.xmin; this->xmax = other.xmax;
        this->ymin = other.ymin; this->ymax = other.ymax;
        this->zmin = other.zmin; this->zmax = other.zmax;
        this->colorShift = other.colorShift;
        this->alphaShift = other.alphaShift;
    }

    return *this;
}

AkColorConvert::YuvColorSpace AkColorConvert::yuvColorSpace() const
{
    return this->d->m_yuvColorSpace;
}

AkColorConvert::YuvColorSpaceType AkColorConvert::yuvColorSpaceType() const
{
    return this->d->m_yuvColorSpaceType;
}

void AkColorConvert::setYuvColorSpace(YuvColorSpace yuvColorSpace)
{
    if (this->d->m_yuvColorSpace == yuvColorSpace)
        return;

    this->d->m_yuvColorSpace = yuvColorSpace;
    emit this->yuvColorSpaceChanged(yuvColorSpace);
}

void AkColorConvert::setYuvColorSpaceType(YuvColorSpaceType yuvColorSpaceType)
{
    if (this->d->m_yuvColorSpaceType == yuvColorSpaceType)
        return;

    this->d->m_yuvColorSpaceType = yuvColorSpaceType;
    emit this->yuvColorSpaceTypeChanged(yuvColorSpaceType);
}

void AkColorConvert::resetYuvColorSpace()
{
    this->setYuvColorSpace(AkColorConvert::YuvColorSpace_ITUR_BT601);
}

void AkColorConvert::resetYuvColorSpaceType()
{
    this->setYuvColorSpaceType(AkColorConvert::YuvColorSpaceType_StudioSwing);
}

void AkColorConvert::loadColorMatrix(ColorMatrix colorMatrix,
                                     int ibitsa,
                                     int ibitsb,
                                     int ibitsc,
                                     int obitsx,
                                     int obitsy,
                                     int obitsz)
{
    switch (colorMatrix) {
    case ColorMatrix_ABC2XYZ:
        this->d->loadAbc2xyzMatrix(ibitsa,
                                   ibitsb,
                                   ibitsc,
                                   obitsx,
                                   obitsy,
                                   obitsz);

        break;

    case ColorMatrix_RGB2YUV:
        this->d->loadRgb2yuvMatrix(this->d->m_yuvColorSpace,
                                   this->d->m_yuvColorSpaceType,
                                   ibitsa,
                                   ibitsb,
                                   ibitsc,
                                   obitsx,
                                   obitsy,
                                   obitsz);

        break;

    case ColorMatrix_YUV2RGB:
        this->d->loadYuv2rgbMatrix(this->d->m_yuvColorSpace,
                                   this->d->m_yuvColorSpaceType,
                                   ibitsa,
                                   ibitsb,
                                   ibitsc,
                                   obitsx,
                                   obitsy,
                                   obitsz);

        break;

    case ColorMatrix_RGB2GRAY:
        this->d->loadRgb2grayMatrix(this->d->m_yuvColorSpace,
                                    ibitsa,
                                    ibitsb,
                                    ibitsc,
                                    obitsx);

        break;

    case ColorMatrix_GRAY2RGB:
        this->d->loadGray2rgbMatrix(ibitsa,
                                    obitsx,
                                    obitsy,
                                    obitsz);

        break;

    case ColorMatrix_YUV2GRAY:
        this->d->loadYuv2grayMatrix(this->d->m_yuvColorSpaceType,
                                    ibitsa,
                                    ibitsb,
                                    ibitsc,
                                    obitsx);

        break;

    case ColorMatrix_GRAY2YUV:
        this->d->loadGray2yuvMatrix(this->d->m_yuvColorSpaceType,
                                    ibitsa,
                                    obitsx,
                                    obitsy,
                                    obitsz);

    default:
        break;
    }
}

void AkColorConvert::loadAlphaMatrix(AkVideoFormatSpec::VideoFormatType formatType,
                                     int ibitsAlpha,
                                     int obitsx,
                                     int obitsy,
                                     int obitsz)
{
    switch (formatType) {
    case AkVideoFormatSpec::VFT_RGB:
        this->d->loadAlphaRgbMatrix(ibitsAlpha);

        break;

    case AkVideoFormatSpec::VFT_YUV:
        this->d->loadAlphaYuvMatrix(this->d->m_yuvColorSpaceType,
                                    ibitsAlpha,
                                    obitsx,
                                    obitsy,
                                    obitsz);

        break;

    case AkVideoFormatSpec::VFT_Gray:
        this->d->loadAlphaGrayMatrix(ibitsAlpha, obitsx);

        break;

    default:
        break;
    }
}

void AkColorConvert::loadMatrix(const AkVideoFormatSpec &from,
                                const AkVideoFormatSpec &to)
{
    ColorMatrix colorMatrix = ColorMatrix_ABC2XYZ;
    int ibitsa = 0;
    int ibitsb = 0;
    int ibitsc = 0;
    int obitsx = 0;
    int obitsy = 0;
    int obitsz = 0;

    if (from.type() == AkVideoFormatSpec::VFT_RGB
        && to.type() == AkVideoFormatSpec::VFT_RGB) {
        colorMatrix = ColorMatrix_ABC2XYZ;
        ibitsa = from.component(AkColorComponent::CT_R).depth();
        ibitsb = from.component(AkColorComponent::CT_G).depth();
        ibitsc = from.component(AkColorComponent::CT_B).depth();
        obitsx = to.component(AkColorComponent::CT_R).depth();
        obitsy = to.component(AkColorComponent::CT_G).depth();
        obitsz = to.component(AkColorComponent::CT_B).depth();
    } else if (from.type() == AkVideoFormatSpec::VFT_RGB
               && to.type() == AkVideoFormatSpec::VFT_YUV) {
        colorMatrix = ColorMatrix_RGB2YUV;
        ibitsa = from.component(AkColorComponent::CT_R).depth();
        ibitsb = from.component(AkColorComponent::CT_G).depth();
        ibitsc = from.component(AkColorComponent::CT_B).depth();
        obitsx = to.component(AkColorComponent::CT_Y).depth();
        obitsy = to.component(AkColorComponent::CT_U).depth();
        obitsz = to.component(AkColorComponent::CT_V).depth();
    } else if (from.type() == AkVideoFormatSpec::VFT_RGB
               && to.type() == AkVideoFormatSpec::VFT_Gray) {
        colorMatrix = ColorMatrix_RGB2GRAY;
        ibitsa = from.component(AkColorComponent::CT_R).depth();
        ibitsb = from.component(AkColorComponent::CT_G).depth();
        ibitsc = from.component(AkColorComponent::CT_B).depth();
        obitsx = to.component(AkColorComponent::CT_Y).depth();
        obitsy = obitsx;
        obitsz = obitsx;
    } else if (from.type() == AkVideoFormatSpec::VFT_YUV
               && to.type() == AkVideoFormatSpec::VFT_RGB) {
        colorMatrix = ColorMatrix_YUV2RGB;
        ibitsa = from.component(AkColorComponent::CT_Y).depth();
        ibitsb = from.component(AkColorComponent::CT_U).depth();
        ibitsc = from.component(AkColorComponent::CT_V).depth();
        obitsx = to.component(AkColorComponent::CT_R).depth();
        obitsy = to.component(AkColorComponent::CT_G).depth();
        obitsz = to.component(AkColorComponent::CT_B).depth();
    } else if (from.type() == AkVideoFormatSpec::VFT_YUV
               && to.type() == AkVideoFormatSpec::VFT_YUV) {
        colorMatrix = ColorMatrix_ABC2XYZ;
        ibitsa = from.component(AkColorComponent::CT_Y).depth();
        ibitsb = from.component(AkColorComponent::CT_U).depth();
        ibitsc = from.component(AkColorComponent::CT_V).depth();
        obitsx = to.component(AkColorComponent::CT_Y).depth();
        obitsy = to.component(AkColorComponent::CT_U).depth();
        obitsz = to.component(AkColorComponent::CT_V).depth();
    } else if (from.type() == AkVideoFormatSpec::VFT_YUV
               && to.type() == AkVideoFormatSpec::VFT_Gray) {
        colorMatrix = ColorMatrix_YUV2GRAY;
        ibitsa = from.component(AkColorComponent::CT_Y).depth();
        ibitsb = from.component(AkColorComponent::CT_U).depth();
        ibitsc = from.component(AkColorComponent::CT_V).depth();
        obitsx = to.component(AkColorComponent::CT_Y).depth();
        obitsy = obitsx;
        obitsz = obitsx;
    } else if (from.type() == AkVideoFormatSpec::VFT_Gray
               && to.type() == AkVideoFormatSpec::VFT_RGB) {
        colorMatrix = ColorMatrix_GRAY2RGB;
        ibitsa = from.component(AkColorComponent::CT_Y).depth();
        ibitsb = ibitsa;
        ibitsc = ibitsa;
        obitsx = to.component(AkColorComponent::CT_R).depth();
        obitsy = to.component(AkColorComponent::CT_G).depth();
        obitsz = to.component(AkColorComponent::CT_B).depth();
    } else if (from.type() == AkVideoFormatSpec::VFT_Gray
               && to.type() == AkVideoFormatSpec::VFT_YUV) {
        colorMatrix = ColorMatrix_GRAY2YUV;
        ibitsa = from.component(AkColorComponent::CT_Y).depth();
        ibitsb = ibitsa;
        ibitsc = ibitsa;
        obitsx = to.component(AkColorComponent::CT_Y).depth();
        obitsy = to.component(AkColorComponent::CT_U).depth();
        obitsz = to.component(AkColorComponent::CT_V).depth();
    } else if (from.type() == AkVideoFormatSpec::VFT_Gray
               && to.type() == AkVideoFormatSpec::VFT_Gray) {
        colorMatrix = ColorMatrix_ABC2XYZ;
        ibitsa = from.component(AkColorComponent::CT_Y).depth();
        ibitsb = ibitsa;
        ibitsc = ibitsa;
        obitsx = to.component(AkColorComponent::CT_Y).depth();
        obitsy = obitsx;
        obitsz = obitsx;
    }

    this->loadColorMatrix(colorMatrix,
                          ibitsa,
                          ibitsb,
                          ibitsc,
                          obitsx,
                          obitsy,
                          obitsz);

    if (from.contains(AkColorComponent::CT_A))
        this->loadAlphaMatrix(to.type(),
                              from.component(AkColorComponent::CT_A).depth(),
                              obitsx,
                              obitsy,
                              obitsz);
}

void AkColorConvert::loadMatrix(const AkVideoCaps::PixelFormat &from,
                                const AkVideoCaps::PixelFormat &to)
{
    this->loadMatrix(AkVideoCaps::formatSpecs(from),
                     AkVideoCaps::formatSpecs(to));
}

void AkColorConvert::registerTypes()
{
    qRegisterMetaType<AkColorConvert>("AkColorConvert");

    qRegisterMetaType<YuvColorSpace>("YuvColorSpace");
    qRegisterMetaType<YuvColorSpaceType>("YuvColorSpaceType");
    qRegisterMetaType<ColorMatrix>("ColorMatrix");
    qmlRegisterSingletonType<AkColorConvert>("Ak", 1, 0, "AkColorConvert",
                                             [] (QQmlEngine *qmlEngine,
                                                QJSEngine *jsEngine) -> QObject * {
                                                 Q_UNUSED(qmlEngine)
                                                 Q_UNUSED(jsEngine)

                                                 return new AkColorConvert();
                                             });
}

QDebug operator <<(QDebug debug, AkColorConvert::YuvColorSpace yuvColorSpace)
{
    AkColorConvert convert;
    int yuvColorSpaceIndex =
        convert.metaObject()->indexOfEnumerator("YuvColorSpace");
    auto colorSpaceEnum = convert.metaObject()->enumerator(yuvColorSpaceIndex);
    QString str(colorSpaceEnum.valueToKey(yuvColorSpace));
    str.remove("YuvColorSpace_");
    debug.nospace() << str.toStdString().c_str();

    return debug.space();
}

QDebug operator <<(QDebug debug, AkColorConvert::YuvColorSpaceType yuvColorSpaceType)
{
    AkColorConvert convert;
    int yuvColorSpaceTypeIndex =
        convert.metaObject()->indexOfEnumerator("YuvColorSpaceType");
    auto colorSpaceTypeEnum = convert.metaObject()->enumerator(yuvColorSpaceTypeIndex);
    QString str(colorSpaceTypeEnum.valueToKey(yuvColorSpaceType));
    str.remove("YuvColorSpaceType_");
    debug.nospace() << str.toStdString().c_str();

    return debug.space();
}

QDebug operator <<(QDebug debug, AkColorConvert::ColorMatrix colorMatrix)
{
    AkColorConvert convert;
    int colorMatrixIndex =
        convert.metaObject()->indexOfEnumerator("ColorMatrix");
    auto colorMatrixEnum = convert.metaObject()->enumerator(colorMatrixIndex);
    QString str(colorMatrixEnum.valueToKey(colorMatrix));
    str.remove("ColorMatrix_");
    debug.nospace() << str.toStdString().c_str();

    return debug.space();
}

AkColorConvertPrivate::AkColorConvertPrivate(AkColorConvert *self):
    self(self)
{

}

void AkColorConvertPrivate::rbConstants(AkColorConvert::YuvColorSpace colorSpace,
                                        qint64 &kr,
                                        qint64 &kb,
                                        qint64 &div) const
{
    kr = 0;
    kb = 0;
    div = 10000;

    // Coefficients taken from https://en.wikipedia.org/wiki/YUV
    switch (colorSpace) {
    // Same weight for all components
    case AkColorConvert::YuvColorSpace_AVG:
        kr = 3333;
        kb = 3333;

        break;

        // https://www.itu.int/rec/R-REC-BT.601/en
    case AkColorConvert::YuvColorSpace_ITUR_BT601:
        kr = 2990;
        kb = 1140;

        break;

        // https://www.itu.int/rec/R-REC-BT.709/en
    case AkColorConvert::YuvColorSpace_ITUR_BT709:
        kr = 2126;
        kb = 722;

        break;

        // https://www.itu.int/rec/R-REC-BT.2020/en
    case AkColorConvert::YuvColorSpace_ITUR_BT2020:
        kr = 2627;
        kb = 593;

        break;

        // http://car.france3.mars.free.fr/HD/INA-%2026%20jan%2006/SMPTE%20normes%20et%20confs/s240m.pdf
    case AkColorConvert::YuvColorSpace_SMPTE_240M:
        kr = 2120;
        kb = 870;

        break;

    default:
        break;
    }
}

qint64 AkColorConvertPrivate::roundedDiv(qint64 num, qint64 den) const
{
    if (den == 0)
        return num < 0?
            std::numeric_limits<qint64>::min():
            std::numeric_limits<qint64>::max();

    if (((num < 0) && (den > 0)) || ((num > 0) && (den < 0)))
        return (2 * num - den) / (2 * den);

    return (2 * num + den) / (2 * den);
}

qint64 AkColorConvertPrivate::nearestPowOf2(qint64 value) const
{
    qint64 val = value;
    qint64 res = 0;

    while (val >>= 1)
        res++;

    if (qAbs((1 << (res + 1)) - value) <= qAbs((1 << res) - value))
        return 1 << (res + 1);

    return 1 << res;
}

void AkColorConvertPrivate::limitsY(int bits,
                                    AkColorConvert::YuvColorSpaceType type,
                                    qint64 &minY,
                                    qint64 &maxY) const
{
    if (type == AkColorConvert::YuvColorSpaceType_FullSwing) {
        minY = 0;
        maxY = (1 << bits) - 1;

        return;
    }

    /* g = 9% is the theoretical maximal overshoot (Gibbs phenomenon)
     *
     * https://en.wikipedia.org/wiki/YUV#Numerical_approximations
     * https://en.wikipedia.org/wiki/Gibbs_phenomenon
     * https://math.stackexchange.com/a/259089
     * https://www.youtube.com/watch?v=Ol0uTeXoKaU
     */
    static const qint64 g = 9;

    qint64 maxValue = (1 << bits) - 1;
    minY = this->nearestPowOf2(this->roundedDiv(maxValue * g, 2 * g + 100));
    maxY = maxValue * (g + 100) / (2 * g + 100);
}

void AkColorConvertPrivate::limitsUV(int bits,
                                     AkColorConvert::YuvColorSpaceType type,
                                     qint64 &minUV,
                                     qint64 &maxUV) const
{
    if (type == AkColorConvert::YuvColorSpaceType_FullSwing) {
        minUV = 0;
        maxUV = (1 << bits) - 1;

        return;
    }

    static const qint64 g = 9;
    qint64 maxValue = (1 << bits) - 1;
    minUV = this->nearestPowOf2(this->roundedDiv(maxValue * g, 2 * g + 100));
    maxUV = (1L << bits) - minUV;
}

void AkColorConvertPrivate::loadAbc2xyzMatrix(int abits,
                                              int bbits,
                                              int cbits,
                                              int xbits,
                                              int ybits,
                                              int zbits)
{
    int shift = qMax(abits, qMax(bbits, cbits));
    qint64 shiftDiv = 1L << shift;
    qint64 rounding = 1L << qAbs(shift - 1);

    qint64 amax = (1L << abits) - 1;
    qint64 bmax = (1L << bbits) - 1;
    qint64 cmax = (1L << cbits) - 1;

    qint64 xmax = (1L << xbits) - 1;
    qint64 ymax = (1L << ybits) - 1;
    qint64 zmax = (1L << zbits) - 1;

    qint64 kx = this->roundedDiv(shiftDiv * xmax, amax);
    qint64 ky = this->roundedDiv(shiftDiv * ymax, bmax);
    qint64 kz = this->roundedDiv(shiftDiv * zmax, cmax);

    self->m00 = kx; self->m01 = 0 ; self->m02 = 0 ; self->m03 = rounding;
    self->m10 = 0 ; self->m11 = ky; self->m12 = 0 ; self->m13 = rounding;
    self->m20 = 0 ; self->m21 = 0 ; self->m22 = kz; self->m23 = rounding;

    self->xmin = 0; self->xmax = xmax;
    self->ymin = 0; self->ymax = ymax;
    self->zmin = 0; self->zmax = zmax;

    self->colorShift = shift;
}

void AkColorConvertPrivate::loadRgb2yuvMatrix(AkColorConvert::YuvColorSpace yuvColorSpace,
                                              AkColorConvert::YuvColorSpaceType yuvColorSpaceType,
                                              int rbits,
                                              int gbits,
                                              int bbits,
                                              int ybits,
                                              int ubits,
                                              int vbits)
{
    qint64 kyr = 0;
    qint64 kyb = 0;
    qint64 div = 0;
    this->rbConstants(yuvColorSpace, kyr, kyb, div);
    qint64 kyg = div - kyr - kyb;

    qint64 kur = -kyr;
    qint64 kug = -kyg;
    qint64 kub = div - kyb;

    qint64 kvr = div - kyr;
    qint64 kvg = -kyg;
    qint64 kvb = -kyb;

    int shift = qMax(rbits, qMax(gbits, bbits));
    qint64 shiftDiv = 1L << shift;
    qint64 rounding = 1L << (shift - 1);

    qint64 rmax = (1L << rbits) - 1;
    qint64 gmax = (1L << gbits) - 1;
    qint64 bmax = (1L << bbits) - 1;

    qint64 minY = 0;
    qint64 maxY = 0;
    this->limitsY(ybits, yuvColorSpaceType, minY, maxY);
    auto diffY = maxY - minY;

    qint64 kiyr = this->roundedDiv(shiftDiv * diffY * kyr, div * rmax);
    qint64 kiyg = this->roundedDiv(shiftDiv * diffY * kyg, div * gmax);
    qint64 kiyb = this->roundedDiv(shiftDiv * diffY * kyb, div * bmax);

    qint64 minU = 0;
    qint64 maxU = 0;
    this->limitsUV(ubits, yuvColorSpaceType, minU, maxU);
    auto diffU = maxU - minU;

    qint64 kiur = this->roundedDiv(shiftDiv * diffU * kur, 2 * rmax * kub);
    qint64 kiug = this->roundedDiv(shiftDiv * diffU * kug, 2 * gmax * kub);
    qint64 kiub = this->roundedDiv(shiftDiv * diffU      , 2 * bmax);

    qint64 minV = 0;
    qint64 maxV = 0;
    this->limitsUV(vbits, yuvColorSpaceType, minV, maxV);
    auto diffV = maxV - minV;

    qint64 kivr = this->roundedDiv(shiftDiv * diffV      , 2 * rmax);
    qint64 kivg = this->roundedDiv(shiftDiv * diffV * kvg, 2 * gmax * kvr);
    qint64 kivb = this->roundedDiv(shiftDiv * diffV * kvb, 2 * bmax * kvr);

    qint64 ciy = rounding + shiftDiv * minY;
    qint64 ciu = rounding + shiftDiv * (minU + maxU) / 2;
    qint64 civ = rounding + shiftDiv * (minV + maxV) / 2;

    self->m00 = kiyr; self->m01 = kiyg; self->m02 = kiyb; self->m03 = ciy;
    self->m10 = kiur; self->m11 = kiug; self->m12 = kiub; self->m13 = ciu;
    self->m20 = kivr; self->m21 = kivg; self->m22 = kivb; self->m23 = civ;

    self->xmin = minY; self->xmax = maxY;
    self->ymin = minU; self->ymax = maxU;
    self->zmin = minV; self->zmax = maxV;

    self->colorShift = shift;
}

void AkColorConvertPrivate::loadYuv2rgbMatrix(AkColorConvert::YuvColorSpace yuvColorSpace,
                                              AkColorConvert::YuvColorSpaceType yuvColorSpaceType,
                                              int ybits,
                                              int ubits,
                                              int vbits,
                                              int rbits,
                                              int gbits,
                                              int bbits)
{
    qint64 kyr = 0;
    qint64 kyb = 0;
    qint64 div = 0;
    this->rbConstants(yuvColorSpace, kyr, kyb, div);
    qint64 kyg = div - kyr - kyb;

    qint64 minY = 0;
    qint64 maxY = 0;
    this->limitsY(ybits, yuvColorSpaceType, minY, maxY);
    auto diffY = maxY - minY;

    qint64 minU = 0;
    qint64 maxU = 0;
    this->limitsUV(ubits, yuvColorSpaceType, minU, maxU);
    auto diffU = maxU - minU;

    qint64 minV = 0;
    qint64 maxV = 0;
    this->limitsUV(vbits, yuvColorSpaceType, minV, maxV);
    auto diffV = maxV - minV;

    int shift = qMax(ybits, qMax(ubits, vbits));
    qint64 shiftDiv = 1L << shift;
    qint64 rounding = 1L << (shift - 1);

    qint64 rmax = (1L << rbits) - 1;
    qint64 gmax = (1L << gbits) - 1;
    qint64 bmax = (1L << bbits) - 1;

    qint64 kry = this->roundedDiv(shiftDiv * rmax, diffY);
    qint64 krv = this->roundedDiv(2 * shiftDiv * rmax * (div - kyr), div * diffV);

    qint64 kgy = this->roundedDiv(shiftDiv * gmax, diffY);
    qint64 kgu = this->roundedDiv(2 * shiftDiv * gmax * kyb * (kyb - div), div * kyg * diffU);
    qint64 kgv = this->roundedDiv(2 * shiftDiv * gmax * kyr * (kyr - div), div * kyg * diffV);

    qint64 kby = this->roundedDiv(shiftDiv * bmax, diffY);
    qint64 kbu = this->roundedDiv(2 * shiftDiv * bmax * (div - kyb), div * diffU);

    qint64 cir = rounding - kry * minY - krv * (minV + maxV) / 2;
    qint64 cig = rounding - kgy * minY - (kgu * (minU + maxU) + kgv * (minV + maxV)) / 2;
    qint64 cib = rounding - kby * minY - kbu * (minU + maxU) / 2;

    self->m00 = kry; self->m01 = 0  ; self->m02 = krv; self->m03 = cir;
    self->m10 = kgy; self->m11 = kgu; self->m12 = kgv; self->m13 = cig;
    self->m20 = kby; self->m21 = kbu; self->m22 = 0  ; self->m23 = cib;

    self->xmin = 0; self->xmax = rmax;
    self->ymin = 0; self->ymax = gmax;
    self->zmin = 0; self->zmax = bmax;

    self->colorShift = shift;
}

void AkColorConvertPrivate::loadRgb2grayMatrix(AkColorConvert::YuvColorSpace yuvColorSpace,
                                               int rbits,
                                               int gbits,
                                               int bbits,
                                               int graybits)
{
    AkColorConvert::YuvColorSpaceType type = AkColorConvert::YuvColorSpaceType_FullSwing;

    qint64 kyr = 0;
    qint64 kyb = 0;
    qint64 div = 0;
    this->rbConstants(yuvColorSpace, kyr, kyb, div);
    qint64 kyg = div - kyr - kyb;

    int shift = qMax(rbits, qMax(gbits, bbits));
    qint64 shiftDiv = 1L << shift;
    qint64 rounding = 1L << (shift - 1);

    qint64 rmax = (1L << rbits) - 1;
    qint64 gmax = (1L << gbits) - 1;
    qint64 bmax = (1L << bbits) - 1;

    qint64 minY = 0;
    qint64 maxY = 0;
    this->limitsY(graybits, type, minY, maxY);
    auto diffY = maxY - minY;

    qint64 kiyr = this->roundedDiv(shiftDiv * diffY * kyr, div * rmax);
    qint64 kiyg = this->roundedDiv(shiftDiv * diffY * kyg, div * gmax);
    qint64 kiyb = this->roundedDiv(shiftDiv * diffY * kyb, div * bmax);

    qint64 minU = 0;
    qint64 maxU = 0;
    this->limitsUV(graybits, type, minU, maxU);

    qint64 minV = 0;
    qint64 maxV = 0;
    this->limitsUV(graybits, type, minV, maxV);

    qint64 ciy = rounding + shiftDiv * minY;
    qint64 ciu = rounding + shiftDiv * (minU + maxU) / 2;
    qint64 civ = rounding + shiftDiv * (minV + maxV) / 2;

    self->m00 = kiyr; self->m01 = kiyg; self->m02 = kiyb; self->m03 = ciy;
    self->m10 = 0   ; self->m11 = 0   ; self->m12 = 0   ; self->m13 = ciu;
    self->m20 = 0   ; self->m21 = 0   ; self->m22 = 0   ; self->m23 = civ;

    self->xmin = minY; self->xmax = maxY;
    self->ymin = minU; self->ymax = maxU;
    self->zmin = minV; self->zmax = maxV;

    self->colorShift = shift;
}

void AkColorConvertPrivate::loadGray2rgbMatrix(int graybits,
                                               int rbits,
                                               int gbits,
                                               int bbits)
{
    int shift = graybits;
    qint64 shiftDiv = 1L << shift;
    qint64 rounding = 1L << (shift - 1);

    qint64 graymax = (1L << graybits) - 1;

    qint64 rmax = (1L << rbits) - 1;
    qint64 gmax = (1L << gbits) - 1;
    qint64 bmax = (1L << bbits) - 1;

    qint64 kr = this->roundedDiv(shiftDiv * rmax, graymax);
    qint64 kg = this->roundedDiv(shiftDiv * gmax, graymax);
    qint64 kb = this->roundedDiv(shiftDiv * bmax, graymax);

    self->m00 = kr; self->m01 = 0; self->m02 = 0; self->m03 = rounding;
    self->m10 = kg; self->m11 = 0; self->m12 = 0; self->m13 = rounding;
    self->m20 = kb; self->m21 = 0; self->m22 = 0; self->m23 = rounding;

    self->xmin = 0; self->xmax = rmax;
    self->ymin = 0; self->ymax = gmax;
    self->zmin = 0; self->zmax = bmax;

    self->colorShift = shift;
}

void AkColorConvertPrivate::loadYuv2grayMatrix(AkColorConvert::YuvColorSpaceType yuvColorSpaceType,
                                               int ybits,
                                               int ubits,
                                               int vbits,
                                               int graybits)
{
    AkColorConvert::YuvColorSpaceType otype = AkColorConvert::YuvColorSpaceType_FullSwing;

    int shift = ybits;
    qint64 shiftDiv = 1L << shift;
    qint64 rounding = 1L << (shift - 1);

    qint64 graymax = (1L << graybits) - 1;

    qint64 minY = 0;
    qint64 maxY = 0;
    this->limitsY(ybits, yuvColorSpaceType, minY, maxY);
    auto diffY = maxY - minY;

    qint64 ky = this->roundedDiv(shiftDiv * graymax, diffY);

    qint64 minU = 0;
    qint64 maxU = 0;
    this->limitsUV(graybits, otype, minU, maxU);

    qint64 minV = 0;
    qint64 maxV = 0;
    this->limitsUV(graybits, otype, minV, maxV);

    qint64 ciy = rounding - this->roundedDiv(shiftDiv * minY * graymax, diffY);
    qint64 ciu = rounding + shiftDiv * (minU + maxU) / 2;
    qint64 civ = rounding + shiftDiv * (minV + maxV) / 2;

    self->m00 = ky; self->m01 = 0; self->m02 = 0; self->m03 = ciy;
    self->m10 = 0 ; self->m11 = 0; self->m12 = 0; self->m13 = ciu;
    self->m20 = 0 ; self->m21 = 0; self->m22 = 0; self->m23 = civ;

    self->xmin = 0; self->xmax = graymax;
    self->ymin = 0; self->ymax = graymax;
    self->zmin = 0; self->zmax = graymax;

    self->colorShift = shift;
}

void AkColorConvertPrivate::loadGray2yuvMatrix(AkColorConvert::YuvColorSpaceType yuvColorSpaceType,
                                               int graybits,
                                               int ybits,
                                               int ubits,
                                               int vbits)
{
    int shift = graybits;
    qint64 shiftDiv = 1L << shift;
    qint64 rounding = 1L << (shift - 1);

    qint64 graymax = (1L << graybits) - 1;

    qint64 minY = 0;
    qint64 maxY = 0;
    this->limitsY(ybits, yuvColorSpaceType, minY, maxY);
    auto diffY = maxY - minY;

    qint64 ky = this->roundedDiv(shiftDiv * diffY, graymax);

    qint64 minU = 0;
    qint64 maxU = 0;
    this->limitsUV(ubits, yuvColorSpaceType, minU, maxU);

    qint64 minV = 0;
    qint64 maxV = 0;
    this->limitsUV(vbits, yuvColorSpaceType, minV, maxV);

    qint64 ciy = rounding + shiftDiv * minY;
    qint64 ciu = rounding + shiftDiv * (minU + maxU) / 2;
    qint64 civ = rounding + shiftDiv * (minV + maxV) / 2;

    self->m00 = ky; self->m01 = 0; self->m02 = 0; self->m03 = ciy;
    self->m10 = 0 ; self->m11 = 0; self->m12 = 0; self->m13 = ciu;
    self->m20 = 0 ; self->m21 = 0; self->m22 = 0; self->m23 = civ;

    self->xmin = minY; self->xmax = maxY;
    self->ymin = minU; self->ymax = maxU;
    self->zmin = minV; self->zmax = maxV;

    self->colorShift = shift;
}

void AkColorConvertPrivate::loadAlphaRgbMatrix(int alphaBits)
{
    qint64 amax = (1L << alphaBits) - 1;
    self->alphaShift = alphaBits;
    qint64 shiftDiv = 1L << self->alphaShift;
    qint64 rounding = 1L << (self->alphaShift - 1);

    auto k = this->roundedDiv(shiftDiv, amax);

    self->a00 = k; self->a01 = 0; self->a02 = rounding;
    self->a10 = k; self->a11 = 0; self->a12 = rounding;
    self->a20 = k; self->a21 = 0; self->a22 = rounding;

}

void AkColorConvertPrivate::loadAlphaYuvMatrix(AkColorConvert::YuvColorSpaceType yuvColorSpaceType,
                                               int alphaBits,
                                               int ybits,
                                               int ubits,
                                               int vbits)
{
    qint64 amax = (1L << alphaBits) - 1;
    self->alphaShift = alphaBits;
    qint64 shiftDiv = 1L << self->alphaShift;
    qint64 rounding = 1L << (self->alphaShift - 1);

    quint64 k = shiftDiv / amax;

    qint64 minY = 0;
    qint64 maxY = 0;
    this->limitsY(ybits, yuvColorSpaceType, minY, maxY);
    qint64 ky = -this->roundedDiv(shiftDiv * minY, amax);

    qint64 minU = 0;
    qint64 maxU = 0;
    this->limitsUV(ubits, yuvColorSpaceType, minU, maxU);
    qint64 ku = -this->roundedDiv(shiftDiv * (minU + maxU), 2 * amax);

    qint64 minV = 0;
    qint64 maxV = 0;
    this->limitsUV(vbits, yuvColorSpaceType, minV, maxV);
    qint64 kv = -this->roundedDiv(shiftDiv * (minV + maxV), 2 * amax);

    qint64 ciy = rounding + shiftDiv * minY;
    qint64 ciu = rounding + shiftDiv * (minU + maxU) / 2;
    qint64 civ = rounding + shiftDiv * (minV + maxV) / 2;

    self->a00 = k; self->a01 = ky; self->a02 = ciy;
    self->a10 = k; self->a11 = ku; self->a12 = ciu;
    self->a20 = k; self->a21 = kv; self->a22 = civ;
}

void AkColorConvertPrivate::loadAlphaGrayMatrix(int alphaBits, int graybits)
{
    AkColorConvert::YuvColorSpaceType otype = AkColorConvert::YuvColorSpaceType_FullSwing;

    qint64 amax = (1L << alphaBits) - 1;
    self->alphaShift = alphaBits;
    qint64 shiftDiv = 1L << self->alphaShift;
    qint64 rounding = 1L << (self->alphaShift - 1);

    auto k = this->roundedDiv(self->alphaShift, amax);

    qint64 minU = 0;
    qint64 maxU = 0;
    this->limitsUV(graybits, otype, minU, maxU);

    qint64 minV = 0;
    qint64 maxV = 0;
    this->limitsUV(graybits, otype, minV, maxV);

    qint64 ciu = rounding + shiftDiv * (minU + maxU) / 2;
    qint64 civ = rounding + shiftDiv * (minV + maxV) / 2;

    self->a00 = k; self->a01 = 0; self->a02 = rounding;
    self->a10 = 0; self->a11 = 0; self->a12 = ciu;
    self->a20 = 0; self->a21 = 0; self->a22 = civ;
}

#include "moc_akcolorconvert.cpp"