DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs
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/formats/mp4/rcheck.h"
10 
11 namespace edash_packager {
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 
62 } // namespace
63 
64 namespace mp4 {
65 
66 ESDescriptor::ESDescriptor() : esid_(0), object_type_(kForbidden) {}
67 
68 ESDescriptor::~ESDescriptor() {}
69 
70 bool ESDescriptor::Parse(const std::vector<uint8_t>& data) {
71  BitReader reader(&data[0], data.size());
72  uint8_t tag;
73  uint32_t size;
74  uint8_t stream_dependency_flag;
75  uint8_t url_flag;
76  uint8_t ocr_stream_flag;
77  uint16_t dummy;
78 
79  RCHECK(reader.ReadBits(8, &tag));
80  RCHECK(tag == kESDescrTag);
81  RCHECK(ReadESSize(&reader, &size));
82 
83  RCHECK(reader.ReadBits(16, &esid_)); // ES_ID
84  RCHECK(reader.ReadBits(1, &stream_dependency_flag));
85  RCHECK(reader.ReadBits(1, &url_flag));
86  RCHECK(!url_flag); // We don't support url flag
87  RCHECK(reader.ReadBits(1, &ocr_stream_flag));
88  RCHECK(reader.ReadBits(5, &dummy)); // streamPriority
89 
90  if (stream_dependency_flag)
91  RCHECK(reader.ReadBits(16, &dummy)); // dependsOn_ES_ID
92  if (ocr_stream_flag)
93  RCHECK(reader.ReadBits(16, &dummy)); // OCR_ES_Id
94 
95  RCHECK(ParseDecoderConfigDescriptor(&reader));
96 
97  return true;
98 }
99 
100 bool ESDescriptor::ParseDecoderConfigDescriptor(BitReader* reader) {
101  uint8_t tag;
102  uint32_t size;
103  uint64_t dummy;
104 
105  RCHECK(reader->ReadBits(8, &tag));
106  RCHECK(tag == kDecoderConfigDescrTag);
107  RCHECK(ReadESSize(reader, &size));
108 
109  RCHECK(reader->ReadBits(8, &object_type_));
110  RCHECK(reader->ReadBits(64, &dummy));
111  RCHECK(reader->ReadBits(32, &dummy));
112  RCHECK(ParseDecoderSpecificInfo(reader));
113 
114  return true;
115 }
116 
117 bool ESDescriptor::ParseDecoderSpecificInfo(BitReader* reader) {
118  DCHECK(reader);
119  uint8_t tag;
120  uint32_t size;
121 
122  RCHECK(reader->ReadBits(8, &tag));
123  RCHECK(tag == kDecoderSpecificInfoTag);
124  RCHECK(ReadESSize(reader, &size));
125 
126  decoder_specific_info_.resize(size);
127  for (uint32_t i = 0; i < size; ++i)
128  RCHECK(reader->ReadBits(8, &decoder_specific_info_[i]));
129 
130  return true;
131 }
132 
133 void ESDescriptor::Write(BufferWriter* writer) const {
134  DCHECK(writer);
135  CHECK_LT(decoder_specific_info_.size(), kMaxDecoderSpecificInfoSize);
136 
137  const std::vector<uint8_t> kEmptyDecodingBufferSize(3, 0);
138  const uint32_t kUnknownBitrate = 0;
139  const uint8_t kNoEsFlags = 0;
140 
141  const uint8_t decoder_specific_info_size = decoder_specific_info_.size();
142 
143  // 6 bit stream type. The last bit is reserved with 1.
144  const uint8_t stream_type = (kAudioStreamType << 2) | 1;
145  const uint8_t decoder_config_size = decoder_specific_info_size + kHeaderSize +
146  sizeof(uint8_t) + // object_type_.
147  sizeof(stream_type) +
148  kEmptyDecodingBufferSize.size() +
149  sizeof(kUnknownBitrate) * 2;
150 
151  const uint8_t sl_config_size = sizeof(uint8_t); // predefined.
152  const uint8_t es_size = decoder_config_size + kHeaderSize + sl_config_size +
153  kHeaderSize + sizeof(esid_) + sizeof(kNoEsFlags);
154 
155  writer->AppendInt(static_cast<uint8_t>(kESDescrTag));
156  writer->AppendInt(es_size);
157  writer->AppendInt(esid_);
158  writer->AppendInt(kNoEsFlags);
159 
160  writer->AppendInt(static_cast<uint8_t>(kDecoderConfigDescrTag));
161  writer->AppendInt(decoder_config_size);
162  writer->AppendInt(static_cast<uint8_t>(object_type_));
163  writer->AppendInt(stream_type);
164  writer->AppendVector(kEmptyDecodingBufferSize);
165  writer->AppendInt(kUnknownBitrate); // max_bitrate.
166  writer->AppendInt(kUnknownBitrate); // avg_bitrate.
167 
168  writer->AppendInt(static_cast<uint8_t>(kDecoderSpecificInfoTag));
169  writer->AppendInt(decoder_specific_info_size);
170  writer->AppendVector(decoder_specific_info_);
171 
172  writer->AppendInt(static_cast<uint8_t>(kSLConfigTag));
173  writer->AppendInt(sl_config_size);
174  writer->AppendInt(static_cast<uint8_t>(kSLPredefinedMP4));
175 }
176 
177 size_t ESDescriptor::ComputeSize() const {
178  // A bit magical. Refer to ESDescriptor::Write for details.
179  const uint8_t decoder_specific_info_size = decoder_specific_info_.size();
180  const uint8_t decoder_config_size = decoder_specific_info_size + kHeaderSize +
181  sizeof(uint8_t) * 5 +
182  sizeof(uint32_t) * 2;
183  const uint8_t sl_config_size = sizeof(uint8_t);
184  const uint8_t es_size = decoder_config_size + kHeaderSize + sl_config_size +
185  kHeaderSize + sizeof(esid_) + sizeof(uint8_t);
186  return es_size + kHeaderSize;
187 }
188 
189 } // namespace mp4
190 
191 } // namespace media
192 } // namespace edash_packager