Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "media/formats/mp2t/es_parser_mpeg1audio.h" | |
| 6 | |
| 7 #include <list> | |
| 8 | |
| 9 #include "base/basictypes.h" | |
| 10 #include "base/bind.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/strings/string_number_conversions.h" | |
| 13 #include "media/base/audio_timestamp_helper.h" | |
| 14 #include "media/base/bit_reader.h" | |
| 15 #include "media/base/buffers.h" | |
| 16 #include "media/base/channel_layout.h" | |
| 17 #include "media/base/media_log.h" | |
| 18 #include "media/base/stream_parser_buffer.h" | |
| 19 #include "media/formats/common/offset_byte_queue.h" | |
| 20 #include "media/formats/mp2t/mp2t_common.h" | |
| 21 #include "media/formats/mpeg/mpeg1_audio_stream_parser.h" | |
| 22 | |
| 23 namespace media { | |
| 24 namespace mp2t { | |
| 25 | |
| 26 static void DummyMediaLog(const std::string& s) { | |
| 27 } | |
| 28 | |
| 29 struct EsParserMpeg1Audio::Mpeg1AudioFrame { | |
| 30 // Pointer to the ES data. | |
| 31 const uint8* data; | |
| 32 | |
| 33 // Frame size. | |
| 34 int size; | |
| 35 | |
| 36 // Number of samples in the frame. | |
| 37 int sample_count; | |
| 38 | |
| 39 // Frame offset in the ES queue. | |
| 40 int64 queue_offset; | |
| 41 }; | |
| 42 | |
| 43 bool EsParserMpeg1Audio::LookForMpeg1AudioFrame( | |
|
acolwell GONE FROM CHROMIUM
2014/09/04 22:20:53
nit: Move this and the skip method to a later poin
damienv1
2014/09/05 20:06:13
Done.
| |
| 44 Mpeg1AudioFrame* mpeg1audio_frame) { | |
| 45 int es_size; | |
| 46 const uint8* es; | |
| 47 es_queue_->Peek(&es, &es_size); | |
| 48 | |
| 49 int max_offset = es_size - MPEG1AudioStreamParser::kHeaderSize; | |
| 50 if (max_offset <= 0) | |
| 51 return false; | |
| 52 | |
| 53 for (int offset = 0; offset < max_offset; offset++) { | |
| 54 const uint8* cur_buf = &es[offset]; | |
| 55 if (cur_buf[0] != 0xff) | |
| 56 continue; | |
| 57 | |
| 58 // TODO(damienv): properly supports media logs. | |
| 59 LogCB log_cb = base::Bind(&DummyMediaLog); | |
| 60 int remaining_size = es_size - offset; | |
| 61 DCHECK_GE(remaining_size, MPEG1AudioStreamParser::kHeaderSize); | |
| 62 MPEG1AudioStreamParser::Header header; | |
| 63 if (!MPEG1AudioStreamParser::ParseHeader(log_cb, cur_buf, &header)) | |
| 64 continue; | |
| 65 | |
| 66 if (remaining_size < header.frame_size) { | |
| 67 // Not a full frame: will resume when we have more data. | |
| 68 es_queue_->Pop(offset); | |
|
acolwell GONE FROM CHROMIUM
2014/09/04 22:20:53
nit: You might want to add some text here indicati
damienv1
2014/09/05 20:06:13
Done.
| |
| 69 return false; | |
| 70 } | |
| 71 | |
| 72 // Check whether there is another frame | |
| 73 // |frame_size| apart from the current one. | |
| 74 if (remaining_size >= header.frame_size + 2 && | |
| 75 cur_buf[header.frame_size] != 0xff) { | |
| 76 continue; | |
| 77 } | |
| 78 | |
| 79 es_queue_->Pop(offset); | |
| 80 es_queue_->Peek(&mpeg1audio_frame->data, &es_size); | |
| 81 mpeg1audio_frame->queue_offset = es_queue_->head(); | |
| 82 mpeg1audio_frame->size = header.frame_size; | |
| 83 mpeg1audio_frame->sample_count = header.sample_count; | |
| 84 DVLOG(LOG_LEVEL_ES) | |
| 85 << "MPEG1 audio syncword @ pos=" << mpeg1audio_frame->queue_offset | |
| 86 << " frame_size=" << mpeg1audio_frame->size; | |
| 87 DVLOG(LOG_LEVEL_ES) | |
| 88 << "MPEG1 audio header: " | |
| 89 << base::HexEncode(mpeg1audio_frame->data, | |
| 90 MPEG1AudioStreamParser::kHeaderSize); | |
| 91 return true; | |
| 92 } | |
| 93 | |
| 94 es_queue_->Pop(max_offset); | |
| 95 return false; | |
| 96 } | |
| 97 | |
| 98 void EsParserMpeg1Audio::SkipMpeg1AudioFrame( | |
| 99 const Mpeg1AudioFrame& mpeg1audio_frame) { | |
| 100 DCHECK_EQ(mpeg1audio_frame.queue_offset, es_queue_->head()); | |
| 101 es_queue_->Pop(mpeg1audio_frame.size); | |
| 102 } | |
| 103 | |
| 104 EsParserMpeg1Audio::EsParserMpeg1Audio( | |
| 105 const NewAudioConfigCB& new_audio_config_cb, | |
| 106 const EmitBufferCB& emit_buffer_cb) | |
| 107 : new_audio_config_cb_(new_audio_config_cb), | |
| 108 emit_buffer_cb_(emit_buffer_cb) { | |
| 109 } | |
| 110 | |
| 111 EsParserMpeg1Audio::~EsParserMpeg1Audio() { | |
| 112 } | |
| 113 | |
| 114 bool EsParserMpeg1Audio::ParseFromEsQueue() { | |
| 115 // Look for every MPEG1 audio frame in the ES buffer. | |
| 116 Mpeg1AudioFrame mpeg1audio_frame; | |
| 117 while (LookForMpeg1AudioFrame(&mpeg1audio_frame)) { | |
| 118 // Update the audio configuration if needed. | |
| 119 DCHECK_GE(mpeg1audio_frame.size, MPEG1AudioStreamParser::kHeaderSize); | |
| 120 if (!UpdateAudioConfiguration(mpeg1audio_frame.data)) | |
| 121 return false; | |
| 122 | |
| 123 // Get the PTS & the duration of this access unit. | |
| 124 TimingDesc current_timing_desc = | |
| 125 GetTimingDescriptor(mpeg1audio_frame.queue_offset); | |
| 126 if (current_timing_desc.pts != kNoTimestamp()) | |
| 127 audio_timestamp_helper_->SetBaseTimestamp(current_timing_desc.pts); | |
|
acolwell GONE FROM CHROMIUM
2014/09/04 22:20:53
It seems like this could introduce drift. Shouldn'
damienv1
2014/09/05 20:06:13
I think the parser cannot do any assumption about
| |
| 128 | |
| 129 if (audio_timestamp_helper_->base_timestamp() == kNoTimestamp()) { | |
| 130 DVLOG(1) << "Audio frame with unknown timestamp"; | |
| 131 return false; | |
| 132 } | |
| 133 base::TimeDelta current_pts = audio_timestamp_helper_->GetTimestamp(); | |
| 134 base::TimeDelta frame_duration = | |
| 135 audio_timestamp_helper_->GetFrameDuration( | |
| 136 mpeg1audio_frame.sample_count); | |
| 137 | |
| 138 // Emit an audio frame. | |
| 139 bool is_key_frame = true; | |
| 140 | |
| 141 // TODO(wolenetz/acolwell): Validate and use a common cross-parser TrackId | |
| 142 // type and allow multiple audio tracks. See https://crbug.com/341581. | |
| 143 scoped_refptr<StreamParserBuffer> stream_parser_buffer = | |
| 144 StreamParserBuffer::CopyFrom( | |
| 145 mpeg1audio_frame.data, | |
| 146 mpeg1audio_frame.size, | |
| 147 is_key_frame, | |
| 148 DemuxerStream::AUDIO, 0); | |
| 149 stream_parser_buffer->set_timestamp(current_pts); | |
| 150 stream_parser_buffer->set_duration(frame_duration); | |
| 151 emit_buffer_cb_.Run(stream_parser_buffer); | |
| 152 | |
| 153 // Update the PTS of the next frame. | |
| 154 audio_timestamp_helper_->AddFrames(mpeg1audio_frame.sample_count); | |
| 155 | |
| 156 // Skip the current frame. | |
| 157 SkipMpeg1AudioFrame(mpeg1audio_frame); | |
| 158 } | |
| 159 | |
| 160 return true; | |
| 161 } | |
| 162 | |
| 163 void EsParserMpeg1Audio::Flush() { | |
| 164 } | |
| 165 | |
| 166 void EsParserMpeg1Audio::ResetInternal() { | |
| 167 last_audio_decoder_config_ = AudioDecoderConfig(); | |
| 168 } | |
| 169 | |
| 170 bool EsParserMpeg1Audio::UpdateAudioConfiguration( | |
| 171 const uint8* mpeg1audio_header) { | |
| 172 // TODO(damienv): properly supports media logs. | |
| 173 LogCB log_cb = base::Bind(&DummyMediaLog); | |
|
acolwell GONE FROM CHROMIUM
2014/09/04 22:20:53
nit: You should probably just make this a member v
damienv1
2014/09/05 20:06:13
Seems to be a good idea :)
| |
| 174 MPEG1AudioStreamParser::Header header; | |
| 175 if (!MPEG1AudioStreamParser::ParseHeader(log_cb, | |
| 176 mpeg1audio_header, | |
| 177 &header)) { | |
| 178 return false; | |
| 179 } | |
| 180 | |
| 181 // TODO(damienv): Verify whether Android playback requires the extra data | |
| 182 // field for Mpeg1 audio. If yes, we should generate this field. | |
| 183 AudioDecoderConfig audio_decoder_config( | |
| 184 kCodecMP3, | |
| 185 kSampleFormatS16, | |
| 186 header.channel_layout, | |
| 187 header.sample_rate, | |
| 188 NULL, 0, | |
| 189 false); | |
| 190 | |
| 191 if (!audio_decoder_config.Matches(last_audio_decoder_config_)) { | |
| 192 DVLOG(1) << "Sampling frequency: " << header.sample_rate; | |
| 193 // Reset the timestamp helper to use a new time scale. | |
| 194 if (audio_timestamp_helper_ && | |
| 195 audio_timestamp_helper_->base_timestamp() != kNoTimestamp()) { | |
| 196 base::TimeDelta base_timestamp = audio_timestamp_helper_->GetTimestamp(); | |
| 197 audio_timestamp_helper_.reset( | |
| 198 new AudioTimestampHelper(header.sample_rate)); | |
| 199 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp); | |
| 200 } else { | |
| 201 audio_timestamp_helper_.reset( | |
| 202 new AudioTimestampHelper(header.sample_rate)); | |
| 203 } | |
| 204 // Audio config notification. | |
| 205 last_audio_decoder_config_ = audio_decoder_config; | |
| 206 new_audio_config_cb_.Run(audio_decoder_config); | |
| 207 } | |
| 208 | |
| 209 return true; | |
| 210 } | |
| 211 | |
| 212 } // namespace mp2t | |
| 213 } // namespace media | |
| OLD | NEW |