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.

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