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