1 /******************************************************************************\
2 Copyright (c) 2005-2019, Intel Corporation
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6 
7 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8 
9 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
10 
11 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
12 
13 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14 
15 This sample was distributed or derived from the Intel's Media Samples package.
16 The original version of this sample may be obtained from https://software.intel.com/en-us/intel-media-server-studio
17 or https://software.intel.com/en-us/media-client-solutions-support.
18 \**********************************************************************************/
19 
20 #include "sample_vpp_utils.h"
21 #include "mfxvideo++.h"
22 #include "vm/time_defs.h"
23 #include "sample_utils.h"
24 
25 #include "sample_vpp_pts.h"
26 
27 #include "sysmem_allocator.h"
28 
29 #ifdef D3D_SURFACES_SUPPORT
30 #include "d3d_device.h"
31 #include "d3d_allocator.h"
32 #endif
33 #ifdef MFX_D3D11_SUPPORT
34 #include "d3d11_device.h"
35 #include "d3d11_allocator.h"
36 #endif
37 #ifdef LIBVA_SUPPORT
38 #include "vaapi_device.h"
39 #include "vaapi_allocator.h"
40 #endif
41 
42 #include "general_allocator.h"
43 #include <algorithm>
44 
45 #if defined(_WIN64) || defined(_WIN32)
46 #include "mfxadapter.h"
47 #endif
48 
49 #define MFX_CHECK_STS(sts) {if (MFX_ERR_NONE != sts) return sts;}
50 
51 #undef min
52 
53 #ifndef MFX_VERSION
54 #error MFX_VERSION not defined
55 #endif
56 
57 /* ******************************************************************* */
58 
59 static
60     void WipeFrameProcessor(sFrameProcessor* pProcessor);
61 
62 static
63     void WipeMemoryAllocator(sMemoryAllocator* pAllocator);
64 
65 void ownToMfxFrameInfo( sOwnFrameInfo* in, mfxFrameInfo* out, bool copyCropParams=false);
66 
67 /* ******************************************************************* */
68 
69 static
FourCC2Str(mfxU32 FourCC)70     const msdk_char* FourCC2Str( mfxU32 FourCC )
71 {
72     switch ( FourCC )
73     {
74     case MFX_FOURCC_NV12:
75         return MSDK_STRING("NV12");
76     case MFX_FOURCC_YV12:
77         return MSDK_STRING("YV12");
78     case MFX_FOURCC_YUY2:
79         return MSDK_STRING("YUY2");
80 #if (MFX_VERSION >= 1028)
81     case MFX_FOURCC_RGB565:
82         return MSDK_STRING("RGB565");
83 #endif
84     case MFX_FOURCC_RGB3:
85         return MSDK_STRING("RGB3");
86     case MFX_FOURCC_RGB4:
87         return MSDK_STRING("RGB4");
88 #if !(defined(_WIN32) || defined(_WIN64))
89     case MFX_FOURCC_RGBP:
90         return MSDK_STRING("RGBP");
91 #endif
92     case MFX_FOURCC_YUV400:
93         return MSDK_STRING("YUV400");
94     case MFX_FOURCC_YUV411:
95         return MSDK_STRING("YUV411");
96     case MFX_FOURCC_YUV422H:
97         return MSDK_STRING("YUV422H");
98     case MFX_FOURCC_YUV422V:
99         return MSDK_STRING("YUV422V");
100     case MFX_FOURCC_YUV444:
101         return MSDK_STRING("YUV444");
102     case MFX_FOURCC_P010:
103         return MSDK_STRING("P010");
104     case MFX_FOURCC_P210:
105         return MSDK_STRING("P210");
106     case MFX_FOURCC_NV16:
107         return MSDK_STRING("NV16");
108     case MFX_FOURCC_A2RGB10:
109         return MSDK_STRING("A2RGB10");
110     case MFX_FOURCC_UYVY:
111         return MSDK_STRING("UYVY");
112     case MFX_FOURCC_AYUV:
113         return MSDK_STRING("AYUV");
114     case MFX_FOURCC_I420:
115         return MSDK_STRING("I420");
116 #if (MFX_VERSION >= 1027)
117     case MFX_FOURCC_Y210:
118         return MSDK_STRING("Y210");
119     case MFX_FOURCC_Y410:
120         return MSDK_STRING("Y410");
121 #endif
122 #if (MFX_VERSION >= 1031)
123     case MFX_FOURCC_P016:
124         return MSDK_STRING("P016");
125     case MFX_FOURCC_Y216:
126         return MSDK_STRING("Y216");
127     case MFX_FOURCC_Y416:
128         return MSDK_STRING("Y416");
129 #endif
130     default:
131         return MSDK_STRING("Unknown");
132     }
133 }
134 
IOpattern2Str(mfxU32 IOpattern)135 const msdk_char* IOpattern2Str( mfxU32 IOpattern)
136 {
137     switch ( IOpattern )
138     {
139     case MFX_IOPATTERN_IN_SYSTEM_MEMORY|MFX_IOPATTERN_OUT_SYSTEM_MEMORY:
140         return MSDK_STRING("sys_to_sys");
141     case MFX_IOPATTERN_IN_SYSTEM_MEMORY|MFX_IOPATTERN_OUT_VIDEO_MEMORY:
142         return MSDK_STRING("sys_to_d3d");
143     case MFX_IOPATTERN_IN_VIDEO_MEMORY|MFX_IOPATTERN_OUT_SYSTEM_MEMORY:
144         return MSDK_STRING("d3d_to_sys");
145     case MFX_IOPATTERN_IN_VIDEO_MEMORY|MFX_IOPATTERN_OUT_VIDEO_MEMORY:
146         return MSDK_STRING("d3d_to_d3d");
147     default:
148         return MSDK_STRING("Not defined");
149     }
150 }
151 
152 /* ******************************************************************* */
153 
154 //static
PicStruct2Str(mfxU16 PicStruct)155 const msdk_char* PicStruct2Str( mfxU16  PicStruct )
156 {
157     switch (PicStruct)
158     {
159     case MFX_PICSTRUCT_PROGRESSIVE:
160         return MSDK_STRING("progressive");
161     case MFX_PICSTRUCT_FIELD_TFF:
162         return MSDK_STRING("interlace (TFF)");
163     case MFX_PICSTRUCT_FIELD_BFF:
164         return MSDK_STRING("interlace (BFF)");
165     case MFX_PICSTRUCT_UNKNOWN:
166         return MSDK_STRING("unknown");
167     default:
168         return MSDK_STRING("interlace (no detail)");
169     }
170 }
171 
172 /* ******************************************************************* */
173 
PrintInfo(sInputParams * pParams,mfxVideoParam * pMfxParams,MFXVideoSession * pMfxSession)174 void PrintInfo(sInputParams* pParams, mfxVideoParam* pMfxParams, MFXVideoSession *pMfxSession)
175 {
176     mfxFrameInfo Info;
177 
178     MSDK_CHECK_POINTER_NO_RET(pParams);
179     MSDK_CHECK_POINTER_NO_RET(pMfxParams);
180 
181     Info = pMfxParams->vpp.In;
182     msdk_printf(MSDK_STRING("Input format\t%s\n"), FourCC2Str( Info.FourCC ));
183     msdk_printf(MSDK_STRING("Resolution\t%dx%d\n"), Info.Width, Info.Height);
184     msdk_printf(MSDK_STRING("Crop X,Y,W,H\t%d,%d,%d,%d\n"), Info.CropX, Info.CropY, Info.CropW, Info.CropH);
185     msdk_printf(MSDK_STRING("Frame rate\t%.2f\n"), (mfxF64)Info.FrameRateExtN / Info.FrameRateExtD);
186     msdk_printf(MSDK_STRING("PicStruct\t%s\n"), PicStruct2Str(Info.PicStruct));
187 
188     Info = pMfxParams->vpp.Out;
189     msdk_printf(MSDK_STRING("Output format\t%s\n"), FourCC2Str( Info.FourCC ));
190     msdk_printf(MSDK_STRING("Resolution\t%dx%d\n"), Info.Width, Info.Height);
191     msdk_printf(MSDK_STRING("Crop X,Y,W,H\t%d,%d,%d,%d\n"), Info.CropX, Info.CropY, Info.CropW, Info.CropH);
192     msdk_printf(MSDK_STRING("Frame rate\t%.2f\n"), (mfxF64)Info.FrameRateExtN / Info.FrameRateExtD);
193     msdk_printf(MSDK_STRING("PicStruct\t%s\n"), PicStruct2Str(Info.PicStruct));
194 
195     msdk_printf(MSDK_STRING("\n"));
196     msdk_printf(MSDK_STRING("Video Enhancement Algorithms\n"));
197     msdk_printf(MSDK_STRING("Deinterlace\t%s\n"), (pParams->frameInfoIn[0].PicStruct != pParams->frameInfoOut[0].PicStruct) ? MSDK_STRING("ON"): MSDK_STRING("OFF"));
198     msdk_printf(MSDK_STRING("Signal info\t%s\n"),   (VPP_FILTER_DISABLED != pParams->videoSignalInfoParam[0].mode) ? MSDK_STRING("ON"): MSDK_STRING("OFF"));
199     msdk_printf(MSDK_STRING("Scaling\t\t%s\n"),     (VPP_FILTER_DISABLED != pParams->bScaling) ? MSDK_STRING("ON"): MSDK_STRING("OFF"));
200 #if MFX_VERSION >= 1025
201     msdk_printf(MSDK_STRING("CromaSiting\t\t%s\n"), (VPP_FILTER_DISABLED != pParams->bChromaSiting) ? MSDK_STRING("ON") : MSDK_STRING("OFF"));
202 #endif
203     msdk_printf(MSDK_STRING("Denoise\t\t%s\n"),     (VPP_FILTER_DISABLED != pParams->denoiseParam[0].mode) ? MSDK_STRING("ON"): MSDK_STRING("OFF"));
204 #ifdef ENABLE_MCTF
205     msdk_printf(MSDK_STRING("MCTF\t\t%s\n"), (VPP_FILTER_DISABLED != pParams->mctfParam[0].mode) ? MSDK_STRING("ON") : MSDK_STRING("OFF"));
206 #endif
207 
208     msdk_printf(MSDK_STRING("ProcAmp\t\t%s\n"),     (VPP_FILTER_DISABLED != pParams->procampParam[0].mode) ? MSDK_STRING("ON"): MSDK_STRING("OFF"));
209     msdk_printf(MSDK_STRING("DetailEnh\t%s\n"),      (VPP_FILTER_DISABLED != pParams->detailParam[0].mode)  ? MSDK_STRING("ON"): MSDK_STRING("OFF"));
210     if(VPP_FILTER_DISABLED != pParams->frcParam[0].mode)
211     {
212         if(MFX_FRCALGM_FRAME_INTERPOLATION == pParams->frcParam[0].algorithm)
213         {
214             msdk_printf(MSDK_STRING("FRC:Interp\tON\n"));
215         }
216         else if(MFX_FRCALGM_DISTRIBUTED_TIMESTAMP == pParams->frcParam[0].algorithm)
217         {
218             msdk_printf(MSDK_STRING("FRC:AdvancedPTS\tON\n"));
219         }
220         else
221         {
222             msdk_printf(MSDK_STRING("FRC:\t\tON\n"));
223         }
224     }
225     //msdk_printf(MSDK_STRING("FRC:Advanced\t%s\n"),   (VPP_FILTER_DISABLED != pParams->frcParam.mode)  ? MSDK_STRING("ON"): MSDK_STRING("OFF"));
226     // MSDK 3.0
227     msdk_printf(MSDK_STRING("GamutMapping \t%s\n"),       (VPP_FILTER_DISABLED != pParams->gamutParam[0].mode)      ? MSDK_STRING("ON"): MSDK_STRING("OFF") );
228     msdk_printf(MSDK_STRING("ColorSaturation\t%s\n"),     (VPP_FILTER_DISABLED != pParams->tccParam[0].mode)   ? MSDK_STRING("ON"): MSDK_STRING("OFF") );
229     msdk_printf(MSDK_STRING("ContrastEnh  \t%s\n"),       (VPP_FILTER_DISABLED != pParams->aceParam[0].mode)   ? MSDK_STRING("ON"): MSDK_STRING("OFF") );
230     msdk_printf(MSDK_STRING("SkinToneEnh  \t%s\n"),       (VPP_FILTER_DISABLED != pParams->steParam[0].mode)       ? MSDK_STRING("ON"): MSDK_STRING("OFF") );
231     msdk_printf(MSDK_STRING("MVC mode    \t%s\n"),       (VPP_FILTER_DISABLED != pParams->multiViewParam[0].mode)  ? MSDK_STRING("ON"): MSDK_STRING("OFF") );
232     // MSDK 6.0
233     msdk_printf(MSDK_STRING("ImgStab    \t%s\n"),            (VPP_FILTER_DISABLED != pParams->istabParam[0].mode)  ? MSDK_STRING("ON"): MSDK_STRING("OFF") );
234     msdk_printf(MSDK_STRING("\n"));
235 
236     msdk_printf(MSDK_STRING("IOpattern type               \t%s\n"), IOpattern2Str( pParams->IOPattern ));
237     msdk_printf(MSDK_STRING("Number of asynchronious tasks\t%d\n"), pParams->asyncNum);
238     if ( pParams->bInitEx )
239     {
240         msdk_printf(MSDK_STRING("GPU Copy mode                \t%d\n"), pParams->GPUCopyValue);
241     }
242     msdk_printf(MSDK_STRING("Time stamps checking         \t%s\n"), pParams->ptsCheck ? MSDK_STRING("ON"): MSDK_STRING("OFF"));
243 
244     // info about ROI testing
245     if( ROI_FIX_TO_FIX == pParams->roiCheckParam.mode )
246     {
247         msdk_printf(MSDK_STRING("ROI checking                 \tOFF\n"));
248     }
249     else
250     {
251         msdk_printf(MSDK_STRING("ROI checking                 \tON (seed1 = %i, seed2 = %i)\n"),pParams->roiCheckParam.srcSeed, pParams->roiCheckParam.dstSeed );
252     }
253 
254     msdk_printf(MSDK_STRING("\n"));
255 
256     //-------------------------------------------------------
257     mfxIMPL impl;
258     pMfxSession->QueryIMPL(&impl);
259     bool isHWlib = (MFX_IMPL_SOFTWARE != impl) ? true:false;
260 
261     const msdk_char* sImpl = (isHWlib) ? MSDK_STRING("hw") : MSDK_STRING("sw");
262     msdk_printf(MSDK_STRING("MediaSDK impl\t%s"), sImpl);
263 
264 #ifndef LIBVA_SUPPORT
265     if (isHWlib || (pParams->vaType & (ALLOC_IMPL_VIA_D3D9 | ALLOC_IMPL_VIA_D3D11)))
266     {
267         bool  isD3D11 = (( ALLOC_IMPL_VIA_D3D11 == pParams->vaType) || (pParams->ImpLib == (MFX_IMPL_HARDWARE | MFX_IMPL_VIA_D3D11))) ?  true : false;
268         const msdk_char* sIface = ( isD3D11 ) ? MSDK_STRING("VIA_D3D11") : MSDK_STRING("VIA_D3D9");
269         msdk_printf(MSDK_STRING(" | %s"), sIface);
270     }
271 #endif
272     msdk_printf(MSDK_STRING("\n"));
273     //-------------------------------------------------------
274 
275     if (isHWlib && !pParams->bPartialAccel)
276         msdk_printf(MSDK_STRING("HW accelaration is enabled\n"));
277     else
278         msdk_printf(MSDK_STRING("HW accelaration is disabled\n"));
279 
280 #if (defined(_WIN64) || defined(_WIN32)) && (MFX_VERSION >= 1031)
281     if (pParams->bPrefferdGfx)
282         msdk_printf(MSDK_STRING("dGfx adapter is preffered\n"));
283 
284     if (pParams->bPrefferiGfx)
285         msdk_printf(MSDK_STRING("iGfx adapter is preffered\n"));
286 #endif
287 
288 
289     mfxVersion ver;
290     pMfxSession->QueryVersion(&ver);
291     msdk_printf(MSDK_STRING("MediaSDK ver\t%d.%d\n"), ver.Major, ver.Minor);
292 
293     return;
294 }
295 
296 /* ******************************************************************* */
297 
ParseGUID(msdk_char strPlgGuid[MSDK_MAX_FILENAME_LEN],mfxU8 DataGUID[16])298 mfxStatus ParseGUID(msdk_char strPlgGuid[MSDK_MAX_FILENAME_LEN], mfxU8 DataGUID[16])
299 {
300     const msdk_char *uid = strPlgGuid;
301     mfxU32 i   = 0;
302     mfxU32 hex = 0;
303     for(i = 0; i != 16; i++)
304     {
305         hex = 0;
306 #if defined(_WIN32) || defined(_WIN64)
307         if (1 != _stscanf_s(uid + 2*i, L"%2x", &hex))
308 #else
309         if (1 != sscanf(uid + 2*i, "%2x", &hex))
310 #endif
311         {
312             msdk_printf(MSDK_STRING("Failed to parse plugin uid: %s"), uid);
313             return MFX_ERR_UNKNOWN;
314         }
315 #if defined(_WIN32) || defined(_WIN64)
316         if (hex == 0 && (uid + 2*i != _tcsstr(uid + 2*i, L"00")))
317 #else
318         if (hex == 0 && (uid + 2*i != strstr(uid + 2*i, "00")))
319 #endif
320         {
321             msdk_printf(MSDK_STRING("Failed to parse plugin uid: %s"), uid);
322             return MFX_ERR_UNKNOWN;
323         }
324         DataGUID[i] = (mfxU8)hex;
325     }
326 
327     return MFX_ERR_NONE;
328 }
329 
InitParamsVPP(MfxVideoParamsWrapper * pParams,sInputParams * pInParams,mfxU32 paramID)330 mfxStatus InitParamsVPP(MfxVideoParamsWrapper* pParams, sInputParams* pInParams, mfxU32 paramID)
331 {
332     MSDK_CHECK_POINTER(pParams,    MFX_ERR_NULL_PTR);
333     MSDK_CHECK_POINTER(pInParams,  MFX_ERR_NULL_PTR);
334 
335     if (pInParams->compositionParam.mode != VPP_FILTER_ENABLED_CONFIGURED && (pInParams->frameInfoIn[paramID].nWidth == 0 || pInParams->frameInfoIn[paramID].nHeight == 0)){
336         vppPrintHelp(MSDK_STRING("sample_vpp"), MSDK_STRING("ERROR: Source width is not defined.\n"));
337         return MFX_ERR_UNSUPPORTED;
338     }
339     if (pInParams->frameInfoOut[paramID].nWidth == 0 || pInParams->frameInfoOut[paramID].nHeight == 0 ){
340         vppPrintHelp(MSDK_STRING("sample_vpp"), MSDK_STRING("ERROR: Source height is not defined.\n"));
341         return MFX_ERR_UNSUPPORTED;
342     }
343     *pParams = MfxVideoParamsWrapper();
344     /* input data */
345     pParams->vpp.In.Shift           = pInParams->frameInfoIn[paramID].Shift;
346     pParams->vpp.In.BitDepthLuma    = pInParams->frameInfoIn[paramID].BitDepthLuma;
347     pParams->vpp.In.BitDepthChroma  = pInParams->frameInfoIn[paramID].BitDepthChroma;
348     pParams->vpp.In.FourCC          = pInParams->frameInfoIn[paramID].FourCC;
349     pParams->vpp.In.ChromaFormat    = MFX_CHROMAFORMAT_YUV420;
350 
351     pParams->vpp.In.CropX = pInParams->frameInfoIn[paramID].CropX;
352     pParams->vpp.In.CropY = pInParams->frameInfoIn[paramID].CropY;
353     pParams->vpp.In.CropW = pInParams->frameInfoIn[paramID].CropW;
354     pParams->vpp.In.CropH = pInParams->frameInfoIn[paramID].CropH;
355     pParams->vpp.In.Width = MSDK_ALIGN16(pInParams->frameInfoIn[paramID].nWidth);
356     pParams->vpp.In.Height = (MFX_PICSTRUCT_PROGRESSIVE == pInParams->frameInfoIn[paramID].PicStruct)?
357                 MSDK_ALIGN16(pInParams->frameInfoIn[paramID].nHeight) : MSDK_ALIGN32(pInParams->frameInfoIn[paramID].nHeight);
358 
359     // width must be a multiple of 16
360     // height must be a multiple of 16 in case of frame picture and
361     // a multiple of 32 in case of field picture
362     mfxU16 maxWidth = 0, maxHeight = 0;
363     if(pInParams->compositionParam.mode == VPP_FILTER_ENABLED_CONFIGURED)
364     {
365         for (mfxU16 i = 0; i < pInParams->numStreams; i++)
366         {
367             pInParams->inFrameInfo[i].nWidth = MSDK_ALIGN16(pInParams->inFrameInfo[i].nWidth);
368             pInParams->inFrameInfo[i].nHeight = (MFX_PICSTRUCT_PROGRESSIVE == pInParams->inFrameInfo[i].PicStruct)?
369                 MSDK_ALIGN16(pInParams->inFrameInfo[i].nHeight) : MSDK_ALIGN32(pInParams->inFrameInfo[i].nHeight);
370             if (pInParams->inFrameInfo[i].nWidth > maxWidth)
371                 maxWidth = pInParams->inFrameInfo[i].nWidth;
372             if (pInParams->inFrameInfo[i].nHeight > maxHeight)
373                 maxHeight = pInParams->inFrameInfo[i].nHeight;
374         }
375 
376         pParams->vpp.In.Width = maxWidth;
377         pParams->vpp.In.Height= maxHeight;
378         pParams->vpp.In.CropX = 0;
379         pParams->vpp.In.CropY = 0;
380         pParams->vpp.In.CropW = maxWidth;
381         pParams->vpp.In.CropH = maxHeight;
382 
383     }
384     pParams->vpp.In.PicStruct = pInParams->frameInfoIn[paramID].PicStruct;
385 
386     ConvertFrameRate(pInParams->frameInfoIn[paramID].dFrameRate,
387         &pParams->vpp.In.FrameRateExtN,
388         &pParams->vpp.In.FrameRateExtD);
389 
390     /* output data */
391     pParams->vpp.Out.Shift           = pInParams->frameInfoOut[paramID].Shift;
392     pParams->vpp.Out.BitDepthLuma    = pInParams->frameInfoOut[paramID].BitDepthLuma;
393     pParams->vpp.Out.BitDepthChroma  = pInParams->frameInfoOut[paramID].BitDepthChroma;
394     pParams->vpp.Out.FourCC          = pInParams->frameInfoOut[paramID].FourCC;
395     pParams->vpp.Out.ChromaFormat    = MFX_CHROMAFORMAT_YUV420;
396 
397     pParams->vpp.Out.CropX = pInParams->frameInfoOut[paramID].CropX;
398     pParams->vpp.Out.CropY = pInParams->frameInfoOut[paramID].CropY;
399     pParams->vpp.Out.CropW = pInParams->frameInfoOut[paramID].CropW;
400     pParams->vpp.Out.CropH = pInParams->frameInfoOut[paramID].CropH;
401 
402     // width must be a multiple of 16
403     // height must be a multiple of 16 in case of frame picture and
404     // a multiple of 32 in case of field picture
405     pParams->vpp.Out.Width = MSDK_ALIGN16(pInParams->frameInfoOut[paramID].nWidth);
406     pParams->vpp.Out.Height= (MFX_PICSTRUCT_PROGRESSIVE == pInParams->frameInfoOut[paramID].PicStruct)?
407         MSDK_ALIGN16(pInParams->frameInfoOut[paramID].nHeight) : MSDK_ALIGN32(pInParams->frameInfoOut[paramID].nHeight);
408     if(pInParams->need_plugin)
409     {
410         mfxPluginUID mfxGuid;
411         ParseGUID(pInParams->strPlgGuid, mfxGuid.Data);
412         if(!memcmp(&mfxGuid,&MFX_PLUGINID_ITELECINE_HW,sizeof(mfxPluginUID)))
413         {
414             //CM PTIR require equal input and output frame sizes
415             pParams->vpp.Out.Height = pParams->vpp.In.Height;
416         }
417     }
418 
419     pParams->vpp.Out.PicStruct = pInParams->frameInfoOut[paramID].PicStruct;
420 
421     ConvertFrameRate(pInParams->frameInfoOut[paramID].dFrameRate,
422         &pParams->vpp.Out.FrameRateExtN,
423         &pParams->vpp.Out.FrameRateExtD);
424 
425     pParams->IOPattern = pInParams->IOPattern;
426 
427     // async depth
428     pParams->AsyncDepth = pInParams->asyncNum;
429 
430     return MFX_ERR_NONE;
431 }
432 
433 /* ******************************************************************* */
434 
435 #if (defined(_WIN64) || defined(_WIN32)) && (MFX_VERSION >= 1031)
GetPreferredAdapterNum(const mfxAdaptersInfo & adapters,const sInputParams & params)436 mfxU32 GetPreferredAdapterNum(const mfxAdaptersInfo & adapters, const sInputParams & params)
437 {
438     if (adapters.NumActual == 0 || !adapters.Adapters)
439         return 0;
440 
441     if (params.bPrefferdGfx)
442     {
443         // Find dGfx adapter in list and return it's index
444 
445         auto idx = std::find_if(adapters.Adapters, adapters.Adapters + adapters.NumActual,
446             [](const mfxAdapterInfo info)
447         {
448             return info.Platform.MediaAdapterType == mfxMediaAdapterType::MFX_MEDIA_DISCRETE;
449         });
450 
451         // No dGfx in list
452         if (idx == adapters.Adapters + adapters.NumActual)
453         {
454             msdk_printf(MSDK_STRING("Warning: No dGfx detected on machine. Will pick another adapter\n"));
455             return 0;
456         }
457 
458         return static_cast<mfxU32>(std::distance(adapters.Adapters, idx));
459     }
460 
461     if (params.bPrefferiGfx)
462     {
463         // Find iGfx adapter in list and return it's index
464 
465         auto idx = std::find_if(adapters.Adapters, adapters.Adapters + adapters.NumActual,
466             [](const mfxAdapterInfo info)
467         {
468             return info.Platform.MediaAdapterType == mfxMediaAdapterType::MFX_MEDIA_INTEGRATED;
469         });
470 
471         // No iGfx in list
472         if (idx == adapters.Adapters + adapters.NumActual)
473         {
474             msdk_printf(MSDK_STRING("Warning: No iGfx detected on machine. Will pick another adapter\n"));
475             return 0;
476         }
477 
478         return static_cast<mfxU32>(std::distance(adapters.Adapters, idx));
479     }
480 
481     // Other ways return 0, i.e. best suitable detected by dispatcher
482     return 0;
483 }
484 
GetImpl(const mfxVideoParam & params,mfxIMPL & impl,const sInputParams & cmd_params)485 mfxStatus GetImpl(const mfxVideoParam & params, mfxIMPL & impl, const sInputParams & cmd_params)
486 {
487     if (!(impl & MFX_IMPL_HARDWARE))
488         return MFX_ERR_NONE;
489 
490     mfxU32 num_adapters_available;
491 
492     mfxStatus sts = MFXQueryAdaptersNumber(&num_adapters_available);
493     MSDK_CHECK_STATUS(sts, "MFXQueryAdaptersNumber failed");
494 
495     mfxComponentInfo interface_request = { mfxComponentType::MFX_COMPONENT_VPP };
496     interface_request.Requirements.vpp = params.vpp;
497 
498     std::vector<mfxAdapterInfo> displays_data(num_adapters_available);
499     mfxAdaptersInfo adapters = { displays_data.data(), mfxU32(displays_data.size()), 0u };
500 
501     sts = MFXQueryAdapters(&interface_request, &adapters);
502     if (sts == MFX_ERR_NOT_FOUND)
503     {
504         msdk_printf(MSDK_STRING("ERROR: No suitable adapters found for this workload\n"));
505     }
506     MSDK_CHECK_STATUS(sts, "MFXQueryAdapters failed");
507 
508     impl &= ~MFX_IMPL_HARDWARE;
509 
510     mfxU32 idx = GetPreferredAdapterNum(adapters, cmd_params);
511     switch (adapters.Adapters[idx].Number)
512     {
513     case 0:
514         impl |= MFX_IMPL_HARDWARE;
515         break;
516     case 1:
517         impl |= MFX_IMPL_HARDWARE2;
518         break;
519     case 2:
520         impl |= MFX_IMPL_HARDWARE3;
521         break;
522     case 3:
523         impl |= MFX_IMPL_HARDWARE4;
524         break;
525 
526     default:
527         // Try searching on all display adapters
528         impl |= MFX_IMPL_HARDWARE_ANY;
529         break;
530     }
531 
532     return MFX_ERR_NONE;
533 }
534 #endif // (defined(_WIN64) || defined(_WIN32)) && (MFX_VERSION >= 1031)
535 
CreateFrameProcessor(sFrameProcessor * pProcessor,mfxVideoParam * pParams,sInputParams * pInParams)536 mfxStatus CreateFrameProcessor(sFrameProcessor* pProcessor, mfxVideoParam* pParams, sInputParams* pInParams)
537 {
538     mfxStatus  sts = MFX_ERR_NONE;
539     mfxVersion version = { {10, 1} };
540     mfxIMPL    impl    = pInParams->ImpLib;
541 
542     MSDK_CHECK_POINTER(pProcessor, MFX_ERR_NULL_PTR);
543     MSDK_CHECK_POINTER(pParams,    MFX_ERR_NULL_PTR);
544 
545     WipeFrameProcessor(pProcessor);
546 
547 #if (defined(_WIN64) || defined(_WIN32)) && (MFX_VERSION >= 1031)
548     sts = GetImpl(*pParams, impl, *pInParams);
549     MSDK_CHECK_STATUS(sts, "GetImpl failed");
550 #endif
551 
552     //MFX session
553     if ( ! pInParams->bInitEx )
554         sts = pProcessor->mfxSession.Init(impl, &version);
555     else
556     {
557         mfxInitParamlWrap initParams;
558         initParams.ExternalThreads = 0;
559         initParams.GPUCopy         = pInParams->GPUCopyValue;
560         initParams.Implementation  = impl;
561         initParams.Version         = version;
562         initParams.NumExtParam     = 0;
563         sts = pProcessor->mfxSession.InitEx(initParams);
564     }
565     MSDK_CHECK_STATUS_SAFE(sts, "pProcessor->mfxSession.Init failed", {WipeFrameProcessor(pProcessor);});
566 
567     // Plug-in
568     if ( pInParams->need_plugin )
569     {
570         pProcessor->plugin = true;
571         ParseGUID(pInParams->strPlgGuid, pProcessor->mfxGuid.Data);
572     }
573 
574     if ( pProcessor->plugin )
575     {
576         sts = MFXVideoUSER_Load(pProcessor->mfxSession, &(pProcessor->mfxGuid), 1);
577         if (MFX_ERR_NONE != sts)
578         {
579             msdk_printf(MSDK_STRING("Failed to load plugin\n"));
580             return sts;
581         }
582     }
583 
584     // VPP
585     pProcessor->pmfxVPP = new MFXVideoVPP(pProcessor->mfxSession);
586 
587     return MFX_ERR_NONE;
588 }
589 
590 /* ******************************************************************* */
591 
InitFrameProcessor(sFrameProcessor * pProcessor,mfxVideoParam * pParams)592 mfxStatus InitFrameProcessor(sFrameProcessor* pProcessor, mfxVideoParam* pParams)
593 {
594     mfxStatus sts = MFX_ERR_NONE;
595 
596     MSDK_CHECK_POINTER(pProcessor,          MFX_ERR_NULL_PTR);
597     MSDK_CHECK_POINTER(pParams,             MFX_ERR_NULL_PTR);
598     MSDK_CHECK_POINTER(pProcessor->pmfxVPP, MFX_ERR_NULL_PTR);
599 
600     // close VPP in case it was initialized
601     sts = pProcessor->pmfxVPP->Close();
602     MSDK_IGNORE_MFX_STS(sts, MFX_ERR_NOT_INITIALIZED);
603     MSDK_CHECK_STATUS(sts, "pProcessor->pmfxVPP->Close failed");
604 
605 
606     // init VPP
607     sts = pProcessor->pmfxVPP->Init(pParams);
608     return sts;
609 }
610 
611 /* ******************************************************************* */
612 
InitSurfaces(sMemoryAllocator * pAllocator,mfxFrameAllocRequest * pRequest,bool isInput,int streamIndex)613 mfxStatus InitSurfaces(
614     sMemoryAllocator* pAllocator,
615     mfxFrameAllocRequest* pRequest,
616     bool isInput,
617     int streamIndex)
618 {
619     mfxStatus sts = MFX_ERR_NONE;
620     mfxU16    nFrames, i;
621 
622     mfxFrameAllocResponse& response = isInput ? pAllocator->responseIn[streamIndex] : pAllocator->responseOut;
623     mfxFrameSurfaceWrap*& pSurfaces = isInput ? pAllocator->pSurfacesIn[streamIndex] : pAllocator->pSurfacesOut;
624 
625     sts = pAllocator->pMfxAllocator->Alloc(pAllocator->pMfxAllocator->pthis, pRequest, &response);
626     MSDK_CHECK_STATUS_SAFE(sts, "pAllocator->pMfxAllocator->Alloc failed", {WipeMemoryAllocator(pAllocator);});
627 
628     nFrames = response.NumFrameActual;
629     pSurfaces = new mfxFrameSurfaceWrap [nFrames];
630 
631     for (i = 0; i < nFrames; i++)
632     {
633         pSurfaces[i].Info = pRequest->Info;
634         pSurfaces[i].Data.MemId = response.mids[i];
635     }
636 
637     return sts;
638 }
639 
640 /* ******************************************************************* */
641 
InitMemoryAllocator(sFrameProcessor * pProcessor,sMemoryAllocator * pAllocator,mfxVideoParam * pParams,sInputParams * pInParams)642 mfxStatus InitMemoryAllocator(
643     sFrameProcessor* pProcessor,
644     sMemoryAllocator* pAllocator,
645     mfxVideoParam* pParams,
646     sInputParams* pInParams)
647 {
648     mfxStatus sts = MFX_ERR_NONE;
649     mfxFrameAllocRequest request[2];// [0] - in, [1] - out
650     //mfxFrameInfo requestFrameInfoRGB;
651 
652     MSDK_CHECK_POINTER(pProcessor,          MFX_ERR_NULL_PTR);
653     MSDK_CHECK_POINTER(pAllocator,          MFX_ERR_NULL_PTR);
654     MSDK_CHECK_POINTER(pParams,             MFX_ERR_NULL_PTR);
655     MSDK_CHECK_POINTER(pProcessor->pmfxVPP, MFX_ERR_NULL_PTR);
656 
657     MSDK_ZERO_MEMORY(request);
658 
659     pAllocator->pMfxAllocator =  new GeneralAllocator;
660 
661     bool isHWLib       = (MFX_IMPL_HARDWARE & pInParams->ImpLib) ? true : false;
662 
663     if(isHWLib)
664     {
665         if((pInParams->ImpLib & IMPL_VIA_MASK) == MFX_IMPL_VIA_D3D9)
666         {
667 #ifdef D3D_SURFACES_SUPPORT
668             // prepare device manager
669             pAllocator->pDevice = new CD3D9Device();
670             sts = pAllocator->pDevice->Init(0, 1, MSDKAdapter::GetNumber(pProcessor->mfxSession));
671             MSDK_CHECK_STATUS_SAFE(sts, "pAllocator->pDevice->Init failed", WipeMemoryAllocator(pAllocator));
672 
673             mfxHDL hdl = 0;
674             sts = pAllocator->pDevice->GetHandle(MFX_HANDLE_D3D9_DEVICE_MANAGER, &hdl);
675             MSDK_CHECK_STATUS_SAFE(sts, "pAllocator->pDevice->GetHandle failed", WipeMemoryAllocator(pAllocator));
676             sts = pProcessor->mfxSession.SetHandle(MFX_HANDLE_D3D9_DEVICE_MANAGER, hdl);
677             MSDK_CHECK_STATUS_SAFE(sts, "pAllocator->pDevice->SetHandle failed", WipeMemoryAllocator(pAllocator));
678 
679             // prepare allocator
680             D3DAllocatorParams *pd3dAllocParams = new D3DAllocatorParams;
681 
682             pd3dAllocParams->pManager = (IDirect3DDeviceManager9*)hdl;
683             pAllocator->pAllocatorParams = pd3dAllocParams;
684 #endif
685         }
686         else if((pInParams->ImpLib & IMPL_VIA_MASK) == MFX_IMPL_VIA_D3D11)
687         {
688 #if MFX_D3D11_SUPPORT
689             pAllocator->pDevice = new CD3D11Device();
690 
691             sts = pAllocator->pDevice->Init(0, 1, MSDKAdapter::GetNumber(pProcessor->mfxSession));
692             MSDK_CHECK_STATUS_SAFE(sts, "pAllocator->pDevice->Init failed", WipeMemoryAllocator(pAllocator));
693 
694             mfxHDL hdl = 0;
695             sts = pAllocator->pDevice->GetHandle(MFX_HANDLE_D3D11_DEVICE, &hdl);
696             MSDK_CHECK_STATUS_SAFE(sts, "pAllocator->pDevice->GetHandle failed", WipeMemoryAllocator(pAllocator));
697             sts = pProcessor->mfxSession.SetHandle(MFX_HANDLE_D3D11_DEVICE, hdl);
698             MSDK_CHECK_STATUS_SAFE(sts, "pAllocator->pDevice->SetHandle failed", WipeMemoryAllocator(pAllocator));
699 
700             // prepare allocator
701             D3D11AllocatorParams *pd3d11AllocParams = new D3D11AllocatorParams;
702 
703             pd3d11AllocParams->pDevice = (ID3D11Device*)hdl;
704             pAllocator->pAllocatorParams = pd3d11AllocParams;
705 #endif
706         }
707         else if ((pInParams->ImpLib & IMPL_VIA_MASK) == MFX_IMPL_VIA_VAAPI)
708         {
709 #ifdef LIBVA_SUPPORT
710             pAllocator->pDevice = CreateVAAPIDevice(pInParams->strDevicePath);
711             MSDK_CHECK_POINTER(pAllocator->pDevice, MFX_ERR_NULL_PTR);
712 
713             sts = pAllocator->pDevice->Init(0, 1, MSDKAdapter::GetNumber(pProcessor->mfxSession));
714             MSDK_CHECK_STATUS_SAFE(sts, "pAllocator->pDevice->Init failed", WipeMemoryAllocator(pAllocator));
715 
716             mfxHDL hdl = 0;
717             sts = pAllocator->pDevice->GetHandle(MFX_HANDLE_VA_DISPLAY, &hdl);
718             MSDK_CHECK_STATUS_SAFE(sts, "pAllocator->pDevice->GetHandle failed", WipeMemoryAllocator(pAllocator));
719             sts = pProcessor->mfxSession.SetHandle(MFX_HANDLE_VA_DISPLAY, hdl);
720             MSDK_CHECK_STATUS_SAFE(sts, "pAllocator->pDevice->SetHandle failed", WipeMemoryAllocator(pAllocator));
721 
722             // prepare allocator
723             vaapiAllocatorParams *pVaapiAllocParams = new vaapiAllocatorParams;
724 
725             pVaapiAllocParams->m_dpy = (VADisplay)hdl;
726             pAllocator->pAllocatorParams = pVaapiAllocParams;
727 
728 #endif
729         }
730     }
731     else
732     {
733 #ifdef LIBVA_SUPPORT
734         //in case of system memory allocator we also have to pass MFX_HANDLE_VA_DISPLAY to HW library
735         mfxIMPL impl;
736         pProcessor->mfxSession.QueryIMPL(&impl);
737 
738         if(MFX_IMPL_HARDWARE == MFX_IMPL_BASETYPE(impl))
739         {
740             pAllocator->pDevice = CreateVAAPIDevice(pInParams->strDevicePath);
741             if (!pAllocator->pDevice) sts = MFX_ERR_MEMORY_ALLOC;
742             MSDK_CHECK_STATUS_SAFE(sts, "pAllocator->pDevice creation failed", WipeMemoryAllocator(pAllocator));
743 
744             mfxHDL hdl = 0;
745             sts = pAllocator->pDevice->GetHandle(MFX_HANDLE_VA_DISPLAY, &hdl);
746             MSDK_CHECK_STATUS_SAFE(sts, "pAllocator->pDevice->GetHandle failed", WipeMemoryAllocator(pAllocator));
747 
748             sts = pProcessor->mfxSession.SetHandle(MFX_HANDLE_VA_DISPLAY, hdl);
749             MSDK_CHECK_STATUS_SAFE(sts, "pAllocator->pDevice->SetHandle failed", WipeMemoryAllocator(pAllocator));
750         }
751 #endif
752 
753     }
754     /* This sample uses external memory allocator model for both system and HW memory */
755     sts = pProcessor->mfxSession.SetFrameAllocator(pAllocator->pMfxAllocator);
756     MSDK_CHECK_STATUS_SAFE(sts, "pProcessor->mfxSession.SetFrameAllocator failed", WipeMemoryAllocator(pAllocator));
757     pAllocator->bUsedAsExternalAllocator = true;
758 
759     sts = pAllocator->pMfxAllocator->Init(pAllocator->pAllocatorParams);
760     MSDK_CHECK_STATUS_SAFE(sts, "pAllocator->pMfxAllocator->Init failed", WipeMemoryAllocator(pAllocator));
761 
762     mfxVideoParam tmpParam={0};
763     tmpParam.ExtParam = pParams->ExtParam;
764     tmpParam.NumExtParam = pParams->NumExtParam;
765     sts = pProcessor->pmfxVPP->Query(pParams, &tmpParam);
766     *pParams=tmpParam;
767     MSDK_CHECK_STATUS_SAFE(sts, "pProcessor->pmfxVPP->Query failed", WipeMemoryAllocator(pAllocator));
768 
769     sts = pProcessor->pmfxVPP->QueryIOSurf(pParams, request);
770     MSDK_IGNORE_MFX_STS(sts, MFX_WRN_PARTIAL_ACCELERATION);
771     MSDK_CHECK_STATUS_SAFE(sts, "pProcessor->pmfxVPP->QueryIOSurf failed", WipeMemoryAllocator(pAllocator));
772 
773     // alloc frames for vpp
774     // [IN]
775     // If we have only one input stream - allocate as many surfaces as were requested. Otherwise (in case of composition) - allocate 1 surface per input
776     // Modify frame info as well
777     if(pInParams->compositionParam.mode != VPP_FILTER_ENABLED_CONFIGURED)
778     {
779         sts = InitSurfaces(pAllocator, &(request[VPP_IN]),true,0);
780        MSDK_CHECK_STATUS_SAFE(sts, "InitSurfaces failed", WipeMemoryAllocator(pAllocator));
781     }
782     else
783     {
784         for(int i=0;i<pInParams->numStreams;i++)
785         {
786             ownToMfxFrameInfo(&pInParams->inFrameInfo[i],&request[VPP_IN].Info,true);
787             request[VPP_IN].NumFrameSuggested = 1;
788             request[VPP_IN].NumFrameMin = request[VPP_IN].NumFrameSuggested;
789             sts = InitSurfaces(pAllocator, &(request[VPP_IN]),true,i);
790             MSDK_CHECK_STATUS_SAFE(sts, "InitSurfaces failed", WipeMemoryAllocator(pAllocator));
791         }
792     }
793 
794     // [OUT]
795     sts = InitSurfaces(pAllocator, &(request[VPP_OUT]), false,0);
796     MSDK_CHECK_STATUS_SAFE(sts, "InitSurfaces failed", WipeMemoryAllocator(pAllocator));
797 
798     return MFX_ERR_NONE;
799 
800 } // mfxStatus InitMemoryAllocator(...)}
801 
802 /* ******************************************************************* */
803 
InitResources(sAppResources * pResources,mfxVideoParam * pParams,sInputParams * pInParams)804 mfxStatus InitResources(sAppResources* pResources, mfxVideoParam* pParams, sInputParams* pInParams)
805 {
806     mfxStatus sts = MFX_ERR_NONE;
807 
808     MSDK_CHECK_POINTER(pResources, MFX_ERR_NULL_PTR);
809     MSDK_CHECK_POINTER(pParams,    MFX_ERR_NULL_PTR);
810     sts = CreateFrameProcessor(pResources->pProcessor, pParams, pInParams);
811     MSDK_CHECK_STATUS_SAFE(sts, "CreateFrameProcessor failed", { WipeResources(pResources); WipeParams(pInParams);});
812 
813     sts = InitMemoryAllocator(pResources->pProcessor, pResources->pAllocator, pParams, pInParams);
814     MSDK_CHECK_STATUS_SAFE(sts, "InitMemoryAllocator failed", { WipeResources(pResources); WipeParams(pInParams);});
815 
816     sts = InitFrameProcessor(pResources->pProcessor, pParams);
817 
818     if (MFX_WRN_PARTIAL_ACCELERATION == sts || MFX_WRN_FILTER_SKIPPED == sts)
819         return sts;
820     else
821     {
822         MSDK_CHECK_STATUS_SAFE(sts, "InitFrameProcessor failed", { WipeResources(pResources); WipeParams(pInParams);});
823     }
824 
825     return sts;
826 }
827 
828 /* ******************************************************************* */
829 
WipeFrameProcessor(sFrameProcessor * pProcessor)830 void WipeFrameProcessor(sFrameProcessor* pProcessor)
831 {
832     MSDK_CHECK_POINTER_NO_RET(pProcessor);
833 
834     MSDK_SAFE_DELETE(pProcessor->pmfxVPP);
835 
836     if ( pProcessor->plugin )
837     {
838         MFXVideoUSER_UnLoad(pProcessor->mfxSession, &(pProcessor->mfxGuid));
839     }
840 
841     pProcessor->mfxSession.Close();
842 }
843 
WipeMemoryAllocator(sMemoryAllocator * pAllocator)844 void WipeMemoryAllocator(sMemoryAllocator* pAllocator)
845 {
846     MSDK_CHECK_POINTER_NO_RET(pAllocator);
847 
848     for(int i=0;i<MAX_INPUT_STREAMS;i++)
849     {
850         MSDK_SAFE_DELETE_ARRAY(pAllocator->pSurfacesIn[i]);
851     }
852     //    MSDK_SAFE_DELETE_ARRAY(pAllocator->pSurfaces[VPP_IN_RGB]);
853     MSDK_SAFE_DELETE_ARRAY(pAllocator->pSurfacesOut);
854 
855     mfxU32 did;
856     for(did = 0; did < 8; did++)
857     {
858         MSDK_SAFE_DELETE_ARRAY(pAllocator->pSvcSurfaces[did]);
859     }
860 
861     // delete frames
862     if (pAllocator->pMfxAllocator)
863     {
864         for(int i=0;i<MAX_INPUT_STREAMS;i++)
865         {
866             if(pAllocator->responseIn[i].NumFrameActual)
867             {
868                 pAllocator->pMfxAllocator->Free(pAllocator->pMfxAllocator->pthis, &pAllocator->responseIn[i]);
869             }
870         }
871         pAllocator->pMfxAllocator->Free(pAllocator->pMfxAllocator->pthis, &pAllocator->responseOut);
872 
873         for(did = 0; did < 8; did++)
874         {
875             pAllocator->pMfxAllocator->Free(pAllocator->pMfxAllocator->pthis, &pAllocator->svcResponse[did]);
876         }
877     }
878 
879     // delete allocator
880     MSDK_SAFE_DELETE(pAllocator->pMfxAllocator);
881     MSDK_SAFE_DELETE(pAllocator->pDevice);
882 
883     // delete allocator parameters
884     MSDK_SAFE_DELETE(pAllocator->pAllocatorParams);
885 
886 } // void WipeMemoryAllocator(sMemoryAllocator* pAllocator)
887 
888 
WipeConfigParam(sAppResources * pResources)889 void WipeConfigParam( sAppResources* pResources )
890 {
891     auto multiViewConfig = pResources->pVppParams->GetExtBuffer<mfxExtMVCSeqDesc>();
892     if (multiViewConfig)
893     {
894         delete [] multiViewConfig->View;
895     }
896 } // void WipeConfigParam( sAppResources* pResources )
897 
898 
WipeResources(sAppResources * pResources)899 void WipeResources(sAppResources* pResources)
900 {
901     MSDK_CHECK_POINTER_NO_RET(pResources);
902 
903     WipeFrameProcessor(pResources->pProcessor);
904 
905     WipeMemoryAllocator(pResources->pAllocator);
906 
907     for (int i = 0; i < pResources->numSrcFiles; i++)
908     {
909         if (pResources->pSrcFileReaders[i])
910         {
911             pResources->pSrcFileReaders[i]->Close();
912         }
913     }
914     pResources->numSrcFiles=0;
915 
916     if (pResources->pDstFileWriters)
917     {
918         for (mfxU32 i = 0; i < pResources->dstFileWritersN; i++)
919         {
920             pResources->pDstFileWriters[i].Close();
921         }
922         delete[] pResources->pDstFileWriters;
923         pResources->dstFileWritersN = 0;
924         pResources->pDstFileWriters = NULL;
925     }
926 
927     auto compositeConfig = pResources->pVppParams->GetExtBuffer<mfxExtVPPComposite>();
928     if(compositeConfig)
929     {
930         delete[] compositeConfig->InputStream;
931         compositeConfig->InputStream=nullptr;
932     }
933 
934     WipeConfigParam( pResources );
935 
936 } // void WipeResources(sAppResources* pResources)
937 
938 /* ******************************************************************* */
939 
WipeParams(sInputParams * pParams)940 void WipeParams(sInputParams* pParams)
941 {
942     pParams->strDstFiles.clear();
943 
944 } // void WipeParams(sInputParams* pParams)
945 
946 /* ******************************************************************* */
947 
CRawVideoReader()948 CRawVideoReader::CRawVideoReader()
949 {
950     m_fSrc = 0;
951     m_isPerfMode = false;
952     m_Repeat = 0;
953     m_pPTSMaker = 0;
954     m_initFcc = 0;
955 }
956 
Init(const msdk_char * strFileName,PTSMaker * pPTSMaker,mfxU32 fcc)957 mfxStatus CRawVideoReader::Init(const msdk_char *strFileName, PTSMaker *pPTSMaker, mfxU32 fcc)
958 {
959     Close();
960 
961     MSDK_CHECK_POINTER(strFileName, MFX_ERR_NULL_PTR);
962 
963     MSDK_FOPEN(m_fSrc,strFileName, MSDK_STRING("rb"));
964     MSDK_CHECK_POINTER(m_fSrc, MFX_ERR_ABORTED);
965 
966     m_pPTSMaker = pPTSMaker;
967     m_initFcc = fcc;
968     return MFX_ERR_NONE;
969 }
970 
~CRawVideoReader()971 CRawVideoReader::~CRawVideoReader()
972 {
973     Close();
974 }
975 
Close()976 void CRawVideoReader::Close()
977 {
978     if (m_fSrc != 0)
979     {
980         fclose(m_fSrc);
981         m_fSrc = 0;
982     }
983     m_SurfacesList.clear();
984 
985 }
986 
LoadNextFrame(mfxFrameData * pData,mfxFrameInfo * pInfo)987 mfxStatus CRawVideoReader::LoadNextFrame(mfxFrameData* pData, mfxFrameInfo* pInfo)
988 {
989     MSDK_CHECK_POINTER(pData, MFX_ERR_NOT_INITIALIZED);
990     MSDK_CHECK_POINTER(pInfo, MFX_ERR_NOT_INITIALIZED);
991 
992     // Only (I420|YV12) -> NV12 in-place conversion supported
993     if (pInfo->FourCC != m_initFcc &&
994         (pInfo->FourCC != MFX_FOURCC_NV12 ||
995         (m_initFcc != MFX_FOURCC_I420 && m_initFcc != MFX_FOURCC_YV12) ) )
996     {
997         return MFX_ERR_INVALID_VIDEO_PARAM;
998     }
999 
1000     mfxU32 w, h, i, pitch;
1001     mfxU32 nBytesRead;
1002     mfxU8 *ptr;
1003 
1004     if (pInfo->CropH > 0 && pInfo->CropW > 0)
1005     {
1006         w = pInfo->CropW;
1007         h = pInfo->CropH;
1008     }
1009     else
1010     {
1011         w = pInfo->Width;
1012         h = pInfo->Height;
1013     }
1014 
1015     pitch = ((mfxU32)pData->PitchHigh << 16) + pData->PitchLow;
1016 
1017     if(pInfo->FourCC == MFX_FOURCC_YV12 || pInfo->FourCC == MFX_FOURCC_I420)
1018     {
1019         ptr = pData->Y + pInfo->CropX + pInfo->CropY * pitch;
1020 
1021         // read luminance plane
1022         for(i = 0; i < h; i++)
1023         {
1024             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, w, m_fSrc);
1025             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA);
1026         }
1027 
1028         w     >>= 1;
1029         h     >>= 1;
1030         pitch >>= 1;
1031         // load U/V
1032         ptr = (pInfo->FourCC == MFX_FOURCC_I420 ? pData->U : pData->V) + (pInfo->CropX >> 1) + (pInfo->CropY >> 1) * pitch;
1033         for(i = 0; i < h; i++)
1034         {
1035             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, w, m_fSrc);
1036             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA);
1037         }
1038         // load V/U
1039         ptr  = (pInfo->FourCC == MFX_FOURCC_I420 ? pData->V : pData->U) + (pInfo->CropX >> 1) + (pInfo->CropY >> 1) * pitch;
1040         for(i = 0; i < h; i++)
1041         {
1042             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, w, m_fSrc);
1043             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA);
1044         }
1045     }
1046     else   if(pInfo->FourCC == MFX_FOURCC_YUV400)
1047     {
1048         ptr = pData->Y + pInfo->CropX + pInfo->CropY * pitch;
1049 
1050         // read luminance plane
1051         for(i = 0; i < h; i++)
1052         {
1053             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, w, m_fSrc);
1054             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA);
1055         }
1056     }
1057     else if(pInfo->FourCC == MFX_FOURCC_YUV411)
1058     {
1059         ptr = pData->Y + pInfo->CropX + pInfo->CropY * pitch;
1060 
1061         // read luminance plane
1062         for(i = 0; i < h; i++)
1063         {
1064             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, w, m_fSrc);
1065             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA);
1066         }
1067 
1068         w /= 4;
1069 
1070         // load U
1071         ptr  = pData->U + (pInfo->CropX >> 1) + (pInfo->CropY >> 1) * pitch;
1072         for(i = 0; i < h; i++)
1073         {
1074             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, w, m_fSrc);
1075             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA);
1076         }
1077         // load V
1078         ptr  = pData->V + (pInfo->CropX >> 1) + (pInfo->CropY >> 1) * pitch;
1079         for(i = 0; i < h; i++)
1080         {
1081             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, w, m_fSrc);
1082             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA);
1083         }
1084     }
1085     else if(pInfo->FourCC == MFX_FOURCC_YUV422H)
1086     {
1087         ptr = pData->Y + pInfo->CropX + pInfo->CropY * pitch;
1088 
1089         // read luminance plane
1090         for(i = 0; i < h; i++)
1091         {
1092             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, w, m_fSrc);
1093             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA);
1094         }
1095 
1096         w     >>= 1;
1097 
1098         // load U
1099         ptr  = pData->U + (pInfo->CropX >> 1) + (pInfo->CropY >> 1) * pitch;
1100         for(i = 0; i < h; i++)
1101         {
1102             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, w, m_fSrc);
1103             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA);
1104         }
1105         // load V
1106         ptr  = pData->V + (pInfo->CropX >> 1) + (pInfo->CropY >> 1) * pitch;
1107         for(i = 0; i < h; i++)
1108         {
1109             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, w, m_fSrc);
1110             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA);
1111         }
1112     }
1113     else if(pInfo->FourCC == MFX_FOURCC_YUV422V)
1114     {
1115         ptr = pData->Y + pInfo->CropX + pInfo->CropY * pitch;
1116 
1117         // read luminance plane
1118         for(i = 0; i < h; i++)
1119         {
1120             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, w, m_fSrc);
1121             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA);
1122         }
1123 
1124         h     >>= 1;
1125 
1126         // load U
1127         ptr  = pData->U + (pInfo->CropX >> 1) + (pInfo->CropY >> 1) * pitch;
1128         for(i = 0; i < h; i++)
1129         {
1130             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, w, m_fSrc);
1131             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA);
1132         }
1133         // load V
1134         ptr  = pData->V + (pInfo->CropX >> 1) + (pInfo->CropY >> 1) * pitch;
1135         for(i = 0; i < h; i++)
1136         {
1137             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, w, m_fSrc);
1138             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA);
1139         }
1140     }
1141     else if(pInfo->FourCC == MFX_FOURCC_YUV444)
1142     {
1143         ptr = pData->Y + pInfo->CropX + pInfo->CropY * pitch;
1144 
1145         // read luminance plane
1146         for(i = 0; i < h; i++)
1147         {
1148             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, w, m_fSrc);
1149             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA);
1150         }
1151 
1152         // load U
1153         ptr  = pData->U + (pInfo->CropX >> 1) + (pInfo->CropY >> 1) * pitch;
1154         for(i = 0; i < h; i++)
1155         {
1156             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, w, m_fSrc);
1157             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA);
1158         }
1159         // load V
1160         ptr  = pData->V + (pInfo->CropX >> 1) + (pInfo->CropY >> 1) * pitch;
1161         for(i = 0; i < h; i++)
1162         {
1163             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, w, m_fSrc);
1164             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA);
1165         }
1166     }
1167     else if (pInfo->FourCC == MFX_FOURCC_NV12)
1168     {
1169         ptr = pData->Y + pInfo->CropX + pInfo->CropY * pitch;
1170 
1171         // read luminance plane
1172         for (i = 0; i < h; i++)
1173         {
1174             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, w, m_fSrc);
1175             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA);
1176         }
1177 
1178         switch (m_initFcc)
1179         {
1180             case MFX_FOURCC_NV12:
1181             {
1182                 // load UV
1183                 h >>= 1;
1184                 ptr = pData->UV + pInfo->CropX + (pInfo->CropY >> 1) * pitch;
1185                 for (i = 0; i < h; i++)
1186                 {
1187                     nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, w, m_fSrc);
1188                     IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA);
1189                 }
1190                 break;
1191             }
1192             case MFX_FOURCC_I420:
1193             case MFX_FOURCC_YV12:
1194             {
1195                 mfxU8 buf[2048]; // maximum supported chroma width for nv12
1196                 mfxU32 j, dstOffset[2];
1197                 w /= 2;
1198                 h /= 2;
1199                 ptr = pData->UV + pInfo->CropX + (pInfo->CropY / 2) * pitch;
1200                 if (w > 2048)
1201                 {
1202                     return MFX_ERR_UNSUPPORTED;
1203                 }
1204 
1205                 if (m_initFcc == MFX_FOURCC_I420) {
1206                     dstOffset[0] = 0;
1207                     dstOffset[1] = 1;
1208                 }
1209                 else {
1210                     dstOffset[0] = 1;
1211                     dstOffset[1] = 0;
1212                 }
1213 
1214                 // load first chroma plane: U (input == I420) or V (input == YV12)
1215                 for (i = 0; i < h; i++)
1216                 {
1217                     nBytesRead = (mfxU32)fread(buf, 1, w, m_fSrc);
1218                     if (w != nBytesRead)
1219                     {
1220                         return MFX_ERR_MORE_DATA;
1221                     }
1222                     for (j = 0; j < w; j++)
1223                     {
1224                         ptr[i * pitch + j * 2 + dstOffset[0]] = buf[j];
1225                     }
1226                 }
1227 
1228                 // load second chroma plane: V (input == I420) or U (input == YV12)
1229                 for (i = 0; i < h; i++)
1230                 {
1231 
1232                     nBytesRead = (mfxU32)fread(buf, 1, w, m_fSrc);
1233 
1234                     if (w != nBytesRead)
1235                     {
1236                         return MFX_ERR_MORE_DATA;
1237                     }
1238                     for (j = 0; j < w; j++)
1239                     {
1240                         ptr[i * pitch + j * 2 + dstOffset[1]] = buf[j];
1241                     }
1242                 }
1243                 break;
1244             }
1245         }
1246     }
1247     else if( pInfo->FourCC == MFX_FOURCC_NV16 )
1248     {
1249         ptr = pData->Y + pInfo->CropX + pInfo->CropY * pitch;
1250 
1251         // read luminance plane
1252         for(i = 0; i < h; i++)
1253         {
1254             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, w, m_fSrc);
1255             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA);
1256         }
1257 
1258         // load UV
1259         ptr = pData->UV + pInfo->CropX + (pInfo->CropY >> 1) * pitch;
1260         for (i = 0; i < h; i++)
1261         {
1262             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, w, m_fSrc);
1263             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA);
1264         }
1265     }
1266     else if( pInfo->FourCC == MFX_FOURCC_P010
1267 #if (MFX_VERSION >= 1031)
1268           || pInfo->FourCC == MFX_FOURCC_P016
1269 #endif
1270             )
1271     {
1272         ptr = pData->Y + pInfo->CropX * 2 + pInfo->CropY * pitch;
1273 
1274         // read luminance plane
1275         for(i = 0; i < h; i++)
1276         {
1277             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, w * 2, m_fSrc);
1278             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, w*2, MFX_ERR_MORE_DATA);
1279         }
1280 
1281         // load UV
1282         h     >>= 1;
1283         ptr = pData->UV + pInfo->CropX + (pInfo->CropY >> 1) * pitch;
1284         for (i = 0; i < h; i++)
1285         {
1286             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, w*2, m_fSrc);
1287             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, w*2, MFX_ERR_MORE_DATA);
1288         }
1289     }
1290     else if( pInfo->FourCC == MFX_FOURCC_P210 )
1291     {
1292         ptr = pData->Y + pInfo->CropX * 2 + pInfo->CropY * pitch;
1293 
1294         // read luminance plane
1295         for(i = 0; i < h; i++)
1296         {
1297             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, w * 2, m_fSrc);
1298             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, w*2, MFX_ERR_MORE_DATA);
1299         }
1300 
1301         // load UV
1302         ptr = pData->UV + pInfo->CropX + (pInfo->CropY >> 1) * pitch;
1303         for (i = 0; i < h; i++)
1304         {
1305             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, w*2, m_fSrc);
1306             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, w*2, MFX_ERR_MORE_DATA);
1307         }
1308     }
1309 #if (MFX_VERSION >= 1028)
1310     else if (pInfo->FourCC == MFX_FOURCC_RGB565)
1311     {
1312         MSDK_CHECK_POINTER(pData->R, MFX_ERR_NOT_INITIALIZED);
1313         MSDK_CHECK_POINTER(pData->G, MFX_ERR_NOT_INITIALIZED);
1314         MSDK_CHECK_POINTER(pData->B, MFX_ERR_NOT_INITIALIZED);
1315 
1316         ptr = pData->B;
1317         ptr = ptr + pInfo->CropX + pInfo->CropY * pitch;
1318 
1319         for(i = 0; i < h; i++)
1320         {
1321             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, 2*w, m_fSrc);
1322             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, 2*w, MFX_ERR_MORE_DATA);
1323         }
1324     }
1325 #endif
1326     else if (pInfo->FourCC == MFX_FOURCC_RGB3)
1327     {
1328         MSDK_CHECK_POINTER(pData->R, MFX_ERR_NOT_INITIALIZED);
1329         MSDK_CHECK_POINTER(pData->G, MFX_ERR_NOT_INITIALIZED);
1330         MSDK_CHECK_POINTER(pData->B, MFX_ERR_NOT_INITIALIZED);
1331 
1332         ptr = std::min(std::min(pData->R, pData->G), pData->B );
1333         ptr = ptr + pInfo->CropX + pInfo->CropY * pitch;
1334 
1335         for(i = 0; i < h; i++)
1336         {
1337             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, 3*w, m_fSrc);
1338             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, 3*w, MFX_ERR_MORE_DATA);
1339         }
1340     }
1341     else if (pInfo->FourCC == MFX_FOURCC_RGB4 || pInfo->FourCC == MFX_FOURCC_A2RGB10)
1342     {
1343         MSDK_CHECK_POINTER(pData->R, MFX_ERR_NOT_INITIALIZED);
1344         MSDK_CHECK_POINTER(pData->G, MFX_ERR_NOT_INITIALIZED);
1345         MSDK_CHECK_POINTER(pData->B, MFX_ERR_NOT_INITIALIZED);
1346         // there is issue with A channel in case of d3d, so A-ch is ignored
1347         //MSDK_CHECK_POINTER(pData->A, MFX_ERR_NOT_INITIALIZED);
1348 
1349         ptr = std::min(std::min(pData->R, pData->G), pData->B );
1350         ptr = ptr + pInfo->CropX + pInfo->CropY * pitch;
1351 
1352         for(i = 0; i < h; i++)
1353         {
1354             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, 4*w, m_fSrc);
1355             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, 4*w, MFX_ERR_MORE_DATA);
1356         }
1357     }
1358     else if (pInfo->FourCC == MFX_FOURCC_YUY2)
1359     {
1360         ptr = pData->Y + pInfo->CropX + pInfo->CropY * pitch;
1361 
1362         for(i = 0; i < h; i++)
1363         {
1364             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, 2*w, m_fSrc);
1365             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, 2*w, MFX_ERR_MORE_DATA);
1366         }
1367     }
1368     else if (pInfo->FourCC == MFX_FOURCC_UYVY)
1369     {
1370         ptr = pData->U + pInfo->CropX + pInfo->CropY * pitch;
1371 
1372         for(i = 0; i < h; i++)
1373         {
1374             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, 2*w, m_fSrc);
1375             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, 2*w, MFX_ERR_MORE_DATA);
1376         }
1377     }
1378     else if (pInfo->FourCC == MFX_FOURCC_IMC3)
1379     {
1380         ptr = pData->Y + pInfo->CropX + pInfo->CropY * pitch;
1381 
1382         // read luminance plane
1383         for(i = 0; i < h; i++)
1384         {
1385             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, w, m_fSrc);
1386             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA);
1387         }
1388 
1389         h     >>= 1;
1390 
1391         // load U
1392         ptr  = pData->V + (pInfo->CropX >> 1) + (pInfo->CropY >> 1) * pitch;
1393         for(i = 0; i < h; i++)
1394         {
1395             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, w, m_fSrc);
1396             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA);
1397         }
1398         // load V
1399         ptr  = pData->U + (pInfo->CropX >> 1) + (pInfo->CropY >> 1) * pitch;
1400         for(i = 0; i < h; i++)
1401         {
1402             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, w, m_fSrc);
1403             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA);
1404         }
1405     }
1406     else if (pInfo->FourCC == MFX_FOURCC_AYUV)
1407     {
1408         ptr = std::min( std::min(pData->Y, pData->U), std::min(pData->V, pData->A) );
1409         ptr = ptr + pInfo->CropX + pInfo->CropY * pitch;
1410 
1411         for(i = 0; i < h; i++)
1412         {
1413             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, 4*w, m_fSrc);
1414             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, 4*w, MFX_ERR_MORE_DATA);
1415         }
1416     }
1417 #if (MFX_VERSION >= 1027)
1418     else if (pInfo->FourCC == MFX_FOURCC_Y210
1419 #if (MFX_VERSION >= 1031)
1420     || pInfo->FourCC == MFX_FOURCC_Y216
1421 #endif
1422 )
1423     {
1424         ptr = (mfxU8*)(pData->Y16 + pInfo->CropX * 2) + pInfo->CropY * pitch;
1425 
1426         for(i = 0; i < h; i++)
1427         {
1428             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, 4*w, m_fSrc);
1429             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, 4*w, MFX_ERR_MORE_DATA);
1430         }
1431     }
1432     else if (pInfo->FourCC == MFX_FOURCC_Y410)
1433     {
1434         ptr = (mfxU8*)(pData->Y410 + pInfo->CropX) + pInfo->CropY * pitch;
1435 
1436         for(i = 0; i < h; i++)
1437         {
1438             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, 4*w, m_fSrc);
1439             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, 4*w, MFX_ERR_MORE_DATA);
1440         }
1441     }
1442 #endif
1443 #if (MFX_VERSION >= 1031)
1444     else if (pInfo->FourCC == MFX_FOURCC_Y416)
1445     {
1446         ptr = (mfxU8*)(pData->U16 + pInfo->CropX * 4) + pInfo->CropY * pitch;
1447 
1448         for (i = 0; i < h; i++)
1449         {
1450             nBytesRead = (mfxU32)fread(ptr + i * pitch, 1, 8 * w, m_fSrc);
1451             IOSTREAM_MSDK_CHECK_NOT_EQUAL(nBytesRead, 8 * w, MFX_ERR_MORE_DATA);
1452         }
1453     }
1454 #endif
1455     else
1456     {
1457         return MFX_ERR_UNSUPPORTED;
1458     }
1459 
1460     return MFX_ERR_NONE;
1461 }
1462 
1463 
GetNextInputFrame(sMemoryAllocator * pAllocator,mfxFrameInfo * pInfo,mfxFrameSurfaceWrap ** pSurface,mfxU16 streamIndex)1464 mfxStatus CRawVideoReader::GetNextInputFrame(sMemoryAllocator* pAllocator, mfxFrameInfo* pInfo, mfxFrameSurfaceWrap** pSurface, mfxU16 streamIndex)
1465 {
1466     mfxStatus sts;
1467     if (!m_isPerfMode)
1468     {
1469         sts = GetFreeSurface(pAllocator->pSurfacesIn[streamIndex], pAllocator->responseIn[streamIndex].NumFrameActual, pSurface);
1470         MSDK_CHECK_STATUS(sts,"GetFreeSurface failed");
1471 
1472         mfxFrameSurfaceWrap* pCurSurf = *pSurface;
1473         if (pCurSurf->Data.MemId || pAllocator->bUsedAsExternalAllocator)
1474         {
1475             // get YUV pointers
1476             sts = pAllocator->pMfxAllocator->Lock(pAllocator->pMfxAllocator->pthis, pCurSurf->Data.MemId, &pCurSurf->Data);
1477             MFX_CHECK_STS(sts);
1478             sts = LoadNextFrame(&pCurSurf->Data, pInfo);
1479             MFX_CHECK_STS(sts);
1480             sts = pAllocator->pMfxAllocator->Unlock(pAllocator->pMfxAllocator->pthis, pCurSurf->Data.MemId, &pCurSurf->Data);
1481             MFX_CHECK_STS(sts);
1482         }
1483         else
1484         {
1485             sts = LoadNextFrame( &pCurSurf->Data, pInfo);
1486             MFX_CHECK_STS(sts);
1487         }
1488     }
1489     else
1490     {
1491         sts = GetPreAllocFrame(pSurface);
1492         MFX_CHECK_STS(sts);
1493     }
1494 
1495     if (m_pPTSMaker)
1496     {
1497         if (!m_pPTSMaker->SetPTS(*pSurface))
1498             return MFX_ERR_UNKNOWN;
1499     }
1500 
1501     return MFX_ERR_NONE;
1502  }
1503 
1504 
GetPreAllocFrame(mfxFrameSurfaceWrap ** pSurface)1505 mfxStatus  CRawVideoReader::GetPreAllocFrame(mfxFrameSurfaceWrap **pSurface)
1506 {
1507     if (m_it == m_SurfacesList.end())
1508     {
1509         m_Repeat--;
1510         m_it = m_SurfacesList.begin();
1511     }
1512 
1513     if (m_it->Data.Locked)
1514         return MFX_ERR_ABORTED;
1515 
1516     *pSurface = &(*m_it);
1517     m_it++;
1518     if (0 == m_Repeat)
1519         return MFX_ERR_MORE_DATA;
1520 
1521     return MFX_ERR_NONE;
1522 
1523 }
1524 
1525 
PreAllocateFrameChunk(mfxVideoParam * pVideoParam,sInputParams * pParams,MFXFrameAllocator * pAllocator)1526 mfxStatus  CRawVideoReader::PreAllocateFrameChunk(mfxVideoParam* pVideoParam,
1527     sInputParams* pParams,
1528     MFXFrameAllocator* pAllocator)
1529 {
1530     mfxStatus sts;
1531     mfxFrameAllocRequest  request;
1532     mfxFrameAllocResponse response;
1533     mfxFrameSurfaceWrap      surface;
1534     m_isPerfMode = true;
1535     m_Repeat = pParams->numRepeat;
1536     request.Info = pVideoParam->vpp.In;
1537     request.Type = (pParams->IOPattern & MFX_IOPATTERN_IN_VIDEO_MEMORY)?(MFX_MEMTYPE_FROM_VPPIN|MFX_MEMTYPE_INTERNAL_FRAME|MFX_MEMTYPE_DXVA2_PROCESSOR_TARGET):
1538         (MFX_MEMTYPE_FROM_VPPIN|MFX_MEMTYPE_INTERNAL_FRAME|MFX_MEMTYPE_SYSTEM_MEMORY);
1539     request.NumFrameSuggested = request.NumFrameMin = (mfxU16)pParams->numFrames;
1540     sts = pAllocator->Alloc(pAllocator, &request, &response);
1541     MFX_CHECK_STS(sts);
1542     for(;m_SurfacesList.size() < pParams->numFrames;)
1543     {
1544         surface.Data.Locked = 0;
1545         surface.Data.MemId = response.mids[m_SurfacesList.size()];
1546         surface.Info = pVideoParam->vpp.In;
1547         memset(surface.reserved, 0, sizeof(surface.reserved));
1548         sts = pAllocator->Lock(pAllocator->pthis, surface.Data.MemId, &surface.Data);
1549         MFX_CHECK_STS(sts);
1550         sts = LoadNextFrame(&surface.Data, &pVideoParam->vpp.In);
1551         MFX_CHECK_STS(sts);
1552         sts = pAllocator->Unlock(pAllocator->pthis, surface.Data.MemId, &surface.Data);
1553         MFX_CHECK_STS(sts);
1554         m_SurfacesList.push_back(surface);
1555     }
1556     m_it = m_SurfacesList.begin();
1557     return MFX_ERR_NONE;
1558 }
1559 /* ******************************************************************* */
1560 
CRawVideoWriter()1561 CRawVideoWriter::CRawVideoWriter()
1562 {
1563     m_fDst = 0;
1564     m_pPTSMaker = 0;
1565     m_forcedOutputFourcc = 0;
1566     return;
1567 }
1568 
Init(const msdk_char * strFileName,PTSMaker * pPTSMaker,mfxU32 forcedOutputFourcc)1569 mfxStatus CRawVideoWriter::Init(const msdk_char *strFileName, PTSMaker *pPTSMaker, mfxU32 forcedOutputFourcc)
1570 {
1571     Close();
1572 
1573     m_pPTSMaker = pPTSMaker;
1574     // no need to generate output
1575     if (0 == strFileName)
1576         return MFX_ERR_NONE;
1577 
1578     //CHECK_POINTER(strFileName, MFX_ERR_NULL_PTR);
1579 
1580     MSDK_FOPEN(m_fDst,strFileName, MSDK_STRING("wb"));
1581     MSDK_CHECK_POINTER(m_fDst, MFX_ERR_ABORTED);
1582     m_forcedOutputFourcc = forcedOutputFourcc;
1583 
1584     return MFX_ERR_NONE;
1585 }
1586 
~CRawVideoWriter()1587 CRawVideoWriter::~CRawVideoWriter()
1588 {
1589     Close();
1590 
1591     return;
1592 }
1593 
Close()1594 void CRawVideoWriter::Close()
1595 {
1596     if (m_fDst != 0){
1597 
1598         fclose(m_fDst);
1599         m_fDst = 0;
1600     }
1601 
1602     return;
1603 }
1604 
PutNextFrame(sMemoryAllocator * pAllocator,mfxFrameInfo * pInfo,mfxFrameSurfaceWrap * pSurface)1605 mfxStatus CRawVideoWriter::PutNextFrame(
1606     sMemoryAllocator* pAllocator,
1607     mfxFrameInfo* pInfo,
1608     mfxFrameSurfaceWrap* pSurface)
1609 {
1610     mfxStatus sts;
1611     if (m_fDst)
1612     {
1613         if (pSurface->Data.MemId)
1614         {
1615             // get YUV pointers
1616             sts = pAllocator->pMfxAllocator->Lock(pAllocator->pMfxAllocator->pthis, pSurface->Data.MemId, &(pSurface->Data));
1617             MSDK_CHECK_NOT_EQUAL(sts, MFX_ERR_NONE, MFX_ERR_ABORTED);
1618 
1619             sts = WriteFrame( &(pSurface->Data), pInfo);
1620             MSDK_CHECK_NOT_EQUAL(sts, MFX_ERR_NONE, MFX_ERR_ABORTED);
1621 
1622             sts = pAllocator->pMfxAllocator->Unlock(pAllocator->pMfxAllocator->pthis, pSurface->Data.MemId, &(pSurface->Data));
1623             MSDK_CHECK_NOT_EQUAL(sts, MFX_ERR_NONE, MFX_ERR_ABORTED);
1624         }
1625         else
1626         {
1627             sts = WriteFrame( &(pSurface->Data), pInfo);
1628             MSDK_CHECK_NOT_EQUAL(sts, MFX_ERR_NONE, MFX_ERR_ABORTED);
1629         }
1630     }
1631     else // performance mode
1632     {
1633         if (pSurface->Data.MemId)
1634         {
1635             sts = pAllocator->pMfxAllocator->Lock(pAllocator->pMfxAllocator->pthis, pSurface->Data.MemId, &(pSurface->Data));
1636             MSDK_CHECK_NOT_EQUAL(sts, MFX_ERR_NONE, MFX_ERR_ABORTED);
1637             sts = pAllocator->pMfxAllocator->Unlock(pAllocator->pMfxAllocator->pthis, pSurface->Data.MemId, &(pSurface->Data));
1638             MSDK_CHECK_NOT_EQUAL(sts, MFX_ERR_NONE, MFX_ERR_ABORTED);
1639         }
1640     }
1641     if (m_pPTSMaker)
1642         return m_pPTSMaker->CheckPTS(pSurface)?MFX_ERR_NONE:MFX_ERR_ABORTED;
1643 
1644     return MFX_ERR_NONE;
1645 }
WriteFrame(mfxFrameData * pData,mfxFrameInfo * pInfo)1646 mfxStatus CRawVideoWriter::WriteFrame(
1647     mfxFrameData* pData,
1648     mfxFrameInfo* pInfo)
1649 {
1650     mfxI32 nBytesRead   = 0;
1651 
1652     mfxI32 i, pitch;
1653     mfxU16 h, w;
1654     mfxU8* ptr;
1655 
1656     MSDK_CHECK_POINTER(pData, MFX_ERR_NOT_INITIALIZED);
1657     MSDK_CHECK_POINTER(pInfo, MFX_ERR_NOT_INITIALIZED);
1658     //-------------------------------------------------------
1659     mfxFrameData outData = *pData;
1660 
1661     if (pInfo->CropH > 0 && pInfo->CropW > 0)
1662     {
1663         w = pInfo->CropW;
1664         h = pInfo->CropH;
1665     }
1666     else
1667     {
1668         w = pInfo->Width;
1669         h = pInfo->Height;
1670     }
1671 
1672     pitch = outData.Pitch;
1673 
1674     if(pInfo->FourCC == MFX_FOURCC_YV12 || pInfo->FourCC == MFX_FOURCC_I420)
1675     {
1676 
1677         ptr   = outData.Y + (pInfo->CropX ) + (pInfo->CropY ) * pitch;
1678 
1679         for (i = 0; i < h; i++)
1680         {
1681             MSDK_CHECK_NOT_EQUAL(fwrite(ptr+ i * pitch, 1, w, m_fDst), w, MFX_ERR_UNDEFINED_BEHAVIOR);
1682         }
1683 
1684         w     >>= 1;
1685         h     >>= 1;
1686         pitch >>= 1;
1687 
1688         ptr  = (pInfo->FourCC == MFX_FOURCC_I420 ? outData.U : outData.V) + (pInfo->CropX >> 1) + (pInfo->CropY >> 1) * pitch;
1689         for(i = 0; i < h; i++)
1690         {
1691             nBytesRead = (mfxU32)fwrite(ptr + i * pitch, 1, w, m_fDst);
1692             MSDK_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA);
1693         }
1694 
1695         ptr  = (pInfo->FourCC == MFX_FOURCC_I420 ? outData.V : outData.U) + (pInfo->CropX >> 1) + (pInfo->CropY >> 1) * pitch;
1696         for(i = 0; i < h; i++)
1697         {
1698             MSDK_CHECK_NOT_EQUAL( fwrite(ptr+ i * pitch, 1, w, m_fDst), w, MFX_ERR_UNDEFINED_BEHAVIOR);
1699         }
1700     }
1701     else if(pInfo->FourCC == MFX_FOURCC_YUV400)
1702     {
1703         ptr   = pData->Y + (pInfo->CropX ) + (pInfo->CropY ) * pitch;
1704 
1705         for (i = 0; i < h; i++)
1706         {
1707             MSDK_CHECK_NOT_EQUAL( fwrite(ptr+ i * pitch, 1, w, m_fDst), w, MFX_ERR_UNDEFINED_BEHAVIOR);
1708         }
1709 
1710         w     >>= 1;
1711         h     >>= 1;
1712         pitch >>= 1;
1713 
1714         ptr  = pData->U + (pInfo->CropX >> 1) + (pInfo->CropY >> 1) * pitch;
1715         for(i = 0; i < h; i++)
1716         {
1717             nBytesRead = (mfxU32)fwrite(ptr + i * pitch, 1, w, m_fDst);
1718             MSDK_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA);
1719         }
1720 
1721         ptr  = pData->V + (pInfo->CropX >> 1) + (pInfo->CropY >> 1) * pitch;
1722         for(i = 0; i < h; i++)
1723         {
1724             MSDK_CHECK_NOT_EQUAL( fwrite(ptr+ i * pitch, 1, w, m_fDst), w, MFX_ERR_UNDEFINED_BEHAVIOR);
1725         }
1726     }
1727     else if(pInfo->FourCC == MFX_FOURCC_YUV411)
1728     {
1729         ptr   = pData->Y + (pInfo->CropX ) + (pInfo->CropY ) * pitch;
1730 
1731         for (i = 0; i < h; i++)
1732         {
1733             MSDK_CHECK_NOT_EQUAL( fwrite(ptr+ i * pitch, 1, w, m_fDst), w, MFX_ERR_UNDEFINED_BEHAVIOR);
1734         }
1735 
1736         w     /= 4;
1737         //pitch /= 4;
1738 
1739         ptr  = pData->U + (pInfo->CropX >> 1) + (pInfo->CropY >> 1) * pitch;
1740         for(i = 0; i < h; i++)
1741         {
1742             nBytesRead = (mfxU32)fwrite(ptr + i * pitch, 1, w, m_fDst);
1743             MSDK_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA);
1744         }
1745 
1746         ptr  = pData->V + (pInfo->CropX >> 1) + (pInfo->CropY >> 1) * pitch;
1747         for(i = 0; i < h; i++)
1748         {
1749             MSDK_CHECK_NOT_EQUAL( fwrite(ptr+ i * pitch, 1, w, m_fDst), w, MFX_ERR_UNDEFINED_BEHAVIOR);
1750         }
1751     }
1752     else if(pInfo->FourCC == MFX_FOURCC_YUV422H)
1753     {
1754         ptr   = pData->Y + (pInfo->CropX ) + (pInfo->CropY ) * pitch;
1755 
1756         for (i = 0; i < h; i++)
1757         {
1758             MSDK_CHECK_NOT_EQUAL( fwrite(ptr+ i * pitch, 1, w, m_fDst), w, MFX_ERR_UNDEFINED_BEHAVIOR);
1759         }
1760 
1761         w     >>= 1;
1762         //pitch >>= 1;
1763 
1764         ptr  = pData->U + (pInfo->CropX >> 1) + (pInfo->CropY >> 1) * pitch;
1765         for(i = 0; i < h; i++)
1766         {
1767             nBytesRead = (mfxU32)fwrite(ptr + i * pitch, 1, w, m_fDst);
1768             MSDK_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA);
1769         }
1770 
1771         ptr  = pData->V + (pInfo->CropX >> 1) + (pInfo->CropY >> 1) * pitch;
1772         for(i = 0; i < h; i++)
1773         {
1774             MSDK_CHECK_NOT_EQUAL( fwrite(ptr+ i * pitch, 1, w, m_fDst), w, MFX_ERR_UNDEFINED_BEHAVIOR);
1775         }
1776     }
1777     else if(pInfo->FourCC == MFX_FOURCC_YUV422V)
1778     {
1779         ptr   = pData->Y + (pInfo->CropX ) + (pInfo->CropY ) * pitch;
1780 
1781         for (i = 0; i < h; i++)
1782         {
1783             MSDK_CHECK_NOT_EQUAL( fwrite(ptr+ i * pitch, 1, w, m_fDst), w, MFX_ERR_UNDEFINED_BEHAVIOR);
1784         }
1785 
1786         h     >>= 1;
1787 
1788         ptr  = pData->U + (pInfo->CropX >> 1) + (pInfo->CropY >> 1) * pitch;
1789         for(i = 0; i < h; i++)
1790         {
1791             nBytesRead = (mfxU32)fwrite(ptr + i * pitch, 1, w, m_fDst);
1792             MSDK_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA);
1793         }
1794 
1795         ptr  = pData->V + (pInfo->CropX >> 1) + (pInfo->CropY >> 1) * pitch;
1796         for(i = 0; i < h; i++)
1797         {
1798             MSDK_CHECK_NOT_EQUAL( fwrite(ptr+ i * pitch, 1, w, m_fDst), w, MFX_ERR_UNDEFINED_BEHAVIOR);
1799         }
1800     }
1801     else if(pInfo->FourCC == MFX_FOURCC_YUV444)
1802     {
1803         ptr   = pData->Y + (pInfo->CropX ) + (pInfo->CropY ) * pitch;
1804 
1805         for (i = 0; i < h; i++)
1806         {
1807             MSDK_CHECK_NOT_EQUAL( fwrite(ptr+ i * pitch, 1, w, m_fDst), w, MFX_ERR_UNDEFINED_BEHAVIOR);
1808         }
1809 
1810         ptr  = pData->U + (pInfo->CropX >> 1) + (pInfo->CropY >> 1) * pitch;
1811         for(i = 0; i < h; i++)
1812         {
1813             nBytesRead = (mfxU32)fwrite(ptr + i * pitch, 1, w, m_fDst);
1814             MSDK_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA);
1815         }
1816 
1817         ptr  = pData->V + (pInfo->CropX >> 1) + (pInfo->CropY >> 1) * pitch;
1818         for(i = 0; i < h; i++)
1819         {
1820             MSDK_CHECK_NOT_EQUAL( fwrite(ptr+ i * pitch, 1, w, m_fDst), w, MFX_ERR_UNDEFINED_BEHAVIOR);
1821         }
1822     }
1823     else if (pInfo->FourCC == MFX_FOURCC_NV12)
1824     {
1825         ptr = pData->Y + (pInfo->CropX) + (pInfo->CropY) * pitch;
1826 
1827         for (i = 0; i < h; i++)
1828         {
1829             MSDK_CHECK_NOT_EQUAL(fwrite(ptr + i * pitch, 1, w, m_fDst), w, MFX_ERR_UNDEFINED_BEHAVIOR);
1830         }
1831 
1832         switch (m_forcedOutputFourcc)
1833         {
1834         case MFX_FOURCC_I420:
1835         {
1836             int j = 0;
1837 
1838             // write U plane first, then V plane
1839             h >>= 1;
1840             w >>= 1;
1841             ptr = pData->UV + (pInfo->CropX) + (pInfo->CropY >> 1) * pitch;
1842 
1843             for (i = 0; i < h; i++)
1844             {
1845                 for (j = 0; j < w; j++)
1846                 {
1847                     fputc(ptr[i*pitch + j * 2], m_fDst);
1848                 }
1849             }
1850             for (i = 0; i < h; i++)
1851             {
1852                 for (j = 0; j < w; j++)
1853                 {
1854                     fputc(ptr[i*pitch + j * 2 + 1], m_fDst);
1855                 }
1856             }
1857         }
1858         break;
1859 
1860         case MFX_FOURCC_YV12:
1861         {
1862             int j = 0;
1863 
1864             // write V plane first, then U plane
1865             h >>= 1;
1866             w >>= 1;
1867             ptr = pData->UV + (pInfo->CropX) + (pInfo->CropY >> 1) * pitch;
1868 
1869             for (i = 0; i < h; i++)
1870             {
1871                 for (j = 0; j < w; j++)
1872                 {
1873                     fputc(ptr[i*pitch + j * 2 + 1], m_fDst);
1874                 }
1875             }
1876             for (i = 0; i < h; i++)
1877             {
1878                 for (j = 0; j < w; j++)
1879                 {
1880                     fputc(ptr[i*pitch + j * 2], m_fDst);
1881                 }
1882             }
1883         }
1884         break;
1885 
1886         default:
1887         {
1888             // write UV data
1889             h >>= 1;
1890             ptr = pData->UV + (pInfo->CropX) + (pInfo->CropY >> 1) * pitch;
1891 
1892             for (i = 0; i < h; i++)
1893             {
1894                 MSDK_CHECK_NOT_EQUAL(fwrite(ptr + i * pitch, 1, w, m_fDst), w, MFX_ERR_UNDEFINED_BEHAVIOR);
1895             }
1896         }
1897         break;
1898         }
1899     }
1900     else if( pInfo->FourCC == MFX_FOURCC_NV16 )
1901     {
1902         ptr   = pData->Y + (pInfo->CropX ) + (pInfo->CropY ) * pitch;
1903 
1904         for (i = 0; i < h; i++)
1905         {
1906             MSDK_CHECK_NOT_EQUAL( fwrite(ptr+ i * pitch, 1, w, m_fDst), w, MFX_ERR_UNDEFINED_BEHAVIOR);
1907         }
1908 
1909         // write UV data
1910         ptr  = pData->UV + (pInfo->CropX ) + (pInfo->CropY >> 1) * pitch;
1911 
1912         for(i = 0; i < h; i++)
1913         {
1914             MSDK_CHECK_NOT_EQUAL( fwrite(ptr+ i * pitch, 1, w, m_fDst), w, MFX_ERR_UNDEFINED_BEHAVIOR);
1915         }
1916     }
1917     else if(    pInfo->FourCC == MFX_FOURCC_P010
1918 #if (MFX_VERSION >= 1031)
1919              || pInfo->FourCC == MFX_FOURCC_P016
1920 #endif
1921            )
1922     {
1923         ptr   = pData->Y + (pInfo->CropX ) + (pInfo->CropY ) * pitch;
1924 
1925         for (i = 0; i < h; i++)
1926         {
1927             MSDK_CHECK_NOT_EQUAL( fwrite(ptr+ i * pitch, 1, w * 2, m_fDst), w * 2u, MFX_ERR_UNDEFINED_BEHAVIOR);
1928         }
1929 
1930         // write UV data
1931         h     >>= 1;
1932         ptr  = pData->UV + (pInfo->CropX ) + (pInfo->CropY >> 1) * pitch ;
1933 
1934         for(i = 0; i < h; i++)
1935         {
1936             MSDK_CHECK_NOT_EQUAL( fwrite(ptr+ i * pitch, 1, w*2, m_fDst), w*2u, MFX_ERR_UNDEFINED_BEHAVIOR);
1937         }
1938     }
1939     else if( pInfo->FourCC == MFX_FOURCC_P210 )
1940     {
1941         ptr   = pData->Y + (pInfo->CropX ) + (pInfo->CropY ) * pitch;
1942 
1943         for (i = 0; i < h; i++)
1944         {
1945             MSDK_CHECK_NOT_EQUAL( fwrite(ptr+ i * pitch, 1, w * 2, m_fDst), w * 2u, MFX_ERR_UNDEFINED_BEHAVIOR);
1946         }
1947 
1948         // write UV data
1949         ptr  = pData->UV + (pInfo->CropX ) + (pInfo->CropY >> 1) * pitch ;
1950 
1951         for(i = 0; i < h; i++)
1952         {
1953             MSDK_CHECK_NOT_EQUAL( fwrite(ptr+ i * pitch, 1, w*2, m_fDst), w*2u, MFX_ERR_UNDEFINED_BEHAVIOR);
1954         }
1955     }
1956     else if( pInfo->FourCC == MFX_FOURCC_YUY2 )
1957     {
1958         ptr = pData->Y + pInfo->CropX + pInfo->CropY * pitch;
1959 
1960         for(i = 0; i < h; i++)
1961         {
1962             MSDK_CHECK_NOT_EQUAL( fwrite(ptr+ i * pitch, 1, 2*w, m_fDst), 2u*w, MFX_ERR_UNDEFINED_BEHAVIOR);
1963         }
1964     }
1965     else if( pInfo->FourCC == MFX_FOURCC_UYVY )
1966     {
1967         ptr = pData->U + pInfo->CropX + pInfo->CropY * pitch;
1968 
1969         for(i = 0; i < h; i++)
1970         {
1971             MSDK_CHECK_NOT_EQUAL( fwrite(ptr+ i * pitch, 1, 2*w, m_fDst), 2u*w, MFX_ERR_UNDEFINED_BEHAVIOR);
1972         }
1973     }
1974     else if ( pInfo->FourCC == MFX_FOURCC_IMC3 )
1975     {
1976         ptr   = pData->Y + (pInfo->CropX ) + (pInfo->CropY ) * pitch;
1977 
1978         for (i = 0; i < h; i++)
1979         {
1980             MSDK_CHECK_NOT_EQUAL( fwrite(ptr+ i * pitch, 1, w, m_fDst), w, MFX_ERR_UNDEFINED_BEHAVIOR);
1981         }
1982 
1983         w     >>= 1;
1984         h     >>= 1;
1985 
1986         ptr  = pData->U + (pInfo->CropX >> 1) + (pInfo->CropY >> 1) * pitch;
1987         for(i = 0; i < h; i++)
1988         {
1989             nBytesRead = (mfxU32)fwrite(ptr + i * pitch, 1, w, m_fDst);
1990             MSDK_CHECK_NOT_EQUAL(nBytesRead, w, MFX_ERR_MORE_DATA);
1991         }
1992 
1993         ptr  = pData->V + (pInfo->CropX >> 1) + (pInfo->CropY >> 1) * pitch;
1994         for(i = 0; i < h; i++)
1995         {
1996             MSDK_CHECK_NOT_EQUAL( fwrite(ptr+ i * pitch, 1, w, m_fDst), w, MFX_ERR_UNDEFINED_BEHAVIOR);
1997         }
1998     }
1999     else if (pInfo->FourCC == MFX_FOURCC_RGB4 || pInfo->FourCC == MFX_FOURCC_A2RGB10)
2000     {
2001         MSDK_CHECK_POINTER(pData->R, MFX_ERR_NOT_INITIALIZED);
2002         MSDK_CHECK_POINTER(pData->G, MFX_ERR_NOT_INITIALIZED);
2003         MSDK_CHECK_POINTER(pData->B, MFX_ERR_NOT_INITIALIZED);
2004         // there is issue with A channel in case of d3d, so A-ch is ignored
2005         //MSDK_CHECK_POINTER(pData->A, MFX_ERR_NOT_INITIALIZED);
2006 
2007         ptr = std::min(std::min(pData->R, pData->G), pData->B );
2008         ptr = ptr + pInfo->CropX + pInfo->CropY * pitch;
2009 
2010         for(i = 0; i < h; i++)
2011         {
2012             MSDK_CHECK_NOT_EQUAL( fwrite(ptr + i * pitch, 1, 4*w, m_fDst), 4u*w, MFX_ERR_UNDEFINED_BEHAVIOR);
2013         }
2014     }
2015 #if !(defined(_WIN32) || defined(_WIN64))
2016     else if (pInfo->FourCC == MFX_FOURCC_RGBP)
2017     {
2018         MSDK_CHECK_POINTER(pData->R, MFX_ERR_NOT_INITIALIZED);
2019         MSDK_CHECK_POINTER(pData->G, MFX_ERR_NOT_INITIALIZED);
2020         MSDK_CHECK_POINTER(pData->B, MFX_ERR_NOT_INITIALIZED);
2021 
2022         ptr = pData->R + pInfo->CropX + pInfo->CropY * pitch;
2023         for(i = 0; i < h; i++)
2024         {
2025             MSDK_CHECK_NOT_EQUAL( fwrite(ptr + i * pitch, 1, w, m_fDst), w, MFX_ERR_UNDEFINED_BEHAVIOR);
2026         }
2027         ptr = pData->G + pInfo->CropX + pInfo->CropY * pitch;
2028         for(i = 0; i < h; i++)
2029         {
2030             MSDK_CHECK_NOT_EQUAL( fwrite(ptr + i * pitch, 1, w, m_fDst), w, MFX_ERR_UNDEFINED_BEHAVIOR);
2031         }
2032         ptr = pData->B + pInfo->CropX + pInfo->CropY * pitch;
2033         for(i = 0; i < h; i++)
2034         {
2035             MSDK_CHECK_NOT_EQUAL( fwrite(ptr + i * pitch, 1, w, m_fDst), w, MFX_ERR_UNDEFINED_BEHAVIOR);
2036         }
2037     }
2038 #endif
2039     else if (pInfo->FourCC == MFX_FOURCC_AYUV)
2040     {
2041         ptr = std::min( std::min(pData->Y, pData->U), std::min(pData->V, pData->A) );
2042         ptr = ptr + pInfo->CropX + pInfo->CropY * pitch;
2043 
2044         for(i = 0; i < h; i++)
2045         {
2046             MSDK_CHECK_NOT_EQUAL( fwrite(ptr + i * pitch, 1, 4*w, m_fDst), 4u*w, MFX_ERR_UNDEFINED_BEHAVIOR);
2047         }
2048     }
2049 #if (MFX_VERSION >= 1027)
2050     else if (pInfo->FourCC == MFX_FOURCC_Y210
2051 #if (MFX_VERSION >= 1031)
2052     || pInfo->FourCC == MFX_FOURCC_Y216
2053 #endif
2054 )
2055     {
2056         ptr = pData->Y + pInfo->CropX + pInfo->CropY * pitch;
2057 
2058         for(i = 0; i < h; i++)
2059         {
2060             MSDK_CHECK_NOT_EQUAL(fwrite(ptr + i * pitch, 1, 4*w, m_fDst), w * 4u, MFX_ERR_UNDEFINED_BEHAVIOR);
2061         }
2062     }
2063     else if (pInfo->FourCC == MFX_FOURCC_Y410)
2064     {
2065         ptr = (mfxU8*)pData->Y410 + pInfo->CropX + pInfo->CropY * pitch;
2066 
2067         for(i = 0; i < h; i++)
2068         {
2069             MSDK_CHECK_NOT_EQUAL(fwrite(ptr + i * pitch, 1, 4*w, m_fDst), w * 4u, MFX_ERR_UNDEFINED_BEHAVIOR);
2070         }
2071     }
2072 #endif
2073 #if (MFX_VERSION >= 1031)
2074     else if (pInfo->FourCC == MFX_FOURCC_Y416)
2075     {
2076         ptr = (mfxU8*)(pData->U16 + pInfo->CropX * 4) + pInfo->CropY * pitch;
2077 
2078         for (i = 0; i < h; i++)
2079         {
2080             MSDK_CHECK_NOT_EQUAL(fwrite(ptr + i * pitch, 1, 8 * w, m_fDst), w * 8u, MFX_ERR_UNDEFINED_BEHAVIOR);
2081         }
2082     }
2083 #endif
2084     else
2085     {
2086         return MFX_ERR_UNSUPPORTED;
2087     }
2088 
2089     return MFX_ERR_NONE;
2090 }
2091 
2092 /* ******************************************************************* */
2093 
GeneralWriter()2094 GeneralWriter::GeneralWriter() :
2095     m_svcMode(false)
2096 {
2097 };
2098 
2099 
~GeneralWriter()2100 GeneralWriter::~GeneralWriter()
2101 {
2102     Close();
2103 };
2104 
2105 
Close()2106 void GeneralWriter::Close()
2107 {
2108     for(mfxU32 did = 0; did < 8; did++)
2109     {
2110         m_ofile[did].reset();
2111     }
2112 };
2113 
2114 
Init(const msdk_char * strFileName,PTSMaker * pPTSMaker,sSVCLayerDescr * pDesc,mfxU32 forcedOutputFourcc)2115 mfxStatus GeneralWriter::Init(
2116     const msdk_char *strFileName,
2117     PTSMaker *pPTSMaker,
2118     sSVCLayerDescr*  pDesc,
2119     mfxU32 forcedOutputFourcc)
2120 {
2121     mfxStatus sts = MFX_ERR_UNKNOWN;
2122 
2123     mfxU32 didCount = (pDesc) ? 8 : 1;
2124     m_svcMode = (pDesc) ? true : false;
2125 
2126     for(mfxU32 did = 0; did < didCount; did++)
2127     {
2128         if( (1 == didCount) || (pDesc[did].active) )
2129         {
2130             m_ofile[did].reset(new CRawVideoWriter() );
2131             if(0 == m_ofile[did].get())
2132             {
2133                 return MFX_ERR_UNKNOWN;
2134             }
2135 
2136             msdk_char out_buf[MSDK_MAX_FILENAME_LEN*4+20];
2137             msdk_char fname[MSDK_MAX_FILENAME_LEN];
2138 
2139 #if defined(_WIN32) || defined(_WIN64)
2140             {
2141                 msdk_char drive[MSDK_MAX_FILENAME_LEN];
2142                 msdk_char dir[MSDK_MAX_FILENAME_LEN];
2143                 msdk_char ext[MSDK_MAX_FILENAME_LEN];
2144 
2145                 _tsplitpath_s(
2146                     strFileName,
2147                     drive,
2148                     dir,
2149                     fname,
2150                     ext);
2151 
2152                 msdk_sprintf(out_buf, MSDK_STRING("%s%s%s_layer%i.yuv"), drive, dir, fname, did);
2153             }
2154 #else
2155             {
2156                 msdk_strncopy_s(fname, MSDK_MAX_FILENAME_LEN, strFileName, MSDK_MAX_FILENAME_LEN - 1);
2157                 fname[MSDK_MAX_FILENAME_LEN - 1] = 0;
2158                 char* pFound = strrchr(fname,'.');
2159                 if(pFound)
2160                 {
2161                     *pFound=0;
2162                 }
2163                 msdk_sprintf(out_buf, MSDK_STRING("%s_layer%i.yuv"), fname, did);
2164             }
2165 #endif
2166 
2167             sts = m_ofile[did]->Init(
2168                 (1 == didCount) ? strFileName : out_buf,
2169                 pPTSMaker,
2170                 forcedOutputFourcc);
2171 
2172             if(sts != MFX_ERR_NONE) break;
2173         }
2174     }
2175 
2176     return sts;
2177 };
2178 
PutNextFrame(sMemoryAllocator * pAllocator,mfxFrameInfo * pInfo,mfxFrameSurfaceWrap * pSurface)2179 mfxStatus  GeneralWriter::PutNextFrame(
2180     sMemoryAllocator* pAllocator,
2181     mfxFrameInfo* pInfo,
2182     mfxFrameSurfaceWrap* pSurface)
2183 {
2184     mfxU32 did = (m_svcMode) ? pSurface->Info.FrameId.DependencyId : 0;//aya: for MVC we have 1 out file only
2185 
2186     mfxStatus sts = m_ofile[did]->PutNextFrame(pAllocator, pInfo, pSurface);
2187 
2188     return sts;
2189 };
2190 
2191 /* ******************************************************************* */
2192 
UpdateSurfacePool(mfxFrameInfo SurfacesInfo,mfxU16 nPoolSize,mfxFrameSurfaceWrap * pSurface)2193 mfxStatus UpdateSurfacePool(mfxFrameInfo SurfacesInfo, mfxU16 nPoolSize, mfxFrameSurfaceWrap* pSurface)
2194 {
2195     MSDK_CHECK_POINTER(pSurface,     MFX_ERR_NULL_PTR);
2196     if (pSurface)
2197     {
2198         for (mfxU16 i = 0; i < nPoolSize; i++)
2199         {
2200             pSurface[i].Info = SurfacesInfo;
2201         }
2202     }
2203     return MFX_ERR_NONE;
2204 }
2205 
GetFreeSurface(mfxFrameSurfaceWrap * pSurfacesPool,mfxU16 nPoolSize,mfxFrameSurfaceWrap ** ppSurface)2206 mfxStatus GetFreeSurface(mfxFrameSurfaceWrap* pSurfacesPool, mfxU16 nPoolSize, mfxFrameSurfaceWrap** ppSurface)
2207 {
2208     MSDK_CHECK_POINTER(pSurfacesPool, MFX_ERR_NULL_PTR);
2209     MSDK_CHECK_POINTER(ppSurface,     MFX_ERR_NULL_PTR);
2210 
2211     mfxU32 timeToSleep = 10; // milliseconds
2212     mfxU32 numSleeps = MSDK_SURFACE_WAIT_INTERVAL / timeToSleep + 1; // at least 1
2213 
2214     mfxU32 i = 0;
2215 
2216     //wait if there's no free surface
2217     while ((MSDK_INVALID_SURF_IDX == GetFreeSurfaceIndex(pSurfacesPool, nPoolSize)) && (i < numSleeps))
2218     {
2219         MSDK_SLEEP(timeToSleep);
2220         i++;
2221     }
2222 
2223     mfxU16 index = GetFreeSurfaceIndex(pSurfacesPool, nPoolSize);
2224 
2225     if (index < nPoolSize)
2226     {
2227         *ppSurface = &(pSurfacesPool[index]);
2228         return MFX_ERR_NONE;
2229     }
2230 
2231     return MFX_ERR_NOT_ENOUGH_BUFFER;
2232 }
2233 
2234 //---------------------------------------------------------
2235 
PrintDllInfo()2236 void PrintDllInfo()
2237 {
2238 #if defined(_WIN32) || defined(_WIN64)
2239     HANDLE   hCurrent = GetCurrentProcess();
2240     HMODULE *pModules;
2241     DWORD    cbNeeded;
2242     int      nModules;
2243     if (NULL == EnumProcessModules(hCurrent, NULL, 0, &cbNeeded))
2244         return;
2245 
2246     nModules = cbNeeded / sizeof(HMODULE);
2247 
2248     pModules = new HMODULE[nModules];
2249     if (NULL == pModules)
2250     {
2251         return;
2252     }
2253     if (NULL == EnumProcessModules(hCurrent, pModules, cbNeeded, &cbNeeded))
2254     {
2255         delete []pModules;
2256         return;
2257     }
2258 
2259     for (int i = 0; i < nModules; i++)
2260     {
2261         msdk_char buf[2048];
2262         GetModuleFileName(pModules[i], buf, ARRAYSIZE(buf));
2263         if (_tcsstr(buf, MSDK_STRING("libmfx")))
2264         {
2265             msdk_printf(MSDK_STRING("MFX dll         %s\n"),buf);
2266         }
2267     }
2268     delete []pModules;
2269 #endif
2270 } // void PrintDllInfo()
2271 
2272 /* ******************************************************************* */
2273 
2274 /* EOF */
2275