static
int transportEnc_writeGASpecificConfig(
                                        HANDLE_FDK_BITSTREAM asc,
                                        CODER_CONFIG *config,
                                        int          extFlg,
                                        UINT         alignAnchor
                                       )
{
  int aot = config->aot;
  int samplesPerFrame = config->samplesPerFrame;

  /* start of GASpecificConfig according to ISO/IEC 14496-3 Subpart 4, 4.4.1 */
  FDKwriteBits( asc, ((samplesPerFrame==960 || samplesPerFrame==480)?1:0), 1);  /* frameLengthFlag: 1 for a 960/480 (I)MDCT, 0 for a 1024/512 (I)MDCT*/
  FDKwriteBits( asc, 0, 1);  /* dependsOnCoreCoder: Sampling Rate Coder Specific, see in ISO/IEC 14496-3 Subpart 4, 4.4.1 */
  FDKwriteBits( asc, extFlg, 1 ); /* Extension Flag: Shall be 1 for aot = 17,19,20,21,22,23 */

  /* Write PCE if channel config is not 1-7 */
  if (getChannelConfig(config->channelMode) == 0) {
      transportEnc_writePCE(asc, config->channelMode, config->samplingRate, 0, 1, 0, 0, alignAnchor);
  }
  if (extFlg) {
    if (aot == AOT_ER_BSAC) {
      FDKwriteBits( asc, config->BSACnumOfSubFrame, 5 ); /* numOfSubFrame */
      FDKwriteBits( asc, config->BSAClayerLength, 11 );  /* layer_length */
    }
    if ((aot == AOT_ER_AAC_LC)   || (aot == AOT_ER_AAC_LTP)  ||
        (aot == AOT_ER_AAC_SCAL) || (aot == AOT_ER_AAC_LD))
    {
      FDKwriteBits( asc, (config->flags & CC_VCB11) ? 1 : 0, 1 ); /* aacSectionDataResillienceFlag */
      FDKwriteBits( asc, (config->flags & CC_RVLC) ? 1 : 0,  1 ); /* aacScaleFactorDataResillienceFlag */
      FDKwriteBits( asc, (config->flags & CC_HCR) ? 1 : 0,   1 ); /* aacSpectralDataResillienceFlag */
    }
    FDKwriteBits( asc, 0, 1 ); /* extensionFlag3: reserved. Shall be '0' */
  }
  return 0;
}
int transportEnc_writeASC (
                            HANDLE_FDK_BITSTREAM asc,
                            CODER_CONFIG *config,
                            CSTpCallBacks *cb
                           )
{
  UINT extFlag = 0;
  int err;
  int epConfig = 0;

  /* Required for the PCE. */
  UINT alignAnchor = FDKgetValidBits(asc);

  /* Extension Flag: Shall be 1 for aot = 17,19,20,21,22,23,39 */
  switch (config->aot) {
    case AOT_ER_AAC_LC:
    case AOT_ER_AAC_LTP:
    case AOT_ER_AAC_SCAL:
    case AOT_ER_TWIN_VQ:
    case AOT_ER_BSAC:
    case AOT_ER_AAC_LD:
    case AOT_ER_AAC_ELD:
    case AOT_USAC:
        extFlag = 1;
        break;
    default:
        break;
  }

  if (config->extAOT == AOT_SBR || config->extAOT == AOT_PS)
    writeAot(asc, config->extAOT);
  else
    writeAot(asc, config->aot);

  {
    writeSampleRate(asc, config->samplingRate);
  }

  /* Try to guess a reasonable channel mode if not given */
  if (config->channelMode == MODE_INVALID) {
    config->channelMode = transportEnc_GetChannelMode(config->noChannels);
    if (config->channelMode == MODE_INVALID)
      return -1;
  }

  FDKwriteBits( asc, getChannelConfig(config->channelMode), 4 );

  if (config->extAOT == AOT_SBR || config->extAOT == AOT_PS) {
    writeSampleRate(asc, config->extSamplingRate);
    writeAot(asc, config->aot);
  }

  switch (config->aot) {
#ifdef TP_GA_ENABLE
    case AOT_AAC_MAIN:
    case AOT_AAC_LC:
    case AOT_AAC_SSR:
    case AOT_AAC_LTP:
    case AOT_AAC_SCAL:
    case AOT_TWIN_VQ:
    case AOT_ER_AAC_LC:
    case AOT_ER_AAC_LTP:
    case AOT_ER_AAC_SCAL:
    case AOT_ER_TWIN_VQ:
    case AOT_ER_BSAC:
    case AOT_ER_AAC_LD:
      err = transportEnc_writeGASpecificConfig(asc, config, extFlag, alignAnchor);
      if (err)
        return err;
      break;
#endif /* TP_GA_ENABLE */
#ifdef TP_ELD_ENABLE
    case AOT_ER_AAC_ELD:
      err = transportEnc_writeELDSpecificConfig(asc, config, epConfig, cb);
      if (err)
        return err;
      break;
#endif /* TP_ELD_ENABLE */
    default:
      return -1;
  }

  switch (config->aot) {
    case AOT_ER_AAC_LC:
    case AOT_ER_AAC_LTP:
    case AOT_ER_AAC_SCAL:
    case AOT_ER_TWIN_VQ:
    case AOT_ER_BSAC:
    case AOT_ER_AAC_LD:
    case AOT_ER_CELP:
    case AOT_ER_HVXC:
    case AOT_ER_HILN:
    case AOT_ER_PARA:
    case AOT_ER_AAC_ELD:
      FDKwriteBits( asc, 0, 2 ); /* epconfig 0 */
      break;
    default:
      break;
  }

  /* Make sure all bits are sync'ed */
  FDKsyncCache( asc );

  return 0;
}
Beispiel #3
0
int adtsWrite_EncodeHeader(HANDLE_ADTS hAdts,
                                 HANDLE_FDK_BITSTREAM hBitStream,
                                 int buffer_fullness,
                                 int frame_length)
{
  INT crcIndex = 0;


  hAdts->headerBits = adtsWrite_GetHeaderBits(hAdts);

  FDK_ASSERT(((frame_length+hAdts->headerBits)/8)<0x2000);      /*13 bit*/
  FDK_ASSERT(buffer_fullness<0x800);    /* 11 bit   */

  if (!hAdts->protection_absent) {
    FDKcrcReset(&hAdts->crcInfo);
  }

  if (hAdts->currentBlock == 0) {
    FDKresetBitbuffer(hBitStream, BS_WRITER);
  }

  hAdts->subFrameStartBit = FDKgetValidBits(hBitStream);

  /* Skip new header if this is raw data block 1..n */
  if (hAdts->currentBlock == 0)
  {
    FDKresetBitbuffer(hBitStream, BS_WRITER);

    if (hAdts->num_raw_blocks == 0) {
      crcIndex = adtsWrite_CrcStartReg(hAdts, hBitStream, 0);
    }

    /* fixed header */
    FDKwriteBits(hBitStream, 0xFFF, 12);
    FDKwriteBits(hBitStream, hAdts->mpeg_id, 1);
    FDKwriteBits(hBitStream, hAdts->layer, 2);
    FDKwriteBits(hBitStream, hAdts->protection_absent, 1);
    FDKwriteBits(hBitStream, hAdts->profile, 2);
    FDKwriteBits(hBitStream, hAdts->sample_freq_index, 4);
    FDKwriteBits(hBitStream, hAdts->private_bit, 1);
    FDKwriteBits(hBitStream, getChannelConfig(hAdts->channel_mode), 3);
    FDKwriteBits(hBitStream, hAdts->original, 1);
    FDKwriteBits(hBitStream, hAdts->home, 1);
    /* variable header */
    FDKwriteBits(hBitStream, hAdts->copyright_id, 1);
    FDKwriteBits(hBitStream, hAdts->copyright_start, 1);
    FDKwriteBits(hBitStream, (frame_length + hAdts->headerBits)>>3, 13);
    FDKwriteBits(hBitStream, buffer_fullness, 11);
    FDKwriteBits(hBitStream, hAdts->num_raw_blocks, 2);

    if (!hAdts->protection_absent) {
      int i;

      /* End header CRC portion for single raw data block and write dummy zero values for unknown fields. */
      if (hAdts->num_raw_blocks == 0) {
        adtsWrite_CrcEndReg(hAdts, hBitStream, crcIndex);
      } else {
        for (i=0; i<hAdts->num_raw_blocks; i++) {
          FDKwriteBits(hBitStream, 0, 16);
        }
      }
      FDKwriteBits(hBitStream, 0, 16);
    }
  } /* End of ADTS header */
TRANSPORTENC_ERROR transportEnc_Init(
        HANDLE_TRANSPORTENC hTpEnc,
        UCHAR             *bsBuffer,
        INT                bsBufferSize,
        TRANSPORT_TYPE     transportFmt,
        CODER_CONFIG      *cconfig,
        UINT               flags
        )
{
  /* Copy configuration structure */
  FDKmemcpy(&hTpEnc->config, cconfig, sizeof(CODER_CONFIG));

  /* Init transportEnc struct. */
  hTpEnc->transportFmt = transportFmt;

  hTpEnc->bsBuffer = bsBuffer;
  hTpEnc->bsBufferSize = bsBufferSize;

  FDKinitBitStream(&hTpEnc->bitStream, hTpEnc->bsBuffer, hTpEnc->bsBufferSize, 0, BS_WRITER);

  switch (transportFmt) {

  case TT_MP4_ADIF:
    /* Sanity checks */
    if ( (hTpEnc->config.aot != AOT_AAC_LC)
       ||(hTpEnc->config.samplesPerFrame != 1024))
    {
      return TRANSPORTENC_INVALID_PARAMETER;
    }
    hTpEnc->writer.adif.headerWritten = 0;
    hTpEnc->writer.adif.samplingRate = hTpEnc->config.samplingRate;
    hTpEnc->writer.adif.bitRate = hTpEnc->config.bitRate;
    hTpEnc->writer.adif.profile = ((int)hTpEnc->config.aot) - 1;
    hTpEnc->writer.adif.cm = hTpEnc->config.channelMode;
    hTpEnc->writer.adif.bVariableRate = 0;
    hTpEnc->writer.adif.instanceTag = 0;
    break;

  case TT_MP4_ADTS:
    /* Sanity checks */
    if ( ( hTpEnc->config.aot != AOT_AAC_LC)
       ||(hTpEnc->config.samplesPerFrame != 1024) )
    {
      return TRANSPORTENC_INVALID_PARAMETER;
    }
    if ( adtsWrite_Init(&hTpEnc->writer.adts, &hTpEnc->config) != 0) {
      return TRANSPORTENC_INVALID_PARAMETER;
    }
    break;

  case TT_DABPLUS:
    /* Sanity checks */
    if ( ( hTpEnc->config.aot != AOT_AAC_LC)
       ||(hTpEnc->config.samplesPerFrame != 960) )
    {
      return TRANSPORTENC_INVALID_PARAMETER;
    }
    if ( dabWrite_Init(&hTpEnc->writer.dab, &hTpEnc->config) != 0) {
      return TRANSPORTENC_INVALID_PARAMETER;
    }
    break;

  case TT_MP4_LOAS:
  case TT_MP4_LATM_MCP0:
  case TT_MP4_LATM_MCP1:
    {
      TRANSPORTENC_ERROR error;

      error = transportEnc_Latm_Init(
                &hTpEnc->writer.latm,
                &hTpEnc->bitStream,
                &hTpEnc->config,
                 flags & TP_FLAG_LATM_AMV,
                 transportFmt,
                &hTpEnc->callbacks
                 );
      if (error != TRANSPORTENC_OK) {
        return error;
      }
    }
    break;

  case TT_MP4_RAW:
    hTpEnc->writer.raw.curSubFrame = 0;
    hTpEnc->writer.raw.nSubFrames = hTpEnc->config.nSubFrames;
    break;



  default:
    return TRANSPORTENC_INVALID_PARAMETER;
  }

  /* pceFrameCounter indicates if PCE must be written in raw_data_block. */
  hTpEnc->pceFrameCounter = getPceRepetitionRate(
                    getChannelConfig(hTpEnc->config.channelMode),
                    transportFmt,
                    hTpEnc->config.headerPeriod,
                    hTpEnc->config.matrixMixdownA);

  return TRANSPORTENC_OK;
}