comparison decoders/shn.c @ 101:c08794028df4

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