audio_convert.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 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. #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 = ff_sample_fmt_is_planar(in_fmt, channels);
  268. out_planar = ff_sample_fmt_is_planar(out_fmt, channels);
  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_AARCH64)
  278. ff_audio_convert_init_aarch64(ac);
  279. if (ARCH_ARM)
  280. ff_audio_convert_init_arm(ac);
  281. if (ARCH_X86)
  282. ff_audio_convert_init_x86(ac);
  283. return ac;
  284. }
  285. int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in)
  286. {
  287. int use_generic = 1;
  288. int len = in->nb_samples;
  289. int p;
  290. if (ac->dc) {
  291. /* dithered conversion */
  292. av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n",
  293. len, av_get_sample_fmt_name(ac->in_fmt),
  294. av_get_sample_fmt_name(ac->out_fmt));
  295. return ff_convert_dither(ac->dc, out, in);
  296. }
  297. /* determine whether to use the optimized function based on pointer and
  298. samples alignment in both the input and output */
  299. if (ac->has_optimized_func) {
  300. int ptr_align = FFMIN(in->ptr_align, out->ptr_align);
  301. int samples_align = FFMIN(in->samples_align, out->samples_align);
  302. int aligned_len = FFALIGN(len, ac->samples_align);
  303. if (!(ptr_align % ac->ptr_align) && samples_align >= aligned_len) {
  304. len = aligned_len;
  305. use_generic = 0;
  306. }
  307. }
  308. av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\n", len,
  309. av_get_sample_fmt_name(ac->in_fmt),
  310. av_get_sample_fmt_name(ac->out_fmt),
  311. use_generic ? ac->func_descr_generic : ac->func_descr);
  312. if (ac->apply_map) {
  313. ChannelMapInfo *map = &ac->avr->ch_map_info;
  314. if (!ff_sample_fmt_is_planar(ac->out_fmt, ac->channels)) {
  315. av_log(ac->avr, AV_LOG_ERROR, "cannot remap packed format during conversion\n");
  316. return AVERROR(EINVAL);
  317. }
  318. if (map->do_remap) {
  319. if (ff_sample_fmt_is_planar(ac->in_fmt, ac->channels)) {
  320. conv_func_flat *convert = use_generic ? ac->conv_flat_generic :
  321. ac->conv_flat;
  322. for (p = 0; p < ac->planes; p++)
  323. if (map->channel_map[p] >= 0)
  324. convert(out->data[p], in->data[map->channel_map[p]], len);
  325. } else {
  326. uint8_t *data[AVRESAMPLE_MAX_CHANNELS];
  327. conv_func_deinterleave *convert = use_generic ?
  328. ac->conv_deinterleave_generic :
  329. ac->conv_deinterleave;
  330. for (p = 0; p < ac->channels; p++)
  331. data[map->input_map[p]] = out->data[p];
  332. convert(data, in->data[0], len, ac->channels);
  333. }
  334. }
  335. if (map->do_copy || map->do_zero) {
  336. for (p = 0; p < ac->planes; p++) {
  337. if (map->channel_copy[p])
  338. memcpy(out->data[p], out->data[map->channel_copy[p]],
  339. len * out->stride);
  340. else if (map->channel_zero[p])
  341. av_samples_set_silence(&out->data[p], 0, len, 1, ac->out_fmt);
  342. }
  343. }
  344. } else {
  345. switch (ac->func_type) {
  346. case CONV_FUNC_TYPE_FLAT: {
  347. if (!in->is_planar)
  348. len *= in->channels;
  349. if (use_generic) {
  350. for (p = 0; p < ac->planes; p++)
  351. ac->conv_flat_generic(out->data[p], in->data[p], len);
  352. } else {
  353. for (p = 0; p < ac->planes; p++)
  354. ac->conv_flat(out->data[p], in->data[p], len);
  355. }
  356. break;
  357. }
  358. case CONV_FUNC_TYPE_INTERLEAVE:
  359. if (use_generic)
  360. ac->conv_interleave_generic(out->data[0], in->data, len,
  361. ac->channels);
  362. else
  363. ac->conv_interleave(out->data[0], in->data, len, ac->channels);
  364. break;
  365. case CONV_FUNC_TYPE_DEINTERLEAVE:
  366. if (use_generic)
  367. ac->conv_deinterleave_generic(out->data, in->data[0], len,
  368. ac->channels);
  369. else
  370. ac->conv_deinterleave(out->data, in->data[0], len,
  371. ac->channels);
  372. break;
  373. }
  374. }
  375. out->nb_samples = in->nb_samples;
  376. return 0;
  377. }