-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmdhd.cc
102 lines (94 loc) · 2.93 KB
/
mdhd.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
//
// Created by wlanjie on 2018/3/9.
//
#include "mdhd.h"
#include "utils.h"
namespace mp4 {
Mdhd *Mdhd::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 > 1) {
return nullptr;
}
return new Mdhd(size, version, flags, stream);
}
Mdhd::Mdhd(UI32 creationTime,
UI32 modificationTime,
UI32 timeScale,
UI64 duration,
const char *language) :
Atom(ATOM_TYPE_MDHD, FULL_ATOM_HEADER_SIZE + 20, 0, 0),
creationTime(creationTime),
modificationTime(modificationTime),
timeScale(timeScale),
duration(duration) {
this->language.assign(language, 3);
if (duration > 0xFFFFFFFF) {
version = 1;
size32 += 12;
}
}
Mdhd::Mdhd(UI32 size, UI08 version, UI32 flags, ByteStream &stream) :
Atom(ATOM_TYPE_MDHD, size, version, flags) {
if (this->version == 0) {
UI32 creationTime;
stream.readUI32(creationTime);
this->creationTime = creationTime;
UI32 modificationTime;
stream.readUI32(modificationTime);
this->modificationTime = modificationTime;
stream.readUI32(timeScale);
UI32 duration;
stream.readUI32(duration);
this->duration = duration;
} else {
stream.readUI64(creationTime);
stream.readUI64(modificationTime);
stream.readUI32(timeScale);
stream.readUI64(duration);
};
unsigned char lang[2];
stream.read(lang, 2);
char l0 = ((lang[0]>>2)&0x1F);
char l1 = (((lang[0]&0x3)<<3) | ((lang[1]>>5)&0x7));
char l2 = ((lang[1]&0x1F));
if (l0 && l1 && l2) {
char lang_str[3] = {(char)(l0+0x60), (char)(l1+0x60), (char)(l2+0x60)};
language.assign(lang_str, 3);
} else {
language.assign("```", 3);
}
}
Result Mdhd::writeFields(ByteStream &stream) {
Result result;
if (version == 0) {
result = stream.writeUI32((UI32) creationTime);
if (FAILED(result)) return result;
result = stream.writeUI32((UI32) modificationTime);
if (FAILED(result)) return result;
result = stream.writeUI32(timeScale);
if (FAILED(result)) return result;
result = stream.writeUI32((UI32) duration);
if (FAILED(result)) return result;
} else {
result = stream.writeUI64(creationTime);
if (FAILED(result)) return result;
result = stream.writeUI64(modificationTime);
if (FAILED(result)) return result;
result = stream.writeUI32(timeScale);
if (FAILED(result)) return result;
result = stream.writeUI64(duration);
if (FAILED(result)) return result;
}
return 0;
}
UI32 Mdhd::getDurationMs() const {
return durationMsFromUnits(duration, timeScale);
}
}