summaryrefslogtreecommitdiffstats
path: root/akode/plugins/mpeg_decoder/mpeg_decoder.cpp
blob: 33466fadc2b7454ce347a55b0d7dd592d6bd020c (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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/*  aKode: MPEG-Decoder (MAD-decoder)

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

    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., 51 Franklin Steet, Fifth Floor, Boston,
    MA  02110-1301, USA.
*/

#include "akodelib.h"

// uncomment to debug the plugin
//#define MPEG_DEBUG

#include <string.h>
#include <mad.h>

#ifdef MPEG_DEBUG
#include <iostream>
using std::cerr;
#endif

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

namespace aKode {

bool MPEGDecoderPlugin::canDecode(File* src) {
    char header[6];
    unsigned char *buf = (unsigned char*)header; // C-stuff
    bool res = false;
    src->openRO();
    if(src->read(header, 4)) {
        // skip id3v2 headers
        if (memcmp(header, "ID3", 3) == 0) {
            src->read(header, 6);
            int size = 0;
            if (buf[1] & 0x10) size += 10;
            size += buf[5];
            size += buf[4] << 7;
            size += buf[3] << 14;
            size += buf[2] << 21;
            src->seek(10+size);
            src->read(header, 4);
        }
        if (buf[0] == 0xff && (buf[1] & 14)) // frame synchronizer
            if((buf[1] & 0x18) != 0x08) // support MPEG 1, 2 and 2½
                if((buf[1] & 0x06) != 0x00) // Layer I, II and III
                    res = true;
    }
    src->close();
    return res;
}

extern "C" { MPEGDecoderPlugin mpeg_decoder; }

struct xing_frame {
    // Xing header:
    bool has_toc;                 // valid table-of-contents
    unsigned int bytes;           // nr-of-bytes (0 if unknown)
    unsigned int frames;          // nr-of-frames (0 if unknown)
    unsigned char toc[100];       // time: x/100 -> place: y/256 (y = toc[x])
};

#define FILEBUFFER_SIZE 8192
struct MPEGDecoder::private_data
{
    private_data() : position(0),
                     length(0),
                     bitrate(0),
                     eof(false),
                     error(false),
                     initialized(false),
                     id3v2size(0),
                     xing_vbr(false),
                     unknown_vbr(false)
    {};
    mad_stream stream;
    mad_frame frame;
    mad_synth synth;

    File *src;
    AudioConfiguration config;

    long position, length;
    int bitrate, layer;

    bool eof, error, initialized;

    int id3v2size;               // size of prepended ID3v2 tag
    bool xing_vbr;               // Xing-compatible VBR
    bool unknown_vbr;            // Oh, bugger...
    xing_frame xing;

    char filebuffer[FILEBUFFER_SIZE];

    bool metaframe_filter(bool fast);
};

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

    mad_stream_init(&m_data->stream);
    mad_frame_init(&m_data->frame);
    mad_synth_init(&m_data->synth);

    mad_stream_options(&m_data->stream, MAD_OPTION_IGNORECRC);

    m_data->eof = false;
    m_data->error = false;

    m_data->src = src;
    if (!m_data->src->openRO()) {
        m_data->error = true;
        #ifdef MPEG_DEBUG
        cerr << "Could not open file: " << m_data->src->filename << "\n";
        #endif
    }
    m_data->src->fadvise();

    m_data->length = m_data->src->length();
}

MPEGDecoder::~MPEGDecoder() {
    mad_stream_finish(&m_data->stream);
    mad_frame_finish(&m_data->frame);
    mad_synth_finish(&m_data->synth);
    m_data->src->close();
    delete m_data;
}

bool MPEGDecoder::skipID3v2() {
    m_data->src->seek(0);
    m_data->id3v2size = 0;
    char buf[16];
    if (m_data->src->read(buf, 10)) {
        // skip id3v2 headers
        if (memcmp(buf, "ID3", 3) == 0) {
            #ifdef MPEG_DEBUG
            cerr << "ID3v2 tag found\n";
            #endif
            int size = 0;
            if (buf[5] & 0x10) size += 10;
            size += buf[9];
            size += buf[8] << 7;
            size += buf[7] << 14;
            size += buf[6] << 21;
            m_data->id3v2size = size;
            if (!m_data->src->seek(10+size)) {
                char space[256];
                for(int i=0; i<size; ) {
                    i += m_data->src->read(space, (size-i) > 256 ?  256 : (size-i));
                }
            }
            return true;
        }
    }
    if (!m_data->src->seek(0)) {
        mad_stream_buffer(&m_data->stream, (uint8_t*)buf, 10);
    }
    return false;
}

bool MPEGDecoder::moreData(bool flush)
{
    mad_stream *stream = &m_data->stream;

    long rem = 0;
    if (stream->next_frame != 0 && !flush) {
        rem = stream->bufend - stream->next_frame;
        memmove(m_data->filebuffer, stream->next_frame, rem);
    }
    #ifdef MPEG_DEBUG
//    cerr << "remainder=" << rem << "\n";
    #endif

    int res = m_data->src->read(m_data->filebuffer+rem, FILEBUFFER_SIZE-rem);
    mad_stream_buffer(stream, (uint8_t*)m_data->filebuffer, res+rem);
    // for some reason MAD can't to do this itself:
    if (stream->error == MAD_ERROR_LOSTSYNC || flush) stream->sync = 0;
    stream->error = MAD_ERROR_NONE;

    if (res == 0) {
        m_data->eof = true;
        return false;
    } else
    if (res < 0) {
        m_data->error = true;
        return false;
    } else
        return true;
}

// originaly from minimad.c
template<int bits>
static inline int32_t scale(mad_fixed_t sample)
{
  /* round */
  sample += (1L << (MAD_F_FRACBITS - bits));

  /* clip */
  if (sample >= MAD_F_ONE)
    sample = MAD_F_ONE - 1;
  else if (sample < -MAD_F_ONE)
    sample = -MAD_F_ONE;

  /* quantize */
  return sample >> (MAD_F_FRACBITS + 1 - bits);
}

static inline long postotime(long pos, int sample_rate) {
    if (sample_rate == 0) return 0;
    long sec = pos/sample_rate;
    long rem = pos%sample_rate;
    long ms = (rem*1000)/sample_rate;
    return sec*1000+ms;
}


// Xing-header documentation is from http://www.multiweb.cz/twoinches/MP3inside.htm
// toc-format verified in LAME source-code
static bool xing_decode(xing_frame *xing, const unsigned char * bytes)
{
    mad_bitptr ptr;
    mad_bit_init(&ptr, bytes);

    if (mad_bit_read(&ptr, 32) == (('X' << 24) | ('i' << 16) | ('n' << 8) | 'g'))
    {
        int flags = mad_bit_read(&ptr, 32);
        if (flags & 1)
            xing->frames = mad_bit_read(&ptr, 32);
        if (flags & 2)
            xing->bytes = mad_bit_read(&ptr, 32);
        if (flags & 4) {
            xing->has_toc = true;
            for (int i=0; i<100; i++) {
                xing->toc[i] = mad_bit_read(&ptr, 8);
            }
        }
        return true;
    }
    return false;
}

// Detect various metaframes
bool MPEGDecoder::private_data::metaframe_filter(bool fast)
{
    if (stream.anc_bitlen < 16) return false;

    const unsigned char* ptr;

    ptr = stream.anc_ptr.byte;
test:
    if ((ptr[0] == 'X') && (ptr[1] == 'i') && (ptr[2] == 'n') && (ptr[3] =='g'))
    {
        #ifdef MPEG_DEBUG
        cerr << "Xing(VBR) header found\n";
        #endif
        xing_vbr = true;
        xing_decode(&xing, ptr);
        return true;
    } /*
    if ((ptr[0] == 'L') || (ptr[1] == 'A') || (ptr[2] == 'M') || (ptr[3] =='E'))
    {
        #ifdef MPEG_DEBUG
        cerr << "LAME frame found\n";
        #endif
        return true;
    }*/
    if ((ptr[0] == 'I') && (ptr[1] == 'n') && (ptr[2] == 'f') && (ptr[3] =='o'))
    {
        #ifdef MPEG_DEBUG
        cerr << "Info(CBR) header found\n";
        #endif
        return true;
    }

    if (!fast) {
        // We can't trust broken-encoders to _not_ fuck this up
        ptr = stream.this_frame;
        // skip frame header and side info
        ptr += 6;
        // Search for metaframe tags:
        for(int i=0; i<64 && ptr < stream.bufend; i++) {
            if (*ptr == 'X' || *ptr== 'I') {
                fast = true;
                goto test;
            }
            ptr++;
        }
    }

    return false;
}

bool MPEGDecoder::sync()
{
    // only try 32 times, if no sync-frame is found the file is properbly not MPEG
    for(int i=0; i<32; i++)  {
        int res = mad_stream_sync(&m_data->stream);
        if (res == 0 ) goto got_sync;
        if (!moreData(true)) break;
    }
    m_data->error=true;
    #ifdef MPEG_DEBUG
    cerr << "Synchronization failed, likely not a mpeg audio file.\n";
    #endif
    return false;

got_sync:
    return true;
}

bool MPEGDecoder::prepare()
{
    skipID3v2();
    // initialize stream
    if (m_data->stream.buffer == 0) moreData();

    if (sync()) {
        m_data->initialized = true;
        return true;
    } else
        return false;
}

static void setChannelConfiguration(AudioConfiguration& config, mad_mode mode)
{
    switch(mode) {
        case MAD_MODE_SINGLE_CHANNEL:
            config.channels=1;
            config.channel_config=MonoStereo;
            break;
        case MAD_MODE_DUAL_CHANNEL:
            config.channels=2;
            config.channel_config=MultiChannel;
            break;
        default: // Stereo and joint stereo
            config.channels=2;
            config.channel_config=MonoStereo;
    }
    config.surround_config = 0;
}

bool MPEGDecoder::readFrame(AudioFrame* frame)
{
    if (m_data->error) return false;

    int res = 0;
    if (!m_data->initialized) {
        if (!prepare()) return false;

        int retries = 0;
    first_frame:
        if (retries >= 8) {
            // fatal error
            m_data->error = true;
            return false;
        }
        res = mad_frame_decode(&m_data->frame, &m_data->stream);
        if (res != 0) {
            if (m_data->stream.error == MAD_ERROR_BUFLEN) {
                retries++;
                if (!moreData(true)) return false;
            }
            else
            if (m_data->stream.error == MAD_ERROR_LOSTSYNC)
            {}
            else
            if (MAD_RECOVERABLE(m_data->stream.error)) {
                #ifdef MPEG_DEBUG
                cerr << "Invalid frame: " << mad_stream_errorstr(&m_data->stream) << "\n";
                #endif
                m_data->metaframe_filter(true);
            }
            goto first_frame;
        }

        m_data->config.sample_rate = m_data->frame.header.samplerate;
        m_data->config.sample_width = 16;
        setChannelConfiguration(m_data->config, m_data->frame.header.mode);

        m_data->bitrate = m_data->frame.header.bitrate;
        m_data->layer = m_data->frame.header.layer;

        m_data->metaframe_filter(false);
    }
    else {
        if (m_data->stream.buffer == 0 ||
            m_data->stream.error==MAD_ERROR_BUFLEN ||
            m_data->stream.error==MAD_ERROR_LOSTSYNC) if (!moreData()) return false;

        int retries = 0;
        bool sync = true;
    retry:
        // give up after 16 new pages
        if (retries >= 16) {
            // fatal error
            m_data->error = true;
            return false;
        }
        res = mad_frame_decode(&m_data->frame, &m_data->stream);

        if (res != 0) {
            if (m_data->stream.error == MAD_ERROR_BUFLEN) {
                retries++;
                if (moreData(!sync)) return false;
                goto retry;
            }
            else
            if (m_data->stream.error == MAD_ERROR_LOSTSYNC) {
                #ifdef MPEG_DEBUG
                cerr << "No sync\n";
                #endif
                sync = false;
                goto retry;
            }
            else
            if (MAD_RECOVERABLE(m_data->stream.error)) {  // invalid frame
                #ifdef MPEG_DEBUG
                cerr << "Invalid frame: " << mad_stream_errorstr(&m_data->stream) << "\n";
                #endif
                m_data->metaframe_filter(true);
                goto retry;
            }
            else {
                // Unrecoverable error
                m_data->error = true;
                return false;
            }
        }
        // fast check for metaframes
        if (m_data->metaframe_filter(false))
            goto retry;
    }

    if (m_data->bitrate && !m_data->xing_vbr)
        if (m_data->bitrate != (int)m_data->frame.header.bitrate)
            m_data->unknown_vbr = true;

    mad_synth_frame(&m_data->synth, &m_data->frame);
    mad_pcm *pcm = &m_data->synth.pcm;

    // Has the format changed? (misread initial frame)
    m_data->config.sample_rate = pcm->samplerate;
    setChannelConfiguration(m_data->config, m_data->frame.header.mode);

    int channels = pcm->channels;
    long length = pcm->length;

    frame->reserveSpace(&m_data->config, length);

    // Scale into frame (could be reduced to copy if we used MADs internal format)
    int16_t** data = (int16_t**)frame->data;
    for(int j=0; j<channels; j++)
        for(int i=0; i<length; i++)
            data[j][i] = scale<16>(pcm->samples[j][i]);

    m_data->position += length;
    frame->pos = postotime(m_data->position, m_data->config.sample_rate);

    return true;
}

static inline long time_madtoakode(mad_timer_t time) {
    return mad_timer_count(time, MAD_UNITS_MILLISECONDS);
}

static float mpeg_length(MPEGDecoder::private_data *m_data) {
    if (m_data->layer == 1)
        return (m_data->xing.frames*384.0)/m_data->config.sample_rate;
    else
        return (m_data->xing.frames*1152.0)/m_data->config.sample_rate;
}

long MPEGDecoder::length() {
    float len;
    if (!m_data->initialized) return -1;
    else {
        if (m_data->xing_vbr || m_data->unknown_vbr) {
            if (m_data->xing_vbr && m_data->xing.frames > 0) {
                len = mpeg_length(m_data)*1000.0;
            }
            else { // VBR shot in the dark (based on average bitrate)
                if (m_data->length > 0) {
                     float bytepos = m_data->src->position()/(float)m_data->length;
                    len = position()/bytepos;
                } else
                    len = 0;
            }
        }
        else {
            if (m_data->length > 0)
                len = ((float)m_data->length*8000.0)/((float)m_data->bitrate);
            else
                len = 0;
        }
    }
    return (long)(len);
}

long MPEGDecoder::position() {
    if (!m_data->initialized) return -1;
    if (m_data->frame.header.samplerate > 0 && m_data->position >= 0)
        return postotime(m_data->position, m_data->config.sample_rate);
    else
        return -1;
}

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

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

bool MPEGDecoder::seekable() {
    return m_data->src->seekable();
}

bool MPEGDecoder::seek(long pos) {
    if (!m_data->initialized) return false;
    bool res = false;
    // VBR with toc:
    if (m_data->xing_vbr && m_data->xing.has_toc && m_data->xing.frames > 0) {
        int procent = (int)((float)pos/(mpeg_length(m_data)*10));
        float place = ((float)m_data->xing.toc[procent])/256.0;
        long bpos = (long)(place * m_data->length);
        #ifdef MPEG_DEBUG
        cerr << "TOC seeking " << procent << "/100 -> " << (long)(place*256.0) << "/256\n" ;
        #endif
        res = m_data->src->seek(bpos);
        if (res)
            m_data->position = (long)(((float)procent/100.0)*mpeg_length(m_data)*m_data->config.sample_rate);
    }
    else { // CBR seeking (and VBR without TOC fallback)
        #ifdef MPEG_DEBUG
        cerr << "CBR seeking\n";
        #endif
        long bpos = (long)((((float)pos)*m_data->bitrate)/8000.0);
        bpos += m_data->id3v2size;
        res = m_data->src->seek(bpos);
        if (res)
            m_data->position = (long)(((float)pos*(float)m_data->config.sample_rate)/1000.0);
    }
    if (res) {
        moreData(true);
        sync();
    }

    return res;
}

const AudioConfiguration* MPEGDecoder::audioConfiguration() {
    if (!m_data->initialized) return 0;
    return &m_data->config;
}

} // namespace