7 #include <packager/media/formats/mp2t/mpeg1_header.h>
9 #include <absl/log/check.h>
11 #include <packager/media/base/bit_reader.h>
12 #include <packager/media/base/bit_writer.h>
13 #include <packager/media/formats/mp2t/mp2t_common.h>
18 const size_t kMpeg1HeaderMinSize = 4;
20 const uint8_t kMpeg1V_INV = 0b01;
21 const uint8_t kMpeg1L_INV = 0b00;
23 const uint8_t kMpeg1L_3 = 0b01;
24 const uint8_t kMpeg1L_2 = 0b10;
25 const uint8_t kMpeg1L_1 = 0b11;
27 const size_t kMpeg1SamplesPerFrameTable[] = {
31 const uint32_t kMpeg1SampleRateTable[][3] = {
34 {44100, 22050, 11025},
35 {48000, 24000, 12000},
36 {32000, 16000, 8000}};
38 const size_t kMpeg1SampleRateTableSize = std::size(kMpeg1SampleRateTable);
40 static inline uint32_t Mpeg1SampleRate(uint8_t sr_idx, uint8_t version) {
41 static int sr_version_indexes[] = {2, -1, 1, 0};
42 DCHECK_NE(version, 1);
43 DCHECK_LT(sr_idx, kMpeg1SampleRateTableSize);
44 return kMpeg1SampleRateTable[sr_idx][sr_version_indexes[version]];
47 const uint32_t kMpeg1BitrateTable[][5] = {
52 { 64, 48, 40, 48, 16},
53 { 96, 56, 48, 56, 24},
54 { 128, 64, 56, 64, 32},
55 { 160, 80, 64, 80, 40},
56 { 192, 96, 80, 96, 48},
57 { 224, 112, 96, 112, 56},
58 { 256, 128, 112, 128, 64},
59 { 288, 160, 128, 144, 80},
60 { 320, 192, 160, 160, 96},
61 { 352, 224, 192, 176, 112},
62 { 384, 256, 224, 192, 128},
63 { 416, 320, 256, 224, 144},
64 { 448, 384, 320, 256, 160}};
66 const size_t kMpeg1BitrateTableSize = std::size(kMpeg1BitrateTable);
68 static inline uint32_t Mpeg1BitRate(uint8_t btr_idx,
71 static int btr_version_indexes[] = {1, -1, 1, 0};
72 static int btr_layer_indexes[] = {-1, 2, 1, 0};
74 DCHECK_NE(version, 1);
76 int vidx = btr_version_indexes[version];
77 int lidx = btr_layer_indexes[layer];
78 if (vidx == 1 && lidx > 1)
81 DCHECK_LT(vidx * 3 + lidx, 5);
82 DCHECK_LT(btr_idx, kMpeg1BitrateTableSize);
83 return kMpeg1BitrateTable[btr_idx][vidx * 3 + lidx] * 1000;
86 static inline size_t Mpeg1FrameSize(uint8_t layer,
90 DCHECK_GT(sample_rate,
static_cast<uint32_t
>(0));
91 if (layer == kMpeg1L_1)
92 return (12 * bitrate / sample_rate + padded) * 4;
93 return 144 * bitrate / sample_rate + padded;
103 return (buf[0] == 0xff) &&
104 ((buf[1] & 0b11100000) == 0b11100000)
106 && ((buf[1] & 0b00011000) != 0b00001000)
108 && ((buf[1] & 0b00000110) != 0b00000000);
112 return kMpeg1HeaderMinSize + 1;
116 static int spf_layer_indexes[] = {-1, 2, 1, 0};
117 DCHECK_NE(layer_, 0);
118 return kMpeg1SamplesPerFrameTable[spf_layer_indexes[layer_]];
124 if (mpeg1_frame_size < kMpeg1HeaderMinSize)
127 BitReader frame(mpeg1_frame, mpeg1_frame_size);
131 RCHECK(sync == 0x7ff);
133 RCHECK(frame.
ReadBits(2, &version_));
134 RCHECK(version_ != kMpeg1V_INV);
136 RCHECK(layer_ != kMpeg1L_INV);
137 RCHECK(frame.
ReadBits(1, &protection_absent_));
140 RCHECK(frame.
ReadBits(4, &btr_idx));
142 bitrate_ = Mpeg1BitRate(btr_idx, version_, layer_);
146 RCHECK(sr_idx < 0b11);
147 sample_rate_ = Mpeg1SampleRate(sr_idx, version_);
149 RCHECK(frame.
ReadBits(1, &padded_));
153 RCHECK(frame.
ReadBits(2, &channel_mode_));
169 return Mpeg1FrameSize(layer_, bitrate_, sample_rate_, padded_);
173 size_t num_bytes)
const {
174 DCHECK_GT(num_bytes,
static_cast<size_t>(2));
175 uint8_t version = (data[1] & 0b00011000) >> 3;
176 uint8_t layer = (data[1] & 0b00000110) >> 1;
177 uint8_t btr_idx = (data[2] & 0b11110000) >> 4;
178 uint8_t sr_idx = (data[2] & 0b00001100) >> 2;
179 uint8_t padded = (data[2] & 0b00000010) >> 1;
181 if ((version == kMpeg1V_INV) || (layer == kMpeg1L_INV) || (btr_idx == 0) ||
185 uint32_t bitrate = Mpeg1BitRate(btr_idx, version, layer);
186 uint32_t samplerate = Mpeg1SampleRate(sr_idx, version);
187 return Mpeg1FrameSize(layer, bitrate, samplerate, padded);
193 static const size_t kConfigFrequencyTable[] = {
194 96000, 88200, 64000, 48000, 44100, 32000, 24000,
195 22050, 16000, 12000, 11025, 8000, 7350};
196 static const size_t kConfigFrequencyTableSize =
197 std::size(kConfigFrequencyTable);
200 for (cft_idx = 0; cft_idx < kConfigFrequencyTableSize; cft_idx++)
201 if (sample_rate_ == kConfigFrequencyTable[cft_idx])
210 if (object_type <= 31) {
230 if (layer_ == kMpeg1L_1)
232 if (layer_ == kMpeg1L_2)
235 DCHECK_EQ(layer_, kMpeg1L_3);
244 if (channel_mode_ == 0b11)
All the methods that are virtual are virtual for mocking.