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 / MPEGDec.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-28  |  2.6 KB  |  114 lines

  1. /* 
  2.  *  MPEGDec.cpp  -  MPEG audio decoding
  3.  *
  4.  *    Copyright (C) Alberto Vigata - January 2000 - ultraflask@yahoo.com
  5.  *
  6.  *  This file is part of FlasKMPEG, a free MPEG to MPEG/AVI converter
  7.  *    
  8.  *  FlasKMPEG is free software; you can redistribute it and/or modify
  9.  *  it under the terms of the GNU General Public License as published by
  10.  *  the Free Software Foundation; either version 2, or (at your option)
  11.  *  any later version.
  12.  *   
  13.  *  FlasKMPEG is distributed in the hope that it will be useful,
  14.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  *  GNU General Public License for more details.
  17.  *   
  18.  *  You should have received a copy of the GNU General Public License
  19.  *  along with GNU Make; see the file COPYING.  If not, write to
  20.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  21.  *
  22.  */
  23.  
  24. #include <memory.h>
  25. #include "MPEGDec.h"
  26. #include <windows.h>
  27.  
  28. //////////////////////////////////////////////////////////////////////
  29. // Construction/Destruction
  30. //////////////////////////////////////////////////////////////////////
  31.  
  32. CMPEGDec::CMPEGDec()
  33. {
  34.  
  35. }
  36.  
  37. CMPEGDec::~CMPEGDec()
  38. {
  39.  
  40. }
  41. int CMPEGDec::Start()
  42. {
  43.     in_ptr     =0;
  44.     frame_size =0;
  45.     if( (decoder=CreateAMPDecoder()) ){
  46.         decoder->Init();
  47.         decoder->setSource(this);
  48.         return 1;
  49.     }
  50.     else
  51.         return 0;
  52. }
  53.  
  54. int CMPEGDec::Stop()
  55. {
  56.     return 0;
  57. }
  58.  
  59. // Audio decoder data reader
  60. int CMPEGDec::read(void *buffer, int bytes){
  61.     int bytes_to_read;
  62.  
  63.     if(bytes > (frame_size - in_ptr) ){
  64.         bytes_to_read = frame_size - in_ptr;
  65.     }
  66.     else
  67.         bytes_to_read = bytes;
  68.     memcpy(buffer, &frame_data[in_ptr], bytes_to_read);
  69.     in_ptr += bytes_to_read;
  70.  
  71.     return bytes_to_read;
  72. }
  73.  
  74. int CMPEGDec::decodeFrame(ui8 *pcm_samples, ui8 *frame_data, ui32 frame_size)
  75. {
  76.     AMPStreamInfo pasi;
  77.     int i;
  78.     in_ptr           = 0;
  79.     this->frame_size = frame_size;
  80.     this->frame_data = frame_data;
  81.   bool bSuccess;
  82.  
  83.   try
  84.   {
  85.       decoder->Reset();
  86.       decoder->ReadHeader();
  87.  
  88.       decoder->getStreamInfo(&pasi);
  89.       if(pasi.fStereo)
  90.           decoder->setDestination((short *)pcm_samples);
  91.       else
  92.           decoder->setDestination(temp_buffer);
  93.  
  94.  
  95.       // Convert mono to stereo
  96.  
  97.     if(decoder->DecodeFrame())
  98.     {
  99.       if(!pasi.fStereo)
  100.         for(i=0; i<FRAME_SAMPLES*2; i++)
  101.           ((short *)pcm_samples)[i] = temp_buffer[i>>1];
  102.         bSuccess = true;
  103.     }
  104.     else
  105.       bSuccess = false;    
  106.   }
  107.   catch(...)
  108.   {
  109.     OutputDebugString("CMPEGDec::decodeFrame() - Mpeg decoder generated unhandled expection.\n");
  110.   }
  111.  
  112.   return bSuccess;
  113. }
  114.