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.

499 lines
17KB

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