Debian Codegraph @

doc/spec/source/snippets/prg_mem.c

raw
1 /*############################################################################
2 # Copyright (C) 2020 Intel Corporation
3 #
4 # SPDX-License-Identifier: MIT
5 ############################################################################*/
6
7 #include <stdlib.h>
8
9 #include "mfxdefs.h"
10 #include "mfxstructures.h"
11 #include "mfxsession.h"
12 #include "mfxvideo.h"
13
14 /* These macro required for code compilation. */
15 #define UNUSED_PARAM(x) (void)(x)
16
17 mfxSession session;
18 mfxStatus sts;
19 mfxFrameSurface1 *outsurface;
20 mfxSyncPoint syncp;
21 mfxHDL device_handle, resource;
22 mfxHandleType device_type;
23 mfxResourceType resource_type;
24
25 static int isDeviceTypeCompatible(mfxHandleType device_type)
26 {
27 UNUSED_PARAM(device_type);
28 return 1;
29 }
30
31 static int isPossibleForMemorySharing(mfxHDL device_handle)
32 {
33 UNUSED_PARAM(device_handle);
34 return 1;
35 }
36
37 static int isResourceTypeCompatible(mfxResourceType resource_type)
38 {
39 UNUSED_PARAM(resource_type);
40 return 1;
41 }
42
43 static void ProcessNativeMemory(mfxHDL hdl)
44 {
45 UNUSED_PARAM(hdl);
46 return;
47 }
48
49 static void ProcessSystemMemory(mfxFrameSurface1 *surf)
50 {
51 UNUSED_PARAM(surf);
52 return;
53 }
54 /* end of internal stuff */
55
56
57 /*beg1*/
58 #define ALIGN32(X) (((mfxU32)((X)+31)) & (~ (mfxU32)31))
59
60 typedef struct {
61 mfxU16 width, height;
62 mfxU8 *base;
63 } mid_struct;
64
65 mfxStatus fa_alloc(mfxHDL pthis, mfxFrameAllocRequest *request, mfxFrameAllocResponse *response) {
66 UNUSED_PARAM(pthis);
67 if (!(request->Type&MFX_MEMTYPE_SYSTEM_MEMORY))
68 return MFX_ERR_UNSUPPORTED;
69 if (request->Info.FourCC!=MFX_FOURCC_NV12)
70 return MFX_ERR_UNSUPPORTED;
71 response->NumFrameActual=request->NumFrameMin;
72 for (int i=0;i<request->NumFrameMin;i++) {
73 mid_struct *mmid=(mid_struct *)malloc(sizeof(mid_struct));
74 mmid->width=ALIGN32(request->Info.Width);
75 mmid->height=ALIGN32(request->Info.Height);
76 mmid->base=(mfxU8*)malloc(mmid->width*mmid->height*3/2);
77 response->mids[i]=mmid;
78 }
79 return MFX_ERR_NONE;
80 }
81
82 mfxStatus fa_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr) {
83 UNUSED_PARAM(pthis);
84 mid_struct *mmid=(mid_struct *)mid;
85 ptr->Pitch=mmid->width;
86 ptr->Y=mmid->base;
87 ptr->U=ptr->Y+mmid->width*mmid->height;
88 ptr->V=ptr->U+1;
89 return MFX_ERR_NONE;
90 }
91
92 mfxStatus fa_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr) {
93 UNUSED_PARAM(pthis);
94 UNUSED_PARAM(mid);
95 if (ptr) ptr->Y=ptr->U=ptr->V=ptr->A=0;
96 return MFX_ERR_NONE;
97 }
98
99 mfxStatus fa_gethdl(mfxHDL pthis, mfxMemId mid, mfxHDL *handle) {
100 UNUSED_PARAM(pthis);
101 UNUSED_PARAM(mid);
102 UNUSED_PARAM(handle);
103 return MFX_ERR_UNSUPPORTED;
104 }
105
106 mfxStatus fa_free(mfxHDL pthis, mfxFrameAllocResponse *response) {
107 UNUSED_PARAM(pthis);
108 for (int i=0;i<response->NumFrameActual;i++) {
109 mid_struct *mmid=(mid_struct *)response->mids[i];
110 free(mmid->base); free(mmid);
111 }
112 return MFX_ERR_NONE;
113 }
114 /*end1*/
115
116 static void prg_mem2 () {
117 /*beg2*/
118 // lets decode frame and try to access output in an optimal way.
119 sts = MFXVideoDECODE_DecodeFrameAsync(session, NULL, NULL, &outsurface, &syncp);
120 if (MFX_ERR_NONE == sts)
121 {
122 mfxStatus s = outsurface->FrameInterface->GetDeviceHandle(outsurface,
123 &device_handle, &device_type);
124 // if application or component is familar with mfxHandleType and it's
125 // possible to share memory created by device_handle.
126 if (MFX_ERR_NONE == s && isDeviceTypeCompatible(device_type)
127 && isPossibleForMemorySharing(device_handle)) {
128 // get native handle and type
129 outsurface->FrameInterface->GetNativeHandle(outsurface,
130 &resource, &resource_type);
131 if (isResourceTypeCompatible(resource_type)) {
132 //use memory directly
133 ProcessNativeMemory(resource);
134 outsurface->FrameInterface->Release(outsurface);
135 }
136 } else {
137 // Application or component is not aware about such DeviceHandle or
138 // Resource type need to map to system memory.
139 outsurface->FrameInterface->Map(outsurface, MFX_MAP_READ);
140 ProcessSystemMemory(outsurface);
141 outsurface->FrameInterface->Unmap(outsurface);
142 outsurface->FrameInterface->Release(outsurface);
143 }
144 }
145 /*end2*/
146 }