Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(519)

Side by Side Diff: media/formats/mp2t/es_parser_mpeg1audio.cc

Issue 506943003: Support MPEG1 audio in the MPEG2-TS stream parser. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Refactoring to reuse code from MP3StreamParser. Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/mp3_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(
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 - MP3StreamParser::kMpeg1AudioHeaderSize;
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, MP3StreamParser::kMpeg1AudioHeaderSize);
62 MP3StreamParser::Mpeg1AudioHeader header;
63 if (!MP3StreamParser::ParserMpeg1AudioFrameHeader(log_cb,
64 cur_buf,
65 &header)) {
66 continue;
67 }
68
69 if (remaining_size < header.frame_size) {
70 // Not a full frame: will resume when we have more data.
71 es_queue_->Pop(offset);
72 return false;
73 }
74
75 // Check whether there is another frame
76 // |frame_size| apart from the current one.
77 if (remaining_size >= header.frame_size + 2 &&
78 cur_buf[header.frame_size] != 0xff) {
79 continue;
80 }
81
82 es_queue_->Pop(offset);
83 es_queue_->Peek(&mpeg1audio_frame->data, &es_size);
84 mpeg1audio_frame->queue_offset = es_queue_->head();
85 mpeg1audio_frame->size = header.frame_size;
86 mpeg1audio_frame->sample_count = header.sample_count;
87 DVLOG(LOG_LEVEL_ES)
88 << "MPEG1 audio syncword @ pos=" << mpeg1audio_frame->queue_offset
89 << " frame_size=" << mpeg1audio_frame->size;
90 DVLOG(LOG_LEVEL_ES)
91 << "MPEG1 audio header: "
92 << base::HexEncode(mpeg1audio_frame->data,
93 MP3StreamParser::kMpeg1AudioHeaderSize);
94 return true;
95 }
96
97 es_queue_->Pop(max_offset);
98 return false;
99 }
100
101 void EsParserMpeg1Audio::SkipMpeg1AudioFrame(
102 const Mpeg1AudioFrame& mpeg1audio_frame) {
103 DCHECK_EQ(mpeg1audio_frame.queue_offset, es_queue_->head());
104 es_queue_->Pop(mpeg1audio_frame.size);
105 }
106
107 EsParserMpeg1Audio::EsParserMpeg1Audio(
108 const NewAudioConfigCB& new_audio_config_cb,
109 const EmitBufferCB& emit_buffer_cb)
110 : new_audio_config_cb_(new_audio_config_cb),
111 emit_buffer_cb_(emit_buffer_cb) {
112 }
113
114 EsParserMpeg1Audio::~EsParserMpeg1Audio() {
115 }
116
117 bool EsParserMpeg1Audio::ParseFromEsQueue() {
118 // Look for every MPEG1 audio frame in the ES buffer.
119 Mpeg1AudioFrame mpeg1audio_frame;
120 while (LookForMpeg1AudioFrame(&mpeg1audio_frame)) {
121 // Update the audio configuration if needed.
122 DCHECK_GE(mpeg1audio_frame.size, MP3StreamParser::kMpeg1AudioHeaderSize);
123 if (!UpdateAudioConfiguration(mpeg1audio_frame.data))
124 return false;
125
126 // Get the PTS & the duration of this access unit.
127 TimingDesc current_timing_desc =
128 GetTimingDescriptor(mpeg1audio_frame.queue_offset);
129 if (current_timing_desc.pts != kNoTimestamp())
130 audio_timestamp_helper_->SetBaseTimestamp(current_timing_desc.pts);
131
132 if (audio_timestamp_helper_->base_timestamp() == kNoTimestamp()) {
133 DVLOG(1) << "Audio frame with unknown timestamp";
134 return false;
135 }
136 base::TimeDelta current_pts = audio_timestamp_helper_->GetTimestamp();
137 base::TimeDelta frame_duration =
138 audio_timestamp_helper_->GetFrameDuration(
139 mpeg1audio_frame.sample_count);
140
141 // Emit an audio frame.
142 bool is_key_frame = true;
143
144 // TODO(wolenetz/acolwell): Validate and use a common cross-parser TrackId
145 // type and allow multiple audio tracks. See https://crbug.com/341581.
146 scoped_refptr<StreamParserBuffer> stream_parser_buffer =
147 StreamParserBuffer::CopyFrom(
148 mpeg1audio_frame.data,
149 mpeg1audio_frame.size,
150 is_key_frame,
151 DemuxerStream::AUDIO, 0);
152 stream_parser_buffer->set_timestamp(current_pts);
153 stream_parser_buffer->set_duration(frame_duration);
154 emit_buffer_cb_.Run(stream_parser_buffer);
155
156 // Update the PTS of the next frame.
157 audio_timestamp_helper_->AddFrames(mpeg1audio_frame.sample_count);
158
159 // Skip the current frame.
160 SkipMpeg1AudioFrame(mpeg1audio_frame);
161 }
162
163 return true;
164 }
165
166 void EsParserMpeg1Audio::Flush() {
167 }
168
169 void EsParserMpeg1Audio::ResetInternal() {
170 last_audio_decoder_config_ = AudioDecoderConfig();
171 }
172
173 bool EsParserMpeg1Audio::UpdateAudioConfiguration(
174 const uint8* mpeg1audio_header) {
175 // TODO(damienv): properly supports media logs.
176 LogCB log_cb = base::Bind(&DummyMediaLog);
177 MP3StreamParser::Mpeg1AudioHeader header;
178 if (!MP3StreamParser::ParserMpeg1AudioFrameHeader(log_cb,
179 mpeg1audio_header,
180 &header)) {
181 return false;
182 }
183
184 // TODO(damienv): Verify whether Android playback requires the extra data
185 // field for Mpeg1 audio. If yes, we should generate this field.
186 AudioDecoderConfig audio_decoder_config(
187 kCodecMP3,
188 kSampleFormatS16,
189 header.channel_layout,
190 header.sample_rate,
191 NULL, 0,
192 false);
193
194 if (!audio_decoder_config.Matches(last_audio_decoder_config_)) {
195 DVLOG(1) << "Sampling frequency: " << header.sample_rate;
196 // Reset the timestamp helper to use a new time scale.
197 if (audio_timestamp_helper_ &&
198 audio_timestamp_helper_->base_timestamp() != kNoTimestamp()) {
199 base::TimeDelta base_timestamp = audio_timestamp_helper_->GetTimestamp();
200 audio_timestamp_helper_.reset(
201 new AudioTimestampHelper(header.sample_rate));
202 audio_timestamp_helper_->SetBaseTimestamp(base_timestamp);
203 } else {
204 audio_timestamp_helper_.reset(
205 new AudioTimestampHelper(header.sample_rate));
206 }
207 // Audio config notification.
208 last_audio_decoder_config_ = audio_decoder_config;
209 new_audio_config_cb_.Run(audio_decoder_config);
210 }
211
212 return true;
213 }
214
215 } // namespace mp2t
216 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698