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 Capability(sout_mux_t *, int, void *, void * );
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 *,
223 /*****************************************************************************
225 *****************************************************************************/
226 static int Open( vlc_object_t *p_this )
228 sout_mux_t *p_mux = (sout_mux_t*)p_this;
229 sout_mux_sys_t *p_sys;
231 msg_Info( p_mux, "Open" );
233 p_sys = malloc( sizeof( sout_mux_sys_t ) );
234 p_sys->i_streams = 0;
235 p_sys->i_add_streams = 0;
236 p_sys->i_del_streams = 0;
237 p_sys->pp_del_streams = 0;
239 p_mux->p_sys = p_sys;
240 p_mux->pf_capacity = Capability;
241 p_mux->pf_addstream = AddStream;
242 p_mux->pf_delstream = DelStream;
245 /* First serial number is random.
246 * (Done like this because on win32 you need to seed the random number
247 * generator once per thread). */
248 srand( (unsigned int)time( NULL ) );
249 p_sys->i_next_serial_no = rand();
254 /*****************************************************************************
255 * Close: Finalize ogg bitstream and close muxer
256 *****************************************************************************/
257 static void Close( vlc_object_t * p_this )
259 sout_mux_t *p_mux = (sout_mux_t*)p_this;
260 sout_mux_sys_t *p_sys = p_mux->p_sys;
262 msg_Info( p_mux, "Close" );
264 if( p_sys->i_del_streams )
266 block_t *p_og = NULL;
270 /* Close the current ogg stream */
271 msg_Dbg( p_mux, "writing footer" );
272 block_ChainAppend( &p_og, OggCreateFooter( p_mux, 0 ) );
274 /* Remove deleted logical streams */
275 for( i = 0; i < p_sys->i_del_streams; i++ )
277 i_dts = p_sys->pp_del_streams[i]->i_dts;
278 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
279 FREE( p_sys->pp_del_streams[i] );
281 FREE( p_sys->pp_del_streams );
282 p_sys->i_streams -= p_sys->i_del_streams;
285 OggSetDate( p_og, i_dts, 0 );
286 sout_AccessOutWrite( p_mux->p_access, p_og );
292 static int Capability( sout_mux_t *p_mux, int i_query, void *p_args,
297 case SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME:
298 *(vlc_bool_t*)p_answer = VLC_TRUE;
299 return( SOUT_MUX_CAP_ERR_OK );
300 case SOUT_MUX_CAP_GET_ADD_STREAM_WAIT:
301 *(vlc_bool_t*)p_answer = VLC_TRUE;
302 return( SOUT_MUX_CAP_ERR_OK );
304 return( SOUT_MUX_CAP_ERR_UNIMPLEMENTED );
308 /*****************************************************************************
309 * AddStream: Add an elementary stream to the muxed stream
310 *****************************************************************************/
311 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
313 sout_mux_sys_t *p_sys = p_mux->p_sys;
314 ogg_stream_t *p_stream;
316 msg_Dbg( p_mux, "adding input" );
318 p_input->p_sys = (void *)p_stream = malloc( sizeof( ogg_stream_t ) );
320 p_stream->i_cat = p_input->p_fmt->i_cat;
321 p_stream->i_fourcc = p_input->p_fmt->i_codec;
322 p_stream->i_serial_no = p_sys->i_next_serial_no++;
323 p_stream->i_packet_no = 0;
325 p_stream->i_sout_headers = 0;
327 memset( &p_stream->oggds_header, 0, sizeof(p_stream->oggds_header) );
328 p_stream->oggds_header.i_packet_type = PACKET_TYPE_HEADER;
329 switch( p_input->p_fmt->i_cat )
332 switch( p_stream->i_fourcc )
334 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
335 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
336 case VLC_FOURCC( 'D', 'I', 'V', '3' ):
337 case VLC_FOURCC( 'M', 'J', 'P', 'G' ):
338 case VLC_FOURCC( 'W', 'M', 'V', '1' ):
339 case VLC_FOURCC( 'W', 'M', 'V', '2' ):
340 memcpy( p_stream->oggds_header.stream_type, "video", 5 );
341 if( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p', '4', 'v' ) )
343 memcpy( p_stream->oggds_header.sub_type, "XVID", 4 );
345 else if( p_stream->i_fourcc == VLC_FOURCC( 'D', 'I', 'V', '3' ) )
347 memcpy( p_stream->oggds_header.sub_type, "DIV3", 4 );
351 memcpy(p_stream->oggds_header.sub_type,&p_stream->i_fourcc,4);
353 SetDWLE( &p_stream->oggds_header.i_size,
354 sizeof( oggds_header_t ) - 1);
355 SetQWLE( &p_stream->oggds_header.i_time_unit,
356 I64C(10000000)/(int64_t)25 ); // FIXME (25fps)
357 SetQWLE( &p_stream->oggds_header.i_samples_per_unit, 1 );
358 SetDWLE( &p_stream->oggds_header.i_default_len, 1 ); /* ??? */
359 SetDWLE( &p_stream->oggds_header.i_buffer_size, 1024*1024 );
360 SetWLE( &p_stream->oggds_header.i_bits_per_sample, 0 );
361 SetDWLE( &p_stream->oggds_header.header.video.i_width,
362 p_input->p_fmt->video.i_width );
363 SetDWLE( &p_stream->oggds_header.header.video.i_height,
364 p_input->p_fmt->video.i_height );
365 msg_Dbg( p_mux, "%4.4s stream", (char *)&p_stream->i_fourcc );
368 case VLC_FOURCC( 't', 'h', 'e', 'o' ):
369 msg_Dbg( p_mux, "theora stream" );
373 FREE( p_input->p_sys );
374 return( VLC_EGENERIC );
379 switch( p_stream->i_fourcc )
381 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
382 case VLC_FOURCC( 'a', '5', '2', ' ' ):
383 memcpy( p_stream->oggds_header.stream_type, "audio", 5 );
384 if( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p', 'g', 'a' ) )
386 memcpy( p_stream->oggds_header.sub_type, "55 ", 4 );
388 else if( p_stream->i_fourcc == VLC_FOURCC( 'a', '5', '2', ' ' ) )
390 memcpy( p_stream->oggds_header.sub_type, "2000", 4 );
392 SetDWLE( &p_stream->oggds_header.i_size,
393 sizeof( oggds_header_t ) - 1);
394 SetQWLE( &p_stream->oggds_header.i_time_unit, 0 /* not used */ );
395 SetDWLE( &p_stream->oggds_header.i_default_len, 1 );
396 SetDWLE( &p_stream->oggds_header.i_buffer_size, 30*1024 );
397 SetQWLE( &p_stream->oggds_header.i_samples_per_unit,
398 p_input->p_fmt->audio.i_rate );
399 SetWLE( &p_stream->oggds_header.i_bits_per_sample, 0 );
400 SetDWLE( &p_stream->oggds_header.header.audio.i_channels,
401 p_input->p_fmt->audio.i_channels );
402 SetDWLE( &p_stream->oggds_header.header.audio.i_block_align,
403 p_input->p_fmt->audio.i_blockalign );
404 SetDWLE( &p_stream->oggds_header.header.audio.i_avgbytespersec, 0);
405 msg_Dbg( p_mux, "mpga/a52 stream" );
408 case VLC_FOURCC( 'v', 'o', 'r', 'b' ):
409 msg_Dbg( p_mux, "vorbis stream" );
412 case VLC_FOURCC( 's', 'p', 'x', ' ' ):
413 msg_Dbg( p_mux, "speex stream" );
416 case VLC_FOURCC( 'f', 'l', 'a', 'c' ):
417 msg_Dbg( p_mux, "flac stream" );
421 FREE( p_input->p_sys );
422 return( VLC_EGENERIC );
427 switch( p_stream->i_fourcc )
429 case VLC_FOURCC( 's', 'u','b', 't' ):
430 memcpy( p_stream->oggds_header.stream_type, "text", 4 );
431 msg_Dbg( p_mux, "subtitles stream" );
435 FREE( p_input->p_sys );
436 return( VLC_EGENERIC );
440 FREE( p_input->p_sys );
441 return( VLC_EGENERIC );
444 p_stream->b_new = VLC_TRUE;
446 p_sys->i_add_streams++;
448 return( VLC_SUCCESS );
451 /*****************************************************************************
452 * DelStream: Delete an elementary stream from the muxed stream
453 *****************************************************************************/
454 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
456 sout_mux_sys_t *p_sys = p_mux->p_sys;
457 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
460 msg_Dbg( p_mux, "removing input" );
462 /* flush all remaining data */
467 if( !p_stream->b_new &&
468 ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
470 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
471 sout_AccessOutWrite( p_mux->p_access, p_og );
474 for( i = 0; i < p_stream->i_sout_headers; i++ )
476 block_Release( p_stream->pp_sout_headers[i] );
477 p_stream->i_sout_headers = 0;
480 /* move input in delete queue */
481 if( !p_stream->b_new )
483 p_sys->pp_del_streams = realloc( p_sys->pp_del_streams,
484 (p_sys->i_del_streams + 1) *
485 sizeof(ogg_stream_t *) );
486 p_sys->pp_del_streams[p_sys->i_del_streams++] = p_stream;
490 /* Wasn't already added so get rid of it */
491 ogg_stream_clear( &p_stream->os );
493 p_sys->i_add_streams--;
497 p_input->p_sys = NULL;
502 /*****************************************************************************
503 * Ogg bitstream manipulation routines
504 *****************************************************************************/
505 static block_t *OggStreamFlush( sout_mux_t *p_mux,
506 ogg_stream_state *p_os, mtime_t i_pts )
508 block_t *p_og, *p_og_first = NULL;
516 if( ( i_result = ogg_stream_flush( p_os, &og ) ) == 0 )
521 i_size = og.header_len + og.body_len;
522 p_og = block_New( p_mux, i_size);
524 memcpy( p_og->p_buffer, og.header, og.header_len );
525 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
530 i_pts = 0; // write it only once
532 block_ChainAppend( &p_og_first, p_og );
535 return( p_og_first );
538 static block_t *OggStreamPageOut( sout_mux_t *p_mux,
539 ogg_stream_state *p_os, mtime_t i_pts )
541 block_t *p_og, *p_og_first = NULL;
549 if( ( i_result = ogg_stream_pageout( p_os, &og ) ) == 0 )
554 i_size = og.header_len + og.body_len;
555 p_og = block_New( p_mux, i_size);
557 memcpy( p_og->p_buffer, og.header, og.header_len );
558 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
563 i_pts = 0; // write them only once
565 block_ChainAppend( &p_og_first, p_og );
568 return( p_og_first );
571 static block_t *OggCreateHeader( sout_mux_t *p_mux, mtime_t i_dts )
573 block_t *p_hdr = NULL;
578 /* Write header for each stream. All b_o_s (beginning of stream) packets
579 * must appear first in the ogg stream so we take care of them first. */
580 for( i = 0; i < p_mux->i_nb_inputs; i++ )
582 ogg_stream_t *p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
583 p_stream->b_new = VLC_FALSE;
585 msg_Dbg( p_mux, "creating header for %4.4s",
586 (char *)&p_stream->i_fourcc );
588 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
589 p_stream->i_packet_no = 0;
591 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
592 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) ||
593 p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
595 /* Special case, headers are already there in the
596 * incoming stream or we backed them up earlier */
598 /* first packet in order: vorbis/speex/theora info */
599 if( !p_stream->i_sout_headers )
601 p_og = block_FifoGet( p_mux->pp_inputs[i]->p_fifo );
602 op.packet = p_og->p_buffer;
603 op.bytes = p_og->i_buffer;
607 op.packetno = p_stream->i_packet_no++;
608 ogg_stream_packetin( &p_stream->os, &op );
609 p_stream->pp_sout_headers[0] =
610 OggStreamFlush( p_mux, &p_stream->os, 0 );
611 p_stream->i_sout_headers++;
613 p_og = block_Duplicate( p_stream->pp_sout_headers[0] );
615 /* Get keyframe_granule_shift for theora granulepos calculation */
616 if( p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
618 int i_keyframe_frequency_force = 1 << (op.packet[36] >> 3);
620 /* granule_shift = i_log( frequency_force -1 ) */
621 p_stream->i_keyframe_granule_shift = 0;
622 i_keyframe_frequency_force--;
623 while( i_keyframe_frequency_force )
625 p_stream->i_keyframe_granule_shift++;
626 i_keyframe_frequency_force >>= 1;
630 else if( p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
632 /* flac stream marker (yeah, only that in the 1st packet) */
638 op.packetno = p_stream->i_packet_no++;
639 ogg_stream_packetin( &p_stream->os, &op );
640 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
645 op.packet = (uint8_t*)&p_stream->oggds_header;
646 op.bytes = sizeof( oggds_header_t );
650 op.packetno = p_stream->i_packet_no++;
651 ogg_stream_packetin( &p_stream->os, &op );
652 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
655 block_ChainAppend( &p_hdr, p_og );
658 /* Take care of the non b_o_s headers */
659 for( i = 0; i < p_mux->i_nb_inputs; i++ )
661 ogg_stream_t *p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
663 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
664 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) ||
665 p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
667 /* Special case, headers are already there in the incoming stream.
668 * We need to gather them an mark them as headers. */
670 for( j = 0; j < 2; j++ )
672 if( p_stream->i_sout_headers < j + 2 )
674 /* next packets in order: comments and codebooks */
675 p_og = block_FifoGet( p_mux->pp_inputs[i]->p_fifo );
676 op.packet = p_og->p_buffer;
677 op.bytes = p_og->i_buffer;
681 op.packetno = p_stream->i_packet_no++;
682 ogg_stream_packetin( &p_stream->os, &op );
683 p_stream->pp_sout_headers[j+1] =
684 OggStreamFlush( p_mux, &p_stream->os, 0 );
685 p_stream->i_sout_headers++;
688 p_og = block_Duplicate( p_stream->pp_sout_headers[j+1] );
689 block_ChainAppend( &p_hdr, p_og );
692 else if( p_stream->i_fourcc != VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
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 block_ChainAppend( &p_hdr, p_og );
711 /* Special case for mp4v and flac */
712 if( ( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p', '4', 'v' ) ||
713 p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) ) &&
714 p_mux->pp_inputs[i]->p_fmt->i_extra )
716 /* Send a packet with the VOL data for mp4v
717 * or STREAMINFO for flac */
718 msg_Dbg( p_mux, "writing extra data" );
719 op.bytes = p_mux->pp_inputs[i]->p_fmt->i_extra;
720 op.packet = p_mux->pp_inputs[i]->p_fmt->p_extra;
721 if( p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
723 /* Skip the flac stream marker */
724 ((uint8_t *)op.bytes) -= 4;
725 ((uint8_t *)op.packet) += 4;
730 op.packetno = p_stream->i_packet_no++;
731 ogg_stream_packetin( &p_stream->os, &op );
732 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
733 block_ChainAppend( &p_hdr, p_og );
737 /* set HEADER flag */
738 for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
740 p_og->i_flags |= BLOCK_FLAG_HEADER;
745 static block_t *OggCreateFooter( sout_mux_t *p_mux, mtime_t i_dts )
747 sout_mux_sys_t *p_sys = p_mux->p_sys;
748 block_t *p_hdr = NULL;
753 /* flush all remaining data */
754 for( i = 0; i < p_mux->i_nb_inputs; i++ )
756 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
758 /* skip newly added streams */
759 if( p_stream->b_new ) continue;
761 if( ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
763 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
764 sout_AccessOutWrite( p_mux->p_access, p_og );
768 /* Write eos packets for each stream. */
769 for( i = 0; i < p_mux->i_nb_inputs; i++ )
771 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
773 /* skip newly added streams */
774 if( p_stream->b_new ) continue;
781 op.packetno = p_stream->i_packet_no++;
782 ogg_stream_packetin( &p_stream->os, &op );
784 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
785 block_ChainAppend( &p_hdr, p_og );
786 ogg_stream_clear( &p_stream->os );
789 for( i = 0; i < p_sys->i_del_streams; i++ )
796 op.packetno = p_sys->pp_del_streams[i]->i_packet_no++;
797 ogg_stream_packetin( &p_sys->pp_del_streams[i]->os, &op );
799 p_og = OggStreamFlush( p_mux, &p_sys->pp_del_streams[i]->os, 0 );
800 block_ChainAppend( &p_hdr, p_og );
801 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
807 static void OggSetDate( block_t *p_og, mtime_t i_dts, mtime_t i_length )
813 for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
817 i_delta = i_length / i_count;
819 for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
821 p_tmp->i_dts = i_dts;
822 p_tmp->i_length = i_delta;
828 /*****************************************************************************
829 * Mux: multiplex available data in input fifos into the Ogg bitstream
830 *****************************************************************************/
831 static int Mux( sout_mux_t *p_mux )
833 sout_mux_sys_t *p_sys = p_mux->p_sys;
834 block_t *p_og = NULL;
838 if( p_sys->i_add_streams || p_sys->i_del_streams )
840 /* Open new ogg stream */
841 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
843 msg_Dbg( p_mux, "waiting for data..." );
844 return( VLC_SUCCESS );
847 if( p_sys->i_streams )
849 /* Close current ogg stream */
852 msg_Dbg( p_mux, "writing footer" );
853 block_ChainAppend( &p_og, OggCreateFooter( p_mux, 0 ) );
855 /* Remove deleted logical streams */
856 for( i = 0; i < p_sys->i_del_streams; i++ )
858 FREE( p_sys->pp_del_streams[i] );
860 FREE( p_sys->pp_del_streams );
861 p_sys->i_streams = 0;
864 msg_Dbg( p_mux, "writing header" );
865 p_sys->i_start_dts = i_dts;
866 p_sys->i_streams = p_mux->i_nb_inputs;
867 p_sys->i_del_streams = 0;
868 p_sys->i_add_streams = 0;
869 block_ChainAppend( &p_og, OggCreateHeader( p_mux, i_dts ) );
871 /* Write header and/or footer */
872 OggSetDate( p_og, i_dts, 0 );
873 sout_AccessOutWrite( p_mux->p_access, p_og );
879 sout_input_t *p_input;
880 ogg_stream_t *p_stream;
884 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
886 return( VLC_SUCCESS );
889 p_input = p_mux->pp_inputs[i_stream];
890 p_stream = (ogg_stream_t*)p_input->p_sys;
891 p_data = block_FifoGet( p_input->p_fifo );
893 if( p_stream->i_fourcc != VLC_FOURCC( 'v', 'o', 'r', 'b' ) &&
894 p_stream->i_fourcc != VLC_FOURCC( 'f', 'l', 'a', 'c' ) &&
895 p_stream->i_fourcc != VLC_FOURCC( 's', 'p', 'x', ' ' ) &&
896 p_stream->i_fourcc != VLC_FOURCC( 't', 'h', 'e', 'o' ) )
898 p_data = block_Realloc( p_data, 1, 0 );
899 p_data->p_buffer[0] = PACKET_IS_SYNCPOINT; // FIXME
902 op.packet = p_data->p_buffer;
903 op.bytes = p_data->i_buffer;
906 op.packetno = p_stream->i_packet_no++;
908 if( p_stream->i_cat == AUDIO_ES )
910 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
911 p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) ||
912 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) )
914 /* number of sample from begining + current packet */
916 ( i_dts - p_sys->i_start_dts + p_data->i_length ) *
917 (mtime_t)p_input->p_fmt->audio.i_rate / I64C(1000000);
921 /* number of sample from begining */
922 op.granulepos = ( i_dts - p_sys->i_start_dts ) *
923 p_stream->oggds_header.i_samples_per_unit / I64C(1000000);
926 else if( p_stream->i_cat == VIDEO_ES )
928 if( p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
930 /* FIXME, we assume only keyframes and 25fps */
931 op.granulepos = ( ( i_dts - p_sys->i_start_dts ) * I64C(25)
932 / I64C(1000000) ) << p_stream->i_keyframe_granule_shift;
935 op.granulepos = ( i_dts - p_sys->i_start_dts ) * I64C(10) /
936 p_stream->oggds_header.i_time_unit;
938 else if( p_stream->i_cat == SPU_ES )
940 /* granulepos is in milisec */
941 op.granulepos = ( i_dts - p_sys->i_start_dts ) / 1000;
944 ogg_stream_packetin( &p_stream->os, &op );
946 if( p_stream->i_cat == SPU_ES ||
947 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) )
949 /* Subtitles or Speex packets are quite small so they
950 * need to be flushed to be sent on time */
951 block_ChainAppend( &p_og, OggStreamFlush( p_mux, &p_stream->os,
956 block_ChainAppend( &p_og, OggStreamPageOut( p_mux, &p_stream->os,
962 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
963 p_stream->i_dts = -1;
964 p_stream->i_length = 0;
966 sout_AccessOutWrite( p_mux->p_access, p_og );
972 if( p_stream->i_dts < 0 )
974 p_stream->i_dts = p_data->i_dts;
976 p_stream->i_length += p_data->i_length;
979 block_Release( p_data );