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