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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mp4_demuxer/Box.h"
#include "mp4_demuxer/Stream.h"
#include "mozilla/EndianUtils.h"
#include "mozilla/Unused.h"
#include <algorithm>
using namespace mozilla;
namespace mp4_demuxer {
// Limit reads to 32MiB max.
// static
const uint64_t Box::kMAX_BOX_READ = 32 * 1024 * 1024;
// Returns the offset from the start of the body of a box of type |aType|
// to the start of its first child.
static uint32_t
BoxOffset(AtomType aType)
{
const uint32_t FULLBOX_OFFSET = 4;
if (aType == AtomType("mp4a") || aType == AtomType("enca")) {
// AudioSampleEntry; ISO 14496-12, section 8.16
return 28;
} else if (aType == AtomType("mp4v") || aType == AtomType("encv")) {
// VideoSampleEntry; ISO 14496-12, section 8.16
return 78;
} else if (aType == AtomType("stsd")) {
// SampleDescriptionBox; ISO 14496-12, section 8.16
// This is a FullBox, and contains a |count| member before its child
// boxes.
return FULLBOX_OFFSET + 4;
}
return 0;
}
Box::Box(BoxContext* aContext, uint64_t aOffset, const Box* aParent)
: mContext(aContext), mParent(aParent)
{
uint8_t header[8];
if (aOffset > INT64_MAX - sizeof(header)) {
return;
}
MediaByteRange headerRange(aOffset, aOffset + sizeof(header));
if (mParent && !mParent->mRange.Contains(headerRange)) {
return;
}
const MediaByteRange* byteRange;
for (int i = 0; ; i++) {
if (i == mContext->mByteRanges.Length()) {
return;
}
byteRange = static_cast<const MediaByteRange*>(&mContext->mByteRanges[i]);
if (byteRange->Contains(headerRange)) {
break;
}
}
size_t bytes;
if (!mContext->mSource->CachedReadAt(aOffset, header, sizeof(header),
&bytes) ||
bytes != sizeof(header)) {
return;
}
uint64_t size = BigEndian::readUint32(header);
if (size == 1) {
uint8_t bigLength[8];
if (aOffset > INT64_MAX - sizeof(header) - sizeof(bigLength)) {
return;
}
MediaByteRange bigLengthRange(headerRange.mEnd,
headerRange.mEnd + sizeof(bigLength));
if ((mParent && !mParent->mRange.Contains(bigLengthRange)) ||
!byteRange->Contains(bigLengthRange) ||
!mContext->mSource->CachedReadAt(aOffset + sizeof(header), bigLength,
sizeof(bigLength), &bytes) ||
bytes != sizeof(bigLength)) {
return;
}
size = BigEndian::readUint64(bigLength);
mBodyOffset = bigLengthRange.mEnd;
} else if (size == 0) {
// box extends to end of file.
size = mContext->mByteRanges.LastInterval().mEnd - aOffset;
mBodyOffset = headerRange.mEnd;
} else {
mBodyOffset = headerRange.mEnd;
}
if (size > INT64_MAX) {
return;
}
int64_t end = static_cast<int64_t>(aOffset) + static_cast<int64_t>(size);
if (end < static_cast<int64_t>(aOffset)) {
// Overflowed.
return;
}
mType = BigEndian::readUint32(&header[4]);
mChildOffset = mBodyOffset + BoxOffset(mType);
MediaByteRange boxRange(aOffset, end);
if (mChildOffset > boxRange.mEnd ||
(mParent && !mParent->mRange.Contains(boxRange)) ||
!byteRange->Contains(boxRange)) {
return;
}
mRange = boxRange;
}
Box::Box()
: mContext(nullptr)
{}
Box
Box::Next() const
{
MOZ_ASSERT(IsAvailable());
return Box(mContext, mRange.mEnd, mParent);
}
Box
Box::FirstChild() const
{
MOZ_ASSERT(IsAvailable());
if (mChildOffset == mRange.mEnd) {
return Box();
}
return Box(mContext, mChildOffset, this);
}
nsTArray<uint8_t>
Box::Read()
{
nsTArray<uint8_t> out;
Unused << Read(&out, mRange);
return out;
}
bool
Box::Read(nsTArray<uint8_t>* aDest, const MediaByteRange& aRange)
{
int64_t length;
if (!mContext->mSource->Length(&length)) {
// The HTTP server didn't give us a length to work with.
// Limit the read to kMAX_BOX_READ max.
length = std::min(aRange.mEnd - mChildOffset, kMAX_BOX_READ);
} else {
length = aRange.mEnd - mChildOffset;
}
aDest->SetLength(length);
size_t bytes;
if (!mContext->mSource->CachedReadAt(mChildOffset, aDest->Elements(),
aDest->Length(), &bytes) ||
bytes != aDest->Length()) {
// Byte ranges are being reported incorrectly
NS_WARNING("Read failed in mp4_demuxer::Box::Read()");
aDest->Clear();
return false;
}
return true;
}
}
|