xref: /rockchip-linux_mpp/mpp/codec/enc_impl.c (revision 437bfbeb9567cca9cd9080e3f6954aa9d6a94f18)
1 /* SPDX-License-Identifier: Apache-2.0 OR MIT */
2 /*
3  * Copyright (c) 2010 Rockchip Electronics Co., Ltd.
4  */
5 
6 #define  MODULE_TAG "enc_impl"
7 
8 #include <string.h>
9 
10 #include "mpp_mem.h"
11 #include "mpp_debug.h"
12 #include "mpp_common.h"
13 #include "mpp_packet_impl.h"
14 
15 #include "h264e_api_v2.h"
16 #include "jpege_api_v2.h"
17 #include "h265e_api.h"
18 #include "vp8e_api_v2.h"
19 #include "enc_impl.h"
20 
21 /*
22  * all encoder controller static register here
23  */
24 static const EncImplApi *enc_apis[] = {
25 #if HAVE_H264E
26     &api_h264e,
27 #endif
28 #if HAVE_H265E
29     &api_h265e,
30 #endif
31 #if HAVE_JPEGE
32     &api_jpege,
33 #endif
34 #if HAVE_VP8E
35     &api_vp8e,
36 #endif
37 };
38 
39 typedef struct EncImplCtx_t {
40     EncImplCfg          cfg;
41     const EncImplApi    *api;
42     void                *ctx;
43 } EncImplCtx;
44 
enc_impl_init(EncImpl * impl,EncImplCfg * cfg)45 MPP_RET enc_impl_init(EncImpl *impl, EncImplCfg *cfg)
46 {
47     if (NULL == impl || NULL == cfg) {
48         mpp_err_f("found NULL input controller %p config %p\n", impl, cfg);
49         return MPP_ERR_NULL_PTR;
50     }
51 
52     *impl = NULL;
53 
54     RK_U32 i;
55     const EncImplApi **apis = enc_apis;
56     RK_U32 api_cnt = MPP_ARRAY_ELEMS(enc_apis);
57 
58     for (i = 0; i < api_cnt; i++) {
59         const EncImplApi *api = apis[i];
60 
61         if (cfg->coding == api->coding) {
62             EncImplCtx *p = mpp_calloc(EncImplCtx, 1);
63             void *ctx = mpp_calloc_size(void, api->ctx_size);
64 
65             if (NULL == ctx || NULL == p) {
66                 mpp_err_f("failed to alloc encoder context\n");
67                 mpp_free(p);
68                 mpp_free(ctx);
69                 return MPP_ERR_MALLOC;
70             }
71 
72             MPP_RET ret = api->init(ctx, cfg);
73             if (MPP_OK != ret) {
74                 mpp_err_f("failed to init controller\n");
75                 mpp_free(p);
76                 mpp_free(ctx);
77                 return ret;
78             }
79 
80             p->api  = api;
81             p->ctx  = ctx;
82             memcpy(&p->cfg, cfg, sizeof(p->cfg));
83             *impl = p;
84             return MPP_OK;
85         }
86     }
87 
88     return MPP_NOK;
89 }
90 
enc_impl_deinit(EncImpl impl)91 MPP_RET enc_impl_deinit(EncImpl impl)
92 {
93     if (NULL == impl) {
94         mpp_err_f("found NULL input\n");
95         return MPP_ERR_NULL_PTR;
96     }
97 
98     EncImplCtx *p = (EncImplCtx *)impl;
99     if (p->api->deinit)
100         p->api->deinit(p->ctx);
101 
102     mpp_free(p->ctx);
103     mpp_free(p);
104     return MPP_OK;
105 }
106 
enc_impl_proc_cfg(EncImpl impl,MpiCmd cmd,void * para)107 MPP_RET enc_impl_proc_cfg(EncImpl impl, MpiCmd cmd, void *para)
108 {
109     if (NULL == impl) {
110         mpp_err_f("found NULL input\n");
111         return MPP_ERR_NULL_PTR;
112     }
113 
114     MPP_RET ret = MPP_OK;
115     EncImplCtx *p = (EncImplCtx *)impl;
116     if (p->api->proc_cfg)
117         ret = p->api->proc_cfg(p->ctx, cmd, para);
118 
119     return ret;
120 }
121 
enc_impl_gen_hdr(EncImpl impl,MppPacket pkt)122 MPP_RET enc_impl_gen_hdr(EncImpl impl, MppPacket pkt)
123 {
124     if (NULL == impl) {
125         mpp_err_f("found NULL input\n");
126         return MPP_ERR_NULL_PTR;
127     }
128 
129     MPP_RET ret = MPP_OK;
130     EncImplCtx *p = (EncImplCtx *)impl;
131 
132     if (p->api->gen_hdr) {
133         if (pkt)
134             mpp_packet_reset_segment(pkt);
135 
136         ret = p->api->gen_hdr(p->ctx, pkt);
137     }
138 
139     return ret;
140 }
141 
enc_impl_start(EncImpl impl,HalEncTask * task)142 MPP_RET enc_impl_start(EncImpl impl, HalEncTask *task)
143 {
144     if (NULL == impl) {
145         mpp_err_f("found NULL input\n");
146         return MPP_ERR_NULL_PTR;
147     }
148 
149     MPP_RET ret = MPP_OK;
150     EncImplCtx *p = (EncImplCtx *)impl;
151     if (p->api->start)
152         ret = p->api->start(p->ctx, task);
153 
154     return ret;
155 }
156 
enc_impl_proc_dpb(EncImpl impl,HalEncTask * task)157 MPP_RET enc_impl_proc_dpb(EncImpl impl, HalEncTask *task)
158 {
159     if (NULL == impl) {
160         mpp_err_f("found NULL input\n");
161         return MPP_ERR_NULL_PTR;
162     }
163 
164     MPP_RET ret = MPP_OK;
165     EncImplCtx *p = (EncImplCtx *)impl;
166     if (p->api->proc_dpb)
167         ret = p->api->proc_dpb(p->ctx, task);
168 
169     return ret;
170 }
171 
enc_impl_proc_hal(EncImpl impl,HalEncTask * task)172 MPP_RET enc_impl_proc_hal(EncImpl impl, HalEncTask *task)
173 {
174     if (NULL == impl || NULL == task) {
175         mpp_err_f("found NULL input\n");
176         return MPP_ERR_NULL_PTR;
177     }
178 
179     MPP_RET ret = MPP_OK;
180     EncImplCtx *p = (EncImplCtx *)impl;
181     if (p->api->proc_hal)
182         ret = p->api->proc_hal(p->ctx, task);
183 
184     return ret;
185 }
186 
enc_impl_add_prefix(EncImpl impl,MppPacket pkt,RK_S32 * length,RK_U8 uuid[16],const void * data,RK_S32 size)187 MPP_RET enc_impl_add_prefix(EncImpl impl, MppPacket pkt, RK_S32 *length,
188                             RK_U8 uuid[16], const void *data, RK_S32 size)
189 {
190     if (NULL == pkt || NULL == data) {
191         mpp_err_f("found NULL input\n");
192         return MPP_ERR_NULL_PTR;
193     }
194 
195     MPP_RET ret = MPP_OK;
196     EncImplCtx *p = (EncImplCtx *)impl;
197 
198     if (NULL == p->api->add_prefix)
199         return ret;
200 
201     if (p->api->add_prefix)
202         ret = p->api->add_prefix(pkt, length, uuid, data, size);
203 
204     return ret;
205 }
206 
enc_impl_sw_enc(EncImpl impl,HalEncTask * task)207 MPP_RET enc_impl_sw_enc(EncImpl impl, HalEncTask *task)
208 {
209     if (NULL == impl || NULL == task) {
210         mpp_err_f("found NULL input\n");
211         return MPP_ERR_NULL_PTR;
212     }
213 
214     MPP_RET ret = MPP_OK;
215     EncImplCtx *p = (EncImplCtx *)impl;
216     if (p->api->sw_enc)
217         ret = p->api->sw_enc(p->ctx, task);
218 
219     return ret;
220 }
221