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.

256 lines
8.4KB

  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 conversion routines
  23. */
  24. #include "avstring.h"
  25. #include "avutil.h"
  26. #include "audioconvert.h"
  27. #include "bprint.h"
  28. struct channel_name {
  29. const char *name;
  30. const char *description;
  31. };
  32. static const struct channel_name channel_names[] = {
  33. [0] = { "FL", "front left" },
  34. [1] = { "FR", "front right" },
  35. [2] = { "FC", "front center" },
  36. [3] = { "LFE", "low frequency" },
  37. [4] = { "BL", "back left" },
  38. [5] = { "BR", "back right" },
  39. [6] = { "FLC", "front left-of-center" },
  40. [7] = { "FRC", "front right-of-center" },
  41. [8] = { "BC", "back center" },
  42. [9] = { "SL", "side left" },
  43. [10] = { "SR", "side right" },
  44. [11] = { "TC", "top center" },
  45. [12] = { "TFL", "top front left" },
  46. [13] = { "TFC", "top front center" },
  47. [14] = { "TFR", "top front right" },
  48. [15] = { "TBL", "top back left" },
  49. [16] = { "TBC", "top back center" },
  50. [17] = { "TBR", "top back right" },
  51. [29] = { "DL", "downmix left" },
  52. [30] = { "DR", "downmix right" },
  53. [31] = { "WL", "wide left" },
  54. [32] = { "WR", "wide right" },
  55. [33] = { "SDL", "surround direct left" },
  56. [34] = { "SDR", "surround direct right" },
  57. };
  58. static const char *get_channel_name(int channel_id)
  59. {
  60. if (channel_id < 0 || channel_id >= FF_ARRAY_ELEMS(channel_names))
  61. return NULL;
  62. return channel_names[channel_id].name;
  63. }
  64. static const struct {
  65. const char *name;
  66. int nb_channels;
  67. uint64_t layout;
  68. } channel_layout_map[] = {
  69. { "mono", 1, AV_CH_LAYOUT_MONO },
  70. { "stereo", 2, AV_CH_LAYOUT_STEREO },
  71. { "2.1", 3, AV_CH_LAYOUT_2POINT1 },
  72. { "3.0", 3, AV_CH_LAYOUT_SURROUND },
  73. { "3.0(back)", 3, AV_CH_LAYOUT_2_1 },
  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. { "3.1", 4, AV_CH_LAYOUT_3POINT1 },
  78. { "5.0", 5, AV_CH_LAYOUT_5POINT0_BACK },
  79. { "5.0(side)", 5, AV_CH_LAYOUT_5POINT0 },
  80. { "4.1", 5, AV_CH_LAYOUT_4POINT1 },
  81. { "5.1", 6, AV_CH_LAYOUT_5POINT1_BACK },
  82. { "5.1(side)", 6, AV_CH_LAYOUT_5POINT1 },
  83. { "6.0", 6, AV_CH_LAYOUT_6POINT0 },
  84. { "6.0(front)", 6, AV_CH_LAYOUT_6POINT0_FRONT },
  85. { "hexagonal", 6, AV_CH_LAYOUT_HEXAGONAL },
  86. { "6.1", 7, AV_CH_LAYOUT_6POINT1 },
  87. { "6.1", 7, AV_CH_LAYOUT_6POINT1_BACK },
  88. { "6.1(front)", 7, AV_CH_LAYOUT_6POINT1_FRONT },
  89. { "7.0", 7, AV_CH_LAYOUT_7POINT0 },
  90. { "7.0(front)", 7, AV_CH_LAYOUT_7POINT0_FRONT },
  91. { "7.1", 8, AV_CH_LAYOUT_7POINT1 },
  92. { "7.1(wide)", 8, AV_CH_LAYOUT_7POINT1_WIDE },
  93. { "octagonal", 8, AV_CH_LAYOUT_OCTAGONAL },
  94. { "downmix", 2, AV_CH_LAYOUT_STEREO_DOWNMIX, },
  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); 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].name &&
  108. strlen(channel_names[i].name) == name_len &&
  109. !memcmp(channel_names[i].name, 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_bprint_channel_layout(struct AVBPrint *bp,
  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; i < FF_ARRAY_ELEMS(channel_layout_map); i++)
  141. if (nb_channels == channel_layout_map[i].nb_channels &&
  142. channel_layout == channel_layout_map[i].layout) {
  143. av_bprintf(bp, "%s", channel_layout_map[i].name);
  144. return;
  145. }
  146. av_bprintf(bp, "%d channels", nb_channels);
  147. if (channel_layout) {
  148. int i, ch;
  149. av_bprintf(bp, " (");
  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_bprintf(bp, "+");
  156. av_bprintf(bp, "%s", name);
  157. }
  158. ch++;
  159. }
  160. }
  161. av_bprintf(bp, ")");
  162. }
  163. }
  164. void av_get_channel_layout_string(char *buf, int buf_size,
  165. int nb_channels, uint64_t channel_layout)
  166. {
  167. AVBPrint bp;
  168. av_bprint_init_for_buffer(&bp, buf, buf_size);
  169. av_bprint_channel_layout(&bp, nb_channels, channel_layout);
  170. }
  171. int av_get_channel_layout_nb_channels(uint64_t channel_layout)
  172. {
  173. return av_popcount64(channel_layout);
  174. }
  175. int64_t av_get_default_channel_layout(int nb_channels) {
  176. int i;
  177. for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map); i++)
  178. if (nb_channels == channel_layout_map[i].nb_channels)
  179. return channel_layout_map[i].layout;
  180. return 0;
  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. const char *av_get_channel_description(uint64_t channel)
  202. {
  203. int i;
  204. if (av_get_channel_layout_nb_channels(channel) != 1)
  205. return NULL;
  206. for (i = 0; i < FF_ARRAY_ELEMS(channel_names); i++)
  207. if ((1ULL<<i) & channel)
  208. return channel_names[i].description;
  209. return NULL;
  210. }
  211. uint64_t av_channel_layout_extract_channel(uint64_t channel_layout, int index)
  212. {
  213. int i;
  214. if (av_get_channel_layout_nb_channels(channel_layout) <= index)
  215. return 0;
  216. for (i = 0; i < 64; i++) {
  217. if ((1ULL << i) & channel_layout && !index--)
  218. return 1ULL << i;
  219. }
  220. return 0;
  221. }
  222. int av_get_standard_channel_layout(unsigned index, uint64_t *layout,
  223. const char **name)
  224. {
  225. if (index >= FF_ARRAY_ELEMS(channel_layout_map))
  226. return AVERROR_EOF;
  227. if (layout) *layout = channel_layout_map[index].layout;
  228. if (name) *name = channel_layout_map[index].name;
  229. return 0;
  230. }