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/video_decoder_config.h"
14 #include "media/base/video_util.h"
15 #include "media/formats/mp4/box_definitions.h"
16 #include "media/formats/mp4/box_reader.h"
17 #include "media/formats/mp4/es_descriptor.h"
18 #include "media/formats/mp4/rcheck.h"
19 #include "media/formats/mpeg/adts_constants.h"
24 MP4StreamParser::MP4StreamParser(const std::set
<int>& audio_object_types
,
26 : state_(kWaitingForInit
),
29 highest_end_offset_(0),
34 audio_object_types_(audio_object_types
),
36 is_audio_track_encrypted_(false),
37 is_video_track_encrypted_(false),
38 num_top_level_box_skipped_(0) {
41 MP4StreamParser::~MP4StreamParser() {}
43 void MP4StreamParser::Init(
44 const InitCB
& init_cb
,
45 const NewConfigCB
& config_cb
,
46 const NewBuffersCB
& new_buffers_cb
,
47 bool /* ignore_text_tracks */,
48 const EncryptedMediaInitDataCB
& encrypted_media_init_data_cb
,
49 const NewMediaSegmentCB
& new_segment_cb
,
50 const base::Closure
& end_of_segment_cb
,
51 const LogCB
& log_cb
) {
52 DCHECK_EQ(state_
, kWaitingForInit
);
53 DCHECK(init_cb_
.is_null());
54 DCHECK(!init_cb
.is_null());
55 DCHECK(!config_cb
.is_null());
56 DCHECK(!new_buffers_cb
.is_null());
57 DCHECK(!encrypted_media_init_data_cb
.is_null());
58 DCHECK(!end_of_segment_cb
.is_null());
60 ChangeState(kParsingBoxes
);
62 config_cb_
= config_cb
;
63 new_buffers_cb_
= new_buffers_cb
;
64 encrypted_media_init_data_cb_
= encrypted_media_init_data_cb
;
65 new_segment_cb_
= new_segment_cb
;
66 end_of_segment_cb_
= end_of_segment_cb
;
70 void MP4StreamParser::Reset() {
77 void MP4StreamParser::Flush() {
78 DCHECK_NE(state_
, kWaitingForInit
);
80 ChangeState(kParsingBoxes
);
83 bool MP4StreamParser::Parse(const uint8
* buf
, int size
) {
84 DCHECK_NE(state_
, kWaitingForInit
);
89 queue_
.Push(buf
, size
);
91 BufferQueue audio_buffers
;
92 BufferQueue video_buffers
;
105 result
= ParseBox(&err
);
108 case kWaitingForSampleData
:
109 result
= HaveEnoughDataToEnqueueSamples();
111 ChangeState(kEmittingSamples
);
114 case kEmittingSamples
:
115 result
= EnqueueSample(&audio_buffers
, &video_buffers
, &err
);
117 int64 max_clear
= runs_
->GetMaxClearOffset() + moof_head_
;
118 err
= !ReadAndDiscardMDATsUntil(max_clear
);
122 } while (result
&& !err
);
125 err
= !SendAndFlushSamples(&audio_buffers
, &video_buffers
);
128 DLOG(ERROR
) << "Error while parsing MP4";
138 bool MP4StreamParser::ParseBox(bool* err
) {
141 queue_
.Peek(&buf
, &size
);
142 if (!size
) return false;
144 scoped_ptr
<BoxReader
> reader(
145 BoxReader::ReadTopLevelBox(buf
, size
, log_cb_
, err
));
146 if (reader
.get() == NULL
) return false;
148 if (reader
->type() == FOURCC_MOOV
) {
149 *err
= !ParseMoov(reader
.get());
150 } else if (reader
->type() == FOURCC_MOOF
) {
151 moof_head_
= queue_
.head();
152 *err
= !ParseMoof(reader
.get());
154 // Set up first mdat offset for ReadMDATsUntil().
155 mdat_tail_
= queue_
.head() + reader
->size();
157 // Return early to avoid evicting 'moof' data from queue. Auxiliary info may
158 // be located anywhere in the file, including inside the 'moof' itself.
159 // (Since 'default-base-is-moof' is mandated, no data references can come
160 // before the head of the 'moof', so keeping this box around is sufficient.)
163 const int kMaxNumLogsForSkippingTopLevelBox
= 5;
165 // TODO(wolenetz): Do not log when skipping ftyp, since strict MSE would
166 // require ftyp. See http://crbug.com/499077
167 LIMITED_MEDIA_LOG(DEBUG
, log_cb_
, num_top_level_box_skipped_
,
168 kMaxNumLogsForSkippingTopLevelBox
)
169 << "Skipping unrecognized top-level box: "
170 << FourCCToString(reader
->type());
173 queue_
.Pop(reader
->size());
178 bool MP4StreamParser::ParseMoov(BoxReader
* reader
) {
179 moov_
.reset(new Movie
);
180 RCHECK(moov_
->Parse(reader
));
186 AudioDecoderConfig audio_config
;
187 VideoDecoderConfig video_config
;
189 for (std::vector
<Track
>::const_iterator track
= moov_
->tracks
.begin();
190 track
!= moov_
->tracks
.end(); ++track
) {
191 // TODO(strobe): Only the first audio and video track present in a file are
192 // used. (Track selection is better accomplished via Source IDs, though, so
193 // adding support for track selection within a stream is low-priority.)
194 const SampleDescription
& samp_descr
=
195 track
->media
.information
.sample_table
.description
;
197 // TODO(strobe): When codec reconfigurations are supported, detect and send
198 // a codec reconfiguration for fragments using a sample description index
199 // different from the previous one
201 for (size_t t
= 0; t
< moov_
->extends
.tracks
.size(); t
++) {
202 const TrackExtends
& trex
= moov_
->extends
.tracks
[t
];
203 if (trex
.track_id
== track
->header
.track_id
) {
204 desc_idx
= trex
.default_sample_description_index
;
208 RCHECK(desc_idx
> 0);
209 desc_idx
-= 1; // BMFF descriptor index is one-based
211 if (track
->media
.handler
.type
== kAudio
&& !audio_config
.IsValidConfig()) {
212 RCHECK(!samp_descr
.audio_entries
.empty());
214 // It is not uncommon to find otherwise-valid files with incorrect sample
215 // description indices, so we fail gracefully in that case.
216 if (desc_idx
>= samp_descr
.audio_entries
.size())
218 const AudioSampleEntry
& entry
= samp_descr
.audio_entries
[desc_idx
];
219 const AAC
& aac
= entry
.esds
.aac
;
221 if (!(entry
.format
== FOURCC_MP4A
||
222 (entry
.format
== FOURCC_ENCA
&&
223 entry
.sinf
.format
.format
== FOURCC_MP4A
))) {
224 MEDIA_LOG(ERROR
, log_cb_
) << "Unsupported audio format 0x" << std::hex
225 << entry
.format
<< " in stsd box.";
229 uint8 audio_type
= entry
.esds
.object_type
;
230 DVLOG(1) << "audio_type " << std::hex
<< static_cast<int>(audio_type
);
231 if (audio_object_types_
.find(audio_type
) == audio_object_types_
.end()) {
232 MEDIA_LOG(ERROR
, log_cb_
) << "audio object type 0x" << std::hex
234 << " does not match what is specified in the"
239 AudioCodec codec
= kUnknownAudioCodec
;
240 ChannelLayout channel_layout
= CHANNEL_LAYOUT_NONE
;
241 int sample_per_second
= 0;
242 std::vector
<uint8
> extra_data
;
243 // Check if it is MPEG4 AAC defined in ISO 14496 Part 3 or
244 // supported MPEG2 AAC varients.
245 if (ESDescriptor::IsAAC(audio_type
)) {
247 channel_layout
= aac
.GetChannelLayout(has_sbr_
);
248 sample_per_second
= aac
.GetOutputSamplesPerSecond(has_sbr_
);
249 #if defined(OS_ANDROID)
250 extra_data
= aac
.codec_specific_data();
253 MEDIA_LOG(ERROR
, log_cb_
) << "Unsupported audio object type 0x"
254 << std::hex
<< audio_type
<< " in esds.";
258 SampleFormat sample_format
;
259 if (entry
.samplesize
== 8) {
260 sample_format
= kSampleFormatU8
;
261 } else if (entry
.samplesize
== 16) {
262 sample_format
= kSampleFormatS16
;
263 } else if (entry
.samplesize
== 32) {
264 sample_format
= kSampleFormatS32
;
266 LOG(ERROR
) << "Unsupported sample size.";
270 is_audio_track_encrypted_
= entry
.sinf
.info
.track_encryption
.is_encrypted
;
271 DVLOG(1) << "is_audio_track_encrypted_: " << is_audio_track_encrypted_
;
272 audio_config
.Initialize(
273 codec
, sample_format
, channel_layout
, sample_per_second
,
274 extra_data
.size() ? &extra_data
[0] : NULL
, extra_data
.size(),
275 is_audio_track_encrypted_
, false, base::TimeDelta(),
278 audio_track_id_
= track
->header
.track_id
;
280 if (track
->media
.handler
.type
== kVideo
&& !video_config
.IsValidConfig()) {
281 RCHECK(!samp_descr
.video_entries
.empty());
282 if (desc_idx
>= samp_descr
.video_entries
.size())
284 const VideoSampleEntry
& entry
= samp_descr
.video_entries
[desc_idx
];
286 if (!entry
.IsFormatValid()) {
287 MEDIA_LOG(ERROR
, log_cb_
) << "Unsupported video format 0x" << std::hex
288 << entry
.format
<< " in stsd box.";
292 // TODO(strobe): Recover correct crop box
293 gfx::Size
coded_size(entry
.width
, entry
.height
);
294 gfx::Rect
visible_rect(coded_size
);
295 gfx::Size natural_size
= GetNaturalSize(visible_rect
.size(),
296 entry
.pixel_aspect
.h_spacing
,
297 entry
.pixel_aspect
.v_spacing
);
298 is_video_track_encrypted_
= entry
.sinf
.info
.track_encryption
.is_encrypted
;
299 DVLOG(1) << "is_video_track_encrypted_: " << is_video_track_encrypted_
;
300 video_config
.Initialize(kCodecH264
, H264PROFILE_MAIN
, VideoFrame::YV12
,
301 VideoFrame::COLOR_SPACE_UNSPECIFIED
, coded_size
,
302 visible_rect
, natural_size
,
303 // No decoder-specific buffer needed for AVC;
304 // SPS/PPS are embedded in the video stream
305 NULL
, 0, is_video_track_encrypted_
, false);
307 video_track_id_
= track
->header
.track_id
;
311 RCHECK(config_cb_
.Run(audio_config
, video_config
, TextTrackConfigMap()));
313 StreamParser::InitParameters
params(kInfiniteDuration());
314 if (moov_
->extends
.header
.fragment_duration
> 0) {
315 params
.duration
= TimeDeltaFromRational(
316 moov_
->extends
.header
.fragment_duration
, moov_
->header
.timescale
);
317 params
.liveness
= DemuxerStream::LIVENESS_RECORDED
;
318 } else if (moov_
->header
.duration
> 0 &&
319 moov_
->header
.duration
!= kuint64max
) {
321 TimeDeltaFromRational(moov_
->header
.duration
, moov_
->header
.timescale
);
322 params
.liveness
= DemuxerStream::LIVENESS_RECORDED
;
324 // In ISO/IEC 14496-12:2005(E), 8.30.2: ".. If an MP4 file is created in
325 // real-time, such as used in live streaming, it is not likely that the
326 // fragment_duration is known in advance and this (mehd) box may be
328 // TODO(wolenetz): Investigate gating liveness detection on timeline_offset
329 // when it's populated. See http://crbug.com/312699
330 params
.liveness
= DemuxerStream::LIVENESS_LIVE
;
333 DVLOG(1) << "liveness: " << params
.liveness
;
335 if (!init_cb_
.is_null())
336 base::ResetAndReturn(&init_cb_
).Run(params
);
338 if (!moov_
->pssh
.empty())
339 OnEncryptedMediaInitData(moov_
->pssh
);
344 bool MP4StreamParser::ParseMoof(BoxReader
* reader
) {
345 RCHECK(moov_
.get()); // Must already have initialization segment
347 RCHECK(moof
.Parse(reader
));
349 runs_
.reset(new TrackRunIterator(moov_
.get(), log_cb_
));
350 RCHECK(runs_
->Init(moof
));
351 RCHECK(ComputeHighestEndOffset(moof
));
353 if (!moof
.pssh
.empty())
354 OnEncryptedMediaInitData(moof
.pssh
);
356 new_segment_cb_
.Run();
357 ChangeState(kWaitingForSampleData
);
361 void MP4StreamParser::OnEncryptedMediaInitData(
362 const std::vector
<ProtectionSystemSpecificHeader
>& headers
) {
363 // TODO(strobe): ensure that the value of init_data (all PSSH headers
364 // concatenated in arbitrary order) matches the EME spec.
365 // See https://www.w3.org/Bugs/Public/show_bug.cgi?id=17673.
366 size_t total_size
= 0;
367 for (size_t i
= 0; i
< headers
.size(); i
++)
368 total_size
+= headers
[i
].raw_box
.size();
370 std::vector
<uint8
> init_data(total_size
);
372 for (size_t i
= 0; i
< headers
.size(); i
++) {
373 memcpy(&init_data
[pos
], &headers
[i
].raw_box
[0],
374 headers
[i
].raw_box
.size());
375 pos
+= headers
[i
].raw_box
.size();
377 encrypted_media_init_data_cb_
.Run(EmeInitDataType::CENC
, init_data
);
380 bool MP4StreamParser::PrepareAVCBuffer(
381 const AVCDecoderConfigurationRecord
& avc_config
,
382 std::vector
<uint8
>* frame_buf
,
383 std::vector
<SubsampleEntry
>* subsamples
) const {
384 // Convert the AVC NALU length fields to Annex B headers, as expected by
385 // decoding libraries. Since this may enlarge the size of the buffer, we also
386 // update the clear byte count for each subsample if encryption is used to
387 // account for the difference in size between the length prefix and Annex B
389 RCHECK(AVC::ConvertFrameToAnnexB(avc_config
.length_size
, frame_buf
));
390 if (!subsamples
->empty()) {
391 const int nalu_size_diff
= 4 - avc_config
.length_size
;
392 size_t expected_size
= runs_
->sample_size() +
393 subsamples
->size() * nalu_size_diff
;
394 RCHECK(frame_buf
->size() == expected_size
);
395 for (size_t i
= 0; i
< subsamples
->size(); i
++)
396 (*subsamples
)[i
].clear_bytes
+= nalu_size_diff
;
399 if (runs_
->is_keyframe()) {
400 // If this is a keyframe, we (re-)inject SPS and PPS headers at the start of
401 // a frame. If subsample info is present, we also update the clear byte
402 // count for that first subsample.
403 RCHECK(AVC::InsertParamSetsAnnexB(avc_config
, frame_buf
, subsamples
));
406 DCHECK(AVC::IsValidAnnexB(*frame_buf
, *subsamples
));
410 bool MP4StreamParser::PrepareAACBuffer(
411 const AAC
& aac_config
, std::vector
<uint8
>* frame_buf
,
412 std::vector
<SubsampleEntry
>* subsamples
) const {
413 // Append an ADTS header to every audio sample.
414 RCHECK(aac_config
.ConvertEsdsToADTS(frame_buf
));
416 // As above, adjust subsample information to account for the headers. AAC is
417 // not required to use subsample encryption, so we may need to add an entry.
418 if (subsamples
->empty()) {
419 subsamples
->push_back(SubsampleEntry(
420 kADTSHeaderMinSize
, frame_buf
->size() - kADTSHeaderMinSize
));
422 (*subsamples
)[0].clear_bytes
+= kADTSHeaderMinSize
;
427 bool MP4StreamParser::EnqueueSample(BufferQueue
* audio_buffers
,
428 BufferQueue
* video_buffers
,
430 DCHECK_EQ(state_
, kEmittingSamples
);
432 if (!runs_
->IsRunValid()) {
433 // Flush any buffers we've gotten in this chunk so that buffers don't
434 // cross NewSegment() calls
435 *err
= !SendAndFlushSamples(audio_buffers
, video_buffers
);
439 // Remain in kEmittingSamples state, discarding data, until the end of
440 // the current 'mdat' box has been appended to the queue.
441 if (!queue_
.Trim(mdat_tail_
))
444 ChangeState(kParsingBoxes
);
445 end_of_segment_cb_
.Run();
449 if (!runs_
->IsSampleValid()) {
458 queue_
.Peek(&buf
, &buf_size
);
459 if (!buf_size
) return false;
461 bool audio
= has_audio_
&& audio_track_id_
== runs_
->track_id();
462 bool video
= has_video_
&& video_track_id_
== runs_
->track_id();
464 // Skip this entire track if it's not one we're interested in
465 if (!audio
&& !video
) {
470 // Attempt to cache the auxiliary information first. Aux info is usually
471 // placed in a contiguous block before the sample data, rather than being
472 // interleaved. If we didn't cache it, this would require that we retain the
473 // start of the segment buffer while reading samples. Aux info is typically
474 // quite small compared to sample data, so this pattern is useful on
475 // memory-constrained devices where the source buffer consumes a substantial
476 // portion of the total system memory.
477 if (runs_
->AuxInfoNeedsToBeCached()) {
478 queue_
.PeekAt(runs_
->aux_info_offset() + moof_head_
, &buf
, &buf_size
);
479 if (buf_size
< runs_
->aux_info_size()) return false;
480 *err
= !runs_
->CacheAuxInfo(buf
, buf_size
);
484 queue_
.PeekAt(runs_
->sample_offset() + moof_head_
, &buf
, &buf_size
);
485 if (buf_size
< runs_
->sample_size()) return false;
487 scoped_ptr
<DecryptConfig
> decrypt_config
;
488 std::vector
<SubsampleEntry
> subsamples
;
489 if (runs_
->is_encrypted()) {
490 decrypt_config
= runs_
->GetDecryptConfig();
491 if (!decrypt_config
) {
495 subsamples
= decrypt_config
->subsamples();
498 std::vector
<uint8
> frame_buf(buf
, buf
+ runs_
->sample_size());
500 if (!PrepareAVCBuffer(runs_
->video_description().avcc
,
501 &frame_buf
, &subsamples
)) {
502 MEDIA_LOG(ERROR
, log_cb_
) << "Failed to prepare AVC sample for decode";
509 if (ESDescriptor::IsAAC(runs_
->audio_description().esds
.object_type
) &&
510 !PrepareAACBuffer(runs_
->audio_description().esds
.aac
,
511 &frame_buf
, &subsamples
)) {
512 MEDIA_LOG(ERROR
, log_cb_
) << "Failed to prepare AAC sample for decode";
518 if (decrypt_config
) {
519 if (!subsamples
.empty()) {
520 // Create a new config with the updated subsamples.
521 decrypt_config
.reset(new DecryptConfig(
522 decrypt_config
->key_id(),
523 decrypt_config
->iv(),
526 // else, use the existing config.
527 } else if ((audio
&& is_audio_track_encrypted_
) ||
528 (video
&& is_video_track_encrypted_
)) {
529 // The media pipeline requires a DecryptConfig with an empty |iv|.
530 // TODO(ddorwin): Refactor so we do not need a fake key ID ("1");
531 decrypt_config
.reset(
532 new DecryptConfig("1", "", std::vector
<SubsampleEntry
>()));
535 StreamParserBuffer::Type buffer_type
= audio
? DemuxerStream::AUDIO
:
536 DemuxerStream::VIDEO
;
538 // TODO(wolenetz/acolwell): Validate and use a common cross-parser TrackId
539 // type and allow multiple tracks for same media type, if applicable. See
540 // https://crbug.com/341581.
542 // NOTE: MPEG's "random access point" concept is equivalent to the
543 // downstream code's "is keyframe" concept.
544 scoped_refptr
<StreamParserBuffer
> stream_buf
=
545 StreamParserBuffer::CopyFrom(&frame_buf
[0], frame_buf
.size(),
546 runs_
->is_random_access_point(),
550 stream_buf
->set_decrypt_config(decrypt_config
.Pass());
552 stream_buf
->set_duration(runs_
->duration());
553 stream_buf
->set_timestamp(runs_
->cts());
554 stream_buf
->SetDecodeTimestamp(runs_
->dts());
556 DVLOG(3) << "Pushing frame: aud=" << audio
557 << ", key=" << runs_
->is_keyframe()
558 << ", rap=" << runs_
->is_random_access_point()
559 << ", dur=" << runs_
->duration().InMilliseconds()
560 << ", dts=" << runs_
->dts().InMilliseconds()
561 << ", cts=" << runs_
->cts().InMilliseconds()
562 << ", size=" << runs_
->sample_size();
565 audio_buffers
->push_back(stream_buf
);
567 video_buffers
->push_back(stream_buf
);
570 runs_
->AdvanceSample();
574 bool MP4StreamParser::SendAndFlushSamples(BufferQueue
* audio_buffers
,
575 BufferQueue
* video_buffers
) {
576 if (audio_buffers
->empty() && video_buffers
->empty())
579 TextBufferQueueMap empty_text_map
;
580 bool success
= new_buffers_cb_
.Run(*audio_buffers
,
583 audio_buffers
->clear();
584 video_buffers
->clear();
588 bool MP4StreamParser::ReadAndDiscardMDATsUntil(int64 max_clear_offset
) {
590 int64 upper_bound
= std::min(max_clear_offset
, queue_
.tail());
591 while (mdat_tail_
< upper_bound
) {
592 const uint8
* buf
= NULL
;
594 queue_
.PeekAt(mdat_tail_
, &buf
, &size
);
598 if (!BoxReader::StartTopLevelBox(buf
, size
, log_cb_
,
599 &type
, &box_sz
, &err
))
602 if (type
!= FOURCC_MDAT
) {
603 MEDIA_LOG(DEBUG
, log_cb_
) << "Unexpected box type while parsing MDATs: "
604 << FourCCToString(type
);
606 mdat_tail_
+= box_sz
;
608 queue_
.Trim(std::min(mdat_tail_
, upper_bound
));
612 void MP4StreamParser::ChangeState(State new_state
) {
613 DVLOG(2) << "Changing state: " << new_state
;
617 bool MP4StreamParser::HaveEnoughDataToEnqueueSamples() {
618 DCHECK_EQ(state_
, kWaitingForSampleData
);
619 // For muxed content, make sure we have data up to |highest_end_offset_|
620 // so we can ensure proper enqueuing behavior. Otherwise assume we have enough
621 // data and allow per sample offset checks to meter sample enqueuing.
622 // TODO(acolwell): Fix trun box handling so we don't have to special case
624 return !(has_audio_
&& has_video_
&&
625 queue_
.tail() < highest_end_offset_
+ moof_head_
);
628 bool MP4StreamParser::ComputeHighestEndOffset(const MovieFragment
& moof
) {
629 highest_end_offset_
= 0;
631 TrackRunIterator
runs(moov_
.get(), log_cb_
);
632 RCHECK(runs
.Init(moof
));
634 while (runs
.IsRunValid()) {
635 int64 aux_info_end_offset
= runs
.aux_info_offset() + runs
.aux_info_size();
636 if (aux_info_end_offset
> highest_end_offset_
)
637 highest_end_offset_
= aux_info_end_offset
;
639 while (runs
.IsSampleValid()) {
640 int64 sample_end_offset
= runs
.sample_offset() + runs
.sample_size();
641 if (sample_end_offset
> highest_end_offset_
)
642 highest_end_offset_
= sample_end_offset
;
644 runs
.AdvanceSample();