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/formats/mp4/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 namespace mp4 {
66 
67 ESDescriptor::ESDescriptor()
68  : esid_(0),
69  object_type_(kForbidden),
70  max_bitrate_(kUnknownBitrate),
71  avg_bitrate_(kUnknownBitrate) {}
72 
73 ESDescriptor::~ESDescriptor() {}
74 
75 bool ESDescriptor::Parse(const std::vector<uint8_t>& data) {
76  BitReader reader(&data[0], data.size());
77  uint8_t tag;
78  uint32_t size;
79  uint8_t stream_dependency_flag;
80  uint8_t url_flag;
81  uint8_t ocr_stream_flag;
82  uint16_t dummy;
83 
84  RCHECK(reader.ReadBits(8, &tag));
85  RCHECK(tag == kESDescrTag);
86  RCHECK(ReadESSize(&reader, &size));
87 
88  RCHECK(reader.ReadBits(16, &esid_)); // ES_ID
89  RCHECK(reader.ReadBits(1, &stream_dependency_flag));
90  RCHECK(reader.ReadBits(1, &url_flag));
91  RCHECK(!url_flag); // We don't support url flag
92  RCHECK(reader.ReadBits(1, &ocr_stream_flag));
93  RCHECK(reader.ReadBits(5, &dummy)); // streamPriority
94 
95  if (stream_dependency_flag)
96  RCHECK(reader.ReadBits(16, &dummy)); // dependsOn_ES_ID
97  if (ocr_stream_flag)
98  RCHECK(reader.ReadBits(16, &dummy)); // OCR_ES_Id
99 
100  RCHECK(ParseDecoderConfigDescriptor(&reader));
101 
102  return true;
103 }
104 
105 bool ESDescriptor::ParseDecoderConfigDescriptor(BitReader* reader) {
106  uint8_t tag;
107  uint32_t size;
108  uint32_t dummy;
109 
110  RCHECK(reader->ReadBits(8, &tag));
111  RCHECK(tag == kDecoderConfigDescrTag);
112  RCHECK(ReadESSize(reader, &size));
113 
114  RCHECK(reader->ReadBits(8, &object_type_));
115  RCHECK(reader->ReadBits(32, &dummy));
116  RCHECK(reader->ReadBits(32, &max_bitrate_));
117  RCHECK(reader->ReadBits(32, &avg_bitrate_));
118  RCHECK(ParseDecoderSpecificInfo(reader));
119 
120  return true;
121 }
122 
123 bool ESDescriptor::ParseDecoderSpecificInfo(BitReader* reader) {
124  DCHECK(reader);
125  uint8_t tag;
126  uint32_t size;
127 
128  RCHECK(reader->ReadBits(8, &tag));
129  RCHECK(tag == kDecoderSpecificInfoTag);
130  RCHECK(ReadESSize(reader, &size));
131 
132  decoder_specific_info_.resize(size);
133  for (uint32_t i = 0; i < size; ++i)
134  RCHECK(reader->ReadBits(8, &decoder_specific_info_[i]));
135  return true;
136 }
137 
138 void ESDescriptor::Write(BufferWriter* writer) const {
139  DCHECK(writer);
140  CHECK_LT(decoder_specific_info_.size(), kMaxDecoderSpecificInfoSize);
141 
142  const std::vector<uint8_t> kEmptyDecodingBufferSize(3, 0);
143  const uint8_t kNoEsFlags = 0;
144 
145  const uint8_t decoder_specific_info_size = decoder_specific_info_.size();
146 
147  // 6 bit stream type. The last bit is reserved with 1.
148  const uint8_t stream_type = (kAudioStreamType << 2) | 1;
149  const uint8_t decoder_config_size = 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 = decoder_specific_info_.size();
184  const uint8_t decoder_config_size = decoder_specific_info_size + kHeaderSize +
185  sizeof(uint8_t) * 5 +
186  sizeof(uint32_t) * 2;
187  const uint8_t sl_config_size = sizeof(uint8_t);
188  const uint8_t es_size = decoder_config_size + kHeaderSize + sl_config_size +
189  kHeaderSize + sizeof(esid_) + sizeof(uint8_t);
190  return es_size + kHeaderSize;
191 }
192 
193 } // namespace mp4
194 
195 } // namespace media
196 } // namespace shaka