DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Implementation

Mercurial (9435724bfce1)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#if !defined(AudioLayout_h)
#  define AudioLayout_h

#  include <cstdint>
#  include <initializer_list>
#  include "mozilla/MathAlgorithms.h"
#  include "nsTArray.h"

namespace mozilla {

class AudioConfig {
 public:
  // Channel definition is conveniently defined to be in the same order as
  // WAVEFORMAT && SMPTE, even though this is unused for now.
  enum Channel {
    CHANNEL_INVALID = -1,
    CHANNEL_FRONT_LEFT = 0,
    CHANNEL_FRONT_RIGHT,
    CHANNEL_FRONT_CENTER,
    CHANNEL_LFE,
    CHANNEL_BACK_LEFT,
    CHANNEL_BACK_RIGHT,
    CHANNEL_FRONT_LEFT_OF_CENTER,
    CHANNEL_FRONT_RIGHT_OF_CENTER,
    CHANNEL_BACK_CENTER,
    CHANNEL_SIDE_LEFT,
    CHANNEL_SIDE_RIGHT,
    // From WAVEFORMAT definition.
    CHANNEL_TOP_CENTER,
    CHANNEL_TOP_FRONT_LEFT,
    CHANNEL_TOP_FRONT_CENTER,
    CHANNEL_TOP_FRONT_RIGHT,
    CHANNEL_TOP_BACK_LEFT,
    CHANNEL_TOP_BACK_CENTER,
    CHANNEL_TOP_BACK_RIGHT
  };

  class ChannelLayout {
   public:
    // The maximum number of channels a channel map can represent.
    static constexpr uint32_t MAX_CHANNELS = 32;

    typedef uint32_t ChannelMap;

    ChannelLayout() : mChannelMap(UNKNOWN_MAP), mValid(false) {}
    explicit ChannelLayout(uint32_t aChannels)
        : ChannelLayout(aChannels, DefaultLayoutForChannels(aChannels)) {}
    ChannelLayout(uint32_t aChannels, const Channel* aConfig)
        : ChannelLayout() {
      if (aChannels == 0 || !aConfig) {
        return;
      }
      mChannels.AppendElements(aConfig, aChannels);
      UpdateChannelMap();
    }
    explicit ChannelLayout(std::initializer_list<Channel> aChannelList)
        : ChannelLayout(aChannelList.size(), aChannelList.begin()) {}
    bool operator==(const ChannelLayout& aOther) const {
      return mChannels == aOther.mChannels;
    }
    bool operator!=(const ChannelLayout& aOther) const {
      return mChannels != aOther.mChannels;
    }
    const Channel& operator[](uint32_t aIndex) const {
      MOZ_ASSERT(mChannels.Length() > aIndex);
      return mChannels[aIndex];
    }
    uint32_t Count() const { return mChannels.Length(); }
    ChannelMap Map() const;

    // Calculate the mapping table from the current layout to aOther such that
    // one can easily go from one layout to the other by doing:
    // out[channel] = in[map[channel]].
    // Returns true if the reordering is possible or false otherwise.
    // If true, then aMap, if set, will be updated to contain the mapping table
    // allowing conversion from the current layout to aOther.
    // If aMap is empty, then MappingTable can be used to simply determine if
    // the current layout can be easily reordered to aOther.
    bool MappingTable(const ChannelLayout& aOther,
                      nsTArray<uint8_t>* aMap = nullptr) const;
    bool IsValid() const { return mValid; }
    bool HasChannel(Channel aChannel) const {
      return mChannelMap & (1 << aChannel);
    }
    // Return the number of channels found in this ChannelMap.
    static uint32_t Channels(ChannelMap aMap) {
      static_assert(sizeof(ChannelMap) == sizeof(uint32_t),
                    "Must adjust ChannelMap type");
      return CountPopulation32(aMap);
    }

    static ChannelLayout SMPTEDefault(const ChannelLayout& aChannelLayout);
    static ChannelLayout SMPTEDefault(ChannelMap aMap);

    static constexpr ChannelMap UNKNOWN_MAP = 0;

    // Common channel layout definitions.
    static constexpr ChannelMap LMONO_MAP = 1 << CHANNEL_FRONT_CENTER;
    static constexpr ChannelMap LMONO_LFE_MAP =
        1 << CHANNEL_FRONT_CENTER | 1 << CHANNEL_LFE;
    static constexpr ChannelMap LSTEREO_MAP =
        1 << CHANNEL_FRONT_LEFT | 1 << CHANNEL_FRONT_RIGHT;
    static constexpr ChannelMap LSTEREO_LFE_MAP =
        1 << CHANNEL_FRONT_LEFT | 1 << CHANNEL_FRONT_RIGHT | 1 << CHANNEL_LFE;
    static constexpr ChannelMap L3F_MAP = 1 << CHANNEL_FRONT_LEFT |
                                          1 << CHANNEL_FRONT_RIGHT |
                                          1 << CHANNEL_FRONT_CENTER;
    static constexpr ChannelMap L3F_LFE_MAP =
        1 << CHANNEL_FRONT_LEFT | 1 << CHANNEL_FRONT_RIGHT |
        1 << CHANNEL_FRONT_CENTER | 1 << CHANNEL_LFE;
    static constexpr ChannelMap L2F1_MAP = 1 << CHANNEL_FRONT_LEFT |
                                           1 << CHANNEL_FRONT_RIGHT |
                                           1 << CHANNEL_BACK_CENTER;
    static constexpr ChannelMap L2F1_LFE_MAP =
        1 << CHANNEL_FRONT_LEFT | 1 << CHANNEL_FRONT_RIGHT | 1 << CHANNEL_LFE |
        1 << CHANNEL_BACK_CENTER;
    static constexpr ChannelMap L3F1_MAP =
        1 << CHANNEL_FRONT_LEFT | 1 << CHANNEL_FRONT_RIGHT |
        1 << CHANNEL_FRONT_CENTER | 1 << CHANNEL_BACK_CENTER;
    static constexpr ChannelMap LSURROUND_MAP = L3F1_MAP;
    static constexpr ChannelMap L3F1_LFE_MAP =
        1 << CHANNEL_FRONT_LEFT | 1 << CHANNEL_FRONT_RIGHT |
        1 << CHANNEL_FRONT_CENTER | 1 << CHANNEL_LFE | 1 << CHANNEL_BACK_CENTER;
    static constexpr ChannelMap L2F2_MAP =
        1 << CHANNEL_FRONT_LEFT | 1 << CHANNEL_FRONT_RIGHT |
        1 << CHANNEL_SIDE_LEFT | 1 << CHANNEL_SIDE_RIGHT;
    static constexpr ChannelMap L2F2_LFE_MAP =
        1 << CHANNEL_FRONT_LEFT | 1 << CHANNEL_FRONT_RIGHT | 1 << CHANNEL_LFE |
        1 << CHANNEL_SIDE_LEFT | 1 << CHANNEL_SIDE_RIGHT;
    static constexpr ChannelMap LQUAD_MAP =
        1 << CHANNEL_FRONT_LEFT | 1 << CHANNEL_FRONT_RIGHT |
        1 << CHANNEL_BACK_LEFT | 1 << CHANNEL_BACK_RIGHT;
    static constexpr ChannelMap LQUAD_LFE_MAP =
        1 << CHANNEL_FRONT_LEFT | 1 << CHANNEL_FRONT_RIGHT | 1 << CHANNEL_LFE |
        1 << CHANNEL_BACK_LEFT | 1 << CHANNEL_BACK_RIGHT;
    static constexpr ChannelMap L3F2_MAP =
        1 << CHANNEL_FRONT_LEFT | 1 << CHANNEL_FRONT_RIGHT |
        1 << CHANNEL_FRONT_CENTER | 1 << CHANNEL_SIDE_LEFT |
        1 << CHANNEL_SIDE_RIGHT;
    static constexpr ChannelMap L3F2_LFE_MAP =
        1 << CHANNEL_FRONT_LEFT | 1 << CHANNEL_FRONT_RIGHT |
        1 << CHANNEL_FRONT_CENTER | 1 << CHANNEL_LFE | 1 << CHANNEL_SIDE_LEFT |
        1 << CHANNEL_SIDE_RIGHT;
    // 3F2_LFE Alias
    static constexpr ChannelMap L5POINT1_SURROUND_MAP = L3F2_LFE_MAP;
    static constexpr ChannelMap L3F2_BACK_MAP =
        1 << CHANNEL_FRONT_LEFT | 1 << CHANNEL_FRONT_RIGHT |
        1 << CHANNEL_FRONT_CENTER | 1 << CHANNEL_BACK_LEFT |
        1 << CHANNEL_BACK_RIGHT;
    static constexpr ChannelMap L3F2_BACK_LFE_MAP =
        L3F2_BACK_MAP | 1 << CHANNEL_LFE;
    static constexpr ChannelMap L3F3R_LFE_MAP =
        1 << CHANNEL_FRONT_LEFT | 1 << CHANNEL_FRONT_RIGHT |
        1 << CHANNEL_FRONT_CENTER | 1 << CHANNEL_LFE |
        1 << CHANNEL_BACK_CENTER | 1 << CHANNEL_SIDE_LEFT |
        1 << CHANNEL_SIDE_RIGHT;
    static ChannelLayout L3F4_LFE;
    static constexpr ChannelMap L3F4_LFE_MAP =
        1 << CHANNEL_FRONT_LEFT | 1 << CHANNEL_FRONT_RIGHT |
        1 << CHANNEL_FRONT_CENTER | 1 << CHANNEL_LFE | 1 << CHANNEL_BACK_LEFT |
        1 << CHANNEL_BACK_RIGHT | 1 << CHANNEL_SIDE_LEFT |
        1 << CHANNEL_SIDE_RIGHT;
    // 3F4_LFE Alias
    static ChannelLayout L7POINT1_SURROUND;
    static constexpr ChannelMap L7POINT1_SURROUND_MAP = L3F4_LFE_MAP;

   private:
    void UpdateChannelMap();
    const Channel* DefaultLayoutForChannels(uint32_t aChannels) const;
    AutoTArray<Channel, MAX_CHANNELS> mChannels;
    ChannelMap mChannelMap;
    bool mValid;
  };

  enum SampleFormat {
    FORMAT_NONE = 0,
    FORMAT_U8,
    FORMAT_S16,
    FORMAT_S24LSB,
    FORMAT_S24,
    FORMAT_S32,
    FORMAT_FLT,
#  if defined(MOZ_SAMPLE_TYPE_FLOAT32)
    FORMAT_DEFAULT = FORMAT_FLT
#  elif defined(MOZ_SAMPLE_TYPE_S16)
    FORMAT_DEFAULT = FORMAT_S16
#  else
#    error "Not supported audio type"
#  endif
  };

  AudioConfig(const ChannelLayout& aChannelLayout, uint32_t aRate,
              AudioConfig::SampleFormat aFormat = FORMAT_DEFAULT,
              bool aInterleaved = true);
  AudioConfig(const ChannelLayout& aChannelLayout, uint32_t aChannels,
              uint32_t aRate,
              AudioConfig::SampleFormat aFormat = FORMAT_DEFAULT,
              bool aInterleaved = true);
  // Will create a channel configuration from default SMPTE ordering.
  AudioConfig(uint32_t aChannels, uint32_t aRate,
              AudioConfig::SampleFormat aFormat = FORMAT_DEFAULT,
              bool aInterleaved = true);

  const ChannelLayout& Layout() const { return mChannelLayout; }
  uint32_t Channels() const {
    if (!mChannelLayout.IsValid()) {
      return mChannels;
    }
    return mChannelLayout.Count();
  }
  uint32_t Rate() const { return mRate; }
  SampleFormat Format() const { return mFormat; }
  bool Interleaved() const { return mInterleaved; }
  bool operator==(const AudioConfig& aOther) const {
    return mChannelLayout == aOther.mChannelLayout && mRate == aOther.mRate &&
           mFormat == aOther.mFormat && mInterleaved == aOther.mInterleaved;
  }
  bool operator!=(const AudioConfig& aOther) const {
    return !(*this == aOther);
  }

  bool IsValid() const {
    return mChannelLayout.IsValid() && Format() != FORMAT_NONE && Rate() > 0;
  }

  static const char* FormatToString(SampleFormat aFormat);
  static uint32_t SampleSize(SampleFormat aFormat);
  static uint32_t FormatToBits(SampleFormat aFormat);

 private:
  // Channels configuration.
  ChannelLayout mChannelLayout;

  // Channel count.
  uint32_t mChannels;

  // Sample rate.
  uint32_t mRate;

  // Sample format.
  SampleFormat mFormat;

  bool mInterleaved;
};

}  // namespace mozilla

#endif  // AudioLayout_h