stream: more work on converter

This commit is contained in:
Wim Taymans 2018-04-06 18:39:07 +02:00
parent 19067dde17
commit 142ef38df9
10 changed files with 2746 additions and 212 deletions

View file

@ -69,8 +69,9 @@ struct port {
bool have_format;
struct spa_audio_info format;
int stride;
int blocks;
uint32_t stride;
uint32_t blocks;
uint32_t size;
struct buffer buffers[MAX_BUFFERS];
uint32_t n_buffers;
@ -120,81 +121,6 @@ static inline void init_type(struct type *type, struct spa_type_map *map)
#include "fmt-ops.c"
typedef void (*convert_func_t) (void *data, int n_dst, void *dst[n_dst],
int n_src, const void *src[n_src], int n_bytes);
static const struct conv_info {
off_t src_fmt;
off_t dst_fmt;
convert_func_t i2i;
convert_func_t i2d;
convert_func_t d2i;
} conv_table[] =
{
/* to f32 */
{ offsetof(struct spa_type_audio_format, U8),
offsetof(struct spa_type_audio_format, F32),
conv_u8_to_f32, conv_u8_to_f32d, conv_u8d_to_f32 },
{ offsetof(struct spa_type_audio_format, S16),
offsetof(struct spa_type_audio_format, F32),
conv_s16_to_f32, conv_s16_to_f32d, conv_s16d_to_f32 },
{ offsetof(struct spa_type_audio_format, F32),
offsetof(struct spa_type_audio_format, F32),
conv_copy, deinterleave_32, interleave_32 },
{ offsetof(struct spa_type_audio_format, S32),
offsetof(struct spa_type_audio_format, F32),
conv_s32_to_f32, conv_s32_to_f32d, conv_s32d_to_f32 },
{ offsetof(struct spa_type_audio_format, S24),
offsetof(struct spa_type_audio_format, F32),
conv_s24_to_f32, conv_s24_to_f32d, conv_s24d_to_f32 },
{ offsetof(struct spa_type_audio_format, S24_32),
offsetof(struct spa_type_audio_format, F32),
conv_s24_32_to_f32, conv_s24_32_to_f32d, conv_s24_32d_to_f32 },
/* from f32 */
{ offsetof(struct spa_type_audio_format, F32),
offsetof(struct spa_type_audio_format, U8),
conv_f32_to_u8, conv_f32_to_u8d, conv_f32d_to_u8 },
{ offsetof(struct spa_type_audio_format, F32),
offsetof(struct spa_type_audio_format, S16),
conv_f32_to_s16, conv_f32_to_s16d, conv_f32d_to_s16 },
{ offsetof(struct spa_type_audio_format, F32),
offsetof(struct spa_type_audio_format, S32),
conv_f32_to_s32, conv_f32_to_s32d, conv_f32d_to_s32 },
{ offsetof(struct spa_type_audio_format, F32),
offsetof(struct spa_type_audio_format, S24),
conv_f32_to_s24, conv_f32_to_s24d, conv_f32d_to_s24 },
{ offsetof(struct spa_type_audio_format, F32),
offsetof(struct spa_type_audio_format, S24_32),
conv_f32_to_s24_32, conv_f32_to_s24_32d, conv_f32d_to_s24_32 },
/* u8 */
{ offsetof(struct spa_type_audio_format, U8),
offsetof(struct spa_type_audio_format, U8),
conv_copy, deinterleave_8, interleave_8 },
/* s16 */
{ offsetof(struct spa_type_audio_format, S16),
offsetof(struct spa_type_audio_format, S16),
conv_copy, deinterleave_16, interleave_16 },
/* s32 */
{ offsetof(struct spa_type_audio_format, S32),
offsetof(struct spa_type_audio_format, S32),
conv_copy, deinterleave_32, interleave_32 },
/* s24 */
{ offsetof(struct spa_type_audio_format, S24),
offsetof(struct spa_type_audio_format, S24),
conv_copy, deinterleave_24, interleave_24 },
/* s24_32 */
{ offsetof(struct spa_type_audio_format, S24_32),
offsetof(struct spa_type_audio_format, S24_32),
conv_copy, deinterleave_32, interleave_32 },
};
struct impl {
struct spa_handle handle;
struct spa_node node;
@ -227,19 +153,6 @@ struct impl {
#define GET_OUT_PORT(this,id) (&this->out_port)
#define GET_PORT(this,d,id) (d == SPA_DIRECTION_INPUT ? GET_IN_PORT(this,id) : GET_OUT_PORT(this,id))
static const struct conv_info *find_conv_info(struct impl *this, uint32_t src_fmt, uint32_t dst_fmt)
{
struct type *t = &this->type;
int i;
for (i = 0; i < SPA_N_ELEMENTS(conv_table); i++) {
if (*SPA_MEMBER(&t->audio_format, conv_table[i].src_fmt, uint32_t) == src_fmt &&
*SPA_MEMBER(&t->audio_format, conv_table[i].dst_fmt, uint32_t) == dst_fmt)
return &conv_table[i];
}
return NULL;
}
static void convert_generic (void *data, int n_dst, void *dst[n_dst],
int n_src, const void *src[n_src], int n_bytes)
{
@ -285,7 +198,7 @@ static int setup_convert(struct impl *this)
return -EINVAL;
/* find fast path */
this->conv[0] = find_conv_info(this, src_fmt, dst_fmt);
this->conv[0] = find_conv_info(&t->audio_format, src_fmt, dst_fmt);
if (this->conv[0] != NULL) {
if (inport->format.info.raw.layout == SPA_AUDIO_LAYOUT_INTERLEAVED) {
if (outport->format.info.raw.layout == SPA_AUDIO_LAYOUT_INTERLEAVED)
@ -303,8 +216,8 @@ static int setup_convert(struct impl *this)
}
/* go through intermediate format */
this->conv[0] = find_conv_info(this, src_fmt, t->audio_format.F32);
this->conv[1] = find_conv_info(this, t->audio_format.F32, dst_fmt);
this->conv[0] = find_conv_info(&t->audio_format, src_fmt, t->audio_format.F32);
this->conv[1] = find_conv_info(&t->audio_format, t->audio_format.F32, dst_fmt);
if (this->conv[0] == NULL || this->conv[1] == NULL)
return -ENOTSUP;
@ -454,7 +367,7 @@ static int port_enum_formats(struct spa_node *node,
t->param.idEnumFormat, t->format,
"I", t->media_type.audio,
"I", t->media_subtype.raw,
":", t->format_audio.format, "Ieu", t->audio_format.S16,
":", t->format_audio.format, "Ieu", other->format.info.raw.format,
SPA_POD_PROP_ENUM(11, t->audio_format.U8,
t->audio_format.S16,
t->audio_format.S16_OE,
@ -466,7 +379,7 @@ static int port_enum_formats(struct spa_node *node,
t->audio_format.S24_OE,
t->audio_format.S24_32,
t->audio_format.S24_32_OE),
":", t->format_audio.layout, "ieu", SPA_AUDIO_LAYOUT_INTERLEAVED,
":", t->format_audio.layout, "ieu", other->format.info.raw.layout,
SPA_POD_PROP_ENUM(2, SPA_AUDIO_LAYOUT_INTERLEAVED,
SPA_AUDIO_LAYOUT_NON_INTERLEAVED),
":", t->format_audio.rate, "i", other->format.info.raw.rate,
@ -540,7 +453,8 @@ impl_node_port_enum_params(struct spa_node *node,
{
struct impl *this;
struct type *t;
struct port *port;
struct port *port, *other;
struct spa_pod *param;
struct spa_pod_builder b = { 0 };
uint8_t buffer[1024];
@ -556,6 +470,7 @@ impl_node_port_enum_params(struct spa_node *node,
spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL);
port = GET_PORT(this, direction, port_id);
other = GET_PORT(this, SPA_DIRECTION_REVERSE(direction), port_id);
next:
spa_pod_builder_init(&b, buffer, sizeof(buffer));
@ -587,15 +502,28 @@ impl_node_port_enum_params(struct spa_node *node,
if (*index > 0)
return 0;
param = spa_pod_builder_object(&b,
id, t->param_buffers.Buffers,
":", t->param_buffers.buffers, "iru", 1,
SPA_POD_PROP_MIN_MAX(1, MAX_BUFFERS),
":", t->param_buffers.blocks, "i", port->blocks,
":", t->param_buffers.size, "iru", 1024 * port->stride,
SPA_POD_PROP_MIN_MAX(16 * port->stride, INT32_MAX / port->stride),
":", t->param_buffers.stride, "i", port->stride,
":", t->param_buffers.align, "i", 16);
if (other->n_buffers > 0) {
param = spa_pod_builder_object(&b,
id, t->param_buffers.Buffers,
":", t->param_buffers.buffers, "iru", other->n_buffers,
SPA_POD_PROP_MIN_MAX(1, MAX_BUFFERS),
":", t->param_buffers.blocks, "i", port->blocks,
":", t->param_buffers.size, "i", (other->size / other->stride) *
port->stride,
":", t->param_buffers.stride, "i", port->stride,
":", t->param_buffers.align, "i", 16);
}
else {
param = spa_pod_builder_object(&b,
id, t->param_buffers.Buffers,
":", t->param_buffers.buffers, "iru", 1,
SPA_POD_PROP_MIN_MAX(1, MAX_BUFFERS),
":", t->param_buffers.blocks, "i", port->blocks,
":", t->param_buffers.size, "iru", 1024 * port->stride,
SPA_POD_PROP_MIN_MAX(16 * port->stride, INT32_MAX / port->stride),
":", t->param_buffers.stride, "i", port->stride,
":", t->param_buffers.align, "i", 16);
}
}
else if (id == t->param.idMeta) {
if (!port->have_format)
@ -746,7 +674,7 @@ impl_node_port_use_buffers(struct spa_node *node,
{
struct impl *this;
struct port *port;
uint32_t i;
uint32_t i, size = SPA_ID_INVALID;
struct type *t;
spa_return_val_if_fail(node != NULL, -EINVAL);
@ -773,6 +701,12 @@ impl_node_port_use_buffers(struct spa_node *node,
b->outbuf = buffers[i];
b->h = spa_buffer_find_meta(buffers[i], t->meta.Header);
if (size == SPA_ID_INVALID)
size = d[0].maxsize;
else
if (size != d[0].maxsize)
return -EINVAL;
if (!((d[0].type == t->data.MemPtr ||
d[0].type == t->data.MemFd ||
d[0].type == t->data.DmaBuf) && d[0].data != NULL)) {
@ -786,6 +720,7 @@ impl_node_port_use_buffers(struct spa_node *node,
SPA_FLAG_SET(b->flags, BUFFER_FLAG_OUT);
}
port->n_buffers = n_buffers;
port->size = size;
return 0;
}
@ -937,7 +872,8 @@ static int impl_node_process(struct spa_node *node)
this->convert(this, n_dst_datas, dst_datas, n_src_datas, src_datas, n_bytes);
dbuf->outbuf->datas[0].chunk->size = n_bytes / 2;
for (i = 0; i < n_dst_datas; i++)
dbuf->outbuf->datas[i].chunk->size = (n_bytes / inport->stride) * outport->stride;
}
outio->status = SPA_STATUS_HAVE_BUFFER;
@ -1062,7 +998,7 @@ impl_enum_interface_info(const struct spa_handle_factory *factory,
return 1;
}
const struct spa_handle_factory spa_audioconvert_factory = {
const struct spa_handle_factory spa_fmtconvert_factory = {
SPA_VERSION_HANDLE_FACTORY,
NAME,
NULL,