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