audioconvert.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. uint64_t layout;
  59. } channel_layout_map[] = {
  60. { "mono", 1, AV_CH_LAYOUT_MONO },
  61. { "stereo", 2, AV_CH_LAYOUT_STEREO },
  62. { "2.1", 3, AV_CH_LAYOUT_2POINT1 },
  63. { "4.0", 4, AV_CH_LAYOUT_4POINT0 },
  64. { "quad", 4, AV_CH_LAYOUT_QUAD },
  65. { "5.0", 5, AV_CH_LAYOUT_5POINT0_BACK },
  66. { "5.0(side)", 5, AV_CH_LAYOUT_5POINT0 },
  67. { "5.1", 6, AV_CH_LAYOUT_5POINT1_BACK },
  68. { "5.1(side)", 6, AV_CH_LAYOUT_5POINT1 },
  69. { "7.1", 8, AV_CH_LAYOUT_7POINT1 },
  70. { "7.1(wide)", 8, AV_CH_LAYOUT_7POINT1_WIDE },
  71. { "downmix", 2, AV_CH_LAYOUT_STEREO_DOWNMIX, },
  72. { 0 }
  73. };
  74. static uint64_t get_channel_layout_single(const char *name, int name_len)
  75. {
  76. int i;
  77. char *end;
  78. int64_t layout;
  79. for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map) - 1; i++) {
  80. if (strlen(channel_layout_map[i].name) == name_len &&
  81. !memcmp(channel_layout_map[i].name, name, name_len))
  82. return channel_layout_map[i].layout;
  83. }
  84. for (i = 0; i < FF_ARRAY_ELEMS(channel_names); i++)
  85. if (channel_names[i] &&
  86. strlen(channel_names[i]) == name_len &&
  87. !memcmp(channel_names[i], name, name_len))
  88. return (int64_t)1 << i;
  89. i = strtol(name, &end, 10);
  90. if (end - name == name_len ||
  91. (end + 1 - name == name_len && *end == 'c'))
  92. return av_get_default_channel_layout(i);
  93. layout = strtoll(name, &end, 0);
  94. if (end - name == name_len)
  95. return FFMAX(layout, 0);
  96. return 0;
  97. }
  98. uint64_t av_get_channel_layout(const char *name)
  99. {
  100. const char *n, *e;
  101. const char *name_end = name + strlen(name);
  102. int64_t layout = 0, layout_single;
  103. for (n = name; n < name_end; n = e + 1) {
  104. for (e = n; e < name_end && *e != '+' && *e != '|'; e++);
  105. layout_single = get_channel_layout_single(n, e - n);
  106. if (!layout_single)
  107. return 0;
  108. layout |= layout_single;
  109. }
  110. return layout;
  111. }
  112. void av_get_channel_layout_string(char *buf, int buf_size,
  113. int nb_channels, uint64_t channel_layout)
  114. {
  115. int i;
  116. if (nb_channels <= 0)
  117. nb_channels = av_get_channel_layout_nb_channels(channel_layout);
  118. for (i = 0; channel_layout_map[i].name; i++)
  119. if (nb_channels == channel_layout_map[i].nb_channels &&
  120. channel_layout == channel_layout_map[i].layout) {
  121. av_strlcpy(buf, channel_layout_map[i].name, buf_size);
  122. return;
  123. }
  124. snprintf(buf, buf_size, "%d channels", nb_channels);
  125. if (channel_layout) {
  126. int i, ch;
  127. av_strlcat(buf, " (", buf_size);
  128. for (i = 0, ch = 0; i < 64; i++) {
  129. if ((channel_layout & (UINT64_C(1) << i))) {
  130. const char *name = get_channel_name(i);
  131. if (name) {
  132. if (ch > 0)
  133. av_strlcat(buf, "+", buf_size);
  134. av_strlcat(buf, name, buf_size);
  135. }
  136. ch++;
  137. }
  138. }
  139. av_strlcat(buf, ")", buf_size);
  140. }
  141. }
  142. int av_get_channel_layout_nb_channels(uint64_t channel_layout)
  143. {
  144. int count;
  145. uint64_t x = channel_layout;
  146. for (count = 0; x; count++)
  147. x &= x-1; // unset lowest set bit
  148. return count;
  149. }
  150. int64_t av_get_default_channel_layout(int nb_channels) {
  151. int i;
  152. for (i = 0; channel_layout_map[i].name; i++)
  153. if (nb_channels == channel_layout_map[i].nb_channels)
  154. return channel_layout_map[i].layout;
  155. return 0;
  156. }