aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/Input/cdaudio
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/Input/cdaudio')
0 files changed, 0 insertions, 0 deletions
er_flac.cpp?id=2d622fd9bcb8da9dd3f3206e296cd6a701fc9d12'>2d622fd9b
a8bf072ec
2d622fd9b


bfbf64f8d



a3e55825c




cfd5af139
66ae0bd66

ed429c036
a3e55825c
ed429c036
b55c45843
2d622fd9b
668e1355e
66ae0bd66
2d622fd9b

d0b331da0

668e1355e
2d622fd9b
668e1355e
2d622fd9b

668e1355e
2d622fd9b
668e1355e


2d622fd9b
668e1355e
























2d622fd9b
668e1355e



2d622fd9b

7ef8d11ba
2d622fd9b
7ef8d11ba
2d622fd9b
2d622fd9b
75f605a0e
2d622fd9b

75f605a0e
2d622fd9b





75f605a0e
2d622fd9b


2d622fd9b

75f605a0e





2d622fd9b



bfbf64f8d
5f4a1ef29


2d622fd9b
7ef8d11ba
75f605a0e
2d622fd9b



75f605a0e
2d622fd9b








2d622fd9b

9d12276bc
5f4a1ef29


2d622fd9b
7ef8d11ba
2d622fd9b

75f605a0e
2d622fd9b

9d12276bc

d0b331da0
9d12276bc
75f605a0e
d0b331da0
75f605a0e
d0b331da0

9d12276bc
75f605a0e
d0b331da0


75f605a0e

d0b331da0
75f605a0e


d0b331da0

2d622fd9b
75f605a0e

2d622fd9b
75f605a0e

2d622fd9b



bfbf64f8d
5f4a1ef29

2d622fd9b
7ef8d11ba
75f605a0e
d0b331da0

75f605a0e
2d622fd9b


bfbf64f8d
5f4a1ef29

2d622fd9b
7ef8d11ba
75f605a0e
d0b331da0
2d622fd9b
75f605a0e
2d622fd9b



bfbf64f8d
5f4a1ef29

2d622fd9b
7ef8d11ba
75f605a0e
d0b331da0

75f605a0e
2d622fd9b


bfbf64f8d


2d622fd9b
7ef8d11ba
2d622fd9b




75f605a0e
2d622fd9b

75f605a0e
2d622fd9b
75f605a0e


d0b331da0
75f605a0e
d0b331da0
75f605a0e
d0b331da0


75f605a0e
d0b331da0
2d622fd9b


bfbf64f8d
2d622fd9b
11b7655bb
2d622fd9b

bfbf64f8d





2d622fd9b


7ef8d11ba

2d622fd9b
a3e55825c
75f605a0e
2d622fd9b

2d622fd9b


75f605a0e
2d622fd9b
75f605a0e


b917024aa
2d622fd9b
362d4e2b4

b917024aa
2d622fd9b

2d622fd9b

75f605a0e
2d622fd9b
a3e55825c

47f1be7e4

b55c45843
66ae0bd66

a3e55825c
66ae0bd66

acb3d9b6c
66ae0bd66
a3e55825c
26724c66d
66ae0bd66


362d4e2b4
66ae0bd66
699ae4cd4

11b7655bb
699ae4cd4
75f605a0e

66ae0bd66
526e04b8b
66ae0bd66
2aa6a2255

66ae0bd66
2aa6a2255
526e04b8b
66ae0bd66
a3e55825c



11b7655bb
a3e55825c



66ae0bd66
11b7655bb
a3e55825c
2d622fd9b

75f605a0e
2d622fd9b
362d4e2b4
11b7655bb
2d622fd9b

2d622fd9b


9d12276bc
d0b331da0



2d622fd9b




cfd5af139

75f605a0e
cfd5af139



75f605a0e
cfd5af139
75f605a0e

2d622fd9b
e6cdd7a53




11b7655bb
e6cdd7a53










75f605a0e
e6cdd7a53
11b7655bb
e6cdd7a53
d0b331da0
0d919a16e
e6cdd7a53



2d622fd9b








75f605a0e
e6cdd7a53
11b7655bb
e6cdd7a53
d0b331da0
0d919a16e
e6cdd7a53

2d622fd9b
e6cdd7a53
11b7655bb
2d622fd9b


75f605a0e
2d622fd9b
11b7655bb
2d622fd9b
76f436c36
75f605a0e
54d001e35
76f436c36
75f605a0e
76f436c36


75f605a0e
435f1da1d

75f605a0e
435f1da1d

75f605a0e
435f1da1d


75f605a0e
435f1da1d

11b7655bb
435f1da1d
362d4e2b4


c955fb39c
362d4e2b4

49284bb0d
66ae0bd66
362d4e2b4


49284bb0d
2d622fd9b
2d622fd9b
013ee07b5
2d622fd9b

868b4f765
2d622fd9b
362d4e2b4

75f605a0e
2d622fd9b

868b4f765
6247dd723
75f605a0e
6247dd723
2d622fd9b
362d4e2b4
2d622fd9b
362d4e2b4

435f1da1d
362d4e2b4

75f605a0e

362d4e2b4
2d622fd9b

75f605a0e
6247dd723
362d4e2b4









75f605a0e
362d4e2b4


b917024aa
362d4e2b4





75f605a0e
362d4e2b4

















75f605a0e
362d4e2b4

75f605a0e
6247dd723
2d622fd9b


75f605a0e

6247dd723
75f605a0e
a3e55825c
75f605a0e

b917024aa
a3e55825c
362d4e2b4

b917024aa
362d4e2b4

d854da17a
362d4e2b4

66ae0bd66
362d4e2b4

7cfd365a4

362d4e2b4
7cfd365a4
362d4e2b4
7cfd365a4
362d4e2b4
c955fb39c

362d4e2b4

435f1da1d
602a9f7bb
66ae0bd66
362d4e2b4
7cfd365a4
2d622fd9b
cfd5af139












76f436c36
54d001e35
76f436c36





























































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
                                                                            
                                                                            
                                                                            













                                                                            
                                                                            


                                                                             



                                                               




                               
                               

                                     
                  
                
                    
                             
                     
                   
                           

                         

                                 
                                                  
                                                                 
                                                

                                                               
                         
 


                                           
 
























                                                                      
 



                                               

 
                                                                           
 
                                                            
                     
 
                                  

     
                                                         





                                                      
                                   


                     

     





                                                                



                   
                                                                                    


                            
 
                                                            
                                                           



                     
                                








                                                              

 
                                                                                        


                                          
 
                                                            

                                                          
                    

                                                       

                                     
                                                                     
     
                                                        
         
                                                                                                            

                                                   
 
                                                     


        

                                                                                         
         


                                                                                                          

         
 

                                                                
                                                                 

                                                                   



                                                      
                                                                                     

                             
 
                                                            
                                   

                                                            
                                  


                                               
                                                                                     

                            
 
                                                            
                                   
                                                            
 
                                    



                                                    
                                                                                         

                                    
 
                                                            
                                   

                                                              
                                         


                                                 


                                                                         
 
                                                            




                                                         
                             

                                                               
                               
                                                       


                                                                      
 
                                                                            
         
                                                                    


            
                              
         


     
                                                                         
 
                 

 





                                                                       


                

                                                                         
 
                           
                      

 


                           
               
     


                                                         
                         
     

                       
                    

 

                              
                       
     

                                                             

                                
                                                   

                                                                                         
                                          

                                                                     
 
                                                                      
             
                                                                    


                                                                                                      
                                                          
                                                              

                                                                           
                                 
                 

                                                         
                                                                                               
                 
                                                                               

                                                               
                                                                                                     
                     
                 
                                                                                 



                                                                          
                             



             
                                                                  
                         
         

     
                                 
     
                                                           
                     

     


                                   
                                     



                              




                                                            

                  
                                          



                                                                          
                                        
     

                                         
                                             




                                                        
                         










                                                 
                                                               
         
                         
         
                                                     
                                                   



                                             








                                       
                                                               
         
                         
         
                                                        
                                               

        
     
                                                    
                     


                                                            
                                 
     
                     
     
 
                                                        
                       
     
                                                                                      


                     
                                   

           
                                                            

              
                                                               


              
                                                               

              
                     
     


                
                                               

                                                          
                                                                        
                                                                     


                     
                                         
 
                                             
                

 
                                     
 

                        
                          

 
                                
 
                           
 
 
                                   
 

                                                   
                                                              

                         

                                                                                             
 

 
                                                         
 









                                                                    
                                     


                                  
                                





                                                        
                                                  

















                                                                                           
                                               

                    
                                           
 


                          

                                                      
 
                                                               
     

                               
                                
      

                        
                       

 
                                          

                                                   
                                          

                         

 
                        
 
                                                   
     
                  

                                               

                                                          
                                                                         
                                                         
                                                                     
                         
     
 












                                                                           
 
                                                





























































                                                    
/***************************************************************************
 *   Copyright (C) 2006-2021 by Ilya Kotov                                 *
 *   forkotov02@ya.ru                                                      *
 *                                                                         *
 *   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 Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/


/* The code is based on MOC by Damian Pietras <daper@daper.net>
   and libxmms-flac written by Josh Coalson. */


#include <taglib/tag.h>
#include <taglib/fileref.h>
#include <taglib/flacfile.h>
#include <taglib/xiphcomment.h>
#include <taglib/tmap.h>
#include <taglib/id3v2header.h>
#include <taglib/tfilestream.h>
#include <taglib/id3v2framefactory.h>
#include <QObject>
#include <QFile>
#include <QIODevice>
#include <QRegularExpression>
#include <FLAC/all.h>
#include <stdint.h>
#include <qmmp/cueparser.h>
#include "decoder_flac.h"

#define BITRATE_CALC_TIME_MS 2000

static size_t pack_pcm_signed (FLAC__byte *output,
                               const FLAC__int32 * const input[],
                               unsigned samples,
                               unsigned channels, unsigned bps)
{
    unsigned channel = 0;

    uint8_t *data8 = (uint8_t *) output;
    uint16_t *data16 = (uint16_t *) output;
    uint32_t *data32 = (uint32_t *) output;

     for(unsigned sample = 0; sample < samples; sample++)
     {
         for (channel = 0; channel < channels; channel++)
         {
             switch (bps)
             {
             case 8:
                 *data8 = input[channel][sample] & 0xff;
                 data8++;
                 break;
             case 16:
                 *data16 = input[channel][sample] & 0xffffff;
                 data16++;
                 break;
             case 24:
                 *data32 = (input[channel][sample] << 8) & 0xffffff00;
                 data32++;
                 break;
             case 32:
                 *data32 = input[channel][sample];
                 data32++;
                 break;
             }
         }
     }

     if(bps == 24) // we encode to 32-bit words
         bps = 32;

     return samples * channels * bps / 8;
}

static int flac_decode (void *client_data, unsigned char *buf, int buf_len)
{
    flac_data *data = static_cast<flac_data *>(client_data);
    unsigned to_copy;

    if (!data->sample_buffer_fill)
    {

        if (FLAC__stream_decoder_get_state(data->decoder)
                == FLAC__STREAM_DECODER_END_OF_STREAM)
        {
            return 0;
        }

        if (!FLAC__stream_decoder_process_single(
                    data->decoder))
        {
            return 0;
        }
    }

    to_copy = qMin((unsigned)buf_len, data->sample_buffer_fill);
    memcpy (buf, data->sample_buffer, to_copy);
    memmove (data->sample_buffer,
             data->sample_buffer + to_copy,
             data->sample_buffer_fill - to_copy);
    data->sample_buffer_fill -= to_copy;
    return to_copy;
}


static FLAC__StreamDecoderReadStatus flac_callback_read (const FLAC__StreamDecoder*,
        FLAC__byte buffer[],
        size_t *bytes,
        void *client_data)
{
    flac_data *data = static_cast<flac_data *>(client_data);
    qint64 res = data->input->read((char *)buffer, *bytes);

    if (res > 0)
    {
        *bytes = res;
        data->read_bytes += res;
        return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
    }
    if (res == 0)
    {
        *bytes = res;
        return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
    }

    return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
}

static FLAC__StreamDecoderWriteStatus flac_callback_write (const FLAC__StreamDecoder *d,
        const FLAC__Frame *frame,
        const FLAC__int32* const buffer[],
        void *client_data)
{
    flac_data *data = static_cast<flac_data *>(client_data);
    const unsigned wide_samples = frame->header.blocksize;

    if (data->abort)
        return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;

    //bitrate calculation
    FLAC__uint64 decode_position = 0;
    if(FLAC__stream_decoder_get_decode_position(d, &decode_position))
    {
        if(decode_position > data->last_decode_position)
        {
            data->bitrate = (decode_position - data->last_decode_position) * 8 * frame->header.sample_rate /
                    frame->header.blocksize / 1000;
        }

        data->last_decode_position = decode_position;
    }
    else
    {
        data->frame_counter += wide_samples;
        if(data->frame_counter * 1000 / frame->header.sample_rate > BITRATE_CALC_TIME_MS)
        {
            data->bitrate = data->read_bytes * 8 * frame->header.sample_rate / data->frame_counter / 1000;
            data->frame_counter = 0;
            data->read_bytes = 0;
        }
    }

    data->sample_buffer_fill = pack_pcm_signed (
                                            data->sample_buffer,
                                            buffer, wide_samples,
                                            data->channels,
                                            data->bits_per_sample);

    return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}

static FLAC__StreamDecoderTellStatus flac_callback_tell (const FLAC__StreamDecoder *,
        FLAC__uint64 *offset,
        void *client_data)
{
    flac_data *data = static_cast<flac_data *>(client_data);
    if(data->input->isSequential())
        return FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED;

    *offset = data->input->pos ();
    return FLAC__STREAM_DECODER_TELL_STATUS_OK;
}

static FLAC__StreamDecoderSeekStatus flac_callback_seek (const FLAC__StreamDecoder *,
        FLAC__uint64 offset,
        void *client_data)
{
    flac_data *data = static_cast<flac_data *>(client_data);
    if(data->input->isSequential())
        return FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED;

    return data->input->seek(offset)
           ? FLAC__STREAM_DECODER_SEEK_STATUS_OK
           : FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
}

static FLAC__StreamDecoderLengthStatus flac_callback_length (const FLAC__StreamDecoder *,
        FLAC__uint64 *stream_length,
        void *client_data)
{
    flac_data *data = static_cast<flac_data *>(client_data);
    if(data->input->isSequential())
        return FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED;

    *stream_length = data->input->size();
    return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
}

static void flac_callback_metadata (const FLAC__StreamDecoder *,
                                    const FLAC__StreamMetadata *metadata,
                                    void *client_data)
{
    flac_data *data = static_cast<flac_data *>(client_data);

    if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO)
    {
        qDebug ("DecoderFLAC: getting metadata info");

        data->total_samples =
            (unsigned)(metadata->data.stream_info.total_samples
                       & 0xffffffff);
        data->bits_per_sample =
            metadata->data.stream_info.bits_per_sample;
        data->channels = metadata->data.stream_info.channels;
        data->sample_rate = metadata->data.stream_info.sample_rate;
        data->length = data->total_samples * 1000 / data->sample_rate;

        if(metadata->data.stream_info.total_samples > 0 && data->length > 0)
        {
            data->bitrate = data->input->size() * 8 /  data->length;
        }
        else
        {
            data->bitrate = 0;
        }
    }
}

static FLAC__bool flac_callback_eof (const FLAC__StreamDecoder *, void *)
{
    return false;
}

static void flac_callback_error (const FLAC__StreamDecoder *,
                                 FLAC__StreamDecoderErrorStatus status,
                                 void *)
{
    Q_UNUSED(status);
}

// Decoder class

DecoderFLAC::DecoderFLAC(const QString &path, QIODevice *i) : Decoder(i),
    m_path(path)
{
    m_data = new flac_data;
    m_data->input = i;
}

DecoderFLAC::~DecoderFLAC()
{
    deinit();
    if (m_data)
    {
        if (m_data->decoder)
            FLAC__stream_decoder_delete(m_data->decoder);
        delete m_data;
        m_data = nullptr;
    }
    if(m_buf)
        delete[] m_buf;
    m_buf = nullptr;
}

bool DecoderFLAC::initialize()
{
    if (!m_data->input)
    {
        if (m_path.startsWith("flac://")) //embeded cue track
        {
            QString p = m_path;
            p.remove("flac://");
            p.remove(QRegularExpression("#\\d+$"));
            TagLib::FileStream stream(QStringToFileName(p), true);
            TagLib::FLAC::File fileRef(&stream, TagLib::ID3v2::FrameFactory::instance());
            //looking for cuesheet comment
            TagLib::Ogg::XiphComment *tag = fileRef.xiphComment();
            TagLib::FLAC::Properties *ap = fileRef.audioProperties();

            if (ap && tag && tag->fieldListMap().contains("CUESHEET"))
            {
                qDebug("DecoderFLAC: using cuesheet xiph comment.");
                m_parser = new CueParser(tag->fieldListMap()["CUESHEET"].toString() .toCString(true));
                m_parser->setDuration(fileRef.audioProperties()->lengthInMilliseconds());
                m_parser->setUrl("flac", p);
                m_track = m_path.section("#", -1).toInt();
                if(m_track < 1 || m_track > m_parser->count())
                {
                    qWarning("DecoderFLAC: invalid cuesheet xiph comment");
                    return false;
                }
                m_data->input = new QFile(p);
                m_data->input->open(QIODevice::ReadOnly);
                if(tag->contains("DISCNUMBER") && !tag->fieldListMap()["DISCNUMBER"].isEmpty())
                {
                    TagLib::StringList fld = tag->fieldListMap()["DISCNUMBER"];
                    for(int i = 1; i <= m_parser->count(); i++)
                    {
                        m_parser->setMetaData(i, Qmmp::DISCNUMBER, TStringToQString(fld.toString()));
                    }
                }
                addMetaData(m_parser->info(m_track)->metaData()); //send metadata
            }
            else
            {
                qWarning("DecoderFLAC: unable to find cuesheet comment.");
                return false;
            }
        }
        else
        {
            qWarning("DecoderFLAC: cannot initialize. No input.");
            return false;
        }
    }

    if (!m_data->input->isOpen())
    {
        qWarning("DecoderFLAC: unable to open input file");
        return false;
    }

    m_data->bitrate = -1;
    m_data->abort = 0;
    m_data->sample_buffer_fill = 0;
    m_data->last_decode_position = 0;
    m_data->read_bytes = 0;
    m_data->frame_counter = 0;


    if (!m_data->decoder)
    {
        qDebug("DecoderFLAC: creating FLAC__StreamDecoder");
        m_data->decoder = FLAC__stream_decoder_new ();
    }
    char buf[500];
    //skip id3v2
    m_data->input->peek(buf, sizeof(buf));
    ulong id3v2_size = findID3v2(buf, sizeof(buf));
    if(id3v2_size)
    {
        qDebug("DecoderFLAC: skipping id3v2 tag (%lu bytes)", id3v2_size);
        m_data->input->seek(id3v2_size);
    }
    m_data->input->peek(buf,sizeof(buf));
    m_data->input->seek(0);
    qDebug("DecoderFLAC: setting callbacks");
    if(!memcmp(buf, "OggS", 4))
    {
        if(!FLAC_API_SUPPORTS_OGG_FLAC)
        {