File: FlacEngine.cpp

package info (click to toggle)
alsaplayer 0.99.76-9%2Betch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 4,752 kB
  • ctags: 2,970
  • sloc: ansic: 16,775; sh: 10,709; cpp: 9,442; makefile: 694
file content (329 lines) | stat: -rw-r--r-- 8,622 bytes parent folder | download | duplicates (4)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
//---------------------------------------------------------------------------
//  This class handles the interface between flac audio streams and
//  AlsaPlayer buffers.
//
//  Copyright (c) 2002 by Drew Hess <dhess@bothan.net>
//
//  Based on wav_engine.c (C) 1999 by Andy Lo A Foe
//  and vorbis_engine.c (C) 2002 by Andy Lo A Foe
//  and mad_engine.c (C) 2002 by Andy Lo A Foe
//  and the flac xmms plugin (C) 2001 by Josh Coalson
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//    This program 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 General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program; if not, write to the Free Software
//    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//--------------------------------------------------------------------------


#include "FlacEngine.h"

#include <iostream>
#include <cmath>
#include <cstring>
#include "FlacStream.h"
#include "CorePlayer.h"
#include "alsaplayer_error.h"

namespace Flac
{

FlacEngine::FlacEngine (FlacStream * f)
    : _f (f),
      _buf (0),
      _apFramesPerFlacFrame (1),
      _currSamp (0),
      _currApFrame (0),
      _lastDecodedFrame (-1)
{
} // FlacEngine::FlacEngine


FlacEngine::FlacEngine ()
    : _f (0),
      _buf (0),
      _apFramesPerFlacFrame (1),
      _currSamp (0),
      _currApFrame (0),
      _lastDecodedFrame (-1)
{
} // FlacEngine::FlacEngine


FlacEngine::~FlacEngine ()
{
    delete [] _buf;
    _buf = 0;

    // don't delete _f

} // FlacEngine::~FlacEngine


bool
FlacEngine::init ()
{
    // Calculate the number of AlsaPlayer frames in a flac frame.
    // This number must be chosen such that apFrameSize is no greater
    // than BUF_SIZE (see alsaplayer/CorePlayer.h).  It should
    // also be a power of 2 so that it nicely divides the stream's
    // samplesPerBlock without fractions, in order to prevent rounding
    // errors when converting AlsaPlayer frame numbers to flac sample
    // numbers and vice versa.
    //
    // I believe this algorithm meets those 2 criteria for all valid
    // block sizes in flac 1.02.

    int tryme;
    for (tryme = 1; tryme <= 32; tryme *= 2)
    {
	if ((_f->samplesPerBlock () * AP_CHANNELS * AP_BYTES_PER_SAMPLE /
	     tryme) <= BUF_SIZE)
	    break;
    }
    if (tryme <= 32)
    {
	_apFramesPerFlacFrame = tryme;
	return true;
    } else {
	// ugh, give up, too big
	alsaplayer_error("FlacEngine::init(): frame size too big"); 
	return false;
    }
} // FlacEngine::init


int
FlacEngine::apFrameSize () const
{
    if (!_f)
	return 0;

    // AlsaPlayer wants us to return a fixed-size audio frame,
    // regardless of what's actually in the audio stream.
    //
    // The flac block size is in units of samples. Each flac frame
    // we decode contains a block of samples for each channel in the
    // audio stream.
    //
    // The AlsaPlayer frame size is in units of bytes.  If the
    // audio is mono, we'll have to copy the data from the single
    // flac channel into the 2nd AlsaPlayer channel.
    //
    // So the AlsaPlayer frame size is the flack block size (samples
    // per channel) times AP_CHANNELS times AP_BYTES_PER_SAMPLE divided
    // by the number of AlsaPlayer frames per flac frame.

    return _f->samplesPerBlock () * AP_CHANNELS * AP_BYTES_PER_SAMPLE /
	   _apFramesPerFlacFrame;

} // FlacEngine::apFrameSize


int
FlacEngine::apFrames () const
{
    if (!_f)
	return 0;

    return (int) ceilf (_apFramesPerFlacFrame *
			((float) _f->totalSamples () /
			 (float) _f->samplesPerBlock ()));

} // FlacEngine::apFrames


float
FlacEngine::frameTime (int frame) const
{
    if (!_f)
	return 0;

    float pos = ((float) frame / (float) _apFramesPerFlacFrame) *
	        _f->samplesPerBlock ();
    return pos / (float) _f->sampleRate ();

} // FlacEngine::frameTime


bool
FlacEngine::seekToFrame (int frame)
{
    if (!_f || frame < 0 || frame > apFrames ())
	return false;

    // Some flac decoder interfaces allow us to seek to an exact sample,
    // so translate the frame number to the corresponding sample
    // number.  Don't actually seek to this sample yet, we do that later
    // in flac_play_frame.

    _currSamp = (FLAC__uint64) (_f->samplesPerBlock () * 
				((float) frame / 
				 (float) _apFramesPerFlacFrame));
    _currApFrame = frame;

    return true;

} // FlacEngine::seekToFrame


bool
FlacEngine::decodeFrame (char * buf)
{
    if (!_f || !buf)
	return false;

    // For some reason I haven't figured out yet, the flac library
    // won't return an error when we try to process a frame beyond
    // the total number of samples, nor will it immediately set
    // the decoder state to EOF when there are no more samples
    // to read.  So we check the current sample number here and
    // if it's greater than the total number of samples, return false.

    if (_currSamp >= _f->totalSamples ())
	return false;

    // Optimize the case where there's a 1:1 ratio of AlsaPlayer frames
    // to flac frames.

    if (_apFramesPerFlacFrame == 1)
	_buf = buf;
    else if (!_buf)
	_buf = new char[apFrameSize () * _apFramesPerFlacFrame];


    // Figure out where the next batch of AlsaPlayer samples comes from

    bool status;
    int currFrame = _currSamp / _f->samplesPerBlock ();
    if (currFrame == _lastDecodedFrame)
    {
	// Next AlsaPlayer frame is contained in the last decoded flac frame
	status = true;
    }

    else if (currFrame == _lastDecodedFrame + 1)
    {
	// Next AlsaPlayer frame is the contained in the next flac frame
	status = _f->processOneFrame ();
	if (status)
	    ++_lastDecodedFrame;
    }

    else
    {
	// Seek to find the next AlsaPlayer frame.  Note that by always
	// seeking to a flac frame boundary, we can use
	// _currApFrame % _apFramesPerFlacFrame to determine
	// the AlsaPlayer frame offset within _buf.

	status = _f->seekAbsolute (currFrame * _f->samplesPerBlock ());
	if (status)
	    _lastDecodedFrame = currFrame;
    }

    if (status)
    {
	if (_buf != buf)
	{
	    memcpy (buf, 
		    _buf + (apFrameSize () * 
			    (_currApFrame % _apFramesPerFlacFrame)),
		    apFrameSize ());
	}
	else
	{
	    // reset the internal buffer pointer
	    _buf = 0;
	}

	// If we chose _apFramesPerFlacFrame well, this conversion
	// will always be exact

	_currSamp += _f->samplesPerBlock () / _apFramesPerFlacFrame;
	_currApFrame++;
    }
    else
    {
	if (_buf == buf)
	    _buf = 0;
    }
    
    return status;

} // FlacEngine::decodeFrame


static const FLAC__int16 PCM_SILENCE = 0;

void
FlacEngine::writeAlsaPlayerBuf (unsigned int apSamps, 
				const FLAC__int32 * ch0,
				const FLAC__int32 * ch1,
				unsigned int flacSamps,
				int shift)
{
    FLAC__int16 * pcm = (FLAC__int16 *) _buf;
    unsigned int asample, fsample;

    for (asample = 0, fsample = 0; fsample < flacSamps; fsample++)
    {
	pcm[asample++] = (FLAC__int16) (ch0[fsample] << shift);
	pcm[asample++] = (FLAC__int16) (ch1[fsample] << shift);
    }
    for (; asample < apSamps; )
    {
	pcm[asample++] = PCM_SILENCE;
	pcm[asample++] = PCM_SILENCE;
    }

} // FlacEngine::writeAlsaPlayerBuf


bool
FlacEngine::writeBuf (const FLAC__Frame * frame,
		      const FLAC__int32 * const buffer[],
		      unsigned int flacChannels,
		      unsigned int bps)
{
    if (!_buf || !_f)
	return false;

    // In flac stereo streams, channel 0 is always left and channel 1
    // is always right.  This matches AlsaPlayer.
    //
    // flac 1.02 doesn't define channel order for > 2 channel sound,
    // so grab the first two channels and hope for the best.

    const FLAC__int32 * left = buffer[0];
    const FLAC__int32 * right = flacChannels == 1 ?
	                        buffer[0] : buffer[1];


    // aww... flac gives us signed PCM data, regardless of the signed-ness
    // of the original audio stream.  How thoughtful.
    //
    // Decode a whole flac frame's worth of AlsaPlayer frames at once.

    if (supportsBps (bps))
	writeAlsaPlayerBuf (apFrameSize () * _apFramesPerFlacFrame / 
			    AP_BYTES_PER_SAMPLE,
			    left, right, frame->header.blocksize,
			    bps == 8 ? 8 : 0);
    else
	return false;

    return true;
	
} // FlacEngine::writeBuf

} // namespace Flac