]> git.sesse.net Git - vlc/blob - modules/mux/ogg.c
* modules/codec/theora.c: don't forget to delete the sout instance.
[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.12 2003/09/28 21:54:21 gbazin Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@netcourrier.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/types.h>
31
32 #include <vlc/vlc.h>
33 #include <vlc/input.h>
34 #include <vlc/sout.h>
35
36 #include "codecs.h"
37
38 #include <ogg/ogg.h>
39
40 /*****************************************************************************
41  * Exported prototypes
42  *****************************************************************************/
43 static int  Open   ( vlc_object_t * );
44 static void Close  ( vlc_object_t * );
45
46 static int Capability(sout_mux_t *, int, void *, void * );
47 static int AddStream( sout_mux_t *, sout_input_t * );
48 static int DelStream( sout_mux_t *, sout_input_t * );
49 static int Mux      ( sout_mux_t * );
50
51 static sout_buffer_t *OggCreateHeader( sout_mux_t *, mtime_t );
52 static sout_buffer_t *OggCreateFooter( sout_mux_t *, mtime_t );
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57 vlc_module_begin();
58     set_description( _("Ogg/ogm muxer") );
59     set_capability( "sout mux", 10 );
60     add_shortcut( "ogg" );
61     add_shortcut( "ogm" );
62     set_callbacks( Open, Close );
63 vlc_module_end();
64
65 /*****************************************************************************
66  * Misc declarations
67  *****************************************************************************/
68 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
69
70 /* Structures used for OggDS headers used in ogm files */
71
72 #define PACKET_TYPE_HEADER   0x01
73 #define PACKET_TYPE_COMMENT  0x03
74 #define PACKET_IS_SYNCPOINT  0x08
75
76 typedef struct
77 #ifdef HAVE_ATTRIBUTE_PACKED
78     __attribute__((__packed__))
79 #endif
80 {
81     int32_t i_width;
82     int32_t i_height;
83 } ogg_stream_header_video_t;
84
85 typedef struct
86 #ifdef HAVE_ATTRIBUTE_PACKED
87     __attribute__((__packed__))
88 #endif
89 {
90     int16_t i_channels;
91     int16_t i_block_align;
92     int32_t i_avgbytespersec;
93 } ogg_stream_header_audio_t;
94
95 typedef struct
96 #ifdef HAVE_ATTRIBUTE_PACKED
97     __attribute__((__packed__))
98 #endif
99 {
100     uint8_t i_packet_type;
101
102     char stream_type[8];
103     char sub_type[4];
104
105     int32_t i_size;
106
107     int64_t i_time_unit;
108     int64_t i_samples_per_unit;
109     int32_t i_default_len;
110
111     int32_t i_buffer_size;
112     int16_t i_bits_per_sample;
113     int16_t i_padding_0;            // hum hum
114     union
115     {
116         ogg_stream_header_video_t video;
117         ogg_stream_header_audio_t audio;
118     } header;
119
120 } ogg_stream_header_t;
121
122 /* Helper writer functions */
123
124 #define SetWLE( p, v ) _SetWLE( (uint8_t*)p, v)
125 static void _SetWLE( uint8_t *p, uint16_t i_dw )
126 {
127     p[1] = ( i_dw >>  8 )&0xff;
128     p[0] = ( i_dw       )&0xff;
129 }
130
131 #define SetDWLE( p, v ) _SetDWLE( (uint8_t*)p, v)
132 static void _SetDWLE( uint8_t *p, uint32_t i_dw )
133 {
134     p[3] = ( i_dw >> 24 )&0xff;
135     p[2] = ( i_dw >> 16 )&0xff;
136     p[1] = ( i_dw >>  8 )&0xff;
137     p[0] = ( i_dw       )&0xff;
138 }
139 #define SetQWLE( p, v ) _SetQWLE( (uint8_t*)p, v)
140 static void _SetQWLE( uint8_t *p, uint64_t i_qw )
141 {
142     SetDWLE( p,   i_qw&0xffffffff );
143     SetDWLE( p+4, ( i_qw >> 32)&0xffffffff );
144 }
145
146 /*
147  * TODO  move this function to src/stream_output.c (used by nearly all muxers)
148  */
149 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
150 {
151     mtime_t i_dts;
152     int     i_stream;
153     int     i;
154
155     for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
156     {
157         sout_fifo_t  *p_fifo;
158
159         p_fifo = p_mux->pp_inputs[i]->p_fifo;
160
161         if( p_fifo->i_depth > 2 )
162         {
163             sout_buffer_t *p_buf;
164
165             p_buf = sout_FifoShow( p_fifo );
166             if( i_stream < 0 || p_buf->i_dts < i_dts )
167             {
168                 i_dts = p_buf->i_dts;
169                 i_stream = i;
170             }
171         }
172         else
173         {
174             // wait that all fifo have at least 3 packets (3 vorbis headers)
175             return( -1 );
176         }
177     }
178     if( pi_stream )
179     {
180         *pi_stream = i_stream;
181     }
182     if( pi_dts )
183     {
184         *pi_dts = i_dts;
185     }
186     return( i_stream );
187 }
188
189 /*****************************************************************************
190  * Definitions of structures and functions used by this plugins 
191  *****************************************************************************/
192 typedef struct
193 {
194     int i_cat;
195     int i_fourcc;
196
197     int b_new;
198
199     mtime_t i_dts;
200     mtime_t i_length;
201     int     i_packet_no;
202     ogg_stream_state os;
203
204     ogg_stream_header_t header;
205
206 } ogg_stream_t;
207
208 struct sout_mux_sys_t
209 {
210     int     i_streams;
211
212     mtime_t i_start_dts;
213
214     /* number of logical streams pending to be added */
215     int i_add_streams;
216
217     /* logical streams pending to be deleted */
218     int i_del_streams;
219     ogg_stream_t **pp_del_streams;
220 };
221
222 static void OggSetDate( sout_buffer_t *, mtime_t , mtime_t  );
223 static sout_buffer_t *OggStreamFlush( sout_mux_t *, ogg_stream_state *,
224                                       mtime_t );
225
226 /*****************************************************************************
227  * Open: Open muxer
228  *****************************************************************************/
229 static int Open( vlc_object_t *p_this )
230 {
231     sout_mux_t      *p_mux = (sout_mux_t*)p_this;
232     sout_mux_sys_t  *p_sys;
233
234     msg_Info( p_mux, "Open" );
235
236     p_sys                 = malloc( sizeof( sout_mux_sys_t ) );
237     p_sys->i_streams      = 0;
238     p_sys->i_add_streams  = 0;
239     p_sys->i_del_streams  = 0;
240     p_sys->pp_del_streams = 0;
241
242     p_mux->p_sys        = p_sys;
243     p_mux->pf_capacity  = Capability;
244     p_mux->pf_addstream = AddStream;
245     p_mux->pf_delstream = DelStream;
246     p_mux->pf_mux       = Mux;
247     p_mux->i_preheader  = 1;
248
249     return VLC_SUCCESS;
250 }
251
252 /*****************************************************************************
253  * Close: Finalize ogg bitstream and close muxer
254  *****************************************************************************/
255 static void Close( vlc_object_t * p_this )
256 {
257     sout_mux_t     *p_mux = (sout_mux_t*)p_this;
258     sout_mux_sys_t *p_sys = p_mux->p_sys;
259
260     msg_Info( p_mux, "Close" );
261
262     if( p_sys->i_del_streams )
263     {
264         sout_buffer_t *p_og = NULL;
265         mtime_t i_dts = -1;
266         int i;
267
268         /* Close the current ogg stream */
269         msg_Dbg( p_mux, "writing footer" );
270         sout_BufferChain( &p_og, OggCreateFooter( p_mux, 0 ) );
271
272         /* Remove deleted logical streams */
273         for( i = 0; i < p_sys->i_del_streams; i++ )
274         {
275             i_dts = p_sys->pp_del_streams[i]->i_dts;
276             ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
277             FREE( p_sys->pp_del_streams[i] );
278         }
279         FREE( p_sys->pp_del_streams );
280         p_sys->i_streams -= p_sys->i_del_streams;
281
282         /* Write footer */
283         OggSetDate( p_og, i_dts, 0 );
284         sout_AccessOutWrite( p_mux->p_access, p_og );
285     }
286
287     free( p_sys );
288 }
289
290 static int Capability( sout_mux_t *p_mux, int i_query, void *p_args,
291                        void *p_answer )
292 {
293    switch( i_query )
294    {
295         case SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME:
296             *(vlc_bool_t*)p_answer = VLC_TRUE;
297             return( SOUT_MUX_CAP_ERR_OK );
298         default:
299             return( SOUT_MUX_CAP_ERR_UNIMPLEMENTED );
300    }
301 }
302
303 /*****************************************************************************
304  * AddStream: Add an elementary stream to the muxed stream
305  *****************************************************************************/
306 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
307 {
308     sout_mux_sys_t  *p_sys = p_mux->p_sys;
309     ogg_stream_t        *p_stream;
310
311     msg_Dbg( p_mux, "adding input" );
312
313     p_input->p_sys = (void *)p_stream = malloc( sizeof( ogg_stream_t ) );
314
315     p_stream->i_cat       = p_input->p_fmt->i_cat;
316     p_stream->i_fourcc    = p_input->p_fmt->i_fourcc;
317     p_stream->i_packet_no = 0;
318
319     p_stream->header.i_packet_type = PACKET_TYPE_HEADER;
320     switch( p_input->p_fmt->i_cat )
321     {
322     case VIDEO_ES:
323         switch( p_stream->i_fourcc )
324         {
325         case VLC_FOURCC( 'm', 'p','4', 'v' ):
326         case VLC_FOURCC( 'D', 'I','V', '3' ):
327             memcpy( p_stream->header.stream_type, "video    ", 8 );
328             if( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p','4', 'v' ) )
329             {
330                 memcpy( p_stream->header.sub_type, "XVID", 4 );
331             }
332             else if( p_stream->i_fourcc == VLC_FOURCC( 'D', 'I','V', '3' ) )
333             {
334                 memcpy( p_stream->header.sub_type, "DIV3", 4 );
335             }
336             SetDWLE( &p_stream->header.i_size,
337                      sizeof( ogg_stream_header_t ) - 1);
338             /* XXX this won't make mplayer happy,
339              * but vlc can read that without any problem so...*/
340             SetQWLE( &p_stream->header.i_time_unit, 10*1000 );
341             //(int64_t)10*1000*1000/(int64_t)25 );  // FIXME (25fps)
342             SetQWLE( &p_stream->header.i_samples_per_unit, 1 );
343             SetDWLE( &p_stream->header.i_default_len, 0 );      /* ??? */
344             SetDWLE( &p_stream->header.i_buffer_size, 1024*1024 );
345             SetWLE( &p_stream->header.i_bits_per_sample, 0 );
346             SetDWLE( &p_stream->header.header.video.i_width,
347                      p_input->p_fmt->i_width );
348             SetDWLE( &p_stream->header.header.video.i_height,
349                      p_input->p_fmt->i_height );
350             msg_Dbg( p_mux, "mp4v/div3 stream" );
351             break;
352
353         case VLC_FOURCC( 't', 'h', 'e', 'o' ):
354             msg_Dbg( p_mux, "theora stream" );
355             break;
356
357         default:
358             FREE( p_input->p_sys );
359             return( VLC_EGENERIC );
360         }
361         break;
362
363     case AUDIO_ES:
364         switch( p_stream->i_fourcc )
365         {
366         case VLC_FOURCC( 'm', 'p','g', 'a' ):
367         case VLC_FOURCC( 'a', '5','2', ' ' ):
368             memcpy( p_stream->header.stream_type, "audio    ", 8 );
369             if( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p','g', 'a' ) )
370             {
371                 memcpy( p_stream->header.sub_type, "55  ", 4 );
372             }
373             else if( p_stream->i_fourcc == VLC_FOURCC( 'a', '5','2', ' ' ) )
374             {
375                 memcpy( p_stream->header.sub_type, "2000", 4 );
376             }
377             SetDWLE( &p_stream->header.i_size,
378                      sizeof( ogg_stream_header_t ) - 1);
379             SetQWLE( &p_stream->header.i_time_unit, 1000000 ); /*is it used ?*/
380             SetDWLE( &p_stream->header.i_default_len, 0 );     /* ??? */
381             SetDWLE( &p_stream->header.i_buffer_size, 30*1024 );
382             SetQWLE( &p_stream->header.i_samples_per_unit,
383                      p_input->p_fmt->i_sample_rate );
384             SetWLE( &p_stream->header.i_bits_per_sample, 0 );
385             SetDWLE( &p_stream->header.header.audio.i_channels,
386                      p_input->p_fmt->i_channels );
387             SetDWLE( &p_stream->header.header.audio.i_block_align,
388                      p_input->p_fmt->i_block_align );
389             SetDWLE( &p_stream->header.header.audio.i_avgbytespersec, 0 );
390             msg_Dbg( p_mux, "mpga/a52 stream" );
391             break;
392
393         case VLC_FOURCC( 'v', 'o', 'r', 'b' ):
394             msg_Dbg( p_mux, "vorbis stream" );
395             break;
396
397         default:
398             FREE( p_input->p_sys );
399             return( VLC_EGENERIC );
400         }
401         break;
402
403     case SPU_ES:
404         switch( p_stream->i_fourcc )
405         {
406         case VLC_FOURCC( 's', 'u','b', 't' ):
407             memcpy( p_stream->header.stream_type, "text     ", 8 );
408             msg_Dbg( p_mux, "subtitles stream" );
409             break;
410
411         default:
412             FREE( p_input->p_sys );
413             return( VLC_EGENERIC );
414         }
415         break;
416     default:
417         FREE( p_input->p_sys );
418         return( VLC_EGENERIC );
419     }
420
421     p_stream->b_new = VLC_TRUE;
422
423     p_sys->i_add_streams++;
424
425     return( VLC_SUCCESS );
426 }
427
428 /*****************************************************************************
429  * DelStream: Delete an elementary stream from the muxed stream
430  *****************************************************************************/
431 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
432 {
433     sout_mux_sys_t *p_sys  = p_mux->p_sys;
434     ogg_stream_t   *p_stream = (ogg_stream_t*)p_input->p_sys;
435     sout_buffer_t  *p_og;
436
437     msg_Dbg( p_mux, "removing input" );
438
439     /* flush all remaining data */
440     if( p_input->p_sys )
441     {
442         if( ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
443         {
444             OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
445             sout_AccessOutWrite( p_mux->p_access, p_og );
446         }
447
448         /* move input in delete queue */
449         if( !p_stream->b_new )
450         {
451             p_sys->pp_del_streams = realloc( p_sys->pp_del_streams,
452                                              (p_sys->i_del_streams + 1) *
453                                              sizeof(ogg_stream_t *) );
454             p_sys->pp_del_streams[p_sys->i_del_streams++] = p_stream;
455         }
456         else
457         {
458             /* Wasn't already added so get rid of it */
459             ogg_stream_clear( &p_stream->os );
460             FREE( p_stream );
461             p_sys->i_add_streams--;
462         }
463     }
464
465     p_input->p_sys = NULL;
466
467     return( 0 );
468 }
469
470 /*****************************************************************************
471  * Ogg bitstream manipulation routines
472  *****************************************************************************/
473 static sout_buffer_t *OggStreamFlush( sout_mux_t *p_mux,
474                                       ogg_stream_state *p_os, mtime_t i_pts )
475 {
476     sout_buffer_t *p_og, *p_og_first = NULL;
477     ogg_page      og;
478
479     for( ;; )
480     {
481         /* flush all data */
482         int i_result;
483         int i_size;
484         if( ( i_result = ogg_stream_flush( p_os, &og ) ) == 0 )
485         {
486             break;
487         }
488
489         i_size = og.header_len + og.body_len;
490         p_og = sout_BufferNew( p_mux->p_sout, i_size);
491
492         memcpy( p_og->p_buffer, og.header, og.header_len );
493         memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
494         p_og->i_size    = i_size;
495         p_og->i_dts     = 0;
496         p_og->i_pts     = i_pts;
497         p_og->i_length  = 0;
498
499         i_pts   = 0; // write it only once
500
501         sout_BufferChain( &p_og_first, p_og );
502     }
503
504     return( p_og_first );
505 }
506
507 static sout_buffer_t *OggStreamPageOut( sout_mux_t *p_mux,
508                                         ogg_stream_state *p_os, mtime_t i_pts )
509 {
510     sout_buffer_t *p_og, *p_og_first = NULL;
511     ogg_page      og;
512
513     for( ;; )
514     {
515         /* flush all data */
516         int i_result;
517         int i_size;
518         if( ( i_result = ogg_stream_pageout( p_os, &og ) ) == 0 )
519         {
520             break;
521         }
522
523         i_size = og.header_len + og.body_len;
524         p_og = sout_BufferNew( p_mux->p_sout, i_size);
525
526         memcpy( p_og->p_buffer, og.header, og.header_len );
527         memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
528         p_og->i_size    = i_size;
529         p_og->i_dts     = 0;
530         p_og->i_pts     = i_pts;
531         p_og->i_length  = 0;
532
533         i_pts   = 0; // write them only once
534
535         sout_BufferChain( &p_og_first, p_og );
536     }
537
538     return( p_og_first );
539 }
540
541 static sout_buffer_t *OggCreateHeader( sout_mux_t *p_mux, mtime_t i_dts )
542 {
543     sout_buffer_t *p_hdr = NULL;
544     sout_buffer_t *p_og;
545     ogg_packet    op;
546     int i;
547
548     /* Write header for each stream. All b_o_s (beginning of stream) packets
549      * must appear first in the ogg stream so we take care of them first. */
550     for( i = 0; i < p_mux->i_nb_inputs; i++ )
551     {
552         ogg_stream_t *p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
553         p_stream->b_new = VLC_FALSE;
554
555         msg_Dbg( p_mux, "creating header for %4.4s",
556                  (char *)&p_stream->i_fourcc );
557
558         ogg_stream_init( &p_stream->os, rand() );
559         p_stream->i_packet_no = 0;
560
561         if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
562             p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
563         {
564             /* Special case, headers are already there in the
565              * incoming stream */
566
567             /* first packet in order: vorbis info */
568             p_og = sout_FifoGet( p_mux->pp_inputs[i]->p_fifo );
569             op.packet = p_og->p_buffer;
570             op.bytes  = p_og->i_size;
571             op.b_o_s  = 1;
572             op.e_o_s  = 0;
573             op.granulepos = 0;
574             op.packetno = p_stream->i_packet_no++;
575             ogg_stream_packetin( &p_stream->os, &op );
576         }
577         else
578         {
579             /* ds header */
580             op.packet = (uint8_t*)&p_stream->header;
581             op.bytes  = sizeof( ogg_stream_header_t );
582             op.b_o_s  = 1;
583             op.e_o_s  = 0;
584             op.granulepos = 0;
585             op.packetno = p_stream->i_packet_no++;
586             ogg_stream_packetin( &p_stream->os, &op );
587         }
588
589         p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
590         sout_BufferChain( &p_hdr, p_og );
591     }
592
593     /* Take care of the non b_o_s headers */
594     for( i = 0; i < p_mux->i_nb_inputs; i++ )
595     {
596         ogg_stream_t *p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
597
598         if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
599             p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
600         {
601             /* Special case, headers are already there in the incoming stream.
602              * We need to gather them an mark them as headers. */
603             int j;
604             for( j = 0; j < 2; j++ )
605             {
606                 /* next packets in order: comments and codebooks */
607                 p_og = sout_FifoGet( p_mux->pp_inputs[i]->p_fifo );
608                 op.packet = p_og->p_buffer;
609                 op.bytes  = p_og->i_size;
610                 op.b_o_s  = 0;
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             }
616         }
617         else
618         {
619             uint8_t com[128];
620             int     i_com;
621
622             /* comment */
623             com[0] = PACKET_TYPE_COMMENT;
624             i_com = snprintf( &com[1], 128, VERSION" stream output" ) + 1;
625             op.packet = com;
626             op.bytes  = i_com;
627             op.b_o_s  = 0;
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         }
633
634         p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
635         sout_BufferChain( &p_hdr, p_og );
636     }
637
638     /* set HEADER flag */
639     for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
640     {
641         p_og->i_flags |= SOUT_BUFFER_FLAGS_HEADER;
642     }
643     return( p_hdr );
644 }
645
646 static sout_buffer_t *OggCreateFooter( sout_mux_t *p_mux, mtime_t i_dts )
647 {
648     sout_mux_sys_t *p_sys = p_mux->p_sys;
649     sout_buffer_t *p_hdr = NULL;
650     sout_buffer_t *p_og;
651     ogg_packet    op;
652     int i;
653
654     /* flush all remaining data */
655     for( i = 0; i < p_mux->i_nb_inputs; i++ )
656     {
657         ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
658
659         /* skip newly added streams */
660         if( p_stream->b_new ) continue;
661
662         if( ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
663         {
664             OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
665             sout_AccessOutWrite( p_mux->p_access, p_og );
666         }
667     }
668
669     /* Write eos packets for each stream. */
670     for( i = 0; i < p_mux->i_nb_inputs; i++ )
671     {
672         ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
673
674         /* skip newly added streams */
675         if( p_stream->b_new ) continue;
676
677         op.packet = NULL;
678         op.bytes  = 0;
679         op.b_o_s  = 0;
680         op.e_o_s  = 1;
681         op.granulepos = -1;
682         op.packetno = p_stream->i_packet_no++;
683         ogg_stream_packetin( &p_stream->os, &op );
684
685         p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
686         sout_BufferChain( &p_hdr, p_og );
687         ogg_stream_clear( &p_stream->os );
688     }
689
690     for( i = 0; i < p_sys->i_del_streams; i++ )
691     {
692         op.packet = NULL;
693         op.bytes  = 0;
694         op.b_o_s  = 0;
695         op.e_o_s  = 1;
696         op.granulepos = -1;
697         op.packetno = p_sys->pp_del_streams[i]->i_packet_no++;
698         ogg_stream_packetin( &p_sys->pp_del_streams[i]->os, &op );
699
700         p_og = OggStreamFlush( p_mux, &p_sys->pp_del_streams[i]->os, 0 );
701         sout_BufferChain( &p_hdr, p_og );
702         ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
703     }
704
705     return( p_hdr );
706 }
707
708 static void OggSetDate( sout_buffer_t *p_og, mtime_t i_dts, mtime_t i_length )
709 {
710     int i_count;
711     sout_buffer_t *p_tmp;
712     mtime_t i_delta;
713
714     for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
715     {
716         i_count++;
717     }
718     i_delta = i_length / i_count;
719
720     for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
721     {
722         p_tmp->i_dts    = i_dts;
723         p_tmp->i_length = i_delta;
724
725         i_dts += i_delta;
726     }
727 }
728
729 /*****************************************************************************
730  * Mux: multiplex available data in input fifos into the Ogg bitstream
731  *****************************************************************************/
732 static int Mux( sout_mux_t *p_mux )
733 {
734     sout_mux_sys_t *p_sys = p_mux->p_sys;
735     sout_buffer_t  *p_og = NULL;
736     int            i_stream;
737     mtime_t        i_dts;
738
739     if( p_sys->i_add_streams || p_sys->i_del_streams )
740     {
741         /* Open new ogg stream */
742         if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
743         {
744             msg_Dbg( p_mux, "waiting for data..." );
745             return( VLC_SUCCESS );
746         }
747
748         if( p_sys->i_streams )
749         {
750             /* Close current ogg stream */
751             int i;
752
753             msg_Dbg( p_mux, "writing footer" );
754             sout_BufferChain( &p_og, OggCreateFooter( p_mux, 0 ) );
755
756             /* Remove deleted logical streams */
757             for( i = 0; i < p_sys->i_del_streams; i++ )
758             {
759                 FREE( p_sys->pp_del_streams[i] );
760             }
761             FREE( p_sys->pp_del_streams );
762             p_sys->i_streams = 0;
763         }
764
765         msg_Dbg( p_mux, "writing header" );
766         p_sys->i_start_dts = i_dts;
767         p_sys->i_streams = p_mux->i_nb_inputs;
768         p_sys->i_del_streams = 0;
769         p_sys->i_add_streams = 0;
770         sout_BufferChain( &p_og, OggCreateHeader( p_mux, i_dts ) );
771
772         /* Write header and/or footer */
773         OggSetDate( p_og, i_dts, 0 );
774         sout_AccessOutWrite( p_mux->p_access, p_og );
775         p_og = NULL;
776     }
777
778     for( ;; )
779     {
780         sout_input_t  *p_input;
781         ogg_stream_t  *p_stream;
782         sout_buffer_t *p_data;
783         ogg_packet    op;
784
785         if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
786         {
787             return( VLC_SUCCESS );
788         }
789
790         p_input  = p_mux->pp_inputs[i_stream];
791         p_stream = (ogg_stream_t*)p_input->p_sys;
792         p_data   = sout_FifoGet( p_input->p_fifo );
793
794         if( p_stream->i_fourcc != VLC_FOURCC( 'v', 'o', 'r', 'b' ) &&
795             p_stream->i_fourcc != VLC_FOURCC( 't', 'h', 'e', 'o' ) )
796         {
797             sout_BufferReallocFromPreHeader( p_mux->p_sout, p_data, 1 );
798             p_data->p_buffer[0] = PACKET_IS_SYNCPOINT;      // FIXME
799         }
800
801         op.packet   = p_data->p_buffer;
802         op.bytes    = p_data->i_size;
803         op.b_o_s    = 0;
804         op.e_o_s    = 0;
805         op.packetno = p_stream->i_packet_no++;
806
807         if( p_stream->i_cat == AUDIO_ES )
808         {
809             if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
810             {
811                 /* number of sample from begining + current packet */
812                 op.granulepos =
813                     ( i_dts + p_data->i_length - p_sys->i_start_dts ) *
814                     p_input->p_fmt->i_sample_rate / (int64_t)1000000;
815             }
816             else
817             {
818                 /* number of sample from begining */
819                 op.granulepos = ( i_dts - p_sys->i_start_dts ) *
820                     p_stream->header.i_samples_per_unit / (int64_t)1000000;
821             }
822         }
823         else if( p_stream->i_cat == VIDEO_ES )
824         {
825             if( p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
826             {
827                 op.granulepos = op.packetno; /* FIXME */
828             }
829             else
830                 op.granulepos = ( i_dts - p_sys->i_start_dts ) / 1000;
831         }
832         else if( p_stream->i_cat == SPU_ES )
833         {
834             /* granulepos is in milisec */
835             op.granulepos = ( i_dts - p_sys->i_start_dts ) / 1000;
836         }
837
838         ogg_stream_packetin( &p_stream->os, &op );
839
840         sout_BufferChain( &p_og, OggStreamPageOut( p_mux, &p_stream->os,
841                                                    p_data->i_dts ) );
842
843         if( p_og )
844         {
845             OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
846             p_stream->i_dts = -1;
847             p_stream->i_length = 0;
848
849             sout_AccessOutWrite( p_mux->p_access, p_og );
850
851             p_og = NULL;
852         }
853         else
854         {
855             if( p_stream->i_dts < 0 )
856             {
857                 p_stream->i_dts = p_data->i_dts;
858             }
859             p_stream->i_length += p_data->i_length;
860         }
861
862         sout_BufferDelete( p_mux->p_sout, p_data );
863     }
864
865     return( VLC_SUCCESS );
866 }