forked from videolan/vlc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathogg.c
1763 lines (1546 loc) · 61 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 muxer module for vlc
*****************************************************************************
* Copyright (C) 2001, 2002, 2006 VLC authors and VideoLAN
* $Id$
*
* Authors: Laurent Aimar <[email protected]>
* Gildas Bazin <[email protected]>
*
* 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_sout.h>
#include <vlc_block.h>
#include <vlc_codecs.h>
#include <limits.h>
#include <vlc_rand.h>
#include "../demux/xiph.h"
#include <ogg/ogg.h>
/*****************************************************************************
* Module descriptor
*****************************************************************************/
#define INDEXINTVL_TEXT N_("Index interval")
#define INDEXINTVL_LONGTEXT N_("Minimal index interval, in microseconds. " \
"Set to 0 to disable index creation.")
#define INDEXRATIO_TEXT N_("Index size ratio")
#define INDEXRATIO_LONGTEXT N_(\
"Set index size ratio. Alters default (60min content) or estimated size." )
static int Open ( vlc_object_t * );
static void Close ( vlc_object_t * );
#define SOUT_CFG_PREFIX "sout-ogg-"
vlc_module_begin ()
set_description( N_("Ogg/OGM muxer") )
set_capability( "sout mux", 10 )
set_category( CAT_SOUT )
set_subcategory( SUBCAT_SOUT_MUX )
add_shortcut( "ogg", "ogm" )
add_integer_with_range( SOUT_CFG_PREFIX "indexintvl", 1000, 0, INT_MAX,
INDEXINTVL_TEXT, INDEXINTVL_LONGTEXT, true )
add_float_with_range( SOUT_CFG_PREFIX "indexratio", 1.0, 1.0, 1000,
INDEXRATIO_TEXT, INDEXRATIO_LONGTEXT, true )
set_callbacks( Open, Close )
vlc_module_end ()
/*****************************************************************************
* Exported prototypes
*****************************************************************************/
static int Control ( sout_mux_t *, int, va_list );
static int AddStream( sout_mux_t *, sout_input_t * );
static void DelStream( sout_mux_t *, sout_input_t * );
static int Mux ( sout_mux_t * );
static int MuxBlock ( sout_mux_t *, sout_input_t * );
static bool OggCreateHeaders( sout_mux_t * );
static void OggFillSkeletonFishead( uint8_t *p_buffer, sout_mux_t *p_mux );
/*****************************************************************************
* Misc declarations
*****************************************************************************/
/* Skeleton */
#define FISBONE_BASE_SIZE 52
#define FISBONE_BASE_OFFSET 44
#define INDEX_BASE_SIZE 42
/* Structures used for OggDS headers used in ogm files */
#define PACKET_TYPE_HEADER 0x01
#define PACKET_TYPE_COMMENT 0x03
#define PACKET_IS_SYNCPOINT 0x08
typedef struct
{
int32_t i_width;
int32_t i_height;
} oggds_header_video_t;
typedef struct
{
int16_t i_channels;
int16_t i_block_align;
int32_t i_avgbytespersec;
} oggds_header_audio_t;
typedef struct
{
uint8_t i_packet_type;
char stream_type[8];
char sub_type[4];
int32_t i_size;
int64_t i_time_unit;
int64_t i_samples_per_unit;
int32_t i_default_len;
int32_t i_buffer_size;
int16_t i_bits_per_sample;
int16_t i_padding_0; /* Because the original is using MSVC packing style */
union
{
oggds_header_video_t video;
oggds_header_audio_t audio;
} header;
int32_t i_padding_1; /* Because the original is using MSVC packing style */
} oggds_header_t;
/*****************************************************************************
* Definitions of structures and functions used by this plugins
*****************************************************************************/
typedef struct
{
int i_cat;
vlc_fourcc_t i_fourcc;
int b_new;
mtime_t i_dts;
mtime_t i_length;
int i_packet_no;
int i_serial_no;
int i_keyframe_granule_shift; /* Theora and Daala only */
int i_last_keyframe; /* dirac and theora */
int i_num_frames; /* Theora only */
uint64_t u_last_granulepos; /* Used for correct EOS page */
int64_t i_num_keyframes;
ogg_stream_state os;
oggds_header_t *p_oggds_header;
bool b_started;
bool b_finished;
struct
{
bool b_fisbone_done;
bool b_index_done;
/* Skeleton index storage */
unsigned char *p_index;
uint64_t i_index_size;
uint64_t i_index_payload; /* real index size */
uint64_t i_index_count;
/* backup values for rewriting index page later */
uint64_t i_index_offset; /* sout offset of the index page */
int64_t i_index_packetno; /* index packet no */
int i_index_pageno;
/* index creation tracking values */
uint64_t i_last_keyframe_pos;
uint64_t i_last_keyframe_time;
} skeleton;
int i_dirac_last_pt;
int i_dirac_last_dt;
mtime_t i_baseptsdelay;
} ogg_stream_t;
struct sout_mux_sys_t
{
int i_streams;
mtime_t i_start_dts;
int i_next_serial_no;
/* number of logical streams pending to be added */
int i_add_streams;
bool b_can_add_streams;
/* logical streams pending to be deleted */
int i_del_streams;
ogg_stream_t **pp_del_streams;
/* Skeleton */
struct
{
bool b_create;
int i_serial_no;
int i_packet_no;
ogg_stream_state os;
bool b_head_done;
/* backup values for rewriting fishead page later */
uint64_t i_fishead_offset; /* sout offset of the fishead page */
int i_index_intvl;
float i_index_ratio;
} skeleton;
/* access position */
ssize_t i_pos;
ssize_t i_data_start;
ssize_t i_segment_start;
};
static void OggSetDate( block_t *, mtime_t , mtime_t );
static block_t *OggStreamFlush( sout_mux_t *, ogg_stream_state *, mtime_t );
static void OggCreateStreamFooter( sout_mux_t *p_mux, ogg_stream_t *p_stream );
static void OggRewriteFisheadPage( sout_mux_t *p_mux );
static bool AllocateIndex( sout_mux_t *p_mux, sout_input_t *p_input );
/*****************************************************************************
* Open: Open muxer
*****************************************************************************/
static int Open( vlc_object_t *p_this )
{
sout_mux_t *p_mux = (sout_mux_t*)p_this;
sout_mux_sys_t *p_sys;
msg_Info( p_mux, "Open" );
p_sys = malloc( sizeof( sout_mux_sys_t ) );
if( !p_sys )
return VLC_ENOMEM;
p_sys->i_streams = 0;
p_sys->i_add_streams = 0;
p_sys->b_can_add_streams = true;
p_sys->i_del_streams = 0;
p_sys->pp_del_streams = 0;
p_sys->i_pos = 0;
p_sys->skeleton.b_create = false;
p_sys->skeleton.b_head_done = false;
p_sys->skeleton.i_index_intvl =
var_InheritInteger( p_this, SOUT_CFG_PREFIX "indexintvl" );
p_sys->skeleton.i_index_ratio =
var_InheritFloat( p_this, SOUT_CFG_PREFIX "indexratio" );
p_sys->i_data_start = 0;
p_sys->i_segment_start = 0;
p_mux->p_sys = p_sys;
p_mux->pf_control = Control;
p_mux->pf_addstream = AddStream;
p_mux->pf_delstream = DelStream;
p_mux->pf_mux = Mux;
/* First serial number is random.
* (Done like this because on win32 you need to seed the random number
* generator once per thread). */
uint32_t r;
vlc_rand_bytes(&r, sizeof(r));
p_sys->i_next_serial_no = r & INT_MAX;
return VLC_SUCCESS;
}
/*****************************************************************************
* Close: Finalize ogg bitstream and close muxer
*****************************************************************************/
static void Close( vlc_object_t * p_this )
{
sout_mux_t *p_mux = (sout_mux_t*)p_this;
sout_mux_sys_t *p_sys = p_mux->p_sys;
ogg_stream_t *p_stream;
msg_Info( p_mux, "Close" );
if( p_sys->i_del_streams )
{
/* Close the current ogg stream */
msg_Dbg( p_mux, "writing footers" );
for(int i = 0; i < p_mux->i_nb_inputs; i++ )
{
p_stream = (ogg_stream_t *) p_mux->pp_inputs[i]->p_sys;
OggCreateStreamFooter( p_mux, p_stream );
free( p_stream->skeleton.p_index );
}
/* Remove deleted logical streams */
for(int i = 0; i < p_sys->i_del_streams; i++ )
{
OggCreateStreamFooter( p_mux, p_sys->pp_del_streams[i] );
free( p_sys->pp_del_streams[i]->p_oggds_header );
free( p_sys->pp_del_streams[i]->skeleton.p_index );
free( p_sys->pp_del_streams[i] );
}
free( p_sys->pp_del_streams );
p_sys->i_streams -= p_sys->i_del_streams;
}
/* rewrite fishead with final values */
if ( p_sys->skeleton.b_create && p_sys->skeleton.b_head_done )
{
OggRewriteFisheadPage( p_mux );
}
free( p_sys );
}
/*****************************************************************************
* Control:
*****************************************************************************/
static int Control( sout_mux_t *p_mux, int i_query, va_list args )
{
VLC_UNUSED(p_mux);
bool *pb_bool;
char **ppsz;
switch( i_query )
{
case MUX_CAN_ADD_STREAM_WHILE_MUXING:
pb_bool = (bool*)va_arg( args, bool * );
*pb_bool = true;
return VLC_SUCCESS;
case MUX_GET_ADD_STREAM_WAIT:
pb_bool = (bool*)va_arg( args, bool * );
*pb_bool = true;
return VLC_SUCCESS;
case MUX_GET_MIME:
ppsz = (char**)va_arg( args, char ** );
*ppsz = strdup( "application/ogg" );
return VLC_SUCCESS;
default:
return VLC_EGENERIC;
}
}
/*****************************************************************************
* AddStream: Add an elementary stream to the muxed stream
*****************************************************************************/
static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
{
sout_mux_sys_t *p_sys = p_mux->p_sys;
ogg_stream_t *p_stream;
uint16_t i_tag;
msg_Dbg( p_mux, "adding input" );
p_input->p_sys = p_stream = calloc( 1, sizeof( ogg_stream_t ) );
if( !p_stream )
return VLC_ENOMEM;
p_stream->i_cat = p_input->p_fmt->i_cat;
p_stream->i_fourcc = p_input->p_fmt->i_codec;
p_stream->i_serial_no = p_sys->i_next_serial_no++;
p_stream->i_packet_no = 0;
p_stream->i_last_keyframe = 0;
p_stream->i_num_keyframes = 0;
p_stream->i_num_frames = 0;
p_stream->p_oggds_header = 0;
p_stream->i_baseptsdelay = -1;
p_stream->i_dirac_last_pt = -1;
p_stream->i_dirac_last_dt = -1;
switch( p_input->p_fmt->i_cat )
{
case VIDEO_ES:
if( !p_input->p_fmt->video.i_frame_rate ||
!p_input->p_fmt->video.i_frame_rate_base )
{
msg_Warn( p_mux, "Missing frame rate, assuming 25fps" );
assert(p_input->p_fmt == &p_input->fmt);
p_input->fmt.video.i_frame_rate = 25;
p_input->fmt.video.i_frame_rate_base = 1;
}
switch( p_stream->i_fourcc )
{
case VLC_CODEC_MP4V:
case VLC_CODEC_MPGV:
case VLC_CODEC_MP1V:
case VLC_CODEC_MP2V:
case VLC_CODEC_DIV3:
case VLC_CODEC_MJPG:
case VLC_CODEC_WMV1:
case VLC_CODEC_WMV2:
case VLC_CODEC_WMV3:
p_stream->p_oggds_header = calloc( 1, sizeof(oggds_header_t) );
if( !p_stream->p_oggds_header )
{
free( p_stream );
return VLC_ENOMEM;
}
p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
memcpy( p_stream->p_oggds_header->stream_type, "video", 5 );
if( p_stream->i_fourcc == VLC_CODEC_MP4V )
{
memcpy( p_stream->p_oggds_header->sub_type, "XVID", 4 );
}
else if( p_stream->i_fourcc == VLC_CODEC_DIV3 )
{
memcpy( p_stream->p_oggds_header->sub_type, "DIV3", 4 );
}
else
{
memcpy( p_stream->p_oggds_header->sub_type,
&p_stream->i_fourcc, 4 );
}
p_stream->p_oggds_header->i_size = 0 ;
p_stream->p_oggds_header->i_time_unit =
INT64_C(10000000) * p_input->p_fmt->video.i_frame_rate_base /
(int64_t)p_input->p_fmt->video.i_frame_rate;
p_stream->p_oggds_header->i_samples_per_unit = 1;
p_stream->p_oggds_header->i_default_len = 1 ; /* ??? */
p_stream->p_oggds_header->i_buffer_size = 1024*1024;
p_stream->p_oggds_header->i_bits_per_sample = 0;
p_stream->p_oggds_header->header.video.i_width = p_input->p_fmt->video.i_width;
p_stream->p_oggds_header->header.video.i_height = p_input->p_fmt->video.i_height;
msg_Dbg( p_mux, "%4.4s stream", (char *)&p_stream->i_fourcc );
break;
case VLC_CODEC_DIRAC:
msg_Dbg( p_mux, "dirac stream" );
break;
case VLC_CODEC_THEORA:
msg_Dbg( p_mux, "theora stream" );
break;
case VLC_CODEC_DAALA:
msg_Dbg( p_mux, "daala stream" );
break;
case VLC_CODEC_VP8:
msg_Dbg( p_mux, "VP8 stream" );
break;
default:
FREENULL( p_input->p_sys );
return VLC_EGENERIC;
}
break;
case AUDIO_ES:
switch( p_stream->i_fourcc )
{
case VLC_CODEC_OPUS:
msg_Dbg( p_mux, "opus stream" );
break;
case VLC_CODEC_VORBIS:
msg_Dbg( p_mux, "vorbis stream" );
break;
case VLC_CODEC_SPEEX:
msg_Dbg( p_mux, "speex stream" );
break;
case VLC_CODEC_FLAC:
msg_Dbg( p_mux, "flac stream" );
break;
default:
fourcc_to_wf_tag( p_stream->i_fourcc, &i_tag );
if( i_tag == WAVE_FORMAT_UNKNOWN )
{
FREENULL( p_input->p_sys );
return VLC_EGENERIC;
}
p_stream->p_oggds_header =
malloc( sizeof(oggds_header_t) + p_input->p_fmt->i_extra );
if( !p_stream->p_oggds_header )
{
free( p_stream );
return VLC_ENOMEM;
}
memset( p_stream->p_oggds_header, 0, sizeof(oggds_header_t) );
p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
p_stream->p_oggds_header->i_size = p_input->p_fmt->i_extra;
if( p_input->p_fmt->i_extra )
{
memcpy( &p_stream->p_oggds_header[1],
p_input->p_fmt->p_extra, p_input->p_fmt->i_extra );
}
memcpy( p_stream->p_oggds_header->stream_type, "audio", 5 );
memset( p_stream->p_oggds_header->sub_type, 0, 4 );
char buf[5];
snprintf( buf, sizeof(buf), "%"PRIx16, i_tag );
strncpy( p_stream->p_oggds_header->sub_type, buf, 4 );
p_stream->p_oggds_header->i_time_unit = INT64_C(10000000);
p_stream->p_oggds_header->i_default_len = 1;
p_stream->p_oggds_header->i_buffer_size = 30*1024 ;
p_stream->p_oggds_header->i_samples_per_unit = p_input->p_fmt->audio.i_rate;
p_stream->p_oggds_header->i_bits_per_sample = p_input->p_fmt->audio.i_bitspersample;
p_stream->p_oggds_header->header.audio.i_channels = p_input->p_fmt->audio.i_channels;
p_stream->p_oggds_header->header.audio.i_block_align = p_input->p_fmt->audio.i_blockalign;
p_stream->p_oggds_header->header.audio.i_avgbytespersec = p_input->p_fmt->i_bitrate / 8;
msg_Dbg( p_mux, "%4.4s stream", (char *)&p_stream->i_fourcc );
break;
}
break;
case SPU_ES:
switch( p_stream->i_fourcc )
{
case VLC_CODEC_SUBT:
p_stream->p_oggds_header = calloc( 1, sizeof(oggds_header_t) );
if( !p_stream->p_oggds_header )
{
free( p_stream );
return VLC_ENOMEM;
}
p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
memcpy( p_stream->p_oggds_header->stream_type, "text", 4 );
msg_Dbg( p_mux, "subtitles stream" );
break;
default:
FREENULL( p_input->p_sys );
return VLC_EGENERIC;
}
break;
default:
FREENULL( p_input->p_sys );
return VLC_EGENERIC;
}
p_stream->b_new = true;
p_sys->i_add_streams++;
return VLC_SUCCESS;
}
/*****************************************************************************
* DelStream: Delete an elementary stream from the muxed stream
*****************************************************************************/
static void DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
{
sout_mux_sys_t *p_sys = p_mux->p_sys;
ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
block_t *p_og;
msg_Dbg( p_mux, "removing input" );
/* flush all remaining data */
if( p_input->p_sys )
{
if( !p_stream->b_new )
{
while( block_FifoCount( p_input->p_fifo ) )
MuxBlock( p_mux, p_input );
}
if( !p_stream->b_new &&
( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
{
OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
p_mux->p_sys->i_pos += sout_AccessOutWrite( p_mux->p_access, p_og );
}
/* move input in delete queue */
if( !p_stream->b_new )
{
p_sys->pp_del_streams = xrealloc( p_sys->pp_del_streams,
(p_sys->i_del_streams + 1) * sizeof(ogg_stream_t *) );
p_sys->pp_del_streams[p_sys->i_del_streams++] = p_stream;
}
else
{
/* wasn't already added so get rid of it */
FREENULL( p_stream->p_oggds_header );
FREENULL( p_stream );
p_sys->i_add_streams--;
}
}
p_input->p_sys = NULL;
}
/*****************************************************************************
* Ogg Skeleton helpers
*****************************************************************************/
static int WriteQWVariableLE( uint64_t i_64, uint64_t i_offset,
uint8_t *p_buffer, int i_buffer_size )
{
uint8_t *p_dest = p_buffer + i_offset;
int i_written = 0;
for(;;)
{
if ( p_dest - p_buffer >= i_buffer_size ) return -1;
*p_dest = (uint8_t) ( i_64 & 0x7F );
i_64 >>= 7;
i_written++;
if ( i_64 == 0 )
{
*p_dest |= 0x80;
return i_written;
}
p_dest++;
}
}
static bool AddIndexEntry( sout_mux_t *p_mux, uint64_t i_time, sout_input_t *p_input )
{
sout_mux_sys_t *p_sys = p_mux->p_sys;
ogg_stream_t *p_stream = (ogg_stream_t *) p_input->p_sys;
uint64_t i_posdelta;
uint64_t i_timedelta;
if ( !p_sys->skeleton.b_create || p_mux->p_sys->skeleton.i_index_intvl == 0
|| !p_stream->skeleton.p_index )
return false;
if ( p_stream->skeleton.i_last_keyframe_pos == 0 )
p_stream->skeleton.i_last_keyframe_pos = p_sys->i_segment_start;
i_posdelta = p_sys->i_pos - p_stream->skeleton.i_last_keyframe_pos;
i_timedelta = i_time - p_stream->skeleton.i_last_keyframe_time;
if ( i_timedelta <= ( (uint64_t) p_mux->p_sys->skeleton.i_index_intvl * 1000 )
|| i_posdelta <= 0xFFFF )
return false;
/* do inserts */
int i_ret;
if ( !p_stream->skeleton.p_index ) return false;
uint64_t i_offset = p_stream->skeleton.i_index_payload;
i_ret = WriteQWVariableLE( i_posdelta, i_offset, p_stream->skeleton.p_index,
p_stream->skeleton.i_index_size );
if ( i_ret == -1 ) return false;
i_offset += i_ret;
i_ret = WriteQWVariableLE( i_timedelta, i_offset, p_stream->skeleton.p_index,
p_stream->skeleton.i_index_size );
if ( i_ret == -1 ) return false;
p_stream->skeleton.i_index_payload = i_offset + i_ret;
p_stream->skeleton.i_index_count++;
/* update diff points */
p_stream->skeleton.i_last_keyframe_pos = p_sys->i_pos;
p_stream->skeleton.i_last_keyframe_time = i_time;
msg_Dbg( p_mux, "Added index on stream %d entry %zd %"PRIu64,
p_stream->i_serial_no, p_sys->i_pos - p_sys->i_segment_start, i_time );
return true;
}
/*****************************************************************************
* Ogg bitstream manipulation routines
*****************************************************************************/
static block_t *OggStreamGetPage( sout_mux_t *p_mux,
ogg_stream_state *p_os, mtime_t i_pts,
bool flush )
{
(void)p_mux;
block_t *p_og, *p_og_first = NULL;
ogg_page og;
int (*pager)( ogg_stream_state*, ogg_page* ) = flush ? ogg_stream_flush : ogg_stream_pageout;
while( pager( p_os, &og ) )
{
/* Flush all data */
p_og = block_Alloc( og.header_len + og.body_len );
memcpy( p_og->p_buffer, og.header, og.header_len );
memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
p_og->i_dts = 0;
p_og->i_pts = i_pts;
p_og->i_length = 0;
i_pts = 0; // write it only once
block_ChainAppend( &p_og_first, p_og );
}
return p_og_first;
}
static block_t *OggStreamFlush( sout_mux_t *p_mux,
ogg_stream_state *p_os, mtime_t i_pts )
{
return OggStreamGetPage( p_mux, p_os, i_pts, true );
}
static block_t *OggStreamPageOut( sout_mux_t *p_mux,
ogg_stream_state *p_os, mtime_t i_pts )
{
return OggStreamGetPage( p_mux, p_os, i_pts, false );
}
static void OggGetSkeletonIndex( uint8_t **pp_buffer, long *pi_size, ogg_stream_t *p_stream )
{
uint8_t *p_buffer = calloc( INDEX_BASE_SIZE + p_stream->skeleton.i_index_size, sizeof(uint8_t) );
if ( !p_buffer ) return;
*pp_buffer = p_buffer;
memcpy( p_buffer, "index", 6 );
SetDWLE( &p_buffer[6], p_stream->i_serial_no );
SetQWLE( &p_buffer[10], p_stream->skeleton.i_index_count ); /* num keypoints */
SetQWLE( &p_buffer[18], 1000000 );
SetQWLE( &p_buffer[34], p_stream->i_length );
memcpy( p_buffer + INDEX_BASE_SIZE, p_stream->skeleton.p_index, p_stream->skeleton.i_index_payload );
*pi_size = INDEX_BASE_SIZE + p_stream->skeleton.i_index_size;
}
static void OggGetSkeletonFisbone( uint8_t **pp_buffer, long *pi_size,
sout_input_t *p_input, sout_mux_t *p_mux )
{
uint8_t *psz_header;
uint8_t *p_buffer;
const char *psz_value = NULL;
ogg_stream_t *p_stream = (ogg_stream_t *) p_input->p_sys;
struct
{
char *psz_content_type;
char *psz_role;
long int i_size;
unsigned int i_count;
} headers = { NULL, NULL, 0, 0 };
*pi_size = 0;
switch( p_stream->i_fourcc )
{
case VLC_CODEC_VORBIS:
psz_value = "audio/vorbis";
break;
case VLC_CODEC_THEORA:
psz_value = "video/theora";
break;
case VLC_CODEC_DAALA:
psz_value = "video/daala";
break;
case VLC_CODEC_SPEEX:
psz_value = "audio/speex";
break;
case VLC_CODEC_FLAC:
psz_value = "audio/flac";
break;
case VLC_CODEC_CMML:
psz_value = "text/cmml";
break;
case VLC_CODEC_KATE:
psz_value = "application/kate";
break;
case VLC_CODEC_VP8:
psz_value = "video/x-vp8";
break;
default:
psz_value = "application/octet-stream";
msg_Warn( p_mux, "Unknown fourcc for stream %s, setting Content-Type to %s",
vlc_fourcc_GetDescription( p_stream->i_cat, p_stream->i_fourcc ),
psz_value );
}
/* Content Type Header */
if ( asprintf( &headers.psz_content_type, "Content-Type: %s\r\n", psz_value ) != -1 )
{
headers.i_size += strlen( headers.psz_content_type );
headers.i_count++;
}
/* Set Role Header */
if ( p_input->p_fmt->i_priority > ES_PRIORITY_NOT_SELECTABLE )
{
int i_max_prio = ES_PRIORITY_MIN;
for ( int i=0; i< p_mux->i_nb_inputs; i++ )
{
if ( p_mux->pp_inputs[i]->p_fmt->i_cat != p_input->p_fmt->i_cat ) continue;
i_max_prio = __MAX( p_mux->pp_inputs[i]->p_fmt->i_priority, i_max_prio );
}
psz_value = NULL;
if ( p_input->p_fmt->i_cat == AUDIO_ES || p_input->p_fmt->i_cat == VIDEO_ES )
{
if ( p_input->p_fmt->i_priority == i_max_prio && i_max_prio >= ES_PRIORITY_SELECTABLE_MIN )
psz_value = ( p_input->p_fmt->i_cat == VIDEO_ES ) ?
"video/main" : "audio/main";
else
psz_value = ( p_input->p_fmt->i_cat == VIDEO_ES ) ?
"video/alternate" : "audio/alternate";
}
else if ( p_input->p_fmt->i_cat == SPU_ES )
{
psz_value = ( p_input->p_fmt->i_codec == VLC_CODEC_KATE ) ?
"text/karaoke" : "text/subtitle";
}
if ( psz_value && asprintf( &headers.psz_role, "Role: %s\r\n", psz_value ) != -1 )
{
headers.i_size += strlen( headers.psz_role );
headers.i_count++;
}
}
*pp_buffer = calloc( FISBONE_BASE_SIZE + headers.i_size, sizeof(uint8_t) );
if ( !*pp_buffer ) return;
p_buffer = *pp_buffer;
memcpy( p_buffer, "fisbone", 8 );
SetDWLE( &p_buffer[8], FISBONE_BASE_OFFSET ); /* offset to message headers */
SetDWLE( &p_buffer[12], p_stream->i_serial_no );
SetDWLE( &p_buffer[16], headers.i_count );
/* granulerate den */
switch ( p_input->p_fmt->i_cat )
{
case VIDEO_ES:
SetQWLE( &(*pp_buffer)[20], p_input->p_fmt->video.i_frame_rate );
SetQWLE( &(*pp_buffer)[28], p_input->p_fmt->video.i_frame_rate_base );
break;
case AUDIO_ES:
SetQWLE( &(*pp_buffer)[20], p_input->p_fmt->audio.i_rate );
SetQWLE( &(*pp_buffer)[28], 1 );
break;
default:
SetQWLE( &(*pp_buffer)[20], 1000 );
SetQWLE( &(*pp_buffer)[28], 1 );
}
/* preroll */
if ( p_input->p_fmt->p_extra )
SetDWLE( &(*pp_buffer)[44], xiph_CountHeaders( p_input->p_fmt->p_extra, p_input->p_fmt->i_extra ) );
if ( headers.i_size > 0 )
{
psz_header = *pp_buffer + FISBONE_BASE_SIZE;
memcpy( psz_header, headers.psz_content_type, strlen( headers.psz_content_type ) );
psz_header += strlen( headers.psz_content_type );
if ( headers.psz_role )
memcpy( psz_header, headers.psz_role, strlen( headers.psz_role ) );
}
*pi_size = FISBONE_BASE_SIZE + headers.i_size;
free( headers.psz_content_type );
free( headers.psz_role );
}
static void OggFillSkeletonFishead( uint8_t *p_buffer, sout_mux_t *p_mux )
{
memcpy( p_buffer, "fishead", 8 );
SetWLE( &p_buffer[8], 4 );
SetWLE( &p_buffer[10], 0 );
SetQWLE( &p_buffer[20], 1000 );
SetQWLE( &p_buffer[36], 1000 );
SetQWLE( &p_buffer[64], p_mux->p_sys->i_pos - p_mux->p_sys->i_segment_start ); /* segment length */
SetQWLE( &p_buffer[72], p_mux->p_sys->i_data_start - p_mux->p_sys->i_segment_start ); /* data start offset */
}
static int32_t OggFillDsHeader( uint8_t *p_buffer, oggds_header_t *p_oggds_header, int i_cat )
{
int index = 0;
p_buffer[index] = p_oggds_header->i_packet_type;
index++;
memcpy( &p_buffer[index], p_oggds_header->stream_type, sizeof(p_oggds_header->stream_type) );
index += sizeof(p_oggds_header->stream_type);
memcpy(&p_buffer[index], p_oggds_header->sub_type, sizeof(p_oggds_header->sub_type) );
index += sizeof(p_oggds_header->sub_type);
/* The size is filled at the end */
uint8_t *p_isize = &p_buffer[index];
index += 4;
SetQWLE( &p_buffer[index], p_oggds_header->i_time_unit );
index += 8;
SetQWLE( &p_buffer[index], p_oggds_header->i_samples_per_unit );
index += 8;
SetDWLE( &p_buffer[index], p_oggds_header->i_default_len );
index += 4;
SetDWLE( &p_buffer[index], p_oggds_header->i_buffer_size );
index += 4;
SetWLE( &p_buffer[index], p_oggds_header->i_bits_per_sample );
index += 2;
SetWLE( &p_buffer[index], p_oggds_header->i_padding_0 );
index += 2;
/* audio or video */
switch( i_cat )
{
case VIDEO_ES:
SetDWLE( &p_buffer[index], p_oggds_header->header.video.i_width );
SetDWLE( &p_buffer[index+4], p_oggds_header->header.video.i_height );
break;
case AUDIO_ES:
SetWLE( &p_buffer[index], p_oggds_header->header.audio.i_channels );
SetWLE( &p_buffer[index+2], p_oggds_header->header.audio.i_block_align );
SetDWLE( &p_buffer[index+4], p_oggds_header->header.audio.i_avgbytespersec );
break;
}
index += 8;
SetDWLE( &p_buffer[index], p_oggds_header->i_padding_1 );
index += 4;
/* extra header */
if( p_oggds_header->i_size > 0 )
{
memcpy( &p_buffer[index], (uint8_t *) p_oggds_header + sizeof(*p_oggds_header), p_oggds_header->i_size );
index += p_oggds_header->i_size;
}
SetDWLE( p_isize, index-1 );
return index;
}
static void OggFillVP8Header( uint8_t *p_buffer, sout_input_t *p_input )
{
memcpy( p_buffer, "OVP80\x01\x01\x00", 8 );
SetWBE( &p_buffer[8], p_input->p_fmt->video.i_width );
SetDWBE( &p_buffer[14], p_input->p_fmt->video.i_sar_den );/* 24 bits, 15~ */
SetDWBE( &p_buffer[11], p_input->p_fmt->video.i_sar_num );/* 24 bits, 12~ */
SetWBE( &p_buffer[10], p_input->p_fmt->video.i_height );
SetDWBE( &p_buffer[18], p_input->p_fmt->video.i_frame_rate );
SetDWBE( &p_buffer[22], p_input->p_fmt->video.i_frame_rate_base );
}
static bool OggCreateHeaders( sout_mux_t *p_mux )
{
block_t *p_hdr = NULL;
block_t *p_og = NULL;
ogg_packet op;
ogg_stream_t *p_stream;
sout_mux_sys_t *p_sys = p_mux->p_sys;
int i;
if( sout_AccessOutControl( p_mux->p_access,
ACCESS_OUT_CAN_SEEK,
&p_sys->skeleton.b_create ) )
{
p_sys->skeleton.b_create = false;
}
p_sys->skeleton.b_create &= !! p_mux->i_nb_inputs;
/* no skeleton for solo vorbis/speex/opus tracks */
if ( p_mux->i_nb_inputs == 1 && p_mux->pp_inputs[0]->p_fmt->i_cat == AUDIO_ES )
{
p_sys->skeleton.b_create = false;
}
else
{
for ( int i=0; i< p_mux->i_nb_inputs; i++ )
{
p_stream = (ogg_stream_t*) p_mux->pp_inputs[i]->p_sys;
if ( p_stream->p_oggds_header )
{
/* We don't want skeleton for OggDS */
p_sys->skeleton.b_create = false;
break;
}
}
}
/* Skeleton's Fishead must be the first page of the stream */
if ( p_sys->skeleton.b_create && !p_sys->skeleton.b_head_done )
{
msg_Dbg( p_mux, "creating header for skeleton" );
p_sys->skeleton.i_serial_no = p_sys->i_next_serial_no++;
ogg_stream_init( &p_sys->skeleton.os, p_sys->skeleton.i_serial_no );
op.bytes = 80;
op.packet = calloc( 1, op.bytes );
if ( op.packet == NULL ) return false;
op.b_o_s = 1;
op.e_o_s = 0;
op.granulepos = 0;
op.packetno = 0;
OggFillSkeletonFishead( op.packet, p_mux );
ogg_stream_packetin( &p_sys->skeleton.os, &op );
p_og = OggStreamFlush( p_mux, &p_sys->skeleton.os, 0 );
block_ChainAppend( &p_hdr, p_og );
p_sys->skeleton.b_head_done = true;
p_sys->skeleton.i_fishead_offset = p_sys->i_pos;
}
/* Write header for each stream. All b_o_s (beginning of stream) packets
* must appear first in the ogg stream so we take care of them first. */
for( int pass = 0; pass < 2; pass++ )
{
for( i = 0; i < p_mux->i_nb_inputs; i++ )
{
sout_input_t *p_input = p_mux->pp_inputs[i];