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