1 /*
2  * portvideo.cpp, port class for video
3  *
4  * Copyright (c) 2009-2010 Wind River Systems, Inc.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include <stdlib.h>
20 #include <string.h>
21 
22 #include <OMX_Core.h>
23 #include <OMX_Component.h>
24 
25 #include <componentbase.h>
26 #include <portvideo.h>
27 
28 #define LOG_TAG "portvideo"
29 #include <log.h>
30 
PortVideo()31 PortVideo::PortVideo()
32 {
33     memset(&videoparam, 0, sizeof(videoparam));
34     ComponentBase::SetTypeHeader(&videoparam, sizeof(videoparam));
35 
36     videoparam.nIndex = 0;
37     videoparam.eCompressionFormat = OMX_VIDEO_CodingUnused;
38     videoparam.eColorFormat = OMX_COLOR_FormatYUV420Planar;
39     videoparam.xFramerate = 15 << 16;
40 
41     memset(&bitrateparam, 0, sizeof(bitrateparam));
42     ComponentBase::SetTypeHeader(&bitrateparam, sizeof(bitrateparam));
43 
44     bitrateparam.eControlRate = OMX_Video_ControlRateConstant;
45     bitrateparam.nTargetBitrate = 64000;
46 
47     memset(&privateinfoparam, 0, sizeof(privateinfoparam));
48     ComponentBase::SetTypeHeader(&privateinfoparam, sizeof(privateinfoparam));
49 
50     privateinfoparam.nCapacity = 0;
51     privateinfoparam.nHolder = NULL;
52 
53     mbufsharing = OMX_FALSE;
54 }
55 
56 //PortVideo::~PortVideo()
57 //{
58 //    if(privateinfoparam.nHolder != NULL) {
59 //        free(privateinfoparam.nHolder);
60 //        privateinfoparam.nHolder = NULL;
61 //    }
62 //}
63 
SetPortVideoParam(const OMX_VIDEO_PARAM_PORTFORMATTYPE * p,bool internal)64 OMX_ERRORTYPE PortVideo::SetPortVideoParam(
65     const OMX_VIDEO_PARAM_PORTFORMATTYPE *p, bool internal)
66 {
67     if (!internal) {
68         OMX_ERRORTYPE ret;
69 
70         ret = ComponentBase::CheckTypeHeader((void *)p, sizeof(*p));
71         if (ret != OMX_ErrorNone)
72             return ret;
73         if (videoparam.nPortIndex != p->nPortIndex)
74             return OMX_ErrorBadPortIndex;
75     }
76     else
77         videoparam.nPortIndex = p->nPortIndex;
78 
79     videoparam.nIndex = p->nIndex;
80     videoparam.eCompressionFormat = p->eCompressionFormat;
81     videoparam.eColorFormat = p->eColorFormat;
82     videoparam.xFramerate = p->xFramerate;
83 
84     return OMX_ErrorNone;
85 }
86 
GetPortVideoParam(void)87 const OMX_VIDEO_PARAM_PORTFORMATTYPE *PortVideo::GetPortVideoParam(void)
88 {
89     return &videoparam;
90 }
91 
SetPortBitrateParam(const OMX_VIDEO_PARAM_BITRATETYPE * p,bool internal)92 OMX_ERRORTYPE PortVideo::SetPortBitrateParam(
93     const OMX_VIDEO_PARAM_BITRATETYPE *p, bool internal)
94 {
95     if (!internal) {
96         OMX_ERRORTYPE ret;
97 
98         ret = ComponentBase::CheckTypeHeader((void *)p, sizeof(*p));
99         if (ret != OMX_ErrorNone)
100             return ret;
101         if (bitrateparam.nPortIndex != p->nPortIndex)
102             return OMX_ErrorBadPortIndex;
103     }
104     else
105         bitrateparam.nPortIndex = p->nPortIndex;
106 
107     bitrateparam.eControlRate = p->eControlRate;
108     bitrateparam.nTargetBitrate = p->nTargetBitrate;
109 
110     return OMX_ErrorNone;
111 }
112 
GetPortBitrateParam(void)113 const OMX_VIDEO_PARAM_BITRATETYPE *PortVideo::GetPortBitrateParam(void)
114 {
115     return &bitrateparam;
116 }
117 
SetPortBufferSharingInfo(OMX_BOOL isbufsharing)118 OMX_ERRORTYPE PortVideo::SetPortBufferSharingInfo(OMX_BOOL isbufsharing)
119 {
120     mbufsharing = isbufsharing;
121 
122     return OMX_ErrorNone;
123 }
124 
GetPortBufferSharingInfo(void)125 const OMX_BOOL *PortVideo::GetPortBufferSharingInfo(void)
126 {
127     return &mbufsharing;
128 }
129 
130 
SetPortPrivateInfoParam(const OMX_VIDEO_CONFIG_PRI_INFOTYPE * p,bool internal)131 OMX_ERRORTYPE PortVideo::SetPortPrivateInfoParam(
132     const OMX_VIDEO_CONFIG_PRI_INFOTYPE *p, bool internal)
133 {
134     if (!internal) {
135         OMX_ERRORTYPE ret;
136 
137         ret = ComponentBase::CheckTypeHeader((void *)p, sizeof(*p));
138         if (ret != OMX_ErrorNone)
139             return ret;
140         if (privateinfoparam.nPortIndex != p->nPortIndex)
141             return OMX_ErrorBadPortIndex;
142     }
143     else
144         privateinfoparam.nPortIndex = p->nPortIndex;
145 
146     const OMX_BOOL *isbufsharing = GetPortBufferSharingInfo();
147     if(*isbufsharing) {
148         //if(privateinfoparam.nHolder != NULL) {
149         //    free(privateinfoparam.nHolder);
150         //    privateinfoparam.nHolder = NULL;
151         //}
152         if(p->nHolder != NULL) {
153             privateinfoparam.nCapacity = p->nCapacity;
154             privateinfoparam.nHolder = (OMX_PTR)malloc(sizeof(OMX_U32) * (p->nCapacity));
155             memcpy(privateinfoparam.nHolder, p->nHolder, sizeof(OMX_U32) * (p->nCapacity));
156         } else {
157             privateinfoparam.nCapacity = 0;
158             privateinfoparam.nHolder = NULL;
159         }
160     }
161 
162     return OMX_ErrorNone;
163 }
164 
GetPortPrivateInfoParam(void)165 const OMX_VIDEO_CONFIG_PRI_INFOTYPE *PortVideo::GetPortPrivateInfoParam(void)
166 {
167     return &privateinfoparam;
168 }
169 
170 /* end of PortVideo */
171 #if 0
172 PortAvc::PortAvc()
173 {
174     OMX_VIDEO_PARAM_PORTFORMATTYPE videoparam;
175     memcpy(&videoparam, GetPortVideoParam(), sizeof(videoparam));
176     videoparam.eCompressionFormat = OMX_VIDEO_CodingAVC;
177     videoparam.eColorFormat = OMX_COLOR_FormatUnused;
178     videoparam.xFramerate = 15 << 16;
179     SetPortVideoParam(&videoparam, false);
180 
181     memset(&avcparam, 0, sizeof(avcparam));
182     ComponentBase::SetTypeHeader(&avcparam, sizeof(avcparam));
183 
184     avcparam.eProfile = OMX_VIDEO_AVCProfileVendorStartUnused;  //defaul profle for buffer sharing in opencore
185     avcparam.eLevel = OMX_VIDEO_AVCLevelVendorStartUnused;     //default level for buffer sharing in opencore
186 
187 //set buffer sharing mode
188 #ifdef COMPONENT_SUPPORT_BUFFER_SHARING
189     SetPortBufferSharingInfo(OMX_TRUE);
190 #endif
191 
192 }
193 OMX_ERRORTYPE PortAvc::SetPortAvcParam(
194     const OMX_VIDEO_PARAM_AVCTYPE *p, bool overwrite_readonly)
195 {
196     if (!overwrite_readonly) {
197         OMX_ERRORTYPE ret;
198 
199         ret = ComponentBase::CheckTypeHeader((void *)p, sizeof(*p));
200         if (ret != OMX_ErrorNone)
201             return ret;
202         if (avcparam.nPortIndex != p->nPortIndex)
203             return OMX_ErrorBadPortIndex;
204     }
205     else {
206         OMX_VIDEO_PARAM_PORTFORMATTYPE videoparam;
207 
208         memcpy(&videoparam, GetPortVideoParam(), sizeof(videoparam));
209         videoparam.nPortIndex = p->nPortIndex;
210         SetPortVideoParam(&videoparam, true);
211 
212         avcparam.nPortIndex = p->nPortIndex;
213     }
214     avcparam.nSliceHeaderSpacing = p->nSliceHeaderSpacing;
215     avcparam.nPFrames = p->nPFrames;
216     avcparam.nBFrames = p->nBFrames;
217     avcparam.bUseHadamard = p->bUseHadamard;
218     avcparam.nRefFrames = p->nRefFrames;
219     avcparam.nRefIdx10ActiveMinus1 = p->nRefIdx10ActiveMinus1;
220     avcparam.nRefIdx11ActiveMinus1 = p->nRefIdx11ActiveMinus1;
221     avcparam.bEnableUEP = p->bEnableUEP;
222     avcparam.bEnableFMO = p->bEnableFMO;
223     avcparam.bEnableASO = p->bEnableASO;
224     avcparam.bEnableRS = p->bEnableRS;
225     avcparam.nAllowedPictureTypes = p->nAllowedPictureTypes;
226     avcparam.bFrameMBsOnly = p->bFrameMBsOnly;
227     avcparam.bMBAFF = p->bMBAFF;
228     avcparam.bEntropyCodingCABAC = p->bEntropyCodingCABAC;
229     avcparam.bWeightedPPrediction = p->bWeightedPPrediction;
230     avcparam.nWeightedBipredicitonMode = p->nWeightedBipredicitonMode;
231     avcparam.bconstIpred = p->bconstIpred;
232     avcparam.bDirect8x8Inference = p->bDirect8x8Inference;
233     avcparam.bDirectSpatialTemporal = p->bDirectSpatialTemporal;
234     avcparam.nCabacInitIdc = p->nCabacInitIdc;
235     avcparam.eLoopFilterMode = p->eLoopFilterMode;
236 
237 #ifdef COMPONENT_SUPPORT_OPENCORE
238 #ifdef COMPONENT_SUPPORT_BUFFER_SHARING
239 // sepcial case ,not change default profile and level for opencore buffer sharing.
240 #else
241     avcparam.eProfile = p->eProfile;
242     avcparam.eLevel = p->eLevel;
243 #endif
244 #else
245     avcparam.eProfile = p->eProfile;
246     avcparam.eLevel = p->eLevel;
247 #endif
248 
249     return OMX_ErrorNone;
250 }
251 
252 const OMX_VIDEO_PARAM_AVCTYPE *PortAvc::GetPortAvcParam(void)
253 {
254     return &avcparam;
255 }
256 
257 /* end of PortAvc */
258 #endif
259 
260