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.

235 lines
7.6KB

  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. } DCADecContext;
  34. static int dcadec_decode_frame(AVCodecContext *avctx, void *data,
  35. int *got_frame_ptr, AVPacket *avpkt)
  36. {
  37. DCADecContext *s = avctx->priv_data;
  38. AVFrame *frame = data;
  39. struct dcadec_exss_info *exss;
  40. int ret, i, k;
  41. int **samples, nsamples, channel_mask, sample_rate, bits_per_sample, profile;
  42. uint32_t mrk;
  43. uint8_t *input = avpkt->data;
  44. int input_size = avpkt->size;
  45. /* convert bytestream syntax to RAW BE format if required */
  46. if (input_size < 8) {
  47. av_log(avctx, AV_LOG_ERROR, "Input size too small\n");
  48. return AVERROR_INVALIDDATA;
  49. }
  50. mrk = AV_RB32(input);
  51. if (mrk != DCA_SYNCWORD_CORE_BE && mrk != DCA_SYNCWORD_SUBSTREAM) {
  52. s->buffer = av_fast_realloc(s->buffer, &s->buffer_size, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  53. if (!s->buffer)
  54. return AVERROR(ENOMEM);
  55. for (i = 0, ret = AVERROR_INVALIDDATA; i < input_size - 3 && ret < 0; i++)
  56. ret = avpriv_dca_convert_bitstream(input + i, input_size - i, s->buffer, s->buffer_size);
  57. if (ret < 0)
  58. return ret;
  59. input = s->buffer;
  60. input_size = ret;
  61. }
  62. if ((ret = dcadec_context_parse(s->ctx, input, input_size)) < 0) {
  63. av_log(avctx, AV_LOG_ERROR, "dcadec_context_parse() failed: %d (%s)\n", -ret, dcadec_strerror(ret));
  64. return AVERROR_EXTERNAL;
  65. }
  66. if ((ret = dcadec_context_filter(s->ctx, &samples, &nsamples, &channel_mask,
  67. &sample_rate, &bits_per_sample, &profile)) < 0) {
  68. av_log(avctx, AV_LOG_ERROR, "dcadec_context_filter() failed: %d (%s)\n", -ret, dcadec_strerror(ret));
  69. return AVERROR_EXTERNAL;
  70. }
  71. avctx->channels = av_get_channel_layout_nb_channels(channel_mask);
  72. avctx->channel_layout = channel_mask;
  73. avctx->sample_rate = sample_rate;
  74. if (bits_per_sample == 16)
  75. avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
  76. else if (bits_per_sample > 16 && bits_per_sample <= 24)
  77. avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
  78. else {
  79. av_log(avctx, AV_LOG_ERROR, "Unsupported number of bits per sample: %d\n",
  80. bits_per_sample);
  81. return AVERROR(ENOSYS);
  82. }
  83. avctx->bits_per_raw_sample = bits_per_sample;
  84. switch (profile) {
  85. case DCADEC_PROFILE_DS:
  86. avctx->profile = FF_PROFILE_DTS;
  87. break;
  88. case DCADEC_PROFILE_DS_96_24:
  89. avctx->profile = FF_PROFILE_DTS_96_24;
  90. break;
  91. case DCADEC_PROFILE_DS_ES:
  92. avctx->profile = FF_PROFILE_DTS_ES;
  93. break;
  94. case DCADEC_PROFILE_HD_HRA:
  95. avctx->profile = FF_PROFILE_DTS_HD_HRA;
  96. break;
  97. case DCADEC_PROFILE_HD_MA:
  98. avctx->profile = FF_PROFILE_DTS_HD_MA;
  99. break;
  100. case DCADEC_PROFILE_EXPRESS:
  101. avctx->profile = FF_PROFILE_DTS_EXPRESS;
  102. break;
  103. case DCADEC_PROFILE_UNKNOWN:
  104. default:
  105. avctx->profile = FF_PROFILE_UNKNOWN;
  106. break;
  107. }
  108. /* bitrate is only meaningful if there are no HD extensions, as they distort the bitrate */
  109. if (profile == DCADEC_PROFILE_DS || profile == DCADEC_PROFILE_DS_96_24 || profile == DCADEC_PROFILE_DS_ES) {
  110. struct dcadec_core_info *info = dcadec_context_get_core_info(s->ctx);
  111. avctx->bit_rate = info->bit_rate;
  112. dcadec_context_free_core_info(info);
  113. } else
  114. avctx->bit_rate = 0;
  115. #if HAVE_STRUCT_DCADEC_EXSS_INFO_MATRIX_ENCODING
  116. if (exss = dcadec_context_get_exss_info(s->ctx)) {
  117. enum AVMatrixEncoding matrix_encoding = AV_MATRIX_ENCODING_NONE;
  118. switch(exss->matrix_encoding) {
  119. case DCADEC_MATRIX_ENCODING_SURROUND:
  120. matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
  121. break;
  122. case DCADEC_MATRIX_ENCODING_HEADPHONE:
  123. matrix_encoding = AV_MATRIX_ENCODING_DOLBYHEADPHONE;
  124. break;
  125. }
  126. dcadec_context_free_exss_info(exss);
  127. if (matrix_encoding != AV_MATRIX_ENCODING_NONE &&
  128. (ret = ff_side_data_update_matrix_encoding(frame, matrix_encoding)) < 0)
  129. return ret;
  130. }
  131. #endif
  132. frame->nb_samples = nsamples;
  133. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  134. return ret;
  135. for (i = 0; i < avctx->channels; i++) {
  136. if (frame->format == AV_SAMPLE_FMT_S16P) {
  137. int16_t *plane = (int16_t *)frame->extended_data[i];
  138. for (k = 0; k < nsamples; k++)
  139. plane[k] = samples[i][k];
  140. } else {
  141. int32_t *plane = (int32_t *)frame->extended_data[i];
  142. int shift = 32 - bits_per_sample;
  143. for (k = 0; k < nsamples; k++)
  144. plane[k] = samples[i][k] << shift;
  145. }
  146. }
  147. *got_frame_ptr = 1;
  148. return avpkt->size;
  149. }
  150. static av_cold void dcadec_flush(AVCodecContext *avctx)
  151. {
  152. DCADecContext *s = avctx->priv_data;
  153. dcadec_context_clear(s->ctx);
  154. }
  155. static av_cold int dcadec_close(AVCodecContext *avctx)
  156. {
  157. DCADecContext *s = avctx->priv_data;
  158. dcadec_context_destroy(s->ctx);
  159. s->ctx = NULL;
  160. av_freep(&s->buffer);
  161. return 0;
  162. }
  163. static av_cold int dcadec_init(AVCodecContext *avctx)
  164. {
  165. DCADecContext *s = avctx->priv_data;
  166. int flags = 0;
  167. /* Affects only lossy DTS profiles. DTS-HD MA is always bitexact */
  168. if (avctx->flags & CODEC_FLAG_BITEXACT)
  169. flags |= DCADEC_FLAG_CORE_BIT_EXACT;
  170. s->ctx = dcadec_context_create(flags);
  171. if (!s->ctx)
  172. return AVERROR(ENOMEM);
  173. avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
  174. avctx->bits_per_raw_sample = 24;
  175. return 0;
  176. }
  177. static const AVProfile profiles[] = {
  178. { FF_PROFILE_DTS, "DTS" },
  179. { FF_PROFILE_DTS_ES, "DTS-ES" },
  180. { FF_PROFILE_DTS_96_24, "DTS 96/24" },
  181. { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
  182. { FF_PROFILE_DTS_HD_MA, "DTS-HD MA" },
  183. { FF_PROFILE_DTS_EXPRESS, "DTS Express" },
  184. { FF_PROFILE_UNKNOWN },
  185. };
  186. AVCodec ff_libdcadec_decoder = {
  187. .name = "libdcadec",
  188. .long_name = NULL_IF_CONFIG_SMALL("dcadec DCA decoder"),
  189. .type = AVMEDIA_TYPE_AUDIO,
  190. .id = AV_CODEC_ID_DTS,
  191. .priv_data_size = sizeof(DCADecContext),
  192. .init = dcadec_init,
  193. .decode = dcadec_decode_frame,
  194. .close = dcadec_close,
  195. .flush = dcadec_flush,
  196. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_CHANNEL_CONF,
  197. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S32P, AV_SAMPLE_FMT_S16P,
  198. AV_SAMPLE_FMT_NONE },
  199. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  200. };