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.

509 lines
16KB

  1. /*
  2. * Copyright (C) 2016 foo86
  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. #include "dcadec.h"
  21. #include "dcadata.h"
  22. static void parse_xll_parameters(DCAExssParser *s, DCAExssAsset *asset)
  23. {
  24. // Size of XLL data in extension substream
  25. asset->xll_size = get_bits(&s->gb, s->exss_size_nbits) + 1;
  26. // XLL sync word present flag
  27. if (asset->xll_sync_present = get_bits1(&s->gb)) {
  28. int xll_delay_nbits;
  29. // Peak bit rate smoothing buffer size
  30. skip_bits(&s->gb, 4);
  31. // Number of bits for XLL decoding delay
  32. xll_delay_nbits = get_bits(&s->gb, 5) + 1;
  33. // Initial XLL decoding delay in frames
  34. asset->xll_delay_nframes = get_bits_long(&s->gb, xll_delay_nbits);
  35. // Number of bytes offset to XLL sync
  36. asset->xll_sync_offset = get_bits(&s->gb, s->exss_size_nbits);
  37. } else {
  38. asset->xll_delay_nframes = 0;
  39. asset->xll_sync_offset = 0;
  40. }
  41. }
  42. static void parse_lbr_parameters(DCAExssParser *s, DCAExssAsset *asset)
  43. {
  44. // Size of LBR component in extension substream
  45. asset->lbr_size = get_bits(&s->gb, 14) + 1;
  46. // LBR sync word present flag
  47. if (get_bits1(&s->gb))
  48. // LBR sync distance
  49. skip_bits(&s->gb, 2);
  50. }
  51. static int parse_descriptor(DCAExssParser *s, DCAExssAsset *asset)
  52. {
  53. int i, j, drc_present, descr_size, descr_pos = get_bits_count(&s->gb);
  54. // Size of audio asset descriptor in bytes
  55. descr_size = get_bits(&s->gb, 9) + 1;
  56. // Audio asset identifier
  57. asset->asset_index = get_bits(&s->gb, 3);
  58. //
  59. // Per stream static metadata
  60. //
  61. if (s->static_fields_present) {
  62. // Asset type descriptor presence
  63. if (get_bits1(&s->gb))
  64. // Asset type descriptor
  65. skip_bits(&s->gb, 4);
  66. // Language descriptor presence
  67. if (get_bits1(&s->gb))
  68. // Language descriptor
  69. skip_bits(&s->gb, 24);
  70. // Additional textual information presence
  71. if (get_bits1(&s->gb)) {
  72. // Byte size of additional text info
  73. int text_size = get_bits(&s->gb, 10) + 1;
  74. // Sanity check available size
  75. if (get_bits_left(&s->gb) < text_size * 8)
  76. return AVERROR_INVALIDDATA;
  77. // Additional textual information string
  78. skip_bits_long(&s->gb, text_size * 8);
  79. }
  80. // PCM bit resolution
  81. asset->pcm_bit_res = get_bits(&s->gb, 5) + 1;
  82. // Maximum sample rate
  83. asset->max_sample_rate = ff_dca_sampling_freqs[get_bits(&s->gb, 4)];
  84. // Total number of channels
  85. asset->nchannels_total = get_bits(&s->gb, 8) + 1;
  86. // One to one map channel to speakers
  87. if (asset->one_to_one_map_ch_to_spkr = get_bits1(&s->gb)) {
  88. int spkr_mask_nbits = 0;
  89. int spkr_remap_nsets;
  90. int nspeakers[8];
  91. // Embedded stereo flag
  92. if (asset->nchannels_total > 2)
  93. asset->embedded_stereo = get_bits1(&s->gb);
  94. // Embedded 6 channels flag
  95. if (asset->nchannels_total > 6)
  96. asset->embedded_6ch = get_bits1(&s->gb);
  97. // Speaker mask enabled flag
  98. if (asset->spkr_mask_enabled = get_bits1(&s->gb)) {
  99. // Number of bits for speaker activity mask
  100. spkr_mask_nbits = (get_bits(&s->gb, 2) + 1) << 2;
  101. // Loudspeaker activity mask
  102. asset->spkr_mask = get_bits(&s->gb, spkr_mask_nbits);
  103. }
  104. // Number of speaker remapping sets
  105. if ((spkr_remap_nsets = get_bits(&s->gb, 3)) && !spkr_mask_nbits) {
  106. av_log(s->avctx, AV_LOG_ERROR, "Speaker mask disabled yet there are remapping sets\n");
  107. return AVERROR_INVALIDDATA;
  108. }
  109. // Standard loudspeaker layout mask
  110. for (i = 0; i < spkr_remap_nsets; i++)
  111. nspeakers[i] = ff_dca_count_chs_for_mask(get_bits(&s->gb, spkr_mask_nbits));
  112. for (i = 0; i < spkr_remap_nsets; i++) {
  113. // Number of channels to be decoded for speaker remapping
  114. int nch_for_remaps = get_bits(&s->gb, 5) + 1;
  115. for (j = 0; j < nspeakers[i]; j++) {
  116. // Decoded channels to output speaker mapping mask
  117. int remap_ch_mask = get_bits_long(&s->gb, nch_for_remaps);
  118. // Loudspeaker remapping codes
  119. skip_bits_long(&s->gb, av_popcount(remap_ch_mask) * 5);
  120. }
  121. }
  122. } else {
  123. asset->embedded_stereo = 0;
  124. asset->embedded_6ch = 0;
  125. asset->spkr_mask_enabled = 0;
  126. asset->spkr_mask = 0;
  127. // Representation type
  128. asset->representation_type = get_bits(&s->gb, 3);
  129. }
  130. }
  131. //
  132. // DRC, DNC and mixing metadata
  133. //
  134. // Dynamic range coefficient presence flag
  135. drc_present = get_bits1(&s->gb);
  136. // Code for dynamic range coefficient
  137. if (drc_present)
  138. skip_bits(&s->gb, 8);
  139. // Dialog normalization presence flag
  140. if (get_bits1(&s->gb))
  141. // Dialog normalization code
  142. skip_bits(&s->gb, 5);
  143. // DRC for stereo downmix
  144. if (drc_present && asset->embedded_stereo)
  145. skip_bits(&s->gb, 8);
  146. // Mixing metadata presence flag
  147. if (s->mix_metadata_enabled && get_bits1(&s->gb)) {
  148. int nchannels_dmix;
  149. // External mixing flag
  150. skip_bits1(&s->gb);
  151. // Post mixing / replacement gain adjustment
  152. skip_bits(&s->gb, 6);
  153. // DRC prior to mixing
  154. if (get_bits(&s->gb, 2) == 3)
  155. // Custom code for mixing DRC
  156. skip_bits(&s->gb, 8);
  157. else
  158. // Limit for mixing DRC
  159. skip_bits(&s->gb, 3);
  160. // Scaling type for channels of main audio
  161. // Scaling parameters of main audio
  162. if (get_bits1(&s->gb))
  163. for (i = 0; i < s->nmixoutconfigs; i++)
  164. skip_bits_long(&s->gb, 6 * s->nmixoutchs[i]);
  165. else
  166. skip_bits_long(&s->gb, 6 * s->nmixoutconfigs);
  167. nchannels_dmix = asset->nchannels_total;
  168. if (asset->embedded_6ch)
  169. nchannels_dmix += 6;
  170. if (asset->embedded_stereo)
  171. nchannels_dmix += 2;
  172. for (i = 0; i < s->nmixoutconfigs; i++) {
  173. if (!s->nmixoutchs[i]) {
  174. av_log(s->avctx, AV_LOG_ERROR, "Invalid speaker layout mask for mixing configuration\n");
  175. return AVERROR_INVALIDDATA;
  176. }
  177. for (j = 0; j < nchannels_dmix; j++) {
  178. // Mix output mask
  179. int mix_map_mask = get_bits(&s->gb, s->nmixoutchs[i]);
  180. // Mixing coefficients
  181. skip_bits_long(&s->gb, av_popcount(mix_map_mask) * 6);
  182. }
  183. }
  184. }
  185. //
  186. // Decoder navigation data
  187. //
  188. // Coding mode for the asset
  189. asset->coding_mode = get_bits(&s->gb, 2);
  190. // Coding components used in asset
  191. switch (asset->coding_mode) {
  192. case 0: // Coding mode that may contain multiple coding components
  193. asset->extension_mask = get_bits(&s->gb, 12);
  194. if (asset->extension_mask & DCA_EXSS_CORE) {
  195. // Size of core component in extension substream
  196. asset->core_size = get_bits(&s->gb, 14) + 1;
  197. // Core sync word present flag
  198. if (get_bits1(&s->gb))
  199. // Core sync distance
  200. skip_bits(&s->gb, 2);
  201. }
  202. if (asset->extension_mask & DCA_EXSS_XBR)
  203. // Size of XBR extension in extension substream
  204. asset->xbr_size = get_bits(&s->gb, 14) + 1;
  205. if (asset->extension_mask & DCA_EXSS_XXCH)
  206. // Size of XXCH extension in extension substream
  207. asset->xxch_size = get_bits(&s->gb, 14) + 1;
  208. if (asset->extension_mask & DCA_EXSS_X96)
  209. // Size of X96 extension in extension substream
  210. asset->x96_size = get_bits(&s->gb, 12) + 1;
  211. if (asset->extension_mask & DCA_EXSS_LBR)
  212. parse_lbr_parameters(s, asset);
  213. if (asset->extension_mask & DCA_EXSS_XLL)
  214. parse_xll_parameters(s, asset);
  215. if (asset->extension_mask & DCA_EXSS_RSV1)
  216. skip_bits(&s->gb, 16);
  217. if (asset->extension_mask & DCA_EXSS_RSV2)
  218. skip_bits(&s->gb, 16);
  219. break;
  220. case 1: // Loss-less coding mode without CBR component
  221. asset->extension_mask = DCA_EXSS_XLL;
  222. parse_xll_parameters(s, asset);
  223. break;
  224. case 2: // Low bit rate mode
  225. asset->extension_mask = DCA_EXSS_LBR;
  226. parse_lbr_parameters(s, asset);
  227. break;
  228. case 3: // Auxiliary coding mode
  229. asset->extension_mask = 0;
  230. // Size of auxiliary coded data
  231. skip_bits(&s->gb, 14);
  232. // Auxiliary codec identification
  233. skip_bits(&s->gb, 8);
  234. // Aux sync word present flag
  235. if (get_bits1(&s->gb))
  236. // Aux sync distance
  237. skip_bits(&s->gb, 3);
  238. break;
  239. }
  240. if (asset->extension_mask & DCA_EXSS_XLL)
  241. // DTS-HD stream ID
  242. asset->hd_stream_id = get_bits(&s->gb, 3);
  243. // One to one mixing flag
  244. // Per channel main audio scaling flag
  245. // Main audio scaling codes
  246. // Decode asset in secondary decoder flag
  247. // Revision 2 DRC metadata
  248. // Reserved
  249. // Zero pad
  250. if (ff_dca_seek_bits(&s->gb, descr_pos + descr_size * 8)) {
  251. av_log(s->avctx, AV_LOG_ERROR, "Read past end of EXSS asset descriptor\n");
  252. return AVERROR_INVALIDDATA;
  253. }
  254. return 0;
  255. }
  256. static int set_exss_offsets(DCAExssAsset *asset)
  257. {
  258. int offs = asset->asset_offset;
  259. int size = asset->asset_size;
  260. if (asset->extension_mask & DCA_EXSS_CORE) {
  261. asset->core_offset = offs;
  262. if (asset->core_size > size)
  263. return AVERROR_INVALIDDATA;
  264. offs += asset->core_size;
  265. size -= asset->core_size;
  266. }
  267. if (asset->extension_mask & DCA_EXSS_XBR) {
  268. asset->xbr_offset = offs;
  269. if (asset->xbr_size > size)
  270. return AVERROR_INVALIDDATA;
  271. offs += asset->xbr_size;
  272. size -= asset->xbr_size;
  273. }
  274. if (asset->extension_mask & DCA_EXSS_XXCH) {
  275. asset->xxch_offset = offs;
  276. if (asset->xxch_size > size)
  277. return AVERROR_INVALIDDATA;
  278. offs += asset->xxch_size;
  279. size -= asset->xxch_size;
  280. }
  281. if (asset->extension_mask & DCA_EXSS_X96) {
  282. asset->x96_offset = offs;
  283. if (asset->x96_size > size)
  284. return AVERROR_INVALIDDATA;
  285. offs += asset->x96_size;
  286. size -= asset->x96_size;
  287. }
  288. if (asset->extension_mask & DCA_EXSS_LBR) {
  289. asset->lbr_offset = offs;
  290. if (asset->lbr_size > size)
  291. return AVERROR_INVALIDDATA;
  292. offs += asset->lbr_size;
  293. size -= asset->lbr_size;
  294. }
  295. if (asset->extension_mask & DCA_EXSS_XLL) {
  296. asset->xll_offset = offs;
  297. if (asset->xll_size > size)
  298. return AVERROR_INVALIDDATA;
  299. offs += asset->xll_size;
  300. size -= asset->xll_size;
  301. }
  302. return 0;
  303. }
  304. int ff_dca_exss_parse(DCAExssParser *s, uint8_t *data, int size)
  305. {
  306. int i, ret, offset, wide_hdr, header_size;
  307. if ((ret = init_get_bits8(&s->gb, data, size)) < 0)
  308. return ret;
  309. // Extension substream sync word
  310. skip_bits_long(&s->gb, 32);
  311. // User defined bits
  312. skip_bits(&s->gb, 8);
  313. // Extension substream index
  314. s->exss_index = get_bits(&s->gb, 2);
  315. // Flag indicating short or long header size
  316. wide_hdr = get_bits1(&s->gb);
  317. // Extension substream header length
  318. header_size = get_bits(&s->gb, 8 + 4 * wide_hdr) + 1;
  319. // Check CRC
  320. if (ff_dca_check_crc(s->avctx, &s->gb, 32 + 8, header_size * 8)) {
  321. av_log(s->avctx, AV_LOG_ERROR, "Invalid EXSS header checksum\n");
  322. return AVERROR_INVALIDDATA;
  323. }
  324. s->exss_size_nbits = 16 + 4 * wide_hdr;
  325. // Number of bytes of extension substream
  326. s->exss_size = get_bits(&s->gb, s->exss_size_nbits) + 1;
  327. if (s->exss_size > size) {
  328. av_log(s->avctx, AV_LOG_ERROR, "Packet too short for EXSS frame\n");
  329. return AVERROR_INVALIDDATA;
  330. }
  331. // Per stream static fields presence flag
  332. if (s->static_fields_present = get_bits1(&s->gb)) {
  333. int active_exss_mask[8];
  334. // Reference clock code
  335. skip_bits(&s->gb, 2);
  336. // Extension substream frame duration
  337. skip_bits(&s->gb, 3);
  338. // Timecode presence flag
  339. if (get_bits1(&s->gb))
  340. // Timecode data
  341. skip_bits_long(&s->gb, 36);
  342. // Number of defined audio presentations
  343. s->npresents = get_bits(&s->gb, 3) + 1;
  344. if (s->npresents > 1) {
  345. avpriv_request_sample(s->avctx, "%d audio presentations", s->npresents);
  346. return AVERROR_PATCHWELCOME;
  347. }
  348. // Number of audio assets in extension substream
  349. s->nassets = get_bits(&s->gb, 3) + 1;
  350. if (s->nassets > 1) {
  351. avpriv_request_sample(s->avctx, "%d audio assets", s->nassets);
  352. return AVERROR_PATCHWELCOME;
  353. }
  354. // Active extension substream mask for audio presentation
  355. for (i = 0; i < s->npresents; i++)
  356. active_exss_mask[i] = get_bits(&s->gb, s->exss_index + 1);
  357. // Active audio asset mask
  358. for (i = 0; i < s->npresents; i++)
  359. skip_bits_long(&s->gb, av_popcount(active_exss_mask[i]) * 8);
  360. // Mixing metadata enable flag
  361. if (s->mix_metadata_enabled = get_bits1(&s->gb)) {
  362. int spkr_mask_nbits;
  363. // Mixing metadata adjustment level
  364. skip_bits(&s->gb, 2);
  365. // Number of bits for mixer output speaker activity mask
  366. spkr_mask_nbits = (get_bits(&s->gb, 2) + 1) << 2;
  367. // Number of mixing configurations
  368. s->nmixoutconfigs = get_bits(&s->gb, 2) + 1;
  369. // Speaker layout mask for mixer output channels
  370. for (i = 0; i < s->nmixoutconfigs; i++)
  371. s->nmixoutchs[i] = ff_dca_count_chs_for_mask(get_bits(&s->gb, spkr_mask_nbits));
  372. }
  373. } else {
  374. s->npresents = 1;
  375. s->nassets = 1;
  376. }
  377. // Size of encoded asset data in bytes
  378. offset = header_size;
  379. for (i = 0; i < s->nassets; i++) {
  380. s->assets[i].asset_offset = offset;
  381. s->assets[i].asset_size = get_bits(&s->gb, s->exss_size_nbits) + 1;
  382. offset += s->assets[i].asset_size;
  383. if (offset > s->exss_size) {
  384. av_log(s->avctx, AV_LOG_ERROR, "EXSS asset out of bounds\n");
  385. return AVERROR_INVALIDDATA;
  386. }
  387. }
  388. // Audio asset descriptor
  389. for (i = 0; i < s->nassets; i++) {
  390. if ((ret = parse_descriptor(s, &s->assets[i])) < 0)
  391. return ret;
  392. if ((ret = set_exss_offsets(&s->assets[i])) < 0) {
  393. av_log(s->avctx, AV_LOG_ERROR, "Invalid extension size in EXSS asset descriptor\n");
  394. return ret;
  395. }
  396. }
  397. // Backward compatible core present
  398. // Backward compatible core substream index
  399. // Backward compatible core asset index
  400. // Reserved
  401. // Byte align
  402. // CRC16 of extension substream header
  403. if (ff_dca_seek_bits(&s->gb, header_size * 8)) {
  404. av_log(s->avctx, AV_LOG_ERROR, "Read past end of EXSS header\n");
  405. return AVERROR_INVALIDDATA;
  406. }
  407. return 0;
  408. }