summaryrefslogtreecommitdiff
path: root/core/multimedia/opieplayer/libmad/libmadplugin.cpp
blob: 428fc28d784b87dd9ee9b837f028aa1410e97168 (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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
/**********************************************************************
** Copyright (C) 2001 Trolltech AS.  All rights reserved.
**
** This file is part of Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/
// largly modified by Maximilian Reiss <max.reiss@gmx.de>

#include "libmadplugin.h"

/* OPIE */
#include <qpe/config.h>
#include <opie2/odebug.h>

/* QT */
#include <qapplication.h>
#include <qmessagebox.h>
#include <qregexp.h>

/* STD */
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <time.h>
#include <locale.h>
#include <math.h>
#include <assert.h>

// for network handling
#include <netinet/in.h>
#include <netdb.h>
#include <linux/limits.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>


//#define HAVE_MMAP

#if defined(HAVE_MMAP)
#   include <sys/mman.h>
#endif


extern "C" {
#include "mad.h"
}


#define MPEG_BUFFER_SIZE    65536
//#define MPEG_BUFFER_SIZE    32768 //16384 // 8192
//#define debugMsg(a)     qDebug(a)
#define debugMsg(a)


class Input {
public:
    char const *path;
    int fd;
#if defined(HAVE_MMAP)
    void *fdm;
#endif
    unsigned long fileLength;
   unsigned char *data;
    unsigned long length;
    int eof;
};


class Output {
public:
    mad_fixed_t attenuate;
    struct filter *filters;
    unsigned int channels_in;
    unsigned int channels_out;
    unsigned int speed_in;
    unsigned int speed_out;
    const char *path;
};


# if defined(HAVE_MMAP)
static void *map_file(int fd, unsigned long *length)
{
  void *fdm;

  *length += MAD_BUFFER_GUARD;

  fdm = mmap(0, *length, PROT_READ, MAP_SHARED, fd, 0);
  if (fdm == MAP_FAILED)
    return 0;

# if defined(HAVE_MADVISE)
  madvise(fdm, *length, MADV_SEQUENTIAL);
# endif

  return fdm;
}


static int unmap_file(void *fdm, unsigned long length)
{
  if (munmap(fdm, length) == -1)
    return -1;

  return 0;
}
# endif


static inline QString tr( const char *str ) {
    // Apparently this is okay from a plugin as it runs in the process space of the owner of the plugin
    return qApp->translate( "OpiePlayer", str, "libmad strings for mp3 file info" );
}


class LibMadPluginData {
public:
    Input input;
    Output output;
    int bad_last_frame;
    struct mad_stream stream;
    struct mad_frame frame;
    struct mad_synth synth;
    bool flush;
};


LibMadPlugin::LibMadPlugin() {
    d = new LibMadPluginData;
    d->input.fd = 0;
#if defined(HAVE_MMAP)
    d->input.fdm = 0;
#endif
    d->input.data = 0;
    d->flush = TRUE;
    info = tr( "No Song Open" );
}


LibMadPlugin::~LibMadPlugin() {
    close();
    delete d;
}


bool LibMadPlugin::isFileSupported( const QString& path ) {
    debugMsg( "LibMadPlugin::isFileSupported" );

    // Mpeg file extensions
    //  "mp2","mp3","m1v","m2v","m2s","mpg","vob","mpeg","ac3"
    // Other media extensions
    //  "wav","mid","mod","s3m","ogg","avi","mov","sid"

    char *ext = strrchr( path.latin1(), '.' );

    // Test file extension
    if ( ext ) {
        if ( strncasecmp(ext, ".mp2", 4) == 0 )
            return TRUE;
        if ( strncasecmp(ext, ".mp3", 4) == 0 )
            return TRUE;
    }

    // UGLY - just for fast testing
    if ( path.left(4) == "http") {
        return TRUE;
    }

   return FALSE;
}



int LibMadPlugin::is_address_multicast(unsigned long address) {
    if ((address & 255) >= 224 && (address & 255) <= 239)
        return (1);
    return (0);
}


int LibMadPlugin::udp_open(char *address, int port) {

    int enable = 1L;
    struct sockaddr_in stAddr;
    struct sockaddr_in stLclAddr;
    struct ip_mreq stMreq;
    struct hostent *host;
    int sock;

    stAddr.sin_family = AF_INET;
    stAddr.sin_port = htons(port);

    if ((host = gethostbyname(address)) == NULL) {
        return (0);
    }

    stAddr.sin_addr = *((struct in_addr *)host->h_addr_list[0]);

    /* Create a UDP socket */
    if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        return (0);
    }

    /* Allow multiple instance of the client to share the same address and port */
    if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&enable, sizeof(unsigned long int)) < 0) {
        return (0);
    }

    /* If the address is multicast, register to the multicast group */
    if (is_address_multicast(stAddr.sin_addr.s_addr)) {
        /* Bind the socket to port */
        stLclAddr.sin_family = AF_INET;
        stLclAddr.sin_addr.s_addr = htonl(INADDR_ANY);
        stLclAddr.sin_port = stAddr.sin_port;
        if (bind(sock, (struct sockaddr *)&stLclAddr, sizeof(stLclAddr)) < 0) {
            return (0);
        }

        /* Register to a multicast address */
        stMreq.imr_multiaddr.s_addr = stAddr.sin_addr.s_addr;
        stMreq.imr_interface.s_addr = INADDR_ANY;
        if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&stMreq, sizeof(stMreq)) < 0) {
            return (0);
        }
    } else {
        /* Bind the socket to port */
        stLclAddr.sin_family = AF_INET;
        stLclAddr.sin_addr.s_addr = htonl(INADDR_ANY);
        stLclAddr.sin_port = htons(0);
        if (bind(sock, (struct sockaddr *)&stLclAddr, sizeof(stLclAddr)) < 0) {
            return (0);
        }
    }
    return (sock);
}

int LibMadPlugin::tcp_open(char *address, int port) {
    struct sockaddr_in stAddr;
    struct hostent *host;
    int sock;
    struct linger l;

    memset(&stAddr, 0, sizeof(stAddr));
    stAddr.sin_family = AF_INET;
    stAddr.sin_port = htons(port);

    if ((host = gethostbyname(address)) == NULL) {
        return (0);
    }

    stAddr.sin_addr = *((struct in_addr *)host->h_addr_list[0]);

    if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
        return (0);
    }

    l.l_onoff = 1;
    l.l_linger = 5;
    if (setsockopt(sock, SOL_SOCKET, SO_LINGER, (char *)&l, sizeof(l)) < 0) {
        return (0);
    }

    if (connect(sock, (struct sockaddr *)&stAddr, sizeof(stAddr)) < 0) {
        return (0);
    }

    return (sock);
}


/**
 * Read a http line header.
 * This function read character by character.
 * @param tcp_sock the socket use to read the stream
 * @param buf a buffer to receive the data
 * @param size size of the buffer
 * @return the size of the stream read or -1 if an error occured
 */
int LibMadPlugin::http_read_line(int tcp_sock, char *buf, int size) {
    int offset = 0;

    do {
        if (::read(tcp_sock, buf + offset, 1) < 0)
            return -1;
        if (buf[offset] != '\r')    /* Strip \r from answer */
            offset++;
    } while (offset < size - 1 && buf[offset - 1] != '\n');

    buf[offset] = 0;
    return offset;
}

int LibMadPlugin::http_open(const QString& path ) {
    char *host;
    int port;
    char *request;
    int tcp_sock;
    char http_request[PATH_MAX];
    char filename[PATH_MAX];
    //char c;
    char *arg =strdup(path.latin1());

    /* Check for URL syntax */
    if (strncmp(arg, "http://", strlen("http://"))) {
        return (0);
    }

    /* Parse URL */
    port = 80;
    host = arg + strlen("http://");
    if ((request = strchr(host, '/')) == NULL) {
        return (0);
    }

    *request++ = 0;

    if (strchr(host, ':') != NULL) { /* port is specified */
        port = atoi(strchr(host, ':') + 1);
        *strchr(host, ':') = 0;
    }

    /* Open a TCP socket */
    if (!(tcp_sock = tcp_open(host, port))) {
        perror("http_open");
        return (0);
    }

    snprintf(filename, sizeof(filename) - strlen(host) - 75, "%s", request);

    /* Send HTTP GET request */
    /* Please don't use a Agent know by shoutcast (Lynx, Mozilla) seems to be reconized and print
     * a html page and not the stream */
    snprintf(http_request, sizeof(http_request), "GET /%s HTTP/1.0\r\n"
    /*  "User-Agent: Mozilla/2.0 (Win95; I)\r\n" */
        "Pragma: no-cache\r\n" "Host: %s\r\n" "Accept: */*\r\n" "\r\n", filename, host);

    send(tcp_sock, http_request, strlen(http_request), 0);

    /* Parse server reply */
#if 0
    do
        read(tcp_sock, &c, sizeof(char));
    while (c != ' ');
    read(tcp_sock, http_request, 4 * sizeof(char));
    http_request[4] = 0;
    if (strcmp(http_request, "200 "))     {
        fprintf(stderr, "http_open: ");
        do {
            read(tcp_sock, &c, sizeof(char));
            fprintf(stderr, "%c", c);
        } while (c != '\r');
        fprintf(stderr, "\n");
        return (0);
    }
#endif

    QString name;
    QString genre;
    QString bitrate;
    QString url;
    QString message = tr("Info: ");
    do {

        int len;

        len = http_read_line(tcp_sock, http_request, sizeof(http_request));

        if (len == -1) {
           //            odebug << "http_open: "+ QString(strerror(errno)) +"\n" << oendl;
            return 0;
        }

        if (QString(http_request).left(9) == "Location:") {
            /* redirect */
            ::close(tcp_sock);
            http_request[strlen(http_request) - 1] = '\0';
            return http_open(&http_request[10]);
        }

        if (QString(http_request).left(4) == "ICY ") {
            /* This is shoutcast/icecast streaming */
            if (strncmp(http_request + 4, "200 ", 4)) {
               //                odebug << "http_open: " + QString(http_request) + "\n" << oendl;
                return 0;
            }
        } else if (QString(http_request).left(4) == "icy-") {
            /* we can have: icy-noticeX, icy-name, icy-genre, icy-url, icy-pub, icy-metaint, icy-br */
            if ( QString( http_request ).left( 8 ) == "icy-name" ) {
                name = tr("Name: ") + QString(http_request).mid(9, (QString(http_request).length())- 9 );
            } else if ( QString( http_request ).left( 9 ) == "icy-genre" ) {
                genre = tr("Genre: ") + QString(http_request).mid(10, (QString(http_request).length())-10 );
            } else if ( QString( http_request ).left( 6 ) == "icy-br" ) {
                bitrate = tr("Bitrate: ") + QString(http_request).mid(7, (QString(http_request).length())- 7 );
            } else if ( QString( http_request ).left( 7 ) == "icy-url" ) {
                url = tr("URL: ") + QString(http_request).mid(8, (QString(http_request).length())- 8 );
            }  else if ( QString( http_request ).left( 10 ) == "icy-notice" ) {
                message += QString(http_request).mid(11, QString(http_request).length()-11 ) ;
            }
        }
    } while (strcmp(http_request, "\n") != 0);

    info = QString(name + genre + url + bitrate + message).replace( QRegExp("\n"), " : " );

    //    odebug << "Stream info: " + info << oendl;

    return (tcp_sock);
}



bool LibMadPlugin::open( const QString& path ) {
    debugMsg( "LibMadPlugin::open" );
    Config cfg("OpiePlayer");
    cfg.setGroup("Options");
    bufferSize = cfg.readNumEntry("MPeg_BufferSize",MPEG_BUFFER_SIZE);
    //    odebug << "buffer size is " << bufferSize << "" << oendl;
    d->bad_last_frame = 0;
    d->flush = TRUE;
    info = QString( "" );

    //odebug << "Opening " << path << "" << oendl;

    if (path.left( 4 ) == "http" ) {
        // in case of any error we get 0 here
        if ( !(http_open(path) == 0) ) {
            d->input.fd = http_open(path);
        } else {
            return FALSE;
        }
    } else {
        d->input.path = path.latin1();
        d->input.fd = ::open( d->input.path, O_RDONLY );
        // thats a better place, since it should only seek for ID3 tags on mp3 files, not streams
        printID3Tags();
    }
    if (d->input.fd == -1) {
       //        odebug << "error opening " << d->input.path << "" << oendl;
        return FALSE;
    }

    struct stat stat;
    if (fstat(d->input.fd, &stat) == -1) {
       //    odebug << "error calling fstat" << oendl;  return FALSE;
    }
    if (S_ISREG(stat.st_mode) && stat.st_size > 0)
  d->input.fileLength = stat.st_size;
    else
  d->input.fileLength = 0;

#if defined(HAVE_MMAP)
    if (S_ISREG(stat.st_mode) && stat.st_size > 0) {
        d->input.length = stat.st_size;
        d->input.fdm = map_file(d->input.fd, &d->input.length);
        if (d->input.fdm == 0) {
           //            odebug << "error mmapping file" << oendl;  return FALSE;
        }
        d->input.data = (unsigned char *)d->input.fdm;
    }
#endif

    if (d->input.data == 0) {
        d->input.data = (unsigned char *)malloc( bufferSize /*MPEG_BUFFER_SIZE*/);
        if (d->input.data == 0) {
           //            odebug << "error allocating input buffer" << oendl;
            return FALSE;
        }
        d->input.length = 0;
    }

    d->input.eof = 0;

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

    return TRUE;
}


bool LibMadPlugin::close() {
    debugMsg( "LibMadPlugin::close" );

    int result = TRUE;

    mad_synth_finish(&d->synth);
    mad_frame_finish(&d->frame);
    mad_stream_finish(&d->stream);

#if defined(HAVE_MMAP)
    if (d->input.fdm) {
        if (unmap_file(d->input.fdm, d->input.length) == -1) {
           //            odebug << "error munmapping file" << oendl;
            result = FALSE;
        }
        d->input.fdm  = 0;
        d->input.data = 0;
    }
#endif

    if (d->input.data) {
        free(d->input.data);
        d->input.data = 0;
    }

    if (::close(d->input.fd) == -1) {
       //        odebug << "error closing file " << d->input.path << "" << oendl;
        result = FALSE;
    }

    d->input.fd = 0;

    return result;
}


bool LibMadPlugin::isOpen() {
    debugMsg( "LibMadPlugin::isOpen" );
    return ( d->input.fd != 0 );
}


int LibMadPlugin::audioStreams() {
    debugMsg( "LibMadPlugin::audioStreams" );
    return 1;
}


int LibMadPlugin::audioChannels( int ) {
    debugMsg( "LibMadPlugin::audioChannels" );
/*
  long t; short t1[5]; audioReadSamples( t1, 2, 1, t, 0 );
  odebug << "LibMadPlugin::audioChannels: " << d->frame.header.mode > 0 ? 2 : 1 << "" << oendl;
  return d->frame.header.mode > 0 ? 2 : 1;
*/
    return 2;
}


int LibMadPlugin::audioFrequency( int ) {
    debugMsg( "LibMadPlugin::audioFrequency" );
    long t; short t1[5]; audioReadSamples( t1, 2, 1, t, 0 );
    //    odebug << "LibMadPlugin::audioFrequency: " << d->frame.header.samplerate << "" << oendl;
    return d->frame.header.samplerate;
}


int LibMadPlugin::audioSamples( int ) {
   debugMsg( "LibMadPlugin::audioSamples" );

   long t; short t1[5]; audioReadSamples( t1, 2, 1, t, 0 );
   mad_header_decode( (struct mad_header *)&d->frame.header, &d->stream );
/*
  odebug << "LibMadPlugin::audioSamples: " <<  d->frame.header.duration.seconds << "*" << d->frame.header.samplerate << oendl;
  return d->frame.header.duration.seconds * d->frame.header.samplerate;
*/
   if ( d->frame.header.bitrate == 0 )
      return 0;
   int samples = (d->input.fileLength / (d->frame.header.bitrate/8)) * d->frame.header.samplerate;

   //   qDebug( "LibMadPlugin::audioSamples: %i * %i * 8 / %i", (int)d->input.fileLength,
   //           (int)d->frame.header.samplerate, (int)d->frame.header.bitrate );
   //   odebug << "LibMadPlugin::audioSamples: " << samples << "" << oendl;

   return samples;

//    return 10000000;
}


bool LibMadPlugin::audioSetSample( long, int ) {
    debugMsg( "LibMadPlugin::audioSetSample" );

//     long totalSamples = audioSamples(0);
//     if ( totalSamples <= 1 )
//   return FALSE;

//     // Seek to requested position
//     odebug << "seek pos: " << (int)((double)pos * d->input.fileLength / totalSamples) << "" << oendl;
//     ::lseek( d->input.fd, (long)((double)pos * d->input.fileLength / totalSamples), SEEK_SET );
//     mad_stream_sync(&d->stream);

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

//     return TRUE;
    debugMsg( "LibMadPlugin::audioSetSample" );
    return FALSE;
}


long LibMadPlugin::audioGetSample( int ) {
    debugMsg( "LibMadPlugin::audioGetSample" );
    return 0;
}

/*
bool LibMadPlugin::audioReadSamples( short *, int, long, int ) {
debugMsg( "LibMadPlugin::audioReadSamples" );
return FALSE;
}


bool LibMadPlugin::audioReReadSamples( short *, int, long, int ) {
debugMsg( "LibMadPlugin::audioReReadSamples" );
    return FALSE;
    }
*/

bool LibMadPlugin::read() {
    debugMsg( "LibMadPlugin::read" );
    int len;

      if (d->input.eof)
  return FALSE;

#if defined(HAVE_MMAP)
      if (d->input.fdm) {
          unsigned long skip = 0;

          if (d->stream.next_frame) {
              struct stat stat;

              if (fstat(d->input.fd, &stat) == -1)
                  return FALSE;

              if (stat.st_size + MAD_BUFFER_GUARD <= (signed)d->input.length)
                  return FALSE;

              // file size changed; update memory map
              skip = d->stream.next_frame - d->input.data;

              if (unmap_file(d->input.fdm, d->input.length) == -1) {
                  d->input.fdm  = 0;
                  d->input.data = 0;
                  return FALSE;
              }

              d->input.length = stat.st_size;

              d->input.fdm = map_file(d->input.fd, &d->input.length);
              if (d->input.fdm == 0) {
                  d->input.data = 0;
                  return FALSE;
              }

              d->input.data = (unsigned char *)d->input.fdm;
          }

          mad_stream_buffer(&d->stream, d->input.data + skip, d->input.length - skip);

      } else
#endif
      {
          if (d->stream.next_frame) {
              memmove(d->input.data, d->stream.next_frame,
                      d->input.length = &d->input.data[d->input.length] - d->stream.next_frame);
          }

          do {
              len = ::read(d->input.fd, d->input.data + d->input.length, bufferSize /* MPEG_BUFFER_SIZE*/ - d->input.length);
          }
          while (len == -1 && errno == EINTR);

          if (len == -1) {
             //              odebug << "error reading audio" << oendl;
              return FALSE;
          }
          else if (len == 0) {
              d->input.eof = 1;

              assert(bufferSize /*MPEG_BUFFER_SIZE*/ - d->input.length >= MAD_BUFFER_GUARD);

              while (len < MAD_BUFFER_GUARD)
                  d->input.data[d->input.length + len++] = 0;
          }

          mad_stream_buffer(&d->stream, d->input.data, d->input.length += len);
      }

      return TRUE;
}


static mad_fixed_t left_err, right_err;
static const int bits = 16;
static const int shift = MAD_F_FRACBITS + 1 - bits;


inline long audio_linear_dither( mad_fixed_t sample, mad_fixed_t& error ) {
    sample += error;
    mad_fixed_t quantized = (sample >= MAD_F_ONE) ? MAD_F_ONE - 1 : ( (sample < -MAD_F_ONE) ? -MAD_F_ONE : sample );
    quantized &= ~((1L << shift) - 1);
    error = sample - quantized;
    return quantized >> shift;
}


inline void audio_pcm( short *data, unsigned int nsamples, mad_fixed_t *left, mad_fixed_t *right ) {
    if ( right ) {
        while (nsamples--) {
            data[0] = audio_linear_dither( *left++,  left_err );
            data[1] = audio_linear_dither( *right++, right_err );
            data += 2;
        }
    } else {
        while (nsamples--) {
            data[0] = data[1] = audio_linear_dither( *left++,  left_err );
            data += 2;
        }
    }
}


bool LibMadPlugin::decode( short *output, long samples, long& samplesMade ) {
    debugMsg( "LibMadPlugin::decode" );

    static int buffered = 0;
    static mad_fixed_t buffer[2][65536 * 2];
    int offset = buffered;
    samplesMade = 0;

    static int maxBuffered = 8000; // 65536;

    if ( samples > maxBuffered ) {
        samples = maxBuffered;
    }

    if ( d->flush ) {
        buffered = 0;
        offset = 0;
        d->flush = FALSE;
    }

    while ( buffered < maxBuffered ) {

        while (mad_frame_decode(&d->frame, &d->stream) == -1) {
            if (!MAD_RECOVERABLE(d->stream.error)) {
                debugMsg( "feed me" );
                return FALSE; // Feed me
            }
            if ( d->stream.error == MAD_ERROR_BADCRC ) {
                mad_frame_mute(&d->frame);
                //                odebug << "error decoding, bad crc" << oendl;
            }
        }

        mad_synth_frame(&d->synth, &d->frame);
        int decodedSamples = d->synth.pcm.length;
        memcpy( &(buffer[0][offset]), d->synth.pcm.samples[0], decodedSamples * sizeof(mad_fixed_t) );
        if ( d->synth.pcm.channels == 2 )
            memcpy( &(buffer[1][offset]), d->synth.pcm.samples[1], decodedSamples * sizeof(mad_fixed_t) );
        offset += decodedSamples;
        buffered += decodedSamples;
    }

//qApp->processEvents();
    audio_pcm( output, samples, buffer[0], (d->synth.pcm.channels == 2) ? buffer[1] : 0 );
//    audio_pcm( output, samples, buffer[1], buffer[0] );
//    audio_pcm( output, samples, buffer[0], buffer[1] );
    samplesMade = samples;
    memmove( buffer[0], &(buffer[0][samples]), (buffered - samples) * sizeof(mad_fixed_t) );
    if ( d->synth.pcm.channels == 2 ) {
        memmove( buffer[1], &(buffer[1][samples]), (buffered - samples) * sizeof(mad_fixed_t) );
    }
    buffered -= samples;

    return TRUE;
}

/*bool LibMadPlugin::audioReadStereoSamples( short *output, long samples, long& samplesMade, int ) {
*/
bool LibMadPlugin::audioReadSamples( short *output, int /*channels*/, long samples, long& samplesMade, int ) {
    debugMsg( "LibMadPlugin::audioReadStereoSamples" );

    static bool needInput = TRUE;

    if ( samples == 0 )
        return FALSE;

    do {
        if ( needInput )
            if ( !read() ) {
                return FALSE;
            }

        needInput = FALSE;

        if ( decode( output, samples, samplesMade ) )
            return TRUE;
        else
            needInput = TRUE;
    }
    while ( ( samplesMade < samples ) && ( !d->input.eof ) );

    return FALSE;
}


double LibMadPlugin::getTime() {
    debugMsg( "LibMadPlugin::getTime" );
    return 0.0;
}


void LibMadPlugin::printID3Tags() {
   //    odebug << "LibMadPlugin::printID3Tags" << oendl;

    char id3v1[128 + 1];

    if ( ::lseek( d->input.fd, -128, SEEK_END ) == -1 ) {
       //        odebug << "error seeking to id3 tags" << oendl;
        return;
    }

    if ( ::read( d->input.fd, id3v1, 128 ) != 128 ) {
       //        odebug << "error reading in id3 tags" << oendl;
        return;
    }

    if ( ::strncmp( (const char *)id3v1, "TAG", 3 ) != 0 ) {
        debugMsg( "sorry, no id3 tags" );
    } else {
        int len[5] = { 30, 30, 30, 4, 30 };
        QString label[5] = { tr( "Title" ), tr( "Artist" ), tr( "Album" ), tr( "Year" ), tr( "Comment" ) };
        char *ptr = id3v1 + 3, *ptr2 = ptr + len[0];
        //        odebug << "ID3 tags in file:" << oendl;
        info = "";
        for ( int i = 0; i < 5; ptr += len[i], i++, ptr2 += len[i] ) {
            char push = *ptr2;
            *ptr2 = '\0';
            char *ptr3 = ptr2;
            while ( ptr3-1 >= ptr && isspace(ptr3[-1]) ) ptr3--;
            char push2 = *ptr3; *ptr3 = '\0';
            if ( strcmp( ptr, "" ) ) {
                if( ((QString)ptr).find("  ") == -1) // don't add anything that has blanks
                info += ( i != 0 ? ", " : "" ) + label[i] + ": " + ptr;
            }
//             odebug << info.latin1() << oendl;
            *ptr3 = push2;
            *ptr2 = push;
        }
        if (id3v1[126] == 0 && id3v1[127] != 0)
            info += tr( ", Track: " ) + id3v1[127];
    }

    if ( ::lseek(d->input.fd, 0, SEEK_SET) == -1 ) {
       //        odebug << "error seeking back to beginning" << oendl;
        return;
    }
}