Shaka Packager SDK
ac3_audio_util.cc
1 // Copyright 2018 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/ac3_audio_util.h"
8 
9 #include "packager/base/strings/string_number_conversions.h"
10 #include "packager/media/base/bit_reader.h"
11 #include "packager/media/base/rcheck.h"
12 
13 namespace shaka {
14 namespace media {
15 
16 namespace {
17 
18 // ASTC Standard A/52:2012 Table 5.8 Audio Coding Mode.
19 const uint8_t kAc3NumChannelsTable[] = {2, 1, 2, 3, 3, 4, 4, 5};
20 
21 bool ExtractAc3Data(const std::vector<uint8_t>& ac3_data,
22  uint8_t* audio_coding_mode,
23  bool* lfe_channel_on) {
24  BitReader bit_reader(ac3_data.data(), ac3_data.size());
25 
26  // fscod: 2 bits
27  // bsid: 5 bits
28  // bsmod: 3 bits
29  // acmod: 3 bits
30  // lfeon: 1 bit
31  // bit_rate_code: 5 bits
32  RCHECK(bit_reader.SkipBits(10));
33  RCHECK(bit_reader.ReadBits(3, audio_coding_mode));
34  RCHECK(bit_reader.ReadBits(1, lfe_channel_on));
35  return true;
36 }
37 
38 } // namespace
39 
40 size_t GetAc3NumChannels(const std::vector<uint8_t>& ac3_data) {
41  uint8_t audio_coding_mode;
42  bool lfe_channel_on;
43  if (!ExtractAc3Data(ac3_data, &audio_coding_mode, &lfe_channel_on)) {
44  LOG(WARNING) << "Seeing invalid AC3 data: "
45  << base::HexEncode(ac3_data.data(), ac3_data.size());
46  return 0;
47  }
48  return kAc3NumChannelsTable[audio_coding_mode] + (lfe_channel_on ? 1 : 0);
49 }
50 
51 } // namespace media
52 } // namespace shaka
All the methods that are virtual are virtual for mocking.