audio_convert.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. #include "dither.h"
  31. enum ConvFuncType {
  32. CONV_FUNC_TYPE_FLAT,
  33. CONV_FUNC_TYPE_INTERLEAVE,
  34. CONV_FUNC_TYPE_DEINTERLEAVE,
  35. };
  36. typedef void (conv_func_flat)(uint8_t *out, const uint8_t *in, int len);
  37. typedef void (conv_func_interleave)(uint8_t *out, uint8_t *const *in,
  38. int len, int channels);
  39. typedef void (conv_func_deinterleave)(uint8_t **out, const uint8_t *in, int len,
  40. int channels);
  41. struct AudioConvert {
  42. AVAudioResampleContext *avr;
  43. DitherContext *dc;
  44. enum AVSampleFormat in_fmt;
  45. enum AVSampleFormat out_fmt;
  46. int apply_map;
  47. int channels;
  48. int planes;
  49. int ptr_align;
  50. int samples_align;
  51. int has_optimized_func;
  52. const char *func_descr;
  53. const char *func_descr_generic;
  54. enum ConvFuncType func_type;
  55. conv_func_flat *conv_flat;
  56. conv_func_flat *conv_flat_generic;
  57. conv_func_interleave *conv_interleave;
  58. conv_func_interleave *conv_interleave_generic;
  59. conv_func_deinterleave *conv_deinterleave;
  60. conv_func_deinterleave *conv_deinterleave_generic;
  61. };
  62. void ff_audio_convert_set_func(AudioConvert *ac, enum AVSampleFormat out_fmt,
  63. enum AVSampleFormat in_fmt, int channels,
  64. int ptr_align, int samples_align,
  65. const char *descr, void *conv)
  66. {
  67. int found = 0;
  68. switch (ac->func_type) {
  69. case CONV_FUNC_TYPE_FLAT:
  70. if (av_get_packed_sample_fmt(ac->in_fmt) == in_fmt &&
  71. av_get_packed_sample_fmt(ac->out_fmt) == out_fmt) {
  72. ac->conv_flat = conv;
  73. ac->func_descr = descr;
  74. ac->ptr_align = ptr_align;
  75. ac->samples_align = samples_align;
  76. if (ptr_align == 1 && samples_align == 1) {
  77. ac->conv_flat_generic = conv;
  78. ac->func_descr_generic = descr;
  79. } else {
  80. ac->has_optimized_func = 1;
  81. }
  82. found = 1;
  83. }
  84. break;
  85. case CONV_FUNC_TYPE_INTERLEAVE:
  86. if (ac->in_fmt == in_fmt && ac->out_fmt == out_fmt &&
  87. (!channels || ac->channels == channels)) {
  88. ac->conv_interleave = conv;
  89. ac->func_descr = descr;
  90. ac->ptr_align = ptr_align;
  91. ac->samples_align = samples_align;
  92. if (ptr_align == 1 && samples_align == 1) {
  93. ac->conv_interleave_generic = conv;
  94. ac->func_descr_generic = descr;
  95. } else {
  96. ac->has_optimized_func = 1;
  97. }
  98. found = 1;
  99. }
  100. break;
  101. case CONV_FUNC_TYPE_DEINTERLEAVE:
  102. if (ac->in_fmt == in_fmt && ac->out_fmt == out_fmt &&
  103. (!channels || ac->channels == channels)) {
  104. ac->conv_deinterleave = conv;
  105. ac->func_descr = descr;
  106. ac->ptr_align = ptr_align;
  107. ac->samples_align = samples_align;
  108. if (ptr_align == 1 && samples_align == 1) {
  109. ac->conv_deinterleave_generic = conv;
  110. ac->func_descr_generic = descr;
  111. } else {
  112. ac->has_optimized_func = 1;
  113. }
  114. found = 1;
  115. }
  116. break;
  117. }
  118. if (found) {
  119. av_log(ac->avr, AV_LOG_DEBUG, "audio_convert: found function: %-4s "
  120. "to %-4s (%s)\n", av_get_sample_fmt_name(ac->in_fmt),
  121. av_get_sample_fmt_name(ac->out_fmt), descr);
  122. }
  123. }
  124. #define CONV_FUNC_NAME(dst_fmt, src_fmt) conv_ ## src_fmt ## _to_ ## dst_fmt
  125. #define CONV_LOOP(otype, expr) \
  126. do { \
  127. *(otype *)po = expr; \
  128. pi += is; \
  129. po += os; \
  130. } while (po < end); \
  131. #define CONV_FUNC_FLAT(ofmt, otype, ifmt, itype, expr) \
  132. static void CONV_FUNC_NAME(ofmt, ifmt)(uint8_t *out, const uint8_t *in, \
  133. int len) \
  134. { \
  135. int is = sizeof(itype); \
  136. int os = sizeof(otype); \
  137. const uint8_t *pi = in; \
  138. uint8_t *po = out; \
  139. uint8_t *end = out + os * len; \
  140. CONV_LOOP(otype, expr) \
  141. }
  142. #define CONV_FUNC_INTERLEAVE(ofmt, otype, ifmt, itype, expr) \
  143. static void CONV_FUNC_NAME(ofmt, ifmt)(uint8_t *out, const uint8_t **in, \
  144. int len, int channels) \
  145. { \
  146. int ch; \
  147. int out_bps = sizeof(otype); \
  148. int is = sizeof(itype); \
  149. int os = channels * out_bps; \
  150. for (ch = 0; ch < channels; ch++) { \
  151. const uint8_t *pi = in[ch]; \
  152. uint8_t *po = out + ch * out_bps; \
  153. uint8_t *end = po + os * len; \
  154. CONV_LOOP(otype, expr) \
  155. } \
  156. }
  157. #define CONV_FUNC_DEINTERLEAVE(ofmt, otype, ifmt, itype, expr) \
  158. static void CONV_FUNC_NAME(ofmt, ifmt)(uint8_t **out, const uint8_t *in, \
  159. int len, int channels) \
  160. { \
  161. int ch; \
  162. int in_bps = sizeof(itype); \
  163. int is = channels * in_bps; \
  164. int os = sizeof(otype); \
  165. for (ch = 0; ch < channels; ch++) { \
  166. const uint8_t *pi = in + ch * in_bps; \
  167. uint8_t *po = out[ch]; \
  168. uint8_t *end = po + os * len; \
  169. CONV_LOOP(otype, expr) \
  170. } \
  171. }
  172. #define CONV_FUNC_GROUP(ofmt, otype, ifmt, itype, expr) \
  173. CONV_FUNC_FLAT( ofmt, otype, ifmt, itype, expr) \
  174. CONV_FUNC_INTERLEAVE( ofmt, otype, ifmt ## P, itype, expr) \
  175. CONV_FUNC_DEINTERLEAVE(ofmt ## P, otype, ifmt, itype, expr)
  176. CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_U8, uint8_t, *(const uint8_t *)pi)
  177. CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_U8, uint8_t, (*(const uint8_t *)pi - 0x80) << 8)
  178. CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_U8, uint8_t, (*(const uint8_t *)pi - 0x80) << 24)
  179. CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t, (*(const uint8_t *)pi - 0x80) * (1.0f / (1 << 7)))
  180. CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t, (*(const uint8_t *)pi - 0x80) * (1.0 / (1 << 7)))
  181. CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t, (*(const int16_t *)pi >> 8) + 0x80)
  182. CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *)pi)
  183. CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *)pi << 16)
  184. CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *)pi * (1.0f / (1 << 15)))
  185. CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *)pi * (1.0 / (1 << 15)))
  186. CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t, (*(const int32_t *)pi >> 24) + 0x80)
  187. CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *)pi >> 16)
  188. CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *)pi)
  189. CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *)pi * (1.0f / (1U << 31)))
  190. CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *)pi * (1.0 / (1U << 31)))
  191. CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8( lrintf(*(const float *)pi * (1 << 7)) + 0x80))
  192. CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16( lrintf(*(const float *)pi * (1 << 15))))
  193. CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *)pi * (1U << 31))))
  194. CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_FLT, float, *(const float *)pi)
  195. CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_FLT, float, *(const float *)pi)
  196. CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8( lrint(*(const double *)pi * (1 << 7)) + 0x80))
  197. CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16( lrint(*(const double *)pi * (1 << 15))))
  198. CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *)pi * (1U << 31))))
  199. CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_DBL, double, *(const double *)pi)
  200. CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_DBL, double, *(const double *)pi)
  201. #define SET_CONV_FUNC_GROUP(ofmt, ifmt) \
  202. ff_audio_convert_set_func(ac, ofmt, ifmt, 0, 1, 1, "C", CONV_FUNC_NAME(ofmt, ifmt)); \
  203. ff_audio_convert_set_func(ac, ofmt ## P, ifmt, 0, 1, 1, "C", CONV_FUNC_NAME(ofmt ## P, ifmt)); \
  204. ff_audio_convert_set_func(ac, ofmt, ifmt ## P, 0, 1, 1, "C", CONV_FUNC_NAME(ofmt, ifmt ## P));
  205. static void set_generic_function(AudioConvert *ac)
  206. {
  207. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8)
  208. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8)
  209. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8)
  210. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8)
  211. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8)
  212. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16)
  213. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16)
  214. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16)
  215. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16)
  216. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16)
  217. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32)
  218. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32)
  219. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32)
  220. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32)
  221. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32)
  222. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT)
  223. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT)
  224. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT)
  225. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT)
  226. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT)
  227. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL)
  228. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL)
  229. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL)
  230. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL)
  231. SET_CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL)
  232. }
  233. void ff_audio_convert_free(AudioConvert **ac)
  234. {
  235. if (!*ac)
  236. return;
  237. ff_dither_free(&(*ac)->dc);
  238. av_freep(ac);
  239. }
  240. AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr,
  241. enum AVSampleFormat out_fmt,
  242. enum AVSampleFormat in_fmt,
  243. int channels, int sample_rate,
  244. int apply_map)
  245. {
  246. AudioConvert *ac;
  247. int in_planar, out_planar;
  248. ac = av_mallocz(sizeof(*ac));
  249. if (!ac)
  250. return NULL;
  251. ac->avr = avr;
  252. ac->out_fmt = out_fmt;
  253. ac->in_fmt = in_fmt;
  254. ac->channels = channels;
  255. ac->apply_map = apply_map;
  256. if (avr->dither_method != AV_RESAMPLE_DITHER_NONE &&
  257. av_get_packed_sample_fmt(out_fmt) == AV_SAMPLE_FMT_S16 &&
  258. av_get_bytes_per_sample(in_fmt) > 2) {
  259. ac->dc = ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate,
  260. apply_map);
  261. if (!ac->dc) {
  262. av_free(ac);
  263. return NULL;
  264. }
  265. return ac;
  266. }
  267. in_planar = av_sample_fmt_is_planar(in_fmt);
  268. out_planar = av_sample_fmt_is_planar(out_fmt);
  269. if (in_planar == out_planar) {
  270. ac->func_type = CONV_FUNC_TYPE_FLAT;
  271. ac->planes = in_planar ? ac->channels : 1;
  272. } else if (in_planar)
  273. ac->func_type = CONV_FUNC_TYPE_INTERLEAVE;
  274. else
  275. ac->func_type = CONV_FUNC_TYPE_DEINTERLEAVE;
  276. set_generic_function(ac);
  277. if (ARCH_ARM)
  278. ff_audio_convert_init_arm(ac);
  279. if (ARCH_X86)
  280. ff_audio_convert_init_x86(ac);
  281. return ac;
  282. }
  283. int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in)
  284. {
  285. int use_generic = 1;
  286. int len = in->nb_samples;
  287. int p;
  288. if (ac->dc) {
  289. /* dithered conversion */
  290. av_dlog(ac->avr, "%d samples - audio_convert: %s to %s (dithered)\n",
  291. len, av_get_sample_fmt_name(ac->in_fmt),
  292. av_get_sample_fmt_name(ac->out_fmt));
  293. return ff_convert_dither(ac->dc, out, in);
  294. }
  295. /* determine whether to use the optimized function based on pointer and
  296. samples alignment in both the input and output */
  297. if (ac->has_optimized_func) {
  298. int ptr_align = FFMIN(in->ptr_align, out->ptr_align);
  299. int samples_align = FFMIN(in->samples_align, out->samples_align);
  300. int aligned_len = FFALIGN(len, ac->samples_align);
  301. if (!(ptr_align % ac->ptr_align) && samples_align >= aligned_len) {
  302. len = aligned_len;
  303. use_generic = 0;
  304. }
  305. }
  306. av_dlog(ac->avr, "%d samples - audio_convert: %s to %s (%s)\n", len,
  307. av_get_sample_fmt_name(ac->in_fmt),
  308. av_get_sample_fmt_name(ac->out_fmt),
  309. use_generic ? ac->func_descr_generic : ac->func_descr);
  310. if (ac->apply_map) {
  311. ChannelMapInfo *map = &ac->avr->ch_map_info;
  312. if (!av_sample_fmt_is_planar(ac->out_fmt)) {
  313. av_log(ac->avr, AV_LOG_ERROR, "cannot remap packed format during conversion\n");
  314. return AVERROR(EINVAL);
  315. }
  316. if (map->do_remap) {
  317. if (av_sample_fmt_is_planar(ac->in_fmt)) {
  318. conv_func_flat *convert = use_generic ? ac->conv_flat_generic :
  319. ac->conv_flat;
  320. for (p = 0; p < ac->planes; p++)
  321. if (map->channel_map[p] >= 0)
  322. convert(out->data[p], in->data[map->channel_map[p]], len);
  323. } else {
  324. uint8_t *data[AVRESAMPLE_MAX_CHANNELS];
  325. conv_func_deinterleave *convert = use_generic ?
  326. ac->conv_deinterleave_generic :
  327. ac->conv_deinterleave;
  328. for (p = 0; p < ac->channels; p++)
  329. data[map->input_map[p]] = out->data[p];
  330. convert(data, in->data[0], len, ac->channels);
  331. }
  332. }
  333. if (map->do_copy || map->do_zero) {
  334. for (p = 0; p < ac->planes; p++) {
  335. if (map->channel_copy[p])
  336. memcpy(out->data[p], out->data[map->channel_copy[p]],
  337. len * out->stride);
  338. else if (map->channel_zero[p])
  339. av_samples_set_silence(&out->data[p], 0, len, 1, ac->out_fmt);
  340. }
  341. }
  342. } else {
  343. switch (ac->func_type) {
  344. case CONV_FUNC_TYPE_FLAT: {
  345. if (!in->is_planar)
  346. len *= in->channels;
  347. if (use_generic) {
  348. for (p = 0; p < ac->planes; p++)
  349. ac->conv_flat_generic(out->data[p], in->data[p], len);
  350. } else {
  351. for (p = 0; p < ac->planes; p++)
  352. ac->conv_flat(out->data[p], in->data[p], len);
  353. }
  354. break;
  355. }
  356. case CONV_FUNC_TYPE_INTERLEAVE:
  357. if (use_generic)
  358. ac->conv_interleave_generic(out->data[0], in->data, len,
  359. ac->channels);
  360. else
  361. ac->conv_interleave(out->data[0], in->data, len, ac->channels);
  362. break;
  363. case CONV_FUNC_TYPE_DEINTERLEAVE:
  364. if (use_generic)
  365. ac->conv_deinterleave_generic(out->data, in->data[0], len,
  366. ac->channels);
  367. else
  368. ac->conv_deinterleave(out->data, in->data[0], len,
  369. ac->channels);
  370. break;
  371. }
  372. }
  373. out->nb_samples = in->nb_samples;
  374. return 0;
  375. }