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.

268 lines
9.3KB

  1. /*
  2. * libdcadec decoder wrapper
  3. * Copyright (C) 2015 Hendrik Leppkes
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <libdcadec/dca_context.h>
  22. #include "libavutil/channel_layout.h"
  23. #include "libavutil/common.h"
  24. #include "libavutil/opt.h"
  25. #include "avcodec.h"
  26. #include "dca.h"
  27. #include "dca_syncwords.h"
  28. #include "internal.h"
  29. typedef struct DCADecContext {
  30. struct dcadec_context *ctx;
  31. uint8_t *buffer;
  32. int buffer_size;
  33. int downmix_warned;
  34. } DCADecContext;
  35. static int dcadec_decode_frame(AVCodecContext *avctx, void *data,
  36. int *got_frame_ptr, AVPacket *avpkt)
  37. {
  38. DCADecContext *s = avctx->priv_data;
  39. AVFrame *frame = data;
  40. struct dcadec_exss_info *exss;
  41. int ret, i, k;
  42. int **samples, nsamples, channel_mask, sample_rate, bits_per_sample, profile;
  43. uint32_t mrk;
  44. uint8_t *input = avpkt->data;
  45. int input_size = avpkt->size;
  46. /* convert bytestream syntax to RAW BE format if required */
  47. if (input_size < 8) {
  48. av_log(avctx, AV_LOG_ERROR, "Input size too small\n");
  49. return AVERROR_INVALIDDATA;
  50. }
  51. mrk = AV_RB32(input);
  52. if (mrk != DCA_SYNCWORD_CORE_BE && mrk != DCA_SYNCWORD_SUBSTREAM) {
  53. s->buffer = av_fast_realloc(s->buffer, &s->buffer_size, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  54. if (!s->buffer)
  55. return AVERROR(ENOMEM);
  56. for (i = 0, ret = AVERROR_INVALIDDATA; i < input_size - 3 && ret < 0; i++)
  57. ret = avpriv_dca_convert_bitstream(input + i, input_size - i, s->buffer, s->buffer_size);
  58. if (ret < 0)
  59. return ret;
  60. input = s->buffer;
  61. input_size = ret;
  62. }
  63. if ((ret = dcadec_context_parse(s->ctx, input, input_size)) < 0) {
  64. av_log(avctx, AV_LOG_ERROR, "dcadec_context_parse() failed: %d (%s)\n", -ret, dcadec_strerror(ret));
  65. return AVERROR_EXTERNAL;
  66. }
  67. if ((ret = dcadec_context_filter(s->ctx, &samples, &nsamples, &channel_mask,
  68. &sample_rate, &bits_per_sample, &profile)) < 0) {
  69. av_log(avctx, AV_LOG_ERROR, "dcadec_context_filter() failed: %d (%s)\n", -ret, dcadec_strerror(ret));
  70. return AVERROR_EXTERNAL;
  71. }
  72. avctx->channels = av_get_channel_layout_nb_channels(channel_mask);
  73. avctx->channel_layout = channel_mask;
  74. avctx->sample_rate = sample_rate;
  75. if (bits_per_sample == 16)
  76. avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
  77. else if (bits_per_sample > 16 && bits_per_sample <= 24)
  78. avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
  79. else {
  80. av_log(avctx, AV_LOG_ERROR, "Unsupported number of bits per sample: %d\n",
  81. bits_per_sample);
  82. return AVERROR(ENOSYS);
  83. }
  84. avctx->bits_per_raw_sample = bits_per_sample;
  85. switch (profile) {
  86. case DCADEC_PROFILE_DS:
  87. avctx->profile = FF_PROFILE_DTS;
  88. break;
  89. case DCADEC_PROFILE_DS_96_24:
  90. avctx->profile = FF_PROFILE_DTS_96_24;
  91. break;
  92. case DCADEC_PROFILE_DS_ES:
  93. avctx->profile = FF_PROFILE_DTS_ES;
  94. break;
  95. case DCADEC_PROFILE_HD_HRA:
  96. avctx->profile = FF_PROFILE_DTS_HD_HRA;
  97. break;
  98. case DCADEC_PROFILE_HD_MA:
  99. avctx->profile = FF_PROFILE_DTS_HD_MA;
  100. break;
  101. case DCADEC_PROFILE_EXPRESS:
  102. avctx->profile = FF_PROFILE_DTS_EXPRESS;
  103. break;
  104. case DCADEC_PROFILE_UNKNOWN:
  105. default:
  106. avctx->profile = FF_PROFILE_UNKNOWN;
  107. break;
  108. }
  109. /* bitrate is only meaningful if there are no HD extensions, as they distort the bitrate */
  110. if (profile == DCADEC_PROFILE_DS || profile == DCADEC_PROFILE_DS_96_24 || profile == DCADEC_PROFILE_DS_ES) {
  111. struct dcadec_core_info *info = dcadec_context_get_core_info(s->ctx);
  112. avctx->bit_rate = info->bit_rate;
  113. dcadec_context_free_core_info(info);
  114. } else
  115. avctx->bit_rate = 0;
  116. #if HAVE_STRUCT_DCADEC_EXSS_INFO_MATRIX_ENCODING
  117. if (exss = dcadec_context_get_exss_info(s->ctx)) {
  118. enum AVMatrixEncoding matrix_encoding = AV_MATRIX_ENCODING_NONE;
  119. if (!s->downmix_warned) {
  120. uint64_t layout = avctx->request_channel_layout;
  121. if (((layout == AV_CH_LAYOUT_STEREO_DOWNMIX || layout == AV_CH_LAYOUT_STEREO) && !exss->embedded_stereo) ||
  122. ( layout == AV_CH_LAYOUT_5POINT1 && !exss->embedded_6ch))
  123. av_log(avctx, AV_LOG_WARNING, "%s downmix was requested but no custom coefficients are available, "
  124. "this may result in clipping\n",
  125. layout == AV_CH_LAYOUT_5POINT1 ? "5.1" : "Stereo");
  126. s->downmix_warned = 1;
  127. }
  128. switch(exss->matrix_encoding) {
  129. case DCADEC_MATRIX_ENCODING_SURROUND:
  130. matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
  131. break;
  132. case DCADEC_MATRIX_ENCODING_HEADPHONE:
  133. matrix_encoding = AV_MATRIX_ENCODING_DOLBYHEADPHONE;
  134. break;
  135. }
  136. dcadec_context_free_exss_info(exss);
  137. if (matrix_encoding != AV_MATRIX_ENCODING_NONE &&
  138. (ret = ff_side_data_update_matrix_encoding(frame, matrix_encoding)) < 0)
  139. return ret;
  140. }
  141. #endif
  142. frame->nb_samples = nsamples;
  143. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  144. return ret;
  145. for (i = 0; i < avctx->channels; i++) {
  146. if (frame->format == AV_SAMPLE_FMT_S16P) {
  147. int16_t *plane = (int16_t *)frame->extended_data[i];
  148. for (k = 0; k < nsamples; k++)
  149. plane[k] = samples[i][k];
  150. } else {
  151. int32_t *plane = (int32_t *)frame->extended_data[i];
  152. int shift = 32 - bits_per_sample;
  153. for (k = 0; k < nsamples; k++)
  154. plane[k] = samples[i][k] << shift;
  155. }
  156. }
  157. *got_frame_ptr = 1;
  158. return avpkt->size;
  159. }
  160. static av_cold void dcadec_flush(AVCodecContext *avctx)
  161. {
  162. DCADecContext *s = avctx->priv_data;
  163. dcadec_context_clear(s->ctx);
  164. }
  165. static av_cold int dcadec_close(AVCodecContext *avctx)
  166. {
  167. DCADecContext *s = avctx->priv_data;
  168. dcadec_context_destroy(s->ctx);
  169. s->ctx = NULL;
  170. av_freep(&s->buffer);
  171. return 0;
  172. }
  173. static av_cold int dcadec_init(AVCodecContext *avctx)
  174. {
  175. DCADecContext *s = avctx->priv_data;
  176. int flags = 0;
  177. /* Affects only lossy DTS profiles. DTS-HD MA is always bitexact */
  178. if (avctx->flags & CODEC_FLAG_BITEXACT)
  179. flags |= DCADEC_FLAG_CORE_BIT_EXACT;
  180. if (avctx->request_channel_layout > 0 && avctx->request_channel_layout != AV_CH_LAYOUT_NATIVE) {
  181. switch (avctx->request_channel_layout) {
  182. case AV_CH_LAYOUT_STEREO:
  183. case AV_CH_LAYOUT_STEREO_DOWNMIX:
  184. /* libdcadec ignores the 2ch flag if used alone when no custom downmix coefficients
  185. are available, silently outputting a 5.1 downmix if possible instead.
  186. Using both the 2ch and 6ch flags together forces a 2ch downmix using default
  187. coefficients in such cases. This matches the behavior of the 6ch flag when used
  188. alone, where a 5.1 downmix is generated if possible, regardless of custom
  189. coefficients being available or not. */
  190. flags |= DCADEC_FLAG_KEEP_DMIX_2CH | DCADEC_FLAG_KEEP_DMIX_6CH;
  191. break;
  192. case AV_CH_LAYOUT_5POINT1:
  193. flags |= DCADEC_FLAG_KEEP_DMIX_6CH;
  194. break;
  195. default:
  196. av_log(avctx, AV_LOG_WARNING, "Invalid request_channel_layout\n");
  197. break;
  198. }
  199. }
  200. s->ctx = dcadec_context_create(flags);
  201. if (!s->ctx)
  202. return AVERROR(ENOMEM);
  203. avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
  204. avctx->bits_per_raw_sample = 24;
  205. return 0;
  206. }
  207. static const AVProfile profiles[] = {
  208. { FF_PROFILE_DTS, "DTS" },
  209. { FF_PROFILE_DTS_ES, "DTS-ES" },
  210. { FF_PROFILE_DTS_96_24, "DTS 96/24" },
  211. { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
  212. { FF_PROFILE_DTS_HD_MA, "DTS-HD MA" },
  213. { FF_PROFILE_DTS_EXPRESS, "DTS Express" },
  214. { FF_PROFILE_UNKNOWN },
  215. };
  216. AVCodec ff_libdcadec_decoder = {
  217. .name = "libdcadec",
  218. .long_name = NULL_IF_CONFIG_SMALL("dcadec DCA decoder"),
  219. .type = AVMEDIA_TYPE_AUDIO,
  220. .id = AV_CODEC_ID_DTS,
  221. .priv_data_size = sizeof(DCADecContext),
  222. .init = dcadec_init,
  223. .decode = dcadec_decode_frame,
  224. .close = dcadec_close,
  225. .flush = dcadec_flush,
  226. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_CHANNEL_CONF,
  227. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S32P, AV_SAMPLE_FMT_S16P,
  228. AV_SAMPLE_FMT_NONE },
  229. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  230. };