FFmpeg
dsd.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 #include "libavutil/thread.h"
25 #include "libavcodec/internal.h"
26 #include "libavcodec/mathops.h"
27 #include "avcodec.h"
28 #include "dsd_tablegen.h"
29 #include "dsd.h"
30 
31 static av_cold void dsd_ctables_tableinit(void)
32 {
33  int t, e, m, sign;
34  double acc[CTABLES];
35  for (e = 0; e < 256; ++e) {
36  memset(acc, 0, sizeof(acc));
37  for (m = 0; m < 8; ++m) {
38  sign = (((e >> (7 - m)) & 1) * 2 - 1);
39  for (t = 0; t < CTABLES; ++t)
40  acc[t] += sign * htaps[t * 8 + m];
41  }
42  for (t = 0; t < CTABLES; ++t)
43  ctables[CTABLES - 1 - t][e] = acc[t];
44  }
45 }
46 
48 {
49  static AVOnce init_static_once = AV_ONCE_INIT;
50  ff_thread_once(&init_static_once, dsd_ctables_tableinit);
51 }
52 
53 void ff_dsd2pcm_translate(DSDContext* s, size_t samples, int lsbf,
54  const uint8_t *src, ptrdiff_t src_stride,
55  float *dst, ptrdiff_t dst_stride)
56 {
57  uint8_t buf[FIFOSIZE];
58  unsigned pos, i;
59  uint8_t* p;
60  double sum;
61 
62  pos = s->pos;
63 
64  memcpy(buf, s->buf, sizeof(buf));
65 
66  while (samples-- > 0) {
67  buf[pos] = lsbf ? ff_reverse[*src] : *src;
68  src += src_stride;
69 
70  p = buf + ((pos - CTABLES) & FIFOMASK);
71  *p = ff_reverse[*p];
72 
73  sum = 0.0;
74  for (i = 0; i < CTABLES; i++) {
75  uint8_t a = buf[(pos - i) & FIFOMASK];
76  uint8_t b = buf[(pos - (CTABLES*2 - 1) + i) & FIFOMASK];
77  sum += ctables[i][a] + ctables[i][b];
78  }
79 
80  *dst = (float)sum;
81  dst += dst_stride;
82 
83  pos = (pos + 1) & FIFOMASK;
84  }
85 
86  s->pos = pos;
87  memcpy(s->buf, buf, sizeof(buf));
88 }
acc
int acc
Definition: yuv2rgb.c:555
thread.h
internal.h
b
#define b
Definition: input.c:41
ff_reverse
const uint8_t ff_reverse[256]
Definition: reverse.c:23
dsd.h
CTABLES
#define CTABLES
Definition: dsd_tablegen.h:31
FIFOMASK
#define FIFOMASK
Definition: dsd.h:33
ff_thread_once
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:175
av_cold
#define av_cold
Definition: attributes.h:90
s
#define s(width, name)
Definition: cbs_vp9.c:257
ctables
static float ctables[CTABLES][256]
Definition: dsd_tablegen.h:74
AV_ONCE_INIT
#define AV_ONCE_INIT
Definition: thread.h:173
src
#define src
Definition: vp8dsp.c:255
mathops.h
AVOnce
#define AVOnce
Definition: thread.h:172
ff_init_dsd_data
av_cold void ff_init_dsd_data(void)
Definition: dsd.c:47
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
i
int i
Definition: input.c:407
uint8_t
uint8_t
Definition: audio_convert.c:194
dsd_ctables_tableinit
static av_cold void dsd_ctables_tableinit(void)
Definition: dsd.c:31
DSDContext
Per-channel buffer.
Definition: dsd.h:42
avcodec.h
ff_dsd2pcm_translate
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:53
pos
unsigned int pos
Definition: spdifenc.c:412
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
FIFOSIZE
#define FIFOSIZE
Definition: dsd.h:32
htaps
static const double htaps[HTAPS]
The 2nd half (48 coeffs) of a 96-tap symmetric lowpass filter.
Definition: dsd_tablegen.h:55
dsd_tablegen.h