1 /*****************************************************************************
2 * ogg.c: ogg muxer module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: ogg.c,v 1.21 2003/11/21 15:32:08 fenrir 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 );
325 return( SOUT_MUX_CAP_ERR_UNIMPLEMENTED );
329 /*****************************************************************************
330 * AddStream: Add an elementary stream to the muxed stream
331 *****************************************************************************/
332 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
334 sout_mux_sys_t *p_sys = p_mux->p_sys;
335 ogg_stream_t *p_stream;
337 msg_Dbg( p_mux, "adding input" );
339 p_input->p_sys = (void *)p_stream = malloc( sizeof( ogg_stream_t ) );
341 p_stream->i_cat = p_input->p_fmt->i_cat;
342 p_stream->i_fourcc = p_input->p_fmt->i_codec;
343 p_stream->i_serial_no = p_sys->i_next_serial_no++;
344 p_stream->i_packet_no = 0;
346 p_stream->i_sout_headers = 0;
348 memset( &p_stream->oggds_header, 0, sizeof(p_stream->oggds_header) );
349 p_stream->oggds_header.i_packet_type = PACKET_TYPE_HEADER;
350 switch( p_input->p_fmt->i_cat )
353 switch( p_stream->i_fourcc )
355 case VLC_FOURCC( 'm', 'p','4', 'v' ):
356 case VLC_FOURCC( 'D', 'I','V', '3' ):
357 memcpy( p_stream->oggds_header.stream_type, "video", 5 );
358 if( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p','4', 'v' ) )
360 memcpy( p_stream->oggds_header.sub_type, "XVID", 4 );
362 else if( p_stream->i_fourcc == VLC_FOURCC( 'D', 'I','V', '3' ) )
364 memcpy( p_stream->oggds_header.sub_type, "DIV3", 4 );
366 SetDWLE( &p_stream->oggds_header.i_size,
367 sizeof( oggds_header_t ) - 1);
368 SetQWLE( &p_stream->oggds_header.i_time_unit,
369 I64C(10000000)/(int64_t)25 ); // FIXME (25fps)
370 SetQWLE( &p_stream->oggds_header.i_samples_per_unit, 1 );
371 SetDWLE( &p_stream->oggds_header.i_default_len, 1 ); /* ??? */
372 SetDWLE( &p_stream->oggds_header.i_buffer_size, 1024*1024 );
373 SetWLE( &p_stream->oggds_header.i_bits_per_sample, 0 );
374 SetDWLE( &p_stream->oggds_header.header.video.i_width,
375 p_input->p_fmt->video.i_width );
376 SetDWLE( &p_stream->oggds_header.header.video.i_height,
377 p_input->p_fmt->video.i_height );
378 msg_Dbg( p_mux, "mp4v/div3 stream" );
381 case VLC_FOURCC( 't', 'h', 'e', 'o' ):
382 msg_Dbg( p_mux, "theora stream" );
386 FREE( p_input->p_sys );
387 return( VLC_EGENERIC );
392 switch( p_stream->i_fourcc )
394 case VLC_FOURCC( 'm', 'p','g', 'a' ):
395 case VLC_FOURCC( 'a', '5','2', ' ' ):
396 memcpy( p_stream->oggds_header.stream_type, "audio", 5 );
397 if( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p','g', 'a' ) )
399 memcpy( p_stream->oggds_header.sub_type, "55 ", 4 );
401 else if( p_stream->i_fourcc == VLC_FOURCC( 'a', '5','2', ' ' ) )
403 memcpy( p_stream->oggds_header.sub_type, "2000", 4 );
405 SetDWLE( &p_stream->oggds_header.i_size,
406 sizeof( oggds_header_t ) - 1);
407 SetQWLE( &p_stream->oggds_header.i_time_unit, 0 /* not used */ );
408 SetDWLE( &p_stream->oggds_header.i_default_len, 1 );
409 SetDWLE( &p_stream->oggds_header.i_buffer_size, 30*1024 );
410 SetQWLE( &p_stream->oggds_header.i_samples_per_unit,
411 p_input->p_fmt->audio.i_rate );
412 SetWLE( &p_stream->oggds_header.i_bits_per_sample, 0 );
413 SetDWLE( &p_stream->oggds_header.header.audio.i_channels,
414 p_input->p_fmt->audio.i_channels );
415 SetDWLE( &p_stream->oggds_header.header.audio.i_block_align,
416 p_input->p_fmt->audio.i_blockalign );
417 SetDWLE( &p_stream->oggds_header.header.audio.i_avgbytespersec, 0);
418 msg_Dbg( p_mux, "mpga/a52 stream" );
421 case VLC_FOURCC( 'v', 'o', 'r', 'b' ):
422 msg_Dbg( p_mux, "vorbis stream" );
425 case VLC_FOURCC( 's', 'p', 'x', ' ' ):
426 msg_Dbg( p_mux, "speex stream" );
430 FREE( p_input->p_sys );
431 return( VLC_EGENERIC );
436 switch( p_stream->i_fourcc )
438 case VLC_FOURCC( 's', 'u','b', 't' ):
439 memcpy( p_stream->oggds_header.stream_type, "text", 4 );
440 msg_Dbg( p_mux, "subtitles stream" );
444 FREE( p_input->p_sys );
445 return( VLC_EGENERIC );
449 FREE( p_input->p_sys );
450 return( VLC_EGENERIC );
453 p_stream->b_new = VLC_TRUE;
455 p_sys->i_add_streams++;
457 return( VLC_SUCCESS );
460 /*****************************************************************************
461 * DelStream: Delete an elementary stream from the muxed stream
462 *****************************************************************************/
463 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
465 sout_mux_sys_t *p_sys = p_mux->p_sys;
466 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
469 msg_Dbg( p_mux, "removing input" );
471 /* flush all remaining data */
476 if( ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
478 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
479 sout_AccessOutWrite( p_mux->p_access, p_og );
482 for( i = 0; i < p_stream->i_sout_headers; i++ )
484 sout_BufferDelete( p_mux->p_sout, p_stream->pp_sout_headers[i] );
485 p_stream->i_sout_headers = 0;
488 /* move input in delete queue */
489 if( !p_stream->b_new )
491 p_sys->pp_del_streams = realloc( p_sys->pp_del_streams,
492 (p_sys->i_del_streams + 1) *
493 sizeof(ogg_stream_t *) );
494 p_sys->pp_del_streams[p_sys->i_del_streams++] = p_stream;
498 /* Wasn't already added so get rid of it */
499 ogg_stream_clear( &p_stream->os );
501 p_sys->i_add_streams--;
505 p_input->p_sys = NULL;
510 /*****************************************************************************
511 * Ogg bitstream manipulation routines
512 *****************************************************************************/
513 static sout_buffer_t *OggStreamFlush( sout_mux_t *p_mux,
514 ogg_stream_state *p_os, mtime_t i_pts )
516 sout_buffer_t *p_og, *p_og_first = NULL;
524 if( ( i_result = ogg_stream_flush( p_os, &og ) ) == 0 )
529 i_size = og.header_len + og.body_len;
530 p_og = sout_BufferNew( p_mux->p_sout, i_size);
532 memcpy( p_og->p_buffer, og.header, og.header_len );
533 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
534 p_og->i_size = i_size;
539 i_pts = 0; // write it only once
541 sout_BufferChain( &p_og_first, p_og );
544 return( p_og_first );
547 static sout_buffer_t *OggStreamPageOut( sout_mux_t *p_mux,
548 ogg_stream_state *p_os, mtime_t i_pts )
550 sout_buffer_t *p_og, *p_og_first = NULL;
558 if( ( i_result = ogg_stream_pageout( p_os, &og ) ) == 0 )
563 i_size = og.header_len + og.body_len;
564 p_og = sout_BufferNew( p_mux->p_sout, i_size);
566 memcpy( p_og->p_buffer, og.header, og.header_len );
567 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
568 p_og->i_size = i_size;
573 i_pts = 0; // write them only once
575 sout_BufferChain( &p_og_first, p_og );
578 return( p_og_first );
581 static sout_buffer_t *OggCreateHeader( sout_mux_t *p_mux, mtime_t i_dts )
583 sout_buffer_t *p_hdr = NULL;
588 /* Write header for each stream. All b_o_s (beginning of stream) packets
589 * must appear first in the ogg stream so we take care of them first. */
590 for( i = 0; i < p_mux->i_nb_inputs; i++ )
592 ogg_stream_t *p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
593 p_stream->b_new = VLC_FALSE;
595 msg_Dbg( p_mux, "creating header for %4.4s",
596 (char *)&p_stream->i_fourcc );
598 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
599 p_stream->i_packet_no = 0;
601 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
602 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) ||
603 p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
605 /* Special case, headers are already there in the
606 * incoming stream or we backed them up earlier */
608 /* first packet in order: vorbis/speex/theora info */
609 if( !p_stream->i_sout_headers )
611 p_og = sout_FifoGet( p_mux->pp_inputs[i]->p_fifo );
612 op.packet = p_og->p_buffer;
613 op.bytes = p_og->i_size;
617 op.packetno = p_stream->i_packet_no++;
618 ogg_stream_packetin( &p_stream->os, &op );
619 p_stream->pp_sout_headers[0] =
620 OggStreamFlush( p_mux, &p_stream->os, 0 );
621 p_stream->i_sout_headers++;
623 p_og = sout_BufferDuplicate( p_mux->p_sout,
624 p_stream->pp_sout_headers[0] );
626 /* Get keyframe_granule_shift for theora granulepos calculation */
627 if( p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
629 int i_keyframe_frequency_force = 1 << (op.packet[36] >> 3);
631 /* granule_shift = i_log( frequency_force -1 ) */
632 p_stream->i_keyframe_granule_shift = 0;
633 i_keyframe_frequency_force--;
634 while( i_keyframe_frequency_force )
636 p_stream->i_keyframe_granule_shift++;
637 i_keyframe_frequency_force >>= 1;
644 op.packet = (uint8_t*)&p_stream->oggds_header;
645 op.bytes = sizeof( oggds_header_t );
649 op.packetno = p_stream->i_packet_no++;
650 ogg_stream_packetin( &p_stream->os, &op );
651 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
654 sout_BufferChain( &p_hdr, p_og );
657 /* Take care of the non b_o_s headers */
658 for( i = 0; i < p_mux->i_nb_inputs; i++ )
660 ogg_stream_t *p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
662 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
663 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) ||
664 p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
666 /* Special case, headers are already there in the incoming stream.
667 * We need to gather them an mark them as headers. */
669 for( j = 0; j < 2; j++ )
671 if( p_stream->i_sout_headers < j + 2 )
673 /* next packets in order: comments and codebooks */
674 p_og = sout_FifoGet( p_mux->pp_inputs[i]->p_fifo );
675 op.packet = p_og->p_buffer;
676 op.bytes = p_og->i_size;
680 op.packetno = p_stream->i_packet_no++;
681 ogg_stream_packetin( &p_stream->os, &op );
682 p_stream->pp_sout_headers[j+1] =
683 OggStreamFlush( p_mux, &p_stream->os, 0 );
684 p_stream->i_sout_headers++;
687 p_og = sout_BufferDuplicate( p_mux->p_sout,
688 p_stream->pp_sout_headers[j+1] );
689 sout_BufferChain( &p_hdr, p_og );
698 com[0] = PACKET_TYPE_COMMENT;
699 i_com = snprintf( &com[1], 128, VERSION" stream output" ) + 1;
705 op.packetno = p_stream->i_packet_no++;
706 ogg_stream_packetin( &p_stream->os, &op );
707 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
708 sout_BufferChain( &p_hdr, p_og );
711 /* Special case for mp4v */
712 if( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p', '4', 'v' ) &&
713 p_mux->pp_inputs[i]->p_fmt->i_extra )
715 /* Send a packet with the VOL data */
716 op.bytes = p_mux->pp_inputs[i]->p_fmt->i_extra;
717 op.packet = p_mux->pp_inputs[i]->p_fmt->p_extra;
721 op.packetno = p_stream->i_packet_no++;
722 ogg_stream_packetin( &p_stream->os, &op );
723 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
724 sout_BufferChain( &p_hdr, p_og );
728 /* set HEADER flag */
729 for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
731 p_og->i_flags |= SOUT_BUFFER_FLAGS_HEADER;
736 static sout_buffer_t *OggCreateFooter( sout_mux_t *p_mux, mtime_t i_dts )
738 sout_mux_sys_t *p_sys = p_mux->p_sys;
739 sout_buffer_t *p_hdr = NULL;
744 /* flush all remaining data */
745 for( i = 0; i < p_mux->i_nb_inputs; i++ )
747 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
749 /* skip newly added streams */
750 if( p_stream->b_new ) continue;
752 if( ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
754 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
755 sout_AccessOutWrite( p_mux->p_access, p_og );
759 /* Write eos packets for each stream. */
760 for( i = 0; i < p_mux->i_nb_inputs; i++ )
762 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
764 /* skip newly added streams */
765 if( p_stream->b_new ) continue;
772 op.packetno = p_stream->i_packet_no++;
773 ogg_stream_packetin( &p_stream->os, &op );
775 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
776 sout_BufferChain( &p_hdr, p_og );
777 ogg_stream_clear( &p_stream->os );
780 for( i = 0; i < p_sys->i_del_streams; i++ )
787 op.packetno = p_sys->pp_del_streams[i]->i_packet_no++;
788 ogg_stream_packetin( &p_sys->pp_del_streams[i]->os, &op );
790 p_og = OggStreamFlush( p_mux, &p_sys->pp_del_streams[i]->os, 0 );
791 sout_BufferChain( &p_hdr, p_og );
792 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
798 static void OggSetDate( sout_buffer_t *p_og, mtime_t i_dts, mtime_t i_length )
801 sout_buffer_t *p_tmp;
804 for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
808 i_delta = i_length / i_count;
810 for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
812 p_tmp->i_dts = i_dts;
813 p_tmp->i_length = i_delta;
819 /*****************************************************************************
820 * Mux: multiplex available data in input fifos into the Ogg bitstream
821 *****************************************************************************/
822 static int Mux( sout_mux_t *p_mux )
824 sout_mux_sys_t *p_sys = p_mux->p_sys;
825 sout_buffer_t *p_og = NULL;
829 if( p_sys->i_add_streams || p_sys->i_del_streams )
831 /* Open new ogg stream */
832 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
834 msg_Dbg( p_mux, "waiting for data..." );
835 return( VLC_SUCCESS );
838 if( p_sys->i_streams )
840 /* Close current ogg stream */
843 msg_Dbg( p_mux, "writing footer" );
844 sout_BufferChain( &p_og, OggCreateFooter( p_mux, 0 ) );
846 /* Remove deleted logical streams */
847 for( i = 0; i < p_sys->i_del_streams; i++ )
849 FREE( p_sys->pp_del_streams[i] );
851 FREE( p_sys->pp_del_streams );
852 p_sys->i_streams = 0;
855 msg_Dbg( p_mux, "writing header" );
856 p_sys->i_start_dts = i_dts;
857 p_sys->i_streams = p_mux->i_nb_inputs;
858 p_sys->i_del_streams = 0;
859 p_sys->i_add_streams = 0;
860 sout_BufferChain( &p_og, OggCreateHeader( p_mux, i_dts ) );
862 /* Write header and/or footer */
863 OggSetDate( p_og, i_dts, 0 );
864 sout_AccessOutWrite( p_mux->p_access, p_og );
870 sout_input_t *p_input;
871 ogg_stream_t *p_stream;
872 sout_buffer_t *p_data;
875 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
877 return( VLC_SUCCESS );
880 p_input = p_mux->pp_inputs[i_stream];
881 p_stream = (ogg_stream_t*)p_input->p_sys;
882 p_data = sout_FifoGet( p_input->p_fifo );
884 if( p_stream->i_fourcc != VLC_FOURCC( 'v', 'o', 'r', 'b' ) &&
885 p_stream->i_fourcc != VLC_FOURCC( 's', 'p', 'x', ' ' ) &&
886 p_stream->i_fourcc != VLC_FOURCC( 't', 'h', 'e', 'o' ) )
888 sout_BufferReallocFromPreHeader( p_mux->p_sout, p_data, 1 );
889 p_data->p_buffer[0] = PACKET_IS_SYNCPOINT; // FIXME
892 op.packet = p_data->p_buffer;
893 op.bytes = p_data->i_size;
896 op.packetno = p_stream->i_packet_no++;
898 if( p_stream->i_cat == AUDIO_ES )
900 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
901 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) )
903 /* number of sample from begining + current packet */
905 ( i_dts + p_data->i_length - p_sys->i_start_dts ) *
906 p_input->p_fmt->audio.i_rate / I64C(1000000);
910 /* number of sample from begining */
911 op.granulepos = ( i_dts - p_sys->i_start_dts ) *
912 p_stream->oggds_header.i_samples_per_unit / I64C(1000000);
915 else if( p_stream->i_cat == VIDEO_ES )
917 if( p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
919 /* FIXME, we assume only keyframes and 25fps */
920 op.granulepos = ( ( i_dts - p_sys->i_start_dts ) * I64C(25)
921 / I64C(1000000) ) << p_stream->i_keyframe_granule_shift;
924 op.granulepos = ( i_dts - p_sys->i_start_dts ) * I64C(10) /
925 p_stream->oggds_header.i_time_unit;
927 else if( p_stream->i_cat == SPU_ES )
929 /* granulepos is in milisec */
930 op.granulepos = ( i_dts - p_sys->i_start_dts ) / 1000;
933 ogg_stream_packetin( &p_stream->os, &op );
935 if( p_stream->i_cat == SPU_ES ||
936 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) )
938 /* Subtitles or Speex packets are quite small so they
939 * need to be flushed to be sent on time */
940 sout_BufferChain( &p_og, OggStreamFlush( p_mux, &p_stream->os,
945 sout_BufferChain( &p_og, OggStreamPageOut( p_mux, &p_stream->os,
951 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
952 p_stream->i_dts = -1;
953 p_stream->i_length = 0;
955 sout_AccessOutWrite( p_mux->p_access, p_og );
961 if( p_stream->i_dts < 0 )
963 p_stream->i_dts = p_data->i_dts;
965 p_stream->i_length += p_data->i_length;
968 sout_BufferDelete( p_mux->p_sout, p_data );
971 return( VLC_SUCCESS );