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.

365 lines
11KB

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