ADTSHeader.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #include "ADTSHeader.h"
  2. #include "nmrCommon/services/stdServiceImpl.h"
  3. enum
  4. {
  5. ADTS_NOT_PROTECTED = 1,
  6. ADTS_PROTECTED = 0,
  7. ADTS_SYNC = 0xFFF,
  8. ADTS_MAIN = 0x00,
  9. ADTS_LC = 0x01,
  10. ADTS_SSR = 0x10,
  11. ADTS_LTP = 0x11
  12. };
  13. const int aac_adts_parse(const aac_adts_header_t header, const __uint8 *buffer)
  14. {
  15. header->syncword = ((buffer[0] << 4) | (buffer[1] >> 4));
  16. if (header->syncword != ADTS_SYNC)
  17. {
  18. return NErr_LostSynchronization;
  19. }
  20. header->id = ((buffer[1] >> 3) & 1);
  21. header->layer = ((buffer[1] >> 1) & 3);
  22. if (header->layer != 0)
  23. {
  24. return NErr_WrongFormat;
  25. }
  26. header->protection = ((buffer[1]) & 1);
  27. header->profile = ((buffer[2] >> 6) & 3);
  28. header->sample_rate_index = ((buffer[2] >> 2) & 0xF);
  29. if (header->sample_rate_index == 15)
  30. {
  31. return NErr_UnsupportedFormat; // might actually be OK if we can separately signal the sample rate
  32. }
  33. if (header->sample_rate_index > 13)
  34. {
  35. return NErr_Reserved;
  36. }
  37. header->private_bit = ((buffer[2] >> 1) & 1);
  38. header->channel_configuration = ((buffer[2] & 1) << 2) | ((buffer[3] >> 6) & 3);
  39. header->original = ((buffer[3] >> 5) & 1);
  40. header->home = ((buffer[3] >> 4) & 1);
  41. header->frame_length = ((buffer[3] & 3) << 11) | (buffer[4]<<3) | ((buffer[5] >> 5) & 7);
  42. header->buffer_fullness = ((buffer[5] & 0x1F) << 6) | (buffer[6] >> 2);
  43. header->num_data_blocks = (buffer[6] & 3);
  44. return NErr_Success;
  45. }
  46. static const unsigned int aac_sratetab[] =
  47. {
  48. 96000,
  49. 88200,
  50. 64000,
  51. 48000,
  52. 44100,
  53. 32000,
  54. 24000,
  55. 22050,
  56. 16000,
  57. 12000,
  58. 11025,
  59. 8000,
  60. 7350,
  61. };
  62. #if 0
  63. const int aac_adts_match(const aac_adts_header_t header1, const aac_adts_header_t header2)
  64. {
  65. if (header1->id != header2->id)
  66. {
  67. return NErr_False;
  68. }
  69. if (header1->profile != header2->profile)
  70. {
  71. return NErr_False;
  72. }
  73. if (header1->sample_rate_index != header2->sample_rate_index)
  74. {
  75. return NErr_False;
  76. }
  77. if (header1->channel_configuration != header2->channel_configuration)
  78. {
  79. return NErr_False;
  80. }
  81. return NErr_True;
  82. }
  83. #endif
  84. const int aac_adts_get_channel_count(const aac_adts_header_t header)
  85. {
  86. switch (header->channel_configuration)
  87. {
  88. case 7:
  89. {
  90. return 8;
  91. }
  92. default:
  93. {
  94. return header->channel_configuration;
  95. }
  96. }
  97. }
  98. const __uint16 getADTSFrameInfo(const char *hdr, unsigned int *samplerate, __uint8 *asc_header)
  99. {
  100. ADTSHeader header = {0};
  101. if (aac_adts_parse(&header, (const __uint8 *)hdr) == NErr_Success)
  102. {
  103. *samplerate = aac_sratetab[header.sample_rate_index];
  104. // we need this when generating flv frames
  105. // as it creates the AudioSpecificConfig
  106. // from the existing ADTS header details
  107. // (is like a mini-ADTS header to create)
  108. if (asc_header)
  109. {
  110. asc_header[0] |= (((header.profile + 1) & 31) << 3) + (header.sample_rate_index >> 1);
  111. asc_header[1] |= ((header.sample_rate_index & 0x1) << 7) + (header.channel_configuration << 3);
  112. }
  113. //*bitrate = (int)(((header.frame_length / 1/*frames*/) * (aac_sratetab[header.sample_rate_index] / 1024.0)) + 0.5) * 8;
  114. return (__uint16)header.frame_length;
  115. }
  116. return 0;
  117. }
  118. const char *AAC_FrameInfo::getVersionName() const
  119. {
  120. if (m_version)
  121. return "v2";
  122. return "v4";
  123. }
  124. const char *AAC_FrameInfo::getAOT() const
  125. {
  126. switch (m_aot)
  127. {
  128. case 2: return "LC";
  129. case 5: return "SBR";
  130. case 29: return "PS";
  131. default: return "unknown profile";
  132. }
  133. }
  134. int getAACFrameLength (const unsigned char *p, unsigned int len)
  135. {
  136. if (len < 6)
  137. return -1;
  138. return ((p[3] & 0x3) << 11) + (p[4] << 3) + ((p[5] & 0xE0) >> 5);
  139. }
  140. int getAACFrameInfo (const unsigned char *p, unsigned int len, AAC_FrameInfo &info)
  141. {
  142. if (len < 6)
  143. return -1;
  144. if ((((long)p[0])<<4 | (p[1]>>4)) != 0xfff)
  145. return -1;
  146. int layer = ((p[1] >> 1) & 3);
  147. if (layer != 0)
  148. return -1;
  149. int sample_rate_index = ((p[2] >> 2) & 0xF);
  150. if (sample_rate_index > 13)
  151. return -1;
  152. int samplerate = aac_sratetab [sample_rate_index];
  153. if (info.m_samplerate)
  154. {
  155. if (info.m_samplerate != samplerate)
  156. return -1;
  157. }
  158. else
  159. info.m_samplerate = samplerate;
  160. info.m_blocks = (p[6] & 0x3) + 1;
  161. info.m_pattern = (((unsigned long)(p[0])<<24) | (p[1]<<16) | (p[2]<<8) | p[0]) & info.m_mask;
  162. return getAACFrameLength (p, len);
  163. }
  164. int AAC_FrameInfo::verifyFrame (const unsigned char *buf, unsigned int len)
  165. {
  166. if (len > 6)
  167. {
  168. unsigned long v = (unsigned long)(buf[0])<<24 | (buf[1]<<16) | (buf[2]<<8) | buf[0];
  169. if ((v & m_mask) == m_pattern)
  170. {
  171. m_blocks = (buf[6] & 0x3) + 1;
  172. m_version = (buf[1] >> 3) & 1; // 1 mpeg2, 0 mpeg4
  173. m_aot = ((buf[2] >> 6) & 3) + 1;
  174. return getAACFrameLength (buf, len);
  175. }
  176. // DLOG ("AAC failed sync, retry..");
  177. return -1;
  178. }
  179. return 0;
  180. }
  181. AAC_FrameInfo::AAC_FrameInfo (unsigned long value) : parserInfo (0xFFFEFDC0, value)
  182. {
  183. m_description = "AAC";
  184. m_blocks = 0;
  185. m_aot = 0;
  186. }
  187. AAC_FrameInfo::AAC_FrameInfo(const unsigned char *p, unsigned int len) : parserInfo()
  188. {
  189. m_mask = 0xFFFEFDC0;
  190. m_description = "AAC";
  191. m_blocks = 0;
  192. getAACFrameInfo (p, len, *this);
  193. }