Shaka Packager SDK
Loading...
Searching...
No Matches
ac3_header.cc
1// Copyright 2017 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/formats/mp2t/ac3_header.h>
8
9#include <absl/log/check.h>
10
11#include <packager/media/base/bit_reader.h>
12#include <packager/media/base/bit_writer.h>
13#include <packager/media/formats/mp2t/mp2t_common.h>
14
15namespace shaka {
16namespace media {
17namespace mp2t {
18namespace {
19
20// ASTC Standard A/52:2012 Table 5.6 Sample Rate Codes.
21const uint32_t kAc3SampleRateTable[] = {48000, 44100, 32000};
22
23// ASTC Standard A/52:2012 Table 5.8 Audio Coding Mode.
24const uint8_t kAc3NumChannelsTable[] = {2, 1, 2, 3, 3, 4, 4, 5};
25
26// ATSC Standard A/52:2012 Table 5.18 Frame Size Code Table
27// (in words = 16 bits).
28const size_t kFrameSizeCodeTable[][3] = {
29 // {32kHz, 44.1kHz, 48kHz}
30 {96, 69, 64}, {96, 70, 64}, {120, 87, 80},
31 {120, 88, 80}, {144, 104, 96}, {144, 105, 96},
32 {168, 121, 112}, {168, 122, 112}, {192, 139, 128},
33 {192, 140, 128}, {240, 174, 160}, {240, 175, 160},
34 {288, 208, 192}, {288, 209, 192}, {336, 243, 224},
35 {336, 244, 224}, {384, 278, 256}, {384, 279, 256},
36 {480, 348, 320}, {480, 349, 320}, {576, 417, 384},
37 {576, 418, 384}, {672, 487, 448}, {672, 488, 448},
38 {768, 557, 512}, {768, 558, 512}, {960, 696, 640},
39 {960, 697, 640}, {1152, 835, 768}, {1152, 836, 768},
40 {1344, 975, 896}, {1344, 976, 896}, {1536, 1114, 1024},
41 {1536, 1115, 1024}, {1728, 1253, 1152}, {1728, 1254, 1152},
42 {1920, 1393, 1280}, {1920, 1394, 1280},
43};
44
45// Calculate the size of the frame from the sample rate code and the
46// frame size code.
47// @return the size of the frame (header + payload).
48size_t CalcFrameSize(uint8_t fscod, uint8_t frmsizecod) {
49 const size_t kNumFscode = std::size(kAc3SampleRateTable);
50 DCHECK_LT(fscod, kNumFscode);
51 DCHECK_LT(frmsizecod, std::size(kFrameSizeCodeTable));
52 // The order of frequencies are reversed in |kFrameSizeCodeTable| compared to
53 // |kAc3SampleRateTable|.
54 const int index = kNumFscode - 1 - fscod;
55 return kFrameSizeCodeTable[frmsizecod][index] * 2;
56}
57
58} // namespace
59
60bool Ac3Header::IsSyncWord(const uint8_t* buf) const {
61 DCHECK(buf);
62 // ATSC Standard A/52:2012 5.4.1 syncinfo: Synchronization Information.
63 return buf[0] == 0x0B && buf[1] == 0x77;
64}
65
67 // Arbitrary. Actual frame size starts with 96 words.
68 const size_t kMinAc3FrameSize = 10u;
69 return kMinAc3FrameSize;
70}
71
73 // ATSC Standard A/52:2012
74 // Annex A: AC-3 Elementary Streams in the MPEG-2 Multiplex.
75 const size_t kSamplesPerAc3Frame = 1536;
76 return kSamplesPerAc3Frame;
77}
78
79bool Ac3Header::Parse(const uint8_t* audio_frame, size_t audio_frame_size) {
80 BitReader frame(audio_frame, audio_frame_size);
81
82 // ASTC Standard A/52:2012 5. BIT STREAM SYNTAX.
83 // syncinfo: synchronization information section.
84 uint16_t syncword;
85 RCHECK(frame.ReadBits(16, &syncword));
86 RCHECK(syncword == 0x0B77);
87 uint16_t crc1;
88 RCHECK(frame.ReadBits(16, &crc1));
89 RCHECK(frame.ReadBits(2, &fscod_));
90 RCHECK(fscod_ < std::size(kAc3SampleRateTable));
91 RCHECK(frame.ReadBits(6, &frmsizecod_));
92 RCHECK(frmsizecod_ < std::size(kFrameSizeCodeTable));
93
94 // bsi: bit stream information section.
95 RCHECK(frame.ReadBits(5, &bsid_));
96 RCHECK(frame.ReadBits(3, &bsmod_));
97
98 RCHECK(frame.ReadBits(3, &acmod_));
99 RCHECK(acmod_ < std::size(kAc3NumChannelsTable));
100 // If 3 front channels.
101 if ((acmod_ & 0x01) && (acmod_ != 0x01))
102 RCHECK(frame.SkipBits(2)); // cmixlev.
103 // If a surround channel exists.
104 if (acmod_ & 0x04)
105 RCHECK(frame.SkipBits(2)); // surmixlev.
106 // If in 2/0 mode.
107 if (acmod_ == 0x02)
108 RCHECK(frame.SkipBits(2)); // dsurmod.
109
110 RCHECK(frame.ReadBits(1, &lfeon_));
111
112 return true;
113}
114
116 // Unlike ADTS, for AC3, the whole frame is included in the media sample, so
117 // return 0 header size.
118 return 0;
119}
120
122 return CalcFrameSize(fscod_, frmsizecod_);
123}
124
125size_t Ac3Header::GetFrameSizeWithoutParsing(const uint8_t* data,
126 size_t num_bytes) const {
127 DCHECK_GT(num_bytes, static_cast<size_t>(4));
128 uint8_t fscod = data[4] >> 6;
129 uint8_t frmsizecod = data[4] & 0x3f;
130 return CalcFrameSize(fscod, frmsizecod);
131}
132
133void Ac3Header::GetAudioSpecificConfig(std::vector<uint8_t>* buffer) const {
134 DCHECK(buffer);
135 buffer->clear();
136 BitWriter config(buffer);
137 // Accoding to ETSI TS 102 366 V1.3.1 (2014-08) F.4 AC3SpecificBox.
138 config.WriteBits(fscod_, 2);
139 config.WriteBits(bsid_, 5);
140 config.WriteBits(bsmod_, 3);
141 config.WriteBits(acmod_, 3);
142 config.WriteBits(lfeon_, 1);
143 const uint8_t bit_rate_code = frmsizecod_ >> 1;
144 config.WriteBits(bit_rate_code, 5);
145 config.Flush();
146}
147
149 // Only useful for AAC. Return a dummy value instead.
150 return 0;
151}
152
154 DCHECK_LT(fscod_, std::size(kAc3SampleRateTable));
155 return kAc3SampleRateTable[fscod_];
156}
157
159 DCHECK_LT(acmod_, std::size(kAc3NumChannelsTable));
160 return kAc3NumChannelsTable[acmod_] + (lfeon_ ? 1 : 0);
161}
162
163} // namespace mp2t
164} // namespace media
165} // namespace shaka
A class to read bit streams.
Definition bit_reader.h:20
bool SkipBits(size_t num_bits)
Definition bit_reader.cc:26
bool ReadBits(size_t num_bits, T *out)
Definition bit_reader.h:38
void Flush()
Write pending bits, and align bitstream with extra zero bits.
Definition bit_writer.cc:33
void WriteBits(uint32_t bits, size_t number_of_bits)
Definition bit_writer.cc:17
bool Parse(const uint8_t *adts_frame, size_t adts_frame_size) override
Definition ac3_header.cc:79
size_t GetMinFrameSize() const override
Definition ac3_header.cc:66
bool IsSyncWord(const uint8_t *buf) const override
Definition ac3_header.cc:60
uint8_t GetObjectType() const override
size_t GetHeaderSize() const override
size_t GetFrameSize() const override
size_t GetFrameSizeWithoutParsing(const uint8_t *data, size_t num_bytes) const override
void GetAudioSpecificConfig(std::vector< uint8_t > *buffer) const override
uint32_t GetSamplingFrequency() const override
uint8_t GetNumChannels() const override
size_t GetSamplesPerFrame() const override
Definition ac3_header.cc:72
All the methods that are virtual are virtual for mocking.