AC3FrameScanner.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright 2014, The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #define LOG_TAG "AudioSPDIF"
  17. #include <string.h>
  18. #include <log/log.h>
  19. #include <audio_utils/spdif/FrameScanner.h>
  20. #include "AC3FrameScanner.h"
  21. namespace android {
  22. // These values are from the AC3 spec. Do not change them.
  23. const uint8_t AC3FrameScanner::kSyncBytes[] = { 0x0B, 0x77 };
  24. const uint16_t AC3FrameScanner::kAC3SampleRateTable[AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES]
  25. = { 48000, 44100, 32000 };
  26. // Table contains number of 16-bit words in an AC3 frame.
  27. // From AC3 spec table 5.13
  28. const uint16_t AC3FrameScanner::kAC3FrameSizeTable[AC3_NUM_FRAME_SIZE_TABLE_ENTRIES]
  29. [AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES] = {
  30. { 64, 69, 96 },
  31. { 64, 70, 96 },
  32. { 80, 87, 120 },
  33. { 80, 88, 120 },
  34. { 96, 104, 144 },
  35. { 96, 105, 144 },
  36. { 112, 121, 168 },
  37. { 112, 122, 168 },
  38. { 128, 139, 192 },
  39. { 128, 140, 192 },
  40. { 160, 174, 240 },
  41. { 160, 175, 240 },
  42. { 192, 208, 288 },
  43. { 192, 209, 288 },
  44. { 224, 243, 336 },
  45. { 224, 244, 336 },
  46. { 256, 278, 384 },
  47. { 256, 279, 384 },
  48. { 320, 348, 480 },
  49. { 320, 349, 480 },
  50. { 384, 417, 576 },
  51. { 384, 418, 576 },
  52. { 448, 487, 672 },
  53. { 448, 488, 672 },
  54. { 512, 557, 768 },
  55. { 512, 558, 768 },
  56. { 640, 696, 960 },
  57. { 640, 697, 960 },
  58. { 768, 835, 1152 },
  59. { 768, 836, 1152 },
  60. { 896, 975, 1344 },
  61. { 896, 976, 1344 },
  62. { 1024, 1114, 1536 },
  63. { 1024, 1115, 1536 },
  64. { 1152, 1253, 1728 },
  65. { 1152, 1254, 1728 },
  66. { 1280, 1393, 1920 },
  67. { 1280, 1394, 1920 }
  68. };
  69. const uint16_t AC3FrameScanner::kEAC3ReducedSampleRateTable[AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES]
  70. = { 24000, 22050, 16000 };
  71. const uint16_t
  72. AC3FrameScanner::kEAC3BlocksPerFrameTable[EAC3_NUM_BLOCKS_PER_FRAME_TABLE_ENTRIES]
  73. = { 1, 2, 3, 6 };
  74. // Defined in IEC61937-2
  75. #define SPDIF_DATA_TYPE_AC3 1
  76. #define SPDIF_DATA_TYPE_E_AC3 21
  77. #define AC3_STREAM_TYPE_0 0
  78. #define AC3_STREAM_TYPE_1 1
  79. #define AC3_STREAM_TYPE_2 2
  80. // -----------------------------------------------------------------------------
  81. // Scanner for AC3 byte streams.
  82. AC3FrameScanner::AC3FrameScanner(audio_format_t format)
  83. : FrameScanner(SPDIF_DATA_TYPE_AC3,
  84. AC3FrameScanner::kSyncBytes,
  85. sizeof(AC3FrameScanner::kSyncBytes), 6)
  86. , mStreamType(0)
  87. , mSubstreamID(0)
  88. , mFormat(format)
  89. {
  90. mAudioBlocksPerSyncFrame = 6;
  91. memset(mSubstreamBlockCounts, 0, sizeof(mSubstreamBlockCounts));
  92. }
  93. AC3FrameScanner::~AC3FrameScanner()
  94. {
  95. }
  96. int AC3FrameScanner::getSampleFramesPerSyncFrame() const
  97. {
  98. return mRateMultiplier
  99. * AC3_MAX_BLOCKS_PER_SYNC_FRAME_BLOCK * AC3_PCM_FRAMES_PER_BLOCK;
  100. }
  101. void AC3FrameScanner::resetBurst()
  102. {
  103. for (int i = 0; i < EAC3_MAX_SUBSTREAMS; i++) {
  104. if (mSubstreamBlockCounts[i] >= AC3_MAX_BLOCKS_PER_SYNC_FRAME_BLOCK) {
  105. mSubstreamBlockCounts[i] -= AC3_MAX_BLOCKS_PER_SYNC_FRAME_BLOCK;
  106. } else if (mSubstreamBlockCounts[i] > 0) {
  107. ALOGW("EAC3 substream[%d] has only %d audio blocks!",
  108. i, mSubstreamBlockCounts[i]);
  109. mSubstreamBlockCounts[i] = 0;
  110. }
  111. }
  112. }
  113. // Per IEC 61973-3:5.3.3, for E-AC3 burst-length shall be in bytes.
  114. uint16_t AC3FrameScanner::convertBytesToLengthCode(uint16_t numBytes) const
  115. {
  116. return (mDataType == SPDIF_DATA_TYPE_E_AC3) ? numBytes : numBytes * 8;
  117. }
  118. // per IEC 61973-3 Paragraph 5.3.3
  119. // We have to send 6 audio blocks on all active substreams.
  120. // Substream zero must be the first.
  121. // We don't know if we have all the blocks we need until we see
  122. // the 7th block of substream#0.
  123. bool AC3FrameScanner::isFirstInBurst()
  124. {
  125. if (mDataType == SPDIF_DATA_TYPE_E_AC3) {
  126. if (((mStreamType == AC3_STREAM_TYPE_0)
  127. || (mStreamType == AC3_STREAM_TYPE_2))
  128. && (mSubstreamID == 0)
  129. // The ">" is intentional. We have to see the beginning
  130. // of the block in the next burst before we can send
  131. // the current burst.
  132. && (mSubstreamBlockCounts[0] > AC3_MAX_BLOCKS_PER_SYNC_FRAME_BLOCK)) {
  133. return true;
  134. }
  135. }
  136. return false;
  137. }
  138. bool AC3FrameScanner::isLastInBurst()
  139. {
  140. // For EAC3 we don't know if we are the end until we see a
  141. // frame that must be at the beginning. See isFirstInBurst().
  142. return (mDataType != SPDIF_DATA_TYPE_E_AC3); // Just one AC3 frame per burst.
  143. }
  144. // TODO Use BitFieldParser
  145. // Parse AC3 header.
  146. // Detect whether the stream is AC3 or EAC3. Extract data depending on type.
  147. //
  148. // @return true if valid
  149. bool AC3FrameScanner::parseHeader()
  150. {
  151. // Interpret bsid based on paragraph E2.3.1.6 of EAC3 spec.
  152. uint32_t bsid = mHeaderBuffer[5] >> 3; // bitstream ID
  153. // Check BSID to see if this is EAC3 or regular AC3.
  154. // These arbitrary BSID numbers do not have any names in the spec.
  155. if ((bsid > 10) && (bsid <= 16)) {
  156. mDataType = SPDIF_DATA_TYPE_E_AC3;
  157. } else if (bsid <= 8) {
  158. mDataType = SPDIF_DATA_TYPE_AC3;
  159. } else {
  160. ALOGW("AC3 bsid = %d not supported", bsid);
  161. return false;
  162. }
  163. // bitstream mode, main, commentary, etc.
  164. uint32_t bsmod = mHeaderBuffer[5] & 7;
  165. mDataTypeInfo = bsmod; // as per IEC61937-3, table 3.
  166. // The names fscod, frmsiz are from the AC3 spec.
  167. uint32_t fscod = mHeaderBuffer[4] >> 6;
  168. if (mDataType == SPDIF_DATA_TYPE_E_AC3) {
  169. mStreamType = mHeaderBuffer[2] >> 6; // strmtyp in spec
  170. mSubstreamID = (mHeaderBuffer[2] >> 3) & 0x07;
  171. // Frame size is explicit in EAC3. Paragraph E2.3.1.3
  172. uint32_t frmsiz = ((mHeaderBuffer[2] & 0x07) << 8) + mHeaderBuffer[3];
  173. mFrameSizeBytes = (frmsiz + 1) * sizeof(int16_t);
  174. uint32_t numblkscod = 3; // 6 blocks default
  175. if (fscod == 3) {
  176. uint32_t fscod2 = (mHeaderBuffer[4] >> 4) & 0x03;
  177. if (fscod2 >= AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES) {
  178. ALOGW("Invalid EAC3 fscod2 = %d", fscod2);
  179. return false;
  180. } else {
  181. mSampleRate = kEAC3ReducedSampleRateTable[fscod2];
  182. }
  183. } else {
  184. mSampleRate = kAC3SampleRateTable[fscod];
  185. numblkscod = (mHeaderBuffer[4] >> 4) & 0x03;
  186. }
  187. mRateMultiplier = EAC3_RATE_MULTIPLIER; // per IEC 61973-3 Paragraph 5.3.3
  188. // Don't send data burst until we have 6 blocks per substream.
  189. mAudioBlocksPerSyncFrame = kEAC3BlocksPerFrameTable[numblkscod];
  190. // Keep track of how many audio blocks we have for each substream.
  191. // This should be safe because mSubstreamID is ANDed with 0x07 above.
  192. // And the array is allocated as [8].
  193. if ((mStreamType == AC3_STREAM_TYPE_0)
  194. || (mStreamType == AC3_STREAM_TYPE_2)) {
  195. mSubstreamBlockCounts[mSubstreamID] += mAudioBlocksPerSyncFrame;
  196. }
  197. // Print enough so we can see all the substreams.
  198. ALOGD_IF((mFormatDumpCount < 3*8 ),
  199. "EAC3 mStreamType = %d, mSubstreamID = %d",
  200. mStreamType, mSubstreamID);
  201. } else { // regular AC3
  202. // Extract sample rate and frame size from codes.
  203. uint32_t frmsizcod = mHeaderBuffer[4] & 0x3F; // frame size code
  204. if (fscod >= AC3_NUM_SAMPLE_RATE_TABLE_ENTRIES) {
  205. ALOGW("Invalid AC3 sampleRateCode = %d", fscod);
  206. return false;
  207. } else if (frmsizcod >= AC3_NUM_FRAME_SIZE_TABLE_ENTRIES) {
  208. ALOGW("Invalid AC3 frameSizeCode = %d", frmsizcod);
  209. return false;
  210. } else {
  211. mSampleRate = kAC3SampleRateTable[fscod];
  212. mRateMultiplier = 1;
  213. mFrameSizeBytes = sizeof(uint16_t)
  214. * kAC3FrameSizeTable[frmsizcod][fscod];
  215. }
  216. mAudioBlocksPerSyncFrame = 6;
  217. if (mFormat == AUDIO_FORMAT_E_AC3) {
  218. ALOGV("Its a Ac3 substream in EAC3 stream");
  219. mStreamType = 2;
  220. mSubstreamID = 0;
  221. mSubstreamBlockCounts[0] += mAudioBlocksPerSyncFrame;
  222. mDataType = SPDIF_DATA_TYPE_E_AC3;
  223. mRateMultiplier = EAC3_RATE_MULTIPLIER;
  224. }
  225. }
  226. ALOGI_IF((mFormatDumpCount == 0),
  227. "AC3 frame rate = %d * %d, size = %zu, audioBlocksPerSyncFrame = %d",
  228. mSampleRate, mRateMultiplier, mFrameSizeBytes, mAudioBlocksPerSyncFrame);
  229. mFormatDumpCount++;
  230. return true;
  231. }
  232. } // namespace android