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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/mp4/mp4_stream_parser.h"
#include "base/callback.h"
#include "base/logging.h"
#include "base/time.h"
#include "media/base/audio_decoder_config.h"
#include "media/base/stream_parser_buffer.h"
#include "media/base/video_decoder_config.h"
#include "media/mp4/box_definitions.h"
#include "media/mp4/box_reader.h"
#include "media/mp4/rcheck.h"
namespace media {
namespace mp4 {
MP4StreamParser::MP4StreamParser()
: state_(kWaitingForInit),
moof_head_(0),
mdat_tail_(0),
has_audio_(false),
has_video_(false) {
}
MP4StreamParser::~MP4StreamParser() {}
void MP4StreamParser::Init(const InitCB& init_cb,
const NewConfigCB& config_cb,
const NewBuffersCB& audio_cb,
const NewBuffersCB& video_cb,
const NeedKeyCB& need_key_cb,
const NewMediaSegmentCB& new_segment_cb) {
DCHECK_EQ(state_, kWaitingForInit);
DCHECK(init_cb_.is_null());
DCHECK(!init_cb.is_null());
DCHECK(!config_cb.is_null());
DCHECK(!audio_cb.is_null() || !video_cb.is_null());
DCHECK(!need_key_cb.is_null());
ChangeState(kParsingBoxes);
init_cb_ = init_cb;
config_cb_ = config_cb;
audio_cb_ = audio_cb;
video_cb_ = video_cb;
need_key_cb_ = need_key_cb;
new_segment_cb_ = new_segment_cb;
}
void MP4StreamParser::Flush() {
DCHECK_NE(state_, kWaitingForInit);
queue_.Reset();
moof_head_ = 0;
mdat_tail_ = 0;
}
bool MP4StreamParser::Parse(const uint8* buf, int size) {
DCHECK_NE(state_, kWaitingForInit);
if (state_ == kError)
return false;
queue_.Push(buf, size);
BufferQueue audio_buffers;
BufferQueue video_buffers;
bool result, err = false;
do {
if (state_ == kParsingBoxes) {
if (mdat_tail_ > queue_.head()) {
result = queue_.Trim(mdat_tail_);
} else {
result = ParseBox(&err);
}
} else {
DCHECK_EQ(kEmittingSamples, state_);
result = EnqueueSample(&audio_buffers, &video_buffers, &err);
if (result) {
int64 max_clear = runs_.GetMaxClearOffset() + moof_head_;
DCHECK(max_clear <= queue_.tail());
err = !(ReadMDATsUntil(max_clear) && queue_.Trim(max_clear));
}
}
} while (result && !err);
if (err) {
DLOG(ERROR) << "Unknown error while parsing MP4";
queue_.Reset();
moov_.reset();
ChangeState(kError);
return false;
}
if (!audio_buffers.empty() &&
(audio_cb_.is_null() || !audio_cb_.Run(audio_buffers)))
return false;
if (!video_buffers.empty() &&
(video_cb_.is_null() || !video_cb_.Run(video_buffers)))
return false;
return true;
}
bool MP4StreamParser::ParseBox(bool* err) {
const uint8* buf;
int size;
queue_.Peek(&buf, &size);
if (!size) return false;
scoped_ptr<BoxReader> reader(BoxReader::ReadTopLevelBox(buf, size, err));
if (reader.get() == NULL) return false;
if (reader->type() == FOURCC_MOOV) {
*err = !ParseMoov(reader.get());
} else if (reader->type() == FOURCC_MOOF) {
moof_head_ = queue_.head();
*err = !ParseMoof(reader.get());
// Set up first mdat offset for ParseMDATsUntil()
mdat_tail_ = queue_.head() + reader->size();
} else {
DVLOG(2) << "Skipping unrecognized top-level box: "
<< FourCCToString(reader->type());
}
queue_.Pop(reader->size());
return !(*err);
}
bool MP4StreamParser::ParseMoov(BoxReader* reader) {
// TODO(strobe): Respect edit lists.
moov_.reset(new Movie);
RCHECK(moov_->Parse(reader));
has_audio_ = false;
has_video_ = false;
parameter_sets_inserted_ = false;
AudioDecoderConfig audio_config;
VideoDecoderConfig video_config;
for (std::vector<Track>::const_iterator track = moov_->tracks.begin();
track != moov_->tracks.end(); ++track) {
// TODO(strobe): Only the first audio and video track present in a file are
// used. (Track selection is better accomplished via Source IDs, though, so
// adding support for track selection within a stream is low-priority.)
const SampleDescription& samp_descr =
track->media.information.sample_table.description;
if (track->media.handler.type == kAudio && !audio_config.IsValidConfig()) {
RCHECK(!samp_descr.audio_entries.empty());
const AudioSampleEntry& entry = samp_descr.audio_entries[0];
// TODO(strobe): We accept all format values, pending clarification on
// the formats used for encrypted media (http://crbug.com/132351).
// RCHECK(entry.format == FOURCC_MP4A ||
// (entry.format == FOURCC_ENCA &&
// entry.sinf.format.format == FOURCC_MP4A));
const ChannelLayout layout =
AVC::ConvertAACChannelCountToChannelLayout(entry.channelcount);
audio_config.Initialize(kCodecAAC, entry.samplesize, layout,
entry.samplerate, NULL, 0, false);
has_audio_ = true;
audio_track_id_ = track->header.track_id;
}
if (track->media.handler.type == kVideo && !video_config.IsValidConfig()) {
RCHECK(!samp_descr.video_entries.empty());
const VideoSampleEntry& entry = samp_descr.video_entries[0];
// RCHECK(entry.format == FOURCC_AVC1 ||
// (entry.format == FOURCC_ENCV &&
// entry.sinf.format.format == FOURCC_AVC1));
// TODO(strobe): Recover correct crop box and pixel aspect ratio
video_config.Initialize(kCodecH264, H264PROFILE_MAIN, VideoFrame::YV12,
gfx::Size(entry.width, entry.height),
gfx::Rect(0, 0, entry.width, entry.height),
// Bogus duration used for framerate, since real
// framerate may be variable
1000, track->media.header.timescale,
1, 1,
// No decoder-specific buffer needed for AVC;
// SPS/PPS are embedded in the video stream
NULL, 0, false);
has_video_ = true;
video_track_id_ = track->header.track_id;
size_of_nalu_length_ = entry.avcc.length_size;
}
}
RCHECK(config_cb_.Run(audio_config, video_config));
base::TimeDelta duration;
if (moov_->extends.header.fragment_duration > 0) {
duration = TimeDeltaFromFrac(moov_->extends.header.fragment_duration,
moov_->header.timescale);
} else if (moov_->header.duration > 0) {
duration = TimeDeltaFromFrac(moov_->header.duration,
moov_->header.timescale);
} else {
duration = kInfiniteDuration();
}
init_cb_.Run(true, duration);
return true;
}
bool MP4StreamParser::ParseMoof(BoxReader* reader) {
RCHECK(moov_.get()); // Must already have initialization segment
MovieFragment moof;
RCHECK(moof.Parse(reader));
RCHECK(runs_.Init(*moov_, moof));
new_segment_cb_.Run(runs_.GetMinDecodeTimestamp());
ChangeState(kEmittingSamples);
return true;
}
bool MP4StreamParser::EnqueueSample(BufferQueue* audio_buffers,
BufferQueue* video_buffers,
bool* err) {
if (!runs_.RunValid()) {
ChangeState(kParsingBoxes);
return true;
}
if (!runs_.SampleValid()) {
runs_.AdvanceRun();
return true;
}
DCHECK(!(*err));
const uint8* buf;
int size;
queue_.Peek(&buf, &size);
if (!size) return false;
bool audio = has_audio_ && audio_track_id_ == runs_.track_id();
bool video = has_video_ && video_track_id_ == runs_.track_id();
// Skip this entire track if it's not one we're interested in
if (!audio && !video) runs_.AdvanceRun();
// Attempt to cache the auxiliary information first. Aux info is usually
// placed in a contiguous block before the sample data, rather than being
// interleaved. If we didn't cache it, this would require that we retain the
// start of the segment buffer while reading samples. Aux info is typically
// quite small compared to sample data, so this pattern is useful on
// memory-constrained devices where the source buffer consumes a substantial
// portion of the total system memory.
if (runs_.NeedsCENC()) {
queue_.PeekAt(runs_.cenc_offset() + moof_head_, &buf, &size);
return runs_.CacheCENC(buf, size);
}
queue_.PeekAt(runs_.offset() + moof_head_, &buf, &size);
if (size < runs_.size()) return false;
std::vector<uint8> frame_buf(buf, buf + runs_.size());
if (video) {
RCHECK(AVC::ConvertToAnnexB(size_of_nalu_length_, &frame_buf));
if (!parameter_sets_inserted_) {
const AVCDecoderConfigurationRecord* avc_config = NULL;
for (size_t t = 0; t < moov_->tracks.size(); t++) {
if (moov_->tracks[t].header.track_id == runs_.track_id()) {
avc_config = &moov_->tracks[t].media.information.
sample_table.description.video_entries[0].avcc;
break;
}
}
RCHECK(avc_config != NULL);
RCHECK(AVC::InsertParameterSets(*avc_config, &frame_buf));
parameter_sets_inserted_ = true;
}
}
scoped_refptr<StreamParserBuffer> stream_buf =
StreamParserBuffer::CopyFrom(&frame_buf[0], frame_buf.size(),
runs_.is_keyframe());
stream_buf->SetDuration(runs_.duration());
// We depend on the decoder performing frame reordering without reordering
// timestamps, and only provide the decode timestamp in the buffer.
stream_buf->SetTimestamp(runs_.dts());
DVLOG(3) << "Pushing frame: aud=" << audio
<< ", key=" << runs_.is_keyframe()
<< ", dur=" << runs_.duration().InMilliseconds()
<< ", dts=" << runs_.dts().InMilliseconds()
<< ", size=" << runs_.size();
if (audio) {
audio_buffers->push_back(stream_buf);
} else {
video_buffers->push_back(stream_buf);
}
runs_.AdvanceSample();
return true;
}
bool MP4StreamParser::ReadMDATsUntil(const int64 tgt_offset) {
DCHECK(tgt_offset <= queue_.tail());
while (mdat_tail_ < tgt_offset) {
const uint8* buf;
int size;
queue_.PeekAt(mdat_tail_, &buf, &size);
FourCC type;
int box_sz;
bool err;
if (!BoxReader::StartTopLevelBox(buf, size, &type, &box_sz, &err))
return false;
if (type != FOURCC_MDAT) {
DLOG(WARNING) << "Unexpected type while parsing MDATs: "
<< FourCCToString(type);
}
mdat_tail_ += box_sz;
}
return true;
}
void MP4StreamParser::ChangeState(State new_state) {
DVLOG(2) << "Changing state: " << new_state;
state_ = new_state;
}
} // namespace mp4
} // namespace media
|