2023-12-01 17:32:19 +00:00
|
|
|
// Copyright 2018 Google LLC. All rights reserved.
|
2018-01-06 01:18:23 +00:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <packager/media/codecs/ac3_audio_util.h>
|
2018-01-06 01:18:23 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <absl/strings/escaping.h>
|
|
|
|
|
|
|
|
#include <packager/media/base/bit_reader.h>
|
|
|
|
#include <packager/media/base/rcheck.h>
|
|
|
|
#include <packager/utils/bytes_to_string_view.h>
|
2018-01-06 01:18:23 +00:00
|
|
|
|
|
|
|
namespace shaka {
|
|
|
|
namespace media {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// ASTC Standard A/52:2012 Table 5.8 Audio Coding Mode.
|
|
|
|
const uint8_t kAc3NumChannelsTable[] = {2, 1, 2, 3, 3, 4, 4, 5};
|
|
|
|
|
|
|
|
bool ExtractAc3Data(const std::vector<uint8_t>& ac3_data,
|
|
|
|
uint8_t* audio_coding_mode,
|
|
|
|
bool* lfe_channel_on) {
|
|
|
|
BitReader bit_reader(ac3_data.data(), ac3_data.size());
|
|
|
|
|
|
|
|
// fscod: 2 bits
|
|
|
|
// bsid: 5 bits
|
|
|
|
// bsmod: 3 bits
|
|
|
|
// acmod: 3 bits
|
|
|
|
// lfeon: 1 bit
|
|
|
|
// bit_rate_code: 5 bits
|
|
|
|
RCHECK(bit_reader.SkipBits(10));
|
|
|
|
RCHECK(bit_reader.ReadBits(3, audio_coding_mode));
|
|
|
|
RCHECK(bit_reader.ReadBits(1, lfe_channel_on));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
size_t GetAc3NumChannels(const std::vector<uint8_t>& ac3_data) {
|
|
|
|
uint8_t audio_coding_mode;
|
|
|
|
bool lfe_channel_on;
|
|
|
|
if (!ExtractAc3Data(ac3_data, &audio_coding_mode, &lfe_channel_on)) {
|
|
|
|
LOG(WARNING) << "Seeing invalid AC3 data: "
|
2023-12-01 17:32:19 +00:00
|
|
|
<< absl::BytesToHexString(
|
|
|
|
byte_vector_to_string_view(ac3_data));
|
2018-01-06 01:18:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return kAc3NumChannelsTable[audio_coding_mode] + (lfe_channel_on ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
|
|
|
} // namespace shaka
|