Debian Codegraph @

doc/spec/source/snippets/prg_vpp.c

raw
1 /*############################################################################
2 # Copyright (C) 2020 Intel Corporation
3 #
4 # SPDX-License-Identifier: MIT
5 ############################################################################*/
6
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10
11 #include "va/va.h"
12
13 #include "mfxdefs.h"
14 #include "mfxvideo.h"
15 #include "mfxmvc.h"
16 #ifdef ONEVPL_EXPERIMENTAL
17 #include "mfxcamera.h"
18 #endif
19
20 /* These macro required for code compilation. */
21 #define INFINITE 0x7FFFFFFF
22 #define UNUSED_PARAM(x) (void)(x)
23
24 mfxSession session;
25 mfxVideoParam init_param;
26 mfxFrameAllocRequest response[2];
27 mfxStatus sts;
28 mfxFrameSurface1 **in_pool, **out_pool;
29 mfxSyncPoint syncp;
30
31 mfxFrameSurface1 *in, *out;
32
33
34 static void allocate_pool_of_surfaces(mfxFrameSurface1 **pool, int nFrames)
35 {
36 UNUSED_PARAM(pool);
37 UNUSED_PARAM(nFrames);
38 return;
39 }
40
41 static mfxFrameSurface1* find_unlocked_surface_and_fill_content(mfxFrameSurface1 **pool)
42 {
43 UNUSED_PARAM(pool);
44 return pool[0];
45 }
46
47 static mfxFrameSurface1* find_unlocked_surface_from_the_pool(mfxFrameSurface1 **pool)
48 {
49 UNUSED_PARAM(pool);
50 return pool[0];
51 }
52
53 static void process_output_frame(mfxFrameSurface1 *surf)
54 {
55 UNUSED_PARAM(surf);
56 return;
57 }
58
59 static void fill_content_for_video_processing(mfxFrameSurface1 *surf)
60 {
61 UNUSED_PARAM(surf);
62 return;
63 }
64
65 static int end_of_stream()
66 {
67 return 1;
68 }
69
70 static void free_pool_of_surfaces(mfxFrameSurface1 **in_pool)
71 {
72 UNUSED_PARAM(in_pool);
73 return;
74 }
75 /* end of internal stuff */
76
77
78 static void prg_vpp1 () {
79 /*beg1*/
80 MFXVideoVPP_QueryIOSurf(session, &init_param, response);
81 allocate_pool_of_surfaces(in_pool, response[0].NumFrameSuggested);
82 allocate_pool_of_surfaces(out_pool, response[1].NumFrameSuggested);
83 MFXVideoVPP_Init(session, &init_param);
84 mfxFrameSurface1 *in=find_unlocked_surface_and_fill_content(in_pool);
85 mfxFrameSurface1 *out=find_unlocked_surface_from_the_pool(out_pool);
86 for (;;) {
87 sts=MFXVideoVPP_RunFrameVPPAsync(session,in,out,NULL,&syncp);
88 if (sts==MFX_ERR_MORE_SURFACE || sts==MFX_ERR_NONE) {
89 MFXVideoCORE_SyncOperation(session,syncp,INFINITE);
90 process_output_frame(out);
91 out=find_unlocked_surface_from_the_pool(out_pool);
92 }
93 if (sts==MFX_ERR_MORE_DATA && in==NULL)
94 break;
95 if (sts==MFX_ERR_NONE || sts==MFX_ERR_MORE_DATA) {
96 in=find_unlocked_surface_from_the_pool(in_pool);
97 fill_content_for_video_processing(in);
98 if (end_of_stream())
99 in=NULL;
100 }
101 }
102 MFXVideoVPP_Close(session);
103 free_pool_of_surfaces(in_pool);
104 free_pool_of_surfaces(out_pool);
105 /*end1*/
106 }
107
108 static void prg_vpp2 () {
109 /*beg2*/
110 /* enable image stabilization filter with default settings */
111 mfxExtVPPDoUse du;
112 mfxU32 al=MFX_EXTBUFF_VPP_IMAGE_STABILIZATION;
113
114 du.Header.BufferId=MFX_EXTBUFF_VPP_DOUSE;
115 du.Header.BufferSz=sizeof(mfxExtVPPDoUse);
116 du.NumAlg=1;
117 du.AlgList=&al;
118
119 /* configure the mfxVideoParam structure */
120 mfxVideoParam conf;
121 mfxExtBuffer *eb=(mfxExtBuffer *)&du;
122
123 memset(&conf,0,sizeof(conf));
124 conf.IOPattern=MFX_IOPATTERN_IN_SYSTEM_MEMORY | MFX_IOPATTERN_OUT_SYSTEM_MEMORY;
125 conf.NumExtParam=1;
126 conf.ExtParam=&eb;
127
128 conf.vpp.In.FourCC=MFX_FOURCC_YV12;
129 conf.vpp.Out.FourCC=MFX_FOURCC_NV12;
130 conf.vpp.In.Width=conf.vpp.Out.Width=1920;
131 conf.vpp.In.Height=conf.vpp.Out.Height=1088;
132
133 /* video processing initialization */
134 MFXVideoVPP_Init(session, &conf);
135 /*end2*/
136 }
137
138 static void prg_vpp3 () {
139 /*beg3*/
140 mfxExtBuffer *eb;
141 mfxExtMVCSeqDesc seq_desc;
142 mfxVideoParam init_param;
143
144 init_param.ExtParam = &eb;
145 init_param.NumExtParam=1;
146 eb=(mfxExtBuffer *)&seq_desc;
147
148 /* init VPP */
149 MFXVideoVPP_Init(session, &init_param);
150
151 /* perform processing */
152 for (;;) {
153 MFXVideoVPP_RunFrameVPPAsync(session,in,out,NULL,&syncp);
154 MFXVideoCORE_SyncOperation(session,syncp,INFINITE);
155 }
156
157 /* close VPP */
158 MFXVideoVPP_Close(session);
159 /*end3*/
160 }
161
162 static void prg_vpp4 () {
163 /*beg4*/
164 VADisplay va_dpy = 0;
165 VASurfaceID surface_id = 0;
166
167 vaInitialize(va_dpy, NULL, NULL);
168
169 // MFX_3DLUT_MEMORY_LAYOUT_INTEL_65LUT indicate 65*65*128*8bytes.
170 mfxU32 seg_size = 65, mul_size = 128;
171 mfxMemId memId = 0;
172
173 // create 3DLUT surface (MFX_3DLUT_MEMORY_LAYOUT_INTEL_65LUT)
174 VASurfaceAttrib surface_attrib = {};
175 surface_attrib.type = VASurfaceAttribPixelFormat;
176 surface_attrib.flags = VA_SURFACE_ATTRIB_SETTABLE;
177 surface_attrib.value.type = VAGenericValueTypeInteger;
178 surface_attrib.value.value.i = VA_FOURCC_RGBA;
179
180 vaCreateSurfaces(va_dpy,
181 VA_RT_FORMAT_RGB32, // 4 bytes
182 seg_size * mul_size, // 65*128
183 seg_size * 2, // 65*2
184 &surface_id,
185 1,
186 &surface_attrib,
187 1);
188
189 *((VASurfaceID*)memId) = surface_id;
190
191 // configure 3DLUT parameters
192 mfxExtVPP3DLut lut3DConfig;
193 memset(&lut3DConfig, 0, sizeof(lut3DConfig));
194 lut3DConfig.Header.BufferId = MFX_EXTBUFF_VPP_3DLUT;
195 lut3DConfig.Header.BufferSz = sizeof(mfxExtVPP3DLut);
196 lut3DConfig.ChannelMapping = MFX_3DLUT_CHANNEL_MAPPING_RGB_RGB;
197 lut3DConfig.BufferType = MFX_RESOURCE_VA_SURFACE;
198 lut3DConfig.VideoBuffer.DataType = MFX_DATA_TYPE_U16;
199 lut3DConfig.VideoBuffer.MemLayout = MFX_3DLUT_MEMORY_LAYOUT_INTEL_65LUT;
200 lut3DConfig.VideoBuffer.MemId = memId;
201
202 // release 3DLUT surface
203 vaDestroySurfaces(va_dpy, &surface_id, 1);
204 /*end4*/
205 }
206
207 static void prg_vpp5 () {
208 /*beg5*/
209
210 // 64 size 3DLUT(3 dimension look up table)
211 // The buffer size(in bytes) for every channel is 64*64*64*sizeof(DataType)
212 mfxU16 dataR[64*64*64], dataG[64*64*64], dataB[64*64*64];
213 mfxChannel channelR, channelG, channelB;
214 channelR.DataType = MFX_DATA_TYPE_U16;
215 channelR.Size = 64;
216 channelR.Data16 = dataR;
217 channelG.DataType = MFX_DATA_TYPE_U16;
218 channelG.Size = 64;
219 channelG.Data16 = dataG;
220 channelB.DataType = MFX_DATA_TYPE_U16;
221 channelB.Size = 64;
222 channelB.Data16 = dataB;
223
224 // configure 3DLUT parameters
225 mfxExtVPP3DLut lut3DConfig;
226 memset(&lut3DConfig, 0, sizeof(lut3DConfig));
227 lut3DConfig.Header.BufferId = MFX_EXTBUFF_VPP_3DLUT;
228 lut3DConfig.Header.BufferSz = sizeof(mfxExtVPP3DLut);
229 lut3DConfig.ChannelMapping = MFX_3DLUT_CHANNEL_MAPPING_RGB_RGB;
230 lut3DConfig.BufferType = MFX_RESOURCE_SYSTEM_SURFACE;
231 lut3DConfig.SystemBuffer.Channel[0] = channelR;
232 lut3DConfig.SystemBuffer.Channel[1] = channelG;
233 lut3DConfig.SystemBuffer.Channel[2] = channelB;
234
235 /*end5*/
236 }
237
238 static void prg_vpp6() {
239 /*beg6*/
240
241 // HDR to SDR (e.g P010 HDR signal -> NV12 SDR signal) in transcoding pipeline
242 // Attach input external buffers as the below for HDR input. SDR is by default, hence no
243 // extra output external buffer.
244 // The input Video Signal Information
245 mfxExtVideoSignalInfo inSignalInfo = {};
246 inSignalInfo.Header.BufferId = MFX_EXTBUFF_VIDEO_SIGNAL_INFO_IN;
247 inSignalInfo.Header.BufferSz = sizeof(mfxExtVideoSignalInfo);
248 inSignalInfo.VideoFullRange = 0; // Limited range P010
249 inSignalInfo.ColourPrimaries = 9; // BT.2020
250 inSignalInfo.TransferCharacteristics = 16; // ST2084
251
252 // The content Light Level Information
253 mfxExtContentLightLevelInfo inContentLight = {};
254 inContentLight.Header.BufferId = MFX_EXTBUFF_CONTENT_LIGHT_LEVEL_INFO;
255 inContentLight.Header.BufferSz = sizeof(mfxExtContentLightLevelInfo);
256 inContentLight.MaxContentLightLevel = 4000; // nits
257 inContentLight.MaxPicAverageLightLevel = 1000; // nits
258
259 // The mastering display colour volume
260 mfxExtMasteringDisplayColourVolume inColourVolume = {};
261 inColourVolume.Header.BufferId = MFX_EXTBUFF_MASTERING_DISPLAY_COLOUR_VOLUME_IN;
262 inColourVolume.Header.BufferSz = sizeof(mfxExtMasteringDisplayColourVolume);
263 // Based on the needs, Please set DisplayPrimaryX/Y[3], WhitePointX/Y, and MaxDisplayMasteringLuminance,
264 // MinDisplayMasteringLuminance
265
266 mfxExtBuffer *ExtBufferIn[3];
267 ExtBufferIn[0] = (mfxExtBuffer *)&inSignalInfo;
268 ExtBufferIn[1] = (mfxExtBuffer *)&inContentLight;
269 ExtBufferIn[2] = (mfxExtBuffer *)&inColourVolume;
270
271 mfxSession session = (mfxSession)0;
272 mfxVideoParam VPPParams = {};
273 VPPParams.NumExtParam = 3;
274 VPPParams.ExtParam = (mfxExtBuffer **)&ExtBufferIn[0];
275 MFXVideoVPP_Init(session, &VPPParams);
276
277 /*end6*/
278 }
279
280 static void prg_vpp7() {
281 /*beg7*/
282
283 // SDR to HDR (e.g NV12 SDR signal -> P010 HDR signal) in transcoding pipeline
284 // Attach output external buffers as the below for HDR output. SDR is by default, hence no
285 // extra input external buffer.
286 // The output Video Signal Information
287 mfxExtVideoSignalInfo outSignalInfo = {};
288 outSignalInfo.Header.BufferId = MFX_EXTBUFF_VIDEO_SIGNAL_INFO_OUT;
289 outSignalInfo.Header.BufferSz = sizeof(mfxExtVideoSignalInfo);
290 outSignalInfo.VideoFullRange = 0; // Limited range P010
291 outSignalInfo.ColourPrimaries = 9; // BT.2020
292 outSignalInfo.TransferCharacteristics = 16; // ST2084
293
294 // The mastering display colour volume
295 mfxExtMasteringDisplayColourVolume outColourVolume = {};
296 outColourVolume.Header.BufferId = MFX_EXTBUFF_MASTERING_DISPLAY_COLOUR_VOLUME_OUT;
297 outColourVolume.Header.BufferSz = sizeof(mfxExtMasteringDisplayColourVolume);
298 // Based on the needs, Please set DisplayPrimaryX/Y[3], WhitePointX/Y, and MaxDisplayMasteringLuminance,
299 // MinDisplayMasteringLuminance
300
301 mfxExtBuffer *ExtBufferOut[2];
302 ExtBufferOut[0] = (mfxExtBuffer *)&outSignalInfo;
303 ExtBufferOut[2] = (mfxExtBuffer *)&outColourVolume;
304
305 mfxSession session = (mfxSession)0;
306 mfxVideoParam VPPParams = {};
307 VPPParams.NumExtParam = 2;
308 VPPParams.ExtParam = (mfxExtBuffer **)&ExtBufferOut[0];
309 MFXVideoVPP_Init(session, &VPPParams);
310 /*end7*/
311 }
312
313 static void prg_vpp8() {
314 /*beg8*/
315
316 // HDR to HDR (e.g P010 HDR signal -> P010 HDR signal) in transcoding pipeline
317 // Attach in/output external buffers as the below for HDR input/output.
318 // The input Video Signal Information
319 mfxExtVideoSignalInfo inSignalInfo = {};
320 inSignalInfo.Header.BufferId = MFX_EXTBUFF_VIDEO_SIGNAL_INFO_IN;
321 inSignalInfo.Header.BufferSz = sizeof(mfxExtVideoSignalInfo);
322 inSignalInfo.VideoFullRange = 0; // Limited range P010
323 inSignalInfo.ColourPrimaries = 9; // BT.2020
324 inSignalInfo.TransferCharacteristics = 16; // ST2084
325
326 // The content Light Level Information
327 mfxExtContentLightLevelInfo inContentLight = {};
328 inContentLight.Header.BufferId = MFX_EXTBUFF_CONTENT_LIGHT_LEVEL_INFO;
329 inContentLight.Header.BufferSz = sizeof(mfxExtContentLightLevelInfo);
330 inContentLight.MaxContentLightLevel = 4000; // nits
331 inContentLight.MaxPicAverageLightLevel = 1000; // nits
332
333 // The mastering display colour volume
334 mfxExtMasteringDisplayColourVolume inColourVolume = {};
335 inColourVolume.Header.BufferId = MFX_EXTBUFF_MASTERING_DISPLAY_COLOUR_VOLUME_IN;
336 inColourVolume.Header.BufferSz = sizeof(mfxExtMasteringDisplayColourVolume);
337 // Based on the needs, Please set DisplayPrimaryX/Y[3], WhitePointX/Y, and MaxDisplayMasteringLuminance,
338 // MinDisplayMasteringLuminance
339
340 mfxExtVideoSignalInfo outSignalInfo = {};
341 outSignalInfo.Header.BufferId = MFX_EXTBUFF_VIDEO_SIGNAL_INFO_OUT;
342 outSignalInfo.Header.BufferSz = sizeof(mfxExtVideoSignalInfo);
343 outSignalInfo.VideoFullRange = 0; // Limited range P010
344 outSignalInfo.ColourPrimaries = 9; // BT.2020
345 outSignalInfo.TransferCharacteristics = 16; // ST2084
346
347 // The mastering display colour volume
348 mfxExtMasteringDisplayColourVolume outColourVolume = {};
349 outColourVolume.Header.BufferId = MFX_EXTBUFF_MASTERING_DISPLAY_COLOUR_VOLUME_OUT;
350 outColourVolume.Header.BufferSz = sizeof(mfxExtMasteringDisplayColourVolume);
351 // Based on the needs, Please set DisplayPrimaryX/Y[3], WhitePointX/Y, and MaxDisplayMasteringLuminance,
352 // MinDisplayMasteringLuminance
353
354 mfxExtBuffer *ExtBuffer[5];
355 ExtBuffer[0] = (mfxExtBuffer *)&inSignalInfo;
356 ExtBuffer[1] = (mfxExtBuffer *)&inContentLight;
357 ExtBuffer[2] = (mfxExtBuffer *)&inColourVolume;
358 ExtBuffer[3] = (mfxExtBuffer *)&outSignalInfo;
359 ExtBuffer[4] = (mfxExtBuffer *)&outColourVolume;
360
361 mfxSession session = (mfxSession)0;
362 mfxVideoParam VPPParams = {};
363 VPPParams.NumExtParam = 5;
364 VPPParams.ExtParam = (mfxExtBuffer **)&ExtBuffer[0];
365 MFXVideoVPP_Init(session, &VPPParams);
366
367 /*end8*/
368 }
369
370 static void prg_vpp9() {
371 /*beg9*/
372 #ifdef ONEVPL_EXPERIMENTAL
373 // Camera Raw Format
374 mfxExtCamPipeControl pipeControl = {};
375 pipeControl.Header.BufferId = MFX_EXTBUF_CAM_PIPECONTROL;
376 pipeControl.Header.BufferSz = sizeof(mfxExtCamPipeControl);
377 pipeControl.RawFormat = (mfxU16)MFX_CAM_BAYER_BGGR;
378
379 // Black level correction
380 mfxExtCamBlackLevelCorrection blackLevelCorrection = {};
381 blackLevelCorrection.Header.BufferId = MFX_EXTBUF_CAM_BLACK_LEVEL_CORRECTION;
382 blackLevelCorrection.Header.BufferSz = sizeof(mfxExtCamBlackLevelCorrection);
383 mfxU16 black_level_B = 16, black_level_G0 = 16, black_level_G1 = 16, black_level_R = 16;
384 // Initialize the value for black level B, G0, G1, R as needed
385 blackLevelCorrection.B = black_level_B;
386 blackLevelCorrection.G0 = black_level_G0;
387 blackLevelCorrection.G1 = black_level_G1;
388 blackLevelCorrection.R = black_level_R;
389
390 mfxExtBuffer *ExtBufferIn[2];
391 ExtBufferIn[0] = (mfxExtBuffer *)&pipeControl;
392 ExtBufferIn[1] = (mfxExtBuffer *)&blackLevelCorrection;
393
394 mfxSession session = (mfxSession)0;
395 mfxVideoParam VPPParams = {};
396 VPPParams.NumExtParam = 2;
397 VPPParams.ExtParam = (mfxExtBuffer **)&ExtBufferIn[0];
398 MFXVideoVPP_Init(session, &VPPParams);
399 #endif
400 /*end9*/
401 }
402
403 static void prg_vpp11() {
404 /*beg11*/
405 #ifdef ONEVPL_EXPERIMENTAL
406 // configure 3DLUT parameters
407 mfxExtVPP3DLut lut3DConfig;
408 memset(&lut3DConfig, 0, sizeof(lut3DConfig));
409 lut3DConfig.Header.BufferId = MFX_EXTBUFF_VPP_3DLUT;
410 lut3DConfig.Header.BufferSz = sizeof(mfxExtVPP3DLut);
411 lut3DConfig.ChannelMapping = MFX_3DLUT_CHANNEL_MAPPING_RGB_RGB;
412 lut3DConfig.BufferType = MFX_RESOURCE_SYSTEM_SURFACE;
413 lut3DConfig.InterpolationMethod = MFX_3DLUT_INTERPOLATION_TETRAHEDRAL;
414 #endif
415 /*end11*/
416 }
417
418 static void prg_vpp12() {
419 /*beg12*/
420 // configure AI super resolution vpp filter
421 mfxExtVPPAISuperResolution aiSuperResolution = {};
422 aiSuperResolution.Header.BufferId = MFX_EXTBUFF_VPP_AI_SUPER_RESOLUTION;
423 aiSuperResolution.Header.BufferSz = sizeof(mfxExtVPPAISuperResolution);
424 aiSuperResolution.SRMode = MFX_AI_SUPER_RESOLUTION_MODE_DEFAULT;
425
426 mfxExtBuffer * ExtParam[1] = { (mfxExtBuffer *)&aiSuperResolution };
427
428 mfxSession session = (mfxSession)0;
429 mfxVideoParam VPPParams = {};
430 VPPParams.NumExtParam = 1;
431 VPPParams.ExtParam = ExtParam;
432 MFXVideoVPP_Init(session, &VPPParams);
433 /*end12*/
434 }
435
436 static void prg_vpp13() {
437 /*beg13*/
438 // configure AI frame interpolation vpp filter
439 mfxExtVPPAIFrameInterpolation aiFrameInterpolation = {};
440 aiFrameInterpolation.Header.BufferId = MFX_EXTBUFF_VPP_AI_FRAME_INTERPOLATION;
441 aiFrameInterpolation.Header.BufferSz = sizeof(mfxExtVPPAIFrameInterpolation);
442 aiFrameInterpolation.FIMode = MFX_AI_FRAME_INTERPOLATION_MODE_DEFAULT;
443 aiFrameInterpolation.EnableScd = 1;
444 mfxExtBuffer * ExtParam[1] = { (mfxExtBuffer *)&aiFrameInterpolation };
445
446 init_param.NumExtParam = 1;
447 init_param.ExtParam = ExtParam;
448 init_param.vpp.In.FrameRateExtN = 30;
449 init_param.vpp.In.FrameRateExtD = 1;
450 init_param.vpp.Out.FrameRateExtN = 60;
451 init_param.vpp.Out.FrameRateExtD = 1;
452 sts = MFXVideoVPP_QueryIOSurf(session, &init_param, response);
453 sts = MFXVideoVPP_Init(session, &init_param);
454
455 // The below code follows the video processing procedure, not specific to AI frame interpolation.
456 allocate_pool_of_surfaces(in_pool, response[0].NumFrameSuggested);
457 allocate_pool_of_surfaces(out_pool, response[1].NumFrameSuggested);
458 mfxFrameSurface1 *in=find_unlocked_surface_and_fill_content(in_pool);
459 mfxFrameSurface1 *out=find_unlocked_surface_from_the_pool(out_pool);
460 for (;;) {
461 sts=MFXVideoVPP_RunFrameVPPAsync(session,in,out,NULL,&syncp);
462 if (sts==MFX_ERR_MORE_SURFACE || sts==MFX_ERR_NONE) {
463 MFXVideoCORE_SyncOperation(session,syncp,INFINITE);
464 process_output_frame(out);
465 out=find_unlocked_surface_from_the_pool(out_pool);
466 }
467 if (sts==MFX_ERR_MORE_DATA && in==NULL)
468 break;
469 if (sts==MFX_ERR_NONE || sts==MFX_ERR_MORE_DATA) {
470 in=find_unlocked_surface_from_the_pool(in_pool);
471 fill_content_for_video_processing(in);
472 if (end_of_stream())
473 in=NULL;
474 }
475 }
476 MFXVideoVPP_Close(session);
477 free_pool_of_surfaces(in_pool);
478 free_pool_of_surfaces(out_pool);
479 /*end13*/
480 }
481