xref: /OK3568_Linux_fs/external/mpp/mpp/base/mpp_frame.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright 2015 Rockchip Electronics Co. LTD
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define MODULE_TAG "mpp_frame"
18 
19 #include <string.h>
20 
21 #include "mpp_log.h"
22 #include "mpp_mem.h"
23 #include "mpp_common.h"
24 #include "mpp_frame_impl.h"
25 #include "mpp_meta_impl.h"
26 #include "mpp_mem_pool.h"
27 
28 static const char *module_name = MODULE_TAG;
29 static MppMemPool mpp_frame_pool = mpp_mem_pool_init_f(module_name, sizeof(MppFrameImpl));
30 
setup_mpp_frame_name(MppFrameImpl * frame)31 static void setup_mpp_frame_name(MppFrameImpl *frame)
32 {
33     frame->name = module_name;
34 }
35 
36 #define check_is_mpp_frame(frame) _check_is_mpp_frame(__FUNCTION__, frame)
37 
_check_is_mpp_frame(const char * func,void * frame)38 MPP_RET _check_is_mpp_frame(const char *func, void *frame)
39 {
40     if (frame && ((MppFrameImpl*)frame)->name == module_name)
41         return MPP_OK;
42 
43     mpp_err("pointer %p failed on %s check mpp_frame\n", frame, func);
44     mpp_abort();
45     return MPP_NOK;
46 }
47 
mpp_frame_init(MppFrame * frame)48 MPP_RET mpp_frame_init(MppFrame *frame)
49 {
50     if (NULL == frame) {
51         mpp_err_f("invalid NULL pointer input\n");
52         return MPP_ERR_NULL_PTR;
53     }
54 
55     MppFrameImpl *p = (MppFrameImpl*)mpp_mem_pool_get(mpp_frame_pool);
56     if (NULL == p) {
57         mpp_err_f("malloc failed\n");
58         return MPP_ERR_NULL_PTR;
59     }
60 
61     setup_mpp_frame_name(p);
62     *frame = p;
63 
64     return MPP_OK;
65 }
66 
mpp_frame_deinit(MppFrame * frame)67 MPP_RET mpp_frame_deinit(MppFrame *frame)
68 {
69     if (NULL == frame || check_is_mpp_frame(*frame)) {
70         mpp_err_f("invalid NULL pointer input\n");
71         return MPP_ERR_NULL_PTR;
72     }
73 
74     MppFrameImpl *p = (MppFrameImpl *)*frame;
75     if (p->buffer)
76         mpp_buffer_put(p->buffer);
77 
78     if (p->meta)
79         mpp_meta_put(p->meta);
80 
81     if (p->stopwatch)
82         mpp_stopwatch_put(p->stopwatch);
83 
84     mpp_mem_pool_put(mpp_frame_pool, *frame);
85     *frame = NULL;
86     return MPP_OK;
87 }
88 
mpp_frame_get_buffer(MppFrame frame)89 MppBuffer mpp_frame_get_buffer(MppFrame frame)
90 {
91     if (check_is_mpp_frame(frame))
92         return NULL;
93 
94     MppFrameImpl *p = (MppFrameImpl *)frame;
95     return (MppFrame)p->buffer;
96 }
97 
mpp_frame_set_buffer(MppFrame frame,MppBuffer buffer)98 void mpp_frame_set_buffer(MppFrame frame, MppBuffer buffer)
99 {
100     if (check_is_mpp_frame(frame))
101         return ;
102 
103     MppFrameImpl *p = (MppFrameImpl *)frame;
104     if (p->buffer != buffer) {
105         if (buffer)
106             mpp_buffer_inc_ref(buffer);
107 
108         if (p->buffer)
109             mpp_buffer_put(p->buffer);
110 
111         p->buffer = buffer;
112     }
113 }
114 
mpp_frame_has_meta(const MppFrame frame)115 RK_S32 mpp_frame_has_meta(const MppFrame frame)
116 {
117     if (check_is_mpp_frame(frame))
118         return 0;
119 
120     MppFrameImpl *p = (MppFrameImpl *)frame;
121 
122     return (NULL != p->meta);
123 }
124 
mpp_frame_get_meta(MppFrame frame)125 MppMeta mpp_frame_get_meta(MppFrame frame)
126 {
127     if (check_is_mpp_frame(frame))
128         return NULL;
129 
130     MppFrameImpl *p = (MppFrameImpl *)frame;
131     if (NULL == p->meta)
132         mpp_meta_get(&p->meta);
133 
134     return p->meta;
135 }
136 
mpp_frame_set_meta(MppFrame frame,MppMeta meta)137 void mpp_frame_set_meta(MppFrame frame, MppMeta meta)
138 {
139     if (check_is_mpp_frame(frame))
140         return ;
141 
142     MppFrameImpl *p = (MppFrameImpl *)frame;
143     if (p->meta) {
144         mpp_meta_put(p->meta);
145         p->meta = NULL;
146     }
147 
148     p->meta = meta;
149 }
150 
mpp_frame_get_status(MppFrame frame)151 MppFrameStatus *mpp_frame_get_status(MppFrame frame)
152 {
153     MppFrameImpl *p = (MppFrameImpl *)frame;
154 
155     return &p->status;
156 }
157 
mpp_frame_set_stopwatch_enable(MppFrame frame,RK_S32 enable)158 void mpp_frame_set_stopwatch_enable(MppFrame frame, RK_S32 enable)
159 {
160     if (check_is_mpp_frame(frame))
161         return ;
162 
163     MppFrameImpl *p = (MppFrameImpl *)frame;
164     if (enable && NULL == p->stopwatch) {
165         char name[32];
166 
167         snprintf(name, sizeof(name) - 1, "frm %8llx", p->pts);
168         p->stopwatch = mpp_stopwatch_get(name);
169         if (p->stopwatch)
170             mpp_stopwatch_set_show_on_exit(p->stopwatch, 1);
171     } else if (!enable && p->stopwatch) {
172         mpp_stopwatch_put(p->stopwatch);
173         p->stopwatch = NULL;
174     }
175 }
176 
mpp_frame_get_stopwatch(const MppFrame frame)177 MppStopwatch mpp_frame_get_stopwatch(const MppFrame frame)
178 {
179     if (check_is_mpp_frame(frame))
180         return NULL;
181 
182     MppFrameImpl *p = (MppFrameImpl *)frame;
183     return p->stopwatch;
184 }
185 
mpp_frame_copy(MppFrame dst,MppFrame src)186 MPP_RET mpp_frame_copy(MppFrame dst, MppFrame src)
187 {
188     if (NULL == dst || check_is_mpp_frame(src)) {
189         mpp_err_f("invalid input dst %p src %p\n", dst, src);
190         return MPP_ERR_UNKNOW;
191     }
192 
193     MppFrameImpl *p = (MppFrameImpl *)dst;
194     if (p->meta)
195         mpp_meta_put(p->meta);
196 
197     memcpy(dst, src, sizeof(MppFrameImpl));
198     p = (MppFrameImpl *)src;
199     if (p->meta)
200         mpp_meta_inc_ref(p->meta);
201 
202     return MPP_OK;
203 }
204 
mpp_frame_info_cmp(MppFrame frame0,MppFrame frame1)205 MPP_RET mpp_frame_info_cmp(MppFrame frame0, MppFrame frame1)
206 {
207     if (check_is_mpp_frame(frame0) || check_is_mpp_frame(frame0)) {
208         mpp_err_f("invalid NULL pointer input\n");
209         return MPP_ERR_NULL_PTR;
210     }
211 
212     MppFrameImpl *f0 = (MppFrameImpl *)frame0;
213     MppFrameImpl *f1 = (MppFrameImpl *)frame1;
214 
215     if ((f0->width              == f1->width)  &&
216         (f0->height             == f1->height) &&
217         (f0->hor_stride         == f1->hor_stride) &&
218         (f0->ver_stride         == f1->ver_stride) &&
219         ((f0->fmt & ~MPP_FRAME_HDR_MASK) == (f1->fmt & ~MPP_FRAME_HDR_MASK)) &&
220         (f0->buf_size           == f1->buf_size)) {
221         return MPP_OK;
222     }
223     return MPP_NOK;
224 }
225 
mpp_frame_get_fbc_offset(MppFrame frame)226 RK_U32 mpp_frame_get_fbc_offset(MppFrame frame)
227 {
228     if (check_is_mpp_frame(frame))
229         return 0;
230 
231     MppFrameImpl *p = (MppFrameImpl *)frame;
232 
233     if (MPP_FRAME_FMT_IS_FBC(p->fmt)) {
234         RK_U32 fbc_version = p->fmt & MPP_FRAME_FBC_MASK;
235         RK_U32 fbc_offset = 0;
236 
237         if (fbc_version == MPP_FRAME_FBC_AFBC_V1) {
238             fbc_offset = MPP_ALIGN(MPP_ALIGN(p->width, 16) *
239                                    MPP_ALIGN(p->height, 16) / 16, SZ_4K);
240         } else if (fbc_version == MPP_FRAME_FBC_AFBC_V2) {
241             fbc_offset = 0;
242         }
243         p->fbc_offset = fbc_offset;
244     }
245 
246     return p->fbc_offset;
247 }
248 
mpp_frame_get_fbc_stride(MppFrame frame)249 RK_U32 mpp_frame_get_fbc_stride(MppFrame frame)
250 {
251     if (check_is_mpp_frame(frame))
252         return 0;
253 
254     MppFrameImpl *p = (MppFrameImpl *)frame;
255     return MPP_ALIGN(p->width, 16);
256 }
257 
258 /*
259  * object access function macro
260  */
261 #define MPP_FRAME_ACCESSORS(type, field) \
262     type mpp_frame_get_##field(const MppFrame s) \
263     { \
264         check_is_mpp_frame((MppFrameImpl*)s); \
265         return ((MppFrameImpl*)s)->field; \
266     } \
267     void mpp_frame_set_##field(MppFrame s, type v) \
268     { \
269         check_is_mpp_frame((MppFrameImpl*)s); \
270         ((MppFrameImpl*)s)->field = v; \
271     }
272 
273 MPP_FRAME_ACCESSORS(RK_U32, width)
274 MPP_FRAME_ACCESSORS(RK_U32, height)
275 MPP_FRAME_ACCESSORS(RK_U32, hor_stride_pixel)
276 MPP_FRAME_ACCESSORS(RK_U32, hor_stride)
277 MPP_FRAME_ACCESSORS(RK_U32, ver_stride)
278 MPP_FRAME_ACCESSORS(RK_U32, fbc_hdr_stride)
279 MPP_FRAME_ACCESSORS(RK_U32, offset_x)
280 MPP_FRAME_ACCESSORS(RK_U32, offset_y)
281 MPP_FRAME_ACCESSORS(RK_U32, mode)
282 MPP_FRAME_ACCESSORS(RK_U32, discard)
283 MPP_FRAME_ACCESSORS(RK_U32, viewid)
284 MPP_FRAME_ACCESSORS(RK_U32, poc)
285 MPP_FRAME_ACCESSORS(RK_S64, pts)
286 MPP_FRAME_ACCESSORS(RK_S64, dts)
287 MPP_FRAME_ACCESSORS(RK_U32, eos)
288 MPP_FRAME_ACCESSORS(RK_U32, info_change)
289 MPP_FRAME_ACCESSORS(MppFrameColorRange, color_range)
290 MPP_FRAME_ACCESSORS(MppFrameColorPrimaries, color_primaries)
291 MPP_FRAME_ACCESSORS(MppFrameColorTransferCharacteristic, color_trc)
292 MPP_FRAME_ACCESSORS(MppFrameColorSpace, colorspace)
293 MPP_FRAME_ACCESSORS(MppFrameChromaLocation, chroma_location)
294 MPP_FRAME_ACCESSORS(MppFrameFormat, fmt)
295 MPP_FRAME_ACCESSORS(MppFrameRational, sar)
296 MPP_FRAME_ACCESSORS(MppFrameMasteringDisplayMetadata, mastering_display)
297 MPP_FRAME_ACCESSORS(MppFrameContentLightMetadata, content_light)
298 MPP_FRAME_ACCESSORS(MppFrameHdrDynamicMeta*, hdr_dynamic_meta)
299 MPP_FRAME_ACCESSORS(size_t, buf_size)
300 MPP_FRAME_ACCESSORS(RK_U32, errinfo)
301 MPP_FRAME_ACCESSORS(MppTask, task)
302 MPP_FRAME_ACCESSORS(RK_U32, thumbnail_en)
303 MPP_FRAME_ACCESSORS(size_t, fbc_size)
304