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.

251 lines
6.9KB

  1. /*
  2. * samplerate conversion for both audio and video
  3. * Copyright (c) 2000 Fabrice Bellard
  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. /**
  22. * @file resample.c
  23. * samplerate conversion for both audio and video
  24. */
  25. #include "avcodec.h"
  26. struct AVResampleContext;
  27. struct ReSampleContext {
  28. struct AVResampleContext *resample_context;
  29. short *temp[2];
  30. int temp_len;
  31. float ratio;
  32. /* channel convert */
  33. int input_channels, output_channels, filter_channels;
  34. };
  35. /* n1: number of samples */
  36. static void stereo_to_mono(short *output, short *input, int n1)
  37. {
  38. short *p, *q;
  39. int n = n1;
  40. p = input;
  41. q = output;
  42. while (n >= 4) {
  43. q[0] = (p[0] + p[1]) >> 1;
  44. q[1] = (p[2] + p[3]) >> 1;
  45. q[2] = (p[4] + p[5]) >> 1;
  46. q[3] = (p[6] + p[7]) >> 1;
  47. q += 4;
  48. p += 8;
  49. n -= 4;
  50. }
  51. while (n > 0) {
  52. q[0] = (p[0] + p[1]) >> 1;
  53. q++;
  54. p += 2;
  55. n--;
  56. }
  57. }
  58. /* n1: number of samples */
  59. static void mono_to_stereo(short *output, short *input, int n1)
  60. {
  61. short *p, *q;
  62. int n = n1;
  63. int v;
  64. p = input;
  65. q = output;
  66. while (n >= 4) {
  67. v = p[0]; q[0] = v; q[1] = v;
  68. v = p[1]; q[2] = v; q[3] = v;
  69. v = p[2]; q[4] = v; q[5] = v;
  70. v = p[3]; q[6] = v; q[7] = v;
  71. q += 8;
  72. p += 4;
  73. n -= 4;
  74. }
  75. while (n > 0) {
  76. v = p[0]; q[0] = v; q[1] = v;
  77. q += 2;
  78. p += 1;
  79. n--;
  80. }
  81. }
  82. /* XXX: should use more abstract 'N' channels system */
  83. static void stereo_split(short *output1, short *output2, short *input, int n)
  84. {
  85. int i;
  86. for(i=0;i<n;i++) {
  87. *output1++ = *input++;
  88. *output2++ = *input++;
  89. }
  90. }
  91. static void stereo_mux(short *output, short *input1, short *input2, int n)
  92. {
  93. int i;
  94. for(i=0;i<n;i++) {
  95. *output++ = *input1++;
  96. *output++ = *input2++;
  97. }
  98. }
  99. static void ac3_5p1_mux(short *output, short *input1, short *input2, int n)
  100. {
  101. int i;
  102. short l,r;
  103. for(i=0;i<n;i++) {
  104. l=*input1++;
  105. r=*input2++;
  106. *output++ = l; /* left */
  107. *output++ = (l/2)+(r/2); /* center */
  108. *output++ = r; /* right */
  109. *output++ = 0; /* left surround */
  110. *output++ = 0; /* right surroud */
  111. *output++ = 0; /* low freq */
  112. }
  113. }
  114. ReSampleContext *audio_resample_init(int output_channels, int input_channels,
  115. int output_rate, int input_rate)
  116. {
  117. ReSampleContext *s;
  118. if ( input_channels > 2)
  119. {
  120. av_log(NULL, AV_LOG_ERROR, "Resampling with input channels greater than 2 unsupported.\n");
  121. return NULL;
  122. }
  123. s = av_mallocz(sizeof(ReSampleContext));
  124. if (!s)
  125. {
  126. av_log(NULL, AV_LOG_ERROR, "Can't allocate memory for resample context.\n");
  127. return NULL;
  128. }
  129. s->ratio = (float)output_rate / (float)input_rate;
  130. s->input_channels = input_channels;
  131. s->output_channels = output_channels;
  132. s->filter_channels = s->input_channels;
  133. if (s->output_channels < s->filter_channels)
  134. s->filter_channels = s->output_channels;
  135. /*
  136. * AC-3 output is the only case where filter_channels could be greater than 2.
  137. * input channels can't be greater than 2, so resample the 2 channels and then
  138. * expand to 6 channels after the resampling.
  139. */
  140. if(s->filter_channels>2)
  141. s->filter_channels = 2;
  142. #define TAPS 16
  143. s->resample_context= av_resample_init(output_rate, input_rate, TAPS, 10, 0, 0.8);
  144. return s;
  145. }
  146. /* resample audio. 'nb_samples' is the number of input samples */
  147. /* XXX: optimize it ! */
  148. int audio_resample(ReSampleContext *s, short *output, short *input, int nb_samples)
  149. {
  150. int i, nb_samples1;
  151. short *bufin[2];
  152. short *bufout[2];
  153. short *buftmp2[2], *buftmp3[2];
  154. int lenout;
  155. if (s->input_channels == s->output_channels && s->ratio == 1.0 && 0) {
  156. /* nothing to do */
  157. memcpy(output, input, nb_samples * s->input_channels * sizeof(short));
  158. return nb_samples;
  159. }
  160. /* XXX: move those malloc to resample init code */
  161. for(i=0; i<s->filter_channels; i++){
  162. bufin[i]= av_malloc( (nb_samples + s->temp_len) * sizeof(short) );
  163. memcpy(bufin[i], s->temp[i], s->temp_len * sizeof(short));
  164. buftmp2[i] = bufin[i] + s->temp_len;
  165. }
  166. /* make some zoom to avoid round pb */
  167. lenout= 4*nb_samples * s->ratio + 16;
  168. bufout[0]= av_malloc( lenout * sizeof(short) );
  169. bufout[1]= av_malloc( lenout * sizeof(short) );
  170. if (s->input_channels == 2 &&
  171. s->output_channels == 1) {
  172. buftmp3[0] = output;
  173. stereo_to_mono(buftmp2[0], input, nb_samples);
  174. } else if (s->output_channels >= 2 && s->input_channels == 1) {
  175. buftmp3[0] = bufout[0];
  176. memcpy(buftmp2[0], input, nb_samples*sizeof(short));
  177. } else if (s->output_channels >= 2) {
  178. buftmp3[0] = bufout[0];
  179. buftmp3[1] = bufout[1];
  180. stereo_split(buftmp2[0], buftmp2[1], input, nb_samples);
  181. } else {
  182. buftmp3[0] = output;
  183. memcpy(buftmp2[0], input, nb_samples*sizeof(short));
  184. }
  185. nb_samples += s->temp_len;
  186. /* resample each channel */
  187. nb_samples1 = 0; /* avoid warning */
  188. for(i=0;i<s->filter_channels;i++) {
  189. int consumed;
  190. int is_last= i+1 == s->filter_channels;
  191. nb_samples1 = av_resample(s->resample_context, buftmp3[i], bufin[i], &consumed, nb_samples, lenout, is_last);
  192. s->temp_len= nb_samples - consumed;
  193. s->temp[i]= av_realloc(s->temp[i], s->temp_len*sizeof(short));
  194. memcpy(s->temp[i], bufin[i] + consumed, s->temp_len*sizeof(short));
  195. }
  196. if (s->output_channels == 2 && s->input_channels == 1) {
  197. mono_to_stereo(output, buftmp3[0], nb_samples1);
  198. } else if (s->output_channels == 2) {
  199. stereo_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
  200. } else if (s->output_channels == 6) {
  201. ac3_5p1_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
  202. }
  203. for(i=0; i<s->filter_channels; i++)
  204. av_free(bufin[i]);
  205. av_free(bufout[0]);
  206. av_free(bufout[1]);
  207. return nb_samples1;
  208. }
  209. void audio_resample_close(ReSampleContext *s)
  210. {
  211. av_resample_close(s->resample_context);
  212. av_freep(&s->temp[0]);
  213. av_freep(&s->temp[1]);
  214. av_free(s);
  215. }