OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/mp4/mp4_stream_parser.h" | 5 #include "media/mp4/mp4_stream_parser.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/callback_helpers.h" | 8 #include "base/callback_helpers.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/time.h" | 10 #include "base/time.h" |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
166 } | 166 } |
167 } | 167 } |
168 RCHECK(desc_idx > 0); | 168 RCHECK(desc_idx > 0); |
169 desc_idx -= 1; // BMFF descriptor index is one-based | 169 desc_idx -= 1; // BMFF descriptor index is one-based |
170 | 170 |
171 if (track->media.handler.type == kAudio && !audio_config.IsValidConfig()) { | 171 if (track->media.handler.type == kAudio && !audio_config.IsValidConfig()) { |
172 RCHECK(!samp_descr.audio_entries.empty()); | 172 RCHECK(!samp_descr.audio_entries.empty()); |
173 | 173 |
174 // It is not uncommon to find otherwise-valid files with incorrect sample | 174 // It is not uncommon to find otherwise-valid files with incorrect sample |
175 // description indices, so we fail gracefully in that case. | 175 // description indices, so we fail gracefully in that case. |
176 if (static_cast<uint32>(desc_idx) >= samp_descr.audio_entries.size()) | 176 if (desc_idx >= samp_descr.audio_entries.size()) |
177 desc_idx = 0; | 177 desc_idx = 0; |
178 const AudioSampleEntry& entry = samp_descr.audio_entries[desc_idx]; | 178 const AudioSampleEntry& entry = samp_descr.audio_entries[desc_idx]; |
179 const AAC& aac = entry.esds.aac; | 179 const AAC& aac = entry.esds.aac; |
180 | 180 |
181 // TODO(strobe): We accept all format values, pending clarification on | 181 RCHECK(entry.format == FOURCC_MP4A || |
182 // the formats used for encrypted media (http://crbug.com/132351). | 182 (entry.format == FOURCC_ENCA && |
183 // RCHECK(entry.format == FOURCC_MP4A || | 183 entry.sinf.format.format == FOURCC_MP4A)); |
184 // (entry.format == FOURCC_ENCA && | 184 RCHECK(EmitKeyNeeded(entry.sinf.info.track_encryption)); |
185 // entry.sinf.format.format == FOURCC_MP4A)); | |
186 | 185 |
187 // Check if it is MPEG4 AAC defined in ISO 14496 Part 3. | 186 // Check if it is MPEG4 AAC defined in ISO 14496 Part 3. |
188 RCHECK(entry.esds.object_type == kISO_14496_3); | 187 RCHECK(entry.esds.object_type == kISO_14496_3); |
189 audio_config.Initialize(kCodecAAC, entry.samplesize, | 188 audio_config.Initialize(kCodecAAC, entry.samplesize, |
190 aac.channel_layout(), aac.frequency(), | 189 aac.channel_layout(), aac.frequency(), |
191 NULL, 0, false); | 190 NULL, 0, false); |
192 | 191 |
193 has_audio_ = true; | 192 has_audio_ = true; |
194 audio_track_id_ = track->header.track_id; | 193 audio_track_id_ = track->header.track_id; |
195 } | 194 } |
196 if (track->media.handler.type == kVideo && !video_config.IsValidConfig()) { | 195 if (track->media.handler.type == kVideo && !video_config.IsValidConfig()) { |
197 RCHECK(!samp_descr.video_entries.empty()); | 196 RCHECK(!samp_descr.video_entries.empty()); |
198 if (static_cast<uint32>(desc_idx) >= samp_descr.video_entries.size()) | 197 if (desc_idx >= samp_descr.video_entries.size()) |
199 desc_idx = 0; | 198 desc_idx = 0; |
200 const VideoSampleEntry& entry = samp_descr.video_entries[desc_idx]; | 199 const VideoSampleEntry& entry = samp_descr.video_entries[desc_idx]; |
201 | 200 |
202 // RCHECK(entry.format == FOURCC_AVC1 || | 201 RCHECK(entry.format == FOURCC_AVC1 || |
203 // (entry.format == FOURCC_ENCV && | 202 (entry.format == FOURCC_ENCV && |
204 // entry.sinf.format.format == FOURCC_AVC1)); | 203 entry.sinf.format.format == FOURCC_AVC1)); |
204 RCHECK(EmitKeyNeeded(entry.sinf.info.track_encryption)); | |
205 | 205 |
206 // TODO(strobe): Recover correct crop box | 206 // TODO(strobe): Recover correct crop box |
207 video_config.Initialize(kCodecH264, H264PROFILE_MAIN, VideoFrame::YV12, | 207 video_config.Initialize(kCodecH264, H264PROFILE_MAIN, VideoFrame::YV12, |
208 gfx::Size(entry.width, entry.height), | 208 gfx::Size(entry.width, entry.height), |
209 gfx::Rect(0, 0, entry.width, entry.height), | 209 gfx::Rect(0, 0, entry.width, entry.height), |
210 // Framerate of zero is provided to signal that | 210 // Framerate of zero is provided to signal that |
211 // the decoder should trust demuxer timestamps | 211 // the decoder should trust demuxer timestamps |
212 0, 1, | 212 0, 1, |
213 entry.pixel_aspect.h_spacing, | 213 entry.pixel_aspect.h_spacing, |
214 entry.pixel_aspect.v_spacing, | 214 entry.pixel_aspect.v_spacing, |
215 // No decoder-specific buffer needed for AVC; | 215 // No decoder-specific buffer needed for AVC; |
216 // SPS/PPS are embedded in the video stream | 216 // SPS/PPS are embedded in the video stream |
217 NULL, 0, false); | 217 NULL, 0, false); |
218 has_video_ = true; | 218 has_video_ = true; |
219 video_track_id_ = track->header.track_id; | 219 video_track_id_ = track->header.track_id; |
220 } | 220 } |
221 } | 221 } |
222 | 222 |
223 // TODO(strobe): For now, we avoid sending new configs on a new | 223 // TODO(strobe): For now, we avoid sending new configs on a new |
224 // reinitialization segment, and instead simply embed the updated parameter | 224 // reinitialization segment, and instead simply embed the updated parameter |
225 // sets into the video stream. The conditional should be removed when | 225 // sets into the video stream. The conditional should be removed when |
226 // http://crbug.com/122913 is fixed. | 226 // http://crbug.com/122913 is fixed. (We detect whether we've already sent |
227 // configs by looking at init_cb_ instead of config_cb_, because init_cb_ | |
228 // should only be fired once even after that bug is fixed.) | |
227 if (!init_cb_.is_null()) | 229 if (!init_cb_.is_null()) |
228 RCHECK(config_cb_.Run(audio_config, video_config)); | 230 RCHECK(config_cb_.Run(audio_config, video_config)); |
229 | 231 |
230 base::TimeDelta duration; | 232 base::TimeDelta duration; |
231 if (moov_->extends.header.fragment_duration > 0) { | 233 if (moov_->extends.header.fragment_duration > 0) { |
232 duration = TimeDeltaFromFrac(moov_->extends.header.fragment_duration, | 234 duration = TimeDeltaFromRational(moov_->extends.header.fragment_duration, |
233 moov_->header.timescale); | 235 moov_->header.timescale); |
234 } else if (moov_->header.duration > 0) { | 236 } else if (moov_->header.duration > 0) { |
235 duration = TimeDeltaFromFrac(moov_->header.duration, | 237 duration = TimeDeltaFromRational(moov_->header.duration, |
236 moov_->header.timescale); | 238 moov_->header.timescale); |
237 } else { | 239 } else { |
238 duration = kInfiniteDuration(); | 240 duration = kInfiniteDuration(); |
239 } | 241 } |
240 | 242 |
241 if (!init_cb_.is_null()) | 243 if (!init_cb_.is_null()) |
242 base::ResetAndReturn(&init_cb_).Run(true, duration); | 244 base::ResetAndReturn(&init_cb_).Run(true, duration); |
243 return true; | 245 return true; |
244 } | 246 } |
245 | 247 |
246 bool MP4StreamParser::ParseMoof(BoxReader* reader) { | 248 bool MP4StreamParser::ParseMoof(BoxReader* reader) { |
247 RCHECK(moov_.get()); // Must already have initialization segment | 249 RCHECK(moov_.get()); // Must already have initialization segment |
248 MovieFragment moof; | 250 MovieFragment moof; |
249 RCHECK(moof.Parse(reader)); | 251 RCHECK(moof.Parse(reader)); |
250 RCHECK(runs_->Init(moof)); | 252 RCHECK(runs_->Init(moof)); |
251 new_segment_cb_.Run(runs_->GetMinDecodeTimestamp()); | 253 new_segment_cb_.Run(runs_->GetMinDecodeTimestamp()); |
252 ChangeState(kEmittingSamples); | 254 ChangeState(kEmittingSamples); |
253 return true; | 255 return true; |
254 } | 256 } |
255 | 257 |
258 bool MP4StreamParser::EmitKeyNeeded(const TrackEncryption& track_encryption) { | |
259 // TODO(strobe): Send the correct value for initData. The format of initData | |
260 // has not yet been defined; see | |
261 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=17673. | |
262 if (!track_encryption.is_encrypted) return true; | |
263 scoped_array<uint8> kid(new uint8[track_encryption.default_kid.size()]); | |
264 memcpy(kid.get(), &track_encryption.default_kid[0], | |
265 track_encryption.default_kid.size()); | |
266 return need_key_cb_.Run(kid.Pass(), track_encryption.default_kid.size()); | |
267 } | |
268 | |
269 bool MP4StreamParser::PrepareAVCBuffer( | |
270 const AVCDecoderConfigurationRecord& avc_config, | |
271 std::vector<uint8>* frame_buf, | |
272 std::vector<SubsampleEntry>* subsamples) const { | |
273 // Convert the AVC NALU length fields to Annex B headers, as expected by | |
274 // decoding libraries. Since this may enlarge the size of the buffer, we also | |
275 // update the clear byte count for each subsample if encryption is used. | |
276 RCHECK(AVC::ConvertFrameToAnnexB(avc_config.length_size, frame_buf)); | |
277 if (!subsamples->empty()) { | |
278 const int nalu_size_diff = 4 - avc_config.length_size; | |
279 size_t expected_size = runs_->sample_size() + | |
280 subsamples->size() * nalu_size_diff; | |
281 RCHECK(frame_buf->size() == expected_size); | |
282 for (size_t i = 0; i < subsamples->size(); i++) | |
283 (*subsamples)[i].clear_bytes += nalu_size_diff; | |
284 } | |
285 | |
286 if (runs_->is_keyframe()) { | |
287 // If this is a keyframe, we (re-)inject SPS and PPS headers at the start of | |
288 // a frame. If subsample info is present, we also update the clear byte | |
289 // count for that first subsample. | |
290 std::vector<uint8> param_sets; | |
291 RCHECK(AVC::ConvertParameterSetsToAnnexB(avc_config, ¶m_sets)); | |
ddorwin
2012/07/24 01:00:10
This is a bit confusing because "ParameterSets" wo
strobe_
2012/07/25 01:05:13
I see your point. Suggestions for alternative name
ddorwin
2012/07/25 07:13:47
Convert[Config]ToAnnexB[ParameterSets]()
"Config"
strobe_
2012/07/25 21:12:36
Done.
| |
292 frame_buf->insert(frame_buf->begin(), | |
293 param_sets.begin(), param_sets.end()); | |
294 if (!subsamples->empty()) | |
295 (*subsamples)[0].clear_bytes += param_sets.size(); | |
296 } | |
297 return true; | |
298 } | |
299 | |
256 bool MP4StreamParser::EnqueueSample(BufferQueue* audio_buffers, | 300 bool MP4StreamParser::EnqueueSample(BufferQueue* audio_buffers, |
257 BufferQueue* video_buffers, | 301 BufferQueue* video_buffers, |
258 bool* err) { | 302 bool* err) { |
259 if (!runs_->RunIsValid()) { | 303 if (!runs_->IsRunValid()) { |
260 // Flush any buffers we've gotten in this chunk so that buffers don't | 304 // Flush any buffers we've gotten in this chunk so that buffers don't |
261 // cross NewSegment() calls | 305 // cross NewSegment() calls |
262 *err = !SendAndFlushSamples(audio_buffers, video_buffers); | 306 *err = !SendAndFlushSamples(audio_buffers, video_buffers); |
263 if (*err) return false; | 307 if (*err) return false; |
264 ChangeState(kParsingBoxes); | 308 ChangeState(kParsingBoxes); |
265 return true; | 309 return true; |
266 } | 310 } |
267 | 311 |
268 if (!runs_->SampleIsValid()) { | 312 if (!runs_->IsSampleValid()) { |
269 runs_->AdvanceRun(); | 313 runs_->AdvanceRun(); |
270 return true; | 314 return true; |
271 } | 315 } |
272 | 316 |
273 DCHECK(!(*err)); | 317 DCHECK(!(*err)); |
274 | 318 |
275 const uint8* buf; | 319 const uint8* buf; |
276 int size; | 320 int buf_size; |
277 queue_.Peek(&buf, &size); | 321 queue_.Peek(&buf, &buf_size); |
278 if (!size) return false; | 322 if (!buf_size) return false; |
279 | 323 |
280 bool audio = has_audio_ && audio_track_id_ == runs_->track_id(); | 324 bool audio = has_audio_ && audio_track_id_ == runs_->track_id(); |
281 bool video = has_video_ && video_track_id_ == runs_->track_id(); | 325 bool video = has_video_ && video_track_id_ == runs_->track_id(); |
282 | 326 |
283 // Skip this entire track if it's not one we're interested in | 327 // Skip this entire track if it's not one we're interested in |
284 if (!audio && !video) runs_->AdvanceRun(); | 328 if (!audio && !video) runs_->AdvanceRun(); |
285 | 329 |
286 queue_.PeekAt(runs_->sample_offset() + moof_head_, &buf, &size); | 330 // Attempt to cache the auxiliary information first. Aux info is usually |
287 if (size < runs_->sample_size()) return false; | 331 // placed in a contiguous block before the sample data, rather than being |
332 // interleaved. If we didn't cache it, this would require that we retain the | |
333 // start of the segment buffer while reading samples. Aux info is typically | |
334 // quite small compared to sample data, so this pattern is useful on | |
335 // memory-constrained devices where the source buffer consumes a substantial | |
336 // portion of the total system memory. | |
337 if (runs_->AuxInfoNeedsToBeCached()) { | |
338 queue_.PeekAt(runs_->aux_info_offset() + moof_head_, &buf, &buf_size); | |
339 if (buf_size < runs_->aux_info_size()) return false; | |
340 *err = !runs_->CacheAuxInfo(buf, buf_size); | |
341 return !*err; | |
342 } | |
343 | |
344 queue_.PeekAt(runs_->sample_offset() + moof_head_, &buf, &buf_size); | |
345 if (buf_size < runs_->sample_size()) return false; | |
346 | |
347 scoped_ptr<DecryptConfig> decrypt_config; | |
348 if (runs_->is_encrypted()) | |
349 decrypt_config = runs_->GetDecryptConfig(); | |
288 | 350 |
289 std::vector<uint8> frame_buf(buf, buf + runs_->sample_size()); | 351 std::vector<uint8> frame_buf(buf, buf + runs_->sample_size()); |
290 if (video) { | 352 if (video) { |
291 const AVCDecoderConfigurationRecord& avc_config = | 353 std::vector<SubsampleEntry> subsamples; |
292 runs_->video_description().avcc; | 354 if (decrypt_config.get()) |
293 RCHECK(AVC::ConvertToAnnexB(avc_config.length_size, &frame_buf)); | 355 subsamples = decrypt_config->subsamples(); |
294 if (runs_->is_keyframe()) | 356 RCHECK(PrepareAVCBuffer(runs_->video_description().avcc, |
295 RCHECK(AVC::InsertParameterSets(avc_config, &frame_buf)); | 357 &frame_buf, &subsamples)); |
358 if (!subsamples.empty()) { | |
359 decrypt_config.reset(new DecryptConfig( | |
360 decrypt_config->key_id(), | |
361 decrypt_config->iv(), | |
362 decrypt_config->checksum(), | |
363 decrypt_config->data_offset(), | |
364 subsamples)); | |
365 } | |
296 } | 366 } |
297 | 367 |
298 if (audio) { | 368 if (audio) { |
299 const AAC& aac = runs_->audio_description().esds.aac; | 369 const AAC& aac = runs_->audio_description().esds.aac; |
300 RCHECK(aac.ConvertEsdsToADTS(&frame_buf)); | 370 RCHECK(aac.ConvertEsdsToADTS(&frame_buf)); |
301 } | 371 } |
302 | 372 |
303 scoped_refptr<StreamParserBuffer> stream_buf = | 373 scoped_refptr<StreamParserBuffer> stream_buf = |
304 StreamParserBuffer::CopyFrom(&frame_buf[0], frame_buf.size(), | 374 StreamParserBuffer::CopyFrom(&frame_buf[0], frame_buf.size(), |
305 runs_->is_keyframe()); | 375 runs_->is_keyframe()); |
306 | 376 |
377 if (runs_->is_encrypted()) | |
378 stream_buf->SetDecryptConfig(decrypt_config.Pass()); | |
379 | |
307 stream_buf->SetDuration(runs_->duration()); | 380 stream_buf->SetDuration(runs_->duration()); |
308 stream_buf->SetTimestamp(runs_->cts()); | 381 stream_buf->SetTimestamp(runs_->cts()); |
309 stream_buf->SetDecodeTimestamp(runs_->dts()); | 382 stream_buf->SetDecodeTimestamp(runs_->dts()); |
310 | 383 |
311 DVLOG(3) << "Pushing frame: aud=" << audio | 384 DVLOG(3) << "Pushing frame: aud=" << audio |
312 << ", key=" << runs_->is_keyframe() | 385 << ", key=" << runs_->is_keyframe() |
313 << ", dur=" << runs_->duration().InMilliseconds() | 386 << ", dur=" << runs_->duration().InMilliseconds() |
314 << ", dts=" << runs_->dts().InMilliseconds() | 387 << ", dts=" << runs_->dts().InMilliseconds() |
315 << ", cts=" << runs_->cts().InMilliseconds() | 388 << ", cts=" << runs_->cts().InMilliseconds() |
316 << ", size=" << runs_->sample_size(); | 389 << ", size=" << runs_->sample_size(); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
365 return true; | 438 return true; |
366 } | 439 } |
367 | 440 |
368 void MP4StreamParser::ChangeState(State new_state) { | 441 void MP4StreamParser::ChangeState(State new_state) { |
369 DVLOG(2) << "Changing state: " << new_state; | 442 DVLOG(2) << "Changing state: " << new_state; |
370 state_ = new_state; | 443 state_ = new_state; |
371 } | 444 } |
372 | 445 |
373 } // namespace mp4 | 446 } // namespace mp4 |
374 } // namespace media | 447 } // namespace media |
OLD | NEW |