ADTSHeader.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "ADTSHeader.h"
  2. #include "foundation/error.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. int nsaac_adts_parse(nsaac_adts_header_t header, const uint8_t *buffer)
  14. {
  15. unsigned int syncword = (buffer[0] << 4) | (buffer[1] >> 4);
  16. unsigned int layer;
  17. unsigned int sample_rate_index;
  18. if (syncword != ADTS_SYNC)
  19. return NErr_LostSynchronization;
  20. header->syncword = syncword;
  21. header->id = (buffer[1] >> 3) & 1;
  22. layer = (buffer[1] >> 1) & 3;
  23. if (layer != 0)
  24. return NErr_WrongFormat;
  25. header->layer = layer;
  26. header->protection = (buffer[1]) & 1;
  27. header->profile = (buffer[2] >> 6) & 3;
  28. sample_rate_index = (buffer[2] >> 2) & 0xF;
  29. if (sample_rate_index == 15)
  30. return NErr_UnsupportedFormat; // might actually be OK if we can separately signal the sample rate
  31. if (sample_rate_index > 13)
  32. return NErr_Reserved;
  33. header->sample_rate_index = sample_rate_index;
  34. header->private_bit = (buffer[2] >> 1) & 1;
  35. header->channel_configuration = ((buffer[2] & 1) << 2) | ((buffer[3] >> 6) & 3);
  36. header->original = (buffer[3] >> 5) &1;
  37. header->home = (buffer[3] >> 4) &1;
  38. //copyright_identification_bit = (buffer[3] >> 3) & 1;
  39. //copyright_identification_start = (buffer[3] >> 2) & 1;
  40. header->frame_length = ((buffer[3] & 3) << 11) | (buffer[4]<<3) | ((buffer[5] >> 5) & 7);
  41. header->buffer_fullness = ((buffer[5] & 0x1F) << 6) | (buffer[6] >> 2);
  42. header->num_data_blocks = buffer[6] & 3;
  43. return NErr_Success;
  44. }
  45. static const unsigned int aac_sratetab[] =
  46. {
  47. 96000,
  48. 88200,
  49. 64000,
  50. 48000,
  51. 44100,
  52. 32000,
  53. 24000,
  54. 22050,
  55. 16000,
  56. 12000,
  57. 11025,
  58. 8000,
  59. 7350,
  60. };
  61. unsigned int nsaac_adts_get_samplerate(nsaac_adts_header_t header)
  62. {
  63. return aac_sratetab[header->sample_rate_index];
  64. }
  65. int nsaac_adts_match(nsaac_adts_header_t header1, nsaac_adts_header_t header2)
  66. {
  67. if (header1->id != header2->id)
  68. return NErr_False;
  69. if (header1->profile != header2->profile)
  70. return NErr_False;
  71. if (header1->sample_rate_index != header2->sample_rate_index)
  72. return NErr_False;
  73. if (header1->channel_configuration != header2->channel_configuration)
  74. return NErr_False;
  75. return NErr_True;
  76. }
  77. int nsaac_adts_get_channel_count(nsaac_adts_header_t header)
  78. {
  79. switch(header->channel_configuration)
  80. {
  81. case 7:
  82. return 8;
  83. default:
  84. return header->channel_configuration;
  85. }
  86. }
  87. size_t nsaac_adts_get_header_size(nsaac_adts_header_t header)
  88. {
  89. if (header->protection == ADTS_PROTECTED)
  90. return 9;
  91. else
  92. return 7;
  93. }
  94. size_t nsaac_adts_get_frame_bitrate(nsaac_adts_header_t header)
  95. {
  96. uint64_t bitrate = nsaac_adts_get_samplerate(header) * header->frame_length * 8 / 1024;
  97. return (size_t)bitrate;
  98. }