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