xref: /aosp_15_r20/external/intel-media-driver/media_driver/linux/common/codec/ddi/media_libva_encoder.h (revision ba62d9d3abf0e404f2022b4cd7a85e107f48596f)
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_encoder.h
24 //! \brief     libva(and its extension) encoder head file
25 //!
26 
27 #ifndef  __MEDIA_LIBVA_ENCODER_H__
28 #define  __MEDIA_LIBVA_ENCODER_H__
29 
30 #include "media_libva.h"
31 #include "media_libva_cp_interface.h"
32 #include <vector>
33 
34 // change to 0x1000 for memory optimization, double check when implement slice header packing in app
35 #define PACKED_HEADER_SIZE_PER_ROW      0x1000
36 
37 #define DDI_ENCODE_MAX_STATUS_REPORT_BUFFER    CODECHAL_ENCODE_STATUS_NUM
38 
39 typedef enum _DDI_ENCODE_FEI_ENC_BUFFER_TYPE
40 {
41     FEI_ENC_BUFFER_TYPE_MVDATA     = 0,
42     FEI_ENC_BUFFER_TYPE_MBCODE     = 1,
43     FEI_ENC_BUFFER_TYPE_DISTORTION = 2,
44     FEI_ENC_BUFFER_TYPE_MAX        = 3
45 } DDI_ENCODE_FEI_ENC_BUFFER_TYPE;
46 
47 typedef enum _DDI_ENCODE_PRE_ENC_BUFFER_TYPE
48 {
49     PRE_ENC_BUFFER_TYPE_MVDATA     = 0,
50     PRE_ENC_BUFFER_TYPE_STATS      = 1,
51     PRE_ENC_BUFFER_TYPE_STATS_BOT  = 2,
52     PRE_ENC_BUFFER_TYPE_MAX        = 3
53 } DDI_ENCODE_PRE_ENC_BUFFER_TYPE;
54 
55 typedef struct _DDI_ENCODE_STATUS_REPORT_INFO
56 {
57     void           *pCodedBuf;              //encoded buffer address
58     uint32_t        uiSize;                 //encoded frame size
59     uint32_t        uiStatus;               // Encode frame status
60     uint32_t        uiInputCtr[4];          // Counter for HDCP2 session
61 } DDI_ENCODE_STATUS_REPORT_INFO;
62 
63 // ENC output buffer checking for FEI_ENC case only
64 typedef struct _DDI_ENCODE_STATUS_REPORT_ENC_INFO
65 {
66     void           *pEncBuf[3];             // ENC buffers address for Mvdata, MbCode and Distortion
67     uint32_t        uiBuffers;              // rendered ENC buffers
68     uint32_t        uiStatus;               // ENC frame status
69 } DDI_ENCODE_STATUS_REPORT_ENC_INFO;
70 
71 // PREENC output buffer checking
72 typedef struct _DDI_ENCODE_STATUS_REPORT_PREENC_INFO
73 {
74     void           *pPreEncBuf[3];          // PREENC buffers address for Mvdata and Statistics, Statistics of Bottom Field
75     uint32_t        uiBuffers;              // rendered ENC buffers
76     uint32_t        uiStatus;               // PREENC frame status
77 } DDI_ENCODE_STATUS_REPORT_PREENC_INFO;
78 
79 typedef struct _DDI_ENCODE_STATUS_REPORT_INFO_BUF
80 {
81     DDI_ENCODE_STATUS_REPORT_INFO          infos[DDI_ENCODE_MAX_STATUS_REPORT_BUFFER];
82     DDI_ENCODE_STATUS_REPORT_ENC_INFO      encInfos[DDI_ENCODE_MAX_STATUS_REPORT_BUFFER];
83     DDI_ENCODE_STATUS_REPORT_PREENC_INFO   preencInfos[DDI_ENCODE_MAX_STATUS_REPORT_BUFFER];
84     uint32_t                               ulHeadPosition;
85     uint32_t                               ulUpdatePosition;
86 } DDI_ENCODE_STATUS_REPORT_INFO_BUF;
87 
88 class DdiEncodeBase;
89 
90 typedef struct _DDI_ENCODE_CONTEXT
91 {
92     DdiEncodeBase                    *m_encode;
93     Codechal                         *pCodecHal;
94     CODECHAL_MODE                     wModeType;
95     CODECHAL_FUNCTION                 codecFunction;
96     VAProfile                         vaProfile;
97     VAEntrypoint                      vaEntrypoint;
98     void                             *pSeqParams;
99     void                             *pVuiParams;
100     void                             *pPicParams;
101     void                             *pSliceParams;
102     void                             *pEncodeStatusReport;
103     void                             *pQmatrixParams;
104     void                             *pFeiPicParams;
105     void                             *pVpxSegParams;
106     bool                              bMbDisableSkipMapEnabled;
107     MOS_RESOURCE                      resPerMBSkipMapBuffer;
108     void                             *pPreEncParams;
109     DDI_ENCODE_STATUS_REPORT_INFO_BUF statusReportBuf;
110     MOS_RESOURCE                      resBitstreamBuffer;
111     MOS_RESOURCE                      resMbCodeBuffer;
112     MOS_RESOURCE                      resProbCoeffBuffer;
113     MOS_SURFACE                       sCoeffSurface;
114     MOS_RESOURCE                      resMBQpBuffer;
115     //CP related
116     DdiCpInterface                   *pCpDdiInterface;
117 
118     uint32_t                          indexNALUnit;
119     uint8_t                           PicParamId;
120     MOS_SURFACE                       segMapBuffer;
121     uint32_t                          segmentMapDataSize;
122     uint8_t                          *pSegmentMap;
123     uint16_t                          wPicWidthInMB;          // Picture Width in MB width count
124     uint16_t                          wPicHeightInMB;         // Picture Height in MB height count
125     uint16_t                          wOriPicWidthInMB;
126     uint16_t                          wOriPicHeightInMB;
127     uint16_t                          wContextPicWidthInMB;
128     uint16_t                          wContextPicHeightInMB;
129     uint32_t                          dworiFrameWidth;        // Original Frame width in luma samples
130     uint32_t                          dworiFrameHeight;       // Original Frame height in luma samples
131     uint32_t                          dwFrameWidth;           // Frame width in luma samples
132     uint32_t                          dwFrameHeight;          // Frame height in luma samples
133     PBSBuffer                         pbsBuffer;
134     uint32_t                          dwNumSlices;
135     bool                              bNewSeq;
136     bool                              bPicQuant;
137     bool                              bNewQmatrixData;
138     bool                              bNewVuiData;
139     uint32_t                          uFrameRate;
140 
141     PCODECHAL_NAL_UNIT_PARAMS        *ppNALUnitParams;
142     PCODEC_ENCODER_SLCDATA            pSliceHeaderData;
143     uint32_t                          uiSliceHeaderCnt;
144     // For square region based rolling I
145     uint32_t                          uiIntraRefreshFrameCnt;
146     uint32_t                          uiIntraRefreshMBx;
147     uint32_t                          uiIntraRefreshMBy;
148 
149     // whether packed slice headers are passed from application;
150     bool                              bHavePackedSliceHdr;
151 
152     // whether the latest packed header type is for slice header
153     bool                              bLastPackedHdrIsSlice;
154 
155     // SEI stream passed by app
156     CodechalEncodeSeiData            *pSEIFromApp;
157     uint32_t                          uiRCMethod;
158 
159     uint32_t                          uiTargetBitRate;
160     uint32_t                          uiMaxBitRate;
161 
162     EncoderParams                     EncodeParams;
163 
164     //VDENC Enable flag
165     bool                              bVdencActive;
166     //VDENC Dynamic slice enabling
167     bool                              EnableSliceLevelRateCtrl;
168     //Per-MB Qp control
169     bool                              bMBQpEnable;
170 
171     DDI_CODEC_RENDER_TARGET_TABLE     RTtbl;
172     DDI_CODEC_COM_BUFFER_MGR          BufMgr;
173     PDDI_MEDIA_CONTEXT                pMediaCtx;
174 
175     uint8_t                           targetUsage;
176 
177 } DDI_ENCODE_CONTEXT, *PDDI_ENCODE_CONTEXT;
178 
179 typedef struct _DDI_ENCODE_MFE_CONTEXT
180 {
181     std::vector<PDDI_ENCODE_CONTEXT> pDdiEncodeContexts;            // Container to keep sub contexts
182     MEDIA_MUTEX_T                    encodeMfeMutex;
183     uint32_t                         currentStreamId;               // Current allocated id, increased monotonically
184     MfeSharedState                   *mfeEncodeSharedState;         // Keep shared state across sub contexts
185     bool                             isFEI;                         // Support legacy only or FEI only
186 }DDI_ENCODE_MFE_CONTEXT, *PDDI_ENCODE_MFE_CONTEXT;
187 
DdiEncode_GetEncContextFromPVOID(void * encCtx)188 static __inline PDDI_ENCODE_CONTEXT DdiEncode_GetEncContextFromPVOID (void *encCtx)
189 {
190     return (PDDI_ENCODE_CONTEXT)encCtx;
191 }
192 
193 //!
194 //! \brief  Get encode context from context ID
195 //!
196 //! \param  [in] ctx
197 //!     Pointer to VA driver context
198 //! \param  [in] vaCtxID
199 //!     VA context ID
200 //!
201 //! \return PDDI_ENCODE_CONTEXT
202 //!     Pointer to ddi encode context
203 //!
204 PDDI_ENCODE_CONTEXT DdiEncode_GetEncContextFromContextID (VADriverContextP ctx, VAContextID vaCtxID);
205 
206 //!
207 //! \brief  Remove from status report queue
208 //!
209 //! \param  [in] encCtx
210 //!     Pointer to ddi encode context
211 //! \param  [in] buf
212 //!     Pointer to ddi media buffer
213 //!
214 //! \return VAStatus
215 //!     VA_STATUS_SUCCESS if success, else fail reason
216 //!
217 VAStatus DdiEncode_RemoveFromStatusReportQueue(PDDI_ENCODE_CONTEXT  encCtx, PDDI_MEDIA_BUFFER buf);
218 
219 //!
220 //! \brief  Remove from encode status report queue
221 //!
222 //! \param  [in] encCtx
223 //!     Pointer to ddi encode context
224 //! \param  [in] buf
225 //!     Pointer to ddi media buffer
226 //! \param  [in] idx
227 //!     Ddi encode FEI encode buffer type
228 //!
229 //! \return VAStatus
230 //!     VA_STATUS_SUCCESS if success, else fail reason
231 //!
232 VAStatus DdiEncode_RemoveFromEncStatusReportQueue(PDDI_ENCODE_CONTEXT encCtx, PDDI_MEDIA_BUFFER buf, DDI_ENCODE_FEI_ENC_BUFFER_TYPE idx);
233 
234 //!
235 //! \brief  Remove form preencode status report queue
236 //!
237 //! \param  [in] encCtx
238 //!     Pointer to ddi encode context
239 //! \param  [in] buf
240 //!     Pointer to ddi media buffer
241 //! \param  [in] idx
242 //!     Ddi encode PRE encode buffer type
243 //!
244 //! \return VAStatus
245 //!     VA_STATUS_SUCCESS if success, else fail reason
246 //!
247 VAStatus DdiEncode_RemoveFromPreEncStatusReportQueue(PDDI_ENCODE_CONTEXT encCtx, PDDI_MEDIA_BUFFER buf, DDI_ENCODE_PRE_ENC_BUFFER_TYPE idx);
248 
249 //!
250 //! \brief  Get encode context from context ID
251 //!
252 //! \param  [in] ctx
253 //!     Pointer to VA driver context
254 //! \param  [in] vaCtxID
255 //!     VA context ID
256 //!
257 //! \return PDDI_ENCODE_CONTEXT
258 //!     Pointer to ddi encode context
259 //!
260 PDDI_ENCODE_CONTEXT DdiEncode_GetEncContextFromContextID (VADriverContextP ctx, VAContextID vaCtxID);
261 
262 //!
263 //! \brief  Coded buffer exist in status report
264 //!
265 //! \param  [in] encCtx
266 //!     Pointer to ddi encode context
267 //! \param  [in] buf
268 //!     Pointer to ddi media buffer
269 //!
270 //! \return bool
271 //!     true if call success, else false
272 //!
273 bool DdiEncode_CodedBufferExistInStatusReport(
274     PDDI_ENCODE_CONTEXT     encCtx,
275     PDDI_MEDIA_BUFFER       buf);
276 
277 //!
278 //! \brief  Encode buffer exist in status report
279 //!
280 //! \param  [in] encCtx
281 //!     Pointer to ddi encode context
282 //! \param  [in] buf
283 //!     Pointer to ddi media buffer
284 //! \param  [in] typeIdx
285 //!     Ddi encode FEI encode buffer type
286 //!
287 //! \return bool
288 //!     true if call success, else false
289 //!
290 bool DdiEncode_EncBufferExistInStatusReport(
291     PDDI_ENCODE_CONTEXT             encCtx,
292     PDDI_MEDIA_BUFFER               buf,
293     DDI_ENCODE_FEI_ENC_BUFFER_TYPE  typeIdx);
294 
295 //!
296 //! \brief  Pre encode buffer exist in status report
297 //!
298 //! \param  [in] encCtx
299 //!     Pointer to ddi encode context
300 //! \param  [in] buf
301 //!     Pointer to ddi media buffer
302 //! \param  [in] typeIdx
303 //!     Ddi encode PRE encode buffer type
304 //!
305 //! \return bool
306 //!     true if call success, else false
307 //!
308 bool DdiEncode_PreEncBufferExistInStatusReport(
309     PDDI_ENCODE_CONTEXT             encCtx,
310     PDDI_MEDIA_BUFFER               buf,
311     DDI_ENCODE_PRE_ENC_BUFFER_TYPE  typeIdx);
312 
313 //!
314 //! \brief  Encode status report
315 //!
316 //! \param  [in] encCtx
317 //!     Pointer to ddi encode context
318 //! \param  [in] mediaBuf
319 //!     Ddi media buffer
320 //! \param  [out] pbuf
321 //!     Pointer buffer
322 //!
323 //! \return VAStatus
324 //!     VA_STATUS_SUCCESS if success, else fail reason
325 //!
326 VAStatus DdiEncode_EncStatusReport (
327     PDDI_ENCODE_CONTEXT encCtx,
328     DDI_MEDIA_BUFFER    *mediaBuf,
329     void                **pbuf
330 );
331 
332 //!
333 //! \brief  Pre encode status report
334 //!
335 //! \param  [in] encCtx
336 //!     Pointer to ddi encode context
337 //! \param  [in] mediaBuf
338 //!     Ddi media buffer
339 //! \param  [out] pbuf
340 //!     Pointer buffer
341 //!
342 //! \return VAStatus
343 //!     VA_STATUS_SUCCESS if success, else fail reason
344 //!
345 VAStatus DdiEncode_PreEncStatusReport (
346     PDDI_ENCODE_CONTEXT encCtx,
347     DDI_MEDIA_BUFFER    *mediaBuf,
348     void                **pbuf
349 );
350 
351 //!
352 //! \brief  Status report
353 //!
354 //! \param  [in] encCtx
355 //!     Pointer to ddi encode context
356 //! \param  [in] mediaBuf
357 //!     Ddi media buffer
358 //! \param  [out] pbuf
359 //!     Pointer buffer
360 //!
361 //! \return VAStatus
362 //!     VA_STATUS_SUCCESS if success, else fail reason
363 //!
364 VAStatus DdiEncode_StatusReport (
365     PDDI_ENCODE_CONTEXT encCtx,
366     DDI_MEDIA_BUFFER    *mediaBuf,
367     void                **pbuf
368 );
369 
370 //!
371 //! \brief  Create context
372 //!
373 //! \param  [in] ctx
374 //!     Pointer to VA driver context
375 //! \param  [in] config_id
376 //!     VA configuration ID
377 //! \param  [in] picture_width
378 //!     The width of picture
379 //! \param  [in] picture_height
380 //!     The height of picture
381 //! \param  [in] flag
382 //!     Flag
383 //! \param  [in] render_targets
384 //!     Render targets
385 //! \param  [in] num_render_targets
386 //!     Number of render targets
387 //! \param  [in] context
388 //!     VA context ID
389 //!
390 //! \return VAStatus
391 //!     VA_STATUS_SUCCESS if success, else fail reason
392 //!
393 VAStatus DdiEncode_CreateContext (
394     VADriverContextP    ctx,
395     VAConfigID          config_id,
396     int32_t             picture_width,
397     int32_t             picture_height,
398     int32_t             flag,
399     VASurfaceID        *render_targets,
400     int32_t             num_render_targets,
401     VAContextID        *context
402 );
403 
404 //!
405 //! \brief  Destroy context
406 //!
407 //! \param  [in] ctx
408 //!     Pointer to VA driver context
409 //! \param  [in] context
410 //!     VA context ID
411 //!
412 //! \return VAStatus
413 //!     VA_STATUS_SUCCESS if success, else fail reason
414 //!
415 VAStatus DdiEncode_DestroyContext (
416     VADriverContextP    ctx,
417     VAContextID         context);
418 
419 //!
420 //! \brief  Create buffer
421 //!
422 //! \param  [in] ctx
423 //!     Pointer to VA driver context
424 //! \param  [in] context
425 //!     VA context ID
426 //! \param  [in] type
427 //!     VA buffer type
428 //! \param  [in] size
429 //!     Size
430 //! \param  [in] num_elements
431 //!     Number of elements
432 //! \param  [in] data
433 //!     Data
434 //! \param  [in] buf_id
435 //!     VA buffer ID
436 //!
437 //! \return VAStatus
438 //!     VA_STATUS_SUCCESS if success, else fail reason
439 //!
440 VAStatus DdiEncode_CreateBuffer (
441     VADriverContextP    ctx,
442     VAContextID         context,
443     VABufferType        type,
444     uint32_t            size,
445     uint32_t            num_elements,
446     void               *data,
447     VABufferID         *buf_id
448 );
449 
450 //!
451 //! \brief  Begin picture
452 //!
453 //! \param  [in] ctx
454 //!     Pointer to VA driver context
455 //! \param  [in] context
456 //!     VA context ID
457 //! \param  [in] render_target
458 //!     VA surface ID
459 //!
460 //! \return VAStatus
461 //!     VA_STATUS_SUCCESS if success, else fail reason
462 //!
463 VAStatus DdiEncode_BeginPicture (
464     VADriverContextP    ctx,
465     VAContextID         context,
466     VASurfaceID         render_target
467 );
468 
469 //!
470 //! \brief  Begin Picture
471 //!
472 //! \param  [in] ctx
473 //!     Pointer to VA driver context
474 //! \param  [in] context
475 //!     VA context ID
476 //! \param  [in] render_target
477 //!     VA surface ID
478 //!
479 //! \return VAStatus
480 //!     VA_STATUS_SUCCESS if success, else fail reason
481 //!
482 VAStatus vDdiEncode_BeginPicture (
483     VADriverContextP    ctx,
484     VAContextID         context,
485     VASurfaceID         render_target
486 );
487 
488 //!
489 //! \brief  Render picture
490 //!
491 //! \param  [in] ctx
492 //!     Pointer to VA driver context
493 //! \param  [in] context
494 //!     VA context ID
495 //! \param  [in] buffers
496 //!     VA buffer ID
497 //! \param  [in] num_buffers
498 //!     Number of buffers
499 //!
500 //! \return VAStatus
501 //!     VA_STATUS_SUCCESS if success, else fail reason
502 //!
503 VAStatus DdiEncode_RenderPicture (
504     VADriverContextP    ctx,
505     VAContextID         context,
506     VABufferID         *buffers,
507     int32_t             num_buffers
508 );
509 
510 //!
511 //! \brief  End picture
512 //!
513 //! \param  [in] ctx
514 //!     Pointer to VA driver context
515 //! \param  [in] context
516 //!     VA context ID
517 //!
518 //! \return VAStatus
519 //!     VA_STATUS_SUCCESS if success, else fail reason
520 //!
521 VAStatus DdiEncode_EndPicture(
522     VADriverContextP    ctx,
523     VAContextID         context);
524 
525 //!
526 //! \brief  MFE submit
527 //!
528 //! \param  [in] ctx
529 //!     Pointer to VA driver context
530 //! \param  [in] mfe_context
531 //!     VA MF context ID
532 //! \param  [in] contexts
533 //!     VA context ID
534 //! \param  [in] num_contexts
535 //!     Number of contexts
536 //!
537 //! \return VAStatus
538 //!     VA_STATUS_SUCCESS if success, else fail reason
539 //!
540 VAStatus DdiEncode_MfeSubmit(
541     VADriverContextP    ctx,
542     VAMFContextID       mfe_context,
543     VAContextID        *contexts,
544     int32_t             num_contexts
545 );
546 
547 //!
548 //! \brief  Set Encode Gpu Priority
549 //!
550 //! \param  [in] encode context
551 //!     Pointer to encode context
552 //! \param  [in] priority
553 //!     priority
554 //! \return VAStatus
555 //!
556 VAStatus DdiEncode_SetGpuPriority(
557     PDDI_ENCODE_CONTEXT encCtx,
558     int32_t             priority
559 );
560 #endif
561