xref: /OK3568_Linux_fs/external/mpp/mpp/codec/mpp_parser.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_dec"
18 
19 #include <string.h>
20 
21 #include "mpp_mem.h"
22 #include "mpp_log.h"
23 #include "mpp_common.h"
24 
25 #include "mpp_parser.h"
26 
27 #include "h263d_api.h"
28 #include "h264d_api.h"
29 #include "h265d_api.h"
30 #include "vp9d_api.h"
31 #include "avsd_api.h"
32 #include "avs2d_api.h"
33 #include "m2vd_api.h"
34 #include "mpg4d_api.h"
35 #include "vp8d_api.h"
36 #include "jpegd_api.h"
37 #include "av1d_api.h"
38 
39 // for test and demo
40 #include "dummy_dec_api.h"
41 
42 /*
43  * all decoder static register here
44  */
45 static const ParserApi *parsers[] = {
46 #if HAVE_AVSD
47     &api_avsd_parser,
48     &api_avsd_plus_parser,
49 #endif
50 #if HAVE_AVS2D
51     &api_avs2d_parser,
52 #endif
53 #if HAVE_H263D
54     &api_h263d_parser,
55 #endif
56 #if HAVE_H264D
57     &api_h264d_parser,
58 #endif
59 #if HAVE_H265D
60     &api_h265d_parser,
61 #endif
62 #if HAVE_MPEG2D
63     &api_m2vd_parser,
64 #endif
65 #if HAVE_MPEG4D
66     &api_mpg4d_parser,
67 #endif
68 #if HAVE_VP8D
69     &api_vp8d_parser,
70 #endif
71 #if HAVE_VP9D
72     &api_vp9d_parser,
73 #endif
74 #if HAVE_JPEGD
75     &api_jpegd_parser,
76 #endif
77     &dummy_dec_parser,
78 #if HAVE_AV1D
79     &api_av1d_parser,
80 #endif
81 };
82 
83 typedef struct ParserImpl_t {
84     const ParserApi     *api;
85     void                *ctx;
86 } ParserImpl;
87 
mpp_parser_init(Parser * prs,ParserCfg * cfg)88 MPP_RET mpp_parser_init(Parser *prs, ParserCfg *cfg)
89 {
90     if (NULL == prs || NULL == cfg) {
91         mpp_err_f("found NULL input parser %p config %p\n", prs, cfg);
92         return MPP_ERR_NULL_PTR;
93     }
94 
95     *prs = NULL;
96 
97     RK_U32 i;
98     for (i = 0; i < MPP_ARRAY_ELEMS(parsers); i++) {
99         const ParserApi *api = parsers[i];
100         if (cfg->coding == api->coding) {
101             ParserImpl *p = mpp_calloc(ParserImpl, 1);
102             void *ctx = mpp_calloc_size(void, api->ctx_size);
103             if (NULL == ctx || NULL == p) {
104                 mpp_err_f("failed to alloc parser context\n");
105                 mpp_free(p);
106                 mpp_free(ctx);
107                 return MPP_ERR_MALLOC;
108             }
109 
110             MPP_RET ret = api->init(ctx, cfg);
111             if (MPP_OK != ret) {
112                 mpp_err_f("failed to init parser\n");
113                 mpp_free(p);
114                 mpp_free(ctx);
115                 return ret;
116             }
117 
118             p->api  = api;
119             p->ctx  = ctx;
120             *prs = p;
121             return MPP_OK;
122         }
123     }
124     return MPP_NOK;
125 }
126 
mpp_parser_deinit(Parser prs)127 MPP_RET mpp_parser_deinit(Parser prs)
128 {
129     if (NULL == prs) {
130         mpp_err_f("found NULL input\n");
131         return MPP_ERR_NULL_PTR;
132     }
133 
134     ParserImpl *p = (ParserImpl *)prs;
135     if (p->api->deinit)
136         p->api->deinit(p->ctx);
137 
138     mpp_free(p->ctx);
139     mpp_free(p);
140     return MPP_OK;
141 }
142 
mpp_parser_prepare(Parser prs,MppPacket pkt,HalDecTask * task)143 MPP_RET mpp_parser_prepare(Parser prs, MppPacket pkt, HalDecTask *task)
144 {
145     if (NULL == prs || NULL == pkt) {
146         mpp_err_f("found NULL input\n");
147         return MPP_ERR_NULL_PTR;
148     }
149 
150     ParserImpl *p = (ParserImpl *)prs;
151     if (!p->api->prepare)
152         return MPP_OK;
153 
154     return p->api->prepare(p->ctx, pkt, task);
155 }
156 
mpp_parser_parse(Parser prs,HalDecTask * task)157 MPP_RET mpp_parser_parse(Parser prs, HalDecTask *task)
158 {
159     if (NULL == prs || NULL == task) {
160         mpp_err_f("found NULL input\n");
161         return MPP_ERR_NULL_PTR;
162     }
163 
164     ParserImpl *p = (ParserImpl *)prs;
165     if (!p->api->parse)
166         return MPP_OK;
167 
168     return p->api->parse(p->ctx, task);
169 }
170 
mpp_parser_callback(void * prs,void * err_info)171 MPP_RET mpp_parser_callback(void *prs, void *err_info)
172 {
173     if (NULL == prs) {
174         mpp_err_f("found NULL input\n");
175         return MPP_ERR_NULL_PTR;
176     }
177     ParserImpl *p = (ParserImpl *)prs;
178     if (!p->api->callback)
179         return MPP_OK;
180     return p->api->callback(p->ctx, err_info);
181 }
182 
mpp_parser_reset(Parser prs)183 MPP_RET mpp_parser_reset(Parser prs)
184 {
185     if (NULL == prs) {
186         mpp_err_f("found NULL input\n");
187         return MPP_ERR_NULL_PTR;
188     }
189 
190     ParserImpl *p = (ParserImpl *)prs;
191     if (!p->api->reset)
192         return MPP_OK;
193 
194     return p->api->reset(p->ctx);
195 }
196 
mpp_parser_flush(Parser prs)197 MPP_RET mpp_parser_flush(Parser prs)
198 {
199     if (NULL == prs) {
200         mpp_err_f("found NULL input\n");
201         return MPP_ERR_NULL_PTR;
202     }
203 
204     ParserImpl *p = (ParserImpl *)prs;
205     if (!p->api->flush)
206         return MPP_OK;
207 
208     return p->api->flush(p->ctx);
209 }
210 
mpp_parser_control(Parser prs,MpiCmd cmd,void * para)211 MPP_RET mpp_parser_control(Parser prs, MpiCmd cmd, void *para)
212 {
213     if (NULL == prs) {
214         mpp_err_f("found NULL input\n");
215         return MPP_ERR_NULL_PTR;
216     }
217 
218     ParserImpl *p = (ParserImpl *)prs;
219     if (!p->api->control)
220         return MPP_OK;
221 
222     return p->api->control(p->ctx, cmd, para);
223 }
224 
225