1 // SPDX-License-Identifier: GPL-2.0
3 // container-au.c - a parser/builder for a container of Sun Audio File.
5 // Copyright (c) 2018 Takashi Sakamoto <o-takashi@sakamocchi.jp>
7 // Licensed under the terms of the GNU General Public License, version 2.
12 // Not portable to all of UNIX platforms.
16 // * http://pubs.opengroup.org/external/auformat.html
18 #define AU_MAGIC ".snd"
19 #define UNKNOWN_SIZE UINT32_MAX
22 CODE_ID_CCIT_MU_LAW_BE = 0x01,
23 CODE_ID_GENERIC_MBLA_S8 = 0x02,
24 CODE_ID_GENERIC_MBLA_S16_BE = 0x03,
25 CODE_ID_GENERIC_MBLA_S32_BE = 0x05,
26 CODE_ID_IEEE754_FLOAT_S32_BE = 0x06,
27 CODE_ID_IEEE754_DOUBLE_S64_BE = 0x07,
28 CODE_ID_CCIT_ADPCM_G721_4BIT_BE = 0x17,
29 CODE_ID_CCIT_ADPCM_G723_3BIT_BE = 0x19,
30 CODE_ID_CCIT_A_LAW_BE = 0x1b,
35 snd_pcm_format_t format;
38 static const struct format_map format_maps[] = {
39 {CODE_ID_GENERIC_MBLA_S8, SND_PCM_FORMAT_S8},
40 {CODE_ID_GENERIC_MBLA_S16_BE, SND_PCM_FORMAT_S16_BE},
41 {CODE_ID_GENERIC_MBLA_S32_BE, SND_PCM_FORMAT_S32_BE},
42 {CODE_ID_IEEE754_FLOAT_S32_BE, SND_PCM_FORMAT_FLOAT_BE},
43 {CODE_ID_IEEE754_DOUBLE_S64_BE, SND_PCM_FORMAT_FLOAT64_BE},
44 // CODE_ID_CCIT_ADPCM_G721_4BIT_BE is not supported by ALSA.
45 // CODE_ID_CCIT_ADPCM_G723_3BIT_BE is not supported due to width of
47 {CODE_ID_CCIT_A_LAW_BE, SND_PCM_FORMAT_A_LAW},
48 {CODE_ID_CCIT_MU_LAW_BE, SND_PCM_FORMAT_MU_LAW},
51 struct container_header {
56 uint32_t frames_per_second;
57 uint32_t samples_per_frame;
60 struct container_annotation {
66 unsigned int samples_per_frame;
67 unsigned int bytes_per_sample;
70 static int au_parser_pre_process(struct container_context *cntr,
71 snd_pcm_format_t *format,
72 unsigned int *samples_per_frame,
73 unsigned int *frames_per_second,
76 struct parser_state *state = cntr->private_data;
77 struct container_header header;
82 // Parse header. 4 bytes are enough to detect supported containers.
83 memcpy(&header.magic, cntr->magic, sizeof(cntr->magic));
84 err = container_recursive_read(cntr,
85 (char *)&header + sizeof(cntr->magic),
86 sizeof(header) - sizeof(cntr->magic));
92 if (memcmp(header.magic, AU_MAGIC, sizeof(header.magic)) != 0)
94 if (be32toh(header.hdr_size) != sizeof(struct container_header))
97 code_id = be32toh(header.code_id);
98 for (i = 0; i < ARRAY_SIZE(format_maps); ++i) {
99 if (format_maps[i].code_id == code_id)
102 if (i == ARRAY_SIZE(format_maps))
104 *format = format_maps[i].format;
105 *frames_per_second = be32toh(header.frames_per_second);
106 *samples_per_frame = be32toh(header.samples_per_frame);
108 state->code_id = code_id;
109 state->samples_per_frame = *samples_per_frame;
110 state->bytes_per_sample = snd_pcm_format_physical_width(*format) / 8;
112 *byte_count = be32toh(header.data_size);
117 struct builder_state {
118 unsigned int bytes_per_sample;
119 unsigned int samples_per_frame;
120 unsigned int frames_per_second;
121 enum code_id code_id;
124 static void build_container_header(struct builder_state *state,
125 struct container_header *header,
126 unsigned int frames_per_second,
129 memcpy(header->magic, AU_MAGIC, sizeof(header->magic));
130 header->hdr_size = htobe32(sizeof(struct container_header));
131 header->data_size = htobe32(byte_count);
132 header->code_id = htobe32(state->code_id);
133 header->frames_per_second = htobe32(frames_per_second);
134 header->samples_per_frame = htobe32(state->samples_per_frame);
137 static int write_container_header(struct container_context *cntr,
140 struct builder_state *state = cntr->private_data;
141 struct container_header header;
143 build_container_header(state, &header, state->frames_per_second,
146 return container_recursive_write(cntr, &header, sizeof(header));
149 static int au_builder_pre_process(struct container_context *cntr,
150 snd_pcm_format_t *format,
151 unsigned int *samples_per_frame,
152 unsigned int *frames_per_second,
153 uint64_t *byte_count)
155 struct builder_state *status = cntr->private_data;
158 for (i = 0; i < ARRAY_SIZE(format_maps); ++i) {
159 if (format_maps[i].format == *format)
162 if (i == ARRAY_SIZE(format_maps))
165 status->code_id = format_maps[i].code_id;
166 status->bytes_per_sample = snd_pcm_format_physical_width(*format) / 8;
167 status->frames_per_second = *frames_per_second;
168 status->samples_per_frame = *samples_per_frame;
170 return write_container_header(cntr, *byte_count);
173 static int au_builder_post_process(struct container_context *cntr,
174 uint64_t handled_byte_count)
178 err = container_seek_offset(cntr, 0);
182 return write_container_header(cntr, handled_byte_count);
185 const struct container_parser container_parser_au = {
186 .format = CONTAINER_FORMAT_AU,
188 .max_size = UINT32_MAX,
190 .pre_process = au_parser_pre_process,
192 .private_size = sizeof(struct parser_state),
195 const struct container_builder container_builder_au = {
196 .format = CONTAINER_FORMAT_AU,
197 .max_size = UINT32_MAX,
199 .pre_process = au_builder_pre_process,
200 .post_process = au_builder_post_process,
202 .private_size = sizeof(struct builder_state),