1 // Copyright (c) 2017-2019 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 #ifndef _MFX_ENC_COMMON_H_
22 #define _MFX_ENC_COMMON_H_
23 
24 #include "umc_defs.h"
25 #include "mfxdefs.h"
26 #include "mfxstructures.h"
27 #include "umc_structures.h"
28 #include "mfx_common.h"
29 #include "mfx_common_int.h"
30 
31 class InputSurfaces
32 {
33 private:
34     bool                    m_bOpaq;
35     bool                    m_bSysMemFrames;
36     VideoCORE*              m_pCore;
37     mfxFrameAllocRequest    m_request;
38     mfxFrameAllocResponse   m_response;
39     bool                    m_bInitialized;
40     mfxFrameInfo            m_Info;
41 
42 
43 public:
InputSurfaces(VideoCORE * pCore)44     InputSurfaces(VideoCORE* pCore):
45       m_bOpaq(false),
46           m_bSysMemFrames(false),
47           m_pCore(pCore),
48           m_bInitialized (false)
49       {
50           memset(&m_request,  0, sizeof(mfxFrameAllocRequest));
51           memset(&m_response, 0, sizeof (mfxFrameAllocResponse));
52           memset(&m_Info,0,sizeof(mfxFrameInfo));
53       }
~InputSurfaces()54      virtual ~InputSurfaces()
55      {
56          Close();
57      }
58      mfxStatus Reset(mfxVideoParam *par, mfxU16 numFrameMin);
59 
60      mfxStatus Close();
61 
62 
isOpaq()63      inline bool isOpaq() {return  m_bOpaq;}
isSysMemFrames()64      inline bool isSysMemFrames () {return m_bSysMemFrames;}
65 
GetOriginalSurface(mfxFrameSurface1 * surface)66      inline mfxFrameSurface1 *GetOriginalSurface(mfxFrameSurface1 *surface)
67      {
68          return m_bOpaq ? m_pCore->GetNativeSurface(surface) : surface;
69      }
70 
GetOpaqSurface(mfxFrameSurface1 * surface)71      inline mfxFrameSurface1 *GetOpaqSurface(mfxFrameSurface1 *surface)
72      {
73          return m_bOpaq ? m_pCore->GetOpaqSurface(surface->Data.MemId) : surface;
74      }
75 };
76 
77 //----MFX data -> UMC data--------------------------------------------
78 uint8_t  CalculateMAXBFrames (mfxU8 GopRefDist);
79 uint16_t CalculateUMCGOPLength (mfxU16 GOPSize, mfxU8 targetUsage);
80 
81 bool SetPROParameters (mfxU8 TargetUsages,uint8_t &MESpeed, bool &UseFB, bool &FastFB,
82                        bool &bIntensityCompensation, bool &bChangeInterpolationType,
83                        bool &bChangeVLCTables,
84                        bool &bTrellisQuantization, bool &bUsePadding,
85                        bool &bVSTransform, bool &deblocking, mfxU8 &smoothing, bool &fastUVMC);
86 bool SetUFParameters(mfxU8 TargetUsages, bool& mixed,uint32_t& twoRef );
87 
88 uint32_t CalculateUMCBitrate(mfxU16    TargetKbps);
89 
90 double CalculateUMCFramerate(mfxU32 FrameRateExtN, mfxU32 FrameRateExtD);
91 void CalculateMFXFramerate(double framerate, mfxU32* FrameRateExtN, mfxU32* FrameRateExtD);
92 void ConvertFrameRateMPEG2(mfxU32 FrameRateExtD, mfxU32 FrameRateExtN, mfxI32 &frame_rate_code, mfxI32 &frame_rate_extension_n, mfxI32 &frame_rate_extension_d);
93 //void ConvertFrameRateMPEG2(mfxU32 FrameRateExtD, mfxU32 FrameRateExtN, mfxI32 &frame_rate_code, mfxI32 &frame_rate_extension_n, mfxI32 &frame_rate_extension_d);
94 
95 mfxStatus CheckFrameRateMPEG2(mfxU32 &FrameRateExtD, mfxU32 &FrameRateExtN);
96 mfxStatus CheckAspectRatioMPEG2 (mfxU16 &aspectRatioW, mfxU16 &aspectRatioH, mfxU32 frame_width, mfxU32 frame_heigth, mfxU16 cropW, mfxU16 cropH);
97 
98 
99 bool IsFrameRateMPEG2Supported(mfxU32 FrameRateExtD, mfxU32 FrameRateExtN);
100 bool IsAspectRatioMPEG2Supported (mfxU32 aspectRatioW, mfxU32 aspectRatioH, mfxU32 frame_width, mfxU32 frame_heigth, mfxU32 cropW, mfxU32 cropH);
101 mfxU8 GetAspectRatioCode (mfxU32 dispAspectRatioW, mfxU32 dispAspectRatioH);
102 bool RecalcFrameMPEG2Rate (mfxU32 FrameRateExtD, mfxU32 FrameRateExtN, mfxU32 &OutFrameRateExtD, mfxU32 &OutFrameRateExtN);
103 mfxU32 TranslateMfxFRCodeMPEG2(mfxFrameInfo *info, mfxU32 *codeN, mfxU32* codeD); // returns mpeg2 fr code
104 
105 mfxExtBuffer*       GetExtBuffer       (mfxExtBuffer** ebuffers, mfxU32 nbuffers, mfxU32 BufferId);
106 mfxExtCodingOption* GetExtCodingOptions(mfxExtBuffer** ebuffers, mfxU32 nbuffers);
107 mfxExtVideoSignalInfo* GetExtVideoSignalInfo(mfxExtBuffer** ebuffers, mfxU32 nbuffers);
108 
109 mfxStatus CheckExtVideoSignalInfo(mfxExtVideoSignalInfo * videoSignalInfo);
110 
min4(mfxI32 a,mfxI32 b,mfxI32 c,mfxI32 d)111 inline mfxI32 min4(mfxI32 a, mfxI32 b,mfxI32 c,mfxI32 d)
112 {
113     if (a>b)
114     {
115         if (c<d)
116         {
117             return (b<c)? b:c;
118         }
119         else
120         {
121              return (b<d)? b:d;
122 
123         }
124 
125     }
126     else
127     {
128         if (c<d)
129         {
130             return (a<c)? a:c;
131         }
132         else
133         {
134              return (a<d)? a:d;
135 
136         }
137     }
138 }
139 
140 UMC::FrameType GetFrameType (mfxU16 FrameOrder, mfxInfoMFX* info);
141 
142 //----UMC data -> MFX data--------------------------------------------
143 mfxU16 CalculateMFXGOPLength (uint16_t GOPSize);
144 mfxU8 CalculateGopRefDist(mfxU8 BNum);
145 
isIntra(mfxU8 FrameType)146 inline bool isIntra(mfxU8 FrameType)
147 {
148     return (FrameType & MFX_FRAMETYPE_I);
149 }
150 bool CorrectProfileLevelMpeg2(mfxU16 &profile, mfxU16 & level, mfxU32 w, mfxU32 h, mfxF64 frame_rate, mfxU32 bitrate, mfxU32 GopRefDist);
151 
152 
CalcDTSForRefFrameMpeg2(mfxI64 PTS,mfxI32 lastRefDist,mfxU32 maxRefDist,mfxF64 frameRate)153 inline mfxI64 CalcDTSForRefFrameMpeg2(mfxI64 PTS, mfxI32 lastRefDist, mfxU32 maxRefDist, mfxF64 frameRate)
154 {
155     return (maxRefDist == 1 || PTS == -1) ? PTS : PTS - (mfxI64)((1.0/frameRate)*(lastRefDist > 0 ? lastRefDist : 1)*90000);
156 }
CalcDTSForNonRefFrameMpeg2(mfxI64 PTS)157 inline mfxI64 CalcDTSForNonRefFrameMpeg2(mfxI64 PTS)
158 {
159     return PTS;
160 }
161 
162 struct Rational {mfxU64 n, d;};
163 
164 #define D3DFMT_NV12 (D3DFORMAT)(MFX_MAKEFOURCC('N', 'V', '1', '2'))
165 #define D3DDDIFMT_NV12 (D3DDDIFORMAT)(MFX_MAKEFOURCC('N', 'V', '1', '2'))
166 #define D3DDDIFMT_YU12 (D3DDDIFORMAT)(MFX_MAKEFOURCC('Y', 'U', '1', '2'))
167 
168 #endif //_MFX_ENC_COMMON_H_
169