forked from videolan/vlc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathogg.c
3343 lines (2961 loc) · 125 KB
/
ogg.c
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
/*****************************************************************************
* ogg.c : ogg stream demux module for vlc
*****************************************************************************
* Copyright (C) 2001-2007 VLC authors and VideoLAN
* $Id$
*
* Authors: Gildas Bazin <[email protected]>
* Andre Pang <[email protected]> (Annodex support)
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser 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.
*****************************************************************************/
/*****************************************************************************
* Preamble
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_access.h>
#include <vlc_demux.h>
#include <vlc_meta.h>
#include <vlc_input.h>
#include <ogg/ogg.h>
#include <vlc_codecs.h>
#include <vlc_bits.h>
#include "xiph.h"
#include "xiph_metadata.h"
#include "ogg.h"
#include "oggseek.h"
#include "opus.h"
/*****************************************************************************
* Module descriptor
*****************************************************************************/
static int Open ( vlc_object_t * );
static void Close( vlc_object_t * );
vlc_module_begin ()
set_shortname ( "OGG" )
set_description( N_("OGG demuxer" ) )
set_category( CAT_INPUT )
set_subcategory( SUBCAT_INPUT_DEMUX )
set_capability( "demux", 50 )
set_callbacks( Open, Close )
add_shortcut( "ogg" )
vlc_module_end ()
/*****************************************************************************
* Definitions of structures and functions used by this plugins
*****************************************************************************/
/* OggDS headers for the new header format (used in ogm files) */
typedef struct
{
ogg_int32_t width;
ogg_int32_t height;
} stream_header_video_t;
typedef struct
{
ogg_int16_t channels;
ogg_int16_t padding;
ogg_int16_t blockalign;
ogg_int32_t avgbytespersec;
} stream_header_audio_t;
typedef struct
{
char streamtype[8];
char subtype[4];
ogg_int32_t size; /* size of the structure */
ogg_int64_t time_unit; /* in reference time */
ogg_int64_t samples_per_unit;
ogg_int32_t default_len; /* in media time */
ogg_int32_t buffersize;
ogg_int16_t bits_per_sample;
ogg_int16_t padding;
union
{
/* Video specific */
stream_header_video_t video;
/* Audio specific */
stream_header_audio_t audio;
} sh;
} stream_header_t;
#define VORBIS_HEADER_IDENTIFICATION 1
#define VORBIS_HEADER_COMMENT 2
#define VORBIS_HEADER_SETUP 3
#define VORBIS_HEADER_TO_FLAG(i) (1 << (i - 1))
#define VORBIS_HEADERS_VALID(p_stream) \
((p_stream->special.vorbis.i_headers_flags & 0x07) == 0x07) // 0b111
/*****************************************************************************
* Local prototypes
*****************************************************************************/
static int Demux ( demux_t * );
static int Control( demux_t *, int, va_list );
/* Bitstream manipulation */
static int Ogg_ReadPage ( demux_t *, ogg_page * );
static void Ogg_UpdatePCR ( demux_t *, logical_stream_t *, ogg_packet * );
static void Ogg_DecodePacket ( demux_t *, logical_stream_t *, ogg_packet * );
static unsigned Ogg_OpusPacketDuration( ogg_packet * );
static void Ogg_SendOrQueueBlocks( demux_t *, logical_stream_t *, block_t * );
static void Ogg_CreateES( demux_t *p_demux );
static int Ogg_BeginningOfStream( demux_t *p_demux );
static int Ogg_FindLogicalStreams( demux_t *p_demux );
static void Ogg_EndOfStream( demux_t *p_demux );
/* */
static void Ogg_LogicalStreamDelete( demux_t *p_demux, logical_stream_t *p_stream );
static bool Ogg_LogicalStreamResetEsFormat( demux_t *p_demux, logical_stream_t *p_stream );
static void Ogg_ResetStream( logical_stream_t *p_stream );
/* */
static void Ogg_ExtractMeta( demux_t *p_demux, es_format_t *p_fmt, const uint8_t *p_headers, int i_headers );
/* Logical bitstream headers */
static bool Ogg_ReadDaalaHeader( logical_stream_t *, ogg_packet * );
static bool Ogg_ReadTheoraHeader( logical_stream_t *, ogg_packet * );
static bool Ogg_ReadVorbisHeader( logical_stream_t *, ogg_packet * );
static bool Ogg_ReadSpeexHeader( logical_stream_t *, ogg_packet * );
static void Ogg_ReadOpusHeader( logical_stream_t *, ogg_packet * );
static bool Ogg_ReadKateHeader( logical_stream_t *, ogg_packet * );
static bool Ogg_ReadFlacHeader( demux_t *, logical_stream_t *, ogg_packet * );
static void Ogg_ReadAnnodexHeader( demux_t *, logical_stream_t *, ogg_packet * );
static bool Ogg_ReadDiracHeader( logical_stream_t *, ogg_packet * );
static bool Ogg_ReadVP8Header( demux_t *, logical_stream_t *, ogg_packet * );
static void Ogg_ReadSkeletonHeader( demux_t *, logical_stream_t *, ogg_packet * );
/* Skeleton */
static void Ogg_ReadSkeletonBones( demux_t *, ogg_packet * );
static void Ogg_ReadSkeletonIndex( demux_t *, ogg_packet * );
static void Ogg_FreeSkeleton( ogg_skeleton_t * );
static void Ogg_ApplySkeleton( logical_stream_t * );
/* Special decoding */
static void Ogg_CleanSpecificData( logical_stream_t * );
#ifdef HAVE_LIBVORBIS
static void Ogg_DecodeVorbisHeader( logical_stream_t *, ogg_packet *, int );
#endif
static void fill_channels_info(audio_format_t *audio)
{
static const int pi_channels_map[9] =
{
0,
AOUT_CHAN_CENTER,
AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
| AOUT_CHAN_REARRIGHT,
AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
| AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
| AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
| AOUT_CHAN_REARCENTER | AOUT_CHAN_MIDDLELEFT
| AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE,
AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
| AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
| AOUT_CHAN_LFE,
};
unsigned chans = audio->i_channels;
if (chans < sizeof(pi_channels_map) / sizeof(pi_channels_map[0]))
audio->i_physical_channels =
audio->i_original_channels = pi_channels_map[chans];
}
/* Special TS value: don't send or derive any pts/pcr from it.
Represents TS state prior first known valid timestamp */
#define VLC_TS_UNKNOWN (VLC_TS_INVALID - 1)
/*****************************************************************************
* Open: initializes ogg demux structures
*****************************************************************************/
static int Open( vlc_object_t * p_this )
{
demux_t *p_demux = (demux_t *)p_this;
demux_sys_t *p_sys;
const uint8_t *p_peek;
/* Check if we are dealing with an ogg stream */
if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
if( !p_demux->b_force && memcmp( p_peek, "OggS", 4 ) )
{
char *psz_mime = stream_ContentType( p_demux->s );
if( !psz_mime )
{
return VLC_EGENERIC;
}
else if ( strcmp( psz_mime, "application/ogg" ) &&
strcmp( psz_mime, "video/ogg" ) &&
strcmp( psz_mime, "audio/ogg" ) )
{
free( psz_mime );
return VLC_EGENERIC;
}
free( psz_mime );
}
/* */
p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
if( !p_sys )
return VLC_ENOMEM;
p_sys->i_length = -1;
p_sys->b_preparsing_done = false;
stream_Control( p_demux->s, ACCESS_GET_PTS_DELAY, & p_sys->i_access_delay );
/* Set exported functions */
p_demux->pf_demux = Demux;
p_demux->pf_control = Control;
/* Initialize the Ogg physical bitstream parser */
ogg_sync_init( &p_sys->oy );
/* */
TAB_INIT( p_sys->i_seekpoints, p_sys->pp_seekpoints );
while ( !p_sys->b_preparsing_done && p_demux->pf_demux( p_demux ) > 0 )
{}
return VLC_SUCCESS;
}
/*****************************************************************************
* Close: frees unused data
*****************************************************************************/
static void Close( vlc_object_t *p_this )
{
demux_t *p_demux = (demux_t *)p_this;
demux_sys_t *p_sys = p_demux->p_sys ;
/* Cleanup the bitstream parser */
ogg_sync_clear( &p_sys->oy );
Ogg_EndOfStream( p_demux );
if( p_sys->p_old_stream )
Ogg_LogicalStreamDelete( p_demux, p_sys->p_old_stream );
free( p_sys );
}
/*****************************************************************************
* Demux: reads and demuxes data packets
*****************************************************************************
* Returns -1 in case of error, 0 in case of EOF, 1 otherwise
*****************************************************************************/
static int Demux( demux_t * p_demux )
{
demux_sys_t *p_sys = p_demux->p_sys;
ogg_packet oggpacket;
int i_stream;
bool b_skipping = false;
bool b_canseek;
int i_active_streams = p_sys->i_streams;
for ( int i=0; i < p_sys->i_streams; i++ )
{
if ( p_sys->pp_stream[i]->b_finished )
i_active_streams--;
}
if ( i_active_streams == 0 )
{
if ( p_sys->i_streams ) /* All finished */
{
msg_Dbg( p_demux, "end of a group of logical streams" );
/* We keep the ES to try reusing it in Ogg_BeginningOfStream
* only 1 ES is supported (common case for ogg web radio) */
if( p_sys->i_streams == 1 )
{
p_sys->p_old_stream = p_sys->pp_stream[0];
TAB_CLEAN( p_sys->i_streams, p_sys->pp_stream );
}
Ogg_EndOfStream( p_demux );
p_sys->b_chained_boundary = true;
p_sys->i_nzpcr_offset = p_sys->i_nzlast_pts;
}
if( Ogg_BeginningOfStream( p_demux ) != VLC_SUCCESS )
return VLC_DEMUXER_EOF;
msg_Dbg( p_demux, "beginning of a group of logical streams" );
if ( !p_sys->b_chained_boundary )
{
/* Find the real duration */
stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_canseek );
if ( b_canseek )
Oggseek_ProbeEnd( p_demux );
}
else
{
p_sys->b_chained_boundary = false;
}
}
if ( p_sys->b_preparsing_done && !p_sys->b_es_created )
{
Ogg_CreateES( p_demux );
p_sys->b_es_created = true;
}
/*
* The first data page of a physical stream is stored in the relevant logical stream
* in Ogg_FindLogicalStreams. Therefore, we must not read a page and only update the
* stream it belongs to if we haven't processed this first page yet. If we do, we
* will only process that first page whenever we find the second page for this stream.
* While this is fine for Vorbis and Theora, which are continuous codecs, which means
* the second page will arrive real quick, this is not fine for Kate, whose second
* data page will typically arrive much later.
* This means it is now possible to seek right at the start of a stream where the last
* logical stream is Kate, without having to wait for the second data page to unblock
* the first one, which is the one that triggers the 'no more headers to backup' code.
* And, as we all know, seeking without having backed up all headers is bad, since the
* codec will fail to initialize if it's missing its headers.
*/
if( !p_sys->b_page_waiting)
{
/*
* Demux an ogg page from the stream
*/
if( Ogg_ReadPage( p_demux, &p_sys->current_page ) != VLC_SUCCESS )
return VLC_DEMUXER_EOF; /* EOF */
/* Test for End of Stream */
if( ogg_page_eos( &p_sys->current_page ) )
{
/* If we delayed restarting encoders/SET_ES_FMT for more
* skeleton provided configuration */
if ( p_sys->p_skelstream )
{
if ( p_sys->p_skelstream->i_serial_no == ogg_page_serialno(&p_sys->current_page) )
{
msg_Dbg( p_demux, "End of Skeleton" );
p_sys->b_preparsing_done = true;
for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
{
logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
Ogg_ApplySkeleton( p_stream );
}
}
}
for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
{
if ( p_sys->pp_stream[i_stream]->i_serial_no == ogg_page_serialno( &p_sys->current_page ) )
{
p_sys->pp_stream[i_stream]->b_finished = true;
break;
}
}
}
}
b_skipping = false;
for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
{
b_skipping |= p_sys->pp_stream[i_stream]->i_skip_frames;
}
for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
{
logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
/* if we've just pulled page, look for the right logical stream */
if( !p_sys->b_page_waiting )
{
if( p_sys->i_streams == 1 &&
ogg_page_serialno( &p_sys->current_page ) != p_stream->os.serialno )
{
msg_Err( p_demux, "Broken Ogg stream (serialno) mismatch" );
Ogg_ResetStream( p_stream );
p_sys->i_nzpcr_offset = p_sys->i_nzlast_pts;
ogg_stream_reset_serialno( &p_stream->os, ogg_page_serialno( &p_sys->current_page ) );
}
/* Does fail if serialno differs */
if( ogg_stream_pagein( &p_stream->os, &p_sys->current_page ) != 0 )
{
continue;
}
}
/* clear the finished flag if pages after eos (ex: after a seek) */
if ( ! ogg_page_eos( &p_sys->current_page ) && p_sys->p_skelstream != p_stream )
p_stream->b_finished = false;
DemuxDebug(
if ( p_stream->fmt.i_cat == VIDEO_ES )
msg_Dbg(p_demux, "DEMUX READ pageno %ld g%"PRId64" (%d packets) cont %d %ld bytes",
ogg_page_pageno( &p_sys->current_page ),
ogg_page_granulepos( &p_sys->current_page ),
ogg_page_packets( &p_sys->current_page ),
ogg_page_continued(&p_sys->current_page),
p_sys->current_page.body_len )
);
const int i_page_packets = ogg_page_packets( &p_sys->current_page );
bool b_doprepcr = false;
if ( p_stream->i_pcr < VLC_TS_0 && ogg_page_granulepos( &p_sys->current_page ) > 0 )
{
// PASS 0
if ( p_stream->fmt.i_codec == VLC_CODEC_OPUS ||
p_stream->fmt.i_codec == VLC_CODEC_VORBIS ||
p_stream->fmt.i_codec == VLC_CODEC_SPEEX ||
p_stream->fmt.i_cat == VIDEO_ES )
{
assert( p_stream->prepcr.pp_blocks == NULL );
b_doprepcr = true;
}
}
int i_real_page_packets = 0;
while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
{
i_real_page_packets++;
int i_max_packets = __MAX(i_page_packets, i_real_page_packets);
if ( b_doprepcr && p_stream->prepcr.i_size < i_max_packets )
{
/* always double alloc for performance */
i_max_packets = __MAX( i_max_packets << 1, 255 );
/* alloc or realloc */
block_t **pp_realloc = realloc( p_stream->prepcr.pp_blocks,
sizeof(block_t *) * i_max_packets );
if ( !pp_realloc )
{
/* drop it then */
continue;
}
p_stream->prepcr.i_size = i_max_packets;
p_stream->prepcr.pp_blocks = pp_realloc;
}
/* Read info from any secondary header packets, if there are any */
if( p_stream->i_secondary_header_packets > 0 )
{
if( p_stream->fmt.i_codec == VLC_CODEC_THEORA &&
oggpacket.bytes >= 7 &&
! memcmp( oggpacket.packet, "\x80theora", 7 ) )
{
Ogg_ReadTheoraHeader( p_stream, &oggpacket );
p_stream->i_secondary_header_packets = 0;
}
else if( p_stream->fmt.i_codec == VLC_CODEC_DAALA &&
oggpacket.bytes >= 6 &&
! memcmp( oggpacket.packet, "\x80""daala", 6 ) )
{
Ogg_ReadDaalaHeader( p_stream, &oggpacket );
p_stream->i_secondary_header_packets = 0;
}
else if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS &&
oggpacket.bytes >= 7 &&
! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
{
Ogg_ReadVorbisHeader( p_stream, &oggpacket );
p_stream->i_secondary_header_packets = 0;
}
else if( p_stream->fmt.i_codec == VLC_CODEC_CMML )
{
p_stream->i_secondary_header_packets = 0;
}
/* update start of data pointer */
p_stream->i_data_start = stream_Tell( p_demux->s );
}
/* If any streams have i_skip_frames, only decode (pre-roll)
* for those streams, but don't skip headers */
if ( b_skipping && p_stream->i_skip_frames == 0
&& p_stream->i_secondary_header_packets ) continue;
if( p_stream->b_reinit )
{
p_stream->b_reinit = false;
if( p_stream->fmt.i_codec == VLC_CODEC_OPUS )
{
p_stream->i_skip_frames = p_stream->i_pre_skip;
}
}
Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
}
if ( p_stream->prepcr.pp_blocks )
{
int64_t pagestamp = Oggseek_GranuleToAbsTimestamp( p_stream, ogg_page_granulepos( &p_sys->current_page ), false );
p_stream->i_previous_pcr = pagestamp;
#ifdef HAVE_LIBVORBIS
int i_prev_blocksize = 0;
#endif
// PASS 1
for( int i=0; i<p_stream->prepcr.i_used; i++ )
{
block_t *p_block = p_stream->prepcr.pp_blocks[i];
ogg_packet dumb_packet;
dumb_packet.bytes = p_block->i_buffer;
dumb_packet.packet = p_block->p_buffer;
switch( p_stream->fmt.i_codec )
{
case VLC_CODEC_SPEEX:
p_block->i_nb_samples = p_stream->special.speex.i_framesize *
p_stream->special.speex.i_framesperpacket;
break;
case VLC_CODEC_OPUS:
p_block->i_nb_samples = Ogg_OpusPacketDuration( &dumb_packet );
break;
#ifdef HAVE_LIBVORBIS
case VLC_CODEC_VORBIS:
{
if( !VORBIS_HEADERS_VALID(p_stream) )
{
msg_Err( p_demux, "missing vorbis headers, can't compute block size" );
break;
}
long i_blocksize = vorbis_packet_blocksize(
p_stream->special.vorbis.p_info, &dumb_packet );
if ( i_prev_blocksize )
p_block->i_nb_samples = ( i_blocksize + i_prev_blocksize ) / 4;
else
p_block->i_nb_samples = i_blocksize / 2;
i_prev_blocksize = i_blocksize;
}
#endif
}
}
// PASS 2
bool b_fixed = false;
for( int i=p_stream->prepcr.i_used - 1; i>=0; i-- )
{
block_t *p_block = p_stream->prepcr.pp_blocks[i];
switch( p_stream->fmt.i_codec )
{
case VLC_CODEC_SPEEX:
case VLC_CODEC_OPUS:
case VLC_CODEC_VORBIS:
pagestamp -= CLOCK_FREQ * p_block->i_nb_samples / p_stream->f_rate;
if ( pagestamp < 0 )
{
p_block->i_pts = VLC_TS_INVALID;
p_block->i_flags |= BLOCK_FLAG_PREROLL;
}
else
p_block->i_pts = VLC_TS_0 + p_sys->i_nzpcr_offset + pagestamp;
b_fixed = true;
break;
default:
if ( p_stream->fmt.i_cat == VIDEO_ES )
{
pagestamp = pagestamp - ( CLOCK_FREQ / p_stream->f_rate );
if( pagestamp < 0 )
pagestamp = 0;
p_block->i_pts = VLC_TS_0 + p_sys->i_nzpcr_offset + pagestamp;
b_fixed = true;
}
}
}
if ( b_fixed )
{
if ( pagestamp < 0 ) pagestamp = 0;
p_stream->i_pcr = VLC_TS_0 + pagestamp;
p_stream->i_pcr += p_sys->i_nzpcr_offset;
p_stream->i_previous_granulepos = ogg_page_granulepos( &p_sys->current_page );
}
FREENULL(p_stream->prepcr.pp_blocks);
p_stream->prepcr.i_used = 0;
Ogg_SendOrQueueBlocks( p_demux, p_stream, NULL );
}
int64_t i_pagestamp = Oggseek_GranuleToAbsTimestamp( p_stream,
ogg_page_granulepos( &p_sys->current_page ), false );
if ( i_pagestamp > -1 )
{
p_stream->i_pcr = VLC_TS_0 + i_pagestamp;
p_stream->i_pcr += p_sys->i_nzpcr_offset;
}
if( !p_sys->b_page_waiting )
break;
}
/* if a page was waiting, it's now processed */
p_sys->b_page_waiting = false;
if ( p_sys->p_skelstream && !p_sys->p_skelstream->b_finished )
p_sys->b_preparsing_done = false;
else
p_sys->b_preparsing_done = true;
/* We will consider the lowest PCR among tracks, because the audio core badly
* handles PCR rewind (mute)
*/
mtime_t i_pcr_candidate = VLC_TS_INVALID;
for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
{
logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
if ( p_sys->b_preparsing_done && p_stream->b_initializing )
{
/* We have 1 or more streams needing more than 1 page for preparsing */
p_sys->b_preparsing_done = false;
}
if( p_stream->fmt.i_cat == SPU_ES )
continue;
if( p_stream->i_pcr < VLC_TS_0 )
continue;
if ( p_stream->b_finished || p_stream->b_initializing )
continue;
if ( p_stream->p_preparse_block )
continue;
if( i_pcr_candidate < VLC_TS_0
|| p_stream->i_pcr <= i_pcr_candidate )
{
i_pcr_candidate = p_stream->i_pcr;
}
}
if ( i_pcr_candidate > VLC_TS_INVALID && p_sys->i_pcr != i_pcr_candidate )
{
if ( p_sys->i_streams == 1 && p_sys->i_access_delay )
{
int64_t i_pcr_jitter = i_pcr_candidate - p_sys->i_pcr;
if ( i_pcr_jitter > p_sys->i_pcr_jitter )
{
p_sys->i_pcr_jitter = i_pcr_jitter;
if ( p_sys->i_access_delay < i_pcr_jitter )
msg_Warn( p_demux, "Consider increasing access caching variable from %"PRId64" to >%"PRId64,
p_sys->i_access_delay / 1000, i_pcr_jitter / 1000 );
}
}
if( ! b_skipping && p_sys->b_preparsing_done )
{
p_sys->i_pcr = i_pcr_candidate;
es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
}
}
return VLC_DEMUXER_SUCCESS;
}
static void Ogg_ResetStream( logical_stream_t *p_stream )
{
#ifdef HAVE_LIBVORBIS
if ( p_stream->fmt.i_codec == VLC_CODEC_VORBIS )
{
p_stream->special.vorbis.i_prev_blocksize = 0;
}
#endif
/* we'll trash all the data until we find the next pcr */
p_stream->b_reinit = true;
p_stream->i_pcr = VLC_TS_UNKNOWN;
p_stream->i_previous_granulepos = -1;
p_stream->i_previous_pcr = VLC_TS_UNKNOWN;
ogg_stream_reset( &p_stream->os );
FREENULL( p_stream->prepcr.pp_blocks );
p_stream->prepcr.i_size = 0;
p_stream->prepcr.i_used = 0;
}
static void Ogg_ResetStreamsHelper( demux_sys_t *p_sys )
{
for( int i = 0; i < p_sys->i_streams; i++ )
Ogg_ResetStream( p_sys->pp_stream[i] );
ogg_sync_reset( &p_sys->oy );
p_sys->i_pcr = VLC_TS_UNKNOWN;
}
static logical_stream_t * Ogg_GetSelectedStream( demux_t *p_demux )
{
demux_sys_t *p_sys = p_demux->p_sys;
logical_stream_t *p_stream = NULL;
for( int i=0; i<p_sys->i_streams; i++ )
{
logical_stream_t *p_candidate = p_sys->pp_stream[i];
if ( !p_candidate->p_es ) continue;
bool b_selected = false;
es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
p_candidate->p_es, &b_selected );
if ( !b_selected ) continue;
if ( !p_stream && p_candidate->fmt.i_cat == AUDIO_ES )
{
p_stream = p_candidate;
continue; /* Try to find video anyway */
}
if ( p_candidate->fmt.i_cat == VIDEO_ES )
{
p_stream = p_candidate;
break;
}
}
return p_stream;
}
/*****************************************************************************
* Control:
*****************************************************************************/
static int Control( demux_t *p_demux, int i_query, va_list args )
{
demux_sys_t *p_sys = p_demux->p_sys;
vlc_meta_t *p_meta;
int64_t *pi64, i64;
double *pf, f;
bool *pb_bool, b;
switch( i_query )
{
case DEMUX_GET_META:
p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
if( p_sys->p_meta )
vlc_meta_Merge( p_meta, p_sys->p_meta );
return VLC_SUCCESS;
case DEMUX_HAS_UNSUPPORTED_META:
pb_bool = (bool*)va_arg( args, bool* );
*pb_bool = true;
return VLC_SUCCESS;
case DEMUX_GET_TIME:
pi64 = (int64_t*)va_arg( args, int64_t * );
*pi64 = p_sys->i_pcr;
return VLC_SUCCESS;
case DEMUX_SET_TIME:
i64 = (int64_t)va_arg( args, int64_t );
logical_stream_t *p_stream = Ogg_GetSelectedStream( p_demux );
if ( !p_stream )
{
msg_Err( p_demux, "No selected seekable stream found" );
return VLC_EGENERIC;
}
stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b );
if ( Oggseek_BlindSeektoAbsoluteTime( p_demux, p_stream, i64, b ) )
{
Ogg_ResetStreamsHelper( p_sys );
es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
VLC_TS_0 + i64 );
return VLC_SUCCESS;
}
else
return VLC_EGENERIC;
case DEMUX_GET_ATTACHMENTS:
{
input_attachment_t ***ppp_attach =
(input_attachment_t***)va_arg( args, input_attachment_t*** );
int *pi_int = (int*)va_arg( args, int * );
if( p_sys->i_attachments <= 0 )
return VLC_EGENERIC;
*pi_int = p_sys->i_attachments;
*ppp_attach = xmalloc( sizeof(input_attachment_t*) * p_sys->i_attachments );
for( int i = 0; i < p_sys->i_attachments; i++ )
(*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachments[i] );
return VLC_SUCCESS;
}
case DEMUX_GET_POSITION:
pf = (double*)va_arg( args, double * );
if( p_sys->i_length > 0 )
{
*pf = (double) p_sys->i_pcr /
(double) ( p_sys->i_length * (mtime_t)1000000 );
}
else if( stream_Size( p_demux->s ) > 0 )
{
i64 = stream_Tell( p_demux->s );
*pf = (double) i64 / stream_Size( p_demux->s );
}
else *pf = 0.0;
return VLC_SUCCESS;
case DEMUX_SET_POSITION:
/* forbid seeking if we haven't initialized all logical bitstreams yet;
if we allowed, some headers would not get backed up and decoder init
would fail, making that logical stream unusable */
for ( int i=0; i< p_sys->i_streams; i++ )
{
if ( p_sys->pp_stream[i]->b_initializing )
return VLC_EGENERIC;
}
p_stream = Ogg_GetSelectedStream( p_demux );
if ( !p_stream )
{
msg_Err( p_demux, "No selected seekable stream found" );
return VLC_EGENERIC;
}
stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b );
f = (double)va_arg( args, double );
if ( p_sys->i_length <= 0 || !b /* || ! ACCESS_CAN_FASTSEEK */ )
{
Ogg_ResetStreamsHelper( p_sys );
Oggseek_BlindSeektoPosition( p_demux, p_stream, f, b );
return VLC_SUCCESS;
}
assert( p_sys->i_length > 0 );
i64 = CLOCK_FREQ * p_sys->i_length * f;
Ogg_ResetStreamsHelper( p_sys );
if ( Oggseek_SeektoAbsolutetime( p_demux, p_stream, i64 ) >= 0 )
{
es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
VLC_TS_0 + i64 );
return VLC_SUCCESS;
}
return VLC_EGENERIC;
case DEMUX_GET_LENGTH:
if ( p_sys->i_length < 0 )
return demux_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
1, i_query, args );
pi64 = (int64_t*)va_arg( args, int64_t * );
*pi64 = p_sys->i_length * 1000000;
return VLC_SUCCESS;
case DEMUX_GET_TITLE_INFO:
{
input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
int *pi_int = (int*)va_arg( args, int* );
int *pi_title_offset = (int*)va_arg( args, int* );
int *pi_seekpoint_offset = (int*)va_arg( args, int* );
if( p_sys->i_seekpoints > 0 )
{
*pi_int = 1;
*ppp_title = malloc( sizeof( input_title_t* ) );
input_title_t *p_title = (*ppp_title)[0] = vlc_input_title_New();
for( int i = 0; i < p_sys->i_seekpoints; i++ )
{
seekpoint_t *p_seekpoint_copy = vlc_seekpoint_Duplicate( p_sys->pp_seekpoints[i] );
if ( likely( p_seekpoint_copy ) )
TAB_APPEND( p_title->i_seekpoint, p_title->seekpoint, p_seekpoint_copy );
}
*pi_title_offset = 0;
*pi_seekpoint_offset = 0;
}
return VLC_SUCCESS;
}
case DEMUX_SET_TITLE:
{
const int i_title = (int)va_arg( args, int );
if( i_title > 1 )
return VLC_EGENERIC;
return VLC_SUCCESS;
}
case DEMUX_SET_SEEKPOINT:
{
const int i_seekpoint = (int)va_arg( args, int );
if( i_seekpoint > p_sys->i_seekpoints )
return VLC_EGENERIC;
for ( int i=0; i< p_sys->i_streams; i++ )
{
if ( p_sys->pp_stream[i]->b_initializing )
return VLC_EGENERIC;
}
i64 = p_sys->pp_seekpoints[i_seekpoint]->i_time_offset;
p_stream = Ogg_GetSelectedStream( p_demux );
if ( !p_stream )
{
msg_Err( p_demux, "No selected seekable stream found" );
return VLC_EGENERIC;
}
stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b );
if ( Oggseek_BlindSeektoAbsoluteTime( p_demux, p_stream, i64, b ) )
{
Ogg_ResetStreamsHelper( p_sys );
es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
VLC_TS_0 + i64 );
p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
p_demux->info.i_seekpoint = i_seekpoint;
return VLC_SUCCESS;
}
else
return VLC_EGENERIC;
}
default:
return demux_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
1, i_query, args );
}
}
/****************************************************************************
* Ogg_ReadPage: Read a full Ogg page from the physical bitstream.
****************************************************************************
* Returns VLC_SUCCESS if a page has been read. An error might happen if we
* are at the end of stream.
****************************************************************************/
static int Ogg_ReadPage( demux_t *p_demux, ogg_page *p_oggpage )
{
demux_sys_t *p_ogg = p_demux->p_sys ;
int i_read = 0;
char *p_buffer;
while( ogg_sync_pageout( &p_ogg->oy, p_oggpage ) != 1 )
{
p_buffer = ogg_sync_buffer( &p_ogg->oy, OGGSEEK_BYTES_TO_READ );
i_read = stream_Read( p_demux->s, p_buffer, OGGSEEK_BYTES_TO_READ );
if( i_read <= 0 )
return VLC_EGENERIC;
ogg_sync_wrote( &p_ogg->oy, i_read );
}
return VLC_SUCCESS;
}
/****************************************************************************
* Ogg_UpdatePCR: update the PCR (90kHz program clock reference) for the
* current stream.
****************************************************************************/
static void Ogg_UpdatePCR( demux_t *p_demux, logical_stream_t *p_stream,
ogg_packet *p_oggpacket )
{
demux_sys_t *p_ogg = p_demux->p_sys;
p_stream->i_end_trim = 0;
/* Convert the granulepos into a pcr */
if ( p_oggpacket->granulepos == 0 )
{
/* We're in headers, and we haven't parsed 1st data packet yet */
// p_stream->i_pcr = VLC_TS_UNKNOWN;
}
else if( p_oggpacket->granulepos > 0 )
{
if( p_stream->fmt.i_codec == VLC_CODEC_THEORA ||
p_stream->fmt.i_codec == VLC_CODEC_DAALA ||
p_stream->fmt.i_codec == VLC_CODEC_KATE ||
p_stream->fmt.i_codec == VLC_CODEC_VP8 ||
p_stream->fmt.i_codec == VLC_CODEC_DIRAC ||
p_stream->fmt.i_codec == VLC_CODEC_SPEEX ||
(p_stream->b_oggds && p_stream->fmt.i_cat == VIDEO_ES) )
{
p_stream->i_pcr = VLC_TS_0 + Oggseek_GranuleToAbsTimestamp( p_stream,
p_oggpacket->granulepos, true );
p_stream->i_pcr += p_ogg->i_nzpcr_offset;
}
else if ( p_stream->i_previous_granulepos > 0 )
{
ogg_int64_t sample = p_stream->i_previous_granulepos;
if( p_stream->fmt.i_codec == VLC_CODEC_OPUS && p_oggpacket->e_o_s )
{
unsigned duration = Ogg_OpusPacketDuration( p_oggpacket );
if( duration > 0 )
{
ogg_int64_t end_sample = p_oggpacket->granulepos;
if( end_sample < ( sample + duration ) )
p_stream->i_end_trim = sample + duration - end_sample;