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 05/35] axfer: add support for a container of Sparc AU format
Date: Tue, 13 Nov 2018 15:41:17 +0900	[thread overview]
Message-ID: <20181113064147.13577-5-o-takashi@sakamocchi.jp> (raw)
In-Reply-To: <20181113064147.13577-1-o-takashi@sakamocchi.jp>

This commit adds support for data of Sparc AU format. In this data format,
values in each of field are encoded in big-endian byte order and available
formats of data sample are restricted in big-endian byte order.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
---
 axfer/Makefile.am    |   3 +-
 axfer/container-au.c | 203 +++++++++++++++++++++++++++++++++++++++++++
 axfer/container.c    |   6 +-
 axfer/container.h    |   4 +
 4 files changed, 214 insertions(+), 2 deletions(-)
 create mode 100644 axfer/container-au.c

diff --git a/axfer/Makefile.am b/axfer/Makefile.am
index 092c966..35aa226 100644
--- a/axfer/Makefile.am
+++ b/axfer/Makefile.am
@@ -25,4 +25,5 @@ axfer_SOURCES = \
 	subcmd-list.c \
 	container.h \
 	container.c \
-	container-riff-wave.c
+	container-riff-wave.c \
+	container-au.c
diff --git a/axfer/container-au.c b/axfer/container-au.c
new file mode 100644
index 00000000..6459b16
--- /dev/null
+++ b/axfer/container-au.c
@@ -0,0 +1,203 @@
+// SPDX-License-Identifier: GPL-2.0
+//
+// container-au.c - a parser/builder for a container of Sun Audio File.
+//
+// Copyright (c) 2018 Takashi Sakamoto <o-takashi@sakamocchi.jp>
+//
+// Licensed under the terms of the GNU General Public License, version 2.
+
+#include "container.h"
+#include "misc.h"
+
+// Not portable to all of UNIX platforms.
+#include <endian.h>
+
+// Reference:
+//  * http://pubs.opengroup.org/external/auformat.html
+
+#define AU_MAGIC	".snd"
+#define UNKNOWN_SIZE	UINT32_MAX
+
+enum code_id {
+	CODE_ID_CCIT_MU_LAW_BE		= 0x01,
+	CODE_ID_GENERIC_MBLA_S8		= 0x02,
+	CODE_ID_GENERIC_MBLA_S16_BE	= 0x03,
+	CODE_ID_GENERIC_MBLA_S32_BE	= 0x05,
+	CODE_ID_IEEE754_FLOAT_S32_BE	= 0x06,
+	CODE_ID_IEEE754_DOUBLE_S64_BE	= 0x07,
+	CODE_ID_CCIT_ADPCM_G721_4BIT_BE	= 0x17,
+	CODE_ID_CCIT_ADPCM_G723_3BIT_BE	= 0x19,
+	CODE_ID_CCIT_A_LAW_BE		= 0x1b,
+};
+
+struct format_map {
+	enum code_id code_id;
+	snd_pcm_format_t format;
+};
+
+static const struct format_map format_maps[] = {
+	{CODE_ID_GENERIC_MBLA_S8,		SND_PCM_FORMAT_S8},
+	{CODE_ID_GENERIC_MBLA_S16_BE,		SND_PCM_FORMAT_S16_BE},
+	{CODE_ID_GENERIC_MBLA_S32_BE,		SND_PCM_FORMAT_S32_BE},
+	{CODE_ID_IEEE754_FLOAT_S32_BE,		SND_PCM_FORMAT_FLOAT_BE},
+	{CODE_ID_IEEE754_DOUBLE_S64_BE,		SND_PCM_FORMAT_FLOAT64_BE},
+	// CODE_ID_CCIT_ADPCM_G721_4BIT_BE is not supported by ALSA.
+	// CODE_ID_CCIT_ADPCM_G723_3BIT_BE is not supported due to width of
+	// its sample.
+	{CODE_ID_CCIT_A_LAW_BE,			SND_PCM_FORMAT_A_LAW},
+	{CODE_ID_CCIT_MU_LAW_BE,		SND_PCM_FORMAT_MU_LAW},
+};
+
+struct container_header {
+	uint8_t magic[4];
+	uint32_t hdr_size;
+	uint32_t data_size;
+	uint32_t code_id;
+	uint32_t frames_per_second;
+	uint32_t samples_per_frame;
+};
+
+struct container_annotation {
+	uint32_t chunks[0];
+};
+
+struct parser_state {
+	enum code_id code_id;
+	unsigned int samples_per_frame;
+	unsigned int bytes_per_sample;
+};
+
+static int au_parser_pre_process(struct container_context *cntr,
+				 snd_pcm_format_t *format,
+				 unsigned int *samples_per_frame,
+				 unsigned int *frames_per_second,
+				 uint64_t *byte_count)
+{
+	struct parser_state *state = cntr->private_data;
+	struct container_header header;
+	enum code_id code_id;
+	int i;
+	int err;
+
+	// Parse header. 4 bytes are enough to detect supported containers.
+	memcpy(&header.magic, cntr->magic, sizeof(cntr->magic));
+	err = container_recursive_read(cntr,
+				       (char *)&header + sizeof(cntr->magic),
+				       sizeof(header) - sizeof(cntr->magic));
+	if (err < 0)
+		return err;
+	if (cntr->eof)
+		return 0;
+
+	if (memcmp(header.magic, AU_MAGIC, sizeof(header.magic)) != 0)
+		return -EINVAL;
+	if (be32toh(header.hdr_size) != sizeof(struct container_header))
+		return -EINVAL;
+
+	code_id = be32toh(header.code_id);
+	for (i = 0; i < ARRAY_SIZE(format_maps); ++i) {
+		if (format_maps[i].code_id == code_id)
+			break;
+	}
+	if (i == ARRAY_SIZE(format_maps))
+		return -EINVAL;
+	*format = format_maps[i].format;
+	*frames_per_second = be32toh(header.frames_per_second);
+	*samples_per_frame = be32toh(header.samples_per_frame);
+
+	state->code_id = code_id;
+	state->samples_per_frame = *samples_per_frame;
+	state->bytes_per_sample = snd_pcm_format_physical_width(*format) / 8;
+
+	*byte_count = be32toh(header.data_size);
+
+	return 0;
+}
+
+struct builder_state {
+	unsigned int bytes_per_sample;
+	unsigned int samples_per_frame;
+	unsigned int frames_per_second;
+	enum code_id code_id;
+};
+
+static void build_container_header(struct builder_state *state,
+				   struct container_header *header,
+				   unsigned int frames_per_second,
+				   uint64_t byte_count)
+{
+	memcpy(header->magic, AU_MAGIC, sizeof(header->magic));
+	header->hdr_size = htobe32(sizeof(struct container_header));
+	header->data_size = htobe32(byte_count);
+	header->code_id = htobe32(state->code_id);
+	header->frames_per_second = htobe32(frames_per_second);
+	header->samples_per_frame = htobe32(state->samples_per_frame);
+}
+
+static int write_container_header(struct container_context *cntr,
+				  uint64_t byte_count)
+{
+	struct builder_state *state = cntr->private_data;
+	struct container_header header;
+
+	build_container_header(state, &header, state->frames_per_second,
+			       byte_count);
+
+	return container_recursive_write(cntr, &header, sizeof(header));
+}
+
+static int au_builder_pre_process(struct container_context *cntr,
+				  snd_pcm_format_t *format,
+				  unsigned int *samples_per_frame,
+				  unsigned int *frames_per_second,
+				  uint64_t *byte_count)
+{
+	struct builder_state *status = cntr->private_data;
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(format_maps); ++i) {
+		if (format_maps[i].format == *format)
+			break;
+	}
+	if (i == ARRAY_SIZE(format_maps))
+		return -EINVAL;
+
+	status->code_id = format_maps[i].code_id;
+	status->bytes_per_sample = snd_pcm_format_physical_width(*format) / 8;
+	status->frames_per_second = *frames_per_second;
+	status->samples_per_frame = *samples_per_frame;
+
+	return write_container_header(cntr, *byte_count);
+}
+
+static int au_builder_post_process(struct container_context *cntr,
+				   uint64_t handled_byte_count)
+{
+	int err;
+
+	err = container_seek_offset(cntr, 0);
+	if (err < 0)
+		return err;
+
+	return write_container_header(cntr, handled_byte_count);
+}
+
+const struct container_parser container_parser_au = {
+	.format = CONTAINER_FORMAT_AU,
+	.magic = AU_MAGIC,
+	.max_size = UINT32_MAX,
+	.ops = {
+		.pre_process = au_parser_pre_process,
+	},
+	.private_size = sizeof(struct parser_state),
+};
+
+const struct container_builder container_builder_au = {
+	.format = CONTAINER_FORMAT_AU,
+	.max_size = UINT32_MAX,
+	.ops = {
+		.pre_process	= au_builder_pre_process,
+		.post_process	= au_builder_post_process,
+	},
+	.private_size = sizeof(struct builder_state),
+};
diff --git a/axfer/container.c b/axfer/container.c
index 04042da..0e25605 100644
--- a/axfer/container.c
+++ b/axfer/container.c
@@ -21,10 +21,12 @@ static const char *const cntr_type_labels[] = {
 
 static const char *const cntr_format_labels[] = {
 	[CONTAINER_FORMAT_RIFF_WAVE] = "riff/wave",
+	[CONTAINER_FORMAT_AU] = "au",
 };
 
 static const char *const suffixes[] = {
-	[CONTAINER_FORMAT_RIFF_WAVE] = ".wav",
+	[CONTAINER_FORMAT_RIFF_WAVE]	= ".wav",
+	[CONTAINER_FORMAT_AU]		= ".au",
 };
 
 
@@ -142,6 +144,7 @@ int container_parser_init(struct container_context *cntr,
 {
 	const struct container_parser *parsers[] = {
 		[CONTAINER_FORMAT_RIFF_WAVE] = &container_parser_riff_wave,
+		[CONTAINER_FORMAT_AU] = &container_parser_au,
 	};
 	const struct container_parser *parser;
 	unsigned int size;
@@ -216,6 +219,7 @@ int container_builder_init(struct container_context *cntr,
 {
 	const struct container_builder *builders[] = {
 		[CONTAINER_FORMAT_RIFF_WAVE] = &container_builder_riff_wave,
+		[CONTAINER_FORMAT_AU] = &container_builder_au,
 	};
 	const struct container_builder *builder;
 	int err;
diff --git a/axfer/container.h b/axfer/container.h
index 5808933..19ef672 100644
--- a/axfer/container.h
+++ b/axfer/container.h
@@ -26,6 +26,7 @@ enum container_type {
 
 enum container_format {
 	CONTAINER_FORMAT_RIFF_WAVE = 0,
+	CONTAINER_FORMAT_AU,
 	CONTAINER_FORMAT_COUNT,
 };
 
@@ -111,4 +112,7 @@ int container_seek_offset(struct container_context *cntr, off64_t offset);
 extern const struct container_parser container_parser_riff_wave;
 extern const struct container_builder container_builder_riff_wave;
 
+extern const struct container_parser container_parser_au;
+extern const struct container_builder container_builder_au;
+
 #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     ` Takashi Sakamoto [this message]
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     ` [PATCH 09/35] axfer: add a common interface to align data frames on different layout Takashi Sakamoto
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-5-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.