Exemplo n.º 1
0
static int init(sh_audio_t *sh){
  int pos = 0;
  int32_t result = HXR_OK;
  uint16_t *temp;
  uint8_t *inBuf;
  int in_len;
  int32_t NumChannels, SampleRate;
  hAACDecoder = (HAACDecoder *)AACInitDecoder();
  ga_config_data configData;
  int i;

  if (!hAACDecoder) {
          printf(" *** Error initializing decoder ***\n");
          return 0;
  }


  if(!sh->codecdata_len && sh->wf) {
    sh->codecdata_len = sh->wf->cbSize;
    sh->codecdata = (char*)(sh->wf+1);

    mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"realaac: codecdata extracted from WAVEFORMATEX\n");
  }

  if(!sh->codecdata_len){
    //return 0;
    sh->a_in_buffer_len = demux_read_data(sh->ds, sh->a_in_buffer, sh->a_in_buffer_size);

     inBuf  = sh->a_in_buffer;
     in_len = sh->a_in_buffer_len;
     temp = (uint16_t *)malloc(sizeof(uint16_t) * sh->audio_out_minsize);
     /* decode ADTS frame from opaque data to get config data */
     result = AACDecode(hAACDecoder,  &inBuf, &in_len, temp);
     free(temp);
     if (result != HXR_OK)
     {
        return 0;
     }

         /* get the config data struct from the decoder */
     AACGetLastFrameInfo(hAACDecoder, frameInfo);

     NumChannels = frameInfo->nChans;
     SampleRate = frameInfo->sampRateCore;
  }else{
    struct BITSTREAM *pBitstream ;

    pBitstream = (struct BITSTREAM *)malloc(sizeof(struct BITSTREAM)) ;
    if (!pBitstream)
        return 0 ;

    pBitstream->cacheBitsLeft = 0 ;
    pBitstream->buffer = sh->codecdata ;
    pBitstream->nBits  = sh->codecdata_len*8 ;
    pBitstream->pkptr = pBitstream->buffer + (0>>3) ;
    pBitstream->cacheBitsLeft = 0 ;
    pBitstream->cache = 0 ;
    pBitstream->inc = 1 ;

     result = ga_config_get_data(pBitstream, &configData);
    if(pBitstream){
      free(pBitstream);
    }

     if (result != HXR_OK) /* config data error */
     {
         return 0;
     }
      NumChannels = frameInfo->nChans=configData.numChannels;
      SampleRate = frameInfo->sampRateCore=configData.samplingFrequency;
     /* see MPEG4 spec for index of each object type */
     if (configData.audioObjectType == 2)
         frameInfo->profile = AAC_PROFILE_LC;
     else if (configData.audioObjectType == 1)
         frameInfo->profile = AAC_PROFILE_MP;
     else if (configData.audioObjectType == 3)
         frameInfo->profile = AAC_PROFILE_SSR;
     else
         return 0; /* don't know - assume LC */

    /* set decoder to handle raw data blocks */
    AACSetRawBlockParams(hAACDecoder, 0, frameInfo);
  }
    mp_msg(MSGT_DECAUDIO,MSGL_V,"realaac: Decoder init done (%dBytes)!\n", sh->a_in_buffer_len); // XXX: remove or move to debug!
    mp_msg(MSGT_DECAUDIO,MSGL_V,"realaac: Negotiated samplerate: %ldHz  channels: %d\n", SampleRate, NumChannels);

    sh->channels = NumChannels;
    if (audio_output_channels <= 2) sh->channels = NumChannels > 1 ? 2 : 1;
    sh->samplerate = SampleRate;
    sh->samplesize=2;

   if(!sh->i_bps) {
      mp_msg(MSGT_DECAUDIO,MSGL_WARN,"realaac: compressed input bitrate missing, assuming 128kbit/s!\n");
      sh->i_bps = 128*1000/8; // XXX: HACK!!! ::atmos
    } else
      mp_msg(MSGT_DECAUDIO,MSGL_V,"realaac: got %dkbit/s bitrate from MP4 header!\n",sh->i_bps*8/1000);

  return 1; // return values: 1=OK 0=ERROR
}
Exemplo n.º 2
0
/*
 * aac_decode_init
 */
HX_RESULT
aac_decode_init(void*              pInitParams,
                UINT32             ulInitParamsSize,
                ra_format_info*    pStreamInfo,
                void**             pDecode,
                void*              pUserMem,
                rm_malloc_func_ptr fpMalloc,
                rm_free_func_ptr   fpFree)
{
    HX_RESULT result = HXR_OK;
    aac_decode* pDec;
    AACFrameInfo *frameInfo;
    ga_config_data configData;
    UCHAR *pData;
    UCHAR *inBuf;
    INT16 *temp;
    UINT32 numBytes, numBits;
    UINT32 cfgType;

    /* for MP4 bitstream parsing */
    struct BITSTREAM *pBs = 0;

    /* allocate the aac_decode struct */
    pDec = (aac_decode*) fpMalloc(pUserMem, sizeof(aac_decode));
    if (pDec == HXNULL)
    {
        return HXR_OUTOFMEMORY;
    }

    memset(pDec, 0, sizeof(aac_decode));
    *pDecode = (void *)pDec;

    /* allocate the frame info struct */
    pDec->pFrameInfo = fpMalloc(pUserMem, sizeof(AACFrameInfo));
    frameInfo = (AACFrameInfo *) pDec->pFrameInfo;
    /* allocate the decoder backend instance */
    pDec->pDecoder = AACInitDecoder(1);
    if (pDec->pFrameInfo == HXNULL || pDec->pDecoder == HXNULL)
    {
        return HXR_OUTOFMEMORY;
    }

    /* save the stream info and init data we'll need later */
    pDec->ulNumChannels = (UINT32)pStreamInfo->usNumChannels;
    pDec->ulBlockSize = pStreamInfo->ulGranularity;
    pDec->ulFrameSize = pStreamInfo->ulBitsPerFrame;
    pDec->ulFramesPerBlock = pDec->ulBlockSize / pDec->ulFrameSize;
    /* output frame size is doubled for safety in case of implicit SBR */
    pDec->ulSamplesPerFrame = 1024*pStreamInfo->usNumChannels*2;
    pDec->ulSampleRateCore = pStreamInfo->ulSampleRate;
    pDec->ulSampleRateOut  = pStreamInfo->ulActualRate;
    if (pStreamInfo->ulOpaqueDataSize < 1)
    {
        return HXR_FAIL; /* insufficient config data */
    }

    /* get the config data */
    pData = (UCHAR *)pStreamInfo->pOpaqueData;
    cfgType = pData[0];
    inBuf = pData + 1;
    numBytes = pStreamInfo->ulOpaqueDataSize - 1;

    if (cfgType == 1) /* ADTS Frame */
    {
        /* allocate temp buffer for decoding first ADTS frame */
        temp = (INT16 *)fpMalloc(pUserMem, sizeof(INT16) * pDec->ulSamplesPerFrame);
        if (temp == HXNULL)
        {
            return HXR_OUTOFMEMORY;
        }
        else
        {
            /* decode ADTS frame from opaque data to get config data */
            result = AACDecode(pDec->pDecoder, &inBuf, (INT32*)&numBytes, temp);
            /* free the temp buffer */
            fpFree(pUserMem, temp);
        }

        if (result == HXR_OK)
        {
            /* get the config data struct from the decoder */
            AACGetLastFrameInfo(pDec->pDecoder, frameInfo);

            pDec->ulNumChannels = frameInfo->nChans;
            pDec->ulSamplesPerFrame = frameInfo->outputSamps;
            pDec->ulSampleRateCore = frameInfo->sampRateCore;
            pDec->ulSampleRateOut = frameInfo->sampRateOut;
            pDec->bSBR = (pDec->ulSampleRateCore != pDec->ulSampleRateOut);
        }
    }
    else if (cfgType == 2) /* MP4 Audio Specific Config Data */
    {
        numBits = numBytes*8;

        if (newBitstream(&pBs, numBits, pUserMem, fpMalloc))
        	{
            return HXR_FAIL;
        	}

        feedBitstream(pBs, (const UCHAR *)inBuf, numBits);
        setAtBitstream(pBs, 0, 1);
        result = ga_config_get_data(pBs, &configData);
        deleteBitstream(pBs, pUserMem, fpFree);
        if (result != HXR_OK) /* config data error */
        {
            return HXR_FAIL;
        }

        pDec->ulNumChannels = configData.numChannels;
        pDec->ulSampleRateCore = configData.samplingFrequency;
        pDec->ulSampleRateOut = configData.extensionSamplingFrequency;
        pDec->bSBR = configData.bSBR;

        /*  ulSamplesPerFrame is set to the maximum possible output length.
         *  The config data has the initial output length, which might
         *  be doubled once the first frame is handed in (if AAC+ is
         *  signalled implicitly).
         */
        pDec->ulSamplesPerFrame = 2*configData.frameLength*configData.numChannels;

        /* setup decoder to handle raw data blocks */
        frameInfo->nChans = pDec->ulNumChannels;
        frameInfo->sampRateCore =  pDec->ulSampleRateCore;

        /* see MPEG4 spec for index of each object type */
        if (configData.audioObjectType == 2)
            frameInfo->profile = AAC_PROFILE_LC;
        else if (configData.audioObjectType == 1)
            frameInfo->profile = AAC_PROFILE_MP;
        else if (configData.audioObjectType == 3)
            frameInfo->profile = AAC_PROFILE_SSR;
        else
            frameInfo->profile = AAC_PROFILE_LC; /* don't know - assume LC */
        frameInfo->audio_send_by_frame=0;
    }
    else /* unsupported config type */
    {
            return HXR_FAIL;
    }

    /* make certain that all the channels can be handled */
    if (pDec->ulNumChannels > AAC_MAX_NCHANS) {
        return HXR_UNSUPPORTED_AUDIO;
    }

    /* set the channel mask - custom maps not supported */
    switch (pDec->ulNumChannels) {
    case  1: pDec->ulChannelMask = 0x00004; /* FC                */
    case  2: pDec->ulChannelMask = 0x00003; /* FL,FR             */
    case  3: pDec->ulChannelMask = 0x00007; /* FL,FR,FC          */
    case  4: pDec->ulChannelMask = 0x00107; /* FL,FR,FC,BC       */
    case  5: pDec->ulChannelMask = 0x00037; /* FL,FR,FC,BL,BR    */
    case  6: pDec->ulChannelMask = 0x0003F; /* FL,FR,FC,LF,BL,BR */
    default: pDec->ulChannelMask = 0xFFFFF; /* Unknown           */
    }

    /* set the delay samples */
    pDec->ulDelayRemaining = pDec->ulSamplesPerFrame;

    /* set decoder to handle raw data blocks */
    AACSetRawBlockParams(pDec->pDecoder, 0, frameInfo);


    return HXR_OK;
}