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.

206 lines
6.6KB

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