home *** CD-ROM | disk | FTP | other *** search
/ Dream 44 / Amiga_Dream_44.iso / Linux / Apps / xanim.tgz / xanim / xanim27064 / xa_acodec.gsm.c < prev    next >
C/C++ Source or Header  |  1997-01-26  |  74KB  |  2,440 lines

  1.  
  2. /*
  3.  * xa_acodec.c
  4.  *
  5.  * Copyright (C) 1994,1995,1996 by Mark Podlipec.
  6.  * All rights reserved.
  7.  *
  8.  * This software may be freely copied, modified and redistributed without
  9.  * fee for non-commerical purposes provided that this copyright notice is
  10.  * preserved intact on all copies and modified copies.
  11.  *
  12.  * There is no warranty or other guarantee of fitness of this software.
  13.  * It is provided solely "as is". The author(s) disclaim(s) all
  14.  * responsibility and liability with respect to this software's usage
  15.  * or its effect upon hardware or computer systems.
  16.  *
  17.  */
  18.  
  19.  
  20. /* This file contains Audio Codecs and the routines to add audio
  21.  * into the XAnim structures.  The Hardware specific audio routines
  22.  * for the various platforms is somewhere else.
  23.  */
  24.  
  25. #include "xa_audio.h"
  26.  
  27. /* external */
  28. XA_SND *XA_Audio_Next_Snd();
  29. extern xaULONG xa_kludge2_dvi;
  30.  
  31. /* externals to make internal */
  32. extern xaUBYTE xa_sign_2_ulaw[];
  33. extern xaULONG xa_ulaw_2_sign[];
  34. extern xaULONG xa_arm_2_signed[];
  35. extern void Gen_Ulaw_2_Signed();
  36. extern void Gen_Arm_2_Signed();
  37.  
  38. /* extern xaULONG  xa_kludge900_aud; */
  39.  
  40. /* Internal */
  41. xaULONG XA_ADecode_1M_1M();
  42. xaULONG XA_ADecode_PCMXM_PCM1M();
  43. xaULONG XA_ADecode_PCM1M_PCM2M();
  44. xaULONG XA_ADecode_PCM1S_PCMxM();
  45. xaULONG XA_ADecode_PCM2X_PCM2M();
  46. xaULONG XA_ADecode_NOP_PCMXM();
  47. xaULONG XA_ADecode_ULAWx_PCM2M();
  48. xaULONG XA_ADecode_ARMLAWx_PCM2M();
  49. xaULONG XA_ADecode_ADPCMS_PCM2M();
  50. xaULONG XA_ADecode_ADPCMM_PCM2M();
  51. xaULONG XA_ADecode_DVIM_PCMxM();
  52. xaULONG XA_ADecode_DVIS_PCMxM();
  53. xaULONG XA_ADecode_IMA4M_PCMxM();
  54. xaULONG XA_ADecode_IMA4S_PCMxM();
  55. xaULONG XA_ADecode_GSMM_PCMxM();
  56.  
  57. xaULONG XA_IPC_Sound();
  58. xaULONG XA_Add_Sound();
  59. extern xaULONG xa_vaudio_hard_buff;
  60. extern xaULONG xa_audio_hard_type;
  61.  
  62.  
  63. /***************************************************************************
  64.  * Below is an Empty Audio Decode Routine. It's mainly for my benefit
  65.  * to cut and paste for new ones.
  66.  */ 
  67.  
  68. #ifdef XA_NEVER_DEFINED_32
  69. xaULONG XA_ADecode_XXX_XXX(snd_hdr,obuf,ocnt,buff_size)
  70. XA_SND *snd_hdr;
  71. xaUBYTE *obuf;
  72. xaULONG ocnt,buff_size;
  73. { XA_ADECODE_DECLARE_LOCAL; 
  74.   if (snd_hdr==0) return(ocnt);
  75.   XA_ADECODE_INIT_LOCAL;    /* Init Local Variables */
  76.   /* Optionally Init Codec Specific Structure Here */ 
  77.   while(ocnt < buff_size)
  78.   {
  79.     if (inc_cnt < (1<<24))
  80.     {    
  81.     /*** Decode Sample and increment byte_cnt ***/
  82.       byte_cnt++;
  83.  
  84.       samp_cnt--;
  85.       inc_cnt += inc;
  86.     }
  87.     while(inc_cnt >= (1<<24))
  88.     { 
  89.     /*** Output Sample ***/
  90.       ocnt++;
  91.       inc_cnt -= (1<<24);    
  92.       if (ocnt >= buff_size) break;
  93.     }
  94.     if (samp_cnt <= 0)        { XA_ADECODE_MOVE_ON; }
  95.   }
  96.   XA_ADECODE_SAVE_LOCAL;     /* save local variables */
  97.   /* Optionally Save Codec Specific Structure Here */
  98.   return(ocnt);
  99. }
  100. #endif
  101.  
  102.  
  103. /*****************************************************************************/
  104.  
  105. #define XA_ADECODE_DECLARE_LOCAL xaUBYTE *ibuf; \
  106. xaULONG byte_cnt, inc, inc_cnt, spec, samp_cnt; xaLONG dataL, dataR
  107.  
  108. #define XA_ADECODE_INIT_LOCAL         \
  109. { spec        = snd_hdr->spec;    \
  110.   inc        = snd_hdr->inc;        \
  111.   inc_cnt    = snd_hdr->inc_cnt;    \
  112.   byte_cnt    = snd_hdr->byte_cnt;    \
  113.   samp_cnt    = snd_hdr->samp_cnt;    \
  114.   dataL        = snd_hdr->dataL;    \
  115.   dataR        = snd_hdr->dataR;    \
  116.   ibuf        = snd_hdr->snd;        \
  117.   ibuf += byte_cnt;             }
  118.  
  119. #define XA_ADECODE_MOVE_ON     {            \
  120. if ( (snd_hdr = XA_Audio_Next_Snd(snd_hdr)) != 0)    \
  121. { snd_hdr->inc_cnt    = inc_cnt;            \
  122.   snd_hdr->dataL    = dataL;            \
  123.   snd_hdr->dataR    = dataL;            \
  124.   ocnt = snd_hdr->delta(snd_hdr,obuf,ocnt,buff_size);    \
  125. }                            \
  126. return(ocnt);                        }        
  127.  
  128. #define XA_ADECODE_SAVE_LOCAL    \
  129. { snd_hdr->inc_cnt    = inc_cnt;    \
  130.   snd_hdr->byte_cnt    = byte_cnt;    \
  131.   snd_hdr->samp_cnt    = samp_cnt;    \
  132.   snd_hdr->dataL    = dataL;    \
  133.   snd_hdr->dataR    = dataL;    }
  134.  
  135. /********** XA_ADecode_1M_M *********************************
  136.  * No conversion. simply pass along.
  137.  *******************************************/
  138. xaULONG XA_ADecode_1M_1M(snd_hdr,obuf,ocnt,buff_size)
  139. XA_SND *snd_hdr;
  140. xaUBYTE *obuf;
  141. xaULONG ocnt,buff_size;
  142. { XA_ADECODE_DECLARE_LOCAL; 
  143.   if (snd_hdr==0) return(ocnt);
  144.   XA_ADECODE_INIT_LOCAL;    /* Init Local Variables */
  145.   while(ocnt < buff_size)
  146.   {
  147.     if (inc_cnt < (1<<24))
  148.     { dataL = dataR = (xaULONG)*ibuf++;        byte_cnt++;
  149.       samp_cnt--;
  150.       inc_cnt += inc;
  151.     }
  152.     while(inc_cnt >= (1<<24))
  153.     { *obuf++ = dataL;
  154.       ocnt++;
  155.       inc_cnt -= (1<<24);    
  156.       if (ocnt >= buff_size) break;
  157.     }
  158.     if (samp_cnt <= 0)        { XA_ADECODE_MOVE_ON; }
  159.   }
  160.   XA_ADECODE_SAVE_LOCAL;     /* save local variables */
  161.   return(ocnt);
  162. }
  163.  
  164. /********** XA_ADecode_PCMXM_PCM1M *********************************
  165.  * Convert PCM 1+2 BPS Mono Samples into PCM 1 BPS Mono Samples
  166.  * The order flag takes care of the various linear/signed/endian
  167.  * conversions
  168.  *  Input        order 1st  2nd
  169.  *  -----------------------------------------
  170.  *  Linear1M      0   D     -
  171.  *  Signed1M      1   D^80  -
  172.  *  Linear2MBig   2   D     skip
  173.  *  Signed2MBig   3   D^80  skip
  174.  *  Linear2MLit   4   skip  D
  175.  *  Signed2MLit   5   skip  D^80
  176.  *
  177.  *  bit 3 (& 0x08) AU output instead of PCM.
  178.  *
  179.  * Global Variables:
  180.  *   XA_Audio_Next_Snd()    routine to move to next sound header.
  181.  ***************************************************************/
  182. xaULONG XA_ADecode_PCMXM_PCM1M(snd_hdr,obuf,ocnt,buff_size)
  183. XA_SND *snd_hdr;
  184. xaUBYTE *obuf;
  185. xaULONG ocnt,buff_size;
  186. { XA_ADECODE_DECLARE_LOCAL;    xaULONG bps;
  187.   if (snd_hdr==0) return(ocnt);
  188.   XA_ADECODE_INIT_LOCAL; 
  189.   bps        = ((spec & 2) | (spec & 4))?(2):(1);
  190.  
  191.   while(ocnt < buff_size)
  192.   {
  193.     if (inc_cnt < (1<<24))
  194.     {    /*** Decode Sample ***/
  195.       dataL = (spec & 4)?(ibuf[1]):(*ibuf);    
  196.       ibuf += bps;    byte_cnt += bps;    samp_cnt--;
  197.       inc_cnt += inc;
  198.     }
  199.  
  200.     while(inc_cnt >= (1<<24))
  201.     {
  202.     /*** Output Sample ***/
  203.       if (spec & 8)
  204.       { /* note: ulaw takes signed input */
  205.     if (spec & 1) *obuf++ = xa_sign_2_ulaw[ dataL ];
  206.     else *obuf++ = xa_sign_2_ulaw[ (dataL ^ 0x80) ];
  207.       }
  208.       else *obuf++ = (spec & 1)?(dataL^0x80):(dataL);
  209.  
  210.       ocnt++;
  211.       inc_cnt -= (1<<24);    
  212.       if (ocnt >= buff_size) break;
  213.     }
  214.     if (samp_cnt <= 0)        { XA_ADECODE_MOVE_ON; }
  215.   }
  216.   XA_ADECODE_SAVE_LOCAL;
  217.   return(ocnt);
  218. }
  219.  
  220. /********** XA_ADecode_PCM1M_PCM2M *********************************
  221.  * Convert PCM 1 BPS Mono Samples into PCM 2 BPS Mono Samples
  222.  * The order flag takes care of the various linear/signed/endian
  223.  * conversions
  224.  *  Input        Ouput       Order  1st  2nd
  225.  *  -----------------------------------------
  226.  *  Linear to Linear Big       0    D     D
  227.  *  Linear to Linear Little    0    D     D
  228.  *  Signed to Linear Big       1    D^80  D
  229.  *  Signed to Linear Little    2    D     D^80
  230.  *  Linear to Signed Big       1    D^80  D
  231.  *  Linear to Signed Little    2    D     D^80
  232.  *  Signed to Signed Big       2    D     D^80
  233.  *  Signed to Signed Little    1    D^80  D
  234.  *
  235.  * Global Variables:
  236.  *   xaUBYTE xa_sign_2_ulaw[256]    conversion table.
  237.  *   XA_Audio_Next_Snd()    routine to move to next sound header.
  238.  ***************************************************************/
  239. xaULONG XA_ADecode_PCM1M_PCM2M(snd_hdr,obuf,ocnt,buff_size)
  240. XA_SND *snd_hdr;
  241. xaUBYTE *obuf;
  242. xaULONG ocnt,buff_size;
  243. { XA_ADECODE_DECLARE_LOCAL;
  244.   if (snd_hdr==0) return(ocnt);
  245.   XA_ADECODE_INIT_LOCAL;
  246.  
  247.   while(ocnt < buff_size)
  248.   {
  249.     if (inc_cnt < (1<<24))
  250.     {   /*** Decode Sample ***/
  251.       dataL = *ibuf++;
  252.       byte_cnt++;        samp_cnt--;
  253.       inc_cnt += inc;
  254.     }
  255.     while(inc_cnt >= (1<<24))
  256.     {  /*** Output Sample ****/
  257.       if (spec==1) { *obuf++ = dataL ^ 0x80; *obuf++ = dataL; }
  258.       else if (spec==2) { *obuf++ = dataL; *obuf++ = dataL ^ 0x80; }
  259.       else { *obuf++ = dataL; *obuf++ = dataL; }
  260.       ocnt++;
  261.       inc_cnt -= (1<<24);
  262.       if (ocnt >= buff_size) break;
  263.     }
  264.     if (samp_cnt <= 0)    { XA_ADECODE_MOVE_ON; }
  265.   }
  266.   XA_ADECODE_SAVE_LOCAL;
  267.   return(ocnt);
  268. }
  269.  
  270. /********** XA_Audio_PCM1S_PCMxM *********************************
  271.  * Convert PCM 1 BPS Stereo Samples into PCM 2 BPS Mono Samples
  272.  * The order flag takes care of the various linear/signed/endian
  273.  * conversions
  274.  *  Input        Ouput       Order  1st  2nd
  275.  *  -----------------------------------------
  276.  *  Linear to Linear Big       0    D     D
  277.  *  Linear to Linear Little    0    D     D
  278.  *  Signed to Linear Big       1 5  D^80  D
  279.  *  Signed to Linear Little    2 6  D     D^80
  280.  *  Linear to Signed Big       1    D^80  D
  281.  *  Linear to Signed Little    2    D     D^80
  282.  *  Signed to Signed Big       2 6  D     D^80
  283.  *  Signed to Signed Little    1 5  D^80  D
  284.  *
  285.  *  bit 2(0x04) indicates incoming is signed(necessary for proper averaging)
  286.  *  bit 3(0x08) indicates output is single byte
  287.  *  bit 4(0x10) indicates output is ulaw(X to signed to ulaw)
  288.  *
  289.  * Global Variables:
  290.  *   xaUBYTE xa_sign_2_ulaw[256]        conversion table.
  291.  *   XA_Audio_Next_Snd()        routine to move to next sound header.
  292.  ***************************************************************/
  293. xaULONG XA_ADecode_PCM1S_PCMxM(snd_hdr,obuf,ocnt,buff_size)
  294. XA_SND *snd_hdr;
  295. xaUBYTE *obuf;
  296. xaULONG ocnt,buff_size;
  297. { XA_ADECODE_DECLARE_LOCAL; 
  298.   if (snd_hdr==0) return(ocnt);
  299.   XA_ADECODE_INIT_LOCAL;
  300.   while(ocnt < buff_size)
  301.   { if (inc_cnt < (1<<24))
  302.     { /*** Decode Sample and increment byte_cnt ***/
  303.       dataL = *ibuf++;
  304.       dataR = *ibuf++;    
  305.       if (spec & 0x04)    /* Signed Input */
  306.       { if (dataL & 0x80) dataL -= 0x100;
  307.         if (dataR & 0x80) dataR -= 0x100;
  308.       }
  309.       byte_cnt += 2;
  310.       samp_cnt--;
  311.       inc_cnt += inc;
  312.     }
  313.     while(inc_cnt >= (1<<24))
  314.     { xaULONG data = ((dataL + dataR)>>1) & 0xff;
  315.     /*** Output Sample ***/
  316.       if (spec & 0x08) /* Single Byte out */
  317.       { if (spec & 0x10)    
  318.         *obuf++ = xa_sign_2_ulaw[((spec & 0x04)?(data):(data ^ 0x80))];
  319.         else    *obuf++ = (spec & 0x01)?(data ^ 0x80):(data);
  320.       }
  321.       else
  322.       {
  323.     if (spec & 0x01)    { *obuf++ = data ^ 0x80;   *obuf++ = data; }
  324.     else if (spec & 0x02)    { *obuf++ = data;   *obuf++ = data ^ 0x80; }
  325.     else            { *obuf++ = data;        *obuf++ = data; }
  326.       }
  327. /* POD Add uLaw and single byte output */
  328.  
  329.       ocnt++;
  330.       inc_cnt -= (1<<24);    
  331.       if (ocnt >= buff_size) break;
  332.     }
  333.     if (samp_cnt <= 0)        { XA_ADECODE_MOVE_ON; }
  334.   }
  335.   XA_ADECODE_SAVE_LOCAL;
  336.   return(ocnt);
  337. }
  338.  
  339. /********** XA_ADecode_ULAWx_PCM2M *********************************
  340.  * Convert Sun's ULAW Mono/Stereo Samples into PCM 2 BPS Mono Samples
  341.  * The spec flag takes care of the various linear/signed/endian
  342.  * conversions
  343.  *  Input        Ouput      Spec  1st  2nd
  344.  *  -----------------------------------------
  345.  *  ULAW to Signed Big       0    D1    D0
  346.  *  ULAW to Signed Little    1    D0    D1
  347.  *  ULAW to Linear Big       2    D1^80 D0
  348.  *  ULAW to Linear Little    3    D0    D1^80
  349.  *
  350.  *  bit 2 (& 0x04) 1 byte output.
  351.  *  bit 3 (& 0x08) stereo input.
  352.  *
  353.  * Global Variables:
  354.  *   xaUBYTE xa_sign_2_ulaw[256]        conversion table.
  355.  *   XA_Audio_Next_Snd()        routine to move to next sound header.
  356.  ***************************************************************/
  357. xaULONG XA_ADecode_ULAWx_PCM2M(snd_hdr,obuf,ocnt,buff_size)
  358. XA_SND *snd_hdr;
  359. xaUBYTE *obuf;
  360. xaULONG ocnt,buff_size;
  361. { XA_ADECODE_DECLARE_LOCAL; 
  362.   if (snd_hdr==0) return(ocnt);
  363.   XA_ADECODE_INIT_LOCAL;
  364.  
  365.   while(ocnt < buff_size)
  366.   {
  367.     if (inc_cnt < (1<<24))
  368.     { /*** Decode Sample and increment byte_cnt ***/
  369.       dataL = xa_ulaw_2_sign[ (*ibuf++) ];
  370.       if (spec & 0x08)
  371.       { dataR = xa_ulaw_2_sign[ (*ibuf++) ];
  372.     if (dataL & 0x8000) dataL -= 0x10000;
  373.     if (dataR & 0x8000) dataR -= 0x10000;
  374.         byte_cnt += 2;
  375.       } else byte_cnt++;
  376.       samp_cnt--;
  377.       inc_cnt += inc;
  378.     }
  379.     while(inc_cnt >= (1<<24))
  380.     { xaULONG data = (spec & 0x08)?((dataL + dataR)>>1):(dataL);
  381.     /*** Output Sample ***/
  382.       if (spec & 0x04)  /* 1 byte output */
  383.       {  xaUBYTE d0 = ((xaULONG)(data) >> 8) & 0xff;
  384.          if (spec & 0x02)    *obuf++ = d0;
  385.          else             *obuf++ = d0 ^ 0x80;
  386. /* POD Add uLaw output */
  387.       }
  388.       else
  389.       { xaUBYTE d1,d0; d1 = (data>>8) & 0xff; d0 = data & 0xff;
  390.         if (spec & 0x02) d1 ^= 0x80;
  391.         if (spec & 0x01) {*obuf++ = d0; *obuf++ = d1; }
  392.         else {*obuf++ = d1; *obuf++ = d0; }
  393.       }
  394.  
  395.       ocnt++;
  396.       inc_cnt -= (1<<24);    
  397.       if (ocnt >= buff_size) break;
  398.     }
  399.     if (samp_cnt <= 0)        { XA_ADECODE_MOVE_ON; }
  400.   }
  401.   XA_ADECODE_SAVE_LOCAL;
  402.   return(ocnt);
  403. }
  404.  
  405. /********** XA_ADecode_ARMLAWx_PCM2M *********************************
  406.  * Convert ARM Logarithmic Mono/Stereo Samples into PCM 2 BPS Mono Samples
  407.  * The spec flag takes care of the various linear/signed/endian
  408.  * conversions
  409.  *  Input        Ouput      Spec  1st  2nd
  410.  *  -----------------------------------------
  411.  *  ULAW to Signed Big       0    D1    D0
  412.  *  ULAW to Signed Little    1    D0    D1
  413.  *  ULAW to Linear Big       2    D1^80 D0
  414.  *  ULAW to Linear Little    3    D0    D1^80
  415.  *
  416.  *  bit 2 (& 0x04) 1 byte output.
  417.  *  bit 3 (& 0x08) stereo input.
  418.  *
  419.  * Global Variables:
  420.  *   xaUBYTE xa_sign_2_ulaw[256]        conversion table.
  421.  *   XA_Audio_Next_Snd()        routine to move to next sound header.
  422.  ***************************************************************/
  423. xaULONG XA_ADecode_ARMLAWx_PCM2M(snd_hdr,obuf,ocnt,buff_size)
  424. XA_SND *snd_hdr;
  425. xaUBYTE *obuf;
  426. xaULONG ocnt,buff_size;
  427. { XA_ADECODE_DECLARE_LOCAL; 
  428.   if (snd_hdr==0) return(ocnt);
  429.   XA_ADECODE_INIT_LOCAL;
  430.  
  431.   while(ocnt < buff_size)
  432.   {
  433.     if (inc_cnt < (1<<24))
  434.     { /*** Decode Sample and increment byte_cnt ***/
  435.       dataL = xa_arm_2_signed[ (*ibuf++) ];
  436.       if (spec & 0x08)
  437.       { dataR = xa_arm_2_signed[ (*ibuf++) ];
  438.     if (dataL & 0x8000) dataL -= 0x10000;
  439.     if (dataR & 0x8000) dataR -= 0x10000;
  440.         byte_cnt += 2;
  441.       } else byte_cnt++;
  442.       samp_cnt--;
  443.       inc_cnt += inc;
  444.     }
  445.     while(inc_cnt >= (1<<24))
  446.     { xaULONG data = (spec & 0x08)?((dataL + dataR)>>1):(dataL);
  447.     /*** Output Sample ***/
  448.       if (spec & 0x04)  /* 1 byte output */
  449.       {  xaUBYTE d0 = ((xaULONG)(data) >> 8) & 0xff;
  450.          if (spec & 0x02)    *obuf++ = d0;
  451.          else             *obuf++ = d0 ^ 0x80;
  452. /* POD Add uLaw output */
  453.       }
  454.       else
  455.       { xaUBYTE d1,d0; d1 = (data>>8) & 0xff; d0 = data & 0xff;
  456.         if (spec & 0x02) d1 ^= 0x80;
  457.         if (spec & 0x01) {*obuf++ = d0; *obuf++ = d1; }
  458.         else {*obuf++ = d1; *obuf++ = d0; }
  459.       }
  460.  
  461.       ocnt++;
  462.       inc_cnt -= (1<<24);    
  463.       if (ocnt >= buff_size) break;
  464.     }
  465.     if (samp_cnt <= 0)        { XA_ADECODE_MOVE_ON; }
  466.   }
  467.   XA_ADECODE_SAVE_LOCAL;
  468.   return(ocnt);
  469. }
  470.  
  471. /********** XA_ADecode_PCM2X_PCM2M *********************************
  472.  * Convert PCM 2 BPS (mono/stereo little/big endian) Samples
  473.  * into PCM 2 BPS Mono (little/big endian) Samples.
  474.  * The flag "spec' takes care of the various linear/signed/endian/stereo
  475.  * conversions.
  476.  *
  477.  * bit   mask  meaning
  478.  * -----------------------------------------
  479.  * bit 0:  1  src endian  0 = big  1 = little
  480.  * bit 1:  2  linear/signed conversion.  0 = none  1 = ^0x8000
  481.  * bit 2:  4  dst endian  0 = big  1 = little
  482.  * bit 3:  8  src stereo  0 = no   1 = yes
  483.  * bit 4: 10  src signed  0 = no   1 = yes
  484.  *
  485.  * Global Variables:
  486.  *   XA_Audio_Next_Snd()        routine to move to next sound header.
  487.  ***************************************************************/
  488. xaULONG XA_ADecode_PCM2X_PCM2M(snd_hdr,obuf,ocnt,buff_size)
  489. XA_SND *snd_hdr;
  490. xaUBYTE *obuf;
  491. xaULONG ocnt,buff_size;
  492. { XA_ADECODE_DECLARE_LOCAL; 
  493.   if (snd_hdr==0) return(ocnt);
  494.   XA_ADECODE_INIT_LOCAL;
  495.  
  496.   while(ocnt < buff_size)
  497.   {
  498.     if (inc_cnt < (1<<24))
  499.     {    /*** Decode Sample and increment byte_cnt ***/
  500.       if (spec & 1)   /* Little Endian Samples */
  501.       { dataL = *ibuf++;  dataL |= *ibuf++ << 8;
  502.     if (spec & 8)    /* stereo input */
  503.         { dataR = *ibuf++;  dataR |= *ibuf++ << 8; byte_cnt += 4;
  504.       if (spec & 0x10)  /* signed stereo input */
  505.       { if (dataL & 0x8000) dataL -= 0x10000;
  506.         if (dataR & 0x8000) dataR -= 0x10000;
  507.       }
  508.     } else byte_cnt += 2;
  509.       }
  510.       else /* Big Endian Samples */
  511.       { dataL = *ibuf++ << 8;  dataL |= *ibuf++;
  512.     if (spec & 8) /* stereo input */
  513.         { dataR = *ibuf++ << 8;  dataR |= *ibuf++; byte_cnt += 4;
  514.       if (spec & 0x10)  /* signed stereo input */
  515.       { if (dataL & 0x8000) dataL -= 0x10000;
  516.         if (dataR & 0x8000) dataR -= 0x10000;
  517.       }
  518.     } else byte_cnt += 2;
  519.       }
  520.       samp_cnt--;
  521.       inc_cnt += inc;
  522.     }
  523.     while(inc_cnt >= (1<<24))
  524.     { register xaULONG d1,d0 = (spec & 8)?((dataL+dataR)>>1):(dataL);
  525.  
  526.     /*** Output Sample ***/
  527.       if (spec & 2)    d0 ^= 0x8000; /* sign conversion */
  528.  
  529.       if (spec & 4)    d1 = d0 >> 8;
  530.       else        { d1 = d0; d0 >>= 8; }
  531.       *obuf++ = (d0 & 0xff);
  532.       *obuf++ = (d1 & 0xff);
  533.     /* POD add single byte and ulaw output */
  534.       ocnt++;
  535.       inc_cnt -= (1<<24);    
  536.       if (ocnt >= buff_size) break;
  537.     }
  538.     if (samp_cnt <= 0)        { XA_ADECODE_MOVE_ON; }
  539.   }
  540.   XA_ADECODE_SAVE_LOCAL;
  541.   return(ocnt);
  542. }
  543.  
  544. /********** XA_ADecode_PCMXS_PCM1M *********************************
  545.  * Convert PCM 1+2 BPS Stereo Samples into PCM 1 BPS Mono Samples
  546.  * The spec flag takes care of the various linear/signed/endian
  547.  * conversions
  548.  *  Input        flag 1st  2nd    3rd  4th
  549.  *  -----------------------------------------
  550.  *  Linear1S      0   D     D     -    -
  551.  *  Signed1S      1   D     D     -    -      ^80
  552.  *  Linear2SBig   2   D     skip  D    skip
  553.  *  Signed2SBig   3   D     skip  D    skip   ^80
  554.  *  Linear2SLit   4   skip  D     skip D
  555.  *  Signed2SLit   5   skip  D     skip D      ^80
  556.  *
  557.  *  bit 3 (& 0x08) AU output instead of PCM.
  558.  *
  559.  * Global Variables:
  560.  *   XA_Audio_Next_Snd()        routine to move to next sound header.
  561.  ***************************************************************/
  562. xaULONG XA_ADecode_PCMXS_PCM1M(snd_hdr,obuf,ocnt,buff_size)
  563. XA_SND *snd_hdr;
  564. xaUBYTE *obuf;
  565. xaULONG ocnt,buff_size;
  566. { XA_ADECODE_DECLARE_LOCAL; 
  567.   if (snd_hdr==0) return(ocnt);
  568.   XA_ADECODE_INIT_LOCAL;
  569.   while(ocnt < buff_size)
  570.   {
  571.     if (inc_cnt < (1<<24))
  572.     {    /*** Decode Sample and increment byte_cnt ***/
  573.  
  574.       if (spec & 2)      /* only read most sig byte */
  575.     { dataL = *ibuf++; ibuf++; dataR = *ibuf++; ibuf++; byte_cnt += 4; }
  576.       else if (spec & 4) 
  577.     { ibuf++; dataL = *ibuf++; ibuf++; dataR = *ibuf++; byte_cnt += 4; }
  578.       else         { dataL = *ibuf++; dataR = *ibuf++; byte_cnt += 2; }
  579.       if (spec & 1) /* signed input */
  580.       { if (dataL & 0x80) dataL -= 0x100;
  581.         if (dataR & 0x80) dataR -= 0x100;
  582.       }
  583.       samp_cnt--;
  584.       inc_cnt += inc;
  585.     }
  586.     while(inc_cnt >= (1<<24))
  587.     { xaULONG data = ((dataL + dataR)>>1) & 0xff;
  588.       if (spec & 8)
  589.       { /* note: ulaw takes signed input */
  590.     if (spec & 1)    *obuf++ = xa_sign_2_ulaw[ data ];
  591.     else        *obuf++ = xa_sign_2_ulaw[ (data ^ 0x80) ];
  592.       }
  593.       else *obuf++ = (spec & 1)?(data^0x80):(data);
  594.       ocnt++;
  595.       inc_cnt -= (1<<24);    
  596.       if (ocnt >= buff_size) break;
  597.     }
  598.     if (samp_cnt <= 0)        { XA_ADECODE_MOVE_ON; }
  599.   }
  600.   XA_ADECODE_SAVE_LOCAL;
  601.   return(ocnt);
  602. }
  603.  
  604. /* NEW CODECS HERE */
  605.  
  606.  
  607. /********** XA_ADecode_NOP_PCMXM *********************************
  608.  * Convert Silence(16bit Signed PCM silence 0x8000) Samples into various
  609.  * other PCM/uLAW samples.
  610.  * The spec flag takes care of the various linear/signed/endian
  611.  * conversions
  612.  *
  613.  *  Input        Ouput        Spec  1st  2nd
  614.  *  -----------------------------------------
  615.  *  MSADPCM to Signed Big       0    D1    D0
  616.  *  MSADPCM to Signed Little    1    D0    D1
  617.  *  MSADPCM to Linear Big       2    D1^80 D0
  618.  *  MSADPCM to Linear Little    3    D0    D1^80
  619.  *
  620.  *  bit 2 (& 0x04) 1 byte output.
  621.  *  bit 3 (& 0x08) AU output instead of linear PCM.
  622.  *
  623.  * Global Variables:
  624.  *   XA_Audio_Next_Snd()        routine to move to next sound header.
  625.  ***************************************************************/
  626. xaULONG XA_ADecode_NOP_PCMXM(snd_hdr,obuf,ocnt,buff_size)
  627. XA_SND *snd_hdr;
  628. xaUBYTE *obuf;
  629. xaULONG ocnt,buff_size;
  630. { XA_ADECODE_DECLARE_LOCAL; 
  631.   if (snd_hdr==0) return(ocnt);
  632.   XA_ADECODE_INIT_LOCAL; 
  633.  
  634.   while(ocnt < buff_size)
  635.   {
  636.     if (inc_cnt < (1<<24))
  637.     {    /*** Decode Sample ***/
  638.       byte_cnt++;    samp_cnt--;
  639.       inc_cnt += inc;
  640.     }
  641.  
  642.     while(inc_cnt >= (1<<24))
  643.     { xaULONG data = 0x8000;
  644.     /*** Output Sample ***/
  645.       if (spec & 4)  /* 1 byte output */
  646.       { xaUBYTE d0 = ((xaULONG)(data) >> 8) & 0xff;
  647.         if (spec & 8)    *obuf++ = xa_sign_2_ulaw[ (xaULONG)(d0) ];
  648.     else             *obuf++ = (spec & 2)?(d0 ^ 0x80):(d0);
  649.       }
  650.       else
  651.       { xaUBYTE d1,d0; d1 = (data>>8) & 0xff; d0 = data & 0xff;
  652.         if (spec & 0x02) d1 ^= 0x80;
  653.         if (spec & 0x01) {*obuf++ = d0; *obuf++ = d1; }
  654.         else {*obuf++ = d1; *obuf++ = d0; }
  655.       }
  656.       ocnt++;
  657.       inc_cnt -= (1<<24);    
  658.       if (ocnt >= buff_size) break;
  659.     }
  660.     if (samp_cnt <= 0)        { XA_ADECODE_MOVE_ON; }
  661.   }
  662.   XA_ADECODE_SAVE_LOCAL;
  663.   return(ocnt);
  664. }
  665.  
  666.  
  667. typedef struct 
  668. {
  669.   xaULONG    L_bpred ,R_bpred;
  670.   xaLONG    L_delta ,R_delta;
  671.   xaLONG    L_samp1 ,R_samp1;
  672.   xaLONG    L_samp2 ,R_samp2;
  673.   xaLONG    L_nyb1  ,R_nyb1;
  674.   xaULONG    nyb_flag;
  675.   xaULONG    flag;    
  676.    /* 0 don't output samps, 1 output samp1, 2 output both  4 un-init'd */
  677. } XA_MSADPCM_HDR;
  678.  
  679. XA_MSADPCM_HDR xa_msadpcm; 
  680.  
  681. #define MSADPCM_NUM_COEF        (7)
  682. #define MSADPCM_MAX_CHANNELS    (2)
  683.  
  684. #define MSADPCM_PSCALE          (8)
  685. #define MSADPCM_PSCALE_NUM      (1 << MSADPCM_PSCALE)
  686. #define MSADPCM_CSCALE          (8)
  687. #define MSADPCM_CSCALE_NUM      (1 << MSADPCM_CSCALE)
  688.  
  689. #define MSADPCM_DELTA4_MIN      (16)
  690.  
  691. static xaLONG  gaiP4[] = { 230, 230, 230, 230, 307, 409, 512, 614,
  692.                           768, 614, 512, 409, 307, 230, 230, 230 };
  693. static xaLONG gaiCoef1[] = { 256,  512,  0, 192, 240,  460,  392 };
  694. static xaLONG gaiCoef2[] = {   0, -256,  0,  64,   0, -208, -232 };
  695.  
  696. #define AUD_READ_MSADPCM_HDR(ptr,bpred,delta,samp1,samp2)    \
  697. { bpred= *ptr++;                     \
  698.   delta = *ptr++;    delta |= (*ptr++)<<8;        \
  699.   samp1 = *ptr++;    samp1 |= (*ptr++)<<8;        \
  700.   samp2 = *ptr++;    samp2 |= (*ptr++)<<8;        \
  701.   if (delta & 0x8000)    delta -= 0x10000;    \
  702.   if (samp1 & 0x8000)    samp1 -= 0x10000;    \
  703.   if (samp2 & 0x8000)    samp2 -= 0x10000;    }
  704.  
  705.  
  706. #define AUD_READ_MSADPCM_SHDR(ptr,Lbpred,Ldelta,Lsamp1,Lsamp2,Rbpred,Rdelta,Rsamp1,Rsamp2) \
  707. { Lbpred= *ptr++;    Rbpred= *ptr++;            \
  708.   Ldelta = *ptr++;    Ldelta |= (*ptr++)<<8;        \
  709.   Rdelta = *ptr++;    Rdelta |= (*ptr++)<<8;        \
  710.   Lsamp1 = *ptr++;    Lsamp1 |= (*ptr++)<<8;        \
  711.   Rsamp1 = *ptr++;    Rsamp1 |= (*ptr++)<<8;        \
  712.   Lsamp2 = *ptr++;    Lsamp2 |= (*ptr++)<<8;        \
  713.   Rsamp2 = *ptr++;    Rsamp2 |= (*ptr++)<<8;        \
  714.   if (Ldelta & 0x8000)    Ldelta = Ldelta - 0x10000;    \
  715.   if (Lsamp1 & 0x8000)    Lsamp1 = Lsamp1 - 0x10000;    \
  716.   if (Lsamp2 & 0x8000)    Lsamp2 = Lsamp2 - 0x10000;    \
  717.   if (Rdelta & 0x8000)    Rdelta = Rdelta - 0x10000;    \
  718.   if (Rsamp1 & 0x8000)    Rsamp1 = Rsamp1 - 0x10000;    \
  719.   if (Rsamp2 & 0x8000)    Rsamp2 = Rsamp2 - 0x10000;    }
  720.  
  721.   
  722. #define AUD_CALC_MSADPCM(lsamp,nyb0,delta,samp1,samp2,coef1,coef2) \
  723. { xaLONG predict;                        \
  724.    /** Compute next Adaptive Scale Factor(ASF) */        \
  725.   idelta = delta;                        \
  726.   delta = (gaiP4[nyb0] * idelta) >> MSADPCM_PSCALE;        \
  727.   if (delta < MSADPCM_DELTA4_MIN) delta = MSADPCM_DELTA4_MIN;    \
  728.   if (nyb0 & 0x08) nyb0 = nyb0 - 0x10;                \
  729.    /** Predict next sample */                    \
  730.   predict = ((samp1 * coef1) + (samp2 * coef2)) >>  MSADPCM_CSCALE;    \
  731.    /** reconstruct original PCM */                \
  732.   lsamp = (nyb0 * idelta) + predict;                \
  733.   if (lsamp > 32767) lsamp = 32767;                \
  734.   else if (lsamp < -32768) lsamp = -32768;            }
  735.  
  736. /********** XA_ADecode_ADPCMM_PCM2M *********************************
  737.  * Convert Microsoft ADPCM Mono Samples into PCM 2 BPS Mono Samples
  738.  * The spec flag takes care of the various linear/signed/endian
  739.  * conversions
  740.  *  Input        Ouput        Spec  1st  2nd
  741.  *  -----------------------------------------
  742.  *  MSADPCM to Signed Big       0    D1    D0
  743.  *  MSADPCM to Signed Little    1    D0    D1  
  744.  *  MSADPCM to Linear Big       2    D1^80 D0
  745.  *  MSADPCM to Linear Little    3    D0    D1^80
  746.  *
  747.  *  bit 2 (& 0x04) 1 byte output.
  748.  *  bit 3 (& 0x08) AU output instead of linear PCM.
  749.  *
  750.  * Global Variables:
  751.  *   xaUBYTE xa_sign_2_ulaw[256]    conversion table.
  752.  *   XA_Audio_Next_Snd()    routine to move to next sound header.
  753.  ***************************************************************/
  754. xaULONG XA_ADecode_ADPCMM_PCM2M(snd_hdr,obuf,ocnt,buff_size)
  755. XA_SND *snd_hdr;
  756. xaUBYTE *obuf;
  757. xaULONG ocnt,buff_size;
  758. { XA_ADECODE_DECLARE_LOCAL; 
  759.   xaULONG bpred, blk_cnt;
  760.   xaLONG delta,coef1,coef2,samp1,samp2;
  761.  
  762.   if (snd_hdr==0) return(ocnt);
  763.   XA_ADECODE_INIT_LOCAL;
  764.   blk_cnt    = snd_hdr->blk_cnt;
  765.  
  766.   /* Init Codec specific Variables */
  767.   bpred = xa_msadpcm.L_bpred;
  768.   delta = xa_msadpcm.L_delta;
  769.   samp1 = xa_msadpcm.L_samp1;
  770.   samp2 = xa_msadpcm.L_samp2;
  771.   coef1 = gaiCoef1[bpred];
  772.   coef2 = gaiCoef2[bpred];
  773.   
  774.   while(ocnt < buff_size)
  775.   { 
  776.     if (inc_cnt < (1<<24))
  777.     { inc_cnt += inc;  
  778.          /*** Decode Sample ***/
  779.       if (snd_hdr->flag == 0)
  780.       {
  781.         AUD_READ_MSADPCM_HDR(ibuf,bpred,delta,samp1,samp2);
  782.     if (bpred >= 7)   /* 7 should be variable from AVI/WAV header */
  783.     { 
  784.       fprintf(stderr,"MSADPC bpred %x blk_cnt %d\n",bpred,blk_cnt);
  785.       return(ocnt);
  786.     }
  787.     coef1 = gaiCoef1[bpred];
  788.     coef2 = gaiCoef2[bpred];
  789.     byte_cnt += 7;
  790.         blk_cnt = snd_hdr->blk_size - 7;
  791.     snd_hdr->flag = 1;
  792.     dataL = samp2;
  793.       }
  794.       else if (snd_hdr->flag == 1)
  795.       {
  796.     snd_hdr->flag = (blk_cnt)?(2):(0);
  797.     dataL = samp1;
  798.       }
  799.       else /* if (snd_hdr->flag == 2) */
  800.       { xaLONG nyb1,nyb0,idelta,lsamp;
  801.     nyb1 = *ibuf++;        byte_cnt++;    blk_cnt--;
  802.     nyb0 = (nyb1 >> 4) & 0x0f;
  803.     nyb1 &= 0x0f;
  804.     AUD_CALC_MSADPCM(lsamp,nyb0,delta,samp1,samp2,coef1,coef2);
  805.     samp2 = samp1; samp1 = lsamp;
  806.     AUD_CALC_MSADPCM(lsamp,nyb1,delta,samp1,samp2,coef1,coef2);
  807.     samp2 = samp1; samp1 = lsamp;
  808.     snd_hdr->flag = 1;
  809.     dataL = samp2;
  810.       }
  811.       samp_cnt--;
  812.     }
  813.  
  814.     while(inc_cnt >= (1<<24))
  815.     { xaULONG data = samp2;
  816.       inc_cnt -= (1<<24);
  817.         /*** Output Sample ***/
  818.       if (spec & 4)  /* 1 byte output */
  819.       { xaUBYTE d0 = ((xaULONG)(data) >> 8) & 0xff;
  820.     if (spec & 8)    *obuf++ = xa_sign_2_ulaw[ (xaULONG)(d0) ];
  821.     else             *obuf++ = (spec & 2)?(d0 ^ 0x80):(d0);
  822.       }
  823.       else
  824.       { xaUBYTE d1,d0; d1 = (data>>8) & 0xff; d0 = data & 0xff;
  825.     if (spec & 0x02) d1 ^= 0x80;
  826.     if (spec & 0x01) {*obuf++ = d0; *obuf++ = d1; }
  827.     else {*obuf++ = d1; *obuf++ = d0; }
  828.       }
  829.       ocnt++;
  830.       if (ocnt >= buff_size) break;
  831.     }
  832.     if (samp_cnt <= 0)          { XA_ADECODE_MOVE_ON; }
  833.   }
  834.   XA_ADECODE_SAVE_LOCAL;
  835.   snd_hdr->blk_cnt    = blk_cnt;
  836.  
  837.   /* Save Codec Specific */
  838.   xa_msadpcm.L_bpred    = bpred;
  839.   xa_msadpcm.L_delta    = delta;
  840.   xa_msadpcm.L_samp1    = samp1;
  841.   xa_msadpcm.L_samp2    = samp2;
  842.   return(ocnt);
  843. }
  844.  
  845.  
  846. /********** XA_ADecode_ADPCMS_PCM2M *********************************
  847.  * Convert Microsoft ADPCM Stereo Samples into PCM 2 BPS Mono Samples
  848.  * The spec flag takes care of the various linear/signed/endian
  849.  * conversions
  850.  *  Input        Ouput        Spec  1st  2nd
  851.  *  -----------------------------------------
  852.  *  MSADPCM to Signed Big       0    D1    D0
  853.  *  MSADPCM to Signed Little    1    D0    D1
  854.  *  MSADPCM to Linear Big       2    D1^80 D0
  855.  *  MSADPCM to Linear Little    3    D0    D1^80
  856.  *
  857.  *  bit 2 (& 0x04) 1 byte output.
  858.  *  bit 3 (& 0x08) AU output instead of linear PCM.
  859.  *
  860.  * Global Variables:
  861.  *   xaUBYTE xa_sign_2_ulaw[256]        conversion table.
  862.  *   XA_Audio_Next_Snd()        routine to move to next sound header.
  863.  ***************************************************************/
  864.  
  865. xaULONG XA_ADecode_ADPCMS_PCM2M(snd_hdr,obuf,ocnt,buff_size)
  866. XA_SND *snd_hdr;
  867. xaUBYTE *obuf;
  868. xaULONG ocnt,buff_size;
  869. { XA_ADECODE_DECLARE_LOCAL;
  870.   xaULONG blk_cnt; 
  871.   xaLONG L_bpred,L_delta,L_coef1,L_coef2,L_samp1,L_samp2;
  872.   xaLONG R_bpred,R_delta,R_coef1,R_coef2,R_samp1,R_samp2;
  873.  
  874.   if (snd_hdr==0) return(ocnt);
  875.   XA_ADECODE_INIT_LOCAL;
  876.   blk_cnt       = snd_hdr->blk_cnt;
  877.   /* Init Codec specific Variables */
  878.   L_bpred = xa_msadpcm.L_bpred; R_bpred = xa_msadpcm.R_bpred;
  879.   L_delta = xa_msadpcm.L_delta; R_delta = xa_msadpcm.R_delta;
  880.   L_samp1 = xa_msadpcm.L_samp1; R_samp1 = xa_msadpcm.R_samp1;
  881.   L_samp2 = xa_msadpcm.L_samp2; R_samp2 = xa_msadpcm.R_samp2;
  882.   L_coef1 = gaiCoef1[L_bpred];  R_coef1 = gaiCoef1[R_bpred];
  883.   L_coef2 = gaiCoef2[L_bpred];  R_coef2 = gaiCoef2[R_bpred];
  884.  
  885.   while(ocnt < buff_size)
  886.   {
  887.     if (inc_cnt < (1<<24))
  888.     { inc_cnt += inc;
  889.          /*** Decode Sample ***/
  890.       if (snd_hdr->flag == 0)
  891.       {
  892.     AUD_READ_MSADPCM_SHDR(ibuf,L_bpred,L_delta,L_samp1,L_samp2,
  893.                    R_bpred,R_delta,R_samp1,R_samp2);
  894.         /* 7 should be variable from AVI/WAV header */
  895.     if ((L_bpred >= 7) || (R_bpred >= 7))
  896.     { fprintf(stderr,"MSADPC bpred %x %x\n",L_bpred,R_bpred);
  897.       return(ocnt);  /* POD do better abort here */
  898.     }
  899.     L_coef1 = gaiCoef1[L_bpred];  R_coef1 = gaiCoef1[R_bpred];
  900.     L_coef2 = gaiCoef2[L_bpred];  R_coef2 = gaiCoef2[R_bpred];
  901.     byte_cnt += 14;
  902.     blk_cnt = snd_hdr->blk_size - 14;
  903.     snd_hdr->flag = 1;
  904.     dataL = L_samp2;    dataR = R_samp2;
  905.       }
  906.       else if (snd_hdr->flag == 1)
  907.       { snd_hdr->flag = 2;
  908.     dataL = L_samp1;    dataR = R_samp1;
  909.       }
  910.       else /* if (snd_hdr->flag == 2) */
  911.       { xaLONG R_nyb,L_nyb,idelta,lsamp;
  912.     R_nyb = *ibuf++;
  913.     L_nyb = (R_nyb >> 4) & 0x0f;
  914.     R_nyb &= 0x0f;
  915.     byte_cnt++;        blk_cnt--;
  916.     AUD_CALC_MSADPCM(lsamp,L_nyb,L_delta,L_samp1,L_samp2,L_coef1,L_coef2);
  917.     L_samp2 = L_samp1; L_samp1 = lsamp;
  918.     AUD_CALC_MSADPCM(lsamp,R_nyb,R_delta,R_samp1,R_samp2,R_coef1,R_coef2);
  919.     R_samp2 = R_samp1; R_samp1 = lsamp;
  920.     dataL = L_samp1;    dataR = R_samp1;
  921.     snd_hdr->flag = (blk_cnt > 0)?(2):(0);
  922.       }
  923.       samp_cnt--;
  924.     }
  925.  
  926.     while(inc_cnt >= (1<<24))
  927.     { xaULONG data = (L_samp2 + R_samp2) / 2;
  928.       inc_cnt -= (1<<24);
  929.         /*** Output Sample ***/
  930.       if (spec & 4)  /* 1 byte output */
  931.       {  xaUBYTE d0 = ((xaULONG)(data) >> 8) & 0xff;
  932.     if (spec & 8)    *obuf++ = xa_sign_2_ulaw[ (xaULONG)(d0) ];
  933.     else             *obuf++ = (spec & 2)?(d0 ^ 0x80):(d0);
  934.       }
  935.       else
  936.       { xaUBYTE d1,d0; d1 = (data>>8) & 0xff; d0 = data & 0xff;
  937.     if (spec & 0x02) d1 ^= 0x80;
  938.     if (spec & 0x01) {*obuf++ = d0; *obuf++ = d1; }
  939.     else {*obuf++ = d1; *obuf++ = d0; }
  940.       }
  941.       ocnt++;
  942.       if (ocnt >= buff_size) break;
  943.     }
  944.     if (samp_cnt <= 0)          { XA_ADECODE_MOVE_ON; }
  945.   }
  946.   XA_ADECODE_SAVE_LOCAL;
  947.   snd_hdr->blk_cnt      = blk_cnt;
  948.  
  949.   /* Save Codec Specific */
  950.   xa_msadpcm.L_bpred = L_bpred; xa_msadpcm.R_bpred = R_bpred;
  951.   xa_msadpcm.L_delta = L_delta; xa_msadpcm.R_delta = R_delta;
  952.   xa_msadpcm.L_samp1 = L_samp1; xa_msadpcm.R_samp1 = R_samp1;
  953.   xa_msadpcm.L_samp2 = L_samp2; xa_msadpcm.R_samp2 = R_samp2;
  954.   return(ocnt);
  955. }
  956.  
  957.  
  958. typedef struct XA_AUDIO_DVI_STRUCT
  959. {
  960.     xaLONG    L_valprev, R_valprev;    /* Previous output value */
  961.     xaBYTE    L_index,   R_index;    /* Index into stepsize table */
  962.     xaLONG    L_store;
  963.     xaLONG    R_store;
  964. } XA_AUDIO_DVI_STATE;
  965.  
  966. XA_AUDIO_DVI_STATE xa_dvi_state;
  967.  
  968. /* Intel ADPCM step variation table */
  969. static int xa_audio_DVI_index_table[16] = {
  970.     -1, -1, -1, -1, 2, 4, 6, 8,
  971.     -1, -1, -1, -1, 2, 4, 6, 8
  972. };
  973.  
  974. static int xa_audio_DVI_stepsize_table[89] = {
  975.     7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
  976.     19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
  977.     50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
  978.     130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
  979.     337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
  980.     876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
  981.     2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
  982.     5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
  983.     15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
  984. };
  985.  
  986. #define AUD_CALC_DVI(valpred,delta,index,step)        \
  987. { xaLONG vpdiff, sign;                    \
  988.   /* Find new index value (for later) */        \
  989.   index += xa_audio_DVI_index_table[delta];        \
  990.   if ( index < 0 ) index = 0;                \
  991.   else if ( index > 88 ) index = 88;            \
  992.   /* Separate sign and magnitude */            \
  993.   sign = delta & 8;                    \
  994.   delta = delta & 7;                    \
  995.   /* Compute difference and new predicted value */    \
  996.   vpdiff = step >> 3;                    \
  997.   if (delta & 4)  vpdiff += step;            \
  998.   if (delta & 2)  vpdiff += step>>1;            \
  999.   if (delta & 1)  vpdiff += step>>2;            \
  1000.   if ( sign )     valpred -= vpdiff;            \
  1001.   else            valpred += vpdiff;            \
  1002.   if ( valpred > 32767 )          valpred = 32767;    \
  1003.   else if ( valpred < -32768 )    valpred = -32768;    \
  1004.   /* update step value */                \
  1005.   step = xa_audio_DVI_stepsize_table[index];        }
  1006.  
  1007. /********** XA_ADecode_DVIM_PCMxM *********************************
  1008.  * Convert DVI ADPCM Mono Samples into PCM 2 BPS Mono Samples
  1009.  * The spec flag takes care of the various linear/signed/endian
  1010.  * conversions
  1011.  *  Input        Ouput        Spec  1st  2nd
  1012.  *  -----------------------------------------
  1013.  *  MSADPCM to Signed Big       0    D1    D0
  1014.  *  MSADPCM to Signed Little    1    D0    D1
  1015.  *  MSADPCM to Linear Big       2    D1^80 D0
  1016.  *  MSADPCM to Linear Little    3    D0    D1^80
  1017.  *
  1018.  *  bit 2 (& 0x04) 1 byte output.
  1019.  *  bit 3 (& 0x08) AU output instead of linear PCM.
  1020.  *
  1021.  * Global Variables:
  1022.  *   xaUBYTE xa_sign_2_ulaw[256]        conversion table.
  1023.  *   XA_Audio_Next_Snd()        routine to move to next sound header.
  1024.  ***************************************************************/
  1025.  
  1026. xaULONG XA_ADecode_DVIM_PCMxM(snd_hdr,obuf,ocnt,buff_size)
  1027. XA_SND *snd_hdr;
  1028. xaUBYTE *obuf;
  1029. xaULONG ocnt,buff_size;
  1030. { XA_ADECODE_DECLARE_LOCAL; 
  1031.   xaLONG blk_cnt,valpred, index, step;
  1032.  
  1033.   if (snd_hdr==0) return(ocnt);
  1034.   XA_ADECODE_INIT_LOCAL; 
  1035.   blk_cnt    = snd_hdr->blk_cnt;
  1036.  
  1037.   /* Init Codec specific Variables */
  1038.   valpred    = xa_dvi_state.L_valprev;
  1039.   index        = xa_dvi_state.L_index;
  1040.   step        = xa_audio_DVI_stepsize_table[index];
  1041.  
  1042.   while(ocnt < buff_size)
  1043.   {
  1044.     if (inc_cnt < (1<<24))
  1045.     { inc_cnt += inc;
  1046.     /*** Decode Sample ***/
  1047.       if (snd_hdr->flag == 0)
  1048.       {  /* Read Header - POD macro */
  1049.     valpred = *ibuf++;    valpred |= (*ibuf++)<<8;
  1050.     if (valpred & 0x8000)    valpred = valpred - 0x10000;
  1051.     index = *ibuf++;
  1052.     ibuf++;   /* pad */
  1053.  
  1054.     if (index > 88)
  1055.     { fprintf(stderr,"DVI: index err %d\n",index);
  1056.       index = 88;
  1057.     }
  1058.     step = xa_audio_DVI_stepsize_table[index];
  1059.     blk_cnt = snd_hdr->blk_size - 4;
  1060.     byte_cnt += 4;
  1061.     snd_hdr->flag = 1;
  1062.     dataL = valpred;
  1063.       }
  1064.       else if (snd_hdr->flag == 1)
  1065.       { xaLONG nyb0,nyb1;
  1066.     if (xa_kludge2_dvi)    /* old style MS DVI implementation */
  1067.     { nyb1 = *ibuf++;        /* MSB held 1st sample */
  1068.       nyb0 = (nyb1 >> 4) & 0x0f;
  1069.       nyb1 &= 0x0f;
  1070.     }
  1071.     else            /* new style MS DVI implementation */
  1072.     { nyb0 = *ibuf++;        /* LSB held 1st sample */
  1073.       nyb1 = (nyb0 >> 4) & 0x0f;
  1074.       nyb0 &= 0x0f;
  1075.     }
  1076.     byte_cnt++;    blk_cnt--;
  1077.  
  1078.  
  1079.     AUD_CALC_DVI(valpred,nyb0,index,step);
  1080.     dataL = valpred;
  1081.     AUD_CALC_DVI(valpred,nyb1,index,step);
  1082.     snd_hdr->flag = 2;
  1083.       }
  1084.       else /* if (snd_hdr->flag == 2) */
  1085.       { dataL = valpred;
  1086.     snd_hdr->flag = (blk_cnt>0)?(1):(0);
  1087.       }
  1088.       samp_cnt--;
  1089.     }
  1090.  
  1091.     while(inc_cnt >= (1<<24))
  1092.     { inc_cnt -= (1<<24);    
  1093.         /*** Output Sample ***/
  1094.       if (spec & 4)  /* 1 byte output */
  1095.       { xaUBYTE d0 = ((xaULONG)(dataL) >> 8) & 0xff;
  1096.     if (spec & 8)    *obuf++ = xa_sign_2_ulaw[ (xaULONG)(d0) ];
  1097.     else             *obuf++ = (spec & 2)?(d0 ^ 0x80):(d0);
  1098.       }
  1099.       else
  1100.       { xaUBYTE d1,d0; d1 = (dataL>>8) & 0xff; d0 = dataL & 0xff;
  1101.     if (spec & 0x02) d1 ^= 0x80;
  1102.     if (spec & 0x01) {*obuf++ = d0; *obuf++ = d1; }
  1103.     else {*obuf++ = d1; *obuf++ = d0; }
  1104.       }
  1105.       ocnt++;
  1106.       if (ocnt >= buff_size) break;
  1107.     }
  1108.     if (samp_cnt <= 0)        { XA_ADECODE_MOVE_ON; }
  1109.   }
  1110.   XA_ADECODE_SAVE_LOCAL;
  1111.   snd_hdr->blk_cnt    = blk_cnt;
  1112.   /* Save Codec Specific */
  1113.   xa_dvi_state.L_valprev    = valpred;
  1114.   xa_dvi_state.L_index        = index;
  1115.   return(ocnt);
  1116. }
  1117.  
  1118.  
  1119. /********** XA_ADecode_DVIS_PCMxM *********************************
  1120.  * Convert DVI ADPCM Stereo Samples into PCM 2 BPS Mono Samples
  1121.  * The spec flag takes care of the various linear/signed/endian
  1122.  * conversions
  1123.  *  Input        Ouput        Spec  1st  2nd
  1124.  *  -----------------------------------------
  1125.  *  MSADPCM to Signed Big       0    D1    D0
  1126.  *  MSADPCM to Signed Little    1    D0    D1
  1127.  *  MSADPCM to Linear Big       2    D1^80 D0
  1128.  *  MSADPCM to Linear Little    3    D0    D1^80
  1129.  *
  1130.  *  bit 2 (& 0x04) 1 byte output.
  1131.  *  bit 3 (& 0x08) AU output instead of linear PCM.
  1132.  *
  1133.  * Global Variables:
  1134.  *   xaUBYTE xa_sign_2_ulaw[256]        conversion table.
  1135.  *   XA_Audio_Next_Snd()        routine to move to next sound header.
  1136.  ***************************************************************/
  1137.  
  1138. xaULONG XA_ADecode_DVIS_PCMxM(snd_hdr,obuf,ocnt,buff_size)
  1139. XA_SND *snd_hdr;
  1140. xaUBYTE *obuf;
  1141. xaULONG ocnt,buff_size;
  1142. { XA_ADECODE_DECLARE_LOCAL; 
  1143.   xaLONG blk_cnt;
  1144.   xaLONG L_valpred, L_index, L_step, R_valpred, R_index, R_step;
  1145.   xaULONG L_store, R_store;
  1146.  
  1147.   if (snd_hdr==0) return(ocnt);
  1148.   XA_ADECODE_INIT_LOCAL; 
  1149.   blk_cnt    = snd_hdr->blk_cnt;
  1150.  
  1151.   /* Init Codec specific Variables */
  1152.   L_valpred    = xa_dvi_state.L_valprev;
  1153.   L_index    = xa_dvi_state.L_index;
  1154.   L_step    = xa_audio_DVI_stepsize_table[L_index];
  1155.   L_store    = xa_dvi_state.L_store;
  1156.   R_valpred    = xa_dvi_state.R_valprev;
  1157.   R_index    = xa_dvi_state.R_index;
  1158.   R_step    = xa_audio_DVI_stepsize_table[R_index];
  1159.   R_store    = xa_dvi_state.R_store;
  1160.  
  1161.   while(ocnt < buff_size)
  1162.   {
  1163.     if (inc_cnt < (1<<24))
  1164.     { inc_cnt += inc;
  1165.     /*** Decode Sample ***/
  1166.       if (snd_hdr->flag == 0)
  1167.       {  /* Read Header - POD macro */
  1168.     L_valpred = *ibuf++;    L_valpred |= (*ibuf++)<<8;
  1169.     if (L_valpred & 0x8000)    L_valpred = L_valpred - 0x10000;
  1170.     L_index = *ibuf++;    if (L_index > 88)    L_index = 88;
  1171.     L_step = xa_audio_DVI_stepsize_table[L_index];
  1172.     ibuf++;   /* pad */
  1173.     R_valpred = *ibuf++;    R_valpred |= (*ibuf++)<<8;
  1174.     if (R_valpred & 0x8000)    R_valpred = R_valpred - 0x10000;
  1175.     R_index = *ibuf++;    if (R_index > 88)    R_index = 88;
  1176.     R_step = xa_audio_DVI_stepsize_table[R_index];
  1177.     ibuf++;   /* pad */
  1178.  
  1179.     blk_cnt = snd_hdr->blk_size - 8;    byte_cnt += 8;
  1180.     snd_hdr->flag = 1;
  1181.     dataL = L_valpred;
  1182.     dataR = R_valpred;
  1183.       }
  1184.       else  /* flag == 1,2,3,4..8 */
  1185.       { xaLONG nyb; 
  1186.     if (snd_hdr->flag == 1)
  1187.     { if (xa_kludge2_dvi)    /* old style MS DVI implementation */
  1188.       { L_store = *ibuf++;            R_store = L_store >> 4;
  1189.         byte_cnt += 1;            blk_cnt -= 1;
  1190.       }
  1191.       else            /* new style MS DVI implementation */ 
  1192.       { L_store  = *ibuf++;
  1193.         L_store |= *ibuf++ <<  8;
  1194.         L_store |= *ibuf++ << 16;        
  1195.         L_store |= *ibuf++ << 24;
  1196.         R_store  = *ibuf++;
  1197.         R_store |= *ibuf++ <<  8;
  1198.         R_store |= *ibuf++ << 16;        
  1199.         R_store |= *ibuf++ << 24;
  1200.         byte_cnt += 8;            blk_cnt -= 8;
  1201.       }
  1202.     }
  1203.     else { L_store >>= 4;  R_store >>= 4; }
  1204.  
  1205.     nyb = L_store & 0x0f;
  1206.     AUD_CALC_DVI(L_valpred,nyb,L_index,L_step);
  1207.     dataL = L_valpred;
  1208.  
  1209.     nyb = R_store & 0x0f;
  1210.     AUD_CALC_DVI(R_valpred,nyb,R_index,R_step);
  1211.     dataR = R_valpred;
  1212.  
  1213.  
  1214.     if ((xa_kludge2_dvi) || (snd_hdr->flag >= 8))
  1215.                 snd_hdr->flag = (blk_cnt > 0)?(1):(0);
  1216.     else            snd_hdr->flag++;
  1217.       }
  1218.       samp_cnt--;
  1219.     }
  1220.  
  1221.     while(inc_cnt >= (1<<24))
  1222.     { xaLONG data = (dataL + dataR) / 2;
  1223.  
  1224.       inc_cnt -= (1<<24);    
  1225.         /*** Output Sample ***/
  1226.       if (spec & 4)  /* 1 byte output */
  1227.       { xaUBYTE d0 = ((xaULONG)(data) >> 8) & 0xff;
  1228.     if (spec & 8)    *obuf++ = xa_sign_2_ulaw[ (xaULONG)(d0) ];
  1229.     else             *obuf++ = (spec & 2)?(d0 ^ 0x80):(d0);
  1230.       }
  1231.       else
  1232.       { xaUBYTE d1,d0; d1 = (data>>8) & 0xff; d0 = data & 0xff;
  1233.     if (spec & 0x02) d1 ^= 0x80;
  1234.     if (spec & 0x01) {*obuf++ = d0; *obuf++ = d1; }
  1235.     else {*obuf++ = d1; *obuf++ = d0; }
  1236.       }
  1237.       ocnt++;
  1238.       if (ocnt >= buff_size) break;
  1239.     }
  1240.     if (samp_cnt <= 0)        { XA_ADECODE_MOVE_ON; }
  1241.   }
  1242.   XA_ADECODE_SAVE_LOCAL;
  1243.   snd_hdr->blk_cnt    = blk_cnt;
  1244.   /* Save Codec Specific */
  1245.   xa_dvi_state.L_valprev    = L_valpred;
  1246.   xa_dvi_state.L_index        = L_index;
  1247.   xa_dvi_state.R_valprev    = R_valpred;
  1248.   xa_dvi_state.R_index        = R_index;
  1249.   xa_dvi_state.L_store        = L_store;
  1250.   xa_dvi_state.R_store        = R_store;
  1251.   return(ocnt);
  1252. }
  1253.  
  1254. /********** XA_ADecode_IMA4M_PCMxM *********************************
  1255.  * Convert QT IMA4 ADPCM Mono Samples into PCM 2 BPS Mono Samples
  1256.  * The spec flag takes care of the various linear/signed/endian
  1257.  * conversions
  1258.  *  Input        Ouput        Spec  1st  2nd
  1259.  *  -----------------------------------------
  1260.  *  MSADPCM to Signed Big       0    D1    D0
  1261.  *  MSADPCM to Signed Little    1    D0    D1
  1262.  *  MSADPCM to Linear Big       2    D1^80 D0
  1263.  *  MSADPCM to Linear Little    3    D0    D1^80
  1264.  *
  1265.  *  bit 2 (& 0x04) 1 byte output.
  1266.  *  bit 3 (& 0x08) AU output instead of linear PCM.
  1267.  *
  1268.  * Global Variables:
  1269.  *   xaUBYTE xa_sign_2_ulaw[256]        conversion table.
  1270.  *   XA_Audio_Next_Snd()        routine to move to next sound header.
  1271.  ***************************************************************/
  1272. xaULONG XA_ADecode_IMA4M_PCMxM(snd_hdr,obuf,ocnt,buff_size)
  1273. XA_SND *snd_hdr;
  1274. xaUBYTE *obuf;
  1275. xaULONG ocnt,buff_size;
  1276. { XA_ADECODE_DECLARE_LOCAL; 
  1277.   xaLONG blk_cnt,valpred, index, step;
  1278.  
  1279.   if (snd_hdr==0) return(ocnt);
  1280.   XA_ADECODE_INIT_LOCAL; 
  1281.   blk_cnt    = snd_hdr->blk_cnt;
  1282.  
  1283.   /* Init Codec specific Variables */
  1284.   valpred    = xa_dvi_state.L_valprev;
  1285.   index        = xa_dvi_state.L_index;
  1286.   step        = xa_audio_DVI_stepsize_table[index];
  1287.  
  1288.   while(ocnt < buff_size)
  1289.   {
  1290.     if (inc_cnt < (1<<24))
  1291.     { inc_cnt += inc;
  1292.     /*** Decode Sample ***/
  1293.       if (snd_hdr->flag == 2)
  1294.       { dataL = valpred;
  1295.     snd_hdr->flag = (blk_cnt>0)?(1):(0);
  1296.       }
  1297.       else
  1298.       { xaLONG nyb0,nyb1;
  1299.         if (snd_hdr->flag == 0)
  1300.         {  /* Read Header - POD macro */
  1301.       valpred = *ibuf++ << 8;    valpred |= (*ibuf++);
  1302.       index = valpred & 0x7f;
  1303.       if (index > 88)
  1304.       { fprintf(stderr,"IMA4: index err %d\n",index);
  1305.         index = 88;
  1306.       }
  1307.       valpred &= 0xff80;
  1308.       if (valpred & 0x8000)    valpred = valpred - 0x10000;
  1309.       blk_cnt = snd_hdr->blk_size - 2;
  1310.       byte_cnt += 2;
  1311.       step = xa_audio_DVI_stepsize_table[index];
  1312.       snd_hdr->flag = 1;
  1313.         }
  1314.  
  1315.     nyb0 = *ibuf++;        /* LSB held 1st sample */
  1316.     nyb1 = (nyb0 >> 4) & 0x0f;
  1317.     nyb0 &= 0x0f;
  1318.     byte_cnt++;    blk_cnt--;
  1319.  
  1320.     AUD_CALC_DVI(valpred,nyb0,index,step);
  1321.     dataL = valpred;
  1322.     AUD_CALC_DVI(valpred,nyb1,index,step);
  1323.     snd_hdr->flag = 2;
  1324.       }
  1325.       samp_cnt--;
  1326.     }
  1327.  
  1328.     while(inc_cnt >= (1<<24))
  1329.     { inc_cnt -= (1<<24);    
  1330.         /*** Output Sample ***/
  1331.       if (spec & 4)  /* 1 byte output */
  1332.       { xaUBYTE d0 = ((xaULONG)(dataL) >> 8) & 0xff;
  1333.     if (spec & 8)    *obuf++ = xa_sign_2_ulaw[ (xaULONG)(d0) ];
  1334.     else             *obuf++ = (spec & 2)?(d0 ^ 0x80):(d0);
  1335.       }
  1336.       else
  1337.       { xaUBYTE d1,d0; d1 = (dataL>>8) & 0xff; d0 = dataL & 0xff;
  1338.     if (spec & 0x02) d1 ^= 0x80;
  1339.     if (spec & 0x01) {*obuf++ = d0; *obuf++ = d1; }
  1340.     else {*obuf++ = d1; *obuf++ = d0; }
  1341.       }
  1342.       ocnt++;
  1343.       if (ocnt >= buff_size) break;
  1344.     }
  1345.     if (samp_cnt <= 0)        { XA_ADECODE_MOVE_ON; }
  1346.   }
  1347.   XA_ADECODE_SAVE_LOCAL;
  1348.   snd_hdr->blk_cnt    = blk_cnt;
  1349.   /* Save Codec Specific */
  1350.   xa_dvi_state.L_valprev    = valpred;
  1351.   xa_dvi_state.L_index        = index;
  1352.   return(ocnt);
  1353. }
  1354.  
  1355. /********** XA_ADecode_IMA4S_PCMxM *********************************
  1356.  * Convert QT IMA4 ADPCM Stereo Samples into PCM 2 BPS Mono Samples
  1357.  * The spec flag takes care of the various linear/signed/endian
  1358.  * conversions
  1359.  *  Input        Ouput        Spec  1st  2nd
  1360.  *  -----------------------------------------
  1361.  *  MSADPCM to Signed Big       0    D1    D0
  1362.  *  MSADPCM to Signed Little    1    D0    D1
  1363.  *  MSADPCM to Linear Big       2    D1^80 D0
  1364.  *  MSADPCM to Linear Little    3    D0    D1^80
  1365.  *
  1366.  *  bit 2 (& 0x04) 1 byte output.
  1367.  *  bit 3 (& 0x08) AU output instead of linear PCM.
  1368.  *
  1369.  * Global Variables:
  1370.  *   xaUBYTE xa_sign_2_ulaw[256]        conversion table.
  1371.  *   XA_Audio_Next_Snd()        routine to move to next sound header.
  1372.  ***************************************************************/
  1373.  
  1374. xaULONG XA_ADecode_IMA4S_PCMxM(snd_hdr,obuf,ocnt,buff_size)
  1375. XA_SND *snd_hdr;
  1376. xaUBYTE *obuf;
  1377. xaULONG ocnt,buff_size;
  1378. { XA_ADECODE_DECLARE_LOCAL; 
  1379.   xaUBYTE *ibufR;
  1380.   xaLONG blk_cnt,blk_size;
  1381.   xaLONG L_valpred, L_index, L_step, R_valpred, R_index, R_step;
  1382.  
  1383.   if (snd_hdr==0) return(ocnt);
  1384.   XA_ADECODE_INIT_LOCAL; 
  1385.   blk_cnt    = snd_hdr->blk_cnt;
  1386.   blk_size    = snd_hdr->blk_size >> 1;
  1387.  
  1388.   /* Init Codec specific Variables */
  1389.   L_valpred    = xa_dvi_state.L_valprev;
  1390.   L_index    = xa_dvi_state.L_index;
  1391.   L_step    = xa_audio_DVI_stepsize_table[L_index];
  1392.   R_valpred    = xa_dvi_state.R_valprev;
  1393.   R_index    = xa_dvi_state.R_index;
  1394.   R_step    = xa_audio_DVI_stepsize_table[R_index];
  1395.  
  1396. /* POD: assume Right channel is always within same block as left channel 
  1397.  * for now at least */
  1398.   ibufR = ibuf;
  1399.   ibufR += blk_size;
  1400.   while(ocnt < buff_size)
  1401.   {
  1402.     if (inc_cnt < (1<<24))
  1403.     { inc_cnt += inc;
  1404.     /*** Decode Sample ***/
  1405.       if (snd_hdr->flag == 2)
  1406.       { dataL = L_valpred;
  1407.     dataR = R_valpred;
  1408.  
  1409.     if (blk_cnt == 0)
  1410.         {  byte_cnt += blk_size;
  1411.        ibuf += blk_size;    /* skip R channel */
  1412.        ibufR += blk_size;
  1413.        snd_hdr->flag = 0;
  1414.     }
  1415.         else snd_hdr->flag = 1;
  1416.       }
  1417.       else 
  1418.       { xaLONG nyb0,nyb1; 
  1419.     if (snd_hdr->flag == 0)
  1420.     {  /* Read Header - POD macro */
  1421.       L_valpred = *ibuf++ << 8;    L_valpred |= (*ibuf++);
  1422.       L_index = L_valpred & 0x7f;
  1423.  
  1424.       if (L_index > 88)
  1425.       { fprintf(stderr,"IMA4: L index err %d\n",L_index);
  1426.         L_index = 88;
  1427.       }
  1428.       L_valpred &= 0xff80;
  1429.       if (L_valpred & 0x8000)    L_valpred = L_valpred - 0x10000;
  1430.       L_step = xa_audio_DVI_stepsize_table[L_index];
  1431.  
  1432.       R_valpred = *ibufR++ << 8;    R_valpred |= (*ibufR++);
  1433.       R_index = R_valpred & 0x7f;
  1434.  
  1435.       if (R_index > 88)
  1436.       { fprintf(stderr,"IMA4: R_index err %d\n",R_index);
  1437.         R_index = 88;
  1438.       }
  1439.       R_valpred &= 0xff80;
  1440.       if (R_valpred & 0x8000)    R_valpred = R_valpred - 0x10000;
  1441.       R_step = xa_audio_DVI_stepsize_table[R_index];
  1442.  
  1443.       blk_cnt = blk_size - 2;        byte_cnt += 2;
  1444.       snd_hdr->flag = 1;
  1445.     }
  1446.  
  1447.     nyb0 = *ibuf++; nyb1 = (nyb0 >> 4) & 0x0f; nyb0 &= 0x0f;
  1448.  
  1449.     byte_cnt += 1;            blk_cnt -= 1;
  1450.     AUD_CALC_DVI(L_valpred,nyb0,L_index,L_step);
  1451.     dataL = L_valpred;
  1452.     AUD_CALC_DVI(L_valpred,nyb1,L_index,L_step);
  1453.  
  1454.     nyb0 = *ibufR++; nyb1 = (nyb0 >> 4) & 0x0f; nyb0 &= 0x0f;
  1455.  
  1456.     AUD_CALC_DVI(R_valpred,nyb0,R_index,R_step);
  1457.     dataR = R_valpred;
  1458.     AUD_CALC_DVI(R_valpred,nyb1,R_index,R_step);
  1459.  
  1460.         snd_hdr->flag = 2;
  1461.       }
  1462.       samp_cnt--;
  1463.     }
  1464.  
  1465.     while(inc_cnt >= (1<<24))
  1466.       { xaLONG data = (dataL + dataR) / 2;
  1467. /*    { xaULONG data = ( (dataL & 0xffff) + (dataR & 0xffff)) >> 1; */
  1468.       inc_cnt -= (1<<24);    
  1469.         /*** Output Sample ***/
  1470.       if (spec & 4)  /* 1 byte output */
  1471.       { xaUBYTE d0 = ((xaULONG)(data) >> 8) & 0xff;
  1472.     if (spec & 8)    *obuf++ = xa_sign_2_ulaw[ (xaULONG)(d0) ];
  1473.     else             *obuf++ = (spec & 2)?(d0 ^ 0x80):(d0);
  1474.       }
  1475.       else
  1476.       { xaUBYTE d1,d0; d1 = (data>>8) & 0xff; d0 = data & 0xff;
  1477.     if (spec & 0x02) d1 ^= 0x80;
  1478.     if (spec & 0x01) {*obuf++ = d0; *obuf++ = d1; }
  1479.     else {*obuf++ = d1; *obuf++ = d0; }
  1480.       }
  1481.       ocnt++;
  1482.       if (ocnt >= buff_size) break;
  1483.     }
  1484.     if (samp_cnt <= 0)        { XA_ADECODE_MOVE_ON; }
  1485.   }
  1486.   XA_ADECODE_SAVE_LOCAL;
  1487.   snd_hdr->blk_cnt    = blk_cnt;
  1488.   /* Save Codec Specific */
  1489.   xa_dvi_state.L_valprev    = L_valpred;
  1490.   xa_dvi_state.L_index        = L_index;
  1491.   xa_dvi_state.R_valprev    = R_valpred;
  1492.   xa_dvi_state.R_index        = R_index;
  1493.   return(ocnt);
  1494. }
  1495.  
  1496.  
  1497. #include "xa_gsm.h"
  1498.  
  1499. typedef struct
  1500. {
  1501.   xaSHORT    buf0[160];
  1502.   xaSHORT    buf1[160];
  1503. } XA_AUDIO_GSM_STATE;
  1504. static XA_AUDIO_GSM_STATE gsm_state;
  1505.  
  1506.  
  1507. /***************************************************************************
  1508.  *
  1509.  *
  1510.  */ 
  1511. xaULONG XA_ADecode_GSMM_PCMxM(snd_hdr,obuf,ocnt,buff_size)
  1512. XA_SND *snd_hdr;
  1513. xaUBYTE *obuf;
  1514. xaULONG ocnt,buff_size;
  1515. { XA_ADECODE_DECLARE_LOCAL; 
  1516.   if (snd_hdr==0) return(ocnt);
  1517.   XA_ADECODE_INIT_LOCAL;    /* Init Local Variables */
  1518.   /* Optionally Init Codec Specific Structure Here */ 
  1519.   while(ocnt < buff_size)
  1520.   {
  1521.     if (inc_cnt < (1<<24))
  1522.     {    
  1523.       if (flag == 0)
  1524.       { 
  1525.     xaSHORT sr;
  1526.     sr = *ibuf++;
  1527.     LARc[0] = sr & 0x3f;  sr >>= 6;
  1528.     sr |= (xaShort)*ibuf++ << 2;
  1529.     LARc[1] = sr & 0x3f;  sr >>= 6;
  1530.     sr |= (xaShort)*ibuf++ << 4;
  1531.     LARc[2] = sr & 0x1f;  sr >>= 5;
  1532.     LARc[3] = sr & 0x1f;  sr >>= 5;
  1533.     sr |= (xaShort)*ibuf++ << 2;
  1534.     LARc[4] = sr & 0xf;  sr >>= 4;
  1535.     LARc[5] = sr & 0xf;  sr >>= 4;
  1536.     sr |= (xaShort)*ibuf++ << 2;            /* 5 */
  1537.     LARc[6] = sr & 0x7;  sr >>= 3;
  1538.     LARc[7] = sr & 0x7;  sr >>= 3;
  1539.     sr |= (xaShort)*ibuf++ << 4;
  1540.     Nc[0] = sr & 0x7f;  sr >>= 7;
  1541.     bc[0] = sr & 0x3;  sr >>= 2;
  1542.     Mc[0] = sr & 0x3;  sr >>= 2;
  1543.     sr |= (xaShort)*ibuf++ << 1;
  1544.     xmaxc[0] = sr & 0x3f;  sr >>= 6;
  1545.     xmc[0] = sr & 0x7;  sr >>= 3;
  1546.     sr = *ibuf++;
  1547.     xmc[1] = sr & 0x7;  sr >>= 3;
  1548.     xmc[2] = sr & 0x7;  sr >>= 3;
  1549.     sr |= (xaShort)*ibuf++ << 2;
  1550.     xmc[3] = sr & 0x7;  sr >>= 3;
  1551.     xmc[4] = sr & 0x7;  sr >>= 3;
  1552.     xmc[5] = sr & 0x7;  sr >>= 3;
  1553.     sr |= (xaShort)*ibuf++ << 1;            /* 10 */
  1554.     xmc[6] = sr & 0x7;  sr >>= 3;
  1555.     xmc[7] = sr & 0x7;  sr >>= 3;
  1556.     xmc[8] = sr & 0x7;  sr >>= 3;
  1557.     sr = *ibuf++;
  1558.     xmc[9] = sr & 0x7;  sr >>= 3;
  1559.     xmc[10] = sr & 0x7;  sr >>= 3;
  1560.     sr |= (xaShort)*ibuf++ << 2;
  1561.     xmc[11] = sr & 0x7;  sr >>= 3;
  1562.     xmc[12] = sr & 0x7;  sr >>= 3;
  1563.     sr |= (xaShort)*ibuf++ << 4;
  1564.     Nc[1] = sr & 0x7f;  sr >>= 7;
  1565.     bc[1] = sr & 0x3;  sr >>= 2;
  1566.     Mc[1] = sr & 0x3;  sr >>= 2;
  1567.     sr |= (xaShort)*ibuf++ << 1;
  1568.     xmaxc[1] = sr & 0x3f;  sr >>= 6;
  1569.     xmc[13] = sr & 0x7;  sr >>= 3;
  1570.     sr = *ibuf++;                /* 15 */
  1571.     xmc[14] = sr & 0x7;  sr >>= 3;
  1572.     xmc[15] = sr & 0x7;  sr >>= 3;
  1573.     sr |= (xaShort)*ibuf++ << 2;
  1574.     xmc[16] = sr & 0x7;  sr >>= 3;
  1575.     xmc[17] = sr & 0x7;  sr >>= 3;
  1576.     xmc[18] = sr & 0x7;  sr >>= 3;
  1577.     sr |= (xaShort)*ibuf++ << 1;
  1578.     xmc[19] = sr & 0x7;  sr >>= 3;
  1579.     xmc[20] = sr & 0x7;  sr >>= 3;
  1580.     xmc[21] = sr & 0x7;  sr >>= 3;
  1581.     sr = *ibuf++;
  1582.     xmc[22] = sr & 0x7;  sr >>= 3;
  1583.     xmc[23] = sr & 0x7;  sr >>= 3;
  1584.     sr |= (xaShort)*ibuf++ << 2;
  1585.     xmc[24] = sr & 0x7;  sr >>= 3;
  1586.     xmc[25] = sr & 0x7;  sr >>= 3;
  1587.     sr |= (xaShort)*ibuf++ << 4;            /* 20 */
  1588.     Nc[2] = sr & 0x7f;  sr >>= 7;
  1589.     bc[2] = sr & 0x3;  sr >>= 2;
  1590.     Mc[2] = sr & 0x3;  sr >>= 2;
  1591.     sr |= (xaShort)*ibuf++ << 1;
  1592.     xmaxc[2] = sr & 0x3f;  sr >>= 6;
  1593.     xmc[26] = sr & 0x7;  sr >>= 3;
  1594.     sr = *ibuf++;
  1595.     xmc[27] = sr & 0x7;  sr >>= 3;
  1596.     xmc[28] = sr & 0x7;  sr >>= 3;
  1597.     sr |= (xaShort)*ibuf++ << 2;
  1598.     xmc[29] = sr & 0x7;  sr >>= 3;
  1599.     xmc[30] = sr & 0x7;  sr >>= 3;
  1600.     xmc[31] = sr & 0x7;  sr >>= 3;
  1601.     sr |= (xaShort)*ibuf++ << 1;
  1602.     xmc[32] = sr & 0x7;  sr >>= 3;
  1603.     xmc[33] = sr & 0x7;  sr >>= 3;
  1604.     xmc[34] = sr & 0x7;  sr >>= 3;
  1605.     sr = *ibuf++;                /* 25 */
  1606.     xmc[35] = sr & 0x7;  sr >>= 3;
  1607.     xmc[36] = sr & 0x7;  sr >>= 3;
  1608.     sr |= (xaShort)*ibuf++ << 2;
  1609.     xmc[37] = sr & 0x7;  sr >>= 3;
  1610.     xmc[38] = sr & 0x7;  sr >>= 3;
  1611.     sr |= (xaShort)*ibuf++ << 4;
  1612.     Nc[3] = sr & 0x7f;  sr >>= 7;
  1613.     bc[3] = sr & 0x3;  sr >>= 2;
  1614.     Mc[3] = sr & 0x3;  sr >>= 2;
  1615.     sr |= (xaShort)*ibuf++ << 1;
  1616.     xmaxc[3] = sr & 0x3f;  sr >>= 6;
  1617.     xmc[39] = sr & 0x7;  sr >>= 3;
  1618.     sr = *ibuf++;
  1619.     xmc[40] = sr & 0x7;  sr >>= 3;
  1620.     xmc[41] = sr & 0x7;  sr >>= 3;
  1621.     sr |= (xaShort)*ibuf++ << 2;            /* 30 */
  1622.     xmc[42] = sr & 0x7;  sr >>= 3;
  1623.     xmc[43] = sr & 0x7;  sr >>= 3;
  1624.     xmc[44] = sr & 0x7;  sr >>= 3;
  1625.     sr |= (xaShort)*ibuf++ << 1;
  1626.     xmc[45] = sr & 0x7;  sr >>= 3;
  1627.     xmc[46] = sr & 0x7;  sr >>= 3;
  1628.     xmc[47] = sr & 0x7;  sr >>= 3;
  1629.     sr = *ibuf++;
  1630.     xmc[48] = sr & 0x7;  sr >>= 3;
  1631.     xmc[49] = sr & 0x7;  sr >>= 3;
  1632.     sr |= (xaShort)*ibuf++ << 2;
  1633.     xmc[50] = sr & 0x7;  sr >>= 3;
  1634.     xmc[51] = sr & 0x7;  sr >>= 3;
  1635.  
  1636.     Gsm_Decoder(&gsm_state, LARc, Nc, bc, Mc, xmaxc, xmc, gsm_buf.buf0);
  1637. /*
  1638.     carry = sr & 0xf;
  1639.     sr = carry;
  1640. */
  1641.     /* 2nd frame */
  1642.     sr &= 0xf;
  1643.     sr |= (xaShort)*ibuf++ << 4;            /* 1 */
  1644.     LARc[0] = sr & 0x3f;  sr >>= 6;
  1645.     LARc[1] = sr & 0x3f;  sr >>= 6;
  1646.     sr = *ibuf++;
  1647.     LARc[2] = sr & 0x1f;  sr >>= 5;
  1648.     sr |= (xaShort)*ibuf++ << 3;
  1649.     LARc[3] = sr & 0x1f;  sr >>= 5;
  1650.     LARc[4] = sr & 0xf;  sr >>= 4;
  1651.     sr |= (xaShort)*ibuf++ << 2;
  1652.     LARc[5] = sr & 0xf;  sr >>= 4;
  1653.     LARc[6] = sr & 0x7;  sr >>= 3;
  1654.     LARc[7] = sr & 0x7;  sr >>= 3;
  1655.     sr = *ibuf++;                /* 5 */
  1656.     Nc[0] = sr & 0x7f;  sr >>= 7;
  1657.     sr |= (xaShort)*ibuf++ << 1;
  1658.     bc[0] = sr & 0x3;  sr >>= 2;
  1659.     Mc[0] = sr & 0x3;  sr >>= 2;
  1660.     sr |= (xaShort)*ibuf++ << 5;
  1661.     xmaxc[0] = sr & 0x3f;  sr >>= 6;
  1662.     xmc[0] = sr & 0x7;  sr >>= 3;
  1663.     xmc[1] = sr & 0x7;  sr >>= 3;
  1664.     sr |= (xaShort)*ibuf++ << 1;
  1665.     xmc[2] = sr & 0x7;  sr >>= 3;
  1666.     xmc[3] = sr & 0x7;  sr >>= 3;
  1667.     xmc[4] = sr & 0x7;  sr >>= 3;
  1668.     sr = *ibuf++;
  1669.     xmc[5] = sr & 0x7;  sr >>= 3;
  1670.     xmc[6] = sr & 0x7;  sr >>= 3;
  1671.     sr |= (xaShort)*ibuf++ << 2;            /* 10 */
  1672.     xmc[7] = sr & 0x7;  sr >>= 3;
  1673.     xmc[8] = sr & 0x7;  sr >>= 3;
  1674.     xmc[9] = sr & 0x7;  sr >>= 3;
  1675.     sr |= (xaShort)*ibuf++ << 1;
  1676.     xmc[10] = sr & 0x7;  sr >>= 3;
  1677.     xmc[11] = sr & 0x7;  sr >>= 3;
  1678.     xmc[12] = sr & 0x7;  sr >>= 3;
  1679.     sr = *ibuf++;
  1680.     Nc[1] = sr & 0x7f;  sr >>= 7;
  1681.     sr |= (xaShort)*ibuf++ << 1;
  1682.     bc[1] = sr & 0x3;  sr >>= 2;
  1683.     Mc[1] = sr & 0x3;  sr >>= 2;
  1684.     sr |= (xaShort)*ibuf++ << 5;
  1685.     xmaxc[1] = sr & 0x3f;  sr >>= 6;
  1686.     xmc[13] = sr & 0x7;  sr >>= 3;
  1687.     xmc[14] = sr & 0x7;  sr >>= 3;
  1688.     sr |= (xaShort)*ibuf++ << 1;            /* 15 */
  1689.     xmc[15] = sr & 0x7;  sr >>= 3;
  1690.     xmc[16] = sr & 0x7;  sr >>= 3;
  1691.     xmc[17] = sr & 0x7;  sr >>= 3;
  1692.     sr = *ibuf++;
  1693.     xmc[18] = sr & 0x7;  sr >>= 3;
  1694.     xmc[19] = sr & 0x7;  sr >>= 3;
  1695.     sr |= (xaShort)*ibuf++ << 2;
  1696.     xmc[20] = sr & 0x7;  sr >>= 3;
  1697.     xmc[21] = sr & 0x7;  sr >>= 3;
  1698.     xmc[22] = sr & 0x7;  sr >>= 3;
  1699.     sr |= (xaShort)*ibuf++ << 1;
  1700.     xmc[23] = sr & 0x7;  sr >>= 3;
  1701.     xmc[24] = sr & 0x7;  sr >>= 3;
  1702.     xmc[25] = sr & 0x7;  sr >>= 3;
  1703.     sr = *ibuf++;
  1704.     Nc[2] = sr & 0x7f;  sr >>= 7;
  1705.     sr |= (xaShort)*ibuf++ << 1;            /* 20 */
  1706.     bc[2] = sr & 0x3;  sr >>= 2;
  1707.     Mc[2] = sr & 0x3;  sr >>= 2;
  1708.     sr |= (xaShort)*ibuf++ << 5;
  1709.     xmaxc[2] = sr & 0x3f;  sr >>= 6;
  1710.     xmc[26] = sr & 0x7;  sr >>= 3;
  1711.     xmc[27] = sr & 0x7;  sr >>= 3;
  1712.     sr |= (xaShort)*ibuf++ << 1;    
  1713.     xmc[28] = sr & 0x7;  sr >>= 3;
  1714.     xmc[29] = sr & 0x7;  sr >>= 3;
  1715.     xmc[30] = sr & 0x7;  sr >>= 3;
  1716.     sr = *ibuf++;
  1717.     xmc[31] = sr & 0x7;  sr >>= 3;
  1718.     xmc[32] = sr & 0x7;  sr >>= 3;
  1719.     sr |= (xaShort)*ibuf++ << 2;
  1720.     xmc[33] = sr & 0x7;  sr >>= 3;
  1721.     xmc[34] = sr & 0x7;  sr >>= 3;
  1722.     xmc[35] = sr & 0x7;  sr >>= 3;
  1723.     sr |= (xaShort)*ibuf++ << 1;            /* 25 */
  1724.     xmc[36] = sr & 0x7;  sr >>= 3;
  1725.     xmc[37] = sr & 0x7;  sr >>= 3;
  1726.     xmc[38] = sr & 0x7;  sr >>= 3;
  1727.     sr = *ibuf++;
  1728.     Nc[3] = sr & 0x7f;  sr >>= 7;
  1729.     sr |= (xaShort)*ibuf++ << 1;        
  1730.     bc[3] = sr & 0x3;  sr >>= 2;
  1731.     Mc[3] = sr & 0x3;  sr >>= 2;
  1732.     sr |= (xaShort)*ibuf++ << 5;
  1733.     xmaxc[3] = sr & 0x3f;  sr >>= 6;
  1734.     xmc[39] = sr & 0x7;  sr >>= 3;
  1735.     xmc[40] = sr & 0x7;  sr >>= 3;
  1736.     sr |= (xaShort)*ibuf++ << 1;
  1737.     xmc[41] = sr & 0x7;  sr >>= 3;
  1738.     xmc[42] = sr & 0x7;  sr >>= 3;
  1739.     xmc[43] = sr & 0x7;  sr >>= 3;
  1740.     sr = *ibuf++;                /* 30 */
  1741.     xmc[44] = sr & 0x7;  sr >>= 3;
  1742.     xmc[45] = sr & 0x7;  sr >>= 3;
  1743.     sr |= (xaShort)*ibuf++ << 2;
  1744.     xmc[46] = sr & 0x7;  sr >>= 3;
  1745.     xmc[47] = sr & 0x7;  sr >>= 3;
  1746.     xmc[48] = sr & 0x7;  sr >>= 3;
  1747.     sr |= (xaShort)*ibuf++ << 1;
  1748.     xmc[49] = sr & 0x7;  sr >>= 3;
  1749.     xmc[50] = sr & 0x7;  sr >>= 3;
  1750.     xmc[51] = sr & 0x7;  sr >>= 3;
  1751.     Gsm_Decoder(&gsm_state, LARc, Nc, bc, Mc, xmaxc, xmc, gsm_buf.buf1);
  1752.  
  1753.     byte_cnt += 65;
  1754.       }
  1755.     /*** Decode Sample and increment byte_cnt ***/
  1756.       Gsm_Decoder(s, LARc, Nc, bc, Mc, xmaxc, xmc, target);
  1757.       if (flag < 160)  dataL  = gsm_buf.buf0[flag];
  1758.       else if (flag < 320)  dataL  = gsm_buf.buf1[ (flag - 160) ];
  1759.       else /* ERROR */ dataL = 0;
  1760.  
  1761.       flag++; if (flag >= 320) flag = 0;
  1762.       samp_cnt--;
  1763.       inc_cnt += inc;
  1764.     }
  1765.     while(inc_cnt >= (1<<24))
  1766.     { 
  1767.         /*** Output Sample ***/
  1768.       if (spec & 4)  /* 1 byte output */
  1769.       { xaUBYTE d0 = ((xaULONG)(dataL) >> 8) & 0xff;
  1770.         if (spec & 8)    *obuf++ = xa_sign_2_ulaw[ (xaULONG)(d0) ];
  1771.         else             *obuf++ = (spec & 2)?(d0 ^ 0x80):(d0);
  1772.       }
  1773.       else
  1774.       { xaUBYTE d1,d0; d1 = (dataL>>8) & 0xff; d0 = dataL & 0xff;
  1775.         if (spec & 0x02) d1 ^= 0x80;
  1776.         if (spec & 0x01) {*obuf++ = d0; *obuf++ = d1; }
  1777.         else {*obuf++ = d1; *obuf++ = d0; }
  1778.       }
  1779.       ocnt++;
  1780.       inc_cnt -= (1<<24);    
  1781.       if (ocnt >= buff_size) break;
  1782.     }
  1783.     if (samp_cnt <= 0)        { XA_ADECODE_MOVE_ON; }
  1784.   }
  1785.   XA_ADECODE_SAVE_LOCAL;     /* save local variables */
  1786.   /* Optionally Save Codec Specific Structure Here */
  1787.   return(ocnt);
  1788. }
  1789.  
  1790.  
  1791. /*+_+_+_+_+_+_+_+___+_+_+_+_+_+_+_+_+_++_+_++_+_+_++_+_+++_+_+_+_+_+*/
  1792. /*+_+_+_+_+_+_+_+___+_+_+_+_+_+_+_+_+_++_+_++_+_+_++_+_+++_+_+_+_+_+*/
  1793. /*+_+_+_+_+_+_+_+___+_+_+_+_+_+_+_+_+_++_+_++_+_+_++_+_+++_+_+_+_+_+*/
  1794.  
  1795. void XA_Audio_Init_Snd(snd_hdr)
  1796. XA_SND *snd_hdr;
  1797. {
  1798.   snd_hdr->flag     = 0;
  1799.   snd_hdr->inc_cnt  = 0;
  1800.   snd_hdr->byte_cnt = 0;
  1801.   snd_hdr->samp_cnt = snd_hdr->tot_samps;
  1802.   Gen_Ulaw_2_Signed();
  1803.   Gen_Arm_2_Signed();
  1804. }
  1805.  
  1806.  
  1807. extern XA_AUD_FLAGS *vaudiof;
  1808. extern xaULONG xa_vaudio_present;
  1809.  
  1810. /********* XA_Add_Sound ****************************************
  1811.  * IMPORTANT: THIS ROUTINE IS IN THE VIDEO DOMAIN
  1812.  *
  1813.  * Global Variables Used:
  1814.  *   double XAAUD->scale    linear scale of frequency
  1815.  *   xaULONG  xa_audio_hard_buff    size of sound chunk - set by XA_Closest_Freq().
  1816.  *   xaULONG  XAAUD->bufferit    xaTRUE if this routine is to convert and buffer
  1817.  *                 the audio data ahead of time.
  1818.  *   xaULONG  xa_audio_hard_type    Audio Type of Current Hardware
  1819.  *
  1820.  * Add sound double checks xa_audio_present. 
  1821.  *   If UNK then it calls XA_Audio_Init().
  1822.  *   If OK adds the sound, else returns false.
  1823.  *****/
  1824. xaULONG XA_Add_Sound(anim_hdr,isnd,itype,fpos,ifreq,ilen,stime,stimelo,
  1825.             blockalign, sampsblock)
  1826. XA_ANIM_HDR *anim_hdr;
  1827. xaUBYTE *isnd;
  1828. xaULONG itype;    /* sound type */
  1829. xaULONG fpos;    /* file position */
  1830. xaULONG ifreq;    /* input frequency */
  1831. xaULONG ilen;    /* length of snd sample chunk in bytes */
  1832. xaLONG *stime;    /* start time of sample */
  1833. xaULONG *stimelo;    /* fractional start time of sample */
  1834. xaULONG blockalign;
  1835. xaULONG sampsblock;
  1836. { XA_SND *new_snd;
  1837.   xaULONG bps,isamps,totsamps,hfreq,inc,ret;
  1838.   double finc,ftime,fadj_freq;
  1839.  
  1840. #ifndef XA_AUDIO
  1841.  
  1842.   if (xa_verbose) 
  1843.   { fprintf(stderr,
  1844.     "Warning: Since Audio Support was not enabled in this executable\n");
  1845.     fprintf(stderr,"the audio portion of this file is ignored.\n");
  1846.   }
  1847.   return(xaFALSE);
  1848.  
  1849. #else
  1850.  
  1851.   /* If audio hasn't been initialized, then init it */
  1852.   if (xa_vaudio_present == XA_AUDIO_UNK)
  1853.   { XA_AUDIO_INIT(xa_vaudio_present);  /* note: also sets xa_forkit */
  1854.     if (xa_forkit==xaFALSE) xa_vaudio_present = XA_AUDIO_ERR;
  1855.   }
  1856.   /* Return xaFALSE if it's not OK */
  1857.   if (xa_vaudio_present != XA_AUDIO_OK)
  1858.   {
  1859.     if (xa_forkit == xaTRUE)
  1860.     { XA_AUDIO_EXIT();
  1861.       xa_forkit = xaFALSE;
  1862.     }
  1863.     return(xaFALSE);
  1864.   }
  1865.  
  1866.   new_snd = (XA_SND *)malloc(sizeof(XA_SND));
  1867.   if (new_snd==0) TheEnd1("snd malloc err");
  1868.  
  1869.   bps = 1;
  1870.  
  1871.   if ( (itype & XA_AUDIO_TYPE_MASK) == XA_AUDIO_IMA4)
  1872.   { xaLONG w,tmp_len;
  1873.     new_snd->blk_size = blockalign;
  1874.  
  1875.      /** calculate total samples in entire chunk for timing purposes */
  1876.     w = (ilen / blockalign);  
  1877.     tmp_len = sampsblock * w;          /* out len based on full blocks */
  1878.  
  1879.     w = ilen - (w * blockalign);    /* bytes remaining for partial blocks*/
  1880.  
  1881.     if (itype == XA_AUDIO_IMA4_M)
  1882.     { w -= 2;    /* sub header */
  1883.       if (w >= 0)
  1884.       { w *= 2;  /* 2 samps per byte */
  1885.         w += 0;  /* 1 samp in header */
  1886.         tmp_len += w;
  1887.       }
  1888.     }
  1889.     if (itype == XA_AUDIO_IMA4_S)
  1890.     { w -= 2;    /* sub header */
  1891.       if (w >= 0)
  1892.       { w *= 1;  /* 1 samps per byte */
  1893.         w += 0;  /* 1 samp in header */
  1894.         tmp_len += w;
  1895.       }
  1896.     }
  1897.     totsamps = isamps = tmp_len;
  1898.   }
  1899.   else if ( (itype & XA_AUDIO_TYPE_MASK) == XA_AUDIO_DVI)
  1900.   { xaLONG w,tmp_len;
  1901.     new_snd->blk_size = blockalign;
  1902.  
  1903.      /** calculate total samples in entire chunk for timing purposes */
  1904.     w = (ilen / blockalign);  
  1905.     tmp_len = sampsblock * w;          /* out len based on full blocks */
  1906.  
  1907.     w = ilen - (w * blockalign);    /* bytes remaining for partial blocks*/
  1908.  
  1909.     if (itype == XA_AUDIO_DVI_M)
  1910.     { w -= 4;    /* sub header */
  1911.       if (w >= 0)
  1912.       { w *= 2;  /* 2 samps per byte */
  1913.         w += 1;  /* 1 samp in header */
  1914.         tmp_len += w;
  1915.       }
  1916.     }
  1917.     if (itype == XA_AUDIO_DVI_S)
  1918.     { w -= 8;    /* sub header */
  1919.       if (w >= 0)
  1920.       { w *= 1;  /* 1 samps per byte */
  1921.         w += 1;  /* 1 samp in header */
  1922.         tmp_len += w;
  1923.       }
  1924.     }
  1925.     totsamps = isamps = tmp_len;
  1926.   }
  1927.   else if ((itype & XA_AUDIO_TYPE_MASK) == XA_AUDIO_ADPCM)
  1928.   { xaLONG w,tmp_len;
  1929.  
  1930.     new_snd->blk_size = blockalign;
  1931.  
  1932.      /** calculate total samples in entire chunk for timing purposes */
  1933.     w = (ilen / blockalign);  
  1934.     tmp_len = sampsblock * w;          /* out len based on full blocks */
  1935.  
  1936.     w = ilen - (w * blockalign);    /* bytes remaining for partial blocks*/
  1937.     if (itype == XA_AUDIO_ADPCM_M)
  1938.     { 
  1939.       w -= (7 * 1);            /* subtract header */
  1940.       if (w >= 0)            /* if anything left */
  1941.       { w *= 2;                /* 2 samps per byte */
  1942.         w += 2;                /* plus samples in header */
  1943.         tmp_len += w;
  1944.       }
  1945.     }
  1946.     else if (itype == XA_AUDIO_ADPCM_S)
  1947.     { 
  1948.       w -= (7 * 2);            /* subtract header */
  1949.       if (w >= 0)            /* if anything left */
  1950.       { w *= 1;                /* 1 stereo samps per byte */
  1951.         w += 2;                /* plus samples in header */
  1952.         tmp_len += w;
  1953.       }
  1954.     }
  1955.     totsamps = isamps = tmp_len;
  1956.   }
  1957.   else
  1958.   {
  1959.     if (itype & XA_AUDIO_STEREO_MSK) bps *= 2;
  1960.     if (itype & XA_AUDIO_BPS_2_MSK) bps *= 2;
  1961.     totsamps = isamps = ilen / bps;
  1962.     new_snd->blk_size = bps;
  1963.   }
  1964.  
  1965.   new_snd->tot_samps = new_snd->samp_cnt = isamps;
  1966.   new_snd->tot_bytes = ilen;
  1967.   new_snd->byte_cnt = 0;
  1968.   new_snd->blk_cnt  = 0;
  1969.  
  1970.   new_snd->fpos = fpos;
  1971.   new_snd->type = itype;
  1972.   new_snd->flag = 0;
  1973.   new_snd->ifreq = ifreq;
  1974.   hfreq = (vaudiof->playrate)?(vaudiof->playrate):(ifreq);
  1975.  
  1976.   if (xa_forkit == xaTRUE) xa_forkit = XA_Video_Send2_Audio(XA_IPC_GET_CFREQ,
  1977.                     NULL,0,hfreq,2000,&hfreq);
  1978.   else hfreq = 0;
  1979.   if (xa_forkit == xaTRUE) xa_forkit = XA_Video_Send2_Audio(XA_IPC_GET_BSIZE,
  1980.                     NULL,0,0,2000,&xa_vaudio_hard_buff);
  1981.   if (xa_forkit == xaFALSE) return(xaFALSE);
  1982.  
  1983.   new_snd->hfreq = hfreq;
  1984.   new_snd->ch_size = xa_vaudio_hard_buff;
  1985.  
  1986.  
  1987.   /* Setup and return Chunk Start Time */
  1988.   new_snd->snd_time = *stime;
  1989.   { xaULONG tint;
  1990.     ftime = ((double)(totsamps) * 1000.0) / (double)(ifreq);
  1991.     tint = (xaULONG)(ftime);   /* get integer time */
  1992.     *stime += tint;
  1993.     ftime -= (double)(tint); /* get fraction time */
  1994.     *stimelo += (xaULONG)( ftime * (double)(1<<24) );
  1995.     while( (*stimelo) > (1<<24)) { *stime += 1; *stimelo -= (1<<24); }
  1996.   }
  1997.  
  1998.   /* Determine f2f inc */
  1999.   /** NEW STYLE  hfreq/ifreq **/
  2000.   fadj_freq = (double)(hfreq) * vaudiof->scale;
  2001.   finc = (double)(fadj_freq)/ (double)(ifreq);
  2002.   new_snd->inc = inc = (xaULONG)( finc * (double)(1<<24) );
  2003.   new_snd->inc_cnt = 0;
  2004.  
  2005.   /* Determine Chunk Time */
  2006.   ftime = ((double)(xa_vaudio_hard_buff) * 1000.0) / (double)(hfreq);
  2007.   new_snd->ch_time = (xaLONG)ftime;
  2008.   ftime -= (double)(new_snd->ch_time);
  2009.   new_snd->ch_timelo = (xaULONG)(ftime * (double)(1<<24));
  2010.  
  2011.   new_snd->prev = 0;
  2012.   new_snd->next = 0;
  2013.  
  2014. /* POD
  2015. fprintf(stderr,"Fork_Add_Sound itype %x\n",new_snd->type);
  2016. */
  2017.  
  2018.   /* Send SND HDR */
  2019.   if (xa_forkit == xaTRUE) xa_forkit = XA_Video_Send2_Audio(XA_IPC_SND_ADD,
  2020.     new_snd, (sizeof(XA_SND)), anim_hdr->file_num, 2000, &ret);
  2021.   if (xa_forkit == xaFALSE) return(xaFALSE);
  2022.   if (ret == xaFALSE) return(xaFALSE);
  2023.  
  2024.   if (isnd==0) ilen = 0;
  2025.  
  2026.   /* Send SND Buffer */
  2027.   if (xa_forkit == xaTRUE) 
  2028.   {
  2029.     xa_forkit = XA_Video_Send2_Audio(XA_IPC_SND_BUF, isnd, 
  2030.                     ilen, anim_hdr->file_num, 2000, &ret);
  2031.     free(isnd); isnd = 0;
  2032.     if (xa_forkit == xaFALSE) return(xaFALSE);
  2033.     if (ret == xaFALSE) return(xaFALSE);
  2034.   } 
  2035.   else 
  2036.   { free(isnd); isnd = 0; 
  2037.     return(xaFALSE); 
  2038.   }
  2039.  
  2040.  
  2041. /* new_snd->snd = isnd; */
  2042. /* new_snd->spec  */
  2043. /* new_snd->delta */
  2044.  
  2045.   return(xaTRUE);
  2046. #endif
  2047. }
  2048.  
  2049.  
  2050. /*******************************
  2051.  *
  2052.  ******************/
  2053. xaULONG XA_IPC_Sound(aud_hdr,new_snd)
  2054. XA_AUD_HDR *aud_hdr;
  2055. XA_SND *new_snd;
  2056.   xaULONG itype = new_snd->type;
  2057.  
  2058. /*POD
  2059. fprintf(stderr,"XA_IPC_Sound itype %x\n",itype);
  2060. */
  2061.   /* Figure out which conversion routine to use */
  2062.   switch(xa_audio_hard_type)
  2063.   {
  2064.     case XA_AUDIO_SUN_AU:
  2065.       {
  2066.     switch(itype)
  2067.     {
  2068.       case XA_AUDIO_SIGNED_1M:
  2069.       case XA_AUDIO_SIGNED_2MB:
  2070.       case XA_AUDIO_SIGNED_2ML:
  2071.       case XA_AUDIO_LINEAR_1M:
  2072.       case XA_AUDIO_LINEAR_2ML:
  2073.       case XA_AUDIO_LINEAR_2MB:
  2074.       case XA_AUDIO_SIGNED_1S:
  2075.       case XA_AUDIO_SIGNED_2SB:
  2076.       case XA_AUDIO_SIGNED_2SL:
  2077.       case XA_AUDIO_LINEAR_1S:
  2078.       case XA_AUDIO_LINEAR_2SL:
  2079.       case XA_AUDIO_LINEAR_2SB:
  2080.         new_snd->spec = 
  2081.             ((itype & XA_AUDIO_TYPE_MASK)==XA_AUDIO_LINEAR)?(0):(1);
  2082.         if (itype & XA_AUDIO_BPS_2_MSK)
  2083.           new_snd->spec |= (itype & XA_AUDIO_BIGEND_MSK)?(2):(4);
  2084.         new_snd->spec |= 8; /* SUN AU bit */
  2085.         if (itype & XA_AUDIO_STEREO_MSK)
  2086.             new_snd->delta = XA_ADecode_PCMXS_PCM1M;
  2087.         else
  2088.             new_snd->delta = XA_ADecode_PCMXM_PCM1M;
  2089.         break;
  2090.       case XA_AUDIO_SUN_AU:
  2091.         new_snd->delta = XA_ADecode_1M_1M;
  2092.         break;
  2093.       case XA_AUDIO_ADPCM_M:
  2094.             new_snd->spec = 2 | 4 | 8;
  2095.         new_snd->delta = XA_ADecode_ADPCMM_PCM2M;
  2096.         break;
  2097.       case XA_AUDIO_ADPCM_S:
  2098.             new_snd->spec = 2 | 4 | 8;
  2099.         new_snd->delta = XA_ADecode_ADPCMS_PCM2M;
  2100.         break;
  2101.       case XA_AUDIO_DVI_M:
  2102.             new_snd->spec = 2 | 4 | 8;
  2103.         new_snd->delta = XA_ADecode_DVIM_PCMxM;
  2104.         break;
  2105.       case XA_AUDIO_DVI_S:
  2106.             new_snd->spec = 2 | 4 | 8;
  2107.         new_snd->delta = XA_ADecode_DVIS_PCMxM;
  2108.         break;
  2109.       case XA_AUDIO_IMA4_M:
  2110.             new_snd->spec = 2 | 4 | 8;
  2111.         new_snd->delta = XA_ADecode_IMA4M_PCMxM;
  2112.         break;
  2113.       case XA_AUDIO_IMA4_S:
  2114.             new_snd->spec = 2 | 4 | 8;
  2115.         new_snd->delta = XA_ADecode_IMA4S_PCMxM;
  2116.         break;
  2117.       case XA_AUDIO_NOP:
  2118.             new_snd->spec = 2 | 4 | 8;
  2119.         new_snd->delta = XA_ADecode_NOP_PCMXM;
  2120.         break;
  2121.       default: 
  2122.         fprintf(stderr,"SUN_AU_AUDIO: Unsupported Software Type %x\n",
  2123.                 itype);
  2124.         return(xaFALSE);
  2125.         break;
  2126.     }
  2127.       }
  2128.       break;
  2129.  
  2130.     case XA_AUDIO_SIGNED_2ML:
  2131.     case XA_AUDIO_SIGNED_2MB:
  2132.       {
  2133.     switch(itype)
  2134.     {
  2135.       case XA_AUDIO_LINEAR_1M:      /* LIN1M -> SIN2M*  */
  2136.         if (xa_audio_hard_type & XA_AUDIO_BIGEND_MSK)
  2137.             new_snd->spec = 1;
  2138.         else    new_snd->spec = 2;
  2139.         new_snd->delta = XA_ADecode_PCM1M_PCM2M;
  2140.         break;
  2141.       case XA_AUDIO_LINEAR_1S:      /* LIN1S -> SIN2M*  */
  2142.         if (xa_audio_hard_type & XA_AUDIO_BIGEND_MSK)
  2143.             new_snd->spec = 1;
  2144.         else    new_snd->spec = 2;
  2145.         new_snd->delta = XA_ADecode_PCM1S_PCMxM;
  2146.         break;
  2147.       case XA_AUDIO_SIGNED_1S:      /* SIN1S -> SIN2M*  */
  2148.         if (xa_audio_hard_type & XA_AUDIO_BIGEND_MSK)
  2149.             new_snd->spec = 6;
  2150.         else    new_snd->spec = 5;
  2151.         new_snd->delta = XA_ADecode_PCM1S_PCMxM;
  2152.         break;
  2153.       case XA_AUDIO_SIGNED_1M:      /* SIN1M -> SIN2M*  */
  2154.         if (xa_audio_hard_type & XA_AUDIO_BIGEND_MSK)
  2155.             new_snd->spec = 2;
  2156.         else    new_snd->spec = 1;
  2157.         new_snd->delta = XA_ADecode_PCM1M_PCM2M;
  2158.         break;
  2159.       case XA_AUDIO_LINEAR_2MB:     /* LIN2M* -> SIN2M*  */
  2160.       case XA_AUDIO_LINEAR_2ML:
  2161.       case XA_AUDIO_SIGNED_2MB:     /* SIN2M* -> SIN2M*  */
  2162.       case XA_AUDIO_SIGNED_2ML:
  2163.       case XA_AUDIO_LINEAR_2SB:     /* LIN2S* -> SIN2M*  */
  2164.       case XA_AUDIO_LINEAR_2SL:
  2165.       case XA_AUDIO_SIGNED_2SB:     /* SIN2S* -> SIN2M*  */
  2166.       case XA_AUDIO_SIGNED_2SL:
  2167.           /* sign conversion? */
  2168.         if ( (itype & XA_AUDIO_TYPE_MASK) == XA_AUDIO_SIGNED)
  2169.             new_snd->spec = 0x10;
  2170.         else    new_snd->spec = 0x02;
  2171.           /* src endian? */
  2172.         new_snd->spec |= (itype & XA_AUDIO_BIGEND_MSK)?(0):(1);
  2173.           /* dst endian? */
  2174.         new_snd->spec |= 
  2175.             (xa_audio_hard_type & XA_AUDIO_BIGEND_MSK)?(0):(4);
  2176.           /* stereo? */
  2177.         new_snd->spec |= (itype & XA_AUDIO_STEREO_MSK)?(8):(0); 
  2178.         new_snd->delta = XA_ADecode_PCM2X_PCM2M;
  2179.         break;
  2180.       case XA_AUDIO_ADPCM_M:
  2181.         if (xa_audio_hard_type & XA_AUDIO_BIGEND_MSK)
  2182.             new_snd->spec = 0;
  2183.         else    new_snd->spec = 1;
  2184.         new_snd->delta = XA_ADecode_ADPCMM_PCM2M;
  2185.         break;
  2186.       case XA_AUDIO_ADPCM_S:
  2187.         if (xa_audio_hard_type & XA_AUDIO_BIGEND_MSK)
  2188.             new_snd->spec = 0;
  2189.         else    new_snd->spec = 1;
  2190.         new_snd->delta = XA_ADecode_ADPCMS_PCM2M;
  2191.         break;
  2192.       case XA_AUDIO_DVI_M:
  2193.         if (xa_audio_hard_type & XA_AUDIO_BIGEND_MSK)
  2194.             new_snd->spec = 0;
  2195.         else    new_snd->spec = 1;
  2196.         new_snd->delta = XA_ADecode_DVIM_PCMxM;
  2197.         break;
  2198.       case XA_AUDIO_DVI_S:
  2199.         if (xa_audio_hard_type & XA_AUDIO_BIGEND_MSK)
  2200.             new_snd->spec = 0;
  2201.         else    new_snd->spec = 1;
  2202.         new_snd->delta = XA_ADecode_DVIS_PCMxM;
  2203.         break;
  2204.       case XA_AUDIO_IMA4_M:
  2205.         if (xa_audio_hard_type & XA_AUDIO_BIGEND_MSK)
  2206.             new_snd->spec = 0;
  2207.         else    new_snd->spec = 1;
  2208.         new_snd->delta = XA_ADecode_IMA4M_PCMxM;
  2209.         break;
  2210.       case XA_AUDIO_IMA4_S:
  2211.         if (xa_audio_hard_type & XA_AUDIO_BIGEND_MSK)
  2212.             new_snd->spec = 0;
  2213.         else    new_snd->spec = 1;
  2214.         new_snd->delta = XA_ADecode_IMA4S_PCMxM;
  2215.         break;
  2216.       case XA_AUDIO_NOP:
  2217.         if (xa_audio_hard_type & XA_AUDIO_BIGEND_MSK)
  2218.             new_snd->spec = 0;
  2219.         else    new_snd->spec = 1;
  2220.         new_snd->delta = XA_ADecode_NOP_PCMXM;
  2221.         break;
  2222.       case XA_AUDIO_ULAWS:
  2223.         if (xa_audio_hard_type & XA_AUDIO_BIGEND_MSK)
  2224.             new_snd->spec = 0 | 8;
  2225.         else    new_snd->spec = 1 | 8;
  2226.         new_snd->delta = XA_ADecode_ULAWx_PCM2M;
  2227.         break;
  2228.       case XA_AUDIO_ULAW:
  2229.         if (xa_audio_hard_type & XA_AUDIO_BIGEND_MSK)
  2230.             new_snd->spec = 0;
  2231.         else    new_snd->spec = 1;
  2232.         new_snd->delta = XA_ADecode_ULAWx_PCM2M;
  2233.         break;
  2234.       case XA_AUDIO_ARMLAWS:
  2235.         if (xa_audio_hard_type & XA_AUDIO_BIGEND_MSK)
  2236.             new_snd->spec = 0 | 8;
  2237.         else    new_snd->spec = 1 | 8;
  2238.         new_snd->delta = XA_ADecode_ARMLAWx_PCM2M;
  2239.         break;
  2240.       case XA_AUDIO_ARMLAW:
  2241.         if (xa_audio_hard_type & XA_AUDIO_BIGEND_MSK)
  2242.             new_snd->spec = 0;
  2243.         else    new_snd->spec = 1;
  2244.         new_snd->delta = XA_ADecode_ARMLAWx_PCM2M;
  2245.         break;
  2246.       case XA_AUDIO_GSM_M:
  2247.         if (xa_audio_hard_type & XA_AUDIO_BIGEND_MSK)
  2248.             new_snd->spec = 0;
  2249.         else    new_snd->spec = 1;
  2250.         new_snd->delta = XA_ADecode_GSMM_PCMxM;
  2251.         break;
  2252.       default:
  2253.     fprintf(stderr,"F_AUDIO_SIN2M: Unsupported Software Type(%x)\n",
  2254.             itype);
  2255.         return(xaFALSE);
  2256.         break;
  2257.  
  2258.     }
  2259.       }
  2260.       break;
  2261.  
  2262.     case XA_AUDIO_LINEAR_1M:
  2263.       {
  2264.     switch(itype)
  2265.     {
  2266.       case XA_AUDIO_SIGNED_1M:
  2267.       case XA_AUDIO_SIGNED_2MB:
  2268.       case XA_AUDIO_SIGNED_2ML:
  2269.       case XA_AUDIO_LINEAR_1M:
  2270.       case XA_AUDIO_LINEAR_2ML:
  2271.       case XA_AUDIO_LINEAR_2MB:
  2272.       case XA_AUDIO_SIGNED_1S:
  2273.       case XA_AUDIO_SIGNED_2SB:
  2274.       case XA_AUDIO_SIGNED_2SL:
  2275.       case XA_AUDIO_LINEAR_1S:
  2276.       case XA_AUDIO_LINEAR_2SL:
  2277.       case XA_AUDIO_LINEAR_2SB:
  2278.         new_snd->spec = 
  2279.             ((itype & XA_AUDIO_TYPE_MASK)==XA_AUDIO_LINEAR)?(0):(1);
  2280.         if (itype & XA_AUDIO_BPS_2_MSK)
  2281.           new_snd->spec |= (itype & XA_AUDIO_BIGEND_MSK)?(2):(4);
  2282.         if (itype & XA_AUDIO_STEREO_MSK)
  2283.             new_snd->delta = XA_ADecode_PCMXS_PCM1M;
  2284.         else
  2285.             new_snd->delta = XA_ADecode_PCMXM_PCM1M;
  2286.         break;
  2287.       case XA_AUDIO_ADPCM_M:
  2288.         new_snd->spec = 2 | 4;  /* 1 byte output */
  2289.         new_snd->delta = XA_ADecode_ADPCMM_PCM2M;
  2290.         break;
  2291.       case XA_AUDIO_ADPCM_S:
  2292.         new_snd->spec = 2 | 4;  /* 1 byte output */
  2293.         new_snd->delta = XA_ADecode_ADPCMS_PCM2M;
  2294.         break;
  2295.       case XA_AUDIO_DVI_M:
  2296.         new_snd->spec = 2 | 4;  /* 1 byte output */
  2297.         new_snd->delta = XA_ADecode_DVIM_PCMxM;
  2298.         break;
  2299.       case XA_AUDIO_DVI_S:
  2300.         new_snd->spec = 2 | 4;  /* 1 byte output */
  2301.         new_snd->delta = XA_ADecode_DVIS_PCMxM;
  2302.         break;
  2303.       case XA_AUDIO_IMA4_M:
  2304.         new_snd->spec = 2 | 4;  /* 1 byte output */
  2305.         new_snd->delta = XA_ADecode_IMA4M_PCMxM;
  2306.         break;
  2307.       case XA_AUDIO_IMA4_S:
  2308.         new_snd->spec = 2 | 4;  /* 1 byte output */
  2309.         new_snd->delta = XA_ADecode_IMA4S_PCMxM;
  2310.         break;
  2311.       case XA_AUDIO_NOP:
  2312.         new_snd->spec = 2 | 4;  /* 1 byte output */
  2313.         new_snd->delta = XA_ADecode_NOP_PCMXM;
  2314.         break;
  2315.       default:
  2316.         fprintf(stderr,"AUDIO_LIN1M: Unsupported Software Type\n");
  2317.         return(xaFALSE);
  2318.         break;
  2319.     }
  2320.       }
  2321.       break;
  2322.  
  2323.     case XA_AUDIO_LINEAR_2ML:
  2324.     case XA_AUDIO_LINEAR_2MB:
  2325.       {
  2326.     switch(itype)
  2327.     {
  2328.       case XA_AUDIO_LINEAR_1M:      /* LIN1M -> LIN2M*  */
  2329.         new_snd->spec = 0;
  2330.         new_snd->delta = XA_ADecode_PCM1M_PCM2M;
  2331.         break;
  2332.       case XA_AUDIO_SIGNED_1M:      /* SIN1M -> LIN2M*  */
  2333.         if (xa_audio_hard_type & XA_AUDIO_BIGEND_MSK)
  2334.             new_snd->spec = 1;
  2335.         else    new_snd->spec = 2;
  2336.         new_snd->delta = XA_ADecode_PCM1M_PCM2M;
  2337.         break;
  2338.       case XA_AUDIO_LINEAR_1S:      /* LIN1S -> LIN2M*  */
  2339.         if (xa_audio_hard_type & XA_AUDIO_BIGEND_MSK)
  2340.             new_snd->spec = 0;
  2341.         else    new_snd->spec = 0;
  2342.         new_snd->delta = XA_ADecode_PCM1S_PCMxM;
  2343.         break;
  2344.       case XA_AUDIO_SIGNED_1S:      /* SIN1S -> LIN2M*  */
  2345.         if (xa_audio_hard_type & XA_AUDIO_BIGEND_MSK)
  2346.             new_snd->spec = 5;
  2347.         else    new_snd->spec = 6;
  2348.         new_snd->delta = XA_ADecode_PCM1S_PCMxM;
  2349.         break;
  2350.       case XA_AUDIO_LINEAR_2MB:     /* LIN2M* -> LIN2M*  */
  2351.       case XA_AUDIO_LINEAR_2ML:
  2352.       case XA_AUDIO_SIGNED_2MB:     /* SIN2M* -> LIN2M*  */
  2353.       case XA_AUDIO_SIGNED_2ML:
  2354.       case XA_AUDIO_LINEAR_2SB:     /* LIN2S* -> LIN2M*  */
  2355.       case XA_AUDIO_LINEAR_2SL:
  2356.       case XA_AUDIO_SIGNED_2SB:     /* SIN2S* -> LIN2M*  */
  2357.       case XA_AUDIO_SIGNED_2SL:
  2358.           /* sign conversion? */
  2359.         if ( (itype & XA_AUDIO_TYPE_MASK) == XA_AUDIO_SIGNED)
  2360.             new_snd->spec = 0x12;
  2361.         else    new_snd->spec = 0;
  2362.           /* src endian? */
  2363.         new_snd->spec |= (itype & XA_AUDIO_BIGEND_MSK)?(0):(1);
  2364.           /* dst endian? */
  2365.         new_snd->spec |= 
  2366.             (xa_audio_hard_type & XA_AUDIO_BIGEND_MSK)?(0):(4);
  2367.           /* stereo? */
  2368.         new_snd->spec |= (itype & XA_AUDIO_STEREO_MSK)?(8):(0); 
  2369.         new_snd->delta = XA_ADecode_PCM2X_PCM2M;
  2370.         break;
  2371.       case XA_AUDIO_ADPCM_M:
  2372.         if (xa_audio_hard_type & XA_AUDIO_BIGEND_MSK)
  2373.             new_snd->spec = 2;
  2374.         else    new_snd->spec = 3;
  2375.         new_snd->delta = XA_ADecode_ADPCMM_PCM2M;
  2376.         break;
  2377.       case XA_AUDIO_ADPCM_S:
  2378.         if (xa_audio_hard_type & XA_AUDIO_BIGEND_MSK)
  2379.             new_snd->spec = 2;
  2380.         else    new_snd->spec = 3;
  2381.         new_snd->delta = XA_ADecode_ADPCMS_PCM2M;
  2382.         break;
  2383.       case XA_AUDIO_DVI_M:
  2384.         if (xa_audio_hard_type & XA_AUDIO_BIGEND_MSK)
  2385.             new_snd->spec = 2;
  2386.         else    new_snd->spec = 3;
  2387.         new_snd->delta = XA_ADecode_DVIM_PCMxM;
  2388.         break;
  2389.       case XA_AUDIO_DVI_S:
  2390.         if (xa_audio_hard_type & XA_AUDIO_BIGEND_MSK)
  2391.             new_snd->spec = 2;
  2392.         else    new_snd->spec = 3;
  2393.         new_snd->delta = XA_ADecode_DVIS_PCMxM;
  2394.         break;
  2395.       case XA_AUDIO_IMA4_M:
  2396.         if (xa_audio_hard_type & XA_AUDIO_BIGEND_MSK)
  2397.             new_snd->spec = 2;
  2398.         else    new_snd->spec = 3;
  2399.         new_snd->delta = XA_ADecode_IMA4M_PCMxM;
  2400.         break;
  2401.       case XA_AUDIO_IMA4_S:
  2402.         if (xa_audio_hard_type & XA_AUDIO_BIGEND_MSK)
  2403.             new_snd->spec = 2;
  2404.         else    new_snd->spec = 3;
  2405.         new_snd->delta = XA_ADecode_IMA4S_PCMxM;
  2406.         break;
  2407.       case XA_AUDIO_NOP:
  2408.         if (xa_audio_hard_type & XA_AUDIO_BIGEND_MSK)
  2409.             new_snd->spec = 2;
  2410.         else    new_snd->spec = 3;
  2411.         new_snd->delta = XA_ADecode_NOP_PCMXM;
  2412.         break;
  2413.       default:
  2414.         fprintf(stderr,"AUDIO_LIN2M: Unsupported Software Type\n");
  2415.         return(xaFALSE);
  2416.         break;
  2417.     }
  2418.       }
  2419.       break;
  2420.  
  2421.  
  2422.     default: 
  2423.       FREE(new_snd,0x507); new_snd = 0;
  2424.       fprintf(stderr,"AUDIO: Unknown Hardware Type\n");
  2425.       return(xaFALSE);
  2426.       break;
  2427.   }
  2428.  
  2429.   /** Set up prev pointer */
  2430.   if (aud_hdr->first_snd==0) new_snd->prev = 0;
  2431.   else new_snd->prev = aud_hdr->last_snd;
  2432.  
  2433.   /** Set up next pointer */
  2434.   if (aud_hdr->first_snd == 0)    aud_hdr->first_snd = new_snd;
  2435.   if (aud_hdr->last_snd)    aud_hdr->last_snd->next = new_snd;
  2436.   aud_hdr->last_snd = new_snd;
  2437.   return(xaTRUE);
  2438. }
  2439.