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 *, 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_capacity = Capability;
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 static int Capability( sout_mux_t *p_mux, int i_query, void *p_args,
296 case SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME:
297 *(vlc_bool_t*)p_answer = VLC_TRUE;
298 return( SOUT_MUX_CAP_ERR_OK );
299 case SOUT_MUX_CAP_GET_ADD_STREAM_WAIT:
300 *(vlc_bool_t*)p_answer = VLC_TRUE;
301 return( SOUT_MUX_CAP_ERR_OK );
303 return( SOUT_MUX_CAP_ERR_UNIMPLEMENTED );
307 /*****************************************************************************
308 * AddStream: Add an elementary stream to the muxed stream
309 *****************************************************************************/
310 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
312 sout_mux_sys_t *p_sys = p_mux->p_sys;
313 ogg_stream_t *p_stream;
315 msg_Dbg( p_mux, "adding input" );
317 p_input->p_sys = p_stream = malloc( sizeof( ogg_stream_t ) );
319 p_stream->i_cat = p_input->p_fmt->i_cat;
320 p_stream->i_fourcc = p_input->p_fmt->i_codec;
321 p_stream->i_serial_no = p_sys->i_next_serial_no++;
322 p_stream->i_packet_no = 0;
324 p_stream->i_sout_headers = 0;
326 memset( &p_stream->oggds_header, 0, sizeof(p_stream->oggds_header) );
327 p_stream->oggds_header.i_packet_type = PACKET_TYPE_HEADER;
328 switch( p_input->p_fmt->i_cat )
331 if( !p_input->p_fmt->video.i_frame_rate ||
332 !p_input->p_fmt->video.i_frame_rate_base )
334 msg_Warn( p_mux, "Missing frame rate, assuming 25fps" );
335 p_input->p_fmt->video.i_frame_rate = 25;
336 p_input->p_fmt->video.i_frame_rate_base = 1;
339 switch( p_stream->i_fourcc )
341 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
342 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
343 case VLC_FOURCC( 'D', 'I', 'V', '3' ):
344 case VLC_FOURCC( 'M', 'J', 'P', 'G' ):
345 case VLC_FOURCC( 'W', 'M', 'V', '1' ):
346 case VLC_FOURCC( 'W', 'M', 'V', '2' ):
347 memcpy( p_stream->oggds_header.stream_type, "video", 5 );
348 if( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p', '4', 'v' ) )
350 memcpy( p_stream->oggds_header.sub_type, "XVID", 4 );
352 else if( p_stream->i_fourcc == VLC_FOURCC( 'D', 'I', 'V', '3' ) )
354 memcpy( p_stream->oggds_header.sub_type, "DIV3", 4 );
358 memcpy(p_stream->oggds_header.sub_type,&p_stream->i_fourcc,4);
360 SetDWLE( &p_stream->oggds_header.i_size,
361 sizeof( oggds_header_t ) - 1);
362 SetQWLE( &p_stream->oggds_header.i_time_unit,
363 I64C(10000000) * p_input->p_fmt->video.i_frame_rate_base /
364 (int64_t)p_input->p_fmt->video.i_frame_rate );
365 SetQWLE( &p_stream->oggds_header.i_samples_per_unit, 1 );
366 SetDWLE( &p_stream->oggds_header.i_default_len, 1 ); /* ??? */
367 SetDWLE( &p_stream->oggds_header.i_buffer_size, 1024*1024 );
368 SetWLE( &p_stream->oggds_header.i_bits_per_sample, 0 );
369 SetDWLE( &p_stream->oggds_header.header.video.i_width,
370 p_input->p_fmt->video.i_width );
371 SetDWLE( &p_stream->oggds_header.header.video.i_height,
372 p_input->p_fmt->video.i_height );
373 msg_Dbg( p_mux, "%4.4s stream", (char *)&p_stream->i_fourcc );
376 case VLC_FOURCC( 't', 'h', 'e', 'o' ):
377 msg_Dbg( p_mux, "theora stream" );
381 FREE( p_input->p_sys );
387 switch( p_stream->i_fourcc )
389 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
390 case VLC_FOURCC( 'a', '5', '2', ' ' ):
391 memcpy( p_stream->oggds_header.stream_type, "audio", 5 );
392 if( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p', 'g', 'a' ) )
394 memcpy( p_stream->oggds_header.sub_type, "55 ", 4 );
396 else if( p_stream->i_fourcc == VLC_FOURCC( 'a', '5', '2', ' ' ) )
398 memcpy( p_stream->oggds_header.sub_type, "2000", 4 );
400 SetDWLE( &p_stream->oggds_header.i_size,
401 sizeof( oggds_header_t ) - 1);
402 SetQWLE( &p_stream->oggds_header.i_time_unit, 0 /* not used */ );
403 SetDWLE( &p_stream->oggds_header.i_default_len, 1 );
404 SetDWLE( &p_stream->oggds_header.i_buffer_size, 30*1024 );
405 SetQWLE( &p_stream->oggds_header.i_samples_per_unit,
406 p_input->p_fmt->audio.i_rate );
407 SetWLE( &p_stream->oggds_header.i_bits_per_sample, 0 );
408 SetDWLE( &p_stream->oggds_header.header.audio.i_channels,
409 p_input->p_fmt->audio.i_channels );
410 SetDWLE( &p_stream->oggds_header.header.audio.i_block_align,
411 p_input->p_fmt->audio.i_blockalign );
412 SetDWLE( &p_stream->oggds_header.header.audio.i_avgbytespersec, 0);
413 msg_Dbg( p_mux, "mpga/a52 stream" );
416 case VLC_FOURCC( 'v', 'o', 'r', 'b' ):
417 msg_Dbg( p_mux, "vorbis stream" );
420 case VLC_FOURCC( 's', 'p', 'x', ' ' ):
421 msg_Dbg( p_mux, "speex stream" );
424 case VLC_FOURCC( 'f', 'l', 'a', 'c' ):
425 msg_Dbg( p_mux, "flac stream" );
429 FREE( p_input->p_sys );
435 switch( p_stream->i_fourcc )
437 case VLC_FOURCC( 's', 'u','b', 't' ):
438 memcpy( p_stream->oggds_header.stream_type, "text", 4 );
439 msg_Dbg( p_mux, "subtitles stream" );
443 FREE( p_input->p_sys );
448 FREE( p_input->p_sys );
452 p_stream->b_new = VLC_TRUE;
454 p_sys->i_add_streams++;
459 /*****************************************************************************
460 * DelStream: Delete an elementary stream from the muxed stream
461 *****************************************************************************/
462 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
464 sout_mux_sys_t *p_sys = p_mux->p_sys;
465 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
468 msg_Dbg( p_mux, "removing input" );
470 /* flush all remaining data */
475 if( !p_stream->b_new &&
476 ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
478 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
479 sout_AccessOutWrite( p_mux->p_access, p_og );
482 for( i = 0; i < p_stream->i_sout_headers; i++ )
484 block_Release( p_stream->pp_sout_headers[i] );
485 p_stream->i_sout_headers = 0;
488 /* move input in delete queue */
489 if( !p_stream->b_new )
491 p_sys->pp_del_streams = realloc( p_sys->pp_del_streams,
492 (p_sys->i_del_streams + 1) *
493 sizeof(ogg_stream_t *) );
494 p_sys->pp_del_streams[p_sys->i_del_streams++] = p_stream;
498 /* Wasn't already added so get rid of it */
500 p_sys->i_add_streams--;
504 p_input->p_sys = NULL;
509 /*****************************************************************************
510 * Ogg bitstream manipulation routines
511 *****************************************************************************/
512 static block_t *OggStreamFlush( sout_mux_t *p_mux,
513 ogg_stream_state *p_os, mtime_t i_pts )
515 block_t *p_og, *p_og_first = NULL;
518 while( ogg_stream_flush( p_os, &og ) )
521 p_og = block_New( p_mux, og.header_len + og.body_len );
523 memcpy( p_og->p_buffer, og.header, og.header_len );
524 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
529 i_pts = 0; // write it only once
531 block_ChainAppend( &p_og_first, p_og );
537 static block_t *OggStreamPageOut( sout_mux_t *p_mux,
538 ogg_stream_state *p_os, mtime_t i_pts )
540 block_t *p_og, *p_og_first = NULL;
543 while( ogg_stream_pageout( p_os, &og ) )
546 p_og = block_New( p_mux, og.header_len + og.body_len );
548 memcpy( p_og->p_buffer, og.header, og.header_len );
549 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
554 i_pts = 0; // write them only once
556 block_ChainAppend( &p_og_first, p_og );
562 static block_t *OggCreateHeader( sout_mux_t *p_mux, mtime_t i_dts )
564 block_t *p_hdr = NULL;
569 /* Write header for each stream. All b_o_s (beginning of stream) packets
570 * must appear first in the ogg stream so we take care of them first. */
571 for( i = 0; i < p_mux->i_nb_inputs; i++ )
573 ogg_stream_t *p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
574 p_stream->b_new = VLC_FALSE;
576 msg_Dbg( p_mux, "creating header for %4.4s",
577 (char *)&p_stream->i_fourcc );
579 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
580 p_stream->i_packet_no = 0;
582 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
583 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) ||
584 p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
586 /* Special case, headers are already there in the
587 * incoming stream or we backed them up earlier */
589 /* first packet in order: vorbis/speex/theora info */
590 if( !p_stream->i_sout_headers )
592 p_og = block_FifoGet( p_mux->pp_inputs[i]->p_fifo );
593 op.packet = p_og->p_buffer;
594 op.bytes = p_og->i_buffer;
598 op.packetno = p_stream->i_packet_no++;
599 ogg_stream_packetin( &p_stream->os, &op );
600 p_stream->pp_sout_headers[0] =
601 OggStreamFlush( p_mux, &p_stream->os, 0 );
602 p_stream->i_sout_headers++;
604 p_og = block_Duplicate( p_stream->pp_sout_headers[0] );
606 /* Get keyframe_granule_shift for theora granulepos calculation */
607 if( p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
609 int i_keyframe_frequency_force =
610 1 << ((op.packet[40] << 6 >> 3) | (op.packet[41] >> 5));
612 /* granule_shift = i_log( frequency_force -1 ) */
613 p_stream->i_keyframe_granule_shift = 0;
614 i_keyframe_frequency_force--;
615 while( i_keyframe_frequency_force )
617 p_stream->i_keyframe_granule_shift++;
618 i_keyframe_frequency_force >>= 1;
622 else if( p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
624 /* flac stream marker (yeah, only that in the 1st packet) */
630 op.packetno = p_stream->i_packet_no++;
631 ogg_stream_packetin( &p_stream->os, &op );
632 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
637 op.packet = (uint8_t*)&p_stream->oggds_header;
638 op.bytes = sizeof( oggds_header_t );
642 op.packetno = p_stream->i_packet_no++;
643 ogg_stream_packetin( &p_stream->os, &op );
644 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
647 block_ChainAppend( &p_hdr, p_og );
650 /* Take care of the non b_o_s headers */
651 for( i = 0; i < p_mux->i_nb_inputs; i++ )
653 ogg_stream_t *p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
655 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
656 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) ||
657 p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
659 /* Special case, headers are already there in the incoming stream.
660 * We need to gather them an mark them as headers. */
662 for( j = 0; j < 2; j++ )
664 if( p_stream->i_sout_headers < j + 2 )
666 /* next packets in order: comments and codebooks */
667 p_og = block_FifoGet( p_mux->pp_inputs[i]->p_fifo );
668 op.packet = p_og->p_buffer;
669 op.bytes = p_og->i_buffer;
673 op.packetno = p_stream->i_packet_no++;
674 ogg_stream_packetin( &p_stream->os, &op );
675 p_stream->pp_sout_headers[j+1] =
676 OggStreamFlush( p_mux, &p_stream->os, 0 );
677 p_stream->i_sout_headers++;
680 p_og = block_Duplicate( p_stream->pp_sout_headers[j+1] );
681 block_ChainAppend( &p_hdr, p_og );
684 else if( p_stream->i_fourcc != VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
690 com[0] = PACKET_TYPE_COMMENT;
691 i_com = snprintf( &com[1], 128, VERSION" stream output" ) + 1;
697 op.packetno = p_stream->i_packet_no++;
698 ogg_stream_packetin( &p_stream->os, &op );
699 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
700 block_ChainAppend( &p_hdr, p_og );
703 /* Special case for mp4v and flac */
704 if( ( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p', '4', 'v' ) ||
705 p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) ) &&
706 p_mux->pp_inputs[i]->p_fmt->i_extra )
708 /* Send a packet with the VOL data for mp4v
709 * or STREAMINFO for flac */
710 msg_Dbg( p_mux, "writing extra data" );
711 op.bytes = p_mux->pp_inputs[i]->p_fmt->i_extra;
712 op.packet = p_mux->pp_inputs[i]->p_fmt->p_extra;
713 if( p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
715 /* Skip the flac stream marker */
722 op.packetno = p_stream->i_packet_no++;
723 ogg_stream_packetin( &p_stream->os, &op );
724 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
725 block_ChainAppend( &p_hdr, p_og );
729 /* set HEADER flag */
730 for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
732 p_og->i_flags |= BLOCK_FLAG_HEADER;
737 static block_t *OggCreateFooter( sout_mux_t *p_mux, mtime_t i_dts )
739 sout_mux_sys_t *p_sys = p_mux->p_sys;
740 block_t *p_hdr = NULL;
745 /* flush all remaining data */
746 for( i = 0; i < p_mux->i_nb_inputs; i++ )
748 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
750 /* skip newly added streams */
751 if( p_stream->b_new ) continue;
753 if( ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
755 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
756 sout_AccessOutWrite( p_mux->p_access, p_og );
760 /* Write eos packets for each stream. */
761 for( i = 0; i < p_mux->i_nb_inputs; i++ )
763 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
765 /* skip newly added streams */
766 if( p_stream->b_new ) continue;
773 op.packetno = p_stream->i_packet_no++;
774 ogg_stream_packetin( &p_stream->os, &op );
776 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
777 block_ChainAppend( &p_hdr, p_og );
778 ogg_stream_clear( &p_stream->os );
781 for( i = 0; i < p_sys->i_del_streams; i++ )
788 op.packetno = p_sys->pp_del_streams[i]->i_packet_no++;
789 ogg_stream_packetin( &p_sys->pp_del_streams[i]->os, &op );
791 p_og = OggStreamFlush( p_mux, &p_sys->pp_del_streams[i]->os, 0 );
792 block_ChainAppend( &p_hdr, p_og );
793 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
799 static void OggSetDate( block_t *p_og, mtime_t i_dts, mtime_t i_length )
805 for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
809 i_delta = i_length / i_count;
811 for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
813 p_tmp->i_dts = i_dts;
814 p_tmp->i_length = i_delta;
820 /*****************************************************************************
821 * Mux: multiplex available data in input fifos into the Ogg bitstream
822 *****************************************************************************/
823 static int Mux( sout_mux_t *p_mux )
825 sout_mux_sys_t *p_sys = p_mux->p_sys;
826 block_t *p_og = NULL;
830 if( p_sys->i_add_streams || p_sys->i_del_streams )
832 /* Open new ogg stream */
833 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
835 msg_Dbg( p_mux, "waiting for data..." );
839 if( p_sys->i_streams )
841 /* Close current ogg stream */
844 msg_Dbg( p_mux, "writing footer" );
845 block_ChainAppend( &p_og, OggCreateFooter( p_mux, 0 ) );
847 /* Remove deleted logical streams */
848 for( i = 0; i < p_sys->i_del_streams; i++ )
850 FREE( p_sys->pp_del_streams[i] );
852 FREE( p_sys->pp_del_streams );
853 p_sys->i_streams = 0;
856 msg_Dbg( p_mux, "writing header" );
857 p_sys->i_start_dts = i_dts;
858 p_sys->i_streams = p_mux->i_nb_inputs;
859 p_sys->i_del_streams = 0;
860 p_sys->i_add_streams = 0;
861 block_ChainAppend( &p_og, OggCreateHeader( p_mux, i_dts ) );
863 /* Write header and/or footer */
864 OggSetDate( p_og, i_dts, 0 );
865 sout_AccessOutWrite( p_mux->p_access, p_og );
871 sout_input_t *p_input;
872 ogg_stream_t *p_stream;
876 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
881 p_input = p_mux->pp_inputs[i_stream];
882 p_stream = (ogg_stream_t*)p_input->p_sys;
883 p_data = block_FifoGet( p_input->p_fifo );
885 if( p_stream->i_fourcc != VLC_FOURCC( 'v', 'o', 'r', 'b' ) &&
886 p_stream->i_fourcc != VLC_FOURCC( 'f', 'l', 'a', 'c' ) &&
887 p_stream->i_fourcc != VLC_FOURCC( 's', 'p', 'x', ' ' ) &&
888 p_stream->i_fourcc != VLC_FOURCC( 't', 'h', 'e', 'o' ) )
890 p_data = block_Realloc( p_data, 1, p_data->i_buffer );
891 p_data->p_buffer[0] = PACKET_IS_SYNCPOINT; // FIXME
894 op.packet = p_data->p_buffer;
895 op.bytes = p_data->i_buffer;
898 op.packetno = p_stream->i_packet_no++;
900 if( p_stream->i_cat == AUDIO_ES )
902 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
903 p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) ||
904 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) )
906 /* number of sample from begining + current packet */
908 ( i_dts - p_sys->i_start_dts + p_data->i_length ) *
909 (mtime_t)p_input->p_fmt->audio.i_rate / I64C(1000000);
913 /* number of sample from begining */
914 op.granulepos = ( i_dts - p_sys->i_start_dts ) *
915 p_stream->oggds_header.i_samples_per_unit / I64C(1000000);
918 else if( p_stream->i_cat == VIDEO_ES )
920 if( p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
922 /* FIXME, we assume only keyframes */
923 op.granulepos = ( ( i_dts - p_sys->i_start_dts ) *
924 p_input->p_fmt->video.i_frame_rate /
925 p_input->p_fmt->video.i_frame_rate_base /
926 I64C(1000000) ) << p_stream->i_keyframe_granule_shift;
929 op.granulepos = ( i_dts - p_sys->i_start_dts ) * I64C(10) /
930 p_stream->oggds_header.i_time_unit;
932 else if( p_stream->i_cat == SPU_ES )
934 /* granulepos is in milisec */
935 op.granulepos = ( i_dts - p_sys->i_start_dts ) / 1000;
938 ogg_stream_packetin( &p_stream->os, &op );
940 if( p_stream->i_cat == SPU_ES ||
941 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) )
943 /* Subtitles or Speex packets are quite small so they
944 * need to be flushed to be sent on time */
945 p_og = OggStreamFlush( p_mux, &p_stream->os, p_data->i_dts );
949 p_og = OggStreamPageOut( p_mux, &p_stream->os, p_data->i_dts );
954 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
955 p_stream->i_dts = -1;
956 p_stream->i_length = 0;
958 sout_AccessOutWrite( p_mux->p_access, p_og );
962 if( p_stream->i_dts < 0 )
964 p_stream->i_dts = p_data->i_dts;
966 p_stream->i_length += p_data->i_length;
969 block_Release( p_data );