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.

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