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.

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