1 /*****************************************************************************
2 * ogg.c: ogg muxer module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: ogg.c,v 1.14 2003/09/29 22:37:36 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;
113 int16_t i_padding_0; // hum hum
116 oggds_header_video_t video;
117 oggds_header_audio_t audio;
122 /* Helper writer functions */
124 #define SetWLE( p, v ) _SetWLE( (uint8_t*)p, v)
125 static void _SetWLE( uint8_t *p, uint16_t i_dw )
127 p[1] = ( i_dw >> 8 )&0xff;
128 p[0] = ( i_dw )&0xff;
131 #define SetDWLE( p, v ) _SetDWLE( (uint8_t*)p, v)
132 static void _SetDWLE( uint8_t *p, uint32_t i_dw )
134 p[3] = ( i_dw >> 24 )&0xff;
135 p[2] = ( i_dw >> 16 )&0xff;
136 p[1] = ( i_dw >> 8 )&0xff;
137 p[0] = ( i_dw )&0xff;
139 #define SetQWLE( p, v ) _SetQWLE( (uint8_t*)p, v)
140 static void _SetQWLE( uint8_t *p, uint64_t i_qw )
142 SetDWLE( p, i_qw&0xffffffff );
143 SetDWLE( p+4, ( i_qw >> 32)&0xffffffff );
147 * TODO move this function to src/stream_output.c (used by nearly all muxers)
149 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
155 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
159 p_fifo = p_mux->pp_inputs[i]->p_fifo;
161 /* We don't really need to have anything in the SPU fifo */
162 if( p_mux->pp_inputs[i]->p_fmt->i_cat == SPU_ES &&
163 p_fifo->i_depth == 0 ) continue;
165 if( p_fifo->i_depth > 2 ||
166 /* Special case for SPUs */
167 ( p_mux->pp_inputs[i]->p_fmt->i_cat == SPU_ES &&
168 p_fifo->i_depth > 0 ) )
170 sout_buffer_t *p_buf;
172 p_buf = sout_FifoShow( p_fifo );
173 if( i_stream < 0 || p_buf->i_dts < i_dts )
175 i_dts = p_buf->i_dts;
181 // wait that all fifo have at least 3 packets (3 vorbis headers)
187 *pi_stream = i_stream;
196 /*****************************************************************************
197 * Definitions of structures and functions used by this plugins
198 *****************************************************************************/
212 oggds_header_t oggds_header;
214 sout_buffer_t *pp_sout_headers[3];
219 struct sout_mux_sys_t
225 /* number of logical streams pending to be added */
228 /* logical streams pending to be deleted */
230 ogg_stream_t **pp_del_streams;
233 static void OggSetDate( sout_buffer_t *, mtime_t , mtime_t );
234 static sout_buffer_t *OggStreamFlush( sout_mux_t *, ogg_stream_state *,
237 /*****************************************************************************
239 *****************************************************************************/
240 static int Open( vlc_object_t *p_this )
242 sout_mux_t *p_mux = (sout_mux_t*)p_this;
243 sout_mux_sys_t *p_sys;
245 msg_Info( p_mux, "Open" );
247 p_sys = malloc( sizeof( sout_mux_sys_t ) );
248 p_sys->i_streams = 0;
249 p_sys->i_add_streams = 0;
250 p_sys->i_del_streams = 0;
251 p_sys->pp_del_streams = 0;
253 p_mux->p_sys = p_sys;
254 p_mux->pf_capacity = Capability;
255 p_mux->pf_addstream = AddStream;
256 p_mux->pf_delstream = DelStream;
258 p_mux->i_preheader = 1;
263 /*****************************************************************************
264 * Close: Finalize ogg bitstream and close muxer
265 *****************************************************************************/
266 static void Close( vlc_object_t * p_this )
268 sout_mux_t *p_mux = (sout_mux_t*)p_this;
269 sout_mux_sys_t *p_sys = p_mux->p_sys;
271 msg_Info( p_mux, "Close" );
273 if( p_sys->i_del_streams )
275 sout_buffer_t *p_og = NULL;
279 /* Close the current ogg stream */
280 msg_Dbg( p_mux, "writing footer" );
281 sout_BufferChain( &p_og, OggCreateFooter( p_mux, 0 ) );
283 /* Remove deleted logical streams */
284 for( i = 0; i < p_sys->i_del_streams; i++ )
286 i_dts = p_sys->pp_del_streams[i]->i_dts;
287 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
288 FREE( p_sys->pp_del_streams[i] );
290 FREE( p_sys->pp_del_streams );
291 p_sys->i_streams -= p_sys->i_del_streams;
294 OggSetDate( p_og, i_dts, 0 );
295 sout_AccessOutWrite( p_mux->p_access, p_og );
301 static int Capability( sout_mux_t *p_mux, int i_query, void *p_args,
306 case SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME:
307 *(vlc_bool_t*)p_answer = VLC_TRUE;
308 return( SOUT_MUX_CAP_ERR_OK );
310 return( SOUT_MUX_CAP_ERR_UNIMPLEMENTED );
314 /*****************************************************************************
315 * AddStream: Add an elementary stream to the muxed stream
316 *****************************************************************************/
317 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
319 sout_mux_sys_t *p_sys = p_mux->p_sys;
320 ogg_stream_t *p_stream;
322 msg_Dbg( p_mux, "adding input" );
324 p_input->p_sys = (void *)p_stream = malloc( sizeof( ogg_stream_t ) );
326 p_stream->i_cat = p_input->p_fmt->i_cat;
327 p_stream->i_fourcc = p_input->p_fmt->i_fourcc;
328 p_stream->i_serial_no = rand();
329 p_stream->i_packet_no = 0;
331 p_stream->i_sout_headers = 0;
333 p_stream->oggds_header.i_packet_type = PACKET_TYPE_HEADER;
334 switch( p_input->p_fmt->i_cat )
337 switch( p_stream->i_fourcc )
339 case VLC_FOURCC( 'm', 'p','4', 'v' ):
340 case VLC_FOURCC( 'D', 'I','V', '3' ):
341 memcpy( p_stream->oggds_header.stream_type, "video ", 8 );
342 if( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p','4', 'v' ) )
344 memcpy( p_stream->oggds_header.sub_type, "XVID", 4 );
346 else if( p_stream->i_fourcc == VLC_FOURCC( 'D', 'I','V', '3' ) )
348 memcpy( p_stream->oggds_header.sub_type, "DIV3", 4 );
350 SetDWLE( &p_stream->oggds_header.i_size,
351 sizeof( oggds_header_t ) - 1);
352 SetQWLE( &p_stream->oggds_header.i_time_unit,
353 I64C(10000000)/(int64_t)25 ); // FIXME (25fps)
354 SetQWLE( &p_stream->oggds_header.i_samples_per_unit, 1 );
355 SetDWLE( &p_stream->oggds_header.i_default_len, 0 ); /* ??? */
356 SetDWLE( &p_stream->oggds_header.i_buffer_size, 1024*1024 );
357 SetWLE( &p_stream->oggds_header.i_bits_per_sample, 0 );
358 SetDWLE( &p_stream->oggds_header.header.video.i_width,
359 p_input->p_fmt->i_width );
360 SetDWLE( &p_stream->oggds_header.header.video.i_height,
361 p_input->p_fmt->i_height );
362 msg_Dbg( p_mux, "mp4v/div3 stream" );
365 case VLC_FOURCC( 't', 'h', 'e', 'o' ):
366 msg_Dbg( p_mux, "theora stream" );
370 FREE( p_input->p_sys );
371 return( VLC_EGENERIC );
376 switch( p_stream->i_fourcc )
378 case VLC_FOURCC( 'm', 'p','g', 'a' ):
379 case VLC_FOURCC( 'a', '5','2', ' ' ):
380 memcpy( p_stream->oggds_header.stream_type, "audio ", 8 );
381 if( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p','g', 'a' ) )
383 memcpy( p_stream->oggds_header.sub_type, "55 ", 4 );
385 else if( p_stream->i_fourcc == VLC_FOURCC( 'a', '5','2', ' ' ) )
387 memcpy( p_stream->oggds_header.sub_type, "2000", 4 );
389 SetDWLE( &p_stream->oggds_header.i_size,
390 sizeof( oggds_header_t ) - 1);
391 SetQWLE( &p_stream->oggds_header.i_time_unit, 0 /* not used */ );
392 SetDWLE( &p_stream->oggds_header.i_default_len, 0 /* not used */ );
393 SetDWLE( &p_stream->oggds_header.i_buffer_size, 30*1024 );
394 SetQWLE( &p_stream->oggds_header.i_samples_per_unit,
395 p_input->p_fmt->i_sample_rate );
396 SetWLE( &p_stream->oggds_header.i_bits_per_sample, 0 );
397 SetDWLE( &p_stream->oggds_header.header.audio.i_channels,
398 p_input->p_fmt->i_channels );
399 SetDWLE( &p_stream->oggds_header.header.audio.i_block_align,
400 p_input->p_fmt->i_block_align );
401 SetDWLE( &p_stream->oggds_header.header.audio.i_avgbytespersec, 0);
402 msg_Dbg( p_mux, "mpga/a52 stream" );
405 case VLC_FOURCC( 'v', 'o', 'r', 'b' ):
406 msg_Dbg( p_mux, "vorbis stream" );
410 FREE( p_input->p_sys );
411 return( VLC_EGENERIC );
416 switch( p_stream->i_fourcc )
418 case VLC_FOURCC( 's', 'u','b', 't' ):
419 memcpy( p_stream->oggds_header.stream_type, "text ", 8 );
420 msg_Dbg( p_mux, "subtitles stream" );
424 FREE( p_input->p_sys );
425 return( VLC_EGENERIC );
429 FREE( p_input->p_sys );
430 return( VLC_EGENERIC );
433 p_stream->b_new = VLC_TRUE;
435 p_sys->i_add_streams++;
437 return( VLC_SUCCESS );
440 /*****************************************************************************
441 * DelStream: Delete an elementary stream from the muxed stream
442 *****************************************************************************/
443 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
445 sout_mux_sys_t *p_sys = p_mux->p_sys;
446 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
449 msg_Dbg( p_mux, "removing input" );
451 /* flush all remaining data */
456 if( ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
458 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
459 sout_AccessOutWrite( p_mux->p_access, p_og );
462 for( i = 0; i < p_stream->i_sout_headers; i++ )
464 sout_BufferDelete( p_mux->p_sout, p_stream->pp_sout_headers[i] );
465 p_stream->i_sout_headers = 0;
468 /* move input in delete queue */
469 if( !p_stream->b_new )
471 p_sys->pp_del_streams = realloc( p_sys->pp_del_streams,
472 (p_sys->i_del_streams + 1) *
473 sizeof(ogg_stream_t *) );
474 p_sys->pp_del_streams[p_sys->i_del_streams++] = p_stream;
478 /* Wasn't already added so get rid of it */
479 ogg_stream_clear( &p_stream->os );
481 p_sys->i_add_streams--;
485 p_input->p_sys = NULL;
490 /*****************************************************************************
491 * Ogg bitstream manipulation routines
492 *****************************************************************************/
493 static sout_buffer_t *OggStreamFlush( sout_mux_t *p_mux,
494 ogg_stream_state *p_os, mtime_t i_pts )
496 sout_buffer_t *p_og, *p_og_first = NULL;
504 if( ( i_result = ogg_stream_flush( p_os, &og ) ) == 0 )
509 i_size = og.header_len + og.body_len;
510 p_og = sout_BufferNew( p_mux->p_sout, i_size);
512 memcpy( p_og->p_buffer, og.header, og.header_len );
513 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
514 p_og->i_size = i_size;
519 i_pts = 0; // write it only once
521 sout_BufferChain( &p_og_first, p_og );
524 return( p_og_first );
527 static sout_buffer_t *OggStreamPageOut( sout_mux_t *p_mux,
528 ogg_stream_state *p_os, mtime_t i_pts )
530 sout_buffer_t *p_og, *p_og_first = NULL;
538 if( ( i_result = ogg_stream_pageout( p_os, &og ) ) == 0 )
543 i_size = og.header_len + og.body_len;
544 p_og = sout_BufferNew( p_mux->p_sout, i_size);
546 memcpy( p_og->p_buffer, og.header, og.header_len );
547 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
548 p_og->i_size = i_size;
553 i_pts = 0; // write them only once
555 sout_BufferChain( &p_og_first, p_og );
558 return( p_og_first );
561 static sout_buffer_t *OggCreateHeader( sout_mux_t *p_mux, mtime_t i_dts )
563 sout_buffer_t *p_hdr = NULL;
568 /* Write header for each stream. All b_o_s (beginning of stream) packets
569 * must appear first in the ogg stream so we take care of them first. */
570 for( i = 0; i < p_mux->i_nb_inputs; i++ )
572 ogg_stream_t *p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
573 p_stream->b_new = VLC_FALSE;
575 msg_Dbg( p_mux, "creating header for %4.4s",
576 (char *)&p_stream->i_fourcc );
578 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
579 p_stream->i_packet_no = 0;
581 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
582 p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
584 /* Special case, headers are already there in the
585 * incoming stream or we backed them up earlier */
587 /* first packet in order: vorbis/theora info */
588 if( !p_stream->i_sout_headers )
590 p_og = sout_FifoGet( p_mux->pp_inputs[i]->p_fifo );
591 op.packet = p_og->p_buffer;
592 op.bytes = p_og->i_size;
596 op.packetno = p_stream->i_packet_no++;
597 ogg_stream_packetin( &p_stream->os, &op );
598 p_stream->pp_sout_headers[0] =
599 OggStreamFlush( p_mux, &p_stream->os, 0 );
600 p_stream->i_sout_headers++;
602 p_og = sout_BufferDuplicate( p_mux->p_sout,
603 p_stream->pp_sout_headers[0] );
608 op.packet = (uint8_t*)&p_stream->oggds_header;
609 op.bytes = sizeof( oggds_header_t );
613 op.packetno = p_stream->i_packet_no++;
614 ogg_stream_packetin( &p_stream->os, &op );
615 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
618 sout_BufferChain( &p_hdr, p_og );
621 /* Take care of the non b_o_s headers */
622 for( i = 0; i < p_mux->i_nb_inputs; i++ )
624 ogg_stream_t *p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
626 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
627 p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
629 /* Special case, headers are already there in the incoming stream.
630 * We need to gather them an mark them as headers. */
632 for( j = 0; j < 2; j++ )
634 if( p_stream->i_sout_headers < j + 2 )
636 /* next packets in order: comments and codebooks */
637 p_og = sout_FifoGet( p_mux->pp_inputs[i]->p_fifo );
638 op.packet = p_og->p_buffer;
639 op.bytes = p_og->i_size;
643 op.packetno = p_stream->i_packet_no++;
644 ogg_stream_packetin( &p_stream->os, &op );
645 p_stream->pp_sout_headers[j+1] =
646 OggStreamFlush( p_mux, &p_stream->os, 0 );
647 p_stream->i_sout_headers++;
650 p_og = sout_BufferDuplicate( p_mux->p_sout,
651 p_stream->pp_sout_headers[j+1] );
652 sout_BufferChain( &p_hdr, p_og );
661 com[0] = PACKET_TYPE_COMMENT;
662 i_com = snprintf( &com[1], 128, VERSION" stream output" ) + 1;
668 op.packetno = p_stream->i_packet_no++;
669 ogg_stream_packetin( &p_stream->os, &op );
670 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
671 sout_BufferChain( &p_hdr, p_og );
675 /* set HEADER flag */
676 for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
678 p_og->i_flags |= SOUT_BUFFER_FLAGS_HEADER;
683 static sout_buffer_t *OggCreateFooter( sout_mux_t *p_mux, mtime_t i_dts )
685 sout_mux_sys_t *p_sys = p_mux->p_sys;
686 sout_buffer_t *p_hdr = NULL;
691 /* flush all remaining data */
692 for( i = 0; i < p_mux->i_nb_inputs; i++ )
694 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
696 /* skip newly added streams */
697 if( p_stream->b_new ) continue;
699 if( ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
701 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
702 sout_AccessOutWrite( p_mux->p_access, p_og );
706 /* Write eos packets for each stream. */
707 for( i = 0; i < p_mux->i_nb_inputs; i++ )
709 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
711 /* skip newly added streams */
712 if( p_stream->b_new ) continue;
719 op.packetno = p_stream->i_packet_no++;
720 ogg_stream_packetin( &p_stream->os, &op );
722 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
723 sout_BufferChain( &p_hdr, p_og );
724 ogg_stream_clear( &p_stream->os );
727 for( i = 0; i < p_sys->i_del_streams; i++ )
734 op.packetno = p_sys->pp_del_streams[i]->i_packet_no++;
735 ogg_stream_packetin( &p_sys->pp_del_streams[i]->os, &op );
737 p_og = OggStreamFlush( p_mux, &p_sys->pp_del_streams[i]->os, 0 );
738 sout_BufferChain( &p_hdr, p_og );
739 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
745 static void OggSetDate( sout_buffer_t *p_og, mtime_t i_dts, mtime_t i_length )
748 sout_buffer_t *p_tmp;
751 for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
755 i_delta = i_length / i_count;
757 for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
759 p_tmp->i_dts = i_dts;
760 p_tmp->i_length = i_delta;
766 /*****************************************************************************
767 * Mux: multiplex available data in input fifos into the Ogg bitstream
768 *****************************************************************************/
769 static int Mux( sout_mux_t *p_mux )
771 sout_mux_sys_t *p_sys = p_mux->p_sys;
772 sout_buffer_t *p_og = NULL;
776 if( p_sys->i_add_streams || p_sys->i_del_streams )
778 /* Open new ogg stream */
779 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
781 msg_Dbg( p_mux, "waiting for data..." );
782 return( VLC_SUCCESS );
785 if( p_sys->i_streams )
787 /* Close current ogg stream */
790 msg_Dbg( p_mux, "writing footer" );
791 sout_BufferChain( &p_og, OggCreateFooter( p_mux, 0 ) );
793 /* Remove deleted logical streams */
794 for( i = 0; i < p_sys->i_del_streams; i++ )
796 FREE( p_sys->pp_del_streams[i] );
798 FREE( p_sys->pp_del_streams );
799 p_sys->i_streams = 0;
802 msg_Dbg( p_mux, "writing header" );
803 p_sys->i_start_dts = i_dts;
804 p_sys->i_streams = p_mux->i_nb_inputs;
805 p_sys->i_del_streams = 0;
806 p_sys->i_add_streams = 0;
807 sout_BufferChain( &p_og, OggCreateHeader( p_mux, i_dts ) );
809 /* Write header and/or footer */
810 OggSetDate( p_og, i_dts, 0 );
811 sout_AccessOutWrite( p_mux->p_access, p_og );
817 sout_input_t *p_input;
818 ogg_stream_t *p_stream;
819 sout_buffer_t *p_data;
822 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
824 return( VLC_SUCCESS );
827 p_input = p_mux->pp_inputs[i_stream];
828 p_stream = (ogg_stream_t*)p_input->p_sys;
829 p_data = sout_FifoGet( p_input->p_fifo );
831 if( p_stream->i_fourcc != VLC_FOURCC( 'v', 'o', 'r', 'b' ) &&
832 p_stream->i_fourcc != VLC_FOURCC( 't', 'h', 'e', 'o' ) )
834 sout_BufferReallocFromPreHeader( p_mux->p_sout, p_data, 1 );
835 p_data->p_buffer[0] = PACKET_IS_SYNCPOINT; // FIXME
838 op.packet = p_data->p_buffer;
839 op.bytes = p_data->i_size;
842 op.packetno = p_stream->i_packet_no++;
844 if( p_stream->i_cat == AUDIO_ES )
846 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
848 /* number of sample from begining + current packet */
850 ( i_dts + p_data->i_length - p_sys->i_start_dts ) *
851 p_input->p_fmt->i_sample_rate / I64C(1000000);
855 /* number of sample from begining */
856 op.granulepos = ( i_dts - p_sys->i_start_dts ) *
857 p_stream->oggds_header.i_samples_per_unit / I64C(1000000);
860 else if( p_stream->i_cat == VIDEO_ES )
862 if( p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
864 op.granulepos = op.packetno; /* FIXME */
867 op.granulepos = ( i_dts - p_sys->i_start_dts ) * I64C(10) /
868 p_stream->oggds_header.i_time_unit;
870 else if( p_stream->i_cat == SPU_ES )
872 /* granulepos is in milisec */
873 op.granulepos = ( i_dts - p_sys->i_start_dts ) / 1000;
876 ogg_stream_packetin( &p_stream->os, &op );
878 if( p_stream->i_cat == SPU_ES )
880 /* Subtitles need to be flushed to be sent on time */
881 sout_BufferChain( &p_og, OggStreamFlush( p_mux, &p_stream->os,
886 sout_BufferChain( &p_og, OggStreamPageOut( p_mux, &p_stream->os,
892 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
893 p_stream->i_dts = -1;
894 p_stream->i_length = 0;
896 sout_AccessOutWrite( p_mux->p_access, p_og );
902 if( p_stream->i_dts < 0 )
904 p_stream->i_dts = p_data->i_dts;
906 p_stream->i_length += p_data->i_length;
909 sout_BufferDelete( p_mux->p_sout, p_data );
912 return( VLC_SUCCESS );