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.

321 lines
10KB

  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. const AVClass *class;
  31. struct dcadec_context *ctx;
  32. uint8_t *buffer;
  33. int buffer_size;
  34. int lfe_filter;
  35. int core_only;
  36. } DCADecContext;
  37. static void my_log_cb(int level, const char *file, int line,
  38. const char *message, void *cbarg)
  39. {
  40. int av_level;
  41. switch (level) {
  42. case DCADEC_LOG_ERROR:
  43. av_level = AV_LOG_ERROR;
  44. break;
  45. case DCADEC_LOG_WARNING:
  46. av_level = AV_LOG_WARNING;
  47. break;
  48. case DCADEC_LOG_INFO:
  49. av_level = AV_LOG_INFO;
  50. break;
  51. case DCADEC_LOG_VERBOSE:
  52. av_level = AV_LOG_VERBOSE;
  53. break;
  54. case DCADEC_LOG_DEBUG:
  55. default:
  56. av_level = AV_LOG_DEBUG;
  57. break;
  58. }
  59. av_log(cbarg, av_level, "%s\n", message);
  60. }
  61. static int dcadec_decode_frame(AVCodecContext *avctx, void *data,
  62. int *got_frame_ptr, AVPacket *avpkt)
  63. {
  64. DCADecContext *s = avctx->priv_data;
  65. AVFrame *frame = data;
  66. struct dcadec_exss_info *exss;
  67. int ret, i, k;
  68. int **samples, nsamples, channel_mask, sample_rate, bits_per_sample, profile;
  69. uint32_t mrk;
  70. uint8_t *input = avpkt->data;
  71. int input_size = avpkt->size;
  72. /* convert bytestream syntax to RAW BE format if required */
  73. if (input_size < 8) {
  74. av_log(avctx, AV_LOG_ERROR, "Input size too small\n");
  75. return AVERROR_INVALIDDATA;
  76. }
  77. mrk = AV_RB32(input);
  78. if (mrk != DCA_SYNCWORD_CORE_BE && mrk != DCA_SYNCWORD_SUBSTREAM) {
  79. s->buffer = av_fast_realloc(s->buffer, &s->buffer_size, avpkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
  80. if (!s->buffer)
  81. return AVERROR(ENOMEM);
  82. for (i = 0, ret = AVERROR_INVALIDDATA; i < input_size - 3 && ret < 0; i++)
  83. ret = avpriv_dca_convert_bitstream(input + i, input_size - i, s->buffer, s->buffer_size);
  84. if (ret < 0)
  85. return ret;
  86. input = s->buffer;
  87. input_size = ret;
  88. }
  89. if ((ret = dcadec_context_parse(s->ctx, input, input_size)) < 0) {
  90. av_log(avctx, AV_LOG_ERROR, "dcadec_context_parse() failed: %d (%s)\n", -ret, dcadec_strerror(ret));
  91. return AVERROR_EXTERNAL;
  92. }
  93. if ((ret = dcadec_context_filter(s->ctx, &samples, &nsamples, &channel_mask,
  94. &sample_rate, &bits_per_sample, &profile)) < 0) {
  95. av_log(avctx, AV_LOG_ERROR, "dcadec_context_filter() failed: %d (%s)\n", -ret, dcadec_strerror(ret));
  96. return AVERROR_EXTERNAL;
  97. }
  98. avctx->channels = av_get_channel_layout_nb_channels(channel_mask);
  99. avctx->channel_layout = channel_mask;
  100. avctx->sample_rate = sample_rate;
  101. if (bits_per_sample == 16)
  102. avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
  103. else if (bits_per_sample > 16 && bits_per_sample <= 24)
  104. avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
  105. else {
  106. av_log(avctx, AV_LOG_ERROR, "Unsupported number of bits per sample: %d\n",
  107. bits_per_sample);
  108. return AVERROR(ENOSYS);
  109. }
  110. avctx->bits_per_raw_sample = bits_per_sample;
  111. switch (profile) {
  112. case DCADEC_PROFILE_DS:
  113. avctx->profile = FF_PROFILE_DTS;
  114. break;
  115. case DCADEC_PROFILE_DS_96_24:
  116. avctx->profile = FF_PROFILE_DTS_96_24;
  117. break;
  118. case DCADEC_PROFILE_DS_ES:
  119. avctx->profile = FF_PROFILE_DTS_ES;
  120. break;
  121. case DCADEC_PROFILE_HD_HRA:
  122. avctx->profile = FF_PROFILE_DTS_HD_HRA;
  123. break;
  124. case DCADEC_PROFILE_HD_MA:
  125. avctx->profile = FF_PROFILE_DTS_HD_MA;
  126. break;
  127. case DCADEC_PROFILE_EXPRESS:
  128. avctx->profile = FF_PROFILE_DTS_EXPRESS;
  129. break;
  130. case DCADEC_PROFILE_UNKNOWN:
  131. default:
  132. avctx->profile = FF_PROFILE_UNKNOWN;
  133. break;
  134. }
  135. /* bitrate is only meaningful if there are no HD extensions, as they distort the bitrate */
  136. if (profile == DCADEC_PROFILE_DS || profile == DCADEC_PROFILE_DS_96_24 || profile == DCADEC_PROFILE_DS_ES) {
  137. struct dcadec_core_info *info = dcadec_context_get_core_info(s->ctx);
  138. avctx->bit_rate = info->bit_rate;
  139. dcadec_context_free_core_info(info);
  140. } else
  141. avctx->bit_rate = 0;
  142. if (exss = dcadec_context_get_exss_info(s->ctx)) {
  143. enum AVMatrixEncoding matrix_encoding = AV_MATRIX_ENCODING_NONE;
  144. switch(exss->matrix_encoding) {
  145. case DCADEC_MATRIX_ENCODING_SURROUND:
  146. matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
  147. break;
  148. case DCADEC_MATRIX_ENCODING_HEADPHONE:
  149. matrix_encoding = AV_MATRIX_ENCODING_DOLBYHEADPHONE;
  150. break;
  151. }
  152. dcadec_context_free_exss_info(exss);
  153. if (matrix_encoding != AV_MATRIX_ENCODING_NONE &&
  154. (ret = ff_side_data_update_matrix_encoding(frame, matrix_encoding)) < 0)
  155. return ret;
  156. }
  157. frame->nb_samples = nsamples;
  158. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  159. return ret;
  160. for (i = 0; i < avctx->channels; i++) {
  161. if (frame->format == AV_SAMPLE_FMT_S16P) {
  162. int16_t *plane = (int16_t *)frame->extended_data[i];
  163. for (k = 0; k < nsamples; k++)
  164. plane[k] = samples[i][k];
  165. } else {
  166. int32_t *plane = (int32_t *)frame->extended_data[i];
  167. int shift = 32 - bits_per_sample;
  168. for (k = 0; k < nsamples; k++)
  169. plane[k] = samples[i][k] << shift;
  170. }
  171. }
  172. *got_frame_ptr = 1;
  173. return avpkt->size;
  174. }
  175. static av_cold void dcadec_flush(AVCodecContext *avctx)
  176. {
  177. DCADecContext *s = avctx->priv_data;
  178. dcadec_context_clear(s->ctx);
  179. }
  180. static av_cold int dcadec_close(AVCodecContext *avctx)
  181. {
  182. DCADecContext *s = avctx->priv_data;
  183. dcadec_context_destroy(s->ctx);
  184. s->ctx = NULL;
  185. av_freep(&s->buffer);
  186. return 0;
  187. }
  188. static av_cold int dcadec_init(AVCodecContext *avctx)
  189. {
  190. DCADecContext *s = avctx->priv_data;
  191. int flags = 0;
  192. /* Affects only lossy DTS profiles. DTS-HD MA is always bitexact */
  193. if (avctx->flags & AV_CODEC_FLAG_BITEXACT)
  194. flags |= DCADEC_FLAG_CORE_BIT_EXACT;
  195. if (avctx->err_recognition & AV_EF_EXPLODE)
  196. flags |= DCADEC_FLAG_STRICT;
  197. if (avctx->request_channel_layout) {
  198. switch (avctx->request_channel_layout) {
  199. case AV_CH_LAYOUT_STEREO:
  200. case AV_CH_LAYOUT_STEREO_DOWNMIX:
  201. flags |= DCADEC_FLAG_KEEP_DMIX_2CH;
  202. break;
  203. case AV_CH_LAYOUT_5POINT1:
  204. flags |= DCADEC_FLAG_KEEP_DMIX_6CH;
  205. break;
  206. case AV_CH_LAYOUT_NATIVE:
  207. flags |= DCADEC_FLAG_NATIVE_LAYOUT;
  208. break;
  209. default:
  210. av_log(avctx, AV_LOG_WARNING, "Invalid request_channel_layout\n");
  211. break;
  212. }
  213. }
  214. if (s->core_only)
  215. flags |= DCADEC_FLAG_CORE_ONLY;
  216. switch (s->lfe_filter) {
  217. #if DCADEC_API_VERSION >= DCADEC_VERSION_CODE(0, 1, 0)
  218. case 1:
  219. flags |= DCADEC_FLAG_CORE_LFE_IIR;
  220. break;
  221. #endif
  222. case 2:
  223. flags |= DCADEC_FLAG_CORE_LFE_FIR;
  224. break;
  225. }
  226. s->ctx = dcadec_context_create(flags);
  227. if (!s->ctx)
  228. return AVERROR(ENOMEM);
  229. dcadec_context_set_log_cb(s->ctx, my_log_cb, avctx);
  230. avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
  231. avctx->bits_per_raw_sample = 24;
  232. return 0;
  233. }
  234. #define OFFSET(x) offsetof(DCADecContext, x)
  235. #define PARAM AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  236. static const AVOption dcadec_options[] = {
  237. { "lfe_filter", "Lossy LFE channel interpolation filter", OFFSET(lfe_filter), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, PARAM, "lfe_filter" },
  238. { "default", "Library default", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, PARAM, "lfe_filter" },
  239. { "iir", "IIR filter", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, PARAM, "lfe_filter" },
  240. { "fir", "FIR filter", 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, INT_MIN, INT_MAX, PARAM, "lfe_filter" },
  241. { "core_only", "Decode core only without extensions", OFFSET(core_only), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, PARAM },
  242. { NULL }
  243. };
  244. static const AVClass dcadec_class = {
  245. .class_name = "libdcadec decoder",
  246. .item_name = av_default_item_name,
  247. .option = dcadec_options,
  248. .version = LIBAVUTIL_VERSION_INT,
  249. .category = AV_CLASS_CATEGORY_DECODER,
  250. };
  251. static const AVProfile profiles[] = {
  252. { FF_PROFILE_DTS, "DTS" },
  253. { FF_PROFILE_DTS_ES, "DTS-ES" },
  254. { FF_PROFILE_DTS_96_24, "DTS 96/24" },
  255. { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
  256. { FF_PROFILE_DTS_HD_MA, "DTS-HD MA" },
  257. { FF_PROFILE_DTS_EXPRESS, "DTS Express" },
  258. { FF_PROFILE_UNKNOWN },
  259. };
  260. AVCodec ff_libdcadec_decoder = {
  261. .name = "libdcadec",
  262. .long_name = NULL_IF_CONFIG_SMALL("dcadec DCA decoder"),
  263. .type = AVMEDIA_TYPE_AUDIO,
  264. .id = AV_CODEC_ID_DTS,
  265. .priv_data_size = sizeof(DCADecContext),
  266. .init = dcadec_init,
  267. .decode = dcadec_decode_frame,
  268. .close = dcadec_close,
  269. .flush = dcadec_flush,
  270. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
  271. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S32P, AV_SAMPLE_FMT_S16P,
  272. AV_SAMPLE_FMT_NONE },
  273. .priv_class = &dcadec_class,
  274. .profiles = NULL_IF_CONFIG_SMALL(profiles),
  275. };