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