resampler.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. ** Copyright 2011, The Android Open-Source Project
  3. **
  4. ** Licensed under the Apache License, Version 2.0 (the "License");
  5. ** you may not use this file except in compliance with the License.
  6. ** You may obtain a copy of the License at
  7. **
  8. ** http://www.apache.org/licenses/LICENSE-2.0
  9. **
  10. ** Unless required by applicable law or agreed to in writing, software
  11. ** distributed under the License is distributed on an "AS IS" BASIS,
  12. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. ** See the License for the specific language governing permissions and
  14. ** limitations under the License.
  15. */
  16. //#define LOG_NDEBUG 0
  17. #define LOG_TAG "resampler"
  18. #include <errno.h>
  19. #include <stdlib.h>
  20. #include <log/log.h>
  21. #include <system/audio.h>
  22. #include <audio_utils/resampler.h>
  23. #include <speex/speex_resampler.h>
  24. struct resampler {
  25. struct resampler_itfe itfe;
  26. SpeexResamplerState *speex_resampler; // handle on speex resampler
  27. struct resampler_buffer_provider *provider; // buffer provider installed by client
  28. uint32_t in_sample_rate; // input sampling rate in Hz
  29. uint32_t out_sample_rate; // output sampling rate in Hz
  30. uint32_t channel_count; // number of channels (interleaved)
  31. int16_t *in_buf; // input buffer
  32. size_t in_buf_size; // input buffer size
  33. size_t frames_in; // number of frames in input buffer
  34. size_t frames_rq; // cached number of output frames
  35. size_t frames_needed; // minimum number of input frames to produce
  36. // frames_rq output frames
  37. int32_t speex_delay_ns; // delay introduced by speex resampler in ns
  38. };
  39. //------------------------------------------------------------------------------
  40. // speex based resampler
  41. //------------------------------------------------------------------------------
  42. static void resampler_reset(struct resampler_itfe *resampler)
  43. {
  44. struct resampler *rsmp = (struct resampler *)resampler;
  45. rsmp->frames_in = 0;
  46. rsmp->frames_rq = 0;
  47. if (rsmp != NULL && rsmp->speex_resampler != NULL) {
  48. speex_resampler_reset_mem(rsmp->speex_resampler);
  49. }
  50. }
  51. static int32_t resampler_delay_ns(struct resampler_itfe *resampler)
  52. {
  53. struct resampler *rsmp = (struct resampler *)resampler;
  54. int32_t delay = (int32_t)((1000000000 * (int64_t)rsmp->frames_in) / rsmp->in_sample_rate);
  55. delay += rsmp->speex_delay_ns;
  56. return delay;
  57. }
  58. // outputs a number of frames less or equal to *outFrameCount and updates *outFrameCount
  59. // with the actual number of frames produced.
  60. int resampler_resample_from_provider(struct resampler_itfe *resampler,
  61. int16_t *out,
  62. size_t *outFrameCount)
  63. {
  64. struct resampler *rsmp = (struct resampler *)resampler;
  65. if (rsmp == NULL || out == NULL || outFrameCount == NULL) {
  66. return -EINVAL;
  67. }
  68. if (rsmp->provider == NULL) {
  69. *outFrameCount = 0;
  70. return -ENOSYS;
  71. }
  72. size_t framesRq = *outFrameCount;
  73. // update and cache the number of frames needed at the input sampling rate to produce
  74. // the number of frames requested at the output sampling rate
  75. if (framesRq != rsmp->frames_rq) {
  76. rsmp->frames_needed = (framesRq * rsmp->in_sample_rate) / rsmp->out_sample_rate + 1;
  77. rsmp->frames_rq = framesRq;
  78. }
  79. size_t framesWr = 0;
  80. spx_uint32_t inFrames = 0;
  81. while (framesWr < framesRq) {
  82. if (rsmp->frames_in < rsmp->frames_needed) {
  83. // make sure that the number of frames present in rsmp->in_buf (rsmp->frames_in) is at
  84. // least the number of frames needed to produce the number of frames requested at
  85. // the output sampling rate
  86. if (rsmp->in_buf_size < rsmp->frames_needed) {
  87. rsmp->in_buf_size = rsmp->frames_needed;
  88. rsmp->in_buf = (int16_t *)realloc(rsmp->in_buf,
  89. rsmp->in_buf_size * rsmp->channel_count * sizeof(int16_t));
  90. }
  91. struct resampler_buffer buf;
  92. buf.frame_count = rsmp->frames_needed - rsmp->frames_in;
  93. rsmp->provider->get_next_buffer(rsmp->provider, &buf);
  94. if (buf.raw == NULL) {
  95. break;
  96. }
  97. memcpy(rsmp->in_buf + rsmp->frames_in * rsmp->channel_count,
  98. buf.raw,
  99. buf.frame_count * rsmp->channel_count * sizeof(int16_t));
  100. rsmp->frames_in += buf.frame_count;
  101. rsmp->provider->release_buffer(rsmp->provider, &buf);
  102. }
  103. spx_uint32_t outFrames = framesRq - framesWr;
  104. inFrames = rsmp->frames_in;
  105. if (rsmp->channel_count == 1) {
  106. speex_resampler_process_int(rsmp->speex_resampler,
  107. 0,
  108. rsmp->in_buf,
  109. &inFrames,
  110. out + framesWr,
  111. &outFrames);
  112. } else {
  113. speex_resampler_process_interleaved_int(rsmp->speex_resampler,
  114. rsmp->in_buf,
  115. &inFrames,
  116. out + framesWr * rsmp->channel_count,
  117. &outFrames);
  118. }
  119. framesWr += outFrames;
  120. rsmp->frames_in -= inFrames;
  121. ALOGW_IF((framesWr != framesRq) && (rsmp->frames_in != 0),
  122. "ReSampler::resample() remaining %zu frames in and %zu frames out",
  123. rsmp->frames_in, (framesRq - framesWr));
  124. }
  125. if (rsmp->frames_in) {
  126. memmove(rsmp->in_buf,
  127. rsmp->in_buf + inFrames * rsmp->channel_count,
  128. rsmp->frames_in * rsmp->channel_count * sizeof(int16_t));
  129. }
  130. *outFrameCount = framesWr;
  131. return 0;
  132. }
  133. int resampler_resample_from_input(struct resampler_itfe *resampler,
  134. int16_t *in,
  135. size_t *inFrameCount,
  136. int16_t *out,
  137. size_t *outFrameCount)
  138. {
  139. struct resampler *rsmp = (struct resampler *)resampler;
  140. if (rsmp == NULL || in == NULL || inFrameCount == NULL ||
  141. out == NULL || outFrameCount == NULL) {
  142. return -EINVAL;
  143. }
  144. if (rsmp->provider != NULL) {
  145. *outFrameCount = 0;
  146. return -ENOSYS;
  147. }
  148. if (rsmp->channel_count == 1) {
  149. speex_resampler_process_int(rsmp->speex_resampler,
  150. 0,
  151. in,
  152. (spx_uint32_t *)inFrameCount,
  153. out,
  154. (spx_uint32_t *)outFrameCount);
  155. } else {
  156. speex_resampler_process_interleaved_int(rsmp->speex_resampler,
  157. in,
  158. (spx_uint32_t *)inFrameCount,
  159. out,
  160. (spx_uint32_t *)outFrameCount);
  161. }
  162. ALOGV("resampler_resample_from_input() DONE in %zu out %zu", *inFrameCount, *outFrameCount);
  163. return 0;
  164. }
  165. int create_resampler(uint32_t inSampleRate,
  166. uint32_t outSampleRate,
  167. uint32_t channelCount,
  168. uint32_t quality,
  169. struct resampler_buffer_provider* provider,
  170. struct resampler_itfe **resampler)
  171. {
  172. int error;
  173. struct resampler *rsmp;
  174. ALOGV("create_resampler() In SR %d Out SR %d channels %d",
  175. inSampleRate, outSampleRate, channelCount);
  176. if (resampler == NULL) {
  177. return -EINVAL;
  178. }
  179. *resampler = NULL;
  180. if (quality <= RESAMPLER_QUALITY_MIN || quality >= RESAMPLER_QUALITY_MAX) {
  181. return -EINVAL;
  182. }
  183. rsmp = (struct resampler *)calloc(1, sizeof(struct resampler));
  184. rsmp->speex_resampler = speex_resampler_init(channelCount,
  185. inSampleRate,
  186. outSampleRate,
  187. quality,
  188. &error);
  189. if (rsmp->speex_resampler == NULL) {
  190. ALOGW("ReSampler: Cannot create speex resampler: %s", speex_resampler_strerror(error));
  191. free(rsmp);
  192. return -ENODEV;
  193. }
  194. rsmp->itfe.reset = resampler_reset;
  195. rsmp->itfe.resample_from_provider = resampler_resample_from_provider;
  196. rsmp->itfe.resample_from_input = resampler_resample_from_input;
  197. rsmp->itfe.delay_ns = resampler_delay_ns;
  198. rsmp->provider = provider;
  199. rsmp->in_sample_rate = inSampleRate;
  200. rsmp->out_sample_rate = outSampleRate;
  201. rsmp->channel_count = channelCount;
  202. rsmp->in_buf = NULL;
  203. rsmp->in_buf_size = 0;
  204. resampler_reset(&rsmp->itfe);
  205. int frames = speex_resampler_get_input_latency(rsmp->speex_resampler);
  206. rsmp->speex_delay_ns = (int32_t)((1000000000 * (int64_t)frames) / rsmp->in_sample_rate);
  207. frames = speex_resampler_get_output_latency(rsmp->speex_resampler);
  208. rsmp->speex_delay_ns += (int32_t)((1000000000 * (int64_t)frames) / rsmp->out_sample_rate);
  209. *resampler = &rsmp->itfe;
  210. ALOGV("create_resampler() DONE rsmp %p &rsmp->itfe %p speex %p",
  211. rsmp, &rsmp->itfe, rsmp->speex_resampler);
  212. return 0;
  213. }
  214. void release_resampler(struct resampler_itfe *resampler)
  215. {
  216. struct resampler *rsmp = (struct resampler *)resampler;
  217. if (rsmp == NULL) {
  218. return;
  219. }
  220. free(rsmp->in_buf);
  221. if (rsmp->speex_resampler != NULL) {
  222. speex_resampler_destroy(rsmp->speex_resampler);
  223. }
  224. free(rsmp);
  225. }