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