DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
adts_header.cc
1 // Copyright 2014 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/adts_header.h"
8 
9 #include "packager/media/base/bit_reader.h"
10 #include "packager/media/formats/mp2t/mp2t_common.h"
11 #include "packager/media/formats/mpeg/adts_constants.h"
12 
13 namespace edash_packager {
14 namespace media {
15 namespace mp2t {
16 
17 AdtsHeader::AdtsHeader()
18  : valid_config_(false),
19  profile_(0),
20  sampling_frequency_index_(0),
21  channel_configuration_(0) {}
22 
23 size_t AdtsHeader::GetAdtsFrameSize(const uint8_t* data, size_t num_bytes) {
24  if (num_bytes < 6)
25  return 0;
26  return ((static_cast<int>(data[5]) >> 5) |
27  (static_cast<int>(data[4]) << 3) |
28  ((static_cast<int>(data[3]) & 0x3) << 11));
29 }
30 
31 size_t AdtsHeader::GetAdtsHeaderSize(const uint8_t* data, size_t num_bytes) {
32  if (num_bytes < 2)
33  return 0;
34  if (data[1] & 0x01)
35  return kAdtsHeaderMinSize;
36  return kAdtsHeaderMinSize + sizeof(uint16_t); // Header + CRC.
37 }
38 
39 bool AdtsHeader::Parse(const uint8_t* adts_frame, size_t adts_frame_size) {
40  CHECK(adts_frame);
41 
42  valid_config_ = false;
43 
44  BitReader frame(adts_frame, adts_frame_size);
45  // Verify frame starts with sync bits (0xfff).
46  uint32_t sync;
47  RCHECK(frame.ReadBits(12, &sync));
48  RCHECK(sync == 0xfff);
49  // Skip MPEG version and layer.
50  RCHECK(frame.SkipBits(3));
51  // Get "protection absent" flag.
52  uint8_t protection_absent;
53  RCHECK(frame.ReadBits(1, &protection_absent));
54  // Get profile.
55  RCHECK(frame.ReadBits(2, &profile_));
56  // Get sampling frequency.
57  RCHECK(frame.ReadBits(4, &sampling_frequency_index_));
58  RCHECK(sampling_frequency_index_ < kAdtsFrequencyTableSize);
59  // Skip private stream bit.
60  RCHECK(frame.SkipBits(1));
61  // Get number of audio channels.
62  RCHECK(frame.ReadBits(3, &channel_configuration_));
63  RCHECK((channel_configuration_ > 0) &&
64  (channel_configuration_ < kAdtsNumChannelsTableSize));
65  // Skip originality, home and copyright info.
66  RCHECK(frame.SkipBits(4));
67  // Verify that the frame size matches input parameters.
68  uint16_t frame_size;
69  RCHECK(frame.ReadBits(13, &frame_size));
70  RCHECK(frame_size == adts_frame_size);
71  // Skip buffer fullness indicator.
72  RCHECK(frame.SkipBits(11));
73  uint8_t num_blocks_minus_1;
74  RCHECK(frame.ReadBits(2, &num_blocks_minus_1));
75  if (num_blocks_minus_1) {
76  NOTIMPLEMENTED() << "ADTS frames with more than one data block "
77  "not supported.";
78  return false;
79  }
80 
81  valid_config_ = true;
82  return true;
83 }
84 
85 bool AdtsHeader::GetAudioSpecificConfig(std::vector<uint8_t>* buffer) const {
86  DCHECK(buffer);
87  if (!valid_config_)
88  return false;
89 
90  buffer->resize(2);
91  (*buffer)[0] = ((profile_ + 1) << 3) | (sampling_frequency_index_ >> 1);
92  (*buffer)[1] = ((sampling_frequency_index_ & 1) << 7) |
93  (channel_configuration_ << 3);
94  return true;
95 }
96 
97 uint8_t AdtsHeader::GetObjectType() const {
98  return profile_ + 1;
99 }
100 
101 uint32_t AdtsHeader::GetSamplingFrequency() const {
102  DCHECK_LT(sampling_frequency_index_, kAdtsFrequencyTableSize);
103  return kAdtsFrequencyTable[sampling_frequency_index_];
104 }
105 
106 uint8_t AdtsHeader::GetNumChannels() const {
107  DCHECK_GT(channel_configuration_, 0);
108  DCHECK_LT(channel_configuration_, kAdtsNumChannelsTableSize);
109  return kAdtsNumChannelsTable[channel_configuration_];
110 }
111 
112 } // namespace mp2t
113 } // namespace media
114 } // namespace edash_packager
bool ReadBits(int num_bits, T *out)
Definition: bit_reader.h:35
A class to read bit streams.
Definition: bit_reader.h:17
bool SkipBits(int num_bits)
Definition: bit_reader.cc:24