-
Notifications
You must be signed in to change notification settings - Fork 4
/
stsc.cc
139 lines (127 loc) · 4.43 KB
/
stsc.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
134
135
136
137
138
139
//
// Created by wlanjie on 2018/3/10.
//
#include "stsc.h"
#include "utils.h"
namespace mp4 {
Stsc *Stsc::create(Size size, ByteStream &stream) {
UI08 version;
UI32 flags;
if (size < FULL_ATOM_HEADER_SIZE) {
return nullptr;
}
if (FAILED(Atom::readFullHeader(stream, version, flags))) {
return nullptr;
}
if (version != 0) {
return nullptr;
}
return new Stsc(size, version, flags, stream);
}
Stsc::Stsc() : Atom(ATOM_TYPE_STSC, FULL_ATOM_HEADER_SIZE + 4, 0, 0), cachedChunkGroup(0) {
}
Stsc::Stsc(UI32 size, UI08 version, UI32 flags, ByteStream &stream) :
Atom(ATOM_TYPE_STSC, size, version, flags),
cachedChunkGroup(0) {
UI32 firstSample = 1;
UI32 entryCount;
if (size - ATOM_HEADER_SIZE < 4) {
return;
}
stream.readUI32(entryCount);
if ((size - ATOM_HEADER_SIZE - 4) / 12 < entryCount) {
return;
}
entries.SetItemCount(entryCount);
unsigned char* buffer = new unsigned char[entryCount * 12];
Result result = stream.read(buffer, entryCount * 12);
if (FAILED(result)) {
delete[] buffer;
return;
}
for (unsigned int i = 0; i < entryCount; i++) {
UI32 firstChunk = bytesToUInt32BE(&buffer[i * 12]);
UI32 samplesPerChunk = bytesToUInt32BE(&buffer[i * 12 + 4]);
UI32 sampleDescriptionIndex = bytesToUInt32BE(&buffer[i * 12 + 8]);
if (i) {
Ordinal prev = i - 1;
entries[prev].chunkCount = firstChunk - entries[prev].firstChunk;
firstSample += entries[prev].chunkCount * entries[prev].samplesPerChunk;
}
entries[i].chunkCount = 0;
entries[i].firstChunk = firstChunk;
entries[i].firstSample = firstSample;
entries[i].samplesPerChunk = samplesPerChunk;
entries[i].sampleDescriptionIndex = sampleDescriptionIndex;
}
delete[] buffer;
}
Result Stsc::writeFields(ByteStream &stream) {
Cardinal entryCount = entries.ItemCount();
Result result = stream.writeUI32(entryCount);
for (Ordinal i = 0; i < entryCount; i++) {
result = stream.writeUI32(entries[i].firstChunk);
if (FAILED(result)) {
return result;
}
result = stream.writeUI32(entries[i].samplesPerChunk);
if (FAILED(result)) {
return result;
}
result = stream.writeUI32(entries[i].sampleDescriptionIndex);
if (FAILED(result)) {
return result;
}
}
return 0;
}
Result Stsc::getChunkForSample(Ordinal sample, Ordinal &chunk, Ordinal &skip, Ordinal &sampleDescriptionIndex) {
ASSERT(sample > 0);
Ordinal group;
if (cachedChunkGroup < entries.ItemCount() && entries[cachedChunkGroup].firstSample <= sample) {
group = cachedChunkGroup;
} else {
group = 0;
}
while (group < entries.ItemCount()) {
Cardinal sampleCount = entries[group].chunkCount * entries[group].samplesPerChunk;
if (sampleCount == 0) {
if (entries[group].firstSample > sample) {
return ERROR_INVALID_FORMAT;
}
} else {
if (entries[group].firstSample + sampleCount <= sample) {
group++;
continue;
}
}
if (entries[group].samplesPerChunk == 0) {
return ERROR_INVALID_FORMAT;
}
unsigned int chunkOffset = ((sample - entries[group].firstSample) / entries[group].samplesPerChunk);
chunk = entries[group].firstChunk + chunkOffset;
skip = sample - (entries[group].firstSample + entries[group].samplesPerChunk * chunkOffset);
sampleDescriptionIndex = entries[group].sampleDescriptionIndex;
cachedChunkGroup = group;
return SUCCESS;
}
return 0;
}
Result Stsc::addEntry(Cardinal chunkCount, Cardinal samplePerChunk, Ordinal sampleDescriptionIndex) {
Ordinal firstChunk;
Ordinal firstSample;
Cardinal entryCount = entries.ItemCount();
if (entryCount == 0) {
firstChunk = 1;
firstSample = 1;
} else {
firstChunk = entries[entryCount - 1].firstChunk + entries[entryCount - 1].chunkCount;
firstSample = entries[entryCount - 1].firstChunk +
entries[entryCount - 1].chunkCount *
entries[entryCount - 1].samplesPerChunk;
}
entries.Append(StscTableEntry(firstChunk, firstSample, chunkCount, samplePerChunk, sampleDescriptionIndex));
size32 += 12;
return SUCCESS;
}
}