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 pic_queue.c
33 *
34 * \brief Recycled piture queue implementation
35 *
36 * \date 03/13/2009 Created
37 *
38 *************************************************************************************
39 */
40 #include "pic_queue.h"
41 #include "decoder_context.h"
42 #include "codec_def.h"
43 #include "memory_align.h"
44
45 namespace WelsDec {
46
47 void FreePicture (PPicture pPic, CMemoryAlign* pMa);
48
49
50 ///////////////////////////////////Recycled queue management for pictures///////////////////////////////////
51 /* ______________________________________
52 -->| P0 | P1 | P2 | P3 | P4 | .. | Pn-1 |-->
53 --------------------------------------
54 *
55 * How does it work?
56 * node <- next; ++ next;
57 *
58 */
59
60
61
AllocPicture(PWelsDecoderContext pCtx,const int32_t kiPicWidth,const int32_t kiPicHeight)62 PPicture AllocPicture (PWelsDecoderContext pCtx, const int32_t kiPicWidth, const int32_t kiPicHeight) {
63 PPicture pPic = NULL;
64 int32_t iPicWidth = 0;
65 int32_t iPicHeight = 0;
66
67 int32_t iPicChromaWidth = 0;
68 int32_t iPicChromaHeight = 0;
69 int32_t iLumaSize = 0;
70 int32_t iChromaSize = 0;
71 CMemoryAlign* pMa = pCtx->pMemAlign;
72
73 pPic = (PPicture) pMa->WelsMallocz (sizeof (SPicture), "PPicture");
74 WELS_VERIFY_RETURN_IF (NULL, NULL == pPic);
75
76 memset (pPic, 0, sizeof (SPicture));
77
78 iPicWidth = WELS_ALIGN (kiPicWidth + (PADDING_LENGTH << 1), PICTURE_RESOLUTION_ALIGNMENT);
79 iPicHeight = WELS_ALIGN (kiPicHeight + (PADDING_LENGTH << 1), PICTURE_RESOLUTION_ALIGNMENT);
80 iPicChromaWidth = iPicWidth >> 1;
81 iPicChromaHeight = iPicHeight >> 1;
82
83 iLumaSize = iPicWidth * iPicHeight;
84 iChromaSize = iPicChromaWidth * iPicChromaHeight;
85
86 if (pCtx->pParam->bParseOnly) {
87 pPic->pBuffer[0] = pPic->pBuffer[1] = pPic->pBuffer[2] = NULL;
88 pPic->pData[0] = pPic->pData[1] = pPic->pData[2] = NULL;
89 pPic->iLinesize[0] = iPicWidth;
90 pPic->iLinesize[1] = pPic->iLinesize[2] = iPicChromaWidth;
91 } else {
92 pPic->pBuffer[0] = static_cast<uint8_t*> (pMa->WelsMallocz (iLumaSize /* luma */
93 + (iChromaSize << 1) /* Cb,Cr */, "_pic->buffer[0]"));
94 WELS_VERIFY_RETURN_PROC_IF (NULL, NULL == pPic->pBuffer[0], FreePicture (pPic, pMa));
95
96 memset (pPic->pBuffer[0], 128, (iLumaSize + (iChromaSize << 1)));
97 pPic->iLinesize[0] = iPicWidth;
98 pPic->iLinesize[1] = pPic->iLinesize[2] = iPicChromaWidth;
99 pPic->pBuffer[1] = pPic->pBuffer[0] + iLumaSize;
100 pPic->pBuffer[2] = pPic->pBuffer[1] + iChromaSize;
101 pPic->pData[0] = pPic->pBuffer[0] + (1 + pPic->iLinesize[0]) * PADDING_LENGTH;
102 pPic->pData[1] = pPic->pBuffer[1] + /*WELS_ALIGN*/ (((1 + pPic->iLinesize[1]) * PADDING_LENGTH) >> 1);
103 pPic->pData[2] = pPic->pBuffer[2] + /*WELS_ALIGN*/ (((1 + pPic->iLinesize[2]) * PADDING_LENGTH) >> 1);
104 }
105 pPic->iPlanes = 3; // yv12 in default
106 pPic->iWidthInPixel = kiPicWidth;
107 pPic->iHeightInPixel = kiPicHeight;
108 pPic->iFrameNum = -1;
109 pPic->iRefCount = 0;
110
111 uint32_t uiMbWidth = (kiPicWidth + 15) >> 4;
112 uint32_t uiMbHeight = (kiPicHeight + 15) >> 4;
113 uint32_t uiMbCount = uiMbWidth * uiMbHeight;
114
115 pPic->pMbCorrectlyDecodedFlag = (bool*)pMa->WelsMallocz (uiMbCount * sizeof (bool), "pPic->pMbCorrectlyDecodedFlag");
116 pPic->pNzc = GetThreadCount (pCtx) > 1 ? (int8_t (*)[24])pMa->WelsMallocz (uiMbCount * 24, "pPic->pNzc") : NULL;
117 pPic->pMbType = (uint32_t*)pMa->WelsMallocz (uiMbCount * sizeof (uint32_t), "pPic->pMbType");
118 pPic->pMv[LIST_0] = (int16_t (*)[16][2])pMa->WelsMallocz (uiMbCount * sizeof (
119 int16_t) * MV_A * MB_BLOCK4x4_NUM, "pPic->pMv[]");
120 pPic->pMv[LIST_1] = (int16_t (*)[16][2])pMa->WelsMallocz (uiMbCount * sizeof (
121 int16_t) * MV_A * MB_BLOCK4x4_NUM, "pPic->pMv[]");
122 pPic->pRefIndex[LIST_0] = (int8_t (*)[16])pMa->WelsMallocz (uiMbCount * sizeof (
123 int8_t) * MB_BLOCK4x4_NUM, "pCtx->sMb.pRefIndex[]");
124 pPic->pRefIndex[LIST_1] = (int8_t (*)[16])pMa->WelsMallocz (uiMbCount * sizeof (
125 int8_t) * MB_BLOCK4x4_NUM, "pCtx->sMb.pRefIndex[]");
126 if (pCtx->pThreadCtx != NULL) {
127 pPic->pReadyEvent = (SWelsDecEvent*)pMa->WelsMallocz (uiMbHeight * sizeof (SWelsDecEvent), "pPic->pReadyEvent");
128 for (uint32_t i = 0; i < uiMbHeight; ++i) {
129 CREATE_EVENT (&pPic->pReadyEvent[i], 1, 0, NULL);
130 }
131 } else {
132 pPic->pReadyEvent = NULL;
133 }
134
135 return pPic;
136 }
137
FreePicture(PPicture pPic,CMemoryAlign * pMa)138 void FreePicture (PPicture pPic, CMemoryAlign* pMa) {
139 if (NULL != pPic) {
140 if (pPic->pBuffer[0]) {
141 pMa->WelsFree (pPic->pBuffer[0], "pPic->pBuffer[0]");
142 pPic->pBuffer[0] = NULL;
143 }
144
145 if (pPic->pMbCorrectlyDecodedFlag) {
146 pMa->WelsFree (pPic->pMbCorrectlyDecodedFlag, "pPic->pMbCorrectlyDecodedFlag");
147 pPic->pMbCorrectlyDecodedFlag = NULL;
148 }
149
150 if (pPic->pNzc) {
151 pMa->WelsFree (pPic->pNzc, "pPic->pNzc");
152 pPic->pNzc = NULL;
153 }
154
155 if (pPic->pMbType) {
156 pMa->WelsFree (pPic->pMbType, "pPic->pMbType");
157 pPic->pMbType = NULL;
158 }
159
160 for (int32_t listIdx = LIST_0; listIdx < LIST_A; ++listIdx) {
161 if (pPic->pMv[listIdx]) {
162 pMa->WelsFree (pPic->pMv[listIdx], "pPic->pMv[]");
163 pPic->pMv[listIdx] = NULL;
164 }
165
166 if (pPic->pRefIndex[listIdx]) {
167 pMa->WelsFree (pPic->pRefIndex[listIdx], "pPic->pRefIndex[]");
168 pPic->pRefIndex[listIdx] = NULL;
169 }
170 }
171 if (pPic->pReadyEvent != NULL) {
172 uint32_t uiMbHeight = (pPic->iHeightInPixel + 15) >> 4;
173 for (uint32_t i = 0; i < uiMbHeight; ++i) {
174 CLOSE_EVENT (&pPic->pReadyEvent[i]);
175 }
176 pMa->WelsFree (pPic->pReadyEvent, "pPic->pReadyEvent");
177 pPic->pReadyEvent = NULL;
178 }
179 pMa->WelsFree (pPic, "pPic");
180 pPic = NULL;
181 }
182 }
PrefetchPic(PPicBuff pPicBuf)183 PPicture PrefetchPic (PPicBuff pPicBuf) {
184 int32_t iPicIdx = 0;
185 PPicture pPic = NULL;
186
187 if (pPicBuf->iCapacity == 0) {
188 return NULL;
189 }
190
191 for (iPicIdx = pPicBuf->iCurrentIdx + 1; iPicIdx < pPicBuf->iCapacity ; ++iPicIdx) {
192 if (pPicBuf->ppPic[iPicIdx] != NULL && !pPicBuf->ppPic[iPicIdx]->bUsedAsRef
193 && pPicBuf->ppPic[iPicIdx]->iRefCount <= 0) {
194 pPic = pPicBuf->ppPic[iPicIdx];
195 break;
196 }
197 }
198 if (pPic != NULL) {
199 pPicBuf->iCurrentIdx = iPicIdx;
200 pPic->iPicBuffIdx = iPicIdx;
201 return pPic;
202 }
203 for (iPicIdx = 0 ; iPicIdx <= pPicBuf->iCurrentIdx ; ++iPicIdx) {
204 if (pPicBuf->ppPic[iPicIdx] != NULL && !pPicBuf->ppPic[iPicIdx]->bUsedAsRef
205 && pPicBuf->ppPic[iPicIdx]->iRefCount <= 0) {
206 pPic = pPicBuf->ppPic[iPicIdx];
207 break;
208 }
209 }
210
211 pPicBuf->iCurrentIdx = iPicIdx;
212 if (pPic != NULL) {
213 pPic->iPicBuffIdx = iPicIdx;
214 }
215 return pPic;
216 }
217
PrefetchPicForThread(PPicBuff pPicBuf)218 PPicture PrefetchPicForThread (PPicBuff pPicBuf) {
219 PPicture pPic = NULL;
220
221 if (pPicBuf->iCapacity == 0) {
222 return NULL;
223 }
224 pPic = pPicBuf->ppPic[pPicBuf->iCurrentIdx];
225 pPic->iPicBuffIdx = pPicBuf->iCurrentIdx;
226 if (++pPicBuf->iCurrentIdx >= pPicBuf->iCapacity) {
227 pPicBuf->iCurrentIdx = 0;
228 }
229 return pPic;
230 }
231
PrefetchLastPicForThread(PPicBuff pPicBuf,const int32_t & iLastPicBuffIdx)232 PPicture PrefetchLastPicForThread (PPicBuff pPicBuf, const int32_t& iLastPicBuffIdx) {
233 PPicture pPic = NULL;
234
235 if (pPicBuf->iCapacity == 0) {
236 return NULL;
237 }
238 if (iLastPicBuffIdx >= 0 && iLastPicBuffIdx < pPicBuf->iCapacity) {
239 pPic = pPicBuf->ppPic[iLastPicBuffIdx];
240 }
241 return pPic;
242 }
243
244 } // namespace WelsDec
245