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.

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