Shaka Packager SDK
ec3_audio_util.cc
1 // Copyright 2016 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include "packager/media/codecs/ec3_audio_util.h"
8 
9 #include "packager/base/macros.h"
10 #include "packager/base/strings/string_number_conversions.h"
11 #include "packager/media/base/bit_reader.h"
12 #include "packager/media/base/rcheck.h"
13 
14 namespace shaka {
15 namespace media {
16 
17 namespace {
18 
19 // Channels bit map. 16 bits.
20 // Bit, Location
21 // 0(MSB), Left
22 // 1, Center
23 // 2, Right
24 // 3, Left Surround
25 // 4, Right Surround
26 // 5, Left center/Right center pair
27 // 6, Left rear surround/Right rear surround pair
28 // 7, Center surround
29 // 8, Top center surround
30 // 9, Left surround direct/Right surround direct pair
31 // 10, Left wide/Right wide pair
32 // 11, Lvertical height/Right vertical height pair
33 // 12, Center vertical height
34 // 13, Lts/Rts pair
35 // 14, LFE2
36 // 15, LFE
37 enum kEC3AudioChannelMap {
38  kLeft = 0x8000,
39  kCenter = 0x4000,
40  kRight = 0x2000,
41  kLeftSurround = 0x1000,
42  kRightSurround = 0x800,
43  kLcRcPair = 0x400,
44  kLrsRrsPair = 0x200,
45  kCenterSurround = 0x100,
46  kTopCenterSurround = 0x80,
47  kLsdRsdPair = 0x40,
48  kLwRwPair = 0x20,
49  kLvhRvhPair = 0x10,
50  kCenterVerticalHeight = 0x8,
51  kLtsRtsPair = 0x4,
52  kLFE2 = 0x2,
53  kLFEScreen = 0x1
54 };
55 // Number of channels for the channel bit above. The first entry corresponds to
56 // kLeft, which has one channel. All the XxxPairs bits have two channels.
57 const size_t kChannelCountArray[] = {
58  1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 2, 2, 1, 2, 1, 1,
59 };
60 static_assert(arraysize(kChannelCountArray) == 16u,
61  "Channel count array should have 16 entries.");
62 
63 // EC3 Audio coding mode map (acmod) to determine EC3 audio channel layout. The
64 // value stands for the existence of Left, Center, Right, Left surround, and
65 // Right surround.
66 const uint16_t kEC3AudioCodingModeMap[] = {
67  kLeft | kRight,
68  kCenter,
69  kLeft | kRight,
70  kLeft | kCenter | kRight,
71  kLeft | kRight | kLeftSurround | kRightSurround,
72  kLeft | kCenter | kRight | kLeftSurround | kRightSurround,
73  kLeft | kRight | kLeftSurround | kRightSurround,
74  kLeft | kCenter | kRight | kLeftSurround | kRightSurround,
75 };
76 
77 // Reverse bit order.
78 uint8_t ReverseBits8(uint8_t n) {
79  n = ((n >> 1) & 0x55) | ((n & 0x55) << 1);
80  n = ((n >> 2) & 0x33) | ((n & 0x33) << 2);
81  return ((n >> 4) & 0x0f) | ((n & 0x0f) << 4);
82 }
83 
84 bool ExtractEc3Data(const std::vector<uint8_t>& ec3_data,
85  uint8_t* audio_coding_mode,
86  bool* lfe_channel_on,
87  uint16_t* dependent_substreams_layout) {
88  BitReader bit_reader(ec3_data.data(), ec3_data.size());
89  // Read number of independent substreams and parse the independent substreams.
90  uint8_t number_independent_substreams;
91  RCHECK(bit_reader.SkipBits(13) &&
92  bit_reader.ReadBits(3, &number_independent_substreams));
93  // The value of this field is one less than the number of independent
94  // substreams present.
95  ++number_independent_substreams;
96 
97  // Parse audio_coding_mode, dependent_substreams_layout and lfe_channel_on
98  // from the first independent substream.
99  // Independent substream in EC3Specific box:
100  // fscod: 2 bits
101  // bsid: 5 bits
102  // reserved_1: 1 bit
103  // asvc: 1 bit
104  // bsmod: 3 bits
105  // acmod: 3 bits
106  // lfeon: 1 bit
107  // reserved_2: 3 bits
108  // num_dep_sub: 4 bits
109  // If num_dep_sub > 0, chan_loc is present and the size is 9 bits.
110  // Otherwise, reserved_3 is present and the size is 1 bit.
111  // chan_loc: 9 bits
112  // reserved_3: 1 bit
113  RCHECK(bit_reader.SkipBits(12));
114  RCHECK(bit_reader.ReadBits(3, audio_coding_mode));
115  RCHECK(bit_reader.ReadBits(1, lfe_channel_on));
116 
117  uint8_t number_dependent_substreams = 0;
118  RCHECK(bit_reader.SkipBits(3));
119  RCHECK(bit_reader.ReadBits(4, &number_dependent_substreams));
120 
121  *dependent_substreams_layout = 0;
122  if (number_dependent_substreams > 0) {
123  RCHECK(bit_reader.ReadBits(9, dependent_substreams_layout));
124  }
125 
126  return true;
127 }
128 
129 } // namespace
130 
131 bool CalculateEC3ChannelMap(const std::vector<uint8_t>& ec3_data,
132  uint32_t* channel_map) {
133  uint8_t audio_coding_mode;
134  bool lfe_channel_on;
135  uint16_t dependent_substreams_layout;
136  if (!ExtractEc3Data(ec3_data, &audio_coding_mode, &lfe_channel_on,
137  &dependent_substreams_layout)) {
138  LOG(WARNING) << "Seeing invalid EC3 data: "
139  << base::HexEncode(ec3_data.data(), ec3_data.size());
140  return false;
141  }
142 
143  // Dependent substreams layout bit map:
144  // Bit, Location
145  // 0, Lc/Rc pair
146  // 1, Lrs/Rrs pair
147  // 2, Cs
148  // 3, Ts
149  // 4, Lsd/Rsd pair
150  // 5, Lw/Rw pair
151  // 6, Lvh/Rvh pair
152  // 7, Cvh
153  // 8(MSB), LFE2
154  // Reverse bit order of dependent substreams channel layout (LFE2 not
155  // included) to apply on channel_map bit 5 - 12.
156  const uint8_t reversed_dependent_substreams_layout =
157  ReverseBits8(dependent_substreams_layout & 0xFF);
158 
159  *channel_map = kEC3AudioCodingModeMap[audio_coding_mode] |
160  (reversed_dependent_substreams_layout << 3);
161  if (dependent_substreams_layout & 0x100)
162  *channel_map |= kLFE2;
163  if (lfe_channel_on)
164  *channel_map |= kLFEScreen;
165  return true;
166 }
167 
168 size_t GetEc3NumChannels(const std::vector<uint8_t>& ec3_data) {
169  uint32_t channel_map;
170  if (!CalculateEC3ChannelMap(ec3_data, &channel_map))
171  return 0;
172 
173  size_t num_channels = 0;
174  int bit = kLeft;
175  for (size_t channel_count : kChannelCountArray) {
176  if (channel_map & bit)
177  num_channels += channel_count;
178  bit >>= 1;
179  }
180  DCHECK_EQ(bit, 0);
181  return num_channels;
182 }
183 
184 } // namespace media
185 } // namespace shaka
All the methods that are virtual are virtual for mocking.