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