audioconvert.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. [31] = "WL", /* wide left */
  49. [32] = "WR", /* wide right */
  50. [33] = "SDL", /* surround direct left */
  51. [34] = "SDR", /* surround direct right */
  52. };
  53. static const char *get_channel_name(int channel_id)
  54. {
  55. if (channel_id < 0 || channel_id >= FF_ARRAY_ELEMS(channel_names))
  56. return NULL;
  57. return channel_names[channel_id];
  58. }
  59. static const struct {
  60. const char *name;
  61. int nb_channels;
  62. uint64_t layout;
  63. } channel_layout_map[] = {
  64. { "mono", 1, AV_CH_LAYOUT_MONO },
  65. { "stereo", 2, AV_CH_LAYOUT_STEREO },
  66. { "2.1", 3, AV_CH_LAYOUT_2POINT1 },
  67. { "3.0", 3, AV_CH_LAYOUT_SURROUND },
  68. { "3.0(back)", 3, AV_CH_LAYOUT_2_1 },
  69. { "4.0", 4, AV_CH_LAYOUT_4POINT0 },
  70. { "quad", 4, AV_CH_LAYOUT_QUAD },
  71. { "quad(side)", 4, AV_CH_LAYOUT_2_2 },
  72. { "3.1", 4, AV_CH_LAYOUT_3POINT1 },
  73. { "5.0", 5, AV_CH_LAYOUT_5POINT0_BACK },
  74. { "5.0(side)", 5, AV_CH_LAYOUT_5POINT0 },
  75. { "4.1", 5, AV_CH_LAYOUT_4POINT1 },
  76. { "5.1", 6, AV_CH_LAYOUT_5POINT1_BACK },
  77. { "5.1(side)", 6, AV_CH_LAYOUT_5POINT1 },
  78. { "6.0", 6, AV_CH_LAYOUT_6POINT0 },
  79. { "6.0(front)", 6, AV_CH_LAYOUT_6POINT0_FRONT },
  80. { "hexagonal", 6, AV_CH_LAYOUT_HEXAGONAL },
  81. { "6.1", 7, AV_CH_LAYOUT_6POINT1 },
  82. { "6.1", 7, AV_CH_LAYOUT_6POINT1_BACK },
  83. { "6.1(front)", 7, AV_CH_LAYOUT_6POINT1_FRONT },
  84. { "7.0", 7, AV_CH_LAYOUT_7POINT0 },
  85. { "7.0(front)", 7, AV_CH_LAYOUT_7POINT0_FRONT },
  86. { "7.1", 8, AV_CH_LAYOUT_7POINT1 },
  87. { "7.1(wide)", 8, AV_CH_LAYOUT_7POINT1_WIDE },
  88. { "octagonal", 8, AV_CH_LAYOUT_OCTAGONAL },
  89. { "downmix", 2, AV_CH_LAYOUT_STEREO_DOWNMIX, },
  90. };
  91. static uint64_t get_channel_layout_single(const char *name, int name_len)
  92. {
  93. int i;
  94. char *end;
  95. int64_t layout;
  96. for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map); i++) {
  97. if (strlen(channel_layout_map[i].name) == name_len &&
  98. !memcmp(channel_layout_map[i].name, name, name_len))
  99. return channel_layout_map[i].layout;
  100. }
  101. for (i = 0; i < FF_ARRAY_ELEMS(channel_names); i++)
  102. if (channel_names[i] &&
  103. strlen(channel_names[i]) == name_len &&
  104. !memcmp(channel_names[i], name, name_len))
  105. return (int64_t)1 << i;
  106. i = strtol(name, &end, 10);
  107. if (end - name == name_len ||
  108. (end + 1 - name == name_len && *end == 'c'))
  109. return av_get_default_channel_layout(i);
  110. layout = strtoll(name, &end, 0);
  111. if (end - name == name_len)
  112. return FFMAX(layout, 0);
  113. return 0;
  114. }
  115. uint64_t av_get_channel_layout(const char *name)
  116. {
  117. const char *n, *e;
  118. const char *name_end = name + strlen(name);
  119. int64_t layout = 0, layout_single;
  120. for (n = name; n < name_end; n = e + 1) {
  121. for (e = n; e < name_end && *e != '+' && *e != '|'; e++);
  122. layout_single = get_channel_layout_single(n, e - n);
  123. if (!layout_single)
  124. return 0;
  125. layout |= layout_single;
  126. }
  127. return layout;
  128. }
  129. void av_get_channel_layout_string(char *buf, int buf_size,
  130. int nb_channels, uint64_t channel_layout)
  131. {
  132. int i;
  133. if (nb_channels <= 0)
  134. nb_channels = av_get_channel_layout_nb_channels(channel_layout);
  135. for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map); i++)
  136. if (nb_channels == channel_layout_map[i].nb_channels &&
  137. channel_layout == channel_layout_map[i].layout) {
  138. av_strlcpy(buf, channel_layout_map[i].name, buf_size);
  139. return;
  140. }
  141. snprintf(buf, buf_size, "%d channels", nb_channels);
  142. if (channel_layout) {
  143. int i, ch;
  144. av_strlcat(buf, " (", buf_size);
  145. for (i = 0, ch = 0; i < 64; i++) {
  146. if ((channel_layout & (UINT64_C(1) << i))) {
  147. const char *name = get_channel_name(i);
  148. if (name) {
  149. if (ch > 0)
  150. av_strlcat(buf, "+", buf_size);
  151. av_strlcat(buf, name, buf_size);
  152. }
  153. ch++;
  154. }
  155. }
  156. av_strlcat(buf, ")", buf_size);
  157. }
  158. }
  159. int av_get_channel_layout_nb_channels(uint64_t channel_layout)
  160. {
  161. int count;
  162. uint64_t x = channel_layout;
  163. for (count = 0; x; count++)
  164. x &= x-1; // unset lowest set bit
  165. return count;
  166. }
  167. int64_t av_get_default_channel_layout(int nb_channels) {
  168. int i;
  169. for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map); i++)
  170. if (nb_channels == channel_layout_map[i].nb_channels)
  171. return channel_layout_map[i].layout;
  172. return 0;
  173. }