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