home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2004 March / PCWELT_3_2004.ISO / pcwsoft / flaskmpeg_078_39_src.z.exe / flaskmpeg / Audio / MPEG / AMPDecoder.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-28  |  6.4 KB  |  287 lines

  1. #include <crtdbg.h>
  2.  
  3. #include "AMPDecoder.h"
  4.  
  5. IAMPDecoder *CreateAMPDecoder() {
  6.     return new AMPDecoder();
  7. }
  8.  
  9. AMPDecoder::AMPDecoder() {
  10. }
  11.  
  12. AMPDecoder::~AMPDecoder() {
  13. }
  14.  
  15. void AMPDecoder::Destroy() {
  16.     delete this;
  17. }
  18.  
  19. char *AMPDecoder::GetAmpVersionString() {
  20.     return "NekoAmp 1.4, based on FreeAmp 1.1.0 source";
  21. }
  22.  
  23. char *AMPDecoder::getErrorString(int err) {
  24.     switch(err) {
  25.     case ERR_NONE:        return "no error";
  26.     case ERR_EOF:        return "unexpected end of stream";
  27.     case ERR_READ:        return "read error";
  28.     case ERR_MPEG25:    return "cannot process Fraunhofer-IIS MPEG 2.5 stream";
  29.     case ERR_FREEFORM:    return "cannot process free-form streams";
  30.     case ERR_SYNC:        return "sync error";
  31.     case ERR_INTERNAL:    return "**Internal Error**";
  32.     case ERR_INCOMPLETEFRAME:    return "not enough data to decode frame";
  33.     }
  34.  
  35.     return "unknown error";
  36. }
  37.  
  38. void AMPDecoder::Init() {
  39.     int i;
  40.     float *pp;
  41.  
  42.     winptr = 0;
  43.  
  44.     for(i=0; i<512; i++)
  45.         window[0][i] = window[1][i] = 0.0f;
  46.  
  47.     pp = (float *)prevblck;
  48.  
  49.     for(i=0; i<SBLIMIT*SSLIMIT*2; i++) {
  50.         *pp++ = 0.0f;
  51.     }
  52.  
  53.     resetbits(0);
  54.  
  55.     Initialize();
  56. }
  57.  
  58. void AMPDecoder::setSource(IAMPBitsource *pSource) {
  59.     this->pSource = pSource;
  60. }
  61.  
  62. void AMPDecoder::setDestination(short *psDest) {
  63.     this->psDest = psDest;
  64. }
  65.  
  66. long AMPDecoder::getSampleCount() {
  67.     return lSampleCount;
  68. }
  69.  
  70. void AMPDecoder::Reset() {
  71. }
  72.  
  73. //////////////////////////////////////
  74.  
  75. static bool isValidMPEGHeader(long hdr) {
  76.     // 0000F0FF 12 bits    sync mark
  77.     //
  78.     // 00000800  1 bit    version
  79.     // 00000600  2 bits    layer (3 = layer I, 2 = layer II, 1 = layer III)
  80.     // 00000100  1 bit    error protection (0 = enabled)
  81.     //
  82.     // 00F00000  4 bits    bitrate_index
  83.     // 000C0000  2 bits    sampling_freq
  84.     // 00020000  1 bit    padding
  85.     // 00010000  1 bit    extension
  86.     //
  87.     // C0000000  2 bits    mode (0=stereo, 1=joint stereo, 2=dual channel, 3=mono)
  88.     // 30000000  2 bits    mode_ext
  89.     // 08000000  1 bit    copyright
  90.     // 04000000  1 bit    original
  91.     // 03000000  2 bits    emphasis
  92.  
  93.     // sync mark?
  94.  
  95.     if ((hdr & 0xf0ff) != 0xf0ff)
  96.         return false;
  97.     
  98.     // 00 for layer ("layer 4") is not valid
  99.     if (!(hdr & 0x00000600))
  100.         return false;
  101.  
  102.     // 1111 for bitrate is not valid
  103.     if ((hdr & 0x00F00000) == 0x00F00000)
  104.         return false;
  105.  
  106.     // 11 for sampling frequency is not valid
  107.     if ((hdr & 0x000C0000) == 0x000C0000)
  108.         return false;
  109.  
  110.     // Looks okay to me...
  111.     return true;
  112. }
  113.  
  114.  
  115. static const int bitrates2[3][15] = {
  116.     {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, },
  117.     {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, },
  118.     {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, },
  119. };
  120.  
  121. static const int bitrates[3][15] = {
  122.           {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448},
  123.           {0,32,48,56,64,80,96,112,128,160,192,224,256,320,384},
  124.           {0,32,40,48,56,64,80,96,112,128,160,192,224,256,320}
  125.         };
  126.  
  127. static const long samp_freq[4] = {44100, 48000, 32000, 0};
  128.  
  129. void AMPDecoder::ReadHeader() {
  130.     union {
  131.         char buf[4];
  132.         long hdr;
  133.     };
  134.  
  135.     // MPEG headers are 4 bytes.
  136.  
  137.     if (4 != pSource->read(buf, 4))
  138.         throw (int)ERR_EOF;
  139.  
  140.     // Is it a valid header?  If not, figure out how many
  141.     // bytes obviously aren't sync and advance by that much.
  142.  
  143.     while(!isValidMPEGHeader(hdr)) {
  144.         int advance = 1;
  145.  
  146.         if (buf[1] != 0xFF || (buf[2]&0xF0) != 0xF0) {
  147.             if (buf[2] != 0xFF || (buf[3]&0xF0) != 0xF0) {
  148.                 if (buf[3] != 0xFF)
  149.                     advance = 4;
  150.                 else
  151.                     advance = 3;
  152.             } else
  153.                 advance = 2;
  154.         }
  155.  
  156.         *(long *)buf >>= advance<<3;
  157.  
  158.         if (advance != pSource->read(buf+4-advance, advance))
  159.             throw (int)ERR_EOF;
  160.     }
  161.  
  162.     // Yay!  A valid MPEG header!  Parse it!
  163.  
  164.     // 0000F0FF 12 bits    sync mark
  165.     //
  166.     // 00000800  1 bit    version
  167.     // 00000600  2 bits    layer (3 = layer I, 2 = layer II, 1 = layer III)
  168.     // 00000100  1 bit    error protection (0 = enabled)
  169.     //
  170.     // 00F00000  4 bits    bitrate_index
  171.     // 000C0000  2 bits    sampling_freq
  172.     // 00020000  1 bit    padding
  173.     // 00010000  1 bit    extension
  174.     //
  175.     // C0000000  2 bits    mode (0=stereo, 1=joint stereo, 2=dual channel, 3=mono)
  176.     // 30000000  2 bits    mode_ext
  177.     // 08000000  1 bit    copyright
  178.     // 04000000  1 bit    original
  179.     // 03000000  2 bits    emphasis
  180.  
  181.     // _RPT1(0,"Header: %08lx\n", hdr);
  182.  
  183.     is_mpeg2            = !(hdr & 0x00000800);
  184.     layer                = 4 - (hdr>>9)&3;
  185.     is_errorprotected    = !(hdr & 0x00000100);
  186.     br_index            = (hdr>>20)&15;
  187.     bitrate                = (is_mpeg2 ? bitrates2 : bitrates)[layer-1][br_index];
  188.     sr_index            = (hdr>>18)&3;
  189.     frequency            = samp_freq[sr_index];
  190.  
  191.     if (is_mpeg2)
  192.         frequency>>=1;
  193.  
  194.     is_padded            = !!(hdr & 0x00020000);
  195.     is_extended            = !!(hdr & 0x00010000);
  196.     mode                = (hdr>>30)&3;
  197.     mode_ext            = (hdr>>28)&3;
  198.     is_copyrighted        = !!(hdr & 0x08000000);
  199.     is_original            = !!(hdr & 0x04000000);
  200.     emphasis            = (hdr>>24)&3;
  201.  
  202.     if (mode == MODE_MONO)
  203.         channels = 1;
  204.     else
  205.         channels = 2;
  206.  
  207.     // Compute the frame size, not including header and layer III side info
  208.  
  209.     if (layer == 1) {    // slots in layer I are 4 bytes
  210.         frame_size = 4 * (12000 * bitrate / frequency);
  211.         if (is_padded)
  212.             frame_size+=4;
  213.     } else {
  214.         if (is_mpeg2)
  215.             frame_size = 72000 * bitrate / frequency;
  216.         else
  217.             frame_size = 144000 * bitrate / frequency;
  218.  
  219.         if (is_padded)
  220.             ++frame_size;
  221.     }
  222.  
  223.     frame_size -= 4;
  224.  
  225.     // if we're processing Layer III, subtract size of side info as well
  226.  
  227.     if (layer == 3) {
  228.         if (is_mpeg2)
  229.             if (mode == MODE_MONO)
  230.                 sideinfo_size = 9;
  231.             else
  232.                 sideinfo_size = 17;
  233.         else
  234.             if (mode == MODE_MONO)
  235.                 sideinfo_size = 17;
  236.             else
  237.                 sideinfo_size = 32;
  238.  
  239.         frame_size -= sideinfo_size;
  240.     }
  241.  
  242.     // Read in CRC if we have error protection
  243.  
  244.     if (is_errorprotected) {
  245.         unsigned short CRC;
  246.  
  247.         if (2 != pSource->read(&CRC, 2))
  248.             throw (int)ERR_READ;
  249.  
  250.         frame_size -= 2;
  251.     }
  252.  
  253.     // Read in Layer3 side info
  254.  
  255.     if (layer == 3)
  256.         L3_GetSideInfo();
  257.  
  258. }
  259.  
  260. void AMPDecoder::getStreamInfo(AMPStreamInfo *pasi) {
  261.     pasi->lBitrate        = bitrate;
  262.     pasi->lSamplingFreq    = frequency;
  263.     pasi->nLayer        = layer;
  264.     pasi->nMPEGVer        = is_mpeg2 ? 2 : 1;
  265.     pasi->fStereo        = mode != MODE_MONO;
  266. }
  267.  
  268. void AMPDecoder::PrereadFrame() {
  269.     switch(layer) {
  270.     case 1:    L1_PrereadFrame(); break;
  271.     case 2:    L2_PrereadFrame(); break;
  272.     case 3:    L3_PrereadFrame(); break;
  273.     default: __assume(false);
  274.     }
  275. }
  276.  
  277. bool AMPDecoder::DecodeFrame() {
  278.     lSampleCount = 0;
  279.  
  280.     switch(layer) {
  281.     case 1:    return L1_DecodeFrame();
  282.     case 2: return L2_DecodeFrame();
  283.     case 3: return L3_DecodeFrame();
  284.     default:    __assume(false);
  285.     }
  286. }
  287.