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