xref: /aosp_15_r20/external/intel-media-driver/media_driver/linux/common/codec/ddi/media_ddi_encode_base.h (revision ba62d9d3abf0e404f2022b4cd7a85e107f48596f)
1 /*
2 * Copyright (c) 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_ddi_encode_base.h
24 //! \brief    Defines base class for DDI media encode.
25 //!
26 
27 #ifndef __MEDIA_DDI_ENCODE_BASE_H__
28 #define __MEDIA_DDI_ENCODE_BASE_H__
29 
30 #include <va/va.h>
31 #include "media_ddi_base.h"
32 #include "media_libva_encoder.h"
33 #include "codechal_setting.h"
34 
35 //!
36 //! \class  DdiEncodeBase
37 //! \brief  Ddi encode base
38 //!
39 class DdiEncodeBase : public DdiMediaBase
40 {
41 public:
42     //! \brief chroma format
43     enum ChromaFormat
44     {
45         monochrome  = 0,
46         yuv420      = 1,
47         yuv422      = 2,
48         yuv444      = 3
49     };
50 
51     //!
52     //! \brief Constructor
53     //!
54     DdiEncodeBase();
55 
56     //!
57     //! \brief    Initialize the encode context and do codechal setting
58     //! \details  Allocate memory for pointer members of encode context, set codechal
59     //!           which used by Codechal::Allocate
60     //!
61     //! \param    [out] codecHalSettings
62     //!           CodechalSetting *
63     //!
64     //! \return   VAStatus
65     //!           VA_STATUS_SUCCESS if success, else fail reason
66     //!
67     virtual VAStatus ContextInitialize(CodechalSetting * codecHalSettings) = 0;
68 
69     //!
70     //! \brief Destructor
71     //!
~DdiEncodeBase()72     virtual ~DdiEncodeBase()
73     {
74         MOS_Delete(m_codechalSettings);
75         m_codechalSettings = nullptr;
76     };
77 
78     virtual VAStatus BeginPicture(
79         VADriverContextP ctx,
80         VAContextID      context,
81         VASurfaceID      renderTarget);
82 
83     virtual VAStatus RenderPicture(
84         VADriverContextP ctx,
85         VAContextID      context,
86         VABufferID *     buffers,
87         int32_t          numBuffers) = 0;
88 
89     virtual VAStatus EndPicture(
90         VADriverContextP ctx,
91         VAContextID      context);
92 
93     //!
94     //! \brief    Report Status for coded buffer.
95     //!
96     //! \param    [in] mediaBuf
97     //!           Pointer to DDI_MEDIA_BUFFER
98     //! \param    [out] buf
99     //!           Pointer to buffer
100     //!
101     //! \return   VAStatus
102     //!           VA_STATUS_SUCCESS if success, else fail reason
103     //!
104     virtual VAStatus StatusReport(
105         DDI_MEDIA_BUFFER *mediaBuf,
106         void             **buf);
107 
108     //!
109     //! \brief    Report Status for Enc buffer.
110     //!
111     //! \param    [in] mediaBuf
112     //!           Pointer to DDI_MEDIA_BUFFER
113     //! \param    [out] buf
114     //!           Pointer to buffer
115     //!
116     //! \return   VAStatus
117     //!           VA_STATUS_SUCCESS if success, else fail reason
118     //!
119     VAStatus EncStatusReport(
120         DDI_MEDIA_BUFFER *mediaBuf,
121         void             **buf);
122 
123     //!
124     //! \brief    Report Status for PreEnc buffer.
125     //!
126     //! \param    [in] mediaBuf
127     //!           Pointer to DDI_MEDIA_BUFFER
128     //! \param    [out] buf
129     //!           Pointer to buffer
130     //!
131     //! \return   VAStatus
132     //!           VA_STATUS_SUCCESS if success, else fail reason
133     //!
134     VAStatus PreEncStatusReport(
135         DDI_MEDIA_BUFFER *mediaBuf,
136         void             **buf);
137 
138     //!
139     //! \brief    Remove Report Status from status report list.
140     //!
141     //! \param    [in] buf
142     //!           Pointer to DDI_MEDIA_BUFFER
143     //!
144     //! \return   VAStatus
145     //!           VA_STATUS_SUCCESS if success, else fail reason
146     //!
147     VAStatus RemoveFromStatusReportQueue(DDI_MEDIA_BUFFER *buf);
148 
149     //!
150     //! \brief    Remove Enc Report Status from status report list.
151     //!
152     //! \param    [in] buf
153     //!           Pointer to DDI_MEDIA_BUFFER
154     //! \param    [in] typeIdx
155     //!           FEI Enc buffer type
156     //!
157     //! \return   VAStatus
158     //!           VA_STATUS_SUCCESS if success, else fail reason
159     //!
160     VAStatus RemoveFromEncStatusReportQueue(
161         DDI_MEDIA_BUFFER               *buf,
162         DDI_ENCODE_FEI_ENC_BUFFER_TYPE typeIdx);
163 
164     //!
165     //! \brief    Remove PreEnc Report Status from status report list.
166     //!
167     //! \param    [in] buf
168     //!           Pointer to DDI_MEDIA_BUFFER
169     //! \param    [in] typeIdx
170     //!           PreEnc buffer type
171     //!
172     //! \return   VAStatus
173     //!           VA_STATUS_SUCCESS if success, else fail reason
174     //!
175     VAStatus RemoveFromPreEncStatusReportQueue(
176         DDI_MEDIA_BUFFER               *buf,
177         DDI_ENCODE_PRE_ENC_BUFFER_TYPE typeIdx);
178 
179     //!
180     //! \brief    Check if coded buffer exist in status report list
181     //!
182     //! \param    [in] buf
183     //!           Pointer to DDI_MEDIA_BUFFER
184     //!
185     //! \return   bool
186     //!           true if exist, else false
187     //!
188     bool CodedBufferExistInStatusReport(DDI_MEDIA_BUFFER *buf);
189 
190     //!
191     //! \brief    Check if Enc buffer exist in status report list
192     //!
193     //! \param    [in] buf
194     //!           Pointer to DDI_MEDIA_BUFFER
195     //! \param    [in] typeIdx
196     //!           FEI Enc buffer type
197     //!
198     //! \return   bool
199     //!           true if exist, else false
200     //!
201     bool EncBufferExistInStatusReport(
202         DDI_MEDIA_BUFFER               *buf,
203         DDI_ENCODE_FEI_ENC_BUFFER_TYPE typeIdx);
204 
205     //!
206     //! \brief    Check if PreEnc buffer exist in status report list
207     //!
208     //! \param    [in] buf
209     //!           Pointer to DDI_MEDIA_BUFFER
210     //! \param    [in] typeIdx
211     //!           PreEnc buffer type
212     //!
213     //! \return   bool
214     //!           true if exist, else false
215     //!
216     bool PreEncBufferExistInStatusReport(
217         DDI_MEDIA_BUFFER               *buf,
218         DDI_ENCODE_PRE_ENC_BUFFER_TYPE typeIdx);
219 
220     //!
221     //! \brief    Create Encode buffer
222     //! \details  Create Encode buffer
223     //!
224     //! \param    [in] ctx
225     //!           Pointer to VA Driver Context
226     //! \param    [in] type
227     //!           Va Buffer type
228     //! \param    [in] size
229     //!           Size of each element in buffer
230     //! \param    [in] elementsNum
231     //!           Number of elements
232     //! \param    [in] data
233     //!           Buffer data
234     //! \param    [in] bufId
235     //!           Pointer to VABufferID
236     //!
237     //! \return   VAStatus
238     //!           VA_STATUS_SUCCESS if successful, else fail reason
239     //!
240     VAStatus CreateBuffer(
241         VADriverContextP ctx,
242         VABufferType     type,
243         uint32_t         size,
244         uint32_t         elementsNum,
245         void             *data,
246         VABufferID       *bufId);
247 
248     //!
249     //! \brief    Init comp buffer
250     //! \details  Init comp buffer
251     //!
252     //! \return   VAStatus
253     //!           VA_STATUS_SUCCESS if successful, else fail reason
254     //!
255     virtual VAStatus InitCompBuffer();
256 
257     //!
258     //! \brief    Free comp buffer
259     //! \details  Free comp buffer
260     //!
261     //! \return   void
262     //!
263     virtual void FreeCompBuffer();
264 
265     DDI_ENCODE_CONTEXT *m_encodeCtx = nullptr; //!< The referred DDI_ENCODE_CONTEXT object.
266     bool m_is10Bit                  = false;   //!< 10 bit flag.
267     ChromaFormat m_chromaFormat     = yuv420;  //!< HCP chroma format.
268     CodechalSetting    *m_codechalSettings = nullptr;    //!< Codechal Settings
269 protected:
270     //!
271     //! \brief    Do Encode in codechal
272     //! \details  Prepare encode parameters, surfaces and buffers and submit
273     //!           to codechal to do encoding
274     //!
275     //! \param    [in] numSlices
276     //!           Number of slices
277     //!
278     //! \return   VAStatus
279     //!           VA_STATUS_SUCCESS if successful, else fail reason
280     //!
281     virtual VAStatus EncodeInCodecHal(uint32_t numSlices) = 0;
282 
283     //!
284     //! \brief    Reset the parameters before each frame
285     //! \details  Called by BeginPicture, reset sps parameter, bsbuffer,
286     //!           packed header information, etc
287     //!
288     //! \return   VAStatus
289     //!           VA_STATUS_SUCCESS if successful, else fail reason
290     //!
291     virtual VAStatus ResetAtFrameLevel() = 0;
292 
293     //!
294     //! \brief    Parse picture params
295     //! \details  Parse picture params called by RenderPicture
296     //!
297     //! \param    [in] mediaCtx
298     //!           Pointer to DDI_MEDIA_CONTEXT
299     //! \param    [in] ptr
300     //!           Pointer to buffer VAEncPictureParameterBufferH264
301     //!
302     //! \return   VAStatus
303     //!           VA_STATUS_SUCCESS if successful, else fail reason
304     //!
305     virtual VAStatus ParsePicParams(
306         DDI_MEDIA_CONTEXT *mediaCtx,
307         void *             ptr) = 0;
308 
309     //!
310     //! \brief    Clear picture params
311     //! \details  Clear picture params called by EndPicture
312     //!
313     //! \return   void
314     //!
315     virtual void ClearPicParams();
316 
317     //!
318     //! \brief    get Slice Parameter Buffer Size
319     //!
320     //! \return   uint32_t
321     //!           Slice Parameter Buffer Size
322     //!
323     virtual uint32_t getSliceParameterBufferSize();
324 
325     //!
326     //! \brief    get Sequence Parameter Buffer Size
327     //!
328     //! \return   uint32_t
329     //!           Sequence Parameter Buffer Size
330     //!
331     virtual uint32_t getSequenceParameterBufferSize();
332 
333     //!
334     //! \brief    get Picture Parameter Buffer Size
335     //!
336     //! \return   uint32_t
337     //!           Picture Parameter Buffer Size
338     //!
339     virtual uint32_t getPictureParameterBufferSize();
340 
341     //!
342     //! \brief    get QMatrix Parameter Buffer Size
343     //!
344     //! \return   uint32_t
345     //!           QMatrix Parameter Buffer Size
346     //!
347     virtual uint32_t getQMatrixBufferSize();
348 
349     //!
350     //! \brief    Add coded buffer information
351     //! \details  Add coded buffer information to Status Report Queue. this function
352     //!           called by RenderPicture
353     //!
354     //! \param    [in] codedBuf
355     //!           Pointer to Coded buffer
356     //!
357     //! \return   VAStatus
358     //!           VA_STATUS_SUCCESS if successful, else fail reason
359     //!
360     VAStatus AddToStatusReportQueue(void *codedBuf);
361 
362     //!
363     //! \brief    Convert rate control method in VAAPI to the term in HAL
364     //!
365     //! \param    [in] vaRC
366     //!           Rate control method in VAAPI
367     //!
368     //! \return   uint8_t
369     //!           Rate control method in HAL
370     //!
371     uint8_t VARC2HalRC(uint32_t vaRC);
372 
373     //!
374     //! \brief    Update status report buffer
375     //! \details  When we get coded buffer's size at StatusReport, update the size to Status
376     //!           Report Buffer. There should be some buffers waiting to be updated which
377     //!           are added in AddToStatusReportQueue.
378     //!
379     //! \param    [in] size
380     //!           Coded buffer's size
381     //! \param    [in] status
382     //!           Buffer status
383     //!
384     //! \return   VAStatus
385     //!           VA_STATUS_SUCCESS if successful, else fail reason
386     //!
387     VAStatus UpdateStatusReportBuffer(
388         uint32_t            size,
389         uint32_t            status);
390 
391     //!
392     //! \brief    Update Enc status report buffer
393     //! \details  There should be some buffers waiting to be updated which
394     //!           are added in AddToEncStatusReportQueue.
395     //!
396     //! \param    [in] status
397     //!           Buffer status
398     //!
399     //! \return   VAStatus
400     //!           VA_STATUS_SUCCESS if successful, else fail reason
401     //!
402     VAStatus UpdateEncStatusReportBuffer(uint32_t status);
403 
404     //!
405     //! \brief    Update PreEnc status report buffer
406     //! \details  There should be some buffers waiting to be updated which
407     //!           are added in AddToPreEncStatusReportQueue.
408     //!
409     //! \param    [in] status
410     //!           Buffer status
411     //!
412     //! \return   VAStatus
413     //!           VA_STATUS_SUCCESS if successful, else fail reason
414     //!
415     VAStatus UpdatePreEncStatusReportBuffer(uint32_t status);
416 
417     //!
418     //! \brief    Get Size From Status Report Buffer
419     //! \details  Get the coded buffer size, status and the index from Status
420     //!           Report Queue when we can match the input buffer.
421     //!
422     //! \param    [in] buf
423     //!           Pointer to DDI_MEDIA_BUFFER
424     //! \param    [out] size
425     //!           Pointer to uint32_t
426     //! \param    [out] status
427     //!           Pointer to uint32_t
428     //! \param    [out] index
429     //!           Pointer to int32_t
430     //!
431     //! \return   VAStatus
432     //!           VA_STATUS_SUCCESS if successful, else fail reason
433     //!
434     VAStatus GetSizeFromStatusReportBuffer(
435         DDI_MEDIA_BUFFER   *buf,
436         uint32_t           *size,
437         uint32_t           *status,
438         int32_t            *index);
439 
440     //!
441     //! \brief    Get Index From Enc Status Report Buffer
442     //! \details  Get the Enc buffer status and the index from Status
443     //!           Report Queue when we can match the input buffer.
444     //!
445     //! \param    [in] buf
446     //!           Pointer to DDI_MEDIA_BUFFER
447     //! \param    [in] typeIdx
448     //!           FEI Enc buffer type
449     //! \param    [out] status
450     //!           Pointer to uint32_t
451     //! \param    [out] index
452     //!           Pointer to int32_t
453     //!
454     //! \return   VAStatus
455     //!           VA_STATUS_SUCCESS if successful, else fail reason
456     //!
457     VAStatus GetIndexFromEncStatusReportBuffer(
458         DDI_MEDIA_BUFFER               *buf,
459         DDI_ENCODE_FEI_ENC_BUFFER_TYPE typeIdx,
460         uint32_t                       *status,
461         int32_t                        *index);
462 
463     //!
464     //! \brief    Get Index From PreEnc Status Report Buffer
465     //! \details  Get the PreEnc buffer status and the index from Status
466     //!           Report Queue when we can match the input buffer.
467     //!
468     //! \param    [in] buf
469     //!           Pointer to DDI_MEDIA_BUFFER
470     //! \param    [in] typeIdx
471     //!           PreEnc buffer type
472     //! \param    [out] status
473     //!           Pointer to uint32_t
474     //! \param    [out] index
475     //!           Pointer to int32_t
476     //!
477     //! \return   VAStatus
478     //!           VA_STATUS_SUCCESS if successful, else fail reason
479     //!
480     VAStatus GetIndexFromPreEncStatusReportBuffer(
481         DDI_MEDIA_BUFFER               *buf,
482         DDI_ENCODE_PRE_ENC_BUFFER_TYPE typeIdx,
483         uint32_t                       *status,
484         int32_t                        *index);
485 
486     //!
487     //! \brief    Report extra encode status for completed coded buffer.
488     //!
489     //! \param    [in] encodeStatusReport
490     //!           Pointer to encode status reported by Codechal
491     //! \param    [out] codedBufferSegment
492     //!           Pointer to coded buffer segment
493     //!
494     //! \return   VAStatus
495     //!           VA_STATUS_SUCCESS if success, else fail reason
496     //!
ReportExtraStatus(EncodeStatusReport * encodeStatusReport,VACodedBufferSegment * codedBufferSegment)497     virtual VAStatus ReportExtraStatus(
498         EncodeStatusReport   *encodeStatusReport,
499         VACodedBufferSegment *codedBufferSegment)
500     {
501         return VA_STATUS_SUCCESS;
502     }
503 
504     //!
505     //! \brief    Clean Up Buffer and Return
506     //!
507     //! \param    [in] buf
508     //!           Pointer to DDI_MEDIA_BUFFER
509     //!
510     //! \return   void
511     void CleanUpBufferandReturn(DDI_MEDIA_BUFFER *buf);
512 
513     bool    m_cpuFormat              = false;    //!< Flag for cpuFormat.
514     bool    m_newSeqHeader           = false;    //!< Flag for new Sequence Header.
515     bool    m_newPpsHeader           = false;    //!< Flag for new Pps Header.
516     bool    m_arbitraryNumMbsInSlice = false;    //!< Flag to indicate if the sliceMapSurface needs to be programmed or not.
517     uint8_t m_scalingLists4x4[6][16]{};          //!< Inverse quantization scale lists 4x4.
518     uint8_t m_scalingLists8x8[2][64]{};          //!< Inverse quantization scale lists 8x8.
519 };
520 #endif /* __MEDIA_DDI_ENCODE_BASE_H__ */
521