]> git.sesse.net Git - vlc/blob - modules/mux/ogg.c
* commit modules/mux/ogg.c: added SNOW fourcc.
[vlc] / modules / mux / ogg.c
1 /*****************************************************************************
2  * ogg.c: ogg muxer module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 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
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 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 General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29 #include <string.h>
30
31 #ifdef HAVE_TIME_H
32 #   include <time.h>
33 #endif
34
35 #include <vlc/vlc.h>
36 #include <vlc/input.h>
37 #include <vlc/sout.h>
38
39 #include "codecs.h"
40
41 #include <ogg/ogg.h>
42
43 /*****************************************************************************
44  * Module descriptor
45  *****************************************************************************/
46 static int  Open   ( vlc_object_t * );
47 static void Close  ( vlc_object_t * );
48
49 vlc_module_begin();
50     set_description( _("Ogg/ogm muxer") );
51     set_capability( "sout mux", 10 );
52     add_shortcut( "ogg" );
53     add_shortcut( "ogm" );
54     set_callbacks( Open, Close );
55 vlc_module_end();
56
57
58 /*****************************************************************************
59  * Exported prototypes
60  *****************************************************************************/
61 static int Control  ( sout_mux_t *, int, va_list );
62 static int AddStream( sout_mux_t *, sout_input_t * );
63 static int DelStream( sout_mux_t *, sout_input_t * );
64 static int Mux      ( sout_mux_t * );
65
66 static block_t *OggCreateHeader( sout_mux_t *, mtime_t );
67 static block_t *OggCreateFooter( sout_mux_t *, mtime_t );
68
69 /*****************************************************************************
70  * Misc declarations
71  *****************************************************************************/
72 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
73
74 /* Structures used for OggDS headers used in ogm files */
75
76 #define PACKET_TYPE_HEADER   0x01
77 #define PACKET_TYPE_COMMENT  0x03
78 #define PACKET_IS_SYNCPOINT  0x08
79
80 typedef struct
81 #ifdef HAVE_ATTRIBUTE_PACKED
82     __attribute__((__packed__))
83 #endif
84 {
85     int32_t i_width;
86     int32_t i_height;
87 } oggds_header_video_t;
88
89 typedef struct
90 #ifdef HAVE_ATTRIBUTE_PACKED
91     __attribute__((__packed__))
92 #endif
93 {
94     int16_t i_channels;
95     int16_t i_block_align;
96     int32_t i_avgbytespersec;
97 } oggds_header_audio_t;
98
99 typedef struct
100 #ifdef HAVE_ATTRIBUTE_PACKED
101     __attribute__((__packed__))
102 #endif
103 {
104     uint8_t i_packet_type;
105
106     char stream_type[8];
107     char sub_type[4];
108
109     int32_t i_size;
110
111     int64_t i_time_unit;
112     int64_t i_samples_per_unit;
113     int32_t i_default_len;
114
115     int32_t i_buffer_size;
116     int16_t i_bits_per_sample;
117
118     int16_t i_padding_0; /* Because the original is using MSVC packing style */
119
120     union
121     {
122         oggds_header_video_t video;
123         oggds_header_audio_t audio;
124     } header;
125
126     int32_t i_padding_1; /* Because the original is using MSVC packing style */
127
128 } oggds_header_t;
129
130 /*
131  * TODO  move this function to src/stream_output.c (used by nearly all muxers)
132  */
133 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
134 {
135     mtime_t i_dts;
136     int     i_stream;
137     int     i;
138
139     for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
140     {
141         block_fifo_t  *p_fifo;
142
143         p_fifo = p_mux->pp_inputs[i]->p_fifo;
144
145         /* We don't really need to have anything in the SPU fifo */
146         if( p_mux->pp_inputs[i]->p_fmt->i_cat == SPU_ES &&
147             p_fifo->i_depth == 0 ) continue;
148
149         if( p_fifo->i_depth )
150         {
151             block_t *p_buf;
152
153             p_buf = block_FifoShow( p_fifo );
154             if( i_stream < 0 || p_buf->i_dts < i_dts )
155             {
156                 i_dts = p_buf->i_dts;
157                 i_stream = i;
158             }
159         }
160         else return -1;
161
162     }
163     if( pi_stream )
164     {
165         *pi_stream = i_stream;
166     }
167     if( pi_dts )
168     {
169         *pi_dts = i_dts;
170     }
171     return i_stream;
172 }
173
174 /*****************************************************************************
175  * Definitions of structures and functions used by this plugins 
176  *****************************************************************************/
177 typedef struct
178 {
179     int i_cat;
180     int i_fourcc;
181
182     int b_new;
183
184     mtime_t i_dts;
185     mtime_t i_length;
186     int     i_packet_no;
187     int     i_serial_no;
188     int     i_keyframe_granule_shift; /* Theora only */
189     ogg_stream_state os;
190
191     oggds_header_t *p_oggds_header;
192
193 } ogg_stream_t;
194
195 struct sout_mux_sys_t
196 {
197     int     i_streams;
198
199     mtime_t i_start_dts;
200     int     i_next_serial_no;
201
202     /* number of logical streams pending to be added */
203     int i_add_streams;
204
205     /* logical streams pending to be deleted */
206     int i_del_streams;
207     ogg_stream_t **pp_del_streams;
208 };
209
210 static void OggSetDate( block_t *, mtime_t , mtime_t  );
211 static block_t *OggStreamFlush( sout_mux_t *, ogg_stream_state *, mtime_t );
212
213 /*****************************************************************************
214  * Open: Open muxer
215  *****************************************************************************/
216 static int Open( vlc_object_t *p_this )
217 {
218     sout_mux_t      *p_mux = (sout_mux_t*)p_this;
219     sout_mux_sys_t  *p_sys;
220
221     msg_Info( p_mux, "Open" );
222
223     p_sys                 = malloc( sizeof( sout_mux_sys_t ) );
224     p_sys->i_streams      = 0;
225     p_sys->i_add_streams  = 0;
226     p_sys->i_del_streams  = 0;
227     p_sys->pp_del_streams = 0;
228
229     p_mux->p_sys        = p_sys;
230     p_mux->pf_control   = Control;
231     p_mux->pf_addstream = AddStream;
232     p_mux->pf_delstream = DelStream;
233     p_mux->pf_mux       = Mux;
234
235     /* First serial number is random.
236      * (Done like this because on win32 you need to seed the random number
237      *  generator once per thread). */
238     srand( (unsigned int)time( NULL ) );
239     p_sys->i_next_serial_no = rand();
240
241     return VLC_SUCCESS;
242 }
243
244 /*****************************************************************************
245  * Close: Finalize ogg bitstream and close muxer
246  *****************************************************************************/
247 static void Close( vlc_object_t * p_this )
248 {
249     sout_mux_t     *p_mux = (sout_mux_t*)p_this;
250     sout_mux_sys_t *p_sys = p_mux->p_sys;
251
252     msg_Info( p_mux, "Close" );
253
254     if( p_sys->i_del_streams )
255     {
256         block_t *p_og = NULL;
257         mtime_t i_dts = -1;
258         int i;
259
260         /* Close the current ogg stream */
261         msg_Dbg( p_mux, "writing footer" );
262         block_ChainAppend( &p_og, OggCreateFooter( p_mux, 0 ) );
263
264         /* Remove deleted logical streams */
265         for( i = 0; i < p_sys->i_del_streams; i++ )
266         {
267             i_dts = p_sys->pp_del_streams[i]->i_dts;
268             ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
269             FREE( p_sys->pp_del_streams[i]->p_oggds_header );
270             FREE( p_sys->pp_del_streams[i] );
271         }
272         FREE( p_sys->pp_del_streams );
273         p_sys->i_streams -= p_sys->i_del_streams;
274
275         /* Write footer */
276         OggSetDate( p_og, i_dts, 0 );
277         sout_AccessOutWrite( p_mux->p_access, p_og );
278     }
279
280     free( p_sys );
281 }
282
283 /*****************************************************************************
284  * Control:
285  *****************************************************************************/
286 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
287 {
288     vlc_bool_t *pb_bool;
289     char **ppsz;
290
291    switch( i_query )
292    {
293        case MUX_CAN_ADD_STREAM_WHILE_MUXING:
294            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
295            *pb_bool = VLC_TRUE;
296            return VLC_SUCCESS;
297
298        case MUX_GET_ADD_STREAM_WAIT:
299            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
300            *pb_bool = VLC_TRUE;
301            return VLC_SUCCESS;
302
303        case MUX_GET_MIME:
304            ppsz = (char**)va_arg( args, char ** );
305            *ppsz = strdup( "application/ogg" );
306            return VLC_SUCCESS;
307
308         default:
309             return VLC_EGENERIC;
310    }
311 }
312 /*****************************************************************************
313  * AddStream: Add an elementary stream to the muxed stream
314  *****************************************************************************/
315 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
316 {
317     sout_mux_sys_t *p_sys = p_mux->p_sys;
318     ogg_stream_t   *p_stream;
319     uint16_t i_tag;
320
321     msg_Dbg( p_mux, "adding input" );
322
323     p_input->p_sys = p_stream = malloc( sizeof( ogg_stream_t ) );
324
325     p_stream->i_cat       = p_input->p_fmt->i_cat;
326     p_stream->i_fourcc    = p_input->p_fmt->i_codec;
327     p_stream->i_serial_no = p_sys->i_next_serial_no++;
328     p_stream->i_packet_no = 0;
329
330     p_stream->p_oggds_header = 0;
331
332     switch( p_input->p_fmt->i_cat )
333     {
334     case VIDEO_ES:
335         if( !p_input->p_fmt->video.i_frame_rate ||
336             !p_input->p_fmt->video.i_frame_rate_base )
337         {
338             msg_Warn( p_mux, "Missing frame rate, assuming 25fps" );
339             p_input->p_fmt->video.i_frame_rate = 25;
340             p_input->p_fmt->video.i_frame_rate_base = 1;
341         }
342
343         switch( p_stream->i_fourcc )
344         {
345         case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
346         case VLC_FOURCC( 'm', 'p', '4', 'v' ):
347         case VLC_FOURCC( 'D', 'I', 'V', '3' ):
348         case VLC_FOURCC( 'M', 'J', 'P', 'G' ):
349         case VLC_FOURCC( 'W', 'M', 'V', '1' ):
350         case VLC_FOURCC( 'W', 'M', 'V', '2' ):
351         case VLC_FOURCC( 'W', 'M', 'V', '3' ):
352         case VLC_FOURCC( 'S', 'N', 'O', 'W' ):
353             p_stream->p_oggds_header = malloc( sizeof(oggds_header_t) );
354             memset( p_stream->p_oggds_header, 0, sizeof(oggds_header_t) );
355             p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
356
357             memcpy( p_stream->p_oggds_header->stream_type, "video", 5 );
358             if( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p', '4', 'v' ) )
359             {
360                 memcpy( p_stream->p_oggds_header->sub_type, "XVID", 4 );
361             }
362             else if( p_stream->i_fourcc == VLC_FOURCC( 'D', 'I', 'V', '3' ) )
363             {
364                 memcpy( p_stream->p_oggds_header->sub_type, "DIV3", 4 );
365             }
366             else
367             {
368                 memcpy( p_stream->p_oggds_header->sub_type,
369                         &p_stream->i_fourcc, 4 );
370             }
371             SetDWLE( &p_stream->p_oggds_header->i_size,
372                      sizeof( oggds_header_t ) - 1 );
373             SetQWLE( &p_stream->p_oggds_header->i_time_unit,
374                      I64C(10000000) * p_input->p_fmt->video.i_frame_rate_base /
375                      (int64_t)p_input->p_fmt->video.i_frame_rate );
376             SetQWLE( &p_stream->p_oggds_header->i_samples_per_unit, 1 );
377             SetDWLE( &p_stream->p_oggds_header->i_default_len, 1 ); /* ??? */
378             SetDWLE( &p_stream->p_oggds_header->i_buffer_size, 1024*1024 );
379             SetWLE( &p_stream->p_oggds_header->i_bits_per_sample, 0 );
380             SetDWLE( &p_stream->p_oggds_header->header.video.i_width,
381                      p_input->p_fmt->video.i_width );
382             SetDWLE( &p_stream->p_oggds_header->header.video.i_height,
383                      p_input->p_fmt->video.i_height );
384             msg_Dbg( p_mux, "%4.4s stream", (char *)&p_stream->i_fourcc );
385             break;
386
387         case VLC_FOURCC( 't', 'h', 'e', 'o' ):
388             msg_Dbg( p_mux, "theora stream" );
389             break;
390
391         default:
392             FREE( p_input->p_sys );
393             return VLC_EGENERIC;
394         }
395         break;
396
397     case AUDIO_ES:
398         switch( p_stream->i_fourcc )
399         {
400         case VLC_FOURCC( 'v', 'o', 'r', 'b' ):
401             msg_Dbg( p_mux, "vorbis stream" );
402             break;
403
404         case VLC_FOURCC( 's', 'p', 'x', ' ' ):
405             msg_Dbg( p_mux, "speex stream" );
406             break;
407
408         case VLC_FOURCC( 'f', 'l', 'a', 'c' ):
409             msg_Dbg( p_mux, "flac stream" );
410             break;
411
412         default:
413             fourcc_to_wf_tag( p_stream->i_fourcc, &i_tag );
414             if( i_tag == WAVE_FORMAT_UNKNOWN )
415             {
416                 FREE( p_input->p_sys );
417                 return VLC_EGENERIC;
418             }
419
420             p_stream->p_oggds_header =
421                 malloc( sizeof(oggds_header_t) + p_input->p_fmt->i_extra );
422             memset( p_stream->p_oggds_header, 0, sizeof(oggds_header_t) );
423             p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
424
425             SetDWLE( &p_stream->p_oggds_header->i_size,
426                      sizeof( oggds_header_t ) - 1 + p_input->p_fmt->i_extra );
427
428             if( p_input->p_fmt->i_extra )
429             {
430                 memcpy( &p_stream->p_oggds_header[1],
431                         p_input->p_fmt->p_extra, p_input->p_fmt->i_extra );
432             }
433
434             memcpy( p_stream->p_oggds_header->stream_type, "audio", 5 );
435
436             memset( p_stream->p_oggds_header->sub_type, 0, 4 );
437             sprintf( p_stream->p_oggds_header->sub_type, "%-x", i_tag );
438
439             SetQWLE( &p_stream->p_oggds_header->i_time_unit, I64C(10000000) );
440             SetDWLE( &p_stream->p_oggds_header->i_default_len, 1 );
441             SetDWLE( &p_stream->p_oggds_header->i_buffer_size, 30*1024 );
442             SetQWLE( &p_stream->p_oggds_header->i_samples_per_unit,
443                      p_input->p_fmt->audio.i_rate );
444             SetWLE( &p_stream->p_oggds_header->i_bits_per_sample,
445                     p_input->p_fmt->audio.i_bitspersample );
446             SetDWLE( &p_stream->p_oggds_header->header.audio.i_channels,
447                      p_input->p_fmt->audio.i_channels );
448             SetDWLE( &p_stream->p_oggds_header->header.audio.i_block_align,
449                      p_input->p_fmt->audio.i_blockalign );
450             SetDWLE( &p_stream->p_oggds_header->header.audio.i_avgbytespersec,
451                      p_input->p_fmt->i_bitrate / 8);
452             msg_Dbg( p_mux, "%4.4s stream", (char *)&p_stream->i_fourcc );
453             break;
454         }
455         break;
456
457     case SPU_ES:
458         switch( p_stream->i_fourcc )
459         {
460         case VLC_FOURCC( 's', 'u','b', 't' ):
461             p_stream->p_oggds_header = malloc( sizeof(oggds_header_t) );
462             memset( p_stream->p_oggds_header, 0, sizeof(oggds_header_t) );
463             p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
464
465             memcpy( p_stream->p_oggds_header->stream_type, "text", 4 );
466             msg_Dbg( p_mux, "subtitles stream" );
467             break;
468
469         default:
470             FREE( p_input->p_sys );
471             return VLC_EGENERIC;
472         }
473         break;
474     default:
475         FREE( p_input->p_sys );
476         return VLC_EGENERIC;
477     }
478
479     p_stream->b_new = VLC_TRUE;
480
481     p_sys->i_add_streams++;
482
483     return VLC_SUCCESS;
484 }
485
486 /*****************************************************************************
487  * DelStream: Delete an elementary stream from the muxed stream
488  *****************************************************************************/
489 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
490 {
491     sout_mux_sys_t *p_sys  = p_mux->p_sys;
492     ogg_stream_t   *p_stream = (ogg_stream_t*)p_input->p_sys;
493     block_t *p_og;
494
495     msg_Dbg( p_mux, "removing input" );
496
497     /* flush all remaining data */
498     if( p_input->p_sys )
499     {
500         if( !p_stream->b_new &&
501             ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
502         {
503             OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
504             sout_AccessOutWrite( p_mux->p_access, p_og );
505         }
506
507         /* move input in delete queue */
508         if( !p_stream->b_new )
509         {
510             p_sys->pp_del_streams = realloc( p_sys->pp_del_streams,
511                                              (p_sys->i_del_streams + 1) *
512                                              sizeof(ogg_stream_t *) );
513             p_sys->pp_del_streams[p_sys->i_del_streams++] = p_stream;
514         }
515         else
516         {
517             /* Wasn't already added so get rid of it */
518             FREE( p_stream->p_oggds_header );
519             FREE( p_stream );
520             p_sys->i_add_streams--;
521         }
522     }
523
524     p_input->p_sys = NULL;
525
526     return 0;
527 }
528
529 /*****************************************************************************
530  * Ogg bitstream manipulation routines
531  *****************************************************************************/
532 static block_t *OggStreamFlush( sout_mux_t *p_mux,
533                                 ogg_stream_state *p_os, mtime_t i_pts )
534 {
535     block_t *p_og, *p_og_first = NULL;
536     ogg_page og;
537
538     while( ogg_stream_flush( p_os, &og ) )
539     {
540         /* Flush all data */
541         p_og = block_New( p_mux, og.header_len + og.body_len );
542
543         memcpy( p_og->p_buffer, og.header, og.header_len );
544         memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
545         p_og->i_dts     = 0;
546         p_og->i_pts     = i_pts;
547         p_og->i_length  = 0;
548
549         i_pts = 0; // write it only once
550
551         block_ChainAppend( &p_og_first, p_og );
552     }
553
554     return p_og_first;
555 }
556
557 static block_t *OggStreamPageOut( sout_mux_t *p_mux,
558                                   ogg_stream_state *p_os, mtime_t i_pts )
559 {
560     block_t *p_og, *p_og_first = NULL;
561     ogg_page og;
562
563     while( ogg_stream_pageout( p_os, &og ) )
564     {
565         /* Flush all data */
566         p_og = block_New( p_mux, og.header_len + og.body_len );
567
568         memcpy( p_og->p_buffer, og.header, og.header_len );
569         memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
570         p_og->i_dts     = 0;
571         p_og->i_pts     = i_pts;
572         p_og->i_length  = 0;
573
574         i_pts = 0; // write them only once
575
576         block_ChainAppend( &p_og_first, p_og );
577     }
578
579     return p_og_first;
580 }
581
582 static block_t *OggCreateHeader( sout_mux_t *p_mux, mtime_t i_dts )
583 {
584     block_t *p_hdr = NULL;
585     block_t *p_og = NULL;
586     ogg_packet op;
587     uint8_t *p_extra;
588     int i, i_extra;
589
590     /* Write header for each stream. All b_o_s (beginning of stream) packets
591      * must appear first in the ogg stream so we take care of them first. */
592     for( i = 0; i < p_mux->i_nb_inputs; i++ )
593     {
594         sout_input_t *p_input = p_mux->pp_inputs[i];
595         ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
596         p_stream->b_new = VLC_FALSE;
597
598         msg_Dbg( p_mux, "creating header for %4.4s",
599                  (char *)&p_stream->i_fourcc );
600
601         ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
602         p_stream->i_packet_no = 0;
603
604         if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
605             p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) ||
606             p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
607         {
608             /* First packet in order: vorbis/speex/theora info */
609             p_extra = p_input->p_fmt->p_extra;
610             i_extra = p_input->p_fmt->i_extra;
611
612             op.bytes = *(p_extra++) << 8;
613             op.bytes |= (*(p_extra++) & 0xFF);
614             op.packet = p_extra;
615             i_extra -= (op.bytes + 2);
616             if( i_extra < 0 )
617             {
618                 msg_Err( p_mux, "header data corrupted");
619                 op.bytes += i_extra;
620             }
621
622             op.b_o_s  = 1;
623             op.e_o_s  = 0;
624             op.granulepos = 0;
625             op.packetno = p_stream->i_packet_no++;
626             ogg_stream_packetin( &p_stream->os, &op );
627             p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
628
629             /* Get keyframe_granule_shift for theora granulepos calculation */
630             if( p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
631             {
632                 int i_keyframe_frequency_force =
633                       1 << ((op.packet[40] << 6 >> 3) | (op.packet[41] >> 5));
634
635                 /* granule_shift = i_log( frequency_force -1 ) */
636                 p_stream->i_keyframe_granule_shift = 0;
637                 i_keyframe_frequency_force--;
638                 while( i_keyframe_frequency_force )
639                 {
640                     p_stream->i_keyframe_granule_shift++;
641                     i_keyframe_frequency_force >>= 1;
642                 }
643             }
644         }
645         else if( p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
646         {
647             /* flac stream marker (yeah, only that in the 1st packet) */
648             op.packet = "fLaC";
649             op.bytes  = 4;
650             op.b_o_s  = 1;
651             op.e_o_s  = 0;
652             op.granulepos = 0;
653             op.packetno = p_stream->i_packet_no++;
654             ogg_stream_packetin( &p_stream->os, &op );
655             p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
656         }
657         else if( p_stream->p_oggds_header )
658         {
659             /* ds header */
660             op.packet = (uint8_t*)p_stream->p_oggds_header;
661             op.bytes  = p_stream->p_oggds_header->i_size + 1;
662             op.b_o_s  = 1;
663             op.e_o_s  = 0;
664             op.granulepos = 0;
665             op.packetno = p_stream->i_packet_no++;
666             ogg_stream_packetin( &p_stream->os, &op );
667             p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
668         }
669
670         block_ChainAppend( &p_hdr, p_og );
671     }
672
673     /* Take care of the non b_o_s headers */
674     for( i = 0; i < p_mux->i_nb_inputs; i++ )
675     {
676         sout_input_t *p_input = p_mux->pp_inputs[i];
677         ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
678
679         if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
680             p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) ||
681             p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
682         {
683             /* Special case, headers are already there in the incoming stream.
684              * We need to gather them an mark them as headers. */
685             int j = 2;
686
687             if( p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) ) j = 1;
688
689             p_extra = p_input->p_fmt->p_extra;
690             i_extra = p_input->p_fmt->i_extra;
691
692             /* Skip 1 header */
693             op.bytes = *(p_extra++) << 8;
694             op.bytes |= (*(p_extra++) & 0xFF);
695             op.packet = p_extra;
696             p_extra += op.bytes;
697             i_extra -= (op.bytes + 2);
698
699             while( j-- )
700             {
701                 op.bytes = *(p_extra++) << 8;
702                 op.bytes |= (*(p_extra++) & 0xFF);
703                 op.packet = p_extra;
704                 p_extra += op.bytes;
705                 i_extra -= (op.bytes + 2);
706                 if( i_extra < 0 )
707                 {
708                     msg_Err( p_mux, "header data corrupted");
709                     op.bytes += i_extra;
710                 }
711
712                 op.b_o_s  = 0;
713                 op.e_o_s  = 0;
714                 op.granulepos = 0;
715                 op.packetno = p_stream->i_packet_no++;
716                 ogg_stream_packetin( &p_stream->os, &op );
717
718                 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
719                 block_ChainAppend( &p_hdr, p_og );
720             }
721         }
722         else if( p_stream->i_fourcc != VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
723         {
724             uint8_t com[128];
725             int     i_com;
726
727             /* comment */
728             com[0] = PACKET_TYPE_COMMENT;
729             i_com = snprintf( &com[1], 128, PACKAGE_VERSION" stream output" )
730                      + 1;
731             op.packet = com;
732             op.bytes  = i_com;
733             op.b_o_s  = 0;
734             op.e_o_s  = 0;
735             op.granulepos = 0;
736             op.packetno = p_stream->i_packet_no++;
737             ogg_stream_packetin( &p_stream->os, &op );
738             p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
739             block_ChainAppend( &p_hdr, p_og );
740         }
741
742         /* Special case for mp4v and flac */
743         if( ( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p', '4', 'v' ) ||
744               p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) ) &&
745             p_input->p_fmt->i_extra )
746         {
747             /* Send a packet with the VOL data for mp4v
748              * or STREAMINFO for flac */
749             msg_Dbg( p_mux, "writing extra data" );
750             op.bytes  = p_input->p_fmt->i_extra;
751             op.packet = p_input->p_fmt->p_extra;
752             if( p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
753             {
754                 /* Skip the flac stream marker */
755                 op.bytes -= 4;
756                 op.packet+= 4;
757             }
758             op.b_o_s  = 0;
759             op.e_o_s  = 0;
760             op.granulepos = 0;
761             op.packetno = p_stream->i_packet_no++;
762             ogg_stream_packetin( &p_stream->os, &op );
763             p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
764             block_ChainAppend( &p_hdr, p_og );
765         }
766     }
767
768     /* set HEADER flag */
769     for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
770     {
771         p_og->i_flags |= BLOCK_FLAG_HEADER;
772     }
773     return p_hdr;
774 }
775
776 static block_t *OggCreateFooter( sout_mux_t *p_mux, mtime_t i_dts )
777 {
778     sout_mux_sys_t *p_sys = p_mux->p_sys;
779     block_t *p_hdr = NULL;
780     block_t *p_og;
781     ogg_packet    op;
782     int i;
783
784     /* flush all remaining data */
785     for( i = 0; i < p_mux->i_nb_inputs; i++ )
786     {
787         ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
788
789         /* skip newly added streams */
790         if( p_stream->b_new ) continue;
791
792         if( ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
793         {
794             OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
795             sout_AccessOutWrite( p_mux->p_access, p_og );
796         }
797     }
798
799     /* Write eos packets for each stream. */
800     for( i = 0; i < p_mux->i_nb_inputs; i++ )
801     {
802         ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
803
804         /* skip newly added streams */
805         if( p_stream->b_new ) continue;
806
807         op.packet = NULL;
808         op.bytes  = 0;
809         op.b_o_s  = 0;
810         op.e_o_s  = 1;
811         op.granulepos = -1;
812         op.packetno = p_stream->i_packet_no++;
813         ogg_stream_packetin( &p_stream->os, &op );
814
815         p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
816         block_ChainAppend( &p_hdr, p_og );
817         ogg_stream_clear( &p_stream->os );
818     }
819
820     for( i = 0; i < p_sys->i_del_streams; i++ )
821     {
822         op.packet = NULL;
823         op.bytes  = 0;
824         op.b_o_s  = 0;
825         op.e_o_s  = 1;
826         op.granulepos = -1;
827         op.packetno = p_sys->pp_del_streams[i]->i_packet_no++;
828         ogg_stream_packetin( &p_sys->pp_del_streams[i]->os, &op );
829
830         p_og = OggStreamFlush( p_mux, &p_sys->pp_del_streams[i]->os, 0 );
831         block_ChainAppend( &p_hdr, p_og );
832         ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
833     }
834
835     return p_hdr;
836 }
837
838 static void OggSetDate( block_t *p_og, mtime_t i_dts, mtime_t i_length )
839 {
840     int i_count;
841     block_t *p_tmp;
842     mtime_t i_delta;
843
844     for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
845     {
846         i_count++;
847     }
848     i_delta = i_length / i_count;
849
850     for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
851     {
852         p_tmp->i_dts    = i_dts;
853         p_tmp->i_length = i_delta;
854
855         i_dts += i_delta;
856     }
857 }
858
859 /*****************************************************************************
860  * Mux: multiplex available data in input fifos into the Ogg bitstream
861  *****************************************************************************/
862 static int Mux( sout_mux_t *p_mux )
863 {
864     sout_mux_sys_t *p_sys = p_mux->p_sys;
865     block_t        *p_og = NULL;
866     int            i_stream;
867     mtime_t        i_dts;
868
869     if( p_sys->i_add_streams || p_sys->i_del_streams )
870     {
871         /* Open new ogg stream */
872         if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
873         {
874             msg_Dbg( p_mux, "waiting for data..." );
875             return VLC_SUCCESS;
876         }
877
878         if( p_sys->i_streams )
879         {
880             /* Close current ogg stream */
881             int i;
882
883             msg_Dbg( p_mux, "writing footer" );
884             block_ChainAppend( &p_og, OggCreateFooter( p_mux, 0 ) );
885
886             /* Remove deleted logical streams */
887             for( i = 0; i < p_sys->i_del_streams; i++ )
888             {
889                 FREE( p_sys->pp_del_streams[i]->p_oggds_header );
890                 FREE( p_sys->pp_del_streams[i] );
891             }
892             FREE( p_sys->pp_del_streams );
893             p_sys->i_streams = 0;
894         }
895
896         msg_Dbg( p_mux, "writing header" );
897         p_sys->i_start_dts = i_dts;
898         p_sys->i_streams = p_mux->i_nb_inputs;
899         p_sys->i_del_streams = 0;
900         p_sys->i_add_streams = 0;
901         block_ChainAppend( &p_og, OggCreateHeader( p_mux, i_dts ) );
902
903         /* Write header and/or footer */
904         OggSetDate( p_og, i_dts, 0 );
905         sout_AccessOutWrite( p_mux->p_access, p_og );
906         p_og = NULL;
907     }
908
909     for( ;; )
910     {
911         sout_input_t *p_input;
912         ogg_stream_t *p_stream;
913         block_t *p_data;
914         ogg_packet op;
915
916         if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
917         {
918             return VLC_SUCCESS;
919         }
920
921         p_input  = p_mux->pp_inputs[i_stream];
922         p_stream = (ogg_stream_t*)p_input->p_sys;
923         p_data   = block_FifoGet( p_input->p_fifo );
924
925         if( p_stream->i_fourcc != VLC_FOURCC( 'v', 'o', 'r', 'b' ) &&
926             p_stream->i_fourcc != VLC_FOURCC( 'f', 'l', 'a', 'c' ) &&
927             p_stream->i_fourcc != VLC_FOURCC( 's', 'p', 'x', ' ' ) &&
928             p_stream->i_fourcc != VLC_FOURCC( 't', 'h', 'e', 'o' ) )
929         {
930             p_data = block_Realloc( p_data, 1, p_data->i_buffer );
931             p_data->p_buffer[0] = PACKET_IS_SYNCPOINT;      // FIXME
932         }
933
934         op.packet   = p_data->p_buffer;
935         op.bytes    = p_data->i_buffer;
936         op.b_o_s    = 0;
937         op.e_o_s    = 0;
938         op.packetno = p_stream->i_packet_no++;
939
940         if( p_stream->i_cat == AUDIO_ES )
941         {
942             if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
943                 p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) ||
944                 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) )
945             {
946                 /* number of sample from begining + current packet */
947                 op.granulepos =
948                     ( i_dts - p_sys->i_start_dts + p_data->i_length ) *
949                     (mtime_t)p_input->p_fmt->audio.i_rate / I64C(1000000);
950             }
951             else if( p_stream->p_oggds_header )
952             {
953                 /* number of sample from begining */
954                 op.granulepos = ( i_dts - p_sys->i_start_dts ) *
955                     p_stream->p_oggds_header->i_samples_per_unit /
956                     I64C(1000000);
957             }
958         }
959         else if( p_stream->i_cat == VIDEO_ES )
960         {
961             if( p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
962             {
963                 /* FIXME, we assume only keyframes */
964                 op.granulepos = ( ( i_dts - p_sys->i_start_dts ) *
965                     p_input->p_fmt->video.i_frame_rate /
966                     p_input->p_fmt->video.i_frame_rate_base /
967                     I64C(1000000) ) << p_stream->i_keyframe_granule_shift;
968             }
969             else if( p_stream->p_oggds_header )
970                 op.granulepos = ( i_dts - p_sys->i_start_dts ) * I64C(10) /
971                     p_stream->p_oggds_header->i_time_unit;
972         }
973         else if( p_stream->i_cat == SPU_ES )
974         {
975             /* granulepos is in milisec */
976             op.granulepos = ( i_dts - p_sys->i_start_dts ) / 1000;
977         }
978
979         ogg_stream_packetin( &p_stream->os, &op );
980
981         if( p_stream->i_cat == SPU_ES ||
982             p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) )
983         {
984             /* Subtitles or Speex packets are quite small so they 
985              * need to be flushed to be sent on time */
986             p_og = OggStreamFlush( p_mux, &p_stream->os, p_data->i_dts );
987         }
988         else
989         {
990             p_og = OggStreamPageOut( p_mux, &p_stream->os, p_data->i_dts );
991         }
992
993         if( p_og )
994         {
995             OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
996             p_stream->i_dts = -1;
997             p_stream->i_length = 0;
998
999             sout_AccessOutWrite( p_mux->p_access, p_og );
1000         }
1001         else
1002         {
1003             if( p_stream->i_dts < 0 )
1004             {
1005                 p_stream->i_dts = p_data->i_dts;
1006             }
1007             p_stream->i_length += p_data->i_length;
1008         }
1009
1010         block_Release( p_data );
1011     }
1012
1013     return VLC_SUCCESS;
1014 }