Guest User

Untitled

a guest
Jun 22nd, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.71 KB | None | 0 0
  1.  /*
  2.  ** Intel/DVI ADPCM coder/decoder.
  3.  **
  4.  ** The algorithm for this coder was taken from the IMA Compatability Project
  5.  ** proceedings, Vol 2, Number 2; May 1992.
  6.  **
  7.  ** Version 1.1, 16-Dec-92.
  8.  **
  9.  ** Change log:
  10.  ** - Fixed a stupid bug, where the delta was computed as
  11.  **   stepsize*code/4 in stead of stepsize*(code+0.5)/4. The old behavior can
  12.  **   still be gotten by defining STUPID_V1_BUG.
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <alloc.h>
  18. #include "adpcm.h"
  19.  
  20. #define INMAX   16384
  21. #define OUTMAX  INMAX
  22. #define MINVAL  -32768
  23. #define MAXVAL  32767
  24.  
  25.  
  26.  
  27. /* Intel ADPCM step variation table */
  28. const int  indexTable[16] = {
  29.  -1, -1, -1, -1, 2, 4, 6, 8,
  30.  -1, -1, -1, -1, 2, 4, 6, 8,
  31. };
  32.  
  33. const int  stepsizeTable[89] = {
  34.     7,  8,  9,  10, 11,     12,     13,     14,
  35.     16,     17, 19,     21,     23,     25,     28,     31,
  36.     34,     37,     41,     45, 50,     55,     60, 66,
  37.     73,     80,     88,     97,     107,    118,    130,    143,
  38.     157,    173,    190,    209,    230,    253,    279,    307,
  39.     337,    371,    408,    449,    494,    544,    598,    658,
  40.     724,    796,    876,    963,    1060,   1166,   1282,   1411,
  41.     1552,   1707,   1878,   2066,   2272,   2499,   2749,   3024,
  42.     3327,   3660,   4026,   4428,   4871,   5358,   5894,   6484,
  43.     7132,   7845,   8630,   9493,   10442,  11487,  12635,  13899,
  44.     15289,  16818,  18500,  20350,  22385,  24623,  27086,  29794,
  45.     32767
  46. };
  47.  
  48. void adpcm_coder(   int  *inp,  unsigned char *outp,
  49.                     int  len,   struct adpcm_state *state)
  50. {
  51. int  val;                    /* Current input sample value */
  52. int  sign;                   /* Current adpcm sign bit */
  53. int  delta;                  /* Current adpcm output value */
  54. int  step;                   /* Stepsize */
  55. int  valprev;                /* virtual previous output value */
  56. int  vpdiff;                 /* Current change to valprev */
  57. int  index;                  /* Current step change index */
  58. int  outputbuffer = 0;       /* place to keep previous 4-bit value */
  59. // this variable is now part of the structure definition!!! in the h file
  60. // static int  bufferstep;      /* toggle between outputbuffer/output */
  61.  
  62.      valprev = state->valprev;
  63.      index = state->index;
  64.      step = stepsizeTable[index];
  65.      state->bufferstep = 1;
  66.      while (--len >= 0) {
  67.     val = ((*inp++) / 4);
  68.  
  69.    /* Step 1 - compute difference with previous value */
  70.          delta = val - valprev;
  71.          sign = (delta < 0) ? 8 : 0;
  72.          if ( sign ) delta = (-delta);
  73.  
  74.    /* Step 2 - Divide and clamp */
  75.          delta = (delta<<2) / step;
  76.          if ( delta > 7 ) delta = 7;
  77.          vpdiff = ((delta*step) >> 2) + (step >> 3);
  78.  
  79.    /* Step 3 - Update previous value */
  80.          if ( sign )
  81.            valprev = ((long)(valprev-vpdiff)<MINVAL) ? MINVAL : (valprev-vpdiff);
  82.          else
  83.            valprev = ((long)(valprev+vpdiff)>MAXVAL) ? MAXVAL : (valprev+vpdiff);
  84.  
  85.    /* Step 4 - Clamp previous value to 16 bits */
  86.  //      if ( valprev > 32767 )
  87.  //        valprev = 32767;
  88.  //      else if ( valprev < -32768 )
  89.  //        valprev = -32768;
  90.  
  91.    /* Step 5 - Assemble value, update index and step values */
  92.          delta |= sign;
  93.          index += indexTable[delta];
  94.          if ( index < 0 ) index = 0;
  95.          if ( index > 88 ) index = 88;
  96.          step = stepsizeTable[index];
  97.  
  98.    /* Step 6 - Output value */
  99.          if ( state->bufferstep ) {
  100.              outputbuffer = (delta << 4) & 0xf0;
  101.          } else {
  102.              *outp++ = (delta & 0x0f) | outputbuffer;
  103.          }
  104.          state->bufferstep = !state->bufferstep;
  105.      }
  106.  
  107.    /* Output last step, if needed */
  108.      if ( !state->bufferstep )
  109.        *outp++ = outputbuffer;
  110.      state->valprev = valprev;
  111.      state->index = index;
  112.  }
  113.  
  114.  
  115. void main (void)
  116. {
  117. FILE            *fIn, *fOut;
  118. int             iLoop, iLength;
  119. char            sFileIn[80], sFileOut[80];
  120. int             iSample, *iptrSample;
  121. unsigned char       cSample, *cptrSample;
  122. struct          adpcm_state state;
  123. int             *iRawInBuffer, *iptrRaw;
  124. unsigned char       *cAdpcmOutBuffer, *cptrADPCM;
  125.  
  126.  
  127.   state.index = 0;
  128.   state.valprev = 0;
  129.   state.bufferstep = 0;
  130.  
  131. // memory allocation for input and output buffers
  132.   printf("Allocating memory for the input data\n");
  133.   if((iRawInBuffer =(int  *) calloc (INMAX, sizeof(int))) == NULL)
  134.     {printf("Allocation of memory failed for input buffer\n");exit(1);}
  135.   printf("Allocating memory for the output data\n");
  136.   if((cAdpcmOutBuffer =(char *) calloc (OUTMAX, sizeof(char))) == NULL)
  137.     {printf("Allocation of memory failed for output buffer\n");exit(1);}
  138.  
  139. // initilaise the memory to a known value (CHECKING ITS USED!!!!)
  140.   printf("initialising the memory to zero\n");
  141.   iptrRaw   = iRawInBuffer;
  142.   cptrADPCM = cAdpcmOutBuffer;
  143.   for(iLoop=0; iLoop<INMAX; iLoop++)  *iptrRaw++ = 0;
  144.   for(iLoop=0; iLoop<OUTMAX; iLoop++) *cptrADPCM++ = '\0';
  145.  
  146. // filename to be loaded + output filename
  147.   for(iLoop=0;iLoop<80;iLoop++)
  148.     {sFileIn[iLoop] = '\0'; sFileOut[iLoop] = '\0'; }
  149.   printf("Enter the raw audio data filename : ");
  150.   scanf("%s", &sFileIn);
  151.   printf("\n\tFilename entered was [%s]\n", sFileIn);
  152.   if ((fIn = fopen(sFileIn, "rb")) == NULL)
  153.     {printf("Input file not opened\n"); exit(1);}
  154.   printf("Enter the output filename :");
  155.   scanf("%s", &sFileOut);
  156.   printf("Encoded output filename is  [%s]\n", sFileOut);
  157.   if ((fOut = fopen(sFileOut, "wb")) == NULL)
  158.     {printf("output file not opened\n");exit(1);}
  159.  
  160. // main encoding calls in here
  161.   printf("Starting coding routine\n");
  162.   iptrRaw   = iRawInBuffer;
  163.   cptrADPCM = cAdpcmOutBuffer;
  164.   fread( iptrRaw, sizeof(int), INMAX, fIn);
  165.   printf("Read in the data file (%d elements) and stored\n", INMAX);
  166.  
  167. // perform conversion to ADPCM
  168.   adpcm_coder( iRawInBuffer, cAdpcmOutBuffer, INMAX, &state);
  169.   printf("Performed conversion\n");
  170.  
  171. //write out the data block from memory
  172.   fwrite( cptrADPCM, sizeof(char), INMAX, fOut);
  173.   printf("written the encoded file out\n");
  174.  
  175.   fclose(fIn);
  176.   fclose(fOut);
  177.  
  178.  
  179.   free( iRawInBuffer );
  180.   free( cAdpcmOutBuffer );
  181.   printf("Deallocated dynamic memory\n");
  182. }
Advertisement
Add Comment
Please, Sign In to add comment