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.

257 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. #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. };
  59. static const char *get_channel_name(int channel_id)
  60. {
  61. if (channel_id < 0 || channel_id >= FF_ARRAY_ELEMS(channel_names))
  62. return NULL;
  63. return channel_names[channel_id].name;
  64. }
  65. static const struct {
  66. const char *name;
  67. int nb_channels;
  68. uint64_t layout;
  69. } channel_layout_map[] = {
  70. { "mono", 1, AV_CH_LAYOUT_MONO },
  71. { "stereo", 2, AV_CH_LAYOUT_STEREO },
  72. { "2.1", 3, AV_CH_LAYOUT_2POINT1 },
  73. { "3.0", 3, AV_CH_LAYOUT_SURROUND },
  74. { "3.0(back)", 3, AV_CH_LAYOUT_2_1 },
  75. { "4.0", 4, AV_CH_LAYOUT_4POINT0 },
  76. { "quad", 4, AV_CH_LAYOUT_QUAD },
  77. { "quad(side)", 4, AV_CH_LAYOUT_2_2 },
  78. { "3.1", 4, AV_CH_LAYOUT_3POINT1 },
  79. { "5.0", 5, AV_CH_LAYOUT_5POINT0_BACK },
  80. { "5.0(side)", 5, AV_CH_LAYOUT_5POINT0 },
  81. { "4.1", 5, AV_CH_LAYOUT_4POINT1 },
  82. { "5.1", 6, AV_CH_LAYOUT_5POINT1_BACK },
  83. { "5.1(side)", 6, AV_CH_LAYOUT_5POINT1 },
  84. { "6.0", 6, AV_CH_LAYOUT_6POINT0 },
  85. { "6.0(front)", 6, AV_CH_LAYOUT_6POINT0_FRONT },
  86. { "hexagonal", 6, AV_CH_LAYOUT_HEXAGONAL },
  87. { "6.1", 7, AV_CH_LAYOUT_6POINT1 },
  88. { "6.1", 7, AV_CH_LAYOUT_6POINT1_BACK },
  89. { "6.1(front)", 7, AV_CH_LAYOUT_6POINT1_FRONT },
  90. { "7.0", 7, AV_CH_LAYOUT_7POINT0 },
  91. { "7.0(front)", 7, AV_CH_LAYOUT_7POINT0_FRONT },
  92. { "7.1", 8, AV_CH_LAYOUT_7POINT1 },
  93. { "7.1(wide)", 8, AV_CH_LAYOUT_7POINT1_WIDE },
  94. { "octagonal", 8, AV_CH_LAYOUT_OCTAGONAL },
  95. { "downmix", 2, AV_CH_LAYOUT_STEREO_DOWNMIX, },
  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); 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].name &&
  109. strlen(channel_names[i].name) == name_len &&
  110. !memcmp(channel_names[i].name, 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_bprint_channel_layout(struct AVBPrint *bp,
  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; i < FF_ARRAY_ELEMS(channel_layout_map); i++)
  142. if (nb_channels == channel_layout_map[i].nb_channels &&
  143. channel_layout == channel_layout_map[i].layout) {
  144. av_bprintf(bp, "%s", channel_layout_map[i].name);
  145. return;
  146. }
  147. av_bprintf(bp, "%d channels", nb_channels);
  148. if (channel_layout) {
  149. int i, ch;
  150. av_bprintf(bp, " (");
  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_bprintf(bp, "+");
  157. av_bprintf(bp, "%s", name);
  158. }
  159. ch++;
  160. }
  161. }
  162. av_bprintf(bp, ")");
  163. }
  164. }
  165. void av_get_channel_layout_string(char *buf, int buf_size,
  166. int nb_channels, uint64_t channel_layout)
  167. {
  168. AVBPrint bp;
  169. av_bprint_init_for_buffer(&bp, buf, buf_size);
  170. av_bprint_channel_layout(&bp, nb_channels, channel_layout);
  171. }
  172. int av_get_channel_layout_nb_channels(uint64_t channel_layout)
  173. {
  174. return av_popcount64(channel_layout);
  175. }
  176. int64_t av_get_default_channel_layout(int nb_channels) {
  177. int i;
  178. for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map); i++)
  179. if (nb_channels == channel_layout_map[i].nb_channels)
  180. return channel_layout_map[i].layout;
  181. return 0;
  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. const char *av_get_channel_description(uint64_t channel)
  203. {
  204. int i;
  205. if (av_get_channel_layout_nb_channels(channel) != 1)
  206. return NULL;
  207. for (i = 0; i < FF_ARRAY_ELEMS(channel_names); i++)
  208. if ((1ULL<<i) & channel)
  209. return channel_names[i].description;
  210. return NULL;
  211. }
  212. uint64_t av_channel_layout_extract_channel(uint64_t channel_layout, int index)
  213. {
  214. int i;
  215. if (av_get_channel_layout_nb_channels(channel_layout) <= index)
  216. return 0;
  217. for (i = 0; i < 64; i++) {
  218. if ((1ULL << i) & channel_layout && !index--)
  219. return 1ULL << i;
  220. }
  221. return 0;
  222. }
  223. int av_get_standard_channel_layout(unsigned index, uint64_t *layout,
  224. const char **name)
  225. {
  226. if (index >= FF_ARRAY_ELEMS(channel_layout_map))
  227. return AVERROR_EOF;
  228. if (layout) *layout = channel_layout_map[index].layout;
  229. if (name) *name = channel_layout_map[index].name;
  230. return 0;
  231. }