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