]> git.sesse.net Git - vlc/blob - modules/mux/ogg.c
* modules/mux/ogg.c: proper fps and ogg packet granulepos when embedding mp4v/div3...
[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.13 2003/09/28 22:18:57 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             SetQWLE( &p_stream->header.i_time_unit,
339                      I64C(10000000)/(int64_t)25 );  // FIXME (25fps)
340             SetQWLE( &p_stream->header.i_samples_per_unit, 1 );
341             SetDWLE( &p_stream->header.i_default_len, 0 );      /* ??? */
342             SetDWLE( &p_stream->header.i_buffer_size, 1024*1024 );
343             SetWLE( &p_stream->header.i_bits_per_sample, 0 );
344             SetDWLE( &p_stream->header.header.video.i_width,
345                      p_input->p_fmt->i_width );
346             SetDWLE( &p_stream->header.header.video.i_height,
347                      p_input->p_fmt->i_height );
348             msg_Dbg( p_mux, "mp4v/div3 stream" );
349             break;
350
351         case VLC_FOURCC( 't', 'h', 'e', 'o' ):
352             msg_Dbg( p_mux, "theora stream" );
353             break;
354
355         default:
356             FREE( p_input->p_sys );
357             return( VLC_EGENERIC );
358         }
359         break;
360
361     case AUDIO_ES:
362         switch( p_stream->i_fourcc )
363         {
364         case VLC_FOURCC( 'm', 'p','g', 'a' ):
365         case VLC_FOURCC( 'a', '5','2', ' ' ):
366             memcpy( p_stream->header.stream_type, "audio    ", 8 );
367             if( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p','g', 'a' ) )
368             {
369                 memcpy( p_stream->header.sub_type, "55  ", 4 );
370             }
371             else if( p_stream->i_fourcc == VLC_FOURCC( 'a', '5','2', ' ' ) )
372             {
373                 memcpy( p_stream->header.sub_type, "2000", 4 );
374             }
375             SetDWLE( &p_stream->header.i_size,
376                      sizeof( ogg_stream_header_t ) - 1);
377             SetQWLE( &p_stream->header.i_time_unit, 1000000 ); /*is it used ?*/
378             SetDWLE( &p_stream->header.i_default_len, 0 );     /* ??? */
379             SetDWLE( &p_stream->header.i_buffer_size, 30*1024 );
380             SetQWLE( &p_stream->header.i_samples_per_unit,
381                      p_input->p_fmt->i_sample_rate );
382             SetWLE( &p_stream->header.i_bits_per_sample, 0 );
383             SetDWLE( &p_stream->header.header.audio.i_channels,
384                      p_input->p_fmt->i_channels );
385             SetDWLE( &p_stream->header.header.audio.i_block_align,
386                      p_input->p_fmt->i_block_align );
387             SetDWLE( &p_stream->header.header.audio.i_avgbytespersec, 0 );
388             msg_Dbg( p_mux, "mpga/a52 stream" );
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             msg_Dbg( p_mux, "subtitles stream" );
407             break;
408
409         default:
410             FREE( p_input->p_sys );
411             return( VLC_EGENERIC );
412         }
413         break;
414     default:
415         FREE( p_input->p_sys );
416         return( VLC_EGENERIC );
417     }
418
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
487         i_size = og.header_len + og.body_len;
488         p_og = sout_BufferNew( p_mux->p_sout, i_size);
489
490         memcpy( p_og->p_buffer, og.header, og.header_len );
491         memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
492         p_og->i_size    = i_size;
493         p_og->i_dts     = 0;
494         p_og->i_pts     = i_pts;
495         p_og->i_length  = 0;
496
497         i_pts   = 0; // write it only once
498
499         sout_BufferChain( &p_og_first, p_og );
500     }
501
502     return( p_og_first );
503 }
504
505 static sout_buffer_t *OggStreamPageOut( sout_mux_t *p_mux,
506                                         ogg_stream_state *p_os, mtime_t i_pts )
507 {
508     sout_buffer_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_pageout( p_os, &og ) ) == 0 )
517         {
518             break;
519         }
520
521         i_size = og.header_len + og.body_len;
522         p_og = sout_BufferNew( p_mux->p_sout, 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_size    = i_size;
527         p_og->i_dts     = 0;
528         p_og->i_pts     = i_pts;
529         p_og->i_length  = 0;
530
531         i_pts   = 0; // write them only once
532
533         sout_BufferChain( &p_og_first, p_og );
534     }
535
536     return( p_og_first );
537 }
538
539 static sout_buffer_t *OggCreateHeader( sout_mux_t *p_mux, mtime_t i_dts )
540 {
541     sout_buffer_t *p_hdr = NULL;
542     sout_buffer_t *p_og;
543     ogg_packet    op;
544     int i;
545
546     /* Write header for each stream. All b_o_s (beginning of stream) packets
547      * must appear first in the ogg stream so we take care of them first. */
548     for( i = 0; i < p_mux->i_nb_inputs; i++ )
549     {
550         ogg_stream_t *p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
551         p_stream->b_new = VLC_FALSE;
552
553         msg_Dbg( p_mux, "creating header for %4.4s",
554                  (char *)&p_stream->i_fourcc );
555
556         ogg_stream_init( &p_stream->os, rand() );
557         p_stream->i_packet_no = 0;
558
559         if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
560             p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
561         {
562             /* Special case, headers are already there in the
563              * incoming stream */
564
565             /* first packet in order: vorbis info */
566             p_og = sout_FifoGet( p_mux->pp_inputs[i]->p_fifo );
567             op.packet = p_og->p_buffer;
568             op.bytes  = p_og->i_size;
569             op.b_o_s  = 1;
570             op.e_o_s  = 0;
571             op.granulepos = 0;
572             op.packetno = p_stream->i_packet_no++;
573             ogg_stream_packetin( &p_stream->os, &op );
574         }
575         else
576         {
577             /* ds header */
578             op.packet = (uint8_t*)&p_stream->header;
579             op.bytes  = sizeof( ogg_stream_header_t );
580             op.b_o_s  = 1;
581             op.e_o_s  = 0;
582             op.granulepos = 0;
583             op.packetno = p_stream->i_packet_no++;
584             ogg_stream_packetin( &p_stream->os, &op );
585         }
586
587         p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
588         sout_BufferChain( &p_hdr, p_og );
589     }
590
591     /* Take care of the non b_o_s headers */
592     for( i = 0; i < p_mux->i_nb_inputs; i++ )
593     {
594         ogg_stream_t *p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
595
596         if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
597             p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
598         {
599             /* Special case, headers are already there in the incoming stream.
600              * We need to gather them an mark them as headers. */
601             int j;
602             for( j = 0; j < 2; j++ )
603             {
604                 /* next packets in order: comments and codebooks */
605                 p_og = sout_FifoGet( p_mux->pp_inputs[i]->p_fifo );
606                 op.packet = p_og->p_buffer;
607                 op.bytes  = p_og->i_size;
608                 op.b_o_s  = 0;
609                 op.e_o_s  = 0;
610                 op.granulepos = 0;
611                 op.packetno = p_stream->i_packet_no++;
612                 ogg_stream_packetin( &p_stream->os, &op );
613             }
614         }
615         else
616         {
617             uint8_t com[128];
618             int     i_com;
619
620             /* comment */
621             com[0] = PACKET_TYPE_COMMENT;
622             i_com = snprintf( &com[1], 128, VERSION" stream output" ) + 1;
623             op.packet = com;
624             op.bytes  = i_com;
625             op.b_o_s  = 0;
626             op.e_o_s  = 0;
627             op.granulepos = 0;
628             op.packetno = p_stream->i_packet_no++;
629             ogg_stream_packetin( &p_stream->os, &op );
630         }
631
632         p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
633         sout_BufferChain( &p_hdr, p_og );
634     }
635
636     /* set HEADER flag */
637     for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
638     {
639         p_og->i_flags |= SOUT_BUFFER_FLAGS_HEADER;
640     }
641     return( p_hdr );
642 }
643
644 static sout_buffer_t *OggCreateFooter( sout_mux_t *p_mux, mtime_t i_dts )
645 {
646     sout_mux_sys_t *p_sys = p_mux->p_sys;
647     sout_buffer_t *p_hdr = NULL;
648     sout_buffer_t *p_og;
649     ogg_packet    op;
650     int i;
651
652     /* flush all remaining data */
653     for( i = 0; i < p_mux->i_nb_inputs; i++ )
654     {
655         ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
656
657         /* skip newly added streams */
658         if( p_stream->b_new ) continue;
659
660         if( ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
661         {
662             OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
663             sout_AccessOutWrite( p_mux->p_access, p_og );
664         }
665     }
666
667     /* Write eos packets for each stream. */
668     for( i = 0; i < p_mux->i_nb_inputs; i++ )
669     {
670         ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
671
672         /* skip newly added streams */
673         if( p_stream->b_new ) continue;
674
675         op.packet = NULL;
676         op.bytes  = 0;
677         op.b_o_s  = 0;
678         op.e_o_s  = 1;
679         op.granulepos = -1;
680         op.packetno = p_stream->i_packet_no++;
681         ogg_stream_packetin( &p_stream->os, &op );
682
683         p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
684         sout_BufferChain( &p_hdr, p_og );
685         ogg_stream_clear( &p_stream->os );
686     }
687
688     for( i = 0; i < p_sys->i_del_streams; i++ )
689     {
690         op.packet = NULL;
691         op.bytes  = 0;
692         op.b_o_s  = 0;
693         op.e_o_s  = 1;
694         op.granulepos = -1;
695         op.packetno = p_sys->pp_del_streams[i]->i_packet_no++;
696         ogg_stream_packetin( &p_sys->pp_del_streams[i]->os, &op );
697
698         p_og = OggStreamFlush( p_mux, &p_sys->pp_del_streams[i]->os, 0 );
699         sout_BufferChain( &p_hdr, p_og );
700         ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
701     }
702
703     return( p_hdr );
704 }
705
706 static void OggSetDate( sout_buffer_t *p_og, mtime_t i_dts, mtime_t i_length )
707 {
708     int i_count;
709     sout_buffer_t *p_tmp;
710     mtime_t i_delta;
711
712     for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
713     {
714         i_count++;
715     }
716     i_delta = i_length / i_count;
717
718     for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
719     {
720         p_tmp->i_dts    = i_dts;
721         p_tmp->i_length = i_delta;
722
723         i_dts += i_delta;
724     }
725 }
726
727 /*****************************************************************************
728  * Mux: multiplex available data in input fifos into the Ogg bitstream
729  *****************************************************************************/
730 static int Mux( sout_mux_t *p_mux )
731 {
732     sout_mux_sys_t *p_sys = p_mux->p_sys;
733     sout_buffer_t  *p_og = NULL;
734     int            i_stream;
735     mtime_t        i_dts;
736
737     if( p_sys->i_add_streams || p_sys->i_del_streams )
738     {
739         /* Open new ogg stream */
740         if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
741         {
742             msg_Dbg( p_mux, "waiting for data..." );
743             return( VLC_SUCCESS );
744         }
745
746         if( p_sys->i_streams )
747         {
748             /* Close current ogg stream */
749             int i;
750
751             msg_Dbg( p_mux, "writing footer" );
752             sout_BufferChain( &p_og, OggCreateFooter( p_mux, 0 ) );
753
754             /* Remove deleted logical streams */
755             for( i = 0; i < p_sys->i_del_streams; i++ )
756             {
757                 FREE( p_sys->pp_del_streams[i] );
758             }
759             FREE( p_sys->pp_del_streams );
760             p_sys->i_streams = 0;
761         }
762
763         msg_Dbg( p_mux, "writing header" );
764         p_sys->i_start_dts = i_dts;
765         p_sys->i_streams = p_mux->i_nb_inputs;
766         p_sys->i_del_streams = 0;
767         p_sys->i_add_streams = 0;
768         sout_BufferChain( &p_og, OggCreateHeader( p_mux, i_dts ) );
769
770         /* Write header and/or footer */
771         OggSetDate( p_og, i_dts, 0 );
772         sout_AccessOutWrite( p_mux->p_access, p_og );
773         p_og = NULL;
774     }
775
776     for( ;; )
777     {
778         sout_input_t  *p_input;
779         ogg_stream_t  *p_stream;
780         sout_buffer_t *p_data;
781         ogg_packet    op;
782
783         if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
784         {
785             return( VLC_SUCCESS );
786         }
787
788         p_input  = p_mux->pp_inputs[i_stream];
789         p_stream = (ogg_stream_t*)p_input->p_sys;
790         p_data   = sout_FifoGet( p_input->p_fifo );
791
792         if( p_stream->i_fourcc != VLC_FOURCC( 'v', 'o', 'r', 'b' ) &&
793             p_stream->i_fourcc != VLC_FOURCC( 't', 'h', 'e', 'o' ) )
794         {
795             sout_BufferReallocFromPreHeader( p_mux->p_sout, p_data, 1 );
796             p_data->p_buffer[0] = PACKET_IS_SYNCPOINT;      // FIXME
797         }
798
799         op.packet   = p_data->p_buffer;
800         op.bytes    = p_data->i_size;
801         op.b_o_s    = 0;
802         op.e_o_s    = 0;
803         op.packetno = p_stream->i_packet_no++;
804
805         if( p_stream->i_cat == AUDIO_ES )
806         {
807             if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
808             {
809                 /* number of sample from begining + current packet */
810                 op.granulepos =
811                     ( i_dts + p_data->i_length - p_sys->i_start_dts ) *
812                     p_input->p_fmt->i_sample_rate / (int64_t)1000000;
813             }
814             else
815             {
816                 /* number of sample from begining */
817                 op.granulepos = ( i_dts - p_sys->i_start_dts ) *
818                     p_stream->header.i_samples_per_unit / (int64_t)1000000;
819             }
820         }
821         else if( p_stream->i_cat == VIDEO_ES )
822         {
823             if( p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
824             {
825                 op.granulepos = op.packetno; /* FIXME */
826             }
827             else
828                 op.granulepos = ( i_dts - p_sys->i_start_dts )
829                                 * I64C(10) / p_stream->header.i_time_unit;
830         }
831         else if( p_stream->i_cat == SPU_ES )
832         {
833             /* granulepos is in milisec */
834             op.granulepos = ( i_dts - p_sys->i_start_dts ) / 1000;
835         }
836
837         ogg_stream_packetin( &p_stream->os, &op );
838
839         sout_BufferChain( &p_og, OggStreamPageOut( p_mux, &p_stream->os,
840                                                    p_data->i_dts ) );
841
842         if( p_og )
843         {
844             OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
845             p_stream->i_dts = -1;
846             p_stream->i_length = 0;
847
848             sout_AccessOutWrite( p_mux->p_access, p_og );
849
850             p_og = NULL;
851         }
852         else
853         {
854             if( p_stream->i_dts < 0 )
855             {
856                 p_stream->i_dts = p_data->i_dts;
857             }
858             p_stream->i_length += p_data->i_length;
859         }
860
861         sout_BufferDelete( p_mux->p_sout, p_data );
862     }
863
864     return( VLC_SUCCESS );
865 }