home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 275 / DPCS0111DVD.ISO / Toolkit / Audio-Visual / VirtualDub / Source / VirtualDub-1.9.10-src.7z / src / Priss / source / engine.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2009-09-14  |  7.9 KB  |  317 lines

  1. //    Priss (NekoAmp 2.0) - MPEG-1/2 audio decoding library
  2. //    Copyright (C) 2003 Avery Lee
  3. //
  4. //    This program is free software; you can redistribute it and/or modify
  5. //    it under the terms of the GNU General Public License as published by
  6. //    the Free Software Foundation; either version 2 of the License, or
  7. //    (at your option) any later version.
  8. //
  9. //    This program is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. //    GNU General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU General Public License
  15. //    along with this program; if not, write to the Free Software
  16. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #include <math.h>
  19. #include <vd2/system/vdtypes.h>
  20.  
  21. #include "engine.h"
  22.  
  23. IVDMPEGAudioDecoder *VDCreateMPEGAudioDecoder() {
  24.     return new VDMPEGAudioDecoder;
  25. }
  26.  
  27. VDMPEGAudioDecoder::VDMPEGAudioDecoder()
  28.     : mpSource(NULL)
  29.     , mpPolyphaseFilter(NULL)
  30. {
  31.     Reset();
  32. }
  33.  
  34. VDMPEGAudioDecoder::~VDMPEGAudioDecoder() {
  35.     delete mpPolyphaseFilter;
  36. }
  37.  
  38. void VDMPEGAudioDecoder::Init() {
  39.     unsigned i,j;
  40.  
  41.     // layer II initialization
  42.  
  43.     for(i=0; i<64; ++i)
  44.         mL2Scalefactors[i] = (float)(2.0 / pow(2.0, i/3.0));
  45.  
  46.     for(i=0; i<32; ++i) {
  47.         mL2Ungroup3[i][0] = (i % 3) - 1;
  48.         mL2Ungroup3[i][1] = ((i / 3) % 3) - 1;
  49.         mL2Ungroup3[i][2] = ((i / 9) % 3) - 1;
  50.     }
  51.  
  52.     // layer III initialization
  53.  
  54.     for(i=0; i<36; ++i)
  55.         mL3Windows[0][i] = (float)sin((3.1415926535897932/36.0)*(i+0.5));
  56.  
  57.     for(i=0; i<18; ++i) {
  58.         mL3Windows[1][i] = (float)sin((3.1415926535897932/36.0)*(i+0.5));
  59.         mL3Windows[3][i+18] = (float)sin((3.1415926535897932/36.0)*(i+18+0.5));
  60.     }
  61.  
  62.     for(i=0; i<6; ++i) {
  63.         mL3Windows[1][i+18] = 1.0f;
  64.         mL3Windows[1][i+24] = (float)sin((3.1415926535897932/12.0)*(i+6+0.5));
  65.         mL3Windows[1][i+30] = 0.0f;
  66.  
  67.         mL3Windows[3][i] = 0.0f;
  68.         mL3Windows[3][i+6] = (float)sin((3.1415926535897932/12.0)*(i+0.5));
  69.         mL3Windows[3][i+12] = 1.0f;
  70.     }
  71.  
  72.     for(i=0; i<12; ++i) {
  73.         mL3Windows[2][i] = (float)sin((3.1415926535897932/12.0)*(i+0.5));
  74.     }
  75.  
  76.     static const float coeff_idct_to_imdct[18]={        // 1/[2 cos (pi/72)(2i+1)]
  77.         0.50047634258166f,
  78.         0.50431448029008f,
  79.         0.51213975715725f,
  80.         0.52426456257041f,
  81.         0.54119610014620f,
  82.         0.56369097343317f,
  83.         0.59284452371708f,
  84.         0.63023620700513f,
  85.         0.67817085245463f,
  86.         0.74009361646113f,
  87.         0.82133981585229f,
  88.         0.93057949835179f,
  89.         1.08284028510010f,
  90.         1.30656296487638f,
  91.         1.66275476171152f,
  92.         2.31011315767265f,
  93.         3.83064878777019f,
  94.         11.46279281302667f,
  95.     };
  96.  
  97.     for(j=0; j<4; ++j) {
  98.         if (j==2)
  99.             continue;
  100.         for(i=0; i<9; ++i) {
  101.             mL3Windows[j][i] *= coeff_idct_to_imdct[i+9];
  102.             mL3Windows[j][i+9] *= coeff_idct_to_imdct[17-i];
  103.             mL3Windows[j][i+18] *= -coeff_idct_to_imdct[8-i];
  104.             mL3Windows[j][i+27] *= -coeff_idct_to_imdct[i];
  105.         }
  106.     }
  107.  
  108.     for(i=0; i<256; ++i) {
  109.         float x = powf(fabsf(i - 128.0f), 4.0f/3.0f);
  110.  
  111.         if (i < 128)
  112.             x = -x;
  113.  
  114.         mL3Pow43Tab[i] = x;
  115.     }
  116. }
  117.  
  118. void VDMPEGAudioDecoder::SetSource(IVDMPEGAudioBitsource *pSource) {
  119.     mpSource = pSource;
  120. }
  121.  
  122. void VDMPEGAudioDecoder::SetDestination(sint16 *psDest) {
  123.     mpSampleDst = psDest;
  124. }
  125.  
  126. uint32 VDMPEGAudioDecoder::GetSampleCount() {
  127.     return mSamplesDecoded;
  128. }
  129.  
  130. uint32 VDMPEGAudioDecoder::GetFrameDataSize() {
  131.     return mFrameDataSize;
  132. }
  133.  
  134. void VDMPEGAudioDecoder::GetStreamInfo(VDMPEGAudioStreamInfo *pasi) {
  135.     *pasi = mHeader;
  136. }
  137.  
  138. const char *VDMPEGAudioDecoder::GetErrorString(int err) {
  139.     switch(err) {
  140.     case ERR_NONE:                return "no error";
  141.     case ERR_EOF:                return "end of file";
  142.     case ERR_READ:                return "read error";
  143.     case ERR_MPEG25:            return "cannot decode MPEG-2.5 streams";
  144.     case ERR_FREEFORM:            return "cannot decode free-form streams";
  145.     case ERR_SYNC:                return "sync error";
  146.     case ERR_INTERNAL:            return "internal error";
  147.     case ERR_INCOMPLETEFRAME:    return "incomplete frame";
  148.     case ERR_INVALIDDATA:        return "invalid or out-of-spec data encountered";
  149.     default:                    return "unknown error code";
  150.     }
  151. }
  152.  
  153. void VDMPEGAudioDecoder::Reset() {
  154.     if (!mpPolyphaseFilter || mpPolyphaseFilter->ShouldRecreate()) {
  155.         delete mpPolyphaseFilter;
  156.         mpPolyphaseFilter = VDMPEGAudioPolyphaseFilter::Create();
  157.     }
  158.  
  159.     mpPolyphaseFilter->Reset();
  160.  
  161.     mL3BufferPos = 0;
  162.     mL3BufferLevel = 0;
  163.  
  164.     memset(mL3OverlapBuffer, 0, sizeof mL3OverlapBuffer);
  165. }
  166.  
  167. void VDMPEGAudioDecoder::ReadHeader() {
  168.     // syncword            12 bits        0000F0FF
  169.     // id                1 bit        00000800
  170.     // layer            2 bits        00000600
  171.     // protection        1 bit        00000100
  172.     // bitrate            4 bits        00F00000
  173.     // sampling rate    2 bits        000C0000
  174.     // padding            1 bit        00020000
  175.     // private bit        1 bit        00010000
  176.     // mode                2 bits        C0000000
  177.     // mode extension    2 bits        30000000
  178.     // copyright        1 bit        08000000
  179.     // original/copy    1 bit        04000000
  180.     // emphasis            2 bits        03000000
  181.  
  182.     union {
  183.         char    buf[4];
  184.         uint32    v;
  185.     } hdr;
  186.     int bytes = 0;
  187.  
  188.     for(;;) {
  189.         while(bytes < 4) {
  190.             int r = mpSource->read(hdr.buf+bytes, 4-bytes);
  191.             if (r<0)
  192.                 throw (int)ERR_READ;
  193.             else if (!r)
  194.                 throw (int)ERR_EOF;
  195.             bytes += r;
  196.         }
  197.  
  198.         if ((hdr.v & 0xe0ff) == 0xe0ff)
  199.             break;
  200.  
  201.         hdr.buf[0] = hdr.buf[1];
  202.         hdr.buf[1] = hdr.buf[2];
  203.         hdr.buf[2] = hdr.buf[3];
  204.         --bytes;
  205.     }
  206.  
  207.     // determine frame size
  208.  
  209.     static const int sBitrateTable[2][3][16]={
  210.         {
  211.             { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0 },    // MPEG-1 layer I
  212.             { 0, 32, 48, 56,  64,  80,  96, 112, 128, 160, 192, 224, 256, 320, 384, 0 },    // MPEG-1 layer II
  213.             { 0, 32, 40, 48,  56,  64,  80,  96, 112, 128, 160, 192, 224, 256, 320, 0 },    // MPEG-1 layer III
  214.         },
  215.         {
  216.             { 0, 32, 48, 56,  64,  80,  96, 112, 128, 144, 160, 176, 192, 224, 256, 0 },    // MPEG-2 layer I
  217.             { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128, 144, 160, 0 },    // MPEG-2 layer II
  218.             { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128, 144, 160, 0 },    // MPEG-2 layer III
  219.         }
  220.     };
  221.  
  222.     static const int sFrequencyTable[2][4]={{44100,48000,32000,0}, {22050,24000,16000,0}};
  223.  
  224.     bool is_mpeg2    = (hdr.v & 0x800) == 0;
  225.     bool is_mpeg25    = (hdr.v & 0x1000) == 0;
  226.     int layer        = 4 - ((hdr.v>>9)&3);
  227.     int bitrate_idx    = (hdr.v>>20)&15;
  228.     int freq_idx    = (hdr.v>>18)&3;
  229.     int padding        = (hdr.v>>17)&1;
  230.     int bitrate        = sBitrateTable[is_mpeg2][layer-1][bitrate_idx];
  231.     int freq        = sFrequencyTable[is_mpeg2][freq_idx];
  232.  
  233.     if (is_mpeg25)
  234.         freq >>= 1;
  235.  
  236.     mHeader.fStereo            = ((hdr.v>>30)&3) != 3;
  237.     mHeader.lBitrate        = bitrate;
  238.     mHeader.lSamplingFreq    = freq;
  239.     mHeader.nLayer            = layer;
  240.     mHeader.nMPEGVer        = (hdr.v & 0x800) != 0 ? 1 : 2;
  241.  
  242.     if (!bitrate_idx)
  243.         throw (int)ERR_FREEFORM;
  244.  
  245.     if (!freq || !bitrate)
  246.         throw (int)ERR_INVALIDDATA;
  247.  
  248.     if (layer == 1)
  249.         mFrameDataSize = 4*(12000*bitrate/freq + padding);
  250.     else {
  251.         if (is_mpeg2 && layer == 3)
  252.             mFrameDataSize = (72000*bitrate/freq + padding);
  253.         else
  254.             mFrameDataSize = (144000*bitrate/freq + padding);
  255.     }
  256.  
  257.     // take off header size
  258.     mFrameDataSize -= 4;
  259.  
  260.     // read CRC
  261.     if (!(hdr.v&0x100)) {
  262.         char crc[2];
  263.  
  264.         int r = mpSource->read(crc, 2);
  265.         if (r < 0)
  266.             throw (int)ERR_READ;
  267.         else if (r < 2)
  268.             throw (int)ERR_EOF;
  269.  
  270.         mFrameDataSize -= 2;
  271.     }
  272.  
  273.     mBitrateIndex        = bitrate_idx;
  274.     mSamplingRateIndex    = freq_idx;
  275.     mMode            = (hdr.v>>30)&3;
  276.     mModeExtension    = (hdr.v>>28)&3;
  277. }
  278.  
  279. void VDMPEGAudioDecoder::PrereadFrame() {
  280.     VDASSERT(mFrameDataSize < sizeof mFrameBuffer);
  281.     int r = mpSource->read(mFrameBuffer, mFrameDataSize);
  282.  
  283.     if (r < (int)mFrameDataSize) {
  284.         if (r < 0)
  285.             throw (int)ERR_READ;
  286.         else
  287.             throw (int)ERR_INCOMPLETEFRAME;
  288.     }
  289.  
  290.     if (mHeader.nLayer == 3)
  291.         PrereadLayerIII();
  292.     else
  293.         mL3BufferLevel = 0;
  294. }
  295.  
  296. bool VDMPEGAudioDecoder::DecodeFrame() {
  297.     mSamplesDecoded = 0;
  298.  
  299.     PrereadFrame();
  300.  
  301.     switch(mHeader.nLayer) {
  302.     case 1:
  303.         return DecodeLayerI();
  304.     case 2:
  305.         return DecodeLayerII();
  306.     case 3:
  307.         return DecodeLayerIII();
  308.     }
  309.  
  310.     return false;
  311. }
  312.  
  313. void VDMPEGAudioDecoder::ConcealFrame() {
  314.     mpPolyphaseFilter->Reset();
  315.     memset(mL3OverlapBuffer, 0, sizeof mL3OverlapBuffer);
  316. }
  317.