All of lore.kernel.org
 help / color / mirror / Atom feed
From: Takashi Sakamoto <o-takashi@sakamocchi.jp>
To: tiwai@suse.de, perex@perex.cz
Cc: alsa-devel@alsa-project.org
Subject: [PATCH 09/35] axfer: add a common interface to align data frames on different layout
Date: Tue, 13 Nov 2018 15:41:21 +0900	[thread overview]
Message-ID: <20181113064147.13577-9-o-takashi@sakamocchi.jp> (raw)
In-Reply-To: <20181113064147.13577-1-o-takashi@sakamocchi.jp>

In current aplay, several files can be handled as source of data frames for
playback, or destination of captured data frames by an option
'--separate-channels' (-I).

On the other hand, in ALSA PCM kernel/user interface, several types of
buffer are used to communicate between application/hardware;
 - mapped page frame for data frames with interleaved alignment
 - mapped page frame for data frames with non-interleaved alignment
 - buffer in user space for data frames with interleaved alignment
 - a list of buffer in user space for data frames with non-interleaved
   alignment

This commit adds a common interface, named as 'mapper' to convert frame
alignment between these two sides. This interface includes two types;
'muxer' and 'demuxer'. The 'muxer' is for playback direction, to
construct playback buffer with PCM frames from several files. The 'demuxer'
is for capture direction, to split PCM frames from capture buffer to
each of file. Unlike multimedia containers such as MPEG 2/4 Systems,
the 'muxer' and 'demuxer' are for playback/capture buffer, not for file
contents.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
---
 axfer/Makefile.am |   7 ++-
 axfer/mapper.c    | 125 ++++++++++++++++++++++++++++++++++++++++++++++
 axfer/mapper.h    |  79 +++++++++++++++++++++++++++++
 3 files changed, 209 insertions(+), 2 deletions(-)
 create mode 100644 axfer/mapper.c
 create mode 100644 axfer/mapper.h

diff --git a/axfer/Makefile.am b/axfer/Makefile.am
index 55fcf71..cb4b188 100644
--- a/axfer/Makefile.am
+++ b/axfer/Makefile.am
@@ -17,7 +17,8 @@ LDADD = \
 noinst_HEADERS = \
 	misc.h \
 	subcmd.h \
-	container.h
+	container.h \
+	mapper.h
 
 axfer_SOURCES = \
 	misc.h \
@@ -29,4 +30,6 @@ axfer_SOURCES = \
 	container-riff-wave.c \
 	container-au.c \
 	container-voc.c \
-	container-raw.c
+	container-raw.c \
+	mapper.h \
+	mapper.c
diff --git a/axfer/mapper.c b/axfer/mapper.c
new file mode 100644
index 00000000..eb63a0e
--- /dev/null
+++ b/axfer/mapper.c
@@ -0,0 +1,125 @@
+// SPDX-License-Identifier: GPL-2.0
+//
+// mapper.c - an interface of muxer/demuxer between buffer with data frames and
+// 	      formatted files.
+//
+// Copyright (c) 2018 Takashi Sakamoto <o-takashi@sakamocchi.jp>
+//
+// Licensed under the terms of the GNU General Public License, version 2.
+
+#include "mapper.h"
+#include "misc.h"
+
+#include <stdio.h>
+
+static const char *const mapper_type_labels[] = {
+	[MAPPER_TYPE_MUXER] = "muxer",
+	[MAPPER_TYPE_DEMUXER] = "demuxer",
+};
+
+static const char *const mapper_target_labels[] = {
+	[MAPPER_TARGET_COUNT] = "",
+};
+
+int mapper_context_init(struct mapper_context *mapper,
+			enum mapper_type type, unsigned int cntr_count,
+			unsigned int verbose)
+{
+	const struct mapper_data *data = NULL;
+
+	assert(mapper);
+	assert(cntr_count > 0);
+
+	// Detect forgotten to destruct.
+	assert(mapper->private_data == NULL);
+
+	memset(mapper, 0, sizeof(*mapper));
+
+	mapper->ops = &data->ops;
+	mapper->type = type;
+
+	mapper->private_data = malloc(data->private_size);
+	if (mapper->private_data == NULL)
+		return -ENOMEM;
+	memset(mapper->private_data, 0, data->private_size);
+
+	mapper->cntr_count = cntr_count;
+	mapper->verbose = verbose;
+
+	return 0;
+}
+
+int mapper_context_pre_process(struct mapper_context *mapper,
+			       snd_pcm_access_t access,
+			       unsigned int bytes_per_sample,
+			       unsigned int samples_per_frame,
+			       unsigned int frames_per_buffer,
+			       struct container_context *cntrs)
+{
+	int err;
+
+	assert(mapper);
+	assert(access >= SND_PCM_ACCESS_MMAP_INTERLEAVED);
+	assert(access <= SND_PCM_ACCESS_RW_NONINTERLEAVED);
+	assert(bytes_per_sample > 0);
+	assert(samples_per_frame > 0);
+	assert(cntrs);
+
+	mapper->access = access;
+	mapper->bytes_per_sample = bytes_per_sample;
+	mapper->samples_per_frame = samples_per_frame;
+	mapper->frames_per_buffer = frames_per_buffer;
+
+	err = mapper->ops->pre_process(mapper, cntrs, mapper->cntr_count);
+	if (err < 0)
+		return err;
+
+	if (mapper->verbose > 0) {
+		fprintf(stderr, "Mapper: %s\n",
+		       mapper_type_labels[mapper->type]);
+		fprintf(stderr, "  target: %s\n",
+		       mapper_target_labels[mapper->target]);
+		fprintf(stderr, "  access: %s\n",
+		       snd_pcm_access_name(mapper->access));
+		fprintf(stderr, "  bytes/sample: %u\n",
+			mapper->bytes_per_sample);
+		fprintf(stderr, "  samples/frame: %u\n",
+			mapper->samples_per_frame);
+		fprintf(stderr, "  frames/buffer: %lu\n",
+			mapper->frames_per_buffer);
+	}
+
+	return 0;
+}
+
+int mapper_context_process_frames(struct mapper_context *mapper,
+				  void *frame_buffer,
+				  unsigned int *frame_count,
+				  struct container_context *cntrs)
+{
+	assert(mapper);
+	assert(frame_buffer);
+	assert(frame_count);
+	assert(*frame_count <= mapper->frames_per_buffer);
+	assert(cntrs);
+
+	return mapper->ops->process_frames(mapper, frame_buffer, frame_count,
+					    cntrs, mapper->cntr_count);
+}
+
+void mapper_context_post_process(struct mapper_context *mapper)
+{
+	assert(mapper);
+
+	if (mapper->ops && mapper->ops->post_process)
+		mapper->ops->post_process(mapper);
+}
+
+void mapper_context_destroy(struct mapper_context *mapper)
+{
+	assert(mapper);
+
+	if (mapper->private_data)
+		free(mapper->private_data);
+	mapper->private_data = NULL;
+}
diff --git a/axfer/mapper.h b/axfer/mapper.h
new file mode 100644
index 00000000..c4caf09
--- /dev/null
+++ b/axfer/mapper.h
@@ -0,0 +1,79 @@
+// SPDX-License-Identifier: GPL-2.0
+//
+// mapper.h - an interface of muxer/demuxer between buffer with data frames and
+//	      formatted files.
+//
+// Copyright (c) 2018 Takashi Sakamoto <o-takashi@sakamocchi.jp>
+//
+// Licensed under the terms of the GNU General Public License, version 2.
+
+#ifndef __ALSA_UTILS_AXFER_MAPPER__H_
+#define __ALSA_UTILS_AXFER_MAPPER__H_
+
+#include "container.h"
+
+enum mapper_type {
+	MAPPER_TYPE_MUXER = 0,
+	MAPPER_TYPE_DEMUXER,
+	MAPPER_TYPE_COUNT,
+};
+
+enum mapper_target {
+	MAPPER_TARGET_COUNT,
+};
+
+struct mapper_ops;
+
+struct mapper_context {
+	enum mapper_type type;
+	enum mapper_target target;
+	const struct mapper_ops *ops;
+	unsigned int private_size;
+
+	void *private_data;
+	unsigned int cntr_count;
+
+	// A part of parameters of PCM substream.
+	snd_pcm_access_t access;
+	unsigned int bytes_per_sample;
+	unsigned int samples_per_frame;
+	snd_pcm_uframes_t frames_per_buffer;
+
+	unsigned int verbose;
+};
+
+int mapper_context_init(struct mapper_context *mapper,
+			enum mapper_type type, unsigned int cntr_count,
+			unsigned int verbose);
+int mapper_context_pre_process(struct mapper_context *mapper,
+			       snd_pcm_access_t access,
+			       unsigned int bytes_per_sample,
+			       unsigned int samples_per_frame,
+			       unsigned int frames_per_buffer,
+			       struct container_context *cntrs);
+int mapper_context_process_frames(struct mapper_context *mapper,
+				  void *frame_buffer,
+				  unsigned int *frame_count,
+				  struct container_context *cntrs);
+void mapper_context_post_process(struct mapper_context *mapper);
+void mapper_context_destroy(struct mapper_context *mapper);
+
+// For internal use in 'mapper' module.
+
+struct mapper_ops {
+	int (*pre_process)(struct mapper_context *mapper,
+			   struct container_context *cntrs,
+			   unsigned int cntr_count);
+	int (*process_frames)(struct mapper_context *mapper,
+			      void *frame_buffer, unsigned int *frame_count,
+			      struct container_context *cntrs,
+			      unsigned int cntr_count);
+	void (*post_process)(struct mapper_context *mapper);
+};
+
+struct mapper_data {
+	struct mapper_ops ops;
+	unsigned int private_size;
+};
+
+#endif
-- 
2.19.1

  parent reply	other threads:[~2018-11-13  6:42 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <1542090296222634077-webhooks-bot@alsa-project.org>
2018-11-13  6:24 ` alsa-utils: axfer: rewrite aplay, adding 'timer-based scheduling' option GitHub pull_request - opened
2018-11-13  6:41   ` [PATCH 01/35] axfer: add an entry point for this command Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 02/35] axfer: add a sub-command to print list of PCMs/devices Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 03/35] axfer: add a common interface to handle a file with audio-specific data format Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 04/35] axfer: add support for a container of Microsoft/IBM RIFF/Wave format Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 05/35] axfer: add support for a container of Sparc AU format Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 06/35] axfer: add support for a container of Creative Tech. voice format Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 07/35] axfer: add support for a container of raw data Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 08/35] axfer: add unit test for container interface Takashi Sakamoto
2018-11-13  6:41     ` Takashi Sakamoto [this message]
2018-11-13  6:41     ` [PATCH 10/35] axfer: add support for a mapper for single target Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 11/35] axfer: add support for a mapper for multiple target Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 12/35] axfer: add a unit test for mapper interface Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 13/35] axfer: add a common interface to transfer data frames Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 14/35] axfer: add a parser for command-line options Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 15/35] axfer: add support to transfer data frames by alsa-lib PCM APIs Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 16/35] axfer: add support for blocking data transmission operation of alsa-lib PCM API Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 17/35] axfer: add a sub-command to transfer data frames Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 18/35] axfer: add informative output and an option to suppress it Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 19/35] axfer: add an option to dump available hardware parameters Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 20/35] axfer: add options related to duration and obsolete '--max-file-size' option Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 21/35] axfer: add an option to finish transmission at XRUN Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 22/35] axfer: add support for non-blocking operation Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 23/35] axfer: add support for MMAP PCM operation Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 24/35] axfer: add an option to suppress event waiting Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 25/35] axfer: add options for buffer arrangement Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 26/35] axfer: add options for software parameters of PCM substream Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 27/35] axfer: add options for plugins in alsa-lib Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 28/35] axfer: add a common interface of waiter for I/O event notification Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 29/35] axfer: add an option for waiter type Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 30/35] axfer: add an implementation of waiter for poll(2) Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 31/35] axfer: add an implementation of waiter for select(2) Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 32/35] axfer: add an implementation of waiter for epoll(7) Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 33/35] axfer: add support for timer-based scheduling model with MMAP operation Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 34/35] axfer: obsolete some unimplemented options Takashi Sakamoto
2018-11-13  6:41     ` [PATCH 35/35] axfer: add support for libffado transmission backend Takashi Sakamoto
2018-11-13 11:27     ` [PATCH 01/35] axfer: add an entry point for this command Takashi Iwai
2018-11-14 16:05       ` Takashi Sakamoto
2018-11-14 16:23         ` Takashi Iwai

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181113064147.13577-9-o-takashi@sakamocchi.jp \
    --to=o-takashi@sakamocchi.jp \
    --cc=alsa-devel@alsa-project.org \
    --cc=perex@perex.cz \
    --cc=tiwai@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.