resample.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. const AVClass *av_class;
  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 SampleFormat 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 SampleFormat sample_fmt_out,
  131. enum SampleFormat 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_format(s->sample_fmt[0])>>3;
  156. s->sample_size[1] = av_get_bits_per_sample_format(s->sample_fmt[1])>>3;
  157. if (s->sample_fmt[0] != SAMPLE_FMT_S16) {
  158. if (!(s->convert_ctx[0] = av_audio_convert_alloc(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. avcodec_get_sample_fmt_name(s->sample_fmt[0]));
  163. av_free(s);
  164. return NULL;
  165. }
  166. }
  167. if (s->sample_fmt[1] != SAMPLE_FMT_S16) {
  168. if (!(s->convert_ctx[1] = av_audio_convert_alloc(s->sample_fmt[1], 1,
  169. 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. avcodec_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. s->av_class= &audioresample_context_class;
  189. return s;
  190. }
  191. #if LIBAVCODEC_VERSION_MAJOR < 53
  192. ReSampleContext *audio_resample_init(int output_channels, int input_channels,
  193. int output_rate, int input_rate)
  194. {
  195. return av_audio_resample_init(output_channels, input_channels,
  196. output_rate, input_rate,
  197. SAMPLE_FMT_S16, SAMPLE_FMT_S16,
  198. TAPS, 10, 0, 0.8);
  199. }
  200. #endif
  201. /* resample audio. 'nb_samples' is the number of input samples */
  202. /* XXX: optimize it ! */
  203. int audio_resample(ReSampleContext *s, short *output, short *input, int nb_samples)
  204. {
  205. int i, nb_samples1;
  206. short *bufin[2];
  207. short *bufout[2];
  208. short *buftmp2[2], *buftmp3[2];
  209. short *output_bak = NULL;
  210. int lenout;
  211. if (s->input_channels == s->output_channels && s->ratio == 1.0 && 0) {
  212. /* nothing to do */
  213. memcpy(output, input, nb_samples * s->input_channels * sizeof(short));
  214. return nb_samples;
  215. }
  216. if (s->sample_fmt[0] != SAMPLE_FMT_S16) {
  217. int istride[1] = { s->sample_size[0] };
  218. int ostride[1] = { 2 };
  219. const void *ibuf[1] = { input };
  220. void *obuf[1];
  221. unsigned input_size = nb_samples*s->input_channels*2;
  222. if (!s->buffer_size[0] || s->buffer_size[0] < input_size) {
  223. av_free(s->buffer[0]);
  224. s->buffer_size[0] = input_size;
  225. s->buffer[0] = av_malloc(s->buffer_size[0]);
  226. if (!s->buffer[0]) {
  227. av_log(s, AV_LOG_ERROR, "Could not allocate buffer\n");
  228. return 0;
  229. }
  230. }
  231. obuf[0] = s->buffer[0];
  232. if (av_audio_convert(s->convert_ctx[0], obuf, ostride,
  233. ibuf, istride, nb_samples*s->input_channels) < 0) {
  234. av_log(s, AV_LOG_ERROR, "Audio sample format conversion failed\n");
  235. return 0;
  236. }
  237. input = s->buffer[0];
  238. }
  239. lenout= 4*nb_samples * s->ratio + 16;
  240. if (s->sample_fmt[1] != SAMPLE_FMT_S16) {
  241. output_bak = output;
  242. if (!s->buffer_size[1] || s->buffer_size[1] < 2*lenout) {
  243. av_free(s->buffer[1]);
  244. s->buffer_size[1] = 2*lenout;
  245. s->buffer[1] = av_malloc(s->buffer_size[1]);
  246. if (!s->buffer[1]) {
  247. av_log(s, AV_LOG_ERROR, "Could not allocate buffer\n");
  248. return 0;
  249. }
  250. }
  251. output = s->buffer[1];
  252. }
  253. /* XXX: move those malloc to resample init code */
  254. for(i=0; i<s->filter_channels; i++){
  255. bufin[i]= av_malloc( (nb_samples + s->temp_len) * sizeof(short) );
  256. memcpy(bufin[i], s->temp[i], s->temp_len * sizeof(short));
  257. buftmp2[i] = bufin[i] + s->temp_len;
  258. }
  259. /* make some zoom to avoid round pb */
  260. bufout[0]= av_malloc( lenout * sizeof(short) );
  261. bufout[1]= av_malloc( lenout * sizeof(short) );
  262. if (s->input_channels == 2 &&
  263. s->output_channels == 1) {
  264. buftmp3[0] = output;
  265. stereo_to_mono(buftmp2[0], input, nb_samples);
  266. } else if (s->output_channels >= 2 && s->input_channels == 1) {
  267. buftmp3[0] = bufout[0];
  268. memcpy(buftmp2[0], input, nb_samples*sizeof(short));
  269. } else if (s->output_channels >= 2) {
  270. buftmp3[0] = bufout[0];
  271. buftmp3[1] = bufout[1];
  272. stereo_split(buftmp2[0], buftmp2[1], input, nb_samples);
  273. } else {
  274. buftmp3[0] = output;
  275. memcpy(buftmp2[0], input, nb_samples*sizeof(short));
  276. }
  277. nb_samples += s->temp_len;
  278. /* resample each channel */
  279. nb_samples1 = 0; /* avoid warning */
  280. for(i=0;i<s->filter_channels;i++) {
  281. int consumed;
  282. int is_last= i+1 == s->filter_channels;
  283. nb_samples1 = av_resample(s->resample_context, buftmp3[i], bufin[i], &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 == 2) {
  291. stereo_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
  292. } else if (s->output_channels == 6) {
  293. ac3_5p1_mux(output, buftmp3[0], buftmp3[1], nb_samples1);
  294. }
  295. if (s->sample_fmt[1] != 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, AV_LOG_ERROR, "Audio sample format convertion failed\n");
  303. return 0;
  304. }
  305. }
  306. for(i=0; i<s->filter_channels; i++)
  307. av_free(bufin[i]);
  308. av_free(bufout[0]);
  309. av_free(bufout[1]);
  310. return nb_samples1;
  311. }
  312. void audio_resample_close(ReSampleContext *s)
  313. {
  314. av_resample_close(s->resample_context);
  315. av_freep(&s->temp[0]);
  316. av_freep(&s->temp[1]);
  317. av_freep(&s->buffer[0]);
  318. av_freep(&s->buffer[1]);
  319. av_audio_convert_free(s->convert_ctx[0]);
  320. av_audio_convert_free(s->convert_ctx[1]);
  321. av_free(s);
  322. }