src/codecs/load_voc.c
author Ozkan Sezer
Wed, 18 Dec 2019 15:51:40 +0300
changeset 1093 6f0bea2ab267
parent 999 1a87fe70802d
child 1129 888c7be704ce
permissions -rw-r--r--
load_voc.c, music_flac.c: fix implicit-fallthrough warnings.
     1 /*
     2   SDL_mixer:  An audio mixer library based on the SDL library
     3   Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 
    21   This is the source needed to decode a Creative Labs VOC file into a
    22   waveform. It's pretty straightforward once you get going. The only
    23   externally-callable function is Mix_LoadVOC_RW(), which is meant to
    24   act as identically to SDL_LoadWAV_RW() as possible.
    25 
    26   This file by Ryan C. Gordon (icculus@icculus.org).
    27 
    28   Heavily borrowed from sox v12.17.1's voc.c.
    29         (http://www.freshmeat.net/projects/sox/)
    30 */
    31 
    32 #include "SDL_mixer.h"
    33 #include "load_voc.h"
    34 
    35 /* Private data for VOC file */
    36 typedef struct vocstuff {
    37     Uint32  rest;           /* bytes remaining in current block */
    38     Uint32  rate;           /* rate code (byte) of this chunk */
    39     int     silent;         /* sound or silence? */
    40     Uint32  srate;          /* rate code (byte) of silence */
    41     Uint32  blockseek;      /* start of current output block */
    42     Uint32  samples;        /* number of samples output */
    43     Uint32  size;           /* word length of data */
    44     Uint8   channels;       /* number of sound channels */
    45     int     has_extended;   /* Has an extended block been read? */
    46 } vs_t;
    47 
    48 /* Size field */
    49 /* SJB: note that the 1st 3 are sometimes used as sizeof(type) */
    50 #define ST_SIZE_BYTE    1
    51 #define ST_SIZE_8BIT    1
    52 #define ST_SIZE_WORD    2
    53 #define ST_SIZE_16BIT   2
    54 #define ST_SIZE_DWORD   4
    55 #define ST_SIZE_32BIT   4
    56 #define ST_SIZE_FLOAT   5
    57 #define ST_SIZE_DOUBLE  6
    58 #define ST_SIZE_IEEE    7   /* IEEE 80-bit floats. */
    59 
    60 /* Style field */
    61 #define ST_ENCODING_UNSIGNED    1 /* unsigned linear: Sound Blaster */
    62 #define ST_ENCODING_SIGN2       2 /* signed linear 2's comp: Mac */
    63 #define ST_ENCODING_ULAW        3 /* U-law signed logs: US telephony, SPARC */
    64 #define ST_ENCODING_ALAW        4 /* A-law signed logs: non-US telephony */
    65 #define ST_ENCODING_ADPCM       5 /* Compressed PCM */
    66 #define ST_ENCODING_IMA_ADPCM   6 /* Compressed PCM */
    67 #define ST_ENCODING_GSM         7 /* GSM 6.10 33-byte frame lossy compression */
    68 
    69 #define VOC_TERM        0
    70 #define VOC_DATA        1
    71 #define VOC_CONT        2
    72 #define VOC_SILENCE     3
    73 #define VOC_MARKER      4
    74 #define VOC_TEXT        5
    75 #define VOC_LOOP        6
    76 #define VOC_LOOPEND     7
    77 #define VOC_EXTENDED    8
    78 #define VOC_DATA_16     9
    79 
    80 #define VOC_BAD_RATE  ~((Uint32)0)
    81 
    82 
    83 static int voc_check_header(SDL_RWops *src)
    84 {
    85     /* VOC magic header */
    86     Uint8  signature[20];  /* "Creative Voice File\032" */
    87     Uint16 datablockofs;
    88 
    89     SDL_RWseek(src, 0, RW_SEEK_SET);
    90 
    91     if (SDL_RWread(src, signature, sizeof (signature), 1) != 1)
    92         return(0);
    93 
    94     if (SDL_memcmp(signature, "Creative Voice File\032", sizeof (signature)) != 0) {
    95         SDL_SetError("Unrecognized file type (not VOC)");
    96         return(0);
    97     }
    98 
    99         /* get the offset where the first datablock is located */
   100     if (SDL_RWread(src, &datablockofs, sizeof (Uint16), 1) != 1)
   101         return(0);
   102 
   103     datablockofs = SDL_SwapLE16(datablockofs);
   104 
   105     if (SDL_RWseek(src, datablockofs, RW_SEEK_SET) != datablockofs)
   106         return(0);
   107 
   108     return(1);  /* success! */
   109 } /* voc_check_header */
   110 
   111 
   112 /* Read next block header, save info, leave position at start of data */
   113 static int voc_get_block(SDL_RWops *src, vs_t *v, SDL_AudioSpec *spec)
   114 {
   115     Uint8 bits24[3];
   116     Uint8 uc, block;
   117     Uint32 sblen;
   118     Uint16 new_rate_short;
   119     Uint32 new_rate_long;
   120     Uint8 trash[6];
   121     Uint16 period;
   122     unsigned int i;
   123 
   124     v->silent = 0;
   125     while (v->rest == 0)
   126     {
   127         if (SDL_RWread(src, &block, sizeof (block), 1) != 1)
   128             return 1;  /* assume that's the end of the file. */
   129 
   130         if (block == VOC_TERM)
   131             return 1;
   132 
   133         if (SDL_RWread(src, bits24, sizeof (bits24), 1) != 1)
   134             return 1;  /* assume that's the end of the file. */
   135 
   136         /* Size is an 24-bit value. Ugh. */
   137         sblen = (Uint32)((bits24[0]) | (bits24[1] << 8) | (bits24[2] << 16));
   138 
   139         switch(block)
   140         {
   141             case VOC_DATA:
   142                 if (SDL_RWread(src, &uc, sizeof (uc), 1) != 1)
   143                     return 0;
   144 
   145                 /* When DATA block preceeded by an EXTENDED     */
   146                 /* block, the DATA blocks rate value is invalid */
   147                 if (!v->has_extended)
   148                 {
   149                     if (uc == 0)
   150                     {
   151                         SDL_SetError("VOC Sample rate is zero?");
   152                         return 0;
   153                     }
   154 
   155                     if ((v->rate != VOC_BAD_RATE) && (uc != v->rate))
   156                     {
   157                         SDL_SetError("VOC sample rate codes differ");
   158                         return 0;
   159                     }
   160 
   161                     v->rate = uc;
   162                     spec->freq = (Uint16)(1000000.0/(256 - v->rate));
   163                     v->channels = 1;
   164                 }
   165 
   166                 if (SDL_RWread(src, &uc, sizeof (uc), 1) != 1)
   167                     return 0;
   168 
   169                 if (uc != 0)
   170                 {
   171                     SDL_SetError("VOC decoder only interprets 8-bit data");
   172                     return 0;
   173                 }
   174 
   175                 v->has_extended = 0;
   176                 v->rest = sblen - 2;
   177                 v->size = ST_SIZE_BYTE;
   178                 return 1;
   179 
   180             case VOC_DATA_16:
   181                 if (SDL_RWread(src, &new_rate_long, sizeof (new_rate_long), 1) != 1)
   182                     return 0;
   183                 new_rate_long = SDL_SwapLE32(new_rate_long);
   184                 if (new_rate_long == 0)
   185                 {
   186                     SDL_SetError("VOC Sample rate is zero?");
   187                     return 0;
   188                 }
   189                 if ((v->rate != VOC_BAD_RATE) && (new_rate_long != v->rate))
   190                 {
   191                     SDL_SetError("VOC sample rate codes differ");
   192                     return 0;
   193                 }
   194                 v->rate = new_rate_long;
   195                 spec->freq = (int)new_rate_long;
   196 
   197                 if (SDL_RWread(src, &uc, sizeof (uc), 1) != 1)
   198                     return 0;
   199 
   200                 switch (uc)
   201                 {
   202                     case 8:  v->size = ST_SIZE_BYTE; break;
   203                     case 16: v->size = ST_SIZE_WORD; break;
   204                     default:
   205                         SDL_SetError("VOC with unknown data size");
   206                         return 0;
   207                 }
   208 
   209                 if (SDL_RWread(src, &v->channels, sizeof (Uint8), 1) != 1)
   210                     return 0;
   211 
   212                 if (SDL_RWread(src, trash, sizeof (Uint8), 6) != 6)
   213                     return 0;
   214 
   215                 v->rest = sblen - 12;
   216                 return 1;
   217 
   218             case VOC_CONT:
   219                 v->rest = sblen;
   220                 return 1;
   221 
   222             case VOC_SILENCE:
   223                 if (SDL_RWread(src, &period, sizeof (period), 1) != 1)
   224                     return 0;
   225                 period = SDL_SwapLE16(period);
   226 
   227                 if (SDL_RWread(src, &uc, sizeof (uc), 1) != 1)
   228                     return 0;
   229                 if (uc == 0)
   230                 {
   231                     SDL_SetError("VOC silence sample rate is zero");
   232                     return 0;
   233                 }
   234 
   235                 /*
   236                  * Some silence-packed files have gratuitously
   237                  * different sample rate codes in silence.
   238                  * Adjust period.
   239                  */
   240                 if ((v->rate != VOC_BAD_RATE) && (uc != v->rate))
   241                     period = (Uint16)((period * (256 - uc))/(256 - v->rate));
   242                 else
   243                     v->rate = uc;
   244                 v->rest = period;
   245                 v->silent = 1;
   246                 return 1;
   247 
   248             case VOC_LOOP:
   249             case VOC_LOOPEND:
   250                 for(i = 0; i < sblen; i++)   /* skip repeat loops. */
   251                 {
   252                     if (SDL_RWread(src, trash, sizeof (Uint8), 1) != 1)
   253                         return 0;
   254                 }
   255                 break;
   256 
   257             case VOC_EXTENDED:
   258                 /* An Extended block is followed by a data block */
   259                 /* Set this byte so we know to use the rate      */
   260                 /* value from the extended block and not the     */
   261                 /* data block.                     */
   262                 v->has_extended = 1;
   263                 if (SDL_RWread(src, &new_rate_short, sizeof (new_rate_short), 1) != 1)
   264                     return 0;
   265                 new_rate_short = SDL_SwapLE16(new_rate_short);
   266                 if (new_rate_short == 0)
   267                 {
   268                    SDL_SetError("VOC sample rate is zero");
   269                    return 0;
   270                 }
   271                 if ((v->rate != VOC_BAD_RATE) && (new_rate_short != v->rate))
   272                 {
   273                    SDL_SetError("VOC sample rate codes differ");
   274                    return 0;
   275                 }
   276                 v->rate = new_rate_short;
   277 
   278                 if (SDL_RWread(src, &uc, sizeof (uc), 1) != 1)
   279                     return 0;
   280 
   281                 if (uc != 0)
   282                 {
   283                     SDL_SetError("VOC decoder only interprets 8-bit data");
   284                     return 0;
   285                 }
   286 
   287                 if (SDL_RWread(src, &uc, sizeof (uc), 1) != 1)
   288                     return 0;
   289 
   290                 if (uc)
   291                     spec->channels = 2;  /* Stereo */
   292                 /* Needed number of channels before finishing
   293                    compute for rate */
   294                 spec->freq = (256000000L/(65536L - v->rate))/spec->channels;
   295                 /* An extended block must be followed by a data */
   296                 /* block to be valid so loop back to top so it  */
   297                 /* can be grabed.                */
   298                 continue;
   299 
   300             case VOC_MARKER:
   301                 if (SDL_RWread(src, trash, sizeof (Uint8), 2) != 2)
   302                     return 0;
   303 
   304                 /* fallthrough */
   305 
   306             default:  /* text block or other krapola. */
   307                 for(i = 0; i < sblen; i++)
   308                 {
   309                     if (SDL_RWread(src, &trash, sizeof (Uint8), 1) != 1)
   310                         return 0;
   311                 }
   312 
   313                 if (block == VOC_TEXT)
   314                     continue;    /* get next block */
   315         }
   316     }
   317 
   318     return 1;
   319 }
   320 
   321 
   322 static Uint32 voc_read(SDL_RWops *src, vs_t *v, Uint8 *buf, SDL_AudioSpec *spec)
   323 {
   324     Uint32 done = 0;
   325     Uint8 silence = 0x80;
   326 
   327     if (v->rest == 0)
   328     {
   329         if (!voc_get_block(src, v, spec))
   330             return 0;
   331     }
   332 
   333     if (v->rest == 0)
   334         return 0;
   335 
   336     if (v->silent)
   337     {
   338         if (v->size == ST_SIZE_WORD)
   339             silence = 0x00;
   340 
   341         /* Fill in silence */
   342         SDL_memset(buf, silence, v->rest);
   343         done = v->rest;
   344         v->rest = 0;
   345     }
   346 
   347     else
   348     {
   349         done = (Uint32)SDL_RWread(src, buf, 1, v->rest);
   350         v->rest -= done;
   351         if (v->size == ST_SIZE_WORD)
   352         {
   353             #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
   354             Uint16 *samples = (Uint16 *)buf;
   355             for (; v->rest > 0; v->rest -= 2)
   356             {
   357                 *samples = SDL_SwapLE16(*samples);
   358                 samples++;
   359             }
   360             #endif
   361             done >>= 1;
   362         }
   363     }
   364 
   365     return done;
   366 } /* voc_read */
   367 
   368 
   369 /* don't call this directly; use Mix_LoadWAV_RW() for now. */
   370 SDL_AudioSpec *Mix_LoadVOC_RW (SDL_RWops *src, int freesrc,
   371         SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
   372 {
   373     vs_t v;
   374     int was_error = 1;
   375     int samplesize;
   376     Uint8 *fillptr;
   377     void *ptr;
   378 
   379     if ((!src) || (!audio_buf) || (!audio_len))   /* sanity checks. */
   380         goto done;
   381 
   382     if (!voc_check_header(src))
   383         goto done;
   384 
   385     v.rate = VOC_BAD_RATE;
   386     v.rest = 0;
   387     v.has_extended = 0;
   388     *audio_buf = NULL;
   389     *audio_len = 0;
   390     SDL_memset(spec, '\0', sizeof (SDL_AudioSpec));
   391 
   392     if (!voc_get_block(src, &v, spec))
   393         goto done;
   394 
   395     if (v.rate == VOC_BAD_RATE) {
   396         SDL_SetError("VOC data had no sound!");
   397         goto done;
   398     }
   399 
   400     spec->format = ((v.size == ST_SIZE_WORD) ? AUDIO_S16 : AUDIO_U8);
   401     if (spec->channels == 0)
   402         spec->channels = v.channels;
   403 
   404     *audio_len = v.rest;
   405     *audio_buf = SDL_malloc(v.rest);
   406     if (*audio_buf == NULL)
   407         goto done;
   408 
   409     fillptr = *audio_buf;
   410 
   411     while (voc_read(src, &v, fillptr, spec))
   412     {
   413         if (!voc_get_block(src, &v, spec))
   414             goto done;
   415 
   416         *audio_len += v.rest;
   417         ptr = SDL_realloc(*audio_buf, *audio_len);
   418         if (ptr == NULL)
   419         {
   420             SDL_free(*audio_buf);
   421             *audio_buf = NULL;
   422             *audio_len = 0;
   423             goto done;
   424         }
   425 
   426         *audio_buf = ptr;
   427         fillptr = ((Uint8 *) ptr) + (*audio_len - v.rest);
   428     }
   429 
   430     spec->samples = (Uint16)(*audio_len / v.size);
   431 
   432     was_error = 0;  /* success, baby! */
   433 
   434     /* Don't return a buffer that isn't a multiple of samplesize */
   435     samplesize = ((spec->format & 0xFF)/8)*spec->channels;
   436     *audio_len &= (Uint32) ~(samplesize-1);
   437 
   438 done:
   439     if (freesrc && src) {
   440         SDL_RWclose(src);
   441     }
   442 
   443     if (was_error) {
   444         spec = NULL;
   445     }
   446 
   447     return(spec);
   448 } /* Mix_LoadVOC_RW */
   449 
   450 /* end of load_voc.c ... */
   451 
   452 /* vi: set ts=4 sw=4 expandtab: */