audioconvert.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 libavcodec/audioconvert.c
  23. * audio conversion
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include "libavutil/avstring.h"
  27. #include "avcodec.h"
  28. #include "audioconvert.h"
  29. typedef struct SampleFmtInfo {
  30. const char *name;
  31. int bits;
  32. } SampleFmtInfo;
  33. /** this table gives more information about formats */
  34. static const SampleFmtInfo sample_fmt_info[SAMPLE_FMT_NB] = {
  35. [SAMPLE_FMT_U8] = { .name = "u8", .bits = 8 },
  36. [SAMPLE_FMT_S16] = { .name = "s16", .bits = 16 },
  37. [SAMPLE_FMT_S32] = { .name = "s32", .bits = 32 },
  38. [SAMPLE_FMT_FLT] = { .name = "flt", .bits = 32 },
  39. [SAMPLE_FMT_DBL] = { .name = "dbl", .bits = 64 },
  40. };
  41. const char *avcodec_get_sample_fmt_name(int sample_fmt)
  42. {
  43. if (sample_fmt < 0 || sample_fmt >= SAMPLE_FMT_NB)
  44. return NULL;
  45. return sample_fmt_info[sample_fmt].name;
  46. }
  47. enum SampleFormat avcodec_get_sample_fmt(const char* name)
  48. {
  49. int i;
  50. for (i=0; i < SAMPLE_FMT_NB; i++)
  51. if (!strcmp(sample_fmt_info[i].name, name))
  52. return i;
  53. return SAMPLE_FMT_NONE;
  54. }
  55. void avcodec_sample_fmt_string (char *buf, int buf_size, int sample_fmt)
  56. {
  57. /* print header */
  58. if (sample_fmt < 0)
  59. snprintf (buf, buf_size, "name " " depth");
  60. else if (sample_fmt < SAMPLE_FMT_NB) {
  61. SampleFmtInfo info= sample_fmt_info[sample_fmt];
  62. snprintf (buf, buf_size, "%-6s" " %2d ", info.name, info.bits);
  63. }
  64. }
  65. static const char* const channel_names[]={
  66. "FL", "FR", "FC", "LFE", "BL", "BR", "FLC", "FRC",
  67. "BC", "SL", "SR", "TC", "TFL", "TFC", "TFR", "TBL",
  68. "TBC", "TBR",
  69. [29] = "DL",
  70. [30] = "DR",
  71. };
  72. const char *get_channel_name(int channel_id)
  73. {
  74. if (channel_id<0 || channel_id>=FF_ARRAY_ELEMS(channel_names))
  75. return NULL;
  76. return channel_names[channel_id];
  77. }
  78. int64_t avcodec_guess_channel_layout(int nb_channels, enum CodecID codec_id, const char *fmt_name)
  79. {
  80. switch(nb_channels) {
  81. case 1: return CH_LAYOUT_MONO;
  82. case 2: return CH_LAYOUT_STEREO;
  83. case 3: return CH_LAYOUT_SURROUND;
  84. case 4: return CH_LAYOUT_QUAD;
  85. case 5: return CH_LAYOUT_5POINT0;
  86. case 6: return CH_LAYOUT_5POINT1;
  87. case 8: return CH_LAYOUT_7POINT1;
  88. default: return 0;
  89. }
  90. }
  91. static const struct {
  92. const char *name;
  93. int nb_channels;
  94. int64_t layout;
  95. } channel_layout_map[] = {
  96. { "mono", 1, CH_LAYOUT_MONO },
  97. { "stereo", 2, CH_LAYOUT_STEREO },
  98. { "surround", 3, CH_LAYOUT_SURROUND },
  99. { "quad", 4, CH_LAYOUT_QUAD },
  100. { "5.0", 5, CH_LAYOUT_5POINT0 },
  101. { "5.1", 6, CH_LAYOUT_5POINT1 },
  102. { "5.1+downmix", 8, CH_LAYOUT_5POINT1|CH_LAYOUT_STEREO_DOWNMIX, },
  103. { "7.1", 8, CH_LAYOUT_7POINT1 },
  104. { "7.1(wide)", 8, CH_LAYOUT_7POINT1_WIDE },
  105. { "7.1+downmix", 10, CH_LAYOUT_7POINT1|CH_LAYOUT_STEREO_DOWNMIX, },
  106. { 0 }
  107. };
  108. void avcodec_get_channel_layout_string(char *buf, int buf_size, int nb_channels, int64_t channel_layout)
  109. {
  110. int i;
  111. if (channel_layout==0)
  112. channel_layout = avcodec_guess_channel_layout(nb_channels, CODEC_ID_NONE, NULL);
  113. for (i=0; channel_layout_map[i].name; i++)
  114. if (nb_channels == channel_layout_map[i].nb_channels &&
  115. channel_layout == channel_layout_map[i].layout) {
  116. av_strlcpy(buf, channel_layout_map[i].name, buf_size);
  117. return;
  118. }
  119. snprintf(buf, buf_size, "%d channels", nb_channels);
  120. if (channel_layout) {
  121. int i,ch;
  122. av_strlcat(buf, " (", buf_size);
  123. for(i=0,ch=0; i<64; i++) {
  124. if ((channel_layout & (1L<<i))) {
  125. const char *name = get_channel_name(i);
  126. if (name) {
  127. if (ch>0) av_strlcat(buf, "|", buf_size);
  128. av_strlcat(buf, name, buf_size);
  129. }
  130. ch++;
  131. }
  132. }
  133. av_strlcat(buf, ")", buf_size);
  134. }
  135. }
  136. struct AVAudioConvert {
  137. int in_channels, out_channels;
  138. int fmt_pair;
  139. };
  140. AVAudioConvert *av_audio_convert_alloc(enum SampleFormat out_fmt, int out_channels,
  141. enum SampleFormat in_fmt, int in_channels,
  142. const float *matrix, int flags)
  143. {
  144. AVAudioConvert *ctx;
  145. if (in_channels!=out_channels)
  146. return NULL; /* FIXME: not supported */
  147. ctx = av_malloc(sizeof(AVAudioConvert));
  148. if (!ctx)
  149. return NULL;
  150. ctx->in_channels = in_channels;
  151. ctx->out_channels = out_channels;
  152. ctx->fmt_pair = out_fmt + SAMPLE_FMT_NB*in_fmt;
  153. return ctx;
  154. }
  155. void av_audio_convert_free(AVAudioConvert *ctx)
  156. {
  157. av_free(ctx);
  158. }
  159. int av_audio_convert(AVAudioConvert *ctx,
  160. void * const out[6], const int out_stride[6],
  161. const void * const in[6], const int in_stride[6], int len)
  162. {
  163. int ch;
  164. //FIXME optimize common cases
  165. for(ch=0; ch<ctx->out_channels; ch++){
  166. const int is= in_stride[ch];
  167. const int os= out_stride[ch];
  168. const uint8_t *pi= in[ch];
  169. uint8_t *po= out[ch];
  170. uint8_t *end= po + os*len;
  171. if(!out[ch])
  172. continue;
  173. #define CONV(ofmt, otype, ifmt, expr)\
  174. if(ctx->fmt_pair == ofmt + SAMPLE_FMT_NB*ifmt){\
  175. do{\
  176. *(otype*)po = expr; pi += is; po += os;\
  177. }while(po < end);\
  178. }
  179. //FIXME put things below under ifdefs so we do not waste space for cases no codec will need
  180. //FIXME rounding and clipping ?
  181. CONV(SAMPLE_FMT_U8 , uint8_t, SAMPLE_FMT_U8 , *(const uint8_t*)pi)
  182. else CONV(SAMPLE_FMT_S16, int16_t, SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)<<8)
  183. else CONV(SAMPLE_FMT_S32, int32_t, SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)<<24)
  184. else CONV(SAMPLE_FMT_FLT, float , SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)*(1.0 / (1<<7)))
  185. else CONV(SAMPLE_FMT_DBL, double , SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)*(1.0 / (1<<7)))
  186. else CONV(SAMPLE_FMT_U8 , uint8_t, SAMPLE_FMT_S16, (*(const int16_t*)pi>>8) + 0x80)
  187. else CONV(SAMPLE_FMT_S16, int16_t, SAMPLE_FMT_S16, *(const int16_t*)pi)
  188. else CONV(SAMPLE_FMT_S32, int32_t, SAMPLE_FMT_S16, *(const int16_t*)pi<<16)
  189. else CONV(SAMPLE_FMT_FLT, float , SAMPLE_FMT_S16, *(const int16_t*)pi*(1.0 / (1<<15)))
  190. else CONV(SAMPLE_FMT_DBL, double , SAMPLE_FMT_S16, *(const int16_t*)pi*(1.0 / (1<<15)))
  191. else CONV(SAMPLE_FMT_U8 , uint8_t, SAMPLE_FMT_S32, (*(const int32_t*)pi>>24) + 0x80)
  192. else CONV(SAMPLE_FMT_S16, int16_t, SAMPLE_FMT_S32, *(const int32_t*)pi>>16)
  193. else CONV(SAMPLE_FMT_S32, int32_t, SAMPLE_FMT_S32, *(const int32_t*)pi)
  194. else CONV(SAMPLE_FMT_FLT, float , SAMPLE_FMT_S32, *(const int32_t*)pi*(1.0 / (1<<31)))
  195. else CONV(SAMPLE_FMT_DBL, double , SAMPLE_FMT_S32, *(const int32_t*)pi*(1.0 / (1<<31)))
  196. else CONV(SAMPLE_FMT_U8 , uint8_t, SAMPLE_FMT_FLT, lrintf(*(const float*)pi * (1<<7)) + 0x80)
  197. else CONV(SAMPLE_FMT_S16, int16_t, SAMPLE_FMT_FLT, lrintf(*(const float*)pi * (1<<15)))
  198. else CONV(SAMPLE_FMT_S32, int32_t, SAMPLE_FMT_FLT, lrintf(*(const float*)pi * (1<<31)))
  199. else CONV(SAMPLE_FMT_FLT, float , SAMPLE_FMT_FLT, *(const float*)pi)
  200. else CONV(SAMPLE_FMT_DBL, double , SAMPLE_FMT_FLT, *(const float*)pi)
  201. else CONV(SAMPLE_FMT_U8 , uint8_t, SAMPLE_FMT_DBL, lrint(*(const double*)pi * (1<<7)) + 0x80)
  202. else CONV(SAMPLE_FMT_S16, int16_t, SAMPLE_FMT_DBL, lrint(*(const double*)pi * (1<<15)))
  203. else CONV(SAMPLE_FMT_S32, int32_t, SAMPLE_FMT_DBL, lrint(*(const double*)pi * (1<<31)))
  204. else CONV(SAMPLE_FMT_FLT, float , SAMPLE_FMT_DBL, *(const double*)pi)
  205. else CONV(SAMPLE_FMT_DBL, double , SAMPLE_FMT_DBL, *(const double*)pi)
  206. else return -1;
  207. }
  208. return 0;
  209. }