| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/formats/mp2t/es_parser_adts.h" | 5 #include "media/formats/mp2t/es_parser_adts.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | |
| 10 #include "base/logging.h" | 9 #include "base/logging.h" |
| 11 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 12 #include "media/base/audio_timestamp_helper.h" | 11 #include "media/base/audio_timestamp_helper.h" |
| 13 #include "media/base/bit_reader.h" | 12 #include "media/base/bit_reader.h" |
| 14 #include "media/base/channel_layout.h" | 13 #include "media/base/channel_layout.h" |
| 15 #include "media/base/stream_parser_buffer.h" | 14 #include "media/base/stream_parser_buffer.h" |
| 16 #include "media/base/timestamp_constants.h" | 15 #include "media/base/timestamp_constants.h" |
| 17 #include "media/formats/common/offset_byte_queue.h" | 16 #include "media/formats/common/offset_byte_queue.h" |
| 18 #include "media/formats/mp2t/mp2t_common.h" | 17 #include "media/formats/mp2t/mp2t_common.h" |
| 19 #include "media/formats/mpeg/adts_constants.h" | 18 #include "media/formats/mpeg/adts_constants.h" |
| 20 | 19 |
| 21 namespace media { | 20 namespace media { |
| 22 | 21 |
| 23 static int ExtractAdtsFrameSize(const uint8* adts_header) { | 22 static int ExtractAdtsFrameSize(const uint8_t* adts_header) { |
| 24 return ((static_cast<int>(adts_header[5]) >> 5) | | 23 return ((static_cast<int>(adts_header[5]) >> 5) | |
| 25 (static_cast<int>(adts_header[4]) << 3) | | 24 (static_cast<int>(adts_header[4]) << 3) | |
| 26 ((static_cast<int>(adts_header[3]) & 0x3) << 11)); | 25 ((static_cast<int>(adts_header[3]) & 0x3) << 11)); |
| 27 } | 26 } |
| 28 | 27 |
| 29 static size_t ExtractAdtsFrequencyIndex(const uint8* adts_header) { | 28 static size_t ExtractAdtsFrequencyIndex(const uint8_t* adts_header) { |
| 30 return ((adts_header[2] >> 2) & 0xf); | 29 return ((adts_header[2] >> 2) & 0xf); |
| 31 } | 30 } |
| 32 | 31 |
| 33 static size_t ExtractAdtsChannelConfig(const uint8* adts_header) { | 32 static size_t ExtractAdtsChannelConfig(const uint8_t* adts_header) { |
| 34 return (((adts_header[3] >> 6) & 0x3) | | 33 return (((adts_header[3] >> 6) & 0x3) | |
| 35 ((adts_header[2] & 0x1) << 2)); | 34 ((adts_header[2] & 0x1) << 2)); |
| 36 } | 35 } |
| 37 | 36 |
| 38 // Return true if buf corresponds to an ADTS syncword. | 37 // Return true if buf corresponds to an ADTS syncword. |
| 39 // |buf| size must be at least 2. | 38 // |buf| size must be at least 2. |
| 40 static bool isAdtsSyncWord(const uint8* buf) { | 39 static bool isAdtsSyncWord(const uint8_t* buf) { |
| 41 // The first 12 bits must be 1. | 40 // The first 12 bits must be 1. |
| 42 // The layer field (2 bits) must be set to 0. | 41 // The layer field (2 bits) must be set to 0. |
| 43 return (buf[0] == 0xff) && ((buf[1] & 0xf6) == 0xf0); | 42 return (buf[0] == 0xff) && ((buf[1] & 0xf6) == 0xf0); |
| 44 } | 43 } |
| 45 | 44 |
| 46 namespace mp2t { | 45 namespace mp2t { |
| 47 | 46 |
| 48 struct EsParserAdts::AdtsFrame { | 47 struct EsParserAdts::AdtsFrame { |
| 49 // Pointer to the ES data. | 48 // Pointer to the ES data. |
| 50 const uint8* data; | 49 const uint8_t* data; |
| 51 | 50 |
| 52 // Frame size; | 51 // Frame size; |
| 53 int size; | 52 int size; |
| 54 | 53 |
| 55 // Frame offset in the ES queue. | 54 // Frame offset in the ES queue. |
| 56 int64 queue_offset; | 55 int64_t queue_offset; |
| 57 }; | 56 }; |
| 58 | 57 |
| 59 bool EsParserAdts::LookForAdtsFrame(AdtsFrame* adts_frame) { | 58 bool EsParserAdts::LookForAdtsFrame(AdtsFrame* adts_frame) { |
| 60 int es_size; | 59 int es_size; |
| 61 const uint8* es; | 60 const uint8_t* es; |
| 62 es_queue_->Peek(&es, &es_size); | 61 es_queue_->Peek(&es, &es_size); |
| 63 | 62 |
| 64 int max_offset = es_size - kADTSHeaderMinSize; | 63 int max_offset = es_size - kADTSHeaderMinSize; |
| 65 if (max_offset <= 0) | 64 if (max_offset <= 0) |
| 66 return false; | 65 return false; |
| 67 | 66 |
| 68 for (int offset = 0; offset < max_offset; offset++) { | 67 for (int offset = 0; offset < max_offset; offset++) { |
| 69 const uint8* cur_buf = &es[offset]; | 68 const uint8_t* cur_buf = &es[offset]; |
| 70 if (!isAdtsSyncWord(cur_buf)) | 69 if (!isAdtsSyncWord(cur_buf)) |
| 71 continue; | 70 continue; |
| 72 | 71 |
| 73 int frame_size = ExtractAdtsFrameSize(cur_buf); | 72 int frame_size = ExtractAdtsFrameSize(cur_buf); |
| 74 if (frame_size < kADTSHeaderMinSize) { | 73 if (frame_size < kADTSHeaderMinSize) { |
| 75 // Too short to be an ADTS frame. | 74 // Too short to be an ADTS frame. |
| 76 continue; | 75 continue; |
| 77 } | 76 } |
| 78 | 77 |
| 79 int remaining_size = es_size - offset; | 78 int remaining_size = es_size - offset; |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 return true; | 174 return true; |
| 176 } | 175 } |
| 177 | 176 |
| 178 void EsParserAdts::Flush() { | 177 void EsParserAdts::Flush() { |
| 179 } | 178 } |
| 180 | 179 |
| 181 void EsParserAdts::ResetInternal() { | 180 void EsParserAdts::ResetInternal() { |
| 182 last_audio_decoder_config_ = AudioDecoderConfig(); | 181 last_audio_decoder_config_ = AudioDecoderConfig(); |
| 183 } | 182 } |
| 184 | 183 |
| 185 bool EsParserAdts::UpdateAudioConfiguration(const uint8* adts_header) { | 184 bool EsParserAdts::UpdateAudioConfiguration(const uint8_t* adts_header) { |
| 186 size_t frequency_index = ExtractAdtsFrequencyIndex(adts_header); | 185 size_t frequency_index = ExtractAdtsFrequencyIndex(adts_header); |
| 187 if (frequency_index >= kADTSFrequencyTableSize) { | 186 if (frequency_index >= kADTSFrequencyTableSize) { |
| 188 // Frequency index 13 & 14 are reserved | 187 // Frequency index 13 & 14 are reserved |
| 189 // while 15 means that the frequency is explicitly written | 188 // while 15 means that the frequency is explicitly written |
| 190 // (not supported). | 189 // (not supported). |
| 191 return false; | 190 return false; |
| 192 } | 191 } |
| 193 | 192 |
| 194 size_t channel_configuration = ExtractAdtsChannelConfig(adts_header); | 193 size_t channel_configuration = ExtractAdtsChannelConfig(adts_header); |
| 195 if (channel_configuration == 0 || | 194 if (channel_configuration == 0 || |
| (...skipping 10 matching lines...) Expand all Loading... |
| 206 // The following code is written according to ISO 14496 Part 3 Table 1.11 and | 205 // The following code is written according to ISO 14496 Part 3 Table 1.11 and |
| 207 // Table 1.22. (Table 1.11 refers to the capping to 48000, Table 1.22 refers | 206 // Table 1.22. (Table 1.11 refers to the capping to 48000, Table 1.22 refers |
| 208 // to SBR doubling the AAC sample rate.) | 207 // to SBR doubling the AAC sample rate.) |
| 209 // TODO(damienv) : Extend sample rate cap to 96kHz for Level 5 content. | 208 // TODO(damienv) : Extend sample rate cap to 96kHz for Level 5 content. |
| 210 int extended_samples_per_second = sbr_in_mimetype_ | 209 int extended_samples_per_second = sbr_in_mimetype_ |
| 211 ? std::min(2 * samples_per_second, 48000) | 210 ? std::min(2 * samples_per_second, 48000) |
| 212 : samples_per_second; | 211 : samples_per_second; |
| 213 | 212 |
| 214 // The following code is written according to ISO 14496 Part 3 Table 1.13 - | 213 // The following code is written according to ISO 14496 Part 3 Table 1.13 - |
| 215 // Syntax of AudioSpecificConfig. | 214 // Syntax of AudioSpecificConfig. |
| 216 uint16 extra_data_int = static_cast<uint16>( | 215 uint16_t extra_data_int = static_cast<uint16_t>( |
| 217 // Note: adts_profile is in the range [0,3], since the ADTS header only | 216 // Note: adts_profile is in the range [0,3], since the ADTS header only |
| 218 // allows two bits for its value. | 217 // allows two bits for its value. |
| 219 ((adts_profile + 1) << 11) + | 218 ((adts_profile + 1) << 11) + |
| 220 // frequency_index is [0..13], per early out above. | 219 // frequency_index is [0..13], per early out above. |
| 221 (frequency_index << 7) + | 220 (frequency_index << 7) + |
| 222 // channel_configuration is [0..7], per early out above. | 221 // channel_configuration is [0..7], per early out above. |
| 223 (channel_configuration << 3)); | 222 (channel_configuration << 3)); |
| 224 std::vector<uint8_t> extra_data; | 223 std::vector<uint8_t> extra_data; |
| 225 extra_data.push_back(static_cast<uint8>(extra_data_int >> 8)); | 224 extra_data.push_back(static_cast<uint8_t>(extra_data_int >> 8)); |
| 226 extra_data.push_back(static_cast<uint8>(extra_data_int & 0xff)); | 225 extra_data.push_back(static_cast<uint8_t>(extra_data_int & 0xff)); |
| 227 | 226 |
| 228 AudioDecoderConfig audio_decoder_config( | 227 AudioDecoderConfig audio_decoder_config( |
| 229 kCodecAAC, | 228 kCodecAAC, |
| 230 kSampleFormatS16, | 229 kSampleFormatS16, |
| 231 kADTSChannelLayoutTable[channel_configuration], | 230 kADTSChannelLayoutTable[channel_configuration], |
| 232 extended_samples_per_second, | 231 extended_samples_per_second, |
| 233 extra_data, | 232 extra_data, |
| 234 false); | 233 false); |
| 235 | 234 |
| 236 if (!audio_decoder_config.Matches(last_audio_decoder_config_)) { | 235 if (!audio_decoder_config.Matches(last_audio_decoder_config_)) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 252 // Audio config notification. | 251 // Audio config notification. |
| 253 last_audio_decoder_config_ = audio_decoder_config; | 252 last_audio_decoder_config_ = audio_decoder_config; |
| 254 new_audio_config_cb_.Run(audio_decoder_config); | 253 new_audio_config_cb_.Run(audio_decoder_config); |
| 255 } | 254 } |
| 256 | 255 |
| 257 return true; | 256 return true; |
| 258 } | 257 } |
| 259 | 258 |
| 260 } // namespace mp2t | 259 } // namespace mp2t |
| 261 } // namespace media | 260 } // namespace media |
| OLD | NEW |