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.

395 lines
13KB

  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 "libavutil/opt.h"
  21. #include "libavutil/channel_layout.h"
  22. #include "dcadec.h"
  23. #include "dcahuff.h"
  24. #include "dca_syncwords.h"
  25. #include "profiles.h"
  26. #define MIN_PACKET_SIZE 16
  27. #define MAX_PACKET_SIZE 0x104000
  28. int ff_dca_set_channel_layout(AVCodecContext *avctx, int *ch_remap, int dca_mask)
  29. {
  30. static const uint8_t dca2wav_norm[28] = {
  31. 2, 0, 1, 9, 10, 3, 8, 4, 5, 9, 10, 6, 7, 12,
  32. 13, 14, 3, 6, 7, 11, 12, 14, 16, 15, 17, 8, 4, 5,
  33. };
  34. static const uint8_t dca2wav_wide[28] = {
  35. 2, 0, 1, 4, 5, 3, 8, 4, 5, 9, 10, 6, 7, 12,
  36. 13, 14, 3, 9, 10, 11, 12, 14, 16, 15, 17, 8, 4, 5,
  37. };
  38. int dca_ch, wav_ch, nchannels = 0;
  39. if (avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE) {
  40. for (dca_ch = 0; dca_ch < DCA_SPEAKER_COUNT; dca_ch++)
  41. if (dca_mask & (1U << dca_ch))
  42. ch_remap[nchannels++] = dca_ch;
  43. avctx->channel_layout = dca_mask;
  44. } else {
  45. int wav_mask = 0;
  46. int wav_map[18];
  47. const uint8_t *dca2wav;
  48. if (dca_mask == DCA_SPEAKER_LAYOUT_7POINT0_WIDE ||
  49. dca_mask == DCA_SPEAKER_LAYOUT_7POINT1_WIDE)
  50. dca2wav = dca2wav_wide;
  51. else
  52. dca2wav = dca2wav_norm;
  53. for (dca_ch = 0; dca_ch < 28; dca_ch++) {
  54. if (dca_mask & (1 << dca_ch)) {
  55. wav_ch = dca2wav[dca_ch];
  56. if (!(wav_mask & (1 << wav_ch))) {
  57. wav_map[wav_ch] = dca_ch;
  58. wav_mask |= 1 << wav_ch;
  59. }
  60. }
  61. }
  62. for (wav_ch = 0; wav_ch < 18; wav_ch++)
  63. if (wav_mask & (1 << wav_ch))
  64. ch_remap[nchannels++] = wav_map[wav_ch];
  65. avctx->channel_layout = wav_mask;
  66. }
  67. avctx->channels = nchannels;
  68. return nchannels;
  69. }
  70. void ff_dca_downmix_to_stereo_fixed(DCADSPContext *dcadsp, int32_t **samples,
  71. int *coeff_l, int nsamples, int ch_mask)
  72. {
  73. int pos, spkr, max_spkr = av_log2(ch_mask);
  74. int *coeff_r = coeff_l + av_popcount(ch_mask);
  75. av_assert0(DCA_HAS_STEREO(ch_mask));
  76. // Scale left and right channels
  77. pos = (ch_mask & DCA_SPEAKER_MASK_C);
  78. dcadsp->dmix_scale(samples[DCA_SPEAKER_L], coeff_l[pos ], nsamples);
  79. dcadsp->dmix_scale(samples[DCA_SPEAKER_R], coeff_r[pos + 1], nsamples);
  80. // Downmix remaining channels
  81. for (spkr = 0; spkr <= max_spkr; spkr++) {
  82. if (!(ch_mask & (1U << spkr)))
  83. continue;
  84. if (*coeff_l && spkr != DCA_SPEAKER_L)
  85. dcadsp->dmix_add(samples[DCA_SPEAKER_L], samples[spkr],
  86. *coeff_l, nsamples);
  87. if (*coeff_r && spkr != DCA_SPEAKER_R)
  88. dcadsp->dmix_add(samples[DCA_SPEAKER_R], samples[spkr],
  89. *coeff_r, nsamples);
  90. coeff_l++;
  91. coeff_r++;
  92. }
  93. }
  94. void ff_dca_downmix_to_stereo_float(AVFloatDSPContext *fdsp, float **samples,
  95. int *coeff_l, int nsamples, int ch_mask)
  96. {
  97. int pos, spkr, max_spkr = av_log2(ch_mask);
  98. int *coeff_r = coeff_l + av_popcount(ch_mask);
  99. const float scale = 1.0f / (1 << 15);
  100. av_assert0(DCA_HAS_STEREO(ch_mask));
  101. // Scale left and right channels
  102. pos = (ch_mask & DCA_SPEAKER_MASK_C);
  103. fdsp->vector_fmul_scalar(samples[DCA_SPEAKER_L], samples[DCA_SPEAKER_L],
  104. coeff_l[pos ] * scale, nsamples);
  105. fdsp->vector_fmul_scalar(samples[DCA_SPEAKER_R], samples[DCA_SPEAKER_R],
  106. coeff_r[pos + 1] * scale, nsamples);
  107. // Downmix remaining channels
  108. for (spkr = 0; spkr <= max_spkr; spkr++) {
  109. if (!(ch_mask & (1U << spkr)))
  110. continue;
  111. if (*coeff_l && spkr != DCA_SPEAKER_L)
  112. fdsp->vector_fmac_scalar(samples[DCA_SPEAKER_L], samples[spkr],
  113. *coeff_l * scale, nsamples);
  114. if (*coeff_r && spkr != DCA_SPEAKER_R)
  115. fdsp->vector_fmac_scalar(samples[DCA_SPEAKER_R], samples[spkr],
  116. *coeff_r * scale, nsamples);
  117. coeff_l++;
  118. coeff_r++;
  119. }
  120. }
  121. static int convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst, int max_size)
  122. {
  123. switch (AV_RB32(src)) {
  124. case DCA_SYNCWORD_CORE_BE:
  125. case DCA_SYNCWORD_SUBSTREAM:
  126. memcpy(dst, src, src_size);
  127. return src_size;
  128. case DCA_SYNCWORD_CORE_LE:
  129. case DCA_SYNCWORD_CORE_14B_BE:
  130. case DCA_SYNCWORD_CORE_14B_LE:
  131. return avpriv_dca_convert_bitstream(src, src_size, dst, max_size);
  132. default:
  133. return AVERROR_INVALIDDATA;
  134. }
  135. }
  136. static int dcadec_decode_frame(AVCodecContext *avctx, void *data,
  137. int *got_frame_ptr, AVPacket *avpkt)
  138. {
  139. DCAContext *s = avctx->priv_data;
  140. AVFrame *frame = data;
  141. uint8_t *input = avpkt->data;
  142. int input_size = avpkt->size;
  143. int i, ret, prev_packet = s->packet;
  144. if (input_size < MIN_PACKET_SIZE || input_size > MAX_PACKET_SIZE) {
  145. av_log(avctx, AV_LOG_ERROR, "Invalid packet size\n");
  146. return AVERROR_INVALIDDATA;
  147. }
  148. av_fast_malloc(&s->buffer, &s->buffer_size,
  149. FFALIGN(input_size, 4096) + DCA_BUFFER_PADDING_SIZE);
  150. if (!s->buffer)
  151. return AVERROR(ENOMEM);
  152. for (i = 0, ret = AVERROR_INVALIDDATA; i < input_size - MIN_PACKET_SIZE + 1 && ret < 0; i++)
  153. ret = convert_bitstream(input + i, input_size - i, s->buffer, s->buffer_size);
  154. if (ret < 0)
  155. return ret;
  156. input = s->buffer;
  157. input_size = ret;
  158. s->packet = 0;
  159. // Parse backward compatible core sub-stream
  160. if (AV_RB32(input) == DCA_SYNCWORD_CORE_BE) {
  161. int frame_size;
  162. if ((ret = ff_dca_core_parse(&s->core, input, input_size)) < 0) {
  163. s->core_residual_valid = 0;
  164. return ret;
  165. }
  166. s->packet |= DCA_PACKET_CORE;
  167. // EXXS data must be aligned on 4-byte boundary
  168. frame_size = FFALIGN(s->core.frame_size, 4);
  169. if (input_size - 4 > frame_size) {
  170. input += frame_size;
  171. input_size -= frame_size;
  172. }
  173. }
  174. if (!s->core_only) {
  175. DCAExssAsset *asset = NULL;
  176. // Parse extension sub-stream (EXSS)
  177. if (AV_RB32(input) == DCA_SYNCWORD_SUBSTREAM) {
  178. if ((ret = ff_dca_exss_parse(&s->exss, input, input_size)) < 0) {
  179. if (avctx->err_recognition & AV_EF_EXPLODE)
  180. return ret;
  181. } else {
  182. s->packet |= DCA_PACKET_EXSS;
  183. asset = &s->exss.assets[0];
  184. }
  185. }
  186. // Parse XLL component in EXSS
  187. if (asset && (asset->extension_mask & DCA_EXSS_XLL)) {
  188. if ((ret = ff_dca_xll_parse(&s->xll, input, asset)) < 0) {
  189. // Conceal XLL synchronization error
  190. if (ret == AVERROR(EAGAIN)
  191. && (prev_packet & DCA_PACKET_XLL)
  192. && (s->packet & DCA_PACKET_CORE))
  193. s->packet |= DCA_PACKET_XLL | DCA_PACKET_RECOVERY;
  194. else if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))
  195. return ret;
  196. } else {
  197. s->packet |= DCA_PACKET_XLL;
  198. }
  199. }
  200. // Parse core extensions in EXSS or backward compatible core sub-stream
  201. if ((s->packet & DCA_PACKET_CORE)
  202. && (ret = ff_dca_core_parse_exss(&s->core, input, asset)) < 0)
  203. return ret;
  204. }
  205. // Filter the frame
  206. if (s->packet & DCA_PACKET_XLL) {
  207. if (s->packet & DCA_PACKET_CORE) {
  208. int x96_synth = -1;
  209. // Enable X96 synthesis if needed
  210. if (s->xll.chset[0].freq == 96000 && s->core.sample_rate == 48000)
  211. x96_synth = 1;
  212. if ((ret = ff_dca_core_filter_fixed(&s->core, x96_synth)) < 0) {
  213. s->core_residual_valid = 0;
  214. return ret;
  215. }
  216. // Force lossy downmixed output on the first core frame filtered.
  217. // This prevents audible clicks when seeking and is consistent with
  218. // what reference decoder does when there are multiple channel sets.
  219. if (!s->core_residual_valid) {
  220. if (s->xll.nreschsets > 0 && s->xll.nchsets > 1)
  221. s->packet |= DCA_PACKET_RECOVERY;
  222. s->core_residual_valid = 1;
  223. }
  224. }
  225. if ((ret = ff_dca_xll_filter_frame(&s->xll, frame)) < 0) {
  226. // Fall back to core unless hard error
  227. if (!(s->packet & DCA_PACKET_CORE))
  228. return ret;
  229. if (ret != AVERROR_INVALIDDATA || (avctx->err_recognition & AV_EF_EXPLODE))
  230. return ret;
  231. if ((ret = ff_dca_core_filter_frame(&s->core, frame)) < 0) {
  232. s->core_residual_valid = 0;
  233. return ret;
  234. }
  235. }
  236. } else if (s->packet & DCA_PACKET_CORE) {
  237. if ((ret = ff_dca_core_filter_frame(&s->core, frame)) < 0) {
  238. s->core_residual_valid = 0;
  239. return ret;
  240. }
  241. s->core_residual_valid = !!(s->core.filter_mode & DCA_FILTER_MODE_FIXED);
  242. } else {
  243. return AVERROR_INVALIDDATA;
  244. }
  245. *got_frame_ptr = 1;
  246. return avpkt->size;
  247. }
  248. static av_cold void dcadec_flush(AVCodecContext *avctx)
  249. {
  250. DCAContext *s = avctx->priv_data;
  251. ff_dca_core_flush(&s->core);
  252. ff_dca_xll_flush(&s->xll);
  253. s->core_residual_valid = 0;
  254. }
  255. static av_cold int dcadec_close(AVCodecContext *avctx)
  256. {
  257. DCAContext *s = avctx->priv_data;
  258. ff_dca_core_close(&s->core);
  259. ff_dca_xll_close(&s->xll);
  260. av_freep(&s->buffer);
  261. s->buffer_size = 0;
  262. return 0;
  263. }
  264. static av_cold int dcadec_init(AVCodecContext *avctx)
  265. {
  266. DCAContext *s = avctx->priv_data;
  267. s->avctx = avctx;
  268. s->core.avctx = avctx;
  269. s->exss.avctx = avctx;
  270. s->xll.avctx = avctx;
  271. ff_dca_init_vlcs();
  272. if (ff_dca_core_init(&s->core) < 0)
  273. return AVERROR(ENOMEM);
  274. ff_dcadsp_init(&s->dcadsp);
  275. s->core.dcadsp = &s->dcadsp;
  276. s->xll.dcadsp = &s->dcadsp;
  277. s->crctab = av_crc_get_table(AV_CRC_16_CCITT);
  278. switch (avctx->request_channel_layout & ~AV_CH_LAYOUT_NATIVE) {
  279. case 0:
  280. s->request_channel_layout = 0;
  281. break;
  282. case AV_CH_LAYOUT_STEREO:
  283. case AV_CH_LAYOUT_STEREO_DOWNMIX:
  284. s->request_channel_layout = DCA_SPEAKER_LAYOUT_STEREO;
  285. break;
  286. case AV_CH_LAYOUT_5POINT0:
  287. s->request_channel_layout = DCA_SPEAKER_LAYOUT_5POINT0;
  288. break;
  289. case AV_CH_LAYOUT_5POINT1:
  290. s->request_channel_layout = DCA_SPEAKER_LAYOUT_5POINT1;
  291. break;
  292. default:
  293. av_log(avctx, AV_LOG_WARNING, "Invalid request_channel_layout\n");
  294. break;
  295. }
  296. avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
  297. avctx->bits_per_raw_sample = 24;
  298. return 0;
  299. }
  300. #define OFFSET(x) offsetof(DCAContext, x)
  301. #define PARAM AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  302. static const AVOption dcadec_options[] = {
  303. { "core_only", "Decode core only without extensions", OFFSET(core_only), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, PARAM },
  304. { NULL }
  305. };
  306. static const AVClass dcadec_class = {
  307. .class_name = "DCA decoder",
  308. .item_name = av_default_item_name,
  309. .option = dcadec_options,
  310. .version = LIBAVUTIL_VERSION_INT,
  311. .category = AV_CLASS_CATEGORY_DECODER,
  312. };
  313. AVCodec ff_dca_decoder = {
  314. .name = "dca",
  315. .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
  316. .type = AVMEDIA_TYPE_AUDIO,
  317. .id = AV_CODEC_ID_DTS,
  318. .priv_data_size = sizeof(DCAContext),
  319. .init = dcadec_init,
  320. .decode = dcadec_decode_frame,
  321. .close = dcadec_close,
  322. .flush = dcadec_flush,
  323. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
  324. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P,
  325. AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE },
  326. .priv_class = &dcadec_class,
  327. .profiles = NULL_IF_CONFIG_SMALL(ff_dca_profiles),
  328. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
  329. };