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.

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