-
Notifications
You must be signed in to change notification settings - Fork 4
/
es_descriptor.cc
133 lines (116 loc) · 3.41 KB
/
es_descriptor.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//
// Created by wlanjie on 2018/3/16.
//
#include "es_descriptor.h"
#include "descriptor_factory.h"
namespace mp4 {
EsDescriptor::EsDescriptor(UI16 esId) :
Descriptor(DESCRIPTOR_TAG_ES, 2, 2 + 1),
esId(esId),
ocrEsId(0),
flags(0),
streamPriority(0),
dependsOn(0) {
return;
}
EsDescriptor::EsDescriptor(ByteStream &stream, Size headerSize, Size payloadSize) :
Descriptor(DESCRIPTOR_TAG_ES, headerSize, payloadSize) {
Position start;
stream.tell(start);
// read descriptor fields
stream.readUI16(esId);
unsigned char bits;
stream.readUI08(bits);
flags = (bits >> 5) & 7;
streamPriority = bits & 0x1F;
if (flags & ES_DESCRIPTOR_FLAG_STREAM_DEPENDENCY) {
stream.readUI16(dependsOn);
} else {
dependsOn = 0;
}
if (flags & ES_DESCRIPTOR_FLAG_URL) {
unsigned char urlLength;
stream.readUI08(urlLength);
if (urlLength) {
auto* url = new char[urlLength + 1];
if (url) {
stream.read(url, urlLength);
url[urlLength] = '\0';
this->url = url;
delete[] url;
}
}
}
if (flags & ES_DESCRIPTOR_FLAG_URL) {
stream.readUI16(ocrEsId);
} else {
ocrEsId = 0;
}
// read other descriptors
Position offset;
stream.tell(offset);
auto* substream = new SubStream(stream, offset, payloadSize - Size(offset - start));
Descriptor* descriptor = nullptr;
while (DescriptorFactory::createDescriptionFromStream(*substream, descriptor) == SUCCESS) {
subDescriptors.add(descriptor);
}
substream->release();
}
EsDescriptor::~EsDescriptor() {
subDescriptors.deleteReferences();
}
Result EsDescriptor::addSubDescriptor(Descriptor *desciptor) {
subDescriptors.add(desciptor);
payloadSize += desciptor->getSize();
return SUCCESS;
}
Result EsDescriptor::writeFields(ByteStream &stream) {
Result result;
// es id
result = stream.writeUI16(esId);
if (FAILED(result)) {
return result;
}
// flags and other bits
UI08 bits = streamPriority | (UI08)(flags << 5);
result = stream.writeUI08(bits);
if (FAILED(result)) {
return result;
}
// optional fields
if (flags & ES_DESCRIPTOR_FLAG_STREAM_DEPENDENCY) {
result = stream.writeUI16(dependsOn);
if (FAILED(result)) {
return result;
}
}
if (flags & ES_DESCRIPTOR_FLAG_URL) {
result = stream.writeUI08((UI08) url.getLength());
if (FAILED(result)) {
return result;
}
result = stream.writeString(url.getChars());
if (FAILED(result)) {
return result;
}
result = stream.writeUI08(0);
if (FAILED(result)) {
return result;
}
}
if (flags & ES_DESCRIPTOR_FLAG_OCR_STREAM) {
result = stream.writeUI16(ocrEsId);
if (FAILED(result)) {
return result;
}
}
// write the sub descriptors
subDescriptors.apply(DescriptorListWriter(stream));
return SUCCESS;
}
const DecoderConfigDescriptor *EsDescriptor::getDecoderConfigDescription() const {
Descriptor* descriptor = nullptr;
auto result = subDescriptors.find(DescriptorFinder(DESCRIPTOR_TAG_DECODER_CONFIG), descriptor);
return SUCCEEDED(result) ? DYNAMIC_CAST(DecoderConfigDescriptor, descriptor) : nullptr;
}
}