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.

231 lines
7.6KB

  1. /*
  2. * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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. #include "common.h"
  28. static const char * const channel_names[] = {
  29. [0] = "FL", /* front left */
  30. [1] = "FR", /* front right */
  31. [2] = "FC", /* front center */
  32. [3] = "LFE", /* low frequency */
  33. [4] = "BL", /* back left */
  34. [5] = "BR", /* back right */
  35. [6] = "FLC", /* front left-of-center */
  36. [7] = "FRC", /* front right-of-center */
  37. [8] = "BC", /* back-center */
  38. [9] = "SL", /* side left */
  39. [10] = "SR", /* side right */
  40. [11] = "TC", /* top center */
  41. [12] = "TFL", /* top front left */
  42. [13] = "TFC", /* top front center */
  43. [14] = "TFR", /* top front right */
  44. [15] = "TBL", /* top back left */
  45. [16] = "TBC", /* top back center */
  46. [17] = "TBR", /* top back right */
  47. [29] = "DL", /* downmix left */
  48. [30] = "DR", /* downmix right */
  49. [31] = "WL", /* wide left */
  50. [32] = "WR", /* wide right */
  51. [33] = "SDL", /* surround direct left */
  52. [34] = "SDR", /* surround direct right */
  53. };
  54. static const char *get_channel_name(int channel_id)
  55. {
  56. if (channel_id < 0 || channel_id >= FF_ARRAY_ELEMS(channel_names))
  57. return NULL;
  58. return channel_names[channel_id];
  59. }
  60. static const struct {
  61. const char *name;
  62. int nb_channels;
  63. uint64_t layout;
  64. } channel_layout_map[] = {
  65. { "mono", 1, AV_CH_LAYOUT_MONO },
  66. { "stereo", 2, AV_CH_LAYOUT_STEREO },
  67. { "stereo", 2, AV_CH_LAYOUT_STEREO_DOWNMIX },
  68. { "2.1", 3, AV_CH_LAYOUT_2POINT1 },
  69. { "3.0", 3, AV_CH_LAYOUT_SURROUND },
  70. { "3.0(back)", 3, AV_CH_LAYOUT_2_1 },
  71. { "3.1", 4, AV_CH_LAYOUT_3POINT1 },
  72. { "4.0", 4, AV_CH_LAYOUT_4POINT0 },
  73. { "quad", 4, AV_CH_LAYOUT_QUAD },
  74. { "quad(side)", 4, AV_CH_LAYOUT_2_2 },
  75. { "4.1", 5, AV_CH_LAYOUT_4POINT1 },
  76. { "5.0", 5, AV_CH_LAYOUT_5POINT0 },
  77. { "5.0", 5, AV_CH_LAYOUT_5POINT0_BACK },
  78. { "5.1", 6, AV_CH_LAYOUT_5POINT1 },
  79. { "5.1", 6, AV_CH_LAYOUT_5POINT1_BACK },
  80. { "6.0", 6, AV_CH_LAYOUT_6POINT0 },
  81. { "6.0(front)", 6, AV_CH_LAYOUT_6POINT0_FRONT },
  82. { "hexagonal", 6, AV_CH_LAYOUT_HEXAGONAL },
  83. { "6.1", 7, AV_CH_LAYOUT_6POINT1 },
  84. { "6.1", 7, AV_CH_LAYOUT_6POINT1_BACK },
  85. { "6.1(front)", 7, AV_CH_LAYOUT_6POINT1_FRONT },
  86. { "7.0", 7, AV_CH_LAYOUT_7POINT0 },
  87. { "7.0(front)", 7, AV_CH_LAYOUT_7POINT0_FRONT },
  88. { "7.1", 8, AV_CH_LAYOUT_7POINT1 },
  89. { "7.1(wide)", 8, AV_CH_LAYOUT_7POINT1_WIDE },
  90. { "7.1(wide)", 8, AV_CH_LAYOUT_7POINT1_WIDE_BACK },
  91. { "octagonal", 8, AV_CH_LAYOUT_OCTAGONAL },
  92. { "downmix", 2, AV_CH_LAYOUT_STEREO_DOWNMIX, },
  93. { 0 }
  94. };
  95. static uint64_t get_channel_layout_single(const char *name, int name_len)
  96. {
  97. int i;
  98. char *end;
  99. int64_t layout;
  100. for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map) - 1; i++) {
  101. if (strlen(channel_layout_map[i].name) == name_len &&
  102. !memcmp(channel_layout_map[i].name, name, name_len))
  103. return channel_layout_map[i].layout;
  104. }
  105. for (i = 0; i < FF_ARRAY_ELEMS(channel_names); i++)
  106. if (channel_names[i] &&
  107. strlen(channel_names[i]) == name_len &&
  108. !memcmp(channel_names[i], name, name_len))
  109. return (int64_t)1 << i;
  110. i = strtol(name, &end, 10);
  111. if (end - name == name_len ||
  112. (end + 1 - name == name_len && *end == 'c'))
  113. return av_get_default_channel_layout(i);
  114. layout = strtoll(name, &end, 0);
  115. if (end - name == name_len)
  116. return FFMAX(layout, 0);
  117. return 0;
  118. }
  119. uint64_t av_get_channel_layout(const char *name)
  120. {
  121. const char *n, *e;
  122. const char *name_end = name + strlen(name);
  123. int64_t layout = 0, layout_single;
  124. for (n = name; n < name_end; n = e + 1) {
  125. for (e = n; e < name_end && *e != '+' && *e != '|'; e++);
  126. layout_single = get_channel_layout_single(n, e - n);
  127. if (!layout_single)
  128. return 0;
  129. layout |= layout_single;
  130. }
  131. return layout;
  132. }
  133. void av_get_channel_layout_string(char *buf, int buf_size,
  134. int nb_channels, uint64_t channel_layout)
  135. {
  136. int i;
  137. if (nb_channels <= 0)
  138. nb_channels = av_get_channel_layout_nb_channels(channel_layout);
  139. for (i = 0; channel_layout_map[i].name; i++)
  140. if (nb_channels == channel_layout_map[i].nb_channels &&
  141. channel_layout == channel_layout_map[i].layout) {
  142. av_strlcpy(buf, channel_layout_map[i].name, buf_size);
  143. return;
  144. }
  145. snprintf(buf, buf_size, "%d channels", nb_channels);
  146. if (channel_layout) {
  147. int i, ch;
  148. av_strlcat(buf, " (", buf_size);
  149. for (i = 0, ch = 0; i < 64; i++) {
  150. if ((channel_layout & (UINT64_C(1) << i))) {
  151. const char *name = get_channel_name(i);
  152. if (name) {
  153. if (ch > 0)
  154. av_strlcat(buf, "|", buf_size);
  155. av_strlcat(buf, name, buf_size);
  156. }
  157. ch++;
  158. }
  159. }
  160. av_strlcat(buf, ")", buf_size);
  161. }
  162. }
  163. int av_get_channel_layout_nb_channels(uint64_t channel_layout)
  164. {
  165. return av_popcount64(channel_layout);
  166. }
  167. uint64_t av_get_default_channel_layout(int nb_channels)
  168. {
  169. switch(nb_channels) {
  170. case 1: return AV_CH_LAYOUT_MONO;
  171. case 2: return AV_CH_LAYOUT_STEREO;
  172. case 3: return AV_CH_LAYOUT_SURROUND;
  173. case 4: return AV_CH_LAYOUT_QUAD;
  174. case 5: return AV_CH_LAYOUT_5POINT0;
  175. case 6: return AV_CH_LAYOUT_5POINT1;
  176. case 7: return AV_CH_LAYOUT_6POINT1;
  177. case 8: return AV_CH_LAYOUT_7POINT1;
  178. default: return 0;
  179. }
  180. }
  181. int av_get_channel_layout_channel_index(uint64_t channel_layout,
  182. uint64_t channel)
  183. {
  184. if (!(channel_layout & channel) ||
  185. av_get_channel_layout_nb_channels(channel) != 1)
  186. return AVERROR(EINVAL);
  187. channel_layout &= channel - 1;
  188. return av_get_channel_layout_nb_channels(channel_layout);
  189. }
  190. const char *av_get_channel_name(uint64_t channel)
  191. {
  192. int i;
  193. if (av_get_channel_layout_nb_channels(channel) != 1)
  194. return NULL;
  195. for (i = 0; i < 64; i++)
  196. if ((1ULL<<i) & channel)
  197. return get_channel_name(i);
  198. return NULL;
  199. }
  200. uint64_t av_channel_layout_extract_channel(uint64_t channel_layout, int index)
  201. {
  202. int i;
  203. if (av_get_channel_layout_nb_channels(channel_layout) <= index)
  204. return 0;
  205. for (i = 0; i < 64; i++) {
  206. if ((1ULL << i) & channel_layout && !index--)
  207. return 1ULL << i;
  208. }
  209. return 0;
  210. }