summaryrefslogtreecommitdiffstats
path: root/akode/plugins/mpc_decoder/mpc_decoder.cpp
blob: a355ff4e919894e7ca4ed5311655da32e66c8a8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*  aKode: Musepack(MPC) Decoder

    Copyright (C) 2004 Allan Sandfeld Jensen <kde@carewolf.com>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "config.h"
//#ifdef AKODE_COMPILE_MPC

#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#else
#include <stdint.h>
#endif

#include "file.h"
#include "audioframe.h"

#include "mpc_dec.h"

#include "mpc_decoder.h"

namespace aKode {

class MPC_reader_impl : public MPC_reader
{
public:
	MPC_reader_impl(aKode::File *file, bool p_seekable) : m_file(file), m_seekable(p_seekable)
	{
            m_file->openRO();
            m_file->fadvise();
	}
        virtual ~MPC_reader_impl() {
            m_file->close();
        }
	size_t read ( void *ptr, size_t size ) {return m_file->read((char*)ptr,size);}
	bool seek ( int offset, int origin ) {return m_file->seek(offset,origin);}
	long tell () {return m_file->position();}
	long get_size () {return m_file->length();}
	bool canseek() {return m_file->seekable();}
private:
	aKode::File * m_file;
	bool m_seekable;
};

bool MPCDecoderPlugin::canDecode(File* src) {
    MPC_reader_impl reader(src, true);
    StreamInfo si;

    int r = si.ReadStreamInfo(&reader);
    return r==0;
}

extern "C" { MPCDecoderPlugin mpc_decoder; }

struct MPCDecoder::private_data
{
    private_data(File *src) : reader(src,true), decoder(&reader),
                              initialized(false), buffer(0), position(0),
                              eof(false), error(false)
    {};
    MPC_reader_impl reader;
    StreamInfo si;
    MPC_decoder decoder;

    bool initialized;
    MPC_SAMPLE_FORMAT *buffer;

    long position;
    bool eof;
    bool error;

    AudioConfiguration config;
};

MPCDecoder::MPCDecoder(File *src) {
    m_data = new private_data(src);
}

MPCDecoder::~MPCDecoder() {
    if (m_data->initialized)
        delete[] m_data->buffer;
    delete m_data;
}

void MPCDecoder::initialize() {
    if (m_data->initialized) return;

    m_data->si.ReadStreamInfo(&m_data->reader);
    m_data->error = !m_data->decoder.Initialize(&m_data->si);
    m_data->buffer = new MPC_SAMPLE_FORMAT[MPC_decoder::DecodeBufferLength];
    m_data->initialized = true;

    m_data->config.channels = m_data->si.simple.Channels;
    m_data->config.sample_rate = m_data->si.simple.SampleFreq;
    m_data->config.sample_width = -32;

    if (m_data->config.channels <=2)
        m_data->config.channel_config = MonoStereo;
    else
        m_data->config.channel_config = MultiChannel;

}
/*
template<int bits>
static inline int32_t scale(float sample)
{
  static const int32_t max = (1<<(bits-1))-1;

  // scale
  sample *= max;

  // round
  sample += 0.5;

  // clip
  if (sample > max)
    sample = max;
  else
  if (sample < -max)
    sample = -max;

  return (int32_t)sample;
}*/

bool MPCDecoder::readFrame(AudioFrame* frame) {
    if (!m_data->initialized) initialize();

    int status = m_data->decoder.Decode(m_data->buffer);

    if (status == -1) {
        m_data->error=true;
        return false;
    }
    if (status == 0) {
        m_data->eof=true;
        return false;
    }

    int channels = m_data->config.channels;
    long length = status;
    frame->reserveSpace(&m_data->config, length);
    m_data->position+=length;

    // Demux into frame
    /*
    int16_t** data = (int16_t**)frame->data;
    for(int i=0; i<length; i++)
        for(int j=0; j<channels; j++) {
            data[j][i] = scale<16>(m_data->buffer[i*channels+j]);
        }*/

    float** data = (float**)frame->data;
    for(int i=0; i<length; i++)
        for(int j=0; j<channels; j++)
            data[j][i] = m_data->buffer[i*channels+j];

    frame->pos = position();
    return true;
}

long MPCDecoder::length() {
    if (!m_data->initialized) return -1;
    return (long)(m_data->si.GetLength()*1000.0);
}

long MPCDecoder::position() {
    if (!m_data->initialized) return -1;
    float mpcpos = ((float)m_data->position)/(float)m_data->si.simple.SampleFreq;
    return (long)(mpcpos*1000.0);
}

bool MPCDecoder::eof() {
    return m_data->eof;
}

bool MPCDecoder::error() {
    return m_data->error;
}

bool MPCDecoder::seekable() {
    return m_data->reader.canseek();
}

bool MPCDecoder::seek(long pos) {
    if (!m_data->initialized) return false;

    long samplepos = (long)((pos*(float)m_data->si.simple.SampleFreq)/1000.0);

    if (m_data->decoder.SeekSample(samplepos)) {
        m_data->position = samplepos;
        return true;
    }
    else
        return false;
}

const AudioConfiguration* MPCDecoder::audioConfiguration() {
    return &m_data->config;
}

} // namespace

//#endif