1 // Copyright (c) 2018-2020 Intel Corporation
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in all
11 // copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 // SOFTWARE.
20 
21 #include "mfx_common.h"
22 
23 #if defined (MFX_ENABLE_VPP)
24 
25 #ifndef __MFX_VPP_DDI_H
26 #define __MFX_VPP_DDI_H
27 
28 #include <vector>
29 #include <memory>
30 #include "mfxdefs.h"
31 #include "libmfx_core.h"
32 #include "mfx_platform_headers.h"
33 
34 
35 #if defined(MFX_VA_LINUX)
36     typedef unsigned int   UINT;
37 
38     typedef struct tagVPreP_StatusParams_V1_0
39     {
40         UINT StatusReportID;
41         UINT Reserved1;
42 
43     } VPreP_StatusParams_V1_0;
44 
45     typedef struct tagFASTCOMP_QUERY_STATUS
46     {
47         UINT QueryStatusID;
48         UINT Status;
49 
50         UINT Reserved0;
51         UINT Reserved1;
52         UINT Reserved2;
53         UINT Reserved3;
54         UINT Reserved4;
55 
56     } FASTCOMP_QUERY_STATUS;
57 
58     typedef enum _VPREP_ISTAB_MODE
59     {
60         ISTAB_MODE_NONE,
61         ISTAB_MODE_BLACKEN,
62         ISTAB_MODE_UPSCALE
63     } VPREP_ISTAB_MODE;
64 #endif
65 
66 namespace MfxHwVideoProcessing
67 {
68     const mfxU32 g_TABLE_SUPPORTED_FOURCC [] =
69     {
70         MFX_FOURCC_NV12      ,
71         MFX_FOURCC_YV12      ,
72         MFX_FOURCC_NV16      ,
73         MFX_FOURCC_YUY2      ,
74         MFX_FOURCC_RGB3      ,
75         MFX_FOURCC_RGB4      ,
76 #if defined (MFX_ENABLE_FOURCC_RGB565)
77         MFX_FOURCC_RGB565    ,
78 #endif
79 #ifdef MFX_ENABLE_RGBP
80         MFX_FOURCC_RGBP      ,
81 #endif
82         MFX_FOURCC_P8        ,
83         MFX_FOURCC_P8_TEXTURE,
84         MFX_FOURCC_P010      ,
85         MFX_FOURCC_P210      ,
86         MFX_FOURCC_BGR4      ,
87         MFX_FOURCC_A2RGB10   ,
88         MFX_FOURCC_ARGB16    ,
89         MFX_FOURCC_R16       ,
90         MFX_FOURCC_AYUV      ,
91         MFX_FOURCC_AYUV_RGB4 ,
92         MFX_FOURCC_UYVY
93 #if (MFX_VERSION >= 1027)
94         , MFX_FOURCC_Y210
95         , MFX_FOURCC_Y410
96 #endif
97 #if (MFX_VERSION >= 1031)
98         , MFX_FOURCC_P016
99         , MFX_FOURCC_Y216
100         , MFX_FOURCC_Y416
101 #endif
102     };
103 
104     typedef enum mfxFormatSupport {
105         MFX_FORMAT_SUPPORT_INPUT   = 1,
106         MFX_FORMAT_SUPPORT_OUTPUT  = 2
107     } mfxFormatSupport;
108 
109     struct RateRational
110     {
111         mfxU32  FrameRateExtN;
112         mfxU32  FrameRateExtD;
113     };
114 
115     struct CustomRateData
116     {
117         RateRational customRate;
118         mfxU32       inputFramesOrFieldPerCycle;
119         mfxU32       outputIndexCountPerCycle;
120         mfxU32       bkwdRefCount;
121         mfxU32       fwdRefCount;
122 
123         mfxU32       indexRateConversion;
124     };
125 
126     struct FrcCaps
127     {
128         std::vector<CustomRateData> customRateData;
129     };
130 
131     struct DstRect
132     {
133         mfxU32 DstX;
134         mfxU32 DstY;
135         mfxU32 DstW;
136         mfxU32 DstH;
137         mfxU16 LumaKeyEnable;
138         mfxU16 LumaKeyMin;
139         mfxU16 LumaKeyMax;
140         mfxU16 GlobalAlphaEnable;
141         mfxU16 GlobalAlpha;
142         mfxU16 PixelAlphaEnable;
143         mfxU16 IOPattern; //sys/video/opaque memory
144         mfxU16 ChromaFormat;
145         mfxU32 FourCC;
146         mfxU32 TileId;
147     };
148 
149 
150     struct mfxVppCaps
151     {
152         mfxU32 uAdvancedDI;
153         mfxU32 uSimpleDI;
154         mfxU32 uInverseTC;
155         mfxU32 uDenoiseFilter;
156 #ifdef MFX_ENABLE_MCTF
157         mfxU32 uMCTF;
158 #endif
159         mfxU32 uDetailFilter;
160         mfxU32 uProcampFilter;
161         mfxU32 uSceneChangeDetection;
162 
163         mfxU32 uFrameRateConversion;
164         mfxU32 uDeinterlacing;
165         mfxU32 uVideoSignalInfo;
166         FrcCaps frcCaps;
167 
168         mfxU32 uIStabFilter;
169         mfxU32 uVariance;
170 
171         mfxI32 iNumBackwardSamples;
172         mfxI32 iNumForwardSamples;
173 
174         mfxU32 uMaxWidth;
175         mfxU32 uMaxHeight;
176 
177         mfxU32 uFieldWeavingControl;
178 
179         mfxU32 uRotation;
180 
181         mfxU32 uScaling;
182 
183         mfxU32 uChromaSiting;
184 
185         std::map <mfxU32, mfxU32> mFormatSupport;
186 
187         mfxU32 uMirroring;
188 
mfxVppCapsmfxVppCaps189         mfxVppCaps()
190             : uAdvancedDI(0)
191             , uSimpleDI(0)
192             , uInverseTC(0)
193             , uDenoiseFilter(0)
194 #ifdef MFX_ENABLE_MCTF
195             , uMCTF(0)
196 #endif
197             , uDetailFilter(0)
198             , uProcampFilter(0)
199             , uSceneChangeDetection(0)
200             , uFrameRateConversion(0)
201             , uDeinterlacing(0)
202             , uVideoSignalInfo(0)
203             , frcCaps()
204             , uIStabFilter(0)
205             , uVariance(0)
206             , iNumBackwardSamples(0)
207             , iNumForwardSamples(0)
208             , uMaxWidth(0)
209             , uMaxHeight(0)
210             , uFieldWeavingControl(0)
211             , uRotation(0)
212             , uScaling(0)
213             , uChromaSiting(0)
214             , mFormatSupport()
215             , uMirroring(0)
216         {
217         };
218     };
219 
220 
221     typedef struct _mfxDrvSurface
222     {
223         mfxFrameInfo frameInfo;
224         mfxHDLPair   hdl;
225 
226         mfxMemId     memId;
227         bool         bExternal;
228 
229         mfxU64       startTimeStamp;
230         mfxU64       endTimeStamp;
231 
232     } mfxDrvSurface;
233 
234     // Scene change detection
235     typedef enum {
236         VPP_NO_SCENE_CHANGE  = 0,
237         VPP_SCENE_NEW        = 1,            // BOB display current field to generate output
238         VPP_MORE_SCENE_CHANGE_DETECTED = 2 // BOB display only first field to avoid out of frame order
239     } vppScene;
240 
241     class mfxExecuteParams
242     {
243         struct SignalInfo {
244             bool   enabled;
245             mfxU16 TransferMatrix;
246             mfxU16 NominalRange;
247 
248             bool operator!=(const SignalInfo & other) const
249             {
250                 return enabled        != other.enabled
251                     || TransferMatrix != other.TransferMatrix
252                     || NominalRange   != other.NominalRange;
253             }
254         };
255 
256     public:
mfxExecuteParams()257             mfxExecuteParams():
258                 targetSurface()
259                ,targetTimeStamp()
260                ,pRefSurfaces(0)
261                ,refCount(0)
262                ,bkwdRefCount(0)
263                ,fwdRefCount(0)
264                ,iDeinterlacingAlgorithm(0)
265                ,bFMDEnable(false)
266                ,bDenoiseAutoAdjust(false)
267                ,denoiseFactor(0)
268                ,denoiseFactorOriginal(0)
269                ,bDetailAutoAdjust(false)
270                ,detailFactor(0)
271                ,detailFactorOriginal(0)
272                ,iTargetInterlacingMode(0)
273                ,bEnableProcAmp(false)
274                ,Brightness(0)
275                ,Contrast(0)
276                ,Hue(0)
277                ,Saturation(0)
278                ,bVarianceEnable(false)
279                ,bImgStabilizationEnable(false)
280                ,istabMode(0)
281                ,bFRCEnable(false)
282                ,frcModeOrig(0)
283                ,bComposite(false)
284                ,dstRects(0)
285                ,bBackgroundRequired(true)
286                ,iBackgroundColor(0)
287                ,iTilesNum4Comp(0)
288                ,execIdx(NO_INDEX)
289                ,initialStreamNum(0)
290                ,statusReportID(0)
291                ,bFieldWeaving(false)
292                ,iFieldProcessingMode(0)
293                ,rotation(0)
294                ,scalingMode(MFX_SCALING_MODE_DEFAULT)
295 #if (MFX_VERSION >= 1033)
296                ,interpolationMethod(MFX_INTERPOLATION_DEFAULT)
297 #endif
298 #if (MFX_VERSION >= 1025)
299                ,chromaSiting(MFX_CHROMA_SITING_UNKNOWN)
300 #endif
301                ,bEOS(false)
302                ,mirroring(0)
303                ,mirroringPosition(0)
304                ,mirroringExt(false)
305                ,scene(VPP_NO_SCENE_CHANGE)
306                ,bDeinterlace30i60p(false)
307 #ifdef MFX_ENABLE_MCTF
308                , bEnableMctf(false)
309                , MctfFilterStrength(0)
310 #ifdef MFX_ENABLE_MCTF_EXT
311                , MctfOverlap(MFX_CODINGOPTION_OFF)
312                , MctfBitsPerPixelx100k(12*100000)
313                , MctfDeblocking (MFX_CODINGOPTION_OFF)
314                , MctfTemporalMode(MFX_MCTF_TEMPORAL_MODE_2REF)
315                , MctfMVPrecision(MFX_MVPRECISION_INTEGER)
316 #endif
317 #endif
318                , reset(0)
319             {
320                    memset(&targetSurface, 0, sizeof(mfxDrvSurface));
321                    dstRects.clear();
322                    memset(&customRateData, 0, sizeof(CustomRateData));
323                    VideoSignalInfoIn.enabled         = false;
324                    VideoSignalInfoIn.NominalRange    = MFX_NOMINALRANGE_16_235;
325                    VideoSignalInfoIn.TransferMatrix  = MFX_TRANSFERMATRIX_BT601;
326                    VideoSignalInfoOut.enabled        = false;
327                    VideoSignalInfoOut.NominalRange   = MFX_NOMINALRANGE_16_235;
328                    VideoSignalInfoOut.TransferMatrix = MFX_TRANSFERMATRIX_BT601;
329 
330                    VideoSignalInfo.clear();
331                    VideoSignalInfo.assign(1, VideoSignalInfoIn);
332             };
333 
IsDoNothing()334             bool IsDoNothing()
335             {
336                 CustomRateData refCustomRateData;
337                 memset(&refCustomRateData, 0, sizeof(CustomRateData));
338                 if (memcmp(&refCustomRateData, &customRateData, sizeof(CustomRateData)))
339                     return false;
340                 if (iDeinterlacingAlgorithm != 0 ||
341                     bFMDEnable != 0 ||
342                     bDenoiseAutoAdjust != 0 ||
343                     bDetailAutoAdjust != 0 ||
344                     denoiseFactor != 0 ||
345                     detailFactor != 0 ||
346                     iTargetInterlacingMode != 0 ||
347                     bEnableProcAmp != false ||
348                     bVarianceEnable != false ||
349                     bImgStabilizationEnable != false ||
350                     bFRCEnable != false ||
351                     bComposite != false ||
352                     bFieldWeaving != false ||
353                     iFieldProcessingMode != 0 ||
354                     rotation != 0 ||
355                     scalingMode != MFX_SCALING_MODE_DEFAULT ||
356                     mirroring != 0 ||
357                     mirroringExt != false ||
358                     scene != VPP_NO_SCENE_CHANGE ||
359                     bDeinterlace30i60p != false
360 #if (MFX_VERSION >= 1025)
361                     || chromaSiting != MFX_CHROMA_SITING_UNKNOWN
362 #endif
363 #ifdef MFX_ENABLE_MCTF
364                     || bEnableMctf != false
365 #endif
366                 )
367                     return false;
368                 if (VideoSignalInfoIn != VideoSignalInfoOut)
369                     return false;
370                 return true;
371             };
372 
373         //surfaces
374         mfxDrvSurface  targetSurface;
375         mfxU64         targetTimeStamp;
376 
377         mfxDrvSurface* pRefSurfaces;
378         int            refCount; // refCount == bkwdRefCount + 1 + fwdRefCount
379 
380         int            bkwdRefCount; // filled from DdiTask
381         int            fwdRefCount;  //
382 
383         // params
384         mfxI32         iDeinterlacingAlgorithm; //0 - none, 1 - BOB, 2 - advanced (means reference need)
385         bool           bFMDEnable;
386 
387         bool           bDenoiseAutoAdjust;
388         mfxU16         denoiseFactor;
389         mfxU16         denoiseFactorOriginal; // Original denoise factor provided by app.
390 
391         bool           bDetailAutoAdjust;
392         mfxU16         detailFactor;
393         mfxU16         detailFactorOriginal;  // Original detail factor provided by app.
394 
395         mfxU32         iTargetInterlacingMode;
396 
397         bool           bEnableProcAmp;
398         mfxF64         Brightness;
399         mfxF64         Contrast;
400         mfxF64         Hue;
401         mfxF64         Saturation;
402 
403         bool           bVarianceEnable;
404         bool           bImgStabilizationEnable;
405         mfxU32         istabMode;
406 
407         bool           bFRCEnable;
408         CustomRateData customRateData;
409         mfxU16         frcModeOrig; // Original mode provided by app
410 
411         bool           bComposite;
412         std::vector<DstRect> dstRects;
413         bool           bBackgroundRequired;
414         mfxU64         iBackgroundColor;
415         mfxU32         iTilesNum4Comp;
416         mfxU32         execIdx; //index call of execute for current frame, actual for composition
417         mfxU32         initialStreamNum;
418 
419         mfxU32         statusReportID;
420 
421         bool           bFieldWeaving;
422 
423         mfxU32         iFieldProcessingMode;
424 
425         int         rotation;
426 
427         mfxU16      scalingMode;
428 
429 #if (MFX_VERSION >= 1033)
430         mfxU16      interpolationMethod;
431 #endif
432 
433         mfxU16      chromaSiting;
434 
435         bool        bEOS;
436 
437         SignalInfo VideoSignalInfoIn;   // Common video signal info set on Init
438         SignalInfo VideoSignalInfoOut;  // Video signal info for output
439 
440         std::vector<SignalInfo> VideoSignalInfo; // Video signal info for each frame in a single run
441 
442         int        mirroring;
443         int        mirroringPosition;
444         bool       mirroringExt;
445 
446         vppScene    scene;     // Keep information about scene change
447         bool        bDeinterlace30i60p;
448 
449 
450 #ifdef MFX_ENABLE_MCTF
451         bool         bEnableMctf;
452         mfxU16       MctfFilterStrength;
453 #ifdef MFX_ENABLE_MCTF_EXT
454         mfxU16       MctfOverlap;
455         mfxU32       MctfBitsPerPixelx100k;
456         mfxU16       MctfDeblocking;
457         mfxU16       MctfTemporalMode;
458         mfxU16       MctfMVPrecision;
459 #endif
460 #endif
461         bool reset;
462     };
463 
464     class DriverVideoProcessing
465     {
466     public:
467 
~DriverVideoProcessing(void)468         virtual ~DriverVideoProcessing(void){}
469 
470         virtual mfxStatus CreateDevice(VideoCORE * core, mfxVideoParam* par, bool isTemporal = false) = 0;
471 
472         virtual mfxStatus ReconfigDevice(mfxU32 indx) = 0;
473 
474         virtual mfxStatus DestroyDevice( void ) = 0;
475 
476         virtual mfxStatus Register(mfxHDLPair* pSurfaces, mfxU32 num, BOOL bRegister) = 0;
477 
478         virtual mfxStatus QueryTaskStatus(mfxU32 idx) = 0;
479 
480         virtual mfxStatus Execute(mfxExecuteParams *pParams) = 0;
481 
482         virtual mfxStatus QueryCapabilities( mfxVppCaps& caps ) = 0;
483 
484         virtual mfxStatus QueryVariance(
485             mfxU32 frameIndex,
486             std::vector<UINT> &variance) = 0;
487 
488     }; // DriverVideoProcessing
489 
490     DriverVideoProcessing* CreateVideoProcessing( VideoCORE* core );
491 
492 }; // namespace
493 
494 
495 /*
496  * Simple proxy for VPP device/caps create. It simplifies having a single
497  * cached vpp processing device accessible thru VideoCORE
498  */
499 class VPPHWResMng
500 {
501 public:
VPPHWResMng()502     VPPHWResMng(): m_ddi(nullptr), m_caps() {};
503 
~VPPHWResMng()504     ~VPPHWResMng() { Close(); };
505 
Close(void)506     mfxStatus Close(void){
507         m_ddi.reset(0);
508         return MFX_ERR_NONE;
509     }
510 
GetDevice(void)511     MfxHwVideoProcessing::DriverVideoProcessing *GetDevice(void) const {
512         return m_ddi.get();
513     };
514 
SetDevice(MfxHwVideoProcessing::DriverVideoProcessing * ddi)515     mfxStatus SetDevice(MfxHwVideoProcessing::DriverVideoProcessing *ddi){
516         MFX_CHECK_NULL_PTR1(ddi);
517         MFX_CHECK_STS(Close());
518         m_ddi.reset(ddi);
519         return MFX_ERR_NONE;
520     }
521 
GetCaps(void)522     MfxHwVideoProcessing::mfxVppCaps GetCaps(void) const {
523         return m_caps;
524     }
525 
SetCaps(const MfxHwVideoProcessing::mfxVppCaps & caps)526     mfxStatus  SetCaps(const MfxHwVideoProcessing::mfxVppCaps &caps){
527         m_caps = caps;
528         return MFX_ERR_NONE;
529     }
530 
531     mfxStatus CreateDevice(VideoCORE * core);
532 
533     // Just to make ResMang easier to use with existing code of DriverVideoProcessing
534     MfxHwVideoProcessing::DriverVideoProcessing *operator->() const {
535         return m_ddi.get();
536     }
537 
538 private:
539     VPPHWResMng(const VPPHWResMng &);
540     VPPHWResMng &operator=(const VPPHWResMng &);
541 
542     std::unique_ptr<MfxHwVideoProcessing::DriverVideoProcessing> m_ddi;
543     MfxHwVideoProcessing::mfxVppCaps                           m_caps;
544 };
545 
546 
547 #endif // __MFX_VPP_BASE_DDI_H
548 #endif // MFX_ENABLE_VPP
549 /* EOF */
550