FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "libavcodec/internal.h"
30 #include "libavcodec/mathops.h"
31 #include "avcodec.h"
32 #include "dsd_tablegen.h"
33 
34 #define FIFOSIZE 16 /** must be a power of two */
35 #define FIFOMASK (FIFOSIZE - 1) /** bit mask for FIFO offsets */
36 
37 #if FIFOSIZE * 8 < HTAPS * 2
38 #error "FIFOSIZE too small"
39 #endif
40 
41 /**
42  * Per-channel buffer
43  */
44 typedef struct {
45  unsigned char buf[FIFOSIZE];
46  unsigned pos;
47 } DSDContext;
48 
49 static void dsd2pcm_translate(DSDContext* s, size_t samples, int lsbf,
50  const unsigned char *src, ptrdiff_t src_stride,
51  float *dst, ptrdiff_t dst_stride)
52 {
53  unsigned pos, i;
54  unsigned char* p;
55  double sum;
56 
57  pos = s->pos;
58 
59  while (samples-- > 0) {
60  s->buf[pos] = lsbf ? ff_reverse[*src] : *src;
61  src += src_stride;
62 
63  p = s->buf + ((pos - CTABLES) & FIFOMASK);
64  *p = ff_reverse[*p];
65 
66  sum = 0.0;
67  for (i = 0; i < CTABLES; i++) {
68  unsigned char a = s->buf[(pos - i) & FIFOMASK];
69  unsigned char b = s->buf[(pos - (CTABLES*2 - 1) + i) & FIFOMASK];
70  sum += ctables[i][a] + ctables[i][b];
71  }
72 
73  *dst = (float)sum;
74  dst += dst_stride;
75 
76  pos = (pos + 1) & FIFOMASK;
77  }
78 
79  s->pos = pos;
80 }
81 
82 static av_cold void init_static_data(void)
83 {
84  static int done = 0;
85  if (done)
86  return;
88  done = 1;
89 }
90 
92 {
93  DSDContext * s;
94  int i;
95 
97 
98  s = av_malloc_array(sizeof(DSDContext), avctx->channels);
99  if (!s)
100  return AVERROR(ENOMEM);
101 
102  for (i = 0; i < avctx->channels; i++) {
103  s[i].pos = 0;
104  memset(s[i].buf, 0x69, sizeof(s[i].buf));
105 
106  /* 0x69 = 01101001
107  * This pattern "on repeat" makes a low energy 352.8 kHz tone
108  * and a high energy 1.0584 MHz tone which should be filtered
109  * out completely by any playback system --> silence
110  */
111  }
112 
114  avctx->priv_data = s;
115  return 0;
116 }
117 
118 static int decode_frame(AVCodecContext *avctx, void *data,
119  int *got_frame_ptr, AVPacket *avpkt)
120 {
121  DSDContext * s = avctx->priv_data;
122  AVFrame *frame = data;
123  int ret, i;
124  int lsbf = avctx->codec_id == AV_CODEC_ID_DSD_LSBF || avctx->codec_id == AV_CODEC_ID_DSD_LSBF_PLANAR;
125  int src_next;
126  int src_stride;
127 
128  frame->nb_samples = avpkt->size / avctx->channels;
129 
131  src_next = frame->nb_samples;
132  src_stride = 1;
133  } else {
134  src_next = 1;
135  src_stride = avctx->channels;
136  }
137 
138  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
139  return ret;
140 
141  for (i = 0; i < avctx->channels; i++) {
142  float * dst = ((float **)frame->extended_data)[i];
143  dsd2pcm_translate(&s[i], frame->nb_samples, lsbf,
144  avpkt->data + i * src_next, src_stride,
145  dst, 1);
146  }
147 
148  *got_frame_ptr = 1;
149  return frame->nb_samples * avctx->channels;
150 }
151 
152 #define DSD_DECODER(id_, name_, long_name_) \
153 AVCodec ff_##name_##_decoder = { \
154  .name = #name_, \
155  .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
156  .type = AVMEDIA_TYPE_AUDIO, \
157  .id = AV_CODEC_ID_##id_, \
158  .init = decode_init, \
159  .decode = decode_frame, \
160  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP, \
161  AV_SAMPLE_FMT_NONE }, \
162 };
163 
164 DSD_DECODER(DSD_LSBF, dsd_lsbf, "DSD (Direct Stream Digital), least significant bit first")
165 DSD_DECODER(DSD_MSBF, dsd_msbf, "DSD (Direct Stream Digital), most significant bit first")
166 DSD_DECODER(DSD_MSBF_PLANAR, dsd_msbf_planar, "DSD (Direct Stream Digital), most significant bit first, planar")
167 DSD_DECODER(DSD_LSBF_PLANAR, dsd_lsbf_planar, "DSD (Direct Stream Digital), least significant bit first, planar")
float, planar
Definition: samplefmt.h:70
const char * s
Definition: avisynth_c.h:631
#define CTABLES
Definition: dsd_tablegen.h:30
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int size
Definition: avcodec.h:1163
const char * b
Definition: vf_curves.c:109
#define DSD_DECODER(id_, name_, long_name_)
Definition: dsddec.c:152
#define FIFOMASK
Definition: dsddec.c:35
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1993
#define av_cold
Definition: attributes.h:74
static av_cold void init_static_data(void)
Definition: dsddec.c:82
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1162
static float ctables[CTABLES][256]
Definition: dsd_tablegen.h:77
#define AVERROR(e)
Definition: error.h:43
Libavcodec external API header.
static void dsd2pcm_translate(DSDContext *s, size_t samples, int lsbf, const unsigned char *src, ptrdiff_t src_stride, float *dst, ptrdiff_t dst_stride)
Definition: dsddec.c:49
ret
Definition: avfilter.c:974
#define FIFOSIZE
Definition: dsddec.c:34
Per-channel buffer.
Definition: dsddec.c:44
AVS_Value src
Definition: avisynth_c.h:482
enum AVCodecID codec_id
Definition: avcodec.h:1258
main external API structure.
Definition: avcodec.h:1241
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:1035
void * buf
Definition: avisynth_c.h:553
static av_cold void dsd_ctables_tableinit(void)
Definition: dsd_tablegen.h:79
unsigned char buf[FIFOSIZE]
Definition: dsddec.c:45
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: dsddec.c:118
unsigned pos
Definition: dsddec.c:46
common internal api header.
void * priv_data
Definition: avcodec.h:1283
int channels
number of audio channels
Definition: avcodec.h:1986
static av_cold int decode_init(AVCodecContext *avctx)
Definition: dsddec.c:91
const uint8_t ff_reverse[256]
Definition: mathtables.c:74
#define av_malloc_array(a, b)
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:215
This structure stores compressed data.
Definition: avcodec.h:1139
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:225