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.

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