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