Sound Open Firmware
Loading...
Searching...
No Matches
audio_buffer.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: BSD-3-Clause
2 *
3 * Copyright(c) 2023 Intel Corporation. All rights reserved.
4 *
5 */
6#ifndef __SOF_AUDIO_BUFFER__
7#define __SOF_AUDIO_BUFFER__
8
9#include <sof/common.h>
10#include <ipc/topology.h>
11#include <sof/coherent.h>
12#include <sof/audio/sink_api.h>
14
15#define BUFFER_TYPE_LEGACY_BUFFER 1
16#define BUFFER_TYPE_RING_BUFFER 2
17
18/* forward def */
19struct sof_audio_buffer;
20
27 void (*free)(struct sof_audio_buffer *buffer);
28
34 void (*reset)(struct sof_audio_buffer *buffer);
35
45 int (*on_audio_format_set)(struct sof_audio_buffer *buffer);
46
52 struct sof_ipc_stream_params *params, bool force_update);
53
59 const uint32_t byte_align,
60 const uint32_t frame_align_req);
61};
62
63struct mod_alloc_ctx;
64/* base class for all buffers, all buffers must inherit from it */
67
68 /* type of the buffer BUFFER_TYPE_* */
69 uint32_t buffer_type;
70
71 bool is_shared; /* buffer structure is shared between 2 cores */
72
73#if CONFIG_PIPELINE_2_0
80 struct sof_audio_buffer *secondary_buffer_sink;
81
86 struct sof_audio_buffer *secondary_buffer_source;
87
88#endif /* CONFIG_PIPELINE_2_0 */
89
90 /* effective runtime stream params
91 * before pipelin2.0 is ready, stream params may be kept in audio_stream structure
92 * also for hybrid buffering audio params need to be shared between primary and secondary
93 * buffers
94 *
95 * So currently only a pointer to effective stream params is here.
96 * Note that the same pointer MUST be set in source and sink api (kept there for
97 * performance reasons)
98 */
99 struct sof_audio_stream_params *audio_stream_params;
100
101
102 /* private: */
103 struct sof_source _source_api;
104 struct sof_sink _sink_api;
105
106 /* virtual methods */
107 const struct audio_buffer_ops *ops;
108
109 /*
110 * legacy params, needed for pipeline binding/iterating, not for data buffering
111 * should not be in struct sof_audio_buffer at all, kept for pipeline2.0 transition
112 */
113 bool walking;
114
116};
117
118#if CONFIG_PIPELINE_2_0
119/*
120 * attach a secondary buffer (any type) before buffer (when at_input == true) or behind a buffer
121 *
122 * before buffer (at_input == true):
123 * 2.0 mod ==> (sink_API) secondary buffer ==>
124 * ==> comp_buffer (audio_stream or source API) ==> 1.0 mod
125 *
126 * after buffer (at_input == false):
127 * 1.0 mod ==> (audio_stream or sink API) ==> comp_buffer ==>
128 * ==> secondary buffer(source API) == 2.0 mod
129 *
130 * If a secondary buffer is attached, it replaces source or sink interface of audio_stream
131 * allowing the module connected to it using all properties of secondary buffer (like
132 * lockless cross-core connection in case of ring_buffer etc.) keeping legacy interface
133 * to other modules
134 *
135 * buffer_sync_secondary_buffer must be called every 1 ms to move data to/from
136 * secondary buffer to comp_buffer
137 *
138 * @param buffer pointer to a buffer
139 * @param at_input true indicates that a secondary buffer is located at data input, replacing
140 * sink API of audio_stream
141 * false indicates that a secondary buffer is located at data output, replacing
142 * source API of audio_stream
143 * @param secondary_buffer pointer to a buffer to be attached
144 *
145 * to be removed when hybrid buffers are no longer needed
146 */
147int audio_buffer_attach_secondary_buffer(struct sof_audio_buffer *buffer, bool at_input,
148 struct sof_audio_buffer *secondary_buffer);
149
150/*
151 * move data from/to secondary buffer, must be called periodically as described above
152 *
153 * @param buffer pointer to a buffer
154 * @param limit data copy limit. Indicates maximum amount of data that will be moved from/to
155 * secondary buffer in an operation
156 *
157 * to be removed when hybrid buffers are no longer needed
158 */
159int audio_buffer_sync_secondary_buffer(struct sof_audio_buffer *buffer, size_t limit);
160
165static inline
166struct sof_sink *audio_buffer_get_sink(struct sof_audio_buffer *buffer)
167{
168 CORE_CHECK_STRUCT(buffer);
169 return buffer->secondary_buffer_sink ?
170 audio_buffer_get_sink(buffer->secondary_buffer_sink) :
171 &buffer->_sink_api;
172}
173
178static inline
179struct sof_source *audio_buffer_get_source(struct sof_audio_buffer *buffer)
180{
181 CORE_CHECK_STRUCT(buffer);
182 return buffer->secondary_buffer_source ?
183 audio_buffer_get_source(buffer->secondary_buffer_source) :
184 &buffer->_source_api;
185}
186
187#else /* CONFIG_PIPELINE_2_0 */
188
193static inline
194struct sof_sink *audio_buffer_get_sink(struct sof_audio_buffer *buffer)
195{
196 CORE_CHECK_STRUCT(buffer);
197 return &buffer->_sink_api;
198}
199
204static inline
205struct sof_source *audio_buffer_get_source(struct sof_audio_buffer *buffer)
206{
207 CORE_CHECK_STRUCT(buffer);
208 return &buffer->_source_api;
209}
210
211#endif /* CONFIG_PIPELINE_2_0 */
212
213static inline bool audio_buffer_is_shared(struct sof_audio_buffer *buffer)
214{
215 return buffer->is_shared;
216}
217
218static inline bool audio_buffer_hw_params_configured(struct sof_audio_buffer *buffer)
219{
220 return buffer->audio_stream_params->state != AUDIOBUF_STATE_INITIAL;
221}
222
224{
225 buffer->audio_stream_params->state = AUDIOBUF_STATE_READY;
226}
227
228static inline void audio_buffer_reset_params(struct sof_audio_buffer *buffer)
229{
230 buffer->audio_stream_params->state = AUDIOBUF_STATE_INITIAL;
231}
232
233static inline enum sof_audio_buffer_state audio_buffer_get_state(
234 const struct sof_audio_buffer *buffer)
235{
236 return buffer->audio_stream_params->state;
237}
238
239static inline void audio_buffer_set_state(struct sof_audio_buffer *buffer,
240 enum sof_audio_buffer_state state)
241{
242 buffer->audio_stream_params->state = state;
243}
244
245static inline void audio_buffer_set_eos(struct sof_audio_buffer *buffer)
246{
247 buffer->audio_stream_params->state = AUDIOBUF_STATE_END_OF_STREAM;
248}
249
250static inline uint16_t audio_buffer_get_chmap(struct sof_audio_buffer *buffer, size_t index)
251{
252 return buffer->audio_stream_params->chmap[index];
253}
254
255static inline void audio_buffer_set_chmap(struct sof_audio_buffer *buffer, size_t index,
256 uint16_t value)
257{
258 buffer->audio_stream_params->chmap[index] = value;
259}
260
264static inline
265struct sof_audio_stream_params *audio_buffer_get_stream_params(struct sof_audio_buffer *buffer)
266{
267 return buffer->audio_stream_params;
268}
269
275static inline struct sof_audio_buffer *sof_audio_buffer_from_sink(struct sof_sink *sink)
276{
277 return container_of(sink, struct sof_audio_buffer, _sink_api);
278}
279
285static inline struct sof_audio_buffer *sof_audio_buffer_from_source(struct sof_source *source)
286{
287 return container_of(source, struct sof_audio_buffer, _source_api);
288}
289
309void audio_buffer_init(struct sof_audio_buffer *buffer, uint32_t buffer_type, bool is_shared,
310 const struct source_ops *source_ops, const struct sink_ops *sink_ops,
312 struct sof_audio_stream_params *audio_stream_params);
313
318
323static inline
325{
326 if (buffer->ops->reset)
327 buffer->ops->reset(buffer);
328
329#if CONFIG_PIPELINE_2_0
330 if (buffer->secondary_buffer_sink && buffer->secondary_buffer_sink->ops->reset)
331 buffer->secondary_buffer_sink->ops->reset(buffer->secondary_buffer_sink);
332
333 if (buffer->secondary_buffer_source && buffer->secondary_buffer_source->ops->reset)
334 buffer->secondary_buffer_source->ops->reset(buffer->secondary_buffer_source);
335#endif
336}
337
338/* Audio-buffer wrappers for the source-sink API */
339int audio_buffer_source_set_ipc_params(struct sof_source *source,
340 struct sof_ipc_stream_params *params, bool force_update);
341int audio_buffer_source_on_audio_format_set(struct sof_source *source);
343 const uint32_t byte_align,
344 const uint32_t frame_align_req);
345int audio_buffer_sink_set_ipc_params(struct sof_sink *sink, struct sof_ipc_stream_params *params,
346 bool force_update);
347int audio_buffer_sink_on_audio_format_set(struct sof_sink *sink);
349 const uint32_t byte_align,
350 const uint32_t frame_align_req);
351uint32_t audio_buffer_sink_get_lft(struct sof_sink *sink);
352
353#endif /* __SOF_AUDIO_BUFFER__ */
static void audio_buffer_set_hw_params_configured(struct sof_audio_buffer *buffer)
Definition audio_buffer.h:223
static struct sof_sink * audio_buffer_get_sink(struct sof_audio_buffer *buffer)
return a handler to sink API of audio_buffer.
Definition audio_buffer.h:194
static struct sof_audio_stream_params * audio_buffer_get_stream_params(struct sof_audio_buffer *buffer)
return a handler to stream params structure
Definition audio_buffer.h:265
static uint16_t audio_buffer_get_chmap(struct sof_audio_buffer *buffer, size_t index)
Definition audio_buffer.h:250
static void audio_buffer_set_eos(struct sof_audio_buffer *buffer)
Definition audio_buffer.h:245
static bool audio_buffer_is_shared(struct sof_audio_buffer *buffer)
Definition audio_buffer.h:213
static void audio_buffer_reset(struct sof_audio_buffer *buffer)
clean all buffer data, set buffer positions to initial, leaving config as is the procedure is to be c...
Definition audio_buffer.h:324
uint32_t audio_buffer_sink_get_lft(struct sof_sink *sink)
int audio_buffer_source_set_ipc_params(struct sof_source *source, struct sof_ipc_stream_params *params, bool force_update)
static void audio_buffer_set_chmap(struct sof_audio_buffer *buffer, size_t index, uint16_t value)
Definition audio_buffer.h:255
static enum sof_audio_buffer_state audio_buffer_get_state(const struct sof_audio_buffer *buffer)
Definition audio_buffer.h:233
static struct sof_audio_buffer * sof_audio_buffer_from_sink(struct sof_sink *sink)
return a pointer to struct sof_audio_buffer from sink pointer NOTE!
Definition audio_buffer.h:275
int audio_buffer_sink_set_ipc_params(struct sof_sink *sink, struct sof_ipc_stream_params *params, bool force_update)
static struct sof_audio_buffer * sof_audio_buffer_from_source(struct sof_source *source)
return a pointer to struct sof_audio_buffer from source pointer NOTE!
Definition audio_buffer.h:285
static bool audio_buffer_hw_params_configured(struct sof_audio_buffer *buffer)
Definition audio_buffer.h:218
int audio_buffer_source_set_alignment_constants(struct sof_source *source, const uint32_t byte_align, const uint32_t frame_align_req)
static struct sof_source * audio_buffer_get_source(struct sof_audio_buffer *buffer)
return a handler to source API of audio_buffer the handler may be used by helper functions defined in...
Definition audio_buffer.h:205
static void audio_buffer_set_state(struct sof_audio_buffer *buffer, enum sof_audio_buffer_state state)
Definition audio_buffer.h:239
int audio_buffer_source_on_audio_format_set(struct sof_source *source)
int audio_buffer_sink_set_alignment_constants(struct sof_sink *sink, const uint32_t byte_align, const uint32_t frame_align_req)
int audio_buffer_sink_on_audio_format_set(struct sof_sink *sink)
static void audio_buffer_reset_params(struct sof_audio_buffer *buffer)
Definition audio_buffer.h:228
void audio_buffer_init(struct sof_audio_buffer *buffer, uint32_t buffer_type, bool is_shared, const struct source_ops *source_ops, const struct sink_ops *sink_ops, const struct audio_buffer_ops *audio_buffer_ops, struct sof_audio_stream_params *audio_stream_params)
initialize audio buffer structures
void audio_buffer_free(struct sof_audio_buffer *buffer)
free buffer and all allocated resources
#define CORE_CHECK_STRUCT(_c)
Definition coherent.h:104
#define container_of(ptr, type, member)
Definition common.h:119
Definition audio_buffer.h:21
void(* free)(struct sof_audio_buffer *buffer)
this method must free all structures allocated by buffer implementation and the buffer itself OBLIGAT...
Definition audio_buffer.h:27
int(* audio_set_ipc_params)(struct sof_audio_buffer *buffer, struct sof_ipc_stream_params *params, bool force_update)
OPTIONAL see sink_set_params comments.
Definition audio_buffer.h:51
int(* on_audio_format_set)(struct sof_audio_buffer *buffer)
OPTIONAL: Notification to the sink implementation about changes in audio format.
Definition audio_buffer.h:45
int(* set_alignment_constants)(struct sof_audio_buffer *buffer, const uint32_t byte_align, const uint32_t frame_align_req)
OPTIONAL see comment for sink_set_alignment_constants.
Definition audio_buffer.h:58
void(* reset)(struct sof_audio_buffer *buffer)
clean all buffer data, set buffer positions to initial, leaving config as is the procedure is to be c...
Definition audio_buffer.h:34
Definition alloc.h:167
Definition audio_buffer.h:65
CORE_CHECK_STRUCT_FIELD
Definition audio_buffer.h:66
const struct audio_buffer_ops * ops
Definition audio_buffer.h:107
bool walking
indicates if the buffer is being walked
Definition audio_buffer.h:113
struct sof_source _source_api
src api handler
Definition audio_buffer.h:103
bool is_shared
Definition audio_buffer.h:71
struct sof_audio_stream_params * audio_stream_params
Definition audio_buffer.h:99
uint32_t buffer_type
Definition audio_buffer.h:69
struct mod_alloc_ctx * alloc
Definition audio_buffer.h:115
struct sof_sink _sink_api
sink api handler
Definition audio_buffer.h:104
Definition stream.h:71
char value[]
Definition tlv.h:2