1 /*****************************************************************************
2 * ogg.c: ogg muxer module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: ogg.c,v 1.16 2003/10/09 18:53:01 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 *****************************************************************************/
30 #include <sys/types.h>
33 #include <vlc/input.h>
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Open ( vlc_object_t * );
44 static void Close ( vlc_object_t * );
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 * );
51 static sout_buffer_t *OggCreateHeader( sout_mux_t *, mtime_t );
52 static sout_buffer_t *OggCreateFooter( sout_mux_t *, mtime_t );
54 /*****************************************************************************
56 *****************************************************************************/
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 );
65 /*****************************************************************************
67 *****************************************************************************/
68 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
70 /* Structures used for OggDS headers used in ogm files */
72 #define PACKET_TYPE_HEADER 0x01
73 #define PACKET_TYPE_COMMENT 0x03
74 #define PACKET_IS_SYNCPOINT 0x08
77 #ifdef HAVE_ATTRIBUTE_PACKED
78 __attribute__((__packed__))
83 } oggds_header_video_t;
86 #ifdef HAVE_ATTRIBUTE_PACKED
87 __attribute__((__packed__))
91 int16_t i_block_align;
92 int32_t i_avgbytespersec;
93 } oggds_header_audio_t;
96 #ifdef HAVE_ATTRIBUTE_PACKED
97 __attribute__((__packed__))
100 uint8_t i_packet_type;
108 int64_t i_samples_per_unit;
109 int32_t i_default_len;
111 int32_t i_buffer_size;
112 int16_t i_bits_per_sample;
114 int16_t i_padding_0; /* Because the original is using MSVC packing style */
118 oggds_header_video_t video;
119 oggds_header_audio_t audio;
122 int32_t i_padding_1; /* Because the original is using MSVC packing style */
126 /* Helper writer functions */
128 #define SetWLE( p, v ) _SetWLE( (uint8_t*)p, v)
129 static void _SetWLE( uint8_t *p, uint16_t i_dw )
131 p[1] = ( i_dw >> 8 )&0xff;
132 p[0] = ( i_dw )&0xff;
135 #define SetDWLE( p, v ) _SetDWLE( (uint8_t*)p, v)
136 static void _SetDWLE( uint8_t *p, uint32_t i_dw )
138 p[3] = ( i_dw >> 24 )&0xff;
139 p[2] = ( i_dw >> 16 )&0xff;
140 p[1] = ( i_dw >> 8 )&0xff;
141 p[0] = ( i_dw )&0xff;
143 #define SetQWLE( p, v ) _SetQWLE( (uint8_t*)p, v)
144 static void _SetQWLE( uint8_t *p, uint64_t i_qw )
146 SetDWLE( p, i_qw&0xffffffff );
147 SetDWLE( p+4, ( i_qw >> 32)&0xffffffff );
151 * TODO move this function to src/stream_output.c (used by nearly all muxers)
153 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
159 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
163 p_fifo = p_mux->pp_inputs[i]->p_fifo;
165 /* We don't really need to have anything in the SPU fifo */
166 if( p_mux->pp_inputs[i]->p_fmt->i_cat == SPU_ES &&
167 p_fifo->i_depth == 0 ) continue;
169 if( p_fifo->i_depth > 2 ||
170 /* Special case for SPUs */
171 ( p_mux->pp_inputs[i]->p_fmt->i_cat == SPU_ES &&
172 p_fifo->i_depth > 0 ) )
174 sout_buffer_t *p_buf;
176 p_buf = sout_FifoShow( p_fifo );
177 if( p_buf->i_dts ) // To ignore vorbis and theora header packets
178 if( i_stream < 0 || p_buf->i_dts < i_dts )
180 i_dts = p_buf->i_dts;
186 // wait that all fifo have at least 3 packets (3 vorbis headers)
192 *pi_stream = i_stream;
201 /*****************************************************************************
202 * Definitions of structures and functions used by this plugins
203 *****************************************************************************/
215 int i_keyframe_granule_shift; /* Theora only */
218 oggds_header_t oggds_header;
220 sout_buffer_t *pp_sout_headers[3];
225 struct sout_mux_sys_t
231 /* number of logical streams pending to be added */
234 /* logical streams pending to be deleted */
236 ogg_stream_t **pp_del_streams;
239 static void OggSetDate( sout_buffer_t *, mtime_t , mtime_t );
240 static sout_buffer_t *OggStreamFlush( sout_mux_t *, ogg_stream_state *,
243 /*****************************************************************************
245 *****************************************************************************/
246 static int Open( vlc_object_t *p_this )
248 sout_mux_t *p_mux = (sout_mux_t*)p_this;
249 sout_mux_sys_t *p_sys;
251 msg_Info( p_mux, "Open" );
253 p_sys = malloc( sizeof( sout_mux_sys_t ) );
254 p_sys->i_streams = 0;
255 p_sys->i_add_streams = 0;
256 p_sys->i_del_streams = 0;
257 p_sys->pp_del_streams = 0;
259 p_mux->p_sys = p_sys;
260 p_mux->pf_capacity = Capability;
261 p_mux->pf_addstream = AddStream;
262 p_mux->pf_delstream = DelStream;
264 p_mux->i_preheader = 1;
269 /*****************************************************************************
270 * Close: Finalize ogg bitstream and close muxer
271 *****************************************************************************/
272 static void Close( vlc_object_t * p_this )
274 sout_mux_t *p_mux = (sout_mux_t*)p_this;
275 sout_mux_sys_t *p_sys = p_mux->p_sys;
277 msg_Info( p_mux, "Close" );
279 if( p_sys->i_del_streams )
281 sout_buffer_t *p_og = NULL;
285 /* Close the current ogg stream */
286 msg_Dbg( p_mux, "writing footer" );
287 sout_BufferChain( &p_og, OggCreateFooter( p_mux, 0 ) );
289 /* Remove deleted logical streams */
290 for( i = 0; i < p_sys->i_del_streams; i++ )
292 i_dts = p_sys->pp_del_streams[i]->i_dts;
293 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
294 FREE( p_sys->pp_del_streams[i] );
296 FREE( p_sys->pp_del_streams );
297 p_sys->i_streams -= p_sys->i_del_streams;
300 OggSetDate( p_og, i_dts, 0 );
301 sout_AccessOutWrite( p_mux->p_access, p_og );
307 static int Capability( sout_mux_t *p_mux, int i_query, void *p_args,
312 case SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME:
313 *(vlc_bool_t*)p_answer = VLC_TRUE;
314 return( SOUT_MUX_CAP_ERR_OK );
316 return( SOUT_MUX_CAP_ERR_UNIMPLEMENTED );
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 = (void *)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_fourcc;
334 p_stream->i_serial_no = rand();
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 switch( p_stream->i_fourcc )
346 case VLC_FOURCC( 'm', 'p','4', 'v' ):
347 case VLC_FOURCC( 'D', 'I','V', '3' ):
348 memcpy( p_stream->oggds_header.stream_type, "video", 5 );
349 if( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p','4', 'v' ) )
351 memcpy( p_stream->oggds_header.sub_type, "XVID", 4 );
353 else if( p_stream->i_fourcc == VLC_FOURCC( 'D', 'I','V', '3' ) )
355 memcpy( p_stream->oggds_header.sub_type, "DIV3", 4 );
357 SetDWLE( &p_stream->oggds_header.i_size,
358 sizeof( oggds_header_t ) - 1);
359 SetQWLE( &p_stream->oggds_header.i_time_unit,
360 I64C(10000000)/(int64_t)25 ); // FIXME (25fps)
361 SetQWLE( &p_stream->oggds_header.i_samples_per_unit, 1 );
362 SetDWLE( &p_stream->oggds_header.i_default_len, 1 ); /* ??? */
363 SetDWLE( &p_stream->oggds_header.i_buffer_size, 1024*1024 );
364 SetWLE( &p_stream->oggds_header.i_bits_per_sample, 0 );
365 SetDWLE( &p_stream->oggds_header.header.video.i_width,
366 p_input->p_fmt->i_width );
367 SetDWLE( &p_stream->oggds_header.header.video.i_height,
368 p_input->p_fmt->i_height );
369 msg_Dbg( p_mux, "mp4v/div3 stream" );
372 case VLC_FOURCC( 't', 'h', 'e', 'o' ):
373 msg_Dbg( p_mux, "theora stream" );
377 FREE( p_input->p_sys );
378 return( VLC_EGENERIC );
383 switch( p_stream->i_fourcc )
385 case VLC_FOURCC( 'm', 'p','g', 'a' ):
386 case VLC_FOURCC( 'a', '5','2', ' ' ):
387 memcpy( p_stream->oggds_header.stream_type, "audio", 5 );
388 if( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p','g', 'a' ) )
390 memcpy( p_stream->oggds_header.sub_type, "55 ", 4 );
392 else if( p_stream->i_fourcc == VLC_FOURCC( 'a', '5','2', ' ' ) )
394 memcpy( p_stream->oggds_header.sub_type, "2000", 4 );
396 SetDWLE( &p_stream->oggds_header.i_size,
397 sizeof( oggds_header_t ) - 1);
398 SetQWLE( &p_stream->oggds_header.i_time_unit, 0 /* not used */ );
399 SetDWLE( &p_stream->oggds_header.i_default_len, 1 );
400 SetDWLE( &p_stream->oggds_header.i_buffer_size, 30*1024 );
401 SetQWLE( &p_stream->oggds_header.i_samples_per_unit,
402 p_input->p_fmt->i_sample_rate );
403 SetWLE( &p_stream->oggds_header.i_bits_per_sample, 0 );
404 SetDWLE( &p_stream->oggds_header.header.audio.i_channels,
405 p_input->p_fmt->i_channels );
406 SetDWLE( &p_stream->oggds_header.header.audio.i_block_align,
407 p_input->p_fmt->i_block_align );
408 SetDWLE( &p_stream->oggds_header.header.audio.i_avgbytespersec, 0);
409 msg_Dbg( p_mux, "mpga/a52 stream" );
412 case VLC_FOURCC( 'v', 'o', 'r', 'b' ):
413 msg_Dbg( p_mux, "vorbis stream" );
417 FREE( p_input->p_sys );
418 return( VLC_EGENERIC );
423 switch( p_stream->i_fourcc )
425 case VLC_FOURCC( 's', 'u','b', 't' ):
426 memcpy( p_stream->oggds_header.stream_type, "text", 4 );
427 msg_Dbg( p_mux, "subtitles stream" );
431 FREE( p_input->p_sys );
432 return( VLC_EGENERIC );
436 FREE( p_input->p_sys );
437 return( VLC_EGENERIC );
440 p_stream->b_new = VLC_TRUE;
442 p_sys->i_add_streams++;
444 return( VLC_SUCCESS );
447 /*****************************************************************************
448 * DelStream: Delete an elementary stream from the muxed stream
449 *****************************************************************************/
450 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
452 sout_mux_sys_t *p_sys = p_mux->p_sys;
453 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
456 msg_Dbg( p_mux, "removing input" );
458 /* flush all remaining data */
463 if( ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
465 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
466 sout_AccessOutWrite( p_mux->p_access, p_og );
469 for( i = 0; i < p_stream->i_sout_headers; i++ )
471 sout_BufferDelete( p_mux->p_sout, p_stream->pp_sout_headers[i] );
472 p_stream->i_sout_headers = 0;
475 /* move input in delete queue */
476 if( !p_stream->b_new )
478 p_sys->pp_del_streams = realloc( p_sys->pp_del_streams,
479 (p_sys->i_del_streams + 1) *
480 sizeof(ogg_stream_t *) );
481 p_sys->pp_del_streams[p_sys->i_del_streams++] = p_stream;
485 /* Wasn't already added so get rid of it */
486 ogg_stream_clear( &p_stream->os );
488 p_sys->i_add_streams--;
492 p_input->p_sys = NULL;
497 /*****************************************************************************
498 * Ogg bitstream manipulation routines
499 *****************************************************************************/
500 static sout_buffer_t *OggStreamFlush( sout_mux_t *p_mux,
501 ogg_stream_state *p_os, mtime_t i_pts )
503 sout_buffer_t *p_og, *p_og_first = NULL;
511 if( ( i_result = ogg_stream_flush( p_os, &og ) ) == 0 )
516 i_size = og.header_len + og.body_len;
517 p_og = sout_BufferNew( p_mux->p_sout, i_size);
519 memcpy( p_og->p_buffer, og.header, og.header_len );
520 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
521 p_og->i_size = i_size;
526 i_pts = 0; // write it only once
528 sout_BufferChain( &p_og_first, p_og );
531 return( p_og_first );
534 static sout_buffer_t *OggStreamPageOut( sout_mux_t *p_mux,
535 ogg_stream_state *p_os, mtime_t i_pts )
537 sout_buffer_t *p_og, *p_og_first = NULL;
545 if( ( i_result = ogg_stream_pageout( p_os, &og ) ) == 0 )
550 i_size = og.header_len + og.body_len;
551 p_og = sout_BufferNew( p_mux->p_sout, i_size);
553 memcpy( p_og->p_buffer, og.header, og.header_len );
554 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
555 p_og->i_size = i_size;
560 i_pts = 0; // write them only once
562 sout_BufferChain( &p_og_first, p_og );
565 return( p_og_first );
568 static sout_buffer_t *OggCreateHeader( sout_mux_t *p_mux, mtime_t i_dts )
570 sout_buffer_t *p_hdr = NULL;
575 /* Write header for each stream. All b_o_s (beginning of stream) packets
576 * must appear first in the ogg stream so we take care of them first. */
577 for( i = 0; i < p_mux->i_nb_inputs; i++ )
579 ogg_stream_t *p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
580 p_stream->b_new = VLC_FALSE;
582 msg_Dbg( p_mux, "creating header for %4.4s",
583 (char *)&p_stream->i_fourcc );
585 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
586 p_stream->i_packet_no = 0;
588 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
589 p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
591 /* Special case, headers are already there in the
592 * incoming stream or we backed them up earlier */
594 /* first packet in order: vorbis/theora info */
595 if( !p_stream->i_sout_headers )
597 p_og = sout_FifoGet( p_mux->pp_inputs[i]->p_fifo );
598 op.packet = p_og->p_buffer;
599 op.bytes = p_og->i_size;
603 op.packetno = p_stream->i_packet_no++;
604 ogg_stream_packetin( &p_stream->os, &op );
605 p_stream->pp_sout_headers[0] =
606 OggStreamFlush( p_mux, &p_stream->os, 0 );
607 p_stream->i_sout_headers++;
609 p_og = sout_BufferDuplicate( p_mux->p_sout,
610 p_stream->pp_sout_headers[0] );
612 /* Get keyframe_granule_shift for theora granulepos calculation */
613 if( p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
615 int i_keyframe_frequency_force = 1 << (op.packet[36] >> 3);
617 /* granule_shift = i_log( frequency_force -1 ) */
618 p_stream->i_keyframe_granule_shift = 0;
619 i_keyframe_frequency_force--;
620 while( i_keyframe_frequency_force )
622 p_stream->i_keyframe_granule_shift++;
623 i_keyframe_frequency_force >>= 1;
630 op.packet = (uint8_t*)&p_stream->oggds_header;
631 op.bytes = sizeof( oggds_header_t );
635 op.packetno = p_stream->i_packet_no++;
636 ogg_stream_packetin( &p_stream->os, &op );
637 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
640 sout_BufferChain( &p_hdr, p_og );
643 /* Take care of the non b_o_s headers */
644 for( i = 0; i < p_mux->i_nb_inputs; i++ )
646 ogg_stream_t *p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
648 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
649 p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
651 /* Special case, headers are already there in the incoming stream.
652 * We need to gather them an mark them as headers. */
654 for( j = 0; j < 2; j++ )
656 if( p_stream->i_sout_headers < j + 2 )
658 /* next packets in order: comments and codebooks */
659 p_og = sout_FifoGet( p_mux->pp_inputs[i]->p_fifo );
660 op.packet = p_og->p_buffer;
661 op.bytes = p_og->i_size;
665 op.packetno = p_stream->i_packet_no++;
666 ogg_stream_packetin( &p_stream->os, &op );
667 p_stream->pp_sout_headers[j+1] =
668 OggStreamFlush( p_mux, &p_stream->os, 0 );
669 p_stream->i_sout_headers++;
672 p_og = sout_BufferDuplicate( p_mux->p_sout,
673 p_stream->pp_sout_headers[j+1] );
674 sout_BufferChain( &p_hdr, p_og );
683 com[0] = PACKET_TYPE_COMMENT;
684 i_com = snprintf( &com[1], 128, VERSION" stream output" ) + 1;
690 op.packetno = p_stream->i_packet_no++;
691 ogg_stream_packetin( &p_stream->os, &op );
692 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
693 sout_BufferChain( &p_hdr, p_og );
697 /* set HEADER flag */
698 for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
700 p_og->i_flags |= SOUT_BUFFER_FLAGS_HEADER;
705 static sout_buffer_t *OggCreateFooter( sout_mux_t *p_mux, mtime_t i_dts )
707 sout_mux_sys_t *p_sys = p_mux->p_sys;
708 sout_buffer_t *p_hdr = NULL;
713 /* flush all remaining data */
714 for( i = 0; i < p_mux->i_nb_inputs; i++ )
716 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
718 /* skip newly added streams */
719 if( p_stream->b_new ) continue;
721 if( ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
723 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
724 sout_AccessOutWrite( p_mux->p_access, p_og );
728 /* Write eos packets for each stream. */
729 for( i = 0; i < p_mux->i_nb_inputs; i++ )
731 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
733 /* skip newly added streams */
734 if( p_stream->b_new ) continue;
741 op.packetno = p_stream->i_packet_no++;
742 ogg_stream_packetin( &p_stream->os, &op );
744 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
745 sout_BufferChain( &p_hdr, p_og );
746 ogg_stream_clear( &p_stream->os );
749 for( i = 0; i < p_sys->i_del_streams; i++ )
756 op.packetno = p_sys->pp_del_streams[i]->i_packet_no++;
757 ogg_stream_packetin( &p_sys->pp_del_streams[i]->os, &op );
759 p_og = OggStreamFlush( p_mux, &p_sys->pp_del_streams[i]->os, 0 );
760 sout_BufferChain( &p_hdr, p_og );
761 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
767 static void OggSetDate( sout_buffer_t *p_og, mtime_t i_dts, mtime_t i_length )
770 sout_buffer_t *p_tmp;
773 for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
777 i_delta = i_length / i_count;
779 for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
781 p_tmp->i_dts = i_dts;
782 p_tmp->i_length = i_delta;
788 /*****************************************************************************
789 * Mux: multiplex available data in input fifos into the Ogg bitstream
790 *****************************************************************************/
791 static int Mux( sout_mux_t *p_mux )
793 sout_mux_sys_t *p_sys = p_mux->p_sys;
794 sout_buffer_t *p_og = NULL;
798 if( p_sys->i_add_streams || p_sys->i_del_streams )
800 /* Open new ogg stream */
801 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
803 msg_Dbg( p_mux, "waiting for data..." );
804 return( VLC_SUCCESS );
807 if( p_sys->i_streams )
809 /* Close current ogg stream */
812 msg_Dbg( p_mux, "writing footer" );
813 sout_BufferChain( &p_og, OggCreateFooter( p_mux, 0 ) );
815 /* Remove deleted logical streams */
816 for( i = 0; i < p_sys->i_del_streams; i++ )
818 FREE( p_sys->pp_del_streams[i] );
820 FREE( p_sys->pp_del_streams );
821 p_sys->i_streams = 0;
824 msg_Dbg( p_mux, "writing header" );
825 p_sys->i_start_dts = i_dts;
826 p_sys->i_streams = p_mux->i_nb_inputs;
827 p_sys->i_del_streams = 0;
828 p_sys->i_add_streams = 0;
829 sout_BufferChain( &p_og, OggCreateHeader( p_mux, i_dts ) );
831 /* Write header and/or footer */
832 OggSetDate( p_og, i_dts, 0 );
833 sout_AccessOutWrite( p_mux->p_access, p_og );
839 sout_input_t *p_input;
840 ogg_stream_t *p_stream;
841 sout_buffer_t *p_data;
844 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
846 return( VLC_SUCCESS );
849 p_input = p_mux->pp_inputs[i_stream];
850 p_stream = (ogg_stream_t*)p_input->p_sys;
851 p_data = sout_FifoGet( p_input->p_fifo );
853 if( p_stream->i_fourcc != VLC_FOURCC( 'v', 'o', 'r', 'b' ) &&
854 p_stream->i_fourcc != VLC_FOURCC( 't', 'h', 'e', 'o' ) )
856 sout_BufferReallocFromPreHeader( p_mux->p_sout, p_data, 1 );
857 p_data->p_buffer[0] = PACKET_IS_SYNCPOINT; // FIXME
860 op.packet = p_data->p_buffer;
861 op.bytes = p_data->i_size;
864 op.packetno = p_stream->i_packet_no++;
866 if( p_stream->i_cat == AUDIO_ES )
868 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
870 /* number of sample from begining + current packet */
872 ( i_dts + p_data->i_length - p_sys->i_start_dts ) *
873 p_input->p_fmt->i_sample_rate / I64C(1000000);
877 /* number of sample from begining */
878 op.granulepos = ( i_dts - p_sys->i_start_dts ) *
879 p_stream->oggds_header.i_samples_per_unit / I64C(1000000);
882 else if( p_stream->i_cat == VIDEO_ES )
884 if( p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
886 /* FIXME, we assume only keyframes and 25fps */
887 op.granulepos = ( ( i_dts - p_sys->i_start_dts ) * I64C(25)
888 / I64C(1000000) ) << p_stream->i_keyframe_granule_shift;
891 op.granulepos = ( i_dts - p_sys->i_start_dts ) * I64C(10) /
892 p_stream->oggds_header.i_time_unit;
894 else if( p_stream->i_cat == SPU_ES )
896 /* granulepos is in milisec */
897 op.granulepos = ( i_dts - p_sys->i_start_dts ) / 1000;
900 ogg_stream_packetin( &p_stream->os, &op );
902 if( p_stream->i_cat == SPU_ES )
904 /* Subtitles need to be flushed to be sent on time */
905 sout_BufferChain( &p_og, OggStreamFlush( p_mux, &p_stream->os,
910 sout_BufferChain( &p_og, OggStreamPageOut( p_mux, &p_stream->os,
916 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
917 p_stream->i_dts = -1;
918 p_stream->i_length = 0;
920 sout_AccessOutWrite( p_mux->p_access, p_og );
926 if( p_stream->i_dts < 0 )
928 p_stream->i_dts = p_data->i_dts;
930 p_stream->i_length += p_data->i_length;
933 sout_BufferDelete( p_mux->p_sout, p_data );
936 return( VLC_SUCCESS );