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.

117 lines
3.9KB

  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.h"
  31. #define DSD_SILENCE 0x69
  32. /* 0x69 = 01101001
  33. * This pattern "on repeat" makes a low energy 352.8 kHz tone
  34. * and a high energy 1.0584 MHz tone which should be filtered
  35. * out completely by any playback system --> silence
  36. */
  37. static av_cold int decode_init(AVCodecContext *avctx)
  38. {
  39. DSDContext * s;
  40. int i;
  41. uint8_t silence;
  42. if (!avctx->channels)
  43. return AVERROR_INVALIDDATA;
  44. ff_init_dsd_data();
  45. s = av_malloc_array(sizeof(DSDContext), avctx->channels);
  46. if (!s)
  47. return AVERROR(ENOMEM);
  48. silence = avctx->codec_id == AV_CODEC_ID_DSD_LSBF || avctx->codec_id == AV_CODEC_ID_DSD_LSBF_PLANAR ? ff_reverse[DSD_SILENCE] : DSD_SILENCE;
  49. for (i = 0; i < avctx->channels; i++) {
  50. s[i].pos = 0;
  51. memset(s[i].buf, silence, sizeof(s[i].buf));
  52. }
  53. avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
  54. avctx->priv_data = s;
  55. return 0;
  56. }
  57. static int decode_frame(AVCodecContext *avctx, void *data,
  58. int *got_frame_ptr, AVPacket *avpkt)
  59. {
  60. DSDContext * s = avctx->priv_data;
  61. AVFrame *frame = data;
  62. int ret, i;
  63. int lsbf = avctx->codec_id == AV_CODEC_ID_DSD_LSBF || avctx->codec_id == AV_CODEC_ID_DSD_LSBF_PLANAR;
  64. int src_next;
  65. int src_stride;
  66. frame->nb_samples = avpkt->size / avctx->channels;
  67. if (avctx->codec_id == AV_CODEC_ID_DSD_LSBF_PLANAR || avctx->codec_id == AV_CODEC_ID_DSD_MSBF_PLANAR) {
  68. src_next = frame->nb_samples;
  69. src_stride = 1;
  70. } else {
  71. src_next = 1;
  72. src_stride = avctx->channels;
  73. }
  74. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  75. return ret;
  76. for (i = 0; i < avctx->channels; i++) {
  77. float * dst = ((float **)frame->extended_data)[i];
  78. ff_dsd2pcm_translate(&s[i], frame->nb_samples, lsbf,
  79. avpkt->data + i * src_next, src_stride,
  80. dst, 1);
  81. }
  82. *got_frame_ptr = 1;
  83. return frame->nb_samples * avctx->channels;
  84. }
  85. #define DSD_DECODER(id_, name_, long_name_) \
  86. AVCodec ff_##name_##_decoder = { \
  87. .name = #name_, \
  88. .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
  89. .type = AVMEDIA_TYPE_AUDIO, \
  90. .id = AV_CODEC_ID_##id_, \
  91. .init = decode_init, \
  92. .decode = decode_frame, \
  93. .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP, \
  94. AV_SAMPLE_FMT_NONE }, \
  95. };
  96. DSD_DECODER(DSD_LSBF, dsd_lsbf, "DSD (Direct Stream Digital), least significant bit first")
  97. DSD_DECODER(DSD_MSBF, dsd_msbf, "DSD (Direct Stream Digital), most significant bit first")
  98. DSD_DECODER(DSD_MSBF_PLANAR, dsd_msbf_planar, "DSD (Direct Stream Digital), most significant bit first, planar")
  99. DSD_DECODER(DSD_LSBF_PLANAR, dsd_lsbf_planar, "DSD (Direct Stream Digital), least significant bit first, planar")