1 #include "packager/media/formats/mp2t/mpeg1_header.h"
3 #include "packager/media/base/bit_reader.h"
4 #include "packager/media/base/bit_writer.h"
5 #include "packager/media/formats/mp2t/mp2t_common.h"
10 const size_t kMpeg1HeaderMinSize = 4;
12 const uint8_t kMpeg1V_INV = 0b01;
13 const uint8_t kMpeg1L_INV = 0b00;
15 const uint8_t kMpeg1L_3 = 0b01;
16 const uint8_t kMpeg1L_2 = 0b10;
17 const uint8_t kMpeg1L_1 = 0b11;
19 const size_t kMpeg1SamplesPerFrameTable[] = {
23 const uint32_t kMpeg1SampleRateTable[][3] = {
26 {44100, 22050, 11025},
27 {48000, 24000, 12000},
28 {32000, 16000, 8000}};
30 const size_t kMpeg1SampleRateTableSize = arraysize(kMpeg1SampleRateTable);
32 static inline uint32_t Mpeg1SampleRate(uint8_t sr_idx, uint8_t version) {
33 static int sr_version_indexes[] = {2, -1, 1, 0};
34 DCHECK_NE(version, 1);
35 DCHECK_LT(sr_idx, kMpeg1SampleRateTableSize);
36 return kMpeg1SampleRateTable[sr_idx][sr_version_indexes[version]];
39 const uint32_t kMpeg1BitrateTable[][5] = {
44 { 64, 48, 40, 48, 16},
45 { 96, 56, 48, 56, 24},
46 { 128, 64, 56, 64, 32},
47 { 160, 80, 64, 80, 40},
48 { 192, 96, 80, 96, 48},
49 { 224, 112, 96, 112, 56},
50 { 256, 128, 112, 128, 64},
51 { 288, 160, 128, 144, 80},
52 { 320, 192, 160, 160, 96},
53 { 352, 224, 192, 176, 112},
54 { 384, 256, 224, 192, 128},
55 { 416, 320, 256, 224, 144},
56 { 448, 384, 320, 256, 160}};
58 const size_t kMpeg1BitrateTableSize = arraysize(kMpeg1BitrateTable);
60 static inline uint32_t Mpeg1BitRate(uint8_t btr_idx,
63 static int btr_version_indexes[] = {1, -1, 1, 0};
64 static int btr_layer_indexes[] = {-1, 2, 1, 0};
66 DCHECK_NE(version, 1);
68 int vidx = btr_version_indexes[version];
69 int lidx = btr_layer_indexes[layer];
70 if (vidx == 1 && lidx > 1)
73 DCHECK_LT(vidx * 3 + lidx, 5);
74 DCHECK_LT(btr_idx, kMpeg1BitrateTableSize);
75 return kMpeg1BitrateTable[btr_idx][vidx * 3 + lidx] * 1000;
78 static inline size_t Mpeg1FrameSize(uint8_t layer,
82 DCHECK_GT(sample_rate,
static_cast<uint32_t
>(0));
83 if (layer == kMpeg1L_1)
84 return (12 * bitrate / sample_rate + padded) * 4;
85 return 144 * bitrate / sample_rate + padded;
95 return (buf[0] == 0xff) &&
96 ((buf[1] & 0b11100000) == 0b11100000)
98 && ((buf[1] & 0b00011000) != 0b00001000)
100 && ((buf[1] & 0b00000110) != 0b00000000);
104 return kMpeg1HeaderMinSize + 1;
108 static int spf_layer_indexes[] = {-1, 2, 1, 0};
109 DCHECK_NE(layer_, 0);
110 return kMpeg1SamplesPerFrameTable[spf_layer_indexes[layer_]];
116 if (mpeg1_frame_size < kMpeg1HeaderMinSize)
119 BitReader frame(mpeg1_frame, mpeg1_frame_size);
123 RCHECK(sync == 0x7ff);
125 RCHECK(frame.
ReadBits(2, &version_));
126 RCHECK(version_ != kMpeg1V_INV);
128 RCHECK(layer_ != kMpeg1L_INV);
129 RCHECK(frame.
ReadBits(1, &protection_absent_));
132 RCHECK(frame.
ReadBits(4, &btr_idx));
134 bitrate_ = Mpeg1BitRate(btr_idx, version_, layer_);
138 RCHECK(sr_idx < 0b11);
139 sample_rate_ = Mpeg1SampleRate(sr_idx, version_);
141 RCHECK(frame.
ReadBits(1, &padded_));
145 RCHECK(frame.
ReadBits(2, &channel_mode_));
161 return Mpeg1FrameSize(layer_, bitrate_, sample_rate_, padded_);
165 size_t num_bytes)
const {
166 DCHECK_GT(num_bytes,
static_cast<size_t>(2));
167 uint8_t version = (data[1] & 0b00011000) >> 3;
168 uint8_t layer = (data[1] & 0b00000110) >> 1;
169 uint8_t btr_idx = (data[2] & 0b11110000) >> 4;
170 uint8_t sr_idx = (data[2] & 0b00001100) >> 2;
171 uint8_t padded = (data[2] & 0b00000010) >> 1;
173 if ((version == kMpeg1V_INV) || (layer == kMpeg1L_INV) || (btr_idx == 0) ||
177 uint32_t bitrate = Mpeg1BitRate(btr_idx, version, layer);
178 uint32_t samplerate = Mpeg1SampleRate(sr_idx, version);
179 return Mpeg1FrameSize(layer, bitrate, samplerate, padded);
185 static const size_t kConfigFrequencyTable[] = {
186 96000, 88200, 64000, 48000, 44100, 32000, 24000,
187 22050, 16000, 12000, 11025, 8000, 7350};
188 static const size_t kConfigFrequencyTableSize =
189 arraysize(kConfigFrequencyTable);
192 for (cft_idx = 0; cft_idx < kConfigFrequencyTableSize; cft_idx++)
193 if (sample_rate_ == kConfigFrequencyTable[cft_idx])
202 if (object_type <= 31) {
222 if (layer_ == kMpeg1L_1)
224 if (layer_ == kMpeg1L_2)
227 DCHECK_EQ(layer_, kMpeg1L_3);
236 if (channel_mode_ == 0b11)
All the methods that are virtual are virtual for mocking.