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.

168 lines
5.0KB

  1. /*
  2. * Direct Stream Digital (DSD) decoder
  3. * based on BSD licensed dsd2pcm by Sebastian Gesemann
  4. * Copyright (c) 2009, 2011 Sebastian Gesemann. All rights reserved.
  5. * Copyright (c) 2014 Peter Ross
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /**
  24. * @file
  25. * Direct Stream Digital (DSD) decoder
  26. */
  27. #include "libavcodec/internal.h"
  28. #include "libavcodec/mathops.h"
  29. #include "avcodec.h"
  30. #include "dsd_tablegen.h"
  31. #define FIFOSIZE 16 /** must be a power of two */
  32. #define FIFOMASK (FIFOSIZE - 1) /** bit mask for FIFO offsets */
  33. #if FIFOSIZE * 8 < HTAPS * 2
  34. #error "FIFOSIZE too small"
  35. #endif
  36. /**
  37. * Per-channel buffer
  38. */
  39. typedef struct {
  40. unsigned char buf[FIFOSIZE];
  41. unsigned pos;
  42. } DSDContext;
  43. static void dsd2pcm_translate(DSDContext* s, size_t samples, int lsbf,
  44. const unsigned char *src, ptrdiff_t src_stride,
  45. float *dst, ptrdiff_t dst_stride)
  46. {
  47. unsigned pos, i;
  48. unsigned char* p;
  49. double sum;
  50. pos = s->pos;
  51. while (samples-- > 0) {
  52. s->buf[pos] = lsbf ? ff_reverse[*src] : *src;
  53. src += src_stride;
  54. p = s->buf + ((pos - CTABLES) & FIFOMASK);
  55. *p = ff_reverse[*p];
  56. sum = 0.0;
  57. for (i = 0; i < CTABLES; i++) {
  58. unsigned char a = s->buf[(pos - i) & FIFOMASK];
  59. unsigned char b = s->buf[(pos - (CTABLES*2 - 1) + i) & FIFOMASK];
  60. sum += ctables[i][a] + ctables[i][b];
  61. }
  62. *dst = (float)sum;
  63. dst += dst_stride;
  64. pos = (pos + 1) & FIFOMASK;
  65. }
  66. s->pos = pos;
  67. }
  68. static av_cold void init_static_data(void)
  69. {
  70. static int done = 0;
  71. if (done)
  72. return;
  73. dsd_ctables_tableinit();
  74. done = 1;
  75. }
  76. static av_cold int decode_init(AVCodecContext *avctx)
  77. {
  78. DSDContext * s;
  79. int i;
  80. init_static_data();
  81. s = av_malloc_array(sizeof(DSDContext), avctx->channels);
  82. if (!s)
  83. return AVERROR(ENOMEM);
  84. for (i = 0; i < avctx->channels; i++) {
  85. s[i].pos = 0;
  86. memset(s[i].buf, 0x69, sizeof(s[i].buf));
  87. /* 0x69 = 01101001
  88. * This pattern "on repeat" makes a low energy 352.8 kHz tone
  89. * and a high energy 1.0584 MHz tone which should be filtered
  90. * out completely by any playback system --> silence
  91. */
  92. }
  93. avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
  94. avctx->priv_data = s;
  95. return 0;
  96. }
  97. static int decode_frame(AVCodecContext *avctx, void *data,
  98. int *got_frame_ptr, AVPacket *avpkt)
  99. {
  100. DSDContext * s = avctx->priv_data;
  101. AVFrame *frame = data;
  102. int ret, i;
  103. int lsbf = avctx->codec_id == AV_CODEC_ID_DSD_LSBF || avctx->codec_id == AV_CODEC_ID_DSD_LSBF_PLANAR;
  104. int src_next;
  105. int src_stride;
  106. frame->nb_samples = avpkt->size / avctx->channels;
  107. if (avctx->codec_id == AV_CODEC_ID_DSD_LSBF_PLANAR || avctx->codec_id == AV_CODEC_ID_DSD_MSBF_PLANAR) {
  108. src_next = frame->nb_samples;
  109. src_stride = 1;
  110. } else {
  111. src_next = 1;
  112. src_stride = avctx->channels;
  113. }
  114. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  115. return ret;
  116. for (i = 0; i < avctx->channels; i++) {
  117. float * dst = ((float **)frame->extended_data)[i];
  118. dsd2pcm_translate(&s[i], frame->nb_samples, lsbf,
  119. avpkt->data + i * src_next, src_stride,
  120. dst, 1);
  121. }
  122. *got_frame_ptr = 1;
  123. return frame->nb_samples * avctx->channels;
  124. }
  125. #define DSD_DECODER(id_, name_, long_name_) \
  126. AVCodec ff_##name_##_decoder = { \
  127. .name = #name_, \
  128. .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
  129. .type = AVMEDIA_TYPE_AUDIO, \
  130. .id = AV_CODEC_ID_##id_, \
  131. .init = decode_init, \
  132. .decode = decode_frame, \
  133. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP, \
  134. AV_SAMPLE_FMT_NONE }, \
  135. };
  136. DSD_DECODER(DSD_LSBF, dsd_lsbf, "DSD (Direct Stream Digital), least significant bit first")
  137. DSD_DECODER(DSD_MSBF, dsd_msbf, "DSD (Direct Stream Digital), most significant bit first")
  138. DSD_DECODER(DSD_MSBF_PLANAR, dsd_msbf_planar, "DSD (Direct Stream Digital), most significant bit first, planar")
  139. DSD_DECODER(DSD_LSBF_PLANAR, dsd_lsbf_planar, "DSD (Direct Stream Digital), least significant bit first, planar")