audio_convert.c 16 KB

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