]> git.sesse.net Git - vlc/blob - modules/mux/ogg.c
* modules/demux/ogg.c, modules/codec/vorbis.c: misc small fixes.
[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.10 2003/09/28 16:50:04 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 #include <sys/types.h>
31
32 #include <vlc/vlc.h>
33 #include <vlc/input.h>
34 #include <vlc/sout.h>
35
36 #include "codecs.h"
37
38 #include <ogg/ogg.h>
39
40 /*****************************************************************************
41  * Exported prototypes
42  *****************************************************************************/
43 static int  Open   ( vlc_object_t * );
44 static void Close  ( vlc_object_t * );
45
46 static int Capability(sout_mux_t *, int, void *, void * );
47 static int AddStream( sout_mux_t *, sout_input_t * );
48 static int DelStream( sout_mux_t *, sout_input_t * );
49 static int Mux      ( sout_mux_t * );
50
51 static sout_buffer_t *OggCreateHeader( sout_mux_t *, mtime_t );
52 static sout_buffer_t *OggCreateFooter( sout_mux_t *, mtime_t );
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57 vlc_module_begin();
58     set_description( _("Ogg/ogm muxer") );
59     set_capability( "sout mux", 10 );
60     add_shortcut( "ogg" );
61     add_shortcut( "ogm" );
62     set_callbacks( Open, Close );
63 vlc_module_end();
64
65 /*****************************************************************************
66  * Misc declarations
67  *****************************************************************************/
68 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
69
70 /* Structures used for OggDS headers used in ogm files */
71
72 #define PACKET_TYPE_HEADER   0x01
73 #define PACKET_TYPE_COMMENT  0x03
74 #define PACKET_IS_SYNCPOINT  0x08
75
76 typedef struct
77 #ifdef HAVE_ATTRIBUTE_PACKED
78     __attribute__((__packed__))
79 #endif
80 {
81     int32_t i_width;
82     int32_t i_height;
83 } ogg_stream_header_video_t;
84
85 typedef struct
86 #ifdef HAVE_ATTRIBUTE_PACKED
87     __attribute__((__packed__))
88 #endif
89 {
90     int16_t i_channels;
91     int16_t i_block_align;
92     int32_t i_avgbytespersec;
93 } ogg_stream_header_audio_t;
94
95 typedef struct
96 #ifdef HAVE_ATTRIBUTE_PACKED
97     __attribute__((__packed__))
98 #endif
99 {
100     uint8_t i_packet_type;
101
102     char stream_type[8];
103     char sub_type[4];
104
105     int32_t i_size;
106
107     int64_t i_time_unit;
108     int64_t i_samples_per_unit;
109     int32_t i_default_len;
110
111     int32_t i_buffer_size;
112     int16_t i_bits_per_sample;
113     int16_t i_padding_0;            // hum hum
114     union
115     {
116         ogg_stream_header_video_t video;
117         ogg_stream_header_audio_t audio;
118     } header;
119
120 } ogg_stream_header_t;
121
122 /* Helper writer functions */
123
124 #define SetWLE( p, v ) _SetWLE( (uint8_t*)p, v)
125 static void _SetWLE( uint8_t *p, uint16_t i_dw )
126 {
127     p[1] = ( i_dw >>  8 )&0xff;
128     p[0] = ( i_dw       )&0xff;
129 }
130
131 #define SetDWLE( p, v ) _SetDWLE( (uint8_t*)p, v)
132 static void _SetDWLE( uint8_t *p, uint32_t i_dw )
133 {
134     p[3] = ( i_dw >> 24 )&0xff;
135     p[2] = ( i_dw >> 16 )&0xff;
136     p[1] = ( i_dw >>  8 )&0xff;
137     p[0] = ( i_dw       )&0xff;
138 }
139 #define SetQWLE( p, v ) _SetQWLE( (uint8_t*)p, v)
140 static void _SetQWLE( uint8_t *p, uint64_t i_qw )
141 {
142     SetDWLE( p,   i_qw&0xffffffff );
143     SetDWLE( p+4, ( i_qw >> 32)&0xffffffff );
144 }
145
146 /*
147  * TODO  move this function to src/stream_output.c (used by nearly all muxers)
148  */
149 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
150 {
151     mtime_t i_dts;
152     int     i_stream;
153     int     i;
154
155     for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
156     {
157         sout_fifo_t  *p_fifo;
158
159         p_fifo = p_mux->pp_inputs[i]->p_fifo;
160
161         if( p_fifo->i_depth > 2 )
162         {
163             sout_buffer_t *p_buf;
164
165             p_buf = sout_FifoShow( p_fifo );
166             if( i_stream < 0 || p_buf->i_dts < i_dts )
167             {
168                 i_dts = p_buf->i_dts;
169                 i_stream = i;
170             }
171         }
172         else
173         {
174             // wait that all fifo have at least 3 packets (3 vorbis headers)
175             return( -1 );
176         }
177     }
178     if( pi_stream )
179     {
180         *pi_stream = i_stream;
181     }
182     if( pi_dts )
183     {
184         *pi_dts = i_dts;
185     }
186     return( i_stream );
187 }
188
189 /*****************************************************************************
190  * Definitions of structures and functions used by this plugins 
191  *****************************************************************************/
192 typedef struct
193 {
194     int i_cat;
195     int i_fourcc;
196
197     int b_new;
198
199     mtime_t i_dts;
200     mtime_t i_length;
201     int     i_packet_no;
202     ogg_stream_state os;
203
204     ogg_stream_header_t header;
205
206 } ogg_stream_t;
207
208 struct sout_mux_sys_t
209 {
210     int     i_streams;
211
212     mtime_t i_start_dts;
213
214     /* number of logical streams pending to be added */
215     int i_add_streams;
216
217     /* logical streams pending to be deleted */
218     int i_del_streams;
219     ogg_stream_t **pp_del_streams;
220 };
221
222 static void OggSetDate( sout_buffer_t *, mtime_t , mtime_t  );
223 static sout_buffer_t *OggStreamFlush( sout_mux_t *, ogg_stream_state *,
224                                       mtime_t );
225
226 /*****************************************************************************
227  * Open: Open muxer
228  *****************************************************************************/
229 static int Open( vlc_object_t *p_this )
230 {
231     sout_mux_t      *p_mux = (sout_mux_t*)p_this;
232     sout_mux_sys_t  *p_sys;
233
234     msg_Info( p_mux, "Open" );
235
236     p_sys                 = malloc( sizeof( sout_mux_sys_t ) );
237     p_sys->i_streams      = 0;
238     p_sys->i_add_streams  = 0;
239     p_sys->i_del_streams  = 0;
240     p_sys->pp_del_streams = 0;
241
242     p_mux->p_sys        = p_sys;
243     p_mux->pf_capacity  = Capability;
244     p_mux->pf_addstream = AddStream;
245     p_mux->pf_delstream = DelStream;
246     p_mux->pf_mux       = Mux;
247     p_mux->i_preheader  = 1;
248
249     return VLC_SUCCESS;
250 }
251
252 /*****************************************************************************
253  * Close: Finalize ogg bitstream and close muxer
254  *****************************************************************************/
255 static void Close( vlc_object_t * p_this )
256 {
257     sout_mux_t     *p_mux = (sout_mux_t*)p_this;
258     sout_mux_sys_t *p_sys = p_mux->p_sys;
259
260     msg_Info( p_mux, "Close" );
261
262     if( p_sys->i_del_streams )
263     {
264         sout_buffer_t *p_og = NULL;
265         mtime_t i_dts = -1;
266         int i;
267
268         /* Close the current ogg stream */
269         msg_Dbg( p_mux, "writing footer" );
270         sout_BufferChain( &p_og, OggCreateFooter( p_mux, 0 ) );
271
272         /* Remove deleted logical streams */
273         for( i = 0; i < p_sys->i_del_streams; i++ )
274         {
275             i_dts = p_sys->pp_del_streams[i]->i_dts;
276             ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
277             FREE( p_sys->pp_del_streams[i] );
278         }
279         FREE( p_sys->pp_del_streams );
280         p_sys->i_streams -= p_sys->i_del_streams;
281
282         /* Write footer */
283         OggSetDate( p_og, i_dts, 0 );
284         sout_AccessOutWrite( p_mux->p_access, p_og );
285     }
286
287     free( p_sys );
288 }
289
290 static int Capability( sout_mux_t *p_mux, int i_query, void *p_args,
291                        void *p_answer )
292 {
293    switch( i_query )
294    {
295         case SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME:
296             *(vlc_bool_t*)p_answer = VLC_TRUE;
297             return( SOUT_MUX_CAP_ERR_OK );
298         default:
299             return( SOUT_MUX_CAP_ERR_UNIMPLEMENTED );
300    }
301 }
302
303 /*****************************************************************************
304  * AddStream: Add an elementary stream to the muxed stream
305  *****************************************************************************/
306 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
307 {
308     sout_mux_sys_t  *p_sys = p_mux->p_sys;
309     ogg_stream_t        *p_stream;
310
311     msg_Dbg( p_mux, "adding input" );
312
313     p_input->p_sys = (void *)p_stream = malloc( sizeof( ogg_stream_t ) );
314
315     p_stream->i_cat       = p_input->p_fmt->i_cat;
316     p_stream->i_fourcc    = p_input->p_fmt->i_fourcc;
317     p_stream->i_packet_no = 0;
318
319     p_stream->header.i_packet_type = PACKET_TYPE_HEADER;
320     switch( p_input->p_fmt->i_cat )
321     {
322     case VIDEO_ES:
323         switch( p_stream->i_fourcc )
324         {
325         case VLC_FOURCC( 'm', 'p','4', 'v' ):
326         case VLC_FOURCC( 'D', 'I','V', '3' ):
327             memcpy( p_stream->header.stream_type, "video    ", 8 );
328             if( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p','4', 'v' ) )
329             {
330                 memcpy( p_stream->header.sub_type, "XVID", 4 );
331             }
332             else if( p_stream->i_fourcc == VLC_FOURCC( 'D', 'I','V', '3' ) )
333             {
334                 memcpy( p_stream->header.sub_type, "DIV3", 4 );
335             }
336             SetDWLE( &p_stream->header.i_size,
337                      sizeof( ogg_stream_header_t ) - 1);
338             /* XXX this won't make mplayer happy,
339              * but vlc can read that without any problem so...*/
340             SetQWLE( &p_stream->header.i_time_unit, 10*1000 );
341             //(int64_t)10*1000*1000/(int64_t)25 );  // FIXME (25fps)
342             SetQWLE( &p_stream->header.i_samples_per_unit, 1 );
343             SetDWLE( &p_stream->header.i_default_len, 0 );      /* ??? */
344             SetDWLE( &p_stream->header.i_buffer_size, 1024*1024 );
345             SetWLE( &p_stream->header.i_bits_per_sample, 0 );
346             SetDWLE( &p_stream->header.header.video.i_width,
347                      p_input->p_fmt->i_width );
348             SetDWLE( &p_stream->header.header.video.i_height,
349                      p_input->p_fmt->i_height );
350             break;
351
352         case VLC_FOURCC( 't', 'h', 'e', 'o' ):
353           msg_Dbg( p_mux, "theora stream" );
354           break;
355
356         default:
357             FREE( p_input->p_sys );
358             return( VLC_EGENERIC );
359         }
360         break;
361
362     case AUDIO_ES:
363         switch( p_stream->i_fourcc )
364         {
365         case VLC_FOURCC( 'm', 'p','g', 'a' ):
366         case VLC_FOURCC( 'a', '5','2', ' ' ):
367             memcpy( p_stream->header.stream_type, "audio    ", 8 );
368             if( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p','g', 'a' ) )
369             {
370                 memcpy( p_stream->header.sub_type, "55  ", 4 );
371             }
372             else if( p_stream->i_fourcc == VLC_FOURCC( 'a', '5','2', ' ' ) )
373             {
374                 memcpy( p_stream->header.sub_type, "2000", 4 );
375             }
376             SetDWLE( &p_stream->header.i_size,
377                      sizeof( ogg_stream_header_t ) - 1);
378             SetQWLE( &p_stream->header.i_time_unit, 1000000 ); /*is it used ?*/
379             SetDWLE( &p_stream->header.i_default_len, 0 );     /* ??? */
380             SetDWLE( &p_stream->header.i_buffer_size, 30*1024 );
381             SetQWLE( &p_stream->header.i_samples_per_unit,
382                      p_input->p_fmt->i_sample_rate );
383             SetWLE( &p_stream->header.i_bits_per_sample, 0 );
384             SetDWLE( &p_stream->header.header.audio.i_channels,
385                      p_input->p_fmt->i_channels );
386             SetDWLE( &p_stream->header.header.audio.i_block_align,
387                      p_input->p_fmt->i_block_align );
388             SetDWLE( &p_stream->header.header.audio.i_avgbytespersec, 0 );
389             break;
390
391         case VLC_FOURCC( 'v', 'o', 'r', 'b' ):
392             msg_Dbg( p_mux, "vorbis stream" );
393             break;
394
395         default:
396             FREE( p_input->p_sys );
397             return( VLC_EGENERIC );
398         }
399         break;
400
401     case SPU_ES:
402         switch( p_stream->i_fourcc )
403         {
404         case VLC_FOURCC( 's', 'u','b', 't' ):
405             memcpy( p_stream->header.stream_type, "text     ", 8 );
406             break;
407
408         default:
409             FREE( p_input->p_sys );
410             return( VLC_EGENERIC );
411         }
412         break;
413     default:
414         FREE( p_input->p_sys );
415         return( VLC_EGENERIC );
416     }
417
418     ogg_stream_init( &p_stream->os, rand () );
419     p_stream->b_new = VLC_TRUE;
420
421     p_sys->i_add_streams++;
422
423     return( VLC_SUCCESS );
424 }
425
426 /*****************************************************************************
427  * DelStream: Delete an elementary stream from the muxed stream
428  *****************************************************************************/
429 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
430 {
431     sout_mux_sys_t *p_sys  = p_mux->p_sys;
432     ogg_stream_t   *p_stream = (ogg_stream_t*)p_input->p_sys;
433     sout_buffer_t  *p_og;
434
435     msg_Dbg( p_mux, "removing input" );
436
437     /* flush all remaining data */
438     if( p_input->p_sys )
439     {
440         if( ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
441         {
442             OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
443             sout_AccessOutWrite( p_mux->p_access, p_og );
444         }
445
446         /* move input in delete queue */
447         if( !p_stream->b_new )
448         {
449             p_sys->pp_del_streams = realloc( p_sys->pp_del_streams,
450                                              (p_sys->i_del_streams + 1) *
451                                              sizeof(ogg_stream_t *) );
452             p_sys->pp_del_streams[p_sys->i_del_streams++] = p_stream;
453         }
454         else
455         {
456             /* Wasn't already added so get rid of it */
457             ogg_stream_clear( &p_stream->os );
458             FREE( p_stream );
459             p_sys->i_add_streams--;
460         }
461     }
462
463     p_input->p_sys = NULL;
464
465     return( 0 );
466 }
467
468 /*****************************************************************************
469  * Ogg bitstream manipulation routines
470  *****************************************************************************/
471 static sout_buffer_t *OggStreamFlush( sout_mux_t *p_mux,
472                                       ogg_stream_state *p_os, mtime_t i_pts )
473 {
474     sout_buffer_t *p_og, *p_og_first = NULL;
475     ogg_page      og;
476
477     for( ;; )
478     {
479         /* flush all data */
480         int i_result;
481         int i_size;
482         if( ( i_result = ogg_stream_flush( p_os, &og ) ) == 0 )
483         {
484             break;
485         }
486         i_size = og.header_len + og.body_len;
487         p_og = sout_BufferNew( p_mux->p_sout, i_size);
488
489         memcpy( p_og->p_buffer, og.header, og.header_len );
490         memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
491         p_og->i_size    = i_size;
492         p_og->i_dts     = 0;
493         p_og->i_pts     = i_pts;
494         p_og->i_length  = 0;
495
496         i_pts   = 0; // write it only once
497
498         sout_BufferChain( &p_og_first, p_og );
499     }
500
501     return( p_og_first );
502 }
503
504 static sout_buffer_t *OggStreamPageOut( sout_mux_t *p_mux,
505                                         ogg_stream_state *p_os, mtime_t i_pts )
506 {
507     sout_buffer_t *p_og, *p_og_first = NULL;
508     ogg_page      og;
509
510     for( ;; )
511     {
512         /* flush all data */
513         int i_result;
514         int i_size;
515         if( ( i_result = ogg_stream_pageout( p_os, &og ) ) == 0 )
516         {
517             break;
518         }
519         i_size = og.header_len + og.body_len;
520         p_og = sout_BufferNew( p_mux->p_sout, i_size);
521
522         memcpy( p_og->p_buffer, og.header, og.header_len );
523         memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
524         p_og->i_size    = i_size;
525         p_og->i_dts     = 0;
526         p_og->i_pts     = i_pts;
527         p_og->i_length  = 0;
528
529         i_pts   = 0; // write them only once
530
531         sout_BufferChain( &p_og_first, p_og );
532     }
533
534     return( p_og_first );
535 }
536
537 static sout_buffer_t *OggCreateHeader( sout_mux_t *p_mux, mtime_t i_dts )
538 {
539     sout_buffer_t *p_hdr = NULL;
540     sout_buffer_t *p_og;
541     ogg_packet    op;
542     int i;
543
544     /* Write header for each stream. All b_o_s (beginning of stream) packets
545      * must appear first in the ogg stream so we take care of them first. */
546     for( i = 0; i < p_mux->i_nb_inputs; i++ )
547     {
548         ogg_stream_t *p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
549         p_stream->b_new = VLC_FALSE;
550
551         if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
552             p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
553         {
554             /* Special case, headers are already there in the
555              * incoming stream */
556
557             /* first packet in order: vorbis info */
558             p_og = sout_FifoGet( p_mux->pp_inputs[i]->p_fifo );
559             op.packet = p_og->p_buffer;
560             op.bytes  = p_og->i_size;
561             op.b_o_s  = 1;
562             op.e_o_s  = 0;
563             op.granulepos = 0;
564             op.packetno = p_stream->i_packet_no++;
565             ogg_stream_packetin( &p_stream->os, &op );
566         }
567         else
568         {
569             /* ds header */
570             op.packet = (uint8_t*)&p_stream->header;
571             op.bytes  = sizeof( ogg_stream_header_t );
572             op.b_o_s  = 1;
573             op.e_o_s  = 0;
574             op.granulepos = 0;
575             op.packetno = p_stream->i_packet_no++;
576             ogg_stream_packetin( &p_stream->os, &op );
577         }
578
579         p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
580         sout_BufferChain( &p_hdr, p_og );
581     }
582
583     /* Take care of the non b_o_s headers */
584     for( i = 0; i < p_mux->i_nb_inputs; i++ )
585     {
586         ogg_stream_t *p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
587
588         if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
589             p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
590         {
591             /* Special case, headers are already there in the incoming stream.
592              * We need to gather them an mark them as headers. */
593             int j;
594             for( j = 0; j < 2; j++ )
595             {
596                 /* next packets in order: comments and codebooks */
597                 p_og = sout_FifoGet( p_mux->pp_inputs[i]->p_fifo );
598                 op.packet = p_og->p_buffer;
599                 op.bytes  = p_og->i_size;
600                 op.b_o_s  = 0;
601                 op.e_o_s  = 0;
602                 op.granulepos = 0;
603                 op.packetno = p_stream->i_packet_no++;
604                 ogg_stream_packetin( &p_stream->os, &op );
605             }
606         }
607         else
608         {
609             uint8_t com[128];
610             int     i_com;
611
612             /* comment */
613             com[0] = PACKET_TYPE_COMMENT;
614             i_com = snprintf( &com[1], 128, VERSION" stream output" ) + 1;
615             op.packet = com;
616             op.bytes  = i_com;
617             op.b_o_s  = 0;
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         }
623
624         p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
625         sout_BufferChain( &p_hdr, p_og );
626     }
627
628     /* set HEADER flag */
629     for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
630     {
631         p_og->i_flags |= SOUT_BUFFER_FLAGS_HEADER;
632     }
633     return( p_hdr );
634 }
635
636 static sout_buffer_t *OggCreateFooter( sout_mux_t *p_mux, mtime_t i_dts )
637 {
638     sout_mux_sys_t *p_sys = p_mux->p_sys;
639     sout_buffer_t *p_hdr = NULL;
640     sout_buffer_t *p_og;
641     ogg_packet    op;
642     int i;
643
644     /* Write eos packets for each stream. */
645     for( i = 0; i < p_mux->i_nb_inputs; i++ )
646     {
647         ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
648
649         /* skip newly added streams */
650         if( p_stream->b_new ) continue;
651
652         op.packet = NULL;
653         op.bytes  = 0;
654         op.b_o_s  = 0;
655         op.e_o_s  = 1;
656         op.granulepos = -1;
657         op.packetno = p_stream->i_packet_no++;
658         ogg_stream_packetin( &p_stream->os, &op );
659
660         p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
661         sout_BufferChain( &p_hdr, p_og );
662     }
663
664     for( i = 0; i < p_sys->i_del_streams; i++ )
665     {
666         op.packet = NULL;
667         op.bytes  = 0;
668         op.b_o_s  = 0;
669         op.e_o_s  = 1;
670         op.granulepos = -1;
671         op.packetno = p_sys->pp_del_streams[i]->i_packet_no++;
672         ogg_stream_packetin( &p_sys->pp_del_streams[i]->os, &op );
673
674         p_og = OggStreamFlush( p_mux, &p_sys->pp_del_streams[i]->os, 0 );
675         sout_BufferChain( &p_hdr, p_og );
676     }
677
678     return( p_hdr );
679 }
680
681 static void OggSetDate( sout_buffer_t *p_og, mtime_t i_dts, mtime_t i_length )
682 {
683     int i_count;
684     sout_buffer_t *p_tmp;
685     mtime_t i_delta;
686
687     for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
688     {
689         i_count++;
690     }
691     i_delta = i_length / i_count;
692
693     for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
694     {
695         p_tmp->i_dts    = i_dts;
696         p_tmp->i_length = i_delta;
697
698         i_dts += i_delta;
699     }
700 }
701
702 /*****************************************************************************
703  * Mux: multiplex available data in input fifos into the Ogg bitstream
704  *****************************************************************************/
705 static int Mux( sout_mux_t *p_mux )
706 {
707     sout_mux_sys_t *p_sys = p_mux->p_sys;
708     sout_buffer_t  *p_og = NULL;
709     int            i_stream;
710     mtime_t        i_dts;
711
712     if( p_sys->i_add_streams || p_sys->i_del_streams )
713     {
714         /* Open new ogg stream */
715         if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
716         {
717             msg_Dbg( p_mux, "waiting for data..." );
718             return( VLC_SUCCESS );
719         }
720
721         if( p_sys->i_streams )
722         {
723             /* Close current ogg stream */
724             int i;
725
726             msg_Dbg( p_mux, "writing footer" );
727             sout_BufferChain( &p_og, OggCreateFooter( p_mux, 0 ) );
728
729             /* Remove deleted logical streams */
730             for( i = 0; i < p_sys->i_del_streams; i++ )
731             {
732                 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
733                 FREE( p_sys->pp_del_streams[i] );
734             }
735             FREE( p_sys->pp_del_streams );
736             p_sys->i_streams = 0;
737         }
738
739         msg_Dbg( p_mux, "writing header" );
740         p_sys->i_start_dts = i_dts;
741         p_sys->i_streams = p_mux->i_nb_inputs;
742         p_sys->i_del_streams = 0;
743         p_sys->i_add_streams = 0;
744         sout_BufferChain( &p_og, OggCreateHeader( p_mux, i_dts ) );
745
746         /* Write header and/or footer */
747         OggSetDate( p_og, i_dts, 0 );
748         sout_AccessOutWrite( p_mux->p_access, p_og );
749         p_og = NULL;
750     }
751
752     for( ;; )
753     {
754         sout_input_t  *p_input;
755         ogg_stream_t  *p_stream;
756         sout_buffer_t *p_data;
757         ogg_packet    op;
758
759         if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
760         {
761             return( VLC_SUCCESS );
762         }
763
764         p_input  = p_mux->pp_inputs[i_stream];
765         p_stream = (ogg_stream_t*)p_input->p_sys;
766         p_data   = sout_FifoGet( p_input->p_fifo );
767
768         if( p_stream->i_fourcc != VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
769         {
770             sout_BufferReallocFromPreHeader( p_mux->p_sout, p_data, 1 );
771             p_data->p_buffer[0] = PACKET_IS_SYNCPOINT;      // FIXME
772         }
773
774         op.packet   = p_data->p_buffer;
775         op.bytes    = p_data->i_size;
776         op.b_o_s    = 0;
777         op.e_o_s    = 0;
778         op.packetno = p_stream->i_packet_no++;
779
780         if( p_stream->i_cat == AUDIO_ES )
781         {
782             if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
783             {
784                 /* number of sample from begining + current packet */
785                 op.granulepos =
786                     ( i_dts + p_data->i_length - p_sys->i_start_dts ) *
787                     p_input->p_fmt->i_sample_rate / (int64_t)1000000;
788             }
789             else
790             {
791                 /* number of sample from begining */
792                 op.granulepos = ( i_dts - p_sys->i_start_dts ) *
793                     p_stream->header.i_samples_per_unit / (int64_t)1000000;
794             }
795         }
796         else if( p_stream->i_cat == VIDEO_ES )
797         {
798             if( p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
799             {
800                 op.granulepos = op.packetno; /* FIXME */
801             }
802             else
803                 op.granulepos = ( i_dts - p_sys->i_start_dts ) / 1000;
804         }
805         else if( p_stream->i_cat == SPU_ES )
806         {
807             /* granulepos is in milisec */
808             op.granulepos = ( i_dts - p_sys->i_start_dts ) / 1000;
809         }
810
811         ogg_stream_packetin( &p_stream->os, &op );
812
813         sout_BufferChain( &p_og, OggStreamPageOut( p_mux, &p_stream->os,
814                                                    p_data->i_dts ) );
815
816         if( p_og )
817         {
818             OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
819             p_stream->i_dts = -1;
820             p_stream->i_length = 0;
821
822             sout_AccessOutWrite( p_mux->p_access, p_og );
823
824             p_og = NULL;
825         }
826         else
827         {
828             if( p_stream->i_dts < 0 )
829             {
830                 p_stream->i_dts = p_data->i_dts;
831             }
832             p_stream->i_length += p_data->i_length;
833         }
834
835         sout_BufferDelete( p_mux->p_sout, p_data );
836     }
837
838     return( VLC_SUCCESS );
839 }