audioconvert.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * audio conversion
  3. * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
  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. * audio conversion
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include "libavutil/avstring.h"
  27. #include "libavutil/libm.h"
  28. #include "libavutil/samplefmt.h"
  29. #include "avcodec.h"
  30. #include "audioconvert.h"
  31. #if FF_API_OLD_SAMPLE_FMT
  32. const char *avcodec_get_sample_fmt_name(int sample_fmt)
  33. {
  34. return av_get_sample_fmt_name(sample_fmt);
  35. }
  36. enum AVSampleFormat avcodec_get_sample_fmt(const char* name)
  37. {
  38. return av_get_sample_fmt(name);
  39. }
  40. void avcodec_sample_fmt_string (char *buf, int buf_size, int sample_fmt)
  41. {
  42. av_get_sample_fmt_string(buf, buf_size, sample_fmt);
  43. }
  44. #endif
  45. uint64_t avcodec_guess_channel_layout(int nb_channels, enum CodecID codec_id, const char *fmt_name)
  46. {
  47. switch(nb_channels) {
  48. case 1: return AV_CH_LAYOUT_MONO;
  49. case 2: return AV_CH_LAYOUT_STEREO;
  50. case 3: return AV_CH_LAYOUT_SURROUND;
  51. case 4: return AV_CH_LAYOUT_QUAD;
  52. case 5: return AV_CH_LAYOUT_5POINT0;
  53. case 6: return AV_CH_LAYOUT_5POINT1;
  54. case 8: return AV_CH_LAYOUT_7POINT1;
  55. default: return 0;
  56. }
  57. }
  58. #if FF_API_OLD_AUDIOCONVERT
  59. int64_t avcodec_get_channel_layout(const char *name)
  60. {
  61. return av_get_channel_layout(name);
  62. }
  63. void avcodec_get_channel_layout_string(char *buf, int buf_size, int nb_channels, int64_t channel_layout)
  64. {
  65. av_get_channel_layout_string(buf, buf_size, nb_channels, channel_layout);
  66. }
  67. int avcodec_channel_layout_num_channels(int64_t channel_layout)
  68. {
  69. return av_get_channel_layout_nb_channels(channel_layout);
  70. }
  71. #endif
  72. struct AVAudioConvert {
  73. int in_channels, out_channels;
  74. int fmt_pair;
  75. };
  76. AVAudioConvert *av_audio_convert_alloc(enum AVSampleFormat out_fmt, int out_channels,
  77. enum AVSampleFormat in_fmt, int in_channels,
  78. const float *matrix, int flags)
  79. {
  80. AVAudioConvert *ctx;
  81. if (in_channels!=out_channels)
  82. return NULL; /* FIXME: not supported */
  83. ctx = av_malloc(sizeof(AVAudioConvert));
  84. if (!ctx)
  85. return NULL;
  86. ctx->in_channels = in_channels;
  87. ctx->out_channels = out_channels;
  88. ctx->fmt_pair = out_fmt + AV_SAMPLE_FMT_NB*in_fmt;
  89. return ctx;
  90. }
  91. void av_audio_convert_free(AVAudioConvert *ctx)
  92. {
  93. av_free(ctx);
  94. }
  95. int av_audio_convert(AVAudioConvert *ctx,
  96. void * const out[6], const int out_stride[6],
  97. const void * const in[6], const int in_stride[6], int len)
  98. {
  99. int ch;
  100. //FIXME optimize common cases
  101. for(ch=0; ch<ctx->out_channels; ch++){
  102. const int is= in_stride[ch];
  103. const int os= out_stride[ch];
  104. const uint8_t *pi= in[ch];
  105. uint8_t *po= out[ch];
  106. uint8_t *end= po + os*len;
  107. if(!out[ch])
  108. continue;
  109. #define CONV(ofmt, otype, ifmt, expr)\
  110. if(ctx->fmt_pair == ofmt + AV_SAMPLE_FMT_NB*ifmt){\
  111. do{\
  112. *(otype*)po = expr; pi += is; po += os;\
  113. }while(po < end);\
  114. }
  115. //FIXME put things below under ifdefs so we do not waste space for cases no codec will need
  116. //FIXME rounding ?
  117. CONV(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_U8 , *(const uint8_t*)pi)
  118. else CONV(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)<<8)
  119. else CONV(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)<<24)
  120. else CONV(AV_SAMPLE_FMT_FLT, float , AV_SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)*(1.0 / (1<<7)))
  121. else CONV(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)*(1.0 / (1<<7)))
  122. else CONV(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_S16, (*(const int16_t*)pi>>8) + 0x80)
  123. else CONV(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_S16, *(const int16_t*)pi)
  124. else CONV(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_S16, *(const int16_t*)pi<<16)
  125. else CONV(AV_SAMPLE_FMT_FLT, float , AV_SAMPLE_FMT_S16, *(const int16_t*)pi*(1.0 / (1<<15)))
  126. else CONV(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_S16, *(const int16_t*)pi*(1.0 / (1<<15)))
  127. else CONV(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_S32, (*(const int32_t*)pi>>24) + 0x80)
  128. else CONV(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_S32, *(const int32_t*)pi>>16)
  129. else CONV(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_S32, *(const int32_t*)pi)
  130. else CONV(AV_SAMPLE_FMT_FLT, float , AV_SAMPLE_FMT_S32, *(const int32_t*)pi*(1.0 / (1U<<31)))
  131. else CONV(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_S32, *(const int32_t*)pi*(1.0 / (1U<<31)))
  132. else CONV(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8( lrintf(*(const float*)pi * (1<<7)) + 0x80))
  133. else CONV(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16( lrintf(*(const float*)pi * (1<<15))))
  134. else CONV(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(const float*)pi * (1U<<31))))
  135. else CONV(AV_SAMPLE_FMT_FLT, float , AV_SAMPLE_FMT_FLT, *(const float*)pi)
  136. else CONV(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_FLT, *(const float*)pi)
  137. else CONV(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8( lrint(*(const double*)pi * (1<<7)) + 0x80))
  138. else CONV(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16( lrint(*(const double*)pi * (1<<15))))
  139. else CONV(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(const double*)pi * (1U<<31))))
  140. else CONV(AV_SAMPLE_FMT_FLT, float , AV_SAMPLE_FMT_DBL, *(const double*)pi)
  141. else CONV(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_DBL, *(const double*)pi)
  142. else return -1;
  143. }
  144. return 0;
  145. }