summaryrefslogtreecommitdiff
path: root/src/libflac/demux_flac.c
blob: 805a0fb76c8e0803dbd98b4216a67adb314dbe52 (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
/* 
 * Copyright (C) 2000-2003 the xine project
 * 
 * This file is part of xine, a free video player.
 * 
 * xine 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.
 * 
 * xine 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
 *
 *
 * John McCutchan
 * FLAC demuxer (http://flac.sf.net)
 *
 * TODO: Skip id3v2 tags.
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sched.h>
#include <string.h>
#include <stdlib.h>

#include <FLAC/seekable_stream_decoder.h>

#define LOG_MODULE "demux_flac"
#define LOG_VERBOSE
/*
#define LOG
*/

#include "xine_internal.h"
#include "xineutils.h"
#include "../demuxers/demux.h"

#include "demux_flac.h"

/* FLAC Demuxer plugin */
typedef struct demux_flac_s {
  demux_plugin_t        demux_plugin;

  xine_stream_t        *stream;
  
  fifo_buffer_t        *audio_fifo;
  fifo_buffer_t        *video_fifo;

  input_plugin_t       *input;

  int status;

  int seek_flag;

  off_t data_start;
  off_t data_size;

  /* FLAC Stuff */
  FLAC__SeekableStreamDecoder *flac_decoder;

  uint64_t total_samples;
  uint64_t bits_per_sample;
  uint64_t channels;
  uint64_t sample_rate;
  uint64_t length_in_msec;
} demux_flac_t ;


/* FLAC Demuxer class */
typedef struct demux_flac_class_s {
  demux_class_t     demux_class;

  xine_t           *xine;
  config_values_t  *config;

} demux_flac_class_t;

/* FLAC Callbacks */
static FLAC__SeekableStreamDecoderReadStatus
flac_read_callback (const FLAC__SeekableStreamDecoder *decoder,
                    FLAC__byte buffer[],
                    unsigned *bytes,
                    void *client_data)
{
    demux_flac_t *this    = (demux_flac_t *)client_data;
    input_plugin_t *input = this->input; 
    off_t offset = *bytes;

    lprintf("flac_read_callback\n");
    
    /* This should only be called when flac is reading the metadata
     * of the flac stream.
     */
    
    offset = input->read (input, buffer, offset);

    lprintf("Read %lld / %u bytes into buffer\n", offset, *bytes);

    *bytes = offset;
    /* This is the way to detect EOF with xine input plugins */
    if ( (offset != *bytes) && (*bytes != 0) )
    {
      lprintf("Marking EOF\n");
      
      this->status = DEMUX_FINISHED;
      return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR;
    }
    else
    {
      lprintf("Read was perfect\n");
    
      return FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK;
    }
}

static FLAC__SeekableStreamDecoderSeekStatus
flac_seek_callback (const FLAC__SeekableStreamDecoder *decoder,
                    FLAC__uint64 absolute_byte_offset,
                    void *client_data)
{
    input_plugin_t *input = ((demux_flac_t *)client_data)->input;
    off_t offset;

    lprintf("flac_seek_callback\n");

    offset = input->seek (input, absolute_byte_offset, SEEK_SET);

    if (offset == -1)
        return FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR;
    else
        return FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK;
}

static FLAC__SeekableStreamDecoderTellStatus
flac_tell_callback (const FLAC__SeekableStreamDecoder *decoder,
                    FLAC__uint64 *absolute_byte_offset,
                    void *client_data)
{
    input_plugin_t *input = ((demux_flac_t *)client_data)->input;
    off_t offset;

    lprintf("flac_tell_callback\n");

    offset = input->get_current_pos (input);

    *absolute_byte_offset = offset;

    return FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK;
}

static FLAC__SeekableStreamDecoderLengthStatus
flac_length_callback (const FLAC__SeekableStreamDecoder *decoder,
                     FLAC__uint64 *stream_length,
                     void *client_data)
{
    input_plugin_t *input = ((demux_flac_t *)client_data)->input;
    off_t offset;

    lprintf("flac_length_callback\n");

    offset = input->get_length (input);

    /* FIXME, can flac handle -1 as offset ? */
    return FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK;
}

static FLAC__bool 
flac_eof_callback (const FLAC__SeekableStreamDecoder *decoder,
                    void *client_data)
{
    demux_flac_t *this = (demux_flac_t *)client_data;

    lprintf("flac_eof_callback\n");

    if (this->status == DEMUX_FINISHED)
    {
      lprintf("flac_eof_callback: True!\n");

      return true;
    }
    else
    {
      lprintf("flac_eof_callback: False!\n");

      return false;
    }
}

static FLAC__StreamDecoderWriteStatus 
flac_write_callback (const FLAC__SeekableStreamDecoder *decoder, 
                     const FLAC__Frame *frame,
                     const FLAC__int32 * const buffer[],
                     void *client_data) 
{
    /* This should never be called, all we use flac for in this demuxer
     * is seeking. We do the decoding in the decoder
     */
    
  lprintf("Error: Write callback was called!\n");

  return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}

static void 
flac_metadata_callback (const FLAC__SeekableStreamDecoder *decoder, 
                        const FLAC__StreamMetadata *metadata, 
                        void *client_data)
{
    demux_flac_t *this = (demux_flac_t *)client_data;
  
    lprintf("IN: Metadata callback\n");

    /* This should be called when we first look at a flac stream,
     * We get information about the stream here.
     */
     if (metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
       lprintf("Got METADATA!\n");

       this->total_samples   = metadata->data.stream_info.total_samples;
       this->bits_per_sample = metadata->data.stream_info.bits_per_sample;
       this->channels        = metadata->data.stream_info.channels;
       this->sample_rate     = metadata->data.stream_info.sample_rate;
       this->length_in_msec  = (this->total_samples * 10 /
                                (this->sample_rate / 100))/1000;
     }
     return;
}

static void 
flac_error_callback (const FLAC__SeekableStreamDecoder *decoder, 
                     FLAC__StreamDecoderErrorStatus status,
                     void *client_data)
{
    demux_flac_t *this = (demux_flac_t *)client_data;
    /* This will be called if there is an error when flac is seeking
     * in the stream.
     */

    xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_flac: flac_error_callback\n");

    if (status == FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC)
      xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, 
	      "demux_flac: Decoder lost synchronization.\n");
    else if (status == FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER)
      xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG,
	      "demux_flac: Decoder encounted a corrupted frame header.\n");
    else if (status == FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH)
      xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, 
	      "demux_flac: Frame's data did not match the CRC in the footer.\n");
    else
      xprintf(this->stream->xine, XINE_VERBOSITY_DEBUG, "demux_flac: unknown error.\n");
    
    this->status = DEMUX_FINISHED;
    
    return;
}

/* FLAC Demuxer plugin */
static int 
demux_flac_send_chunk (demux_plugin_t *this_gen) {
    demux_flac_t *this = (demux_flac_t *) this_gen;
    buf_element_t *buf = NULL;
    off_t current_file_pos;
    int64_t current_pts;
    unsigned int remaining_sample_bytes = 0;

    remaining_sample_bytes = 2048;

    current_file_pos = this->input->get_current_pos (this->input) 
                        - this->data_start;

    current_pts = current_file_pos;
    current_pts *= 90000;
    if (this->sample_rate != 0)
    {
        current_pts /= this->sample_rate;
    }

    if (this->seek_flag) {
        _x_demux_control_newpts (this->stream, current_pts, 0);
        this->seek_flag = 0;
    }

    while (remaining_sample_bytes)
    {
        if(!this->audio_fifo) {
          this->status = DEMUX_FINISHED;
          break;
        }

        buf = this->audio_fifo->buffer_pool_alloc (this->audio_fifo);
        buf->type = BUF_AUDIO_FLAC;
        buf->extra_info->input_pos    = current_file_pos;
        buf->extra_info->input_length = this->data_size;
        buf->extra_info->input_time   = current_pts / 90;
        //buf->pts = current_pts;

        if (remaining_sample_bytes > buf->max_size)
            buf->size = buf->max_size;
        else
            buf->size = remaining_sample_bytes;

        remaining_sample_bytes -= buf->size;

        if (this->input->read (this->input,buf->content,buf->size)!=buf->size) {
	  lprintf("buf->size != input->read()\n");

	  buf->free_buffer (buf);
	  this->status = DEMUX_FINISHED;
	  break;
        }

        /*
        if (!remaining_sample_bytes)
        {
            buf->decoder_flags |= BUF_FLAG_FRAME_END;
        }*/

        this->audio_fifo->put (this->audio_fifo, buf);
    }

    return this->status;
}

static void 
demux_flac_send_headers (demux_plugin_t *this_gen) {
    demux_flac_t *this = (demux_flac_t *) this_gen;

    buf_element_t *buf;

    lprintf("demux_flac_send_headers\n");

    this->video_fifo = this->stream->video_fifo;
    this->audio_fifo = this->stream->audio_fifo;

    this->status = DEMUX_OK;

    _x_stream_info_set(this->stream, XINE_STREAM_INFO_HAS_VIDEO, 0);
    _x_stream_info_set(this->stream, XINE_STREAM_INFO_HAS_AUDIO, 1);
    _x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_CHANNELS, this->channels);
    _x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_SAMPLERATE, this->sample_rate);
    _x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_BITS, this->bits_per_sample);

    _x_demux_control_start (this->stream);

    if (this->audio_fifo) {
        buf = this->audio_fifo->buffer_pool_alloc (this->audio_fifo);
        buf->type = BUF_AUDIO_FLAC;
        buf->decoder_flags   = BUF_FLAG_HEADER;
        buf->decoder_info[0] = 0;
        buf->decoder_info[1] = this->sample_rate;
        buf->decoder_info[2] = this->bits_per_sample;
        buf->decoder_info[3] = this->channels;
        buf->size = 0;
        this->audio_fifo->put (this->audio_fifo, buf);
    }
}

static void 
demux_flac_dispose (demux_plugin_t *this_gen) {
    demux_flac_t *this = (demux_flac_t *) this_gen;

    lprintf("demux_flac_dispose\n");

    if (this->flac_decoder)
        FLAC__seekable_stream_decoder_delete (this->flac_decoder);

    free(this);
    return;
}

static int 
demux_flac_get_status (demux_plugin_t *this_gen) {
    demux_flac_t *this = (demux_flac_t *) this_gen;

    lprintf("demux_flac_get_status\n");

    return this->status;
}


static int 
demux_flac_seek (demux_plugin_t *this_gen, off_t start_pos, int start_time, int playing) {
    demux_flac_t *this = (demux_flac_t *) this_gen;

    lprintf("demux_flac_seek\n");

    if (start_pos || !start_time) {
        
        this->input->seek (this->input, start_pos, SEEK_SET);
        lprintf ("Seek to position: %lld\n", start_pos);
    
    } else {
      
        double distance = (double)start_time;
        uint64_t target_sample = (uint64_t)(distance * this->total_samples);
        FLAC__bool s = false;

        if (this->length_in_msec != 0)
        {
            distance /= (double)this->length_in_msec;
        }


        s = FLAC__seekable_stream_decoder_seek_absolute (this->flac_decoder,
                                                         target_sample);

        if (s) {
	  lprintf ("Seek to: %d successfull!\n", start_time);
        } else
            this->status = DEMUX_FINISHED;
    }

    _x_demux_flush_engine (this->stream);

    return this->status;
}

static int 
demux_flac_get_stream_length (demux_plugin_t *this_gen) {
    demux_flac_t *this = (demux_flac_t *) this_gen; 

    lprintf("demux_flac_get_stream_length\n");

    if (this->flac_decoder)
        return this->length_in_msec;
    else
        return 0;
}

static uint32_t 
demux_flac_get_capabilities (demux_plugin_t *this_gen) {
  lprintf("demux_flac_get_capabilities\n");

  return DEMUX_CAP_NOCAP;
}

static int 
demux_flac_get_optional_data (demux_plugin_t *this_gen, void *data, int dtype) {
  lprintf("demux_flac_get_optional_data\n");

  return DEMUX_OPTIONAL_UNSUPPORTED;
}

static demux_plugin_t *
open_plugin (demux_class_t *class_gen, 
             xine_stream_t *stream, 
             input_plugin_t *input) {
    demux_flac_t *this;

    lprintf("open_plugin\n");

    switch (stream->content_detection_method) {
        case METHOD_BY_CONTENT:
        {
          uint8_t      buf[MAX_PREVIEW_SIZE];
          int          len;

          /* 
           * try to get a preview of the data
           */
          len = input->get_optional_data (input, buf, INPUT_OPTIONAL_DATA_PREVIEW);
          if (len == INPUT_OPTIONAL_UNSUPPORTED) {
      
            if (input->get_capabilities (input) & INPUT_CAP_SEEKABLE) {
      
              input->seek (input, 0, SEEK_SET);
              if ( (len=input->read (input, buf, 1024)) <= 0)
                return NULL;
              input->seek (input, 0, SEEK_SET);
      
            } else
              return NULL;
          }      

          /* FIXME: Skip id3v2 tag */
          /* Look for fLaC tag at the beginning of file */
          if ( (buf[0] != 'f') || (buf[1] != 'L') ||
               (buf[2] != 'a') || (buf[3] != 'C') )
            return NULL;
        }
        break;
        case METHOD_BY_EXTENSION: {
            char *ending, *mrl;
    
            mrl = input->get_mrl (input);
    
            ending = strrchr (mrl, '.');

            if (!ending || (strlen (ending) < 5))
                return NULL;

            if (strncasecmp (ending, ".flac", 5))
                return NULL;
        }
        break;
        case METHOD_EXPLICIT:
        break;
        default:
            return NULL;
        break;
    }

    /*
    * if we reach this point, the input has been accepted.
    */

    this         = xine_xmalloc (sizeof (demux_flac_t));
    this->stream = stream;
    this->input  = input;

    this->demux_plugin.send_headers      = demux_flac_send_headers;
    this->demux_plugin.send_chunk        = demux_flac_send_chunk;
    this->demux_plugin.seek              = demux_flac_seek;
    this->demux_plugin.dispose           = demux_flac_dispose;
    this->demux_plugin.get_status        = demux_flac_get_status;
    this->demux_plugin.get_stream_length = demux_flac_get_stream_length;
    this->demux_plugin.get_capabilities  = demux_flac_get_capabilities;
    this->demux_plugin.get_optional_data = demux_flac_get_optional_data;
    this->demux_plugin.demux_class       = class_gen;

    this->seek_flag = 0;
  

    /* Get a new FLAC decoder and hook up callbacks */
    this->flac_decoder = FLAC__seekable_stream_decoder_new();
    lprintf("this->flac_decoder: %p\n", this->flac_decoder);

    FLAC__seekable_stream_decoder_set_md5_checking  (this->flac_decoder, false);
    FLAC__seekable_stream_decoder_set_read_callback (this->flac_decoder,
                                                     flac_read_callback);
    FLAC__seekable_stream_decoder_set_seek_callback (this->flac_decoder,
                                                     flac_seek_callback);
    FLAC__seekable_stream_decoder_set_tell_callback (this->flac_decoder,
                                                     flac_tell_callback);
    FLAC__seekable_stream_decoder_set_length_callback (this->flac_decoder,
                                                     flac_length_callback);
    FLAC__seekable_stream_decoder_set_eof_callback  (this->flac_decoder,
                                                     flac_eof_callback);
    FLAC__seekable_stream_decoder_set_metadata_callback (this->flac_decoder,
                                                     flac_metadata_callback);
    FLAC__seekable_stream_decoder_set_write_callback (this->flac_decoder,
                                                     flac_write_callback);
    FLAC__seekable_stream_decoder_set_error_callback (this->flac_decoder,
                                                     flac_error_callback);
    FLAC__seekable_stream_decoder_set_client_data    (this->flac_decoder,
                                                     this);

    FLAC__seekable_stream_decoder_init (this->flac_decoder);

    /* Get some stream info */
    this->data_size  = this->input->get_length (this->input);
    this->data_start = this->input->get_current_pos (this->input);

    /* This will cause FLAC to give us the rest of the information on
     * this flac stream
     */
    this->status = DEMUX_OK;
    FLAC__seekable_stream_decoder_process_until_end_of_metadata (this->flac_decoder);
    
    lprintf("Processed file until end of metadata\n");

    return &this->demux_plugin;
}


/* FLAC Demuxer class */

static char *
get_description (demux_class_t *this_gen) {
    return "FLAC demux plugin";
}
 
static char *
get_identifier (demux_class_t *this_gen) {
    return "FLAC";
}

static char *
get_extensions (demux_class_t *this_gen) {
    return "flac";
}

static char *
get_mimetypes (demux_class_t *this_gen) {
    return "application/x-flac: flac: FLAC Audio;";
}

static void 
class_dispose (demux_class_t *this_gen) {
    demux_flac_class_t *this = (demux_flac_class_t *) this_gen;

    lprintf("class_dispose\n");

    free (this);
}

void *
demux_flac_init_class (xine_t *xine, void *data) {

    demux_flac_class_t     *this;
  
    lprintf("demux_flac_init_class\n");

    this         = xine_xmalloc (sizeof (demux_flac_class_t));
    this->config = xine->config;
    this->xine   = xine;

    this->demux_class.open_plugin     = open_plugin;
    this->demux_class.get_description = get_description;
    this->demux_class.get_identifier  = get_identifier;
    this->demux_class.get_mimetypes   = get_mimetypes;
    this->demux_class.get_extensions  = get_extensions;
    this->demux_class.dispose         = class_dispose;

    return this;
}