1 /*****************************************************************************
2 * ogg.c: ogg muxer module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: ogg.c,v 1.25 2003/12/07 17:09:33 gbazin Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@netcourrier.com>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
36 #include <vlc/input.h>
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Open ( vlc_object_t * );
47 static void Close ( vlc_object_t * );
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 * );
54 static sout_buffer_t *OggCreateHeader( sout_mux_t *, mtime_t );
55 static sout_buffer_t *OggCreateFooter( sout_mux_t *, mtime_t );
57 /*****************************************************************************
59 *****************************************************************************/
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 );
68 /*****************************************************************************
70 *****************************************************************************/
71 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
73 /* Structures used for OggDS headers used in ogm files */
75 #define PACKET_TYPE_HEADER 0x01
76 #define PACKET_TYPE_COMMENT 0x03
77 #define PACKET_IS_SYNCPOINT 0x08
80 #ifdef HAVE_ATTRIBUTE_PACKED
81 __attribute__((__packed__))
86 } oggds_header_video_t;
89 #ifdef HAVE_ATTRIBUTE_PACKED
90 __attribute__((__packed__))
94 int16_t i_block_align;
95 int32_t i_avgbytespersec;
96 } oggds_header_audio_t;
99 #ifdef HAVE_ATTRIBUTE_PACKED
100 __attribute__((__packed__))
103 uint8_t i_packet_type;
111 int64_t i_samples_per_unit;
112 int32_t i_default_len;
114 int32_t i_buffer_size;
115 int16_t i_bits_per_sample;
117 int16_t i_padding_0; /* Because the original is using MSVC packing style */
121 oggds_header_video_t video;
122 oggds_header_audio_t audio;
125 int32_t i_padding_1; /* Because the original is using MSVC packing style */
129 /* Helper writer functions */
131 #define SetWLE( p, v ) _SetWLE( (uint8_t*)p, v)
132 static void _SetWLE( uint8_t *p, uint16_t i_dw )
134 p[1] = ( i_dw >> 8 )&0xff;
135 p[0] = ( i_dw )&0xff;
138 #define SetDWLE( p, v ) _SetDWLE( (uint8_t*)p, v)
139 static void _SetDWLE( uint8_t *p, uint32_t i_dw )
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;
146 #define SetQWLE( p, v ) _SetQWLE( (uint8_t*)p, v)
147 static void _SetQWLE( uint8_t *p, uint64_t i_qw )
149 SetDWLE( p, i_qw&0xffffffff );
150 SetDWLE( p+4, ( i_qw >> 32)&0xffffffff );
154 * TODO move this function to src/stream_output.c (used by nearly all muxers)
156 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
162 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
166 p_fifo = p_mux->pp_inputs[i]->p_fifo;
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;
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 ) )
177 sout_buffer_t *p_buf;
179 p_buf = sout_FifoShow( p_fifo );
180 if( i_stream < 0 || p_buf->i_dts < i_dts )
182 i_dts = p_buf->i_dts;
188 // wait that all fifo have at least 3 packets (3 vorbis headers)
194 *pi_stream = i_stream;
203 /*****************************************************************************
204 * Definitions of structures and functions used by this plugins
205 *****************************************************************************/
217 int i_keyframe_granule_shift; /* Theora only */
220 oggds_header_t oggds_header;
222 sout_buffer_t *pp_sout_headers[3];
227 struct sout_mux_sys_t
232 int i_next_serial_no;
234 /* number of logical streams pending to be added */
237 /* logical streams pending to be deleted */
239 ogg_stream_t **pp_del_streams;
242 static void OggSetDate( sout_buffer_t *, mtime_t , mtime_t );
243 static sout_buffer_t *OggStreamFlush( sout_mux_t *, ogg_stream_state *,
246 /*****************************************************************************
248 *****************************************************************************/
249 static int Open( vlc_object_t *p_this )
251 sout_mux_t *p_mux = (sout_mux_t*)p_this;
252 sout_mux_sys_t *p_sys;
254 msg_Info( p_mux, "Open" );
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;
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;
267 p_mux->i_preheader = 1;
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();
278 /*****************************************************************************
279 * Close: Finalize ogg bitstream and close muxer
280 *****************************************************************************/
281 static void Close( vlc_object_t * p_this )
283 sout_mux_t *p_mux = (sout_mux_t*)p_this;
284 sout_mux_sys_t *p_sys = p_mux->p_sys;
286 msg_Info( p_mux, "Close" );
288 if( p_sys->i_del_streams )
290 sout_buffer_t *p_og = NULL;
294 /* Close the current ogg stream */
295 msg_Dbg( p_mux, "writing footer" );
296 sout_BufferChain( &p_og, OggCreateFooter( p_mux, 0 ) );
298 /* Remove deleted logical streams */
299 for( i = 0; i < p_sys->i_del_streams; i++ )
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] );
305 FREE( p_sys->pp_del_streams );
306 p_sys->i_streams -= p_sys->i_del_streams;
309 OggSetDate( p_og, i_dts, 0 );
310 sout_AccessOutWrite( p_mux->p_access, p_og );
316 static int Capability( sout_mux_t *p_mux, int i_query, void *p_args,
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 );
328 return( SOUT_MUX_CAP_ERR_UNIMPLEMENTED );
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 )
337 sout_mux_sys_t *p_sys = p_mux->p_sys;
338 ogg_stream_t *p_stream;
340 msg_Dbg( p_mux, "adding input" );
342 p_input->p_sys = (void *)p_stream = malloc( sizeof( ogg_stream_t ) );
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;
349 p_stream->i_sout_headers = 0;
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 )
356 switch( p_stream->i_fourcc )
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' ) )
364 memcpy( p_stream->oggds_header.sub_type, "XVID", 4 );
366 else if( p_stream->i_fourcc == VLC_FOURCC( 'D', 'I','V', '3' ) )
368 memcpy( p_stream->oggds_header.sub_type, "DIV3", 4 );
372 memcpy(p_stream->oggds_header.sub_type,&p_stream->i_fourcc,4);
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" );
389 case VLC_FOURCC( 't', 'h', 'e', 'o' ):
390 msg_Dbg( p_mux, "theora stream" );
394 FREE( p_input->p_sys );
395 return( VLC_EGENERIC );
400 switch( p_stream->i_fourcc )
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' ) )
407 memcpy( p_stream->oggds_header.sub_type, "55 ", 4 );
409 else if( p_stream->i_fourcc == VLC_FOURCC( 'a', '5','2', ' ' ) )
411 memcpy( p_stream->oggds_header.sub_type, "2000", 4 );
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" );
429 case VLC_FOURCC( 'v', 'o', 'r', 'b' ):
430 msg_Dbg( p_mux, "vorbis stream" );
433 case VLC_FOURCC( 's', 'p', 'x', ' ' ):
434 msg_Dbg( p_mux, "speex stream" );
437 case VLC_FOURCC( 'f', 'l', 'a', 'c' ):
438 msg_Dbg( p_mux, "flac stream" );
442 FREE( p_input->p_sys );
443 return( VLC_EGENERIC );
448 switch( p_stream->i_fourcc )
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" );
456 FREE( p_input->p_sys );
457 return( VLC_EGENERIC );
461 FREE( p_input->p_sys );
462 return( VLC_EGENERIC );
465 p_stream->b_new = VLC_TRUE;
467 p_sys->i_add_streams++;
469 return( VLC_SUCCESS );
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 )
477 sout_mux_sys_t *p_sys = p_mux->p_sys;
478 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
481 msg_Dbg( p_mux, "removing input" );
483 /* flush all remaining data */
488 if( ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
490 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
491 sout_AccessOutWrite( p_mux->p_access, p_og );
494 for( i = 0; i < p_stream->i_sout_headers; i++ )
496 sout_BufferDelete( p_mux->p_sout, p_stream->pp_sout_headers[i] );
497 p_stream->i_sout_headers = 0;
500 /* move input in delete queue */
501 if( !p_stream->b_new )
503 p_sys->pp_del_streams = realloc( p_sys->pp_del_streams,
504 (p_sys->i_del_streams + 1) *
505 sizeof(ogg_stream_t *) );
506 p_sys->pp_del_streams[p_sys->i_del_streams++] = p_stream;
510 /* Wasn't already added so get rid of it */
511 ogg_stream_clear( &p_stream->os );
513 p_sys->i_add_streams--;
517 p_input->p_sys = NULL;
522 /*****************************************************************************
523 * Ogg bitstream manipulation routines
524 *****************************************************************************/
525 static sout_buffer_t *OggStreamFlush( sout_mux_t *p_mux,
526 ogg_stream_state *p_os, mtime_t i_pts )
528 sout_buffer_t *p_og, *p_og_first = NULL;
536 if( ( i_result = ogg_stream_flush( p_os, &og ) ) == 0 )
541 i_size = og.header_len + og.body_len;
542 p_og = sout_BufferNew( p_mux->p_sout, i_size);
544 memcpy( p_og->p_buffer, og.header, og.header_len );
545 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
546 p_og->i_size = i_size;
551 i_pts = 0; // write it only once
553 sout_BufferChain( &p_og_first, p_og );
556 return( p_og_first );
559 static sout_buffer_t *OggStreamPageOut( sout_mux_t *p_mux,
560 ogg_stream_state *p_os, mtime_t i_pts )
562 sout_buffer_t *p_og, *p_og_first = NULL;
570 if( ( i_result = ogg_stream_pageout( p_os, &og ) ) == 0 )
575 i_size = og.header_len + og.body_len;
576 p_og = sout_BufferNew( p_mux->p_sout, i_size);
578 memcpy( p_og->p_buffer, og.header, og.header_len );
579 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
580 p_og->i_size = i_size;
585 i_pts = 0; // write them only once
587 sout_BufferChain( &p_og_first, p_og );
590 return( p_og_first );
593 static sout_buffer_t *OggCreateHeader( sout_mux_t *p_mux, mtime_t i_dts )
595 sout_buffer_t *p_hdr = NULL;
600 /* Write header for each stream. All b_o_s (beginning of stream) packets
601 * must appear first in the ogg stream so we take care of them first. */
602 for( i = 0; i < p_mux->i_nb_inputs; i++ )
604 ogg_stream_t *p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
605 p_stream->b_new = VLC_FALSE;
607 msg_Dbg( p_mux, "creating header for %4.4s",
608 (char *)&p_stream->i_fourcc );
610 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
611 p_stream->i_packet_no = 0;
613 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
614 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) ||
615 p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
617 /* Special case, headers are already there in the
618 * incoming stream or we backed them up earlier */
620 /* first packet in order: vorbis/speex/theora info */
621 if( !p_stream->i_sout_headers )
623 p_og = sout_FifoGet( p_mux->pp_inputs[i]->p_fifo );
624 op.packet = p_og->p_buffer;
625 op.bytes = p_og->i_size;
629 op.packetno = p_stream->i_packet_no++;
630 ogg_stream_packetin( &p_stream->os, &op );
631 p_stream->pp_sout_headers[0] =
632 OggStreamFlush( p_mux, &p_stream->os, 0 );
633 p_stream->i_sout_headers++;
635 p_og = sout_BufferDuplicate( p_mux->p_sout,
636 p_stream->pp_sout_headers[0] );
638 /* Get keyframe_granule_shift for theora granulepos calculation */
639 if( p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
641 int i_keyframe_frequency_force = 1 << (op.packet[36] >> 3);
643 /* granule_shift = i_log( frequency_force -1 ) */
644 p_stream->i_keyframe_granule_shift = 0;
645 i_keyframe_frequency_force--;
646 while( i_keyframe_frequency_force )
648 p_stream->i_keyframe_granule_shift++;
649 i_keyframe_frequency_force >>= 1;
653 else if( p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
655 /* flac stream marker (yeah, only that in the 1st packet) */
661 op.packetno = p_stream->i_packet_no++;
662 ogg_stream_packetin( &p_stream->os, &op );
663 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
668 op.packet = (uint8_t*)&p_stream->oggds_header;
669 op.bytes = sizeof( oggds_header_t );
673 op.packetno = p_stream->i_packet_no++;
674 ogg_stream_packetin( &p_stream->os, &op );
675 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
678 sout_BufferChain( &p_hdr, p_og );
681 /* Take care of the non b_o_s headers */
682 for( i = 0; i < p_mux->i_nb_inputs; i++ )
684 ogg_stream_t *p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
686 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
687 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) ||
688 p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
690 /* Special case, headers are already there in the incoming stream.
691 * We need to gather them an mark them as headers. */
693 for( j = 0; j < 2; j++ )
695 if( p_stream->i_sout_headers < j + 2 )
697 /* next packets in order: comments and codebooks */
698 p_og = sout_FifoGet( p_mux->pp_inputs[i]->p_fifo );
699 op.packet = p_og->p_buffer;
700 op.bytes = p_og->i_size;
704 op.packetno = p_stream->i_packet_no++;
705 ogg_stream_packetin( &p_stream->os, &op );
706 p_stream->pp_sout_headers[j+1] =
707 OggStreamFlush( p_mux, &p_stream->os, 0 );
708 p_stream->i_sout_headers++;
711 p_og = sout_BufferDuplicate( p_mux->p_sout,
712 p_stream->pp_sout_headers[j+1] );
713 sout_BufferChain( &p_hdr, p_og );
716 else if( p_stream->i_fourcc != VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
722 com[0] = PACKET_TYPE_COMMENT;
723 i_com = snprintf( &com[1], 128, VERSION" stream output" ) + 1;
729 op.packetno = p_stream->i_packet_no++;
730 ogg_stream_packetin( &p_stream->os, &op );
731 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
732 sout_BufferChain( &p_hdr, p_og );
735 /* Special case for mp4v and flac */
736 if( ( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p', '4', 'v' ) ||
737 p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) ) &&
738 p_mux->pp_inputs[i]->p_fmt->i_extra )
740 /* Send a packet with the VOL data for mp4v
741 * or STREAMINFO for flac */
742 msg_Dbg( p_mux, "writing extra data" );
743 op.bytes = p_mux->pp_inputs[i]->p_fmt->i_extra;
744 op.packet = p_mux->pp_inputs[i]->p_fmt->p_extra;
745 if( p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
747 /* Skip the flac stream marker */
748 ((uint8_t *)op.bytes) -= 4;
749 ((uint8_t *)op.packet) += 4;
754 op.packetno = p_stream->i_packet_no++;
755 ogg_stream_packetin( &p_stream->os, &op );
756 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
757 sout_BufferChain( &p_hdr, p_og );
761 /* set HEADER flag */
762 for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
764 p_og->i_flags |= SOUT_BUFFER_FLAGS_HEADER;
769 static sout_buffer_t *OggCreateFooter( sout_mux_t *p_mux, mtime_t i_dts )
771 sout_mux_sys_t *p_sys = p_mux->p_sys;
772 sout_buffer_t *p_hdr = NULL;
777 /* flush all remaining data */
778 for( i = 0; i < p_mux->i_nb_inputs; i++ )
780 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
782 /* skip newly added streams */
783 if( p_stream->b_new ) continue;
785 if( ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
787 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
788 sout_AccessOutWrite( p_mux->p_access, p_og );
792 /* Write eos packets for each stream. */
793 for( i = 0; i < p_mux->i_nb_inputs; i++ )
795 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
797 /* skip newly added streams */
798 if( p_stream->b_new ) continue;
805 op.packetno = p_stream->i_packet_no++;
806 ogg_stream_packetin( &p_stream->os, &op );
808 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
809 sout_BufferChain( &p_hdr, p_og );
810 ogg_stream_clear( &p_stream->os );
813 for( i = 0; i < p_sys->i_del_streams; i++ )
820 op.packetno = p_sys->pp_del_streams[i]->i_packet_no++;
821 ogg_stream_packetin( &p_sys->pp_del_streams[i]->os, &op );
823 p_og = OggStreamFlush( p_mux, &p_sys->pp_del_streams[i]->os, 0 );
824 sout_BufferChain( &p_hdr, p_og );
825 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
831 static void OggSetDate( sout_buffer_t *p_og, mtime_t i_dts, mtime_t i_length )
834 sout_buffer_t *p_tmp;
837 for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
841 i_delta = i_length / i_count;
843 for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
845 p_tmp->i_dts = i_dts;
846 p_tmp->i_length = i_delta;
852 /*****************************************************************************
853 * Mux: multiplex available data in input fifos into the Ogg bitstream
854 *****************************************************************************/
855 static int Mux( sout_mux_t *p_mux )
857 sout_mux_sys_t *p_sys = p_mux->p_sys;
858 sout_buffer_t *p_og = NULL;
862 if( p_sys->i_add_streams || p_sys->i_del_streams )
864 /* Open new ogg stream */
865 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
867 msg_Dbg( p_mux, "waiting for data..." );
868 return( VLC_SUCCESS );
871 if( p_sys->i_streams )
873 /* Close current ogg stream */
876 msg_Dbg( p_mux, "writing footer" );
877 sout_BufferChain( &p_og, OggCreateFooter( p_mux, 0 ) );
879 /* Remove deleted logical streams */
880 for( i = 0; i < p_sys->i_del_streams; i++ )
882 FREE( p_sys->pp_del_streams[i] );
884 FREE( p_sys->pp_del_streams );
885 p_sys->i_streams = 0;
888 msg_Dbg( p_mux, "writing header" );
889 p_sys->i_start_dts = i_dts;
890 p_sys->i_streams = p_mux->i_nb_inputs;
891 p_sys->i_del_streams = 0;
892 p_sys->i_add_streams = 0;
893 sout_BufferChain( &p_og, OggCreateHeader( p_mux, i_dts ) );
895 /* Write header and/or footer */
896 OggSetDate( p_og, i_dts, 0 );
897 sout_AccessOutWrite( p_mux->p_access, p_og );
903 sout_input_t *p_input;
904 ogg_stream_t *p_stream;
905 sout_buffer_t *p_data;
908 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
910 return( VLC_SUCCESS );
913 p_input = p_mux->pp_inputs[i_stream];
914 p_stream = (ogg_stream_t*)p_input->p_sys;
915 p_data = sout_FifoGet( p_input->p_fifo );
917 if( p_stream->i_fourcc != VLC_FOURCC( 'v', 'o', 'r', 'b' ) &&
918 p_stream->i_fourcc != VLC_FOURCC( 'f', 'l', 'a', 'c' ) &&
919 p_stream->i_fourcc != VLC_FOURCC( 's', 'p', 'x', ' ' ) &&
920 p_stream->i_fourcc != VLC_FOURCC( 't', 'h', 'e', 'o' ) )
922 sout_BufferReallocFromPreHeader( p_mux->p_sout, p_data, 1 );
923 p_data->p_buffer[0] = PACKET_IS_SYNCPOINT; // FIXME
926 op.packet = p_data->p_buffer;
927 op.bytes = p_data->i_size;
930 op.packetno = p_stream->i_packet_no++;
932 if( p_stream->i_cat == AUDIO_ES )
934 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
935 p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) ||
936 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) )
938 /* number of sample from begining + current packet */
940 ( i_dts - p_sys->i_start_dts + p_data->i_length ) *
941 (mtime_t)p_input->p_fmt->audio.i_rate / I64C(1000000);
945 /* number of sample from begining */
946 op.granulepos = ( i_dts - p_sys->i_start_dts ) *
947 p_stream->oggds_header.i_samples_per_unit / I64C(1000000);
950 else if( p_stream->i_cat == VIDEO_ES )
952 if( p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
954 /* FIXME, we assume only keyframes and 25fps */
955 op.granulepos = ( ( i_dts - p_sys->i_start_dts ) * I64C(25)
956 / I64C(1000000) ) << p_stream->i_keyframe_granule_shift;
959 op.granulepos = ( i_dts - p_sys->i_start_dts ) * I64C(10) /
960 p_stream->oggds_header.i_time_unit;
962 else if( p_stream->i_cat == SPU_ES )
964 /* granulepos is in milisec */
965 op.granulepos = ( i_dts - p_sys->i_start_dts ) / 1000;
968 ogg_stream_packetin( &p_stream->os, &op );
970 if( p_stream->i_cat == SPU_ES ||
971 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) )
973 /* Subtitles or Speex packets are quite small so they
974 * need to be flushed to be sent on time */
975 sout_BufferChain( &p_og, OggStreamFlush( p_mux, &p_stream->os,
980 sout_BufferChain( &p_og, OggStreamPageOut( p_mux, &p_stream->os,
986 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
987 p_stream->i_dts = -1;
988 p_stream->i_length = 0;
990 sout_AccessOutWrite( p_mux->p_access, p_og );
996 if( p_stream->i_dts < 0 )
998 p_stream->i_dts = p_data->i_dts;
1000 p_stream->i_length += p_data->i_length;
1003 sout_BufferDelete( p_mux->p_sout, p_data );
1006 return( VLC_SUCCESS );