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.

440 lines
14KB

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