OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "media/formats/ac3/ac3_util.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/macros.h" |
| 9 #include "media/base/bit_reader.h" |
| 10 |
| 11 namespace media { |
| 12 |
| 13 namespace { |
| 14 |
| 15 // The size in byte of a (E-)AC3 synchronization frame header. |
| 16 const int kHeaderSizeInByte = 8; |
| 17 // The number of new samples per (E-)AC3 audio block. |
| 18 const int kAudioSamplesPerAudioBlock = 256; |
| 19 // Each synchronization frame has 6 blocks that provide 256 new audio samples. |
| 20 const int kAudioSamplePerAc3SyncFrame = 6 * kAudioSamplesPerAudioBlock; |
| 21 // Number of audio blocks per E-AC3 synchronization frame, indexed by |
| 22 // numblkscod. |
| 23 const int kBlocksPerSyncFrame[] = {1, 2, 3, 6}; |
| 24 // Sample rates, indexed by fscod. |
| 25 const int kSampleRate[] = {48000, 44100, 32000}; |
| 26 // Nominal bitrates in kbps, indexed by frmsizecod / 2. |
| 27 const int kBitrate[] = {32, 40, 48, 56, 64, 80, 96, 112, 128, 160, |
| 28 192, 224, 256, 320, 384, 448, 512, 576, 640}; |
| 29 // 16-bit words per synchronization frame, indexed by frmsizecod. |
| 30 const int kSyncFrameSizeInWordsFor44kHz[] = { |
| 31 69, 70, 87, 88, 104, 105, 121, 122, 139, 140, 174, 175, 208, |
| 32 209, 243, 244, 278, 279, 348, 349, 417, 418, 487, 488, 557, 558, |
| 33 696, 697, 835, 836, 975, 976, 1114, 1115, 1253, 1254, 1393, 1394}; |
| 34 |
| 35 // Utility for unpacking (E-)AC3 header. Note that all fields are encoded. |
| 36 class Ac3Header { |
| 37 public: |
| 38 Ac3Header(const uint8_t* data, int size); |
| 39 |
| 40 uint32_t eac3_frame_size_code() const { return eac3_frame_size_code_; } |
| 41 |
| 42 uint32_t sample_rate_code() const { return sample_rate_code_; } |
| 43 |
| 44 uint32_t eac3_number_of_audio_block_code() const { |
| 45 DCHECK(sample_rate_code_ != 3); |
| 46 return eac3_number_of_audio_block_code_; |
| 47 } |
| 48 |
| 49 uint32_t ac3_frame_size_code() const { return ac3_frame_size_code_; } |
| 50 |
| 51 private: |
| 52 // bit[5:15] for E-AC3 |
| 53 uint32_t eac3_frame_size_code_; |
| 54 // bit[16:17] for (E-)AC3 |
| 55 uint32_t sample_rate_code_; |
| 56 // bit[18:23] for AC3 |
| 57 uint32_t ac3_frame_size_code_; |
| 58 // bit[18:19] for E-AC3 |
| 59 uint32_t eac3_number_of_audio_block_code_; |
| 60 }; |
| 61 |
| 62 Ac3Header::Ac3Header(const uint8_t* data, int size) { |
| 63 DCHECK_GE(size, kHeaderSizeInByte); |
| 64 |
| 65 BitReader reader(data, size); |
| 66 uint16_t sync_word; |
| 67 reader.ReadBits(16, &sync_word); |
| 68 DCHECK(sync_word == 0x0B77); |
| 69 |
| 70 reader.SkipBits(5); |
| 71 reader.ReadBits(11, &eac3_frame_size_code_); |
| 72 reader.ReadBits(2, &sample_rate_code_); |
| 73 reader.ReadBits(6, &ac3_frame_size_code_); |
| 74 eac3_number_of_audio_block_code_ = ac3_frame_size_code_ >> 4; |
| 75 } |
| 76 |
| 77 // Search for next synchronization word, wihch is 0x0B-0x77. |
| 78 const uint8_t* FindNextSyncWord(const uint8_t* const begin, |
| 79 const uint8_t* const end) { |
| 80 DCHECK(begin); |
| 81 DCHECK(end); |
| 82 DCHECK_LE(begin, end); |
| 83 |
| 84 const uint8_t* current = begin; |
| 85 |
| 86 while (current < end - 1) { |
| 87 if (current[0] == 0x0B && current[1] == 0x77) { |
| 88 if (current != begin) |
| 89 DVLOG(2) << __FUNCTION__ << " skip " << current - begin << " bytes."; |
| 90 |
| 91 return current; |
| 92 } else if (current[1] != 0x0B) { |
| 93 current += 2; |
| 94 } else { |
| 95 ++current; |
| 96 } |
| 97 } |
| 98 |
| 99 return nullptr; |
| 100 } |
| 101 |
| 102 // Returns the number of audio samples represented by the given E-AC3 |
| 103 // synchronization frame. |
| 104 int ParseEac3SyncFrameSampleCount(Ac3Header& header) { |
| 105 unsigned blocks = |
| 106 header.sample_rate_code() == 0x03 |
| 107 ? 6 |
| 108 : kBlocksPerSyncFrame[header.eac3_number_of_audio_block_code()]; |
| 109 return kAudioSamplesPerAudioBlock * blocks; |
| 110 } |
| 111 |
| 112 // Returns the size in bytes of the given E-AC3 synchronization frame. |
| 113 int ParseEac3SyncFrameSize(Ac3Header& header) { |
| 114 return 2 * (header.eac3_frame_size_code() + 1); |
| 115 } |
| 116 |
| 117 // Returns the number of audio samples in an AC3 synchronization frame. |
| 118 int GetAc3SyncFrameSampleCount() { |
| 119 return kAudioSamplePerAc3SyncFrame; |
| 120 } |
| 121 |
| 122 // Returns the size in bytes of the given AC3 synchronization frame. |
| 123 int ParseAc3SyncFrameSize(Ac3Header& header) { |
| 124 if (header.sample_rate_code() >= arraysize(kSampleRate) || |
| 125 header.ac3_frame_size_code() >= |
| 126 arraysize(kSyncFrameSizeInWordsFor44kHz)) { |
| 127 DVLOG(2) << __FUNCTION__ << " Invalid frame header." |
| 128 << " fscod:" << header.sample_rate_code() |
| 129 << " frmsizecod:" << header.ac3_frame_size_code(); |
| 130 return -1; |
| 131 } |
| 132 |
| 133 // See http://atsc.org/wp-content/uploads/2015/03/A52-201212-17.pdf table |
| 134 // 5.18, frame size code table. |
| 135 |
| 136 int sample_rate = kSampleRate[header.sample_rate_code()]; |
| 137 if (sample_rate == 44100) { |
| 138 return 2 * kSyncFrameSizeInWordsFor44kHz[header.ac3_frame_size_code()]; |
| 139 } |
| 140 |
| 141 int bitrate = kBitrate[header.ac3_frame_size_code() / 2]; |
| 142 if (sample_rate == 32000) { |
| 143 return 6 * bitrate; |
| 144 } |
| 145 |
| 146 // sample_rate == 48000 |
| 147 return 4 * bitrate; |
| 148 } |
| 149 |
| 150 // Returns the total number of audio samples in the given buffer, which contains |
| 151 // several complete (E-)AC3 syncframes. |
| 152 int ParseTotalSampleCount(const uint8_t* data, size_t size, bool is_eac3) { |
| 153 DCHECK(data); |
| 154 |
| 155 if (size < kHeaderSizeInByte) { |
| 156 return 0; |
| 157 } |
| 158 |
| 159 const uint8_t* const end = data + size; |
| 160 const uint8_t* current = FindNextSyncWord(data, end); |
| 161 int total_sample_count = 0; |
| 162 |
| 163 while (current && end - current > kHeaderSizeInByte) { |
| 164 Ac3Header header(current, end - current); |
| 165 |
| 166 int frame_size = is_eac3 ? ParseEac3SyncFrameSize(header) |
| 167 : ParseAc3SyncFrameSize(header); |
| 168 int sample_count = is_eac3 ? ParseEac3SyncFrameSampleCount(header) |
| 169 : GetAc3SyncFrameSampleCount(); |
| 170 |
| 171 if (frame_size > 0 && sample_count > 0) { |
| 172 current += frame_size; |
| 173 if (current > end) { |
| 174 DVLOG(2) << __FUNCTION__ << " Incomplete frame, missing " |
| 175 << current - end << " bytes."; |
| 176 break; |
| 177 } |
| 178 |
| 179 total_sample_count += sample_count; |
| 180 } else { |
| 181 DVLOG(2) |
| 182 << __FUNCTION__ |
| 183 << " Invalid frame, skip 2 bytes to find next synchronization word."; |
| 184 current += 2; |
| 185 } |
| 186 |
| 187 current = FindNextSyncWord(current, end); |
| 188 } |
| 189 |
| 190 return total_sample_count; |
| 191 } |
| 192 |
| 193 } // namespace anonymous |
| 194 |
| 195 // static |
| 196 int Ac3Util::ParseTotalAc3SampleCount(const uint8_t* data, size_t size) { |
| 197 return ParseTotalSampleCount(data, size, false); |
| 198 } |
| 199 |
| 200 // static |
| 201 int Ac3Util::ParseTotalEac3SampleCount(const uint8_t* data, size_t size) { |
| 202 return ParseTotalSampleCount(data, size, true); |
| 203 } |
| 204 |
| 205 } // namespace media |
OLD | NEW |