You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

256 lines
9.0KB

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