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.

199 lines
6.7KB

  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. static const char * const channel_names[] = {
  29. [0] = "FL", /* front left */
  30. [1] = "FR", /* front right */
  31. [2] = "FC", /* front center */
  32. [3] = "LFE", /* low frequency */
  33. [4] = "BL", /* back left */
  34. [5] = "BR", /* back right */
  35. [6] = "FLC", /* front left-of-center */
  36. [7] = "FRC", /* front right-of-center */
  37. [8] = "BC", /* back-center */
  38. [9] = "SL", /* side left */
  39. [10] = "SR", /* side right */
  40. [11] = "TC", /* top center */
  41. [12] = "TFL", /* top front left */
  42. [13] = "TFC", /* top front center */
  43. [14] = "TFR", /* top front right */
  44. [15] = "TBL", /* top back left */
  45. [16] = "TBC", /* top back center */
  46. [17] = "TBR", /* top back right */
  47. [29] = "DL", /* downmix left */
  48. [30] = "DR", /* downmix right */
  49. [31] = "WL", /* wide left */
  50. [32] = "WR", /* wide right */
  51. [33] = "SDL", /* surround direct left */
  52. [34] = "SDR", /* surround direct right */
  53. };
  54. static const char *get_channel_name(int channel_id)
  55. {
  56. if (channel_id < 0 || channel_id >= FF_ARRAY_ELEMS(channel_names))
  57. return NULL;
  58. return channel_names[channel_id];
  59. }
  60. static const struct {
  61. const char *name;
  62. int nb_channels;
  63. uint64_t layout;
  64. } channel_layout_map[] = {
  65. { "mono", 1, AV_CH_LAYOUT_MONO },
  66. { "stereo", 2, AV_CH_LAYOUT_STEREO },
  67. { "2.1", 3, AV_CH_LAYOUT_2POINT1 },
  68. { "3.0", 3, AV_CH_LAYOUT_SURROUND },
  69. { "3.0(back)", 3, AV_CH_LAYOUT_2_1 },
  70. { "4.0", 4, AV_CH_LAYOUT_4POINT0 },
  71. { "quad", 4, AV_CH_LAYOUT_QUAD },
  72. { "quad(side)", 4, AV_CH_LAYOUT_2_2 },
  73. { "3.1", 4, AV_CH_LAYOUT_3POINT1 },
  74. { "5.0", 5, AV_CH_LAYOUT_5POINT0_BACK },
  75. { "5.0(side)", 5, AV_CH_LAYOUT_5POINT0 },
  76. { "4.1", 5, AV_CH_LAYOUT_4POINT1 },
  77. { "5.1", 6, AV_CH_LAYOUT_5POINT1_BACK },
  78. { "5.1(side)", 6, AV_CH_LAYOUT_5POINT1 },
  79. { "6.0", 6, AV_CH_LAYOUT_6POINT0 },
  80. { "6.0(front)", 6, AV_CH_LAYOUT_6POINT0_FRONT },
  81. { "hexagonal", 6, AV_CH_LAYOUT_HEXAGONAL },
  82. { "6.1", 7, AV_CH_LAYOUT_6POINT1 },
  83. { "6.1", 7, AV_CH_LAYOUT_6POINT1_BACK },
  84. { "6.1(front)", 7, AV_CH_LAYOUT_6POINT1_FRONT },
  85. { "7.0", 7, AV_CH_LAYOUT_7POINT0 },
  86. { "7.0(front)", 7, AV_CH_LAYOUT_7POINT0_FRONT },
  87. { "7.1", 8, AV_CH_LAYOUT_7POINT1 },
  88. { "7.1(wide)", 8, AV_CH_LAYOUT_7POINT1_WIDE },
  89. { "octagonal", 8, AV_CH_LAYOUT_OCTAGONAL },
  90. { "downmix", 2, AV_CH_LAYOUT_STEREO_DOWNMIX, },
  91. };
  92. static uint64_t get_channel_layout_single(const char *name, int name_len)
  93. {
  94. int i;
  95. char *end;
  96. int64_t layout;
  97. for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map); i++) {
  98. if (strlen(channel_layout_map[i].name) == name_len &&
  99. !memcmp(channel_layout_map[i].name, name, name_len))
  100. return channel_layout_map[i].layout;
  101. }
  102. for (i = 0; i < FF_ARRAY_ELEMS(channel_names); i++)
  103. if (channel_names[i] &&
  104. strlen(channel_names[i]) == name_len &&
  105. !memcmp(channel_names[i], name, name_len))
  106. return (int64_t)1 << i;
  107. i = strtol(name, &end, 10);
  108. if (end - name == name_len ||
  109. (end + 1 - name == name_len && *end == 'c'))
  110. return av_get_default_channel_layout(i);
  111. layout = strtoll(name, &end, 0);
  112. if (end - name == name_len)
  113. return FFMAX(layout, 0);
  114. return 0;
  115. }
  116. uint64_t av_get_channel_layout(const char *name)
  117. {
  118. const char *n, *e;
  119. const char *name_end = name + strlen(name);
  120. int64_t layout = 0, layout_single;
  121. for (n = name; n < name_end; n = e + 1) {
  122. for (e = n; e < name_end && *e != '+' && *e != '|'; e++);
  123. layout_single = get_channel_layout_single(n, e - n);
  124. if (!layout_single)
  125. return 0;
  126. layout |= layout_single;
  127. }
  128. return layout;
  129. }
  130. void av_bprint_channel_layout(struct AVBPrint *bp,
  131. int nb_channels, uint64_t channel_layout)
  132. {
  133. int i;
  134. if (nb_channels <= 0)
  135. nb_channels = av_get_channel_layout_nb_channels(channel_layout);
  136. for (i = 0; i < FF_ARRAY_ELEMS(channel_layout_map); i++)
  137. if (nb_channels == channel_layout_map[i].nb_channels &&
  138. channel_layout == channel_layout_map[i].layout) {
  139. av_bprintf(bp, "%s", channel_layout_map[i].name);
  140. return;
  141. }
  142. av_bprintf(bp, "%d channels", nb_channels);
  143. if (channel_layout) {
  144. int i, ch;
  145. av_bprintf(bp, " (");
  146. for (i = 0, ch = 0; i < 64; i++) {
  147. if ((channel_layout & (UINT64_C(1) << i))) {
  148. const char *name = get_channel_name(i);
  149. if (name) {
  150. if (ch > 0)
  151. av_bprintf(bp, "+");
  152. av_bprintf(bp, "%s", name);
  153. }
  154. ch++;
  155. }
  156. }
  157. av_bprintf(bp, ")");
  158. }
  159. }
  160. void av_get_channel_layout_string(char *buf, int buf_size,
  161. int nb_channels, uint64_t channel_layout)
  162. {
  163. AVBPrint bp;
  164. av_bprint_init_for_buffer(&bp, buf, buf_size);
  165. av_bprint_channel_layout(&bp, nb_channels, channel_layout);
  166. }
  167. int av_get_channel_layout_nb_channels(uint64_t channel_layout)
  168. {
  169. int count;
  170. uint64_t x = channel_layout;
  171. for (count = 0; x; count++)
  172. x &= x-1; // unset lowest set bit
  173. return count;
  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. }