This source file includes following definitions.
- ParseFrameHeader
#include "media/formats/mpeg/mp3_stream_parser.h"
namespace media {
static const uint32 kMP3StartCodeMask = 0xffe00000;
static const bool kIsAllowed[17][4] = {
{ true, true, true, true },
{ true, false, false, false },
{ true, false, false, false },
{ true, false, false, false },
{ true, true, true, true },
{ true, false, false, false },
{ true, true, true, true },
{ true, true, true, true },
{ true, true, true, true },
{ true, true, true, true },
{ true, true, true, true },
{ false, true, true, true },
{ false, true, true, true },
{ false, true, true, true },
{ false, true, true, true },
{ false, false, false, false }
};
static const int kVersionLayerMap[4][4] = {
{ 5, 4, 4, 3 },
{ 5, 5, 5, 5 },
{ 5, 4, 4, 3 },
{ 5, 2, 1, 0 }
};
static const int kBitrateMap[16][6] = {
{ 0, 0, 0, 0, 0, 0 },
{ 32, 32, 32, 32, 8, 0 },
{ 64, 48, 40, 48, 16, 0 },
{ 96, 56, 48, 56, 24, 0 },
{ 128, 64, 56, 64, 32, 0 },
{ 160, 80, 64, 80, 40, 0 },
{ 192, 96, 80, 96, 48, 0 },
{ 224, 112, 96, 112, 56, 0 },
{ 256, 128, 112, 128, 64, 0 },
{ 288, 160, 128, 144, 80, 0 },
{ 320, 192, 160, 160, 96, 0 },
{ 352, 224, 192, 176, 112, 0 },
{ 384, 256, 224, 192, 128, 0 },
{ 416, 320, 256, 224, 144, 0 },
{ 448, 384, 320, 256, 160, 0 },
{ 0, 0, 0, 0, 0}
};
static const int kSampleRateMap[4][4] = {
{ 11025, 0, 22050, 44100 },
{ 12000, 0, 24000, 48000 },
{ 8000, 0, 16000, 32000 },
{ 0, 0, 0, 0 }
};
static const int kVersion2 = 2;
static const int kVersionReserved = 1;
static const int kVersion2_5 = 0;
static const int kLayerReserved = 0;
static const int kLayer1 = 3;
static const int kLayer2 = 2;
static const int kLayer3 = 1;
static const int kBitrateFree = 0;
static const int kBitrateBad = 0xf;
static const int kSampleRateReserved = 3;
MP3StreamParser::MP3StreamParser()
: MPEGAudioStreamParserBase(kMP3StartCodeMask, kCodecMP3) {}
MP3StreamParser::~MP3StreamParser() {}
int MP3StreamParser::ParseFrameHeader(const uint8* data,
int size,
int* frame_size,
int* sample_rate,
ChannelLayout* channel_layout,
int* sample_count) const {
DCHECK(data);
DCHECK_GE(size, 0);
DCHECK(frame_size);
if (size < 4)
return 0;
BitReader reader(data, size);
int sync;
int version;
int layer;
int is_protected;
int bitrate_index;
int sample_rate_index;
int has_padding;
int is_private;
int channel_mode;
int other_flags;
if (!reader.ReadBits(11, &sync) ||
!reader.ReadBits(2, &version) ||
!reader.ReadBits(2, &layer) ||
!reader.ReadBits(1, &is_protected) ||
!reader.ReadBits(4, &bitrate_index) ||
!reader.ReadBits(2, &sample_rate_index) ||
!reader.ReadBits(1, &has_padding) ||
!reader.ReadBits(1, &is_private) ||
!reader.ReadBits(2, &channel_mode) ||
!reader.ReadBits(6, &other_flags)) {
return -1;
}
DVLOG(2) << "Header data :" << std::hex
<< " sync 0x" << sync
<< " version 0x" << version
<< " layer 0x" << layer
<< " bitrate_index 0x" << bitrate_index
<< " sample_rate_index 0x" << sample_rate_index
<< " channel_mode 0x" << channel_mode;
if (sync != 0x7ff ||
version == kVersionReserved ||
layer == kLayerReserved ||
bitrate_index == kBitrateFree || bitrate_index == kBitrateBad ||
sample_rate_index == kSampleRateReserved) {
MEDIA_LOG(log_cb()) << "Invalid header data :" << std::hex
<< " sync 0x" << sync
<< " version 0x" << version
<< " layer 0x" << layer
<< " bitrate_index 0x" << bitrate_index
<< " sample_rate_index 0x" << sample_rate_index
<< " channel_mode 0x" << channel_mode;
return -1;
}
if (layer == kLayer2 && kIsAllowed[bitrate_index][channel_mode]) {
MEDIA_LOG(log_cb()) << "Invalid (bitrate_index, channel_mode) combination :"
<< std::hex
<< " bitrate_index " << bitrate_index
<< " channel_mode " << channel_mode;
return -1;
}
int bitrate = kBitrateMap[bitrate_index][kVersionLayerMap[version][layer]];
if (bitrate == 0) {
MEDIA_LOG(log_cb()) << "Invalid bitrate :" << std::hex
<< " version " << version
<< " layer " << layer
<< " bitrate_index " << bitrate_index;
return -1;
}
DVLOG(2) << " bitrate " << bitrate;
int frame_sample_rate = kSampleRateMap[sample_rate_index][version];
if (frame_sample_rate == 0) {
MEDIA_LOG(log_cb()) << "Invalid sample rate :" << std::hex
<< " version " << version
<< " sample_rate_index " << sample_rate_index;
return -1;
}
if (sample_rate)
*sample_rate = frame_sample_rate;
int samples_per_frame;
switch (layer) {
case kLayer1:
samples_per_frame = 384;
break;
case kLayer2:
samples_per_frame = 1152;
break;
case kLayer3:
if (version == kVersion2 || version == kVersion2_5)
samples_per_frame = 576;
else
samples_per_frame = 1152;
break;
default:
return -1;
}
if (sample_count)
*sample_count = samples_per_frame;
if (layer == kLayer1) {
*frame_size = 4 * (12 * bitrate * 1000 / frame_sample_rate);
} else {
*frame_size =
((samples_per_frame / 8) * bitrate * 1000) / frame_sample_rate;
}
if (has_padding)
*frame_size += (layer == kLayer1) ? 4 : 1;
if (channel_layout) {
*channel_layout =
(channel_mode == 3) ? CHANNEL_LAYOUT_MONO : CHANNEL_LAYOUT_STEREO;
}
return 4;
}
}