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.

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