From f2dee23085e4b7deb0cb9deeef757eba76b22484 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Fri, 24 Jun 2022 11:09:01 +0200 Subject: [PATCH] buffer: add EMPTY chunk flag Add an EMPTY chunk flag to mark a piece of memory as 'empty'. For audio this means silence. Use the empty flag to avoid mixing 0 samples. Set the empty flag in output buffers on audioconvert. --- spa/include/spa/buffer/buffer.h | 3 +++ spa/plugins/audioconvert/audioconvert.c | 5 ++++- spa/plugins/audiomixer/audiomixer.c | 7 +++++-- spa/plugins/audiomixer/mixer-dsp.c | 8 ++++++-- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/spa/include/spa/buffer/buffer.h b/spa/include/spa/buffer/buffer.h index c4aa8a556..47204acbc 100644 --- a/spa/include/spa/buffer/buffer.h +++ b/spa/include/spa/buffer/buffer.h @@ -63,6 +63,9 @@ struct spa_chunk { int32_t stride; /**< stride of valid data */ #define SPA_CHUNK_FLAG_NONE 0 #define SPA_CHUNK_FLAG_CORRUPTED (1u<<0) /**< chunk data is corrupted in some way */ +#define SPA_CHUNK_FLAG_EMPTY (1u<<1) /**< chunk data is empty with media specific + * neutral data such as silence or black. This + * could be used to optimize processing. */ int32_t flags; /**< chunk flags */ }; diff --git a/spa/plugins/audioconvert/audioconvert.c b/spa/plugins/audioconvert/audioconvert.c index c976386e0..192d05872 100644 --- a/spa/plugins/audioconvert/audioconvert.c +++ b/spa/plugins/audioconvert/audioconvert.c @@ -2160,7 +2160,7 @@ static int impl_node_process(void *object) struct dir *dir; int tmp = 0, res = 0; bool in_passthrough, mix_passthrough, resample_passthrough, out_passthrough, end_passthrough; - bool in_avail = false, flush_in = false, flush_out = false, draining = false; + bool in_avail = false, flush_in = false, flush_out = false, draining = false, in_empty = true; struct spa_io_buffers *io, *ctrlio = NULL; const struct spa_pod_sequence *ctrl = NULL; @@ -2219,6 +2219,8 @@ static int impl_node_process(void *object) offs = SPA_MIN(bd->chunk->offset, bd->maxsize); size = SPA_MIN(bd->maxsize - offs, bd->chunk->size); + if (!SPA_FLAG_IS_SET(bd->chunk->flags, SPA_CHUNK_FLAG_EMPTY)) + in_empty = false; if (port->is_control) { spa_log_trace_fp(this->log, "%p: control %d", this, @@ -2487,6 +2489,7 @@ static int impl_node_process(void *object) for (j = 0; j < port->blocks; j++) { bd = &buf->buf->datas[j]; bd->chunk->size = this->out_offset * port->stride; + SPA_FLAG_UPDATE(bd->chunk->flags, SPA_CHUNK_FLAG_EMPTY, in_empty); spa_log_trace_fp(this->log, "out: %d %d %d", this->out_offset, port->stride, bd->chunk->size); } diff --git a/spa/plugins/audiomixer/audiomixer.c b/spa/plugins/audiomixer/audiomixer.c index 046264b19..09128eb73 100644 --- a/spa/plugins/audiomixer/audiomixer.c +++ b/spa/plugins/audiomixer/audiomixer.c @@ -782,8 +782,10 @@ static int impl_node_process(void *object) i, inio, outio, inio->status, inio->buffer_id, offs, size); - datas[n_buffers] = SPA_PTROFF(bd->data, offs, void); - buffers[n_buffers++] = inb; + if (!SPA_FLAG_IS_SET(bd->chunk->flags, SPA_CHUNK_FLAG_EMPTY)) { + datas[n_buffers] = SPA_PTROFF(bd->data, offs, void); + buffers[n_buffers++] = inb; + } inio->status = SPA_STATUS_NEED_DATA; } @@ -805,6 +807,7 @@ static int impl_node_process(void *object) d[0].chunk->offset = 0; d[0].chunk->size = maxsize; d[0].chunk->stride = this->stride; + SPA_FLAG_UPDATE(d[0].chunk->flags, SPA_CHUNK_FLAG_EMPTY, n_buffers == 0); mix_ops_process(&this->ops, d[0].data, datas, n_buffers, maxsize / this->stride); diff --git a/spa/plugins/audiomixer/mixer-dsp.c b/spa/plugins/audiomixer/mixer-dsp.c index 5ae0397b9..494d599a5 100644 --- a/spa/plugins/audiomixer/mixer-dsp.c +++ b/spa/plugins/audiomixer/mixer-dsp.c @@ -744,8 +744,10 @@ static int impl_node_process(void *object) i, inio, outio, inio->status, inio->buffer_id, offs, size); - datas[n_buffers] = SPA_PTROFF(bd->data, offs, void); - buffers[n_buffers++] = inb; + if (!SPA_FLAG_IS_SET(bd->chunk->flags, SPA_CHUNK_FLAG_EMPTY)) { + datas[n_buffers] = SPA_PTROFF(bd->data, offs, void); + buffers[n_buffers++] = inb; + } inio->status = SPA_STATUS_NEED_DATA; } @@ -759,6 +761,7 @@ static int impl_node_process(void *object) *outb->buffer = *buffers[0]->buffer; } else { struct spa_data *d = outb->buf.datas; + *outb->buffer = outb->buf; maxsize = SPA_MIN(maxsize, d[0].maxsize); @@ -766,6 +769,7 @@ static int impl_node_process(void *object) d[0].chunk->offset = 0; d[0].chunk->size = maxsize; d[0].chunk->stride = sizeof(float); + SPA_FLAG_UPDATE(d[0].chunk->flags, SPA_CHUNK_FLAG_EMPTY, n_buffers == 0); spa_log_trace_fp(this->log, "%p: %d mix %d", this, n_buffers, maxsize);