📄 macroblock.c
字号:
/*COPYRIGHT, LICENSE AND WARRANTY INFORMATIONThis software module has been originally developed by Nokia Corporation. Provided that a person, entity or a company willing to use the Software (hereinafter Licensee) comply with all the terms and conditions of this Statement and subject to the limitations set forth in this Statement Nokia grants to such Licensee a non-exclusive, sub-licensable, worldwide, limited license under copyrights owned by Nokia to use the Software for the sole purpose of creating, manufacturing, selling, marketing, or distributing (including the right to make modifications to the Software) a fully compliant decoder implementation (hereinafter "Decoder") of ITU-T Recommendation H.264 / ISO/IEC International Standard 14496-10 and an encoder implementation producing output that is decodable with the Decoder.Nokia retains the ownership of copyrights to the Software. There is no patent nor other intellectual property right of Nokia licensed under this Statement (except the copyright license above). Licensee hereby assumes sole responsibility to secure any other intellectual property rights needed, if any. For example, if patent licenses are required, it is their responsibility to acquire the license before utilizing the Software.The license by Nokia is subject to that the Licensee grants to Nokia the non-exclusive, worldwide, royalty-free, perpetual and irrevocable covenant that the Licensee(s) shall not bring a suit before any court or administrative agency or otherwise assert a claim for infringement under the Licensee intellectual property rights that, but for a license, would be infringed by the Software against (a) Nokia or Nokia's Affiliate; or (b) other recipient of a license and covenant not to sue with respect to the Software from Nokia; or (c) contractor, customer or distributor of a party listed above in a or b, which suit or claim is related to the Software or use thereof.The Licensee(s) further agrees to grant a reciprocal license to Nokia (as granted by Nokia to the Licensee(s) on the modifications made by Licensee(s) to the Software. THE SOFTWARE IS PROVIDED "AS IS" AND THE ORIGINAL DEVELOPER DISCLAIMS ANY AND ALL WARRANTIES WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. THOSE INTENDING TO USE THE SOFTWARE ARE EXPRESSLY ADVISED THAT ITS USE MAY INFRINGE EXISTING PATENTS AND BE SUBJECT TO ROYALTY PAYMENTS TO PATENT OWNERS. ANYONE USING THE SOFTWARE ON THE BASIS OF THIS LICENSE AGREES TO OBTAIN THE NECESSARY PERMISSIONS FROM ANY AND ALL APPLICABLE PATENT OWNERS FOR SUCH USE.IN NO EVENT SHALL THE ORIGINAL DEVELOPER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.This copyright, license and warranty information notice must be retained in all copies and derivative works of the Software or substantial portions thereof.*/#include "nccglob.h"#include "globals.h"#include "debug.h"#include "bitbuffer.h"#include "macroblock.h"#include "intrapred.h"#include "motcomp.h"#include "prederrordec.h"#include "loopfilter.h"#include "framebuffer.h"#include "vld.h"/* * Static functions */static int getMacroblock(macroblock_s *mb, int numRefFrames, int8 *ipTab, int8 **numCoefUpPred, int diffVecs[][2], int picType, int chromaQpIdx, bitbuffer_s *bitbuf);static int getMbAvailability(macroblock_s *mb, mbAttributes_s *mbData, int picWidth, int constrainedIntra);/* * * getMacroblock: * * Parameters: * mb Macroblock parameters * multRef 1 -> multiple reference frames used * ipTab Macroblock intra pred. modes * numCoefUpPred Block coefficient counts of upper MBs * diffVecs Macroblock delta motion vectors * picType Picture type (intra/inter) * chromaQpIdx Chroma QP index relative to luma QP * bitbuf Bitbuffer handle * * Function: * Get macroblock parameters from bitbuffer * * Returns: * MBK_OK for no error, MBK_ERROR for error * */static int getMacroblock(macroblock_s *mb, int numRefFrames, int8 *ipTab, int8 **numCoefUpPred, int diffVecs[][2], int picType, int chromaQpIdx, bitbuffer_s *bitbuf){ vldMBtype_s hdr; int numVecs; int delta_qp; int i; int8 *numCoefPtrY, *numCoefPtrU, *numCoefPtrV; int retCode; numCoefPtrY = &numCoefUpPred[0][mb->blkX]; numCoefPtrU = &numCoefUpPred[1][mb->blkX>>1]; numCoefPtrV = &numCoefUpPred[2][mb->blkX>>1]; /* * Get Macroblock type */ /* Check if we have to fetch run indicator */ if (IS_SLICE_P(picType) && mb->numSkipped < 0) { mb->numSkipped = vldGetRunIndicator(bitbuf); if (bibGetStatus(bitbuf) < 0) return MBK_ERROR; } if (IS_SLICE_P(picType) && mb->numSkipped > 0) { /* If skipped MBs, set MB to COPY */ mb->type = MBK_INTER; mb->interMode = MOT_COPY; mb->refNum[0] = 0; mb->cbpY = mb->cbpChromaDC = mb->cbpC = 0; mb->numSkipped -= 1; vldGetZeroLumaCoeffs(numCoefPtrY, mb->numCoefLeftPred); vldGetZeroChromaCoeffs(numCoefPtrU, numCoefPtrV, mb->numCoefLeftPredC); return MBK_OK; } else { if (vldGetMBtype(bitbuf, &hdr, picType) < 0) { deb0f(stderr, "Error: illegal MB type\n"); return MBK_ERROR; } mb->type = hdr.type; mb->intraType = hdr.intraType; mb->intraMode = hdr.intraMode; mb->interMode = hdr.interMode; for (i = 0; i < 4; i++) mb->inter8x8modes[i] = hdr.inter8x8modes[i]; mb->cbpY = hdr.cbpY; mb->cbpChromaDC = hdr.cbpChromaDC; mb->cbpC = hdr.cbpC; mb->numSkipped -= 1; } if (mb->type == MBK_INTRA && mb->intraType == MBK_INTRA_TYPE_PCM) { vldGetAllCoeffs(numCoefPtrY, numCoefPtrU, numCoefPtrV, mb->numCoefLeftPred, mb->numCoefLeftPredC); return MBK_OK; } /* * 4x4 intra prediction modes */ if (mb->type == MBK_INTRA && mb->intraType == MBK_INTRA_TYPE1) { if (vldGetIntraPred(bitbuf, ipTab) < 0) { deb0f(stderr, "Error: illegal intra pred\n"); return MBK_ERROR; } } /* * 8x8 chroma intra prediction mode */ if (mb->type == MBK_INTRA) { mb->intraModeChroma = vldGetChromaIntraPred(bitbuf); if (mb->intraModeChroma < 0) { deb0f(stderr, "Error: illegal chroma intra pred\n"); return MBK_ERROR; } } /* * Reference frame number and motion vectors */ if (mb->type == MBK_INTER) { numVecs = mcpGetNumMotVecs(mb->interMode, mb->inter8x8modes); retCode = vldGetMotVecs(bitbuf, mb->interMode, numRefFrames, mb->refNum, diffVecs, numVecs); if (retCode < 0) { deb0f(stderr, "Error: illegal motion vectors\n"); return MBK_ERROR; } } /* * Coded block pattern */ if (!(mb->type == MBK_INTRA && mb->intraType == MBK_INTRA_TYPE2)) { retCode = vldGetCBP(bitbuf, mb->type, &mb->cbpY, &mb->cbpChromaDC, &mb->cbpC); if (retCode < 0) { deb0f(stderr, "Error: illegal CBP\n"); return MBK_ERROR; } } /* Delta QP */ if ((mb->type == MBK_INTRA && mb->intraType == MBK_INTRA_TYPE2) || (mb->cbpY | mb->cbpChromaDC | mb->cbpC) != 0) { retCode = vldGetDeltaqp(bitbuf, &delta_qp); if (retCode < 0 || delta_qp < -(MAX_QP-MIN_QP+1)/2 || delta_qp >= (MAX_QP-MIN_QP+1)/2) { deb0f(stderr, "Error: illegal delta qp\n"); return MBK_ERROR; } if (delta_qp != 0) { int qp = mb->qp + delta_qp; if (qp < MIN_QP) qp += (MAX_QP-MIN_QP+1); if (qp > MAX_QP) qp -= (MAX_QP-MIN_QP+1); mb->qp = qp; mb->qpC = qpChroma[clip(MIN_QP, MAX_QP, mb->qp + chromaQpIdx)]; } } /* * Get transform coefficients */ /* * Luma DC coefficients (if 16x16 intra) */ if (mb->type == MBK_INTRA && mb->intraType == MBK_INTRA_TYPE2) { retCode = vldGetLumaDCcoeffs(bitbuf, mb->dcCoefY, numCoefPtrY, mb->numCoefLeftPred, mb->mbAvailBits); if (retCode < 0) { deb0f(stderr, "Error: illegal luma DC coefficient\n"); return MBK_ERROR; } } /* * Luma AC coefficients */ if (mb->cbpY != 0) { retCode = vldGetLumaCoeffs(bitbuf, mb->type, mb->intraType, &mb->cbpY, mb->coefY, numCoefPtrY, mb->numCoefLeftPred, mb->mbAvailBits); if (retCode < 0) { deb0f(stderr, "Error: illegal luma AC coefficient\n"); return MBK_ERROR; } } else vldGetZeroLumaCoeffs(numCoefPtrY, mb->numCoefLeftPred); /* * Chroma DC coefficients */ if (mb->cbpChromaDC != 0) { retCode = vldGetChromaDCcoeffs(bitbuf, mb->dcCoefC, &mb->cbpChromaDC); if (retCode < 0) { deb0f(stderr, "Error: illegal chroma DC coefficient\n"); return MBK_ERROR; } } /* * Chroma AC coefficients */ if (mb->cbpC != 0) { retCode = vldGetChromaCoeffs(bitbuf, mb->coefC, &mb->cbpC, numCoefPtrU, numCoefPtrV, mb->numCoefLeftPredC[0], mb->numCoefLeftPredC[1], mb->mbAvailBits); if (retCode < 0) { deb0f(stderr, "Error: illegal chroma AC coefficient\n"); return MBK_ERROR; } } else { vldGetZeroChromaCoeffs(numCoefPtrU, numCoefPtrV, mb->numCoefLeftPredC); } return MBK_OK;}/* * * mbkSetInitialQP: * * Parameters: * mb Macroblock object * qp Quantization parameter * chromaQpIdx Chroma QP index relative to luma QP * * Function: * Set macroblock qp. * * Returns: * - * */void mbkSetInitialQP(macroblock_s *mb, int qp, int chromaQpIdx){ mb->qp = qp; mb->qpC = qpChroma[clip(MIN_QP, MAX_QP, qp + chromaQpIdx)]; mb->numSkipped = -1;}/* * * recoPcmMb: * * Parameters: * reco Reconstruction frame * pixX MB horizontal position * pixY MB vertical position * width Picture width * bitbuf Bitbuffer handle * * Function: * Get PCM macroblock. * * Returns: * MBK_OK for ok, MBK_ERROR for error * */static int recoPcmMb(frmBuf_s *reco, int pixX, int pixY, int width, bitbuffer_s *bitbuf){ int x, y; u_int8 *recoPtr; int byteRet; if (bibGetNumRemainingBits(bitbuf) < 8*(3*MBK_SIZE*MBK_SIZE/2)) return MBK_ERROR; /* Get y */ recoPtr = reco->y + pixY*width + pixX; for (y = 0; y < MBK_SIZE; y++) { for (x = 0; x < MBK_SIZE; x++) { bibGetByte(bitbuf, &byteRet); recoPtr[y*width+x] = (u_int8)byteRet; } } pixX >>= 1; pixY >>= 1; width >>= 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -