7 #include "packager/media/base/bit_reader.h"
8 #include "packager/media/base/rcheck.h"
9 #include "packager/media/filters/ec3_audio_util.h"
34 enum kEC3AudioChannelMap {
38 kLeftSurround = 0x1000,
39 kRightSurround = 0x800,
42 kCenterSurround = 0x100,
43 kTopCenterSurround = 0x80,
47 kCenterVerticalHeight = 0x8,
56 const uint16_t kEC3AudioCodingModeMap[] = {
60 kLeft | kCenter | kRight,
61 kLeft | kRight | kLeftSurround | kRightSurround,
62 kLeft | kCenter | kRight | kLeftSurround | kRightSurround,
63 kLeft | kRight | kLeftSurround | kRightSurround,
64 kLeft | kCenter | kRight | kLeftSurround | kRightSurround,
68 uint8_t ReverseBits8(uint8_t n) {
69 n = ((n >> 1) & 0x55) | ((n & 0x55) << 1);
70 n = ((n >> 2) & 0x33) | ((n & 0x33) << 2);
71 return ((n >> 4) & 0x0f) | ((n & 0x0f) << 4);
74 bool ExtractEc3Data(
const std::vector<uint8_t>& ec3_data,
75 uint8_t* audio_coding_mode,
77 uint16_t* dependent_substreams_layout) {
78 BitReader bit_reader(ec3_data.data(), ec3_data.size());
80 uint8_t number_independent_substreams;
81 RCHECK(bit_reader.SkipBits(13) &&
82 bit_reader.ReadBits(3, &number_independent_substreams));
85 ++number_independent_substreams;
103 RCHECK(bit_reader.SkipBits(12));
104 RCHECK(bit_reader.ReadBits(3, audio_coding_mode));
105 RCHECK(bit_reader.ReadBits(1, lfe_channel_on));
107 uint8_t number_dependent_substreams = 0;
108 RCHECK(bit_reader.SkipBits(3));
109 RCHECK(bit_reader.ReadBits(4, &number_dependent_substreams));
111 *dependent_substreams_layout = 0;
112 if (number_dependent_substreams > 0) {
113 RCHECK(bit_reader.ReadBits(9, dependent_substreams_layout));
121 bool CalculateEC3ChannelMap(
const std::vector<uint8_t>& ec3_data, uint32_t* channel_map) {
122 uint8_t audio_coding_mode;
124 uint16_t dependent_substreams_layout;
125 if (!ExtractEc3Data(ec3_data, &audio_coding_mode, &lfe_channel_on,
126 &dependent_substreams_layout))
142 const uint8_t reversed_dependent_substreams_layout =
143 ReverseBits8(dependent_substreams_layout & 0xFF);
145 *channel_map = kEC3AudioCodingModeMap[audio_coding_mode] |
146 (reversed_dependent_substreams_layout << 3);
147 if (dependent_substreams_layout & 0x100)
148 *channel_map |= kLFE2;
150 *channel_map |= kLFEScreen;