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.

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