Esempio n. 1
0
static int AEChannelMapToAUDIOTRACKChannelMask(CAEChannelInfo info)
{
  info.ResolveChannels(CAEChannelInfo(KnownChannels));

  // Detect layouts with 6 channels including one LFE channel
  // We currently support the following layouts:
  // 5.1            FL+FR+FC+LFE+BL+BR
  // 5.1(side)      FL+FR+FC+LFE+SL+SR
  // According to CEA-861-D only RR and RL are defined
  // Therefore we let Android decide about the 5.1 mapping
  // For 8 channel layouts including one LFE channel
  // we leave the same decision to Android
  if (info.Count() == 6 && info.HasChannel(AE_CH_LFE))
    return CJNIAudioFormat::CHANNEL_OUT_5POINT1;

  if (info.Count() == 8 && info.HasChannel(AE_CH_LFE))
    return CJNIAudioFormat::CHANNEL_OUT_7POINT1_SURROUND;

  int atMask = 0;

  for (unsigned int i = 0; i < info.Count(); i++)
    atMask |= AEChannelToAUDIOTRACKChannel(info[i]);

  return atMask;
}
Esempio n. 2
0
uint64_t CAEUtil::GetAVChannelLayout(const CAEChannelInfo &info)
{
  uint64_t channelLayout = 0;
  if (info.HasChannel(AE_CH_FL))   channelLayout |= AV_CH_FRONT_LEFT;
  if (info.HasChannel(AE_CH_FR))   channelLayout |= AV_CH_FRONT_RIGHT;
  if (info.HasChannel(AE_CH_FC))   channelLayout |= AV_CH_FRONT_CENTER;
  if (info.HasChannel(AE_CH_LFE))  channelLayout |= AV_CH_LOW_FREQUENCY;
  if (info.HasChannel(AE_CH_BL))   channelLayout |= AV_CH_BACK_LEFT;
  if (info.HasChannel(AE_CH_BR))   channelLayout |= AV_CH_BACK_RIGHT;
  if (info.HasChannel(AE_CH_FLOC)) channelLayout |= AV_CH_FRONT_LEFT_OF_CENTER;
  if (info.HasChannel(AE_CH_FROC)) channelLayout |= AV_CH_FRONT_RIGHT_OF_CENTER;
  if (info.HasChannel(AE_CH_BC))   channelLayout |= AV_CH_BACK_CENTER;
  if (info.HasChannel(AE_CH_SL))   channelLayout |= AV_CH_SIDE_LEFT;
  if (info.HasChannel(AE_CH_SR))   channelLayout |= AV_CH_SIDE_RIGHT;
  if (info.HasChannel(AE_CH_TC))   channelLayout |= AV_CH_TOP_CENTER;
  if (info.HasChannel(AE_CH_TFL))  channelLayout |= AV_CH_TOP_FRONT_LEFT;
  if (info.HasChannel(AE_CH_TFC))  channelLayout |= AV_CH_TOP_FRONT_CENTER;
  if (info.HasChannel(AE_CH_TFR))  channelLayout |= AV_CH_TOP_FRONT_RIGHT;
  if (info.HasChannel(AE_CH_TBL))   channelLayout |= AV_CH_TOP_BACK_LEFT;
  if (info.HasChannel(AE_CH_TBC))   channelLayout |= AV_CH_TOP_BACK_CENTER;
  if (info.HasChannel(AE_CH_TBR))   channelLayout |= AV_CH_TOP_BACK_RIGHT;

  return channelLayout;
}
Esempio n. 3
0
bool CAERemap::Initialize(CAEChannelInfo input, CAEChannelInfo output, bool finalStage, bool forceNormalize/* = false */, enum AEStdChLayout stdChLayout/* = AE_CH_LAYOUT_INVALID */)
{
  if (!input.Count() || !output.Count())
    return false;

  /* build the downmix matrix */
  memset(m_mixInfo, 0, sizeof(m_mixInfo));
  m_output = output;

  /* figure which channels we have */
  for (unsigned int o = 0; o < output.Count(); ++o)
    m_mixInfo[output[o]].in_dst = true;

  /* flag invalid channels for forced downmix */
  if (stdChLayout != AE_CH_LAYOUT_INVALID)
  {
    CAEChannelInfo layout = stdChLayout;
    for (unsigned int o = 0; o < output.Count(); ++o)
      if (!layout.HasChannel(output[o]))
        m_mixInfo[output[o]].in_dst = false;
  }

  m_outChannels = output.Count();

  /* lookup the channels that exist in the output */
  for (unsigned int i = 0; i < input.Count(); ++i)
  {
    AEMixInfo  *info = &m_mixInfo[input[i]];
    AEMixLevel *lvl  = &info->srcIndex[info->srcCount++];
    info->in_src = true;
    lvl->index   = i;
    lvl->level   = 1.0f;
    if (!info->in_dst)
    {
      for (unsigned int o = 0; o < output.Count(); ++o)
      {
        if (input[i] == output[o])
        {
          info->outIndex = o;
          break;
        }
      }
    }

    m_inChannels = i;
  }
  ++m_inChannels;

  /* the final stage does not need any down/upmix */
  if (finalStage)
    return true;

  /* downmix from the specified channel to the specified list of channels */
  #define RM(from, ...) \
    static AEChannel downmix_##from[] = {__VA_ARGS__, AE_CH_NULL}; \
    ResolveMix(from, CAEChannelInfo(downmix_##from));

  /*
    the order of this is important as we can not mix channels
    into ones that have already been resolved... eg

    TBR -> BR
    TBC -> TBL & TBR

    TBR will get resolved to BR, and then TBC will get
    resolved to TBL, but since TBR has been resolved it will
    never make it to the output. The order has to be reversed
    as the TBC center depends on the TBR downmix... eg

    TBC -> TBL & TBR
    TBR -> BR

    if for any reason out of order mapping becomes required
    looping this list should resolve the channels.
  */
  RM(AE_CH_TBC , AE_CH_TBL, AE_CH_TBR);
  RM(AE_CH_TBR , AE_CH_BR);
  RM(AE_CH_TBL , AE_CH_BL);
  RM(AE_CH_TC  , AE_CH_TFL, AE_CH_TFR);
  RM(AE_CH_TFC , AE_CH_TFL, AE_CH_TFR);
  RM(AE_CH_TFR , AE_CH_FR);
  RM(AE_CH_TFL , AE_CH_FL);
  RM(AE_CH_SR  , AE_CH_BR, AE_CH_FR);
  RM(AE_CH_SL  , AE_CH_BL, AE_CH_FL);
  RM(AE_CH_BC  , AE_CH_BL, AE_CH_BR);
  RM(AE_CH_FROC, AE_CH_FR, AE_CH_FC);
  RM(AE_CH_FLOC, AE_CH_FL, AE_CH_FC);
  RM(AE_CH_BL  , AE_CH_FL);
  RM(AE_CH_BR  , AE_CH_FR);
  RM(AE_CH_LFE , AE_CH_FL, AE_CH_FR);
  RM(AE_CH_FL  , AE_CH_FC);
  RM(AE_CH_FR  , AE_CH_FC);
  RM(AE_CH_BROC, AE_CH_BR, AE_CH_BC);
  RM(AE_CH_BLOC, AE_CH_BL, AE_CH_BC);

  /* since everything eventually mixes down to FC we need special handling for it */
  if (m_mixInfo[AE_CH_FC].in_src)
  {
    /* if there is no output FC channel, try to mix it the best possible way */
    if (!m_mixInfo[AE_CH_FC].in_dst)
    {
      /* if we have TFC & FL & FR */
      if (m_mixInfo[AE_CH_TFC].in_dst && m_mixInfo[AE_CH_FL].in_dst && m_mixInfo[AE_CH_FR].in_dst)
      {
        RM(AE_CH_FC, AE_CH_TFC, AE_CH_FL, AE_CH_FR);
      }
      /* if we have TFC */
      else if (m_mixInfo[AE_CH_TFC].in_dst)
      {
        RM(AE_CH_FC, AE_CH_TFC);
      }
      /* if we have FLOC & FROC */
      else if (m_mixInfo[AE_CH_FLOC].in_dst && m_mixInfo[AE_CH_FROC].in_dst)
      {
        RM(AE_CH_FC, AE_CH_FLOC, AE_CH_FROC);
      }
      /* if we have TC & FL & FR */
      else if (m_mixInfo[AE_CH_TC].in_dst && m_mixInfo[AE_CH_FL].in_dst && m_mixInfo[AE_CH_FR].in_dst)
      {
        RM(AE_CH_FC, AE_CH_TC, AE_CH_FL, AE_CH_FR);
      }
      /* if we have FL & FR */
      else if (m_mixInfo[AE_CH_FL].in_dst && m_mixInfo[AE_CH_FR].in_dst)
      {
        RM(AE_CH_FC, AE_CH_FL, AE_CH_FR);
      }
      /* if we have TFL & TFR */
      else if (m_mixInfo[AE_CH_TFL].in_dst && m_mixInfo[AE_CH_TFR].in_dst)
      {
        RM(AE_CH_FC, AE_CH_TFL, AE_CH_TFR);
      }
      /* we dont have enough speakers to emulate FC */
      else
        return false;
    }
    else
    {
      /* if there is only one channel in the source and it is the FC and we have FL & FR, upmix to dual mono */
      if (m_inChannels == 1 && m_mixInfo[AE_CH_FL].in_dst && m_mixInfo[AE_CH_FR].in_dst)
      {
        RM(AE_CH_FC, AE_CH_FL, AE_CH_FR);
      }
    }
  }

  #undef RM

  if (g_guiSettings.GetBool("audiooutput.stereoupmix"))
    BuildUpmixMatrix(input, output);

  /* normalize the values */
  bool normalize;
  if (forceNormalize)
    normalize = true;
  else
  {
    //FIXME: guisetting is reversed, change the setting name after frodo
    normalize = !g_guiSettings.GetBool("audiooutput.normalizelevels");
    CLog::Log(LOGDEBUG, "AERemap: Downmix normalization is %s", (normalize ? "enabled" : "disabled"));
  }

  if (normalize)
  {
    float max = 0;
    for (unsigned int o = 0; o < output.Count(); ++o)
    {
      AEMixInfo *info = &m_mixInfo[output[o]];
      float sum = 0;
      for (int i = 0; i < info->srcCount; ++i)
        sum += info->srcIndex[i].level;

      if (sum > max)
        max = sum;
    }

    float scale = 1.0f / max;
    for (unsigned int o = 0; o < output.Count(); ++o)
    {
      AEMixInfo *info = &m_mixInfo[output[o]];
      for (int i = 0; i < info->srcCount; ++i)
        info->srcIndex[i].level *= scale;
    }
  }

#if 0
  /* dump the matrix */
  CLog::Log(LOGINFO, "==[Downmix Matrix]==");
  for (unsigned int o = 0; o < output.Count(); ++o)
  {
    AEMixInfo *info = &m_mixInfo[output[o]];
    if (info->srcCount == 0)
      continue;

    std::stringstream s;
    s << CAEChannelInfo::GetChName(output[o]) << " =";
    for (int i = 0; i < info->srcCount; ++i)
      s << " " << CAEChannelInfo::GetChName(input[info->srcIndex[i].index]) << "(" << info->srcIndex[i].level << ")";

    CLog::Log(LOGINFO, "%s", s.str().c_str());
  }
  CLog::Log(LOGINFO, "====================\n");
#endif

  return true;
}
Esempio n. 4
0
void CAEChannelInfo::ResolveChannels(const CAEChannelInfo& rhs)
{
  /* mono gets upmixed to dual mono */
  if (m_channelCount == 1 && m_channels[0] == AE_CH_FC)
  {
    Reset();
    *this += AE_CH_FL;
    *this += AE_CH_FR;
    return;
  }

  bool srcHasSL = false;
  bool srcHasSR = false;
  bool srcHasRL = false;
  bool srcHasRR = false;
  bool srcHasBC = false;

  bool dstHasSL = false;
  bool dstHasSR = false;
  bool dstHasRL = false;
  bool dstHasRR = false;
  bool dstHasBC = false;

  for (unsigned int c = 0; c < rhs.m_channelCount; ++c)
    switch(rhs.m_channels[c])
    {
      case AE_CH_SL: dstHasSL = true; break;
      case AE_CH_SR: dstHasSR = true; break;
      case AE_CH_BL: dstHasRL = true; break;
      case AE_CH_BR: dstHasRR = true; break;
      case AE_CH_BC: dstHasBC = true; break;
      default:
        break;
    }

  CAEChannelInfo newInfo;
  for (unsigned int i = 0; i < m_channelCount; ++i)
  {
    switch (m_channels[i])
    {
      case AE_CH_SL: srcHasSL = true; break;
      case AE_CH_SR: srcHasSR = true; break;
      case AE_CH_BL: srcHasRL = true; break;
      case AE_CH_BR: srcHasRR = true; break;
      case AE_CH_BC: srcHasBC = true; break;
      default:
        break;
    }

    bool found = false;
    for (unsigned int c = 0; c < rhs.m_channelCount; ++c)
      if (m_channels[i] == rhs.m_channels[c])
      {
        found = true;
        break;
      }

    if (found)
      newInfo += m_channels[i];
  }

  /* we need to ensure we end up with rear or side channels for downmix to work */
  if (srcHasSL && !dstHasSL && dstHasRL && !newInfo.HasChannel(AE_CH_BL))
    newInfo += AE_CH_BL;
  if (srcHasSR && !dstHasSR && dstHasRR && !newInfo.HasChannel(AE_CH_BR))
    newInfo += AE_CH_BR;
  if (srcHasRL && !dstHasRL && dstHasSL && !newInfo.HasChannel(AE_CH_SL))
    newInfo += AE_CH_SL;
  if (srcHasRR && !dstHasRR && dstHasSR && !newInfo.HasChannel(AE_CH_SR))
    newInfo += AE_CH_SR;

  // mix back center if not available in destination layout
  // prefer mixing into backs if available
  if (srcHasBC && !dstHasBC)
  {
    if (dstHasRL && !newInfo.HasChannel(AE_CH_BL))
      newInfo += AE_CH_BL;
    else if (dstHasSL && !newInfo.HasChannel(AE_CH_SL))
      newInfo += AE_CH_SL;

    if (dstHasRR && !newInfo.HasChannel(AE_CH_BR))
      newInfo += AE_CH_BR;
    else if (dstHasSR && !newInfo.HasChannel(AE_CH_SR))
      newInfo += AE_CH_SR;
  }

  *this = newInfo;
}
Esempio n. 5
0
static uint32_t GetChannelMap(const CAEChannelInfo &channelLayout, bool passthrough)
{
  unsigned int channels = channelLayout.Count();
  uint32_t channel_map = 0;
  if (passthrough)
    return 0;

  static const unsigned char map_normal[] =
  {
    0, //AE_CH_RAW ,
    1, //AE_CH_FL
    2, //AE_CH_FR
    4, //AE_CH_FC
    3, //AE_CH_LFE
    7, //AE_CH_BL
    8, //AE_CH_BR
    1, //AE_CH_FLOC,
    2, //AE_CH_FROC,
    4, //AE_CH_BC,
    5, //AE_CH_SL
    6, //AE_CH_SR
  };
  static const unsigned char map_back[] =
  {
    0, //AE_CH_RAW ,
    1, //AE_CH_FL
    2, //AE_CH_FR
    4, //AE_CH_FC
    3, //AE_CH_LFE
    5, //AE_CH_BL
    6, //AE_CH_BR
    1, //AE_CH_FLOC,
    2, //AE_CH_FROC,
    4, //AE_CH_BC,
    5, //AE_CH_SL
    6, //AE_CH_SR
  };
  const unsigned char *map = map_normal;
  // According to CEA-861-D only RL and RR are known. In case of a format having SL and SR channels
  // but no BR BL channels, we use the wide map in order to open only the num of channels really
  // needed.
  if (channelLayout.HasChannel(AE_CH_BL) && !channelLayout.HasChannel(AE_CH_SL))
    map = map_back;

  for (unsigned int i = 0; i < channels; ++i)
  {
    AEChannel c = channelLayout[i];
    unsigned int chan = 0;
    if ((unsigned int)c < sizeof map_normal / sizeof *map_normal)
      chan = map[(unsigned int)c];
    if (chan > 0)
      channel_map |= (chan-1) << (3*i);
  }
  // These numbers are from Table 28 Audio InfoFrame Data byte 4 of CEA 861
  // and describe the speaker layout
  static const uint8_t cea_map[] = {
    0xff, // 0
    0xff, // 1
    0x00, // 2.0
    0x02, // 3.0
    0x08, // 4.0
    0x0a, // 5.0
    0xff, // 6
    0x12, // 7.0
    0xff, // 8
  };
  static const uint8_t cea_map_lfe[] = {
    0xff, // 0
    0xff, // 1
    0xff, // 2
    0x01, // 2.1
    0x03, // 3.1
    0x09, // 4.1
    0x0b, // 5.1
    0xff, // 7
    0x13, // 7.1
  };
  uint8_t cea = channelLayout.HasChannel(AE_CH_LFE) ? cea_map_lfe[channels] : cea_map[channels];
  if (cea == 0xff)
    CLog::Log(LOGERROR, "%s::%s - Unexpected CEA mapping %d,%d", CLASSNAME, __func__, channelLayout.HasChannel(AE_CH_LFE), channels);

  channel_map |= cea << 24;

  return channel_map;
}
Esempio n. 6
0
//Note: in multichannel mode CA will either pull 2 channels of data (stereo) or 6/8 channels of data
//(every speaker setup with more then 2 speakers). The difference between the number of real speakers
//and 6/8 channels needs to be padded with unknown channels so that the sample size fits 6/8 channels
//
//device [in] - the device whose channel layout should be used
//channelMap [in/out] - if filled it will it indicates that we are called from initialize and we log the requested map, out returns the channelMap for device
//channelsPerFrame [in] - the number of channels this device is configured to (e.x. 2 or 6/8)
void AEDeviceEnumerationOSX::GetAEChannelMap(CAEChannelInfo &channelMap, unsigned int channelsPerFrame) const
{
  CCoreAudioChannelLayout calayout;
  bool logMapping = channelMap.Count() > 0; // only log if the engine requests a layout during init
  bool mapAvailable = false;
  unsigned int numberChannelsInDeviceLayout = CA_MAX_CHANNELS; // default number of channels from CAChannelMap
  AudioChannelLayout *layout = NULL;
  
  // try to fetch either the multichannel or the stereo channel layout from the device
  if (channelsPerFrame == 2 || channelMap.Count() == 2)
    mapAvailable = m_caDevice.GetPreferredChannelLayoutForStereo(calayout);
  else
    mapAvailable = m_caDevice.GetPreferredChannelLayout(calayout);
  
  // if a map was fetched - check if it is usable
  if (mapAvailable)
  {
    layout = calayout;
    if (layout == NULL || layout->mChannelLayoutTag != kAudioChannelLayoutTag_UseChannelDescriptions)
      mapAvailable = false;// wrong map format
    else
      numberChannelsInDeviceLayout = layout->mNumberChannelDescriptions;
  }
  
  // start the mapping action
  // the number of channels to be added to the outgoing channelmap
  // this is CA_MAX_CHANNELS at max and might be lower for some output devices (channelsPerFrame)
  unsigned int numChannelsToMap = std::min((unsigned int)CA_MAX_CHANNELS, (unsigned int)channelsPerFrame);
  
  // if there was a map fetched we force the number of
  // channels to map to channelsPerFrame (this allows mapping
  // of more then CA_MAX_CHANNELS if needed)
  if (mapAvailable)
    numChannelsToMap = channelsPerFrame;
  
  std::string layoutStr;
  
  if (logMapping)
  {
    CLog::Log(LOGDEBUG, "%s Engine requests layout %s", __FUNCTION__, ((std::string)channelMap).c_str());
    
    if (mapAvailable)
      CLog::Log(LOGDEBUG, "%s trying to map to %s layout: %s", __FUNCTION__, channelsPerFrame == 2 ? "stereo" : "multichannel", calayout.ChannelLayoutToString(*layout, layoutStr));
    else
      CLog::Log(LOGDEBUG, "%s no map available - using static multichannel map layout", __FUNCTION__);
  }
  
  channelMap.Reset();// start with an empty map
  
  for (unsigned int channel = 0; channel < numChannelsToMap; channel++)
  {
    // we only try to map channels which are defined in the device layout
    enum AEChannel currentChannel;
    if (channel < numberChannelsInDeviceLayout)
    {
      // get the channel from the fetched map
      if (mapAvailable)
        currentChannel = caChannelToAEChannel(layout->mChannelDescriptions[channel].mChannelLabel);
      else// get the channel from the default map
        currentChannel = CAChannelMap[channel];
      
    }
    else// fill with unknown channels
      currentChannel = caChannelToAEChannel(kAudioChannelLabel_Unknown);
    
    if(!channelMap.HasChannel(currentChannel))// only add if not already added
      channelMap += currentChannel;
  }
  
  if (logMapping)
    CLog::Log(LOGDEBUG, "%s mapped channels to layout %s", __FUNCTION__, ((std::string)channelMap).c_str());
}