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