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.

322 lines
11KB

  1. /*
  2. * DCA ExSS extension
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/common.h"
  21. #include "libavutil/log.h"
  22. #include "dca.h"
  23. #include "get_bits.h"
  24. /* extensions that reside in core substream */
  25. #define DCA_CORE_EXTS (DCA_EXT_XCH | DCA_EXT_XXCH | DCA_EXT_X96)
  26. /* these are unconfirmed but should be mostly correct */
  27. enum DCAExSSSpeakerMask {
  28. DCA_EXSS_FRONT_CENTER = 0x0001,
  29. DCA_EXSS_FRONT_LEFT_RIGHT = 0x0002,
  30. DCA_EXSS_SIDE_REAR_LEFT_RIGHT = 0x0004,
  31. DCA_EXSS_LFE = 0x0008,
  32. DCA_EXSS_REAR_CENTER = 0x0010,
  33. DCA_EXSS_FRONT_HIGH_LEFT_RIGHT = 0x0020,
  34. DCA_EXSS_REAR_LEFT_RIGHT = 0x0040,
  35. DCA_EXSS_FRONT_HIGH_CENTER = 0x0080,
  36. DCA_EXSS_OVERHEAD = 0x0100,
  37. DCA_EXSS_CENTER_LEFT_RIGHT = 0x0200,
  38. DCA_EXSS_WIDE_LEFT_RIGHT = 0x0400,
  39. DCA_EXSS_SIDE_LEFT_RIGHT = 0x0800,
  40. DCA_EXSS_LFE2 = 0x1000,
  41. DCA_EXSS_SIDE_HIGH_LEFT_RIGHT = 0x2000,
  42. DCA_EXSS_REAR_HIGH_CENTER = 0x4000,
  43. DCA_EXSS_REAR_HIGH_LEFT_RIGHT = 0x8000,
  44. };
  45. /**
  46. * Return the number of channels in an ExSS speaker mask (HD)
  47. */
  48. static int dca_exss_mask2count(int mask)
  49. {
  50. /* count bits that mean speaker pairs twice */
  51. return av_popcount(mask) +
  52. av_popcount(mask & (DCA_EXSS_CENTER_LEFT_RIGHT |
  53. DCA_EXSS_FRONT_LEFT_RIGHT |
  54. DCA_EXSS_FRONT_HIGH_LEFT_RIGHT |
  55. DCA_EXSS_WIDE_LEFT_RIGHT |
  56. DCA_EXSS_SIDE_LEFT_RIGHT |
  57. DCA_EXSS_SIDE_HIGH_LEFT_RIGHT |
  58. DCA_EXSS_SIDE_REAR_LEFT_RIGHT |
  59. DCA_EXSS_REAR_LEFT_RIGHT |
  60. DCA_EXSS_REAR_HIGH_LEFT_RIGHT));
  61. }
  62. /**
  63. * Skip mixing coefficients of a single mix out configuration (HD)
  64. */
  65. static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
  66. {
  67. int i;
  68. for (i = 0; i < channels; i++) {
  69. int mix_map_mask = get_bits(gb, out_ch);
  70. int num_coeffs = av_popcount(mix_map_mask);
  71. skip_bits_long(gb, num_coeffs * 6);
  72. }
  73. }
  74. /**
  75. * Parse extension substream asset header (HD)
  76. */
  77. static int dca_exss_parse_asset_header(DCAContext *s)
  78. {
  79. int header_pos = get_bits_count(&s->gb);
  80. int header_size;
  81. int channels = 0;
  82. int embedded_stereo = 0;
  83. int embedded_6ch = 0;
  84. int drc_code_present;
  85. int extensions_mask = 0;
  86. int i, j;
  87. if (get_bits_left(&s->gb) < 16)
  88. return AVERROR_INVALIDDATA;
  89. /* We will parse just enough to get to the extensions bitmask with which
  90. * we can set the profile value. */
  91. header_size = get_bits(&s->gb, 9) + 1;
  92. skip_bits(&s->gb, 3); // asset index
  93. if (s->static_fields) {
  94. if (get_bits1(&s->gb))
  95. skip_bits(&s->gb, 4); // asset type descriptor
  96. if (get_bits1(&s->gb))
  97. skip_bits_long(&s->gb, 24); // language descriptor
  98. if (get_bits1(&s->gb)) {
  99. /* How can one fit 1024 bytes of text here if the maximum value
  100. * for the asset header size field above was 512 bytes? */
  101. int text_length = get_bits(&s->gb, 10) + 1;
  102. if (get_bits_left(&s->gb) < text_length * 8)
  103. return AVERROR_INVALIDDATA;
  104. skip_bits_long(&s->gb, text_length * 8); // info text
  105. }
  106. skip_bits(&s->gb, 5); // bit resolution - 1
  107. skip_bits(&s->gb, 4); // max sample rate code
  108. channels = get_bits(&s->gb, 8) + 1;
  109. if (get_bits1(&s->gb)) { // 1-to-1 channels to speakers
  110. int spkr_remap_sets;
  111. int spkr_mask_size = 16;
  112. int num_spkrs[7];
  113. if (channels > 2)
  114. embedded_stereo = get_bits1(&s->gb);
  115. if (channels > 6)
  116. embedded_6ch = get_bits1(&s->gb);
  117. if (get_bits1(&s->gb)) {
  118. spkr_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
  119. skip_bits(&s->gb, spkr_mask_size); // spkr activity mask
  120. }
  121. spkr_remap_sets = get_bits(&s->gb, 3);
  122. for (i = 0; i < spkr_remap_sets; i++) {
  123. /* std layout mask for each remap set */
  124. num_spkrs[i] = dca_exss_mask2count(get_bits(&s->gb, spkr_mask_size));
  125. }
  126. for (i = 0; i < spkr_remap_sets; i++) {
  127. int num_dec_ch_remaps = get_bits(&s->gb, 5) + 1;
  128. if (get_bits_left(&s->gb) < 0)
  129. return AVERROR_INVALIDDATA;
  130. for (j = 0; j < num_spkrs[i]; j++) {
  131. int remap_dec_ch_mask = get_bits_long(&s->gb, num_dec_ch_remaps);
  132. int num_dec_ch = av_popcount(remap_dec_ch_mask);
  133. skip_bits_long(&s->gb, num_dec_ch * 5); // remap codes
  134. }
  135. }
  136. } else {
  137. skip_bits(&s->gb, 3); // representation type
  138. }
  139. }
  140. drc_code_present = get_bits1(&s->gb);
  141. if (drc_code_present)
  142. get_bits(&s->gb, 8); // drc code
  143. if (get_bits1(&s->gb))
  144. skip_bits(&s->gb, 5); // dialog normalization code
  145. if (drc_code_present && embedded_stereo)
  146. get_bits(&s->gb, 8); // drc stereo code
  147. if (s->mix_metadata && get_bits1(&s->gb)) {
  148. skip_bits(&s->gb, 1); // external mix
  149. skip_bits(&s->gb, 6); // post mix gain code
  150. if (get_bits(&s->gb, 2) != 3) // mixer drc code
  151. skip_bits(&s->gb, 3); // drc limit
  152. else
  153. skip_bits(&s->gb, 8); // custom drc code
  154. if (get_bits1(&s->gb)) // channel specific scaling
  155. for (i = 0; i < s->num_mix_configs; i++)
  156. skip_bits_long(&s->gb, s->mix_config_num_ch[i] * 6); // scale codes
  157. else
  158. skip_bits_long(&s->gb, s->num_mix_configs * 6); // scale codes
  159. for (i = 0; i < s->num_mix_configs; i++) {
  160. if (get_bits_left(&s->gb) < 0)
  161. return AVERROR_INVALIDDATA;
  162. dca_exss_skip_mix_coeffs(&s->gb, channels, s->mix_config_num_ch[i]);
  163. if (embedded_6ch)
  164. dca_exss_skip_mix_coeffs(&s->gb, 6, s->mix_config_num_ch[i]);
  165. if (embedded_stereo)
  166. dca_exss_skip_mix_coeffs(&s->gb, 2, s->mix_config_num_ch[i]);
  167. }
  168. }
  169. switch (get_bits(&s->gb, 2)) {
  170. case 0:
  171. extensions_mask = get_bits(&s->gb, 12);
  172. break;
  173. case 1:
  174. extensions_mask = DCA_EXT_EXSS_XLL;
  175. break;
  176. case 2:
  177. extensions_mask = DCA_EXT_EXSS_LBR;
  178. break;
  179. case 3:
  180. extensions_mask = 0; /* aux coding */
  181. break;
  182. }
  183. /* not parsed further, we were only interested in the extensions mask */
  184. if (get_bits_left(&s->gb) < 0)
  185. return AVERROR_INVALIDDATA;
  186. if (get_bits_count(&s->gb) - header_pos > header_size * 8) {
  187. av_log(s->avctx, AV_LOG_WARNING, "Asset header size mismatch.\n");
  188. return AVERROR_INVALIDDATA;
  189. }
  190. skip_bits_long(&s->gb, header_pos + header_size * 8 - get_bits_count(&s->gb));
  191. if (extensions_mask & DCA_EXT_EXSS_XLL)
  192. s->profile = FF_PROFILE_DTS_HD_MA;
  193. else if (extensions_mask & (DCA_EXT_EXSS_XBR | DCA_EXT_EXSS_X96 |
  194. DCA_EXT_EXSS_XXCH))
  195. s->profile = FF_PROFILE_DTS_HD_HRA;
  196. if (!(extensions_mask & DCA_EXT_CORE))
  197. av_log(s->avctx, AV_LOG_WARNING, "DTS core detection mismatch.\n");
  198. if ((extensions_mask & DCA_CORE_EXTS) != s->core_ext_mask)
  199. av_log(s->avctx, AV_LOG_WARNING,
  200. "DTS extensions detection mismatch (%d, %d)\n",
  201. extensions_mask & DCA_CORE_EXTS, s->core_ext_mask);
  202. return 0;
  203. }
  204. /**
  205. * Parse extension substream header (HD)
  206. */
  207. void ff_dca_exss_parse_header(DCAContext *s)
  208. {
  209. int ss_index;
  210. int blownup;
  211. int num_audiop = 1;
  212. int num_assets = 1;
  213. int active_ss_mask[8];
  214. int i, j;
  215. if (get_bits_left(&s->gb) < 52)
  216. return;
  217. skip_bits(&s->gb, 8); // user data
  218. ss_index = get_bits(&s->gb, 2);
  219. blownup = get_bits1(&s->gb);
  220. skip_bits(&s->gb, 8 + 4 * blownup); // header_size
  221. skip_bits(&s->gb, 16 + 4 * blownup); // hd_size
  222. s->static_fields = get_bits1(&s->gb);
  223. if (s->static_fields) {
  224. skip_bits(&s->gb, 2); // reference clock code
  225. skip_bits(&s->gb, 3); // frame duration code
  226. if (get_bits1(&s->gb))
  227. skip_bits_long(&s->gb, 36); // timestamp
  228. /* a single stream can contain multiple audio assets that can be
  229. * combined to form multiple audio presentations */
  230. num_audiop = get_bits(&s->gb, 3) + 1;
  231. if (num_audiop > 1) {
  232. avpriv_request_sample(s->avctx,
  233. "Multiple DTS-HD audio presentations");
  234. /* ignore such streams for now */
  235. return;
  236. }
  237. num_assets = get_bits(&s->gb, 3) + 1;
  238. if (num_assets > 1) {
  239. avpriv_request_sample(s->avctx, "Multiple DTS-HD audio assets");
  240. /* ignore such streams for now */
  241. return;
  242. }
  243. for (i = 0; i < num_audiop; i++)
  244. active_ss_mask[i] = get_bits(&s->gb, ss_index + 1);
  245. for (i = 0; i < num_audiop; i++)
  246. for (j = 0; j <= ss_index; j++)
  247. if (active_ss_mask[i] & (1 << j))
  248. skip_bits(&s->gb, 8); // active asset mask
  249. s->mix_metadata = get_bits1(&s->gb);
  250. if (s->mix_metadata) {
  251. int mix_out_mask_size;
  252. skip_bits(&s->gb, 2); // adjustment level
  253. mix_out_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
  254. s->num_mix_configs = get_bits(&s->gb, 2) + 1;
  255. for (i = 0; i < s->num_mix_configs; i++) {
  256. int mix_out_mask = get_bits(&s->gb, mix_out_mask_size);
  257. s->mix_config_num_ch[i] = dca_exss_mask2count(mix_out_mask);
  258. }
  259. }
  260. }
  261. for (i = 0; i < num_assets; i++)
  262. skip_bits_long(&s->gb, 16 + 4 * blownup); // asset size
  263. for (i = 0; i < num_assets; i++) {
  264. if (dca_exss_parse_asset_header(s))
  265. return;
  266. }
  267. /* not parsed further, we were only interested in the extensions mask
  268. * from the asset header */
  269. }