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.

401 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 dcadec_decode_frame(AVCodecContext *avctx, void *data,
  122. int *got_frame_ptr, AVPacket *avpkt)
  123. {
  124. DCAContext *s = avctx->priv_data;
  125. AVFrame *frame = data;
  126. uint8_t *input = avpkt->data;
  127. int input_size = avpkt->size;
  128. int i, ret, prev_packet = s->packet;
  129. uint32_t mrk;
  130. if (input_size < MIN_PACKET_SIZE || input_size > MAX_PACKET_SIZE) {
  131. av_log(avctx, AV_LOG_ERROR, "Invalid packet size\n");
  132. return AVERROR_INVALIDDATA;
  133. }
  134. // Convert input to BE format
  135. mrk = AV_RB32(input);
  136. if (mrk != DCA_SYNCWORD_CORE_BE && mrk != DCA_SYNCWORD_SUBSTREAM) {
  137. av_fast_padded_malloc(&s->buffer, &s->buffer_size, input_size);
  138. if (!s->buffer)
  139. return AVERROR(ENOMEM);
  140. for (i = 0, ret = AVERROR_INVALIDDATA; i < input_size - MIN_PACKET_SIZE + 1 && ret < 0; i++)
  141. ret = avpriv_dca_convert_bitstream(input + i, input_size - i, s->buffer, s->buffer_size);
  142. if (ret < 0) {
  143. av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
  144. return ret;
  145. }
  146. input = s->buffer;
  147. input_size = ret;
  148. }
  149. s->packet = 0;
  150. // Parse backward compatible core sub-stream
  151. if (AV_RB32(input) == DCA_SYNCWORD_CORE_BE) {
  152. int frame_size;
  153. if ((ret = ff_dca_core_parse(&s->core, input, input_size)) < 0)
  154. return ret;
  155. s->packet |= DCA_PACKET_CORE;
  156. // EXXS data must be aligned on 4-byte boundary
  157. frame_size = FFALIGN(s->core.frame_size, 4);
  158. if (input_size - 4 > frame_size) {
  159. input += frame_size;
  160. input_size -= frame_size;
  161. }
  162. }
  163. if (!s->core_only) {
  164. DCAExssAsset *asset = NULL;
  165. // Parse extension sub-stream (EXSS)
  166. if (AV_RB32(input) == DCA_SYNCWORD_SUBSTREAM) {
  167. if ((ret = ff_dca_exss_parse(&s->exss, input, input_size)) < 0) {
  168. if (avctx->err_recognition & AV_EF_EXPLODE)
  169. return ret;
  170. } else {
  171. s->packet |= DCA_PACKET_EXSS;
  172. asset = &s->exss.assets[0];
  173. }
  174. }
  175. // Parse XLL component in EXSS
  176. if (asset && (asset->extension_mask & DCA_EXSS_XLL)) {
  177. if ((ret = ff_dca_xll_parse(&s->xll, input, asset)) < 0) {
  178. // Conceal XLL synchronization error
  179. if (ret == AVERROR(EAGAIN)
  180. && (prev_packet & DCA_PACKET_XLL)
  181. && (s->packet & DCA_PACKET_CORE))
  182. s->packet |= DCA_PACKET_XLL | DCA_PACKET_RECOVERY;
  183. else if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))
  184. return ret;
  185. } else {
  186. s->packet |= DCA_PACKET_XLL;
  187. }
  188. }
  189. // Parse LBR component in EXSS
  190. if (asset && (asset->extension_mask & DCA_EXSS_LBR)) {
  191. if ((ret = ff_dca_lbr_parse(&s->lbr, input, asset)) < 0) {
  192. if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))
  193. return ret;
  194. } else {
  195. s->packet |= DCA_PACKET_LBR;
  196. }
  197. }
  198. // Parse core extensions in EXSS or backward compatible core sub-stream
  199. if ((s->packet & DCA_PACKET_CORE)
  200. && (ret = ff_dca_core_parse_exss(&s->core, input, asset)) < 0)
  201. return ret;
  202. }
  203. // Filter the frame
  204. if (s->packet & DCA_PACKET_LBR) {
  205. if ((ret = ff_dca_lbr_filter_frame(&s->lbr, frame)) < 0)
  206. return ret;
  207. } else if (s->packet & DCA_PACKET_XLL) {
  208. if (s->packet & DCA_PACKET_CORE) {
  209. int x96_synth = -1;
  210. // Enable X96 synthesis if needed
  211. if (s->xll.chset[0].freq == 96000 && s->core.sample_rate == 48000)
  212. x96_synth = 1;
  213. if ((ret = ff_dca_core_filter_fixed(&s->core, x96_synth)) < 0)
  214. return ret;
  215. // Force lossy downmixed output on the first core frame filtered.
  216. // This prevents audible clicks when seeking and is consistent with
  217. // what reference decoder does when there are multiple channel sets.
  218. if (!(prev_packet & DCA_PACKET_RESIDUAL) && s->xll.nreschsets > 0
  219. && s->xll.nchsets > 1) {
  220. av_log(avctx, AV_LOG_VERBOSE, "Forcing XLL recovery mode\n");
  221. s->packet |= DCA_PACKET_RECOVERY;
  222. }
  223. // Set 'residual ok' flag for the next frame
  224. s->packet |= DCA_PACKET_RESIDUAL;
  225. }
  226. if ((ret = ff_dca_xll_filter_frame(&s->xll, frame)) < 0) {
  227. // Fall back to core unless hard error
  228. if (!(s->packet & DCA_PACKET_CORE))
  229. return ret;
  230. if (ret != AVERROR_INVALIDDATA || (avctx->err_recognition & AV_EF_EXPLODE))
  231. return ret;
  232. if ((ret = ff_dca_core_filter_frame(&s->core, frame)) < 0)
  233. return ret;
  234. }
  235. } else if (s->packet & DCA_PACKET_CORE) {
  236. if ((ret = ff_dca_core_filter_frame(&s->core, frame)) < 0)
  237. return ret;
  238. if (s->core.filter_mode & DCA_FILTER_MODE_FIXED)
  239. s->packet |= DCA_PACKET_RESIDUAL;
  240. } else {
  241. av_log(avctx, AV_LOG_ERROR, "No valid DCA sub-stream found\n");
  242. if (s->core_only)
  243. av_log(avctx, AV_LOG_WARNING, "Consider disabling 'core_only' option\n");
  244. return AVERROR_INVALIDDATA;
  245. }
  246. *got_frame_ptr = 1;
  247. return avpkt->size;
  248. }
  249. static av_cold void dcadec_flush(AVCodecContext *avctx)
  250. {
  251. DCAContext *s = avctx->priv_data;
  252. ff_dca_core_flush(&s->core);
  253. ff_dca_xll_flush(&s->xll);
  254. ff_dca_lbr_flush(&s->lbr);
  255. s->packet &= DCA_PACKET_MASK;
  256. }
  257. static av_cold int dcadec_close(AVCodecContext *avctx)
  258. {
  259. DCAContext *s = avctx->priv_data;
  260. ff_dca_core_close(&s->core);
  261. ff_dca_xll_close(&s->xll);
  262. ff_dca_lbr_close(&s->lbr);
  263. av_freep(&s->buffer);
  264. s->buffer_size = 0;
  265. return 0;
  266. }
  267. static av_cold int dcadec_init(AVCodecContext *avctx)
  268. {
  269. DCAContext *s = avctx->priv_data;
  270. s->avctx = avctx;
  271. s->core.avctx = avctx;
  272. s->exss.avctx = avctx;
  273. s->xll.avctx = avctx;
  274. s->lbr.avctx = avctx;
  275. ff_dca_init_vlcs();
  276. if (ff_dca_core_init(&s->core) < 0)
  277. return AVERROR(ENOMEM);
  278. if (ff_dca_lbr_init(&s->lbr) < 0)
  279. return AVERROR(ENOMEM);
  280. ff_dcadsp_init(&s->dcadsp);
  281. s->core.dcadsp = &s->dcadsp;
  282. s->xll.dcadsp = &s->dcadsp;
  283. s->lbr.dcadsp = &s->dcadsp;
  284. s->crctab = av_crc_get_table(AV_CRC_16_CCITT);
  285. switch (avctx->request_channel_layout & ~AV_CH_LAYOUT_NATIVE) {
  286. case 0:
  287. s->request_channel_layout = 0;
  288. break;
  289. case AV_CH_LAYOUT_STEREO:
  290. case AV_CH_LAYOUT_STEREO_DOWNMIX:
  291. s->request_channel_layout = DCA_SPEAKER_LAYOUT_STEREO;
  292. break;
  293. case AV_CH_LAYOUT_5POINT0:
  294. s->request_channel_layout = DCA_SPEAKER_LAYOUT_5POINT0;
  295. break;
  296. case AV_CH_LAYOUT_5POINT1:
  297. s->request_channel_layout = DCA_SPEAKER_LAYOUT_5POINT1;
  298. break;
  299. default:
  300. av_log(avctx, AV_LOG_WARNING, "Invalid request_channel_layout\n");
  301. break;
  302. }
  303. return 0;
  304. }
  305. #define OFFSET(x) offsetof(DCAContext, x)
  306. #define PARAM AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  307. static const AVOption dcadec_options[] = {
  308. { "core_only", "Decode core only without extensions", OFFSET(core_only), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, PARAM },
  309. { NULL }
  310. };
  311. static const AVClass dcadec_class = {
  312. .class_name = "DCA decoder",
  313. .item_name = av_default_item_name,
  314. .option = dcadec_options,
  315. .version = LIBAVUTIL_VERSION_INT,
  316. .category = AV_CLASS_CATEGORY_DECODER,
  317. };
  318. AVCodec ff_dca_decoder = {
  319. .name = "dca",
  320. .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
  321. .type = AVMEDIA_TYPE_AUDIO,
  322. .id = AV_CODEC_ID_DTS,
  323. .priv_data_size = sizeof(DCAContext),
  324. .init = dcadec_init,
  325. .decode = dcadec_decode_frame,
  326. .close = dcadec_close,
  327. .flush = dcadec_flush,
  328. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
  329. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P,
  330. AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE },
  331. .priv_class = &dcadec_class,
  332. .profiles = NULL_IF_CONFIG_SMALL(ff_dca_profiles),
  333. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
  334. };