Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

234 строки
5.6KB

  1. #include "mp3-header.h"
  2. #include <stdint.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. // layer-1, layer-2, layer-3
  6. static int s_bitrate_mpeg1[3][16] = {
  7. { 0/*free*/, 32000, 64000, 96000, 128000, 160000, 192000, 224000, 256000, 288000, 320000, 352000, 384000, 416000, 448000, -1 },
  8. { 0/*free*/, 32000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 160000, 192000, 224000, 256000, 320000, 384000, -1 },
  9. { 0/*free*/, 32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 160000, 192000, 224000, 256000, 320000, -1 },
  10. };
  11. // layer-1, layer-2, layer-3
  12. static int s_bitrate_mpeg2[3][16] = {
  13. { 0/*free*/, 32000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 144000, 160000, 176000, 192000, 224000, 256000, -1 },
  14. { 0/*free*/, 8000, 16000, 24000, 32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 144000, 160000, -1 },
  15. { 0/*free*/, 8000, 16000, 24000, 32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 144000, 160000, -1 },
  16. };
  17. // layer-1, layer-2, layer-3
  18. static int s_frequency_mpeg1[4] = { 44100, 48000, 32000, -1 };
  19. static int s_frequency_mpeg2[4] = { 22050, 24000, 16000, -1 };
  20. static int s_frequency_mpeg25[4] = { 11025, 12000, 8000, -1 };
  21. // layer-1, layer-2, layer-3
  22. //static int s_frames_mpeg1[3] = { 384, 1152, 1152 };
  23. //static int s_frames_mpeg2[3] = { 384, 1152, 576 };
  24. //static int s_frames_mpeg25[3] = { 384, 1152, 576 };
  25. // layer-1 bytes = ((frames / 8 * bitrate) / frequency + padding * 4
  26. // layer-2/3 bytes = ((frames / 8 * bitrate) / frequency + padding
  27. int mp3_header_load(struct mp3_header_t* mp3, const void* data, int bytes)
  28. {
  29. const uint8_t* p;
  30. if (bytes < 4)
  31. return 0;
  32. p = data;
  33. if (0 == memcmp("TAG", p, 3))
  34. {
  35. if (bytes < 128/*ID3v1*/ + 4)
  36. return 0;
  37. p += 128;
  38. }
  39. else if (0 == memcmp("ID3", p, 3))
  40. {
  41. uint32_t n;
  42. if (3 != p[3]/*version*/ || bytes < 10)
  43. return 0;
  44. n = (((uint32_t)p[6] & 0x7F) << 21) | (((uint32_t)p[7] & 0x7F) << 14) | (((uint32_t)p[8] & 0x7F) << 7) | (p[9] & 0x7F);
  45. if (bytes < (int)n + 10)
  46. return 0;
  47. p += n + 10;
  48. }
  49. //sync: 1111 1111 111
  50. if (0xFF != p[0] || 0xE0 != (p[1] & 0xE0))
  51. {
  52. assert(0);
  53. return 0;
  54. }
  55. mp3->version = (p[1] >> 3) & 0x03;
  56. mp3->layer = (p[1] >> 1) & 0x03;
  57. mp3->protection = p[1] & 0x01;
  58. mp3->bitrate_index = (p[2] >> 4) & 0x0F;
  59. mp3->sampling_frequency = (p[2] >> 2) & 0x03;
  60. mp3->priviate = p[2] & 0x01;
  61. mp3->mode = (p[3] >> 6) & 0x03;
  62. mp3->mode_extension = (p[3] >> 4) & 0x03;
  63. mp3->copyright = (p[3] >> 3) & 0x01;
  64. mp3->original = (p[3] >> 2) & 0x01;
  65. mp3->emphasis = p[3] & 0x03;
  66. return (int)(p - (uint8_t*)data) + 4;
  67. }
  68. int mp3_header_save(const struct mp3_header_t* mp3, void* data, int bytes)
  69. {
  70. uint8_t* p;
  71. if (bytes < 4)
  72. return 0;
  73. p = data;
  74. p[0] = 0xFF;
  75. p[1] = (uint8_t)(0xE0 | (mp3->version << 3) | (mp3->layer << 1) | mp3->protection);
  76. p[2] = (uint8_t)((mp3->bitrate_index << 4) | (mp3->sampling_frequency << 2) | 0x00 /*padding*/ | mp3->priviate);
  77. p[3] = (uint8_t)((mp3->mode << 6) | (mp3->mode_extension << 4) | (mp3->copyright << 3) | (mp3->original << 2) | mp3->emphasis);
  78. return 4;
  79. }
  80. int mp3_get_channel(const struct mp3_header_t* mp3)
  81. {
  82. return 0x03 == mp3->mode ? 1 : 2;
  83. }
  84. int mp3_get_bitrate(const struct mp3_header_t* mp3)
  85. {
  86. if (mp3->layer < 1 || mp3->layer > 3)
  87. {
  88. assert(0);
  89. return -1;
  90. }
  91. switch (mp3->version)
  92. {
  93. case MP3_MPEG1:
  94. return s_bitrate_mpeg1[3 - mp3->layer][mp3->bitrate_index];
  95. case MP3_MPEG2:
  96. case MP3_MPEG2_5:
  97. return s_bitrate_mpeg2[3 - mp3->layer][mp3->bitrate_index];
  98. default:
  99. assert(0);
  100. return -1;
  101. }
  102. }
  103. static int mp3_find_bitrate(const int* arr, int bitrate)
  104. {
  105. int i;
  106. for (i = 0; i < 16; i++)
  107. {
  108. if (bitrate == arr[i])
  109. return i;
  110. }
  111. return -1;
  112. }
  113. int mp3_set_bitrate(struct mp3_header_t* mp3, int bitrate)
  114. {
  115. int r;
  116. if (mp3->layer < 1 || mp3->layer > 3)
  117. {
  118. assert(0);
  119. return -1;
  120. }
  121. switch (mp3->version)
  122. {
  123. case MP3_MPEG1:
  124. r = mp3_find_bitrate(s_bitrate_mpeg1[3 - mp3->layer], bitrate);
  125. break;
  126. case MP3_MPEG2:
  127. case MP3_MPEG2_5:
  128. r = mp3_find_bitrate(s_bitrate_mpeg2[3 - mp3->layer], bitrate);
  129. break;
  130. default:
  131. assert(0);
  132. r = -1;
  133. }
  134. if (-1 == r)
  135. return -1;
  136. mp3->bitrate_index = (unsigned int)r;
  137. return 0;
  138. }
  139. int mp3_get_frequency(const struct mp3_header_t* mp3)
  140. {
  141. if (mp3->sampling_frequency < 0 || mp3->sampling_frequency > 3)
  142. return -1;
  143. switch (mp3->version)
  144. {
  145. case MP3_MPEG1: return s_frequency_mpeg1[mp3->sampling_frequency];
  146. case MP3_MPEG2: return s_frequency_mpeg2[mp3->sampling_frequency];
  147. case MP3_MPEG2_5: return s_frequency_mpeg25[mp3->sampling_frequency];
  148. default: assert(0); return -1;
  149. }
  150. }
  151. static int mp3_find_frequency(const int* arr, int frequency)
  152. {
  153. int i;
  154. for (i = 0; i < 4; i++)
  155. {
  156. if (frequency == arr[i])
  157. return i;
  158. }
  159. return -1;
  160. }
  161. int mp3_set_frequency(struct mp3_header_t* mp3, int frequency)
  162. {
  163. int r;
  164. switch (mp3->version)
  165. {
  166. case MP3_MPEG1:
  167. r = mp3_find_frequency(s_frequency_mpeg1, frequency);
  168. break;
  169. case MP3_MPEG2:
  170. r = mp3_find_frequency(s_frequency_mpeg2, frequency);
  171. break;
  172. case MP3_MPEG2_5:
  173. r = mp3_find_frequency(s_frequency_mpeg25, frequency);
  174. break;
  175. default:
  176. assert(0);
  177. r = -1;
  178. }
  179. if (-1 == r)
  180. return -1;
  181. mp3->sampling_frequency = (unsigned int)r;
  182. return 0;
  183. }
  184. #if defined(DEBUG) || defined(_DEBUG)
  185. void mp3_header_test(void)
  186. {
  187. uint8_t v[4] = { 0xff, 0xfb, 0xe0, 0x64 };
  188. uint8_t v2[4];
  189. struct mp3_header_t mp3;
  190. assert(4 == mp3_header_load(&mp3, v, 4));
  191. assert(MP3_MPEG1 == mp3.version && MP3_LAYER3 == mp3.layer);
  192. assert(14 == mp3.bitrate_index && 320000 == mp3_get_bitrate(&mp3));
  193. assert(0 == mp3.sampling_frequency && 44100 == mp3_get_frequency(&mp3));
  194. assert(1 == mp3.mode && 1 == mp3.protection);
  195. assert(4 == mp3_header_save(&mp3, v2, 4));
  196. assert(0 == memcmp(v, v2, 4));
  197. }
  198. #endif