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.

232 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 channel layout utility functions
  23. */
  24. #include "avstring.h"
  25. #include "avutil.h"
  26. #include "channel_layout.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. [35] = "LFE2", /* low frequency 2 */
  54. };
  55. static const char *get_channel_name(int channel_id)
  56. {
  57. if (channel_id < 0 || channel_id >= FF_ARRAY_ELEMS(channel_names))
  58. return NULL;
  59. return channel_names[channel_id];
  60. }
  61. static const struct {
  62. const char *name;
  63. int nb_channels;
  64. uint64_t layout;
  65. } channel_layout_map[] = {
  66. { "mono", 1, AV_CH_LAYOUT_MONO },
  67. { "stereo", 2, AV_CH_LAYOUT_STEREO },
  68. { "stereo", 2, AV_CH_LAYOUT_STEREO_DOWNMIX },
  69. { "2.1", 3, AV_CH_LAYOUT_2POINT1 },
  70. { "3.0", 3, AV_CH_LAYOUT_SURROUND },
  71. { "3.0(back)", 3, AV_CH_LAYOUT_2_1 },
  72. { "3.1", 4, AV_CH_LAYOUT_3POINT1 },
  73. { "4.0", 4, AV_CH_LAYOUT_4POINT0 },
  74. { "quad", 4, AV_CH_LAYOUT_QUAD },
  75. { "quad(side)", 4, AV_CH_LAYOUT_2_2 },
  76. { "4.1", 5, AV_CH_LAYOUT_4POINT1 },
  77. { "5.0", 5, AV_CH_LAYOUT_5POINT0 },
  78. { "5.0", 5, AV_CH_LAYOUT_5POINT0_BACK },
  79. { "5.1", 6, AV_CH_LAYOUT_5POINT1 },
  80. { "5.1", 6, AV_CH_LAYOUT_5POINT1_BACK },
  81. { "6.0", 6, AV_CH_LAYOUT_6POINT0 },
  82. { "6.0(front)", 6, AV_CH_LAYOUT_6POINT0_FRONT },
  83. { "hexagonal", 6, AV_CH_LAYOUT_HEXAGONAL },
  84. { "6.1", 7, AV_CH_LAYOUT_6POINT1 },
  85. { "6.1", 7, AV_CH_LAYOUT_6POINT1_BACK },
  86. { "6.1(front)", 7, AV_CH_LAYOUT_6POINT1_FRONT },
  87. { "7.0", 7, AV_CH_LAYOUT_7POINT0 },
  88. { "7.0(front)", 7, AV_CH_LAYOUT_7POINT0_FRONT },
  89. { "7.1", 8, AV_CH_LAYOUT_7POINT1 },
  90. { "7.1(wide)", 8, AV_CH_LAYOUT_7POINT1_WIDE },
  91. { "7.1(wide)", 8, AV_CH_LAYOUT_7POINT1_WIDE_BACK },
  92. { "octagonal", 8, AV_CH_LAYOUT_OCTAGONAL },
  93. { "downmix", 2, AV_CH_LAYOUT_STEREO_DOWNMIX, },
  94. { 0 }
  95. };
  96. static uint64_t get_channel_layout_single(const char *name, int name_len)
  97. {
  98. int i;
  99. char *end;
  100. int64_t layout;
  101. for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map) - 1; i++) {
  102. if (strlen(channel_layout_map[i].name) == name_len &&
  103. !memcmp(channel_layout_map[i].name, name, name_len))
  104. return channel_layout_map[i].layout;
  105. }
  106. for (i = 0; i < FF_ARRAY_ELEMS(channel_names); i++)
  107. if (channel_names[i] &&
  108. strlen(channel_names[i]) == name_len &&
  109. !memcmp(channel_names[i], name, name_len))
  110. return (int64_t)1 << i;
  111. i = strtol(name, &end, 10);
  112. if (end - name == name_len ||
  113. (end + 1 - name == name_len && *end == 'c'))
  114. return av_get_default_channel_layout(i);
  115. layout = strtoll(name, &end, 0);
  116. if (end - name == name_len)
  117. return FFMAX(layout, 0);
  118. return 0;
  119. }
  120. uint64_t av_get_channel_layout(const char *name)
  121. {
  122. const char *n, *e;
  123. const char *name_end = name + strlen(name);
  124. int64_t layout = 0, layout_single;
  125. for (n = name; n < name_end; n = e + 1) {
  126. for (e = n; e < name_end && *e != '+' && *e != '|'; e++);
  127. layout_single = get_channel_layout_single(n, e - n);
  128. if (!layout_single)
  129. return 0;
  130. layout |= layout_single;
  131. }
  132. return layout;
  133. }
  134. void av_get_channel_layout_string(char *buf, int buf_size,
  135. int nb_channels, uint64_t channel_layout)
  136. {
  137. int i;
  138. if (nb_channels <= 0)
  139. nb_channels = av_get_channel_layout_nb_channels(channel_layout);
  140. for (i = 0; channel_layout_map[i].name; i++)
  141. if (nb_channels == channel_layout_map[i].nb_channels &&
  142. channel_layout == channel_layout_map[i].layout) {
  143. av_strlcpy(buf, channel_layout_map[i].name, buf_size);
  144. return;
  145. }
  146. snprintf(buf, buf_size, "%d channels", nb_channels);
  147. if (channel_layout) {
  148. int i, ch;
  149. av_strlcat(buf, " (", buf_size);
  150. for (i = 0, ch = 0; i < 64; i++) {
  151. if ((channel_layout & (UINT64_C(1) << i))) {
  152. const char *name = get_channel_name(i);
  153. if (name) {
  154. if (ch > 0)
  155. av_strlcat(buf, "|", buf_size);
  156. av_strlcat(buf, name, buf_size);
  157. }
  158. ch++;
  159. }
  160. }
  161. av_strlcat(buf, ")", buf_size);
  162. }
  163. }
  164. int av_get_channel_layout_nb_channels(uint64_t channel_layout)
  165. {
  166. return av_popcount64(channel_layout);
  167. }
  168. uint64_t av_get_default_channel_layout(int nb_channels)
  169. {
  170. switch(nb_channels) {
  171. case 1: return AV_CH_LAYOUT_MONO;
  172. case 2: return AV_CH_LAYOUT_STEREO;
  173. case 3: return AV_CH_LAYOUT_SURROUND;
  174. case 4: return AV_CH_LAYOUT_QUAD;
  175. case 5: return AV_CH_LAYOUT_5POINT0;
  176. case 6: return AV_CH_LAYOUT_5POINT1;
  177. case 7: return AV_CH_LAYOUT_6POINT1;
  178. case 8: return AV_CH_LAYOUT_7POINT1;
  179. default: return 0;
  180. }
  181. }
  182. int av_get_channel_layout_channel_index(uint64_t channel_layout,
  183. uint64_t channel)
  184. {
  185. if (!(channel_layout & channel) ||
  186. av_get_channel_layout_nb_channels(channel) != 1)
  187. return AVERROR(EINVAL);
  188. channel_layout &= channel - 1;
  189. return av_get_channel_layout_nb_channels(channel_layout);
  190. }
  191. const char *av_get_channel_name(uint64_t channel)
  192. {
  193. int i;
  194. if (av_get_channel_layout_nb_channels(channel) != 1)
  195. return NULL;
  196. for (i = 0; i < 64; i++)
  197. if ((1ULL<<i) & channel)
  198. return get_channel_name(i);
  199. return NULL;
  200. }
  201. uint64_t av_channel_layout_extract_channel(uint64_t channel_layout, int index)
  202. {
  203. int i;
  204. if (av_get_channel_layout_nb_channels(channel_layout) <= index)
  205. return 0;
  206. for (i = 0; i < 64; i++) {
  207. if ((1ULL << i) & channel_layout && !index--)
  208. return 1ULL << i;
  209. }
  210. return 0;
  211. }