1 /*
2 * Copyright (c) 2009-2017, Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22 //!
23 //! \file     media_libva.h
24 //! \brief    libva(and its extension) interface head file
25 //!
26 
27 #ifndef __MEDIA_LIBVA_H__
28 #define __MEDIA_LIBVA_H__
29 
30 #include <va/va.h>
31 #include <va/va_backend.h>
32 #include "va/va_dec_vp8.h"
33 #include <va/va_enc_h264.h>
34 #include <va/va_enc_mpeg2.h>
35 #include <va/va_enc_jpeg.h>
36 #include <va/va_dec_jpeg.h>
37 #include <va/va_enc_vp8.h>
38 #include <va/va_dec_vp9.h>
39 #include <va/va_enc_hevc.h>
40 #include <va/va_vpp.h>
41 #include <va/va_backend_vpp.h>
42 #ifdef ANDROID
43 #if VA_MAJOR_VERSION < 1
44 #include "va_internal_android.h"
45 #endif
46 #endif // ANDROID
47 #include <va/va_dec_hevc.h>
48 #include "codechal.h"
49 #include "codechal_decoder.h"
50 #include "codechal_encoder_base.h"
51 #include "media_libva_common.h"
52 
53 #define DDI_CODEC_GEN_MAX_PROFILES                 31   //  the number of va profiles, some profiles in va_private.h
54 #define DDI_CODEC_GEN_MAX_ENTRYPOINTS              7    // VAEntrypointVLD, VAEntrypointEncSlice, VAEntrypointEncSliceLP, VAEntrypointVideoProc
55 
56 #define DDI_CODEC_GEN_MAX_IMAGE_FORMATS            2    // NV12 and P010
57 #define DDI_CODEC_GEN_MAX_SUBPIC_FORMATS           4    // no sub-pic blending support, still set to 4 for further implementation
58 #if VA_MAJOR_VERSION < 1
59 #define DDI_CODEC_GEN_MAX_DISPLAY_ATTRIBUTES       4
60 #else
61 #define DDI_CODEC_GEN_MAX_DISPLAY_ATTRIBUTES       0    // set it to zero, unsupported.
62 #endif
63 #define DDI_CODEC_GEN_MAX_ATTRIBS_TYPE             4    //VAConfigAttribRTFormat,    VAConfigAttribRateControl,    VAConfigAttribDecSliceMode,    VAConfigAttribEncPackedHeaders
64 
65 #define DDI_CODEC_GEN_MAX_SURFACE_ATTRIBUTES       22
66 #define DDI_CODEC_GEN_STR_VENDOR                   "Intel iHD driver for Intel(R) Gen Graphics - " MEDIA_VERSION " (" MEDIA_VERSION_DETAILS ")"
67 
68 #define DDI_CODEC_GET_VTABLE(ctx)                  (ctx->vtable)
69 #define DDI_CODEC_GET_VTABLE_VPP(ctx)              (ctx->vtable_vpp)
70 #define DDI_CODEC_GET_VTABLE_TPI(ctx)              (ctx->vtable_tpi)
71 
72 #define DDI_CODEC_BATCH_BUFFER_SIZE                0x80000
73 #define DDI_CODEC_LEFT_SHIFT_FOR_REFLIST1          16
74 
75 /* Number of supported input color formats */
76 #define DDI_VP_NUM_INPUT_COLOR_STD    6
77 /* Number of supported output color formats */
78 #define DDI_VP_NUM_OUT_COLOR_STD      6
79 /* Number of forward references */
80 #define DDI_CODEC_NUM_FWD_REF         0
81 /* Number of backward references */
82 #define DDI_CODEC_NUM_BK_REF          0
83 /* Number of vp surface attributes */
84 #define DDI_CODEC_NUM_QUERY_ATTR_VP   9
85 
86 #define DDI_CODEC_MAX_BITSTREAM_BUFFER        16
87 #define DDI_CODEC_MAX_BITSTREAM_BUFFER_MINUS1 (DDI_CODEC_MAX_BITSTREAM_BUFFER - 1)
88 #define DDI_CODEC_BITSTREAM_BUFFER_INDEX_BITS 4  //the bitstream buffer index is 4 bits length
89 #define DDI_CODEC_MAX_BITSTREAM_BUFFER_INDEX  0xF  // the maximum bitstream buffer index is 0xF
90 #define DDI_CODEC_INVALID_BUFFER_INDEX        -1
91 #define DDI_CODEC_VP8_MAX_REF_FRAMES          5
92 #define DDI_CODEC_MIN_VALUE_OF_MAX_BS_SIZE    10240
93 #define DDI_CODEC_VDENC_MAX_L0_REF_FRAMES     3
94 #define DDI_CODEC_VDENC_MAX_L1_REF_FRAMES     0
95 
96 #define DDI_CODEC_FEI_MAX_NUM_MVPREDICTOR     4
97 #define DDI_CODEC_FEI_MAX_INTERFACE_REVISION  1000
98 #define DDI_CODEC_FEI_CTB_CMD_SIZE_SKL        16
99 #define DDI_CODEC_FEI_CU_RECORD_SIZE_SKL_KBL  64
100 #define DDI_CODEC_STATS_MAX_NUM_PAST_REFS     1
101 #define DDI_CODEC_STATS_MAX_NUM_FUTURE_REFS   1
102 #define DDI_CODEC_STATS_MAX_NUM_OUTPUTS       3
103 #define DDI_CODEC_STATS_INTERLACED_SUPPORT    1
104 
105 #define DDI_CODEC_GEN_CONFIG_ATTRIBUTES_DEC_BASE   0    // Dec config_id starts at this value
106 
107 #define DDI_CODEC_GEN_CONFIG_ATTRIBUTES_ENC_BASE   1024 // Enc config_id starts at this value
108 
109 /* Some filters in va_private.h */
110 #define DDI_VP_MAX_NUM_FILTERS  8
111 
112 #define DDI_VP_GEN_CONFIG_ATTRIBUTES_BASE    2048 // VP config_id starts at this value
113 
114 // Enable unlimited output buffer, delete this build option (remove multiple output buffer) when it is verified
115 #define ENABLE_ENC_UNLIMITED_OUTPUT
116 
117 typedef struct _DDI_CODEC_VC1BITPLANE_OBJECT
118 {
119     uint8_t       *pBitPlaneBase;
120     bool           bUsed;
121 } DDI_CODEC_VC1BITPLANE_OBJECT;
122 
123 typedef struct _DDI_CODEC_BITSTREAM_BUFFER_INFO
124 {
125     uint8_t            *pBaseAddress; // For JPEG it is a memory address when allocate slice data from CPU.
126     uint32_t            uiOffset;
127     uint32_t            uiLength;
128     VABufferID          vaBufferId;
129     bool                bRendered; // whether this slice data will be rendered.
130     PDDI_MEDIA_BUFFER   pMappedGPUBuffer; // the GPU mapping for this buffer.
131     bool                bIsUseExtBuf;
132     uint8_t            *pSliceBuf;
133 } DDI_CODEC_BITSTREAM_BUFFER_INFO;
134 
135 typedef struct _DDI_CODEC_BUFFER_PARAM_H264
136 {
137     // slice control buffer
138     VASliceParameterBufferH264                  *pVASliceParaBufH264;
139     VASliceParameterBufferBase                  *pVASliceParaBufH264Base;
140 
141     // one picture buffer
142     VAPictureParameterBufferH264                 PicParam264;
143 
144     // one IQ buffer
145     VAIQMatrixBufferH264                         IQm264;
146 } DDI_CODEC_BUFFER_PARAM_H264;
147 
148 typedef struct _DDI_CODEC_BUFFER_PARAM_MPEG2
149 {
150     // slice control buffer
151     VASliceParameterBufferMPEG2                  *pVASliceParaBufMPEG2;
152 
153     // one picture buffer
154     VAPictureParameterBufferMPEG2                 PicParamMPEG2;
155 
156     // one IQ buffer
157     VAIQMatrixBufferMPEG2                         IQmMPEG2;
158 } DDI_CODEC_BUFFER_PARAM_MPEG2;
159 
160 typedef struct _DDI_CODEC_BUFFER_PARAM_VC1
161 {
162     // slice control buffer
163     VASliceParameterBufferVC1                     *pVASliceParaBufVC1;
164 
165     // one picture buffer
166     VAPictureParameterBufferVC1                   PicParamVC1;
167     uint8_t                                      *pBitPlaneBuffer;
168     DDI_MEDIA_BUFFER                             *pVC1BitPlaneBuffObject[DDI_CODEC_MAX_BITSTREAM_BUFFER];
169     DDI_CODEC_VC1BITPLANE_OBJECT                  VC1BitPlane[DDI_CODEC_MAX_BITSTREAM_BUFFER];
170     uint32_t                                      dwVC1BitPlaneIndex;
171     MOS_RESOURCE                                  resBitPlaneBuffer;
172 } DDI_CODEC_BUFFER_PARAM_VC1;
173 
174 typedef struct _DDI_CODEC_BUFFER_PARAM_JPEG
175 {
176     // slice parameter buffer
177     VASliceParameterBufferJPEGBaseline                    *pVASliceParaBufJPEG;
178 
179     // picture parameter buffer
180     VAPictureParameterBufferJPEGBaseline                  PicParamJPEG;
181 
182     //IQ Matrix Buffer
183     VAIQMatrixBufferJPEGBaseline                          IQmJPEG;
184 } DDI_CODEC_BUFFER_PARAM_JPEG;
185 
186 typedef struct _DDI_CODEC_BUFFER_PARAM_VP8
187 {
188     // slice control buffer
189     VASliceParameterBufferVP8                   *pVASliceParaBufVP8;
190 
191     // one picture buffer
192     VAPictureParameterBufferVP8                  PicParamVP8;
193 
194     // one IQ buffer
195     VAIQMatrixBufferVP8                          IQmVP8;
196 
197     // Probability data
198     DDI_MEDIA_BUFFER                            *pVP8ProbabilityDataBuffObject;
199     MOS_RESOURCE                                 resProbabilityDataBuffer;
200     uint8_t                                     *pProbabilityDataBase;
201     VAProbabilityDataBufferVP8                   ProbabilityDataVP8;
202 
203     // Reference frames
204     DDI_MEDIA_SURFACE                           *pReferenceFrames[DDI_CODEC_VP8_MAX_REF_FRAMES];
205 } DDI_CODEC_BUFFER_PARAM_VP8;
206 
207 typedef struct _DDI_CODEC_BUFFER_PARAM_HEVC
208 {
209     // slice control buffer
210     VASliceParameterBufferHEVC                  *pVASliceParaBufHEVC;
211     VASliceParameterBufferBase                  *pVASliceParaBufBaseHEVC;
212     //slice control buffe for range extension
213     VASliceParameterBufferHEVCExtension          *pVASliceParaBufHEVCRext;
214 
215     // one picture buffer
216     VAPictureParameterBufferHEVC                 PicParamHEVC;
217 
218     //one picture buffer for range extension
219     VAPictureParameterBufferHEVCExtension        PicParamHEVCRext;
220 
221     // one IQ buffer
222     VAIQMatrixBufferHEVC                         IQmHEVC;
223 } DDI_CODEC_BUFFER_PARAM_HEVC;
224 
225 typedef struct _DDI_CODEC_BUFFER_PARAM_VP9
226 {
227     // one picture buffer
228     VADecPictureParameterBufferVP9               PicParamVP9;
229 
230     // slice control buffer: 8 * sizeof(VASegmentParameterVP9)
231     VASliceParameterBufferVP9                   *pVASliceParaBufVP9;
232 } DDI_CODEC_BUFFER_PARAM_VP9;
233 
234 typedef struct _DDI_CODEC_COM_BUFFER_MGR
235 {
236     // bitstream buffer
237     DDI_MEDIA_BUFFER                            *pBitStreamBuffObject[DDI_CODEC_MAX_BITSTREAM_BUFFER];
238     uint8_t                                     *pBitStreamBase[DDI_CODEC_MAX_BITSTREAM_BUFFER];
239     uint32_t                                     dwBitstreamIndex;   //indicating which bitstream buffer is used now
240     uint64_t                                     ui64BitstreamOrder; //save  bitstream buffer index used by previous 15 frames and current frame. the MSB is the oldest one, the LSB is current one.
241     MOS_RESOURCE                                 resBitstreamBuffer;
242     uint8_t                                     *pBitstreamBuffer;
243     DDI_CODEC_BITSTREAM_BUFFER_INFO             *pSliceData;
244     uint32_t                                     m_maxNumSliceData;
245     uint32_t                                     dwNumSliceData;
246     uint32_t                                     dwNumSliceControl;
247     uint32_t                                     dwMaxBsSize;
248 
249     uint32_t                                     dwSizeOfRenderedSliceData; // Size of all the rendered slice data buffer
250     uint32_t                                     dwNumOfRenderedSliceData; // how many slice data buffers will be rendered.
251     uint32_t                                     dwNumOfRenderedSlicePara; // how many slice parameters buffers will be rendered.
252     int32_t                                     *pNumOfRenderedSliceParaForOneBuffer; // how many slice headers in one slice parameter buffer.
253     int32_t                                     *pRenderedOrder; // a array to keep record the sequence when slice data rendered.
254     bool                                         bIsSliceOverSize;
255     //decode parameters
256     union
257     {
258         DDI_CODEC_BUFFER_PARAM_H264  Codec_Param_H264;
259         DDI_CODEC_BUFFER_PARAM_MPEG2 Codec_Param_MPEG2;
260         DDI_CODEC_BUFFER_PARAM_VC1   Codec_Param_VC1;
261         DDI_CODEC_BUFFER_PARAM_JPEG  Codec_Param_JPEG;
262         DDI_CODEC_BUFFER_PARAM_VP8   Codec_Param_VP8;
263         DDI_CODEC_BUFFER_PARAM_HEVC  Codec_Param_HEVC;
264         DDI_CODEC_BUFFER_PARAM_VP9   Codec_Param_VP9;
265     } Codec_Param;
266 
267     uint32_t                                     dwEncodeNumSliceControl;
268 
269     void                                        *pHDCP2ParameterBuffer;
270 
271     VACodedBufferSegment                        *pCodedBufferSegment; // For bitstream output
272     VAProcPipelineParameterBuffer                ProcPipelineParamBuffer;
273     VAProcFilterParameterBuffer                  ProcFilterParamBuffer;
274     VACodedBufferSegment                        *pCodedBufferSegmentForStatusReport; // for extended Status report such as long-term reference for VP8-F encode
275     void                                        *pCodecParamReserved;
276     void                                        *pCodecSlcParamReserved;
277 
278     // for External decode StreamOut Buffer
279     MOS_RESOURCE                                 resExternalStreamOutBuffer;
280 } DDI_CODEC_COM_BUFFER_MGR;
281 
282 typedef struct _DDI_CODEC_RENDER_TARGET_TABLE
283 {
284     int32_t                      iNumRenderTargets;
285     DDI_MEDIA_SURFACE           *pCurrentRT;           // raw input for encode
286     DDI_MEDIA_SURFACE           *pCurrentReconTarget;  // recon surface for encode
287     DDI_MEDIA_SURFACE           *pRT[DDI_MEDIA_MAX_SURFACE_NUMBER_CONTEXT];
288     uint8_t                      ucRTFlag[DDI_MEDIA_MAX_SURFACE_NUMBER_CONTEXT];
289 } DDI_CODEC_RENDER_TARGET_TABLE, *PDDI_CODEC_RENDER_TARGET_TABLE;
290 
291 #define DDI_CODEC_INVALID_FRAME_INDEX              0xffffffff
292 #define DDI_CODEC_NUM_MAX_REF_FRAME                16
293 
294 #define DDI_CODEC_NUM_MACROBLOCKS_WIDTH(dwWidth)     ((dwWidth + (CODECHAL_MACROBLOCK_WIDTH - 1)) / CODECHAL_MACROBLOCK_WIDTH)
295 #define DDI_CODEC_NUM_MACROBLOCKS_HEIGHT(dwHeight)   ((dwHeight + (CODECHAL_MACROBLOCK_HEIGHT - 1)) / CODECHAL_MACROBLOCK_HEIGHT)
296 
297 //! \brief  Map buffer
298 //!
299 //! \param  [in] ctx
300 //!     Pointer to VA driver context
301 //! \param  [in] buf_id
302 //!     VA buffer ID
303 //! \param  [out] pbuf
304 //!     Pointer to buffer
305 //!
306 //! \return VAStatus
307 //!     VA_STATUS_SUCCESS if success, else fail reason
308 //!
309 VAStatus DdiMedia_MapBuffer (
310     VADriverContextP    ctx,
311     VABufferID          buf_id,
312     void                **pbuf
313 );
314 
315 //! \brief  Unmap buffer
316 //!
317 //! \param  [in] ctx
318 //!     Pointer to VA driver context
319 //! \param  [in] buf_id
320 //!     VA buffer ID
321 //!
322 //! \return VAStatus
323 //!     VA_STATUS_SUCCESS if success, else fail reason
324 //!
325 VAStatus DdiMedia_UnmapBuffer (
326     VADriverContextP    ctx,
327     VABufferID          buf_id
328 );
329 
330 #ifdef __cplusplus
331 extern "C" {
332 #endif
333 
334 //! \brief  Hybrid query buffer attributes
335 //!
336 //! \param  [in] dpy
337 //!     VA display
338 //! \param  [in] context
339 //!     VA context ID
340 //! \param  [in] bufferType
341 //!     VA buffer type
342 //! \param  [out] outputData
343 //!     Output data
344 //! \param  [out] outputDataLen
345 //!     Length of output data
346 //!
347 //! \return VAStatus
348 //!     VA_STATUS_SUCCESS if success, else fail reason
349 //!
350 MEDIAAPI_EXPORT VAStatus DdiMedia_HybridQueryBufferAttributes(
351     VADisplay    dpy,
352     VAContextID  context,
353     VABufferType bufferType,
354     void        *outputData,
355     uint32_t    *outputDataLen);
356 
357 //! \brief  Set frame ID
358 //!
359 //! \param  [in] ctx
360 //!     Pointer to VA driver context
361 //! \param  [in] surface
362 //!     VA surface ID
363 //! \param  [in] frame_id
364 //!     Frame ID
365 //!
366 //! \return VAStatus
367 //!     VA_STATUS_SUCCESS if success, else fail reason
368 //!
369 VAStatus DdiMedia_SetFrameID(
370     VADriverContextP    ctx,
371     VASurfaceID         surface,
372     uint32_t            frame_id);
373 
374 #ifdef __cplusplus
375 }
376 #endif
377 
378 #endif // __MEDIA_LIBVA_H__
379 
380