OFL.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include "OFL.h"
  2. #include "foundation/error.h"
  3. static void crcofl(unsigned short crcPoly, unsigned short crcMask, unsigned long *crc, unsigned char byte)
  4. {
  5. int i;
  6. for (i=0; i<8; i++)
  7. {
  8. unsigned short flag = (*crc) & crcMask ? 1:0;
  9. flag ^= (byte & 0x80 ? 1 : 0);
  10. (*crc)<<=1;
  11. byte <<= 1;
  12. if(flag)
  13. (*crc) ^= crcPoly;
  14. }
  15. }
  16. int OFL::GetGaps(size_t *pregap, size_t *postgap)
  17. {
  18. /* TODO: verify the postgap calculation */
  19. if (codec_delay >= 529)
  20. {
  21. *pregap = codec_delay;
  22. size_t endcut;
  23. endcut = samples_per_frame - ((total_length + codec_delay) % samples_per_frame); // how many 0 samples had to be added?
  24. *postgap = endcut;
  25. return NErr_Success;
  26. }
  27. return NErr_Empty;
  28. }
  29. double OFL::GetLengthSeconds() const
  30. {
  31. return (double)GetSamples() / (double)sample_rate;
  32. }
  33. uint64_t OFL::GetSamples() const
  34. {
  35. return total_length;
  36. }
  37. uint32_t OFL::GetFrames() const
  38. {
  39. uint64_t real_samples = (total_length+codec_delay)*samples_per_frame;
  40. return (uint32_t) (real_samples/samples_per_frame);
  41. }
  42. int OFL::Read(const MPEGFrame &header, const uint8_t *buffer, size_t buffer_len)
  43. {
  44. if (header.layer != MPEGFrame::Layer3)
  45. return NErr_False;
  46. sample_rate = header.GetSampleRate();
  47. samples_per_frame = header.GetSamplesPerFrame();
  48. if (header.channelMode == MPEGFrame::Mono)
  49. {
  50. if (header.mpegVersion == MPEGFrame::MPEG1)
  51. {
  52. // 0-9 : main_data_end
  53. int16_t main_data_end = (buffer[0] << 1) | (buffer[1] >> 7);
  54. // read the 2 part2_3_lengths out so we know how big the main data section is
  55. uint16_t part2_3_length = ((buffer[2] & 0x3F) << 6) | (buffer[3]>>2); // bits 18-30
  56. part2_3_length += ((buffer[9] & 0x7) << 9) | (buffer[10] << 1) | (buffer[11] >> 7) ; // bits 77-89
  57. size_t offset = 17 + (part2_3_length+7)/8;
  58. if (offset+9 < buffer_len && buffer[offset] == 0xb4)
  59. {
  60. unsigned long crc=255;
  61. for (int i=0;i<9;i++)
  62. crcofl(0x0045, 0x0080, &crc, buffer[offset+i]);
  63. if ((crc & 0xFF) == buffer[offset+9])
  64. {
  65. total_length = (buffer[offset+3] << 24) | (buffer[offset+4] << 16) | (buffer[offset+5] << 8) | (buffer[offset+6]);
  66. codec_delay = (buffer[offset+1] << 8) | (buffer[offset+2]);
  67. additional_delay= (buffer[offset+7] << 8) | (buffer[offset+8]);
  68. return NErr_Success;
  69. }
  70. }
  71. }
  72. else
  73. { // MPEG2 and 2.5
  74. // 0-8 : main_data_end
  75. uint16_t main_data_end = buffer[0];
  76. // read the 2 part2_3_lengths out so we know how big the main data section is
  77. uint16_t part2_3_length = ((buffer[1] & 0x7F) << 5) | (buffer[2]>>3); // bits 9-21
  78. size_t offset = 9 + (part2_3_length+7)/8;
  79. if (offset+9 < buffer_len && buffer[offset] == 0xb4)
  80. {
  81. unsigned long crc=255;
  82. for (int i=0;i<9;i++)
  83. crcofl(0x0045, 0x0080, &crc, buffer[offset+i]);
  84. if ((crc & 0xFF) == buffer[offset+9])
  85. {
  86. total_length = (buffer[offset+3] << 24) | (buffer[offset+4] << 16) | (buffer[offset+5] << 8) | (buffer[offset+6]);
  87. codec_delay = (buffer[offset+1] << 8) | (buffer[offset+2]);
  88. additional_delay= (buffer[offset+7] << 8) | (buffer[offset+8]);
  89. return NErr_Success;
  90. }
  91. }
  92. }
  93. }
  94. else
  95. {
  96. if (header.mpegVersion == MPEGFrame::MPEG1)
  97. {
  98. // 0-9 : main_data_end
  99. uint16_t main_data_end = (buffer[0] << 1) | (buffer[1] >> 7);
  100. // read the 4 part2_3_lengths out so we know how big the main data section is
  101. uint16_t part2_3_length = ((buffer[2] & 0xF) << 8) | buffer[3]; // bits 20-32
  102. part2_3_length += ((buffer[9] & 0x1) << 11) | (buffer[10] << 3) | (buffer[11] >> 5) ; // bits 79-91
  103. part2_3_length += ((buffer[17] & 0x3F) << 6) | (buffer[18] >> 2); // bits 138-150
  104. part2_3_length += ((buffer[24] & 0x7) << 9) | (buffer[25] << 1) | (buffer[26] >> 7); // bits 197-209
  105. size_t offset = 32 + (part2_3_length+7)/8;
  106. if (offset+9 < buffer_len && buffer[offset] == 0xb4)
  107. {
  108. unsigned long crc=255;
  109. for (int i=0;i<9;i++)
  110. crcofl(0x0045, 0x0080, &crc, buffer[offset+i]);
  111. if ((crc & 0xFF) == buffer[offset+9])
  112. {
  113. total_length = (buffer[offset+3] << 24) | (buffer[offset+4] << 16) | (buffer[offset+5] << 8) | (buffer[offset+6]);
  114. codec_delay = (buffer[offset+1] << 8) | (buffer[offset+2]);
  115. additional_delay= (buffer[offset+7] << 8) | (buffer[offset+8]);
  116. return NErr_Success;
  117. }
  118. }
  119. }
  120. else
  121. { // MPEG2 and 2.5
  122. // 0-8 : main_data_end
  123. uint16_t main_data_end = buffer[0];
  124. // read the 4 part2_3_lengths out so we know how big the main data section is
  125. uint16_t part2_3_length = ((buffer[1] & 0x3F) << 6) | (buffer[2] >> 2); // bits 10-22
  126. part2_3_length += ((buffer[8] & 0x7) << 9) | (buffer[9] << 1) | (buffer[10] >> 7) ; // bits 69-81
  127. size_t offset = 17 + (part2_3_length+7)/8;
  128. if (offset+9 < buffer_len && buffer[offset] == 0xb4)
  129. {
  130. unsigned long crc=255;
  131. for (int i=0;i<9;i++)
  132. crcofl(0x0045, 0x0080, &crc, buffer[offset+i]);
  133. if ((crc & 0xFF) == buffer[offset+9])
  134. {
  135. total_length = (buffer[offset+3] << 24) | (buffer[offset+4] << 16) | (buffer[offset+5] << 8) | (buffer[offset+6]);
  136. codec_delay = (buffer[offset+1] << 8) | (buffer[offset+2]);
  137. additional_delay= (buffer[offset+7] << 8) | (buffer[offset+8]);
  138. return NErr_Success;
  139. }
  140. }
  141. }
  142. }
  143. return NErr_False;
  144. }