| 1 | /***************************************************************************** |
| 2 | * Copyright (C) 2013 x265 project |
| 3 | * |
| 4 | * Authors: Gopu Govindaswamy <gopu@multicorewareinc.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA. |
| 19 | * |
| 20 | * This program is also available under a commercial proprietary license. |
| 21 | * For more information, contact us at license @ x265.com. |
| 22 | *****************************************************************************/ |
| 23 | |
| 24 | #ifndef X265_LOWRES_H |
| 25 | #define X265_LOWRES_H |
| 26 | |
| 27 | #include "primitives.h" |
| 28 | #include "common.h" |
| 29 | #include "picyuv.h" |
| 30 | #include "mv.h" |
| 31 | |
| 32 | namespace x265 { |
| 33 | // private namespace |
| 34 | |
| 35 | struct ReferencePlanes |
| 36 | { |
| 37 | ReferencePlanes() { memset(this, 0, sizeof(ReferencePlanes)); } |
| 38 | |
| 39 | pixel* fpelPlane[3]; |
| 40 | pixel* lowresPlane[4]; |
| 41 | PicYuv* reconPic; |
| 42 | |
| 43 | bool isWeighted; |
| 44 | bool isLowres; |
| 45 | |
| 46 | intptr_t lumaStride; |
| 47 | intptr_t chromaStride; |
| 48 | |
| 49 | struct { |
| 50 | int weight; |
| 51 | int offset; |
| 52 | int shift; |
| 53 | int round; |
| 54 | } w[3]; |
| 55 | |
| 56 | pixel* getLumaAddr(uint32_t ctuAddr, uint32_t absPartIdx) { return fpelPlane[0] + reconPic->m_cuOffsetY[ctuAddr] + reconPic->m_buOffsetY[absPartIdx]; } |
| 57 | pixel* getCbAddr(uint32_t ctuAddr, uint32_t absPartIdx) { return fpelPlane[1] + reconPic->m_cuOffsetC[ctuAddr] + reconPic->m_buOffsetC[absPartIdx]; } |
| 58 | pixel* getCrAddr(uint32_t ctuAddr, uint32_t absPartIdx) { return fpelPlane[2] + reconPic->m_cuOffsetC[ctuAddr] + reconPic->m_buOffsetC[absPartIdx]; } |
| 59 | |
| 60 | /* lowres motion compensation, you must provide a buffer and stride for QPEL averaged pixels |
| 61 | * in case QPEL is required. Else it returns a pointer to the HPEL pixels */ |
| 62 | inline pixel *lowresMC(intptr_t blockOffset, const MV& qmv, pixel *buf, intptr_t& outstride) |
| 63 | { |
| 64 | if ((qmv.x | qmv.y) & 1) |
| 65 | { |
| 66 | int hpelA = (qmv.y & 2) | ((qmv.x & 2) >> 1); |
| 67 | pixel *frefA = lowresPlane[hpelA] + blockOffset + (qmv.x >> 2) + (qmv.y >> 2) * lumaStride; |
| 68 | int qmvx = qmv.x + (qmv.x & 1); |
| 69 | int qmvy = qmv.y + (qmv.y & 1); |
| 70 | int hpelB = (qmvy & 2) | ((qmvx & 2) >> 1); |
| 71 | pixel *frefB = lowresPlane[hpelB] + blockOffset + (qmvx >> 2) + (qmvy >> 2) * lumaStride; |
| 72 | primitives.pixelavg_pp[LUMA_8x8](buf, outstride, frefA, lumaStride, frefB, lumaStride, 32); |
| 73 | return buf; |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | outstride = lumaStride; |
| 78 | int hpel = (qmv.y & 2) | ((qmv.x & 2) >> 1); |
| 79 | return lowresPlane[hpel] + blockOffset + (qmv.x >> 2) + (qmv.y >> 2) * lumaStride; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | inline int lowresQPelCost(pixel *fenc, intptr_t blockOffset, const MV& qmv, pixelcmp_t comp) |
| 84 | { |
| 85 | if ((qmv.x | qmv.y) & 1) |
| 86 | { |
| 87 | ALIGN_VAR_16(pixel, subpelbuf[8 * 8]); |
| 88 | int hpelA = (qmv.y & 2) | ((qmv.x & 2) >> 1); |
| 89 | pixel *frefA = lowresPlane[hpelA] + blockOffset + (qmv.x >> 2) + (qmv.y >> 2) * lumaStride; |
| 90 | int qmvx = qmv.x + (qmv.x & 1); |
| 91 | int qmvy = qmv.y + (qmv.y & 1); |
| 92 | int hpelB = (qmvy & 2) | ((qmvx & 2) >> 1); |
| 93 | pixel *frefB = lowresPlane[hpelB] + blockOffset + (qmvx >> 2) + (qmvy >> 2) * lumaStride; |
| 94 | primitives.pixelavg_pp[LUMA_8x8](subpelbuf, 8, frefA, lumaStride, frefB, lumaStride, 32); |
| 95 | return comp(fenc, FENC_STRIDE, subpelbuf, 8); |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | int hpel = (qmv.y & 2) | ((qmv.x & 2) >> 1); |
| 100 | pixel *fref = lowresPlane[hpel] + blockOffset + (qmv.x >> 2) + (qmv.y >> 2) * lumaStride; |
| 101 | return comp(fenc, FENC_STRIDE, fref, lumaStride); |
| 102 | } |
| 103 | } |
| 104 | }; |
| 105 | |
| 106 | /* lowres buffers, sizes and strides */ |
| 107 | struct Lowres : public ReferencePlanes |
| 108 | { |
| 109 | pixel *buffer[4]; |
| 110 | |
| 111 | int frameNum; // Presentation frame number |
| 112 | int sliceType; // Slice type decided by lookahead |
| 113 | int width; // width of lowres frame in pixels |
| 114 | int lines; // height of lowres frame in pixel lines |
| 115 | int leadingBframes; // number of leading B frames for P or I |
| 116 | |
| 117 | bool bIntraCalculated; |
| 118 | bool bScenecut; // Set to false if the frame cannot possibly be part of a real scenecut. |
| 119 | bool bKeyframe; |
| 120 | bool bLastMiniGopBFrame; |
| 121 | |
| 122 | /* lookahead output data */ |
| 123 | int64_t costEst[X265_BFRAME_MAX + 2][X265_BFRAME_MAX + 2]; |
| 124 | int64_t costEstAq[X265_BFRAME_MAX + 2][X265_BFRAME_MAX + 2]; |
| 125 | int32_t* rowSatds[X265_BFRAME_MAX + 2][X265_BFRAME_MAX + 2]; |
| 126 | int intraMbs[X265_BFRAME_MAX + 2]; |
| 127 | int32_t* intraCost; |
| 128 | uint8_t* intraMode; |
| 129 | int64_t satdCost; |
| 130 | uint16_t* lowresCostForRc; |
| 131 | uint16_t(*lowresCosts[X265_BFRAME_MAX + 2][X265_BFRAME_MAX + 2]); |
| 132 | int32_t* lowresMvCosts[2][X265_BFRAME_MAX + 1]; |
| 133 | MV* lowresMvs[2][X265_BFRAME_MAX + 1]; |
| 134 | |
| 135 | /* used for vbvLookahead */ |
| 136 | int plannedType[X265_LOOKAHEAD_MAX + 1]; |
| 137 | int64_t plannedSatd[X265_LOOKAHEAD_MAX + 1]; |
| 138 | int indB; |
| 139 | int bframes; |
| 140 | |
| 141 | /* rate control / adaptive quant data */ |
| 142 | double* qpAqOffset; // AQ QP offset values for each 16x16 CU |
| 143 | double* qpCuTreeOffset; // cuTree QP offset values for each 16x16 CU |
| 144 | int* invQscaleFactor; // qScale values for qp Aq Offsets |
| 145 | uint64_t wp_ssd[3]; // This is different than SSDY, this is sum(pixel^2) - sum(pixel)^2 for entire frame |
| 146 | uint64_t wp_sum[3]; |
| 147 | |
| 148 | /* cutree intermediate data */ |
| 149 | uint16_t* propagateCost; |
| 150 | double weightedCostDelta[X265_BFRAME_MAX + 2]; |
| 151 | |
| 152 | bool create(PicYuv *origPic, int _bframes, bool bAqEnabled); |
| 153 | void destroy(); |
| 154 | void init(PicYuv *origPic, int poc, int sliceType); |
| 155 | }; |
| 156 | } |
| 157 | |
| 158 | #endif // ifndef X265_LOWRES_H |