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.

376 lines
12KB

  1. /*
  2. * samplerate conversion for both audio and video
  3. * Copyright (c) 2000 Fabrice Bellard
  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. /**
  22. * @file
  23. * samplerate conversion for both audio and video
  24. */
  25. #include <string.h>
  26. #include "avcodec.h"
  27. #include "audioconvert.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/mem.h"
  30. #include "libavutil/samplefmt.h"
  31. #define MAX_CHANNELS 8
  32. struct AVResampleContext;
  33. static const char *context_to_name(void *ptr)
  34. {
  35. return "audioresample";
  36. }
  37. static const AVOption options[] = {{NULL}};
  38. static const AVClass audioresample_context_class = {
  39. "ReSampleContext", context_to_name, options, LIBAVUTIL_VERSION_INT
  40. };
  41. struct ReSampleContext {
  42. struct AVResampleContext *resample_context;
  43. short *temp[MAX_CHANNELS];
  44. int temp_len;
  45. float ratio;
  46. /* channel convert */
  47. int input_channels, output_channels, filter_channels;
  48. AVAudioConvert *convert_ctx[2];
  49. enum AVSampleFormat sample_fmt[2]; ///< input and output sample format
  50. unsigned sample_size[2]; ///< size of one sample in sample_fmt
  51. short *buffer[2]; ///< buffers used for conversion to S16
  52. unsigned buffer_size[2]; ///< sizes of allocated buffers
  53. };
  54. /* n1: number of samples */
  55. static void stereo_to_mono(short *output, short *input, int n1)
  56. {
  57. short *p, *q;
  58. int n = n1;
  59. p = input;
  60. q = output;
  61. while (n >= 4) {
  62. q[0] = (p[0] + p[1]) >> 1;
  63. q[1] = (p[2] + p[3]) >> 1;
  64. q[2] = (p[4] + p[5]) >> 1;
  65. q[3] = (p[6] + p[7]) >> 1;
  66. q += 4;
  67. p += 8;
  68. n -= 4;
  69. }
  70. while (n > 0) {
  71. q[0] = (p[0] + p[1]) >> 1;
  72. q++;
  73. p += 2;
  74. n--;
  75. }
  76. }
  77. /* n1: number of samples */
  78. static void mono_to_stereo(short *output, short *input, int n1)
  79. {
  80. short *p, *q;
  81. int n = n1;
  82. int v;
  83. p = input;
  84. q = output;
  85. while (n >= 4) {
  86. v = p[0]; q[0] = v; q[1] = v;
  87. v = p[1]; q[2] = v; q[3] = v;
  88. v = p[2]; q[4] = v; q[5] = v;
  89. v = p[3]; q[6] = v; q[7] = v;
  90. q += 8;
  91. p += 4;
  92. n -= 4;
  93. }
  94. while (n > 0) {
  95. v = p[0]; q[0] = v; q[1] = v;
  96. q += 2;
  97. p += 1;
  98. n--;
  99. }
  100. }
  101. static void deinterleave(short **output, short *input, int channels, int samples)
  102. {
  103. int i, j;
  104. for (i = 0; i < samples; i++) {
  105. for (j = 0; j < channels; j++) {
  106. *output[j]++ = *input++;
  107. }
  108. }
  109. }
  110. static void interleave(short *output, short **input, int channels, int samples)
  111. {
  112. int i, j;
  113. for (i = 0; i < samples; i++) {
  114. for (j = 0; j < channels; j++) {
  115. *output++ = *input[j]++;
  116. }
  117. }
  118. }
  119. static void ac3_5p1_mux(short *output, short *input1, short *input2, int n)
  120. {
  121. int i;
  122. short l, r;
  123. for (i = 0; i < n; i++) {
  124. l = *input1++;
  125. r = *input2++;
  126. *output++ = l; /* left */
  127. *output++ = (l / 2) + (r / 2); /* center */
  128. *output++ = r; /* right */
  129. *output++ = 0; /* left surround */
  130. *output++ = 0; /* right surroud */
  131. *output++ = 0; /* low freq */
  132. }
  133. }
  134. ReSampleContext *av_audio_resample_init(int output_channels, int input_channels,
  135. int output_rate, int input_rate,
  136. enum AVSampleFormat sample_fmt_out,
  137. enum AVSampleFormat sample_fmt_in,
  138. int filter_length, int log2_phase_count,
  139. int linear, double cutoff)
  140. {
  141. ReSampleContext *s;
  142. if (input_channels > MAX_CHANNELS) {
  143. av_log(NULL, AV_LOG_ERROR,
  144. "Resampling with input channels greater than %d is unsupported.\n",
  145. MAX_CHANNELS);
  146. return NULL;
  147. }
  148. if (output_channels != input_channels &&
  149. (input_channels > 2 ||
  150. output_channels > 2 &&
  151. !(output_channels == 6 && input_channels == 2))) {
  152. av_log(NULL, AV_LOG_ERROR,
  153. "Resampling output channel count must be 1 or 2 for mono input; 1, 2 or 6 for stereo input; or N for N channel input.\n");
  154. return NULL;
  155. }
  156. s = av_mallocz(sizeof(ReSampleContext));
  157. if (!s) {
  158. av_log(NULL, AV_LOG_ERROR, "Can't allocate memory for resample context.\n");
  159. return NULL;
  160. }
  161. s->ratio = (float)output_rate / (float)input_rate;
  162. s->input_channels = input_channels;
  163. s->output_channels = output_channels;
  164. s->filter_channels = s->input_channels;
  165. if (s->output_channels < s->filter_channels)
  166. s->filter_channels = s->output_channels;
  167. s->sample_fmt[0] = sample_fmt_in;
  168. s->sample_fmt[1] = sample_fmt_out;
  169. s->sample_size[0] = av_get_bytes_per_sample(s->sample_fmt[0]);
  170. s->sample_size[1] = av_get_bytes_per_sample(s->sample_fmt[1]);
  171. if (s->sample_fmt[0] != AV_SAMPLE_FMT_S16) {
  172. if (!(s->convert_ctx[0] = av_audio_convert_alloc(AV_SAMPLE_FMT_S16, 1,
  173. s->sample_fmt[0], 1, NULL, 0))) {
  174. av_log(s, AV_LOG_ERROR,
  175. "Cannot convert %s sample format to s16 sample format\n",
  176. av_get_sample_fmt_name(s->sample_fmt[0]));
  177. av_free(s);
  178. return NULL;
  179. }
  180. }
  181. if (s->sample_fmt[1] != AV_SAMPLE_FMT_S16) {
  182. if (!(s->convert_ctx[1] = av_audio_convert_alloc(s->sample_fmt[1], 1,
  183. AV_SAMPLE_FMT_S16, 1, NULL, 0))) {
  184. av_log(s, AV_LOG_ERROR,
  185. "Cannot convert s16 sample format to %s sample format\n",
  186. av_get_sample_fmt_name(s->sample_fmt[1]));
  187. av_audio_convert_free(s->convert_ctx[0]);
  188. av_free(s);
  189. return NULL;
  190. }
  191. }
  192. s->resample_context = av_resample_init(output_rate, input_rate,
  193. filter_length, log2_phase_count,
  194. linear, cutoff);
  195. *(const AVClass**)s->resample_context = &audioresample_context_class;
  196. return s;
  197. }
  198. /* resample audio. 'nb_samples' is the number of input samples */
  199. /* XXX: optimize it ! */
  200. int audio_resample(ReSampleContext *s, short *output, short *input, int nb_samples)
  201. {
  202. int i, nb_samples1;
  203. short *bufin[MAX_CHANNELS];
  204. short *bufout[MAX_CHANNELS];
  205. short *buftmp2[MAX_CHANNELS], *buftmp3[MAX_CHANNELS];
  206. short *output_bak = NULL;
  207. int lenout;
  208. if (s->input_channels == s->output_channels && s->ratio == 1.0 && 0) {
  209. /* nothing to do */
  210. memcpy(output, input, nb_samples * s->input_channels * sizeof(short));
  211. return nb_samples;
  212. }
  213. if (s->sample_fmt[0] != AV_SAMPLE_FMT_S16) {
  214. int istride[1] = { s->sample_size[0] };
  215. int ostride[1] = { 2 };
  216. const void *ibuf[1] = { input };
  217. void *obuf[1];
  218. unsigned input_size = nb_samples * s->input_channels * 2;
  219. if (!s->buffer_size[0] || s->buffer_size[0] < input_size) {
  220. av_free(s->buffer[0]);
  221. s->buffer_size[0] = input_size;
  222. s->buffer[0] = av_malloc(s->buffer_size[0]);
  223. if (!s->buffer[0]) {
  224. av_log(s->resample_context, AV_LOG_ERROR, "Could not allocate buffer\n");
  225. return 0;
  226. }
  227. }
  228. obuf[0] = s->buffer[0];
  229. if (av_audio_convert(s->convert_ctx[0], obuf, ostride,
  230. ibuf, istride, nb_samples * s->input_channels) < 0) {
  231. av_log(s->resample_context, AV_LOG_ERROR,
  232. "Audio sample format conversion failed\n");
  233. return 0;
  234. }
  235. input = s->buffer[0];
  236. }
  237. lenout = 4 * nb_samples * s->ratio + 16;
  238. if (s->sample_fmt[1] != AV_SAMPLE_FMT_S16) {
  239. int out_size = lenout * av_get_bytes_per_sample(s->sample_fmt[1]) *
  240. s->output_channels;
  241. output_bak = output;
  242. if (!s->buffer_size[1] || s->buffer_size[1] < out_size) {
  243. av_free(s->buffer[1]);
  244. s->buffer_size[1] = out_size;
  245. s->buffer[1] = av_malloc(s->buffer_size[1]);
  246. if (!s->buffer[1]) {
  247. av_log(s->resample_context, AV_LOG_ERROR, "Could not allocate buffer\n");
  248. return 0;
  249. }
  250. }
  251. output = s->buffer[1];
  252. }
  253. /* XXX: move those malloc to resample init code */
  254. for (i = 0; i < s->filter_channels; i++) {
  255. bufin[i] = av_malloc((nb_samples + s->temp_len) * sizeof(short));
  256. memcpy(bufin[i], s->temp[i], s->temp_len * sizeof(short));
  257. buftmp2[i] = bufin[i] + s->temp_len;
  258. bufout[i] = av_malloc(lenout * sizeof(short));
  259. }
  260. if (s->input_channels == 2 && s->output_channels == 1) {
  261. buftmp3[0] = output;
  262. stereo_to_mono(buftmp2[0], input, nb_samples);
  263. } else if (s->output_channels >= 2 && s->input_channels == 1) {
  264. buftmp3[0] = bufout[0];
  265. memcpy(buftmp2[0], input, nb_samples * sizeof(short));
  266. } else if (s->output_channels >= s->input_channels && s->input_channels >= 2) {
  267. for (i = 0; i < s->input_channels; i++) {
  268. buftmp3[i] = bufout[i];
  269. }
  270. deinterleave(buftmp2, input, s->input_channels, nb_samples);
  271. } else {
  272. buftmp3[0] = output;
  273. memcpy(buftmp2[0], input, nb_samples * sizeof(short));
  274. }
  275. nb_samples += s->temp_len;
  276. /* resample each channel */
  277. nb_samples1 = 0; /* avoid warning */
  278. for (i = 0; i < s->filter_channels; i++) {
  279. int consumed;
  280. int is_last = i + 1 == s->filter_channels;
  281. nb_samples1 = av_resample(s->resample_context, buftmp3[i], bufin[i],
  282. &consumed, nb_samples, lenout, is_last);
  283. s->temp_len = nb_samples - consumed;
  284. s->temp[i] = av_realloc(s->temp[i], s->temp_len * sizeof(short));
  285. memcpy(s->temp[i], bufin[i] + consumed, s->temp_len * sizeof(short));
  286. }
  287. if (s->output_channels == 2 && s->input_channels == 1) {
  288. mono_to_stereo(output, buftmp3[0], nb_samples1);
  289. } else if (s->output_channels == 6 && s->input_channels == 2) {
  290. ac3_5p1_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
  291. } else if (s->output_channels == s->input_channels && s->input_channels >= 2) {
  292. interleave(output, buftmp3, s->output_channels, nb_samples1);
  293. }
  294. if (s->sample_fmt[1] != AV_SAMPLE_FMT_S16) {
  295. int istride[1] = { 2 };
  296. int ostride[1] = { s->sample_size[1] };
  297. const void *ibuf[1] = { output };
  298. void *obuf[1] = { output_bak };
  299. if (av_audio_convert(s->convert_ctx[1], obuf, ostride,
  300. ibuf, istride, nb_samples1 * s->output_channels) < 0) {
  301. av_log(s->resample_context, AV_LOG_ERROR,
  302. "Audio sample format convertion failed\n");
  303. return 0;
  304. }
  305. }
  306. for (i = 0; i < s->filter_channels; i++) {
  307. av_free(bufin[i]);
  308. av_free(bufout[i]);
  309. }
  310. return nb_samples1;
  311. }
  312. void audio_resample_close(ReSampleContext *s)
  313. {
  314. int i;
  315. av_resample_close(s->resample_context);
  316. for (i = 0; i < s->filter_channels; i++)
  317. av_freep(&s->temp[i]);
  318. av_freep(&s->buffer[0]);
  319. av_freep(&s->buffer[1]);
  320. av_audio_convert_free(s->convert_ctx[0]);
  321. av_audio_convert_free(s->convert_ctx[1]);
  322. av_free(s);
  323. }