summaryrefslogtreecommitdiff
path: root/vdr_decoder_mp3.c
blob: 65697600d9690c34b2652823b60d099248c29762 (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
/*!
 * \file  vdr_decoder_mp3.h
 * \brief An mp3 decoder for a VDR media plugin (muggle)
 *
 * \version $Revision: 1.2 $
 * \date    $Date$
 * \author  Ralf Klueber, Lars von Wedel, Andreas Kellner
 * \author  Responsible author: $Author$
 *
 * $Id$
 *
 * Adapted from
 * MP3/MPlayer plugin to VDR (C++)
 * (C) 2001,2002 Stefan Huelswitt <huels@iname.com>
 */

#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <cmath>
#include <iostream>

#include "vdr_config.h"
#include "vdr_decoder_mp3.h"
#include "vdr_stream.h"
#include "vdr_setup.h"

#include "mg_tools.h"
#include "mg_content.h"

#define d(x) x

// ----------------------------------------------------------------

int
mgMadStream (struct mad_stream *stream, mgStream * str)
{
    unsigned char *data;
    unsigned long len;
    if (str->stream (data, len, stream->next_frame))
    {
        if (len > 0)
        {
            mad_stream_buffer (stream, data, len);
        }
        return len;
    }
    return -1;
}


// --- mgMP3Decoder -------------------------------------------------------------

mgMP3Decoder::mgMP3Decoder (mgContentItem * item, bool preinit):mgDecoder
(item)
{
    m_stream = 0;
    m_isStream = false;

    m_filename = item->getSourceFile ();

    if (preinit)
    {
        m_stream = new mgStream (m_filename);
        printf ("m_stream for %s\n", m_filename.c_str ());

    }
    m_madframe = 0;
    m_madsynth = 0;

    memset (&m_madstream, 0, sizeof (m_madstream));

    init ();
}


mgMP3Decoder::~mgMP3Decoder ()
{
    clean ();
    delete m_stream;
}


void
mgMP3Decoder::init ()
{
    clean ();
    mad_stream_init (&m_madstream);

    m_madframe = new mad_frame;
    mad_frame_init (m_madframe);

    m_madsynth = new mad_synth;
    mad_synth_init (m_madsynth);

    mad_stream_options (&m_madstream, MAD_OPTION_IGNORECRC);

    m_playtime = mad_timer_zero;
    m_skiptime = mad_timer_zero;
    m_framenum = m_framemax = 0;
    m_frameinfo = 0;
    m_mute = m_errcount = 0;
}


void
mgMP3Decoder::clean ()
{
    m_playing = false;
    if (m_madsynth)
    {
        mad_synth_finish (m_madsynth);
        delete m_madsynth;
        m_madsynth = 0;
    }

    if (m_madframe)
    {
        mad_frame_finish (m_madframe);
        delete m_madframe;
        m_madframe = 0;
    }
    mad_stream_finish (&m_madstream);
}


bool mgMP3Decoder::valid (void)
{
    bool
        res = false;
    if (tryLock ())
    {
        if (start ())
        {
            struct mgDecode *
                dd;
            int
                count = 10;
            do
            {
                dd = decode ();
                if (dd->status == dsEof)
                {
                    count = 0;
                }
                if (dd->status != dsPlay)
                {
                    break;
                }
            }
            while (--count);
            if (!count)
            {
                res = true;
            }
            stop ();
        }
        unlock ();
    }
    return res;
}


mgPlayInfo *
mgMP3Decoder::playInfo ()
{
    if (m_playing)
    {
        m_playinfo.m_index = mad_timer_count (m_playtime, MAD_UNITS_SECONDS);

        return &m_playinfo;
    }
    return 0;
}


bool mgMP3Decoder::start ()
{
    lock (true);
    init ();
    m_playing = true;

    if (m_stream->open (true))
    {
        if (!m_isStream)
        {
            m_stream->seek ();

            printf
                ("mgMP3Decoder::start: m_framemax not determined, rewinding disabled!!!\n");

/* m_framemax = scan->Frames+20; // TODO

   m_frameinfo = new struct FrameInfo[m_framemax];
   if(!m_frameinfo)
   {
   printf( "mgMP3Decoder::start: no memory for frame index, rewinding disabled" );
   }
 */
        }
        unlock ();

        printf ("mgMP3Decoder::start: true\n");
        return true;
    }
    m_stream->close ();
    clean ();
    unlock ();
    printf ("mgMP3Decoder::start: false");
    return false;
}


bool mgMP3Decoder::stop (void)
{
    lock ();

    if (m_playing)
    {
        m_stream->close ();
        clean ();
    }
    unlock ();
    return true;
}


struct mgDecode *
mgMP3Decoder::done (eDecodeStatus status)
{
    m_ds.status = status;
    m_ds.index = mad_timer_count (m_playtime, MAD_UNITS_MILLISECONDS);
    m_ds.pcm = &m_madsynth->pcm;
    unlock ();                                    // release the lock from Decode()

    return &m_ds;
}


eDecodeStatus mgMP3Decoder::decodeError (bool hdr)
{
    if (m_madstream.error == MAD_ERROR_BUFLEN
        || m_madstream.error == MAD_ERROR_BUFPTR)
    {
        int
            s = mgMadStream (&m_madstream, m_stream);
        if (s < 0)
        {
            printf ("mgMP3Decoder::decodeError: dsError returned\n");
            return dsError;
        }
        if (s == 0)
        {
            printf ("mgMP3Decoder::decodeError: dsEof returned\n");
            return dsEof;
        }
    }
    else if (!MAD_RECOVERABLE (m_madstream.error))
    {
        printf
            ("mgMP3Decoder::decodeError: mad decode %sfailed, frame=%d: %s. Returning dsError\n",
            hdr ? "hdr " : "", m_framenum, mad_stream_errorstr (&m_madstream));
        return dsError;
    }
    else
    {
        m_errcount += hdr ? 1 : 100;
        printf
            ("mgMP3Decoder::decodeError: mad decode %s error, frame=%d count=%d: %s. Returning dsOK\n",
            hdr ? "hdr " : "", m_framenum, m_errcount,
            mad_stream_errorstr (&m_madstream));
    }
    return dsOK;
}


struct mgDecode *
mgMP3Decoder::decode ()
{
    lock ();                                      // this is released in Done()
    eDecodeStatus r;

    while (m_playing)
    {
        if (m_errcount >= MAX_FRAME_ERR * 100)
        {
            printf ("mgMP3Decoder::decode: excessive decoding errors,"
                " aborting file %s\n", m_filename.c_str ());
            return done (dsError);
        }

        if (mad_header_decode (&m_madframe->header, &m_madstream) == -1)
        {
            if ((r = decodeError (true)))
            {
                return done (r);
            }
        }
        else
        {
            if (!m_isStream)
            {
#ifdef DEBUG2
                if (m_framenum >= m_framemax)
                {
                    printf ("mgMP3Decoder::start: framenum >= framemax!!!!\n");
                }
#endif
                if (m_frameinfo && m_framenum < m_framemax)
                {
                    m_frameinfo[m_framenum].Pos =
                        m_stream->bufferPos () +
                        (m_madstream.this_frame - m_madstream.buffer);
                    m_frameinfo[m_framenum].Time = m_playtime;
                }
            }

            mad_timer_add (&m_playtime, m_madframe->header.duration);
            m_framenum++;

            if (mad_timer_compare (m_playtime, m_skiptime) >= 0)
            {
                m_skiptime = mad_timer_zero;
            }
            else
            {
                return done (dsSkip);             // skipping, decode next header
            }

            if (mad_frame_decode (m_madframe, &m_madstream) == -1)
            {
                if ((r = decodeError (false)))
                {
                    return done (r);
                }
            }
            else
            {
                m_errcount = 0;

// TODO: // m_scan->InfoHook( &frame->header );

                mad_synth_frame (m_madsynth, m_madframe);

                if (m_mute)
                {
                    m_mute--;
                    return done (dsSkip);
                }
                return done (dsPlay);
            }
        }
    }
    return done (dsError);
}


void
mgMP3Decoder::makeSkipTime (mad_timer_t * skiptime,
mad_timer_t playtime,
int secs, int avail, int dvbrate)
{
    mad_timer_t time;

    *skiptime = playtime;

    mad_timer_set (&time, abs (secs), 0, 0);

    if (secs < 0)
    {
        mad_timer_negate (&time);
    }

    mad_timer_add (skiptime, time);

                                                  // Byte/s = samplerate * 16 bit * 2 chan
    float bufsecs = (float) avail / (float) (dvbrate * (16 / 8 * 2));

    printf ("mgMP3Decoder::makeSkipTime: skip: avail=%d bufsecs=%f\n", avail,
        bufsecs);

    int full = (int) bufsecs;
    bufsecs -= (float) full;

    mad_timer_set (&time, full, (int) (bufsecs * 1000.0), 1000);

    mad_timer_negate (&time);

    mad_timer_add (skiptime, time);

    printf
        ("mgMP3Decoder::makeSkipTime: skip: playtime=%ld secs=%d full=%d bufsecs=%f skiptime=%ld\n",
        mad_timer_count (playtime, MAD_UNITS_MILLISECONDS), secs, full, bufsecs,
        mad_timer_count (*skiptime, MAD_UNITS_MILLISECONDS));
}


bool mgMP3Decoder::skip (int seconds, int avail, int rate)
{
    lock ();

    bool
        res = false;
    if (m_playing && !m_isStream)
    {
        if (!mad_timer_compare (m_skiptime, mad_timer_zero))
        {                                         // allow only one skip at any time
            mad_timer_t
                time;
            makeSkipTime (&time, m_playtime, seconds, avail, rate);

            if (mad_timer_compare (m_playtime, time) <= 0)
            {                                     // forward skip
#ifdef DEBUG
                int
                    i = mad_timer_count (time, MAD_UNITS_SECONDS);
                printf ("mgMP3Decoder::skip: forward skipping to %02d:%02d\n",
                    i / 60, i % 60);
#endif
                m_skiptime = time;
                m_mute = 1;
                res = true;
            }
            else
            {                                     // backward skip
                if (m_frameinfo)
                {
#ifdef DEBUG
                    int
                        i = mad_timer_count (time, MAD_UNITS_SECONDS);
                    printf ("mgMP3Decoder::skip: rewinding to %02d:%02d\n",
                        i / 60, i % 60);
#endif
                    while (m_framenum
                        && mad_timer_compare (time,
                        m_frameinfo[--m_framenum].
                        Time) < 0);
                    m_mute = 2;
                    if (m_framenum >= 2)
                    {
                        m_framenum -= 2;
                    }
                    m_playtime = m_frameinfo[m_framenum].Time;
                    m_stream->seek (m_frameinfo[m_framenum].Pos);
                                                  // reset stream buffer
                    mad_stream_finish (&m_madstream);
                    mad_stream_init (&m_madstream);
#ifdef DEBUG
                    i = mad_timer_count (m_playtime, MAD_UNITS_MILLISECONDS);
                    printf
                        ("mgMP3Decoder::skip: new playtime=%d framenum=%d filepos=%lld\n",
                        i, m_framenum, m_frameinfo[m_framenum].Pos);
#endif
                    res = true;
                }
            }
        }
    }
    unlock ();
    return res;
}