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