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 <cmath>
10#include <cstddef>
11#include <cstdint>
12#include <cstdio>
13
14#include <absl/log/check.h>
15#include <absl/log/log.h>
16
17#include <packager/media/base/bit_reader.h>
18#include <packager/media/base/rcheck.h>
19
20using namespace shaka::media;
21
22namespace shaka {
23namespace media {
24namespace {
25
26// ch_mode - TS 103 190-2 table 78
27const int CH_MODE_MONO = 0;
28const int CH_MODE_STEREO = 1;
29const int CH_MODE_3_0 = 2;
30const int CH_MODE_5_0 = 3;
31const int CH_MODE_5_1 = 4;
32const int CH_MODE_70_34 = 5;
33const int CH_MODE_71_34 = 6;
34const int CH_MODE_70_52 = 7;
35const int CH_MODE_71_52 = 8;
36const int CH_MODE_70_322 = 9;
37const int CH_MODE_71_322 = 10;
38const int CH_MODE_7_0_4 = 11;
39const int CH_MODE_7_1_4 = 12;
40const int CH_MODE_9_0_4 = 13;
41const int CH_MODE_9_1_4 = 14;
42const int CH_MODE_22_2 = 15;
43const int CH_MODE_RESERVED = 16;
44
45int ReadAc4VariableBits(BitReader* reader, int nBits) {
46 int value = 0;
47 int extra_value;
48 int b_moreBits;
49 do {
50 RCHECK(reader->ReadBits(nBits, &extra_value));
51 value += extra_value;
52 RCHECK(reader->ReadBits(1, &b_moreBits));
53 if (b_moreBits == 1) {
54 value <<= nBits;
55 value += (1 << nBits);
56 }
57 } while (b_moreBits == 1);
58 return value;
59}
60
61} // namespace
62
63AC4Parser::AC4Parser() = default;
64AC4Parser::~AC4Parser() = default;
65
66bool AC4Parser::Parse(const uint8_t* data, size_t data_size) {
67 BitReader reader(data, data_size);
68 if (!ParseAc4Toc(&reader))
69 return false;
70 return true;
71}
72
73bool AC4Parser::ParseAc4Toc(BitReader* reader) {
74 // Ac4Toc ac4_toc;
75 toc_size = reader->bit_position();
76 RCHECK(reader->ReadBits(2, &ac4_toc.bitstream_version));
77 if (ac4_toc.bitstream_version == 3) {
78 ac4_toc.bitstream_version = ReadAc4VariableBits(reader, 2);
79 }
80 RCHECK(reader->ReadBits(10, &ac4_toc.sequence_counter));
81 RCHECK(reader->ReadBits(1, &ac4_toc.b_wait_frames));
82 if (ac4_toc.b_wait_frames) {
83 RCHECK(reader->ReadBits(3, &ac4_toc.wait_frames));
84 if (ac4_toc.wait_frames > 0) {
85 RCHECK(reader->ReadBits(2, &ac4_toc.br_code));
86 }
87 }
88 RCHECK(reader->ReadBits(1, &ac4_toc.fs_index));
89 RCHECK(reader->ReadBits(4, &ac4_toc.frame_rate_index));
90 RCHECK(reader->ReadBits(1, &ac4_toc.b_iframe_global));
91 RCHECK(reader->ReadBits(1, &ac4_toc.b_single_presentation));
92 if (ac4_toc.b_single_presentation) {
93 ac4_toc.n_presentations = 1;
94 } else {
95 RCHECK(reader->ReadBits(1, &ac4_toc.b_more_presentations));
96 if (ac4_toc.b_more_presentations) {
97 ac4_toc.n_presentations = ReadAc4VariableBits(reader, 2) + 2;
98 } else {
99 ac4_toc.n_presentations = 0;
100 }
101 }
102 int payload_base = 0;
103 RCHECK(reader->ReadBits(1, &ac4_toc.b_payload_base));
104 if (ac4_toc.b_payload_base) {
105 RCHECK(reader->ReadBits(5, &ac4_toc.payload_base_minus1));
106 payload_base = ac4_toc.payload_base_minus1 + 1;
107 if (payload_base == 0x20) {
108 payload_base = ReadAc4VariableBits(reader, 3);
109 }
110 }
111 if (ac4_toc.bitstream_version <= 1) {
112 printf("Warning: Bitstream version 0 is deprecated\n");
113 } else {
114 RCHECK(reader->ReadBits(1, &ac4_toc.b_program_id));
115 if (ac4_toc.b_program_id) {
116 RCHECK(reader->ReadBits(16, &ac4_toc.short_program_id));
117 RCHECK(reader->ReadBits(1, &ac4_toc.b_program_uuid_present));
118 if (ac4_toc.b_program_uuid_present) {
119 for (int cnt = 0; cnt < 16; cnt++) {
120 RCHECK(reader->ReadBits(8, &ac4_toc.program_uuid));
121 }
122 }
123 }
124 int max_group_index = 0;
125 ac4_toc.presentation_v1_infos =
126 new Ac4PresentationV1Info[ac4_toc.n_presentations];
127 for (int i = 0; i < ac4_toc.n_presentations; i++) {
128 ParseAc4PresentationV1Info(reader, ac4_toc.presentation_v1_infos[i],
129 max_group_index);
130 }
131 ac4_toc.total_n_substream_groups = max_group_index + 1;
132 ac4_toc.substream_group_infos =
133 new Ac4SubstreamGroupInfo[ac4_toc.total_n_substream_groups];
134 for (int j = 0; j < ac4_toc.total_n_substream_groups; j++) {
135 ParseAc4SubstreamGroupInfo(reader, ac4_toc.substream_group_infos[j], j);
136 }
137 }
138
139 // ts_10319001v010301p (ETSI TS 103190-1 v1.3.1)
140 int n_substreams = 0;
141 RCHECK(reader->ReadBits(2, &n_substreams));
142 if (n_substreams == 0) {
143 n_substreams = ReadAc4VariableBits(reader, 2) + 4;
144 }
145 int b_size_present = 0;
146 if (n_substreams == 1) {
147 RCHECK(reader->ReadBits(1, &b_size_present));
148 } else {
149 b_size_present = 1;
150 }
151 if (b_size_present) {
152 for (int s = 0; s < n_substreams; s++) {
153 int b_more_bits = 0;
154 RCHECK(reader->ReadBits(1, &b_more_bits));
155 int substream_size = 0;
156 RCHECK(reader->ReadBits(10, &substream_size));
157 if (b_more_bits) {
158 substream_size += (ReadAc4VariableBits(reader, 2) << 10);
159 }
160 }
161 }
162 toc_size = reader->bit_position() - toc_size;
163
164 return true;
165}
166
167bool AC4Parser::ParseAc4PresentationV1Info(
168 BitReader* reader,
169 Ac4PresentationV1Info& ac4_presentation_v1_info,
170 int& max_group_index) {
171 int group_index = 0;
172 RCHECK(
173 reader->ReadBits(1, &ac4_presentation_v1_info.b_single_substream_group));
174 if (ac4_presentation_v1_info.b_single_substream_group != 1) {
175 RCHECK(reader->ReadBits(3, &ac4_presentation_v1_info.presentation_config));
176 if (ac4_presentation_v1_info.presentation_config == 7) {
177 ac4_presentation_v1_info.presentation_config =
178 ReadAc4VariableBits(reader, 2);
179 }
180 }
181 if (ac4_toc.bitstream_version != 1) {
182 // presentation_version();
183 // int presentation_version = 0;
184 int more_bits;
185 while (reader->ReadBits(1, &more_bits) && more_bits) {
186 ac4_presentation_v1_info.presentation_version++;
187 }
188 }
189 if (ac4_presentation_v1_info.b_single_substream_group != 1 &&
190 ac4_presentation_v1_info.presentation_config == 6) {
191 ac4_presentation_v1_info.b_add_emdf_substreams = 1;
192 } else {
193 if (ac4_toc.bitstream_version != 1) {
194 RCHECK(reader->ReadBits(3, &ac4_presentation_v1_info.mdcompat));
195 }
196 RCHECK(reader->ReadBits(1, &ac4_presentation_v1_info.b_presentation_id));
197 if (ac4_presentation_v1_info.b_presentation_id) {
198 ac4_presentation_v1_info.b_presentation_id =
199 ReadAc4VariableBits(reader, 2);
200 }
201 ParseFrameRateMultiplyInfo(reader);
202 ParseFrameRateFractionsInfo(reader);
203 ParseEmdfInfo(reader);
204
205 RCHECK(
206 reader->ReadBits(1, &ac4_presentation_v1_info.b_presentation_filter));
207
208 if (ac4_presentation_v1_info.b_presentation_filter) {
209 RCHECK(
210 reader->ReadBits(1, &ac4_presentation_v1_info.b_enable_presentation));
211 }
212 if (ac4_presentation_v1_info.b_single_substream_group == 1) {
213 group_index = ParseAc4SgiSpecifier(reader);
214 max_group_index =
215 group_index > max_group_index ? group_index : max_group_index;
216 ac4_presentation_v1_info.n_substream_groups = 1;
217 } else {
218 reader->ReadBits(1, &ac4_presentation_v1_info.b_multi_pid);
219 switch (ac4_presentation_v1_info.presentation_config) {
220 case 0:
221 group_index = ParseAc4SgiSpecifier(reader);
222 max_group_index =
223 group_index > max_group_index ? group_index : max_group_index;
224 group_index = ParseAc4SgiSpecifier(reader);
225 max_group_index =
226 group_index > max_group_index ? group_index : max_group_index;
227 ac4_presentation_v1_info.n_substream_groups = 2;
228 break;
229 case 1:
230 group_index = ParseAc4SgiSpecifier(reader);
231 max_group_index =
232 group_index > max_group_index ? group_index : max_group_index;
233 group_index = ParseAc4SgiSpecifier(reader);
234 max_group_index =
235 group_index > max_group_index ? group_index : max_group_index;
236 ac4_presentation_v1_info.n_substream_groups = 1;
237 break;
238 case 2:
239 group_index = ParseAc4SgiSpecifier(reader);
240 max_group_index =
241 group_index > max_group_index ? group_index : max_group_index;
242 group_index = ParseAc4SgiSpecifier(reader);
243 max_group_index =
244 group_index > max_group_index ? group_index : max_group_index;
245 ac4_presentation_v1_info.n_substream_groups = 2;
246 break;
247 case 3:
248 group_index = ParseAc4SgiSpecifier(reader);
249 max_group_index =
250 group_index > max_group_index ? group_index : max_group_index;
251 group_index = ParseAc4SgiSpecifier(reader);
252 max_group_index =
253 group_index > max_group_index ? group_index : max_group_index;
254 group_index = ParseAc4SgiSpecifier(reader);
255 max_group_index =
256 group_index > max_group_index ? group_index : max_group_index;
257 ac4_presentation_v1_info.n_substream_groups = 3;
258 break;
259 case 4:
260 group_index = ParseAc4SgiSpecifier(reader);
261 max_group_index =
262 group_index > max_group_index ? group_index : max_group_index;
263 group_index = ParseAc4SgiSpecifier(reader);
264 max_group_index =
265 group_index > max_group_index ? group_index : max_group_index;
266 group_index = ParseAc4SgiSpecifier(reader);
267 max_group_index =
268 group_index > max_group_index ? group_index : max_group_index;
269 ac4_presentation_v1_info.n_substream_groups = 2;
270 break;
271 case 5:
272 RCHECK(reader->ReadBits(
273 2, &ac4_presentation_v1_info.n_substream_groups_minus2));
274 ac4_presentation_v1_info.n_substream_groups =
275 ac4_presentation_v1_info.n_substream_groups_minus2 + 2;
276 if (ac4_presentation_v1_info.n_substream_groups == 5) {
277 ac4_presentation_v1_info.n_substream_groups +=
278 ReadAc4VariableBits(reader, 2);
279 }
280 for (int sg = 0; sg < ac4_presentation_v1_info.n_substream_groups;
281 sg++) {
282 group_index = ParseAc4SgiSpecifier(reader);
283 max_group_index =
284 group_index > max_group_index ? group_index : max_group_index;
285 }
286 break;
287 default:
288 ParsePresentationConfigExtInfo(reader);
289 break;
290 }
291 }
292 RCHECK(reader->ReadBits(1, &ac4_presentation_v1_info.b_pre_virtualized));
293 RCHECK(
294 reader->ReadBits(1, &ac4_presentation_v1_info.b_add_emdf_substreams));
295 ParseAc4PresentationSubstreamInfo(reader);
296 }
297 if (ac4_presentation_v1_info.b_add_emdf_substreams) {
298 RCHECK(
299 reader->ReadBits(2, &ac4_presentation_v1_info.n_add_emdf_substreams));
300 if (ac4_presentation_v1_info.n_add_emdf_substreams == 0) {
301 ac4_presentation_v1_info.n_add_emdf_substreams =
302 ReadAc4VariableBits(reader, 2) + 4;
303 }
304 for (int i = 0; i < ac4_presentation_v1_info.n_add_emdf_substreams; i++) {
305 ParseEmdfInfo(reader);
306 }
307 }
308 return true;
309}
310
311bool AC4Parser::ParseFrameRateMultiplyInfo(BitReader* reader) {
312 FrameRateMultiplyInfo frame_rate_multiply_info;
313 switch (ac4_toc.frame_rate_index) {
314 case 2:
315 case 3:
316 case 4:
317 RCHECK(reader->ReadBits(1, &frame_rate_multiply_info.b_multiplier));
318 if (frame_rate_multiply_info.b_multiplier) { // b_multiplier
319 RCHECK(reader->ReadBits(
320 1, &frame_rate_multiply_info.multiplier_bit)); // multiplier_bit
321 }
322 break;
323 case 0:
324 case 1:
325 case 7:
326 case 8:
327 case 9:
328 RCHECK(reader->ReadBits(1, &frame_rate_multiply_info.b_multiplier));
329 break;
330 default:
331 break;
332 }
333 return true;
334}
335
336bool AC4Parser::ParseFrameRateFractionsInfo(BitReader* reader) {
337 FrameRateFractionsInfo frame_rate_fractions_info;
338 if (ac4_toc.frame_rate_index >= 5 && ac4_toc.frame_rate_index <= 9) {
339 RCHECK(
340 reader->ReadBits(1, &frame_rate_fractions_info.b_frame_rate_fraction));
341 }
342 if (ac4_toc.frame_rate_index >= 10 && ac4_toc.frame_rate_index <= 12) {
343 RCHECK(
344 reader->ReadBits(1, &frame_rate_fractions_info.b_frame_rate_fraction));
345 if (frame_rate_fractions_info.b_frame_rate_fraction == 1) {
346 RCHECK(reader->ReadBits(
347 1, &frame_rate_fractions_info.b_frame_rate_fraction_is_4));
348 }
349 }
350 return true;
351}
352
353bool AC4Parser::ParseEmdfInfo(BitReader* reader) {
354 EmdfInfo emdf_info;
355 RCHECK(reader->ReadBits(2, &emdf_info.emdf_version));
356 if (emdf_info.emdf_version == 3) {
357 emdf_info.emdf_version += ReadAc4VariableBits(reader, 2);
358 }
359 RCHECK(reader->ReadBits(3, &emdf_info.key_id));
360 if (emdf_info.key_id == 7) {
361 emdf_info.key_id += ReadAc4VariableBits(reader, 3);
362 }
363 RCHECK(reader->ReadBits(1, &emdf_info.b_emdf_payloads_substream_info));
364 if (emdf_info.b_emdf_payloads_substream_info) {
365 RCHECK(reader->ReadBits(2, &emdf_info.substream_index));
366 if (emdf_info.substream_index == 3) {
367 emdf_info.substream_index += ReadAc4VariableBits(reader, 2);
368 }
369 }
370 RCHECK(reader->ReadBits(2, &emdf_info.protection_length_primary));
371 RCHECK(reader->ReadBits(2, &emdf_info.protection_length_secondary));
372 switch (emdf_info.protection_length_primary) {
373 case 1:
374 RCHECK(reader->ReadBits(8, &emdf_info.protection_bits_primary[0]));
375 break;
376 case 2:
377 for (unsigned idx = 0; idx < 4; idx++) {
378 RCHECK(reader->ReadBits(8, &emdf_info.protection_bits_primary[idx]));
379 }
380 break;
381 case 3:
382 for (unsigned idx = 0; idx < 16; idx++) {
383 RCHECK(reader->ReadBits(8, &emdf_info.protection_bits_primary[idx]));
384 }
385 break;
386 default:
387 LOG(ERROR) << "Invalid EMDF primary protection length: "
388 << static_cast<int>(emdf_info.protection_length_primary);
389 return false;
390 break;
391 }
392 // protection_bits_secondary
393 switch (emdf_info.protection_length_secondary) {
394 case 0:
395 break;
396 case 1:
397 RCHECK(reader->ReadBits(8, &emdf_info.protection_bits_secondary[0]));
398 break;
399 case 2:
400 for (unsigned idx = 0; idx < 4; idx++) {
401 RCHECK(reader->ReadBits(8, &emdf_info.protection_bits_secondary[idx]));
402 }
403 break;
404 case 3:
405 for (unsigned idx = 0; idx < 16; idx++) {
406 RCHECK(reader->ReadBits(8, &emdf_info.protection_bits_secondary[idx]));
407 }
408 break;
409 default:
410 LOG(ERROR) << "Invalid EMDF secondary protection length: "
411 << static_cast<int>(emdf_info.protection_length_secondary);
412 return false;
413 }
414 return true;
415}
416
417bool AC4Parser::ParseAc4PresentationSubstreamInfo(BitReader* reader) {
418 Ac4PresentationV1Info ac4_presentation_v1_info;
419 RCHECK(reader->ReadBits(1, &ac4_presentation_v1_info.b_alternative));
420 RCHECK(reader->ReadBits(1, &ac4_presentation_v1_info.b_pres_ndot));
421 RCHECK(reader->ReadBits(2, &ac4_presentation_v1_info.substream_index));
422 if (ac4_presentation_v1_info.substream_index == 3) {
423 ac4_presentation_v1_info.substream_index += ReadAc4VariableBits(reader, 2);
424 }
425 return true;
426}
427
428int AC4Parser::ParseAc4SgiSpecifier(BitReader* reader) {
429 int group_index = 0;
430 if (ac4_toc.bitstream_version == 1) {
431 // ac4_substream_group_info();
432 } else {
433 RCHECK(reader->ReadBits(3, &group_index));
434 if (group_index == 7) {
435 group_index += ReadAc4VariableBits(reader, 2);
436 }
437 }
438 return group_index;
439}
440
441bool AC4Parser::ParsePresentationConfigExtInfo(BitReader* reader) {
442 int n_skip_bytes;
443 int b_more_skip_bytes;
444 RCHECK(reader->ReadBits(1, &n_skip_bytes));
445 RCHECK(reader->ReadBits(1, &b_more_skip_bytes));
446 if (b_more_skip_bytes) {
447 n_skip_bytes += ReadAc4VariableBits(reader, 2) << 5;
448 }
449 for (int i = 0; i < n_skip_bytes; i++) {
450 reader->SkipBytes(8);
451 }
452 return true;
453}
454
455int AC4Parser::GetPresentationIdx(int substream_group_index) {
456 for (int idx = 0; idx < ac4_toc.n_presentations; idx++) {
457 for (int sg = 0; sg < ac4_toc.presentation_v1_infos[idx].n_substream_groups;
458 sg++) {
459 if (substream_group_index ==
460 ac4_toc.presentation_v1_infos[idx].group_index[sg]) {
461 return idx;
462 }
463 }
464 }
465 return 0;
466}
467
468int AC4Parser::GetPresentationVersion(int substream_group_index) {
469 for (int idx = 0; idx < ac4_toc.n_presentations; idx++) {
470 for (int sg = 0; sg < ac4_toc.presentation_v1_infos[idx].n_substream_groups;
471 sg++) {
472 if (substream_group_index ==
473 ac4_toc.presentation_v1_infos[idx].group_index[sg]) {
474 return ac4_toc.presentation_v1_infos[idx].presentation_version;
475 }
476 }
477 }
478 return 0;
479}
480
481bool AC4Parser::ParseAc4SubstreamGroupInfo(
482 BitReader* reader,
483 Ac4SubstreamGroupInfo& ac4_substream_group_info,
484 int substream_group_index) {
485 RCHECK(reader->ReadBits(1, &ac4_substream_group_info.b_substreams_present));
486 RCHECK(reader->ReadBits(1, &ac4_substream_group_info.b_hsf_ext));
487 RCHECK(reader->ReadBits(1, &ac4_substream_group_info.b_single_substream));
488 if (ac4_substream_group_info.b_single_substream) {
489 ac4_substream_group_info.n_lf_substreams = 1;
490 } else {
491 RCHECK(
492 reader->ReadBits(2, &ac4_substream_group_info.n_lf_substreams_minus2));
493 ac4_substream_group_info.n_lf_substreams =
494 ac4_substream_group_info.n_lf_substreams_minus2 + 2;
495 if (ac4_substream_group_info.n_lf_substreams == 5) {
496 ac4_substream_group_info.n_lf_substreams +=
497 ReadAc4VariableBits(reader, 2);
498 }
499 }
500 RCHECK(reader->ReadBits(1, &ac4_substream_group_info.b_channel_coded));
501 // find the presentation of current substream group
502 int pre_idx = GetPresentationIdx(substream_group_index);
503 int frame_rate_factor =
504 (ac4_toc.presentation_v1_infos[pre_idx]
505 .frame_rate_multiply_info.dsi_frame_rate_multiply_info == 0)
506 ? 1
507 : (ac4_toc.presentation_v1_infos[pre_idx]
508 .frame_rate_multiply_info.dsi_frame_rate_multiply_info *
509 2);
510 if (ac4_substream_group_info.b_channel_coded) {
511 for (int sus = 0; sus < ac4_substream_group_info.n_lf_substreams; sus++) {
512 if (ac4_toc.bitstream_version == 1) {
513 // RCHECK(reader->ReadBits(1, &ac4_substream_group_info.sus_ver)));
514 } else {
515 ac4_substream_group_info.sus_ver = 1;
516 }
517 ParseAc4SubstreamInfoChan(reader,
518 GetPresentationVersion(substream_group_index),
519 ac4_toc.fs_index, frame_rate_factor,
520 ac4_substream_group_info.b_substreams_present);
521 if (ac4_substream_group_info.b_hsf_ext) {
522 ParseAc4HsfExtSubstreamInfo(reader, ac4_substream_group_info);
523 }
524 }
525 } else {
526 RCHECK(reader->ReadBits(1, &ac4_substream_group_info.b_oamd_substream));
527 if (ac4_substream_group_info.b_oamd_substream) {
528 ParseOamdSubstreamInfo(reader, ac4_substream_group_info);
529 }
530 for (int sus = 0; sus < ac4_substream_group_info.n_lf_substreams; sus++) {
531 RCHECK(reader->ReadBits(1, &ac4_substream_group_info.b_ajoc));
532 if (ac4_substream_group_info.b_ajoc) {
533 ParseAc4SubstreamInfoAjoc(
534 reader, ac4_toc.fs_index, frame_rate_factor,
535 ac4_substream_group_info.b_substreams_present);
536 if (ac4_substream_group_info.b_hsf_ext) {
537 ParseAc4HsfExtSubstreamInfo(reader, ac4_substream_group_info);
538 }
539 } else {
540 ParseAc4SubstreamInfoObj(reader, ac4_toc.fs_index, frame_rate_factor,
541 ac4_substream_group_info.b_substreams_present);
542 if (ac4_substream_group_info.b_hsf_ext) {
543 ParseAc4HsfExtSubstreamInfo(reader, ac4_substream_group_info);
544 }
545 }
546 }
547 }
548 RCHECK(reader->ReadBits(1, &ac4_substream_group_info.b_content_type));
549 if (ac4_substream_group_info.b_content_type) {
550 ParseContentType(reader, ac4_substream_group_info);
551 }
552 return true;
553}
554
555bool AC4Parser::ParseContentType(
556 BitReader* reader,
557 Ac4SubstreamGroupInfo& ac4_substream_group_info) {
558 RCHECK(reader->ReadBits(3, &ac4_substream_group_info.content_classifier));
559 RCHECK(reader->ReadBits(1, &ac4_substream_group_info.b_language_indicator));
560 if (ac4_substream_group_info.b_language_indicator) {
561 RCHECK(reader->ReadBits(
562 1, &ac4_substream_group_info.b_serialized_language_tag));
563 if (ac4_substream_group_info.b_serialized_language_tag) {
564 RCHECK(reader->ReadBits(1, &ac4_substream_group_info.b_start_tag));
565 RCHECK(
566 reader->ReadBits(16, &ac4_substream_group_info.language_tag_chunk));
567 } else {
568 RCHECK(
569 reader->ReadBits(6, &ac4_substream_group_info.n_language_tag_bytes));
570 for (int i = 0; i < ac4_substream_group_info.n_language_tag_bytes; i++) {
571 RCHECK(reader->ReadBits(
572 8, &ac4_substream_group_info.language_tag_bytes[i]));
573 }
574 }
575 }
576 return true;
577}
578
579bool AC4Parser::ParseOamdSubstreamInfo(
580 BitReader* reader,
581 Ac4SubstreamGroupInfo& ac4_substream_group_info) {
582 RCHECK(reader->ReadBits(1, &ac4_substream_group_info.b_oamd_ndot));
583 if (ac4_substream_group_info.b_substreams_present == 1) {
584 RCHECK(reader->ReadBits(2, &ac4_substream_group_info.substream_index));
585 if (ac4_substream_group_info.substream_index == 3) {
586 ac4_substream_group_info.substream_index +=
587 ReadAc4VariableBits(reader, 2);
588 }
589 }
590 return true;
591}
592
593bool AC4Parser::ParseAc4HsfExtSubstreamInfo(
594 BitReader* reader,
595 Ac4SubstreamGroupInfo& ac4_substream_group_info) {
596 if (ac4_substream_group_info.b_substreams_present == 1) {
597 RCHECK(reader->ReadBits(2, &ac4_substream_group_info.substream_index));
598 if (ac4_substream_group_info.substream_index == 3) {
599 ac4_substream_group_info.substream_index +=
600 ReadAc4VariableBits(reader, 2);
601 }
602 }
603 return true;
604}
605
606bool AC4Parser::ParseAc4SubstreamInfoChan(BitReader* reader,
607 int presentation_version,
608 int fs_index,
609 int frame_rate_factor,
610 int b_substreams_present) {
611 Ac4SubstreamInfoChan ac4_substream_info_chan;
612 ac4_substream_info_chan.channel_mode =
613 ParseChannelMode(reader, presentation_version);
614 if (ac4_substream_info_chan.channel_mode == 0b111111111) {
615 ac4_substream_info_chan.channel_mode += ReadAc4VariableBits(reader, 2);
616 }
617 if ((ac4_substream_info_chan.channel_mode >= CH_MODE_7_0_4) &&
618 (ac4_substream_info_chan.channel_mode <= CH_MODE_9_1_4)) {
619 RCHECK(reader->ReadBits(
620 1, &ac4_substream_info_chan.b_4_back_channels_present));
621 RCHECK(reader->ReadBits(1, &ac4_substream_info_chan.b_centre_present));
622 RCHECK(reader->ReadBits(2, &ac4_substream_info_chan.top_channels_present));
623 }
624 if (fs_index == 1) {
625 RCHECK(reader->ReadBits(1, &ac4_substream_info_chan.b_sf_multiplier));
626 if (ac4_substream_info_chan.b_sf_multiplier) {
627 RCHECK(reader->ReadBits(1, &ac4_substream_info_chan.sf_multiplier));
628 }
629 }
630 RCHECK(reader->ReadBits(1, &ac4_substream_info_chan.b_bitrate_info));
631 if (ac4_substream_info_chan.b_bitrate_info) {
632 RCHECK(reader->ReadBits(3, &ac4_substream_info_chan.bitrate_indicator));
633 if ((ac4_substream_info_chan.bitrate_indicator & 0x1) == 1) {
634 int more_bits = 0;
635 RCHECK(reader->ReadBits(2, &more_bits));
636 ac4_substream_info_chan.bitrate_indicator =
637 (ac4_substream_info_chan.bitrate_indicator << 2) + more_bits;
638 }
639 }
640 if (ac4_substream_info_chan.channel_mode >= CH_MODE_70_52 &&
641 ac4_substream_info_chan.channel_mode <= CH_MODE_71_322) {
642 RCHECK(reader->ReadBits(1, &ac4_substream_info_chan.add_ch_base));
643 }
644 for (int i = 0; i < frame_rate_factor; i++) {
645 RCHECK(reader->ReadBits(1, &ac4_substream_info_chan.b_audio_ndot));
646 }
647 if (b_substreams_present == 1) {
648 RCHECK(reader->ReadBits(2, &ac4_substream_info_chan.substream_index));
649 if (ac4_substream_info_chan.substream_index == 3) {
650 ac4_substream_info_chan.substream_index += ReadAc4VariableBits(reader, 2);
651 }
652 }
653 return true;
654}
655
656bool AC4Parser::ParseAc4SubstreamInfoAjoc(BitReader* reader,
657 int fs_index,
658 int frame_rate_factor,
659 int b_substreams_present) {
660 Ac4SubstreamInfoAjoc ac4_substream_info_ajoc;
661 RCHECK(reader->ReadBits(1, &ac4_substream_info_ajoc.b_lfe));
662 RCHECK(reader->ReadBits(1, &ac4_substream_info_ajoc.b_static_dmx));
663 if (ac4_substream_info_ajoc.b_static_dmx) {
664 ac4_substream_info_ajoc.n_fullband_dmx_signals = 5;
665 } else {
666 RCHECK(reader->ReadBits(
667 4, &ac4_substream_info_ajoc.n_fullband_dmx_signals_minus1));
668 ac4_substream_info_ajoc.n_fullband_dmx_signals =
669 ac4_substream_info_ajoc.n_fullband_dmx_signals_minus1 + 1;
670 ParseBedDynObjAssignment(reader,
671 ac4_substream_info_ajoc.n_fullband_dmx_signals,
672 ac4_substream_info_ajoc);
673 }
674 RCHECK(
675 reader->ReadBits(1, &ac4_substream_info_ajoc.b_oamd_common_data_present));
676 if (ac4_substream_info_ajoc.b_oamd_common_data_present) {
677 ParseOamdCommonData(reader);
678 }
679 RCHECK(reader->ReadBits(
680 4, &ac4_substream_info_ajoc.n_fullband_upmix_signals_minus1));
681 if (ac4_substream_info_ajoc.n_fullband_upmix_signals_minus1 + 1 == 16) {
682 ac4_substream_info_ajoc.n_fullband_upmix_signals_minus1 +=
683 ReadAc4VariableBits(reader, 3);
684 }
685 ParseBedDynObjAssignment(
686 reader, ac4_substream_info_ajoc.n_fullband_upmix_signals_minus1 + 1,
687 ac4_substream_info_ajoc);
688 if (fs_index == 1) {
689 RCHECK(reader->ReadBits(1, &ac4_substream_info_ajoc.b_sf_multiplier));
690 if (ac4_substream_info_ajoc.b_sf_multiplier) {
691 RCHECK(reader->ReadBits(1, &ac4_substream_info_ajoc.sf_multiplier));
692 }
693 }
694 RCHECK(reader->ReadBits(1, &ac4_substream_info_ajoc.b_bitrate_info));
695 if (ac4_substream_info_ajoc.b_bitrate_info) {
696 RCHECK(reader->ReadBits(3, &ac4_substream_info_ajoc.bitrate_indicator));
697 if ((ac4_substream_info_ajoc.bitrate_indicator & 0x1) == 1) {
698 int more_bits = 0;
699 RCHECK(reader->ReadBits(2, &more_bits));
700 ac4_substream_info_ajoc.bitrate_indicator =
701 (ac4_substream_info_ajoc.bitrate_indicator << 2) + more_bits;
702 }
703 }
704 for (int i = 0; i < frame_rate_factor; i++) {
705 RCHECK(reader->ReadBits(1, &ac4_substream_info_ajoc.b_audio_ndot));
706 }
707 if (b_substreams_present == 1) {
708 RCHECK(reader->ReadBits(2, &ac4_substream_info_ajoc.substream_index));
709 if (ac4_substream_info_ajoc.substream_index == 3) {
710 ac4_substream_info_ajoc.substream_index += ReadAc4VariableBits(reader, 2);
711 }
712 }
713 // sus_ver = 1;
714 return true;
715}
716
717bool AC4Parser::ParseBedDynObjAssignment(
718 BitReader* reader,
719 int n_signals,
720 Ac4SubstreamInfoAjoc& ac4_substream_info_ajoc) {
721 RCHECK(reader->ReadBits(1, &ac4_substream_info_ajoc.b_dyn_objects_only));
722 if (ac4_substream_info_ajoc.b_dyn_objects_only == 0) {
723 RCHECK(reader->ReadBits(1, &ac4_substream_info_ajoc.b_isf));
724 if (ac4_substream_info_ajoc.b_isf) {
725 RCHECK(reader->ReadBits(3, &ac4_substream_info_ajoc.isf_config));
726 } else {
727 RCHECK(reader->ReadBits(1, &ac4_substream_info_ajoc.b_ch_assign_code));
728 if (ac4_substream_info_ajoc.b_ch_assign_code) {
729 RCHECK(
730 reader->ReadBits(3, &ac4_substream_info_ajoc.bed_chan_assign_code));
731 } else {
732 RCHECK(
733 reader->ReadBits(1, &ac4_substream_info_ajoc.b_chan_assign_mask));
734 if (ac4_substream_info_ajoc.b_chan_assign_mask) {
735 RCHECK(reader->ReadBits(
736 1, &ac4_substream_info_ajoc.b_nonstd_bed_channel_assignment));
737 if (ac4_substream_info_ajoc.b_nonstd_bed_channel_assignment) {
738 RCHECK(reader->ReadBits(
739 17,
740 &ac4_substream_info_ajoc.nonstd_bed_channel_assignment_mask));
741 } else {
742 RCHECK(reader->ReadBits(
743 10, &ac4_substream_info_ajoc.std_bed_channel_assignment_mask));
744 }
745 } else {
746 if (n_signals > 1) {
747 int bed_ch_bits = (int)ceil(log((float)n_signals) / log((float)2));
748 RCHECK(reader->ReadBits(
749 bed_ch_bits, &ac4_substream_info_ajoc.n_bed_signals_minus1));
750 ac4_substream_info_ajoc.n_bed_signals =
751 ac4_substream_info_ajoc.n_bed_signals_minus1 + 1;
752 } else {
753 ac4_substream_info_ajoc.n_bed_signals = 1;
754 }
755 for (int b = 0; b < ac4_substream_info_ajoc.n_bed_signals; b++) {
756 RCHECK(reader->ReadBits(
757 4, &ac4_substream_info_ajoc.nonstd_bed_channel_assignment));
758 }
759 }
760 }
761 }
762 }
763 return true;
764}
765
766bool AC4Parser::ParseOamdCommonData(BitReader* reader) {
767 OamdCommonData oamd_common_data;
768 RCHECK(reader->ReadBits(1, &oamd_common_data.b_default_screen_size_ratio));
769 if (oamd_common_data.b_default_screen_size_ratio == 0) {
770 RCHECK(
771 reader->ReadBits(5, &oamd_common_data.master_screen_size_ratio_code));
772 }
773 RCHECK(reader->ReadBits(1, &oamd_common_data.b_bed_object_chan_distribute));
774 RCHECK(reader->ReadBits(1, &oamd_common_data.b_additional_data));
775 if (oamd_common_data.b_additional_data) {
776 RCHECK(reader->ReadBits(1, &oamd_common_data.add_data_bytes_minus1));
777 oamd_common_data.add_data_bytes =
778 oamd_common_data.add_data_bytes_minus1 + 1;
779 if (oamd_common_data.add_data_bytes == 2) {
780 oamd_common_data.add_data_bytes += ReadAc4VariableBits(reader, 2);
781 }
782 reader->SkipBytes(oamd_common_data.add_data_bytes);
783 }
784 return true;
785}
786
787bool AC4Parser::ParseAc4SubstreamInfoObj(BitReader* reader,
788 int fs_index,
789 int frame_rate_factor,
790 int b_substreams_present) {
791 Ac4SubstreamInfoObj ac4_substream_info_obj;
792 RCHECK(reader->ReadBits(3, &ac4_substream_info_obj.n_objects_code));
793 RCHECK(reader->ReadBits(1, &ac4_substream_info_obj.b_dynamic_objects));
794 if (ac4_substream_info_obj.b_dynamic_objects) {
795 RCHECK(reader->ReadBits(1, &ac4_substream_info_obj.b_lfe));
796 } else {
797 RCHECK(reader->ReadBits(1, &ac4_substream_info_obj.b_bed_objects));
798 if (ac4_substream_info_obj.b_bed_objects) {
799 RCHECK(reader->ReadBits(1, &ac4_substream_info_obj.b_bed_start));
800 if (ac4_substream_info_obj.b_bed_start) {
801 RCHECK(reader->ReadBits(1, &ac4_substream_info_obj.b_ch_assign_code));
802 if (ac4_substream_info_obj.b_ch_assign_code) {
803 RCHECK(reader->ReadBits(
804 3, &ac4_substream_info_obj.bed_chan_assign_code));
805 } else {
806 RCHECK(reader->ReadBits(
807 1, &ac4_substream_info_obj.b_nonstd_bed_channel_assignment));
808 if (ac4_substream_info_obj.b_nonstd_bed_channel_assignment) {
809 RCHECK(reader->ReadBits(
810 17,
811 &ac4_substream_info_obj.nonstd_bed_channel_assignment_mask));
812 } else {
813 RCHECK(reader->ReadBits(
814 10, &ac4_substream_info_obj.std_bed_channel_assignment_mask));
815 }
816 }
817 }
818 } else {
819 RCHECK(reader->ReadBits(1, &ac4_substream_info_obj.b_isf));
820 if (ac4_substream_info_obj.b_isf) {
821 RCHECK(reader->ReadBits(1, &ac4_substream_info_obj.b_isf_start));
822 if (ac4_substream_info_obj.b_isf_start) {
823 RCHECK(reader->ReadBits(1, &ac4_substream_info_obj.isf_config));
824 }
825 } else {
826 int res_bytes = 0;
827 RCHECK(reader->ReadBits(4, &res_bytes));
828 reader->SkipBits(res_bytes * 8);
829 }
830 }
831 }
832 if (fs_index == 1) {
833 RCHECK(reader->ReadBits(1, &ac4_substream_info_obj.b_sf_multiplier));
834 if (ac4_substream_info_obj.b_sf_multiplier) {
835 RCHECK(reader->ReadBits(1, &ac4_substream_info_obj.sf_multiplier));
836 }
837 }
838 RCHECK(reader->ReadBits(1, &ac4_substream_info_obj.b_bitrate_info));
839 if (ac4_substream_info_obj.b_bitrate_info) {
840 RCHECK(reader->ReadBits(3, &ac4_substream_info_obj.bitrate_indicator));
841 if ((ac4_substream_info_obj.bitrate_indicator & 0x1) == 1) {
842 int more_bits = 0;
843 RCHECK(reader->ReadBits(2, &more_bits));
844 ac4_substream_info_obj.bitrate_indicator =
845 (ac4_substream_info_obj.bitrate_indicator << 2) + more_bits;
846 }
847 }
848 for (int i = 0; i < frame_rate_factor; i++) {
849 RCHECK(reader->ReadBits(1, &ac4_substream_info_obj.b_audio_ndot));
850 }
851 if (b_substreams_present == 1) {
852 RCHECK(reader->ReadBits(2, &ac4_substream_info_obj.substream_index));
853 if (ac4_substream_info_obj.substream_index == 3) {
854 ac4_substream_info_obj.substream_index += ReadAc4VariableBits(reader, 2);
855 }
856 }
857 // sus_ver = 1;
858 return true;
859}
860
861int AC4Parser::ParseChannelMode(BitReader* reader, int presentation_version) {
862 int channel_mode_code = 0;
863 int read_more = 0;
864 RCHECK(reader->ReadBits(1, &channel_mode_code));
865 if (channel_mode_code == 0) {
866 return CH_MODE_MONO;
867 }
868 RCHECK(reader->ReadBits(1, &read_more));
869 channel_mode_code = (channel_mode_code << 1) | read_more;
870 if (channel_mode_code == 2) { // Stereo 0b10
871 return CH_MODE_STEREO;
872 }
873 RCHECK(reader->ReadBits(2, &read_more));
874 channel_mode_code = (channel_mode_code << 2) | read_more;
875 switch (channel_mode_code) {
876 case 12: // 3.0 0b1100
877 return CH_MODE_3_0;
878 case 13: // 5.0 0b1101
879 return CH_MODE_5_0;
880 case 14: // 5.1 0b1110
881 return CH_MODE_5_1;
882 }
883 RCHECK(reader->ReadBits(3, &read_more));
884 channel_mode_code = (channel_mode_code << 3) | read_more;
885 switch (channel_mode_code) {
886 case 120: // 0b1111000
887 if (presentation_version == 2) { // IMS (all content)
888 return CH_MODE_STEREO;
889 } else { // 7.0: 3/4/0
890 return CH_MODE_70_34;
891 }
892 case 121: // 0b1111001
893 if (presentation_version == 2) { // IMS (Atmos content)
894 // dolby_atmos_indicator |= 1;
895 return CH_MODE_STEREO;
896 } else { // 7.1: 3/4/0.1
897 return CH_MODE_71_34;
898 }
899 case 122: // 7.0: 5/2/0 0b1111010
900 return CH_MODE_70_52;
901 case 123: // 7.1: 5/2/0.1 0b1111011
902 return CH_MODE_71_52;
903 case 124: // 7.0: 3/2/2 0b1111100
904 return CH_MODE_70_322;
905 case 125: // 7.1: 3/2/2.1 0b1111101
906 return CH_MODE_71_322;
907 }
908 RCHECK(reader->ReadBits(1, &read_more));
909 channel_mode_code = (channel_mode_code << 1) | read_more;
910 switch (channel_mode_code) {
911 case 252: // 7.0.4 0b11111100
912 return CH_MODE_7_0_4;
913 case 253: // 7.1.4 0b11111101
914 return CH_MODE_7_1_4;
915 }
916 RCHECK(reader->ReadBits(1, &read_more));
917 channel_mode_code = (channel_mode_code << 1) | read_more;
918 switch (channel_mode_code) {
919 case 508: // 9.0.4 0b111111100
920 return CH_MODE_9_0_4;
921 case 509: // 9.1.4 0b111111101
922 return CH_MODE_9_1_4;
923 case 510: // 22.2 0b111111110
924 return CH_MODE_22_2;
925 case 511: // Reserved, escape value 0b111111111
926 default:
927 ReadAc4VariableBits(reader, 2);
928 return CH_MODE_RESERVED;
929 }
930}
931
932} // namespace media
933} // namespace shaka
A class to read bit streams.
Definition bit_reader.h:19
size_t bit_position() const
Definition bit_reader.h:96
bool SkipBits(size_t num_bits)
Definition bit_reader.cc:28
bool SkipBytes(size_t num_bytes)
Definition bit_reader.cc:67
bool ReadBits(size_t num_bits, T *out)
Definition bit_reader.h:37
All the methods that are virtual are virtual for mocking.