signed short BitsNBytes::SI16()
{
	unsigned short x;
	ByteAlign();
	if (next == NULL || next >= fence) return 0;
	x  =  (unsigned short)(*next++);
	if (next == NULL || next >= fence) return x;
	x |= ((unsigned short)(*next++)) << 8;
	return (signed short)x;
}
unsigned long BitsNBytes::UI32()
{
	unsigned long x;
	ByteAlign();
	if (next == NULL || next >= fence) return 0;
	x  =  (unsigned short)(*next++);
	if (next == NULL || next >= fence) return x;
	x |= ((unsigned short)(*next++)) << 8;
	if (next == NULL || next >= fence) return x;
	x |= ((unsigned short)(*next++)) << 16;
	if (next == NULL || next >= fence) return x;
	x |= ((unsigned short)(*next++)) << 24;
	return x;
}
// name: iDecodeVOLHeader
// Purpose: decode VOL header
// return:  error code
OSCL_EXPORT_REF int16 iDecodeVOLHeader(mp4StreamType *psBits, int32 *width, int32 *height, int32 *display_width, int32 *display_height, int32 *profilelevel, uint32 *interlaced)
{
    int16 iErrorStat;
    uint32 codeword;
    int32 time_increment_resolution, nbits_time_increment;
    int32 i, j;

    *profilelevel = 0x0000FFFF; // init to some invalid value. When this value is returned, then no profilelevel info is available

    ShowBits(psBits, 32, &codeword);

    if (codeword == VISUAL_OBJECT_SEQUENCE_START_CODE)
    {
        //DV: this is the wrong way to skip bits, use FLush or Read psBits->dataBitPos += 32;
        ReadBits(psBits, 32, &codeword); // skip 32 bits of the Start code

        ReadBits(psBits, 8, &codeword);

        // record profile and level
        *profilelevel = (int) codeword;

        ShowBits(psBits, 32, &codeword);
        if (codeword == USER_DATA_START_CODE)
        {
            iErrorStat = DecodeUserData(psBits);
            if (iErrorStat) return MP4_INVALID_VOL_PARAM;
        }


        ReadBits(psBits, 32, &codeword);
        if (codeword != VISUAL_OBJECT_START_CODE) return MP4_INVALID_VOL_PARAM;

        /*  is_visual_object_identifier            */
        ReadBits(psBits, 1, &codeword);

        if (codeword)
        {
            /* visual_object_verid                            */
            ReadBits(psBits, 4, &codeword);
            /* visual_object_priority                         */
            ReadBits(psBits, 3, &codeword);
        }
        /* visual_object_type                                 */
        ReadBits(psBits, 4, &codeword);

        if (codeword == 1)
        {   /* video_signal_type */
            ReadBits(psBits, 1, &codeword);
            if (codeword == 1)
            {
                /* video_format */
                ReadBits(psBits, 3, &codeword);
                /* video_range  */
                ReadBits(psBits, 1, &codeword);
                /* color_description */
                ReadBits(psBits, 1, &codeword);
                if (codeword == 1)
                {
                    /* color_primaries */
                    ReadBits(psBits, 8, &codeword);;
                    /* transfer_characteristics */
                    ReadBits(psBits, 8, &codeword);
                    /* matrix_coefficients */
                    ReadBits(psBits, 8, &codeword);
                }
            }
        }
        else
        {
            int16 status = 0;
            do
            {
                /* Search for VOL_HEADER */
                status = SearchNextM4VFrame(psBits); /* search 0x00 0x00 0x01 */
                if (status != 0)
                    return MP4_INVALID_VOL_PARAM;

                status = ReadBits(psBits, VOL_START_CODE_LENGTH, &codeword);
            }
            while ((codeword != VOL_START_CODE) && (status == 0));
            goto decode_vol;
        }
        /* next_start_code() */
        ByteAlign(psBits);

        ShowBits(psBits, 32, &codeword);
        if (codeword == USER_DATA_START_CODE)
        {
            iErrorStat = DecodeUserData(psBits);
            if (iErrorStat) return MP4_INVALID_VOL_PARAM;
        }
        ShowBits(psBits, 27, &codeword);
    }
    else
    {
        ShowBits(psBits, 27, &codeword);
    }

    if (codeword == VO_START_CODE)
    {

        ReadBits(psBits, 32, &codeword);

        /* video_object_layer_start_code                   */
        ReadBits(psBits, 28, &codeword);
        if (codeword != VOL_START_CODE)
        {
            if (psBits->dataBitPos >= (psBits->numBytes << 3))
            {
                return SHORT_HEADER_MODE; /* SH */
            }
            else
            {
                int16 status = 0;
                do
                {
                    /* Search for VOL_HEADER */
                    status = SearchNextM4VFrame(psBits); /* search 0x00 0x00 0x01 */
                    if (status != 0)
                        return MP4_INVALID_VOL_PARAM;

                    status = ReadBits(psBits, VOL_START_CODE_LENGTH, &codeword);
                }
                while ((codeword != VOL_START_CODE) && (status == 0));
                goto decode_vol;
            }
        }
decode_vol:

        uint32 vol_id;

        /* vol_id (4 bits) */
        ReadBits(psBits, 4, & vol_id);

        // RandomAccessibleVOLFlag
        ReadBits(psBits, 1, &codeword);

        //Video Object Type Indication
        ReadBits(psBits, 8, &codeword);
        if (codeword != 1)
        {
            //return MP4_INVALID_VOL_PARAM; //TI supports this feature
        }

        // is_object_layer_identifier
        ReadBits(psBits, 1, &codeword);

        if (codeword)
        {
            ReadBits(psBits, 4, &codeword);
            ReadBits(psBits, 3, &codeword);
        }

        // aspect ratio
        ReadBits(psBits, 4, &codeword);

        if (codeword == 0xF)
        {
            // Extended Parameter
            /* width */
            ReadBits(psBits, 8, &codeword);
            /* height */
            ReadBits(psBits, 8, &codeword);
        }

        ReadBits(psBits, 1, &codeword);

        if (codeword)
        {
            ReadBits(psBits, 2, &codeword);
            if (codeword != 1)
            {
                return MP4_INVALID_VOL_PARAM;
            }

            ReadBits(psBits, 1, &codeword);

            if (!codeword)
            {
                //return MP4_INVALID_VOL_PARAM; //TI supports this feature
            }

            ReadBits(psBits, 1, &codeword);
            if (codeword)   /* if (vbv_parameters) {}, page 36 */
            {
                ReadBits(psBits, 15, &codeword);
                ReadBits(psBits, 1, &codeword);
                if (codeword != 1)
                {
                    return MP4_INVALID_VOL_PARAM;
                }

                ReadBits(psBits, 15, &codeword);
                ReadBits(psBits, 1, &codeword);
                if (codeword != 1)
                {
                    return MP4_INVALID_VOL_PARAM;
                }


                ReadBits(psBits, 19, &codeword);
                if (!(codeword & 0x8))
                {
                    return MP4_INVALID_VOL_PARAM;
                }

                ReadBits(psBits, 11, &codeword);
                ReadBits(psBits, 1, &codeword);
                if (codeword != 1)
                {
                    return MP4_INVALID_VOL_PARAM;
                }

                ReadBits(psBits, 15, &codeword);
                ReadBits(psBits, 1, &codeword);
                if (codeword != 1)
                {
                    return MP4_INVALID_VOL_PARAM;
                }
            }

        }

        ReadBits(psBits, 2, &codeword);

        if (codeword != 0)
        {
            return MP4_INVALID_VOL_PARAM;
        }

        ReadBits(psBits, 1, &codeword);
        if (codeword != 1)
        {
            return MP4_INVALID_VOL_PARAM;
        }

        ReadBits(psBits, 16, &codeword);
        time_increment_resolution = codeword;


        ReadBits(psBits, 1, &codeword);
        if (codeword != 1)
        {
            return MP4_INVALID_VOL_PARAM;
        }



        ReadBits(psBits, 1, &codeword);

        if (codeword && time_increment_resolution > 2)
        {
            i = time_increment_resolution - 1;
            j = 1;
            while (i >>= 1)
            {
                j++;
            }
            nbits_time_increment = j;

            ReadBits(psBits, nbits_time_increment, &codeword);
        }
示例#4
0
/*!
  The function decodes one aac frame. The decoding of coupling channel
  elements are not supported. The transport layer might signal, that the
  data of the current frame is invalid, e.g. as a result of a packet
  loss in streaming mode.
*/
int CAacDecoder_DecodeFrame(AACDECODER self,            /*!< pointer to aacdecoder structure */
                            int *frameSize,             /*!< pointer to frame size */
                            int *sampleRate,            /*!< pointer to sample rate */
                            int *numChannels,           /*!< pointer to number of channels */
                            char *channelMode,          /*!< mode: 0=mono, 1=stereo */
                            char frameOK                /*!< indicates if current frame data is valid */
                            )
{
  unsigned char aacChannels=0;
  long tmp;
  unsigned char ch;
  int type = 0;
  int ErrorStatus = frameOK;
  static int BlockNumber = 0;
  HANDLE_BIT_BUF bs = self->pBs;
  int previous_element;
  int channel;

  COUNT_sub_start("CAacDecoder_DecodeFrame");

  MOVE(7); INDIRECT(1); PTR_INIT(1); /* counting previous operations */

  /* initialize pointer to CAacDecoderChannelInfo for each channel */
  PTR_INIT(4); /* self->pAacDecoderChannelInfo[ch]
                  pAacDecoderChannelInfoInit[ch];
                  pAacDecoderDynamicDataInit[ch]
                  WorkBufferCore[ch*MaximumBinsLong];
               */
  LOOP(1);
  for (ch=0; ch<Channels; ch++)
  {
    INDIRECT(1); PTR_INIT(5);
    self->pAacDecoderChannelInfo[ch] = pAacDecoderChannelInfoInit[ch];
    self->pAacDecoderChannelInfo[ch]->pCodeBook = pAacDecoderDynamicDataInit[ch]->aCodeBook;
    self->pAacDecoderChannelInfo[ch]->pScaleFactor = pAacDecoderDynamicDataInit[ch]->aScaleFactor;
    self->pAacDecoderChannelInfo[ch]->pSpectralCoefficient = &WorkBufferCore[ch*MaximumBinsLong];
    self->pAacDecoderChannelInfo[ch]->pJointStereoData = &pAacDecoderDynamicCommonDataInit->JointStereoData;
    self->pAacDecoderChannelInfo[ch]->pPnsInterChannelData = &pAacDecoderDynamicCommonDataInit->PnsInterChannelData;
    self->pAacDecoderChannelInfo[ch]->pPnsStaticInterChannelData = &PnsStaticInterChannelData;
  }

  INDIRECT(1); MOVE(1);
  self->frameOK = 1;

  PTR_INIT(1); /* self->pAacDecoderChannelInfo[channel] */
  LOOP(1);
  for (channel=0; channel < Channels; channel++)
  {
    FUNC(1);
    CPns_InitPns(self->pAacDecoderChannelInfo[channel]);
  }

  INDIRECT(1); FUNC(1);
  CPns_InitInterChannelData(self->pAacDecoderChannelInfo[0]);

  MOVE(1);
  aacChannels = 0;

  INDIRECT(1); PTR_INIT(1); FUNC(2);
  ByteAlign(bs,&self->byteAlignBits);

  MOVE(1);
  previous_element = ID_END;

  LOOP(1);
  while ( (type != ID_END) && self->frameOK )
  {
    ADD(1); LOGIC(1); /* while() condition */

    FUNC(2);
    type = GetBits(bs,3);

    INDIRECT(1); BRANCH(1);
    if (bs->cntBits < 0)
    {
      INDIRECT(1); MOVE(1);
      self->frameOK = 0;
    }

    BRANCH(1);
    switch (type)
    {
      case ID_SCE:
      
        /*
          Consistency check
        */

        ADD(1); BRANCH(1);
        if(aacChannels >= Channels){

          INDIRECT(1); MOVE(1);
          self->frameOK = 0;
          break;
        }
          
        INDIRECT(1); MOVE(1);
        self->pStreamSbr->sbrElement[self->pStreamSbr->NrElements].ElementID = SBR_ID_SCE;

        INDIRECT(1); BRANCH(1); 
        if(self->frameOK) {

          INDIRECT(3); PTR_INIT(1); FUNC(3);
          ErrorStatus = CSingleChannelElement_Read(bs,self->pAacDecoderChannelInfo,self->pStreamInfo);

          BRANCH(1);
          if (ErrorStatus) {

            INDIRECT(1); MOVE(1);
            self->frameOK = 0;
          }
        }

        INDIRECT(1); BRANCH(1);
        if ( self->frameOK )
        {
          INDIRECT(1); FUNC(1);
          CSingleChannelElement_Decode(self->pAacDecoderChannelInfo);

          ADD(1);
          aacChannels += 1;
        }

        break;
      
      case ID_CPE:
       
        /*
          Consistency check
        */
        ADD(1); BRANCH(1);
        if(aacChannels >= Channels){

          INDIRECT(1); MOVE(1);
          self->frameOK = 0;
          break;
        }
          
        INDIRECT(1); MOVE(1);
        self->pStreamSbr->sbrElement[self->pStreamSbr->NrElements].ElementID = SBR_ID_CPE;


        INDIRECT(1); BRANCH(1);
        if (self->frameOK) {

          INDIRECT(3); PTR_INIT(1); FUNC(3);
          ErrorStatus = CChannelPairElement_Read(bs,self->pAacDecoderChannelInfo,self->pStreamInfo);

          BRANCH(1);
          if (ErrorStatus) {

            INDIRECT(1); MOVE(1);
            self->frameOK = 0;
          }
        }

        INDIRECT(1); BRANCH(1);
        if ( self->frameOK ){

          INDIRECT(1); FUNC(1);
          CChannelPairElement_Decode(self->pAacDecoderChannelInfo);

          ADD(1);
          aacChannels += 2;
        }

        break;
      
      case ID_CCE:
      
        INDIRECT(1); MOVE(2);
        ErrorStatus = AAC_DEC_UNIMPLEMENTED_CCE;
        self->frameOK = 0;
        break;

      case ID_LFE:

        INDIRECT(1); MOVE(2);
        ErrorStatus = AAC_DEC_UNIMPLEMENTED_LFE;
        self->frameOK = 0;
        break;
      
      case ID_DSE:

        INDIRECT(1); PTR_INIT(1); FUNC(2);
        CDse_Read(bs, &self->byteAlignBits);
        break;

      case ID_PCE:

        INDIRECT(1); MOVE(2);
        ErrorStatus = AAC_DEC_UNIMPLEMENTED_PCE;
        self->frameOK = 0;

        break;
      
      case ID_FIL:

        INDIRECT(1); MOVE(1);
        tmp = bs->cntBits;

        FFRaacplus_checkForPayload(bs,self->pStreamSbr,previous_element);
        break;
      
      case ID_END:
        break;
    }

    MOVE(1);
    previous_element = type;
  }


  /* Update number of channels (if valid) */
  INDIRECT(1); BRANCH(1);
  if (self->frameOK) {

    INDIRECT(1); MOVE(1);
    self->pStreamInfo->Channels = aacChannels;
  }

  INDIRECT(1); LOGIC(1); STORE(1);
  self->frameOK = self->frameOK && frameOK;

  /*
    Inverse transform
  */
  PTR_INIT(3); /* self->pAacDecoderStaticChannelInfo[ch]
                  self->pAacDecoderChannelInfo[ch]
                  self->pTimeData[ch*MaximumBinsLong]
               */
  INDIRECT(1); LOOP(1);
  for (ch=0; ch<self->pStreamInfo->Channels; ch++) {

    /*
      Conceal erred spectral data 
    */
    FUNC(3);
    CConcealment_Apply(self->pAacDecoderStaticChannelInfo[ch],
                       self->pAacDecoderChannelInfo[ch],
                       self->frameOK);
  
  
    PTR_INIT(1); FUNC(1); BRANCH(1);
    if (IsLongBlock(&self->pAacDecoderChannelInfo[ch]->IcsInfo))
    {
      PTR_INIT(1); FUNC(4);
      CLongBlock_FrequencyToTime(self->pAacDecoderStaticChannelInfo[ch],
                                 self->pAacDecoderChannelInfo[ch],
                                 &self->pTimeData[ch*MaximumBinsLong],
                                 1);
    }
    else
    {
      PTR_INIT(1); FUNC(4);
      CShortBlock_FrequencyToTime(self->pAacDecoderStaticChannelInfo[ch],
                                  self->pAacDecoderChannelInfo[ch],
                                  &self->pTimeData[ch*MaximumBinsLong],
                                  1);
    }
  }


  INDIRECT(1); PTR_INIT(1); FUNC(2);
  ByteAlign(bs,&self->byteAlignBits);

  INDIRECT(3); MOVE(3);
  *frameSize = self->pStreamInfo->SamplesPerFrame;
  *sampleRate = self->pStreamInfo->SamplingRate;
  *numChannels = self->pStreamInfo->Channels;

  ADD(1); BRANCH(1);
  if (*numChannels == 1)
  {
    MOVE(1);
    *channelMode = 0;
  }
  else {
    BRANCH(1); MOVE(1);
    if (*numChannels == 2)
      *channelMode = 1;
    else
      *channelMode = 2;
  }

  ADD(1);
  BlockNumber++;

  COUNT_sub_end();

  return ErrorStatus;
}
unsigned char BitsNBytes::UI8()
{
	ByteAlign();
	if (next == NULL || next >= fence) return 0;
	return *next++;
}
示例#6
0
static int CountBitstream(faacEncHandle hEncoder,
                          CoderInfo *coderInfo,
                          ChannelInfo *channelInfo,
                          BitStream *bitStream,
                          int numChannel)
{
    int channel;
    int bits = 0;
    int bitsLeftAfterFill, numFillBits;


    if(hEncoder->config.outputFormat == 1){
        bits += WriteADTSHeader(hEncoder, bitStream, 0);
    }else{
        bits = 0; // compilier will remove it, byt anyone will see that current size of bitstream is 0
    }

/* sur: faad2 complains about scalefactor error if we are writing FAAC String */
#ifndef DRM
    if (hEncoder->frameNum == 4)
      bits += WriteFAACStr(bitStream, hEncoder->config.name, 0);
#endif

    for (channel = 0; channel < numChannel; channel++) {

        if (channelInfo[channel].present) {

            /* Write out a single_channel_element */
            if (!channelInfo[channel].cpe) {

                if (channelInfo[channel].lfe) {
                    /* Write out lfe */
                    bits += WriteLFE(&coderInfo[channel],
                        &channelInfo[channel],
                        bitStream,
                        hEncoder->config.aacObjectType,
                        0);
                } else {
                    /* Write out sce */
                    bits += WriteSCE(&coderInfo[channel],
                        &channelInfo[channel],
                        bitStream,
                        hEncoder->config.aacObjectType,
                        0);
                }

            } else {

                if (channelInfo[channel].ch_is_left) {
                    /* Write out cpe */
                    bits += WriteCPE(&coderInfo[channel],
                        &coderInfo[channelInfo[channel].paired_ch],
                        &channelInfo[channel],
                        bitStream,
                        hEncoder->config.aacObjectType,
                        0);
                }
            }
        }
    }

    /* Compute how many fill bits are needed to avoid overflowing bit reservoir */
    /* Save room for ID_END terminator */
    if (bits < (8 - LEN_SE_ID) ) {
        numFillBits = 8 - LEN_SE_ID - bits;
    } else {
        numFillBits = 0;
    }

    /* Write AAC fill_elements, smallest fill element is 7 bits. */
    /* Function may leave up to 6 bits left after fill, so tell it to fill a few extra */
    numFillBits += 6;
    bitsLeftAfterFill = WriteAACFillBits(bitStream, numFillBits, 0);
    bits += (numFillBits - bitsLeftAfterFill);

    /* Write ID_END terminator */
    bits += LEN_SE_ID;

    /* Now byte align the bitstream */
    bits += ByteAlign(bitStream, 0, bits);

    hEncoder->usedBytes = bit2byte(bits);

    return bits;
}