24bit.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include "MP3Coder.h"
  2. #include <malloc.h>
  3. static const float const_1_div_128_ = 32768.0 / 128.0; /* 8 bit multiplier */
  4. static const double const_1_div_2147483648_ = 32768.0 / 2147483648.0; /* 32 bit multiplier */
  5. static void Int8_To_Float32(float *dest, void *sourceBuffer, signed int sourceStride, unsigned int count)
  6. {
  7. signed char *src = (signed char*)sourceBuffer;
  8. while( count-- )
  9. {
  10. float samp = *src * const_1_div_128_;
  11. *dest = samp;
  12. src += sourceStride;
  13. dest ++;
  14. }
  15. }
  16. static void Int24_To_Float32(float *dest, void *sourceBuffer, signed int sourceStride, unsigned int count)
  17. {
  18. unsigned char *src = (unsigned char*)sourceBuffer;
  19. while ( count-- )
  20. {
  21. signed long temp = (((long)src[0]) << 8);
  22. temp = temp | (((long)src[1]) << 16);
  23. temp = temp | (((long)src[2]) << 24);
  24. *dest = (float) ((double)temp * const_1_div_2147483648_);
  25. src += sourceStride * 3;
  26. dest ++;
  27. }
  28. }
  29. int AudioCoderMP3_24::Encode(int framepos, void *in, int in_avail, int *in_used, void *out, int out_avail)
  30. {
  31. if (m_err) return -1;
  32. if (!hbeStream)
  33. {
  34. if (beInitStream(&beConfig, &ibuf_size_spls, &obuf_size, (PHBE_STREAM) &hbeStream) != BE_ERR_SUCCESSFUL)
  35. {
  36. m_err++;
  37. return -1;
  38. }
  39. ibuf_size = ibuf_size_spls * bytesPerSample;
  40. if (is_downmix) ibuf_size *= 2;
  41. bs = (char*)malloc(ibuf_size);
  42. bs_size = 0;
  43. }
  44. *in_used = 0;
  45. int needbytes = ibuf_size - bs_size;
  46. if (needbytes > 0 && in_avail > 0)
  47. {
  48. if (needbytes > in_avail)
  49. needbytes = in_avail;
  50. memcpy(bs + bs_size, in, needbytes);
  51. bs_size += needbytes;
  52. *in_used = needbytes;
  53. }
  54. if (!done)
  55. {
  56. if (bs_size < (int)ibuf_size) return 0;
  57. }
  58. if (out_avail < (int)obuf_size) return 0;
  59. int feedBytes = min(bs_size, (int)ibuf_size);
  60. int feedSamples = feedBytes / bytesPerSample;
  61. bs_size -= feedBytes;
  62. /*
  63. if (is_downmix)
  64. {
  65. int x;
  66. int l = feedBytes / 4;
  67. short *b = (short*)bs;
  68. for (x = 0; x < l; x ++)
  69. {
  70. b[x] = b[x * 2] / 2 + b[x * 2 + 1] / 2;
  71. }
  72. }
  73. */
  74. DWORD dwo = 0;
  75. if (feedSamples)
  76. {
  77. if (mono_input)
  78. {
  79. float *float_l = (float *)alloca(sizeof(float) * feedSamples);
  80. switch(bytesPerSample)
  81. {
  82. case 8:
  83. Int8_To_Float32(float_l, bs, 1, feedSamples);
  84. break;
  85. case 24:
  86. Int24_To_Float32(float_l, bs, 1, feedSamples);
  87. break;
  88. }
  89. if (beEncodeChunkFloatS16NI(hbeStream, feedSamples, float_l, float_l, (unsigned char *)out, &dwo) != BE_ERR_SUCCESSFUL)
  90. {
  91. m_err++;
  92. return -1;
  93. }
  94. }
  95. else
  96. {
  97. float *float_l = (float *)alloca(sizeof(float) * feedSamples / 2);
  98. float *float_r = (float *)alloca(sizeof(float) * feedSamples / 2);
  99. switch(bytesPerSample)
  100. {
  101. case 1: // 8 bit
  102. Int8_To_Float32(float_l, bs, 2, feedSamples / 2);
  103. Int8_To_Float32(float_r, bs + 1, 2, feedSamples / 2);
  104. break;
  105. case 3: // 24 bit
  106. Int24_To_Float32(float_l, bs, 2, feedSamples / 2);
  107. Int24_To_Float32(float_r, bs + 3, 2, feedSamples / 2);
  108. break;
  109. }
  110. if (beEncodeChunkFloatS16NI(hbeStream, feedSamples / 2, float_l, float_r, (unsigned char *)out, &dwo) != BE_ERR_SUCCESSFUL)
  111. {
  112. m_err++;
  113. return -1;
  114. }
  115. }
  116. }
  117. if (!dwo && done == 1)
  118. {
  119. if (beDeinitStream(hbeStream, (unsigned char *)out, &dwo) != BE_ERR_SUCCESSFUL)
  120. {
  121. m_err++;
  122. return -1;
  123. }
  124. done++;
  125. }
  126. return dwo;
  127. }