Skip to content

Conversation

@dbaluta
Copy link
Collaborator

@dbaluta dbaluta commented Sep 18, 2020

Introduce two new API functions, that will copy audio streams data
to/from raw buffers.

This is useful for the new codec adapter where we need to interface
data copying between SOF modules and library buffers.

For example, in this new usecase data sent from an audio component to
a decoder component is compressed so we cannot interpret it with builtin
types (s16/s24/s32) but we interpret it as 'raw'.

Signed-off-by: Daniel Baluta daniel.baluta@nxp.com

Introduce two new API functions, that will copy audio streams data
to/from raw buffers.

This is useful for the new codec adapter where we need to interface
data copying between SOF modules and library buffers.

For example, in this new usecase data sent from an audio component to
a decoder component is compressed so we cannot interpret it with builtin
types (s16/s24/s32) but we interpret it as 'raw'.

Signed-off-by: Daniel Baluta <daniel.baluta@nxp.com>
Copy link
Collaborator

@paulstelian97 paulstelian97 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the function also check that there is data (for the from_ variant) or room (for the to_ variant) for the data, and limit the amount of data copied? Or do we assume the caller knows what they're doing and we can for example copy and wrap around multiple times?

@dbaluta
Copy link
Collaborator Author

dbaluta commented Sep 18, 2020

@paulstelian97 audio_stream functions internally deal with wrap around, so I assumed that it is ok for now.

Comment on lines +596 to +624
static inline void audio_stream_copy_to_buf(const struct audio_stream *source,
uint32_t ioffset_bytes,
void *snk, uint32_t bytes_snk,
uint32_t bytes)
{
void *src = audio_stream_wrap(source,
(char *)source->r_ptr + ioffset_bytes);
uint32_t bytes_src;
uint32_t bytes_copied;
int ret;

/* sink buf is linear, don't go over its limit */
if (bytes > bytes_snk)
bytes = bytes_snk;

while (bytes) {
bytes_src = audio_stream_bytes_without_wrap(source, src);
bytes_copied = MIN(bytes, MIN(bytes_src, bytes_snk));

ret = memcpy_s(snk, bytes_snk, src, bytes_copied);
assert(!ret);

bytes -= bytes_copied;
src = (char *)src + bytes_copied;
snk = (char *)snk + bytes_copied;

src = audio_stream_wrap(source, src);
}
}
Copy link
Member

@ktrzcinx ktrzcinx Sep 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need for while loop - only source is circular buffer, let's look for my solution:

static void audio_stream_copy_to_buf(const struct audio_stream *source, void *lib_buff, size_t bytes)
{
	uint32_t head_size = MIN(bytes, audio_stream_bytes_without_wrap(source, source->r_ptr));
	uint32_t tail_size = bytes - head_size;

	memcpy(lib_buff, source->r_ptr, head_size);
	if (tail_size)
		memcpy((uint8_t *)lib_buff + head_size, source->addr, tail_size);
}

@paulstelian97
Copy link
Collaborator

@paulstelian97 audio_stream functions internally deal with wrap around, so I assumed that it is ok for now.

With the wraparound they do (you won't overrun the buffer itself). But none of the functions you have used check the avail/free bytes. So you can read from an empty buffer/write in a full one. You won't get anything that Valgrind would complain about in the unit tests, that's true. But you can extract 1536 bytes from a 768 byte buffer (and you'd see the contents of that 768 byte buffer twice in the extracted data), for example.

@dbaluta
Copy link
Collaborator Author

dbaluta commented Sep 18, 2020

@mrajwa I'm bringing up this PR in order to discuss how we should copy the data from SOF component to library buffer.

If my understanding is correct, this code assumes:

static void codec_adapter_copy_from_lib_to_sink(void *source, struct audio_stream *sink,

that data copied is not compressed, while this is not true for most of the decoders.

Other issue, is that after processing is done library output buffer size might not be equal with input buffer size.

@lgirdwood lgirdwood added this to the v1.7 milestone Sep 18, 2020
@lgirdwood
Copy link
Member

@dbaluta any update here ?

@dbaluta
Copy link
Collaborator Author

dbaluta commented Oct 7, 2020

@lgirdwood this patch together with suggestions was picked up by @mrajwa in 97a9482

@mrajwa, as I suggested in your commit lets move these copy functions in audio_stream.h and use them in your PR. I can do that, you only need to use it in your PR.

@lgirdwood
Copy link
Member

@dbaluta ok, so does this mean we can close or are there other parts of this PR to merge ?

@dbaluta
Copy link
Collaborator Author

dbaluta commented Oct 7, 2020

@lgirdwood we can close this after @mrajwa takes care of my comments for updating 97a9482

Later edit: I just closed this and keep an eye on @mrajwa 's PR

@dbaluta dbaluta closed this Oct 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants