www.pudn.com > w_ipp-sample-media_p_5.0.017.zip > main.cpp


/*////////////////////////////////////////////////////////////////////////////// 
// 
//                  INTEL CORPORATION PROPRIETARY INFORMATION 
//     This software is supplied under the terms of a license agreement or 
//     nondisclosure agreement with Intel Corporation and may not be copied 
//     or disclosed except in accordance with the terms of that agreement. 
//          Copyright(c) 2004-2005 Intel Corporation. All Rights Reserved. 
// 
*/ 
 
#define VM_DEBUG 7 
 
#include "vm_thread.h" 
#include "vm_mmap.h" 
#include "vm_time.h" 
 
#include "vm_debug.h" 
 
#include  
#include "dbg.h" 
#include "umc_structures.h" 
#include "umc_audio_codec.h" 
#include "params.h" 
#include "umc_file_reader.h" 
 
#ifdef UMC_ENABLE_MP4_SPLITTER 
    #include "umc_mp4_spl.h" 
#endif 
#ifdef EX_PARAM 
    #include "umc_aac_decoder.h" 
#endif 
 
#include "audio_codec_object.h" 
#include "factory.h" 
 
#include "wav_file.h" 
#include "simple_file.h" 
 
#include "umc_dual_thread_codec.h" 
#include "umc_cyclic_buffer.h" 
#include "umc_defs.h" 
 
using namespace UMC; 
 
struct sCommandLineParams 
{ 
  vm_char input_file[256]; 
  vm_char output_file[256]; 
 
  int bUsePluginDll; 
}; 
 
void LoadPlugins(Factory & factory); 
 
int main(int argc, vm_char *argv[]) 
{ 
  AudioFile*                 p_in_file; 
  AudioFile*                 p_out_file; 
 
  SimpleFile                 file_in; 
  SimpleFile                 file_out; 
 
  WavFile                    wav_file_in; 
  WavFile                    wav_file_out; 
 
  WavFile::Info              info_file_in; 
  WavFile::Info              info_file_out; 
 
  int                        is_input_wav = 0; 
  int                        err; 
  int                        isSplitterRequired = 0; 
  int                        acp_allocated = 0; 
 
  vm_tick                    t_freq, t_start, t_end; 
  double                     t_encode_time; 
  int                        numFramesToProcess = 0; 
 
  UMC::MediaData*            pOut = NULL; 
 
  UMC::MediaData             inMediaBuffer; 
  size_t                     size = 0; 
 
  UMC::Status                sts; 
 
  UMC::AudioCodec*           pAudioCodec; 
  UMC::MediaBuffer*          pMediaBuffer; 
  UMC::DualThreadedCodec     dual_treaded_codec; 
 
  UMC::AudioCodecParams*     audio_codec_params; 
#ifdef EX_PARAM 
  UMC::AACDecoderParams*     aacdec_params = NULL; 
#endif 
 
  UMC::MediaBufferParams *   media_buffer_params; 
  UMC::DualThreadCodecParams codec_init; 
 
  sProgramParameters         params; 
  Factory                    factory; 
 
  MediaData                  in_data; 
 
  Factory::Iterator < AudioCodecObject > iterator; 
 
 
  // Initialization 
  pAudioCodec  = NULL; 
  pMediaBuffer = NULL; 
  pAudioCodec  = NULL; 
  audio_codec_params  = NULL; 
  media_buffer_params = NULL; 
 
  LoadPlugins(factory); 
 
  if (argc < 2) { 
    params_print_usage(); 
    return -1; 
  } 
 
  params_fill_by_default(¶ms); 
  params_parse_command_line(¶ms, argc, argv); 
 
  if (params.enumerate) { 
    iterator = factory; 
 
    vm_string_printf(VM_STRING("The following codecs are available:\n")); 
    vm_string_printf(VM_STRING("%-25s %s\n------\n"), 
                     VM_STRING("Codec name"), VM_STRING("Version")); 
 
    while (iterator != NULL) { 
      vm_string_printf(VM_STRING("%-25s %s\n"), iterator->GetName(), iterator->GetVersion()); 
      iterator++; 
    } 
    return 0; 
  } 
 
  p_in_file = &wav_file_in; 
  p_out_file = &wav_file_out; 
 
  if ((!vm_string_strncmp(params.codec_name, VM_STRING("aac_enc"), 7)) || 
      (!vm_string_strncmp(params.codec_name, VM_STRING("aac_enc_i"), 9))) { 
    audio_codec_params = DynamicCast(¶ms.aacenc_params); 
  } else { 
  audio_codec_params = new UMC::AudioCodecParams; 
  if (NULL == audio_codec_params) { 
     vm_string_printf(VM_STRING("Memory allocation error\n")); 
     return -1; 
    } 
    acp_allocated = 1; 
  } 
 
  UMC::SplitterParams   splInitParams; 
  UMC::SplitterInfo     spl_info; 
  UMC::FileReaderParams frInitParams; 
 
  UMC::FileReader* pStream = NULL; 
  UMC::Splitter*   spl     = NULL; 
 
  UMC::Status umcRes = UMC::UMC_OK; 
 
  while (1) { 
    // Init FileReader 
    pStream = new UMC::FileReader; 
 
    if (NULL == pStream) { 
      vm_string_printf(VM_STRING("Memory allocation error\n")); 
      err = -1; 
      break; 
    } 
 
    vm_string_strcpy(frInitParams.m_file_name, params.input_file); 
 
    if (pStream->Init(&frInitParams) != UMC::UMC_OK) { 
      vm_string_printf(VM_STRING("Can't open input file: %s\n"), params.input_file); 
      err = -1; 
      break; 
    } 
 
    // Select splitter 
    SystemStreamType type_splitter = UMC::Splitter::GetStreamType(pStream); 
 
    if (type_splitter == UMC::MP4_ATOM_STREAM) { 
#ifdef UMC_ENABLE_MP4_SPLITTER 
      spl = new UMC::MP4Splitter; 
      if (NULL == spl) { 
        vm_string_printf(VM_STRING("Memory allocation error\n")); 
        err = -1; 
        break; 
      } 
#endif 
      if (NULL == spl) { 
          vm_string_printf(VM_STRING("Required splitter is not supported. Decoding is not possible\n\n")); 
          err = -6; 
          break; 
      }; 
 
      // Parametrs of splitter 
      splInitParams.m_lFlags      = UMC::AUDIO_SPLITTER; 
      splInitParams.m_pDataReader = pStream; 
 
      // Init Splitter 
      if (spl->Init(splInitParams) != UMC::UMC_OK) { 
        vm_string_printf(VM_STRING("Splitter initializion error\n\n")); 
        err = -1; 
        break; 
      } 
 
      // Get info about a mp4 file 
      spl->GetInfo(&spl_info); 
      if (spl_info.number_audio_tracks < 1) { 
        vm_string_printf(VM_STRING("Audio tracks are absent in input files\n\n")); 
        err = -7; 
        break; 
      } 
 
      audio_codec_params->m_info_in = spl_info.m_audio_info; 
      isSplitterRequired = 1; 
 
    } else { 
      isSplitterRequired = 0; 
      audio_codec_params->m_info_in.stream_type = UNDEF_AUDIO; 
      err = p_in_file->Open(params.input_file, AudioFile::AFM_NO_CONTENT_WRN); 
 
      if (err < 0) { 
        vm_debug_trace(-1, VM_STRING("Can't open input file '%s'"), 
            params.input_file); 
        vm_string_printf(VM_STRING("Can't open input file '%s'"), 
            params.input_file); 
        err = -2; 
        break; 
 
      } else if (err == 0) { 
        is_input_wav = 1; 
        err = wav_file_in.GetInfo(&info_file_in); 
 
        if (err == 0) { 
          if (info_file_in.format_tag == 1) { 
            audio_codec_params->m_info_in.stream_type = PCM_AUDIO; 
            p_out_file = &file_out; 
          } else { 
            audio_codec_params->m_info_in.stream_type = UNDEF_AUDIO; 
            p_out_file = &wav_file_out; 
          } 
 
          audio_codec_params->m_info_in.channel_mask = 0; 
          audio_codec_params->m_info_in.sample_frequency = 
              info_file_in.sample_rate; 
          audio_codec_params->m_info_in.bitPerSample = info_file_in.resolution; 
          audio_codec_params->m_info_in.channels = 
              info_file_in.channels_number; 
          audio_codec_params->m_info_in.bitrate = 0; 
          audio_codec_params->m_info_out.bitrate = params.bitrate; 
        } 
      } else { 
        p_in_file->Close(); 
        p_in_file = &file_in; 
 
        err = p_in_file->Open(params.input_file, AudioFile::AFM_NO_CONTENT_WRN); 
 
        if (err < 0) { 
          vm_debug_trace(-1, VM_STRING("Can't open input file '%s'"), 
            params.input_file); 
          vm_string_printf(VM_STRING("Can't open input file '%s'"), 
            params.input_file); 
          err = -2; 
          break; 
        } 
      } 
    } 
 
    err = p_out_file->Open(params.output_file, AudioFile::AFM_CREATE); 
 
    if (err) { 
        vm_debug_trace(-1, __VM_STRING("Can't create output file '%s'"), 
            params.output_file); 
        vm_string_printf(__VM_STRING("Can't create output file '%s'"), 
            params.output_file); 
        err = -3; 
        break; 
    } 
 
    iterator = factory; 
 
    if (false == iterator.Find(params.codec_name)) { 
        vm_debug_trace(-1, VM_STRING("The codec '%s'is not found.\n"), 
            params.codec_name); 
        vm_string_printf(VM_STRING("The codec '%s'is not found.\n"), 
            params.codec_name); 
        err = -4; 
        break; 
    } 
 
    pAudioCodec = (UMC::AudioCodec *) iterator->Create(); 
 
    if (pAudioCodec == NULL) { 
        vm_debug_trace(-1, __VM_STRING("Can't create audio codec object '%s'\n"), 
            params.codec_name); 
        vm_string_printf(__VM_STRING("Can't create audio codec object '%s'\n"), 
            params.codec_name); 
        err = -5; 
        break; 
    } 
 
    if (isSplitterRequired) { 
        spl->GetNextAudioData(&in_data); 
        audio_codec_params->m_pData = &in_data; 
        pMediaBuffer = DynamicCast < MediaBuffer > (new SampleBuffer); 
        if (NULL == pMediaBuffer) { 
            vm_string_printf(VM_STRING("Memory allocation error\n")); 
            err = -1; 
            break; 
        } 
    } else { 
        audio_codec_params->m_pData = NULL; 
        pMediaBuffer = DynamicCast < MediaBuffer > (new LinearBuffer); 
 
        if (NULL == pMediaBuffer) { 
            vm_string_printf(VM_STRING("Memory allocation error\n")); 
            err = -1; 
            break; 
        } 
    } 
 
    media_buffer_params = 
        DynamicCast < MediaBufferParams > (new MediaBufferParams); 
 
    pOut = new MediaData(1024*2*10); 
    if (NULL == pOut) { 
        vm_string_printf(VM_STRING("Memory allocation error\n")); 
        err = -1; 
        break; 
    } 
 
    media_buffer_params->m_numberOfFrames      = 10; 
    media_buffer_params->m_prefInputBufferSize = 768 * 10; 
 
    codec_init.m_pCodec             = pAudioCodec; 
    codec_init.m_pMediaBuffer       = pMediaBuffer; 
    codec_init.m_pCodecInitParams   = audio_codec_params; 
    codec_init.m_pMediaBufferParams = media_buffer_params; 
 
    sts = dual_treaded_codec.InitCodec(&codec_init); 
    if (sts != UMC::UMC_OK) { 
        vm_string_printf(VM_STRING("Failed to initalize codec plugin\n")); 
        return sts; 
    } 
 
    if (isSplitterRequired) { 
        sts = dual_treaded_codec.LockInputBuffer(&inMediaBuffer); 
 
        if (sts == UMC::UMC_OK) { 
            memcpy(inMediaBuffer.GetBufferPointer(), in_data.GetDataPointer(), 
                in_data.GetDataSize()); 
            inMediaBuffer.SetDataSize(in_data.GetDataSize()); 
 
            dual_treaded_codec.UnLockInputBuffer(&inMediaBuffer); 
        } // if (sts == UMC::UMC_OK) 
    } // if (isSplitterRequired) 
 
    if (sts == UMC::UMC_OK) { 
        t_freq = vm_time_get_frequency(); 
        t_end = 0; 
 
#ifdef EX_PARAM 
    aacdec_params = new AACDecoderParams; 
 
    if ( params.ModeDecode ) 
      aacdec_params->ModeDecodeHEAACprofile = HEAAC_LP_MODE; 
    else 
      aacdec_params->ModeDecodeHEAACprofile = HEAAC_HQ_MODE; 
 
    if ( params.ModeDownSample ) 
      aacdec_params->ModeDwnmxHEAACprofile  = HEAAC_DWNSMPL_ON; 
    else 
      aacdec_params->ModeDwnmxHEAACprofile  = HEAAC_DWNSMPL_OFF; 
 
    pAudioCodec->SetParams( DynamicCast(aacdec_params) ); 
#endif 
 
    if (!vm_string_strncmp(params.codec_name, VM_STRING("ac3_dec"), 7)) { 
      pAudioCodec->SetParams(DynamicCast(&(params.ac3dec_params))); 
    } 
 
    if (isSplitterRequired) { 
 
        do { 
            sts = dual_treaded_codec.LockInputBuffer(&inMediaBuffer); 
 
            if (sts == UMC::UMC_OK) { 
                umcRes = spl->GetNextAudioData(&in_data); 
                memcpy(inMediaBuffer.GetBufferPointer(), in_data.GetDataPointer(), 
                    in_data.GetDataSize()); 
 
                inMediaBuffer.SetDataSize(in_data.GetDataSize()); 
 
                assert(in_data.GetDataSize() <= inMediaBuffer.GetBufferSize()); 
 
                dual_treaded_codec.UnLockInputBuffer(&inMediaBuffer); 
            } 
 
            do { 
            t_start = vm_time_get_tick(); 
            sts = dual_treaded_codec.GetFrame(pOut); 
            t_end += (vm_time_get_tick() - t_start); 
 
            if (sts == UMC::UMC_OK) { 
                p_out_file->Write(pOut->GetDataPointer(), pOut->GetDataSize()); 
                numFramesToProcess++; 
//                vm_string_printf(VM_STRING("m_frame_num = %d\r"), numFramesToProcess); 
                double start, end; 
 
                pOut->GetTime(start, end); 
            } 
            } while (sts == UMC::UMC_OK); 
        } while ((umcRes != UMC_END_OF_STREAM) && (sts != UMC::UMC_BAD_STREAM)); 
 
        } else { 
        do { 
            sts = dual_treaded_codec.LockInputBuffer(&inMediaBuffer); 
 
            if (sts == UMC::UMC_OK) { 
          size_t needSize = inMediaBuffer.GetBufferSize(); 
          size = 
              p_in_file->Read(inMediaBuffer.GetBufferPointer(), needSize); 
          inMediaBuffer.SetDataSize(size); 
          if (size == needSize) 
            dual_treaded_codec.UnLockInputBuffer(&inMediaBuffer); 
          else 
            dual_treaded_codec.UnLockInputBuffer(&inMediaBuffer, UMC_END_OF_STREAM); 
            } 
 
            do { 
            t_start = vm_time_get_tick(); 
            sts = dual_treaded_codec.GetFrame(pOut); 
            t_end += (vm_time_get_tick() - t_start); 
 
            if (sts == UMC::UMC_OK) { 
 
                p_out_file->Write(pOut->GetDataPointer(), pOut->GetDataSize()); 
 
                if (params.is_text_out) { 
                    dbg_dump_array_16s((short *)pOut->GetDataPointer(), 
                                        pOut->GetDataSize() / 2, "%s%07d.txt", 
                                        params.output_txt, numFramesToProcess); 
                } 
 
                numFramesToProcess++; 
//                vm_string_printf(VM_STRING("m_frame_num = %d\r"), numFramesToProcess); 
 
                double start, end; 
 
                pOut->GetTime(start, end); 
            } 
            } while (sts == UMC::UMC_OK); 
        } while ((size) && (sts != UMC::UMC_BAD_STREAM)); 
        } 
 
        t_encode_time = (double)((vm_var64s) (t_end)) / (vm_var64s) t_freq; 
    } 
 
 
    pAudioCodec->GetInfo(audio_codec_params); 
 
    // float dur; 
    // pAudioCodec->GetDuration(&dur); 
    //  vm_string_printf(__VM_STRING 
    //                   ("Encoding/decoding time, sec : %f [%06d frames]\n"), 
    //                   t_encode_time, numFramesToProcess); 
    //  vm_string_printf(__VM_STRING("Stream duration, sec        : %f\n"), dur); 
    // t_encode_time /= dur; 
    //  vm_string_printf(__VM_STRING("Performance,(sec/sec)       : %f\n"), 
    //                   t_encode_time); 
    //  vm_string_printf(__VM_STRING 
    //                   ("* Multiply the performance by the CPU frequency to convert into MHz\n")); 
 
    if (audio_codec_params->m_info_out.stream_type == PCM_AUDIO) { 
        info_file_out.format_tag = 1; 
        info_file_out.channels_number = audio_codec_params->m_info_out.channels; 
        info_file_out.sample_rate = 
            audio_codec_params->m_info_out.sample_frequency; 
        info_file_out.resolution = audio_codec_params->m_info_out.bitPerSample; 
        info_file_out.channel_mask = audio_codec_params->m_info_out.channel_mask; 
 
        wav_file_out.SetInfo(&info_file_out); 
    } 
 
    break; 
  } // while (1) 
 
  p_out_file->Close(); 
  if (!isSplitterRequired) 
    p_in_file->Close(); 
 
  if (pOut) 
    delete pOut; 
 
  if (pAudioCodec) 
    delete pAudioCodec; 
 
  pAudioCodec = NULL; 
 
  if (pMediaBuffer) 
    delete pMediaBuffer; 
 
  pMediaBuffer = NULL; 
 
  if (acp_allocated) { 
  if (audio_codec_params) 
    delete audio_codec_params; 
  } 
 
  audio_codec_params = NULL; 
 
  if (media_buffer_params) 
    delete media_buffer_params; 
 
  media_buffer_params = NULL; 
 
#ifdef EX_PARAM 
  if ( aacdec_params ) 
    delete aacdec_params; 
 
  aacdec_params = NULL; 
#endif 
 
  if (!err) { 
  //    vm_string_printf(VM_STRING("Processing is completed successfully\n")); 
  } 
 
  return err; 
} 
 
void LoadPlugins(Factory & factory) 
{ 
 
// / Register static plugins into factory 
  factory.InitStatic(); 
 
#ifndef STATIC_OBJECTS_ONLY 
 
#if defined (_WIN32_WCE) 
  factory.Load(VM_STRING("aac_dec_i.dll")); 
  factory.Load(VM_STRING("aac_enc_i.dll")); 
  factory.Load(VM_STRING("mp3_dec_i.dll")); 
  factory.Load(VM_STRING("mp3_enc_i.dll")); 
#elif defined (WIN32) 
  factory.Load("mp3_dec.dll"); 
  factory.Load("mp3_dec_i.dll"); 
  factory.Load("mp3_enc.dll"); 
  factory.Load("mp3_enc_i.dll"); 
  factory.Load("aac_dec.dll"); 
  factory.Load("aac_dec_i.dll"); 
  factory.Load("aac_enc.dll"); 
  factory.Load("aac_enc_i.dll"); 
  factory.Load("ac3_dec.dll"); 
#elif defined (LINUX32) 
  factory.Load("libmp3_dec.so"); 
  factory.Load("libmp3_dec_i.so"); 
  factory.Load("libmp3_enc.so"); 
  factory.Load("libmp3_enc_i.so"); 
  factory.Load("libaac_dec.so"); 
  factory.Load("libaac_dec_i.so"); 
  factory.Load("libaac_enc.so"); 
  factory.Load("libaac_enc_i.so"); 
  factory.Load("libac3_dec.so"); 
#endif 
 
#else  /* STATIC_OBJECTS_ONLY */ 
 
#error Please select platform for building 
 
#endif  /* STATIC_OBJECTS_ONLY */ 
 
}