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
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
|
/*
* 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
*
* $Id: demux_mpgaudio.c,v 1.139 2004/12/16 13:59:11 mroi Exp $
*
* demultiplexer for mpeg audio (i.e. mp3) streams
*
* mp3 file structure:
* [id3v2] [Xing] Frame1 Frame2 Frame3... [id3v1]
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#define LOG_MODULE "demux_mpeg_audio"
#define LOG_VERBOSE
/*
#define LOG
*/
#include "xine_internal.h"
#include "xineutils.h"
#include "demux.h"
#include "compat.h"
#include "bswap.h"
#include "group_audio.h"
#include "id3.h"
#define NUM_PREVIEW_BUFFERS 10
#define SNIFF_BUFFER_LENGTH 1024
#define WRAP_THRESHOLD 120000
#define FOURCC_TAG BE_FOURCC
#define RIFF_CHECK_BYTES 1024
#define RIFF_TAG FOURCC_TAG('R', 'I', 'F', 'F')
#define AVI_TAG FOURCC_TAG('A', 'V', 'I', ' ')
#define CDXA_TAG FOURCC_TAG('C', 'D', 'X', 'A')
#define MPEG_MARKER FOURCC_TAG( 0x00, 0x00, 0x01, 0xBA )
/* Xing header stuff */
#define XING_TAG FOURCC_TAG('X', 'i', 'n', 'g')
#define XING_FRAMES_FLAG 0x0001
#define XING_BYTES_FLAG 0x0002
#define XING_TOC_FLAG 0x0004
#define XING_VBR_SCALE_FLAG 0x0008
#define XING_TOC_LENGTH 100
typedef struct {
/* header */
uint16_t frame_sync;
uint8_t mpeg25_bit;
uint8_t lsf_bit;
uint8_t layer;
uint8_t protection_bit;
uint8_t bitrate_idx;
uint8_t freq_idx;
uint8_t padding_bit;
uint8_t private_bit;
uint8_t channel_mode;
uint8_t mode_extension;
uint8_t copyright;
uint8_t original;
uint8_t emphasis;
uint8_t version_idx;
int bitrate;
int samplerate;
int length; /* in bytes */
double duration; /* in 1/90000 s */
} mpg_audio_frame_t;
typedef struct {
demux_plugin_t demux_plugin;
xine_stream_t *stream;
fifo_buffer_t *audio_fifo;
input_plugin_t *input;
int status;
int stream_length;
int br; /* bitrate */
int abr; /* average bitrate */
int64_t last_pts;
int send_newpts;
int buf_flag_seek;
uint32_t blocksize;
mpg_audio_frame_t cur_frame;
double cur_fpts;
int is_vbr;
int meta_info_flag;
/* Xing header */
int check_xing;
uint32_t xflags;
uint32_t xframes;
uint32_t xbytes;
uint8_t xtoc[100];
uint32_t xvbr_scale;
} demux_mpgaudio_t ;
typedef struct {
demux_class_t demux_class;
/* class-wide, global variables here */
xine_t *xine;
} demux_mpgaudio_class_t;
/* bitrate table tabsel_123[mpeg version][layer][bitrate index]
* values stored in kbps
*/
const int tabsel_123[2][3][16] = {
{ {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,},
{0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,},
{0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} },
{ {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,},
{0, 8,16,24,32,40,48, 56, 64, 80, 96,112,128,144,160,},
{0, 8,16,24,32,40,48, 56, 64, 80, 96,112,128,144,160,} }
};
static int frequencies[3][3] = {
{ 44100, 48000, 32000 },
{ 22050, 24000, 16000 },
{ 11025, 12000, 8000 }
};
static int mpg123_xhead_check(char *buf)
{
return (BE_32(buf) == XING_TAG);
}
static void check_newpts (demux_mpgaudio_t *this, int64_t pts) {
int64_t diff;
diff = pts - this->last_pts;
if( pts &&
(this->send_newpts || (this->last_pts && abs(diff)>WRAP_THRESHOLD) ) ) {
if (this->buf_flag_seek) {
_x_demux_control_newpts(this->stream, pts, BUF_FLAG_SEEK);
this->buf_flag_seek = 0;
} else {
_x_demux_control_newpts(this->stream, pts, 0);
}
this->send_newpts = 0;
}
if( pts )
this->last_pts = pts;
}
static int mpg123_parse_frame_header(mpg_audio_frame_t *frame, uint8_t *buf) {
uint32_t head;
head = BE_32(buf);
lprintf("header: %08X\n", head);
frame->frame_sync = head >> 21;
if (frame->frame_sync != 0x7ff) {
lprintf("invalid frame sync\n");
return 0;
}
frame->mpeg25_bit = (head >> 20) & 0x1;
frame->lsf_bit = (head >> 19) & 0x1;
if (!frame->mpeg25_bit) {
if (frame->lsf_bit) {
lprintf("reserved mpeg25 lsf combination\n");
return 0;
} else
frame->version_idx = 2; /* MPEG Version 2.5 */
} else {
if (!frame->lsf_bit)
frame->version_idx = 1; /* MPEG Version 2 */
else
frame->version_idx = 0; /* MPEG Version 1 */
}
frame->layer = 4 - ((head >> 17) & 0x3);
if (frame->layer == 4) {
lprintf("reserved layer\n");
return 0;
}
frame->protection_bit = (head >> 16) & 0x1;
frame->bitrate_idx = (head >> 12) & 0xf;
if ((frame->bitrate_idx == 0) || (frame->bitrate_idx == 15)) {
lprintf("invalid bitrate index\n");
return 0;
}
frame->freq_idx = (head >> 10) & 0x3;
if (frame->freq_idx == 3) {
lprintf("invalid frequence index\n");
return 0;
}
frame->padding_bit = (head >> 9) & 0x1;
frame->private_bit = (head >> 8) & 0x1;
frame->channel_mode = (head >> 6) & 0x3;
frame->mode_extension = (head >> 4) & 0x3;
frame->copyright = (head >> 3) & 0x1;
frame->original = (head >> 2) & 0x1;
frame->emphasis = head & 0x3;
#if defined(OPT_STRICT)
/*
* ISO/IEC 11172-3 says this is a reserved emphasis value, but
* streams exist which use it anyway. Since the value is not important
* to the decoder proper, we allow it unless OPT_STRICT is defined.
*/
if (frame->emphasis == 2) {
lprintf("reserved emphasis\n");
return 0;
}
#endif
frame->bitrate = tabsel_123[!frame->lsf_bit][frame->layer - 1][frame->bitrate_idx] * 1000;
frame->samplerate = frequencies[frame->version_idx][frame->freq_idx];
if (frame->layer == 1) {
frame->length = (12 * frame->bitrate / frame->samplerate + frame->padding_bit) * 4;
frame->duration = 90000.0 * 384.0 / (double)frame->samplerate;
} else {
int slots_per_frame;
slots_per_frame = (frame->layer == 3 && !frame->lsf_bit) ? 72 : 144;
frame->length = slots_per_frame * frame->bitrate / frame->samplerate +
frame->padding_bit;
frame->duration = 90000.0 * slots_per_frame * 8.0 / (double)frame->samplerate;
}
lprintf("mpeg %d, layer %d\n", frame->version_idx + 1, frame->layer);
lprintf("bitrate: %d bps, samplerate: %d Hz\n", frame->bitrate, frame->samplerate);
lprintf("length: %d bytes, %f pts\n", frame->length, frame->duration);
return 1;
}
static int mpg123_parse_xing_header(demux_mpgaudio_t *this, uint8_t *buf, int bufsize) {
int i;
uint8_t *ptr = buf;
double frame_duration;
/* offset of the Xing header */
if (this->cur_frame.lsf_bit) {
if( this->cur_frame.channel_mode != 3 )
ptr += (32 + 4);
else
ptr += (17 + 4);
} else {
if( this->cur_frame.channel_mode != 3 )
ptr += (17 + 4);
else
ptr += (9 + 4);
}
if (ptr >= (buf + bufsize - 4)) return 0;
lprintf("checking %08X\n", *ptr);
if (mpg123_xhead_check(ptr)) {
lprintf("Xing header found\n");
ptr += 4;
if (ptr >= (buf + bufsize - 4)) return 0;
this->xflags = BE_32(ptr); ptr += 4;
if (this->xflags & XING_FRAMES_FLAG) {
if (ptr >= (buf + bufsize - 4)) return 0;
this->xframes = BE_32(ptr); ptr += 4;
lprintf("xframes: %d\n", this->xframes);
}
if (this->xflags & XING_BYTES_FLAG) {
if (ptr >= (buf + bufsize - 4)) return 0;
this->xbytes = BE_32(ptr); ptr += 4;
lprintf("xbytes: %d\n", this->xbytes);
}
if (this->xflags & XING_TOC_FLAG) {
lprintf("toc found\n");
if (ptr >= (buf + bufsize - XING_TOC_LENGTH)) return 0;
for (i = 0; i < XING_TOC_LENGTH; i++) {
this->xtoc[i] = *(ptr + i);
#ifdef LOG
printf("%d ", this->xtoc[i]);
#endif
}
#ifdef LOG
printf("\n");
#endif
ptr += XING_TOC_LENGTH;
}
this->xvbr_scale = -1;
if (this->xflags & XING_VBR_SCALE_FLAG) {
if (ptr >= (buf + bufsize - 4)) return 0;
this->xvbr_scale = BE_32(ptr);
lprintf("xvbr_scale: %d\n", this->xvbr_scale);
}
/* 1 kbit = 1000 bits ! (and not 1024 bits) */
if (this->xflags & (XING_FRAMES_FLAG | XING_BYTES_FLAG)) {
if (this->cur_frame.layer == 1) {
frame_duration = 384.0 / (double)this->cur_frame.samplerate;
} else {
int slots_per_frame;
slots_per_frame = (this->cur_frame.layer == 3 &&
!this->cur_frame.lsf_bit) ? 72 : 144;
frame_duration = slots_per_frame * 8.0 / (double)this->cur_frame.samplerate;
}
this->abr = ((double)this->xbytes * 8.0) / ((double)this->xframes * frame_duration);
this->stream_length = (double)this->xframes * frame_duration;
this->is_vbr = 1;
lprintf("abr: %d bps\n", this->abr);
lprintf("stream_length: %d s, %d min %d s\n", this->stream_length,
this->stream_length / 60, this->stream_length % 60);
} else {
/* it's a stupid Xing header */
this->is_vbr = 0;
}
return 1;
} else {
lprintf("Xing header not found\n");
return 0;
}
}
static int mpg123_parse_frame_payload(demux_mpgaudio_t *this,
uint8_t *frame_header,
int decoder_flags) {
buf_element_t *buf;
off_t frame_pos, len;
uint64_t pts = 0;
frame_pos = this->input->get_current_pos(this->input) - 4;
lprintf("frame_pos = %lld\n", frame_pos);
buf = this->audio_fifo->buffer_pool_alloc(this->audio_fifo);
if (this->cur_frame.length > buf->max_size) {
xprintf(this->stream->xine, XINE_VERBOSITY_LOG,
"demux_mpgaudio: frame size is greater than fifo buffer size\n");
buf->free_buffer(buf);
return 0;
}
/* the decoder needs the frame header */
memcpy(buf->mem, frame_header, 4);
len = this->input->read(this->input, buf->mem + 4, this->cur_frame.length - 4);
if (len != (this->cur_frame.length - 4)) {
buf->free_buffer(buf);
return 0;
}
/*
* compute stream length (in s)
* use the Xing header if there is one (VBR) otherwise use CBR formula
* set stream_info before sending any buffer to the decoder
*/
if (this->check_xing) {
mpg123_parse_xing_header(this, buf->mem, len + 4);
if (!this->is_vbr) {
this->stream_length = this->input->get_length(this->input) / (this->br / 8);
_x_stream_info_set(this->stream, XINE_STREAM_INFO_BITRATE, this->br);
_x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_BITRATE, this->br);
} else {
_x_stream_info_set(this->stream, XINE_STREAM_INFO_BITRATE, this->abr);
_x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_BITRATE, this->abr);
}
this->check_xing = 0;
}
/* set codec infos here
* the decoder doesn't know if the stream is VBR
*/
if (!this->meta_info_flag) {
char scratch_buf[256];
char *mpeg_ver[3] = {"1", "2", "2.5"};
snprintf(scratch_buf, 256, "MPEG %s Layer %1d%s",
mpeg_ver[this->cur_frame.version_idx], this->cur_frame.layer,
(this->is_vbr)? " VBR" : " CBR" );
_x_meta_info_set_utf8(this->stream, XINE_META_INFO_AUDIOCODEC, scratch_buf);
this->meta_info_flag = 1;
}
this->cur_fpts += this->cur_frame.duration;
pts = (int64_t)this->cur_fpts;
check_newpts(this, pts);
if( this->input->get_length (this->input) )
buf->extra_info->input_normpos = (int)( (double) frame_pos *
65535 / this->input->get_length (this->input) );
buf->extra_info->input_time = pts / 90;
buf->pts = pts;
buf->size = len + 4;
buf->content = buf->mem;
buf->type = BUF_AUDIO_MPEG;
buf->decoder_info[0] = 1;
buf->decoder_flags = decoder_flags;
this->audio_fifo->put(this->audio_fifo, buf);
return 1;
}
static unsigned char * demux_mpgaudio_read_buffer_header (input_plugin_t *input)
{
int count;
uint8_t buf[MAX_PREVIEW_SIZE];
unsigned char *retval;
if(!input)
return 0;
if((input->get_capabilities(input) & INPUT_CAP_SEEKABLE) != 0) {
input->seek(input, 0, SEEK_SET);
count = input->read(input, buf, SNIFF_BUFFER_LENGTH);
if (count < SNIFF_BUFFER_LENGTH)
{
return NULL;
}
} else if ((input->get_capabilities(input) & INPUT_CAP_PREVIEW) != 0) {
input->get_optional_data (input, buf, INPUT_OPTIONAL_DATA_PREVIEW);
} else {
return NULL;
}
retval = xine_xmalloc (SNIFF_BUFFER_LENGTH);
memcpy (retval, buf, SNIFF_BUFFER_LENGTH);
return retval;
}
/* Scan through the first SNIFF_BUFFER_LENGTH bytes of the
* buffer to find a potential 32-bit MP3 frame header. */
static int sniff_buffer_looks_like_mp3 (input_plugin_t *input)
{
int offset;
unsigned char *buf;
mpg_audio_frame_t frame;
buf = demux_mpgaudio_read_buffer_header (input);
if (buf == NULL)
return 0;
for (offset = 0; offset + 4 < SNIFF_BUFFER_LENGTH; offset++) {
if (mpg123_parse_frame_header(&frame, buf + offset)) {
size_t length = frame.length;
/* Since one frame is available, is there another frame
* just to be sure this is more likely to be a real MP3
* buffer? */
offset += length;
if (offset + 4 > SNIFF_BUFFER_LENGTH)
{
free (buf);
return 0;
}
if (mpg123_parse_frame_header(&frame, buf + offset)) {
free (buf);
lprintf("mpeg audio frame detected\n");
return 1;
}
break;
}
}
free (buf);
return 0;
}
static int mpg123_read_frame_header(demux_mpgaudio_t *this, uint8_t *header_buf, int bytes) {
off_t len;
int i;
for (i = 0; i < (4 - bytes); i++) {
header_buf[i] = header_buf[i + bytes];
}
len = this->input->read(this->input, header_buf + 4 - bytes, bytes);
if (len != ((off_t) bytes)) {
return 0;
}
return 1;
}
static int demux_mpgaudio_next (demux_mpgaudio_t *this, int decoder_flags) {
uint8_t header_buf[4];
int bytes = 4;
if (!this->audio_fifo)
return 0;
for (;;) {
if (mpg123_read_frame_header(this, header_buf, bytes)) {
if (mpg123_parse_frame_header(&this->cur_frame, header_buf)) {
if (!this->br) {
this->br = this->cur_frame.bitrate;
}
return mpg123_parse_frame_payload(this, header_buf, decoder_flags);
} else if ((BE_32(header_buf)) == ID3V22_TAG) {
xprintf(this->stream->xine, XINE_VERBOSITY_LOG,
"demux_mpgaudio: ID3V2.2 tag\n");
if (!id3v22_parse_tag(this->input, this->stream, header_buf)) {
xprintf(this->stream->xine, XINE_VERBOSITY_LOG,
"demux_mpgaudio: ID3V2.2 tag parsing error\n");
bytes = 1; /* resync */
} else {
bytes = 4;
}
} else if ((BE_32(header_buf)) == ID3V23_TAG) {
xprintf(this->stream->xine, XINE_VERBOSITY_LOG,
"demux_mpgaudio: ID3V2.3 tag\n");
if (!id3v23_parse_tag(this->input, this->stream, header_buf)) {
xprintf(this->stream->xine, XINE_VERBOSITY_LOG,
"demux_mpgaudio: ID3V2.3 tag parsing error\n");
bytes = 1; /* resync */
} else {
bytes = 4;
}
} else if ((BE_32(header_buf)) == ID3V24_TAG) {
xprintf(this->stream->xine, XINE_VERBOSITY_LOG,
"demux_mpgaudio: ID3V2.4 tag\n");
/* TODO: add parsing here */
bytes = 1; /* resync */
} else {
/* skip */
bytes = 1;
}
} else {
lprintf("read error\n");
return 0;
}
}
}
static int demux_mpgaudio_send_chunk (demux_plugin_t *this_gen) {
demux_mpgaudio_t *this = (demux_mpgaudio_t *) this_gen;
if (!demux_mpgaudio_next (this, 0))
this->status = DEMUX_FINISHED;
return this->status;
}
static int demux_mpgaudio_get_status (demux_plugin_t *this_gen) {
demux_mpgaudio_t *this = (demux_mpgaudio_t *) this_gen;
return this->status;
}
static int demux_mpgaudio_read_head(input_plugin_t *input, uint8_t *buf) {
int bs = 0;
int i, optional;
if(!input)
return 0;
if((input->get_capabilities(input) & INPUT_CAP_SEEKABLE) != 0) {
input->seek(input, 0, SEEK_SET);
if (input->get_capabilities (input) & INPUT_CAP_BLOCK)
bs = input->get_blocksize(input);
if(!bs)
bs = MAX_PREVIEW_SIZE;
input->read(input, buf, bs);
lprintf("stream is seekable\n");
} else if ((input->get_capabilities(input) & INPUT_CAP_PREVIEW) != 0) {
lprintf("input plugin provides preview\n");
optional = input->get_optional_data (input, buf, INPUT_OPTIONAL_DATA_PREVIEW);
optional = optional > 256 ? 256 : optional;
lprintf("got preview %02x %02x %02x %02x\n",
buf[0], buf[1], buf[2], buf[3]);
for(i = 0; i < (optional - 4); i++) {
if (BE_32(buf + i) == RIFF_TAG)
return 1;
}
} else {
lprintf("not seekable, no preview\n");
return 0;
}
return 1;
}
static void demux_mpgaudio_send_headers (demux_plugin_t *this_gen) {
demux_mpgaudio_t *this = (demux_mpgaudio_t *) this_gen;
int i;
this->stream_length = 0;
this->last_pts = 0;
this->status = DEMUX_OK;
this->check_xing = 1;
_x_stream_info_set(this->stream, XINE_STREAM_INFO_HAS_VIDEO, 0);
_x_stream_info_set(this->stream, XINE_STREAM_INFO_HAS_AUDIO, 1);
/* read id3 info only from inputs with seeking and without "live" flag */
if (this->input->get_capabilities(this->input) & INPUT_CAP_SEEKABLE) {
off_t pos;
/* check ID3 v1 at the end of the stream */
pos = this->input->get_length(this->input) - 128;
if(pos > 0) {
if (pos == this->input->seek (this->input, pos, SEEK_SET))
id3v1_parse_tag (this->input, this->stream);
}
}
/*
* send preview buffers
*/
_x_demux_control_start (this->stream);
if ((this->input->get_capabilities(this->input) & INPUT_CAP_SEEKABLE) != 0)
this->input->seek (this->input, 0, SEEK_SET);
for (i = 0; i < NUM_PREVIEW_BUFFERS; i++) {
if (!demux_mpgaudio_next (this, BUF_FLAG_PREVIEW)) {
break;
}
}
if (this->cur_frame.samplerate) {
if (this->cur_frame.layer == 1)
_x_stream_info_set(this->stream, XINE_STREAM_INFO_FRAME_DURATION,
384000 / this->cur_frame.samplerate);
else
_x_stream_info_set(this->stream, XINE_STREAM_INFO_FRAME_DURATION,
1152000 / this->cur_frame.samplerate);
}
this->status = DEMUX_OK;
}
/* interpolate in Xing TOC to get file seek point in bytes */
static off_t xing_get_seek_point(demux_mpgaudio_t *this, int time)
{
off_t seekpoint;
int a;
float fa, fb, fx;
float percent;
percent = ((float)time / 10.0f)/ (float)this->stream_length;
if (percent < 0.0f) percent = 0.0f;
if (percent > 100.0f) percent = 100.0f;
a = (int)percent;
if (a > 99) a = 99;
fa = this->xtoc[a];
if (a < 99) {
fb = this->xtoc[a + 1];
} else {
fb = 256.0f;
}
fx = fa + (fb - fa) * (percent - a);
seekpoint = (off_t)((1.0f / 256.0f) * fx * this->xbytes);
return seekpoint;
}
/* interpolate in Xing TOC to get file seek point in ms */
static int xing_get_seek_time(demux_mpgaudio_t *this, off_t pos)
{
int seektime;
int a, b;
float fb, fx;
float percent;
fx = 256.0f * (float)pos / (float)this->xbytes;
if (fx < 0.0f) fx = 0.0f;
if (fx > 256.0f) fx = 256.0f;
for (b = 0; b < 100; b++) {
fb = this->xtoc[b];
if (fb > fx)
break;
}
if (b > 0) {
a = b - 1;
} else {
a = 0;
}
percent = a + (fx - this->xtoc[a]);
seektime = 10.0f * percent * this->stream_length;
return seektime;
}
static int demux_mpgaudio_seek (demux_plugin_t *this_gen,
off_t start_pos, int start_time, int playing) {
demux_mpgaudio_t *this = (demux_mpgaudio_t *) this_gen;
start_pos = (off_t) ( (double) start_pos / 65535 *
this->input->get_length (this->input) );
if ((this->input->get_capabilities(this->input) & INPUT_CAP_SEEKABLE) != 0) {
if (!start_pos && start_time && this->stream_length > 0) {
if (this->is_vbr && (this->xflags & (XING_TOC_FLAG | XING_BYTES_FLAG))) {
/* vbr */
start_pos = xing_get_seek_point(this, start_time);
lprintf("time seek: vbr: time=%d, pos=%lld\n", start_time, start_pos);
} else {
/* cbr */
off_t input_length = this->input->get_length(this->input);
if ((input_length > 0) && (this->stream_length > 0)) {
start_pos = start_time * input_length / (1000 * this->stream_length);
lprintf("time seek: cbr: time=%d, pos=%lld\n", start_time, start_pos);
}
}
} else {
if (this->is_vbr && (this->xflags & (XING_TOC_FLAG | XING_BYTES_FLAG))) {
/* vbr */
start_time = xing_get_seek_time(this, start_pos);
lprintf("pos seek: vbr: time=%d, pos=%lld\n", start_time, start_pos);
} else {
/* cbr */
off_t input_length = this->input->get_length(this->input);
if((input_length > 0) && (this->stream_length > 0)) {
start_time = (1000 * start_pos * this->stream_length) / input_length;
lprintf("pos seek: cbr\n");
}
}
}
this->cur_fpts = 90.0 * (double)start_time;
this->input->seek (this->input, start_pos, SEEK_SET);
}
this->status = DEMUX_OK;
this->send_newpts = 1;
if( !playing ) {
this->buf_flag_seek = 0;
} else {
this->buf_flag_seek = 1;
_x_demux_flush_engine(this->stream);
}
return this->status;
}
static void demux_mpgaudio_dispose (demux_plugin_t *this) {
free (this);
}
static int demux_mpgaudio_get_stream_length (demux_plugin_t *this_gen) {
demux_mpgaudio_t *this = (demux_mpgaudio_t *) this_gen;
if (this->stream_length > 0) {
return this->stream_length * 1000;
} else
return 0;
}
static uint32_t demux_mpgaudio_get_capabilities(demux_plugin_t *this_gen) {
return DEMUX_CAP_NOCAP;
}
static int demux_mpgaudio_get_optional_data(demux_plugin_t *this_gen,
void *data, int data_type) {
return DEMUX_OPTIONAL_UNSUPPORTED;
}
static demux_plugin_t *open_plugin (demux_class_t *class_gen, xine_stream_t *stream,
input_plugin_t *input) {
demux_mpgaudio_t *this;
id3v2_header_t tag_header;
mpg_audio_frame_t frame;
uint8_t buf[MAX_PREVIEW_SIZE];
uint8_t *riff_check;
int i;
uint8_t *ptr;
lprintf("trying to open %s...\n", input->get_mrl(input));
switch (stream->content_detection_method) {
case METHOD_BY_CONTENT: {
uint32_t head;
if (!demux_mpgaudio_read_head(input, buf))
return NULL;
head = BE_32(buf);
lprintf("head is %8X\n", head);
if (head == RIFF_TAG) {
int ok;
lprintf("found RIFF tag\n");
/* skip the length */
ptr = buf + 8;
riff_check = ptr; ptr += 4;
if ((buf + MAX_PREVIEW_SIZE) < ptr)
return NULL;
/* disqualify the file if it is, in fact, an AVI file or has a CDXA
* marker */
if ((BE_32(riff_check) == AVI_TAG) ||
(BE_32(riff_check) == CDXA_TAG)) {
lprintf("found AVI or CDXA tag\n");
return NULL;
}
/* skip 4 more bytes */
ptr += 4;
/* get the length of the next chunk */
riff_check = ptr; ptr += 4;
if ((buf + MAX_PREVIEW_SIZE) < ptr)
return NULL;
/* head gets to be a generic variable in this case */
head = LE_32(riff_check);
/* skip over the chunk and the 'data' tag and length */
ptr += 8;
/* load the next, I don't know...n bytes, and check for a valid
* MPEG audio header */
riff_check = ptr; ptr += RIFF_CHECK_BYTES;
if ((buf + MAX_PREVIEW_SIZE) < ptr)
return NULL;
ok = 0;
for (i = 0; i < RIFF_CHECK_BYTES - 4; i++) {
head = BE_32(riff_check + i);
lprintf("checking %08X\n", head);
if (sniff_buffer_looks_like_mp3(input))
ok = 1;
}
if (!ok)
return NULL;
} else if ((head == ID3V22_TAG) ||
(head == ID3V23_TAG) ||
(head == ID3V24_TAG)) {
/* check if a mp3 frame follows the tag
* id3v2 are not specific to mp3 files,
* flac files can contain id3v2 tags
*/
ptr = buf + 4;
tag_header.size = (ptr[2] << 21) + (ptr[3] << 14) + (ptr[4] << 7) + ptr[5];
lprintf("id3v2.%d tag detected, size: %d\n", buf[3], tag_header.size);
ptr += tag_header.size + 6;
if ((ptr + 4) <= (buf + MAX_PREVIEW_SIZE)) {
if (!mpg123_parse_frame_header(&frame, ptr)) {
lprintf ("invalid mp3 frame header\n");
return NULL;
}
lprintf ("a valid mp3 frame follows the id3 tag\n");
} else {
lprintf ("the id3v2 tag is too long\n");
return NULL;
}
} else if (head == MPEG_MARKER) {
lprintf ("discard mpeg video\n");
return NULL;
} else if (!sniff_buffer_looks_like_mp3 (input)) {
lprintf ("sniff_buffer_looks_like_mp3 failed\n");
return NULL;
}
}
break;
case METHOD_BY_EXTENSION: {
char *mrl = input->get_mrl(input);
char *extensions = class_gen->get_extensions (class_gen);
lprintf ("stage by extension %s\n", mrl);
if (!_x_demux_check_extension (mrl, extensions))
return NULL;
}
break;
case METHOD_EXPLICIT:
break;
default:
return NULL;
}
this = xine_xmalloc (sizeof (demux_mpgaudio_t));
this->demux_plugin.send_headers = demux_mpgaudio_send_headers;
this->demux_plugin.send_chunk = demux_mpgaudio_send_chunk;
this->demux_plugin.seek = demux_mpgaudio_seek;
this->demux_plugin.dispose = demux_mpgaudio_dispose;
this->demux_plugin.get_status = demux_mpgaudio_get_status;
this->demux_plugin.get_stream_length = demux_mpgaudio_get_stream_length;
this->demux_plugin.get_capabilities = demux_mpgaudio_get_capabilities;
this->demux_plugin.get_optional_data = demux_mpgaudio_get_optional_data;
this->demux_plugin.demux_class = class_gen;
this->input = input;
this->audio_fifo = stream->audio_fifo;
this->status = DEMUX_FINISHED;
this->stream = stream;
return &this->demux_plugin;
}
/*
* demux mpegaudio class
*/
static char *get_description (demux_class_t *this_gen) {
return "MPEG audio demux plugin";
}
static char *get_identifier (demux_class_t *this_gen) {
return "MPEGAUDIO";
}
static char *get_extensions (demux_class_t *this_gen) {
return "mp3 mp2 mpa mpega";
}
static char *get_mimetypes (demux_class_t *this_gen) {
return "audio/mpeg2: mp2: MPEG audio;"
"audio/x-mpeg2: mp2: MPEG audio;"
"audio/mpeg3: mp3: MPEG audio;"
"audio/x-mpeg3: mp3: MPEG audio;"
"audio/mpeg: mpa,abs,mpega: MPEG audio;"
"audio/x-mpeg: mpa,abs,mpega: MPEG audio;"
"x-mpegurl: mp3: MPEG audio;"
"audio/mpegurl: mp3: MPEG audio;"
"audio/mp3: mp3: MPEG audio;"
"audio/x-mp3: mp3: MPEG audio;";
}
static void class_dispose (demux_class_t *this_gen) {
demux_mpgaudio_class_t *this = (demux_mpgaudio_class_t *) this_gen;
free (this);
}
void *demux_mpgaudio_init_class (xine_t *xine, void *data) {
demux_mpgaudio_class_t *this;
this = xine_xmalloc (sizeof (demux_mpgaudio_class_t));
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;
}
|