1 /*!
2 * \copy
3 * Copyright (c) 2009-2013, Cisco Systems
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 *
32 * \file picture_handle.c
33 *
34 * \brief picture pData handling
35 *
36 * \date 5/20/2009 Created
37 *
38 *************************************************************************************/
39 #include "picture_handle.h"
40 #include "svc_motion_estimate.h"
41
42 namespace WelsEnc {
43 /*!
44 * \brief alloc picture pData with borders for each plane based width and height of picture
45 * \param cx width of picture in pixels
46 * \param cy height of picture in pixels
47 * \param need_data need pData allocation
48 * \pram need_expand need borders expanding
49 * \return successful if effective picture pointer returned, otherwise failed with NULL
50 */
AllocPicture(CMemoryAlign * pMa,const int32_t kiWidth,const int32_t kiHeight,bool bNeedMbInfo,int32_t iNeedFeatureStorage)51 SPicture* AllocPicture (CMemoryAlign* pMa, const int32_t kiWidth , const int32_t kiHeight,
52 bool bNeedMbInfo, int32_t iNeedFeatureStorage) {
53 SPicture* pPic = NULL;
54 int32_t iPicWidth = 0;
55 int32_t iPicHeight = 0;
56
57 int32_t iPicChromaWidth = 0;
58 int32_t iPicChromaHeight = 0;
59 int32_t iLumaSize = 0;
60 int32_t iChromaSize = 0;
61
62 pPic = static_cast<SPicture*> (pMa->WelsMallocz (sizeof (SPicture), "pPic"));
63
64 WELS_VERIFY_RETURN_IF (NULL, NULL == pPic);
65
66 iPicWidth = WELS_ALIGN (kiWidth, MB_WIDTH_LUMA) + (PADDING_LENGTH << 1); // with width of horizon
67 iPicHeight = WELS_ALIGN (kiHeight, MB_HEIGHT_LUMA) + (PADDING_LENGTH << 1); // with height of vertical
68 iPicChromaWidth = iPicWidth >> 1;
69 iPicChromaHeight = iPicHeight >> 1;
70 iPicWidth = WELS_ALIGN (iPicWidth,
71 32); // 32(or 16 for chroma below) to match original imp. here instead of cache_line_size
72 iPicChromaWidth = WELS_ALIGN (iPicChromaWidth, 16);
73 iLumaSize = iPicWidth * iPicHeight;
74 iChromaSize = iPicChromaWidth * iPicChromaHeight;
75
76 pPic->pBuffer = (uint8_t*)pMa->WelsMalloc (iLumaSize /* luma */
77 + (iChromaSize << 1) /* Cb,Cr */
78 , "pPic->pBuffer");
79 WELS_VERIFY_RETURN_PROC_IF (NULL, NULL == pPic->pBuffer, FreePicture (pMa, &pPic));
80 pPic->iLineSize[0] = iPicWidth;
81 pPic->iLineSize[1] = pPic->iLineSize[2] = iPicChromaWidth;
82 pPic->pData[0] = pPic->pBuffer + (1 + pPic->iLineSize[0]) * PADDING_LENGTH;
83 pPic->pData[1] = pPic->pBuffer + iLumaSize + (((1 + pPic->iLineSize[1]) * PADDING_LENGTH) >> 1);
84 pPic->pData[2] = pPic->pBuffer + iLumaSize + iChromaSize + (((1 + pPic->iLineSize[2]) * PADDING_LENGTH) >> 1);
85
86 pPic->iWidthInPixel = kiWidth;
87 pPic->iHeightInPixel = kiHeight;
88 pPic->iFrameNum = -1;
89
90 pPic->bIsLongRef = false;
91 pPic->iLongTermPicNum = -1;
92 pPic->uiRecieveConfirmed = 0;
93 pPic->iMarkFrameNum = -1;
94
95 if (bNeedMbInfo) {
96 const uint32_t kuiCountMbNum = ((15 + kiWidth) >> 4) * ((15 + kiHeight) >> 4);
97
98 pPic->uiRefMbType = (uint32_t*)pMa->WelsMallocz (kuiCountMbNum * sizeof (uint32_t), "pPic->uiRefMbType");
99 WELS_VERIFY_RETURN_PROC_IF (NULL, NULL == pPic->uiRefMbType, FreePicture (pMa, &pPic));
100
101 pPic->pRefMbQp = (uint8_t*)pMa->WelsMallocz (kuiCountMbNum * sizeof (uint8_t), "pPic->pRefMbQp");
102 WELS_VERIFY_RETURN_PROC_IF (NULL, NULL == pPic->pRefMbQp, FreePicture (pMa, &pPic));
103
104 pPic->sMvList = static_cast<SMVUnitXY*> (pMa->WelsMallocz (kuiCountMbNum * sizeof (SMVUnitXY),
105 "pPic->sMvList"));
106 WELS_VERIFY_RETURN_PROC_IF (NULL, NULL == pPic->sMvList, FreePicture (pMa, &pPic));
107
108 pPic->pMbSkipSad = (int32_t*)pMa->WelsMallocz (kuiCountMbNum * sizeof (int32_t), "pPic->pMbSkipSad");
109 WELS_VERIFY_RETURN_PROC_IF (NULL, NULL == pPic->pMbSkipSad, FreePicture (pMa, &pPic));
110 }
111
112 if (iNeedFeatureStorage) {
113 pPic->pScreenBlockFeatureStorage = static_cast<SScreenBlockFeatureStorage*> (pMa->WelsMallocz (sizeof (
114 SScreenBlockFeatureStorage), "pScreenBlockFeatureStorage"));
115 int32_t iReturn = RequestScreenBlockFeatureStorage (pMa, kiWidth, kiHeight, iNeedFeatureStorage,
116 pPic->pScreenBlockFeatureStorage);
117 WELS_VERIFY_RETURN_PROC_IF (NULL, ENC_RETURN_SUCCESS != iReturn, FreePicture (pMa, &pPic));
118 } else {
119 pPic->pScreenBlockFeatureStorage = NULL;
120 }
121 return pPic;
122 }
123
124 /*!
125 * \brief free picture pData planes
126 * \param pPic picture pointer to be destoryed
127 * \return none
128 */
FreePicture(CMemoryAlign * pMa,SPicture ** ppPic)129 void FreePicture (CMemoryAlign* pMa, SPicture** ppPic) {
130 if (NULL != ppPic && NULL != *ppPic) {
131 SPicture* pPic = *ppPic;
132
133 if (NULL != pPic->pBuffer) {
134 pMa->WelsFree (pPic->pBuffer, "pPic->pBuffer");
135 pPic->pBuffer = NULL;
136 }
137 pPic->pBuffer = NULL;
138 pPic->pData[0] =
139 pPic->pData[1] =
140 pPic->pData[2] = NULL;
141 pPic->iLineSize[0] =
142 pPic->iLineSize[1] =
143 pPic->iLineSize[2] = 0;
144
145 pPic->iWidthInPixel = 0;
146 pPic->iHeightInPixel = 0;
147 pPic->iFrameNum = -1;
148
149 pPic->bIsLongRef = false;
150 pPic->uiRecieveConfirmed = 0;
151 pPic->iLongTermPicNum = -1;
152 pPic->iMarkFrameNum = -1;
153
154 if (pPic->uiRefMbType) {
155 pMa->WelsFree (pPic->uiRefMbType, "pPic->uiRefMbType");
156 pPic->uiRefMbType = NULL;
157 }
158 if (pPic->pRefMbQp) {
159 pMa->WelsFree (pPic->pRefMbQp, "pPic->pRefMbQp");
160 pPic->pRefMbQp = NULL;
161 }
162
163 if (pPic->sMvList) {
164 pMa->WelsFree (pPic->sMvList, "pPic->sMvList");
165 pPic->sMvList = NULL;
166 }
167 if (pPic->pMbSkipSad) {
168 pMa->WelsFree (pPic->pMbSkipSad, "pPic->pMbSkipSad");
169 pPic->pMbSkipSad = NULL;
170 }
171
172 if (pPic->pScreenBlockFeatureStorage) {
173 ReleaseScreenBlockFeatureStorage (pMa, pPic->pScreenBlockFeatureStorage);
174 pMa->WelsFree (pPic->pScreenBlockFeatureStorage, "pPic->pScreenBlockFeatureStorage");
175 pPic->pScreenBlockFeatureStorage = NULL;
176 }
177
178 pMa->WelsFree (*ppPic, "pPic");
179 *ppPic = NULL;
180 }
181 }
182
183 } // namespace WelsEnc
184
185