7 #include "packager/media/codecs/ac4_audio_util.h"
9 #include "packager/base/macros.h"
10 #include "packager/base/strings/string_number_conversions.h"
11 #include "packager/media/base/bit_reader.h"
12 #include "packager/media/base/rcheck.h"
40 enum kAC4AudioChannelGroupIndex {
50 kTopfrontCentre = 0x200,
51 kTopbackCentre = 0x400,
55 kBottomFrontCentre = 0x4000,
57 kLscrRscrPair = 0x10000,
59 kVhlVhrPair = 0x40000,
65 uint32_t AC4ChannelMasktoMPEGValue(uint32_t channel_mask) {
68 switch (channel_mask) {
75 case kCentre | kLRPair:
78 case kCentre | kLRPair | kBackCentre:
81 case kCentre | kLRPair | kLsRsPair:
84 case kCentre | kLRPair | kLsRsPair | kLFE:
87 case kCentre | kLRPair | kLsRsPair | kLFE | kLwRw:
90 case kBackCentre | kLRPair:
93 case kLRPair | kLsRsPair:
96 case kCentre | kLRPair | kLsRsPair | kLFE | kBackCentre:
99 case kCentre | kLRPair | kLsRsPair | kLbRbPair | kLFE:
102 case kLwRw | kBackCentre | kBottomFrontCentre | kBflBfrPair | kLFE2 |
103 kTopCentre | kTopbackCentre | kTopfrontCentre | kTslTsrPair | kLFE |
104 kTblTbrPair | kTflTfrPair | kLbRbPair | kLsRsPair | kCentre | kLRPair:
105 case kVhlVhrPair | kLwRw | kBackCentre | kBottomFrontCentre | kBflBfrPair|
106 kLFE2 | kTopCentre | kTopbackCentre | kTopfrontCentre | kTslTsrPair |
107 kLFE | kTblTbrPair | kLbRbPair | kLsRsPair | kCentre | kLRPair:
110 case kLFE | kTflTfrPair | kLsRsPair | kCentre | kLRPair:
111 case kVhlVhrPair | kLFE | kCentre | kLRPair | kLsRsPair:
114 case kLFE2 | kTopbackCentre | kLFE | kTflTfrPair | kCentre | kLRPair |
115 kLsRsPair | kLbRbPair:
116 case kVhlVhrPair | kLFE2 | kTopbackCentre | kLFE | kCentre | kLRPair |
117 kLsRsPair | kLbRbPair:
120 case kLFE | kTblTbrPair | kTflTfrPair | kLsRsPair | kCentre | kLRPair:
121 case kVhlVhrPair | kLFE | kTblTbrPair | kLsRsPair | kCentre | kLRPair:
124 case kTopCentre | kTopfrontCentre | kLFE | kTblTbrPair | kTflTfrPair |
125 kLsRsPair | kCentre | kLRPair:
126 case kVhlVhrPair | kTopCentre | kTopfrontCentre | kLFE | kTblTbrPair |
127 kLsRsPair | kCentre | kLRPair:
130 case kTopCentre | kTopfrontCentre | kLFE | kTblTbrPair | kTflTfrPair |
131 kCentre | kLRPair | kLsRsPair | kLbRbPair:
132 case kVhlVhrPair | kTopCentre | kTopfrontCentre | kLFE | kTblTbrPair |
133 kCentre | kLRPair | kLsRsPair | kLbRbPair:
136 case kLFE | kTblTbrPair | kTflTfrPair | kCentre | kLRPair | kLsRsPair |
138 case kVhlVhrPair | kLFE | kTblTbrPair | kCentre | kLRPair | kLsRsPair |
142 case kLscrRscrPair | kLFE | kTblTbrPair | kTflTfrPair | kCentre | kLRPair |
143 kLsRsPair | kLbRbPair:
144 case kVhlVhrPair | kLscrRscrPair | kLFE | kTblTbrPair | kCentre | kLRPair |
145 kLsRsPair | kLbRbPair:
156 bool ParseAC4SubStreamGroupDsi(BitReader& bit_reader) {
157 bool b_substream_present;
158 RCHECK(bit_reader.ReadBits(1, &b_substream_present));
160 RCHECK(bit_reader.ReadBits(1, &b_hsf_ext));
161 bool b_channel_coded;
162 RCHECK(bit_reader.ReadBits(1, &b_channel_coded));
163 uint8_t n_substreams;
164 RCHECK(bit_reader.ReadBits(8, &n_substreams));
165 for (uint8_t i = 0; i < n_substreams; i++) {
166 RCHECK(bit_reader.SkipBits(2));
167 bool b_substream_bitrate_indicator;
168 RCHECK(bit_reader.ReadBits(1, &b_substream_bitrate_indicator));
169 if (b_substream_bitrate_indicator) {
170 RCHECK(bit_reader.SkipBits(5));
172 if (b_channel_coded) {
173 RCHECK(bit_reader.SkipBits(24));
176 RCHECK(bit_reader.ReadBits(1, &b_ajoc));
179 RCHECK(bit_reader.ReadBits(1, &b_static_dmx));
181 RCHECK(bit_reader.SkipBits(4));
183 RCHECK(bit_reader.SkipBits(6));
185 RCHECK(bit_reader.SkipBits(4));
189 RCHECK(bit_reader.ReadBits(1, &b_content_type));
190 if (b_content_type) {
191 RCHECK(bit_reader.SkipBits(3));
192 bool b_language_indicator;
193 RCHECK(bit_reader.ReadBits(1, &b_language_indicator));
194 if (b_language_indicator) {
195 uint8_t n_language_tag_bytes;
196 RCHECK(bit_reader.ReadBits(6, &n_language_tag_bytes));
197 RCHECK(bit_reader.SkipBits(n_language_tag_bytes * 8));
205 bool ParseAC4PresentationV1Dsi(BitReader& bit_reader,
208 uint32_t* presentation_channel_mask_v1,
209 bool* dolby_cbi_indicator,
210 uint8_t* dolby_atmos_indicator) {
213 const size_t presentation_start = bit_reader.bit_position();
214 uint8_t presentation_config_v1;
215 RCHECK(bit_reader.ReadBits(5, &presentation_config_v1));
216 uint8_t b_add_emdf_substreams;
219 *presentation_channel_mask_v1 = 2;
220 *dolby_cbi_indicator =
false;
221 *dolby_atmos_indicator = 0;
222 if (presentation_config_v1 == 0x06) {
223 b_add_emdf_substreams = 1;
225 RCHECK(bit_reader.ReadBits(3, mdcompat));
226 bool b_presentation_id;
227 RCHECK(bit_reader.ReadBits(1, &b_presentation_id));
228 if (b_presentation_id) {
229 RCHECK(bit_reader.SkipBits(5));
231 RCHECK(bit_reader.SkipBits(19));
232 bool b_presentation_channel_coded;
233 RCHECK(bit_reader.ReadBits(1, &b_presentation_channel_coded));
234 *presentation_channel_mask_v1 = 0;
235 if (b_presentation_channel_coded) {
236 uint8_t dsi_presentation_ch_mode;
237 RCHECK(bit_reader.ReadBits(5, &dsi_presentation_ch_mode));
238 if (dsi_presentation_ch_mode >= 11 && dsi_presentation_ch_mode <= 14) {
239 RCHECK(bit_reader.SkipBits(1));
240 uint8_t pres_top_channel_pairs;
241 RCHECK(bit_reader.ReadBits(2, &pres_top_channel_pairs));
242 if (pres_top_channel_pairs) {
243 *dolby_cbi_indicator =
true;
245 }
else if (dsi_presentation_ch_mode == 15) {
246 *dolby_cbi_indicator =
true;
248 RCHECK(bit_reader.ReadBits(24, presentation_channel_mask_v1));
250 bool b_presentation_core_differs;
251 RCHECK(bit_reader.ReadBits(1, &b_presentation_core_differs));
252 if (b_presentation_core_differs) {
253 bool b_presentation_core_channel_coded;
254 RCHECK(bit_reader.ReadBits(1, &b_presentation_core_channel_coded));
255 if (b_presentation_core_channel_coded) {
256 RCHECK(bit_reader.SkipBits(2));
259 bool b_presentation_filter;
260 RCHECK(bit_reader.ReadBits(1, &b_presentation_filter));
261 if (b_presentation_filter) {
262 RCHECK(bit_reader.SkipBits(1));
263 uint8_t n_filter_bytes;
264 RCHECK(bit_reader.ReadBits(8, &n_filter_bytes));
265 RCHECK(bit_reader.SkipBits(n_filter_bytes * 8));
267 if (presentation_config_v1 == 0x1f) {
268 ret &= ParseAC4SubStreamGroupDsi(bit_reader);
270 RCHECK(bit_reader.SkipBits(1));
271 if (presentation_config_v1 == 0 ||
272 presentation_config_v1 == 1 ||
273 presentation_config_v1 == 2) {
274 ret &= ParseAC4SubStreamGroupDsi(bit_reader);
275 ret &= ParseAC4SubStreamGroupDsi(bit_reader);
277 if (presentation_config_v1 == 3 || presentation_config_v1 == 4) {
278 ret &= ParseAC4SubStreamGroupDsi(bit_reader);
279 ret &= ParseAC4SubStreamGroupDsi(bit_reader);
280 ret &= ParseAC4SubStreamGroupDsi(bit_reader);
282 if (presentation_config_v1 == 5) {
283 uint8_t n_substream_groups_minus2;
284 RCHECK(bit_reader.ReadBits(3, &n_substream_groups_minus2));
285 for (uint8_t sg = 0; sg < n_substream_groups_minus2 + 2; sg++) {
286 ret &= ParseAC4SubStreamGroupDsi(bit_reader);
289 if (presentation_config_v1 > 5) {
290 uint8_t n_skip_bytes;
291 RCHECK(bit_reader.ReadBits(7, &n_skip_bytes));
292 RCHECK(bit_reader.SkipBits(n_skip_bytes * 8));
295 RCHECK(bit_reader.SkipBits(1));
296 RCHECK(bit_reader.ReadBits(1, &b_add_emdf_substreams));
298 if (b_add_emdf_substreams) {
299 uint8_t n_add_emdf_substreams;
300 RCHECK(bit_reader.ReadBits(7, &n_add_emdf_substreams));
301 RCHECK(bit_reader.SkipBits(n_add_emdf_substreams * 15));
303 bool b_presentation_bitrate_info;
304 RCHECK(bit_reader.ReadBits(1, &b_presentation_bitrate_info));
305 if (b_presentation_bitrate_info) {
307 RCHECK(bit_reader.SkipBits(66));
310 RCHECK(bit_reader.ReadBits(1, &b_alternative));
312 bit_reader.SkipToNextByte();
315 RCHECK(bit_reader.ReadBits(16, &name_len));
316 RCHECK(bit_reader.SkipBits(name_len * 8));
318 RCHECK(bit_reader.ReadBits(5, &n_targets));
319 RCHECK(bit_reader.SkipBits(n_targets * 11));
321 bit_reader.SkipToNextByte();
322 if ((bit_reader.bit_position() - presentation_start) <=
323 (pres_bytes - 1) * 8) {
324 RCHECK(bit_reader.SkipBits(1));
325 RCHECK(bit_reader.ReadBits(1, dolby_atmos_indicator));
326 RCHECK(bit_reader.SkipBits(4));
327 bool b_extended_presentation_group_index;
328 RCHECK(bit_reader.ReadBits(1, &b_extended_presentation_group_index));
329 if (b_extended_presentation_group_index) {
330 RCHECK(bit_reader.SkipBits(9));
332 RCHECK(bit_reader.SkipBits(1));
338 bool ExtractAc4Data(
const std::vector<uint8_t>& ac4_data,
339 uint8_t* bitstream_version,
340 uint8_t* presentation_version,
342 uint32_t* presentation_channel_mask_v1,
343 bool* dolby_ims_indicator,
344 bool* dolby_cbi_indicator) {
345 BitReader bit_reader(ac4_data.data(), ac4_data.size());
347 uint16_t n_presentation;
348 RCHECK(bit_reader.SkipBits(3) && bit_reader.ReadBits(7, bitstream_version));
349 RCHECK(bit_reader.SkipBits(5) && bit_reader.ReadBits(9, &n_presentation));
351 if (*bitstream_version == 2) {
352 uint8_t b_program_id = 0;
353 RCHECK(bit_reader.ReadBits(1, &b_program_id));
355 RCHECK(bit_reader.SkipBits(16));
357 RCHECK(bit_reader.ReadBits(1, &b_uuid));
359 RCHECK(bit_reader.SkipBits(16 * 8));
362 }
else if (*bitstream_version == 0 || *bitstream_version == 1) {
363 LOG(WARNING) <<
"Bitstream version 0 or 1 is not supported";
366 LOG(WARNING) <<
"Invalid Bitstream version";
370 RCHECK(bit_reader.SkipBits(66));
371 bit_reader.SkipToNextByte();
378 const uint8_t ott_n_presentation = 1;
379 for (uint8_t i = 0; i < ott_n_presentation; i++) {
380 RCHECK(bit_reader.ReadBits(8, presentation_version));
382 if ((*presentation_version == 2 && n_presentation > 2) ||
383 (*presentation_version == 1 && n_presentation > 1) ) {
384 LOG(WARNING) <<
"Seeing multiple presentations, only single presentation "
385 <<
"(including IMS presentation) is supported";
389 RCHECK(bit_reader.ReadBits(8, &pres_bytes));
390 if (pres_bytes == 255) {
391 uint32_t add_pres_bytes;
392 RCHECK(bit_reader.ReadBits(16, &add_pres_bytes));
393 pres_bytes += add_pres_bytes;
396 size_t presentation_bits = 0;
397 *dolby_ims_indicator =
false;
398 if (*presentation_version == 0) {
399 LOG(WARNING) <<
"Presentation version 0 is not supported";
402 if (*presentation_version == 1 || *presentation_version == 2) {
403 if (*presentation_version == 2) {
404 *dolby_ims_indicator =
true;
406 const size_t presentation_start = bit_reader.bit_position();
411 uint8_t dolby_atmos_indicator;
412 if (!ParseAC4PresentationV1Dsi(bit_reader, pres_bytes, mdcompat,
413 presentation_channel_mask_v1,
415 &dolby_atmos_indicator)) {
418 const size_t presentation_end = bit_reader.bit_position();
419 presentation_bits = presentation_end - presentation_start;
421 LOG(WARNING) <<
"Invalid Presentation version";
425 size_t skip_bits = pres_bytes * 8 - presentation_bits;
426 RCHECK(bit_reader.SkipBits(skip_bits));
432 bool CalculateAC4ChannelMask(
const std::vector<uint8_t>& ac4_data,
433 uint32_t* ac4_channel_mask) {
434 uint8_t bitstream_version;
435 uint8_t presentation_version;
437 uint32_t pre_channel_mask;
438 bool dolby_ims_indicator;
439 bool dolby_cbi_indicator;
441 if (!ExtractAc4Data(ac4_data, &bitstream_version, &presentation_version,
442 &mdcompat, &pre_channel_mask, &dolby_ims_indicator,
443 &dolby_cbi_indicator)) {
444 LOG(WARNING) <<
"Seeing invalid AC4 data: "
445 << base::HexEncode(ac4_data.data(), ac4_data.size());
449 if (pre_channel_mask) {
450 *ac4_channel_mask = pre_channel_mask;
452 *ac4_channel_mask = 0x800000;
457 bool CalculateAC4ChannelMPEGValue(
const std::vector<uint8_t>& ac4_data,
458 uint32_t* ac4_channel_mpeg_value) {
459 uint8_t bitstream_version;
460 uint8_t presentation_version;
462 uint32_t pre_channel_mask;
463 bool dolby_ims_indicator;
464 bool dolby_cbi_indicator;
466 if (!ExtractAc4Data(ac4_data, &bitstream_version, &presentation_version,
467 &mdcompat, &pre_channel_mask, &dolby_ims_indicator,
468 &dolby_cbi_indicator)) {
469 LOG(WARNING) <<
"Seeing invalid AC4 data: "
470 << base::HexEncode(ac4_data.data(), ac4_data.size());
474 *ac4_channel_mpeg_value = AC4ChannelMasktoMPEGValue(pre_channel_mask);
478 bool GetAc4CodecInfo(
const std::vector<uint8_t>& ac4_data,
479 uint8_t* ac4_codec_info) {
480 uint8_t bitstream_version;
481 uint8_t presentation_version;
483 uint32_t pre_channel_mask;
484 bool dolby_ims_indicator;
485 bool dolby_cbi_indicator;
487 if (!ExtractAc4Data(ac4_data, &bitstream_version, &presentation_version,
488 &mdcompat, &pre_channel_mask, &dolby_ims_indicator,
489 &dolby_cbi_indicator)) {
490 LOG(WARNING) <<
"Seeing invalid AC4 data: "
491 << base::HexEncode(ac4_data.data(), ac4_data.size());
502 *ac4_codec_info = ((bitstream_version << 5) |
503 ((presentation_version << 3) & 0x1F) |
508 bool GetAc4ImmersiveInfo(
const std::vector<uint8_t>& ac4_data,
510 bool* ac4_cbi_flag) {
511 uint8_t bitstream_version;
512 uint8_t presentation_version;
514 uint32_t pre_channel_mask;
516 if (!ExtractAc4Data(ac4_data, &bitstream_version, &presentation_version,
517 &mdcompat, &pre_channel_mask, ac4_ims_flag,
519 LOG(WARNING) <<
"Seeing invalid AC4 data: "
520 << base::HexEncode(ac4_data.data(), ac4_data.size());