1 /*****************************************************************************
2 * ogg.c: ogg muxer module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
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 * );
50 set_description( _("Ogg/ogm muxer") );
51 set_capability( "sout mux", 10 );
52 add_shortcut( "ogg" );
53 add_shortcut( "ogm" );
54 set_callbacks( Open, Close );
58 /*****************************************************************************
60 *****************************************************************************/
61 static int Control ( sout_mux_t *, int, va_list );
62 static int AddStream( sout_mux_t *, sout_input_t * );
63 static int DelStream( sout_mux_t *, sout_input_t * );
64 static int Mux ( sout_mux_t * );
66 static block_t *OggCreateHeader( sout_mux_t *, mtime_t );
67 static block_t *OggCreateFooter( sout_mux_t *, mtime_t );
69 /*****************************************************************************
71 *****************************************************************************/
72 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
74 /* Structures used for OggDS headers used in ogm files */
76 #define PACKET_TYPE_HEADER 0x01
77 #define PACKET_TYPE_COMMENT 0x03
78 #define PACKET_IS_SYNCPOINT 0x08
81 #ifdef HAVE_ATTRIBUTE_PACKED
82 __attribute__((__packed__))
87 } oggds_header_video_t;
90 #ifdef HAVE_ATTRIBUTE_PACKED
91 __attribute__((__packed__))
95 int16_t i_block_align;
96 int32_t i_avgbytespersec;
97 } oggds_header_audio_t;
100 #ifdef HAVE_ATTRIBUTE_PACKED
101 __attribute__((__packed__))
104 uint8_t i_packet_type;
112 int64_t i_samples_per_unit;
113 int32_t i_default_len;
115 int32_t i_buffer_size;
116 int16_t i_bits_per_sample;
118 int16_t i_padding_0; /* Because the original is using MSVC packing style */
122 oggds_header_video_t video;
123 oggds_header_audio_t audio;
126 int32_t i_padding_1; /* Because the original is using MSVC packing style */
131 * TODO move this function to src/stream_output.c (used by nearly all muxers)
133 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
139 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
141 block_fifo_t *p_fifo;
143 p_fifo = p_mux->pp_inputs[i]->p_fifo;
145 /* We don't really need to have anything in the SPU fifo */
146 if( p_mux->pp_inputs[i]->p_fmt->i_cat == SPU_ES &&
147 p_fifo->i_depth == 0 ) continue;
149 if( p_fifo->i_depth > 2 ||
150 /* Special case for SPUs */
151 ( p_mux->pp_inputs[i]->p_fmt->i_cat == SPU_ES &&
152 p_fifo->i_depth > 0 ) )
156 p_buf = block_FifoShow( p_fifo );
157 if( i_stream < 0 || p_buf->i_dts < i_dts )
159 i_dts = p_buf->i_dts;
165 // wait that all fifo have at least 3 packets (3 vorbis headers)
171 *pi_stream = i_stream;
180 /*****************************************************************************
181 * Definitions of structures and functions used by this plugins
182 *****************************************************************************/
194 int i_keyframe_granule_shift; /* Theora only */
197 oggds_header_t oggds_header;
199 block_t *pp_sout_headers[3];
204 struct sout_mux_sys_t
209 int i_next_serial_no;
211 /* number of logical streams pending to be added */
214 /* logical streams pending to be deleted */
216 ogg_stream_t **pp_del_streams;
219 static void OggSetDate( block_t *, mtime_t , mtime_t );
220 static block_t *OggStreamFlush( sout_mux_t *, ogg_stream_state *, mtime_t );
222 /*****************************************************************************
224 *****************************************************************************/
225 static int Open( vlc_object_t *p_this )
227 sout_mux_t *p_mux = (sout_mux_t*)p_this;
228 sout_mux_sys_t *p_sys;
230 msg_Info( p_mux, "Open" );
232 p_sys = malloc( sizeof( sout_mux_sys_t ) );
233 p_sys->i_streams = 0;
234 p_sys->i_add_streams = 0;
235 p_sys->i_del_streams = 0;
236 p_sys->pp_del_streams = 0;
238 p_mux->p_sys = p_sys;
239 p_mux->pf_control = Control;
240 p_mux->pf_addstream = AddStream;
241 p_mux->pf_delstream = DelStream;
244 /* First serial number is random.
245 * (Done like this because on win32 you need to seed the random number
246 * generator once per thread). */
247 srand( (unsigned int)time( NULL ) );
248 p_sys->i_next_serial_no = rand();
253 /*****************************************************************************
254 * Close: Finalize ogg bitstream and close muxer
255 *****************************************************************************/
256 static void Close( vlc_object_t * p_this )
258 sout_mux_t *p_mux = (sout_mux_t*)p_this;
259 sout_mux_sys_t *p_sys = p_mux->p_sys;
261 msg_Info( p_mux, "Close" );
263 if( p_sys->i_del_streams )
265 block_t *p_og = NULL;
269 /* Close the current ogg stream */
270 msg_Dbg( p_mux, "writing footer" );
271 block_ChainAppend( &p_og, OggCreateFooter( p_mux, 0 ) );
273 /* Remove deleted logical streams */
274 for( i = 0; i < p_sys->i_del_streams; i++ )
276 i_dts = p_sys->pp_del_streams[i]->i_dts;
277 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
278 FREE( p_sys->pp_del_streams[i] );
280 FREE( p_sys->pp_del_streams );
281 p_sys->i_streams -= p_sys->i_del_streams;
284 OggSetDate( p_og, i_dts, 0 );
285 sout_AccessOutWrite( p_mux->p_access, p_og );
291 /*****************************************************************************
293 *****************************************************************************/
294 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
301 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
302 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
306 case MUX_GET_ADD_STREAM_WAIT:
307 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
312 ppsz = (char**)va_arg( args, char ** );
313 *ppsz = strdup( "application/ogg" );
320 /*****************************************************************************
321 * AddStream: Add an elementary stream to the muxed stream
322 *****************************************************************************/
323 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
325 sout_mux_sys_t *p_sys = p_mux->p_sys;
326 ogg_stream_t *p_stream;
328 msg_Dbg( p_mux, "adding input" );
330 p_input->p_sys = p_stream = malloc( sizeof( ogg_stream_t ) );
332 p_stream->i_cat = p_input->p_fmt->i_cat;
333 p_stream->i_fourcc = p_input->p_fmt->i_codec;
334 p_stream->i_serial_no = p_sys->i_next_serial_no++;
335 p_stream->i_packet_no = 0;
337 p_stream->i_sout_headers = 0;
339 memset( &p_stream->oggds_header, 0, sizeof(p_stream->oggds_header) );
340 p_stream->oggds_header.i_packet_type = PACKET_TYPE_HEADER;
341 switch( p_input->p_fmt->i_cat )
344 if( !p_input->p_fmt->video.i_frame_rate ||
345 !p_input->p_fmt->video.i_frame_rate_base )
347 msg_Warn( p_mux, "Missing frame rate, assuming 25fps" );
348 p_input->p_fmt->video.i_frame_rate = 25;
349 p_input->p_fmt->video.i_frame_rate_base = 1;
352 switch( p_stream->i_fourcc )
354 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
355 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
356 case VLC_FOURCC( 'D', 'I', 'V', '3' ):
357 case VLC_FOURCC( 'M', 'J', 'P', 'G' ):
358 case VLC_FOURCC( 'W', 'M', 'V', '1' ):
359 case VLC_FOURCC( 'W', 'M', 'V', '2' ):
360 memcpy( p_stream->oggds_header.stream_type, "video", 5 );
361 if( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p', '4', 'v' ) )
363 memcpy( p_stream->oggds_header.sub_type, "XVID", 4 );
365 else if( p_stream->i_fourcc == VLC_FOURCC( 'D', 'I', 'V', '3' ) )
367 memcpy( p_stream->oggds_header.sub_type, "DIV3", 4 );
371 memcpy(p_stream->oggds_header.sub_type,&p_stream->i_fourcc,4);
373 SetDWLE( &p_stream->oggds_header.i_size,
374 sizeof( oggds_header_t ) - 1);
375 SetQWLE( &p_stream->oggds_header.i_time_unit,
376 I64C(10000000) * p_input->p_fmt->video.i_frame_rate_base /
377 (int64_t)p_input->p_fmt->video.i_frame_rate );
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, "%4.4s stream", (char *)&p_stream->i_fourcc );
389 case VLC_FOURCC( 't', 'h', 'e', 'o' ):
390 msg_Dbg( p_mux, "theora stream" );
394 FREE( p_input->p_sys );
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 );
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 );
461 FREE( p_input->p_sys );
465 p_stream->b_new = VLC_TRUE;
467 p_sys->i_add_streams++;
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_stream->b_new &&
489 ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
491 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
492 sout_AccessOutWrite( p_mux->p_access, p_og );
495 for( i = 0; i < p_stream->i_sout_headers; i++ )
497 block_Release( p_stream->pp_sout_headers[i] );
498 p_stream->i_sout_headers = 0;
501 /* move input in delete queue */
502 if( !p_stream->b_new )
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;
511 /* Wasn't already added so get rid of it */
513 p_sys->i_add_streams--;
517 p_input->p_sys = NULL;
522 /*****************************************************************************
523 * Ogg bitstream manipulation routines
524 *****************************************************************************/
525 static block_t *OggStreamFlush( sout_mux_t *p_mux,
526 ogg_stream_state *p_os, mtime_t i_pts )
528 block_t *p_og, *p_og_first = NULL;
531 while( ogg_stream_flush( p_os, &og ) )
534 p_og = block_New( p_mux, og.header_len + og.body_len );
536 memcpy( p_og->p_buffer, og.header, og.header_len );
537 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
542 i_pts = 0; // write it only once
544 block_ChainAppend( &p_og_first, p_og );
550 static block_t *OggStreamPageOut( sout_mux_t *p_mux,
551 ogg_stream_state *p_os, mtime_t i_pts )
553 block_t *p_og, *p_og_first = NULL;
556 while( ogg_stream_pageout( p_os, &og ) )
559 p_og = block_New( p_mux, og.header_len + og.body_len );
561 memcpy( p_og->p_buffer, og.header, og.header_len );
562 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
567 i_pts = 0; // write them only once
569 block_ChainAppend( &p_og_first, p_og );
575 static block_t *OggCreateHeader( sout_mux_t *p_mux, mtime_t i_dts )
577 block_t *p_hdr = NULL;
582 /* Write header for each stream. All b_o_s (beginning of stream) packets
583 * must appear first in the ogg stream so we take care of them first. */
584 for( i = 0; i < p_mux->i_nb_inputs; i++ )
586 ogg_stream_t *p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
587 p_stream->b_new = VLC_FALSE;
589 msg_Dbg( p_mux, "creating header for %4.4s",
590 (char *)&p_stream->i_fourcc );
592 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
593 p_stream->i_packet_no = 0;
595 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
596 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) ||
597 p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
599 /* Special case, headers are already there in the
600 * incoming stream or we backed them up earlier */
602 /* first packet in order: vorbis/speex/theora info */
603 if( !p_stream->i_sout_headers )
605 p_og = block_FifoGet( p_mux->pp_inputs[i]->p_fifo );
606 op.packet = p_og->p_buffer;
607 op.bytes = p_og->i_buffer;
611 op.packetno = p_stream->i_packet_no++;
612 ogg_stream_packetin( &p_stream->os, &op );
613 p_stream->pp_sout_headers[0] =
614 OggStreamFlush( p_mux, &p_stream->os, 0 );
615 p_stream->i_sout_headers++;
617 p_og = block_Duplicate( p_stream->pp_sout_headers[0] );
619 /* Get keyframe_granule_shift for theora granulepos calculation */
620 if( p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
622 int i_keyframe_frequency_force =
623 1 << ((op.packet[40] << 6 >> 3) | (op.packet[41] >> 5));
625 /* granule_shift = i_log( frequency_force -1 ) */
626 p_stream->i_keyframe_granule_shift = 0;
627 i_keyframe_frequency_force--;
628 while( i_keyframe_frequency_force )
630 p_stream->i_keyframe_granule_shift++;
631 i_keyframe_frequency_force >>= 1;
635 else if( p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
637 /* flac stream marker (yeah, only that in the 1st packet) */
643 op.packetno = p_stream->i_packet_no++;
644 ogg_stream_packetin( &p_stream->os, &op );
645 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
650 op.packet = (uint8_t*)&p_stream->oggds_header;
651 op.bytes = sizeof( oggds_header_t );
655 op.packetno = p_stream->i_packet_no++;
656 ogg_stream_packetin( &p_stream->os, &op );
657 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
660 block_ChainAppend( &p_hdr, p_og );
663 /* Take care of the non b_o_s headers */
664 for( i = 0; i < p_mux->i_nb_inputs; i++ )
666 ogg_stream_t *p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
668 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
669 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) ||
670 p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
672 /* Special case, headers are already there in the incoming stream.
673 * We need to gather them an mark them as headers. */
675 for( j = 0; j < 2; j++ )
677 if( p_stream->i_sout_headers < j + 2 )
679 /* next packets in order: comments and codebooks */
680 p_og = block_FifoGet( p_mux->pp_inputs[i]->p_fifo );
681 op.packet = p_og->p_buffer;
682 op.bytes = p_og->i_buffer;
686 op.packetno = p_stream->i_packet_no++;
687 ogg_stream_packetin( &p_stream->os, &op );
688 p_stream->pp_sout_headers[j+1] =
689 OggStreamFlush( p_mux, &p_stream->os, 0 );
690 p_stream->i_sout_headers++;
693 p_og = block_Duplicate( p_stream->pp_sout_headers[j+1] );
694 block_ChainAppend( &p_hdr, p_og );
697 else if( p_stream->i_fourcc != VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
703 com[0] = PACKET_TYPE_COMMENT;
704 i_com = snprintf( &com[1], 128, VERSION" stream output" ) + 1;
710 op.packetno = p_stream->i_packet_no++;
711 ogg_stream_packetin( &p_stream->os, &op );
712 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
713 block_ChainAppend( &p_hdr, p_og );
716 /* Special case for mp4v and flac */
717 if( ( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p', '4', 'v' ) ||
718 p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) ) &&
719 p_mux->pp_inputs[i]->p_fmt->i_extra )
721 /* Send a packet with the VOL data for mp4v
722 * or STREAMINFO for flac */
723 msg_Dbg( p_mux, "writing extra data" );
724 op.bytes = p_mux->pp_inputs[i]->p_fmt->i_extra;
725 op.packet = p_mux->pp_inputs[i]->p_fmt->p_extra;
726 if( p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
728 /* Skip the flac stream marker */
735 op.packetno = p_stream->i_packet_no++;
736 ogg_stream_packetin( &p_stream->os, &op );
737 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
738 block_ChainAppend( &p_hdr, p_og );
742 /* set HEADER flag */
743 for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
745 p_og->i_flags |= BLOCK_FLAG_HEADER;
750 static block_t *OggCreateFooter( sout_mux_t *p_mux, mtime_t i_dts )
752 sout_mux_sys_t *p_sys = p_mux->p_sys;
753 block_t *p_hdr = NULL;
758 /* flush all remaining data */
759 for( i = 0; i < p_mux->i_nb_inputs; i++ )
761 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
763 /* skip newly added streams */
764 if( p_stream->b_new ) continue;
766 if( ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
768 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
769 sout_AccessOutWrite( p_mux->p_access, p_og );
773 /* Write eos packets for each stream. */
774 for( i = 0; i < p_mux->i_nb_inputs; i++ )
776 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
778 /* skip newly added streams */
779 if( p_stream->b_new ) continue;
786 op.packetno = p_stream->i_packet_no++;
787 ogg_stream_packetin( &p_stream->os, &op );
789 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
790 block_ChainAppend( &p_hdr, p_og );
791 ogg_stream_clear( &p_stream->os );
794 for( i = 0; i < p_sys->i_del_streams; i++ )
801 op.packetno = p_sys->pp_del_streams[i]->i_packet_no++;
802 ogg_stream_packetin( &p_sys->pp_del_streams[i]->os, &op );
804 p_og = OggStreamFlush( p_mux, &p_sys->pp_del_streams[i]->os, 0 );
805 block_ChainAppend( &p_hdr, p_og );
806 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
812 static void OggSetDate( block_t *p_og, mtime_t i_dts, mtime_t i_length )
818 for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
822 i_delta = i_length / i_count;
824 for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
826 p_tmp->i_dts = i_dts;
827 p_tmp->i_length = i_delta;
833 /*****************************************************************************
834 * Mux: multiplex available data in input fifos into the Ogg bitstream
835 *****************************************************************************/
836 static int Mux( sout_mux_t *p_mux )
838 sout_mux_sys_t *p_sys = p_mux->p_sys;
839 block_t *p_og = NULL;
843 if( p_sys->i_add_streams || p_sys->i_del_streams )
845 /* Open new ogg stream */
846 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
848 msg_Dbg( p_mux, "waiting for data..." );
852 if( p_sys->i_streams )
854 /* Close current ogg stream */
857 msg_Dbg( p_mux, "writing footer" );
858 block_ChainAppend( &p_og, OggCreateFooter( p_mux, 0 ) );
860 /* Remove deleted logical streams */
861 for( i = 0; i < p_sys->i_del_streams; i++ )
863 FREE( p_sys->pp_del_streams[i] );
865 FREE( p_sys->pp_del_streams );
866 p_sys->i_streams = 0;
869 msg_Dbg( p_mux, "writing header" );
870 p_sys->i_start_dts = i_dts;
871 p_sys->i_streams = p_mux->i_nb_inputs;
872 p_sys->i_del_streams = 0;
873 p_sys->i_add_streams = 0;
874 block_ChainAppend( &p_og, OggCreateHeader( p_mux, i_dts ) );
876 /* Write header and/or footer */
877 OggSetDate( p_og, i_dts, 0 );
878 sout_AccessOutWrite( p_mux->p_access, p_og );
884 sout_input_t *p_input;
885 ogg_stream_t *p_stream;
889 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
894 p_input = p_mux->pp_inputs[i_stream];
895 p_stream = (ogg_stream_t*)p_input->p_sys;
896 p_data = block_FifoGet( p_input->p_fifo );
898 if( p_stream->i_fourcc != VLC_FOURCC( 'v', 'o', 'r', 'b' ) &&
899 p_stream->i_fourcc != VLC_FOURCC( 'f', 'l', 'a', 'c' ) &&
900 p_stream->i_fourcc != VLC_FOURCC( 's', 'p', 'x', ' ' ) &&
901 p_stream->i_fourcc != VLC_FOURCC( 't', 'h', 'e', 'o' ) )
903 p_data = block_Realloc( p_data, 1, p_data->i_buffer );
904 p_data->p_buffer[0] = PACKET_IS_SYNCPOINT; // FIXME
907 op.packet = p_data->p_buffer;
908 op.bytes = p_data->i_buffer;
911 op.packetno = p_stream->i_packet_no++;
913 if( p_stream->i_cat == AUDIO_ES )
915 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
916 p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) ||
917 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) )
919 /* number of sample from begining + current packet */
921 ( i_dts - p_sys->i_start_dts + p_data->i_length ) *
922 (mtime_t)p_input->p_fmt->audio.i_rate / I64C(1000000);
926 /* number of sample from begining */
927 op.granulepos = ( i_dts - p_sys->i_start_dts ) *
928 p_stream->oggds_header.i_samples_per_unit / I64C(1000000);
931 else if( p_stream->i_cat == VIDEO_ES )
933 if( p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
935 /* FIXME, we assume only keyframes */
936 op.granulepos = ( ( i_dts - p_sys->i_start_dts ) *
937 p_input->p_fmt->video.i_frame_rate /
938 p_input->p_fmt->video.i_frame_rate_base /
939 I64C(1000000) ) << p_stream->i_keyframe_granule_shift;
942 op.granulepos = ( i_dts - p_sys->i_start_dts ) * I64C(10) /
943 p_stream->oggds_header.i_time_unit;
945 else if( p_stream->i_cat == SPU_ES )
947 /* granulepos is in milisec */
948 op.granulepos = ( i_dts - p_sys->i_start_dts ) / 1000;
951 ogg_stream_packetin( &p_stream->os, &op );
953 if( p_stream->i_cat == SPU_ES ||
954 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) )
956 /* Subtitles or Speex packets are quite small so they
957 * need to be flushed to be sent on time */
958 p_og = OggStreamFlush( p_mux, &p_stream->os, p_data->i_dts );
962 p_og = OggStreamPageOut( p_mux, &p_stream->os, p_data->i_dts );
967 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
968 p_stream->i_dts = -1;
969 p_stream->i_length = 0;
971 sout_AccessOutWrite( p_mux->p_access, p_og );
975 if( p_stream->i_dts < 0 )
977 p_stream->i_dts = p_data->i_dts;
979 p_stream->i_length += p_data->i_length;
982 block_Release( p_data );