audio_convert.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
  3. * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <stdint.h>
  22. #include "config.h"
  23. #include "libavutil/libm.h"
  24. #include "libavutil/log.h"
  25. #include "libavutil/mem.h"
  26. #include "libavutil/samplefmt.h"
  27. #include "audio_convert.h"
  28. #include "audio_data.h"
  29. enum ConvFuncType {
  30. CONV_FUNC_TYPE_FLAT,
  31. CONV_FUNC_TYPE_INTERLEAVE,
  32. CONV_FUNC_TYPE_DEINTERLEAVE,
  33. };
  34. typedef void (conv_func_flat)(uint8_t *out, const uint8_t *in, int len);
  35. typedef void (conv_func_interleave)(uint8_t *out, uint8_t *const *in,
  36. int len, int channels);
  37. typedef void (conv_func_deinterleave)(uint8_t **out, const uint8_t *in, int len,
  38. int channels);
  39. struct AudioConvert {
  40. AVAudioResampleContext *avr;
  41. enum AVSampleFormat in_fmt;
  42. enum AVSampleFormat out_fmt;
  43. int channels;
  44. int planes;
  45. int ptr_align;
  46. int samples_align;
  47. int has_optimized_func;
  48. const char *func_descr;
  49. const char *func_descr_generic;
  50. enum ConvFuncType func_type;
  51. conv_func_flat *conv_flat;
  52. conv_func_flat *conv_flat_generic;
  53. conv_func_interleave *conv_interleave;
  54. conv_func_interleave *conv_interleave_generic;
  55. conv_func_deinterleave *conv_deinterleave;
  56. conv_func_deinterleave *conv_deinterleave_generic;
  57. };
  58. void ff_audio_convert_set_func(AudioConvert *ac, enum AVSampleFormat out_fmt,
  59. enum AVSampleFormat in_fmt, int channels,
  60. int ptr_align, int samples_align,
  61. const char *descr, void *conv)
  62. {
  63. int found = 0;
  64. switch (ac->func_type) {
  65. case CONV_FUNC_TYPE_FLAT:
  66. if (av_get_packed_sample_fmt(ac->in_fmt) == in_fmt &&
  67. av_get_packed_sample_fmt(ac->out_fmt) == out_fmt) {
  68. ac->conv_flat = conv;
  69. ac->func_descr = descr;
  70. ac->ptr_align = ptr_align;
  71. ac->samples_align = samples_align;
  72. if (ptr_align == 1 && samples_align == 1) {
  73. ac->conv_flat_generic = conv;
  74. ac->func_descr_generic = descr;
  75. } else {
  76. ac->has_optimized_func = 1;
  77. }
  78. found = 1;
  79. }
  80. break;
  81. case CONV_FUNC_TYPE_INTERLEAVE:
  82. if (ac->in_fmt == in_fmt && ac->out_fmt == out_fmt &&
  83. (!channels || ac->channels == channels)) {
  84. ac->conv_interleave = conv;
  85. ac->func_descr = descr;
  86. ac->ptr_align = ptr_align;
  87. ac->samples_align = samples_align;
  88. if (ptr_align == 1 && samples_align == 1) {
  89. ac->conv_interleave_generic = conv;
  90. ac->func_descr_generic = descr;
  91. } else {
  92. ac->has_optimized_func = 1;
  93. }
  94. found = 1;
  95. }
  96. break;
  97. case CONV_FUNC_TYPE_DEINTERLEAVE:
  98. if (ac->in_fmt == in_fmt && ac->out_fmt == out_fmt &&
  99. (!channels || ac->channels == channels)) {
  100. ac->conv_deinterleave = conv;
  101. ac->func_descr = descr;
  102. ac->ptr_align = ptr_align;
  103. ac->samples_align = samples_align;
  104. if (ptr_align == 1 && samples_align == 1) {
  105. ac->conv_deinterleave_generic = conv;
  106. ac->func_descr_generic = descr;
  107. } else {
  108. ac->has_optimized_func = 1;
  109. }
  110. found = 1;
  111. }
  112. break;
  113. }
  114. if (found) {
  115. av_log(ac->avr, AV_LOG_DEBUG, "audio_convert: found function: %-4s "
  116. "to %-4s (%s)\n", av_get_sample_fmt_name(ac->in_fmt),
  117. av_get_sample_fmt_name(ac->out_fmt), descr);
  118. }
  119. }
  120. #define CONV_FUNC_NAME(dst_fmt, src_fmt) conv_ ## src_fmt ## _to_ ## dst_fmt
  121. #define CONV_LOOP(otype, expr) \
  122. do { \
  123. *(otype *)po = expr; \
  124. pi += is; \
  125. po += os; \
  126. } while (po < end); \
  127. #define CONV_FUNC_FLAT(ofmt, otype, ifmt, itype, expr) \
  128. static void CONV_FUNC_NAME(ofmt, ifmt)(uint8_t *out, const uint8_t *in, \
  129. int len) \
  130. { \
  131. int is = sizeof(itype); \
  132. int os = sizeof(otype); \
  133. const uint8_t *pi = in; \
  134. uint8_t *po = out; \
  135. uint8_t *end = out + os * len; \
  136. CONV_LOOP(otype, expr) \
  137. }
  138. #define CONV_FUNC_INTERLEAVE(ofmt, otype, ifmt, itype, expr) \
  139. static void CONV_FUNC_NAME(ofmt, ifmt)(uint8_t *out, const uint8_t **in, \
  140. int len, int channels) \
  141. { \
  142. int ch; \
  143. int out_bps = sizeof(otype); \
  144. int is = sizeof(itype); \
  145. int os = channels * out_bps; \
  146. for (ch = 0; ch < channels; ch++) { \
  147. const uint8_t *pi = in[ch]; \
  148. uint8_t *po = out + ch * out_bps; \
  149. uint8_t *end = po + os * len; \
  150. CONV_LOOP(otype, expr) \
  151. } \
  152. }
  153. #define CONV_FUNC_DEINTERLEAVE(ofmt, otype, ifmt, itype, expr) \
  154. static void CONV_FUNC_NAME(ofmt, ifmt)(uint8_t **out, const uint8_t *in, \
  155. int len, int channels) \
  156. { \
  157. int ch; \
  158. int in_bps = sizeof(itype); \
  159. int is = channels * in_bps; \
  160. int os = sizeof(otype); \
  161. for (ch = 0; ch < channels; ch++) { \
  162. const uint8_t *pi = in + ch * in_bps; \
  163. uint8_t *po = out[ch]; \
  164. uint8_t *end = po + os * len; \
  165. CONV_LOOP(otype, expr) \
  166. } \
  167. }
  168. #define CONV_FUNC_GROUP(ofmt, otype, ifmt, itype, expr) \
  169. CONV_FUNC_FLAT( ofmt, otype, ifmt, itype, expr) \
  170. CONV_FUNC_INTERLEAVE( ofmt, otype, ifmt ## P, itype, expr) \
  171. CONV_FUNC_DEINTERLEAVE(ofmt ## P, otype, ifmt, itype, expr)
  172. CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_U8, uint8_t, *(const uint8_t *)pi)
  173. CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_U8, uint8_t, (*(const uint8_t *)pi - 0x80) << 8)
  174. CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_U8, uint8_t, (*(const uint8_t *)pi - 0x80) << 24)
  175. CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t, (*(const uint8_t *)pi - 0x80) * (1.0f / (1 << 7)))
  176. CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t, (*(const uint8_t *)pi - 0x80) * (1.0 / (1 << 7)))
  177. CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t, (*(const int16_t *)pi >> 8) + 0x80)
  178. CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *)pi)
  179. CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *)pi << 16)
  180. CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *)pi * (1.0f / (1 << 15)))
  181. CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *)pi * (1.0 / (1 << 15)))
  182. CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t, (*(const int32_t *)pi >> 24) + 0x80)
  183. CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *)pi >> 16)
  184. CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *)pi)
  185. CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *)pi * (1.0f / (1U << 31)))
  186. CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *)pi * (1.0 / (1U << 31)))
  187. CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8( lrintf(*(const float *)pi * (1 << 7)) + 0x80))
  188. CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16( lrintf(*(const float *)pi * (1 << 15))))
  189. CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *)pi * (1U << 31))))
  190. CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_FLT, float, *(const float *)pi)
  191. CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_FLT, float, *(const float *)pi)
  192. CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8( lrint(*(const double *)pi * (1 << 7)) + 0x80))
  193. CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16( lrint(*(const double *)pi * (1 << 15))))
  194. CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *)pi * (1U << 31))))
  195. CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_DBL, double, *(const double *)pi)
  196. CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_DBL, double, *(const double *)pi)
  197. #define SET_CONV_FUNC_GROUP(ofmt, ifmt) \
  198. ff_audio_convert_set_func(ac, ofmt, ifmt, 0, 1, 1, "C", CONV_FUNC_NAME(ofmt, ifmt)); \
  199. ff_audio_convert_set_func(ac, ofmt ## P, ifmt, 0, 1, 1, "C", CONV_FUNC_NAME(ofmt ## P, ifmt)); \
  200. ff_audio_convert_set_func(ac, ofmt, ifmt ## P, 0, 1, 1, "C", CONV_FUNC_NAME(ofmt, ifmt ## P));
  201. static void set_generic_function(AudioConvert *ac)
  202. {
  203. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8)
  204. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8)
  205. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8)
  206. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8)
  207. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8)
  208. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16)
  209. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16)
  210. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16)
  211. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16)
  212. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16)
  213. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32)
  214. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32)
  215. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32)
  216. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32)
  217. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32)
  218. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT)
  219. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT)
  220. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT)
  221. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT)
  222. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT)
  223. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL)
  224. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL)
  225. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL)
  226. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL)
  227. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL)
  228. }
  229. AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr,
  230. enum AVSampleFormat out_fmt,
  231. enum AVSampleFormat in_fmt,
  232. int channels)
  233. {
  234. AudioConvert *ac;
  235. int in_planar, out_planar;
  236. ac = av_mallocz(sizeof(*ac));
  237. if (!ac)
  238. return NULL;
  239. ac->avr = avr;
  240. ac->out_fmt = out_fmt;
  241. ac->in_fmt = in_fmt;
  242. ac->channels = channels;
  243. in_planar = av_sample_fmt_is_planar(in_fmt);
  244. out_planar = av_sample_fmt_is_planar(out_fmt);
  245. if (in_planar == out_planar) {
  246. ac->func_type = CONV_FUNC_TYPE_FLAT;
  247. ac->planes = in_planar ? ac->channels : 1;
  248. } else if (in_planar)
  249. ac->func_type = CONV_FUNC_TYPE_INTERLEAVE;
  250. else
  251. ac->func_type = CONV_FUNC_TYPE_DEINTERLEAVE;
  252. set_generic_function(ac);
  253. if (ARCH_X86)
  254. ff_audio_convert_init_x86(ac);
  255. return ac;
  256. }
  257. int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in, int len)
  258. {
  259. int use_generic = 1;
  260. /* determine whether to use the optimized function based on pointer and
  261. samples alignment in both the input and output */
  262. if (ac->has_optimized_func) {
  263. int ptr_align = FFMIN(in->ptr_align, out->ptr_align);
  264. int samples_align = FFMIN(in->samples_align, out->samples_align);
  265. int aligned_len = FFALIGN(len, ac->samples_align);
  266. if (!(ptr_align % ac->ptr_align) && samples_align >= aligned_len) {
  267. len = aligned_len;
  268. use_generic = 0;
  269. }
  270. }
  271. av_dlog(ac->avr, "%d samples - audio_convert: %s to %s (%s)\n", len,
  272. av_get_sample_fmt_name(ac->in_fmt),
  273. av_get_sample_fmt_name(ac->out_fmt),
  274. use_generic ? ac->func_descr_generic : ac->func_descr);
  275. switch (ac->func_type) {
  276. case CONV_FUNC_TYPE_FLAT: {
  277. int p;
  278. if (!in->is_planar)
  279. len *= in->channels;
  280. if (use_generic) {
  281. for (p = 0; p < ac->planes; p++)
  282. ac->conv_flat_generic(out->data[p], in->data[p], len);
  283. } else {
  284. for (p = 0; p < ac->planes; p++)
  285. ac->conv_flat(out->data[p], in->data[p], len);
  286. }
  287. break;
  288. }
  289. case CONV_FUNC_TYPE_INTERLEAVE:
  290. if (use_generic)
  291. ac->conv_interleave_generic(out->data[0], in->data, len, ac->channels);
  292. else
  293. ac->conv_interleave(out->data[0], in->data, len, ac->channels);
  294. break;
  295. case CONV_FUNC_TYPE_DEINTERLEAVE:
  296. if (use_generic)
  297. ac->conv_deinterleave_generic(out->data, in->data[0], len, ac->channels);
  298. else
  299. ac->conv_deinterleave(out->data, in->data[0], len, ac->channels);
  300. break;
  301. }
  302. out->nb_samples = in->nb_samples;
  303. return 0;
  304. }