You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

452 lines
15KB

  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 Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. AudioData *fifo_buf = NULL;
  207. int ret = 0;
  208. if (compensation_distance < 0)
  209. return AVERROR(EINVAL);
  210. if (!compensation_distance && sample_delta)
  211. return AVERROR(EINVAL);
  212. if (!avr->resample_needed) {
  213. av_log(avr, AV_LOG_ERROR, "Unable to set resampling compensation\n");
  214. return AVERROR(EINVAL);
  215. }
  216. c = avr->resample;
  217. c->compensation_distance = compensation_distance;
  218. if (compensation_distance) {
  219. c->dst_incr = c->ideal_dst_incr - c->ideal_dst_incr *
  220. (int64_t)sample_delta / compensation_distance;
  221. } else {
  222. c->dst_incr = c->ideal_dst_incr;
  223. }
  224. return 0;
  225. reinit_fail:
  226. ff_audio_data_free(&fifo_buf);
  227. return ret;
  228. }
  229. static int resample(ResampleContext *c, void *dst, const void *src,
  230. int *consumed, int src_size, int dst_size, int update_ctx,
  231. int nearest_neighbour)
  232. {
  233. int dst_index;
  234. unsigned int index = c->index;
  235. int frac = c->frac;
  236. int dst_incr_frac = c->dst_incr % c->src_incr;
  237. int dst_incr = c->dst_incr / c->src_incr;
  238. int compensation_distance = c->compensation_distance;
  239. if (!dst != !src)
  240. return AVERROR(EINVAL);
  241. if (nearest_neighbour) {
  242. uint64_t index2 = ((uint64_t)index) << 32;
  243. int64_t incr = (1LL << 32) * c->dst_incr / c->src_incr;
  244. dst_size = FFMIN(dst_size,
  245. (src_size-1-index) * (int64_t)c->src_incr /
  246. c->dst_incr);
  247. if (dst) {
  248. for(dst_index = 0; dst_index < dst_size; dst_index++) {
  249. c->resample_nearest(dst, dst_index, src, index2 >> 32);
  250. index2 += incr;
  251. }
  252. } else {
  253. dst_index = dst_size;
  254. }
  255. index += dst_index * dst_incr;
  256. index += (frac + dst_index * (int64_t)dst_incr_frac) / c->src_incr;
  257. frac = (frac + dst_index * (int64_t)dst_incr_frac) % c->src_incr;
  258. } else {
  259. for (dst_index = 0; dst_index < dst_size; dst_index++) {
  260. int sample_index = index >> c->phase_shift;
  261. if (sample_index + c->filter_length > src_size)
  262. break;
  263. if (dst)
  264. c->resample_one(c, dst, dst_index, src, index, frac);
  265. frac += dst_incr_frac;
  266. index += dst_incr;
  267. if (frac >= c->src_incr) {
  268. frac -= c->src_incr;
  269. index++;
  270. }
  271. if (dst_index + 1 == compensation_distance) {
  272. compensation_distance = 0;
  273. dst_incr_frac = c->ideal_dst_incr % c->src_incr;
  274. dst_incr = c->ideal_dst_incr / c->src_incr;
  275. }
  276. }
  277. }
  278. if (consumed)
  279. *consumed = index >> c->phase_shift;
  280. if (update_ctx) {
  281. index &= c->phase_mask;
  282. if (compensation_distance) {
  283. compensation_distance -= dst_index;
  284. if (compensation_distance <= 0)
  285. return AVERROR_BUG;
  286. }
  287. c->frac = frac;
  288. c->index = index;
  289. c->dst_incr = dst_incr_frac + c->src_incr*dst_incr;
  290. c->compensation_distance = compensation_distance;
  291. }
  292. return dst_index;
  293. }
  294. int ff_audio_resample(ResampleContext *c, AudioData *dst, AudioData *src)
  295. {
  296. int ch, in_samples, in_leftover, consumed = 0, out_samples = 0;
  297. int ret = AVERROR(EINVAL);
  298. int nearest_neighbour = (c->compensation_distance == 0 &&
  299. c->filter_length == 1 &&
  300. c->phase_shift == 0);
  301. in_samples = src ? src->nb_samples : 0;
  302. in_leftover = c->buffer->nb_samples;
  303. /* add input samples to the internal buffer */
  304. if (src) {
  305. ret = ff_audio_data_combine(c->buffer, in_leftover, src, 0, in_samples);
  306. if (ret < 0)
  307. return ret;
  308. } else if (in_leftover <= c->final_padding_samples) {
  309. /* no remaining samples to flush */
  310. return 0;
  311. }
  312. if (!c->initial_padding_filled) {
  313. int bps = av_get_bytes_per_sample(c->avr->internal_sample_fmt);
  314. int i;
  315. if (src && c->buffer->nb_samples < 2 * c->padding_size)
  316. return 0;
  317. for (i = 0; i < c->padding_size; i++)
  318. for (ch = 0; ch < c->buffer->channels; ch++) {
  319. if (c->buffer->nb_samples > 2 * c->padding_size - i) {
  320. memcpy(c->buffer->data[ch] + bps * i,
  321. c->buffer->data[ch] + bps * (2 * c->padding_size - i), bps);
  322. } else {
  323. memset(c->buffer->data[ch] + bps * i, 0, bps);
  324. }
  325. }
  326. c->initial_padding_filled = 1;
  327. }
  328. if (!src && !c->final_padding_filled) {
  329. int bps = av_get_bytes_per_sample(c->avr->internal_sample_fmt);
  330. int i;
  331. ret = ff_audio_data_realloc(c->buffer,
  332. FFMAX(in_samples, in_leftover) +
  333. c->padding_size);
  334. if (ret < 0) {
  335. av_log(c->avr, AV_LOG_ERROR, "Error reallocating resampling buffer\n");
  336. return AVERROR(ENOMEM);
  337. }
  338. for (i = 0; i < c->padding_size; i++)
  339. for (ch = 0; ch < c->buffer->channels; ch++) {
  340. if (in_leftover > i) {
  341. memcpy(c->buffer->data[ch] + bps * (in_leftover + i),
  342. c->buffer->data[ch] + bps * (in_leftover - i - 1),
  343. bps);
  344. } else {
  345. memset(c->buffer->data[ch] + bps * (in_leftover + i),
  346. 0, bps);
  347. }
  348. }
  349. c->buffer->nb_samples += c->padding_size;
  350. c->final_padding_samples = c->padding_size;
  351. c->final_padding_filled = 1;
  352. }
  353. /* calculate output size and reallocate output buffer if needed */
  354. /* TODO: try to calculate this without the dummy resample() run */
  355. if (!dst->read_only && dst->allow_realloc) {
  356. out_samples = resample(c, NULL, NULL, NULL, c->buffer->nb_samples,
  357. INT_MAX, 0, nearest_neighbour);
  358. ret = ff_audio_data_realloc(dst, out_samples);
  359. if (ret < 0) {
  360. av_log(c->avr, AV_LOG_ERROR, "error reallocating output\n");
  361. return ret;
  362. }
  363. }
  364. /* resample each channel plane */
  365. for (ch = 0; ch < c->buffer->channels; ch++) {
  366. out_samples = resample(c, (void *)dst->data[ch],
  367. (const void *)c->buffer->data[ch], &consumed,
  368. c->buffer->nb_samples, dst->allocated_samples,
  369. ch + 1 == c->buffer->channels, nearest_neighbour);
  370. }
  371. if (out_samples < 0) {
  372. av_log(c->avr, AV_LOG_ERROR, "error during resampling\n");
  373. return out_samples;
  374. }
  375. /* drain consumed samples from the internal buffer */
  376. ff_audio_data_drain(c->buffer, consumed);
  377. c->initial_padding_samples = FFMAX(c->initial_padding_samples - consumed, 0);
  378. av_log(c->avr, AV_LOG_TRACE, "resampled %d in + %d leftover to %d out + %d leftover\n",
  379. in_samples, in_leftover, out_samples, c->buffer->nb_samples);
  380. dst->nb_samples = out_samples;
  381. return 0;
  382. }
  383. int avresample_get_delay(AVAudioResampleContext *avr)
  384. {
  385. ResampleContext *c = avr->resample;
  386. if (!avr->resample_needed || !avr->resample)
  387. return 0;
  388. return FFMAX(c->buffer->nb_samples - c->padding_size, 0);
  389. }