Index: media/mpeg2/es_parser_adts.cc |
diff --git a/media/mpeg2/es_parser_adts.cc b/media/mpeg2/es_parser_adts.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c21ac9edacd3bc6074579431ba8aeb9d91f67d70 |
--- /dev/null |
+++ b/media/mpeg2/es_parser_adts.cc |
@@ -0,0 +1,294 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "media/mpeg2/es_parser_adts.h" |
+ |
+#include <list> |
+ |
+#include "base/basictypes.h" |
+#include "base/logging.h" |
+#include "base/strings/string_number_conversions.h" |
+#include "media/base/audio_decoder_config.h" |
+#include "media/base/bit_reader.h" |
+#include "media/base/channel_layout.h" |
+#include "media/base/stream_parser_buffer.h" |
+#include "media/mpeg2/mpeg2ts_common.h" |
+ |
+namespace { |
acolwell GONE FROM CHROMIUM
2013/08/29 20:44:24
nit: Move these into the mpeg2 namespace and use s
damienv1
2013/09/04 01:37:14
According to http://www.chromium.org/developers/co
|
+// Adts header is at least 7 bytes (can be 9 bytes). |
+const int kAdtsHeaderMinSize = 7; |
+ |
+const int adts_frequency_table[16] = { |
+ 96000, |
+ 88200, |
+ 64000, |
+ 48000, |
+ 44100, |
+ 32000, |
+ 24000, |
+ 22050, |
+ 16000, |
+ 12000, |
+ 11025, |
+ 8000, |
+ 7350, |
+ 0, |
+ 0, |
+ 0, |
+}; |
+const int kExplicitFrequencyIndex = 15; |
+ |
+media::ChannelLayout adts_channel_layout[8] = { |
+ media::CHANNEL_LAYOUT_NONE, |
+ media::CHANNEL_LAYOUT_MONO, |
+ media::CHANNEL_LAYOUT_STEREO, |
+ media::CHANNEL_LAYOUT_SURROUND, |
+ media::CHANNEL_LAYOUT_4_0, |
+ media::CHANNEL_LAYOUT_5_0_BACK, |
+ media::CHANNEL_LAYOUT_5_1_BACK, |
+ media::CHANNEL_LAYOUT_7_1, |
+}; |
+ |
+// Number of samples per frame. |
+const int kNumberSamplesPerAACFrame = 1024; |
+const int kNumberSamplesPerHeAACFrame = 2048; |
+const int kNumberSamplesPerAACLcFrame = 960; |
+ |
+int ExtractAdtsFrameSize(const uint8* adts_header) { |
acolwell GONE FROM CHROMIUM
2013/08/29 20:44:24
nit: move these into the mpeg2 namespace and make
damienv1
2013/09/04 01:37:14
ditto.
If you have an updated coding guideline and
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
The majority of the media code uses static instead
|
+ int frame_size = |
+ (static_cast<int>(adts_header[5]) >> 5) | |
+ (static_cast<int>(adts_header[4]) << 3) | |
+ ((static_cast<int>(adts_header[3]) & 0x3) << 11); |
+ return frame_size; |
+} |
+ |
+int ExtractAdtsFrequencyIndex(const uint8* adts_header) { |
+ int frequency_index = |
+ (adts_header[2] >> 2) & 0xf; |
+ return frequency_index; |
+} |
+ |
+int ExtractAdtsChannelConfig(const uint8* adts_header) { |
+ int channel_config = |
+ ((adts_header[3] >> 6) & 0x3) | |
+ ((adts_header[2] & 0x1) << 2); |
+ return channel_config; |
+} |
+ |
+// Look for an ADTS syncword. |
acolwell GONE FROM CHROMIUM
2013/08/29 20:44:24
nit: Please document the parameters and return val
damienv1
2013/09/04 01:37:14
Done.
|
+bool LookForSyncWord(const std::vector<uint8>& buf, |
+ int pos, |
+ int* new_pos, int* frame_sz) { |
+ int max_offset = buf.size() - kAdtsHeaderMinSize; |
+ if (max_offset < 0) { |
acolwell GONE FROM CHROMIUM
2013/08/29 20:44:24
nit: remove {} for single line bodies here and eve
damienv1
2013/09/04 01:37:14
Done.
|
+ max_offset = 0; |
+ } |
+ |
+ for (int offset = pos; offset < max_offset; offset++) { |
+ const uint8* cur_buf = &buf[offset]; |
+ |
+ if ((cur_buf[0] != 0xff) || ((cur_buf[1] & 0xf6) != 0xf0)) { |
+ // The first 12 bits must be 1. |
+ // The layer field (2 bits) must be set to 0. |
+ continue; |
+ } |
+ |
+ int frequency_index = ExtractAdtsFrequencyIndex(cur_buf); |
+ if (frequency_index == kExplicitFrequencyIndex) { |
+ // 15 is a forbidden value. |
+ continue; |
+ } |
+ |
+ int frame_size = ExtractAdtsFrameSize(cur_buf); |
+ if (frame_size < kAdtsHeaderMinSize) { |
+ // Too short to be an ADTS frame. |
+ continue; |
+ } |
+ |
+ // Check whether there is another frame |
+ // |size| apart from the current one. |
+ int remaining_size = buf.size() - offset; |
+ if (remaining_size >= frame_size + 2) { |
+ if ((cur_buf[frame_size] != 0xff) || |
+ (cur_buf[frame_size + 1] & 0xf6) != 0xf0) { |
+ continue; |
+ } |
+ } |
+ |
+ *new_pos = offset; |
+ *frame_sz = frame_size; |
+ return true; |
+ } |
+ |
+ *new_pos = max_offset; |
+ return false; |
+} |
+ |
+} // namespace |
+ |
+namespace media { |
+namespace mpeg2ts { |
+ |
+EsParserAdts::EsParserAdts( |
+ NewAudioConfigCB new_audio_config_cb, |
+ EmitBufferCB emit_buffer_cb) |
+ : first_frame_(true), |
+ new_audio_config_cb_(new_audio_config_cb), |
+ emit_buffer_cb_(emit_buffer_cb), |
+ is_audio_config_known_(false), |
+ sampling_frequency_(0), |
+ channel_configuration_(0) { |
+} |
+ |
+EsParserAdts::~EsParserAdts() { |
+} |
+ |
+void EsParserAdts::Parse(const uint8* buf, int size, |
+ bool is_pts_valid, base::TimeDelta pts, |
+ bool is_dts_valid, base::TimeDelta dts) { |
+ // The incoming PTS applies to the access unit that comes just after |
+ // the beginning of |buf|. |
+ if (is_pts_valid) { |
+ pts_list_.push_back(EsPts(raw_es_.size(), pts)); |
+ } |
+ |
+ // Copy the input data to the ES buffer. |
+ int old_size = raw_es_.size(); |
+ raw_es_.resize(old_size + size); |
+ memcpy(&raw_es_[old_size], buf, size); |
acolwell GONE FROM CHROMIUM
2013/08/29 20:44:24
nit: use media::ByteQueue to avoid doing this stuf
damienv1
2013/09/04 01:37:14
Done.
|
+ |
+ // Look for every ADTS frame in the ES buffer starting at offset = 0 |
+ int es_position = 0; |
+ int frame_size; |
+ while (LookForSyncWord(raw_es_, es_position, |
+ &es_position, &frame_size)) { |
+ VLOG(LOG_LEVEL_ES) << "ADTS syncword @ pos=" << es_position |
+ << " frame_size=" << frame_size; |
+ VLOG(LOG_LEVEL_ES) << "ADTS header: " |
+ << base::HexEncode(&raw_es_[es_position], 7); |
+ |
+ // Do not process the frame if this one is a partial frame. |
+ int remaining_size = raw_es_.size() - es_position; |
+ if (frame_size > remaining_size) { |
+ break; |
+ } |
+ |
+ // Update the audio configuration if needed. |
+ DCHECK_GE(frame_size, kAdtsHeaderMinSize); |
+ UpdateAudioConfiguration(&raw_es_[es_position]); |
+ |
+ // Get the PTS of this access unit. |
+ base::TimeDelta current_pts = estimated_pts_; |
+ while (!pts_list_.empty() && |
+ pts_list_.front().first <= es_position) { |
+ current_pts = pts_list_.front().second; |
+ pts_list_.pop_front(); |
+ } |
+ VLOG(LOG_LEVEL_ES) |
+ << "Current PTS: " << current_pts.InMilliseconds() |
+ << " Estimated PTS: " << estimated_pts_.InMilliseconds(); |
+ |
+ // Verify that PTS is increasing. |
+ if (!first_frame_ && current_pts < last_frame_pts_) { |
+ LOG(WARNING) << "ADTS: pts not monotonic"; |
acolwell GONE FROM CHROMIUM
2013/08/29 20:44:24
This seems like it should be a DCHECK or at least
damienv1
2013/09/04 01:37:14
At the ES level, nothing is preventing an audio fr
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
My concern is that having code here makes me think
|
+ } |
+ first_frame_ = false; |
+ last_frame_pts_ = current_pts; |
+ |
+ // Emit an audio frame. |
+ bool is_key_frame = true; |
+ scoped_refptr<StreamParserBuffer> stream_parser_buffer = |
+ StreamParserBuffer::CopyFrom( |
+ &raw_es_[es_position], |
+ frame_size, |
+ is_key_frame); |
+ stream_parser_buffer->SetDecodeTimestamp(current_pts); |
+ stream_parser_buffer->set_timestamp(current_pts); |
acolwell GONE FROM CHROMIUM
2013/08/29 20:44:24
nit: Set the duration of the buffer too since it i
damienv1
2013/09/04 01:37:14
Done.
|
+ emit_buffer_cb_.Run(stream_parser_buffer); |
+ |
+ // Update the PTS of the next frame. |
+ base::TimeDelta frame_duration = |
+ base::TimeDelta::FromMicroseconds( |
+ (1000000 * kNumberSamplesPerAACFrame) / sampling_frequency_); |
acolwell GONE FROM CHROMIUM
2013/08/29 20:44:24
Use media::AudioTimestampHelper for this type of c
|
+ estimated_pts_ = current_pts + frame_duration; |
+ |
+ // Skip the current frame. |
+ es_position += frame_size; |
+ } |
+ |
+ // Discard all the bytes that have been processed. |
+ DiscardEs(es_position); |
+} |
+ |
+void EsParserAdts::Flush() { |
+ // All the complete frames have been emitted, |
+ // so just clear the ES buffer. |
+ raw_es_.clear(); |
+ pts_list_.clear(); |
+} |
+ |
+void EsParserAdts::UpdateAudioConfiguration(const uint8* adts_header) { |
+ int frequency_index = ExtractAdtsFrequencyIndex(adts_header); |
+ if (frequency_index > 12) { |
+ // Frequency index 13 & 14 are reserved |
+ // while 15 means that the frequency is explicitly written |
+ // (not supported). |
+ return; |
acolwell GONE FROM CHROMIUM
2013/08/29 20:44:24
This should probably cause a parse error and print
damienv1
2013/09/04 01:37:14
I slightly changed the behavior.
Now, ADTS syncwor
|
+ } |
+ int samples_per_second = adts_frequency_table[frequency_index]; |
+ |
+ int channel_configuration = ExtractAdtsChannelConfig(adts_header); |
+ int adts_profile = (adts_header[2] >> 6) & 0x3; |
+ |
+#if 0 |
acolwell GONE FROM CHROMIUM
2013/08/29 20:44:24
nit: Remove if this isn't going to be turned on in
damienv1
2013/09/04 01:37:14
Done.
|
+ // TODO(damienv): support HE-AAC frequency doubling (SBR) |
+ if (adts_profile == kAdtsProfileHeAAC) { |
+ samples_per_second *= 2; |
+ } |
+#endif |
+ |
+ if (!is_audio_config_known_ || |
+ sampling_frequency_ != samples_per_second || |
+ channel_configuration_ != channel_configuration) { |
+ is_audio_config_known_ = true; |
+ sampling_frequency_ = samples_per_second; |
+ channel_configuration_ = channel_configuration; |
+ |
+ LOG(INFO) << "Sampling frequency: " << samples_per_second; |
+ LOG(INFO) << "Channel config: " << channel_configuration; |
+ LOG(INFO) << "Adts profile: " << adts_profile; |
+ AudioDecoderConfig audio_decoder_config( |
+ kCodecAAC, |
+ kSampleFormatS16, |
+ adts_channel_layout[channel_configuration], |
+ samples_per_second, |
+ NULL, 0, |
+ false); |
+ new_audio_config_cb_.Run(audio_decoder_config); |
+ } |
+} |
+ |
+void EsParserAdts::DiscardEs(int nbytes) { |
+ if (nbytes <= 0) { |
+ return; |
+ } |
+ |
+ // Adjust the ES position of each PTS. |
+ EsPtsList::iterator it = pts_list_.begin(); |
+ for (; it != pts_list_.end(); ++it) { |
+ it->first -= nbytes; |
+ } |
+ |
+ // Discard |nbytes| of ES. |
+ int old_size = raw_es_.size(); |
+ int new_size = old_size - nbytes; |
+ CHECK_LE(nbytes, old_size); |
+ if (new_size > 0) { |
+ memmove(&raw_es_[0], &raw_es_[nbytes], new_size); |
+ } |
+ raw_es_.resize(new_size); |
+} |
+ |
+} // namespace mpeg2ts |
+} // namespace media |