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.

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