1 /* |
|
2 * Copyright (c) 2010 Ixonos Plc. |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - Initial contribution |
|
11 * |
|
12 * Contributors: |
|
13 * Ixonos Plc |
|
14 * |
|
15 * Description: |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 #include <string.h> |
|
21 #include "globals.h" |
|
22 #include "framebuffer.h" |
|
23 |
|
24 /* |
|
25 * Static functions |
|
26 */ |
|
27 |
|
28 static void *allocMem(int blkSize, size_t unitSize); |
|
29 |
|
30 static frmBuf_s *initRefFrame(frmBuf_s *recoBuf, frmBuf_s *refBuf); |
|
31 |
|
32 |
|
33 |
|
34 /* |
|
35 * |
|
36 * allocMem: |
|
37 * |
|
38 * Parameters: |
|
39 * blkSize Block size |
|
40 * unitSize unit size |
|
41 * |
|
42 * Function: |
|
43 * Allocate blkSize*unitSize bytes of memory |
|
44 * |
|
45 * Returns: |
|
46 * Pointer to allocated memory or NULL |
|
47 * |
|
48 */ |
|
49 static void *allocMem(int blkSize, size_t unitSize) |
|
50 { |
|
51 void *mem; |
|
52 |
|
53 if ((mem = User::Alloc(blkSize*unitSize)) == NULL) { |
|
54 PRINT((_L("Cannot allocate memory for frame\n"))); |
|
55 } |
|
56 |
|
57 return mem; |
|
58 } |
|
59 |
|
60 |
|
61 /* |
|
62 * |
|
63 * frmOpen: |
|
64 * |
|
65 * Parameters: |
|
66 * mbData Macroblock state buffer |
|
67 * width Width of the frame |
|
68 * height Height of the frame |
|
69 * |
|
70 * Function: |
|
71 * Allocate memory for frame buffer and macroblock state buffer. |
|
72 * |
|
73 * Returns: |
|
74 * Pointer to allocated frame buffer or NULL |
|
75 * |
|
76 */ |
|
77 frmBuf_s *frmOpen(mbAttributes_s **mbData, int width, int height) |
|
78 { |
|
79 // int numPels = width*height; |
|
80 int numBlksPerLine = width/BLK_SIZE; |
|
81 int numBlocks = numBlksPerLine*height/BLK_SIZE; |
|
82 int numMacroblocks = width/MBK_SIZE*height/MBK_SIZE; |
|
83 frmBuf_s *recoBuf; |
|
84 mbAttributes_s *mbDataTmp; |
|
85 |
|
86 if ((recoBuf = (frmBuf_s *)User::Alloc(sizeof(frmBuf_s))) == NULL) |
|
87 return NULL; |
|
88 |
|
89 memset(recoBuf, 0, sizeof(frmBuf_s)); |
|
90 |
|
91 recoBuf->width = width; |
|
92 recoBuf->height = height; |
|
93 |
|
94 if ((mbDataTmp = (mbAttributes_s *)User::Alloc(sizeof(mbAttributes_s))) == NULL) |
|
95 return NULL; |
|
96 |
|
97 memset(mbDataTmp, 0, sizeof(mbAttributes_s)); |
|
98 |
|
99 if ((mbDataTmp->sliceMap = (int *)allocMem(numMacroblocks, sizeof(int))) == NULL) |
|
100 return NULL; |
|
101 if ((mbDataTmp->mbTypeTable = (int8 *)allocMem(numMacroblocks, sizeof(int8))) == NULL) |
|
102 return NULL; |
|
103 if ((mbDataTmp->qpTable = (int8 *)allocMem(numMacroblocks, sizeof(int8))) == NULL) |
|
104 return NULL; |
|
105 if ((mbDataTmp->refIdxTable = (int8 *)allocMem(numBlocks, sizeof(int8))) == NULL) |
|
106 return NULL; |
|
107 if ((mbDataTmp->cbpTable = (int *)allocMem(numMacroblocks, sizeof(int))) == NULL) |
|
108 return NULL; |
|
109 if ((mbDataTmp->motVecTable = (motVec_s *)allocMem(numBlocks, sizeof(motVec_s))) == NULL) |
|
110 return NULL; |
|
111 if ((mbDataTmp->ipModesUpPred = (int8 *)allocMem(numBlksPerLine, sizeof(int8))) == NULL) |
|
112 return NULL; |
|
113 |
|
114 if ((mbDataTmp->numCoefUpPred[0] = (int8 *)allocMem(numBlksPerLine, sizeof(int8))) == NULL) |
|
115 return NULL; |
|
116 if ((mbDataTmp->numCoefUpPred[1] = (int8 *)allocMem(numBlksPerLine/2, sizeof(int8))) == NULL) |
|
117 return NULL; |
|
118 if ((mbDataTmp->numCoefUpPred[2] = (int8 *)allocMem(numBlksPerLine/2, sizeof(int8))) == NULL) |
|
119 return NULL; |
|
120 |
|
121 if ((mbDataTmp->filterModeTab = (int8 *)allocMem(numMacroblocks, sizeof(int8))) == NULL) |
|
122 return NULL; |
|
123 if ((mbDataTmp->alphaOffset = (int8 *)allocMem(numMacroblocks, sizeof(int8))) == NULL) |
|
124 return NULL; |
|
125 if ((mbDataTmp->betaOffset = (int8 *)allocMem(numMacroblocks, sizeof(int8))) == NULL) |
|
126 return NULL; |
|
127 |
|
128 *mbData = mbDataTmp; |
|
129 |
|
130 return recoBuf; |
|
131 } |
|
132 |
|
133 |
|
134 /* |
|
135 * |
|
136 * frmOpenRef: |
|
137 * |
|
138 * Parameters: |
|
139 * width Width of the frame |
|
140 * height Height of the frame |
|
141 * |
|
142 * Function: |
|
143 * Allocate memory for reference frame buffer |
|
144 * |
|
145 * Returns: |
|
146 * Pointer to Reference frame or NULL |
|
147 * |
|
148 */ |
|
149 frmBuf_s *frmOpenRef(int width, int height) |
|
150 { |
|
151 // int numPels = width*height; |
|
152 frmBuf_s *ref; |
|
153 |
|
154 if ((ref = (frmBuf_s *)User::Alloc(sizeof(frmBuf_s))) == NULL) |
|
155 return NULL; |
|
156 |
|
157 memset(ref, 0, sizeof(frmBuf_s)); |
|
158 |
|
159 ref->width = width; |
|
160 ref->height = height; |
|
161 ref->imgPadding = 0; |
|
162 |
|
163 ref->forOutput = 0; |
|
164 ref->refType = FRM_NON_REF_PIC; |
|
165 |
|
166 return ref; |
|
167 } |
|
168 |
|
169 |
|
170 /* |
|
171 * |
|
172 * frmClose: |
|
173 * |
|
174 * Parameters: |
|
175 * frame Frame |
|
176 * mbData MB state buffers |
|
177 * |
|
178 * Function: |
|
179 * Deallocate frame buffer and state array memory. |
|
180 * |
|
181 * Returns: |
|
182 * Nothing |
|
183 * |
|
184 */ |
|
185 void frmClose(frmBuf_s *recoBuf, mbAttributes_s *mbData) |
|
186 { |
|
187 if (!recoBuf) |
|
188 return; |
|
189 |
|
190 User::Free(mbData->sliceMap); |
|
191 User::Free(mbData->mbTypeTable); |
|
192 User::Free(mbData->qpTable); |
|
193 User::Free(mbData->refIdxTable); |
|
194 User::Free(mbData->cbpTable); |
|
195 User::Free(mbData->motVecTable); |
|
196 User::Free(mbData->ipModesUpPred); |
|
197 User::Free(mbData->numCoefUpPred[0]); |
|
198 User::Free(mbData->numCoefUpPred[1]); |
|
199 User::Free(mbData->numCoefUpPred[2]); |
|
200 User::Free(mbData->filterModeTab); |
|
201 User::Free(mbData->alphaOffset); |
|
202 User::Free(mbData->betaOffset); |
|
203 User::Free(recoBuf); |
|
204 User::Free(mbData); |
|
205 } |
|
206 |
|
207 |
|
208 /* |
|
209 * |
|
210 * frmCloseRef: |
|
211 * |
|
212 * Parameters: |
|
213 * ref Reference frame |
|
214 * |
|
215 * Function: |
|
216 * Deallocate reference frame buffer memory. |
|
217 * |
|
218 * Returns: |
|
219 * Nothing |
|
220 * |
|
221 */ |
|
222 void frmCloseRef(frmBuf_s *ref) |
|
223 { |
|
224 if (!ref) |
|
225 return; |
|
226 |
|
227 User::Free(ref); |
|
228 } |
|
229 |
|
230 |
|
231 /* |
|
232 * |
|
233 * initRefFrame: |
|
234 * |
|
235 * Parameters: |
|
236 * recoBuf Reconstruction frame |
|
237 * frameBuf Frame buffer to initialize |
|
238 * |
|
239 * Function: |
|
240 * Initialize reference frame buffer refBuf using reconstructed buffer |
|
241 * recoBuf. If width and height of the reference buffer do not those |
|
242 * of the reconstructed buffer, reference buffer is reallocated. |
|
243 * |
|
244 * Returns: |
|
245 * Pointer to reference frame |
|
246 * |
|
247 */ |
|
248 static frmBuf_s *initRefFrame(frmBuf_s *recoBuf, frmBuf_s *refBuf) |
|
249 { |
|
250 |
|
251 /* If pic size is different, reopen with new size */ |
|
252 if (!refBuf || refBuf->width != recoBuf->width || refBuf->height != recoBuf->height) { |
|
253 frmCloseRef(refBuf); |
|
254 if ((refBuf = frmOpenRef(recoBuf->width, recoBuf->height)) == NULL) |
|
255 return NULL; |
|
256 } |
|
257 |
|
258 /* Copy variables */ |
|
259 *refBuf = *recoBuf; |
|
260 |
|
261 return refBuf; |
|
262 } |
|
263 |
|
264 |
|
265 /* |
|
266 * |
|
267 * frmMakeRefFrame: |
|
268 * |
|
269 * Parameters: |
|
270 * recoBuf Reconstructed frame |
|
271 * refBuf Reference frame |
|
272 * |
|
273 * Function: |
|
274 * Generate reference frame refBuf using reconstructed frame recoBuf. |
|
275 * Function does not copy pixel data, but it simply swaps pointers. |
|
276 * |
|
277 * Returns: |
|
278 * Pointer to reference frame |
|
279 * |
|
280 */ |
|
281 frmBuf_s *frmMakeRefFrame(frmBuf_s *recoBuf, frmBuf_s *refBuf) |
|
282 { |
|
283 |
|
284 refBuf = initRefFrame(recoBuf, refBuf); |
|
285 if (refBuf == NULL) |
|
286 return NULL; |
|
287 |
|
288 return refBuf; |
|
289 } |
|