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