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