DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
es_descriptor.cc
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "packager/media/codecs/es_descriptor.h"
6 
7 #include "packager/media/base/bit_reader.h"
8 #include "packager/media/base/buffer_writer.h"
9 #include "packager/media/base/rcheck.h"
10 
11 namespace shaka {
12 namespace media {
13 namespace {
14 
15 // ISO/IEC 14496-1:2004 Section 7.2.6.6 Table 6: StreamType values.
16 enum StreamType {
17  kForbiddenStreamType = 0x00,
18  kObjectDescriptorStreamType = 0x01,
19  kClockReferenceStreamType = 0x02,
20  kSceneDescriptionStreamType = 0x03,
21  kVisualStreamType = 0x04,
22  kAudioStreamType = 0x05,
23  kMPEG7StreamType = 0x06,
24  kIPMPStreamType = 0x07,
25  kObjectContentInfoStreamType = 0x08,
26  kMPEGJStreamType = 0x09,
27  kInteractionStream = 0x0A,
28  kIPMPToolStreamType = 0x0B,
29 };
30 
31 // ISO/IEC 14496-1:2004 Section 7.3.2.3 Table 12: ISO SL Config Descriptor.
32 enum SLPredefinedTags {
33  kSLPredefinedNull = 0x01,
34  kSLPredefinedMP4 = 0x02,
35 };
36 
37 // The elementary stream size is specific by up to 4 bytes.
38 // The MSB of a byte indicates if there are more bytes for the size.
39 bool ReadESSize(BitReader* reader, uint32_t* size) {
40  uint8_t msb;
41  uint8_t byte;
42 
43  *size = 0;
44 
45  for (size_t i = 0; i < 4; ++i) {
46  RCHECK(reader->ReadBits(1, &msb));
47  RCHECK(reader->ReadBits(7, &byte));
48  *size = (*size << 7) + byte;
49 
50  if (msb == 0)
51  break;
52  }
53 
54  return true;
55 }
56 
57 // Descryptor Header Size: 1 byte tag and 1 byte size (we don't support
58 // multi-bytes size for now).
59 const size_t kHeaderSize = 2;
60 const size_t kMaxDecoderSpecificInfoSize = 64;
61 const uint32_t kUnknownBitrate = 0;
62 
63 } // namespace
64 
65 ESDescriptor::ESDescriptor()
66  : esid_(0),
67  object_type_(kForbidden),
68  max_bitrate_(kUnknownBitrate),
69  avg_bitrate_(kUnknownBitrate) {}
70 
71 ESDescriptor::~ESDescriptor() {}
72 
73 bool ESDescriptor::Parse(const std::vector<uint8_t>& data) {
74  BitReader reader(&data[0], data.size());
75  uint8_t tag;
76  uint32_t size;
77  uint8_t stream_dependency_flag;
78  uint8_t url_flag;
79  uint8_t ocr_stream_flag;
80  uint16_t dummy;
81 
82  RCHECK(reader.ReadBits(8, &tag));
83  RCHECK(tag == kESDescrTag);
84  RCHECK(ReadESSize(&reader, &size));
85 
86  RCHECK(reader.ReadBits(16, &esid_)); // ES_ID
87  RCHECK(reader.ReadBits(1, &stream_dependency_flag));
88  RCHECK(reader.ReadBits(1, &url_flag));
89  RCHECK(!url_flag); // We don't support url flag
90  RCHECK(reader.ReadBits(1, &ocr_stream_flag));
91  RCHECK(reader.ReadBits(5, &dummy)); // streamPriority
92 
93  if (stream_dependency_flag)
94  RCHECK(reader.ReadBits(16, &dummy)); // dependsOn_ES_ID
95  if (ocr_stream_flag)
96  RCHECK(reader.ReadBits(16, &dummy)); // OCR_ES_Id
97 
98  RCHECK(ParseDecoderConfigDescriptor(&reader));
99 
100  return true;
101 }
102 
103 bool ESDescriptor::ParseDecoderConfigDescriptor(BitReader* reader) {
104  uint8_t tag;
105  uint32_t size;
106  uint32_t dummy;
107 
108  RCHECK(reader->ReadBits(8, &tag));
109  RCHECK(tag == kDecoderConfigDescrTag);
110  RCHECK(ReadESSize(reader, &size));
111 
112  RCHECK(reader->ReadBits(8, &object_type_));
113  RCHECK(reader->ReadBits(32, &dummy));
114  RCHECK(reader->ReadBits(32, &max_bitrate_));
115  RCHECK(reader->ReadBits(32, &avg_bitrate_));
116  RCHECK(ParseDecoderSpecificInfo(reader));
117 
118  return true;
119 }
120 
121 bool ESDescriptor::ParseDecoderSpecificInfo(BitReader* reader) {
122  DCHECK(reader);
123  uint8_t tag;
124  uint32_t size;
125 
126  RCHECK(reader->ReadBits(8, &tag));
127  RCHECK(tag == kDecoderSpecificInfoTag);
128  RCHECK(ReadESSize(reader, &size));
129 
130  decoder_specific_info_.resize(size);
131  for (uint32_t i = 0; i < size; ++i)
132  RCHECK(reader->ReadBits(8, &decoder_specific_info_[i]));
133  return true;
134 }
135 
136 void ESDescriptor::Write(BufferWriter* writer) const {
137  DCHECK(writer);
138  CHECK_LT(decoder_specific_info_.size(), kMaxDecoderSpecificInfoSize);
139 
140  const std::vector<uint8_t> kEmptyDecodingBufferSize(3, 0);
141  const uint8_t kNoEsFlags = 0;
142 
143  const uint8_t decoder_specific_info_size =
144  static_cast<uint8_t>(decoder_specific_info_.size());
145 
146  // 6 bit stream type. The last bit is reserved with 1.
147  const uint8_t stream_type = (kAudioStreamType << 2) | 1;
148  const uint8_t decoder_config_size =
149  static_cast<uint8_t>(decoder_specific_info_size + kHeaderSize +
150  sizeof(uint8_t) + // object_type_.
151  sizeof(stream_type) +
152  kEmptyDecodingBufferSize.size() +
153  sizeof(kUnknownBitrate) * 2);
154 
155  const uint8_t sl_config_size = sizeof(uint8_t); // predefined.
156  const uint8_t es_size = decoder_config_size + kHeaderSize + sl_config_size +
157  kHeaderSize + sizeof(esid_) + sizeof(kNoEsFlags);
158 
159  writer->AppendInt(static_cast<uint8_t>(kESDescrTag));
160  writer->AppendInt(es_size);
161  writer->AppendInt(esid_);
162  writer->AppendInt(kNoEsFlags);
163 
164  writer->AppendInt(static_cast<uint8_t>(kDecoderConfigDescrTag));
165  writer->AppendInt(decoder_config_size);
166  writer->AppendInt(static_cast<uint8_t>(object_type_));
167  writer->AppendInt(stream_type);
168  writer->AppendVector(kEmptyDecodingBufferSize);
169  writer->AppendInt(max_bitrate_);
170  writer->AppendInt(avg_bitrate_);
171 
172  writer->AppendInt(static_cast<uint8_t>(kDecoderSpecificInfoTag));
173  writer->AppendInt(decoder_specific_info_size);
174  writer->AppendVector(decoder_specific_info_);
175 
176  writer->AppendInt(static_cast<uint8_t>(kSLConfigTag));
177  writer->AppendInt(sl_config_size);
178  writer->AppendInt(static_cast<uint8_t>(kSLPredefinedMP4));
179 }
180 
181 size_t ESDescriptor::ComputeSize() const {
182  // A bit magical. Refer to ESDescriptor::Write for details.
183  const uint8_t decoder_specific_info_size =
184  static_cast<uint8_t>(decoder_specific_info_.size());
185  const uint8_t decoder_config_size = decoder_specific_info_size + kHeaderSize +
186  sizeof(uint8_t) * 5 +
187  sizeof(uint32_t) * 2;
188  const uint8_t sl_config_size = sizeof(uint8_t);
189  const uint8_t es_size = decoder_config_size + kHeaderSize + sl_config_size +
190  kHeaderSize + sizeof(esid_) + sizeof(uint8_t);
191  return es_size + kHeaderSize;
192 }
193 
194 } // namespace media
195 } // namespace shaka