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/** 25 * @file 26 * Direct Stream Digital (DSD) decoder 27 */ 28 29#include "libavcodec/internal.h" 30#include "avcodec.h" 31#include "codec_internal.h" 32#include "dsd.h" 33 34#define DSD_SILENCE 0x69 35#define DSD_SILENCE_REVERSED 0x96 36/* 0x69 = 01101001 37 * This pattern "on repeat" makes a low energy 352.8 kHz tone 38 * and a high energy 1.0584 MHz tone which should be filtered 39 * out completely by any playback system --> silence 40 */ 41 42static av_cold int decode_init(AVCodecContext *avctx) 43{ 44 DSDContext * s; 45 int i; 46 uint8_t silence; 47 48 if (!avctx->ch_layout.nb_channels) 49 return AVERROR_INVALIDDATA; 50 51 ff_init_dsd_data(); 52 53 s = av_malloc_array(sizeof(DSDContext), avctx->ch_layout.nb_channels); 54 if (!s) 55 return AVERROR(ENOMEM); 56 57 silence = avctx->codec_id == AV_CODEC_ID_DSD_LSBF || avctx->codec_id == AV_CODEC_ID_DSD_LSBF_PLANAR ? DSD_SILENCE_REVERSED : DSD_SILENCE; 58 for (i = 0; i < avctx->ch_layout.nb_channels; i++) { 59 s[i].pos = 0; 60 memset(s[i].buf, silence, sizeof(s[i].buf)); 61 } 62 63 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP; 64 avctx->priv_data = s; 65 return 0; 66} 67 68typedef struct ThreadData { 69 AVFrame *frame; 70 const AVPacket *avpkt; 71} ThreadData; 72 73static int dsd_channel(AVCodecContext *avctx, void *tdata, int j, int threadnr) 74{ 75 int lsbf = avctx->codec_id == AV_CODEC_ID_DSD_LSBF || avctx->codec_id == AV_CODEC_ID_DSD_LSBF_PLANAR; 76 DSDContext *s = avctx->priv_data; 77 ThreadData *td = tdata; 78 AVFrame *frame = td->frame; 79 const AVPacket *avpkt = td->avpkt; 80 int src_next, src_stride; 81 float *dst = ((float **)frame->extended_data)[j]; 82 83 if (avctx->codec_id == AV_CODEC_ID_DSD_LSBF_PLANAR || avctx->codec_id == AV_CODEC_ID_DSD_MSBF_PLANAR) { 84 src_next = frame->nb_samples; 85 src_stride = 1; 86 } else { 87 src_next = 1; 88 src_stride = avctx->ch_layout.nb_channels; 89 } 90 91 ff_dsd2pcm_translate(&s[j], frame->nb_samples, lsbf, 92 avpkt->data + j * src_next, src_stride, 93 dst, 1); 94 95 return 0; 96} 97 98static int decode_frame(AVCodecContext *avctx, AVFrame *frame, 99 int *got_frame_ptr, AVPacket *avpkt) 100{ 101 ThreadData td; 102 int ret; 103 104 frame->nb_samples = avpkt->size / avctx->ch_layout.nb_channels; 105 106 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) 107 return ret; 108 109 td.frame = frame; 110 td.avpkt = avpkt; 111 avctx->execute2(avctx, dsd_channel, &td, NULL, avctx->ch_layout.nb_channels); 112 113 *got_frame_ptr = 1; 114 return frame->nb_samples * avctx->ch_layout.nb_channels; 115} 116 117#define DSD_DECODER(id_, name_, long_name_) \ 118const FFCodec ff_ ## name_ ## _decoder = { \ 119 .p.name = #name_, \ 120 .p.long_name = NULL_IF_CONFIG_SMALL(long_name_), \ 121 .p.type = AVMEDIA_TYPE_AUDIO, \ 122 .p.id = AV_CODEC_ID_##id_, \ 123 .init = decode_init, \ 124 FF_CODEC_DECODE_CB(decode_frame), \ 125 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SLICE_THREADS, \ 126 .p.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP, \ 127 AV_SAMPLE_FMT_NONE }, \ 128 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE, \ 129}; 130 131DSD_DECODER(DSD_LSBF, dsd_lsbf, "DSD (Direct Stream Digital), least significant bit first") 132DSD_DECODER(DSD_MSBF, dsd_msbf, "DSD (Direct Stream Digital), most significant bit first") 133DSD_DECODER(DSD_MSBF_PLANAR, dsd_msbf_planar, "DSD (Direct Stream Digital), most significant bit first, planar") 134DSD_DECODER(DSD_LSBF_PLANAR, dsd_lsbf_planar, "DSD (Direct Stream Digital), least significant bit first, planar") 135