7#include <packager/media/codecs/ac4_parser.h>
11#include <absl/log/check.h>
12#include <absl/log/log.h>
14#include <packager/macros/logging.h>
15#include <packager/media/base/bit_reader.h>
16#include <packager/media/base/rcheck.h>
18using namespace shaka::media;
25const int CH_MODE_MONO = 0;
26const int CH_MODE_STEREO = 1;
27const int CH_MODE_3_0 = 2;
28const int CH_MODE_5_0 = 3;
29const int CH_MODE_5_1 = 4;
30const int CH_MODE_70_34 = 5;
31const int CH_MODE_71_34 = 6;
32const int CH_MODE_70_52 = 7;
33const int CH_MODE_71_52 = 8;
34const int CH_MODE_70_322 = 9;
35const int CH_MODE_71_322 = 10;
36const int CH_MODE_7_0_4 = 11;
37const int CH_MODE_7_1_4 = 12;
38const int CH_MODE_9_0_4 = 13;
39const int CH_MODE_9_1_4 = 14;
40const int CH_MODE_22_2 = 15;
41const int CH_MODE_RESERVED = 16;
43int ReadAc4VariableBits(
BitReader* reader,
int nBits) {
48 RCHECK(reader->
ReadBits(nBits, &extra_value));
50 RCHECK(reader->
ReadBits(1, &b_moreBits));
51 if (b_moreBits == 1) {
53 value += (1 << nBits);
55 }
while (b_moreBits == 1);
61AC4Parser::AC4Parser() =
default;
62AC4Parser::~AC4Parser() =
default;
64bool AC4Parser::Parse(
const uint8_t* data,
size_t data_size) {
66 if (!ParseAc4Toc(&reader))
71bool AC4Parser::ParseAc4Toc(
BitReader* reader) {
74 RCHECK(reader->
ReadBits(2, &ac4_toc.bitstream_version));
75 if (ac4_toc.bitstream_version == 3) {
76 ac4_toc.bitstream_version = ReadAc4VariableBits(reader, 2);
78 RCHECK(reader->
ReadBits(10, &ac4_toc.sequence_counter));
79 RCHECK(reader->
ReadBits(1, &ac4_toc.b_wait_frames));
80 if (ac4_toc.b_wait_frames) {
81 RCHECK(reader->
ReadBits(3, &ac4_toc.wait_frames));
82 if (ac4_toc.wait_frames > 0) {
83 RCHECK(reader->
ReadBits(2, &ac4_toc.br_code));
86 RCHECK(reader->
ReadBits(1, &ac4_toc.fs_index));
87 RCHECK(reader->
ReadBits(4, &ac4_toc.frame_rate_index));
88 RCHECK(reader->
ReadBits(1, &ac4_toc.b_iframe_global));
89 RCHECK(reader->
ReadBits(1, &ac4_toc.b_single_presentation));
90 if (ac4_toc.b_single_presentation) {
91 ac4_toc.n_presentations = 1;
93 RCHECK(reader->
ReadBits(1, &ac4_toc.b_more_presentations));
94 if (ac4_toc.b_more_presentations) {
95 ac4_toc.n_presentations = ReadAc4VariableBits(reader, 2) + 2;
97 ac4_toc.n_presentations = 0;
100 int payload_base = 0;
101 RCHECK(reader->
ReadBits(1, &ac4_toc.b_payload_base));
102 if (ac4_toc.b_payload_base) {
103 RCHECK(reader->
ReadBits(5, &ac4_toc.payload_base_minus1));
104 payload_base = ac4_toc.payload_base_minus1 + 1;
105 if (payload_base == 0x20) {
106 payload_base = ReadAc4VariableBits(reader, 3);
109 if (ac4_toc.bitstream_version <= 1) {
110 printf(
"Warning: Bitstream version 0 is deprecated\n");
112 RCHECK(reader->
ReadBits(1, &ac4_toc.b_program_id));
113 if (ac4_toc.b_program_id) {
114 RCHECK(reader->
ReadBits(16, &ac4_toc.short_program_id));
115 RCHECK(reader->
ReadBits(1, &ac4_toc.b_program_uuid_present));
116 if (ac4_toc.b_program_uuid_present) {
117 for (
int cnt = 0; cnt < 16; cnt++) {
118 RCHECK(reader->
ReadBits(8, &ac4_toc.program_uuid));
122 int max_group_index = 0;
123 ac4_toc.presentation_v1_infos =
124 new Ac4PresentationV1Info[ac4_toc.n_presentations];
125 for (
int i = 0; i < ac4_toc.n_presentations; i++) {
126 ParseAc4PresentationV1Info(reader, ac4_toc.presentation_v1_infos[i],
129 ac4_toc.total_n_substream_groups = max_group_index + 1;
130 ac4_toc.substream_group_infos =
131 new Ac4SubstreamGroupInfo[ac4_toc.total_n_substream_groups];
132 for (
int j = 0; j < ac4_toc.total_n_substream_groups; j++) {
133 ParseAc4SubstreamGroupInfo(reader, ac4_toc.substream_group_infos[j], j);
138 int n_substreams = 0;
139 RCHECK(reader->
ReadBits(2, &n_substreams));
140 if (n_substreams == 0) {
141 n_substreams = ReadAc4VariableBits(reader, 2) + 4;
143 int b_size_present = 0;
144 if (n_substreams == 1) {
145 RCHECK(reader->
ReadBits(1, &b_size_present));
149 if (b_size_present) {
150 for (
int s = 0; s < n_substreams; s++) {
152 RCHECK(reader->
ReadBits(1, &b_more_bits));
153 int substream_size = 0;
154 RCHECK(reader->
ReadBits(10, &substream_size));
156 substream_size += (ReadAc4VariableBits(reader, 2) << 10);
165bool AC4Parser::ParseAc4PresentationV1Info(
167 Ac4PresentationV1Info& ac4_presentation_v1_info,
168 int& max_group_index) {
171 reader->
ReadBits(1, &ac4_presentation_v1_info.b_single_substream_group));
172 if (ac4_presentation_v1_info.b_single_substream_group != 1) {
173 RCHECK(reader->
ReadBits(3, &ac4_presentation_v1_info.presentation_config));
174 if (ac4_presentation_v1_info.presentation_config == 7) {
175 ac4_presentation_v1_info.presentation_config =
176 ReadAc4VariableBits(reader, 2);
179 if (ac4_toc.bitstream_version != 1) {
183 while (reader->
ReadBits(1, &more_bits) && more_bits) {
184 ac4_presentation_v1_info.presentation_version++;
187 if (ac4_presentation_v1_info.b_single_substream_group != 1 &&
188 ac4_presentation_v1_info.presentation_config == 6) {
189 ac4_presentation_v1_info.b_add_emdf_substreams = 1;
191 if (ac4_toc.bitstream_version != 1) {
192 RCHECK(reader->
ReadBits(3, &ac4_presentation_v1_info.mdcompat));
194 RCHECK(reader->
ReadBits(1, &ac4_presentation_v1_info.b_presentation_id));
195 if (ac4_presentation_v1_info.b_presentation_id) {
196 ac4_presentation_v1_info.b_presentation_id =
197 ReadAc4VariableBits(reader, 2);
199 ParseFrameRateMultiplyInfo(reader);
200 ParseFrameRateFractionsInfo(reader);
201 ParseEmdfInfo(reader);
204 reader->
ReadBits(1, &ac4_presentation_v1_info.b_presentation_filter));
206 if (ac4_presentation_v1_info.b_presentation_filter) {
208 reader->
ReadBits(1, &ac4_presentation_v1_info.b_enable_presentation));
210 if (ac4_presentation_v1_info.b_single_substream_group == 1) {
211 group_index = ParseAc4SgiSpecifier(reader);
213 group_index > max_group_index ? group_index : max_group_index;
214 ac4_presentation_v1_info.n_substream_groups = 1;
216 reader->
ReadBits(1, &ac4_presentation_v1_info.b_multi_pid);
217 switch (ac4_presentation_v1_info.presentation_config) {
219 group_index = ParseAc4SgiSpecifier(reader);
221 group_index > max_group_index ? group_index : max_group_index;
222 group_index = ParseAc4SgiSpecifier(reader);
224 group_index > max_group_index ? group_index : max_group_index;
225 ac4_presentation_v1_info.n_substream_groups = 2;
228 group_index = ParseAc4SgiSpecifier(reader);
230 group_index > max_group_index ? group_index : max_group_index;
231 group_index = ParseAc4SgiSpecifier(reader);
233 group_index > max_group_index ? group_index : max_group_index;
234 ac4_presentation_v1_info.n_substream_groups = 1;
237 group_index = ParseAc4SgiSpecifier(reader);
239 group_index > max_group_index ? group_index : max_group_index;
240 group_index = ParseAc4SgiSpecifier(reader);
242 group_index > max_group_index ? group_index : max_group_index;
243 ac4_presentation_v1_info.n_substream_groups = 2;
246 group_index = ParseAc4SgiSpecifier(reader);
248 group_index > max_group_index ? group_index : max_group_index;
249 group_index = ParseAc4SgiSpecifier(reader);
251 group_index > max_group_index ? group_index : max_group_index;
252 group_index = ParseAc4SgiSpecifier(reader);
254 group_index > max_group_index ? group_index : max_group_index;
255 ac4_presentation_v1_info.n_substream_groups = 3;
258 group_index = ParseAc4SgiSpecifier(reader);
260 group_index > max_group_index ? group_index : max_group_index;
261 group_index = ParseAc4SgiSpecifier(reader);
263 group_index > max_group_index ? group_index : max_group_index;
264 group_index = ParseAc4SgiSpecifier(reader);
266 group_index > max_group_index ? group_index : max_group_index;
267 ac4_presentation_v1_info.n_substream_groups = 2;
271 2, &ac4_presentation_v1_info.n_substream_groups_minus2));
272 ac4_presentation_v1_info.n_substream_groups =
273 ac4_presentation_v1_info.n_substream_groups_minus2 + 2;
274 if (ac4_presentation_v1_info.n_substream_groups == 5) {
275 ac4_presentation_v1_info.n_substream_groups +=
276 ReadAc4VariableBits(reader, 2);
278 for (
int sg = 0; sg < ac4_presentation_v1_info.n_substream_groups;
280 group_index = ParseAc4SgiSpecifier(reader);
282 group_index > max_group_index ? group_index : max_group_index;
286 ParsePresentationConfigExtInfo(reader);
290 RCHECK(reader->
ReadBits(1, &ac4_presentation_v1_info.b_pre_virtualized));
292 reader->
ReadBits(1, &ac4_presentation_v1_info.b_add_emdf_substreams));
293 ParseAc4PresentationSubstreamInfo(reader);
295 if (ac4_presentation_v1_info.b_add_emdf_substreams) {
297 reader->
ReadBits(2, &ac4_presentation_v1_info.n_add_emdf_substreams));
298 if (ac4_presentation_v1_info.n_add_emdf_substreams == 0) {
299 ac4_presentation_v1_info.n_add_emdf_substreams =
300 ReadAc4VariableBits(reader, 2) + 4;
302 for (
int i = 0; i < ac4_presentation_v1_info.n_add_emdf_substreams; i++) {
303 ParseEmdfInfo(reader);
309bool AC4Parser::ParseFrameRateMultiplyInfo(
BitReader* reader) {
310 FrameRateMultiplyInfo frame_rate_multiply_info;
311 switch (ac4_toc.frame_rate_index) {
315 RCHECK(reader->
ReadBits(1, &frame_rate_multiply_info.b_multiplier));
316 if (frame_rate_multiply_info.b_multiplier) {
318 1, &frame_rate_multiply_info.multiplier_bit));
326 RCHECK(reader->
ReadBits(1, &frame_rate_multiply_info.b_multiplier));
334bool AC4Parser::ParseFrameRateFractionsInfo(
BitReader* reader) {
335 FrameRateFractionsInfo frame_rate_fractions_info;
336 if (ac4_toc.frame_rate_index >= 5 && ac4_toc.frame_rate_index <= 9) {
338 reader->
ReadBits(1, &frame_rate_fractions_info.b_frame_rate_fraction));
340 if (ac4_toc.frame_rate_index >= 10 && ac4_toc.frame_rate_index <= 12) {
342 reader->
ReadBits(1, &frame_rate_fractions_info.b_frame_rate_fraction));
343 if (frame_rate_fractions_info.b_frame_rate_fraction == 1) {
345 1, &frame_rate_fractions_info.b_frame_rate_fraction_is_4));
351bool AC4Parser::ParseEmdfInfo(
BitReader* reader) {
353 RCHECK(reader->
ReadBits(2, &emdf_info.emdf_version));
354 if (emdf_info.emdf_version == 3) {
355 emdf_info.emdf_version += ReadAc4VariableBits(reader, 2);
357 RCHECK(reader->
ReadBits(3, &emdf_info.key_id));
358 if (emdf_info.key_id == 7) {
359 emdf_info.key_id += ReadAc4VariableBits(reader, 3);
361 RCHECK(reader->
ReadBits(1, &emdf_info.b_emdf_payloads_substream_info));
362 if (emdf_info.b_emdf_payloads_substream_info) {
363 RCHECK(reader->
ReadBits(2, &emdf_info.substream_index));
364 if (emdf_info.substream_index == 3) {
365 emdf_info.substream_index += ReadAc4VariableBits(reader, 2);
368 RCHECK(reader->
ReadBits(2, &emdf_info.protection_length_primary));
369 RCHECK(reader->
ReadBits(2, &emdf_info.protection_length_secondary));
370 switch (emdf_info.protection_length_primary) {
372 RCHECK(reader->
ReadBits(8, &emdf_info.protection_bits_primary[0]));
375 for (
unsigned idx = 0; idx < 4; idx++) {
376 RCHECK(reader->
ReadBits(8, &emdf_info.protection_bits_primary[idx]));
380 for (
unsigned idx = 0; idx < 16; idx++) {
381 RCHECK(reader->
ReadBits(8, &emdf_info.protection_bits_primary[idx]));
385 LOG(ERROR) <<
"Invalid EMDF primary protection length: "
386 <<
static_cast<int>(emdf_info.protection_length_primary);
391 switch (emdf_info.protection_length_secondary) {
395 RCHECK(reader->
ReadBits(8, &emdf_info.protection_bits_secondary[0]));
398 for (
unsigned idx = 0; idx < 4; idx++) {
399 RCHECK(reader->
ReadBits(8, &emdf_info.protection_bits_secondary[idx]));
403 for (
unsigned idx = 0; idx < 16; idx++) {
404 RCHECK(reader->
ReadBits(8, &emdf_info.protection_bits_secondary[idx]));
408 LOG(ERROR) <<
"Invalid EMDF secondary protection length: "
409 <<
static_cast<int>(emdf_info.protection_length_secondary);
415bool AC4Parser::ParseAc4PresentationSubstreamInfo(
BitReader* reader) {
416 Ac4PresentationV1Info ac4_presentation_v1_info;
417 RCHECK(reader->
ReadBits(1, &ac4_presentation_v1_info.b_alternative));
418 RCHECK(reader->
ReadBits(1, &ac4_presentation_v1_info.b_pres_ndot));
419 RCHECK(reader->
ReadBits(2, &ac4_presentation_v1_info.substream_index));
420 if (ac4_presentation_v1_info.substream_index == 3) {
421 ac4_presentation_v1_info.substream_index += ReadAc4VariableBits(reader, 2);
426int AC4Parser::ParseAc4SgiSpecifier(
BitReader* reader) {
428 if (ac4_toc.bitstream_version == 1) {
431 RCHECK(reader->
ReadBits(3, &group_index));
432 if (group_index == 7) {
433 group_index += ReadAc4VariableBits(reader, 2);
439bool AC4Parser::ParsePresentationConfigExtInfo(
BitReader* reader) {
441 int b_more_skip_bytes;
442 RCHECK(reader->
ReadBits(1, &n_skip_bytes));
443 RCHECK(reader->
ReadBits(1, &b_more_skip_bytes));
444 if (b_more_skip_bytes) {
445 n_skip_bytes += ReadAc4VariableBits(reader, 2) << 5;
447 for (
int i = 0; i < n_skip_bytes; i++) {
453int AC4Parser::GetPresentationIdx(
int substream_group_index) {
454 for (
int idx = 0; idx < ac4_toc.n_presentations; idx++) {
455 for (
int sg = 0; sg < ac4_toc.presentation_v1_infos[idx].n_substream_groups;
457 if (substream_group_index ==
458 ac4_toc.presentation_v1_infos[idx].group_index[sg]) {
466int AC4Parser::GetPresentationVersion(
int substream_group_index) {
467 for (
int idx = 0; idx < ac4_toc.n_presentations; idx++) {
468 for (
int sg = 0; sg < ac4_toc.presentation_v1_infos[idx].n_substream_groups;
470 if (substream_group_index ==
471 ac4_toc.presentation_v1_infos[idx].group_index[sg]) {
472 return ac4_toc.presentation_v1_infos[idx].presentation_version;
479bool AC4Parser::ParseAc4SubstreamGroupInfo(
481 Ac4SubstreamGroupInfo& ac4_substream_group_info,
482 int substream_group_index) {
483 RCHECK(reader->
ReadBits(1, &ac4_substream_group_info.b_substreams_present));
484 RCHECK(reader->
ReadBits(1, &ac4_substream_group_info.b_hsf_ext));
485 RCHECK(reader->
ReadBits(1, &ac4_substream_group_info.b_single_substream));
486 if (ac4_substream_group_info.b_single_substream) {
487 ac4_substream_group_info.n_lf_substreams = 1;
490 reader->
ReadBits(2, &ac4_substream_group_info.n_lf_substreams_minus2));
491 ac4_substream_group_info.n_lf_substreams =
492 ac4_substream_group_info.n_lf_substreams_minus2 + 2;
493 if (ac4_substream_group_info.n_lf_substreams == 5) {
494 ac4_substream_group_info.n_lf_substreams +=
495 ReadAc4VariableBits(reader, 2);
498 RCHECK(reader->
ReadBits(1, &ac4_substream_group_info.b_channel_coded));
500 int pre_idx = GetPresentationIdx(substream_group_index);
501 int frame_rate_factor =
502 (ac4_toc.presentation_v1_infos[pre_idx]
503 .frame_rate_multiply_info.dsi_frame_rate_multiply_info == 0)
505 : (ac4_toc.presentation_v1_infos[pre_idx]
506 .frame_rate_multiply_info.dsi_frame_rate_multiply_info *
508 if (ac4_substream_group_info.b_channel_coded) {
509 for (
int sus = 0; sus < ac4_substream_group_info.n_lf_substreams; sus++) {
510 if (ac4_toc.bitstream_version == 1) {
513 ac4_substream_group_info.sus_ver = 1;
515 ParseAc4SubstreamInfoChan(reader,
516 GetPresentationVersion(substream_group_index),
517 ac4_toc.fs_index, frame_rate_factor,
518 ac4_substream_group_info.b_substreams_present);
519 if (ac4_substream_group_info.b_hsf_ext) {
520 ParseAc4HsfExtSubstreamInfo(reader, ac4_substream_group_info);
524 RCHECK(reader->
ReadBits(1, &ac4_substream_group_info.b_oamd_substream));
525 if (ac4_substream_group_info.b_oamd_substream) {
526 ParseOamdSubstreamInfo(reader, ac4_substream_group_info);
528 for (
int sus = 0; sus < ac4_substream_group_info.n_lf_substreams; sus++) {
529 RCHECK(reader->
ReadBits(1, &ac4_substream_group_info.b_ajoc));
530 if (ac4_substream_group_info.b_ajoc) {
531 ParseAc4SubstreamInfoAjoc(
532 reader, ac4_toc.fs_index, frame_rate_factor,
533 ac4_substream_group_info.b_substreams_present);
534 if (ac4_substream_group_info.b_hsf_ext) {
535 ParseAc4HsfExtSubstreamInfo(reader, ac4_substream_group_info);
538 ParseAc4SubstreamInfoObj(reader, ac4_toc.fs_index, frame_rate_factor,
539 ac4_substream_group_info.b_substreams_present);
540 if (ac4_substream_group_info.b_hsf_ext) {
541 ParseAc4HsfExtSubstreamInfo(reader, ac4_substream_group_info);
546 RCHECK(reader->
ReadBits(1, &ac4_substream_group_info.b_content_type));
547 if (ac4_substream_group_info.b_content_type) {
548 ParseContentType(reader, ac4_substream_group_info);
553bool AC4Parser::ParseContentType(
555 Ac4SubstreamGroupInfo& ac4_substream_group_info) {
556 RCHECK(reader->
ReadBits(3, &ac4_substream_group_info.content_classifier));
557 RCHECK(reader->
ReadBits(1, &ac4_substream_group_info.b_language_indicator));
558 if (ac4_substream_group_info.b_language_indicator) {
560 1, &ac4_substream_group_info.b_serialized_language_tag));
561 if (ac4_substream_group_info.b_serialized_language_tag) {
562 RCHECK(reader->
ReadBits(1, &ac4_substream_group_info.b_start_tag));
564 reader->
ReadBits(16, &ac4_substream_group_info.language_tag_chunk));
567 reader->
ReadBits(6, &ac4_substream_group_info.n_language_tag_bytes));
568 for (
int i = 0; i < ac4_substream_group_info.n_language_tag_bytes; i++) {
570 8, &ac4_substream_group_info.language_tag_bytes[i]));
577bool AC4Parser::ParseOamdSubstreamInfo(
579 Ac4SubstreamGroupInfo& ac4_substream_group_info) {
580 RCHECK(reader->
ReadBits(1, &ac4_substream_group_info.b_oamd_ndot));
581 if (ac4_substream_group_info.b_substreams_present == 1) {
582 RCHECK(reader->
ReadBits(2, &ac4_substream_group_info.substream_index));
583 if (ac4_substream_group_info.substream_index == 3) {
584 ac4_substream_group_info.substream_index +=
585 ReadAc4VariableBits(reader, 2);
591bool AC4Parser::ParseAc4HsfExtSubstreamInfo(
593 Ac4SubstreamGroupInfo& ac4_substream_group_info) {
594 if (ac4_substream_group_info.b_substreams_present == 1) {
595 RCHECK(reader->
ReadBits(2, &ac4_substream_group_info.substream_index));
596 if (ac4_substream_group_info.substream_index == 3) {
597 ac4_substream_group_info.substream_index +=
598 ReadAc4VariableBits(reader, 2);
604bool AC4Parser::ParseAc4SubstreamInfoChan(
BitReader* reader,
605 int presentation_version,
607 int frame_rate_factor,
608 int b_substreams_present) {
609 Ac4SubstreamInfoChan ac4_substream_info_chan;
610 ac4_substream_info_chan.channel_mode =
611 ParseChannelMode(reader, presentation_version);
612 if (ac4_substream_info_chan.channel_mode == 0b111111111) {
613 ac4_substream_info_chan.channel_mode += ReadAc4VariableBits(reader, 2);
615 if ((ac4_substream_info_chan.channel_mode >= CH_MODE_7_0_4) &&
616 (ac4_substream_info_chan.channel_mode <= CH_MODE_9_1_4)) {
618 1, &ac4_substream_info_chan.b_4_back_channels_present));
619 RCHECK(reader->
ReadBits(1, &ac4_substream_info_chan.b_centre_present));
620 RCHECK(reader->
ReadBits(2, &ac4_substream_info_chan.top_channels_present));
623 RCHECK(reader->
ReadBits(1, &ac4_substream_info_chan.b_sf_multiplier));
624 if (ac4_substream_info_chan.b_sf_multiplier) {
625 RCHECK(reader->
ReadBits(1, &ac4_substream_info_chan.sf_multiplier));
628 RCHECK(reader->
ReadBits(1, &ac4_substream_info_chan.b_bitrate_info));
629 if (ac4_substream_info_chan.b_bitrate_info) {
630 RCHECK(reader->
ReadBits(3, &ac4_substream_info_chan.bitrate_indicator));
631 if ((ac4_substream_info_chan.bitrate_indicator & 0x1) == 1) {
633 RCHECK(reader->
ReadBits(2, &more_bits));
634 ac4_substream_info_chan.bitrate_indicator =
635 (ac4_substream_info_chan.bitrate_indicator << 2) + more_bits;
638 if (ac4_substream_info_chan.channel_mode >= CH_MODE_70_52 &&
639 ac4_substream_info_chan.channel_mode <= CH_MODE_71_322) {
640 RCHECK(reader->
ReadBits(1, &ac4_substream_info_chan.add_ch_base));
642 for (
int i = 0; i < frame_rate_factor; i++) {
643 RCHECK(reader->
ReadBits(1, &ac4_substream_info_chan.b_audio_ndot));
645 if (b_substreams_present == 1) {
646 RCHECK(reader->
ReadBits(2, &ac4_substream_info_chan.substream_index));
647 if (ac4_substream_info_chan.substream_index == 3) {
648 ac4_substream_info_chan.substream_index += ReadAc4VariableBits(reader, 2);
654bool AC4Parser::ParseAc4SubstreamInfoAjoc(
BitReader* reader,
656 int frame_rate_factor,
657 int b_substreams_present) {
658 Ac4SubstreamInfoAjoc ac4_substream_info_ajoc;
659 RCHECK(reader->
ReadBits(1, &ac4_substream_info_ajoc.b_lfe));
660 RCHECK(reader->
ReadBits(1, &ac4_substream_info_ajoc.b_static_dmx));
661 if (ac4_substream_info_ajoc.b_static_dmx) {
662 ac4_substream_info_ajoc.n_fullband_dmx_signals = 5;
665 4, &ac4_substream_info_ajoc.n_fullband_dmx_signals_minus1));
666 ac4_substream_info_ajoc.n_fullband_dmx_signals =
667 ac4_substream_info_ajoc.n_fullband_dmx_signals_minus1 + 1;
668 ParseBedDynObjAssignment(reader,
669 ac4_substream_info_ajoc.n_fullband_dmx_signals,
670 ac4_substream_info_ajoc);
673 reader->
ReadBits(1, &ac4_substream_info_ajoc.b_oamd_common_data_present));
674 if (ac4_substream_info_ajoc.b_oamd_common_data_present) {
675 ParseOamdCommonData(reader);
678 4, &ac4_substream_info_ajoc.n_fullband_upmix_signals_minus1));
679 if (ac4_substream_info_ajoc.n_fullband_upmix_signals_minus1 + 1 == 16) {
680 ac4_substream_info_ajoc.n_fullband_upmix_signals_minus1 +=
681 ReadAc4VariableBits(reader, 3);
683 ParseBedDynObjAssignment(
684 reader, ac4_substream_info_ajoc.n_fullband_upmix_signals_minus1 + 1,
685 ac4_substream_info_ajoc);
687 RCHECK(reader->
ReadBits(1, &ac4_substream_info_ajoc.b_sf_multiplier));
688 if (ac4_substream_info_ajoc.b_sf_multiplier) {
689 RCHECK(reader->
ReadBits(1, &ac4_substream_info_ajoc.sf_multiplier));
692 RCHECK(reader->
ReadBits(1, &ac4_substream_info_ajoc.b_bitrate_info));
693 if (ac4_substream_info_ajoc.b_bitrate_info) {
694 RCHECK(reader->
ReadBits(3, &ac4_substream_info_ajoc.bitrate_indicator));
695 if ((ac4_substream_info_ajoc.bitrate_indicator & 0x1) == 1) {
697 RCHECK(reader->
ReadBits(2, &more_bits));
698 ac4_substream_info_ajoc.bitrate_indicator =
699 (ac4_substream_info_ajoc.bitrate_indicator << 2) + more_bits;
702 for (
int i = 0; i < frame_rate_factor; i++) {
703 RCHECK(reader->
ReadBits(1, &ac4_substream_info_ajoc.b_audio_ndot));
705 if (b_substreams_present == 1) {
706 RCHECK(reader->
ReadBits(2, &ac4_substream_info_ajoc.substream_index));
707 if (ac4_substream_info_ajoc.substream_index == 3) {
708 ac4_substream_info_ajoc.substream_index += ReadAc4VariableBits(reader, 2);
715bool AC4Parser::ParseBedDynObjAssignment(
718 Ac4SubstreamInfoAjoc& ac4_substream_info_ajoc) {
719 RCHECK(reader->
ReadBits(1, &ac4_substream_info_ajoc.b_dyn_objects_only));
720 if (ac4_substream_info_ajoc.b_dyn_objects_only == 0) {
721 RCHECK(reader->
ReadBits(1, &ac4_substream_info_ajoc.b_isf));
722 if (ac4_substream_info_ajoc.b_isf) {
723 RCHECK(reader->
ReadBits(3, &ac4_substream_info_ajoc.isf_config));
725 RCHECK(reader->
ReadBits(1, &ac4_substream_info_ajoc.b_ch_assign_code));
726 if (ac4_substream_info_ajoc.b_ch_assign_code) {
728 reader->
ReadBits(3, &ac4_substream_info_ajoc.bed_chan_assign_code));
731 reader->
ReadBits(1, &ac4_substream_info_ajoc.b_chan_assign_mask));
732 if (ac4_substream_info_ajoc.b_chan_assign_mask) {
734 1, &ac4_substream_info_ajoc.b_nonstd_bed_channel_assignment));
735 if (ac4_substream_info_ajoc.b_nonstd_bed_channel_assignment) {
738 &ac4_substream_info_ajoc.nonstd_bed_channel_assignment_mask));
741 10, &ac4_substream_info_ajoc.std_bed_channel_assignment_mask));
745 int bed_ch_bits = (int)ceil(log((
float)n_signals) / log((
float)2));
747 bed_ch_bits, &ac4_substream_info_ajoc.n_bed_signals_minus1));
748 ac4_substream_info_ajoc.n_bed_signals =
749 ac4_substream_info_ajoc.n_bed_signals_minus1 + 1;
751 ac4_substream_info_ajoc.n_bed_signals = 1;
753 for (
int b = 0; b < ac4_substream_info_ajoc.n_bed_signals; b++) {
755 4, &ac4_substream_info_ajoc.nonstd_bed_channel_assignment));
764bool AC4Parser::ParseOamdCommonData(
BitReader* reader) {
765 OamdCommonData oamd_common_data;
766 RCHECK(reader->
ReadBits(1, &oamd_common_data.b_default_screen_size_ratio));
767 if (oamd_common_data.b_default_screen_size_ratio == 0) {
769 reader->
ReadBits(5, &oamd_common_data.master_screen_size_ratio_code));
771 RCHECK(reader->
ReadBits(1, &oamd_common_data.b_bed_object_chan_distribute));
772 RCHECK(reader->
ReadBits(1, &oamd_common_data.b_additional_data));
773 if (oamd_common_data.b_additional_data) {
774 RCHECK(reader->
ReadBits(1, &oamd_common_data.add_data_bytes_minus1));
775 oamd_common_data.add_data_bytes =
776 oamd_common_data.add_data_bytes_minus1 + 1;
777 if (oamd_common_data.add_data_bytes == 2) {
778 oamd_common_data.add_data_bytes += ReadAc4VariableBits(reader, 2);
780 reader->
SkipBytes(oamd_common_data.add_data_bytes);
785bool AC4Parser::ParseAc4SubstreamInfoObj(
BitReader* reader,
787 int frame_rate_factor,
788 int b_substreams_present) {
789 Ac4SubstreamInfoObj ac4_substream_info_obj;
790 RCHECK(reader->
ReadBits(3, &ac4_substream_info_obj.n_objects_code));
791 RCHECK(reader->
ReadBits(1, &ac4_substream_info_obj.b_dynamic_objects));
792 if (ac4_substream_info_obj.b_dynamic_objects) {
793 RCHECK(reader->
ReadBits(1, &ac4_substream_info_obj.b_lfe));
795 RCHECK(reader->
ReadBits(1, &ac4_substream_info_obj.b_bed_objects));
796 if (ac4_substream_info_obj.b_bed_objects) {
797 RCHECK(reader->
ReadBits(1, &ac4_substream_info_obj.b_bed_start));
798 if (ac4_substream_info_obj.b_bed_start) {
799 RCHECK(reader->
ReadBits(1, &ac4_substream_info_obj.b_ch_assign_code));
800 if (ac4_substream_info_obj.b_ch_assign_code) {
802 3, &ac4_substream_info_obj.bed_chan_assign_code));
805 1, &ac4_substream_info_obj.b_nonstd_bed_channel_assignment));
806 if (ac4_substream_info_obj.b_nonstd_bed_channel_assignment) {
809 &ac4_substream_info_obj.nonstd_bed_channel_assignment_mask));
812 10, &ac4_substream_info_obj.std_bed_channel_assignment_mask));
817 RCHECK(reader->
ReadBits(1, &ac4_substream_info_obj.b_isf));
818 if (ac4_substream_info_obj.b_isf) {
819 RCHECK(reader->
ReadBits(1, &ac4_substream_info_obj.b_isf_start));
820 if (ac4_substream_info_obj.b_isf_start) {
821 RCHECK(reader->
ReadBits(1, &ac4_substream_info_obj.isf_config));
825 RCHECK(reader->
ReadBits(4, &res_bytes));
831 RCHECK(reader->
ReadBits(1, &ac4_substream_info_obj.b_sf_multiplier));
832 if (ac4_substream_info_obj.b_sf_multiplier) {
833 RCHECK(reader->
ReadBits(1, &ac4_substream_info_obj.sf_multiplier));
836 RCHECK(reader->
ReadBits(1, &ac4_substream_info_obj.b_bitrate_info));
837 if (ac4_substream_info_obj.b_bitrate_info) {
838 RCHECK(reader->
ReadBits(3, &ac4_substream_info_obj.bitrate_indicator));
839 if ((ac4_substream_info_obj.bitrate_indicator & 0x1) == 1) {
841 RCHECK(reader->
ReadBits(2, &more_bits));
842 ac4_substream_info_obj.bitrate_indicator =
843 (ac4_substream_info_obj.bitrate_indicator << 2) + more_bits;
846 for (
int i = 0; i < frame_rate_factor; i++) {
847 RCHECK(reader->
ReadBits(1, &ac4_substream_info_obj.b_audio_ndot));
849 if (b_substreams_present == 1) {
850 RCHECK(reader->
ReadBits(2, &ac4_substream_info_obj.substream_index));
851 if (ac4_substream_info_obj.substream_index == 3) {
852 ac4_substream_info_obj.substream_index += ReadAc4VariableBits(reader, 2);
859int AC4Parser::ParseChannelMode(
BitReader* reader,
int presentation_version) {
860 int channel_mode_code = 0;
862 RCHECK(reader->
ReadBits(1, &channel_mode_code));
863 if (channel_mode_code == 0) {
866 RCHECK(reader->
ReadBits(1, &read_more));
867 channel_mode_code = (channel_mode_code << 1) | read_more;
868 if (channel_mode_code == 2) {
869 return CH_MODE_STEREO;
871 RCHECK(reader->
ReadBits(2, &read_more));
872 channel_mode_code = (channel_mode_code << 2) | read_more;
873 switch (channel_mode_code) {
881 RCHECK(reader->
ReadBits(3, &read_more));
882 channel_mode_code = (channel_mode_code << 3) | read_more;
883 switch (channel_mode_code) {
885 if (presentation_version == 2) {
886 return CH_MODE_STEREO;
888 return CH_MODE_70_34;
891 if (presentation_version == 2) {
893 return CH_MODE_STEREO;
895 return CH_MODE_71_34;
898 return CH_MODE_70_52;
900 return CH_MODE_71_52;
902 return CH_MODE_70_322;
904 return CH_MODE_71_322;
906 RCHECK(reader->
ReadBits(1, &read_more));
907 channel_mode_code = (channel_mode_code << 1) | read_more;
908 switch (channel_mode_code) {
910 return CH_MODE_7_0_4;
912 return CH_MODE_7_1_4;
914 RCHECK(reader->
ReadBits(1, &read_more));
915 channel_mode_code = (channel_mode_code << 1) | read_more;
916 switch (channel_mode_code) {
918 return CH_MODE_9_0_4;
920 return CH_MODE_9_1_4;
925 ReadAc4VariableBits(reader, 2);
926 return CH_MODE_RESERVED;
All the methods that are virtual are virtual for mocking.