Roll ANGLE e754fb8..6ffeb74
[chromium-blink-merge.git] / media / formats / mp2t / es_parser_mpeg1audio.cc
blob51176140e3a07d6bda830cd5d7955a2ac64a6a22
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"
8 #include "base/basictypes.h"
9 #include "base/bind.h"
10 #include "base/logging.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "media/base/audio_timestamp_helper.h"
13 #include "media/base/bit_reader.h"
14 #include "media/base/buffers.h"
15 #include "media/base/channel_layout.h"
16 #include "media/base/stream_parser_buffer.h"
17 #include "media/formats/common/offset_byte_queue.h"
18 #include "media/formats/mp2t/mp2t_common.h"
19 #include "media/formats/mpeg/mpeg1_audio_stream_parser.h"
21 namespace media {
22 namespace mp2t {
24 struct EsParserMpeg1Audio::Mpeg1AudioFrame {
25 // Pointer to the ES data.
26 const uint8* data;
28 // Frame size.
29 int size;
31 // Number of samples in the frame.
32 int sample_count;
34 // Frame offset in the ES queue.
35 int64 queue_offset;
38 EsParserMpeg1Audio::EsParserMpeg1Audio(
39 const NewAudioConfigCB& new_audio_config_cb,
40 const EmitBufferCB& emit_buffer_cb,
41 const LogCB& log_cb)
42 : log_cb_(log_cb),
43 new_audio_config_cb_(new_audio_config_cb),
44 emit_buffer_cb_(emit_buffer_cb) {
47 EsParserMpeg1Audio::~EsParserMpeg1Audio() {
50 bool EsParserMpeg1Audio::ParseFromEsQueue() {
51 // Look for every MPEG1 audio frame in the ES buffer.
52 Mpeg1AudioFrame mpeg1audio_frame;
53 while (LookForMpeg1AudioFrame(&mpeg1audio_frame)) {
54 // Update the audio configuration if needed.
55 DCHECK_GE(mpeg1audio_frame.size, MPEG1AudioStreamParser::kHeaderSize);
56 if (!UpdateAudioConfiguration(mpeg1audio_frame.data))
57 return false;
59 // Get the PTS & the duration of this access unit.
60 TimingDesc current_timing_desc =
61 GetTimingDescriptor(mpeg1audio_frame.queue_offset);
62 if (current_timing_desc.pts != kNoTimestamp())
63 audio_timestamp_helper_->SetBaseTimestamp(current_timing_desc.pts);
65 if (audio_timestamp_helper_->base_timestamp() == kNoTimestamp()) {
66 DVLOG(1) << "Skipping audio frame with unknown timestamp";
67 SkipMpeg1AudioFrame(mpeg1audio_frame);
68 continue;
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,
84 is_key_frame,
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);
97 return true;
100 void EsParserMpeg1Audio::Flush() {
103 void EsParserMpeg1Audio::ResetInternal() {
104 last_audio_decoder_config_ = AudioDecoderConfig();
107 bool EsParserMpeg1Audio::LookForMpeg1AudioFrame(
108 Mpeg1AudioFrame* mpeg1audio_frame) {
109 int es_size;
110 const uint8* es;
111 es_queue_->Peek(&es, &es_size);
113 int max_offset = es_size - MPEG1AudioStreamParser::kHeaderSize;
114 if (max_offset <= 0)
115 return false;
117 for (int offset = 0; offset < max_offset; offset++) {
118 const uint8* cur_buf = &es[offset];
119 if (cur_buf[0] != 0xff)
120 continue;
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))
126 continue;
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);
133 return false;
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) {
140 continue;
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;
148 DVLOG(LOG_LEVEL_ES)
149 << "MPEG1 audio syncword @ pos=" << mpeg1audio_frame->queue_offset
150 << " frame_size=" << mpeg1audio_frame->size;
151 DVLOG(LOG_LEVEL_ES)
152 << "MPEG1 audio header: "
153 << base::HexEncode(mpeg1audio_frame->data,
154 MPEG1AudioStreamParser::kHeaderSize);
155 return true;
158 es_queue_->Pop(max_offset);
159 return false;
162 bool EsParserMpeg1Audio::UpdateAudioConfiguration(
163 const uint8* mpeg1audio_header) {
164 MPEG1AudioStreamParser::Header header;
165 if (!MPEG1AudioStreamParser::ParseHeader(log_cb_,
166 mpeg1audio_header,
167 &header)) {
168 return false;
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(
174 kCodecMP3,
175 kSampleFormatS16,
176 header.channel_layout,
177 header.sample_rate,
178 NULL, 0,
179 false);
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);
191 } else {
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);
200 return true;
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);
209 } // namespace mp2t
210 } // namespace media