resample.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
  3. * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/common.h"
  22. #include "libavutil/libm.h"
  23. #include "libavutil/log.h"
  24. #include "internal.h"
  25. #include "resample.h"
  26. #include "audio_data.h"
  27. /* double template */
  28. #define CONFIG_RESAMPLE_DBL
  29. #include "resample_template.c"
  30. #undef CONFIG_RESAMPLE_DBL
  31. /* float template */
  32. #define CONFIG_RESAMPLE_FLT
  33. #include "resample_template.c"
  34. #undef CONFIG_RESAMPLE_FLT
  35. /* s32 template */
  36. #define CONFIG_RESAMPLE_S32
  37. #include "resample_template.c"
  38. #undef CONFIG_RESAMPLE_S32
  39. /* s16 template */
  40. #include "resample_template.c"
  41. /* 0th order modified Bessel function of the first kind. */
  42. static double bessel(double x)
  43. {
  44. double v = 1;
  45. double lastv = 0;
  46. double t = 1;
  47. int i;
  48. x = x * x / 4;
  49. for (i = 1; v != lastv; i++) {
  50. lastv = v;
  51. t *= x / (i * i);
  52. v += t;
  53. }
  54. return v;
  55. }
  56. /* Build a polyphase filterbank. */
  57. static int build_filter(ResampleContext *c, double factor)
  58. {
  59. int ph, i;
  60. double x, y, w;
  61. double *tab;
  62. int tap_count = c->filter_length;
  63. int phase_count = 1 << c->phase_shift;
  64. const int center = (tap_count - 1) / 2;
  65. tab = av_malloc(tap_count * sizeof(*tab));
  66. if (!tab)
  67. return AVERROR(ENOMEM);
  68. for (ph = 0; ph < phase_count; ph++) {
  69. double norm = 0;
  70. for (i = 0; i < tap_count; i++) {
  71. x = M_PI * ((double)(i - center) - (double)ph / phase_count) * factor;
  72. if (x == 0) y = 1.0;
  73. else y = sin(x) / x;
  74. switch (c->filter_type) {
  75. case AV_RESAMPLE_FILTER_TYPE_CUBIC: {
  76. const float d = -0.5; //first order derivative = -0.5
  77. x = fabs(((double)(i - center) - (double)ph / phase_count) * factor);
  78. if (x < 1.0) y = 1 - 3 * x*x + 2 * x*x*x + d * ( -x*x + x*x*x);
  79. else y = d * (-4 + 8 * x - 5 * x*x + x*x*x);
  80. break;
  81. }
  82. case AV_RESAMPLE_FILTER_TYPE_BLACKMAN_NUTTALL:
  83. w = 2.0 * x / (factor * tap_count) + M_PI;
  84. y *= 0.3635819 - 0.4891775 * cos( w) +
  85. 0.1365995 * cos(2 * w) -
  86. 0.0106411 * cos(3 * w);
  87. break;
  88. case AV_RESAMPLE_FILTER_TYPE_KAISER:
  89. w = 2.0 * x / (factor * tap_count * M_PI);
  90. y *= bessel(c->kaiser_beta * sqrt(FFMAX(1 - w * w, 0)));
  91. break;
  92. }
  93. tab[i] = y;
  94. norm += y;
  95. }
  96. /* normalize so that an uniform color remains the same */
  97. for (i = 0; i < tap_count; i++)
  98. tab[i] = tab[i] / norm;
  99. c->set_filter(c->filter_bank, tab, ph, tap_count);
  100. }
  101. av_free(tab);
  102. return 0;
  103. }
  104. ResampleContext *ff_audio_resample_init(AVAudioResampleContext *avr)
  105. {
  106. ResampleContext *c;
  107. int out_rate = avr->out_sample_rate;
  108. int in_rate = avr->in_sample_rate;
  109. double factor = FFMIN(out_rate * avr->cutoff / in_rate, 1.0);
  110. int phase_count = 1 << avr->phase_shift;
  111. int felem_size;
  112. if (avr->internal_sample_fmt != AV_SAMPLE_FMT_S16P &&
  113. avr->internal_sample_fmt != AV_SAMPLE_FMT_S32P &&
  114. avr->internal_sample_fmt != AV_SAMPLE_FMT_FLTP &&
  115. avr->internal_sample_fmt != AV_SAMPLE_FMT_DBLP) {
  116. av_log(avr, AV_LOG_ERROR, "Unsupported internal format for "
  117. "resampling: %s\n",
  118. av_get_sample_fmt_name(avr->internal_sample_fmt));
  119. return NULL;
  120. }
  121. c = av_mallocz(sizeof(*c));
  122. if (!c)
  123. return NULL;
  124. c->avr = avr;
  125. c->phase_shift = avr->phase_shift;
  126. c->phase_mask = phase_count - 1;
  127. c->linear = avr->linear_interp;
  128. c->filter_length = FFMAX((int)ceil(avr->filter_size / factor), 1);
  129. c->filter_type = avr->filter_type;
  130. c->kaiser_beta = avr->kaiser_beta;
  131. switch (avr->internal_sample_fmt) {
  132. case AV_SAMPLE_FMT_DBLP:
  133. c->resample_one = c->linear ? resample_linear_dbl : resample_one_dbl;
  134. c->resample_nearest = resample_nearest_dbl;
  135. c->set_filter = set_filter_dbl;
  136. break;
  137. case AV_SAMPLE_FMT_FLTP:
  138. c->resample_one = c->linear ? resample_linear_flt : resample_one_flt;
  139. c->resample_nearest = resample_nearest_flt;
  140. c->set_filter = set_filter_flt;
  141. break;
  142. case AV_SAMPLE_FMT_S32P:
  143. c->resample_one = c->linear ? resample_linear_s32 : resample_one_s32;
  144. c->resample_nearest = resample_nearest_s32;
  145. c->set_filter = set_filter_s32;
  146. break;
  147. case AV_SAMPLE_FMT_S16P:
  148. c->resample_one = c->linear ? resample_linear_s16 : resample_one_s16;
  149. c->resample_nearest = resample_nearest_s16;
  150. c->set_filter = set_filter_s16;
  151. break;
  152. }
  153. if (ARCH_AARCH64)
  154. ff_audio_resample_init_aarch64(c, avr->internal_sample_fmt);
  155. if (ARCH_ARM)
  156. ff_audio_resample_init_arm(c, avr->internal_sample_fmt);
  157. felem_size = av_get_bytes_per_sample(avr->internal_sample_fmt);
  158. c->filter_bank = av_mallocz(c->filter_length * (phase_count + 1) * felem_size);
  159. if (!c->filter_bank)
  160. goto error;
  161. if (build_filter(c, factor) < 0)
  162. goto error;
  163. memcpy(&c->filter_bank[(c->filter_length * phase_count + 1) * felem_size],
  164. c->filter_bank, (c->filter_length - 1) * felem_size);
  165. memcpy(&c->filter_bank[c->filter_length * phase_count * felem_size],
  166. &c->filter_bank[(c->filter_length - 1) * felem_size], felem_size);
  167. c->compensation_distance = 0;
  168. if (!av_reduce(&c->src_incr, &c->dst_incr, out_rate,
  169. in_rate * (int64_t)phase_count, INT32_MAX / 2))
  170. goto error;
  171. c->ideal_dst_incr = c->dst_incr;
  172. c->padding_size = (c->filter_length - 1) / 2;
  173. c->initial_padding_filled = 0;
  174. c->index = 0;
  175. c->frac = 0;
  176. /* allocate internal buffer */
  177. c->buffer = ff_audio_data_alloc(avr->resample_channels, c->padding_size,
  178. avr->internal_sample_fmt,
  179. "resample buffer");
  180. if (!c->buffer)
  181. goto error;
  182. c->buffer->nb_samples = c->padding_size;
  183. c->initial_padding_samples = c->padding_size;
  184. av_log(avr, AV_LOG_DEBUG, "resample: %s from %d Hz to %d Hz\n",
  185. av_get_sample_fmt_name(avr->internal_sample_fmt),
  186. avr->in_sample_rate, avr->out_sample_rate);
  187. return c;
  188. error:
  189. ff_audio_data_free(&c->buffer);
  190. av_free(c->filter_bank);
  191. av_free(c);
  192. return NULL;
  193. }
  194. void ff_audio_resample_free(ResampleContext **c)
  195. {
  196. if (!*c)
  197. return;
  198. ff_audio_data_free(&(*c)->buffer);
  199. av_free((*c)->filter_bank);
  200. av_freep(c);
  201. }
  202. int avresample_set_compensation(AVAudioResampleContext *avr, int sample_delta,
  203. int compensation_distance)
  204. {
  205. ResampleContext *c;
  206. if (compensation_distance < 0)
  207. return AVERROR(EINVAL);
  208. if (!compensation_distance && sample_delta)
  209. return AVERROR(EINVAL);
  210. if (!avr->resample_needed) {
  211. av_log(avr, AV_LOG_ERROR, "Unable to set resampling compensation\n");
  212. return AVERROR(EINVAL);
  213. }
  214. c = avr->resample;
  215. c->compensation_distance = compensation_distance;
  216. if (compensation_distance) {
  217. c->dst_incr = c->ideal_dst_incr - c->ideal_dst_incr *
  218. (int64_t)sample_delta / compensation_distance;
  219. } else {
  220. c->dst_incr = c->ideal_dst_incr;
  221. }
  222. return 0;
  223. }
  224. static int resample(ResampleContext *c, void *dst, const void *src,
  225. int *consumed, int src_size, int dst_size, int update_ctx,
  226. int nearest_neighbour)
  227. {
  228. int dst_index;
  229. unsigned int index = c->index;
  230. int frac = c->frac;
  231. int dst_incr_frac = c->dst_incr % c->src_incr;
  232. int dst_incr = c->dst_incr / c->src_incr;
  233. int compensation_distance = c->compensation_distance;
  234. if (!dst != !src)
  235. return AVERROR(EINVAL);
  236. if (nearest_neighbour) {
  237. uint64_t index2 = ((uint64_t)index) << 32;
  238. int64_t incr = (1LL << 32) * c->dst_incr / c->src_incr;
  239. dst_size = FFMIN(dst_size,
  240. (src_size-1-index) * (int64_t)c->src_incr /
  241. c->dst_incr);
  242. if (dst) {
  243. for(dst_index = 0; dst_index < dst_size; dst_index++) {
  244. c->resample_nearest(dst, dst_index, src, index2 >> 32);
  245. index2 += incr;
  246. }
  247. } else {
  248. dst_index = dst_size;
  249. }
  250. index += dst_index * dst_incr;
  251. index += (frac + dst_index * (int64_t)dst_incr_frac) / c->src_incr;
  252. frac = (frac + dst_index * (int64_t)dst_incr_frac) % c->src_incr;
  253. } else {
  254. for (dst_index = 0; dst_index < dst_size; dst_index++) {
  255. int sample_index = index >> c->phase_shift;
  256. if (sample_index + c->filter_length > src_size)
  257. break;
  258. if (dst)
  259. c->resample_one(c, dst, dst_index, src, index, frac);
  260. frac += dst_incr_frac;
  261. index += dst_incr;
  262. if (frac >= c->src_incr) {
  263. frac -= c->src_incr;
  264. index++;
  265. }
  266. if (dst_index + 1 == compensation_distance) {
  267. compensation_distance = 0;
  268. dst_incr_frac = c->ideal_dst_incr % c->src_incr;
  269. dst_incr = c->ideal_dst_incr / c->src_incr;
  270. }
  271. }
  272. }
  273. if (consumed)
  274. *consumed = index >> c->phase_shift;
  275. if (update_ctx) {
  276. index &= c->phase_mask;
  277. if (compensation_distance) {
  278. compensation_distance -= dst_index;
  279. if (compensation_distance <= 0)
  280. return AVERROR_BUG;
  281. }
  282. c->frac = frac;
  283. c->index = index;
  284. c->dst_incr = dst_incr_frac + c->src_incr*dst_incr;
  285. c->compensation_distance = compensation_distance;
  286. }
  287. return dst_index;
  288. }
  289. int ff_audio_resample(ResampleContext *c, AudioData *dst, AudioData *src)
  290. {
  291. int ch, in_samples, in_leftover, consumed = 0, out_samples = 0;
  292. int ret = AVERROR(EINVAL);
  293. int nearest_neighbour = (c->compensation_distance == 0 &&
  294. c->filter_length == 1 &&
  295. c->phase_shift == 0);
  296. in_samples = src ? src->nb_samples : 0;
  297. in_leftover = c->buffer->nb_samples;
  298. /* add input samples to the internal buffer */
  299. if (src) {
  300. ret = ff_audio_data_combine(c->buffer, in_leftover, src, 0, in_samples);
  301. if (ret < 0)
  302. return ret;
  303. } else if (in_leftover <= c->final_padding_samples) {
  304. /* no remaining samples to flush */
  305. return 0;
  306. }
  307. if (!c->initial_padding_filled) {
  308. int bps = av_get_bytes_per_sample(c->avr->internal_sample_fmt);
  309. int i;
  310. if (src && c->buffer->nb_samples < 2 * c->padding_size)
  311. return 0;
  312. for (i = 0; i < c->padding_size; i++)
  313. for (ch = 0; ch < c->buffer->channels; ch++) {
  314. if (c->buffer->nb_samples > 2 * c->padding_size - i) {
  315. memcpy(c->buffer->data[ch] + bps * i,
  316. c->buffer->data[ch] + bps * (2 * c->padding_size - i), bps);
  317. } else {
  318. memset(c->buffer->data[ch] + bps * i, 0, bps);
  319. }
  320. }
  321. c->initial_padding_filled = 1;
  322. }
  323. if (!src && !c->final_padding_filled) {
  324. int bps = av_get_bytes_per_sample(c->avr->internal_sample_fmt);
  325. int i;
  326. ret = ff_audio_data_realloc(c->buffer,
  327. FFMAX(in_samples, in_leftover) +
  328. c->padding_size);
  329. if (ret < 0) {
  330. av_log(c->avr, AV_LOG_ERROR, "Error reallocating resampling buffer\n");
  331. return AVERROR(ENOMEM);
  332. }
  333. for (i = 0; i < c->padding_size; i++)
  334. for (ch = 0; ch < c->buffer->channels; ch++) {
  335. if (in_leftover > i) {
  336. memcpy(c->buffer->data[ch] + bps * (in_leftover + i),
  337. c->buffer->data[ch] + bps * (in_leftover - i - 1),
  338. bps);
  339. } else {
  340. memset(c->buffer->data[ch] + bps * (in_leftover + i),
  341. 0, bps);
  342. }
  343. }
  344. c->buffer->nb_samples += c->padding_size;
  345. c->final_padding_samples = c->padding_size;
  346. c->final_padding_filled = 1;
  347. }
  348. /* calculate output size and reallocate output buffer if needed */
  349. /* TODO: try to calculate this without the dummy resample() run */
  350. if (!dst->read_only && dst->allow_realloc) {
  351. out_samples = resample(c, NULL, NULL, NULL, c->buffer->nb_samples,
  352. INT_MAX, 0, nearest_neighbour);
  353. ret = ff_audio_data_realloc(dst, out_samples);
  354. if (ret < 0) {
  355. av_log(c->avr, AV_LOG_ERROR, "error reallocating output\n");
  356. return ret;
  357. }
  358. }
  359. /* resample each channel plane */
  360. for (ch = 0; ch < c->buffer->channels; ch++) {
  361. out_samples = resample(c, (void *)dst->data[ch],
  362. (const void *)c->buffer->data[ch], &consumed,
  363. c->buffer->nb_samples, dst->allocated_samples,
  364. ch + 1 == c->buffer->channels, nearest_neighbour);
  365. }
  366. if (out_samples < 0) {
  367. av_log(c->avr, AV_LOG_ERROR, "error during resampling\n");
  368. return out_samples;
  369. }
  370. /* drain consumed samples from the internal buffer */
  371. ff_audio_data_drain(c->buffer, consumed);
  372. c->initial_padding_samples = FFMAX(c->initial_padding_samples - consumed, 0);
  373. av_log(c->avr, AV_LOG_TRACE, "resampled %d in + %d leftover to %d out + %d leftover\n",
  374. in_samples, in_leftover, out_samples, c->buffer->nb_samples);
  375. dst->nb_samples = out_samples;
  376. return 0;
  377. }
  378. int avresample_get_delay(AVAudioResampleContext *avr)
  379. {
  380. ResampleContext *c = avr->resample;
  381. if (!avr->resample_needed || !avr->resample)
  382. return 0;
  383. return FFMAX(c->buffer->nb_samples - c->padding_size, 0);
  384. }