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.

375 lines
12KB

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