1 // Copyright 2014 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.
5 #include "media/formats/mp4/mp4_stream_parser.h"
7 #include "base/callback_helpers.h"
8 #include "base/logging.h"
9 #include "base/time/time.h"
10 #include "media/base/audio_decoder_config.h"
11 #include "media/base/stream_parser_buffer.h"
12 #include "media/base/text_track_config.h"
13 #include "media/base/timestamp_constants.h"
14 #include "media/base/video_decoder_config.h"
15 #include "media/base/video_util.h"
16 #include "media/formats/mp4/box_definitions.h"
17 #include "media/formats/mp4/box_reader.h"
18 #include "media/formats/mp4/es_descriptor.h"
19 #include "media/formats/mp4/rcheck.h"
20 #include "media/formats/mpeg/adts_constants.h"
25 MP4StreamParser::MP4StreamParser(const std::set
<int>& audio_object_types
,
27 : state_(kWaitingForInit
),
30 highest_end_offset_(0),
35 audio_object_types_(audio_object_types
),
37 is_audio_track_encrypted_(false),
38 is_video_track_encrypted_(false),
39 num_top_level_box_skipped_(0) {
42 MP4StreamParser::~MP4StreamParser() {}
44 void MP4StreamParser::Init(
45 const InitCB
& init_cb
,
46 const NewConfigCB
& config_cb
,
47 const NewBuffersCB
& new_buffers_cb
,
48 bool /* ignore_text_tracks */,
49 const EncryptedMediaInitDataCB
& encrypted_media_init_data_cb
,
50 const NewMediaSegmentCB
& new_segment_cb
,
51 const base::Closure
& end_of_segment_cb
,
52 const scoped_refptr
<MediaLog
>& media_log
) {
53 DCHECK_EQ(state_
, kWaitingForInit
);
54 DCHECK(init_cb_
.is_null());
55 DCHECK(!init_cb
.is_null());
56 DCHECK(!config_cb
.is_null());
57 DCHECK(!new_buffers_cb
.is_null());
58 DCHECK(!encrypted_media_init_data_cb
.is_null());
59 DCHECK(!end_of_segment_cb
.is_null());
61 ChangeState(kParsingBoxes
);
63 config_cb_
= config_cb
;
64 new_buffers_cb_
= new_buffers_cb
;
65 encrypted_media_init_data_cb_
= encrypted_media_init_data_cb
;
66 new_segment_cb_
= new_segment_cb
;
67 end_of_segment_cb_
= end_of_segment_cb
;
68 media_log_
= media_log
;
71 void MP4StreamParser::Reset() {
78 void MP4StreamParser::Flush() {
79 DCHECK_NE(state_
, kWaitingForInit
);
81 ChangeState(kParsingBoxes
);
84 bool MP4StreamParser::Parse(const uint8
* buf
, int size
) {
85 DCHECK_NE(state_
, kWaitingForInit
);
90 queue_
.Push(buf
, size
);
92 BufferQueue audio_buffers
;
93 BufferQueue video_buffers
;
100 case kWaitingForInit
:
106 result
= ParseBox(&err
);
109 case kWaitingForSampleData
:
110 result
= HaveEnoughDataToEnqueueSamples();
112 ChangeState(kEmittingSamples
);
115 case kEmittingSamples
:
116 result
= EnqueueSample(&audio_buffers
, &video_buffers
, &err
);
118 int64 max_clear
= runs_
->GetMaxClearOffset() + moof_head_
;
119 err
= !ReadAndDiscardMDATsUntil(max_clear
);
123 } while (result
&& !err
);
126 err
= !SendAndFlushSamples(&audio_buffers
, &video_buffers
);
129 DLOG(ERROR
) << "Error while parsing MP4";
139 bool MP4StreamParser::ParseBox(bool* err
) {
142 queue_
.Peek(&buf
, &size
);
143 if (!size
) return false;
145 scoped_ptr
<BoxReader
> reader(
146 BoxReader::ReadTopLevelBox(buf
, size
, media_log_
, err
));
147 if (reader
.get() == NULL
) return false;
149 if (reader
->type() == FOURCC_MOOV
) {
150 *err
= !ParseMoov(reader
.get());
151 } else if (reader
->type() == FOURCC_MOOF
) {
152 moof_head_
= queue_
.head();
153 *err
= !ParseMoof(reader
.get());
155 // Set up first mdat offset for ReadMDATsUntil().
156 mdat_tail_
= queue_
.head() + reader
->size();
158 // Return early to avoid evicting 'moof' data from queue. Auxiliary info may
159 // be located anywhere in the file, including inside the 'moof' itself.
160 // (Since 'default-base-is-moof' is mandated, no data references can come
161 // before the head of the 'moof', so keeping this box around is sufficient.)
164 // TODO(wolenetz,chcunningham): Enforce more strict adherence to MSE byte
165 // stream spec for ftyp and styp. See http://crbug.com/504514.
166 DVLOG(2) << "Skipping unrecognized top-level box: "
167 << FourCCToString(reader
->type());
170 queue_
.Pop(reader
->size());
174 bool MP4StreamParser::ParseMoov(BoxReader
* reader
) {
175 moov_
.reset(new Movie
);
176 RCHECK(moov_
->Parse(reader
));
182 AudioDecoderConfig audio_config
;
183 VideoDecoderConfig video_config
;
185 for (std::vector
<Track
>::const_iterator track
= moov_
->tracks
.begin();
186 track
!= moov_
->tracks
.end(); ++track
) {
187 // TODO(strobe): Only the first audio and video track present in a file are
188 // used. (Track selection is better accomplished via Source IDs, though, so
189 // adding support for track selection within a stream is low-priority.)
190 const SampleDescription
& samp_descr
=
191 track
->media
.information
.sample_table
.description
;
193 // TODO(strobe): When codec reconfigurations are supported, detect and send
194 // a codec reconfiguration for fragments using a sample description index
195 // different from the previous one
197 for (size_t t
= 0; t
< moov_
->extends
.tracks
.size(); t
++) {
198 const TrackExtends
& trex
= moov_
->extends
.tracks
[t
];
199 if (trex
.track_id
== track
->header
.track_id
) {
200 desc_idx
= trex
.default_sample_description_index
;
204 RCHECK(desc_idx
> 0);
205 desc_idx
-= 1; // BMFF descriptor index is one-based
207 if (track
->media
.handler
.type
== kAudio
&& !audio_config
.IsValidConfig()) {
208 RCHECK(!samp_descr
.audio_entries
.empty());
210 // It is not uncommon to find otherwise-valid files with incorrect sample
211 // description indices, so we fail gracefully in that case.
212 if (desc_idx
>= samp_descr
.audio_entries
.size())
214 const AudioSampleEntry
& entry
= samp_descr
.audio_entries
[desc_idx
];
215 const AAC
& aac
= entry
.esds
.aac
;
217 if (!(entry
.format
== FOURCC_MP4A
||
218 (entry
.format
== FOURCC_ENCA
&&
219 entry
.sinf
.format
.format
== FOURCC_MP4A
))) {
220 MEDIA_LOG(ERROR
, media_log_
) << "Unsupported audio format 0x"
221 << std::hex
<< entry
.format
226 uint8 audio_type
= entry
.esds
.object_type
;
227 DVLOG(1) << "audio_type " << std::hex
<< static_cast<int>(audio_type
);
228 if (audio_object_types_
.find(audio_type
) == audio_object_types_
.end()) {
229 MEDIA_LOG(ERROR
, media_log_
)
230 << "audio object type 0x" << std::hex
<< audio_type
231 << " does not match what is specified in the"
236 AudioCodec codec
= kUnknownAudioCodec
;
237 ChannelLayout channel_layout
= CHANNEL_LAYOUT_NONE
;
238 int sample_per_second
= 0;
239 std::vector
<uint8
> extra_data
;
240 // Check if it is MPEG4 AAC defined in ISO 14496 Part 3 or
241 // supported MPEG2 AAC varients.
242 if (ESDescriptor::IsAAC(audio_type
)) {
244 channel_layout
= aac
.GetChannelLayout(has_sbr_
);
245 sample_per_second
= aac
.GetOutputSamplesPerSecond(has_sbr_
);
246 #if defined(OS_ANDROID)
247 extra_data
= aac
.codec_specific_data();
250 MEDIA_LOG(ERROR
, media_log_
) << "Unsupported audio object type 0x"
251 << std::hex
<< audio_type
<< " in esds.";
255 SampleFormat sample_format
;
256 if (entry
.samplesize
== 8) {
257 sample_format
= kSampleFormatU8
;
258 } else if (entry
.samplesize
== 16) {
259 sample_format
= kSampleFormatS16
;
260 } else if (entry
.samplesize
== 32) {
261 sample_format
= kSampleFormatS32
;
263 LOG(ERROR
) << "Unsupported sample size.";
267 is_audio_track_encrypted_
= entry
.sinf
.info
.track_encryption
.is_encrypted
;
268 DVLOG(1) << "is_audio_track_encrypted_: " << is_audio_track_encrypted_
;
269 audio_config
.Initialize(
270 codec
, sample_format
, channel_layout
, sample_per_second
,
271 extra_data
.size() ? &extra_data
[0] : NULL
, extra_data
.size(),
272 is_audio_track_encrypted_
, base::TimeDelta(), 0);
274 audio_track_id_
= track
->header
.track_id
;
276 if (track
->media
.handler
.type
== kVideo
&& !video_config
.IsValidConfig()) {
277 RCHECK(!samp_descr
.video_entries
.empty());
278 if (desc_idx
>= samp_descr
.video_entries
.size())
280 const VideoSampleEntry
& entry
= samp_descr
.video_entries
[desc_idx
];
282 if (!entry
.IsFormatValid()) {
283 MEDIA_LOG(ERROR
, media_log_
) << "Unsupported video format 0x"
284 << std::hex
<< entry
.format
289 // TODO(strobe): Recover correct crop box
290 gfx::Size
coded_size(entry
.width
, entry
.height
);
291 gfx::Rect
visible_rect(coded_size
);
293 // If PASP is available, use the coded size and PASP to calculate the
294 // natural size. Otherwise, use the size in track header for natural size.
295 gfx::Size
natural_size(visible_rect
.size());
296 if (entry
.pixel_aspect
.h_spacing
!= 1 ||
297 entry
.pixel_aspect
.v_spacing
!= 1) {
299 GetNaturalSize(visible_rect
.size(), entry
.pixel_aspect
.h_spacing
,
300 entry
.pixel_aspect
.v_spacing
);
301 } else if (track
->header
.width
&& track
->header
.height
) {
303 gfx::Size(track
->header
.width
, track
->header
.height
);
306 is_video_track_encrypted_
= entry
.sinf
.info
.track_encryption
.is_encrypted
;
307 DVLOG(1) << "is_video_track_encrypted_: " << is_video_track_encrypted_
;
308 video_config
.Initialize(entry
.video_codec
, entry
.video_codec_profile
,
309 PIXEL_FORMAT_YV12
, COLOR_SPACE_HD_REC709
,
310 coded_size
, visible_rect
, natural_size
,
311 // No decoder-specific buffer needed for AVC;
312 // SPS/PPS are embedded in the video stream
313 NULL
, 0, is_video_track_encrypted_
);
315 video_track_id_
= track
->header
.track_id
;
319 RCHECK(config_cb_
.Run(audio_config
, video_config
, TextTrackConfigMap()));
321 StreamParser::InitParameters
params(kInfiniteDuration());
322 if (moov_
->extends
.header
.fragment_duration
> 0) {
323 params
.duration
= TimeDeltaFromRational(
324 moov_
->extends
.header
.fragment_duration
, moov_
->header
.timescale
);
325 params
.liveness
= DemuxerStream::LIVENESS_RECORDED
;
326 } else if (moov_
->header
.duration
> 0 &&
327 moov_
->header
.duration
!= kuint64max
) {
329 TimeDeltaFromRational(moov_
->header
.duration
, moov_
->header
.timescale
);
330 params
.liveness
= DemuxerStream::LIVENESS_RECORDED
;
332 // In ISO/IEC 14496-12:2005(E), 8.30.2: ".. If an MP4 file is created in
333 // real-time, such as used in live streaming, it is not likely that the
334 // fragment_duration is known in advance and this (mehd) box may be
336 // TODO(wolenetz): Investigate gating liveness detection on timeline_offset
337 // when it's populated. See http://crbug.com/312699
338 params
.liveness
= DemuxerStream::LIVENESS_LIVE
;
341 DVLOG(1) << "liveness: " << params
.liveness
;
343 if (!init_cb_
.is_null())
344 base::ResetAndReturn(&init_cb_
).Run(params
);
346 if (!moov_
->pssh
.empty())
347 OnEncryptedMediaInitData(moov_
->pssh
);
352 bool MP4StreamParser::ParseMoof(BoxReader
* reader
) {
353 RCHECK(moov_
.get()); // Must already have initialization segment
355 RCHECK(moof
.Parse(reader
));
357 runs_
.reset(new TrackRunIterator(moov_
.get(), media_log_
));
358 RCHECK(runs_
->Init(moof
));
359 RCHECK(ComputeHighestEndOffset(moof
));
361 if (!moof
.pssh
.empty())
362 OnEncryptedMediaInitData(moof
.pssh
);
364 new_segment_cb_
.Run();
365 ChangeState(kWaitingForSampleData
);
369 void MP4StreamParser::OnEncryptedMediaInitData(
370 const std::vector
<ProtectionSystemSpecificHeader
>& headers
) {
371 // TODO(strobe): ensure that the value of init_data (all PSSH headers
372 // concatenated in arbitrary order) matches the EME spec.
373 // See https://www.w3.org/Bugs/Public/show_bug.cgi?id=17673.
374 size_t total_size
= 0;
375 for (size_t i
= 0; i
< headers
.size(); i
++)
376 total_size
+= headers
[i
].raw_box
.size();
378 std::vector
<uint8
> init_data(total_size
);
380 for (size_t i
= 0; i
< headers
.size(); i
++) {
381 memcpy(&init_data
[pos
], &headers
[i
].raw_box
[0],
382 headers
[i
].raw_box
.size());
383 pos
+= headers
[i
].raw_box
.size();
385 encrypted_media_init_data_cb_
.Run(EmeInitDataType::CENC
, init_data
);
388 bool MP4StreamParser::PrepareAACBuffer(
389 const AAC
& aac_config
, std::vector
<uint8
>* frame_buf
,
390 std::vector
<SubsampleEntry
>* subsamples
) const {
391 // Append an ADTS header to every audio sample.
392 RCHECK(aac_config
.ConvertEsdsToADTS(frame_buf
));
394 // As above, adjust subsample information to account for the headers. AAC is
395 // not required to use subsample encryption, so we may need to add an entry.
396 if (subsamples
->empty()) {
397 subsamples
->push_back(SubsampleEntry(
398 kADTSHeaderMinSize
, frame_buf
->size() - kADTSHeaderMinSize
));
400 (*subsamples
)[0].clear_bytes
+= kADTSHeaderMinSize
;
405 bool MP4StreamParser::EnqueueSample(BufferQueue
* audio_buffers
,
406 BufferQueue
* video_buffers
,
408 DCHECK_EQ(state_
, kEmittingSamples
);
410 if (!runs_
->IsRunValid()) {
411 // Flush any buffers we've gotten in this chunk so that buffers don't
412 // cross NewSegment() calls
413 *err
= !SendAndFlushSamples(audio_buffers
, video_buffers
);
417 // Remain in kEmittingSamples state, discarding data, until the end of
418 // the current 'mdat' box has been appended to the queue.
419 if (!queue_
.Trim(mdat_tail_
))
422 ChangeState(kParsingBoxes
);
423 end_of_segment_cb_
.Run();
427 if (!runs_
->IsSampleValid()) {
436 queue_
.Peek(&buf
, &buf_size
);
437 if (!buf_size
) return false;
439 bool audio
= has_audio_
&& audio_track_id_
== runs_
->track_id();
440 bool video
= has_video_
&& video_track_id_
== runs_
->track_id();
442 // Skip this entire track if it's not one we're interested in
443 if (!audio
&& !video
) {
448 // Attempt to cache the auxiliary information first. Aux info is usually
449 // placed in a contiguous block before the sample data, rather than being
450 // interleaved. If we didn't cache it, this would require that we retain the
451 // start of the segment buffer while reading samples. Aux info is typically
452 // quite small compared to sample data, so this pattern is useful on
453 // memory-constrained devices where the source buffer consumes a substantial
454 // portion of the total system memory.
455 if (runs_
->AuxInfoNeedsToBeCached()) {
456 queue_
.PeekAt(runs_
->aux_info_offset() + moof_head_
, &buf
, &buf_size
);
457 if (buf_size
< runs_
->aux_info_size()) return false;
458 *err
= !runs_
->CacheAuxInfo(buf
, buf_size
);
462 queue_
.PeekAt(runs_
->sample_offset() + moof_head_
, &buf
, &buf_size
);
463 if (buf_size
< runs_
->sample_size()) return false;
465 scoped_ptr
<DecryptConfig
> decrypt_config
;
466 std::vector
<SubsampleEntry
> subsamples
;
467 if (runs_
->is_encrypted()) {
468 decrypt_config
= runs_
->GetDecryptConfig();
469 if (!decrypt_config
) {
473 subsamples
= decrypt_config
->subsamples();
476 std::vector
<uint8
> frame_buf(buf
, buf
+ runs_
->sample_size());
478 DCHECK(runs_
->video_description().frame_bitstream_converter
);
479 if (!runs_
->video_description().frame_bitstream_converter
->ConvertFrame(
480 &frame_buf
, runs_
->is_keyframe(), &subsamples
)) {
481 MEDIA_LOG(ERROR
, media_log_
)
482 << "Failed to prepare video sample for decode";
489 if (ESDescriptor::IsAAC(runs_
->audio_description().esds
.object_type
) &&
490 !PrepareAACBuffer(runs_
->audio_description().esds
.aac
,
491 &frame_buf
, &subsamples
)) {
492 MEDIA_LOG(ERROR
, media_log_
) << "Failed to prepare AAC sample for decode";
498 if (decrypt_config
) {
499 if (!subsamples
.empty()) {
500 // Create a new config with the updated subsamples.
501 decrypt_config
.reset(new DecryptConfig(
502 decrypt_config
->key_id(),
503 decrypt_config
->iv(),
506 // else, use the existing config.
507 } else if ((audio
&& is_audio_track_encrypted_
) ||
508 (video
&& is_video_track_encrypted_
)) {
509 // The media pipeline requires a DecryptConfig with an empty |iv|.
510 // TODO(ddorwin): Refactor so we do not need a fake key ID ("1");
511 decrypt_config
.reset(
512 new DecryptConfig("1", "", std::vector
<SubsampleEntry
>()));
515 StreamParserBuffer::Type buffer_type
= audio
? DemuxerStream::AUDIO
:
516 DemuxerStream::VIDEO
;
518 // TODO(wolenetz/acolwell): Validate and use a common cross-parser TrackId
519 // type and allow multiple tracks for same media type, if applicable. See
520 // https://crbug.com/341581.
521 scoped_refptr
<StreamParserBuffer
> stream_buf
=
522 StreamParserBuffer::CopyFrom(&frame_buf
[0], frame_buf
.size(),
523 runs_
->is_keyframe(),
527 stream_buf
->set_decrypt_config(decrypt_config
.Pass());
529 stream_buf
->set_duration(runs_
->duration());
530 stream_buf
->set_timestamp(runs_
->cts());
531 stream_buf
->SetDecodeTimestamp(runs_
->dts());
533 DVLOG(3) << "Pushing frame: aud=" << audio
534 << ", key=" << runs_
->is_keyframe()
535 << ", dur=" << runs_
->duration().InMilliseconds()
536 << ", dts=" << runs_
->dts().InMilliseconds()
537 << ", cts=" << runs_
->cts().InMilliseconds()
538 << ", size=" << runs_
->sample_size();
541 audio_buffers
->push_back(stream_buf
);
543 video_buffers
->push_back(stream_buf
);
546 runs_
->AdvanceSample();
550 bool MP4StreamParser::SendAndFlushSamples(BufferQueue
* audio_buffers
,
551 BufferQueue
* video_buffers
) {
552 if (audio_buffers
->empty() && video_buffers
->empty())
555 TextBufferQueueMap empty_text_map
;
556 bool success
= new_buffers_cb_
.Run(*audio_buffers
,
559 audio_buffers
->clear();
560 video_buffers
->clear();
564 bool MP4StreamParser::ReadAndDiscardMDATsUntil(int64 max_clear_offset
) {
566 int64 upper_bound
= std::min(max_clear_offset
, queue_
.tail());
567 while (mdat_tail_
< upper_bound
) {
568 const uint8
* buf
= NULL
;
570 queue_
.PeekAt(mdat_tail_
, &buf
, &size
);
574 if (!BoxReader::StartTopLevelBox(buf
, size
, media_log_
, &type
, &box_sz
,
578 if (type
!= FOURCC_MDAT
) {
579 MEDIA_LOG(DEBUG
, media_log_
)
580 << "Unexpected box type while parsing MDATs: "
581 << FourCCToString(type
);
583 mdat_tail_
+= box_sz
;
585 queue_
.Trim(std::min(mdat_tail_
, upper_bound
));
589 void MP4StreamParser::ChangeState(State new_state
) {
590 DVLOG(2) << "Changing state: " << new_state
;
594 bool MP4StreamParser::HaveEnoughDataToEnqueueSamples() {
595 DCHECK_EQ(state_
, kWaitingForSampleData
);
596 // For muxed content, make sure we have data up to |highest_end_offset_|
597 // so we can ensure proper enqueuing behavior. Otherwise assume we have enough
598 // data and allow per sample offset checks to meter sample enqueuing.
599 // TODO(acolwell): Fix trun box handling so we don't have to special case
601 return !(has_audio_
&& has_video_
&&
602 queue_
.tail() < highest_end_offset_
+ moof_head_
);
605 bool MP4StreamParser::ComputeHighestEndOffset(const MovieFragment
& moof
) {
606 highest_end_offset_
= 0;
608 TrackRunIterator
runs(moov_
.get(), media_log_
);
609 RCHECK(runs
.Init(moof
));
611 while (runs
.IsRunValid()) {
612 int64 aux_info_end_offset
= runs
.aux_info_offset() + runs
.aux_info_size();
613 if (aux_info_end_offset
> highest_end_offset_
)
614 highest_end_offset_
= aux_info_end_offset
;
616 while (runs
.IsSampleValid()) {
617 int64 sample_end_offset
= runs
.sample_offset() + runs
.sample_size();
618 if (sample_end_offset
> highest_end_offset_
)
619 highest_end_offset_
= sample_end_offset
;
621 runs
.AdvanceSample();