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/mp2t/es_parser_mpeg1audio.h"
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"
25 struct EsParserMpeg1Audio::Mpeg1AudioFrame {
26 // Pointer to the ES data.
32 // Number of samples in the frame.
35 // Frame offset in the ES queue.
39 EsParserMpeg1Audio::EsParserMpeg1Audio(
40 const NewAudioConfigCB& new_audio_config_cb,
41 const EmitBufferCB& emit_buffer_cb,
44 new_audio_config_cb_(new_audio_config_cb),
45 emit_buffer_cb_(emit_buffer_cb) {
48 EsParserMpeg1Audio::~EsParserMpeg1Audio() {
51 bool EsParserMpeg1Audio::ParseFromEsQueue() {
52 // Look for every MPEG1 audio frame in the ES buffer.
53 Mpeg1AudioFrame mpeg1audio_frame;
54 while (LookForMpeg1AudioFrame(&mpeg1audio_frame)) {
55 // Update the audio configuration if needed.
56 DCHECK_GE(mpeg1audio_frame.size, MPEG1AudioStreamParser::kHeaderSize);
57 if (!UpdateAudioConfiguration(mpeg1audio_frame.data))
60 // Get the PTS & the duration of this access unit.
61 TimingDesc current_timing_desc =
62 GetTimingDescriptor(mpeg1audio_frame.queue_offset);
63 if (current_timing_desc.pts != kNoTimestamp())
64 audio_timestamp_helper_->SetBaseTimestamp(current_timing_desc.pts);
66 if (audio_timestamp_helper_->base_timestamp() == kNoTimestamp()) {
67 DVLOG(1) << "Audio frame with unknown timestamp";
70 base::TimeDelta current_pts = audio_timestamp_helper_->GetTimestamp();
71 base::TimeDelta frame_duration =
72 audio_timestamp_helper_->GetFrameDuration(
73 mpeg1audio_frame.sample_count);
75 // Emit an audio frame.
76 bool is_key_frame = true;
78 // TODO(wolenetz/acolwell): Validate and use a common cross-parser TrackId
79 // type and allow multiple audio tracks. See https://crbug.com/341581.
80 scoped_refptr<StreamParserBuffer> stream_parser_buffer =
81 StreamParserBuffer::CopyFrom(
82 mpeg1audio_frame.data,
83 mpeg1audio_frame.size,
85 DemuxerStream::AUDIO, 0);
86 stream_parser_buffer->set_timestamp(current_pts);
87 stream_parser_buffer->set_duration(frame_duration);
88 emit_buffer_cb_.Run(stream_parser_buffer);
90 // Update the PTS of the next frame.
91 audio_timestamp_helper_->AddFrames(mpeg1audio_frame.sample_count);
93 // Skip the current frame.
94 SkipMpeg1AudioFrame(mpeg1audio_frame);
100 void EsParserMpeg1Audio::Flush() {
103 void EsParserMpeg1Audio::ResetInternal() {
104 last_audio_decoder_config_ = AudioDecoderConfig();
107 bool EsParserMpeg1Audio::LookForMpeg1AudioFrame(
108 Mpeg1AudioFrame* mpeg1audio_frame) {
111 es_queue_->Peek(&es, &es_size);
113 int max_offset = es_size - MPEG1AudioStreamParser::kHeaderSize;
117 for (int offset = 0; offset < max_offset; offset++) {
118 const uint8* cur_buf = &es[offset];
119 if (cur_buf[0] != 0xff)
122 int remaining_size = es_size - offset;
123 DCHECK_GE(remaining_size, MPEG1AudioStreamParser::kHeaderSize);
124 MPEG1AudioStreamParser::Header header;
125 if (!MPEG1AudioStreamParser::ParseHeader(log_cb_, cur_buf, &header))
128 if (remaining_size < header.frame_size) {
129 // Not a full frame: will resume when we have more data.
130 // Remove all the bytes located before the frame header,
131 // these bytes will not be used anymore.
132 es_queue_->Pop(offset);
136 // Check whether there is another frame
137 // |frame_size| apart from the current one.
138 if (remaining_size >= header.frame_size + 1 &&
139 cur_buf[header.frame_size] != 0xff) {
143 es_queue_->Pop(offset);
144 es_queue_->Peek(&mpeg1audio_frame->data, &es_size);
145 mpeg1audio_frame->queue_offset = es_queue_->head();
146 mpeg1audio_frame->size = header.frame_size;
147 mpeg1audio_frame->sample_count = header.sample_count;
149 << "MPEG1 audio syncword @ pos=" << mpeg1audio_frame->queue_offset
150 << " frame_size=" << mpeg1audio_frame->size;
152 << "MPEG1 audio header: "
153 << base::HexEncode(mpeg1audio_frame->data,
154 MPEG1AudioStreamParser::kHeaderSize);
158 es_queue_->Pop(max_offset);
162 bool EsParserMpeg1Audio::UpdateAudioConfiguration(
163 const uint8* mpeg1audio_header) {
164 MPEG1AudioStreamParser::Header header;
165 if (!MPEG1AudioStreamParser::ParseHeader(log_cb_,
171 // TODO(damienv): Verify whether Android playback requires the extra data
172 // field for Mpeg1 audio. If yes, we should generate this field.
173 AudioDecoderConfig audio_decoder_config(
176 header.channel_layout,
181 if (!audio_decoder_config.Matches(last_audio_decoder_config_)) {
182 DVLOG(1) << "Sampling frequency: " << header.sample_rate;
183 DVLOG(1) << "Channel layout: " << header.channel_layout;
184 // Reset the timestamp helper to use a new time scale.
185 if (audio_timestamp_helper_ &&
186 audio_timestamp_helper_->base_timestamp() != kNoTimestamp()) {
187 base::TimeDelta base_timestamp = audio_timestamp_helper_->GetTimestamp();
188 audio_timestamp_helper_.reset(
189 new AudioTimestampHelper(header.sample_rate));
190 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp);
192 audio_timestamp_helper_.reset(
193 new AudioTimestampHelper(header.sample_rate));
195 // Audio config notification.
196 last_audio_decoder_config_ = audio_decoder_config;
197 new_audio_config_cb_.Run(audio_decoder_config);
203 void EsParserMpeg1Audio::SkipMpeg1AudioFrame(
204 const Mpeg1AudioFrame& mpeg1audio_frame) {
205 DCHECK_EQ(mpeg1audio_frame.queue_offset, es_queue_->head());
206 es_queue_->Pop(mpeg1audio_frame.size);