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.h"
33 
35 {
36  DSDContext * s;
37  int i;
38 
40 
41  s = av_malloc_array(sizeof(DSDContext), avctx->channels);
42  if (!s)
43  return AVERROR(ENOMEM);
44 
45  for (i = 0; i < avctx->channels; i++) {
46  s[i].pos = 0;
47  memset(s[i].buf, 0x69, sizeof(s[i].buf));
48 
49  /* 0x69 = 01101001
50  * This pattern "on repeat" makes a low energy 352.8 kHz tone
51  * and a high energy 1.0584 MHz tone which should be filtered
52  * out completely by any playback system --> silence
53  */
54  }
55 
57  avctx->priv_data = s;
58  return 0;
59 }
60 
61 static int decode_frame(AVCodecContext *avctx, void *data,
62  int *got_frame_ptr, AVPacket *avpkt)
63 {
64  DSDContext * s = avctx->priv_data;
65  AVFrame *frame = data;
66  int ret, i;
67  int lsbf = avctx->codec_id == AV_CODEC_ID_DSD_LSBF || avctx->codec_id == AV_CODEC_ID_DSD_LSBF_PLANAR;
68  int src_next;
69  int src_stride;
70 
71  frame->nb_samples = avpkt->size / avctx->channels;
72 
74  src_next = frame->nb_samples;
75  src_stride = 1;
76  } else {
77  src_next = 1;
78  src_stride = avctx->channels;
79  }
80 
81  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
82  return ret;
83 
84  for (i = 0; i < avctx->channels; i++) {
85  float * dst = ((float **)frame->extended_data)[i];
86  ff_dsd2pcm_translate(&s[i], frame->nb_samples, lsbf,
87  avpkt->data + i * src_next, src_stride,
88  dst, 1);
89  }
90 
91  *got_frame_ptr = 1;
92  return frame->nb_samples * avctx->channels;
93 }
94 
95 #define DSD_DECODER(id_, name_, long_name_) \
96 AVCodec ff_##name_##_decoder = { \
97  .name = #name_, \
98  .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
99  .type = AVMEDIA_TYPE_AUDIO, \
100  .id = AV_CODEC_ID_##id_, \
101  .init = decode_init, \
102  .decode = decode_frame, \
103  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP, \
104  AV_SAMPLE_FMT_NONE }, \
105 };
106 
107 DSD_DECODER(DSD_LSBF, dsd_lsbf, "DSD (Direct Stream Digital), least significant bit first")
108 DSD_DECODER(DSD_MSBF, dsd_msbf, "DSD (Direct Stream Digital), most significant bit first")
109 DSD_DECODER(DSD_MSBF_PLANAR, dsd_msbf_planar, "DSD (Direct Stream Digital), most significant bit first, planar")
110 DSD_DECODER(DSD_LSBF_PLANAR, dsd_lsbf_planar, "DSD (Direct Stream Digital), least significant bit first, planar")
float, planar
Definition: samplefmt.h:69
const char * s
Definition: avisynth_c.h:631
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
int size
Definition: avcodec.h:1581
av_cold void ff_init_dsd_data(void)
Definition: dsd.c:46
#define DSD_DECODER(id_, name_, long_name_)
Definition: dsddec.c:95
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2418
#define av_cold
Definition: attributes.h:82
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1580
#define AVERROR(e)
Definition: error.h:43
Per-channel buffer.
Definition: dsd.h:42
void ff_dsd2pcm_translate(DSDContext *s, size_t samples, int lsbf, const unsigned char *src, ptrdiff_t src_stride, float *dst, ptrdiff_t dst_stride)
Definition: dsd.c:55
Libavcodec external API header.
enum AVCodecID codec_id
Definition: avcodec.h:1666
main external API structure.
Definition: avcodec.h:1649
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:928
void * buf
Definition: avisynth_c.h:553
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: dsddec.c:61
unsigned pos
Definition: dsd.h:44
common internal api header.
void * priv_data
Definition: avcodec.h:1691
int channels
number of audio channels
Definition: avcodec.h:2411
static av_cold int decode_init(AVCodecContext *avctx)
Definition: dsddec.c:34
#define av_malloc_array(a, b)
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:231
This structure stores compressed data.
Definition: avcodec.h:1557
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:241