Esempio n. 1
0
        public void writeMBQpDelta(MEncoder encoder, MBType prevMbType, int mbQpDelta)
        {
            int ctx = 60;

            ctx += prevMbType == null || prevMbType == MBType.I_PCM || (prevMbType != MBType.I_16x16 && prevCBP == 0) ||
                   prevMbQpDelta == 0 ? 0 : 1;

            prevMbQpDelta = mbQpDelta;
            if (mbQpDelta-- == 0)
            {
                encoder.encodeBin(ctx, 0);
            }
            else
            {
                encoder.encodeBin(ctx, 1);
                if (mbQpDelta-- == 0)
                {
                    encoder.encodeBin(62, 0);
                }
                else
                {
                    while (mbQpDelta-- > 0)
                    {
                        encoder.encodeBin(63, 1);
                    }
                    encoder.encodeBin(63, 0);
                }
            }
        }
Esempio n. 2
0
        public int readMBQpDelta(MDecoder decoder, MBType prevMbType)
        {
            int ctx = 60;

            ctx += prevMbType == null || prevMbType == MBType.I_PCM || (prevMbType != MBType.I_16x16 && prevCBP == 0) ||
                   prevMbQpDelta == 0 ? 0 : 1;

            int val = 0;

            if (decoder.decodeBin(ctx) == 1)
            {
                val++;
                if (decoder.decodeBin(62) == 1)
                {
                    val++;
                    while (decoder.decodeBin(63) == 1)
                    {
                        val++;
                    }
                }
            }
            prevMbQpDelta = Utility.golomb2Signed(val);

            return(prevMbQpDelta);
        }
Esempio n. 3
0
        public int readIntraChromaPredMode(MDecoder decoder, int mbX, MBType left, MBType top, bool leftAvailable,
                                           bool topAvailable)
        {
            int ctx = 64;

            ctx += !leftAvailable || left == null || !left.isIntra() || chromaPredModeLeft == 0 ? 0 : 1;
            ctx += !topAvailable || top == null || !top.isIntra() || chromaPredModeTop[mbX] == 0 ? 0 : 1;
            int mode;

            if (decoder.decodeBin(ctx) == 0)
            {
                mode = 0;
            }
            else if (decoder.decodeBin(67) == 0)
            {
                mode = 1;
            }
            else if (decoder.decodeBin(67) == 0)
            {
                mode = 2;
            }
            else
            {
                mode = 3;
            }
            chromaPredModeLeft = chromaPredModeTop[mbX] = mode;

            return(mode);
        }
Esempio n. 4
0
        public void writeMBTypeI(MEncoder encoder, MBType left, MBType top, bool leftAvailable, bool topAvailable,
                                 int mbType)
        {
            int ctx = 3;

            ctx += !leftAvailable || left == MBType.I_NxN ? 0 : 1;
            ctx += !topAvailable || top == MBType.I_NxN ? 0 : 1;

            if (mbType == 0)
            {
                encoder.encodeBin(ctx, 0);
            }
            else
            {
                encoder.encodeBin(ctx, 1);
                if (mbType == 25)
                {
                    encoder.encodeBinFinal(1);
                }
                else
                {
                    encoder.encodeBinFinal(0);
                    writeMBType16x16(encoder, mbType - 1);
                }
            }
        }
Esempio n. 5
0
        public VLC getCoeffTokenVLCForLuma(bool leftAvailable, MBType leftMBType, int leftToken, bool topAvailable,
                                           MBType topMBType, int topToken)
        {
            int nc = codeTableLuma(leftAvailable, leftMBType, leftToken, topAvailable, topMBType, topToken);

            return(H264Const.coeffToken[Math.Min(nc, 8)]);
        }
Esempio n. 6
0
        public void writeLumaDCBlock(BitWriter outb, int blkIndX, int blkIndY, MBType leftMBType, MBType topMBType,
                                     int[] coeff, VLC[] totalZerosTab, int firstCoeff, int maxCoeff, int[] scan)
        {
            VLC coeffTokenTab = getCoeffTokenVLCForLuma(blkIndX != 0, leftMBType, tokensLeft[blkIndY & mbMask],
                                                        blkIndY != 0, topMBType, tokensTop[blkIndX]);

            writeBlockGen(outb, coeff, totalZerosTab, firstCoeff, maxCoeff, scan, coeffTokenTab);
        }
Esempio n. 7
0
        public void readLumaDCBlock(BitReader reader, int[] coeff, int mbX, bool leftAvailable, MBType leftMbType,
                                    bool topAvailable, MBType topMbType, int[] zigzag4x4)
        {
            VLC coeffTokenTab = getCoeffTokenVLCForLuma(leftAvailable, leftMbType, tokensLeft[0], topAvailable, topMbType,
                                                        tokensTop[mbX << 2]);

            readCoeffs(reader, coeffTokenTab, H264Const.totalZeros16, coeff, 0, 16, zigzag4x4);
        }
Esempio n. 8
0
        public bool readTransform8x8Flag(MDecoder mDecoder, bool leftAvailable, bool topAvailable,
                                         MBType leftType, MBType topType, bool is8x8Left, bool is8x8Top)
        {
            int ctx = 399 + (leftAvailable && leftType != null && is8x8Left ? 1 : 0)
                      + (topAvailable && topType != null && is8x8Top ? 1 : 0);

            return(mDecoder.decodeBin(ctx) == 1);
        }
Esempio n. 9
0
        public int readACBlock(BitReader reader, int[] coeff, int blkIndX, int blkIndY, bool leftAvailable,
                               MBType leftMbType, bool topAvailable, MBType topMbType, int firstCoeff, int nCoeff, int[] zigzag4x4)
        {
            VLC coeffTokenTab = getCoeffTokenVLCForLuma(leftAvailable, leftMbType, tokensLeft[blkIndY & mbMask],
                                                        topAvailable, topMbType, tokensTop[blkIndX]);

            int readCoeffsc = readCoeffs(reader, coeffTokenTab, H264Const.totalZeros16, coeff, firstCoeff, nCoeff, zigzag4x4);

            tokensLeft[blkIndY & mbMask] = tokensTop[blkIndX] = readCoeffsc;

            return(totalCoeff(readCoeffsc));
        }
Esempio n. 10
0
        public int readCodedBlockFlagChromaDC(MDecoder decoder, int mbX, int comp, MBType left, MBType top,
                                              bool leftAvailable, bool topAvailable, int leftCBPChroma, int topCBPChroma, MBType cur)
        {
            int tLeft = condTerm(cur, leftAvailable, left, left != null && leftCBPChroma != 0, codedBlkDCLeft[comp]);
            int tTop  = condTerm(cur, topAvailable, top, top != null && topCBPChroma != 0, codedBlkDCTop[comp][mbX]);

            int decoded = decoder.decodeBin(BlockType.CHROMA_DC.codedBlockCtxOff + tLeft + 2 * tTop);

            codedBlkDCLeft[comp]     = decoded;
            codedBlkDCTop[comp][mbX] = decoded;

            return(decoded);
        }
Esempio n. 11
0
        public int readCodedBlockFlagLumaDC(MDecoder decoder, int mbX, MBType left, MBType top, bool leftAvailable,
                                            bool topAvailable, MBType cur)
        {
            int tLeft = condTerm(cur, leftAvailable, left, left == MBType.I_16x16, codedBlkDCLeft[0]);
            int tTop  = condTerm(cur, topAvailable, top, top == MBType.I_16x16, codedBlkDCTop[0][mbX]);

            int decoded = decoder.decodeBin(BlockType.LUMA_16_DC.codedBlockCtxOff + tLeft + 2 * tTop);

            codedBlkDCLeft[0]     = decoded;
            codedBlkDCTop[0][mbX] = decoded;

            return(decoded);
        }
Esempio n. 12
0
        public void writeIntraChromaPredMode(MEncoder encoder, int mbX, MBType left, MBType top, bool leftAvailable,
                                             bool topAvailable, int mode)
        {
            int ctx = 64;

            ctx += !leftAvailable || !left.isIntra() || chromaPredModeLeft == 0 ? 0 : 1;
            ctx += !topAvailable || !top.isIntra() || chromaPredModeTop[mbX] == 0 ? 0 : 1;
            encoder.encodeBin(ctx, mode-- == 0 ? 0 : 1);
            for (int i = 0; mode >= 0 && i < 2; i++)
            {
                encoder.encodeBin(67, mode-- == 0 ? 0 : 1);
            }
            chromaPredModeLeft = chromaPredModeTop[mbX] = mode;
        }
Esempio n. 13
0
        public int readRefIdx(MDecoder mDecoder, bool leftAvailable, bool topAvailable, MBType leftType,
                              MBType topType, H264Const.PartPred leftPred, H264Const.PartPred topPred, H264Const.PartPred curPred, int mbX, int partX, int partY,
                              int partW, int partH, int list)
        {
            int partAbsX = (mbX << 2) + partX;

            bool predEqA = leftPred != null && leftPred != H264Const.PartPred.Direct &&
                           (leftPred == H264Const.PartPred.Bi || leftPred == curPred || (curPred == H264Const.PartPred.Bi && leftPred.usesList(list)));
            bool predEqB = topPred != null && topPred != H264Const.PartPred.Direct &&
                           (topPred == H264Const.PartPred.Bi || topPred == curPred || (curPred == H264Const.PartPred.Bi && topPred.usesList(list)));

            int ctA = !leftAvailable || leftType == null || leftType.isIntra() || !predEqA || refIdxLeft[list][partY] == 0 ? 0
                : 1;
            int ctB = !topAvailable || topType == null || topType.isIntra() || !predEqB || refIdxTop[list][partAbsX] == 0 ? 0
                : 1;
            int b0 = mDecoder.decodeBin(54 + ctA + 2 * ctB);
            int val;

            if (b0 == 0)
            {
                val = 0;
            }
            else
            {
                int b1 = mDecoder.decodeBin(58);
                if (b1 == 0)
                {
                    val = 1;
                }
                else
                {
                    for (val = 2; mDecoder.decodeBin(59) == 1; val++)
                    {
                        ;
                    }
                }
            }

            for (int i = 0; i < partW; i++)
            {
                refIdxTop[list][partAbsX + i] = val;
            }

            for (int i = 0; i < partH; i++)
            {
                refIdxLeft[list][partY + i] = val;
            }

            return(val);
        }
Esempio n. 14
0
        public int readMBTypeB(MDecoder mDecoder, MBType left, MBType top, bool leftAvailable, bool topAvailable)
        {
            int ctx = 27;

            ctx += !leftAvailable || left == null || left == MBType.B_Direct_16x16 ? 0 : 1;
            ctx += !topAvailable || top == null || top == MBType.B_Direct_16x16 ? 0 : 1;

            if (mDecoder.decodeBin(ctx) == 0)
            {
                return(0); // B Direct
            }
            if (mDecoder.decodeBin(30) == 0)
            {
                return(1 + mDecoder.decodeBin(32));
            }

            int b1 = mDecoder.decodeBin(31);

            if (b1 == 0)
            {
                return(3 + ((mDecoder.decodeBin(32) << 2) | (mDecoder.decodeBin(32) << 1) | mDecoder.decodeBin(32)));
            }
            else
            {
                if (mDecoder.decodeBin(32) == 0)
                {
                    return(12 + ((mDecoder.decodeBin(32) << 2) | (mDecoder.decodeBin(32) << 1) | mDecoder.decodeBin(32)));
                }
                else
                {
                    switch ((mDecoder.decodeBin(32) << 1) + mDecoder.decodeBin(32))
                    {
                    case 0:
                        return(20 + mDecoder.decodeBin(32));

                    case 1:
                        return(23 + readIntraP(mDecoder, 32));

                    case 2:
                        return(11);

                    case 3:
                        return(22);
                    }
                }
            }

            return(0);
        }
Esempio n. 15
0
        public int codedBlockPatternIntra(MDecoder mDecoder, bool leftAvailable, bool topAvailable, int cbpLeft,
                                          int cbpTop, MBType mbLeft, MBType mbTop)
        {
            int cbp0 = mDecoder.decodeBin(73 + condTerm(leftAvailable, mbLeft, (cbpLeft >> 1) & 1) + 2
                                          * condTerm(topAvailable, mbTop, (cbpTop >> 2) & 1));
            int cbp1 = mDecoder.decodeBin(73 + (1 - cbp0) + 2 * condTerm(topAvailable, mbTop, (cbpTop >> 3) & 1));
            int cbp2 = mDecoder.decodeBin(73 + condTerm(leftAvailable, mbLeft, (cbpLeft >> 3) & 1) + 2 * (1 - cbp0));
            int cbp3 = mDecoder.decodeBin(73 + (1 - cbp2) + 2 * (1 - cbp1));

            int cr0 = mDecoder.decodeBin(77 + condTermCr0(leftAvailable, mbLeft, cbpLeft >> 4) + 2
                                         * condTermCr0(topAvailable, mbTop, cbpTop >> 4));
            int cr1 = cr0 != 0 ? mDecoder.decodeBin(81 + condTermCr1(leftAvailable, mbLeft, cbpLeft >> 4) + 2
                                                    * condTermCr1(topAvailable, mbTop, cbpTop >> 4)) : 0;

            return(cbp0 | (cbp1 << 1) | (cbp2 << 2) | (cbp3 << 3) | (cr0 << 4) | (cr1 << 5));
        }
Esempio n. 16
0
 public int condTerm(MBType mbCur, bool nAvb, MBType mbN, bool nBlkAvb, int cbpN)
 {
     if (!nAvb)
     {
         return(mbCur.isIntra() ? 1 : 0);
     }
     if (mbN == MBType.I_PCM)
     {
         return(1);
     }
     if (!nBlkAvb)
     {
         return(0);
     }
     return(cbpN);
 }
Esempio n. 17
0
        public int readMBTypeI(MDecoder decoder, MBType left, MBType top, bool leftAvailable, bool topAvailable)
        {
            int ctx = 3;

            ctx += !leftAvailable || left == MBType.I_NxN ? 0 : 1;
            ctx += !topAvailable || top == MBType.I_NxN ? 0 : 1;

            if (decoder.decodeBin(ctx) == 0)
            {
                return(0);
            }
            else
            {
                return(decoder.decodeFinalBin() == 1 ? 25 : 1 + readMBType16x16(decoder));
            }
        }
Esempio n. 18
0
            private Frame init(int[][] buffer, MemoryStream segment, NALUnit marker)
            {
                firstNu = marker;

                shr = new SliceHeaderReader();
                BitReader br = new BitReader(segment.duplicate());

                firstSliceHeader = shr.readPart1(br);
                activePps        = vdecoder.pps[firstSliceHeader.pic_parameter_set_id];
                activeSps        = vdecoder.sps[activePps.seq_parameter_set_id];
                shr.readPart2(firstSliceHeader, marker, activeSps, activePps, br);
                int picWidthInMbs  = activeSps.pic_width_in_mbs_minus1 + 1;
                int picHeightInMbs = Utility.getPicHeightInMbs(activeSps);

                int[][] nCoeff = (int[][])Array.CreateInstance(typeof(int), new int[] { picHeightInMbs << 2, picWidthInMbs << 2 }); //new int[picHeightInMbs << 2][picWidthInMbs << 2];

                mvs = (int[][][][])Array.CreateInstance(typeof(int), new int[] { 2, picHeightInMbs << 2, picWidthInMbs << 2, 3 });  //new int[2][picHeightInMbs << 2][picWidthInMbs << 2][3];
                MBType[]      mbTypes   = new MBType[picHeightInMbs * picWidthInMbs];
                bool[]        tr8x8Used = new bool[picHeightInMbs * picWidthInMbs];
                int[][]       mbQps     = (int[][])Array.CreateInstance(typeof(int), new int[] { 3, picHeightInMbs * picWidthInMbs });//new int[3][picHeightInMbs * picWidthInMbs];
                SliceHeader[] shs       = new SliceHeader[picHeightInMbs * picWidthInMbs];
                Frame[][][]   refsUsed  = new Frame[picHeightInMbs * picWidthInMbs][][];

                if (vdecoder.sRefs == null)
                {
                    vdecoder.sRefs = new Frame[1 << (firstSliceHeader.sps.log2_max_frame_num_minus4 + 4)];
                    vdecoder.lRefs = new Dictionary <int, Frame>();
                }

                Frame result = createFrame(activeSps, buffer, firstSliceHeader.frame_num, mvs, refsUsed,
                                           vdecoder.poc.calcPOC(firstSliceHeader, firstNu));

                decoder = new SliceDecoder(activeSps, activePps, nCoeff, mvs, mbTypes, mbQps, shs, tr8x8Used, refsUsed,
                                           result, vdecoder.sRefs, vdecoder.lRefs);
                decoder.setDebug(vdecoder.debug);

                filter = new DeblockingFilter(picWidthInMbs, activeSps.bit_depth_chroma_minus8 + 8, nCoeff, mvs, mbTypes,
                                              mbQps, shs, tr8x8Used, refsUsed);

                return(result);
            }
Esempio n. 19
0
        protected int codeTableLuma(bool leftAvailable, MBType leftMBType, int leftToken, bool topAvailable,
                                    MBType topMBType, int topToken)
        {
            int nA = leftMBType == null ? 0 : totalCoeff(leftToken);
            int nB = topMBType == null ? 0 : totalCoeff(topToken);

            if (leftAvailable && topAvailable)
            {
                return((nA + nB + 1) >> 1);
            }
            else if (leftAvailable)
            {
                return(nA);
            }
            else if (topAvailable)
            {
                return(nB);
            }
            else
            {
                return(0);
            }
        }
Esempio n. 20
0
        public int readCodedBlockFlagLumaAC(MDecoder decoder, BlockType blkType, int blkX, int blkY, int comp, MBType left,
                                            MBType top, bool leftAvailable, bool topAvailable, int leftCBPLuma, int topCBPLuma, int curCBPLuma,
                                            MBType cur)
        {
            int blkOffLeft = blkX & 3, blkOffTop = blkY & 3;

            int tLeft;

            if (blkOffLeft == 0)
            {
                tLeft = condTerm(cur, leftAvailable, left, left != null && left != MBType.I_PCM && cbp(leftCBPLuma, 3, blkOffTop),
                                 codedBlkLeft[comp][blkOffTop]);
            }
            else
            {
                tLeft = condTerm(cur, true, cur, cbp(curCBPLuma, blkOffLeft - 1, blkOffTop), codedBlkLeft[comp][blkOffTop]);
            }

            int tTop;

            if (blkOffTop == 0)
            {
                tTop = condTerm(cur, topAvailable, top, top != null && top != MBType.I_PCM && cbp(topCBPLuma, blkOffLeft, 3),
                                codedBlkTop[comp][blkX]);
            }
            else
            {
                tTop = condTerm(cur, true, cur, cbp(curCBPLuma, blkOffLeft, blkOffTop - 1), codedBlkTop[comp][blkX]);
            }

            int decoded = decoder.decodeBin(blkType.codedBlockCtxOff + tLeft + 2 * tTop);

            codedBlkLeft[comp][blkOffTop] = decoded;
            codedBlkTop[comp][blkX]       = decoded;

            return(decoded);
        }
Esempio n. 21
0
        public int readMVD(MDecoder decoder, int comp, bool leftAvailable, bool topAvailable, MBType leftType,
                           MBType topType, H264Const.PartPred leftPred, H264Const.PartPred topPred, H264Const.PartPred curPred, int mbX, int partX, int partY,
                           int partW, int partH, int list)
        {
            int ctx = comp == 0 ? 40 : 47;

            int partAbsX = (mbX << 2) + partX;

            bool predEqA = leftPred != null && leftPred != H264Const.PartPred.Direct &&
                           (leftPred == H264Const.PartPred.Bi || leftPred == curPred || (curPred == H264Const.PartPred.Bi && leftPred.usesList(list)));
            bool predEqB = topPred != null && topPred != H264Const.PartPred.Direct &&
                           (topPred == H264Const.PartPred.Bi || topPred == curPred || (curPred == H264Const.PartPred.Bi && topPred.usesList(list)));

            // prefix and suffix as given by UEG3 with signedValFlag=1, uCoff=9
            int absMvdComp = !leftAvailable || leftType == null || leftType.isIntra() || !predEqA ? 0 : Math
                             .Abs(mvdLeft[list][comp][partY]);

            absMvdComp += !topAvailable || topType == null || topType.isIntra() || !predEqB ? 0 : Math
                          .Abs(mvdTop[list][comp][partAbsX]);

            int val, b = decoder.decodeBin(ctx + (absMvdComp < 3 ? 0 : (absMvdComp > 32 ? 2 : 1)));

            for (val = 0; b != 0 && val < 8; val++)
            {
                b = decoder.decodeBin(Math.Min(ctx + val + 3, ctx + 6));
            }
            val += b;

            if (val != 0)
            {
                if (val == 9)
                {
                    int log = 2, add = 0, sum = 0, leftover = 0;
                    do
                    {
                        sum += leftover;
                        log++;
                        b        = decoder.decodeBinBypass();
                        leftover = 1 << log;
                    } while (b != 0);

                    --log;

                    for (; log >= 0; log--)
                    {
                        add |= decoder.decodeBinBypass() << log;
                    }
                    val += add + sum;
                }

                val = toSigned(val, -decoder.decodeBinBypass());
            }

            for (int i = 0; i < partW; i++)
            {
                mvdTop[list][comp][partAbsX + i] = val;
            }
            for (int i = 0; i < partH; i++)
            {
                mvdLeft[list][comp][partY + i] = val;
            }

            return(val);
        }
Esempio n. 22
0
 private int condTerm(bool avb, MBType mbt, int cbp)
 {
     return(!avb || mbt == MBType.I_PCM || (mbt != null && cbp == 1) ? 0 : 1);
 }
Esempio n. 23
0
 private int condTermCr1(bool avb, MBType mbt, int cbpChroma)
 {
     return(avb && (mbt == MBType.I_PCM || mbt != null && (cbpChroma & 2) != 0) ? 1 : 0);
 }
Esempio n. 24
0
        public int readCodedBlockFlagChromaAC(MDecoder decoder, int blkX, int blkY, int comp, MBType left, MBType top,
                                              bool leftAvailable, bool topAvailable, int leftCBPChroma, int topCBPChroma, MBType cur)
        {
            int blkOffLeft = blkX & 1, blkOffTop = blkY & 1;

            int tLeft;

            if (blkOffLeft == 0)
            {
                tLeft = condTerm(cur, leftAvailable, left, left != null && left != MBType.I_PCM && (leftCBPChroma & 2) != 0,
                                 codedBlkLeft[comp][blkOffTop]);
            }
            else
            {
                tLeft = condTerm(cur, true, cur, true, codedBlkLeft[comp][blkOffTop]);
            }
            int tTop;

            if (blkOffTop == 0)
            {
                tTop = condTerm(cur, topAvailable, top, top != null && top != MBType.I_PCM && (topCBPChroma & 2) != 0,
                                codedBlkTop[comp][blkX]);
            }
            else
            {
                tTop = condTerm(cur, true, cur, true, codedBlkTop[comp][blkX]);
            }

            int decoded = decoder.decodeBin(BlockType.CHROMA_AC.codedBlockCtxOff + tLeft + 2 * tTop);

            codedBlkLeft[comp][blkOffTop] = decoded;
            codedBlkTop[comp][blkX]       = decoded;

            return(decoded);
        }