Shaka Packager SDK
Loading...
Searching...
No Matches
ac4_parser.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/ac4_parser.h>
8
9#include <algorithm>
10
11#include <absl/log/check.h>
12#include <absl/log/log.h>
13
14#include <packager/macros/logging.h>
15#include <packager/media/base/bit_reader.h>
16#include <packager/media/base/rcheck.h>
17
18using namespace shaka::media;
19
20namespace shaka {
21namespace media {
22namespace {
23
24// ch_mode - TS 103 190-2 table 78
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;
42
43int ReadAc4VariableBits(BitReader* reader, int nBits) {
44 int value = 0;
45 int extra_value;
46 int b_moreBits;
47 do {
48 RCHECK(reader->ReadBits(nBits, &extra_value));
49 value += extra_value;
50 RCHECK(reader->ReadBits(1, &b_moreBits));
51 if (b_moreBits == 1) {
52 value <<= nBits;
53 value += (1 << nBits);
54 }
55 } while (b_moreBits == 1);
56 return value;
57}
58
59} // namespace
60
61AC4Parser::AC4Parser() = default;
62AC4Parser::~AC4Parser() = default;
63
64bool AC4Parser::Parse(const uint8_t* data, size_t data_size) {
65 BitReader reader(data, data_size);
66 if (!ParseAc4Toc(&reader))
67 return false;
68 return true;
69}
70
71bool AC4Parser::ParseAc4Toc(BitReader* reader) {
72 // Ac4Toc ac4_toc;
73 toc_size = reader->bit_position();
74 RCHECK(reader->ReadBits(2, &ac4_toc.bitstream_version));
75 if (ac4_toc.bitstream_version == 3) {
76 ac4_toc.bitstream_version = ReadAc4VariableBits(reader, 2);
77 }
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));
84 }
85 }
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;
92 } else {
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;
96 } else {
97 ac4_toc.n_presentations = 0;
98 }
99 }
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);
107 }
108 }
109 if (ac4_toc.bitstream_version <= 1) {
110 printf("Warning: Bitstream version 0 is deprecated\n");
111 } else {
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));
119 }
120 }
121 }
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],
127 max_group_index);
128 }
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);
134 }
135 }
136
137 // ts_10319001v010301p (ETSI TS 103190-1 v1.3.1)
138 int n_substreams = 0;
139 RCHECK(reader->ReadBits(2, &n_substreams));
140 if (n_substreams == 0) {
141 n_substreams = ReadAc4VariableBits(reader, 2) + 4;
142 }
143 int b_size_present = 0;
144 if (n_substreams == 1) {
145 RCHECK(reader->ReadBits(1, &b_size_present));
146 } else {
147 b_size_present = 1;
148 }
149 if (b_size_present) {
150 for (int s = 0; s < n_substreams; s++) {
151 int b_more_bits = 0;
152 RCHECK(reader->ReadBits(1, &b_more_bits));
153 int substream_size = 0;
154 RCHECK(reader->ReadBits(10, &substream_size));
155 if (b_more_bits) {
156 substream_size += (ReadAc4VariableBits(reader, 2) << 10);
157 }
158 }
159 }
160 toc_size = reader->bit_position() - toc_size;
161
162 return true;
163}
164
165bool AC4Parser::ParseAc4PresentationV1Info(
166 BitReader* reader,
167 Ac4PresentationV1Info& ac4_presentation_v1_info,
168 int& max_group_index) {
169 int group_index = 0;
170 RCHECK(
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);
177 }
178 }
179 if (ac4_toc.bitstream_version != 1) {
180 // presentation_version();
181 // int presentation_version = 0;
182 int more_bits;
183 while (reader->ReadBits(1, &more_bits) && more_bits) {
184 ac4_presentation_v1_info.presentation_version++;
185 }
186 }
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;
190 } else {
191 if (ac4_toc.bitstream_version != 1) {
192 RCHECK(reader->ReadBits(3, &ac4_presentation_v1_info.mdcompat));
193 }
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);
198 }
199 ParseFrameRateMultiplyInfo(reader);
200 ParseFrameRateFractionsInfo(reader);
201 ParseEmdfInfo(reader);
202
203 RCHECK(
204 reader->ReadBits(1, &ac4_presentation_v1_info.b_presentation_filter));
205
206 if (ac4_presentation_v1_info.b_presentation_filter) {
207 RCHECK(
208 reader->ReadBits(1, &ac4_presentation_v1_info.b_enable_presentation));
209 }
210 if (ac4_presentation_v1_info.b_single_substream_group == 1) {
211 group_index = ParseAc4SgiSpecifier(reader);
212 max_group_index =
213 group_index > max_group_index ? group_index : max_group_index;
214 ac4_presentation_v1_info.n_substream_groups = 1;
215 } else {
216 reader->ReadBits(1, &ac4_presentation_v1_info.b_multi_pid);
217 switch (ac4_presentation_v1_info.presentation_config) {
218 case 0:
219 group_index = ParseAc4SgiSpecifier(reader);
220 max_group_index =
221 group_index > max_group_index ? group_index : max_group_index;
222 group_index = ParseAc4SgiSpecifier(reader);
223 max_group_index =
224 group_index > max_group_index ? group_index : max_group_index;
225 ac4_presentation_v1_info.n_substream_groups = 2;
226 break;
227 case 1:
228 group_index = ParseAc4SgiSpecifier(reader);
229 max_group_index =
230 group_index > max_group_index ? group_index : max_group_index;
231 group_index = ParseAc4SgiSpecifier(reader);
232 max_group_index =
233 group_index > max_group_index ? group_index : max_group_index;
234 ac4_presentation_v1_info.n_substream_groups = 1;
235 break;
236 case 2:
237 group_index = ParseAc4SgiSpecifier(reader);
238 max_group_index =
239 group_index > max_group_index ? group_index : max_group_index;
240 group_index = ParseAc4SgiSpecifier(reader);
241 max_group_index =
242 group_index > max_group_index ? group_index : max_group_index;
243 ac4_presentation_v1_info.n_substream_groups = 2;
244 break;
245 case 3:
246 group_index = ParseAc4SgiSpecifier(reader);
247 max_group_index =
248 group_index > max_group_index ? group_index : max_group_index;
249 group_index = ParseAc4SgiSpecifier(reader);
250 max_group_index =
251 group_index > max_group_index ? group_index : max_group_index;
252 group_index = ParseAc4SgiSpecifier(reader);
253 max_group_index =
254 group_index > max_group_index ? group_index : max_group_index;
255 ac4_presentation_v1_info.n_substream_groups = 3;
256 break;
257 case 4:
258 group_index = ParseAc4SgiSpecifier(reader);
259 max_group_index =
260 group_index > max_group_index ? group_index : max_group_index;
261 group_index = ParseAc4SgiSpecifier(reader);
262 max_group_index =
263 group_index > max_group_index ? group_index : max_group_index;
264 group_index = ParseAc4SgiSpecifier(reader);
265 max_group_index =
266 group_index > max_group_index ? group_index : max_group_index;
267 ac4_presentation_v1_info.n_substream_groups = 2;
268 break;
269 case 5:
270 RCHECK(reader->ReadBits(
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);
277 }
278 for (int sg = 0; sg < ac4_presentation_v1_info.n_substream_groups;
279 sg++) {
280 group_index = ParseAc4SgiSpecifier(reader);
281 max_group_index =
282 group_index > max_group_index ? group_index : max_group_index;
283 }
284 break;
285 default:
286 ParsePresentationConfigExtInfo(reader);
287 break;
288 }
289 }
290 RCHECK(reader->ReadBits(1, &ac4_presentation_v1_info.b_pre_virtualized));
291 RCHECK(
292 reader->ReadBits(1, &ac4_presentation_v1_info.b_add_emdf_substreams));
293 ParseAc4PresentationSubstreamInfo(reader);
294 }
295 if (ac4_presentation_v1_info.b_add_emdf_substreams) {
296 RCHECK(
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;
301 }
302 for (int i = 0; i < ac4_presentation_v1_info.n_add_emdf_substreams; i++) {
303 ParseEmdfInfo(reader);
304 }
305 }
306 return true;
307}
308
309bool AC4Parser::ParseFrameRateMultiplyInfo(BitReader* reader) {
310 FrameRateMultiplyInfo frame_rate_multiply_info;
311 switch (ac4_toc.frame_rate_index) {
312 case 2:
313 case 3:
314 case 4:
315 RCHECK(reader->ReadBits(1, &frame_rate_multiply_info.b_multiplier));
316 if (frame_rate_multiply_info.b_multiplier) { // b_multiplier
317 RCHECK(reader->ReadBits(
318 1, &frame_rate_multiply_info.multiplier_bit)); // multiplier_bit
319 }
320 break;
321 case 0:
322 case 1:
323 case 7:
324 case 8:
325 case 9:
326 RCHECK(reader->ReadBits(1, &frame_rate_multiply_info.b_multiplier));
327 break;
328 default:
329 break;
330 }
331 return true;
332}
333
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) {
337 RCHECK(
338 reader->ReadBits(1, &frame_rate_fractions_info.b_frame_rate_fraction));
339 }
340 if (ac4_toc.frame_rate_index >= 10 && ac4_toc.frame_rate_index <= 12) {
341 RCHECK(
342 reader->ReadBits(1, &frame_rate_fractions_info.b_frame_rate_fraction));
343 if (frame_rate_fractions_info.b_frame_rate_fraction == 1) {
344 RCHECK(reader->ReadBits(
345 1, &frame_rate_fractions_info.b_frame_rate_fraction_is_4));
346 }
347 }
348 return true;
349}
350
351bool AC4Parser::ParseEmdfInfo(BitReader* reader) {
352 EmdfInfo emdf_info;
353 RCHECK(reader->ReadBits(2, &emdf_info.emdf_version));
354 if (emdf_info.emdf_version == 3) {
355 emdf_info.emdf_version += ReadAc4VariableBits(reader, 2);
356 }
357 RCHECK(reader->ReadBits(3, &emdf_info.key_id));
358 if (emdf_info.key_id == 7) {
359 emdf_info.key_id += ReadAc4VariableBits(reader, 3);
360 }
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);
366 }
367 }
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) {
371 case 1:
372 RCHECK(reader->ReadBits(8, &emdf_info.protection_bits_primary[0]));
373 break;
374 case 2:
375 for (unsigned idx = 0; idx < 4; idx++) {
376 RCHECK(reader->ReadBits(8, &emdf_info.protection_bits_primary[idx]));
377 }
378 break;
379 case 3:
380 for (unsigned idx = 0; idx < 16; idx++) {
381 RCHECK(reader->ReadBits(8, &emdf_info.protection_bits_primary[idx]));
382 }
383 break;
384 default:
385 LOG(ERROR) << "Invalid EMDF primary protection length: "
386 << static_cast<int>(emdf_info.protection_length_primary);
387 return false;
388 break;
389 }
390 // protection_bits_secondary
391 switch (emdf_info.protection_length_secondary) {
392 case 0:
393 break;
394 case 1:
395 RCHECK(reader->ReadBits(8, &emdf_info.protection_bits_secondary[0]));
396 break;
397 case 2:
398 for (unsigned idx = 0; idx < 4; idx++) {
399 RCHECK(reader->ReadBits(8, &emdf_info.protection_bits_secondary[idx]));
400 }
401 break;
402 case 3:
403 for (unsigned idx = 0; idx < 16; idx++) {
404 RCHECK(reader->ReadBits(8, &emdf_info.protection_bits_secondary[idx]));
405 }
406 break;
407 default:
408 LOG(ERROR) << "Invalid EMDF secondary protection length: "
409 << static_cast<int>(emdf_info.protection_length_secondary);
410 return false;
411 }
412 return true;
413}
414
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);
422 }
423 return true;
424}
425
426int AC4Parser::ParseAc4SgiSpecifier(BitReader* reader) {
427 int group_index = 0;
428 if (ac4_toc.bitstream_version == 1) {
429 // ac4_substream_group_info();
430 } else {
431 RCHECK(reader->ReadBits(3, &group_index));
432 if (group_index == 7) {
433 group_index += ReadAc4VariableBits(reader, 2);
434 }
435 }
436 return group_index;
437}
438
439bool AC4Parser::ParsePresentationConfigExtInfo(BitReader* reader) {
440 int n_skip_bytes;
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;
446 }
447 for (int i = 0; i < n_skip_bytes; i++) {
448 reader->SkipBytes(8);
449 }
450 return true;
451}
452
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;
456 sg++) {
457 if (substream_group_index ==
458 ac4_toc.presentation_v1_infos[idx].group_index[sg]) {
459 return idx;
460 }
461 }
462 }
463 return 0;
464}
465
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;
469 sg++) {
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;
473 }
474 }
475 }
476 return 0;
477}
478
479bool AC4Parser::ParseAc4SubstreamGroupInfo(
480 BitReader* reader,
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;
488 } else {
489 RCHECK(
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);
496 }
497 }
498 RCHECK(reader->ReadBits(1, &ac4_substream_group_info.b_channel_coded));
499 // find the presentation of current substream group
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)
504 ? 1
505 : (ac4_toc.presentation_v1_infos[pre_idx]
506 .frame_rate_multiply_info.dsi_frame_rate_multiply_info *
507 2);
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) {
511 // RCHECK(reader->ReadBits(1, &ac4_substream_group_info.sus_ver)));
512 } else {
513 ac4_substream_group_info.sus_ver = 1;
514 }
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);
521 }
522 }
523 } else {
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);
527 }
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);
536 }
537 } else {
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);
542 }
543 }
544 }
545 }
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);
549 }
550 return true;
551}
552
553bool AC4Parser::ParseContentType(
554 BitReader* reader,
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) {
559 RCHECK(reader->ReadBits(
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));
563 RCHECK(
564 reader->ReadBits(16, &ac4_substream_group_info.language_tag_chunk));
565 } else {
566 RCHECK(
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++) {
569 RCHECK(reader->ReadBits(
570 8, &ac4_substream_group_info.language_tag_bytes[i]));
571 }
572 }
573 }
574 return true;
575}
576
577bool AC4Parser::ParseOamdSubstreamInfo(
578 BitReader* reader,
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);
586 }
587 }
588 return true;
589}
590
591bool AC4Parser::ParseAc4HsfExtSubstreamInfo(
592 BitReader* reader,
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);
599 }
600 }
601 return true;
602}
603
604bool AC4Parser::ParseAc4SubstreamInfoChan(BitReader* reader,
605 int presentation_version,
606 int fs_index,
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);
614 }
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)) {
617 RCHECK(reader->ReadBits(
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));
621 }
622 if (fs_index == 1) {
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));
626 }
627 }
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) {
632 int more_bits = 0;
633 RCHECK(reader->ReadBits(2, &more_bits));
634 ac4_substream_info_chan.bitrate_indicator =
635 (ac4_substream_info_chan.bitrate_indicator << 2) + more_bits;
636 }
637 }
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));
641 }
642 for (int i = 0; i < frame_rate_factor; i++) {
643 RCHECK(reader->ReadBits(1, &ac4_substream_info_chan.b_audio_ndot));
644 }
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);
649 }
650 }
651 return true;
652}
653
654bool AC4Parser::ParseAc4SubstreamInfoAjoc(BitReader* reader,
655 int fs_index,
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;
663 } else {
664 RCHECK(reader->ReadBits(
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);
671 }
672 RCHECK(
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);
676 }
677 RCHECK(reader->ReadBits(
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);
682 }
683 ParseBedDynObjAssignment(
684 reader, ac4_substream_info_ajoc.n_fullband_upmix_signals_minus1 + 1,
685 ac4_substream_info_ajoc);
686 if (fs_index == 1) {
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));
690 }
691 }
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) {
696 int more_bits = 0;
697 RCHECK(reader->ReadBits(2, &more_bits));
698 ac4_substream_info_ajoc.bitrate_indicator =
699 (ac4_substream_info_ajoc.bitrate_indicator << 2) + more_bits;
700 }
701 }
702 for (int i = 0; i < frame_rate_factor; i++) {
703 RCHECK(reader->ReadBits(1, &ac4_substream_info_ajoc.b_audio_ndot));
704 }
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);
709 }
710 }
711 // sus_ver = 1;
712 return true;
713}
714
715bool AC4Parser::ParseBedDynObjAssignment(
716 BitReader* reader,
717 int n_signals,
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));
724 } else {
725 RCHECK(reader->ReadBits(1, &ac4_substream_info_ajoc.b_ch_assign_code));
726 if (ac4_substream_info_ajoc.b_ch_assign_code) {
727 RCHECK(
728 reader->ReadBits(3, &ac4_substream_info_ajoc.bed_chan_assign_code));
729 } else {
730 RCHECK(
731 reader->ReadBits(1, &ac4_substream_info_ajoc.b_chan_assign_mask));
732 if (ac4_substream_info_ajoc.b_chan_assign_mask) {
733 RCHECK(reader->ReadBits(
734 1, &ac4_substream_info_ajoc.b_nonstd_bed_channel_assignment));
735 if (ac4_substream_info_ajoc.b_nonstd_bed_channel_assignment) {
736 RCHECK(reader->ReadBits(
737 17,
738 &ac4_substream_info_ajoc.nonstd_bed_channel_assignment_mask));
739 } else {
740 RCHECK(reader->ReadBits(
741 10, &ac4_substream_info_ajoc.std_bed_channel_assignment_mask));
742 }
743 } else {
744 if (n_signals > 1) {
745 int bed_ch_bits = (int)ceil(log((float)n_signals) / log((float)2));
746 RCHECK(reader->ReadBits(
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;
750 } else {
751 ac4_substream_info_ajoc.n_bed_signals = 1;
752 }
753 for (int b = 0; b < ac4_substream_info_ajoc.n_bed_signals; b++) {
754 RCHECK(reader->ReadBits(
755 4, &ac4_substream_info_ajoc.nonstd_bed_channel_assignment));
756 }
757 }
758 }
759 }
760 }
761 return true;
762}
763
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) {
768 RCHECK(
769 reader->ReadBits(5, &oamd_common_data.master_screen_size_ratio_code));
770 }
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);
779 }
780 reader->SkipBytes(oamd_common_data.add_data_bytes);
781 }
782 return true;
783}
784
785bool AC4Parser::ParseAc4SubstreamInfoObj(BitReader* reader,
786 int fs_index,
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));
794 } else {
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) {
801 RCHECK(reader->ReadBits(
802 3, &ac4_substream_info_obj.bed_chan_assign_code));
803 } else {
804 RCHECK(reader->ReadBits(
805 1, &ac4_substream_info_obj.b_nonstd_bed_channel_assignment));
806 if (ac4_substream_info_obj.b_nonstd_bed_channel_assignment) {
807 RCHECK(reader->ReadBits(
808 17,
809 &ac4_substream_info_obj.nonstd_bed_channel_assignment_mask));
810 } else {
811 RCHECK(reader->ReadBits(
812 10, &ac4_substream_info_obj.std_bed_channel_assignment_mask));
813 }
814 }
815 }
816 } else {
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));
822 }
823 } else {
824 int res_bytes = 0;
825 RCHECK(reader->ReadBits(4, &res_bytes));
826 reader->SkipBits(res_bytes * 8);
827 }
828 }
829 }
830 if (fs_index == 1) {
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));
834 }
835 }
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) {
840 int more_bits = 0;
841 RCHECK(reader->ReadBits(2, &more_bits));
842 ac4_substream_info_obj.bitrate_indicator =
843 (ac4_substream_info_obj.bitrate_indicator << 2) + more_bits;
844 }
845 }
846 for (int i = 0; i < frame_rate_factor; i++) {
847 RCHECK(reader->ReadBits(1, &ac4_substream_info_obj.b_audio_ndot));
848 }
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);
853 }
854 }
855 // sus_ver = 1;
856 return true;
857}
858
859int AC4Parser::ParseChannelMode(BitReader* reader, int presentation_version) {
860 int channel_mode_code = 0;
861 int read_more = 0;
862 RCHECK(reader->ReadBits(1, &channel_mode_code));
863 if (channel_mode_code == 0) {
864 return CH_MODE_MONO;
865 }
866 RCHECK(reader->ReadBits(1, &read_more));
867 channel_mode_code = (channel_mode_code << 1) | read_more;
868 if (channel_mode_code == 2) { // Stereo 0b10
869 return CH_MODE_STEREO;
870 }
871 RCHECK(reader->ReadBits(2, &read_more));
872 channel_mode_code = (channel_mode_code << 2) | read_more;
873 switch (channel_mode_code) {
874 case 12: // 3.0 0b1100
875 return CH_MODE_3_0;
876 case 13: // 5.0 0b1101
877 return CH_MODE_5_0;
878 case 14: // 5.1 0b1110
879 return CH_MODE_5_1;
880 }
881 RCHECK(reader->ReadBits(3, &read_more));
882 channel_mode_code = (channel_mode_code << 3) | read_more;
883 switch (channel_mode_code) {
884 case 120: // 0b1111000
885 if (presentation_version == 2) { // IMS (all content)
886 return CH_MODE_STEREO;
887 } else { // 7.0: 3/4/0
888 return CH_MODE_70_34;
889 }
890 case 121: // 0b1111001
891 if (presentation_version == 2) { // IMS (Atmos content)
892 // dolby_atmos_indicator |= 1;
893 return CH_MODE_STEREO;
894 } else { // 7.1: 3/4/0.1
895 return CH_MODE_71_34;
896 }
897 case 122: // 7.0: 5/2/0 0b1111010
898 return CH_MODE_70_52;
899 case 123: // 7.1: 5/2/0.1 0b1111011
900 return CH_MODE_71_52;
901 case 124: // 7.0: 3/2/2 0b1111100
902 return CH_MODE_70_322;
903 case 125: // 7.1: 3/2/2.1 0b1111101
904 return CH_MODE_71_322;
905 }
906 RCHECK(reader->ReadBits(1, &read_more));
907 channel_mode_code = (channel_mode_code << 1) | read_more;
908 switch (channel_mode_code) {
909 case 252: // 7.0.4 0b11111100
910 return CH_MODE_7_0_4;
911 case 253: // 7.1.4 0b11111101
912 return CH_MODE_7_1_4;
913 }
914 RCHECK(reader->ReadBits(1, &read_more));
915 channel_mode_code = (channel_mode_code << 1) | read_more;
916 switch (channel_mode_code) {
917 case 508: // 9.0.4 0b111111100
918 return CH_MODE_9_0_4;
919 case 509: // 9.1.4 0b111111101
920 return CH_MODE_9_1_4;
921 case 510: // 22.2 0b111111110
922 return CH_MODE_22_2;
923 case 511: // Reserved, escape value 0b111111111
924 default:
925 ReadAc4VariableBits(reader, 2);
926 return CH_MODE_RESERVED;
927 }
928}
929
930} // namespace media
931} // namespace shaka
A class to read bit streams.
Definition bit_reader.h:20
size_t bit_position() const
Definition bit_reader.h:97
bool SkipBits(size_t num_bits)
Definition bit_reader.cc:26
bool SkipBytes(size_t num_bytes)
Definition bit_reader.cc:65
bool ReadBits(size_t num_bits, T *out)
Definition bit_reader.h:38
All the methods that are virtual are virtual for mocking.