1 /*****************************************************************************
2 * ogg.c: ogg muxer module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: ogg.c,v 1.15 2003/10/09 11:48:41 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( i_stream < 0 || p_buf->i_dts < i_dts )
179 i_dts = p_buf->i_dts;
185 // wait that all fifo have at least 3 packets (3 vorbis headers)
191 *pi_stream = i_stream;
200 /*****************************************************************************
201 * Definitions of structures and functions used by this plugins
202 *****************************************************************************/
216 oggds_header_t oggds_header;
218 sout_buffer_t *pp_sout_headers[3];
223 struct sout_mux_sys_t
229 /* number of logical streams pending to be added */
232 /* logical streams pending to be deleted */
234 ogg_stream_t **pp_del_streams;
237 static void OggSetDate( sout_buffer_t *, mtime_t , mtime_t );
238 static sout_buffer_t *OggStreamFlush( sout_mux_t *, ogg_stream_state *,
241 /*****************************************************************************
243 *****************************************************************************/
244 static int Open( vlc_object_t *p_this )
246 sout_mux_t *p_mux = (sout_mux_t*)p_this;
247 sout_mux_sys_t *p_sys;
249 msg_Info( p_mux, "Open" );
251 p_sys = malloc( sizeof( sout_mux_sys_t ) );
252 p_sys->i_streams = 0;
253 p_sys->i_add_streams = 0;
254 p_sys->i_del_streams = 0;
255 p_sys->pp_del_streams = 0;
257 p_mux->p_sys = p_sys;
258 p_mux->pf_capacity = Capability;
259 p_mux->pf_addstream = AddStream;
260 p_mux->pf_delstream = DelStream;
262 p_mux->i_preheader = 1;
267 /*****************************************************************************
268 * Close: Finalize ogg bitstream and close muxer
269 *****************************************************************************/
270 static void Close( vlc_object_t * p_this )
272 sout_mux_t *p_mux = (sout_mux_t*)p_this;
273 sout_mux_sys_t *p_sys = p_mux->p_sys;
275 msg_Info( p_mux, "Close" );
277 if( p_sys->i_del_streams )
279 sout_buffer_t *p_og = NULL;
283 /* Close the current ogg stream */
284 msg_Dbg( p_mux, "writing footer" );
285 sout_BufferChain( &p_og, OggCreateFooter( p_mux, 0 ) );
287 /* Remove deleted logical streams */
288 for( i = 0; i < p_sys->i_del_streams; i++ )
290 i_dts = p_sys->pp_del_streams[i]->i_dts;
291 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
292 FREE( p_sys->pp_del_streams[i] );
294 FREE( p_sys->pp_del_streams );
295 p_sys->i_streams -= p_sys->i_del_streams;
298 OggSetDate( p_og, i_dts, 0 );
299 sout_AccessOutWrite( p_mux->p_access, p_og );
305 static int Capability( sout_mux_t *p_mux, int i_query, void *p_args,
310 case SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME:
311 *(vlc_bool_t*)p_answer = VLC_TRUE;
312 return( SOUT_MUX_CAP_ERR_OK );
314 return( SOUT_MUX_CAP_ERR_UNIMPLEMENTED );
318 /*****************************************************************************
319 * AddStream: Add an elementary stream to the muxed stream
320 *****************************************************************************/
321 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
323 sout_mux_sys_t *p_sys = p_mux->p_sys;
324 ogg_stream_t *p_stream;
326 msg_Dbg( p_mux, "adding input" );
328 p_input->p_sys = (void *)p_stream = malloc( sizeof( ogg_stream_t ) );
330 p_stream->i_cat = p_input->p_fmt->i_cat;
331 p_stream->i_fourcc = p_input->p_fmt->i_fourcc;
332 p_stream->i_serial_no = rand();
333 p_stream->i_packet_no = 0;
335 p_stream->i_sout_headers = 0;
337 memset( &p_stream->oggds_header, 0, sizeof(p_stream->oggds_header) );
338 p_stream->oggds_header.i_packet_type = PACKET_TYPE_HEADER;
339 switch( p_input->p_fmt->i_cat )
342 switch( p_stream->i_fourcc )
344 case VLC_FOURCC( 'm', 'p','4', 'v' ):
345 case VLC_FOURCC( 'D', 'I','V', '3' ):
346 memcpy( p_stream->oggds_header.stream_type, "video", 5 );
347 if( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p','4', 'v' ) )
349 memcpy( p_stream->oggds_header.sub_type, "XVID", 4 );
351 else if( p_stream->i_fourcc == VLC_FOURCC( 'D', 'I','V', '3' ) )
353 memcpy( p_stream->oggds_header.sub_type, "DIV3", 4 );
355 SetDWLE( &p_stream->oggds_header.i_size,
356 sizeof( oggds_header_t ) - 1);
357 SetQWLE( &p_stream->oggds_header.i_time_unit,
358 I64C(10000000)/(int64_t)25 ); // FIXME (25fps)
359 SetQWLE( &p_stream->oggds_header.i_samples_per_unit, 1 );
360 SetDWLE( &p_stream->oggds_header.i_default_len, 1 ); /* ??? */
361 SetDWLE( &p_stream->oggds_header.i_buffer_size, 1024*1024 );
362 SetWLE( &p_stream->oggds_header.i_bits_per_sample, 0 );
363 SetDWLE( &p_stream->oggds_header.header.video.i_width,
364 p_input->p_fmt->i_width );
365 SetDWLE( &p_stream->oggds_header.header.video.i_height,
366 p_input->p_fmt->i_height );
367 msg_Dbg( p_mux, "mp4v/div3 stream" );
370 case VLC_FOURCC( 't', 'h', 'e', 'o' ):
371 msg_Dbg( p_mux, "theora stream" );
375 FREE( p_input->p_sys );
376 return( VLC_EGENERIC );
381 switch( p_stream->i_fourcc )
383 case VLC_FOURCC( 'm', 'p','g', 'a' ):
384 case VLC_FOURCC( 'a', '5','2', ' ' ):
385 memcpy( p_stream->oggds_header.stream_type, "audio", 5 );
386 if( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p','g', 'a' ) )
388 memcpy( p_stream->oggds_header.sub_type, "55 ", 4 );
390 else if( p_stream->i_fourcc == VLC_FOURCC( 'a', '5','2', ' ' ) )
392 memcpy( p_stream->oggds_header.sub_type, "2000", 4 );
394 SetDWLE( &p_stream->oggds_header.i_size,
395 sizeof( oggds_header_t ) - 1);
396 SetQWLE( &p_stream->oggds_header.i_time_unit, 0 /* not used */ );
397 SetDWLE( &p_stream->oggds_header.i_default_len, 1 );
398 SetDWLE( &p_stream->oggds_header.i_buffer_size, 30*1024 );
399 SetQWLE( &p_stream->oggds_header.i_samples_per_unit,
400 p_input->p_fmt->i_sample_rate );
401 SetWLE( &p_stream->oggds_header.i_bits_per_sample, 0 );
402 SetDWLE( &p_stream->oggds_header.header.audio.i_channels,
403 p_input->p_fmt->i_channels );
404 SetDWLE( &p_stream->oggds_header.header.audio.i_block_align,
405 p_input->p_fmt->i_block_align );
406 SetDWLE( &p_stream->oggds_header.header.audio.i_avgbytespersec, 0);
407 msg_Dbg( p_mux, "mpga/a52 stream" );
410 case VLC_FOURCC( 'v', 'o', 'r', 'b' ):
411 msg_Dbg( p_mux, "vorbis stream" );
415 FREE( p_input->p_sys );
416 return( VLC_EGENERIC );
421 switch( p_stream->i_fourcc )
423 case VLC_FOURCC( 's', 'u','b', 't' ):
424 memcpy( p_stream->oggds_header.stream_type, "text", 4 );
425 msg_Dbg( p_mux, "subtitles stream" );
429 FREE( p_input->p_sys );
430 return( VLC_EGENERIC );
434 FREE( p_input->p_sys );
435 return( VLC_EGENERIC );
438 p_stream->b_new = VLC_TRUE;
440 p_sys->i_add_streams++;
442 return( VLC_SUCCESS );
445 /*****************************************************************************
446 * DelStream: Delete an elementary stream from the muxed stream
447 *****************************************************************************/
448 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
450 sout_mux_sys_t *p_sys = p_mux->p_sys;
451 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
454 msg_Dbg( p_mux, "removing input" );
456 /* flush all remaining data */
461 if( ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
463 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
464 sout_AccessOutWrite( p_mux->p_access, p_og );
467 for( i = 0; i < p_stream->i_sout_headers; i++ )
469 sout_BufferDelete( p_mux->p_sout, p_stream->pp_sout_headers[i] );
470 p_stream->i_sout_headers = 0;
473 /* move input in delete queue */
474 if( !p_stream->b_new )
476 p_sys->pp_del_streams = realloc( p_sys->pp_del_streams,
477 (p_sys->i_del_streams + 1) *
478 sizeof(ogg_stream_t *) );
479 p_sys->pp_del_streams[p_sys->i_del_streams++] = p_stream;
483 /* Wasn't already added so get rid of it */
484 ogg_stream_clear( &p_stream->os );
486 p_sys->i_add_streams--;
490 p_input->p_sys = NULL;
495 /*****************************************************************************
496 * Ogg bitstream manipulation routines
497 *****************************************************************************/
498 static sout_buffer_t *OggStreamFlush( sout_mux_t *p_mux,
499 ogg_stream_state *p_os, mtime_t i_pts )
501 sout_buffer_t *p_og, *p_og_first = NULL;
509 if( ( i_result = ogg_stream_flush( p_os, &og ) ) == 0 )
514 i_size = og.header_len + og.body_len;
515 p_og = sout_BufferNew( p_mux->p_sout, i_size);
517 memcpy( p_og->p_buffer, og.header, og.header_len );
518 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
519 p_og->i_size = i_size;
524 i_pts = 0; // write it only once
526 sout_BufferChain( &p_og_first, p_og );
529 return( p_og_first );
532 static sout_buffer_t *OggStreamPageOut( sout_mux_t *p_mux,
533 ogg_stream_state *p_os, mtime_t i_pts )
535 sout_buffer_t *p_og, *p_og_first = NULL;
543 if( ( i_result = ogg_stream_pageout( p_os, &og ) ) == 0 )
548 i_size = og.header_len + og.body_len;
549 p_og = sout_BufferNew( p_mux->p_sout, i_size);
551 memcpy( p_og->p_buffer, og.header, og.header_len );
552 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
553 p_og->i_size = i_size;
558 i_pts = 0; // write them only once
560 sout_BufferChain( &p_og_first, p_og );
563 return( p_og_first );
566 static sout_buffer_t *OggCreateHeader( sout_mux_t *p_mux, mtime_t i_dts )
568 sout_buffer_t *p_hdr = NULL;
573 /* Write header for each stream. All b_o_s (beginning of stream) packets
574 * must appear first in the ogg stream so we take care of them first. */
575 for( i = 0; i < p_mux->i_nb_inputs; i++ )
577 ogg_stream_t *p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
578 p_stream->b_new = VLC_FALSE;
580 msg_Dbg( p_mux, "creating header for %4.4s",
581 (char *)&p_stream->i_fourcc );
583 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
584 p_stream->i_packet_no = 0;
586 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
587 p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
589 /* Special case, headers are already there in the
590 * incoming stream or we backed them up earlier */
592 /* first packet in order: vorbis/theora info */
593 if( !p_stream->i_sout_headers )
595 p_og = sout_FifoGet( p_mux->pp_inputs[i]->p_fifo );
596 op.packet = p_og->p_buffer;
597 op.bytes = p_og->i_size;
601 op.packetno = p_stream->i_packet_no++;
602 ogg_stream_packetin( &p_stream->os, &op );
603 p_stream->pp_sout_headers[0] =
604 OggStreamFlush( p_mux, &p_stream->os, 0 );
605 p_stream->i_sout_headers++;
607 p_og = sout_BufferDuplicate( p_mux->p_sout,
608 p_stream->pp_sout_headers[0] );
613 op.packet = (uint8_t*)&p_stream->oggds_header;
614 op.bytes = sizeof( oggds_header_t );
618 op.packetno = p_stream->i_packet_no++;
619 ogg_stream_packetin( &p_stream->os, &op );
620 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
623 sout_BufferChain( &p_hdr, p_og );
626 /* Take care of the non b_o_s headers */
627 for( i = 0; i < p_mux->i_nb_inputs; i++ )
629 ogg_stream_t *p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
631 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
632 p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
634 /* Special case, headers are already there in the incoming stream.
635 * We need to gather them an mark them as headers. */
637 for( j = 0; j < 2; j++ )
639 if( p_stream->i_sout_headers < j + 2 )
641 /* next packets in order: comments and codebooks */
642 p_og = sout_FifoGet( p_mux->pp_inputs[i]->p_fifo );
643 op.packet = p_og->p_buffer;
644 op.bytes = p_og->i_size;
648 op.packetno = p_stream->i_packet_no++;
649 ogg_stream_packetin( &p_stream->os, &op );
650 p_stream->pp_sout_headers[j+1] =
651 OggStreamFlush( p_mux, &p_stream->os, 0 );
652 p_stream->i_sout_headers++;
655 p_og = sout_BufferDuplicate( p_mux->p_sout,
656 p_stream->pp_sout_headers[j+1] );
657 sout_BufferChain( &p_hdr, p_og );
666 com[0] = PACKET_TYPE_COMMENT;
667 i_com = snprintf( &com[1], 128, VERSION" stream output" ) + 1;
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 );
676 sout_BufferChain( &p_hdr, p_og );
680 /* set HEADER flag */
681 for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
683 p_og->i_flags |= SOUT_BUFFER_FLAGS_HEADER;
688 static sout_buffer_t *OggCreateFooter( sout_mux_t *p_mux, mtime_t i_dts )
690 sout_mux_sys_t *p_sys = p_mux->p_sys;
691 sout_buffer_t *p_hdr = NULL;
696 /* flush all remaining data */
697 for( i = 0; i < p_mux->i_nb_inputs; i++ )
699 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
701 /* skip newly added streams */
702 if( p_stream->b_new ) continue;
704 if( ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
706 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
707 sout_AccessOutWrite( p_mux->p_access, p_og );
711 /* Write eos packets for each stream. */
712 for( i = 0; i < p_mux->i_nb_inputs; i++ )
714 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
716 /* skip newly added streams */
717 if( p_stream->b_new ) continue;
724 op.packetno = p_stream->i_packet_no++;
725 ogg_stream_packetin( &p_stream->os, &op );
727 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
728 sout_BufferChain( &p_hdr, p_og );
729 ogg_stream_clear( &p_stream->os );
732 for( i = 0; i < p_sys->i_del_streams; i++ )
739 op.packetno = p_sys->pp_del_streams[i]->i_packet_no++;
740 ogg_stream_packetin( &p_sys->pp_del_streams[i]->os, &op );
742 p_og = OggStreamFlush( p_mux, &p_sys->pp_del_streams[i]->os, 0 );
743 sout_BufferChain( &p_hdr, p_og );
744 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
750 static void OggSetDate( sout_buffer_t *p_og, mtime_t i_dts, mtime_t i_length )
753 sout_buffer_t *p_tmp;
756 for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
760 i_delta = i_length / i_count;
762 for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
764 p_tmp->i_dts = i_dts;
765 p_tmp->i_length = i_delta;
771 /*****************************************************************************
772 * Mux: multiplex available data in input fifos into the Ogg bitstream
773 *****************************************************************************/
774 static int Mux( sout_mux_t *p_mux )
776 sout_mux_sys_t *p_sys = p_mux->p_sys;
777 sout_buffer_t *p_og = NULL;
781 if( p_sys->i_add_streams || p_sys->i_del_streams )
783 /* Open new ogg stream */
784 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
786 msg_Dbg( p_mux, "waiting for data..." );
787 return( VLC_SUCCESS );
790 if( p_sys->i_streams )
792 /* Close current ogg stream */
795 msg_Dbg( p_mux, "writing footer" );
796 sout_BufferChain( &p_og, OggCreateFooter( p_mux, 0 ) );
798 /* Remove deleted logical streams */
799 for( i = 0; i < p_sys->i_del_streams; i++ )
801 FREE( p_sys->pp_del_streams[i] );
803 FREE( p_sys->pp_del_streams );
804 p_sys->i_streams = 0;
807 msg_Dbg( p_mux, "writing header" );
808 p_sys->i_start_dts = i_dts;
809 p_sys->i_streams = p_mux->i_nb_inputs;
810 p_sys->i_del_streams = 0;
811 p_sys->i_add_streams = 0;
812 sout_BufferChain( &p_og, OggCreateHeader( p_mux, i_dts ) );
814 /* Write header and/or footer */
815 OggSetDate( p_og, i_dts, 0 );
816 sout_AccessOutWrite( p_mux->p_access, p_og );
822 sout_input_t *p_input;
823 ogg_stream_t *p_stream;
824 sout_buffer_t *p_data;
827 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
829 return( VLC_SUCCESS );
832 p_input = p_mux->pp_inputs[i_stream];
833 p_stream = (ogg_stream_t*)p_input->p_sys;
834 p_data = sout_FifoGet( p_input->p_fifo );
836 if( p_stream->i_fourcc != VLC_FOURCC( 'v', 'o', 'r', 'b' ) &&
837 p_stream->i_fourcc != VLC_FOURCC( 't', 'h', 'e', 'o' ) )
839 sout_BufferReallocFromPreHeader( p_mux->p_sout, p_data, 1 );
840 p_data->p_buffer[0] = PACKET_IS_SYNCPOINT; // FIXME
843 op.packet = p_data->p_buffer;
844 op.bytes = p_data->i_size;
847 op.packetno = p_stream->i_packet_no++;
849 if( p_stream->i_cat == AUDIO_ES )
851 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
853 /* number of sample from begining + current packet */
855 ( i_dts + p_data->i_length - p_sys->i_start_dts ) *
856 p_input->p_fmt->i_sample_rate / I64C(1000000);
860 /* number of sample from begining */
861 op.granulepos = ( i_dts - p_sys->i_start_dts ) *
862 p_stream->oggds_header.i_samples_per_unit / I64C(1000000);
865 else if( p_stream->i_cat == VIDEO_ES )
867 if( p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
869 op.granulepos = op.packetno; /* FIXME */
872 op.granulepos = ( i_dts - p_sys->i_start_dts ) * I64C(10) /
873 p_stream->oggds_header.i_time_unit;
875 else if( p_stream->i_cat == SPU_ES )
877 /* granulepos is in milisec */
878 op.granulepos = ( i_dts - p_sys->i_start_dts ) / 1000;
881 ogg_stream_packetin( &p_stream->os, &op );
883 if( p_stream->i_cat == SPU_ES )
885 /* Subtitles need to be flushed to be sent on time */
886 sout_BufferChain( &p_og, OggStreamFlush( p_mux, &p_stream->os,
891 sout_BufferChain( &p_og, OggStreamPageOut( p_mux, &p_stream->os,
897 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
898 p_stream->i_dts = -1;
899 p_stream->i_length = 0;
901 sout_AccessOutWrite( p_mux->p_access, p_og );
907 if( p_stream->i_dts < 0 )
909 p_stream->i_dts = p_data->i_dts;
911 p_stream->i_length += p_data->i_length;
914 sout_BufferDelete( p_mux->p_sout, p_data );
917 return( VLC_SUCCESS );