1 /*******************************************************************************
2 * Copyright (C) 2018 Cadence Design Systems, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to use this Software with Cadence processor cores only and
7 * not with any other processors and platforms, subject to
8 * the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included
11 * in all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
18 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 
21 ******************************************************************************/
22 
23 /*******************************************************************************
24  * xa-class-base.h
25  *
26  * Generic Xtensa Audio codecs interfaces
27  *
28  ******************************************************************************/
29 
30 #ifndef __XA_CLASS_BASE_H
31 #define __XA_CLASS_BASE_H
32 
33 /*******************************************************************************
34  * Includes
35  ******************************************************************************/
36 
37 /* ...audio-specific API */
38 #include "audio/xa_type_def.h"
39 #include "audio/xa_error_standards.h"
40 #include "audio/xa_apicmd_standards.h"
41 #include "audio/xa_memory_standards.h"
42 
43 /*******************************************************************************
44  * Generic codec structure
45  ******************************************************************************/
46 
47 typedef struct XACodecBase XACodecBase;
48 
49 /* ...memory buffer initialization */
50 typedef XA_ERRORCODE  (*xa_codec_memtab_f)(XACodecBase *codec, WORD32 i, WORD32 type, WORD32 size, WORD32 align, u32 core);
51 
52 /* ...preprocessing operation */
53 typedef XA_ERRORCODE  (*xa_codec_preprocess_f)(XACodecBase *);
54 
55 /* ...postprocessing operation */
56 typedef XA_ERRORCODE  (*xa_codec_postprocess_f)(XACodecBase *, int);
57 
58 /* ...parameter setting function */
59 typedef XA_ERRORCODE  (*xa_codec_setparam_f)(XACodecBase *, WORD32, pVOID p);
60 
61 /* ...parameter retrival function */
62 typedef XA_ERRORCODE  (*xa_codec_getparam_f)(XACodecBase *, WORD32, pVOID p);
63 
64 /*******************************************************************************
65  * Codec instance structure
66  ******************************************************************************/
67 
68 struct XACodecBase
69 {
70     /***************************************************************************
71      * Control data
72      **************************************************************************/
73 
74     /* ...generic component handle */
75     xf_component_t          component;
76 
77     /* ...codec API entry point (function) */
78     xa_codec_func_t        *process;
79 
80     /* ...codec API handle, passed to *process */
81     xf_mm_buffer_t          api;
82 
83     /* ...memory table buffer */
84     xf_mm_buffer_t          mem_tabs;
85 
86     /* ...persistent memory buffer */
87     xf_mm_buffer_t          persist;
88 
89     /* ...scratch memory pointer */
90     void                   *scratch;
91 
92     /* ...codec control state */
93     u32                     state;
94 
95     /***************************************************************************
96      * Codec-specific methods
97      **************************************************************************/
98 
99     /* ...memory buffer initialization */
100     xa_codec_memtab_f       memtab;
101 
102     /* ...preprocessing function */
103     xa_codec_preprocess_f   preprocess;
104 
105     /* ...postprocessing function */
106     xa_codec_postprocess_f  postprocess;
107 
108     /* ...configuration parameter setting function */
109     xa_codec_setparam_f     setparam;
110 
111     /* ...configuration parameter retrieval function */
112     xa_codec_getparam_f     getparam;
113 
114     /* ...command-processing table */
115     XA_ERRORCODE (* const * command)(XACodecBase *, xf_message_t *);
116 
117     /* ...command-processing table size */
118     u32                     command_num;
119 };
120 
121 /*******************************************************************************
122  * Base codec execution flags
123  ******************************************************************************/
124 
125 /* ...codec static initialization completed */
126 #define XA_BASE_FLAG_POSTINIT           (1 << 0)
127 
128 /* ...codec runtime initialization sequence */
129 #define XA_BASE_FLAG_RUNTIME_INIT       (1 << 1)
130 
131 /* ...codec steady execution state */
132 #define XA_BASE_FLAG_EXECUTION          (1 << 2)
133 
134 /* ...execution stage completed */
135 #define XA_BASE_FLAG_COMPLETED          (1 << 3)
136 
137 /* ...data processing scheduling flag */
138 #define XA_BASE_FLAG_SCHEDULE           (1 << 4)
139 
140 /* ...base codec flags accessor */
141 #define __XA_BASE_FLAGS(flags)          ((flags) & ((1 << 5) - 1))
142 
143 /* ...custom execution flag */
144 #define __XA_BASE_FLAG(f)               ((f) << 5)
145 
146 /*******************************************************************************
147  * Local macros definitions
148  ******************************************************************************/
149 
150 /* ...audio-framework API function execution */
151 #define XA_CHK(cond)                            \
152 ({                                              \
153     XA_ERRORCODE  __e = (cond);                 \
154     if (__e != XA_NO_ERROR)                     \
155     {                                           \
156         if (XA_ERROR_SEVERITY(__e))             \
157         {                                       \
158             TRACE(ERROR, _x("error: %X"), __e); \
159             return __e;                         \
160         }                                       \
161         TRACE(WARNING, _x("warning: %X"), __e); \
162     }                                           \
163     __e;                                        \
164 })
165 
166 /* ...low-level codec API function execution */
167 #define XA_API(codec, cmd, idx, pv)                                                         \
168 ({                                                                                          \
169     XA_ERRORCODE  __e;                                                                      \
170     __e = (codec)->process((xa_codec_handle_t)(codec)->api.addr, (cmd), (idx), (pv));       \
171     if (__e != XA_NO_ERROR)                                                                 \
172     {                                                                                       \
173         if (XA_ERROR_SEVERITY(__e))                                                         \
174         {                                                                                   \
175             TRACE(ERROR, _x("[%p]:(%d, %d, %p): %X"), (codec), (cmd), (idx), (pv), __e);    \
176             return __e;                                                                     \
177         }                                                                                   \
178         TRACE(WARNING, _x("%X"), __e);                                                      \
179     }                                                                                       \
180     __e;                                                                                    \
181 })
182 
183 #define XA_API_NORET(codec, cmd, idx, pv)                                                   \
184 ({                                                                                          \
185     XA_ERRORCODE  __e;                                                                      \
186     __e = (codec)->process((xa_codec_handle_t)(codec)->api.addr, (cmd), (idx), (pv));       \
187     if (__e != XA_NO_ERROR)                                                                 \
188     {                                                                                       \
189         if (XA_ERROR_SEVERITY(__e))                                                         \
190         {                                                                                   \
191             TRACE(ERROR, _x("[%p]:(%d, %d, %p): %X"), (codec), (cmd), (idx), (pv), __e);    \
192         }                                                                                   \
193         TRACE(WARNING, _x("%X"), __e);                                                      \
194     }                                                                                       \
195     __e;                                                                                    \
196 })
197 
198 /* ...codec hook invocation */
199 #define CODEC_API(codec, func, ...)                                 \
200 ({                                                                  \
201     XA_ERRORCODE    __e = (codec)->func((codec), ##__VA_ARGS__);    \
202                                                                     \
203     if (__e != XA_NO_ERROR)                                         \
204     {                                                               \
205         if (XA_ERROR_SEVERITY(__e))                                 \
206         {                                                           \
207             /* ...actual error is reported by the codec */          \
208             TRACE(ERROR, _x("[%p]: " #func ": %X"), (codec), __e);  \
209             return __e;                                             \
210         }                                                           \
211                                                                     \
212         TRACE(WARNING, _x("warning: %X"), __e);                     \
213     }                                                               \
214     __e;                                                            \
215 })
216 
217 /* ...allocate local memory on specific core */
218 #define XMALLOC(p, size, align, core)                                           \
219 do                                                                              \
220 {                                                                               \
221     if (xf_mm_alloc_buffer((size), (align), (core), (p)) != 0)                  \
222     {                                                                           \
223         TRACE(ERROR, _x("Failed to allocate %d bytes of memory"), (size));      \
224         return XA_API_FATAL_MEM_ALLOC;                                          \
225     }                                                                           \
226                                                                                 \
227     if (((u32)((p)->addr) & ((align) - 1)) != 0)                                \
228     {                                                                           \
229         TRACE(ERROR, _x("Invalid %d-algnment: %p"), (align), (p)->addr);        \
230         return XA_API_FATAL_MEM_ALIGN;                                          \
231     }                                                                           \
232 }                                                                               \
233 while (0)
234 
235 /*******************************************************************************
236  * Public API
237  ******************************************************************************/
238 
239 /* ...SET-PARAM processing */
240 extern XA_ERRORCODE xa_base_set_param(XACodecBase *base, xf_message_t *m);
241 
242 /* ...GET-PARAM-EXT message processing */
243 extern XA_ERRORCODE xa_base_set_param_ext(XACodecBase *base, xf_message_t *m);
244 
245 /* ...GET-PARAM message processing */
246 extern XA_ERRORCODE xa_base_get_param(XACodecBase *base, xf_message_t *m);
247 
248 /* ...GET-PARAM-EXT message processing */
249 extern XA_ERRORCODE xa_base_get_param_ext(XACodecBase *base, xf_message_t *m);
250 
251 /* ...data processing scheduling */
252 extern void xa_base_schedule(XACodecBase *base, u32 dts);
253 
254 /* ...cancel internal scheduling message */
255 extern void xa_base_cancel(XACodecBase *base);
256 
257 /* ...base codec factory */
258 extern XACodecBase * xa_base_factory(u32 core, u32 size, xa_codec_func_t process);
259 
260 /* ...base codec destructor */
261 extern void xa_base_destroy(XACodecBase *base, u32 size, u32 core);
262 
263 #endif  /* __XA_CLASS_BASE_H */
264