1 /*****************************************************************************
2 
3 INTEL CORPORATION PROPRIETARY INFORMATION
4 This software is supplied under the terms of a license agreement or
5 nondisclosure agreement with Intel Corporation and may not be copied
6 or disclosed except in accordance with the terms of that agreement.
7 Copyright(c) 2005-2014 Intel Corporation. All Rights Reserved.
8 
9 *****************************************************************************/
10 
11 #include "common_utils.h"
12 
13 // =================================================================
14 // Utility functions, not directly tied to Intel Media SDK functionality
15 //
16 
PrintErrString(int err,const char * filestr,int line)17 void PrintErrString(int err, const char *filestr, int line)
18 {
19 	switch (err) {
20 	case 0:
21 		printf("\n No error.\n");
22 		break;
23 	case -1:
24 		printf("\n Unknown error: %s %d\n", filestr, line);
25 		break;
26 	case -2:
27 		printf("\n Null pointer.  Check filename/path + permissions? %s %d\n",
28 		       filestr, line);
29 		break;
30 	case -3:
31 		printf("\n Unsupported feature/library load error. %s %d\n",
32 		       filestr, line);
33 		break;
34 	case -4:
35 		printf("\n Could not allocate memory. %s %d\n", filestr, line);
36 		break;
37 	case -5:
38 		printf("\n Insufficient IO buffers. %s %d\n", filestr, line);
39 		break;
40 	case -6:
41 		printf("\n Invalid handle. %s %d\n", filestr, line);
42 		break;
43 	case -7:
44 		printf("\n Memory lock failure. %s %d\n", filestr, line);
45 		break;
46 	case -8:
47 		printf("\n Function called before initialization. %s %d\n",
48 		       filestr, line);
49 		break;
50 	case -9:
51 		printf("\n Specified object not found. %s %d\n", filestr, line);
52 		break;
53 	case -10:
54 		printf("\n More input data expected. %s %d\n", filestr, line);
55 		break;
56 	case -11:
57 		printf("\n More output surfaces expected. %s %d\n", filestr,
58 		       line);
59 		break;
60 	case -12:
61 		printf("\n Operation aborted. %s %d\n", filestr, line);
62 		break;
63 	case -13:
64 		printf("\n HW device lost. %s %d\n", filestr, line);
65 		break;
66 	case -14:
67 		printf("\n Incompatible video parameters. %s %d\n", filestr,
68 		       line);
69 		break;
70 	case -15:
71 		printf("\n Invalid video parameters. %s %d\n", filestr, line);
72 		break;
73 	case -16:
74 		printf("\n Undefined behavior. %s %d\n", filestr, line);
75 		break;
76 	case -17:
77 		printf("\n Device operation failure. %s %d\n", filestr, line);
78 		break;
79 	case -18:
80 		printf("\n More bitstream data expected. %s %d\n", filestr,
81 		       line);
82 		break;
83 	case -19:
84 		printf("\n Incompatible audio parameters. %s %d\n", filestr,
85 		       line);
86 		break;
87 	case -20:
88 		printf("\n Invalid audio parameters. %s %d\n", filestr, line);
89 		break;
90 	default:
91 		printf("\nError code %d,\t%s\t%d\n\n", err, filestr, line);
92 	}
93 }
94 
ReadPlaneData(mfxU16 w,mfxU16 h,mfxU8 * buf,mfxU8 * ptr,mfxU16 pitch,mfxU16 offset,FILE * fSource)95 mfxStatus ReadPlaneData(mfxU16 w, mfxU16 h, mfxU8 *buf, mfxU8 *ptr,
96 			mfxU16 pitch, mfxU16 offset, FILE *fSource)
97 {
98 	mfxU32 nBytesRead;
99 	for (mfxU16 i = 0; i < h; i++) {
100 		nBytesRead = (mfxU32)fread(buf, 1, w, fSource);
101 		if (w != nBytesRead)
102 			return MFX_ERR_MORE_DATA;
103 		for (mfxU16 j = 0; j < w; j++)
104 			ptr[i * pitch + j * 2 + offset] = buf[j];
105 	}
106 	return MFX_ERR_NONE;
107 }
108 
LoadRawFrame(mfxFrameSurface1 * pSurface,FILE * fSource)109 mfxStatus LoadRawFrame(mfxFrameSurface1 *pSurface, FILE *fSource)
110 {
111 	if (!fSource) {
112 		// Simulate instantaneous access to 1000 "empty" frames.
113 		static int frameCount = 0;
114 		if (1000 == frameCount++)
115 			return MFX_ERR_MORE_DATA;
116 		else
117 			return MFX_ERR_NONE;
118 	}
119 
120 	mfxStatus sts = MFX_ERR_NONE;
121 	mfxU32 nBytesRead;
122 	mfxU16 w, h, i, pitch;
123 	mfxU8 *ptr;
124 	mfxFrameInfo *pInfo = &pSurface->Info;
125 	mfxFrameData *pData = &pSurface->Data;
126 
127 	if (pInfo->CropH > 0 && pInfo->CropW > 0) {
128 		w = pInfo->CropW;
129 		h = pInfo->CropH;
130 	} else {
131 		w = pInfo->Width;
132 		h = pInfo->Height;
133 	}
134 
135 	pitch = pData->Pitch;
136 	ptr = pData->Y + pInfo->CropX + pInfo->CropY * pData->Pitch;
137 
138 	// read luminance plane
139 	for (i = 0; i < h; i++) {
140 		nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, w, fSource);
141 		if (w != nBytesRead)
142 			return MFX_ERR_MORE_DATA;
143 	}
144 
145 	mfxU8 buf[2048]; // maximum supported chroma width for nv12
146 	w /= 2;
147 	h /= 2;
148 	ptr = pData->UV + pInfo->CropX + (pInfo->CropY / 2) * pitch;
149 	if (w > 2048)
150 		return MFX_ERR_UNSUPPORTED;
151 
152 	// load U
153 	sts = ReadPlaneData(w, h, buf, ptr, pitch, 0, fSource);
154 	if (MFX_ERR_NONE != sts)
155 		return sts;
156 	// load V
157 	sts = ReadPlaneData(w, h, buf, ptr, pitch, 1, fSource);
158 	if (MFX_ERR_NONE != sts)
159 		return sts;
160 
161 	return MFX_ERR_NONE;
162 }
163 
LoadRawRGBFrame(mfxFrameSurface1 * pSurface,FILE * fSource)164 mfxStatus LoadRawRGBFrame(mfxFrameSurface1 *pSurface, FILE *fSource)
165 {
166 	if (!fSource) {
167 		// Simulate instantaneous access to 1000 "empty" frames.
168 		static int frameCount = 0;
169 		if (1000 == frameCount++)
170 			return MFX_ERR_MORE_DATA;
171 		else
172 			return MFX_ERR_NONE;
173 	}
174 
175 	size_t nBytesRead;
176 	mfxU16 w, h;
177 	mfxFrameInfo *pInfo = &pSurface->Info;
178 
179 	if (pInfo->CropH > 0 && pInfo->CropW > 0) {
180 		w = pInfo->CropW;
181 		h = pInfo->CropH;
182 	} else {
183 		w = pInfo->Width;
184 		h = pInfo->Height;
185 	}
186 
187 	for (mfxU16 i = 0; i < h; i++) {
188 		nBytesRead = fread(pSurface->Data.B + i * pSurface->Data.Pitch,
189 				   1, w * 4, fSource);
190 		if ((size_t)(w * 4) != nBytesRead)
191 			return MFX_ERR_MORE_DATA;
192 	}
193 
194 	return MFX_ERR_NONE;
195 }
196 
WriteBitStreamFrame(mfxBitstream * pMfxBitstream,FILE * fSink)197 mfxStatus WriteBitStreamFrame(mfxBitstream *pMfxBitstream, FILE *fSink)
198 {
199 	mfxU32 nBytesWritten =
200 		(mfxU32)fwrite(pMfxBitstream->Data + pMfxBitstream->DataOffset,
201 			       1, pMfxBitstream->DataLength, fSink);
202 	if (nBytesWritten != pMfxBitstream->DataLength)
203 		return MFX_ERR_UNDEFINED_BEHAVIOR;
204 
205 	pMfxBitstream->DataLength = 0;
206 
207 	return MFX_ERR_NONE;
208 }
209 
ReadBitStreamData(mfxBitstream * pBS,FILE * fSource)210 mfxStatus ReadBitStreamData(mfxBitstream *pBS, FILE *fSource)
211 {
212 	memmove(pBS->Data, pBS->Data + pBS->DataOffset, pBS->DataLength);
213 	pBS->DataOffset = 0;
214 
215 	mfxU32 nBytesRead = (mfxU32)fread(pBS->Data + pBS->DataLength, 1,
216 					  pBS->MaxLength - pBS->DataLength,
217 					  fSource);
218 
219 	if (0 == nBytesRead)
220 		return MFX_ERR_MORE_DATA;
221 
222 	pBS->DataLength += nBytesRead;
223 
224 	return MFX_ERR_NONE;
225 }
226 
WriteSection(mfxU8 * plane,mfxU16 factor,mfxU16 chunksize,mfxFrameInfo * pInfo,mfxFrameData * pData,mfxU32 i,mfxU32 j,FILE * fSink)227 mfxStatus WriteSection(mfxU8 *plane, mfxU16 factor, mfxU16 chunksize,
228 		       mfxFrameInfo *pInfo, mfxFrameData *pData, mfxU32 i,
229 		       mfxU32 j, FILE *fSink)
230 {
231 	if (chunksize != fwrite(plane +
232 					(pInfo->CropY * pData->Pitch / factor +
233 					 pInfo->CropX) +
234 					i * pData->Pitch + j,
235 				1, chunksize, fSink))
236 		return MFX_ERR_UNDEFINED_BEHAVIOR;
237 	return MFX_ERR_NONE;
238 }
239 
WriteRawFrame(mfxFrameSurface1 * pSurface,FILE * fSink)240 mfxStatus WriteRawFrame(mfxFrameSurface1 *pSurface, FILE *fSink)
241 {
242 	mfxFrameInfo *pInfo = &pSurface->Info;
243 	mfxFrameData *pData = &pSurface->Data;
244 	mfxU32 i, j, h, w;
245 	mfxStatus sts = MFX_ERR_NONE;
246 
247 	for (i = 0; i < pInfo->CropH; i++)
248 		sts = WriteSection(pData->Y, 1, pInfo->CropW, pInfo, pData, i,
249 				   0, fSink);
250 
251 	h = pInfo->CropH / 2;
252 	w = pInfo->CropW;
253 	for (i = 0; i < h; i++)
254 		for (j = 0; j < w; j += 2)
255 			sts = WriteSection(pData->UV, 2, 1, pInfo, pData, i, j,
256 					   fSink);
257 	for (i = 0; i < h; i++)
258 		for (j = 1; j < w; j += 2)
259 			sts = WriteSection(pData->UV, 2, 1, pInfo, pData, i, j,
260 					   fSink);
261 
262 	return sts;
263 }
264 
GetFreeTaskIndex(Task * pTaskPool,mfxU16 nPoolSize)265 int GetFreeTaskIndex(Task *pTaskPool, mfxU16 nPoolSize)
266 {
267 	if (pTaskPool)
268 		for (int i = 0; i < nPoolSize; i++)
269 			if (!pTaskPool[i].syncp)
270 				return i;
271 	return MFX_ERR_NOT_FOUND;
272 }
273 
ClearYUVSurfaceSysMem(mfxFrameSurface1 * pSfc,mfxU16 width,mfxU16 height)274 void ClearYUVSurfaceSysMem(mfxFrameSurface1 *pSfc, mfxU16 width, mfxU16 height)
275 {
276 	// In case simulating direct access to frames we initialize the allocated surfaces with default pattern
277 	memset(pSfc->Data.Y, 100, width * height);      // Y plane
278 	memset(pSfc->Data.U, 50, (width * height) / 2); // UV plane
279 }
280 
281 // Get free raw frame surface
GetFreeSurfaceIndex(mfxFrameSurface1 ** pSurfacesPool,mfxU16 nPoolSize)282 int GetFreeSurfaceIndex(mfxFrameSurface1 **pSurfacesPool, mfxU16 nPoolSize)
283 {
284 	if (pSurfacesPool)
285 		for (mfxU16 i = 0; i < nPoolSize; i++)
286 			if (0 == pSurfacesPool[i]->Data.Locked)
287 				return i;
288 	return MFX_ERR_NOT_FOUND;
289 }
290 
mfxFrameTypeString(mfxU16 FrameType)291 char mfxFrameTypeString(mfxU16 FrameType)
292 {
293 	mfxU8 FrameTmp = FrameType & 0xF;
294 	char FrameTypeOut;
295 	switch (FrameTmp) {
296 	case MFX_FRAMETYPE_I:
297 		FrameTypeOut = 'I';
298 		break;
299 	case MFX_FRAMETYPE_P:
300 		FrameTypeOut = 'P';
301 		break;
302 	case MFX_FRAMETYPE_B:
303 		FrameTypeOut = 'B';
304 		break;
305 	default:
306 		FrameTypeOut = '*';
307 	}
308 	return FrameTypeOut;
309 }
310