Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(509)

Side by Side Diff: media/mp4/box_reader.cc

Issue 10536014: Implement ISO BMFF support in Media Source. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "media/mp4/box_reader.h"
6
7 #include "base/logging.h"
8 #include "media/mp4/rcheck.h"
acolwell GONE FROM CHROMIUM 2012/06/06 17:32:39 alphabetical order
strobe_ 2012/06/07 16:33:54 Done.
9 #include "media/mp4/box_definitions.h"
10 #include <string.h>
acolwell GONE FROM CHROMIUM 2012/06/06 17:32:39 system headers after box_reader.h
strobe_ 2012/06/07 16:33:54 Done.
11 #include <algorithm>
12 #include <map>
13 #include <set>
14
15 namespace media {
16 namespace mp4 {
17
18 bool BufferReader::Read(uint8* v) {
19 RCHECK(HasBytes(1));
20 *v = buf_[pos_++];
21 return true;
22 }
23
24 bool BufferReader::Read(int8* v) {
25 return Read(reinterpret_cast<uint8*>(v));
26 }
27
28 // Internal implementation of multi-byte reads
29 template<typename T> bool BufferReader::Read_(T* v) {
30 RCHECK(HasBytes(sizeof(T)));
31
32 T tmp = 0;
33 for (size_t i = 0; i < sizeof(T); i++) {
34 tmp <<= 8;
35 tmp += buf_[pos_++];
36 }
37 *v = tmp;
38 return true;
39 }
40
41 bool BufferReader::Read(int16* v) { return Read_(v); }
42 bool BufferReader::Read(int32* v) { return Read_(v); }
43 bool BufferReader::Read(int64* v) { return Read_(v); }
44 bool BufferReader::Read(uint16* v) { return Read_(v); }
45 bool BufferReader::Read(uint32* v) { return Read_(v); }
46 bool BufferReader::Read(uint64* v) { return Read_(v); }
47
48 bool BufferReader::Read(std::vector<uint8>* t, size_t count) {
49 RCHECK(HasBytes(count));
50 t->clear();
51 t->insert(t->end(), buf_ + pos_, buf_ + pos_ + count);
52 pos_ += count;
53 return true;
54 }
55
56 bool BufferReader::Skip(size_t bytes) {
57 RCHECK(HasBytes(bytes));
58 pos_ += bytes;
59 return true;
60 }
61
62 bool BufferReader::Read4(uint64* v) {
63 uint32 tmp;
64 RCHECK(Read(&tmp));
65 *v = tmp;
66 return true;
67 }
68
69 bool BufferReader::Read4(int64* v) {
70 return Read4(reinterpret_cast<uint64*>(v));
71 }
72
73
74 BoxReader::BoxReader(const uint8* buf, const uint64 size)
75 : BufferReader(buf, size), scanned_(false) {};
76
77 BoxReader::~BoxReader() {
78 if (scanned_ && !children_.empty()) {
79 for (auto itr = children_.begin(); itr != children_.end(); ++itr) {
80 DVLOG(1) << "Skipping unknown box: " << FourCCToString(itr->first);
81 }
82 }
83 }
84
85 BoxReader* BoxReader::ReadTopLevelBox(const uint8* buf,
86 const size_t buf_size,
87 bool* err) {
88 BoxReader* r = new BoxReader(buf, buf_size);
89 if (r->ReadHeader(err) && r->size() <= buf_size) {
90 return r;
91 } else {
acolwell GONE FROM CHROMIUM 2012/06/06 17:32:39 drop else to avoid a level of indent
strobe_ 2012/06/07 16:33:54 Done.
92 delete r;
93 return NULL;
94 }
95 }
96
97 bool BoxReader::StartTopLevelBox(const uint8* buf,
98 const size_t buf_size,
99 FourCC* type,
100 size_t* box_size,
101 bool* err) {
102 BoxReader r(buf, buf_size);
103 if (!r.ReadHeader(err)) return false;
104 *type = r.type();
105 *box_size = r.size();
106 return true;
107 }
108
109 bool BoxReader::ScanChildren() {
110 DCHECK(!scanned_);
111 scanned_ = true;
112
113 bool err;
114 // TODO(strobe): Check or correct for multimap not inserting elements in
115 // consistent order.
116 while (pos() < size()) {
117 BoxReader child(&buf_[pos_], size_ - pos_);
118 if (!child.ReadHeader(&err)) break;
119
120 children_.insert(std::pair<FourCC, BoxReader>(child.type(), child));
121 pos_ += child.size();
122 }
123
124 DCHECK(!err);
125 return !err && pos() == size();
126 }
127
128 bool BoxReader::ReadFullBoxHeader() {
129 uint32 vflags;
130 RCHECK(Read(&vflags));
131 version_ = vflags >> 24;
132 flags_ = vflags & 0xffffff;
133 return true;
134 }
135
136 bool BoxReader::ReadHeader(bool* err) {
137 uint64 size;
138 *err = false;
139
140 if (!HasBytes(8)) return false;
141 DCHECK(Read4(&size) && Read(&type_));
acolwell GONE FROM CHROMIUM 2012/06/06 17:32:39 I believe you want CHECK here and below. Otherwise
strobe_ 2012/06/07 16:33:54 Done.
142
143 if (size == 0) {
acolwell GONE FROM CHROMIUM 2012/06/06 17:32:39 Break this if/else chain up by early returning mor
strobe_ 2012/06/07 16:33:54 Done.
144 // Media Source specific: we do not support boxes that run to EOS.
145 *err = true;
146 } else if (size == 1) {
147 if (!HasBytes(8)) return false;
148 DCHECK(Read(&size));
149 if (size < 16) {
acolwell GONE FROM CHROMIUM 2012/06/06 17:32:39 remove {}
strobe_ 2012/06/07 16:33:54 Done.
150 *err = true;
151 }
152 } else {
153 if (size < 8) {
acolwell GONE FROM CHROMIUM 2012/06/06 17:32:39 remove {}
strobe_ 2012/06/07 16:33:54 Done.
154 *err = true;
155 }
156 }
157 // Note that the pos_ head has advanced to the byte immediately after the
158 // header, which is where we want it.
159 size_ = size;
160 return !*err;
161 }
162
163 } // namespace mp4
164 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698