1*cca8938dSgson /* $NetBSD: sun.c,v 1.11 2024/02/27 21:05:34 gson Exp $ */
237188d08Smrg
337188d08Smrg /*
495676db7Smrg * Copyright (c) 2002, 2013, 2015 Matthew R. Green
537188d08Smrg * All rights reserved.
637188d08Smrg *
737188d08Smrg * Redistribution and use in source and binary forms, with or without
837188d08Smrg * modification, are permitted provided that the following conditions
937188d08Smrg * are met:
1037188d08Smrg * 1. Redistributions of source code must retain the above copyright
1137188d08Smrg * notice, this list of conditions and the following disclaimer.
1237188d08Smrg * 2. Redistributions in binary form must reproduce the above copyright
1337188d08Smrg * notice, this list of conditions and the following disclaimer in the
1437188d08Smrg * documentation and/or other materials provided with the distribution.
1537188d08Smrg *
1637188d08Smrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1737188d08Smrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1837188d08Smrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1937188d08Smrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2037188d08Smrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2137188d08Smrg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2237188d08Smrg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2337188d08Smrg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2437188d08Smrg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2537188d08Smrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2637188d08Smrg * SUCH DAMAGE.
2737188d08Smrg */
2837188d08Smrg
2937188d08Smrg /*
3037188d08Smrg * XXX this is slightly icky in places...
3137188d08Smrg */
32c0db2196Sagc #include <sys/cdefs.h>
33c0db2196Sagc
34c0db2196Sagc #ifndef lint
35*cca8938dSgson __RCSID("$NetBSD: sun.c,v 1.11 2024/02/27 21:05:34 gson Exp $");
36c0db2196Sagc #endif
37c0db2196Sagc
3837188d08Smrg
3937188d08Smrg #include <sys/types.h>
4037188d08Smrg #include <sys/audioio.h>
4137188d08Smrg #include <sys/ioctl.h>
4237188d08Smrg #include <sys/time.h>
4337188d08Smrg
4437188d08Smrg #include <ctype.h>
4537188d08Smrg #include <err.h>
4637188d08Smrg #include <stdio.h>
4737188d08Smrg #include <stdlib.h>
4837188d08Smrg #include <string.h>
490c2e0646Smrg #include <unistd.h>
5037188d08Smrg
5137188d08Smrg #include "libaudio.h"
520c2e0646Smrg #include "auconv.h"
5337188d08Smrg
5437188d08Smrg /*
5537188d08Smrg * SunOS/NeXT .au format helpers
5637188d08Smrg */
57c36a7298Sjoerg static const struct {
5837188d08Smrg int file_encoding;
5937188d08Smrg int encoding;
6037188d08Smrg int precision;
6137188d08Smrg } file2sw_encodings[] = {
6237188d08Smrg { AUDIO_FILE_ENCODING_MULAW_8, AUDIO_ENCODING_ULAW, 8 },
639ab6411cSmrg { AUDIO_FILE_ENCODING_LINEAR_8, AUDIO_ENCODING_SLINEAR_BE, 8 },
649ab6411cSmrg { AUDIO_FILE_ENCODING_LINEAR_16, AUDIO_ENCODING_SLINEAR_BE, 16 },
659ab6411cSmrg { AUDIO_FILE_ENCODING_LINEAR_24, AUDIO_ENCODING_SLINEAR_BE, 24 },
669ab6411cSmrg { AUDIO_FILE_ENCODING_LINEAR_32, AUDIO_ENCODING_SLINEAR_BE, 32 },
6737188d08Smrg #if 0
6837188d08Smrg /*
6937188d08Smrg * we should make some of these available. the, eg ultrasparc, port
7037188d08Smrg * can use the VIS instructions (if available) do do some of these
7137188d08Smrg * mpeg ones.
7237188d08Smrg */
7337188d08Smrg { AUDIO_FILE_ENCODING_FLOAT, AUDIO_ENCODING_ULAW, 32 },
7437188d08Smrg { AUDIO_FILE_ENCODING_DOUBLE, AUDIO_ENCODING_ULAW, 64 },
7537188d08Smrg { AUDIO_FILE_ENCODING_ADPCM_G721, AUDIO_ENCODING_ULAW, 4 },
7637188d08Smrg { AUDIO_FILE_ENCODING_ADPCM_G722, AUDIO_ENCODING_ULAW, 0 },
7737188d08Smrg { AUDIO_FILE_ENCODING_ADPCM_G723_3, AUDIO_ENCODING_ULAW, 3 },
7837188d08Smrg { AUDIO_FILE_ENCODING_ADPCM_G723_5, AUDIO_ENCODING_ULAW, 5 },
7937188d08Smrg #endif
8037188d08Smrg { AUDIO_FILE_ENCODING_ALAW_8, AUDIO_ENCODING_ALAW, 8 },
81f3d3c4b6Schristos { -1, -1, -1 }
8237188d08Smrg };
8337188d08Smrg
8437188d08Smrg int
audio_sun_to_encoding(int sun_encoding,u_int * encp,u_int * precp)85c36a7298Sjoerg audio_sun_to_encoding(int sun_encoding, u_int *encp, u_int *precp)
8637188d08Smrg {
8737188d08Smrg int i;
8837188d08Smrg
8937188d08Smrg for (i = 0; file2sw_encodings[i].file_encoding != -1; i++)
9037188d08Smrg if (file2sw_encodings[i].file_encoding == sun_encoding) {
9137188d08Smrg *precp = file2sw_encodings[i].precision;
9237188d08Smrg *encp = file2sw_encodings[i].encoding;
9337188d08Smrg return (0);
9437188d08Smrg }
9537188d08Smrg return (1);
9637188d08Smrg }
9737188d08Smrg
9837188d08Smrg int
audio_encoding_to_sun(int encoding,int precision,int * sunep)99c36a7298Sjoerg audio_encoding_to_sun(int encoding, int precision, int *sunep)
10037188d08Smrg {
10137188d08Smrg int i;
10237188d08Smrg
10337188d08Smrg for (i = 0; file2sw_encodings[i].file_encoding != -1; i++)
10437188d08Smrg if (file2sw_encodings[i].encoding == encoding &&
10537188d08Smrg file2sw_encodings[i].precision == precision) {
10637188d08Smrg *sunep = file2sw_encodings[i].file_encoding;
10737188d08Smrg return (0);
10837188d08Smrg }
10937188d08Smrg return (1);
11037188d08Smrg }
1110c2e0646Smrg
1120c2e0646Smrg int
sun_prepare_header(struct track_info * ti,void ** hdrp,size_t * lenp,int * leftp)113a0e3e391Smrg sun_prepare_header(struct track_info *ti, void **hdrp, size_t *lenp, int *leftp)
1140c2e0646Smrg {
1150c2e0646Smrg static int warned = 0;
1160c2e0646Smrg static sun_audioheader auh;
117a0e3e391Smrg int sunenc, oencoding = ti->encoding;
1180c2e0646Smrg
1190c2e0646Smrg /* only perform conversions if we don't specify the encoding */
120a0e3e391Smrg switch (ti->encoding) {
1210c2e0646Smrg
1220c2e0646Smrg case AUDIO_ENCODING_ULINEAR_LE:
1230c2e0646Smrg #if BYTE_ORDER == LITTLE_ENDIAN
1240c2e0646Smrg case AUDIO_ENCODING_ULINEAR:
1250c2e0646Smrg #endif
126a0e3e391Smrg if (ti->precision == 16 || ti->precision == 32)
127a0e3e391Smrg ti->encoding = AUDIO_ENCODING_SLINEAR_BE;
1280c2e0646Smrg break;
1290c2e0646Smrg
1300c2e0646Smrg case AUDIO_ENCODING_ULINEAR_BE:
1310c2e0646Smrg #if BYTE_ORDER == BIG_ENDIAN
1320c2e0646Smrg case AUDIO_ENCODING_ULINEAR:
1330c2e0646Smrg #endif
134a0e3e391Smrg if (ti->precision == 16 || ti->precision == 32)
135a0e3e391Smrg ti->encoding = AUDIO_ENCODING_SLINEAR_BE;
1360c2e0646Smrg break;
1370c2e0646Smrg
1380c2e0646Smrg case AUDIO_ENCODING_SLINEAR_LE:
1390c2e0646Smrg #if BYTE_ORDER == LITTLE_ENDIAN
1400c2e0646Smrg case AUDIO_ENCODING_SLINEAR:
1410c2e0646Smrg #endif
142a0e3e391Smrg if (ti->precision == 16 || ti->precision == 32)
143a0e3e391Smrg ti->encoding = AUDIO_ENCODING_SLINEAR_BE;
1440c2e0646Smrg break;
1450c2e0646Smrg
1460c2e0646Smrg #if BYTE_ORDER == BIG_ENDIAN
1470c2e0646Smrg case AUDIO_ENCODING_SLINEAR:
148a0e3e391Smrg ti->encoding = AUDIO_ENCODING_SLINEAR_BE;
1490c2e0646Smrg break;
1500c2e0646Smrg #endif
1510c2e0646Smrg }
1520c2e0646Smrg
1530c2e0646Smrg /* if we can't express this as a Sun header, don't write any */
154a0e3e391Smrg if (audio_encoding_to_sun(ti->encoding, ti->precision, &sunenc) != 0) {
155a0e3e391Smrg if (!ti->qflag && !warned) {
1560c2e0646Smrg const char *s = audio_enc_from_val(oencoding);
1570c2e0646Smrg
1580c2e0646Smrg if (s == NULL)
1590c2e0646Smrg s = "(unknown)";
1600c2e0646Smrg warnx("failed to convert to sun encoding from %s "
1610c2e0646Smrg "(precision %d);\nSun audio header not written",
162a0e3e391Smrg s, ti->precision);
1630c2e0646Smrg }
164a0e3e391Smrg ti->format = AUDIO_FORMAT_NONE;
1650c2e0646Smrg warned = 1;
1660c2e0646Smrg return -1;
1670c2e0646Smrg }
1680c2e0646Smrg
1690c2e0646Smrg auh.magic = htonl(AUDIO_FILE_MAGIC);
170a0e3e391Smrg if (ti->outfd == STDOUT_FILENO)
1710c2e0646Smrg auh.data_size = htonl(AUDIO_UNKNOWN_SIZE);
172a0e3e391Smrg else if (ti->total_size != -1)
173a0e3e391Smrg auh.data_size = htonl(ti->total_size);
1740c2e0646Smrg else
1750c2e0646Smrg auh.data_size = 0;
1760c2e0646Smrg auh.encoding = htonl(sunenc);
177a0e3e391Smrg auh.sample_rate = htonl(ti->sample_rate);
178a0e3e391Smrg auh.channels = htonl(ti->channels);
179a0e3e391Smrg if (ti->header_info) {
1800c2e0646Smrg int len, infolen;
1810c2e0646Smrg
182a0e3e391Smrg infolen = ((len = strlen(ti->header_info)) + 7) & 0xfffffff8;
1830c2e0646Smrg *leftp = infolen - len;
1840c2e0646Smrg auh.hdr_size = htonl(sizeof(auh) + infolen);
1850c2e0646Smrg } else {
1860c2e0646Smrg *leftp = sizeof(audio_default_info);
1870c2e0646Smrg auh.hdr_size = htonl(sizeof(auh) + *leftp);
1880c2e0646Smrg }
1890c2e0646Smrg *(sun_audioheader **)hdrp = &auh;
1900c2e0646Smrg *lenp = sizeof auh;
1910c2e0646Smrg return 0;
1920c2e0646Smrg }
1930c2e0646Smrg
1940c2e0646Smrg write_conv_func
sun_write_get_conv_func(struct track_info * ti)195a0e3e391Smrg sun_write_get_conv_func(struct track_info *ti)
1960c2e0646Smrg {
1970c2e0646Smrg write_conv_func conv_func = NULL;
1980c2e0646Smrg
1990c2e0646Smrg /* only perform conversions if we don't specify the encoding */
200a0e3e391Smrg switch (ti->encoding) {
2010c2e0646Smrg
2020c2e0646Smrg case AUDIO_ENCODING_ULINEAR_LE:
2030c2e0646Smrg #if BYTE_ORDER == LITTLE_ENDIAN
2040c2e0646Smrg case AUDIO_ENCODING_ULINEAR:
2050c2e0646Smrg #endif
206a0e3e391Smrg if (ti->precision == 16)
2070c2e0646Smrg conv_func = change_sign16_swap_bytes_le;
208a0e3e391Smrg else if (ti->precision == 32)
2090c2e0646Smrg conv_func = change_sign32_swap_bytes_le;
2100c2e0646Smrg break;
2110c2e0646Smrg
2120c2e0646Smrg case AUDIO_ENCODING_ULINEAR_BE:
2130c2e0646Smrg #if BYTE_ORDER == BIG_ENDIAN
2140c2e0646Smrg case AUDIO_ENCODING_ULINEAR:
2150c2e0646Smrg #endif
216a0e3e391Smrg if (ti->precision == 16)
2170c2e0646Smrg conv_func = change_sign16_be;
218a0e3e391Smrg else if (ti->precision == 32)
2190c2e0646Smrg conv_func = change_sign32_be;
2200c2e0646Smrg break;
2210c2e0646Smrg
2220c2e0646Smrg case AUDIO_ENCODING_SLINEAR_LE:
2230c2e0646Smrg #if BYTE_ORDER == LITTLE_ENDIAN
2240c2e0646Smrg case AUDIO_ENCODING_SLINEAR:
2250c2e0646Smrg #endif
226a0e3e391Smrg if (ti->precision == 16)
2270c2e0646Smrg conv_func = swap_bytes;
228a0e3e391Smrg else if (ti->precision == 32)
2290c2e0646Smrg conv_func = swap_bytes32;
2300c2e0646Smrg break;
2310c2e0646Smrg }
2320c2e0646Smrg
2330c2e0646Smrg return conv_func;
2340c2e0646Smrg }
235