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.

483 lines
16KB

  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. struct ResampleContext {
  28. AVAudioResampleContext *avr;
  29. AudioData *buffer;
  30. uint8_t *filter_bank;
  31. int filter_length;
  32. int ideal_dst_incr;
  33. int dst_incr;
  34. int index;
  35. int frac;
  36. int src_incr;
  37. int compensation_distance;
  38. int phase_shift;
  39. int phase_mask;
  40. int linear;
  41. enum AVResampleFilterType filter_type;
  42. int kaiser_beta;
  43. double factor;
  44. void (*set_filter)(void *filter, double *tab, int phase, int tap_count);
  45. void (*resample_one)(struct ResampleContext *c, void *dst0,
  46. int dst_index, const void *src0, int src_size,
  47. int index, int frac);
  48. void (*resample_nearest)(void *dst0, int dst_index,
  49. const void *src0, int index);
  50. int padding_size;
  51. };
  52. /* double template */
  53. #define CONFIG_RESAMPLE_DBL
  54. #include "resample_template.c"
  55. #undef CONFIG_RESAMPLE_DBL
  56. /* float template */
  57. #define CONFIG_RESAMPLE_FLT
  58. #include "resample_template.c"
  59. #undef CONFIG_RESAMPLE_FLT
  60. /* s32 template */
  61. #define CONFIG_RESAMPLE_S32
  62. #include "resample_template.c"
  63. #undef CONFIG_RESAMPLE_S32
  64. /* s16 template */
  65. #include "resample_template.c"
  66. /* 0th order modified bessel function of the first kind. */
  67. static double bessel(double x)
  68. {
  69. double v = 1;
  70. double lastv = 0;
  71. double t = 1;
  72. int i;
  73. x = x * x / 4;
  74. for (i = 1; v != lastv; i++) {
  75. lastv = v;
  76. t *= x / (i * i);
  77. v += t;
  78. }
  79. return v;
  80. }
  81. /* Build a polyphase filterbank. */
  82. static int build_filter(ResampleContext *c)
  83. {
  84. int ph, i;
  85. double x, y, w, factor;
  86. double *tab;
  87. int tap_count = c->filter_length;
  88. int phase_count = 1 << c->phase_shift;
  89. const int center = (tap_count - 1) / 2;
  90. tab = av_malloc(tap_count * sizeof(*tab));
  91. if (!tab)
  92. return AVERROR(ENOMEM);
  93. /* if upsampling, only need to interpolate, no filter */
  94. factor = FFMIN(c->factor, 1.0);
  95. for (ph = 0; ph < phase_count; ph++) {
  96. double norm = 0;
  97. for (i = 0; i < tap_count; i++) {
  98. x = M_PI * ((double)(i - center) - (double)ph / phase_count) * factor;
  99. if (x == 0) y = 1.0;
  100. else y = sin(x) / x;
  101. switch (c->filter_type) {
  102. case AV_RESAMPLE_FILTER_TYPE_CUBIC: {
  103. const float d = -0.5; //first order derivative = -0.5
  104. x = fabs(((double)(i - center) - (double)ph / phase_count) * factor);
  105. if (x < 1.0) y = 1 - 3 * x*x + 2 * x*x*x + d * ( -x*x + x*x*x);
  106. else y = d * (-4 + 8 * x - 5 * x*x + x*x*x);
  107. break;
  108. }
  109. case AV_RESAMPLE_FILTER_TYPE_BLACKMAN_NUTTALL:
  110. w = 2.0 * x / (factor * tap_count) + M_PI;
  111. y *= 0.3635819 - 0.4891775 * cos( w) +
  112. 0.1365995 * cos(2 * w) -
  113. 0.0106411 * cos(3 * w);
  114. break;
  115. case AV_RESAMPLE_FILTER_TYPE_KAISER:
  116. w = 2.0 * x / (factor * tap_count * M_PI);
  117. y *= bessel(c->kaiser_beta * sqrt(FFMAX(1 - w * w, 0)));
  118. break;
  119. }
  120. tab[i] = y;
  121. norm += y;
  122. }
  123. /* normalize so that an uniform color remains the same */
  124. for (i = 0; i < tap_count; i++)
  125. tab[i] = tab[i] / norm;
  126. c->set_filter(c->filter_bank, tab, ph, tap_count);
  127. }
  128. av_free(tab);
  129. return 0;
  130. }
  131. ResampleContext *ff_audio_resample_init(AVAudioResampleContext *avr)
  132. {
  133. ResampleContext *c;
  134. int out_rate = avr->out_sample_rate;
  135. int in_rate = avr->in_sample_rate;
  136. double factor = FFMIN(out_rate * avr->cutoff / in_rate, 1.0);
  137. int phase_count = 1 << avr->phase_shift;
  138. int felem_size;
  139. if (avr->internal_sample_fmt != AV_SAMPLE_FMT_S16P &&
  140. avr->internal_sample_fmt != AV_SAMPLE_FMT_S32P &&
  141. avr->internal_sample_fmt != AV_SAMPLE_FMT_FLTP &&
  142. avr->internal_sample_fmt != AV_SAMPLE_FMT_DBLP) {
  143. av_log(avr, AV_LOG_ERROR, "Unsupported internal format for "
  144. "resampling: %s\n",
  145. av_get_sample_fmt_name(avr->internal_sample_fmt));
  146. return NULL;
  147. }
  148. c = av_mallocz(sizeof(*c));
  149. if (!c)
  150. return NULL;
  151. c->avr = avr;
  152. c->phase_shift = avr->phase_shift;
  153. c->phase_mask = phase_count - 1;
  154. c->linear = avr->linear_interp;
  155. c->factor = factor;
  156. c->filter_length = FFMAX((int)ceil(avr->filter_size / factor), 1);
  157. c->filter_type = avr->filter_type;
  158. c->kaiser_beta = avr->kaiser_beta;
  159. switch (avr->internal_sample_fmt) {
  160. case AV_SAMPLE_FMT_DBLP:
  161. c->resample_one = resample_one_dbl;
  162. c->resample_nearest = resample_nearest_dbl;
  163. c->set_filter = set_filter_dbl;
  164. break;
  165. case AV_SAMPLE_FMT_FLTP:
  166. c->resample_one = resample_one_flt;
  167. c->resample_nearest = resample_nearest_flt;
  168. c->set_filter = set_filter_flt;
  169. break;
  170. case AV_SAMPLE_FMT_S32P:
  171. c->resample_one = resample_one_s32;
  172. c->resample_nearest = resample_nearest_s32;
  173. c->set_filter = set_filter_s32;
  174. break;
  175. case AV_SAMPLE_FMT_S16P:
  176. c->resample_one = resample_one_s16;
  177. c->resample_nearest = resample_nearest_s16;
  178. c->set_filter = set_filter_s16;
  179. break;
  180. }
  181. felem_size = av_get_bytes_per_sample(avr->internal_sample_fmt);
  182. c->filter_bank = av_mallocz(c->filter_length * (phase_count + 1) * felem_size);
  183. if (!c->filter_bank)
  184. goto error;
  185. if (build_filter(c) < 0)
  186. goto error;
  187. memcpy(&c->filter_bank[(c->filter_length * phase_count + 1) * felem_size],
  188. c->filter_bank, (c->filter_length - 1) * felem_size);
  189. memcpy(&c->filter_bank[c->filter_length * phase_count * felem_size],
  190. &c->filter_bank[(c->filter_length - 1) * felem_size], felem_size);
  191. c->compensation_distance = 0;
  192. if (!av_reduce(&c->src_incr, &c->dst_incr, out_rate,
  193. in_rate * (int64_t)phase_count, INT32_MAX / 2))
  194. goto error;
  195. c->ideal_dst_incr = c->dst_incr;
  196. c->padding_size = (c->filter_length - 1) / 2;
  197. c->index = -phase_count * ((c->filter_length - 1) / 2);
  198. c->frac = 0;
  199. /* allocate internal buffer */
  200. c->buffer = ff_audio_data_alloc(avr->resample_channels, 0,
  201. avr->internal_sample_fmt,
  202. "resample buffer");
  203. if (!c->buffer)
  204. goto error;
  205. av_log(avr, AV_LOG_DEBUG, "resample: %s from %d Hz to %d Hz\n",
  206. av_get_sample_fmt_name(avr->internal_sample_fmt),
  207. avr->in_sample_rate, avr->out_sample_rate);
  208. return c;
  209. error:
  210. ff_audio_data_free(&c->buffer);
  211. av_free(c->filter_bank);
  212. av_free(c);
  213. return NULL;
  214. }
  215. void ff_audio_resample_free(ResampleContext **c)
  216. {
  217. if (!*c)
  218. return;
  219. ff_audio_data_free(&(*c)->buffer);
  220. av_free((*c)->filter_bank);
  221. av_freep(c);
  222. }
  223. int avresample_set_compensation(AVAudioResampleContext *avr, int sample_delta,
  224. int compensation_distance)
  225. {
  226. ResampleContext *c;
  227. AudioData *fifo_buf = NULL;
  228. int ret = 0;
  229. if (compensation_distance < 0)
  230. return AVERROR(EINVAL);
  231. if (!compensation_distance && sample_delta)
  232. return AVERROR(EINVAL);
  233. if (!avr->resample_needed) {
  234. #if FF_API_RESAMPLE_CLOSE_OPEN
  235. /* if resampling was not enabled previously, re-initialize the
  236. AVAudioResampleContext and force resampling */
  237. int fifo_samples;
  238. int restore_matrix = 0;
  239. double matrix[AVRESAMPLE_MAX_CHANNELS * AVRESAMPLE_MAX_CHANNELS] = { 0 };
  240. /* buffer any remaining samples in the output FIFO before closing */
  241. fifo_samples = av_audio_fifo_size(avr->out_fifo);
  242. if (fifo_samples > 0) {
  243. fifo_buf = ff_audio_data_alloc(avr->out_channels, fifo_samples,
  244. avr->out_sample_fmt, NULL);
  245. if (!fifo_buf)
  246. return AVERROR(EINVAL);
  247. ret = ff_audio_data_read_from_fifo(avr->out_fifo, fifo_buf,
  248. fifo_samples);
  249. if (ret < 0)
  250. goto reinit_fail;
  251. }
  252. /* save the channel mixing matrix */
  253. if (avr->am) {
  254. ret = avresample_get_matrix(avr, matrix, AVRESAMPLE_MAX_CHANNELS);
  255. if (ret < 0)
  256. goto reinit_fail;
  257. restore_matrix = 1;
  258. }
  259. /* close the AVAudioResampleContext */
  260. avresample_close(avr);
  261. avr->force_resampling = 1;
  262. /* restore the channel mixing matrix */
  263. if (restore_matrix) {
  264. ret = avresample_set_matrix(avr, matrix, AVRESAMPLE_MAX_CHANNELS);
  265. if (ret < 0)
  266. goto reinit_fail;
  267. }
  268. /* re-open the AVAudioResampleContext */
  269. ret = avresample_open(avr);
  270. if (ret < 0)
  271. goto reinit_fail;
  272. /* restore buffered samples to the output FIFO */
  273. if (fifo_samples > 0) {
  274. ret = ff_audio_data_add_to_fifo(avr->out_fifo, fifo_buf, 0,
  275. fifo_samples);
  276. if (ret < 0)
  277. goto reinit_fail;
  278. ff_audio_data_free(&fifo_buf);
  279. }
  280. #else
  281. av_log(avr, AV_LOG_ERROR, "Unable to set resampling compensation\n");
  282. return AVERROR(EINVAL);
  283. #endif
  284. }
  285. c = avr->resample;
  286. c->compensation_distance = compensation_distance;
  287. if (compensation_distance) {
  288. c->dst_incr = c->ideal_dst_incr - c->ideal_dst_incr *
  289. (int64_t)sample_delta / compensation_distance;
  290. } else {
  291. c->dst_incr = c->ideal_dst_incr;
  292. }
  293. return 0;
  294. reinit_fail:
  295. ff_audio_data_free(&fifo_buf);
  296. return ret;
  297. }
  298. static int resample(ResampleContext *c, void *dst, const void *src,
  299. int *consumed, int src_size, int dst_size, int update_ctx,
  300. int nearest_neighbour)
  301. {
  302. int dst_index;
  303. int index = c->index;
  304. int frac = c->frac;
  305. int dst_incr_frac = c->dst_incr % c->src_incr;
  306. int dst_incr = c->dst_incr / c->src_incr;
  307. int compensation_distance = c->compensation_distance;
  308. if (!dst != !src)
  309. return AVERROR(EINVAL);
  310. if (nearest_neighbour) {
  311. int64_t index2 = ((int64_t)index) << 32;
  312. int64_t incr = (1LL << 32) * c->dst_incr / c->src_incr;
  313. dst_size = FFMIN(dst_size,
  314. (src_size-1-index) * (int64_t)c->src_incr /
  315. c->dst_incr);
  316. if (dst) {
  317. for(dst_index = 0; dst_index < dst_size; dst_index++) {
  318. c->resample_nearest(dst, dst_index, src, index2 >> 32);
  319. index2 += incr;
  320. }
  321. } else {
  322. dst_index = dst_size;
  323. }
  324. index += dst_index * dst_incr;
  325. index += (frac + dst_index * (int64_t)dst_incr_frac) / c->src_incr;
  326. frac = (frac + dst_index * (int64_t)dst_incr_frac) % c->src_incr;
  327. } else {
  328. for (dst_index = 0; dst_index < dst_size; dst_index++) {
  329. int sample_index = index >> c->phase_shift;
  330. if (sample_index + c->filter_length > src_size ||
  331. -sample_index >= src_size)
  332. break;
  333. if (dst)
  334. c->resample_one(c, dst, dst_index, src, src_size, index, frac);
  335. frac += dst_incr_frac;
  336. index += dst_incr;
  337. if (frac >= c->src_incr) {
  338. frac -= c->src_incr;
  339. index++;
  340. }
  341. if (dst_index + 1 == compensation_distance) {
  342. compensation_distance = 0;
  343. dst_incr_frac = c->ideal_dst_incr % c->src_incr;
  344. dst_incr = c->ideal_dst_incr / c->src_incr;
  345. }
  346. }
  347. }
  348. if (consumed)
  349. *consumed = FFMAX(index, 0) >> c->phase_shift;
  350. if (update_ctx) {
  351. if (index >= 0)
  352. index &= c->phase_mask;
  353. if (compensation_distance) {
  354. compensation_distance -= dst_index;
  355. if (compensation_distance <= 0)
  356. return AVERROR_BUG;
  357. }
  358. c->frac = frac;
  359. c->index = index;
  360. c->dst_incr = dst_incr_frac + c->src_incr*dst_incr;
  361. c->compensation_distance = compensation_distance;
  362. }
  363. return dst_index;
  364. }
  365. int ff_audio_resample(ResampleContext *c, AudioData *dst, AudioData *src)
  366. {
  367. int ch, in_samples, in_leftover, consumed = 0, out_samples = 0;
  368. int ret = AVERROR(EINVAL);
  369. int nearest_neighbour = (c->compensation_distance == 0 &&
  370. c->filter_length == 1 &&
  371. c->phase_shift == 0);
  372. in_samples = src ? src->nb_samples : 0;
  373. in_leftover = c->buffer->nb_samples;
  374. /* add input samples to the internal buffer */
  375. if (src) {
  376. ret = ff_audio_data_combine(c->buffer, in_leftover, src, 0, in_samples);
  377. if (ret < 0)
  378. return ret;
  379. } else if (!in_leftover) {
  380. /* no remaining samples to flush */
  381. return 0;
  382. } else {
  383. /* TODO: pad buffer to flush completely */
  384. }
  385. /* calculate output size and reallocate output buffer if needed */
  386. /* TODO: try to calculate this without the dummy resample() run */
  387. if (!dst->read_only && dst->allow_realloc) {
  388. out_samples = resample(c, NULL, NULL, NULL, c->buffer->nb_samples,
  389. INT_MAX, 0, nearest_neighbour);
  390. ret = ff_audio_data_realloc(dst, out_samples);
  391. if (ret < 0) {
  392. av_log(c->avr, AV_LOG_ERROR, "error reallocating output\n");
  393. return ret;
  394. }
  395. }
  396. /* resample each channel plane */
  397. for (ch = 0; ch < c->buffer->channels; ch++) {
  398. out_samples = resample(c, (void *)dst->data[ch],
  399. (const void *)c->buffer->data[ch], &consumed,
  400. c->buffer->nb_samples, dst->allocated_samples,
  401. ch + 1 == c->buffer->channels, nearest_neighbour);
  402. }
  403. if (out_samples < 0) {
  404. av_log(c->avr, AV_LOG_ERROR, "error during resampling\n");
  405. return out_samples;
  406. }
  407. /* drain consumed samples from the internal buffer */
  408. ff_audio_data_drain(c->buffer, consumed);
  409. av_dlog(c->avr, "resampled %d in + %d leftover to %d out + %d leftover\n",
  410. in_samples, in_leftover, out_samples, c->buffer->nb_samples);
  411. dst->nb_samples = out_samples;
  412. return 0;
  413. }
  414. int avresample_get_delay(AVAudioResampleContext *avr)
  415. {
  416. ResampleContext *c = avr->resample;
  417. if (!avr->resample_needed || !avr->resample)
  418. return 0;
  419. return FFMAX(c->buffer->nb_samples - c->padding_size, 0);
  420. }