Debian Codegraph @

media_softlet/linux/common/codec/ddi/enc/ddi_encode_base_specific.h

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