]> git.sesse.net Git - ffmpeg/blob - libavcodec/dsddec.c
avcodec/movtextdec: Fix decode_styl() cleanup
[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 static av_cold int decode_init(AVCodecContext *avctx)
35 {
36     DSDContext * s;
37     int i;
38
39     ff_init_dsd_data();
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
56     avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
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
73     if (avctx->codec_id == AV_CODEC_ID_DSD_LSBF_PLANAR || avctx->codec_id == AV_CODEC_ID_DSD_MSBF_PLANAR) {
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")