audioconvert.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * audio conversion routines
  23. */
  24. #include "avstring.h"
  25. #include "avutil.h"
  26. #include "audioconvert.h"
  27. static const char * const channel_names[] = {
  28. "FL", "FR", "FC", "LFE", "BL", "BR", "FLC", "FRC",
  29. "BC", "SL", "SR", "TC", "TFL", "TFC", "TFR", "TBL",
  30. "TBC", "TBR",
  31. [29] = "DL",
  32. [30] = "DR",
  33. };
  34. static const char *get_channel_name(int channel_id)
  35. {
  36. if (channel_id < 0 || channel_id >= FF_ARRAY_ELEMS(channel_names))
  37. return NULL;
  38. return channel_names[channel_id];
  39. }
  40. static const struct {
  41. const char *name;
  42. int nb_channels;
  43. int64_t layout;
  44. } channel_layout_map[] = {
  45. { "mono", 1, AV_CH_LAYOUT_MONO },
  46. { "stereo", 2, AV_CH_LAYOUT_STEREO },
  47. { "4.0", 4, AV_CH_LAYOUT_4POINT0 },
  48. { "quad", 4, AV_CH_LAYOUT_QUAD },
  49. { "5.0", 5, AV_CH_LAYOUT_5POINT0 },
  50. { "5.0(back)", 5, AV_CH_LAYOUT_5POINT0_BACK },
  51. { "5.1", 6, AV_CH_LAYOUT_5POINT1 },
  52. { "5.1(back)", 6, AV_CH_LAYOUT_5POINT1_BACK },
  53. { "5.1+downmix", 8, AV_CH_LAYOUT_5POINT1|AV_CH_LAYOUT_STEREO_DOWNMIX, },
  54. { "7.1", 8, AV_CH_LAYOUT_7POINT1 },
  55. { "7.1(wide)", 8, AV_CH_LAYOUT_7POINT1_WIDE },
  56. { "7.1+downmix", 10, AV_CH_LAYOUT_7POINT1|AV_CH_LAYOUT_STEREO_DOWNMIX, },
  57. { 0 }
  58. };
  59. int64_t av_get_channel_layout(const char *name)
  60. {
  61. int i = 0;
  62. do {
  63. if (!strcmp(channel_layout_map[i].name, name))
  64. return channel_layout_map[i].layout;
  65. i++;
  66. } while (channel_layout_map[i].name);
  67. return 0;
  68. }
  69. void av_get_channel_layout_string(char *buf, int buf_size,
  70. int nb_channels, int64_t channel_layout)
  71. {
  72. int i;
  73. if (nb_channels <= 0)
  74. nb_channels = av_get_channel_layout_nb_channels(channel_layout);
  75. for (i = 0; channel_layout_map[i].name; i++)
  76. if (nb_channels == channel_layout_map[i].nb_channels &&
  77. channel_layout == channel_layout_map[i].layout) {
  78. av_strlcpy(buf, channel_layout_map[i].name, buf_size);
  79. return;
  80. }
  81. snprintf(buf, buf_size, "%d channels", nb_channels);
  82. if (channel_layout) {
  83. int i,ch;
  84. av_strlcat(buf, " (", buf_size);
  85. for(i=0,ch=0; i<64; i++) {
  86. if ((channel_layout & (1L<<i))) {
  87. const char *name = get_channel_name(i);
  88. if (name) {
  89. if (ch>0) av_strlcat(buf, "|", buf_size);
  90. av_strlcat(buf, name, buf_size);
  91. }
  92. ch++;
  93. }
  94. }
  95. av_strlcat(buf, ")", buf_size);
  96. }
  97. }
  98. int av_get_channel_layout_nb_channels(int64_t channel_layout)
  99. {
  100. int count;
  101. uint64_t x = channel_layout;
  102. for (count = 0; x; count++)
  103. x &= x-1; // unset lowest set bit
  104. return count;
  105. }