akvideopacket.cpp 38.4 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 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
/* Webcamoid, webcam capture application.
 * Copyright (C) 2016  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 <QDebug>
#include <QVariant>
#include <QImage>
#include <QQmlEngine>

#include "akvideopacket.h"
#include "akcolorconvert.h"
#include "akfrac.h"
#include "akpacket.h"
#include "akvideoformatspec.h"

#define MAX_PLANES 4

enum FillType
{
    FillType_Vector,
    FillType_1,
    FillType_3,
};

enum FillDataTypes
{
    FillDataTypes_8,
    FillDataTypes_16,
    FillDataTypes_32,
};

enum AlphaMode
{
    AlphaMode_AO,
    AlphaMode_O,
};

class FillParameters
{
    public:
        AkColorConvert colorConvert;
        FillType fillType {FillType_3};
        FillDataTypes fillDataTypes {FillDataTypes_8};
        AlphaMode alphaMode {AlphaMode_AO};

        int endianess {Q_BYTE_ORDER};

        int width {0};
        int height {0};

        int *dstWidthOffsetX {nullptr};
        int *dstWidthOffsetY {nullptr};
        int *dstWidthOffsetZ {nullptr};
        int *dstWidthOffsetA {nullptr};

        int planeXo {0};
        int planeYo {0};
        int planeZo {0};
        int planeAo {0};

        AkColorComponent compXo;
        AkColorComponent compYo;
        AkColorComponent compZo;
        AkColorComponent compAo;

        size_t xoOffset {0};
        size_t yoOffset {0};
        size_t zoOffset {0};
        size_t aoOffset {0};

        size_t xoShift {0};
        size_t yoShift {0};
        size_t zoShift {0};
        size_t aoShift {0};

        quint64 maskXo {0};
        quint64 maskYo {0};
        quint64 maskZo {0};
        quint64 maskAo {0};

        FillParameters();
        FillParameters(const FillParameters &other);
        ~FillParameters();
        FillParameters &operator =(const FillParameters &other);
        inline void clearBuffers();
        inline void allocateBuffers(const AkVideoCaps &caps);
        void configure(const AkVideoCaps &caps, AkColorConvert &colorConvert);
        void configureFill(const AkVideoCaps &caps);
        void reset();
};

class AkVideoPacketPrivate
{
    public:
        AkVideoCaps m_caps;
        quint8 *m_data {nullptr};
        size_t m_dataSize {0};
        size_t m_nPlanes {0};
        quint8 *m_planes[MAX_PLANES];
        size_t m_planeSize[MAX_PLANES];
        size_t m_planeOffset[MAX_PLANES];
        size_t m_pixelSize[MAX_PLANES];
        size_t m_lineSize[MAX_PLANES];
        size_t m_bytesUsed[MAX_PLANES];
        size_t m_widthDiv[MAX_PLANES];
        size_t m_heightDiv[MAX_PLANES];
        size_t m_align {32};

        void updateParams(const AkVideoFormatSpec &specs);
        inline void updatePlanes();
        template<typename T>
        static inline T alignUp(const T &value, const T &align)
        {
            return (value + align - 1) & ~(align - 1);
        }

        // Endianness conversion functions for color components

        inline quint8 swapBytes(quint8 &&value, int endianness) const
        {
            Q_UNUSED(endianness)

            return value;
        }

        inline quint16 swapBytes(quint16 &&value, int endianness) const
        {
            if (endianness == Q_BYTE_ORDER)
                return value;

            quint16 result;
            auto pv = reinterpret_cast<quint8 *>(&value);
            auto pr = reinterpret_cast<quint8 *>(&result);
            pr[0] = pv[1];
            pr[1] = pv[0];

            return result;
        }

        inline quint32 swapBytes(quint32 &&value, int endianness) const
        {
            if (endianness == Q_BYTE_ORDER)
                return value;

            quint32 result;
            auto pv = reinterpret_cast<quint8 *>(&value);
            auto pr = reinterpret_cast<quint8 *>(&result);
            pr[0] = pv[3];
            pr[1] = pv[2];
            pr[2] = pv[1];
            pr[3] = pv[0];

            return result;
        }

        inline quint64 swapBytes(quint64 &&value, int endianness) const
        {
            if (endianness == Q_BYTE_ORDER)
                return value;

            quint64 result;
            auto pv = reinterpret_cast<quint8 *>(&value);
            auto pr = reinterpret_cast<quint8 *>(&result);
            pr[0] = pv[7];
            pr[1] = pv[6];
            pr[2] = pv[5];
            pr[3] = pv[4];
            pr[4] = pv[3];
            pr[5] = pv[2];
            pr[6] = pv[1];
            pr[7] = pv[0];

            return result;
        }

        /* Fill functions */

        template <typename DataType>
        void fill3(const FillParameters &fc, QRgb color) const
        {
            auto xi = qRed(color);
            auto yi = qGreen(color);
            auto zi = qBlue(color);
            auto ai = qAlpha(color);

            qint64 xo_ = 0;
            qint64 yo_ = 0;
            qint64 zo_ = 0;
            fc.colorConvert.applyMatrix(xi, yi, zi, &xo_, &yo_, &zo_);
            fc.colorConvert.applyAlpha(ai, &xo_, &yo_, &zo_);

            auto line_x = this->m_planes[fc.planeXo] + fc.xoOffset;
            auto line_y = this->m_planes[fc.planeYo] + fc.yoOffset;
            auto line_z = this->m_planes[fc.planeZo] + fc.zoOffset;

            auto width = qMax<size_t>(8 * this->m_pixelSize[0] / this->m_caps.bpp(), 1);

            for (int x = 0; x < width; ++x) {
                int &xd_x = fc.dstWidthOffsetX[x];
                int &xd_y = fc.dstWidthOffsetY[x];
                int &xd_z = fc.dstWidthOffsetZ[x];

                auto xo = reinterpret_cast<DataType *>(line_x + xd_x);
                auto yo = reinterpret_cast<DataType *>(line_y + xd_y);
                auto zo = reinterpret_cast<DataType *>(line_z + xd_z);

                *xo = (*xo & DataType(fc.maskXo)) | (DataType(xo_) << fc.xoShift);
                *yo = (*yo & DataType(fc.maskYo)) | (DataType(yo_) << fc.yoShift);
                *zo = (*zo & DataType(fc.maskZo)) | (DataType(zo_) << fc.zoShift);

                auto xot = this->swapBytes(DataType(*xo), fc.endianess);
                auto yot = this->swapBytes(DataType(*yo), fc.endianess);
                auto zot = this->swapBytes(DataType(*zo), fc.endianess);

                *xo = xot;
                *yo = yot;
                *zo = zot;
            }
        }

        template <typename DataType>
        void fill3A(const FillParameters &fc, QRgb color) const
        {
            auto xi = qRed(color);
            auto yi = qGreen(color);
            auto zi = qBlue(color);
            auto ai = qAlpha(color);

            qint64 xo_ = 0;
            qint64 yo_ = 0;
            qint64 zo_ = 0;
            fc.colorConvert.applyMatrix(xi, yi, zi, &xo_, &yo_, &zo_);

            auto line_x = this->m_planes[fc.planeXo] + fc.xoOffset;
            auto line_y = this->m_planes[fc.planeYo] + fc.yoOffset;
            auto line_z = this->m_planes[fc.planeZo] + fc.zoOffset;
            auto line_a = this->m_planes[fc.planeAo] + fc.aoOffset;

            auto width = qMax<size_t>(8 * this->m_pixelSize[0] / this->m_caps.bpp(), 1);

            for (int x = 0; x < width; ++x) {
                int &xd_x = fc.dstWidthOffsetX[x];
                int &xd_y = fc.dstWidthOffsetY[x];
                int &xd_z = fc.dstWidthOffsetZ[x];
                int &xd_a = fc.dstWidthOffsetA[x];

                auto xo = reinterpret_cast<DataType *>(line_x + xd_x);
                auto yo = reinterpret_cast<DataType *>(line_y + xd_y);
                auto zo = reinterpret_cast<DataType *>(line_z + xd_z);
                auto ao = reinterpret_cast<DataType *>(line_a + xd_a);

                *xo = (*xo & DataType(fc.maskXo)) | (DataType(xo_) << fc.xoShift);
                *yo = (*yo & DataType(fc.maskYo)) | (DataType(yo_) << fc.yoShift);
                *zo = (*zo & DataType(fc.maskZo)) | (DataType(zo_) << fc.zoShift);
                *ao = (*ao & DataType(fc.maskAo)) | (DataType(ai) << fc.aoShift);

                auto xot = this->swapBytes(DataType(*xo), fc.endianess);
                auto yot = this->swapBytes(DataType(*yo), fc.endianess);
                auto zot = this->swapBytes(DataType(*zo), fc.endianess);
                auto aot = this->swapBytes(DataType(*ao), fc.endianess);

                *xo = xot;
                *yo = yot;
                *zo = zot;
                *ao = aot;
            }
        }

        template <typename DataType>
        void fill1(const FillParameters &fc, QRgb color) const
        {
            auto xi = qRed(color);
            auto yi = qGreen(color);
            auto zi = qBlue(color);
            auto ai = qAlpha(color);

            qint64 xo_ = 0;
            fc.colorConvert.applyPoint(xi, yi, zi, &xo_);
            fc.colorConvert.applyAlpha(ai, &xo_);

            auto line_x = this->m_planes[fc.planeXo] + fc.xoOffset;
            auto width = qMax<size_t>(8 * this->m_pixelSize[0] / this->m_caps.bpp(), 1);

            for (int x = 0; x < width; ++x) {
                int &xd_x = fc.dstWidthOffsetX[x];
                auto xo = reinterpret_cast<DataType *>(line_x + xd_x);
                *xo = (*xo & DataType(fc.maskXo)) | (DataType(xo_) << fc.xoShift);
                *xo = this->swapBytes(DataType(*xo), fc.endianess);
            }
        }

        template <typename DataType>
        void fill1A(const FillParameters &fc, QRgb color) const
        {
            auto xi = qRed(color);
            auto yi = qGreen(color);
            auto zi = qBlue(color);
            auto ai = qAlpha(color);

            qint64 xo_ = 0;
            fc.colorConvert.applyPoint(xi, yi, zi, &xo_);

            auto line_x = this->m_planes[fc.planeXo] + fc.xoOffset;
            auto line_a = this->m_planes[fc.planeAo] + fc.aoOffset;

            auto width = qMax<size_t>(8 * this->m_pixelSize[0] / this->m_caps.bpp(), 1);

            for (int x = 0; x < width; ++x) {
                int &xd_x = fc.dstWidthOffsetX[x];
                int &xd_a = fc.dstWidthOffsetA[x];

                auto xo = reinterpret_cast<DataType *>(line_x + xd_x);
                auto ao = reinterpret_cast<DataType *>(line_a + xd_a);

                *xo = (*xo & DataType(fc.maskXo)) | (DataType(xo_) << fc.xoShift);
                *ao = (*ao & DataType(fc.maskAo)) | (DataType(ai) << fc.aoShift);

                auto xot = this->swapBytes(DataType(*xo), fc.endianess);
                auto aot = this->swapBytes(DataType(*ao), fc.endianess);

                *xo = xot;
                *ao = aot;
            }
        }

        // Vectorized fill functions

        template <typename DataType>
        void fillV3(const FillParameters &fc, QRgb color) const
        {
            auto xi = qRed(color);
            auto yi = qGreen(color);
            auto zi = qBlue(color);
            auto ai = qAlpha(color);

            qint64 xo_ = 0;
            qint64 yo_ = 0;
            qint64 zo_ = 0;
            fc.colorConvert.applyVector(xi, yi, zi, &xo_, &yo_, &zo_);
            fc.colorConvert.applyAlpha(ai, &xo_, &yo_, &zo_);

            auto line_x = this->m_planes[fc.planeXo] + fc.xoOffset;
            auto line_y = this->m_planes[fc.planeYo] + fc.yoOffset;
            auto line_z = this->m_planes[fc.planeZo] + fc.zoOffset;

            auto width = qMax<size_t>(8 * this->m_pixelSize[0] / this->m_caps.bpp(), 1);

            for (int x = 0; x < width; ++x) {
                int &xd_x = fc.dstWidthOffsetX[x];
                int &xd_y = fc.dstWidthOffsetY[x];
                int &xd_z = fc.dstWidthOffsetZ[x];

                auto xo = reinterpret_cast<DataType *>(line_x + xd_x);
                auto yo = reinterpret_cast<DataType *>(line_y + xd_y);
                auto zo = reinterpret_cast<DataType *>(line_z + xd_z);

                *xo = (*xo & DataType(fc.maskXo)) | (DataType(xo_) << fc.xoShift);
                *yo = (*yo & DataType(fc.maskYo)) | (DataType(yo_) << fc.yoShift);
                *zo = (*zo & DataType(fc.maskZo)) | (DataType(zo_) << fc.zoShift);

                auto xot = this->swapBytes(DataType(*xo), fc.endianess);
                auto yot = this->swapBytes(DataType(*yo), fc.endianess);
                auto zot = this->swapBytes(DataType(*zo), fc.endianess);

                *xo = xot;
                *yo = yot;
                *zo = zot;
            }
        }

        template <typename DataType>
        void fillV3A(const FillParameters &fc, QRgb color) const
        {
            auto xi = qRed(color);
            auto yi = qGreen(color);
            auto zi = qBlue(color);
            auto ai = qAlpha(color);

            qint64 xo_ = 0;
            qint64 yo_ = 0;
            qint64 zo_ = 0;
            fc.colorConvert.applyVector(xi, yi, zi, &xo_, &yo_, &zo_);

            auto line_x = this->m_planes[fc.planeXo] + fc.xoOffset;
            auto line_y = this->m_planes[fc.planeYo] + fc.yoOffset;
            auto line_z = this->m_planes[fc.planeZo] + fc.zoOffset;
            auto line_a = this->m_planes[fc.planeAo] + fc.aoOffset;

            auto width = qMax<size_t>(8 * this->m_pixelSize[0] / this->m_caps.bpp(), 1);

            for (int x = 0; x < width; ++x) {
                int &xd_x = fc.dstWidthOffsetX[x];
                int &xd_y = fc.dstWidthOffsetY[x];
                int &xd_z = fc.dstWidthOffsetZ[x];
                int &xd_a = fc.dstWidthOffsetA[x];

                auto xo = reinterpret_cast<DataType *>(line_x + xd_x);
                auto yo = reinterpret_cast<DataType *>(line_y + xd_y);
                auto zo = reinterpret_cast<DataType *>(line_z + xd_z);
                auto ao = reinterpret_cast<DataType *>(line_a + xd_a);

                *xo = (*xo & DataType(fc.maskXo)) | (DataType(xo_) << fc.xoShift);
                *yo = (*yo & DataType(fc.maskYo)) | (DataType(yo_) << fc.yoShift);
                *zo = (*zo & DataType(fc.maskZo)) | (DataType(zo_) << fc.zoShift);
                *ao = (*ao & DataType(fc.maskAo)) | (DataType(ai) << fc.aoShift);

                auto xot = this->swapBytes(DataType(*xo), fc.endianess);
                auto yot = this->swapBytes(DataType(*yo), fc.endianess);
                auto zot = this->swapBytes(DataType(*zo), fc.endianess);
                auto aot = this->swapBytes(DataType(*ao), fc.endianess);

                *xo = xot;
                *yo = yot;
                *zo = zot;
                *ao = aot;
            }
        }

#define FILL_FUNC(components) \
        template <typename DataType> \
            inline void fillFrame##components(const FillParameters &fc, \
                                              QRgb color) const \
        { \
                switch (fc.alphaMode) { \
                case AlphaMode_AO: \
                    this->fill##components##A<DataType>(fc, color); \
                    break; \
                case AlphaMode_O: \
                    this->fill##components<DataType>(fc, color); \
                    break; \
            }; \
        }

#define FILLV_FUNC(components) \
        template <typename DataType> \
            inline void fillVFrame##components(const FillParameters &fc, \
                                               QRgb color) const \
        { \
                switch (fc.alphaMode) { \
                case AlphaMode_AO: \
                    this->fillV##components##A<DataType>(fc, color); \
                    break; \
                case AlphaMode_O: \
                    this->fillV##components<DataType>(fc, color); \
                    break; \
            }; \
        }

        FILL_FUNC(1)
        FILL_FUNC(3)
        FILLV_FUNC(3)

        template <typename DataType>
        inline void fill(const FillParameters &fc, QRgb color)
        {
            switch (fc.fillType) {
            case FillType_Vector:
                this->fillVFrame3<DataType>(fc, color);
                break;
            case FillType_3:
                this->fillFrame3<DataType>(fc, color);
                break;
            case FillType_1:
                this->fillFrame1<DataType>(fc, color);
                break;
            }
        }

        inline void fill(QRgb color);
};

AkVideoPacket::AkVideoPacket(QObject *parent):
    AkPacketBase(parent)
{
    this->d = new AkVideoPacketPrivate;
}

AkVideoPacket::AkVideoPacket(const AkVideoCaps &caps,
                             bool initialized,
                             size_t align):
    AkPacketBase()
{
    this->d = new AkVideoPacketPrivate;
    this->d->m_caps = caps;
    this->d->m_align = align;
    auto specs = AkVideoCaps::formatSpecs(this->d->m_caps.format());
    this->d->m_nPlanes = specs.planes();
    this->d->updateParams(specs);

    if (this->d->m_dataSize > 0) {
            this->d->m_data = new quint8 [this->d->m_dataSize];

            if (initialized)
                memset(this->d->m_data, 0, this->d->m_dataSize);
    }

    this->d->updatePlanes();
}

AkVideoPacket::AkVideoPacket(const AkPacket &other):
    AkPacketBase(other)
{
    this->d = new AkVideoPacketPrivate;

    if (other.type() == AkPacket::PacketVideo) {
        auto data = reinterpret_cast<AkVideoPacket *>(other.privateData());
        this->d->m_caps = data->d->m_caps;

        if (data->d->m_data && data->d->m_dataSize > 0) {
                this->d->m_data = new quint8 [data->d->m_dataSize];
                memcpy(this->d->m_data, data->d->m_data, data->d->m_dataSize);
        }

        this->d->m_dataSize = data->d->m_dataSize;
        this->d->m_nPlanes = data->d->m_nPlanes;

        if (this->d->m_nPlanes > 0) {
            const size_t dataSize = MAX_PLANES * sizeof(size_t);
            memcpy(this->d->m_planeSize, data->d->m_planeSize, dataSize);
            memcpy(this->d->m_planeOffset, data->d->m_planeOffset, dataSize);
            memcpy(this->d->m_pixelSize, data->d->m_pixelSize, dataSize);
            memcpy(this->d->m_lineSize, data->d->m_lineSize, dataSize);
            memcpy(this->d->m_bytesUsed, data->d->m_bytesUsed, dataSize);
            memcpy(this->d->m_widthDiv, data->d->m_widthDiv, dataSize);
            memcpy(this->d->m_heightDiv, data->d->m_heightDiv, dataSize);
        }

        this->d->m_align = data->d->m_align;
        this->d->updatePlanes();
    }
}

AkVideoPacket::AkVideoPacket(const AkVideoPacket &other):
    AkPacketBase(other)
{
    this->d = new AkVideoPacketPrivate;
    this->d->m_caps = other.d->m_caps;

    if (other.d->m_data && other.d->m_dataSize > 0) {
        this->d->m_data = new quint8 [other.d->m_dataSize];
        memcpy(this->d->m_data, other.d->m_data, other.d->m_dataSize);
    }

    this->d->m_dataSize = other.d->m_dataSize;
    this->d->m_nPlanes = other.d->m_nPlanes;

    if (this->d->m_nPlanes > 0) {
        const size_t dataSize = MAX_PLANES * sizeof(size_t);
        memcpy(this->d->m_planeSize, other.d->m_planeSize, dataSize);
        memcpy(this->d->m_planeOffset, other.d->m_planeOffset, dataSize);
        memcpy(this->d->m_pixelSize, other.d->m_pixelSize, dataSize);
        memcpy(this->d->m_lineSize, other.d->m_lineSize, dataSize);
        memcpy(this->d->m_bytesUsed, other.d->m_bytesUsed, dataSize);
        memcpy(this->d->m_widthDiv, other.d->m_widthDiv, dataSize);
        memcpy(this->d->m_heightDiv, other.d->m_heightDiv, dataSize);
    }

    this->d->m_align = other.d->m_align;
    this->d->updatePlanes();
}

AkVideoPacket::~AkVideoPacket()
{
    if (this->d->m_data)
        delete [] this->d->m_data;

    delete this->d;
}

AkVideoPacket &AkVideoPacket::operator =(const AkPacket &other)
{
    if (other.type() == AkPacket::PacketVideo) {
        auto data = reinterpret_cast<AkVideoPacket *>(other.privateData());
        this->d->m_caps = data->d->m_caps;

        if (this->d->m_data) {
            delete [] this->d->m_data;
            this->d->m_data = nullptr;
        }

        if (data->d->m_data && data->d->m_dataSize > 0) {
            this->d->m_data = new quint8 [data->d->m_dataSize];
            memcpy(this->d->m_data, data->d->m_data, data->d->m_dataSize);
        }

        this->d->m_dataSize = data->d->m_dataSize;
        this->d->m_nPlanes = data->d->m_nPlanes;

        if (this->d->m_nPlanes > 0) {
            const size_t dataSize = MAX_PLANES * sizeof(size_t);
            memcpy(this->d->m_planeSize, data->d->m_planeSize, dataSize);
            memcpy(this->d->m_planeOffset, data->d->m_planeOffset, dataSize);
            memcpy(this->d->m_pixelSize, data->d->m_pixelSize, dataSize);
            memcpy(this->d->m_lineSize, data->d->m_lineSize, dataSize);
            memcpy(this->d->m_bytesUsed, data->d->m_bytesUsed, dataSize);
            memcpy(this->d->m_widthDiv, data->d->m_widthDiv, dataSize);
            memcpy(this->d->m_heightDiv, data->d->m_heightDiv, dataSize);
        }

        this->d->m_align = data->d->m_align;
        this->d->updatePlanes();
    } else {
        this->d->m_caps = AkVideoCaps();

        if (this->d->m_data) {
            delete [] this->d->m_data;
            this->d->m_data = nullptr;
        }

        this->d->m_dataSize = 0;
        this->d->m_nPlanes = 0;
        this->d->m_align = 32;
    }

    this->copyMetadata(other);

    return *this;
}

AkVideoPacket &AkVideoPacket::operator =(const AkVideoPacket &other)
{
    if (this != &other) {
        this->d->m_caps = other.d->m_caps;

        if (this->d->m_data) {
            delete [] this->d->m_data;
            this->d->m_data = nullptr;
        }

        if (other.d->m_data && other.d->m_dataSize > 0) {
            this->d->m_data = new quint8 [other.d->m_dataSize];
            memcpy(this->d->m_data, other.d->m_data, other.d->m_dataSize);
        }

        this->d->m_dataSize = other.d->m_dataSize;
        this->d->m_nPlanes = other.d->m_nPlanes;

        if (this->d->m_nPlanes > 0) {
            memcpy(this->d->m_planeSize, other.d->m_planeSize, this->d->m_nPlanes * sizeof(size_t));
            memcpy(this->d->m_planeOffset, other.d->m_planeOffset, this->d->m_nPlanes * sizeof(size_t));
            memcpy(this->d->m_pixelSize, other.d->m_pixelSize, this->d->m_nPlanes * sizeof(size_t));
            memcpy(this->d->m_lineSize, other.d->m_lineSize, this->d->m_nPlanes * sizeof(size_t));
            memcpy(this->d->m_bytesUsed, other.d->m_bytesUsed, this->d->m_nPlanes * sizeof(size_t));
            memcpy(this->d->m_widthDiv, other.d->m_widthDiv, this->d->m_nPlanes * sizeof(size_t));
            memcpy(this->d->m_heightDiv, other.d->m_heightDiv, this->d->m_nPlanes * sizeof(size_t));
        }

        this->copyMetadata(other);
        this->d->m_align = other.d->m_align;
        this->d->updatePlanes();
    }

    return *this;
}

AkVideoPacket::operator bool() const
{
    return this->d->m_caps && this->d->m_data;
}

AkVideoPacket::operator AkPacket() const
{
    AkPacket packet;
    packet.setType(AkPacket::PacketVideo);
    packet.setPrivateData(new AkVideoPacket(*this),
                          [] (void *data) -> void * {
                              return new AkVideoPacket(*reinterpret_cast<AkVideoPacket *>(data));
                          },
                          [] (void *data) {
                              delete reinterpret_cast<AkVideoPacket *>(data);
                          });
    packet.copyMetadata(*this);

    return packet;
}

const AkVideoCaps &AkVideoPacket::caps() const
{
    return this->d->m_caps;
}

size_t AkVideoPacket::size() const
{
    return this->d->m_dataSize;
}

size_t AkVideoPacket::planes() const
{
    return this->d->m_nPlanes;
}

size_t AkVideoPacket::planeSize(int plane) const
{
    return this->d->m_planeSize[plane];
}

size_t AkVideoPacket::pixelSize(int plane) const
{
    return this->d->m_pixelSize[plane];
}

size_t AkVideoPacket::lineSize(int plane) const
{
    return this->d->m_lineSize[plane];
}

size_t AkVideoPacket::bytesUsed(int plane) const
{
    return this->d->m_bytesUsed[plane];
}

size_t AkVideoPacket::widthDiv(int plane) const
{
    return this->d->m_widthDiv[plane];
}

size_t AkVideoPacket::heightDiv(int plane) const
{
    return this->d->m_heightDiv[plane];
}

const char *AkVideoPacket::constData() const
{
    return reinterpret_cast<char *>(this->d->m_data);
}

char *AkVideoPacket::data()
{
    return reinterpret_cast<char *>(this->d->m_data);
}

const quint8 *AkVideoPacket::constPlane(int plane) const
{
    return this->d->m_planes[plane];
}

quint8 *AkVideoPacket::plane(int plane)
{
    return this->d->m_planes[plane];
}

const quint8 *AkVideoPacket::constLine(int plane, int y) const
{
    return this->d->m_planes[plane]
            + size_t(y >> this->d->m_heightDiv[plane])
            * this->d->m_lineSize[plane];
}

quint8 *AkVideoPacket::line(int plane, int y)
{
    return this->d->m_planes[plane]
            + size_t(y >> this->d->m_heightDiv[plane])
            * this->d->m_lineSize[plane];
}

AkVideoPacket AkVideoPacket::copy(int x, int y, int width, int height) const
{
    auto ocaps = this->d->m_caps;
    ocaps.setWidth(width);
    ocaps.setHeight(height);
    AkVideoPacket dst(ocaps, true);
    dst.copyMetadata(*this);

    auto maxX = qMin(x + width, this->d->m_caps.width());
    auto maxY = qMin(y + height, this->d->m_caps.height());
    auto copyWidth = qMax(maxX - x, 0);

    if (copyWidth < 1)
        return dst;

    auto diffY = maxY - y;

    for (int plane = 0; plane < this->d->m_nPlanes; plane++) {
        size_t offset = x
                        * this->d->m_bytesUsed[plane]
                        / this->d->m_caps.width();
        size_t copyBytes = copyWidth
                           * this->d->m_bytesUsed[plane]
                           / this->d->m_caps.width();
        auto srcLineOffset = this->d->m_lineSize[plane];
        auto dstLineOffset = dst.d->m_lineSize[plane];
        auto srcLine = this->constLine(plane, y) + offset;
        auto dstLine = dst.d->m_planes[plane];
        auto maxY = diffY >> this->d->m_heightDiv[plane];

        for (int y = 0; y < maxY; y++) {
            memcpy(dstLine, srcLine, copyBytes);
            srcLine += srcLineOffset;
            dstLine += dstLineOffset;
        }
    }

    return dst;
}

void AkVideoPacket::fillRgb(QRgb color)
{
    return this->d->fill(color);
}

void AkVideoPacket::registerTypes()
{
    qRegisterMetaType<AkVideoPacket>("AkVideoPacket");
    qmlRegisterSingletonType<AkVideoPacket>("Ak", 1, 0, "AkVideoPacket",
                                            [] (QQmlEngine *qmlEngine,
                                                QJSEngine *jsEngine) -> QObject * {
        Q_UNUSED(qmlEngine)
        Q_UNUSED(jsEngine)

        return new AkVideoPacket();
    });
}

QDebug operator <<(QDebug debug, const AkVideoPacket &packet)
{
    debug.nospace() << "AkVideoPacket("
                    << "caps="
                    << packet.caps()
                    << ",dataSize="
                    << packet.size()
                    << ",id="
                    << packet.id()
                    << ",pts="
                    << packet.pts()
                    << "("
                    << packet.pts() * packet.timeBase().value()
                    << ")"
                    << ",duration="
                    << packet.duration()
                    << "("
                    << packet.duration() * packet.timeBase().value()
                    << ")"
                    << ",timeBase="
                    << packet.timeBase()
                    << ",index="
                    << packet.index()
                    << ")";

    return debug.space();
}

void AkVideoPacketPrivate::updateParams(const AkVideoFormatSpec &specs)
{
    if (this->m_align < 1)
        this->m_align = 32;

    this->m_dataSize = 0;
    int i = 0;

    for (size_t j = 0; j < specs.planes(); ++j) {
        auto &plane = specs.plane(j);
        size_t bytesUsed = plane.bitsSize() * this->m_caps.width() / 8;
        size_t lineSize =
                AkVideoPacketPrivate::alignUp(bytesUsed, size_t(this->m_align));

        this->m_pixelSize[i] = plane.pixelSize();
        this->m_lineSize[i] = lineSize;
        this->m_bytesUsed[i] = bytesUsed;

        size_t planeSize = (lineSize * this->m_caps.height()) >> plane.heightDiv();

        this->m_planeSize[i] = planeSize;
        this->m_planeOffset[i] = this->m_dataSize;

        this->m_dataSize += planeSize;

        this->m_widthDiv[i] = plane.widthDiv();
        this->m_heightDiv[i] = plane.heightDiv();

        i++;
    }
}

void AkVideoPacketPrivate::updatePlanes()
{
    for (int i = 0; i < this->m_nPlanes; ++i)
        this->m_planes[i] = this->m_data + this->m_planeOffset[i];
}

#define DEFINE_FILL_FUNC(size) \
case FillDataTypes_##size: \
    this->fill<quint##size>(fc, color); \
    break;

void AkVideoPacketPrivate::fill(QRgb color)
{
    FillParameters fc;
    fc.configure(this->m_caps, fc.colorConvert);
    fc.configureFill(this->m_caps);

    switch (fc.fillDataTypes) {
        DEFINE_FILL_FUNC(8)
        DEFINE_FILL_FUNC(16)
        DEFINE_FILL_FUNC(32)
    }

    for (size_t plane = 0; plane < this->m_nPlanes; plane++) {
        auto &lineSize = this->m_lineSize[plane];
        auto &pixelSize = this->m_pixelSize[plane];
        auto line0 = this->m_planes[plane];
        auto line = line0 + pixelSize;
        auto width = lineSize / pixelSize;
        auto height = fc.height >> this->m_heightDiv[plane];

        for (int x = 1; x < width; ++x) {
            memcpy(line, line0, pixelSize);
            line += pixelSize;
        }

        line = line0 + lineSize;

        for (int y = 1; y < height; ++y) {
            memcpy(line, line0, lineSize);
            line += lineSize;
        }
    }
}

FillParameters::FillParameters()
{
}

FillParameters::FillParameters(const FillParameters &other):
    colorConvert(other.colorConvert),
    fillType(other.fillType),
    fillDataTypes(other.fillDataTypes),
    alphaMode(other.alphaMode),
    endianess(other.endianess),
    width(other.width),
    height(other.height),
    planeXo(other.planeXo),
    planeYo(other.planeYo),
    planeZo(other.planeZo),
    planeAo(other.planeAo),
    compXo(other.compXo),
    compYo(other.compYo),
    compZo(other.compZo),
    compAo(other.compAo),
    xoOffset(other.xoOffset),
    yoOffset(other.yoOffset),
    zoOffset(other.zoOffset),
    aoOffset(other.aoOffset),
    xoShift(other.xoShift),
    yoShift(other.yoShift),
    zoShift(other.zoShift),
    aoShift(other.aoShift),
    maskXo(other.maskXo),
    maskYo(other.maskYo),
    maskZo(other.maskZo),
    maskAo(other.maskAo)
{
    if (this->width > 0) {
        size_t oWidthDataSize = sizeof(int) * this->width;

        if (other.dstWidthOffsetX) {
            this->dstWidthOffsetX = new int [this->width];
            memcpy(this->dstWidthOffsetX, other.dstWidthOffsetX, oWidthDataSize);
        }

        if (other.dstWidthOffsetY) {
            this->dstWidthOffsetY = new int [this->width];
            memcpy(this->dstWidthOffsetY, other.dstWidthOffsetY, oWidthDataSize);
        }

        if (other.dstWidthOffsetZ) {
            this->dstWidthOffsetZ = new int [this->width];
            memcpy(this->dstWidthOffsetZ, other.dstWidthOffsetZ, oWidthDataSize);
        }

        if (other.dstWidthOffsetA) {
            this->dstWidthOffsetA = new int [this->width];
            memcpy(this->dstWidthOffsetA, other.dstWidthOffsetA, oWidthDataSize);
        }
    }
}

FillParameters::~FillParameters()
{
    this->clearBuffers();
}

FillParameters &FillParameters::operator =(const FillParameters &other)
{
    if (this != &other) {
        this->colorConvert = other.colorConvert;
        this->fillType = other.fillType;
        this->fillDataTypes = other.fillDataTypes;
        this->alphaMode = other.alphaMode;
        this->endianess = other.endianess;
        this->width = other.width;
        this->height = other.height;
        this->planeXo = other.planeXo;
        this->planeYo = other.planeYo;
        this->planeZo = other.planeZo;
        this->planeAo = other.planeAo;
        this->compXo = other.compXo;
        this->compYo = other.compYo;
        this->compZo = other.compZo;
        this->compAo = other.compAo;
        this->xoOffset = other.xoOffset;
        this->yoOffset = other.yoOffset;
        this->zoOffset = other.zoOffset;
        this->aoOffset = other.aoOffset;
        this->xoShift = other.xoShift;
        this->yoShift = other.yoShift;
        this->zoShift = other.zoShift;
        this->aoShift = other.aoShift;
        this->maskXo = other.maskXo;
        this->maskYo = other.maskYo;
        this->maskZo = other.maskZo;
        this->maskAo = other.maskAo;

        if (this->width > 0) {
            size_t oWidthDataSize = sizeof(int) * this->width;

            if (other.dstWidthOffsetX) {
                this->dstWidthOffsetX = new int [this->width];
                memcpy(this->dstWidthOffsetX, other.dstWidthOffsetX, oWidthDataSize);
            }

            if (other.dstWidthOffsetY) {
                this->dstWidthOffsetY = new int [this->width];
                memcpy(this->dstWidthOffsetY, other.dstWidthOffsetY, oWidthDataSize);
            }

            if (other.dstWidthOffsetZ) {
                this->dstWidthOffsetZ = new int [this->width];
                memcpy(this->dstWidthOffsetZ, other.dstWidthOffsetZ, oWidthDataSize);
            }

            if (other.dstWidthOffsetA) {
                this->dstWidthOffsetA = new int [this->width];
                memcpy(this->dstWidthOffsetA, other.dstWidthOffsetA, oWidthDataSize);
            }
        }
    }

    return *this;
}

void FillParameters::clearBuffers()
{
    if (this->dstWidthOffsetX) {
        delete [] this->dstWidthOffsetX;
        this->dstWidthOffsetX = nullptr;
    }

    if (this->dstWidthOffsetY) {
        delete [] this->dstWidthOffsetY;
        this->dstWidthOffsetY = nullptr;
    }

    if (this->dstWidthOffsetZ) {
        delete [] this->dstWidthOffsetZ;
        this->dstWidthOffsetZ = nullptr;
    }

    if (this->dstWidthOffsetA) {
        delete [] this->dstWidthOffsetA;
        this->dstWidthOffsetA = nullptr;
    }
}

void FillParameters::allocateBuffers(const AkVideoCaps &caps)
{
    this->clearBuffers();

    if (caps.width() > 0) {
        this->dstWidthOffsetX = new int [caps.width()];
        this->dstWidthOffsetY = new int [caps.width()];
        this->dstWidthOffsetZ = new int [caps.width()];
        this->dstWidthOffsetA = new int [caps.width()];
    }
}

#define DEFINE_FILL_TYPES(size) \
    if (ospecs.depth() == size) \
        this->fillDataTypes = FillDataTypes_##size;

void FillParameters::configure(const AkVideoCaps &caps,
                               AkColorConvert &colorConvert)
{
    auto ispecs = AkVideoCaps::formatSpecs(AkVideoCaps::Format_argbpack);
    auto ospecs = AkVideoCaps::formatSpecs(caps.format());

    DEFINE_FILL_TYPES(8);
    DEFINE_FILL_TYPES(16);
    DEFINE_FILL_TYPES(32);

    auto components = ospecs.mainComponents();

    switch (components) {
    case 3:
        this->fillType =
            ospecs.type() == AkVideoFormatSpec::VFT_RGB?
                                FillType_Vector:
                                FillType_3;

        break;

    case 1:
        this->fillType = FillType_1;

        break;

    default:
        break;
    }

    this->endianess = ospecs.endianness();
    colorConvert.loadMatrix(ispecs, ospecs);

    switch (ospecs.type()) {
    case AkVideoFormatSpec::VFT_RGB:
        this->planeXo = ospecs.componentPlane(AkColorComponent::CT_R);
        this->planeYo = ospecs.componentPlane(AkColorComponent::CT_G);
        this->planeZo = ospecs.componentPlane(AkColorComponent::CT_B);

        this->compXo = ospecs.component(AkColorComponent::CT_R);
        this->compYo = ospecs.component(AkColorComponent::CT_G);
        this->compZo = ospecs.component(AkColorComponent::CT_B);

        break;

    case AkVideoFormatSpec::VFT_YUV:
        this->planeXo = ospecs.componentPlane(AkColorComponent::CT_Y);
        this->planeYo = ospecs.componentPlane(AkColorComponent::CT_U);
        this->planeZo = ospecs.componentPlane(AkColorComponent::CT_V);

        this->compXo = ospecs.component(AkColorComponent::CT_Y);
        this->compYo = ospecs.component(AkColorComponent::CT_U);
        this->compZo = ospecs.component(AkColorComponent::CT_V);

        break;

    case AkVideoFormatSpec::VFT_Gray:
        this->planeXo = ospecs.componentPlane(AkColorComponent::CT_Y);
        this->compXo = ospecs.component(AkColorComponent::CT_Y);

        break;

    default:
        break;
    }

    this->planeAo = ospecs.componentPlane(AkColorComponent::CT_A);
    this->compAo = ospecs.component(AkColorComponent::CT_A);

    this->xoOffset = this->compXo.offset();
    this->yoOffset = this->compYo.offset();
    this->zoOffset = this->compZo.offset();
    this->aoOffset = this->compAo.offset();

    this->xoShift = this->compXo.shift();
    this->yoShift = this->compYo.shift();
    this->zoShift = this->compZo.shift();
    this->aoShift = this->compAo.shift();

    this->maskXo = ~(this->compXo.max<quint64>() << this->compXo.shift());
    this->maskYo = ~(this->compYo.max<quint64>() << this->compYo.shift());
    this->maskZo = ~(this->compZo.max<quint64>() << this->compZo.shift());
    this->maskAo = ~(this->compAo.max<quint64>() << this->compAo.shift());

    this->alphaMode = ospecs.contains(AkColorComponent::CT_A)?
                          AlphaMode_AO:
                          AlphaMode_O;
}

void FillParameters::configureFill(const AkVideoCaps &caps)
{
    this->allocateBuffers(caps);

    for (int x = 0; x < caps.width(); ++x) {
        this->dstWidthOffsetX[x] = (x >> this->compXo.widthDiv()) * this->compXo.step();
        this->dstWidthOffsetY[x] = (x >> this->compYo.widthDiv()) * this->compYo.step();
        this->dstWidthOffsetZ[x] = (x >> this->compZo.widthDiv()) * this->compZo.step();
        this->dstWidthOffsetA[x] = (x >> this->compAo.widthDiv()) * this->compAo.step();
    }

    this->width = caps.width();
    this->height = caps.height();
}

void FillParameters::reset()
{
    this->fillType = FillType_3;
    this->fillDataTypes = FillDataTypes_8;
    this->alphaMode = AlphaMode_AO;

    this->endianess = Q_BYTE_ORDER;

    this->clearBuffers();

    this->width = 0;
    this->height = 0;

    this->planeXo = 0;
    this->planeYo = 0;
    this->planeZo = 0;
    this->planeAo = 0;

    this->compXo = {};
    this->compYo = {};
    this->compZo = {};
    this->compAo = {};

    this->xoOffset = 0;
    this->yoOffset = 0;
    this->zoOffset = 0;
    this->aoOffset = 0;

    this->xoShift = 0;
    this->yoShift = 0;
    this->zoShift = 0;
    this->aoShift = 0;

    this->maskXo = 0;
    this->maskYo = 0;
    this->maskZo = 0;
    this->maskAo = 0;
}

#include "moc_akvideopacket.cpp"