channel_layout.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 },
  95. { "octagonal", 8, AV_CH_LAYOUT_OCTAGONAL },
  96. { "downmix", 2, AV_CH_LAYOUT_STEREO_DOWNMIX, },
  97. };
  98. static uint64_t get_channel_layout_single(const char *name, int name_len)
  99. {
  100. int i;
  101. char *end;
  102. int64_t layout;
  103. for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map); i++) {
  104. if (strlen(channel_layout_map[i].name) == name_len &&
  105. !memcmp(channel_layout_map[i].name, name, name_len))
  106. return channel_layout_map[i].layout;
  107. }
  108. for (i = 0; i < FF_ARRAY_ELEMS(channel_names); i++)
  109. if (channel_names[i].name &&
  110. strlen(channel_names[i].name) == name_len &&
  111. !memcmp(channel_names[i].name, name, name_len))
  112. return (int64_t)1 << i;
  113. i = strtol(name, &end, 10);
  114. if (end - name == name_len ||
  115. (end + 1 - name == name_len && *end == 'c'))
  116. return av_get_default_channel_layout(i);
  117. layout = strtoll(name, &end, 0);
  118. if (end - name == name_len)
  119. return FFMAX(layout, 0);
  120. return 0;
  121. }
  122. uint64_t av_get_channel_layout(const char *name)
  123. {
  124. const char *n, *e;
  125. const char *name_end = name + strlen(name);
  126. int64_t layout = 0, layout_single;
  127. for (n = name; n < name_end; n = e + 1) {
  128. for (e = n; e < name_end && *e != '+' && *e != '|'; e++);
  129. layout_single = get_channel_layout_single(n, e - n);
  130. if (!layout_single)
  131. return 0;
  132. layout |= layout_single;
  133. }
  134. return layout;
  135. }
  136. void av_bprint_channel_layout(struct AVBPrint *bp,
  137. int nb_channels, uint64_t channel_layout)
  138. {
  139. int i;
  140. if (nb_channels <= 0)
  141. nb_channels = av_get_channel_layout_nb_channels(channel_layout);
  142. for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map); i++)
  143. if (nb_channels == channel_layout_map[i].nb_channels &&
  144. channel_layout == channel_layout_map[i].layout) {
  145. av_bprintf(bp, "%s", channel_layout_map[i].name);
  146. return;
  147. }
  148. av_bprintf(bp, "%d channels", nb_channels);
  149. if (channel_layout) {
  150. int i, ch;
  151. av_bprintf(bp, " (");
  152. for (i = 0, ch = 0; i < 64; i++) {
  153. if ((channel_layout & (UINT64_C(1) << i))) {
  154. const char *name = get_channel_name(i);
  155. if (name) {
  156. if (ch > 0)
  157. av_bprintf(bp, "+");
  158. av_bprintf(bp, "%s", name);
  159. }
  160. ch++;
  161. }
  162. }
  163. av_bprintf(bp, ")");
  164. }
  165. }
  166. void av_get_channel_layout_string(char *buf, int buf_size,
  167. int nb_channels, uint64_t channel_layout)
  168. {
  169. AVBPrint bp;
  170. av_bprint_init_for_buffer(&bp, buf, buf_size);
  171. av_bprint_channel_layout(&bp, nb_channels, channel_layout);
  172. }
  173. int av_get_channel_layout_nb_channels(uint64_t channel_layout)
  174. {
  175. return av_popcount64(channel_layout);
  176. }
  177. int64_t av_get_default_channel_layout(int nb_channels) {
  178. int i;
  179. for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map); i++)
  180. if (nb_channels == channel_layout_map[i].nb_channels)
  181. return channel_layout_map[i].layout;
  182. return 0;
  183. }
  184. int av_get_channel_layout_channel_index(uint64_t channel_layout,
  185. uint64_t channel)
  186. {
  187. if (!(channel_layout & channel) ||
  188. av_get_channel_layout_nb_channels(channel) != 1)
  189. return AVERROR(EINVAL);
  190. channel_layout &= channel - 1;
  191. return av_get_channel_layout_nb_channels(channel_layout);
  192. }
  193. const char *av_get_channel_name(uint64_t channel)
  194. {
  195. int i;
  196. if (av_get_channel_layout_nb_channels(channel) != 1)
  197. return NULL;
  198. for (i = 0; i < 64; i++)
  199. if ((1ULL<<i) & channel)
  200. return get_channel_name(i);
  201. return NULL;
  202. }
  203. const char *av_get_channel_description(uint64_t channel)
  204. {
  205. int i;
  206. if (av_get_channel_layout_nb_channels(channel) != 1)
  207. return NULL;
  208. for (i = 0; i < FF_ARRAY_ELEMS(channel_names); i++)
  209. if ((1ULL<<i) & channel)
  210. return channel_names[i].description;
  211. return NULL;
  212. }
  213. uint64_t av_channel_layout_extract_channel(uint64_t channel_layout, int index)
  214. {
  215. int i;
  216. if (av_get_channel_layout_nb_channels(channel_layout) <= index)
  217. return 0;
  218. for (i = 0; i < 64; i++) {
  219. if ((1ULL << i) & channel_layout && !index--)
  220. return 1ULL << i;
  221. }
  222. return 0;
  223. }
  224. int av_get_standard_channel_layout(unsigned index, uint64_t *layout,
  225. const char **name)
  226. {
  227. if (index >= FF_ARRAY_ELEMS(channel_layout_map))
  228. return AVERROR_EOF;
  229. if (layout) *layout = channel_layout_map[index].layout;
  230. if (name) *name = channel_layout_map[index].name;
  231. return 0;
  232. }