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