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