1 /*
2  * SDL_sound -- An abstract sound format decoding API.
3  * Copyright (C) 2001  Ryan C. Gordon.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19 
20 /*
21  * Shorten decoder for SDL_sound.
22  *
23  * This driver handles Shorten-compressed waveforms. Despite the fact that
24  *  SHNs tend to be much bigger than MP3s, they are still the de facto
25  *  standard in online music trading communities. If an MP3 crunches the
26  *  waveform to 10-20 percent of its original size, SHNs only go to about
27  *  50-60%. Why do the Phish fans of the world use this format then? Rabid
28  *  music traders appreciate the sound quality; SHNs, unlike MP3s, do not
29  *  throw away any part of the waveform. Yes, there are people that notice
30  *  this, and further more, they demand it...and if they can't get a good
31  *  transfer of those larger files over the 'net, they haven't underestimated
32  *  the bandwidth of CDs travelling the world through the postal system.
33  *
34  * Shorten homepage: http://www.softsound.com/Shorten.html
35  *
36  * The Shorten format was gleaned from the shorten codebase, by Tony
37  *  Robinson and SoftSound Limited.
38  *
39  * Please see the file COPYING in the source's root directory.
40  *
41  *  This file written by Ryan C. Gordon. (icculus@icculus.org)
42  */
43 
44 #if HAVE_CONFIG_H
45 #  include <config.h>
46 #endif
47 
48 #ifdef SOUND_SUPPORTS_SHN
49 
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <math.h>
54 
55 #include "SDL_sound.h"
56 
57 #define __SDL_SOUND_INTERNAL__
58 #include "SDL_sound_internal.h"
59 
60 static int SHN_init(void);
61 static void SHN_quit(void);
62 static int SHN_open(Sound_Sample *sample, const char *ext);
63 static void SHN_close(Sound_Sample *sample);
64 static Uint32 SHN_read(Sound_Sample *sample);
65 static int SHN_rewind(Sound_Sample *sample);
66 static int SHN_seek(Sound_Sample *sample, Uint32 ms);
67 
68 static const char *extensions_shn[] = { "SHN", NULL };
69 const Sound_DecoderFunctions __Sound_DecoderFunctions_SHN =
70 {
71     {
72         extensions_shn,
73         "Shorten-compressed audio data",
74         "Ryan C. Gordon <icculus@icculus.org>",
75         "http://www.icculus.org/SDL_sound/"
76     },
77 
78     SHN_init,       /*   init() method */
79     SHN_quit,       /*   quit() method */
80     SHN_open,       /*   open() method */
81     SHN_close,      /*  close() method */
82     SHN_read,       /*   read() method */
83     SHN_rewind,     /* rewind() method */
84     SHN_seek        /*   seek() method */
85 };
86 
87 
88 #define SHN_BUFSIZ  512
89 
90 typedef struct
91 {
92     Sint32 version;
93     Sint32 datatype;
94     Sint32 nchan;
95     Sint32 blocksize;
96     Sint32 maxnlpc;
97     Sint32 nmean;
98     Sint32 nwrap;
99     Sint32 **buffer;
100     Sint32 **offset;
101     Sint32 *qlpc;
102     Sint32 lpcqoffset;
103     Sint32 bitshift;
104     int nbitget;
105     int nbyteget;
106     Uint8 *getbuf;
107     Uint8 *getbufp;
108     Uint32 gbuffer;
109     Uint8 *backBuffer;
110     Uint32 backBufferSize;
111     Uint32 backBufLeft;
112     Uint32 start_pos;
113 } shn_t;
114 
115 
116 static const Uint32 mask_table[] =
117 {
118     0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000F, 0x0000001F,
119     0x0000003F, 0x0000007F, 0x000000FF, 0x000001FF, 0x000003FF, 0x000007FF,
120     0x00000FFF, 0x00001FFF, 0x00003FFF, 0x00007FFF, 0x0000FFFF, 0x0001FFFF,
121     0x0003FFFF, 0x0007FFFF, 0x000FFFFF, 0x001FFFFF, 0x003FFFFF, 0x007FFFFF,
122     0x00FFFFFF, 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF, 0x1FFFFFFF,
123     0x3FFFFFFF, 0x7FFFFFFF, 0xFFFFFFFF
124 };
125 
126 
127 static const Uint8 ulaw_outward[13][256] = {
128 {127,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,255,254,253,252,251,250,249,248,247,246,245,244,243,242,241,240,239,238,237,236,235,234,233,232,231,230,229,228,227,226,225,224,223,222,221,220,219,218,217,216,215,214,213,212,211,210,209,208,207,206,205,204,203,202,201,200,199,198,197,196,195,194,193,192,191,190,189,188,187,186,185,184,183,182,181,180,179,178,177,176,175,174,173,172,171,170,169,168,167,166,165,164,163,162,161,160,159,158,157,156,155,154,153,152,151,150,149,148,147,146,145,144,143,142,141,140,139,138,137,136,135,134,133,132,131,130,129,128},
129 {112,114,116,118,120,122,124,126,127,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,113,115,117,119,121,123,125,255,253,251,249,247,245,243,241,239,238,237,236,235,234,233,232,231,230,229,228,227,226,225,224,223,222,221,220,219,218,217,216,215,214,213,212,211,210,209,208,207,206,205,204,203,202,201,200,199,198,197,196,195,194,193,192,191,190,189,188,187,186,185,184,183,182,181,180,179,178,177,176,175,174,173,172,171,170,169,168,167,166,165,164,163,162,161,160,159,158,157,156,155,154,153,152,151,150,149,148,147,146,145,144,143,142,141,140,139,138,137,136,135,134,133,132,131,130,129,128,254,252,250,248,246,244,242,240},
130 {96,98,100,102,104,106,108,110,112,113,114,116,117,118,120,121,122,124,125,126,127,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,97,99,101,103,105,107,109,111,115,119,123,255,251,247,243,239,237,235,233,231,229,227,225,223,222,221,220,219,218,217,216,215,214,213,212,211,210,209,208,207,206,205,204,203,202,201,200,199,198,197,196,195,194,193,192,191,190,189,188,187,186,185,184,183,182,181,180,179,178,177,176,175,174,173,172,171,170,169,168,167,166,165,164,163,162,161,160,159,158,157,156,155,154,153,152,151,150,149,148,147,146,145,144,143,142,141,140,139,138,137,136,135,134,133,132,131,130,129,128,254,253,252,250,249,248,246,245,244,242,241,240,238,236,234,232,230,228,226,224},
131 {80,82,84,86,88,90,92,94,96,97,98,100,101,102,104,105,106,108,109,110,112,113,114,115,116,117,118,120,121,122,123,124,125,126,127,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,81,83,85,87,89,91,93,95,99,103,107,111,119,255,247,239,235,231,227,223,221,219,217,215,213,211,209,207,206,205,204,203,202,201,200,199,198,197,196,195,194,193,192,191,190,189,188,187,186,185,184,183,182,181,180,179,178,177,176,175,174,173,172,171,170,169,168,167,166,165,164,163,162,161,160,159,158,157,156,155,154,153,152,151,150,149,148,147,146,145,144,143,142,141,140,139,138,137,136,135,134,133,132,131,130,129,128,254,253,252,251,250,249,248,246,245,244,243,242,241,240,238,237,236,234,233,232,230,229,228,226,225,224,222,220,218,216,214,212,210,208},
132 {64,66,68,70,72,74,76,78,80,81,82,84,85,86,88,89,90,92,93,94,96,97,98,99,100,101,102,104,105,106,107,108,109,110,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,65,67,69,71,73,75,77,79,83,87,91,95,103,111,255,239,231,223,219,215,211,207,205,203,201,199,197,195,193,191,190,189,188,187,186,185,184,183,182,181,180,179,178,177,176,175,174,173,172,171,170,169,168,167,166,165,164,163,162,161,160,159,158,157,156,155,154,153,152,151,150,149,148,147,146,145,144,143,142,141,140,139,138,137,136,135,134,133,132,131,130,129,128,254,253,252,251,250,249,248,247,246,245,244,243,242,241,240,238,237,236,235,234,233,232,230,229,228,227,226,225,224,222,221,220,218,217,216,214,213,212,210,209,208,206,204,202,200,198,196,194,192},
133 {49,51,53,55,57,59,61,63,64,66,67,68,70,71,72,74,75,76,78,79,80,81,82,84,85,86,87,88,89,90,92,93,94,95,96,97,98,99,100,101,102,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,50,52,54,56,58,60,62,65,69,73,77,83,91,103,255,231,219,211,205,201,197,193,190,188,186,184,182,180,178,176,175,174,173,172,171,170,169,168,167,166,165,164,163,162,161,160,159,158,157,156,155,154,153,152,151,150,149,148,147,146,145,144,143,142,141,140,139,138,137,136,135,134,133,132,131,130,129,128,254,253,252,251,250,249,248,247,246,245,244,243,242,241,240,239,238,237,236,235,234,233,232,230,229,228,227,226,225,224,223,222,221,220,218,217,216,215,214,213,212,210,209,208,207,206,204,203,202,200,199,198,196,195,194,192,191,189,187,185,183,181,179,177},
134 {32,34,36,38,40,42,44,46,48,49,51,52,53,55,56,57,59,60,61,63,64,65,66,67,68,70,71,72,73,74,75,76,78,79,80,81,82,83,84,85,86,87,88,89,90,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,33,35,37,39,41,43,45,47,50,54,58,62,69,77,91,255,219,205,197,190,186,182,178,175,173,171,169,167,165,163,161,159,158,157,156,155,154,153,152,151,150,149,148,147,146,145,144,143,142,141,140,139,138,137,136,135,134,133,132,131,130,129,128,254,253,252,251,250,249,248,247,246,245,244,243,242,241,240,239,238,237,236,235,234,233,232,231,230,229,228,227,226,225,224,223,222,221,220,218,217,216,215,214,213,212,211,210,209,208,207,206,204,203,202,201,200,199,198,196,195,194,193,192,191,189,188,187,185,184,183,181,180,179,177,176,174,172,170,168,166,164,162,160},
135 {16,18,20,22,24,26,28,30,32,33,34,36,37,38,40,41,42,44,45,46,48,49,50,51,52,53,55,56,57,58,59,60,61,63,64,65,66,67,68,69,70,71,72,73,74,75,76,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,17,19,21,23,25,27,29,31,35,39,43,47,54,62,77,255,205,190,182,175,171,167,163,159,157,155,153,151,149,147,145,143,142,141,140,139,138,137,136,135,134,133,132,131,130,129,128,254,253,252,251,250,249,248,247,246,245,244,243,242,241,240,239,238,237,236,235,234,233,232,231,230,229,228,227,226,225,224,223,222,221,220,219,218,217,216,215,214,213,212,211,210,209,208,207,206,204,203,202,201,200,199,198,197,196,195,194,193,192,191,189,188,187,186,185,184,183,181,180,179,178,177,176,174,173,172,170,169,168,166,165,164,162,161,160,158,156,154,152,150,148,146,144},
136 {2,4,6,8,10,12,14,16,17,18,20,21,22,24,25,26,28,29,30,32,33,34,35,36,37,38,40,41,42,43,44,45,46,48,49,50,51,52,53,54,55,56,57,58,59,60,61,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,0,1,3,5,7,9,11,13,15,19,23,27,31,39,47,62,255,190,175,167,159,155,151,147,143,141,139,137,135,133,131,129,254,253,252,251,250,249,248,247,246,245,244,243,242,241,240,239,238,237,236,235,234,233,232,231,230,229,228,227,226,225,224,223,222,221,220,219,218,217,216,215,214,213,212,211,210,209,208,207,206,205,204,203,202,201,200,199,198,197,196,195,194,193,192,191,189,188,187,186,185,184,183,182,181,180,179,178,177,176,174,173,172,171,170,169,168,166,165,164,163,162,161,160,158,157,156,154,153,152,150,149,148,146,145,144,142,140,138,136,134,132,130,128},
137 {1,2,4,5,6,8,9,10,12,13,14,16,17,18,19,20,21,22,24,25,26,27,28,29,30,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,0,3,7,11,15,23,31,47,255,175,159,151,143,139,135,131,254,253,252,251,250,249,248,247,246,245,244,243,242,241,240,239,238,237,236,235,234,233,232,231,230,229,228,227,226,225,224,223,222,221,220,219,218,217,216,215,214,213,212,211,210,209,208,207,206,205,204,203,202,201,200,199,198,197,196,195,194,193,192,191,190,189,188,187,186,185,184,183,182,181,180,179,178,177,176,174,173,172,171,170,169,168,167,166,165,164,163,162,161,160,158,157,156,155,154,153,152,150,149,148,147,146,145,144,142,141,140,138,137,136,134,133,132,130,129,128},
138 {1,2,3,4,5,6,8,9,10,11,12,13,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,0,7,15,31,255,159,143,135,254,253,252,251,250,249,248,247,246,245,244,243,242,241,240,239,238,237,236,235,234,233,232,231,230,229,228,227,226,225,224,223,222,221,220,219,218,217,216,215,214,213,212,211,210,209,208,207,206,205,204,203,202,201,200,199,198,197,196,195,194,193,192,191,190,189,188,187,186,185,184,183,182,181,180,179,178,177,176,175,174,173,172,171,170,169,168,167,166,165,164,163,162,161,160,158,157,156,155,154,153,152,151,150,149,148,147,146,145,144,142,141,140,139,138,137,136,134,133,132,131,130,129,128},
139 {1,2,3,4,5,6,7,8,9,10,11,12,13,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,0,15,255,143,254,253,252,251,250,249,248,247,246,245,244,243,242,241,240,239,238,237,236,235,234,233,232,231,230,229,228,227,226,225,224,223,222,221,220,219,218,217,216,215,214,213,212,211,210,209,208,207,206,205,204,203,202,201,200,199,198,197,196,195,194,193,192,191,190,189,188,187,186,185,184,183,182,181,180,179,178,177,176,175,174,173,172,171,170,169,168,167,166,165,164,163,162,161,160,159,158,157,156,155,154,153,152,151,150,149,148,147,146,145,144,142,141,140,139,138,137,136,135,134,133,132,131,130,129,128},
140 {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,0,255,254,253,252,251,250,249,248,247,246,245,244,243,242,241,240,239,238,237,236,235,234,233,232,231,230,229,228,227,226,225,224,223,222,221,220,219,218,217,216,215,214,213,212,211,210,209,208,207,206,205,204,203,202,201,200,199,198,197,196,195,194,193,192,191,190,189,188,187,186,185,184,183,182,181,180,179,178,177,176,175,174,173,172,171,170,169,168,167,166,165,164,163,162,161,160,159,158,157,156,155,154,153,152,151,150,149,148,147,146,145,144,143,142,141,140,139,138,137,136,135,134,133,132,131,130,129,128}
141 };
142 
143 
144 #ifndef	MIN_MACRO
145 #define MIN_MACRO(a,b) (((a)<(b))?(a):(b))
146 #endif
147 
148 #ifndef	MAX_MACRO
149 #define MAX_MACRO(a,b) (((a)>(b))?(a):(b))
150 #endif
151 
152 #define POSITIVE_ULAW_ZERO 0xff
153 #define NEGATIVE_ULAW_ZERO 0x7f
154 
155 #define CAPMAXSCHAR(x)  ((x > 127) ? 127 : x)
156 #define CAPMAXUCHAR(x)  ((x > 255) ? 255 : x)
157 #define CAPMAXSHORT(x)  ((x > 32767) ? 32767 : x)
158 #define CAPMAXUSHORT(x) ((x > 65535) ? 65535 : x)
159 
160 #define UNDEFINED_UINT      -1
161 #define DEFAULT_BLOCK_SIZE  256
162 #define DEFAULT_V0NMEAN     0
163 #define DEFAULT_V2NMEAN     4
164 #define DEFAULT_MAXNLPC     0
165 #define DEFAULT_NCHAN       1
166 #define DEFAULT_NSKIP       0
167 #define DEFAULT_NDISCARD    0
168 #define NBITPERLONG         32
169 #define DEFAULT_MINSNR      256
170 #define DEFAULT_QUANTERROR  0
171 #define MINBITRATE          2.5
172 
173 #define MEAN_VERSION0   0
174 #define MEAN_VERSION2   4
175 
176 #define SHN_FN_DIFF0        0
177 #define SHN_FN_DIFF1        1
178 #define SHN_FN_DIFF2        2
179 #define SHN_FN_DIFF3        3
180 #define SHN_FN_QUIT         4
181 #define SHN_FN_BLOCKSIZE    5
182 #define SHN_FN_BITSHIFT     6
183 #define SHN_FN_QLPC         7
184 #define SHN_FN_ZERO         8
185 #define SHN_FN_VERBATIM     9
186 
187 #define SHN_TYPE_AU1              0
188 #define SHN_TYPE_S8               1
189 #define SHN_TYPE_U8               2
190 #define SHN_TYPE_S16HL            3
191 #define SHN_TYPE_U16HL            4
192 #define SHN_TYPE_S16LH            5
193 #define SHN_TYPE_U16LH            6
194 #define SHN_TYPE_ULAW             7
195 #define SHN_TYPE_AU2              8
196 #define SHN_TYPE_AU3              9
197 #define SHN_TYPE_ALAW            10
198 #define SHN_TYPE_RIFF_WAVE       11
199 #define SHN_TYPE_EOF             12
200 #define SHN_TYPE_GENERIC_ULAW   128
201 #define SHN_TYPE_GENERIC_ALAW   129
202 
203 #define SHN_FNSIZE                  2
204 #define SHN_CHANNELSIZE             0
205 #define SHN_TYPESIZE                4
206 #define SHN_ULONGSIZE               2
207 #define SHN_NSKIPSIZE               1
208 #define SHN_LPCQSIZE                2
209 #define SHN_LPCQUANT                5
210 #define SHN_XBYTESIZE               7
211 #define SHN_VERBATIM_CKSIZE_SIZE    5
212 #define SHN_VERBATIM_BYTE_SIZE      8
213 #define SHN_ENERGYSIZE              3
214 #define SHN_BITSHIFTSIZE            2
215 
216 #define SHN_LPCQOFFSET_VER2 (1 << SHN_LPCQUANT)
217 
218 
219 #define SHN_MAGIC  0x676B6A61   /* looks like "ajkg" as chars. */
220 
221 #ifndef M_LN2
222 #define M_LN2   0.69314718055994530942
223 #endif
224 
225 #ifndef M_PI
226 #define M_PI    3.14159265358979323846
227 #endif
228 
229 
word_get(shn_t * shn,SDL_RWops * rw,Uint32 * word)230 static int word_get(shn_t *shn, SDL_RWops *rw, Uint32 *word)
231 {
232     if (shn->nbyteget < 4)
233     {
234         shn->nbyteget += SDL_RWread(rw, shn->getbuf, 1, SHN_BUFSIZ);
235         BAIL_IF_MACRO(shn->nbyteget < 4, NULL, 0);
236         shn->getbufp = shn->getbuf;
237     } /* if */
238 
239     if (word != NULL)
240     {
241         *word = (((Sint32) shn->getbufp[0]) << 24) |
242                 (((Sint32) shn->getbufp[1]) << 16) |
243                 (((Sint32) shn->getbufp[2]) <<  8) |
244                 (((Sint32) shn->getbufp[3])      );
245     } /* if */
246 
247     shn->getbufp += 4;
248     shn->nbyteget -= 4;
249 
250     return(1);
251 } /* word_get */
252 
253 
uvar_get(int nbin,shn_t * shn,SDL_RWops * rw,Sint32 * word)254 static int uvar_get(int nbin, shn_t *shn, SDL_RWops *rw, Sint32 *word)
255 {
256     Sint32 result;
257 
258     if (shn->nbitget == 0)
259     {
260         BAIL_IF_MACRO(!word_get(shn, rw, &shn->gbuffer), NULL, 0);
261         shn->nbitget = 32;
262     } /* if */
263 
264     for (result = 0; !(shn->gbuffer & (1L << --shn->nbitget)); result++)
265     {
266         if (shn->nbitget == 0)
267         {
268             BAIL_IF_MACRO(!word_get(shn, rw, &shn->gbuffer), NULL, 0);
269             shn->nbitget = 32;
270         } /* if */
271     } /* for */
272 
273     while (nbin != 0)
274     {
275         if (shn->nbitget >= nbin)
276         {
277             result = ( (result << nbin) |
278                         ((shn->gbuffer >> (shn->nbitget - nbin)) &
279                         mask_table[nbin]) );
280             shn->nbitget -= nbin;
281             break;
282         } /* if */
283         else
284         {
285             result = (result << shn->nbitget) |
286                      (shn->gbuffer & mask_table[shn->nbitget]);
287             BAIL_IF_MACRO(!word_get(shn, rw, &shn->gbuffer), NULL, 0);
288             nbin -= shn->nbitget;
289             shn->nbitget = 32;
290         } /* else */
291     } /* while */
292 
293     if (word != NULL)
294         *word = result;
295 
296     return(1);
297 } /* uvar_get */
298 
299 
var_get(int nbin,shn_t * shn,SDL_RWops * rw,Sint32 * word)300 static int var_get(int nbin, shn_t *shn, SDL_RWops *rw, Sint32 *word)
301 {
302     BAIL_IF_MACRO(!uvar_get(nbin + 1, shn, rw, word), NULL, 0);
303 
304     if ((*word) & 1)
305         *word = (Sint32) ~((*word) >> 1);
306     else
307         *word = (Sint32) ((*word) >> 1);
308 
309     return(1);
310 } /* var_get */
311 
312 
ulong_get(shn_t * shn,SDL_RWops * rw,Sint32 * word)313 static int ulong_get(shn_t *shn, SDL_RWops *rw, Sint32 *word)
314 {
315     Sint32 nbit;
316     Sint32 retval;
317     BAIL_IF_MACRO(!uvar_get(SHN_ULONGSIZE, shn, rw, &nbit), NULL, 0);
318     BAIL_IF_MACRO(!uvar_get(nbit, shn, rw, &retval), NULL, 0);
319 
320     if (word != NULL)
321         *word = retval;
322 
323     return(1);
324 } /* ulong_get */
325 
326 
uint_get(int nbit,shn_t * shn,SDL_RWops * rw,Sint32 * w)327 static __inline__ int uint_get(int nbit, shn_t *shn, SDL_RWops *rw, Sint32 *w)
328 {
329     return((shn->version == 0) ?
330                 uvar_get(nbit, shn, rw, w) :
331                 ulong_get(shn, rw, w));
332 } /* uint_get */
333 
334 
SHN_init(void)335 static int SHN_init(void)
336 {
337     return(1);  /* initialization always successful. */
338 } /* SHN_init */
339 
340 
SHN_quit(void)341 static void SHN_quit(void)
342 {
343     /* it's a no-op. */
344 } /* SHN_quit */
345 
346 
347 /*
348  * Look through the whole file for a SHN magic number. This is costly, so
349  *  it should only be done if the user SWEARS they have a Shorten stream...
350  */
extended_shn_magic_search(Sound_Sample * sample)351 static __inline__ int extended_shn_magic_search(Sound_Sample *sample)
352 {
353     SDL_RWops *rw = ((Sound_SampleInternal *) sample->opaque)->rw;
354     Uint32 word = 0;
355     Uint8 ch;
356 
357     while (1)
358     {
359         BAIL_IF_MACRO(SDL_RWread(rw, &ch, sizeof (ch), 1) != 1, NULL, -1);
360         word = ((word << 8) & 0xFFFFFF00) | ch;
361         if (SDL_SwapBE32(word) == SHN_MAGIC)
362         {
363             BAIL_IF_MACRO(SDL_RWread(rw, &ch, sizeof (ch), 1) != 1, NULL, -1);
364             return((int) ch);
365         } /* if */
366     } /* while */
367 
368     return((int) ch);
369 } /* extended_shn_magic_search */
370 
371 
372 /* look for the magic number in the RWops and see what kind of file this is. */
determine_shn_version(Sound_Sample * sample,const char * ext)373 static __inline__ int determine_shn_version(Sound_Sample *sample,
374                                             const char *ext)
375 {
376     SDL_RWops *rw = ((Sound_SampleInternal *) sample->opaque)->rw;
377     Uint32 magic;
378     Uint8 ch;
379 
380     /*
381      * Apparently the magic number can start at any byte offset in the file,
382      *  and we should just discard prior data, but I'm going to restrict it
383      *  to offset zero for now, so we don't chug down every file that might
384      *  happen to pass through here. If the extension is explicitly "SHN", we
385      *  check the whole stream, though.
386      */
387 
388     if (__Sound_strcasecmp(ext, "shn") == 0)
389         return(extended_shn_magic_search(sample));
390 
391     BAIL_IF_MACRO(SDL_RWread(rw, &magic, sizeof (magic), 1) != 1, NULL, -1);
392     BAIL_IF_MACRO(SDL_SwapLE32(magic) != SHN_MAGIC, "SHN: Not a SHN file", -1);
393     BAIL_IF_MACRO(SDL_RWread(rw, &ch, sizeof (ch), 1) != 1, NULL, -1);
394     BAIL_IF_MACRO(ch > 3, "SHN: Unsupported file version", -1);
395 
396     return((int) ch);
397 } /* determine_shn_version */
398 
399 
init_shn_offset(Sint32 ** offset,int nchan,int nblock,int ftype)400 static void init_shn_offset(Sint32 **offset, int nchan, int nblock, int ftype)
401 {
402     Sint32 mean = 0;
403     int chan;
404 
405     switch (ftype)
406     {
407         case SHN_TYPE_AU1:
408         case SHN_TYPE_S8:
409         case SHN_TYPE_S16HL:
410         case SHN_TYPE_S16LH:
411         case SHN_TYPE_ULAW:
412         case SHN_TYPE_AU2:
413         case SHN_TYPE_AU3:
414         case SHN_TYPE_ALAW:
415             mean = 0;
416             break;
417         case SHN_TYPE_U8:
418             mean = 0x80;
419             break;
420         case SHN_TYPE_U16HL:
421         case SHN_TYPE_U16LH:
422             mean = 0x8000;
423             break;
424         default:
425             __Sound_SetError("SHN: unknown file type");
426             return;
427     } /* switch */
428 
429     for(chan = 0; chan < nchan; chan++)
430     {
431         int i;
432         for(i = 0; i < nblock; i++)
433             offset[chan][i] = mean;
434     } /* for */
435 } /* init_shn_offset */
436 
437 
cvt_shnftype_to_sdlfmt(Sint16 shntype)438 static __inline__ Uint16 cvt_shnftype_to_sdlfmt(Sint16 shntype)
439 {
440     switch (shntype)
441     {
442         case SHN_TYPE_S8:
443             return(AUDIO_S8);
444 
445         case SHN_TYPE_ALAW:
446         case SHN_TYPE_ULAW:
447         case SHN_TYPE_AU1:
448         case SHN_TYPE_AU2:
449         case SHN_TYPE_AU3:
450         case SHN_TYPE_U8:
451             return(AUDIO_U8);
452 
453         case SHN_TYPE_S16HL:
454             return(AUDIO_S16MSB);
455 
456         case SHN_TYPE_S16LH:
457             return(AUDIO_S16LSB);
458 
459         case SHN_TYPE_U16HL:
460             return(AUDIO_U16MSB);
461 
462         case SHN_TYPE_U16LH:
463             return(AUDIO_U16LSB);
464     } /* switch */
465 
466     return(0);
467 } /* cvt_shnftype_to_sdlfmt */
468 
469 
skip_bits(shn_t * shn,SDL_RWops * rw)470 static __inline__ int skip_bits(shn_t *shn, SDL_RWops *rw)
471 {
472     int i;
473     Sint32 skip;
474     Sint32 trash;
475 
476     BAIL_IF_MACRO(!uint_get(SHN_NSKIPSIZE, shn, rw, &skip), NULL, 0);
477     for(i = 0; i < skip; i++)
478     {
479         BAIL_IF_MACRO(!uint_get(SHN_XBYTESIZE, shn, rw, &trash), NULL, 0);
480     } /* for */
481 
482     return(1);
483 } /* skip_bits */
484 
485 
shn_long2d(Uint32 n0,Uint32 n1)486 static Sint32 **shn_long2d(Uint32 n0, Uint32 n1)
487 {
488     Sint32 **array0;
489     Uint32 size = (n0 * sizeof (Sint32 *)) + (n0 * n1 * sizeof (Sint32));
490 
491     array0 = (Sint32 **) malloc(size);
492     if (array0 != NULL)
493     {
494         int i;
495         Sint32 *array1 = (Sint32 *) (array0 + n0);
496         for(i = 0; i < n0; i++)
497             array0[i] = array1 + (i * n1);
498     } /* if */
499 
500     return(array0);
501 } /* shn_long2d */
502 
503 #define riffID 0x46464952  /* "RIFF", in ascii. */
504 #define waveID 0x45564157  /* "WAVE", in ascii. */
505 #define fmtID  0x20746D66  /* "fmt ", in ascii. */
506 #define dataID 0x61746164  /* "data", in ascii. */
507 
verb_ReadLE32(shn_t * shn,SDL_RWops * rw,Uint32 * word)508 static int verb_ReadLE32(shn_t *shn, SDL_RWops *rw, Uint32 *word)
509 {
510     int i;
511     Uint8 chars[4];
512     Sint32 byte;
513 
514     for (i = 0; i < 4; i++)
515     {
516         if (!uvar_get(SHN_VERBATIM_BYTE_SIZE, shn, rw, &byte))
517             return(0);
518         chars[i] = (Uint8) byte;
519     } /* for */
520 
521     memcpy(word, chars, sizeof (*word));
522     *word = SDL_SwapLE32(*word);
523 
524     return(1);
525 } /* verb_ReadLE32 */
526 
527 
verb_ReadLE16(shn_t * shn,SDL_RWops * rw,Uint16 * word)528 static int verb_ReadLE16(shn_t *shn, SDL_RWops *rw, Uint16 *word)
529 {
530     int i;
531     Uint8 chars[2];
532     Sint32 byte;
533 
534     for (i = 0; i < 2; i++)
535     {
536         if (!uvar_get(SHN_VERBATIM_BYTE_SIZE, shn, rw, &byte))
537             return(0);
538         chars[i] = (Uint8) byte;
539     } /* for */
540 
541     memcpy(word, chars, sizeof (*word));
542     *word = SDL_SwapLE16(*word);
543 
544     return(1);
545 } /* verb_ReadLE16 */
546 
547 
parse_riff_header(shn_t * shn,Sound_Sample * sample)548 static __inline__ int parse_riff_header(shn_t *shn, Sound_Sample *sample)
549 {
550     Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
551     SDL_RWops *rw = internal->rw;
552     Uint16 u16;
553     Uint32 u32;
554     Sint32 cklen;
555 
556     BAIL_IF_MACRO(!uvar_get(SHN_VERBATIM_CKSIZE_SIZE, shn, rw, &cklen), NULL, 0);
557 
558     BAIL_IF_MACRO(!verb_ReadLE32(shn, rw, &u32), NULL, 0); /* RIFF header */
559     BAIL_IF_MACRO(u32 != riffID, "SHN: No RIFF header.", 0);
560     BAIL_IF_MACRO(!verb_ReadLE32(shn, rw, &u32), NULL, 0); /* length */
561 
562     BAIL_IF_MACRO(!verb_ReadLE32(shn, rw, &u32), NULL, 0); /* WAVE header */
563     BAIL_IF_MACRO(u32 != waveID, "SHN: No WAVE header.", 0);
564 
565     BAIL_IF_MACRO(!verb_ReadLE32(shn, rw, &u32), NULL, 0); /* 'fmt ' header */
566     BAIL_IF_MACRO(u32 != fmtID,  "SHN: No 'fmt ' header.", 0);
567 
568     BAIL_IF_MACRO(!verb_ReadLE32(shn, rw, &u32), NULL, 0); /* chunksize */
569     BAIL_IF_MACRO(!verb_ReadLE16(shn, rw, &u16), NULL, 0); /* format */
570     BAIL_IF_MACRO(!verb_ReadLE16(shn, rw, &u16), NULL, 0); /* channels */
571     sample->actual.channels = u16;
572     BAIL_IF_MACRO(!verb_ReadLE32(shn, rw, &u32), NULL, 0); /* sample rate */
573     sample->actual.rate = u32;
574 
575     BAIL_IF_MACRO(!verb_ReadLE32(shn, rw, &u32), NULL, 0); /* bytespersec */
576     BAIL_IF_MACRO(!verb_ReadLE16(shn, rw, &u16), NULL, 0); /* blockalign */
577     BAIL_IF_MACRO(!verb_ReadLE16(shn, rw, &u16), NULL, 0); /* bitspersample */
578 
579     BAIL_IF_MACRO(!verb_ReadLE32(shn, rw, &u32), NULL, 0); /* 'data' header */
580     BAIL_IF_MACRO(u32 != dataID,  "SHN: No 'data' header.", 0);
581     BAIL_IF_MACRO(!verb_ReadLE32(shn, rw, &u32), NULL, 0); /* chunksize */
582 
583     return(1);
584 } /* parse_riff_header */
585 
586 
SHN_open(Sound_Sample * sample,const char * ext)587 static int SHN_open(Sound_Sample *sample, const char *ext)
588 {
589     Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
590     SDL_RWops *rw = internal->rw;
591     shn_t _shn;
592     shn_t *shn = &_shn;  /* malloc and copy later. */
593     Sint32 cmd;
594     Sint32 chan;
595 
596     memset(shn, '\0', sizeof (shn_t));
597     shn->getbufp = shn->getbuf = (Uint8 *) malloc(SHN_BUFSIZ);
598     shn->datatype = SHN_TYPE_EOF;
599     shn->nchan = DEFAULT_NCHAN;
600     shn->blocksize = DEFAULT_BLOCK_SIZE;
601     shn->maxnlpc = DEFAULT_MAXNLPC;
602     shn->nmean = UNDEFINED_UINT;
603     shn->version = determine_shn_version(sample, ext);
604 
605     if (shn->version == -1) goto shn_open_puke;
606     if (!uint_get(SHN_TYPESIZE, shn, rw, &shn->datatype)) goto shn_open_puke;
607     if (!uint_get(SHN_CHANNELSIZE, shn, rw, &shn->nchan)) goto shn_open_puke;
608 
609     sample->actual.format = cvt_shnftype_to_sdlfmt(shn->datatype);
610     if (sample->actual.format == 0)
611     {
612         SDL_SetError(ERR_UNSUPPORTED_FORMAT);
613         goto shn_open_puke;
614     } /* if */
615 
616     if (shn->version > 0)
617     {
618         int rc = uint_get((int) (log((double) DEFAULT_BLOCK_SIZE) / M_LN2),
619                            shn, rw, &shn->blocksize);
620         if (!rc)  goto shn_open_puke;;
621         if (!uint_get(SHN_LPCQSIZE, shn, rw, &shn->maxnlpc)) goto shn_open_puke;
622         if (!uint_get(0, shn, rw, &shn->nmean)) goto shn_open_puke;
623         if (!skip_bits(shn, rw)) goto shn_open_puke;
624     } /* else */
625 
626     shn->nwrap = (shn->maxnlpc > 3) ? shn->maxnlpc : 3;
627 
628     /* grab some space for the input buffer */
629     shn->buffer = shn_long2d((Uint32) shn->nchan, shn->blocksize + shn->nwrap);
630     shn->offset = shn_long2d((Uint32) shn->nchan, MAX_MACRO(1, shn->nmean));
631 
632     for (chan = 0; chan < shn->nchan; chan++)
633     {
634         int i;
635         for(i = 0; i < shn->nwrap; i++)
636             shn->buffer[chan][i] = 0;
637         shn->buffer[chan] += shn->nwrap;
638     } /* for */
639 
640     if (shn->maxnlpc > 0)
641     {
642         shn->qlpc = (int *) malloc((Uint32) (shn->maxnlpc * sizeof (Sint32)));
643         if (shn->qlpc == NULL)
644         {
645             __Sound_SetError(ERR_OUT_OF_MEMORY);
646             goto shn_open_puke;
647         } /* if */
648     } /* if */
649 
650     if (shn->version > 1)
651         shn->lpcqoffset = SHN_LPCQOFFSET_VER2;
652 
653     init_shn_offset(shn->offset, shn->nchan,
654                     MAX_MACRO(1, shn->nmean), shn->datatype);
655 
656     if ( (!uvar_get(SHN_FNSIZE, shn, rw, &cmd)) ||
657          (cmd != SHN_FN_VERBATIM) ||
658          (!parse_riff_header(shn, sample)) )
659     {
660         if (cmd != SHN_FN_VERBATIM) /* the other conditions set error state */
661             __Sound_SetError("SHN: Expected VERBATIM function");
662 
663         goto shn_open_puke;
664         return(0);
665     } /* if */
666 
667     shn->start_pos = SDL_RWtell(rw);
668 
669     shn = (shn_t *) malloc(sizeof (shn_t));
670     if (shn == NULL)
671     {
672         __Sound_SetError(ERR_OUT_OF_MEMORY);
673         goto shn_open_puke;
674     } /* if */
675 
676     memcpy(shn, &_shn, sizeof (shn_t));
677     internal->decoder_private = shn;
678 
679     SNDDBG(("SHN: Accepting data stream.\n"));
680     sample->flags = SOUND_SAMPLEFLAG_NONE;
681     return(1); /* we'll handle this data. */
682 
683 shn_open_puke:
684     if (_shn.getbuf)
685         free(_shn.getbuf);
686     if (_shn.buffer != NULL)
687         free(_shn.buffer);
688     if (_shn.offset != NULL)
689         free(_shn.offset);
690     if (_shn.qlpc != NULL)
691         free(_shn.qlpc);
692 
693     return(0);
694 } /* SHN_open */
695 
696 
fix_bitshift(Sint32 * buffer,int nitem,int bitshift,int ftype)697 static void fix_bitshift(Sint32 *buffer, int nitem, int bitshift, int ftype)
698 {
699     int i;
700 
701     if (ftype == SHN_TYPE_AU1)
702     {
703         for (i = 0; i < nitem; i++)
704             buffer[i] = ulaw_outward[bitshift][buffer[i] + 128];
705     } /* if */
706     else if (ftype == SHN_TYPE_AU2)
707     {
708         for(i = 0; i < nitem; i++)
709         {
710             if (buffer[i] >= 0)
711                 buffer[i] = ulaw_outward[bitshift][buffer[i] + 128];
712             else if (buffer[i] == -1)
713                 buffer[i] = NEGATIVE_ULAW_ZERO;
714             else
715                 buffer[i] = ulaw_outward[bitshift][buffer[i] + 129];
716         } /* for */
717     } /* else if */
718     else
719     {
720         if (bitshift != 0)
721         {
722             for(i = 0; i < nitem; i++)
723                 buffer[i] <<= bitshift;
724         } /* if */
725     } /* else */
726 } /* fix_bitshift */
727 
728 
SHN_close(Sound_Sample * sample)729 static void SHN_close(Sound_Sample *sample)
730 {
731     Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
732     shn_t *shn = (shn_t *) internal->decoder_private;
733 
734     if (shn->qlpc != NULL)
735         free(shn->qlpc);
736 
737     if (shn->backBuffer != NULL)
738         free(shn->backBuffer);
739 
740     if (shn->offset != NULL)
741         free(shn->offset);
742 
743     if (shn->buffer != NULL)
744         free(shn->buffer);
745 
746     if (shn->getbuf != NULL)
747         free(shn->getbuf);
748 
749     free(shn);
750 } /* SHN_close */
751 
752 
753 /* xLaw conversions... */
754 
755 /* adapted by ajr for int input */
Slinear2ulaw(int sample)756 static Uint8 Slinear2ulaw(int sample)
757 {
758 /*
759 ** This routine converts from linear to ulaw.
760 **
761 ** Craig Reese: IDA/Supercomputing Research Center
762 ** Joe Campbell: Department of Defense
763 ** 29 September 1989
764 **
765 ** References:
766 ** 1) CCITT Recommendation G.711  (very difficult to follow)
767 ** 2) "A New Digital Technique for Implementation of Any
768 **     Continuous PCM Companding Law," Villeret, Michel,
769 **     et al. 1973 IEEE Int. Conf. on Communications, Vol 1,
770 **     1973, pg. 11.12-11.17
771 ** 3) MIL-STD-188-113,"Interoperability and Performance Standards
772 **     for Analog-to_Digital Conversion Techniques,"
773 **     17 February 1987
774 **
775 ** Input: Signed 16 bit linear sample
776 ** Output: 8 bit ulaw sample
777 */
778 
779 #define BIAS 0x84   /* define the add-in bias for 16 bit samples */
780 #define CLIP 32635
781 
782   int sign, exponent, mantissa;
783   Uint8 ulawbyte;
784   static const int exp_lut[256] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
785                                    4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
786                                    5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
787                                    5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
788                                    6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
789                                    6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
790                                    6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
791                                    6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
792                                    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
793                                    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
794                                    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
795                                    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
796                                    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
797                                    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
798                                    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
799                                    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};
800 
801     /* Get the sample into sign-magnitude. */
802     if (sample >= 0)
803         sign = 0;
804     else
805     {
806         sign = 0x80;
807         sample = -sample;
808     } /* else */
809 
810     /* clip the magnitude */
811     if (sample > CLIP)
812         sample = CLIP;
813 
814     /* Convert from 16 bit linear to ulaw. */
815     sample = sample + BIAS;
816     exponent = exp_lut[( sample >> 7 ) & 0xFF];
817     mantissa = (sample >> (exponent + 3)) & 0x0F;
818     ulawbyte = ~(sign | (exponent << 4) | mantissa);
819 
820     return(ulawbyte);
821 } /* Slinear2ulaw */
822 
823 
824 /* this is derived from the Sun code - it is a bit simpler and has int input */
825 #define QUANT_MASK      (0xf)           /* Quantization field mask. */
826 #define NSEGS           (8)             /* Number of A-law segments. */
827 #define SEG_SHIFT       (4)             /* Left shift for segment number. */
828 
829 
Slinear2alaw(Sint32 linear)830 static Uint8 Slinear2alaw(Sint32 linear)
831 {
832     int	seg;
833     Uint8 aval, mask;
834     static const Sint32 seg_aend[NSEGS] =
835     {
836         0x1f,0x3f,0x7f,0xff,0x1ff,0x3ff,0x7ff,0xfff
837     };
838 
839     linear >>= 3;
840     if(linear >= 0)
841         mask = 0xd5;		/* sign (7th) bit = 1 */
842     else
843     {
844         mask = 0x55;		/* sign bit = 0 */
845         linear = -linear - 1;
846     } /* else */
847 
848     /* Convert the scaled magnitude to segment number. */
849     for (seg = 0; (seg < NSEGS) && (linear > seg_aend[seg]); seg++);
850 
851     /* Combine the sign, segment, and quantization bits. */
852     if (seg >= NSEGS)    /* out of range, return maximum value. */
853         return((Uint8) (0x7F ^ mask));
854 
855     aval = (Uint8) seg << SEG_SHIFT;
856     if (seg < 2)
857         aval |= (linear >> 1) & QUANT_MASK;
858     else
859         aval |= (linear >> seg) & QUANT_MASK;
860 
861     return (aval ^ mask);
862 } /* Slinear2alaw */
863 
864 
865 /* convert from signed ints to a given type and write */
put_to_buffers(Sound_Sample * sample,Uint32 bw)866 static Uint32 put_to_buffers(Sound_Sample *sample, Uint32 bw)
867 {
868     Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
869     shn_t *shn = (shn_t *) internal->decoder_private;
870     int i, chan;
871     Sint32 *data0 = shn->buffer[0];
872     Sint32 nitem = shn->blocksize;
873     int datasize = ((sample->actual.format & 0xFF) / 8);
874     Uint32 bsiz = shn->nchan * nitem * datasize;
875 
876     assert(shn->backBufLeft == 0);
877 
878     if (shn->backBufferSize < bsiz)
879     {
880         void *rc = realloc(shn->backBuffer, bsiz);
881         if (rc == NULL)
882         {
883             sample->flags |= SOUND_SAMPLEFLAG_ERROR;
884             BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
885         } /* if */
886         shn->backBuffer = (Uint8 *) rc;
887         shn->backBufferSize = bsiz;
888     } /* if */
889 
890     switch (shn->datatype)
891     {
892         case SHN_TYPE_AU1: /* leave the conversion to fix_bitshift() */
893         case SHN_TYPE_AU2:
894         {
895             Uint8 *writebufp = shn->backBuffer;
896             if (shn->nchan == 1)
897             {
898                 for (i = 0; i < nitem; i++)
899                     *writebufp++ = data0[i];
900             } /* if */
901             else
902             {
903                 for (i = 0; i < nitem; i++)
904                 {
905                     for (chan = 0; chan < shn->nchan; chan++)
906                         *writebufp++ = shn->buffer[chan][i];
907                 } /* for */
908             } /* else */
909         } /* case */
910         break;
911 
912         case SHN_TYPE_U8:
913         {
914             Uint8 *writebufp = shn->backBuffer;
915             if (shn->nchan == 1)
916             {
917                 for (i = 0; i < nitem; i++)
918                     *writebufp++ = CAPMAXUCHAR(data0[i]);
919             } /* if */
920             else
921             {
922                 for (i = 0; i < nitem; i++)
923                 {
924                     for (chan = 0; chan < shn->nchan; chan++)
925                         *writebufp++ = CAPMAXUCHAR(shn->buffer[chan][i]);
926                 } /* for */
927             } /* else */
928         } /* case */
929         break;
930 
931         case SHN_TYPE_S8:
932         {
933             Sint8 *writebufp = (Sint8 *) shn->backBuffer;
934             if (shn->nchan == 1)
935             {
936                 for(i = 0; i < nitem; i++)
937                     *writebufp++ = CAPMAXSCHAR(data0[i]);
938             } /* if */
939             else
940             {
941                 for(i = 0; i < nitem; i++)
942                 {
943                     for(chan = 0; chan < shn->nchan; chan++)
944                         *writebufp++ = CAPMAXSCHAR(shn->buffer[chan][i]);
945                 } /* for */
946             } /* else */
947         } /* case */
948         break;
949 
950         case SHN_TYPE_S16HL:
951         case SHN_TYPE_S16LH:
952         {
953             Sint16 *writebufp = (Sint16 *) shn->backBuffer;
954             if (shn->nchan == 1)
955             {
956                 for (i = 0; i < nitem; i++)
957                     *writebufp++ = CAPMAXSHORT(data0[i]);
958             } /* if */
959             else
960             {
961                 for (i = 0; i < nitem; i++)
962                 {
963                     for (chan = 0; chan < shn->nchan; chan++)
964                         *writebufp++ = CAPMAXSHORT(shn->buffer[chan][i]);
965                 } /* for */
966             } /* else */
967         } /* case */
968         break;
969 
970         case SHN_TYPE_U16HL:
971         case SHN_TYPE_U16LH:
972         {
973             Uint16 *writebufp = (Uint16 *) shn->backBuffer;
974             if (shn->nchan == 1)
975             {
976                 for (i = 0; i < nitem; i++)
977                     *writebufp++ = CAPMAXUSHORT(data0[i]);
978             } /* if */
979             else
980             {
981                 for (i = 0; i < nitem; i++)
982                 {
983                     for (chan = 0; chan < shn->nchan; chan++)
984                         *writebufp++ = CAPMAXUSHORT(shn->buffer[chan][i]);
985                 } /* for */
986             } /* else */
987         } /* case */
988         break;
989 
990         case SHN_TYPE_ULAW:
991         {
992             Uint8 *writebufp = shn->backBuffer;
993             if (shn->nchan == 1)
994             {
995                 for(i = 0; i < nitem; i++)
996                     *writebufp++ = Slinear2ulaw(CAPMAXSHORT((data0[i] << 3)));
997             } /* if */
998             else
999             {
1000                 for(i = 0; i < nitem; i++)
1001                 {
1002                     for(chan = 0; chan < shn->nchan; chan++)
1003                         *writebufp++ = Slinear2ulaw(CAPMAXSHORT((shn->buffer[chan][i] << 3)));
1004                 } /* for */
1005             } /* else */
1006         } /* case */
1007         break;
1008 
1009         case SHN_TYPE_AU3:
1010         {
1011             Uint8 *writebufp = shn->backBuffer;
1012             if (shn->nchan == 1)
1013             {
1014                 for (i = 0; i < nitem; i++)
1015                     if(data0[i] < 0)
1016                         *writebufp++ = (127 - data0[i]) ^ 0xd5;
1017                     else
1018                         *writebufp++ = (data0[i] + 128) ^ 0x55;
1019             } /* if */
1020             else
1021             {
1022                 for (i = 0; i < nitem; i++)
1023                 {
1024                     for (chan = 0; chan < shn->nchan; chan++)
1025                     {
1026                         if (shn->buffer[chan][i] < 0)
1027                             *writebufp++ = (127 - shn->buffer[chan][i]) ^ 0xd5;
1028                         else
1029                             *writebufp++ = (shn->buffer[chan][i] + 128) ^ 0x55;
1030                     } /* for */
1031                 } /* for */
1032             } /* else */
1033         } /* case */
1034         break;
1035 
1036         case SHN_TYPE_ALAW:
1037         {
1038             Uint8 *writebufp = shn->backBuffer;
1039             if (shn->nchan == 1)
1040             {
1041                 for (i = 0; i < nitem; i++)
1042                     *writebufp++ = Slinear2alaw(CAPMAXSHORT((data0[i] << 3)));
1043             } /* if */
1044             else
1045             {
1046                 for (i = 0; i < nitem; i++)
1047                 {
1048                     for(chan = 0; chan < shn->nchan; chan++)
1049                         *writebufp++ = Slinear2alaw(CAPMAXSHORT((shn->buffer[chan][i] << 3)));
1050                 } /* for */
1051             }/* else */
1052         } /* case */
1053         break;
1054     } /* switch */
1055 
1056     i = MIN_MACRO(internal->buffer_size - bw, bsiz);
1057     memcpy((char *)internal->buffer + bw, shn->backBuffer, i);
1058     shn->backBufLeft = bsiz - i;
1059     memcpy(shn->backBuffer, shn->backBuffer + i, shn->backBufLeft);
1060     return(i);
1061 } /* put_to_buffers */
1062 
1063 
1064 #define ROUNDEDSHIFTDOWN(x, n) (((n) == 0) ? (x) : ((x) >> ((n) - 1)) >> 1)
1065 
SHN_read(Sound_Sample * sample)1066 static Uint32 SHN_read(Sound_Sample *sample)
1067 {
1068     Uint32 retval = 0;
1069     Sint32 chan = 0;
1070     Uint32 cpyBytes = 0;
1071     Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
1072     SDL_RWops *rw = internal->rw;
1073     shn_t *shn = (shn_t *) internal->decoder_private;
1074     Sint32 cmd;
1075 
1076     assert(shn->backBufLeft >= 0);
1077 
1078         /* see if there are leftovers to copy... */
1079     if (shn->backBufLeft > 0)
1080     {
1081         retval = MIN_MACRO(shn->backBufLeft, internal->buffer_size);
1082         memcpy(internal->buffer, shn->backBuffer, retval);
1083         shn->backBufLeft -= retval;
1084         memcpy(shn->backBuffer, shn->backBuffer + retval, shn->backBufLeft);
1085     } /* if */
1086 
1087     assert((shn->backBufLeft == 0) || (retval == internal->buffer_size));
1088 
1089     /* get commands from file and execute them */
1090     while (retval < internal->buffer_size)
1091     {
1092         if (!uvar_get(SHN_FNSIZE, shn, rw, &cmd))
1093         {
1094             sample->flags |= SOUND_SAMPLEFLAG_ERROR;
1095             return(retval);
1096         } /* if */
1097 
1098         if (cmd == SHN_FN_QUIT)
1099         {
1100             sample->flags |= SOUND_SAMPLEFLAG_EOF;
1101             return(retval);
1102         } /* if */
1103 
1104         switch(cmd)
1105         {
1106             case SHN_FN_ZERO:
1107             case SHN_FN_DIFF0:
1108             case SHN_FN_DIFF1:
1109             case SHN_FN_DIFF2:
1110             case SHN_FN_DIFF3:
1111             case SHN_FN_QLPC:
1112             {
1113                 Sint32 i;
1114                 Sint32 coffset, *cbuffer = shn->buffer[chan];
1115                 Sint32 resn = 0, nlpc, j;
1116 
1117                 if (cmd != SHN_FN_ZERO)
1118                 {
1119                     if (!uvar_get(SHN_ENERGYSIZE, shn, rw, &resn))
1120                     {
1121                         sample->flags |= SOUND_SAMPLEFLAG_ERROR;
1122                         return(retval);
1123                     } /* if */
1124 
1125                     /* version 0 differed in definition of var_get */
1126                     if (shn->version == 0)
1127                         resn--;
1128                 } /* if */
1129 
1130                 /* find mean offset : N.B. this code duplicated */
1131                 if (shn->nmean == 0)
1132                     coffset = shn->offset[chan][0];
1133                 else
1134                 {
1135                     Sint32 sum = (shn->version < 2) ? 0 : shn->nmean / 2;
1136                     for (i = 0; i < shn->nmean; i++)
1137                         sum += shn->offset[chan][i];
1138 
1139                     if (shn->version < 2)
1140                         coffset = sum / shn->nmean;
1141                     else
1142                         coffset = ROUNDEDSHIFTDOWN(sum / shn->nmean, shn->bitshift);
1143                 } /* else */
1144 
1145                 switch (cmd)
1146                 {
1147                     case SHN_FN_ZERO:
1148                         for (i = 0; i < shn->blocksize; i++)
1149                             cbuffer[i] = 0;
1150                         break;
1151 
1152                     case SHN_FN_DIFF0:
1153                         for(i = 0; i < shn->blocksize; i++)
1154                         {
1155                             if (!var_get(resn, shn, rw, &cbuffer[i]))
1156                             {
1157                                 sample->flags |= SOUND_SAMPLEFLAG_ERROR;
1158                                 return(retval);
1159                             } /* if */
1160                             cbuffer[i] += coffset;
1161                         } /* for */
1162                         break;
1163 
1164                     case SHN_FN_DIFF1:
1165                         for(i = 0; i < shn->blocksize; i++)
1166                         {
1167                             if (!var_get(resn, shn, rw, &cbuffer[i]))
1168                             {
1169                                 sample->flags |= SOUND_SAMPLEFLAG_ERROR;
1170                                 return(retval);
1171                             } /* if */
1172                             cbuffer[i] += cbuffer[i - 1];
1173                         } /* for */
1174                         break;
1175 
1176                     case SHN_FN_DIFF2:
1177                         for (i = 0; i < shn->blocksize; i++)
1178                         {
1179                             if (!var_get(resn, shn, rw, &cbuffer[i]))
1180                             {
1181                                 sample->flags |= SOUND_SAMPLEFLAG_ERROR;
1182                                 return(retval);
1183                             } /* if */
1184                             cbuffer[i] += (2 * cbuffer[i-1] - cbuffer[i-2]);
1185                         } /* for */
1186                         break;
1187 
1188                     case SHN_FN_DIFF3:
1189                         for (i = 0; i < shn->blocksize; i++)
1190                         {
1191                             if (!var_get(resn, shn, rw, &cbuffer[i]))
1192                             {
1193                                 sample->flags |= SOUND_SAMPLEFLAG_ERROR;
1194                                 return(retval);
1195                             } /* if */
1196                             cbuffer[i] += 3 * (cbuffer[i - 1] - cbuffer[i - 2]) + cbuffer[i - 3];
1197                         } /* for */
1198                         break;
1199 
1200                     case SHN_FN_QLPC:
1201                         if (!uvar_get(SHN_LPCQSIZE, shn, rw, &nlpc))
1202                         {
1203                             sample->flags |= SOUND_SAMPLEFLAG_ERROR;
1204                             return(retval);
1205                         } /* if */
1206 
1207                         for(i = 0; i < nlpc; i++)
1208                         {
1209                             if (!var_get(SHN_LPCQUANT, shn, rw, &shn->qlpc[i]))
1210                             {
1211                                 sample->flags |= SOUND_SAMPLEFLAG_ERROR;
1212                                 return(retval);
1213                             } /* if */
1214                         } /* for */
1215 
1216                         for(i = 0; i < nlpc; i++)
1217                             cbuffer[i - nlpc] -= coffset;
1218 
1219                         for(i = 0; i < shn->blocksize; i++)
1220                         {
1221                             Sint32 sum = shn->lpcqoffset;
1222 
1223                             for(j = 0; j < nlpc; j++)
1224                                 sum += shn->qlpc[j] * cbuffer[i - j - 1];
1225 
1226                             if (!var_get(resn, shn, rw, &cbuffer[i]))
1227                             {
1228                                 sample->flags |= SOUND_SAMPLEFLAG_ERROR;
1229                                 return(retval);
1230                             } /* if */
1231                             cbuffer[i] += (sum >> SHN_LPCQUANT);
1232                         } /* for */
1233 
1234                         if (coffset != 0)
1235                         {
1236                             for(i = 0; i < shn->blocksize; i++)
1237                                 cbuffer[i] += coffset;
1238                         } /* if */
1239 
1240                         break;
1241                 } /* switch */
1242 
1243                 /* store mean value if appropriate : N.B. Duplicated code */
1244                 if (shn->nmean > 0)
1245                 {
1246                     Sint32 sum = (shn->version < 2) ? 0 : shn->blocksize / 2;
1247                     for (i = 0; i < shn->blocksize; i++)
1248                         sum += cbuffer[i];
1249 
1250                     for(i = 1; i < shn->nmean; i++)
1251                         shn->offset[chan][i - 1] = shn->offset[chan][i];
1252 
1253                     if (shn->version < 2)
1254                         shn->offset[chan][shn->nmean - 1] = sum / shn->blocksize;
1255                     else
1256                         shn->offset[chan][shn->nmean - 1] = (sum / shn->blocksize) << shn->bitshift;
1257                 } /* if */
1258 
1259                 /* do the wrap */
1260                 for(i = -shn->nwrap; i < 0; i++)
1261                     cbuffer[i] = cbuffer[i + shn->blocksize];
1262 
1263                 fix_bitshift(cbuffer, shn->blocksize, shn->bitshift, shn->datatype);
1264 
1265                 if (chan == shn->nchan - 1)
1266                 {
1267                     retval += put_to_buffers(sample, retval);
1268                     if (sample->flags & SOUND_SAMPLEFLAG_ERROR)
1269                         return(retval);
1270                 } /* if */
1271 
1272                 chan = (chan + 1) % shn->nchan;
1273                 break;
1274             } /* case */
1275 
1276             case SHN_FN_BLOCKSIZE:
1277                 if (!uint_get((int) (log((double) shn->blocksize) / M_LN2),
1278                               shn, rw, &shn->blocksize))
1279                 {
1280                     sample->flags |= SOUND_SAMPLEFLAG_ERROR;
1281                     return(retval);
1282                 } /* if */
1283                 break;
1284 
1285             case SHN_FN_BITSHIFT:
1286                 if (!uvar_get(SHN_BITSHIFTSIZE, shn, rw, &shn->bitshift))
1287                 {
1288                     sample->flags |= SOUND_SAMPLEFLAG_ERROR;
1289                     return(retval);
1290                 } /* if */
1291                 break;
1292 
1293             case SHN_FN_VERBATIM:
1294             default:
1295                 sample->flags |= SOUND_SAMPLEFLAG_ERROR;
1296                 BAIL_MACRO("SHN: Unhandled function.", retval);
1297         } /* switch */
1298     } /* while */
1299 
1300     return(retval);
1301 } /* SHN_read */
1302 
1303 
SHN_rewind(Sound_Sample * sample)1304 static int SHN_rewind(Sound_Sample *sample)
1305 {
1306     Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque;
1307     shn_t *shn = (shn_t *) internal->decoder_private;
1308 
1309 #if 0
1310     int rc = SDL_RWseek(internal->rw, shn->start_pos, SEEK_SET);
1311     BAIL_IF_MACRO(rc != shn->start_pos, ERR_IO_ERROR, 0);
1312     /* !!! FIXME: set state. */
1313     return(1);
1314 #else
1315     /*
1316      * !!! FIXME: This is really unacceptable; state should be reset and
1317      * !!! FIXME:  the RWops should be pointed to the start of the data
1318      * !!! FIXME:  to decode. The below kludge adds unneeded overhead and
1319      * !!! FIXME:  risk of failure.
1320      */
1321     BAIL_IF_MACRO(SDL_RWseek(internal->rw, 0, SEEK_SET) != 0, ERR_IO_ERROR, 0);
1322     SHN_close(sample);
1323     return(SHN_open(sample, "SHN"));
1324 #endif
1325 } /* SHN_rewind */
1326 
1327 
SHN_seek(Sound_Sample * sample,Uint32 ms)1328 static int SHN_seek(Sound_Sample *sample, Uint32 ms)
1329 {
1330     /*
1331      * (This CAN be done for SHNs that have a seek table at the end of the
1332      *  stream, btw.)
1333      */
1334     BAIL_MACRO("SHN: Seeking not implemented", 0);
1335 } /* SHN_seek */
1336 
1337 
1338 #endif  /* defined SOUND_SUPPORTS_SHN */
1339 
1340 /* end of shn.c ... */
1341 
1342