]> git.sesse.net Git - vlc/blob - modules/mux/ogg.c
access: archive: remove dead initialization
[vlc] / modules / mux / ogg.c
1 /*****************************************************************************
2  * ogg.c: ogg muxer module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001, 2002, 2006 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_sout.h>
36 #include <vlc_block.h>
37 #include <vlc_codecs.h>
38 #include <limits.h>
39 #include <vlc_rand.h>
40 #include "../demux/xiph.h"
41
42 #include <ogg/ogg.h>
43
44 /*****************************************************************************
45  * Module descriptor
46  *****************************************************************************/
47 #define INDEXINTVL_TEXT N_("Index interval")
48 #define INDEXINTVL_LONGTEXT N_("Minimal index interval, in microseconds. " \
49     "Set to 0 to disable index creation.")
50 #define INDEXRATIO_TEXT N_("Index size ratio")
51 #define INDEXRATIO_LONGTEXT N_(\
52     "Set index size ratio. Alters default (60min content) or estimated size." )
53
54 static int  Open   ( vlc_object_t * );
55 static void Close  ( vlc_object_t * );
56
57 #define SOUT_CFG_PREFIX "sout-ogg-"
58
59 vlc_module_begin ()
60     set_description( N_("Ogg/OGM muxer") )
61     set_capability( "sout mux", 10 )
62     set_category( CAT_SOUT )
63     set_subcategory( SUBCAT_SOUT_MUX )
64     add_shortcut( "ogg", "ogm" )
65     add_integer_with_range( SOUT_CFG_PREFIX "indexintvl", 1000, 0, INT_MAX,
66                             INDEXINTVL_TEXT, INDEXINTVL_LONGTEXT, true )
67     add_float_with_range( SOUT_CFG_PREFIX "indexratio", 1.0, 1.0, 1000,
68                           INDEXRATIO_TEXT, INDEXRATIO_LONGTEXT, true )
69     set_callbacks( Open, Close )
70 vlc_module_end ()
71
72
73 /*****************************************************************************
74  * Exported prototypes
75  *****************************************************************************/
76 static int Control  ( sout_mux_t *, int, va_list );
77 static int AddStream( sout_mux_t *, sout_input_t * );
78 static void DelStream( sout_mux_t *, sout_input_t * );
79 static int Mux      ( sout_mux_t * );
80 static int MuxBlock ( sout_mux_t *, sout_input_t * );
81
82 static bool OggCreateHeaders( sout_mux_t * );
83 static void OggFillSkeletonFishead( uint8_t *p_buffer, sout_mux_t *p_mux );
84
85 /*****************************************************************************
86  * Misc declarations
87  *****************************************************************************/
88
89 /* Skeleton */
90 #define FISBONE_BASE_SIZE 52
91 #define FISBONE_BASE_OFFSET 44
92 #define INDEX_BASE_SIZE 42
93
94 /* Structures used for OggDS headers used in ogm files */
95
96 #define PACKET_TYPE_HEADER   0x01
97 #define PACKET_TYPE_COMMENT  0x03
98 #define PACKET_IS_SYNCPOINT  0x08
99
100 typedef struct
101 {
102     int32_t i_width;
103     int32_t i_height;
104 } oggds_header_video_t;
105
106 typedef struct
107 {
108     int16_t i_channels;
109     int16_t i_block_align;
110     int32_t i_avgbytespersec;
111 } oggds_header_audio_t;
112
113 typedef struct
114 {
115     uint8_t i_packet_type;
116
117     char stream_type[8];
118     char sub_type[4];
119
120     int32_t i_size;
121
122     int64_t i_time_unit;
123     int64_t i_samples_per_unit;
124     int32_t i_default_len;
125
126     int32_t i_buffer_size;
127     int16_t i_bits_per_sample;
128
129     int16_t i_padding_0; /* Because the original is using MSVC packing style */
130
131     union
132     {
133         oggds_header_video_t video;
134         oggds_header_audio_t audio;
135     } header;
136
137     int32_t i_padding_1; /* Because the original is using MSVC packing style */
138
139 } oggds_header_t;
140
141 /*****************************************************************************
142  * Definitions of structures and functions used by this plugins
143  *****************************************************************************/
144 typedef struct
145 {
146     int i_cat;
147     vlc_fourcc_t i_fourcc;
148
149     int b_new;
150
151     mtime_t i_dts;
152     mtime_t i_length;
153     int     i_packet_no;
154     int     i_serial_no;
155     int     i_keyframe_granule_shift; /* Theora and Daala only */
156     int     i_last_keyframe; /* dirac and theora */
157     int     i_num_frames; /* Theora only */
158     uint64_t u_last_granulepos; /* Used for correct EOS page */
159     int64_t i_num_keyframes;
160     ogg_stream_state os;
161
162     oggds_header_t *p_oggds_header;
163     bool b_started;
164     bool b_finished;
165
166     struct
167     {
168          bool b_fisbone_done;
169          bool b_index_done;
170          /* Skeleton index storage */
171          unsigned char *p_index;
172          uint64_t i_index_size;
173          uint64_t i_index_payload; /* real index size */
174          uint64_t i_index_count;
175          /* backup values for rewriting index page later */
176          uint64_t i_index_offset;  /* sout offset of the index page */
177          int64_t i_index_packetno;  /* index packet no */
178          int i_index_pageno;
179          /* index creation tracking values */
180          uint64_t i_last_keyframe_pos;
181          uint64_t i_last_keyframe_time;
182     } skeleton;
183
184     int             i_dirac_last_pt;
185     int             i_dirac_last_dt;
186     mtime_t         i_baseptsdelay;
187
188 } ogg_stream_t;
189
190 struct sout_mux_sys_t
191 {
192     int     i_streams;
193
194     mtime_t i_start_dts;
195     int     i_next_serial_no;
196
197     /* number of logical streams pending to be added */
198     int i_add_streams;
199     bool b_can_add_streams;
200
201     /* logical streams pending to be deleted */
202     int i_del_streams;
203     ogg_stream_t **pp_del_streams;
204
205     /* Skeleton */
206     struct
207     {
208         bool b_create;
209         int i_serial_no;
210         int i_packet_no;
211         ogg_stream_state os;
212         bool b_head_done;
213         /* backup values for rewriting fishead page later */
214         uint64_t i_fishead_offset;  /* sout offset of the fishead page */
215         int i_index_intvl;
216         float i_index_ratio;
217     } skeleton;
218
219     /* access position */
220     ssize_t i_pos;
221     ssize_t i_data_start;
222     ssize_t i_segment_start;
223 };
224
225 static void OggSetDate( block_t *, mtime_t , mtime_t  );
226 static block_t *OggStreamFlush( sout_mux_t *, ogg_stream_state *, mtime_t );
227 static void OggCreateStreamFooter( sout_mux_t *p_mux, ogg_stream_t *p_stream );
228 static void OggRewriteFisheadPage( sout_mux_t *p_mux );
229 static bool AllocateIndex( sout_mux_t *p_mux, sout_input_t *p_input );
230
231 /*****************************************************************************
232  * Open: Open muxer
233  *****************************************************************************/
234 static int Open( vlc_object_t *p_this )
235 {
236     sout_mux_t      *p_mux = (sout_mux_t*)p_this;
237     sout_mux_sys_t  *p_sys;
238
239     msg_Info( p_mux, "Open" );
240
241     p_sys                 = malloc( sizeof( sout_mux_sys_t ) );
242     if( !p_sys )
243         return VLC_ENOMEM;
244     p_sys->i_streams      = 0;
245     p_sys->i_add_streams  = 0;
246     p_sys->b_can_add_streams = true;
247     p_sys->i_del_streams  = 0;
248     p_sys->pp_del_streams = 0;
249     p_sys->i_pos = 0;
250     p_sys->skeleton.b_create = false;
251     p_sys->skeleton.b_head_done = false;
252     p_sys->skeleton.i_index_intvl =
253             var_InheritInteger( p_this, SOUT_CFG_PREFIX "indexintvl" );
254     p_sys->skeleton.i_index_ratio =
255             var_InheritFloat( p_this, SOUT_CFG_PREFIX "indexratio" );
256     p_sys->i_data_start = 0;
257     p_sys->i_segment_start = 0;
258     p_mux->p_sys        = p_sys;
259     p_mux->pf_control   = Control;
260     p_mux->pf_addstream = AddStream;
261     p_mux->pf_delstream = DelStream;
262     p_mux->pf_mux       = Mux;
263
264     /* First serial number is random.
265      * (Done like this because on win32 you need to seed the random number
266      *  generator once per thread). */
267     uint32_t r;
268     vlc_rand_bytes(&r, sizeof(r));
269     p_sys->i_next_serial_no = r & INT_MAX;
270
271     return VLC_SUCCESS;
272 }
273
274 /*****************************************************************************
275  * Close: Finalize ogg bitstream and close muxer
276  *****************************************************************************/
277 static void Close( vlc_object_t * p_this )
278 {
279     sout_mux_t     *p_mux = (sout_mux_t*)p_this;
280     sout_mux_sys_t *p_sys = p_mux->p_sys;
281     ogg_stream_t *p_stream;
282
283     msg_Info( p_mux, "Close" );
284
285     if( p_sys->i_del_streams )
286     {
287         /* Close the current ogg stream */
288         msg_Dbg( p_mux, "writing footers" );
289
290         for(int i = 0; i < p_mux->i_nb_inputs; i++ )
291         {
292             p_stream = (ogg_stream_t *) p_mux->pp_inputs[i]->p_sys;
293             OggCreateStreamFooter( p_mux, p_stream );
294             free( p_stream->skeleton.p_index );
295         }
296
297         /* Remove deleted logical streams */
298         for(int i = 0; i < p_sys->i_del_streams; i++ )
299         {
300             OggCreateStreamFooter( p_mux, p_sys->pp_del_streams[i] );
301             free( p_sys->pp_del_streams[i]->p_oggds_header );
302             free( p_sys->pp_del_streams[i]->skeleton.p_index );
303             free( p_sys->pp_del_streams[i] );
304         }
305         free( p_sys->pp_del_streams );
306         p_sys->i_streams -= p_sys->i_del_streams;
307     }
308
309     /* rewrite fishead with final values */
310     if ( p_sys->skeleton.b_create && p_sys->skeleton.b_head_done )
311     {
312         OggRewriteFisheadPage( p_mux );
313     }
314
315     free( p_sys );
316 }
317
318 /*****************************************************************************
319  * Control:
320  *****************************************************************************/
321 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
322 {
323     VLC_UNUSED(p_mux);
324     bool *pb_bool;
325     char **ppsz;
326
327    switch( i_query )
328    {
329        case MUX_CAN_ADD_STREAM_WHILE_MUXING:
330            pb_bool = (bool*)va_arg( args, bool * );
331            *pb_bool = true;
332            return VLC_SUCCESS;
333
334        case MUX_GET_ADD_STREAM_WAIT:
335            pb_bool = (bool*)va_arg( args, bool * );
336            *pb_bool = true;
337            return VLC_SUCCESS;
338
339        case MUX_GET_MIME:
340            ppsz = (char**)va_arg( args, char ** );
341            *ppsz = strdup( "application/ogg" );
342            return VLC_SUCCESS;
343
344         default:
345             return VLC_EGENERIC;
346    }
347 }
348 /*****************************************************************************
349  * AddStream: Add an elementary stream to the muxed stream
350  *****************************************************************************/
351 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
352 {
353     sout_mux_sys_t *p_sys = p_mux->p_sys;
354     ogg_stream_t   *p_stream;
355     uint16_t i_tag;
356
357     msg_Dbg( p_mux, "adding input" );
358
359     p_input->p_sys = p_stream = calloc( 1, sizeof( ogg_stream_t ) );
360     if( !p_stream )
361         return VLC_ENOMEM;
362
363     p_stream->i_cat       = p_input->p_fmt->i_cat;
364     p_stream->i_fourcc    = p_input->p_fmt->i_codec;
365     p_stream->i_serial_no = p_sys->i_next_serial_no++;
366     p_stream->i_packet_no = 0;
367     p_stream->i_last_keyframe = 0;
368     p_stream->i_num_keyframes = 0;
369     p_stream->i_num_frames = 0;
370
371     p_stream->p_oggds_header = 0;
372
373     p_stream->i_baseptsdelay = -1;
374     p_stream->i_dirac_last_pt = -1;
375     p_stream->i_dirac_last_dt = -1;
376
377     switch( p_input->p_fmt->i_cat )
378     {
379     case VIDEO_ES:
380         if( !p_input->p_fmt->video.i_frame_rate ||
381             !p_input->p_fmt->video.i_frame_rate_base )
382         {
383             msg_Warn( p_mux, "Missing frame rate, assuming 25fps" );
384             assert(p_input->p_fmt == &p_input->fmt);
385             p_input->fmt.video.i_frame_rate = 25;
386             p_input->fmt.video.i_frame_rate_base = 1;
387         }
388
389         switch( p_stream->i_fourcc )
390         {
391         case VLC_CODEC_MP4V:
392         case VLC_CODEC_MPGV:
393         case VLC_CODEC_MP1V:
394         case VLC_CODEC_MP2V:
395         case VLC_CODEC_DIV3:
396         case VLC_CODEC_MJPG:
397         case VLC_CODEC_WMV1:
398         case VLC_CODEC_WMV2:
399         case VLC_CODEC_WMV3:
400             p_stream->p_oggds_header = calloc( 1, sizeof(oggds_header_t) );
401             if( !p_stream->p_oggds_header )
402             {
403                 free( p_stream );
404                 return VLC_ENOMEM;
405             }
406             p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
407
408             memcpy( p_stream->p_oggds_header->stream_type, "video", 5 );
409             if( p_stream->i_fourcc == VLC_CODEC_MP4V )
410             {
411                 memcpy( p_stream->p_oggds_header->sub_type, "XVID", 4 );
412             }
413             else if( p_stream->i_fourcc == VLC_CODEC_DIV3 )
414             {
415                 memcpy( p_stream->p_oggds_header->sub_type, "DIV3", 4 );
416             }
417             else
418             {
419                 memcpy( p_stream->p_oggds_header->sub_type,
420                         &p_stream->i_fourcc, 4 );
421             }
422             p_stream->p_oggds_header->i_size = 0 ;
423             p_stream->p_oggds_header->i_time_unit =
424                      INT64_C(10000000) * p_input->p_fmt->video.i_frame_rate_base /
425                      (int64_t)p_input->p_fmt->video.i_frame_rate;
426             p_stream->p_oggds_header->i_samples_per_unit = 1;
427             p_stream->p_oggds_header->i_default_len = 1 ; /* ??? */
428             p_stream->p_oggds_header->i_buffer_size = 1024*1024;
429             p_stream->p_oggds_header->i_bits_per_sample = 0;
430             p_stream->p_oggds_header->header.video.i_width = p_input->p_fmt->video.i_width;
431             p_stream->p_oggds_header->header.video.i_height = p_input->p_fmt->video.i_height;
432             msg_Dbg( p_mux, "%4.4s stream", (char *)&p_stream->i_fourcc );
433             break;
434
435         case VLC_CODEC_DIRAC:
436             msg_Dbg( p_mux, "dirac stream" );
437             break;
438
439         case VLC_CODEC_THEORA:
440             msg_Dbg( p_mux, "theora stream" );
441             break;
442
443         case VLC_CODEC_DAALA:
444             msg_Dbg( p_mux, "daala stream" );
445             break;
446
447         case VLC_CODEC_VP8:
448             msg_Dbg( p_mux, "VP8 stream" );
449             break;
450
451         default:
452             FREENULL( p_input->p_sys );
453             return VLC_EGENERIC;
454         }
455         break;
456
457     case AUDIO_ES:
458         switch( p_stream->i_fourcc )
459         {
460         case VLC_CODEC_OPUS:
461             msg_Dbg( p_mux, "opus stream" );
462             break;
463
464         case VLC_CODEC_VORBIS:
465             msg_Dbg( p_mux, "vorbis stream" );
466             break;
467
468         case VLC_CODEC_SPEEX:
469             msg_Dbg( p_mux, "speex stream" );
470             break;
471
472         case VLC_CODEC_FLAC:
473             msg_Dbg( p_mux, "flac stream" );
474             break;
475
476         default:
477             fourcc_to_wf_tag( p_stream->i_fourcc, &i_tag );
478             if( i_tag == WAVE_FORMAT_UNKNOWN )
479             {
480                 FREENULL( p_input->p_sys );
481                 return VLC_EGENERIC;
482             }
483
484             p_stream->p_oggds_header =
485                 malloc( sizeof(oggds_header_t) + p_input->p_fmt->i_extra );
486             if( !p_stream->p_oggds_header )
487             {
488                 free( p_stream );
489                 return VLC_ENOMEM;
490             }
491             memset( p_stream->p_oggds_header, 0, sizeof(oggds_header_t) );
492             p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
493
494             p_stream->p_oggds_header->i_size = p_input->p_fmt->i_extra;
495
496             if( p_input->p_fmt->i_extra )
497             {
498                 memcpy( &p_stream->p_oggds_header[1],
499                         p_input->p_fmt->p_extra, p_input->p_fmt->i_extra );
500             }
501
502             memcpy( p_stream->p_oggds_header->stream_type, "audio", 5 );
503
504             memset( p_stream->p_oggds_header->sub_type, 0, 4 );
505             char buf[5];
506             snprintf( buf, sizeof(buf), "%"PRIx16, i_tag );
507             strncpy( p_stream->p_oggds_header->sub_type, buf, 4 );
508
509             p_stream->p_oggds_header->i_time_unit = INT64_C(10000000);
510             p_stream->p_oggds_header->i_default_len = 1;
511             p_stream->p_oggds_header->i_buffer_size = 30*1024 ;
512             p_stream->p_oggds_header->i_samples_per_unit = p_input->p_fmt->audio.i_rate;
513             p_stream->p_oggds_header->i_bits_per_sample = p_input->p_fmt->audio.i_bitspersample;
514             p_stream->p_oggds_header->header.audio.i_channels = p_input->p_fmt->audio.i_channels;
515             p_stream->p_oggds_header->header.audio.i_block_align =  p_input->p_fmt->audio.i_blockalign;
516             p_stream->p_oggds_header->header.audio.i_avgbytespersec =  p_input->p_fmt->i_bitrate / 8;
517             msg_Dbg( p_mux, "%4.4s stream", (char *)&p_stream->i_fourcc );
518             break;
519         }
520         break;
521
522     case SPU_ES:
523         switch( p_stream->i_fourcc )
524         {
525         case VLC_CODEC_SUBT:
526             p_stream->p_oggds_header = calloc( 1, sizeof(oggds_header_t) );
527             if( !p_stream->p_oggds_header )
528             {
529                 free( p_stream );
530                 return VLC_ENOMEM;
531             }
532             p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
533
534             memcpy( p_stream->p_oggds_header->stream_type, "text", 4 );
535             msg_Dbg( p_mux, "subtitles stream" );
536             break;
537
538         default:
539             FREENULL( p_input->p_sys );
540             return VLC_EGENERIC;
541         }
542         break;
543     default:
544         FREENULL( p_input->p_sys );
545         return VLC_EGENERIC;
546     }
547
548     p_stream->b_new = true;
549
550     p_sys->i_add_streams++;
551
552     return VLC_SUCCESS;
553 }
554
555 /*****************************************************************************
556  * DelStream: Delete an elementary stream from the muxed stream
557  *****************************************************************************/
558 static void DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
559 {
560     sout_mux_sys_t *p_sys  = p_mux->p_sys;
561     ogg_stream_t   *p_stream = (ogg_stream_t*)p_input->p_sys;
562     block_t *p_og;
563
564     msg_Dbg( p_mux, "removing input" );
565
566     /* flush all remaining data */
567     if( p_input->p_sys )
568     {
569         if( !p_stream->b_new )
570         {
571             while( block_FifoCount( p_input->p_fifo ) )
572                 MuxBlock( p_mux, p_input );
573         }
574
575         if( !p_stream->b_new &&
576             ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
577         {
578             OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
579             p_mux->p_sys->i_pos += sout_AccessOutWrite( p_mux->p_access, p_og );
580         }
581
582         /* move input in delete queue */
583         if( !p_stream->b_new )
584         {
585             p_sys->pp_del_streams = xrealloc( p_sys->pp_del_streams,
586                         (p_sys->i_del_streams + 1) * sizeof(ogg_stream_t *) );
587             p_sys->pp_del_streams[p_sys->i_del_streams++] = p_stream;
588         }
589         else
590         {
591             /* wasn't already added so get rid of it */
592             FREENULL( p_stream->p_oggds_header );
593             FREENULL( p_stream );
594             p_sys->i_add_streams--;
595         }
596     }
597
598     p_input->p_sys = NULL;
599 }
600
601 /*****************************************************************************
602  * Ogg Skeleton helpers
603  *****************************************************************************/
604 static int WriteQWVariableLE( uint64_t i_64, uint64_t i_offset,
605                               uint8_t *p_buffer, int i_buffer_size )
606 {
607     uint8_t *p_dest = p_buffer + i_offset;
608     int i_written = 0;
609
610     for(;;)
611     {
612         if ( p_dest - p_buffer >= i_buffer_size ) return -1;
613
614         *p_dest = (uint8_t) ( i_64 & 0x7F );
615         i_64 >>= 7;
616         i_written++;
617
618         if ( i_64 == 0 )
619         {
620             *p_dest |= 0x80;
621             return i_written;
622         }
623
624         p_dest++;
625     }
626 }
627
628 static bool AddIndexEntry( sout_mux_t *p_mux, uint64_t i_time, sout_input_t *p_input )
629 {
630     sout_mux_sys_t *p_sys = p_mux->p_sys;
631     ogg_stream_t *p_stream = (ogg_stream_t *) p_input->p_sys;
632     uint64_t i_posdelta;
633     uint64_t i_timedelta;
634     if ( !p_sys->skeleton.b_create || p_mux->p_sys->skeleton.i_index_intvl == 0
635          || !p_stream->skeleton.p_index )
636         return false;
637
638     if ( p_stream->skeleton.i_last_keyframe_pos == 0 )
639         p_stream->skeleton.i_last_keyframe_pos = p_sys->i_segment_start;
640     i_posdelta = p_sys->i_pos - p_stream->skeleton.i_last_keyframe_pos;
641     i_timedelta = i_time - p_stream->skeleton.i_last_keyframe_time;
642
643     if ( i_timedelta <= ( (uint64_t) p_mux->p_sys->skeleton.i_index_intvl * 1000 )
644          || i_posdelta <= 0xFFFF )
645         return false;
646
647     /* do inserts */
648     int i_ret;
649     if ( !p_stream->skeleton.p_index ) return false;
650     uint64_t i_offset = p_stream->skeleton.i_index_payload;
651     i_ret = WriteQWVariableLE( i_posdelta, i_offset, p_stream->skeleton.p_index,
652                                p_stream->skeleton.i_index_size );
653     if ( i_ret == -1 ) return false;
654     i_offset += i_ret;
655     i_ret = WriteQWVariableLE( i_timedelta, i_offset, p_stream->skeleton.p_index,
656                                p_stream->skeleton.i_index_size );
657     if ( i_ret == -1 ) return false;
658     p_stream->skeleton.i_index_payload = i_offset + i_ret;
659     p_stream->skeleton.i_index_count++;
660
661     /* update diff points */
662     p_stream->skeleton.i_last_keyframe_pos = p_sys->i_pos;
663     p_stream->skeleton.i_last_keyframe_time = i_time;
664     msg_Dbg( p_mux, "Added index on stream %d entry %zd %"PRIu64,
665              p_stream->i_serial_no, p_sys->i_pos - p_sys->i_segment_start, i_time );
666
667     return true;
668 }
669
670 /*****************************************************************************
671  * Ogg bitstream manipulation routines
672  *****************************************************************************/
673 static block_t *OggStreamGetPage( sout_mux_t *p_mux,
674                                   ogg_stream_state *p_os, mtime_t i_pts,
675                                   bool flush )
676 {
677     (void)p_mux;
678     block_t *p_og, *p_og_first = NULL;
679     ogg_page og;
680     int (*pager)( ogg_stream_state*, ogg_page* ) = flush ? ogg_stream_flush : ogg_stream_pageout;
681
682     while( pager( p_os, &og ) )
683     {
684         /* Flush all data */
685         p_og = block_Alloc( og.header_len + og.body_len );
686
687         memcpy( p_og->p_buffer, og.header, og.header_len );
688         memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
689         p_og->i_dts     = 0;
690         p_og->i_pts     = i_pts;
691         p_og->i_length  = 0;
692
693         i_pts = 0; // write it only once
694
695         block_ChainAppend( &p_og_first, p_og );
696     }
697
698     return p_og_first;
699 }
700
701 static block_t *OggStreamFlush( sout_mux_t *p_mux,
702                                 ogg_stream_state *p_os, mtime_t i_pts )
703 {
704     return OggStreamGetPage( p_mux, p_os, i_pts, true );
705 }
706
707 static block_t *OggStreamPageOut( sout_mux_t *p_mux,
708                                   ogg_stream_state *p_os, mtime_t i_pts )
709 {
710     return OggStreamGetPage( p_mux, p_os, i_pts, false );
711 }
712
713 static void OggGetSkeletonIndex( uint8_t **pp_buffer, long *pi_size, ogg_stream_t *p_stream )
714 {
715     uint8_t *p_buffer = calloc( INDEX_BASE_SIZE + p_stream->skeleton.i_index_size, sizeof(uint8_t) );
716     if ( !p_buffer ) return;
717     *pp_buffer = p_buffer;
718
719     memcpy( p_buffer, "index", 6 );
720     SetDWLE( &p_buffer[6], p_stream->i_serial_no );
721     SetQWLE( &p_buffer[10], p_stream->skeleton.i_index_count ); /* num keypoints */
722     SetQWLE( &p_buffer[18], 1000000 );
723     SetQWLE( &p_buffer[34], p_stream->i_length );
724     memcpy( p_buffer + INDEX_BASE_SIZE, p_stream->skeleton.p_index, p_stream->skeleton.i_index_payload );
725     *pi_size = INDEX_BASE_SIZE + p_stream->skeleton.i_index_size;
726 }
727
728 static void OggGetSkeletonFisbone( uint8_t **pp_buffer, long *pi_size,
729                                    sout_input_t *p_input, sout_mux_t *p_mux )
730 {
731     uint8_t *psz_header;
732     uint8_t *p_buffer;
733     const char *psz_value = NULL;
734     ogg_stream_t *p_stream = (ogg_stream_t *) p_input->p_sys;
735     struct
736     {
737         char *psz_content_type;
738         char *psz_role;
739         long int i_size;
740         unsigned int i_count;
741     } headers = { NULL, NULL, 0, 0 };
742     *pi_size = 0;
743
744     switch( p_stream->i_fourcc )
745     {
746         case VLC_CODEC_VORBIS:
747             psz_value = "audio/vorbis";
748             break;
749         case VLC_CODEC_THEORA:
750             psz_value = "video/theora";
751             break;
752         case VLC_CODEC_DAALA:
753             psz_value = "video/daala";
754             break;
755         case VLC_CODEC_SPEEX:
756             psz_value = "audio/speex";
757             break;
758         case VLC_CODEC_FLAC:
759             psz_value = "audio/flac";
760             break;
761         case VLC_CODEC_CMML:
762             psz_value = "text/cmml";
763             break;
764         case VLC_CODEC_KATE:
765             psz_value = "application/kate";
766             break;
767         case VLC_CODEC_VP8:
768             psz_value = "video/x-vp8";
769             break;
770         default:
771             psz_value = "application/octet-stream";
772             msg_Warn( p_mux, "Unknown fourcc for stream %s, setting Content-Type to %s",
773                   vlc_fourcc_GetDescription( p_stream->i_cat, p_stream->i_fourcc ),
774                   psz_value );
775     }
776
777     /* Content Type Header */
778     if ( asprintf( &headers.psz_content_type, "Content-Type: %s\r\n", psz_value ) != -1 )
779     {
780         headers.i_size += strlen( headers.psz_content_type );
781         headers.i_count++;
782     }
783
784     /* Set Role Header */
785     if ( p_input->p_fmt->i_priority > ES_PRIORITY_NOT_SELECTABLE )
786     {
787         int i_max_prio = ES_PRIORITY_MIN;
788         for ( int i=0; i< p_mux->i_nb_inputs; i++ )
789         {
790             if ( p_mux->pp_inputs[i]->p_fmt->i_cat != p_input->p_fmt->i_cat ) continue;
791             i_max_prio = __MAX( p_mux->pp_inputs[i]->p_fmt->i_priority, i_max_prio );
792         }
793
794         psz_value = NULL;
795         if ( p_input->p_fmt->i_cat == AUDIO_ES || p_input->p_fmt->i_cat == VIDEO_ES )
796         {
797             if ( p_input->p_fmt->i_priority == i_max_prio && i_max_prio >= ES_PRIORITY_SELECTABLE_MIN )
798                 psz_value = ( p_input->p_fmt->i_cat == VIDEO_ES ) ?
799                             "video/main" : "audio/main";
800             else
801                 psz_value = ( p_input->p_fmt->i_cat == VIDEO_ES ) ?
802                             "video/alternate" : "audio/alternate";
803         }
804         else if ( p_input->p_fmt->i_cat == SPU_ES )
805         {
806             psz_value = ( p_input->p_fmt->i_codec == VLC_CODEC_KATE ) ?
807                         "text/karaoke" : "text/subtitle";
808         }
809
810         if ( psz_value && asprintf( &headers.psz_role, "Role: %s\r\n", psz_value ) != -1 )
811         {
812             headers.i_size += strlen( headers.psz_role );
813             headers.i_count++;
814         }
815     }
816
817     *pp_buffer = calloc( FISBONE_BASE_SIZE + headers.i_size, sizeof(uint8_t) );
818     if ( !*pp_buffer ) return;
819     p_buffer = *pp_buffer;
820
821     memcpy( p_buffer, "fisbone", 8 );
822     SetDWLE( &p_buffer[8], FISBONE_BASE_OFFSET ); /* offset to message headers */
823     SetDWLE( &p_buffer[12], p_stream->i_serial_no );
824     SetDWLE( &p_buffer[16], headers.i_count );
825
826     /* granulerate den */
827     switch ( p_input->p_fmt->i_cat )
828     {
829         case VIDEO_ES:
830             SetQWLE( &(*pp_buffer)[20], p_input->p_fmt->video.i_frame_rate );
831             SetQWLE( &(*pp_buffer)[28], p_input->p_fmt->video.i_frame_rate_base );
832         break;
833         case AUDIO_ES:
834             SetQWLE( &(*pp_buffer)[20], p_input->p_fmt->audio.i_rate );
835             SetQWLE( &(*pp_buffer)[28], 1 );
836         break;
837         default:
838             SetQWLE( &(*pp_buffer)[20], 1000 );
839             SetQWLE( &(*pp_buffer)[28], 1 );
840     }
841
842     /* preroll */
843     if ( p_input->p_fmt->p_extra )
844         SetDWLE( &(*pp_buffer)[44], xiph_CountHeaders( p_input->p_fmt->p_extra, p_input->p_fmt->i_extra ) );
845
846     if ( headers.i_size > 0 )
847     {
848         psz_header = *pp_buffer + FISBONE_BASE_SIZE;
849         memcpy( psz_header, headers.psz_content_type, strlen( headers.psz_content_type ) );
850         psz_header += strlen( headers.psz_content_type );
851         if ( headers.psz_role )
852             memcpy( psz_header, headers.psz_role, strlen( headers.psz_role ) );
853     }
854     *pi_size = FISBONE_BASE_SIZE + headers.i_size;
855
856     free( headers.psz_content_type );
857     free( headers.psz_role );
858 }
859
860 static void OggFillSkeletonFishead( uint8_t *p_buffer, sout_mux_t *p_mux )
861 {
862     memcpy( p_buffer, "fishead", 8 );
863     SetWLE( &p_buffer[8], 4 );
864     SetWLE( &p_buffer[10], 0 );
865     SetQWLE( &p_buffer[20], 1000 );
866     SetQWLE( &p_buffer[36], 1000 );
867     SetQWLE( &p_buffer[64], p_mux->p_sys->i_pos - p_mux->p_sys->i_segment_start ); /* segment length */
868     SetQWLE( &p_buffer[72], p_mux->p_sys->i_data_start - p_mux->p_sys->i_segment_start ); /* data start offset */
869 }
870
871 static int32_t OggFillDsHeader( uint8_t *p_buffer, oggds_header_t *p_oggds_header, int i_cat )
872 {
873     int index = 0;
874     p_buffer[index] = p_oggds_header->i_packet_type;
875     index++;
876     memcpy( &p_buffer[index], p_oggds_header->stream_type, sizeof(p_oggds_header->stream_type) );
877     index += sizeof(p_oggds_header->stream_type);
878     memcpy(&p_buffer[index], p_oggds_header->sub_type, sizeof(p_oggds_header->sub_type) );
879     index += sizeof(p_oggds_header->sub_type);
880
881     /* The size is filled at the end */
882     uint8_t *p_isize = &p_buffer[index];
883     index += 4;
884
885     SetQWLE( &p_buffer[index], p_oggds_header->i_time_unit );
886     index += 8;
887     SetQWLE( &p_buffer[index], p_oggds_header->i_samples_per_unit );
888     index += 8;
889     SetDWLE( &p_buffer[index], p_oggds_header->i_default_len );
890     index += 4;
891     SetDWLE( &p_buffer[index], p_oggds_header->i_buffer_size );
892     index += 4;
893     SetWLE( &p_buffer[index], p_oggds_header->i_bits_per_sample );
894     index += 2;
895     SetWLE( &p_buffer[index], p_oggds_header->i_padding_0 );
896     index += 2;
897     /* audio or video */
898     switch( i_cat )
899     {
900     case VIDEO_ES:
901         SetDWLE( &p_buffer[index], p_oggds_header->header.video.i_width );
902         SetDWLE( &p_buffer[index+4], p_oggds_header->header.video.i_height );
903         break;
904     case AUDIO_ES:
905         SetWLE( &p_buffer[index], p_oggds_header->header.audio.i_channels );
906         SetWLE( &p_buffer[index+2], p_oggds_header->header.audio.i_block_align );
907         SetDWLE( &p_buffer[index+4], p_oggds_header->header.audio.i_avgbytespersec );
908         break;
909     }
910     index += 8;
911     SetDWLE( &p_buffer[index], p_oggds_header->i_padding_1 );
912     index += 4;
913
914     /* extra header */
915     if( p_oggds_header->i_size > 0 )
916     {
917         memcpy( &p_buffer[index], (uint8_t *) p_oggds_header + sizeof(*p_oggds_header), p_oggds_header->i_size );
918         index += p_oggds_header->i_size;
919     }
920
921     SetDWLE( p_isize, index-1 );
922     return index;
923 }
924
925 static void OggFillVP8Header( uint8_t *p_buffer, sout_input_t *p_input )
926 {
927     memcpy( p_buffer, "OVP80\x01\x01\x00", 8 );
928     SetWBE( &p_buffer[8], p_input->p_fmt->video.i_width );
929     SetDWBE( &p_buffer[14], p_input->p_fmt->video.i_sar_den );/* 24 bits, 15~ */
930     SetDWBE( &p_buffer[11], p_input->p_fmt->video.i_sar_num );/* 24 bits, 12~ */
931     SetWBE( &p_buffer[10], p_input->p_fmt->video.i_height );
932     SetDWBE( &p_buffer[18], p_input->p_fmt->video.i_frame_rate );
933     SetDWBE( &p_buffer[22], p_input->p_fmt->video.i_frame_rate_base );
934 }
935
936 static bool OggCreateHeaders( sout_mux_t *p_mux )
937 {
938     block_t *p_hdr = NULL;
939     block_t *p_og = NULL;
940     ogg_packet op;
941     ogg_stream_t *p_stream;
942     sout_mux_sys_t *p_sys = p_mux->p_sys;
943     int i;
944
945     if( sout_AccessOutControl( p_mux->p_access,
946                                ACCESS_OUT_CAN_SEEK,
947                                &p_sys->skeleton.b_create ) )
948     {
949         p_sys->skeleton.b_create = false;
950     }
951
952     p_sys->skeleton.b_create &= !! p_mux->i_nb_inputs;
953
954     /* no skeleton for solo vorbis/speex/opus tracks */
955     if ( p_mux->i_nb_inputs == 1 && p_mux->pp_inputs[0]->p_fmt->i_cat == AUDIO_ES )
956     {
957         p_sys->skeleton.b_create = false;
958     }
959     else
960     {
961         for ( int i=0; i< p_mux->i_nb_inputs; i++ )
962         {
963             p_stream = (ogg_stream_t*) p_mux->pp_inputs[i]->p_sys;
964             if ( p_stream->p_oggds_header )
965             {
966                 /* We don't want skeleton for OggDS */
967                 p_sys->skeleton.b_create = false;
968                 break;
969             }
970         }
971     }
972
973     /* Skeleton's Fishead must be the first page of the stream */
974     if ( p_sys->skeleton.b_create && !p_sys->skeleton.b_head_done )
975     {
976         msg_Dbg( p_mux, "creating header for skeleton" );
977         p_sys->skeleton.i_serial_no = p_sys->i_next_serial_no++;
978         ogg_stream_init( &p_sys->skeleton.os, p_sys->skeleton.i_serial_no );
979         op.bytes = 80;
980         op.packet = calloc( 1, op.bytes );
981         if ( op.packet == NULL ) return false;
982         op.b_o_s = 1;
983         op.e_o_s = 0;
984         op.granulepos = 0;
985         op.packetno = 0;
986         OggFillSkeletonFishead( op.packet, p_mux );
987         ogg_stream_packetin( &p_sys->skeleton.os, &op );
988         p_og = OggStreamFlush( p_mux, &p_sys->skeleton.os, 0 );
989         block_ChainAppend( &p_hdr, p_og );
990         p_sys->skeleton.b_head_done = true;
991         p_sys->skeleton.i_fishead_offset = p_sys->i_pos;
992     }
993
994     /* Write header for each stream. All b_o_s (beginning of stream) packets
995      * must appear first in the ogg stream so we take care of them first. */
996     for( int pass = 0; pass < 2; pass++ )
997     {
998         for( i = 0; i < p_mux->i_nb_inputs; i++ )
999         {
1000             sout_input_t *p_input = p_mux->pp_inputs[i];
1001             p_stream = (ogg_stream_t*)p_input->p_sys;
1002
1003             bool video = ( p_stream->i_fourcc == VLC_CODEC_THEORA ||
1004                            p_stream->i_fourcc == VLC_CODEC_DIRAC ||
1005                            p_stream->i_fourcc == VLC_CODEC_DAALA );
1006             if( ( ( pass == 0 && !video ) || ( pass == 1 && video ) ) )
1007                 continue;
1008
1009             msg_Dbg( p_mux, "creating header for %4.4s",
1010                      (char *)&p_stream->i_fourcc );
1011
1012             ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
1013             p_stream->b_new = false;
1014             p_stream->i_packet_no = 0;
1015             p_stream->b_started = true;
1016
1017             if( p_stream->i_fourcc == VLC_CODEC_VORBIS ||
1018                 p_stream->i_fourcc == VLC_CODEC_SPEEX ||
1019                 p_stream->i_fourcc == VLC_CODEC_OPUS ||
1020                 p_stream->i_fourcc == VLC_CODEC_THEORA ||
1021                 p_stream->i_fourcc == VLC_CODEC_DAALA )
1022             {
1023                 /* First packet in order: vorbis/speex/opus/theora/daala info */
1024                 unsigned pi_size[XIPH_MAX_HEADER_COUNT];
1025                 void     *pp_data[XIPH_MAX_HEADER_COUNT];
1026                 unsigned i_count;
1027                 if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
1028                                        p_input->p_fmt->i_extra, p_input->p_fmt->p_extra ) )
1029                 {
1030                     i_count = 0;
1031                     pi_size[0] = 0;
1032                     pp_data[0] = NULL;
1033                 }
1034
1035                 op.bytes  = pi_size[0];
1036                 op.packet = pp_data[0];
1037                 if( pi_size[0] <= 0 )
1038                     msg_Err( p_mux, "header data corrupted");
1039
1040                 op.b_o_s  = 1;
1041                 op.e_o_s  = 0;
1042                 op.granulepos = 0;
1043                 op.packetno = p_stream->i_packet_no++;
1044                 ogg_stream_packetin( &p_stream->os, &op );
1045                 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
1046
1047                 /* Get keyframe_granule_shift for theora or daala granulepos calculation */
1048                 if( p_stream->i_fourcc == VLC_CODEC_THEORA ||
1049                     p_stream->i_fourcc == VLC_CODEC_DAALA )
1050                 {
1051                     p_stream->i_keyframe_granule_shift =
1052                         ( (op.packet[40] & 0x03) << 3 ) | ( (op.packet[41] & 0xe0) >> 5 );
1053                 }
1054             }
1055             else if( p_stream->i_fourcc == VLC_CODEC_DIRAC )
1056             {
1057                 op.packet = p_input->p_fmt->p_extra;
1058                 op.bytes  = p_input->p_fmt->i_extra;
1059                 op.b_o_s  = 1;
1060                 op.e_o_s  = 0;
1061                 op.granulepos = ~0;
1062                 op.packetno = p_stream->i_packet_no++;
1063                 ogg_stream_packetin( &p_stream->os, &op );
1064                 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
1065             }
1066             else if( p_stream->i_fourcc == VLC_CODEC_FLAC )
1067             {
1068                 /* flac stream marker (yeah, only that in the 1st packet) */
1069                 op.packet = (unsigned char *)"fLaC";
1070                 op.bytes  = 4;
1071                 op.b_o_s  = 1;
1072                 op.e_o_s  = 0;
1073                 op.granulepos = 0;
1074                 op.packetno = p_stream->i_packet_no++;
1075                 ogg_stream_packetin( &p_stream->os, &op );
1076                 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
1077             }
1078             else if( p_stream->i_fourcc == VLC_CODEC_VP8 )
1079             {
1080                 /* VP8 Header */
1081                 op.packet = malloc( 26 );
1082                 if( !op.packet )
1083                     return false;
1084                 op.bytes = 26;
1085                 OggFillVP8Header( op.packet, p_input );
1086                 op.b_o_s = 1;
1087                 op.e_o_s = 0;
1088                 op.granulepos = 0;
1089                 op.packetno = p_stream->i_packet_no++;
1090                 ogg_stream_packetin( &p_stream->os, &op );
1091                 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
1092                 free( op.packet );
1093             }
1094             else if( p_stream->p_oggds_header )
1095             {
1096                 /* ds header */
1097                 op.packet = malloc( sizeof(*p_stream->p_oggds_header) + p_stream->p_oggds_header->i_size );
1098                 if( !op.packet )
1099                     return false;
1100                 op.bytes  = OggFillDsHeader( op.packet, p_stream->p_oggds_header, p_stream->i_cat );
1101                 op.b_o_s  = 1;
1102                 op.e_o_s  = 0;
1103                 op.granulepos = 0;
1104                 op.packetno = p_stream->i_packet_no++;
1105                 ogg_stream_packetin( &p_stream->os, &op );
1106                 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
1107                 free( op.packet );
1108             }
1109
1110             block_ChainAppend( &p_hdr, p_og );
1111         }
1112     }
1113
1114     /* Create fisbones if any */
1115     if ( p_sys->skeleton.b_create )
1116     {
1117         for( i = 0; i < p_mux->i_nb_inputs; i++ )
1118         {
1119             sout_input_t *p_input = p_mux->pp_inputs[i];
1120             ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
1121             if ( p_stream->skeleton.b_fisbone_done ) continue;
1122             OggGetSkeletonFisbone( &op.packet, &op.bytes, p_input, p_mux );
1123             if ( op.packet == NULL ) return false;
1124             op.b_o_s = 0;
1125             op.e_o_s = 0;
1126             op.granulepos = 0;
1127             op.packetno = p_sys->skeleton.i_packet_no++;
1128             ogg_stream_packetin( &p_sys->skeleton.os, &op );
1129             p_og = OggStreamFlush( p_mux, &p_sys->skeleton.os, 0 );
1130             block_ChainAppend( &p_hdr, p_og );
1131             p_stream->skeleton.b_fisbone_done = true;
1132         }
1133     }
1134
1135     /* Write previous headers */
1136     for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
1137     {
1138         /* flag headers to be resent for streaming clients */
1139         p_og->i_flags |= BLOCK_FLAG_HEADER;
1140     }
1141     p_mux->p_sys->i_pos += sout_AccessOutWrite( p_mux->p_access, p_hdr );
1142     p_hdr = NULL;
1143
1144     /* Create indexes if any */
1145     for( i = 0; i < p_mux->i_nb_inputs; i++ )
1146     {
1147         sout_input_t *p_input = p_mux->pp_inputs[i];
1148         ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
1149         /* flush stream && save offset */
1150         if ( p_sys->skeleton.b_create && !p_stream->skeleton.b_index_done )
1151         {
1152             if ( !p_stream->skeleton.p_index ) AllocateIndex( p_mux, p_input );
1153             if ( p_stream->skeleton.p_index )
1154             {
1155                 msg_Dbg( p_mux, "Creating index for stream %d", p_stream->i_serial_no );
1156                 OggGetSkeletonIndex( &op.packet, &op.bytes, p_stream );
1157                 if ( op.packet == NULL ) return false;
1158                 op.b_o_s = 0;
1159                 op.e_o_s = 0;
1160                 op.granulepos = 0;
1161                 op.packetno = p_sys->skeleton.i_packet_no++;
1162
1163                 /* backup some values */
1164                 p_stream->skeleton.i_index_offset = p_mux->p_sys->i_pos;
1165                 p_stream->skeleton.i_index_packetno = p_sys->skeleton.os.packetno;
1166                 p_stream->skeleton.i_index_pageno = p_sys->skeleton.os.pageno;
1167
1168                 ogg_stream_packetin( &p_sys->skeleton.os, &op );
1169                 p_og = OggStreamFlush( p_mux, &p_sys->skeleton.os, 0 );
1170                 p_mux->p_sys->i_pos += sout_AccessOutWrite( p_mux->p_access, p_og );
1171             }
1172             p_stream->skeleton.b_index_done = true;
1173         }
1174     }
1175
1176     /* Take care of the non b_o_s headers */
1177     for( i = 0; i < p_mux->i_nb_inputs; i++ )
1178     {
1179         sout_input_t *p_input = p_mux->pp_inputs[i];
1180         ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
1181
1182         if( p_stream->i_fourcc == VLC_CODEC_VORBIS ||
1183             p_stream->i_fourcc == VLC_CODEC_SPEEX ||
1184             p_stream->i_fourcc == VLC_CODEC_OPUS ||
1185             p_stream->i_fourcc == VLC_CODEC_THEORA ||
1186             p_stream->i_fourcc == VLC_CODEC_DAALA )
1187         {
1188             unsigned pi_size[XIPH_MAX_HEADER_COUNT];
1189             void     *pp_data[XIPH_MAX_HEADER_COUNT];
1190             unsigned i_count;
1191             if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
1192                                    p_input->p_fmt->i_extra, p_input->p_fmt->p_extra ) )
1193                 i_count = 0;
1194
1195             /* Special case, headers are already there in the incoming stream.
1196              * We need to gather them an mark them as headers. */
1197             for( unsigned i = 1; i < i_count; i++ )
1198             {
1199                 op.bytes  = pi_size[i];
1200                 op.packet = pp_data[i];
1201                 if( pi_size[i] <= 0 )
1202                     msg_Err( p_mux, "header data corrupted");
1203
1204                 op.b_o_s  = 0;
1205                 op.e_o_s  = 0;
1206                 op.granulepos = 0;
1207                 op.packetno = p_stream->i_packet_no++;
1208                 ogg_stream_packetin( &p_stream->os, &op );
1209                 msg_Dbg( p_mux, "adding non bos, secondary header" );
1210                 if( i == i_count - 1 )
1211                     p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
1212                 else
1213                     p_og = OggStreamPageOut( p_mux, &p_stream->os, 0 );
1214                 if( p_og )
1215                     block_ChainAppend( &p_hdr, p_og );
1216             }
1217         }
1218         else if( p_stream->i_fourcc != VLC_CODEC_FLAC &&
1219                  p_stream->i_fourcc != VLC_CODEC_DIRAC )
1220         {
1221             uint8_t com[128];
1222             int     i_com;
1223
1224             /* comment */
1225             com[0] = PACKET_TYPE_COMMENT;
1226             i_com = snprintf( (char *)(com+1), 127,
1227                               PACKAGE_VERSION" stream output" )
1228                      + 1;
1229             op.packet = com;
1230             op.bytes  = i_com;
1231             op.b_o_s  = 0;
1232             op.e_o_s  = 0;
1233             op.granulepos = 0;
1234             op.packetno = p_stream->i_packet_no++;
1235             ogg_stream_packetin( &p_stream->os, &op );
1236             p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
1237             block_ChainAppend( &p_hdr, p_og );
1238         }
1239
1240         /* Special case for mp4v and flac */
1241         if( ( p_stream->i_fourcc == VLC_CODEC_MP4V ||
1242               p_stream->i_fourcc == VLC_CODEC_FLAC ) &&
1243             p_input->p_fmt->i_extra )
1244         {
1245             /* Send a packet with the VOL data for mp4v
1246              * or STREAMINFO for flac */
1247             msg_Dbg( p_mux, "writing extra data" );
1248             op.bytes  = p_input->p_fmt->i_extra;
1249             op.packet = p_input->p_fmt->p_extra;
1250             uint8_t flac_streaminfo[34 + 4];
1251             if( p_stream->i_fourcc == VLC_CODEC_FLAC )
1252             {
1253                 if (op.bytes == 42 && !memcmp(op.packet, "fLaC", 4)) {
1254                     op.bytes -= 4;
1255                     memcpy(flac_streaminfo, op.packet + 4, 38);
1256                     op.packet = flac_streaminfo;
1257                 } else if (op.bytes == 34) {
1258                     op.bytes += 4;
1259                     memcpy(flac_streaminfo + 4, op.packet, 34);
1260                     flac_streaminfo[0] = 0x80; /* last block, streaminfo */
1261                     flac_streaminfo[1] = 0;
1262                     flac_streaminfo[2] = 0;
1263                     flac_streaminfo[3] = 34; /* block size */
1264                     op.packet = flac_streaminfo;
1265                 } else {
1266                     msg_Err(p_mux, "Invalid FLAC streaminfo (%ld bytes)",
1267                             op.bytes);
1268                 }
1269             }
1270             op.b_o_s  = 0;
1271             op.e_o_s  = 0;
1272             op.granulepos = 0;
1273             op.packetno = p_stream->i_packet_no++;
1274             ogg_stream_packetin( &p_stream->os, &op );
1275             p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
1276             block_ChainAppend( &p_hdr, p_og );
1277         }
1278     }
1279
1280     if ( p_sys->skeleton.b_create )
1281     {
1282         msg_Dbg( p_mux, "ending skeleton" );
1283         op.packet = NULL;
1284         op.bytes = 0;
1285         op.b_o_s = 0;
1286         op.e_o_s = 1;
1287         op.granulepos = 0;
1288         op.packetno = p_sys->skeleton.i_packet_no++;
1289         ogg_stream_packetin( &p_sys->skeleton.os, &op );
1290         p_og = OggStreamFlush( p_mux, &p_sys->skeleton.os, 0 );
1291         block_ChainAppend( &p_hdr, p_og );
1292     }
1293
1294     /* set HEADER flag */
1295     /* flag headers to be resent for streaming clients */
1296     for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
1297     {
1298         p_og->i_flags |= BLOCK_FLAG_HEADER;
1299     }
1300
1301     /* Write previous headers */
1302     p_mux->p_sys->i_pos += sout_AccessOutWrite( p_mux->p_access, p_hdr );
1303
1304     return true;
1305 }
1306
1307 static void OggCreateStreamFooter( sout_mux_t *p_mux, ogg_stream_t *p_stream )
1308 {
1309     block_t *p_og;
1310     ogg_packet op;
1311     sout_mux_sys_t *p_sys = p_mux->p_sys;
1312
1313     /* as stream is finished, overwrite the index, if there was any */
1314     if ( p_sys->skeleton.b_create && p_stream->skeleton.p_index
1315          && p_stream->skeleton.i_index_payload )
1316     {
1317         sout_AccessOutSeek( p_mux->p_access, p_stream->skeleton.i_index_offset );
1318         OggGetSkeletonIndex( &op.packet, &op.bytes, p_stream );
1319         if ( op.packet != NULL )
1320         {
1321             msg_Dbg(p_mux, "Rewriting index at %"PRId64, p_stream->skeleton.i_index_offset );
1322             ogg_stream_reset_serialno( &p_sys->skeleton.os, p_sys->skeleton.i_serial_no );
1323             op.b_o_s = 0;
1324             op.e_o_s = 0;
1325             op.granulepos = 0;
1326             op.packetno = p_stream->skeleton.i_index_packetno + 1;
1327             /* fake our stream state */
1328             p_sys->skeleton.os.pageno = p_stream->skeleton.i_index_pageno;
1329             p_sys->skeleton.os.packetno = p_stream->skeleton.i_index_packetno;
1330             p_sys->skeleton.os.granulepos = 0;
1331             p_sys->skeleton.os.b_o_s = 1;
1332             p_sys->skeleton.os.e_o_s = 0;
1333             ogg_stream_packetin( &p_sys->skeleton.os, &op );
1334             p_og = OggStreamFlush( p_mux, &p_sys->skeleton.os, 0 );
1335             sout_AccessOutWrite( p_mux->p_access, p_og );
1336         }
1337         sout_AccessOutSeek( p_mux->p_access, p_sys->i_pos );
1338     }
1339
1340     /* clear skeleton */
1341     p_stream->skeleton.b_fisbone_done = false;
1342     p_stream->skeleton.b_index_done = false;
1343     p_stream->skeleton.i_index_offset = 0;
1344     p_stream->skeleton.i_index_payload = 0;
1345     p_stream->skeleton.i_last_keyframe_pos = 0;
1346     p_stream->skeleton.i_last_keyframe_time = 0;
1347     /* clear accounting */
1348     p_stream->i_num_frames = 0;
1349     p_stream->i_num_keyframes = 0;
1350
1351     /* Write eos packet for stream. */
1352     op.packet = NULL;
1353     op.bytes  = 0;
1354     op.b_o_s  = 0;
1355     op.e_o_s  = 1;
1356     op.granulepos = p_stream->u_last_granulepos;
1357     op.packetno = p_stream->i_packet_no++;
1358     ogg_stream_packetin( &p_stream->os, &op );
1359
1360     /* flush it with all remaining data */
1361     if( ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
1362     {
1363         /* Write footer */
1364         OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
1365         p_mux->p_sys->i_pos += sout_AccessOutWrite( p_mux->p_access, p_og );
1366     }
1367
1368     ogg_stream_clear( &p_stream->os );
1369 }
1370
1371 static void OggSetDate( block_t *p_og, mtime_t i_dts, mtime_t i_length )
1372 {
1373     int i_count;
1374     block_t *p_tmp;
1375     mtime_t i_delta;
1376
1377     for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
1378     {
1379         i_count++;
1380     }
1381
1382     if( i_count == 0 ) return; /* ignore. */
1383
1384     i_delta = i_length / i_count;
1385
1386     for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
1387     {
1388         p_tmp->i_dts    = i_dts;
1389         p_tmp->i_length = i_delta;
1390
1391         i_dts += i_delta;
1392     }
1393 }
1394
1395 static void OggRewriteFisheadPage( sout_mux_t *p_mux )
1396 {
1397     sout_mux_sys_t *p_sys = p_mux->p_sys;
1398     ogg_packet op;
1399     op.bytes = 80;
1400     op.packet = calloc( 1, op.bytes );
1401     if ( op.packet != NULL )
1402     {
1403         op.b_o_s = 1;
1404         op.e_o_s = 0;
1405         op.granulepos = 0;
1406         op.packetno = 0;
1407         ogg_stream_reset_serialno( &p_sys->skeleton.os, p_sys->skeleton.i_serial_no );
1408         OggFillSkeletonFishead( op.packet, p_mux );
1409         ogg_stream_packetin( &p_sys->skeleton.os, &op );
1410         msg_Dbg( p_mux, "rewriting fishead at %"PRId64, p_mux->p_sys->skeleton.i_fishead_offset );
1411         sout_AccessOutSeek( p_mux->p_access, p_mux->p_sys->skeleton.i_fishead_offset );
1412         sout_AccessOutWrite( p_mux->p_access,
1413                              OggStreamFlush( p_mux, &p_sys->skeleton.os, 0 ) );
1414         sout_AccessOutSeek( p_mux->p_access, p_mux->p_sys->i_pos );
1415     }
1416 }
1417
1418 static bool AllocateIndex( sout_mux_t *p_mux, sout_input_t *p_input )
1419 {
1420     ogg_stream_t *p_stream = (ogg_stream_t *) p_input->p_sys;
1421     size_t i_size;
1422
1423     if ( p_stream->i_length )
1424     {
1425         uint64_t i_interval = (uint64_t)p_mux->p_sys->skeleton.i_index_intvl * 1000;
1426         uint64_t i;
1427
1428         if( p_input->p_fmt->i_cat == VIDEO_ES &&
1429                 p_input->p_fmt->video.i_frame_rate )
1430         {
1431             /* optimize for fps < 1 */
1432             i_interval= __MAX( p_mux->p_sys->skeleton.i_index_intvl * 1000,
1433                        INT64_C(10000000) *
1434                        p_input->p_fmt->video.i_frame_rate_base /
1435                        p_input->p_fmt->video.i_frame_rate );
1436         }
1437
1438         size_t i_tuple_size = 0;
1439         /* estimate length of pos value */
1440         if ( p_input->p_fmt->i_bitrate )
1441         {
1442             i = i_interval * p_input->p_fmt->i_bitrate / 1000000;
1443             while ( i <<= 1 ) i_tuple_size++;
1444         }
1445         else
1446         {
1447             /* Likely 64KB<<keyframe interval<<16MB */
1448             /* We can't really guess due to muxing */
1449             i_tuple_size = 24 / 8;
1450         }
1451
1452         /* add length of interval value */
1453         i = i_interval;
1454         while ( i <<= 1 ) i_tuple_size++;
1455
1456         i_size = i_tuple_size * ( p_stream->i_length / i_interval + 2 );
1457     }
1458     else
1459     {
1460         i_size = ( INT64_C(3600) * 11.2 * 1000 / p_mux->p_sys->skeleton.i_index_intvl )
1461                 * p_mux->p_sys->skeleton.i_index_ratio;
1462         msg_Dbg( p_mux, "No stream length, using default allocation for index" );
1463     }
1464     i_size *= ( 8.0 / 7 ); /* 7bits encoding overhead */
1465     msg_Dbg( p_mux, "allocating %zu bytes for index", i_size );
1466     p_stream->skeleton.p_index = calloc( i_size, sizeof(uint8_t) );
1467     if ( !p_stream->skeleton.p_index ) return false;
1468     p_stream->skeleton.i_index_size = i_size;
1469     p_stream->skeleton.i_index_payload = 0;
1470     return true;
1471 }
1472
1473 /*****************************************************************************
1474  * Mux: multiplex available data in input fifos into the Ogg bitstream
1475  *****************************************************************************/
1476 static int Mux( sout_mux_t *p_mux )
1477 {
1478     sout_mux_sys_t *p_sys = p_mux->p_sys;
1479     mtime_t        i_dts;
1480
1481     /* End any stream that ends in that group */
1482     if ( p_sys->i_del_streams )
1483     {
1484         /* Remove deleted logical streams */
1485         for( int i = 0; i < p_sys->i_del_streams; i++ )
1486         {
1487             OggCreateStreamFooter( p_mux, p_sys->pp_del_streams[i] );
1488             FREENULL( p_sys->pp_del_streams[i]->p_oggds_header );
1489             FREENULL( p_sys->pp_del_streams[i] );
1490         }
1491         FREENULL( p_sys->pp_del_streams );
1492         p_sys->i_del_streams = 0;
1493     }
1494
1495     if ( p_sys->i_streams == 0 )
1496     {
1497         /* All streams have been deleted, or none have ever been created
1498            From this point, we are allowed to start a new group of logical streams */
1499         p_sys->skeleton.b_head_done = false;
1500         p_sys->b_can_add_streams = true;
1501         p_sys->i_segment_start = p_sys->i_pos;
1502     }
1503
1504     if ( p_sys->i_add_streams )
1505     {
1506         if ( !p_sys->b_can_add_streams )
1507         {
1508             msg_Warn( p_mux, "Can't add new stream %d/%d: Considerer increasing sout-mux-caching variable", p_sys->i_del_streams, p_mux->p_sys->i_streams);
1509             msg_Warn( p_mux, "Resetting and setting new identity to current streams");
1510
1511             /* resetting all active streams */
1512             for ( int i=0; i < p_mux->p_sys->i_streams; i++ )
1513             {
1514                 ogg_stream_t * p_stream = (ogg_stream_t *) p_mux->pp_inputs[i]->p_sys;
1515                 if ( p_stream->b_finished || !p_stream->b_started ) continue;
1516                 OggCreateStreamFooter( p_mux, p_stream );
1517                 p_stream->i_serial_no = p_sys->i_next_serial_no++;
1518                 p_stream->i_packet_no = 0;
1519                 p_stream->b_finished = true;
1520             }
1521
1522             /* rewrite fishead with final values */
1523             if ( p_sys->skeleton.b_head_done )
1524             {
1525                 OggRewriteFisheadPage( p_mux );
1526             }
1527
1528             p_sys->b_can_add_streams = true;
1529             p_sys->skeleton.b_head_done = false;
1530             p_sys->i_segment_start = p_sys->i_pos;
1531         }
1532
1533         /* Open new ogg stream */
1534         if( sout_MuxGetStream( p_mux, 1, &i_dts) < 0 )
1535         {
1536             msg_Dbg( p_mux, "waiting for data..." );
1537             return VLC_SUCCESS;
1538         }
1539         msg_Dbg( p_mux, "writing streams headers" );
1540         p_sys->i_start_dts = i_dts;
1541         p_sys->i_streams = p_mux->i_nb_inputs;
1542         p_sys->i_del_streams = 0;
1543         p_sys->i_add_streams = 0;
1544         p_sys->skeleton.b_create = true;
1545
1546         if ( ! OggCreateHeaders( p_mux ) )
1547             return VLC_ENOMEM;
1548
1549         /* If we're switching to end of headers, then that's data start */
1550         if ( p_sys->b_can_add_streams )
1551         {
1552             msg_Dbg( p_mux, "data starts from %zu", p_sys->i_pos );
1553             p_sys->i_data_start = p_sys->i_pos;
1554         }
1555
1556         /* Since we started sending secondaryheader or data pages,
1557              * we're no longer allowed to create new streams, until all streams end */
1558         p_sys->b_can_add_streams = false;
1559     }
1560
1561     /* Do the regular data mux thing */
1562     for( ;; )
1563     {
1564         int i_stream = sout_MuxGetStream( p_mux, 1, NULL );
1565         if( i_stream < 0 )
1566             return VLC_SUCCESS;
1567         MuxBlock( p_mux, p_mux->pp_inputs[i_stream] );
1568     }
1569
1570     return VLC_SUCCESS;
1571 }
1572
1573 static int MuxBlock( sout_mux_t *p_mux, sout_input_t *p_input )
1574 {
1575     sout_mux_sys_t *p_sys = p_mux->p_sys;
1576     ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
1577     block_t *p_data = block_FifoGet( p_input->p_fifo );
1578     block_t *p_og = NULL;
1579     ogg_packet op;
1580     uint64_t i_time;
1581
1582     if( p_stream->i_fourcc != VLC_CODEC_VORBIS &&
1583         p_stream->i_fourcc != VLC_CODEC_FLAC &&
1584         p_stream->i_fourcc != VLC_CODEC_SPEEX &&
1585         p_stream->i_fourcc != VLC_CODEC_OPUS &&
1586         p_stream->i_fourcc != VLC_CODEC_THEORA &&
1587         p_stream->i_fourcc != VLC_CODEC_DAALA &&
1588         p_stream->i_fourcc != VLC_CODEC_VP8 &&
1589         p_stream->i_fourcc != VLC_CODEC_DIRAC )
1590     {
1591         p_data = block_Realloc( p_data, 1, p_data->i_buffer );
1592         p_data->p_buffer[0] = PACKET_IS_SYNCPOINT;      // FIXME
1593     }
1594
1595     if ( p_stream->i_fourcc == VLC_CODEC_DIRAC && p_stream->i_baseptsdelay < 0 )
1596         p_stream->i_baseptsdelay = p_data->i_pts - p_data->i_dts;
1597
1598     op.packet   = p_data->p_buffer;
1599     op.bytes    = p_data->i_buffer;
1600     op.b_o_s    = 0;
1601     op.e_o_s    = 0;
1602     op.packetno = p_stream->i_packet_no++;
1603     op.granulepos = -1;
1604
1605     if( p_stream->i_cat == AUDIO_ES )
1606     {
1607         if( p_stream->i_fourcc == VLC_CODEC_VORBIS ||
1608             p_stream->i_fourcc == VLC_CODEC_FLAC ||
1609             p_stream->i_fourcc == VLC_CODEC_OPUS ||
1610             p_stream->i_fourcc == VLC_CODEC_SPEEX )
1611         {
1612             /* number of sample from begining + current packet */
1613             op.granulepos =
1614                 ( p_data->i_dts - p_sys->i_start_dts + p_data->i_length ) *
1615                 (mtime_t)p_input->p_fmt->audio.i_rate / CLOCK_FREQ;
1616
1617             i_time = p_data->i_dts - p_sys->i_start_dts;
1618             AddIndexEntry( p_mux, i_time, p_input );
1619         }
1620         else if( p_stream->p_oggds_header )
1621         {
1622             /* number of sample from begining */
1623             op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) *
1624                 p_stream->p_oggds_header->i_samples_per_unit / CLOCK_FREQ;
1625         }
1626     }
1627     else if( p_stream->i_cat == VIDEO_ES )
1628     {
1629         if( p_stream->i_fourcc == VLC_CODEC_THEORA ||
1630             p_stream->i_fourcc == VLC_CODEC_DAALA )
1631         {
1632             p_stream->i_num_frames++;
1633             if( p_data->i_flags & BLOCK_FLAG_TYPE_I )
1634             {
1635                 p_stream->i_num_keyframes++;
1636                 p_stream->i_last_keyframe = p_stream->i_num_frames;
1637
1638                 /* presentation time */
1639                 i_time = CLOCK_FREQ * ( p_stream->i_num_frames - 1 ) *
1640                          p_input->p_fmt->video.i_frame_rate_base /  p_input->p_fmt->video.i_frame_rate;
1641                 AddIndexEntry( p_mux, i_time, p_input );
1642             }
1643
1644             op.granulepos = (p_stream->i_last_keyframe << p_stream->i_keyframe_granule_shift )
1645                           | (p_stream->i_num_frames-p_stream->i_last_keyframe);
1646         }
1647         else if( p_stream->i_fourcc == VLC_CODEC_DIRAC )
1648         {
1649
1650 #define FRAME_ROUND(a) \
1651     if ( ( a + 5000 / CLOCK_FREQ ) > ( a / CLOCK_FREQ ) )\
1652         a += 5000;\
1653     a /= CLOCK_FREQ;
1654
1655             mtime_t dt = (p_data->i_dts - p_sys->i_start_dts) * p_input->p_fmt->video.i_frame_rate / p_input->p_fmt->video.i_frame_rate_base;
1656             FRAME_ROUND( dt );
1657
1658             mtime_t pt = (p_data->i_pts - p_sys->i_start_dts - p_stream->i_baseptsdelay ) * p_input->p_fmt->video.i_frame_rate / p_input->p_fmt->video.i_frame_rate_base;
1659             FRAME_ROUND( pt );
1660
1661             /* (shro) some PTS could be repeated within 1st frames */
1662             if ( pt == p_stream->i_dirac_last_pt )
1663                 pt++;
1664             else
1665                 p_stream->i_dirac_last_pt = pt;
1666
1667             /* (shro) some DTS could be repeated within 1st frames */
1668             if ( dt == p_stream->i_dirac_last_dt )
1669                 dt++;
1670             else
1671                 p_stream->i_dirac_last_dt = dt;
1672
1673             if( p_data->i_flags & BLOCK_FLAG_TYPE_I )
1674                 p_stream->i_last_keyframe = dt;
1675             mtime_t dist = dt - p_stream->i_last_keyframe;
1676
1677             /* Everything increments by two for progressive */
1678             if ( true )
1679             {
1680                 pt *=2;
1681                 dt *=2;
1682             }
1683
1684             mtime_t delay = pt - dt;
1685             if ( delay < 0 ) delay *= -1;
1686
1687             op.granulepos = (pt - delay) << 31 | (dist&0xff00) << 14
1688                           | (delay&0x1fff) << 9 | (dist&0xff);
1689 #ifndef NDEBUG
1690             msg_Dbg( p_mux, "dts %"PRId64" pts %"PRId64" dt %"PRId64" pt %"PRId64" delay %"PRId64" granule %"PRId64,
1691                      (p_data->i_dts - p_sys->i_start_dts),
1692                      (p_data->i_pts - p_sys->i_start_dts ),
1693                      dt, pt, delay, op.granulepos );
1694 #endif
1695
1696             AddIndexEntry( p_mux, dt, p_input );
1697         }
1698         else if( p_stream->i_fourcc == VLC_CODEC_VP8 )
1699         {
1700             p_stream->i_num_frames++;
1701             if( p_data->i_flags & BLOCK_FLAG_TYPE_I )
1702             {
1703                 p_stream->i_num_keyframes++;
1704                 p_stream->i_last_keyframe = p_stream->i_num_frames;
1705
1706                 /* presentation time */
1707                 i_time = CLOCK_FREQ * ( p_stream->i_num_frames - 1 ) *
1708                          p_input->p_fmt->video.i_frame_rate_base /  p_input->p_fmt->video.i_frame_rate;
1709                 AddIndexEntry( p_mux, i_time, p_input );
1710             }
1711             op.granulepos = ( ((int64_t)p_stream->i_num_frames) << 32 ) |
1712             ( ( ( p_stream->i_num_frames - p_stream->i_last_keyframe ) & 0x07FFFFFF ) << 3 );
1713         }
1714         else if( p_stream->p_oggds_header )
1715             op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) * INT64_C(10) /
1716                 p_stream->p_oggds_header->i_time_unit;
1717     }
1718     else if( p_stream->i_cat == SPU_ES )
1719     {
1720         /* granulepos is in millisec */
1721         op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) / 1000;
1722     }
1723     else
1724         return VLC_EGENERIC;
1725
1726     p_stream->u_last_granulepos = op.granulepos;
1727     ogg_stream_packetin( &p_stream->os, &op );
1728
1729     if( p_stream->i_cat == SPU_ES ||
1730         p_stream->i_fourcc == VLC_CODEC_SPEEX ||
1731         p_stream->i_fourcc == VLC_CODEC_DIRAC )
1732     {
1733         /* Subtitles or Speex packets are quite small so they
1734          * need to be flushed to be sent on time */
1735         /* The OggDirac mapping suggests ever so strongly that a
1736          * page flush occurs after each OggDirac packet, so to make
1737          * the timestamps unambiguous */
1738         p_og = OggStreamFlush( p_mux, &p_stream->os, p_data->i_dts );
1739     }
1740     else
1741     {
1742         p_og = OggStreamPageOut( p_mux, &p_stream->os, p_data->i_dts );
1743     }
1744
1745     if( p_og )
1746     {
1747         OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
1748         p_stream->i_dts = -1;
1749         p_stream->i_length = 0;
1750         p_mux->p_sys->i_pos += sout_AccessOutWrite( p_mux->p_access, p_og );
1751     }
1752     else
1753     {
1754         if( p_stream->i_dts < 0 )
1755         {
1756             p_stream->i_dts = p_data->i_dts;
1757         }
1758         p_stream->i_length += p_data->i_length;
1759     }
1760
1761     block_Release( p_data );
1762     return VLC_SUCCESS;
1763 }