FFmpeg
Loading...
Searching...
No Matches
dsddec.c
Go to the documentation of this file.
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
45{
46 DSDContext * s;
47 int i;
48 uint8_t silence;
49
50 if (!avctx->ch_layout.nb_channels)
52
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 ||
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
67 avctx->priv_data = s;
68 return 0;
69}
70
71typedef struct ThreadData {
75
76static 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
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
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_) \
121const 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};
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")
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
static AVFrame * frame
av_cold void ff_init_dsd_data(void)
Definition dsd.c:92
void ff_dsd2pcm_translate(DSDContext *s, size_t samples, int lsbf, const uint8_t *src, ptrdiff_t src_stride, float *dst, ptrdiff_t dst_stride)
Definition dsd.c:98
#define DSD_SILENCE_REVERSED
Definition dsddec.c:37
#define DSD_SILENCE
Definition dsddec.c:36
static int decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition dsddec.c:101
static av_cold int decode_init(AVCodecContext *avctx)
Definition dsddec.c:44
#define DSD_DECODER(id_, name_, long_name_)
Definition dsddec.c:120
static int dsd_channel(AVCodecContext *avctx, void *tdata, int j, int threadnr)
Definition dsddec.c:76
@ AV_CODEC_ID_DSD_LSBF
Definition codec_id.h:526
@ AV_CODEC_ID_DSD_MSBF_PLANAR
Definition codec_id.h:529
@ AV_CODEC_ID_DSD_LSBF_PLANAR
Definition codec_id.h:528
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
#define av_cold
Definition attributes.h:117
Memory handling functions.
int nb_channels
Number of channels in this layout.
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
int(* execute2)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg, int jobnr, int threadnr), void *arg2, int *ret, int count)
The codec may call this to execute several independent things.
Definition avcodec.h:1628
enum AVCodecID codec_id
Definition avcodec.h:453
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
Per-channel buffer.
Definition dsd.h:41
Used for passing data between threads.
Definition dsddec.c:71
const AVPacket * avpkt
Definition dsddec.c:73
AVFrame * frame
Definition dsddec.c:72
#define av_malloc_array(a, b)