-
Notifications
You must be signed in to change notification settings - Fork 4
/
sampletable.cc
125 lines (118 loc) · 3.87 KB
/
sampletable.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
//
// Created by wlanjie on 2018/3/10.
//
#include "sampletable.h"
#include "stsd.h"
#include "stts.h"
#include "stsc.h"
#include "stsz.h"
#include "stss.h"
#include "ctts.h"
#include "stco.h"
#include "co64.h"
namespace mp4 {
Result SampleTable::generateStbl(Container *&stbl) {
stbl = new Container(ATOM_TYPE_STBL);
auto* stsd = new Stsd(this);
auto* stts = new Stts();
auto* stsc = new Stsc();
auto* stsz = new Stsz();
auto* stss = new Stss();
Ctts* ctts = nullptr;
Ordinal currentChunkIndex = 0;
Size currentChunkSize = 0;
Position currentChunkOffset = 0;
Cardinal currentSamplesInChunk = 0;
Ordinal currentSampleDescriptionIndex = 0;
UI32 currentDuration = 0;
Cardinal currentDurationRun = 0;
UI32 currentCtsDelta = 0;
Cardinal currentCtsDeltaRun = 0;
Array<Position> chunkOffsets;
bool allSampleAreSync = false;
Cardinal sampleCount = getSampleCount();
for (Ordinal i = 0; i < sampleCount; i++) {
Sample sample;
getSample(i, sample);
UI32 newDuration = sample.getDuration();
if (newDuration != currentDuration && currentDurationRun != 0) {
stts->addEntry(currentDurationRun, currentDuration);
currentDurationRun = 0;
}
++currentDurationRun;
currentDuration = newDuration;
UI32 newCtsDelta = sample.getCtsDelta();
if (newCtsDelta != currentCtsDelta && currentCtsDeltaRun != 0) {
if (ctts == nullptr) {
ctts = new Ctts();
}
ctts->addEntry(currentCtsDeltaRun, currentCtsDelta);
currentCtsDeltaRun = 0;
}
++currentCtsDeltaRun;
currentCtsDelta = newCtsDelta;
stsz->addEntry(sample.getSize());
if (sample.isSync()) {
stss->addEntry(i + 1);
if (i == 0) {
allSampleAreSync = true;
}
} else {
allSampleAreSync = false;
}
Ordinal chunkIndex = 0;
Ordinal positionInChunk = 0;
Result result = getSampleChunkPosition(i, chunkIndex, positionInChunk);
if (SUCCEEDED(result)) {
if (chunkIndex != currentChunkIndex && currentSamplesInChunk != 0) {
chunkOffsets.Append(currentChunkOffset);
currentChunkOffset += currentChunkSize;
stsc->addEntry(1, currentSamplesInChunk, currentSampleDescriptionIndex + 1);
currentSamplesInChunk = 0;
currentChunkSize = 0;
}
currentChunkIndex = chunkIndex;
}
currentSampleDescriptionIndex = sample.getDescriptionIndex();
currentChunkSize += sample.getSize();
++currentSamplesInChunk;
}
if (sampleCount) {
stts->addEntry(currentDurationRun, currentDuration);
}
if (ctts) {
ASSERT(currentCtsDeltaRun != 0);
ctts->addEntry(currentCtsDeltaRun, currentCtsDelta);
}
if (currentSamplesInChunk != 0) {
chunkOffsets.Append(currentChunkOffset);
stsc->addEntry(1, currentSamplesInChunk, currentSampleDescriptionIndex + 1);
}
stbl->addChild(stsd);
stbl->addChild(stts);
if (ctts) {
stbl->addChild(ctts);
}
stbl->addChild(stsc);
stbl->addChild(stsz);
if (!allSampleAreSync && stss->getEntries().ItemCount() != 0) {
stbl->addChild(stss);
} else {
delete stss;
}
Size chunkCount = chunkOffsets.ItemCount();
if (currentChunkOffset <= 0xFFFFFFFF) {
UI32* chunkOffset32 = new UI32[chunkCount];
for (unsigned int i = 0; i < chunkCount; i++) {
chunkOffset32[i] = (UI32) chunkOffsets[i];
}
Stco* stco = new Stco(&chunkOffset32[0], chunkCount);
stbl->addChild(stco);
delete[] chunkOffset32;
} else {
Co64* co64 = new Co64(&chunkOffsets[0], chunkCount);
stbl->addChild(co64);
}
return SUCCESS;
}
}