Shaka Packager SDK
Loading...
Searching...
No Matches
ec3_audio_util.cc
1// Copyright 2016 Google LLC. 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 <absl/log/check.h>
10#include <absl/strings/escaping.h>
11
12#include <packager/media/base/bit_reader.h>
13#include <packager/media/base/rcheck.h>
14#include <packager/utils/bytes_to_string_view.h>
15
16namespace shaka {
17namespace media {
18
19namespace {
20
21// Channels bit map. 16 bits.
22// Bit, Location
23// 0(MSB), Left
24// 1, Center
25// 2, Right
26// 3, Left Surround
27// 4, Right Surround
28// 5, Left center/Right center pair
29// 6, Left rear surround/Right rear surround pair
30// 7, Center surround
31// 8, Top center surround
32// 9, Left surround direct/Right surround direct pair
33// 10, Left wide/Right wide pair
34// 11, Lvertical height/Right vertical height pair
35// 12, Center vertical height
36// 13, Lts/Rts pair
37// 14, LFE2
38// 15, LFE
39enum kEC3AudioChannelMap {
40 kLeft = 0x8000,
41 kCenter = 0x4000,
42 kRight = 0x2000,
43 kLeftSurround = 0x1000,
44 kRightSurround = 0x800,
45 kLcRcPair = 0x400,
46 kLrsRrsPair = 0x200,
47 kCenterSurround = 0x100,
48 kTopCenterSurround = 0x80,
49 kLsdRsdPair = 0x40,
50 kLwRwPair = 0x20,
51 kLvhRvhPair = 0x10,
52 kCenterVerticalHeight = 0x8,
53 kLtsRtsPair = 0x4,
54 kLFE2 = 0x2,
55 kLFEScreen = 0x1
56};
57// Number of channels for the channel bit above. The first entry corresponds to
58// kLeft, which has one channel. All the XxxPairs bits have two channels.
59const size_t kChannelCountArray[] = {
60 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 2, 2, 1, 2, 1, 1,
61};
62static_assert(std::size(kChannelCountArray) == 16u,
63 "Channel count array should have 16 entries.");
64
65// EC3 Audio coding mode map (acmod) to determine EC3 audio channel layout. The
66// value stands for the existence of Left, Center, Right, Left surround, and
67// Right surround.
68const uint16_t kEC3AudioCodingModeMap[] = {
69 kLeft | kRight,
70 kCenter,
71 kLeft | kRight,
72 kLeft | kCenter | kRight,
73 kLeft | kRight | kLeftSurround | kRightSurround,
74 kLeft | kCenter | kRight | kLeftSurround | kRightSurround,
75 kLeft | kRight | kLeftSurround | kRightSurround,
76 kLeft | kCenter | kRight | kLeftSurround | kRightSurround,
77};
78
79// Reverse bit order.
80uint8_t ReverseBits8(uint8_t n) {
81 n = ((n >> 1) & 0x55) | ((n & 0x55) << 1);
82 n = ((n >> 2) & 0x33) | ((n & 0x33) << 2);
83 return ((n >> 4) & 0x0f) | ((n & 0x0f) << 4);
84}
85
86// Mapping of channel configurations to the MPEG audio value based on
87// ETSI TS 102 366 V1.4.1 Digital Audio Compression (AC-3, Enhanced AC-3)
88// Standard Table I.1.1
89uint32_t EC3ChannelMaptoMPEGValue(uint32_t channel_map) {
90 uint32_t ret = 0;
91
92 switch (channel_map) {
93 case kCenter:
94 ret = 1;
95 break;
96 case kLeft | kRight:
97 ret = 2;
98 break;
99 case kCenter | kLeft | kRight:
100 ret = 3;
101 break;
102 case kCenter | kLeft | kRight | kCenterSurround:
103 ret = 4;
104 break;
105 case kCenter | kLeft | kRight | kLeftSurround | kRightSurround:
106 ret = 5;
107 break;
108 case kCenter | kLeft | kRight | kLeftSurround | kRightSurround | kLFEScreen:
109 ret = 6;
110 break;
111 case kCenter | kLeft | kRight | kLwRwPair | kLeftSurround | kRightSurround |
112 kLFEScreen:
113 ret = 7;
114 break;
115 case kLeft | kRight | kCenterSurround:
116 ret = 9;
117 break;
118 case kLeft | kRight | kLeftSurround | kRightSurround:
119 ret = 10;
120 break;
121 case kCenter | kLeft | kRight | kLrsRrsPair | kCenterSurround | kLFEScreen:
122 ret = 11;
123 break;
124 case kCenter | kLeft | kRight | kLeftSurround | kRightSurround |
125 kLrsRrsPair | kLFEScreen:
126 ret = 12;
127 break;
128 case kCenter | kLeft | kRight | kLeftSurround | kRightSurround |
129 kLFEScreen | kLvhRvhPair:
130 ret = 14;
131 break;
132 case kCenter | kLeft | kRight | kLeftSurround | kRightSurround |
133 kLFEScreen | kLvhRvhPair | kLtsRtsPair:
134 ret = 16;
135 break;
136 case kCenter | kLeft | kRight | kLeftSurround | kRightSurround |
137 kLFEScreen | kLvhRvhPair | kCenterVerticalHeight | kLtsRtsPair |
138 kTopCenterSurround:
139 ret = 17;
140 break;
141 case kCenter | kLeft | kRight | kLsdRsdPair | kLrsRrsPair | kLFEScreen |
142 kLvhRvhPair | kLtsRtsPair:
143 ret = 19;
144 break;
145 default:
146 ret = 0xFFFFFFFF;
147 }
148 return ret;
149}
150
151bool ExtractEc3Data(const std::vector<uint8_t>& ec3_data,
152 uint8_t* audio_coding_mode,
153 bool* lfe_channel_on,
154 uint16_t* dependent_substreams_layout,
155 uint32_t* ec3_joc_complexity) {
156 BitReader bit_reader(ec3_data.data(), ec3_data.size());
157 // Read number of independent substreams and parse the independent substreams.
158 uint8_t number_independent_substreams;
159 RCHECK(bit_reader.SkipBits(13) &&
160 bit_reader.ReadBits(3, &number_independent_substreams));
161 // The value of this field is one less than the number of independent
162 // substreams present.
163 ++number_independent_substreams;
164
165 // Parse audio_coding_mode, dependent_substreams_layout and lfe_channel_on
166 // from the first independent substream.
167 // Independent substream in EC3Specific box:
168 // fscod: 2 bits
169 // bsid: 5 bits
170 // reserved_1: 1 bit
171 // asvc: 1 bit
172 // bsmod: 3 bits
173 // acmod: 3 bits
174 // lfeon: 1 bit
175 // reserved_2: 3 bits
176 // num_dep_sub: 4 bits
177 // If num_dep_sub > 0, chan_loc is present and the size is 9 bits.
178 // Otherwise, reserved_3 is present and the size is 1 bit.
179 // chan_loc: 9 bits
180 // reserved_3: 1 bit
181 RCHECK(bit_reader.SkipBits(12));
182 RCHECK(bit_reader.ReadBits(3, audio_coding_mode));
183 RCHECK(bit_reader.ReadBits(1, lfe_channel_on));
184
185 uint8_t number_dependent_substreams = 0;
186 RCHECK(bit_reader.SkipBits(3));
187 RCHECK(bit_reader.ReadBits(4, &number_dependent_substreams));
188
189 *dependent_substreams_layout = 0;
190 if (number_dependent_substreams > 0) {
191 RCHECK(bit_reader.ReadBits(9, dependent_substreams_layout));
192 } else {
193 RCHECK(bit_reader.SkipBits(1));
194 }
195 *ec3_joc_complexity = 0;
196 if (bit_reader.bits_available() < 16) {
197 return true;
198 }
199
200 RCHECK(bit_reader.SkipBits(7));
201 bool ec3_joc_flag;
202 RCHECK(bit_reader.ReadBits(1, &ec3_joc_flag));
203 if (ec3_joc_flag) {
204 RCHECK(bit_reader.ReadBits(8, ec3_joc_complexity));
205 }
206 return true;
207}
208
209} // namespace
210
211bool CalculateEC3ChannelMap(const std::vector<uint8_t>& ec3_data,
212 uint32_t* channel_map) {
213 uint8_t audio_coding_mode;
214 bool lfe_channel_on;
215 uint16_t dependent_substreams_layout;
216 uint32_t ec3_joc_complexity;
217 if (!ExtractEc3Data(ec3_data, &audio_coding_mode, &lfe_channel_on,
218 &dependent_substreams_layout, &ec3_joc_complexity)) {
219 LOG(WARNING) << "Seeing invalid EC3 data: "
220 << absl::BytesToHexString(
222 return false;
223 }
224
225 // Dependent substreams layout bit map:
226 // Bit, Location
227 // 0, Lc/Rc pair
228 // 1, Lrs/Rrs pair
229 // 2, Cs
230 // 3, Ts
231 // 4, Lsd/Rsd pair
232 // 5, Lw/Rw pair
233 // 6, Lvh/Rvh pair
234 // 7, Cvh
235 // 8(MSB), LFE2
236 // Reverse bit order of dependent substreams channel layout (LFE2 not
237 // included) to apply on channel_map bit 5 - 12.
238 const uint8_t reversed_dependent_substreams_layout =
239 ReverseBits8(dependent_substreams_layout & 0xFF);
240
241 *channel_map = kEC3AudioCodingModeMap[audio_coding_mode] |
242 (reversed_dependent_substreams_layout << 3);
243 if (dependent_substreams_layout & 0x100)
244 *channel_map |= kLFE2;
245 if (lfe_channel_on)
246 *channel_map |= kLFEScreen;
247 return true;
248}
249
250bool CalculateEC3ChannelMPEGValue(const std::vector<uint8_t>& ec3_data,
251 uint32_t* ec3_channel_mpeg_value) {
252 uint32_t channel_map;
253 if (!CalculateEC3ChannelMap(ec3_data, &channel_map))
254 return false;
255 *ec3_channel_mpeg_value = EC3ChannelMaptoMPEGValue(channel_map);
256 return true;
257}
258
259size_t GetEc3NumChannels(const std::vector<uint8_t>& ec3_data) {
260 uint32_t channel_map;
261 if (!CalculateEC3ChannelMap(ec3_data, &channel_map))
262 return 0;
263
264 size_t num_channels = 0;
265 int bit = kLeft;
266 for (size_t channel_count : kChannelCountArray) {
267 if (channel_map & bit)
268 num_channels += channel_count;
269 bit >>= 1;
270 }
271 DCHECK_EQ(bit, 0);
272 return num_channels;
273}
274
275bool GetEc3JocComplexity(const std::vector<uint8_t>& ec3_data,
276 uint32_t* ec3_joc_complexity) {
277 uint8_t audio_coding_mode;
278 bool lfe_channel_on;
279 uint16_t dependent_substreams_layout;
280
281 if (!ExtractEc3Data(ec3_data, &audio_coding_mode, &lfe_channel_on,
282 &dependent_substreams_layout, ec3_joc_complexity)) {
283 LOG(WARNING) << "Seeing invalid EC3 data: "
284 << absl::BytesToHexString(
286 return false;
287 }
288 return true;
289}
290
291} // namespace media
292} // namespace shaka
All the methods that are virtual are virtual for mocking.
std::string_view byte_vector_to_string_view(const std::vector< uint8_t > &bytes)
Convert byte vector to string_view.