| 1 | /***************************************************************************** |
| 2 | * Copyright (C) 2013 x265 project |
| 3 | * |
| 4 | * Authors: Steve Borho <steve@borho.org> |
| 5 | * Deepthi Devaki <deepthidevaki@multicorewareinc.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA. |
| 20 | * |
| 21 | * This program is also available under a commercial proprietary license. |
| 22 | * For more information, contact us at license @ x265.com. |
| 23 | *****************************************************************************/ |
| 24 | |
| 25 | #include "common.h" |
| 26 | #include "primitives.h" |
| 27 | #include "slice.h" |
| 28 | #include "picyuv.h" |
| 29 | |
| 30 | #include "reference.h" |
| 31 | |
| 32 | using namespace x265; |
| 33 | |
| 34 | MotionReference::MotionReference() |
| 35 | { |
| 36 | m_weightBuffer = NULL; |
| 37 | } |
| 38 | |
| 39 | int MotionReference::init(PicYuv* recPic, WeightParam *w) |
| 40 | { |
| 41 | m_reconPic = recPic; |
| 42 | lumaStride = recPic->m_stride; |
| 43 | intptr_t startpad = recPic->m_lumaMarginY * lumaStride + recPic->m_lumaMarginX; |
| 44 | |
| 45 | /* directly reference the pre-extended integer pel plane */ |
| 46 | fpelPlane = recPic->m_picBuf[0] + startpad; |
| 47 | isWeighted = false; |
| 48 | |
| 49 | if (w) |
| 50 | { |
| 51 | if (!m_weightBuffer) |
| 52 | { |
| 53 | uint32_t numCUinHeight = (recPic->m_picHeight + g_maxCUSize - 1) / g_maxCUSize; |
| 54 | size_t padheight = (numCUinHeight * g_maxCUSize) + recPic->m_lumaMarginY * 2; |
| 55 | m_weightBuffer = X265_MALLOC(pixel, lumaStride * padheight); |
| 56 | if (!m_weightBuffer) |
| 57 | return -1; |
| 58 | } |
| 59 | |
| 60 | isWeighted = true; |
| 61 | weight = w->inputWeight; |
| 62 | offset = w->inputOffset * (1 << (X265_DEPTH - 8)); |
| 63 | shift = w->log2WeightDenom; |
| 64 | round = shift ? 1 << (shift - 1) : 0; |
| 65 | m_numWeightedRows = 0; |
| 66 | |
| 67 | /* use our buffer which will have weighted pixels written to it */ |
| 68 | fpelPlane = m_weightBuffer + startpad; |
| 69 | } |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | MotionReference::~MotionReference() |
| 75 | { |
| 76 | X265_FREE(m_weightBuffer); |
| 77 | } |
| 78 | |
| 79 | void MotionReference::applyWeight(int rows, int numRows) |
| 80 | { |
| 81 | rows = X265_MIN(rows, numRows); |
| 82 | if (m_numWeightedRows >= rows) |
| 83 | return; |
| 84 | int marginX = m_reconPic->m_lumaMarginX; |
| 85 | int marginY = m_reconPic->m_lumaMarginY; |
| 86 | pixel* src = (pixel*)m_reconPic->m_picOrg[0] + (m_numWeightedRows * (int)g_maxCUSize * lumaStride); |
| 87 | pixel* dst = fpelPlane + ((m_numWeightedRows * (int)g_maxCUSize) * lumaStride); |
| 88 | int width = m_reconPic->m_picWidth; |
| 89 | int height = ((rows - m_numWeightedRows) * g_maxCUSize); |
| 90 | if (rows == numRows) |
| 91 | height = ((m_reconPic->m_picHeight % g_maxCUSize) ? (m_reconPic->m_picHeight % g_maxCUSize) : g_maxCUSize); |
| 92 | |
| 93 | // Computing weighted CU rows |
| 94 | int correction = IF_INTERNAL_PREC - X265_DEPTH; // intermediate interpolation depth |
| 95 | int padwidth = (width + 15) & ~15; // weightp assembly needs even 16 byte widths |
| 96 | primitives.weight_pp(src, dst, lumaStride, padwidth, height, |
| 97 | weight, round << correction, shift + correction, offset); |
| 98 | |
| 99 | // Extending Left & Right |
| 100 | primitives.extendRowBorder(dst, lumaStride, width, height, marginX); |
| 101 | |
| 102 | // Extending Above |
| 103 | if (m_numWeightedRows == 0) |
| 104 | { |
| 105 | pixel *pixY = fpelPlane - marginX; |
| 106 | for (int y = 0; y < marginY; y++) |
| 107 | memcpy(pixY - (y + 1) * lumaStride, pixY, lumaStride * sizeof(pixel)); |
| 108 | } |
| 109 | |
| 110 | // Extending Bottom |
| 111 | if (rows == numRows) |
| 112 | { |
| 113 | pixel *pixY = fpelPlane - marginX + (m_reconPic->m_picHeight - 1) * lumaStride; |
| 114 | for (int y = 0; y < marginY; y++) |
| 115 | memcpy(pixY + (y + 1) * lumaStride, pixY, lumaStride * sizeof(pixel)); |
| 116 | } |
| 117 | m_numWeightedRows = rows; |
| 118 | } |