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.

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