channel_layout.c 8.6 KB

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