audioconvert.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. [0] = "FL", /* front left */
  29. [1] = "FR", /* front right */
  30. [2] = "FC", /* front center */
  31. [3] = "LFE", /* low frequency */
  32. [4] = "BL", /* back left */
  33. [5] = "BR", /* back right */
  34. [6] = "FLC", /* front left-of-center */
  35. [7] = "FRC", /* front right-of-center */
  36. [8] = "BC", /* back-center */
  37. [9] = "SL", /* side left */
  38. [10] = "SR", /* side right */
  39. [11] = "TC", /* top center */
  40. [12] = "TFL", /* top front left */
  41. [13] = "TFC", /* top front center */
  42. [14] = "TFR", /* top front right */
  43. [15] = "TBL", /* top back left */
  44. [16] = "TBC", /* top back center */
  45. [17] = "TBR", /* top back right */
  46. [29] = "DL", /* downmix left */
  47. [30] = "DR", /* downmix right */
  48. };
  49. static const char *get_channel_name(int channel_id)
  50. {
  51. if (channel_id < 0 || channel_id >= FF_ARRAY_ELEMS(channel_names))
  52. return NULL;
  53. return channel_names[channel_id];
  54. }
  55. static const struct {
  56. const char *name;
  57. int nb_channels;
  58. int64_t layout;
  59. } channel_layout_map[] = {
  60. { "mono", 1, AV_CH_LAYOUT_MONO },
  61. { "stereo", 2, AV_CH_LAYOUT_STEREO },
  62. { "4.0", 4, AV_CH_LAYOUT_4POINT0 },
  63. { "quad", 4, AV_CH_LAYOUT_QUAD },
  64. { "5.0(side)", 5, AV_CH_LAYOUT_5POINT0 },
  65. { "5.0", 5, AV_CH_LAYOUT_5POINT0_BACK },
  66. { "5.1(side)", 6, AV_CH_LAYOUT_5POINT1 },
  67. { "5.1", 6, AV_CH_LAYOUT_5POINT1_BACK },
  68. { "7.1", 8, AV_CH_LAYOUT_7POINT1 },
  69. { "7.1(wide)", 8, AV_CH_LAYOUT_7POINT1_WIDE },
  70. { "5.1+downmix", 8, AV_CH_LAYOUT_5POINT1|AV_CH_LAYOUT_STEREO_DOWNMIX, },
  71. { "7.1+downmix", 10, AV_CH_LAYOUT_7POINT1|AV_CH_LAYOUT_STEREO_DOWNMIX, },
  72. { 0 }
  73. };
  74. int64_t av_get_channel_layout(const char *name)
  75. {
  76. int i = 0;
  77. do {
  78. if (!strcmp(channel_layout_map[i].name, name))
  79. return channel_layout_map[i].layout;
  80. i++;
  81. } while (channel_layout_map[i].name);
  82. return 0;
  83. }
  84. void av_get_channel_layout_string(char *buf, int buf_size,
  85. int nb_channels, int64_t channel_layout)
  86. {
  87. int i;
  88. if (nb_channels <= 0)
  89. nb_channels = av_get_channel_layout_nb_channels(channel_layout);
  90. for (i = 0; channel_layout_map[i].name; i++)
  91. if (nb_channels == channel_layout_map[i].nb_channels &&
  92. channel_layout == channel_layout_map[i].layout) {
  93. av_strlcpy(buf, channel_layout_map[i].name, buf_size);
  94. return;
  95. }
  96. snprintf(buf, buf_size, "%d channels", nb_channels);
  97. if (channel_layout) {
  98. int i, ch;
  99. av_strlcat(buf, " (", buf_size);
  100. for (i = 0, ch = 0; i < 64; i++) {
  101. if ((channel_layout & (1L << i))) {
  102. const char *name = get_channel_name(i);
  103. if (name) {
  104. if (ch > 0)
  105. av_strlcat(buf, "|", buf_size);
  106. av_strlcat(buf, name, buf_size);
  107. }
  108. ch++;
  109. }
  110. }
  111. av_strlcat(buf, ")", buf_size);
  112. }
  113. }
  114. int av_get_channel_layout_nb_channels(int64_t channel_layout)
  115. {
  116. int count;
  117. uint64_t x = channel_layout;
  118. for (count = 0; x; count++)
  119. x &= x-1; // unset lowest set bit
  120. return count;
  121. }
  122. int64_t av_get_default_channel_layout(int nb_channels) {
  123. int i;
  124. for (i = 0; channel_layout_map[i].name; i++)
  125. if (nb_channels == channel_layout_map[i].nb_channels)
  126. return channel_layout_map[i].layout;
  127. return 0;
  128. }