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@videolan.org>
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 Control ( sout_mux_t *, int, va_list );
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 )
153 p_buf = block_FifoShow( p_fifo );
154 if( i_stream < 0 || p_buf->i_dts < i_dts )
156 i_dts = p_buf->i_dts;
165 *pi_stream = i_stream;
174 /*****************************************************************************
175 * Definitions of structures and functions used by this plugins
176 *****************************************************************************/
188 int i_keyframe_granule_shift; /* Theora only */
191 oggds_header_t *p_oggds_header;
195 struct sout_mux_sys_t
200 int i_next_serial_no;
202 /* number of logical streams pending to be added */
205 /* logical streams pending to be deleted */
207 ogg_stream_t **pp_del_streams;
210 static void OggSetDate( block_t *, mtime_t , mtime_t );
211 static block_t *OggStreamFlush( sout_mux_t *, ogg_stream_state *, mtime_t );
213 /*****************************************************************************
215 *****************************************************************************/
216 static int Open( vlc_object_t *p_this )
218 sout_mux_t *p_mux = (sout_mux_t*)p_this;
219 sout_mux_sys_t *p_sys;
221 msg_Info( p_mux, "Open" );
223 p_sys = malloc( sizeof( sout_mux_sys_t ) );
224 p_sys->i_streams = 0;
225 p_sys->i_add_streams = 0;
226 p_sys->i_del_streams = 0;
227 p_sys->pp_del_streams = 0;
229 p_mux->p_sys = p_sys;
230 p_mux->pf_control = Control;
231 p_mux->pf_addstream = AddStream;
232 p_mux->pf_delstream = DelStream;
235 /* First serial number is random.
236 * (Done like this because on win32 you need to seed the random number
237 * generator once per thread). */
238 srand( (unsigned int)time( NULL ) );
239 p_sys->i_next_serial_no = rand();
244 /*****************************************************************************
245 * Close: Finalize ogg bitstream and close muxer
246 *****************************************************************************/
247 static void Close( vlc_object_t * p_this )
249 sout_mux_t *p_mux = (sout_mux_t*)p_this;
250 sout_mux_sys_t *p_sys = p_mux->p_sys;
252 msg_Info( p_mux, "Close" );
254 if( p_sys->i_del_streams )
256 block_t *p_og = NULL;
260 /* Close the current ogg stream */
261 msg_Dbg( p_mux, "writing footer" );
262 block_ChainAppend( &p_og, OggCreateFooter( p_mux, 0 ) );
264 /* Remove deleted logical streams */
265 for( i = 0; i < p_sys->i_del_streams; i++ )
267 i_dts = p_sys->pp_del_streams[i]->i_dts;
268 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
269 FREE( p_sys->pp_del_streams[i]->p_oggds_header );
270 FREE( p_sys->pp_del_streams[i] );
272 FREE( p_sys->pp_del_streams );
273 p_sys->i_streams -= p_sys->i_del_streams;
276 OggSetDate( p_og, i_dts, 0 );
277 sout_AccessOutWrite( p_mux->p_access, p_og );
283 /*****************************************************************************
285 *****************************************************************************/
286 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
293 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
294 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
298 case MUX_GET_ADD_STREAM_WAIT:
299 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
304 ppsz = (char**)va_arg( args, char ** );
305 *ppsz = strdup( "application/ogg" );
312 /*****************************************************************************
313 * AddStream: Add an elementary stream to the muxed stream
314 *****************************************************************************/
315 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
317 sout_mux_sys_t *p_sys = p_mux->p_sys;
318 ogg_stream_t *p_stream;
321 msg_Dbg( p_mux, "adding input" );
323 p_input->p_sys = p_stream = malloc( sizeof( ogg_stream_t ) );
325 p_stream->i_cat = p_input->p_fmt->i_cat;
326 p_stream->i_fourcc = p_input->p_fmt->i_codec;
327 p_stream->i_serial_no = p_sys->i_next_serial_no++;
328 p_stream->i_packet_no = 0;
330 p_stream->p_oggds_header = 0;
332 switch( p_input->p_fmt->i_cat )
335 if( !p_input->p_fmt->video.i_frame_rate ||
336 !p_input->p_fmt->video.i_frame_rate_base )
338 msg_Warn( p_mux, "Missing frame rate, assuming 25fps" );
339 p_input->p_fmt->video.i_frame_rate = 25;
340 p_input->p_fmt->video.i_frame_rate_base = 1;
343 switch( p_stream->i_fourcc )
345 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
346 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
347 case VLC_FOURCC( 'D', 'I', 'V', '3' ):
348 case VLC_FOURCC( 'M', 'J', 'P', 'G' ):
349 case VLC_FOURCC( 'W', 'M', 'V', '1' ):
350 case VLC_FOURCC( 'W', 'M', 'V', '2' ):
351 case VLC_FOURCC( 'W', 'M', 'V', '3' ):
352 p_stream->p_oggds_header = malloc( sizeof(oggds_header_t) );
353 memset( p_stream->p_oggds_header, 0, sizeof(oggds_header_t) );
354 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
356 memcpy( p_stream->p_oggds_header->stream_type, "video", 5 );
357 if( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p', '4', 'v' ) )
359 memcpy( p_stream->p_oggds_header->sub_type, "XVID", 4 );
361 else if( p_stream->i_fourcc == VLC_FOURCC( 'D', 'I', 'V', '3' ) )
363 memcpy( p_stream->p_oggds_header->sub_type, "DIV3", 4 );
367 memcpy( p_stream->p_oggds_header->sub_type,
368 &p_stream->i_fourcc, 4 );
370 SetDWLE( &p_stream->p_oggds_header->i_size,
371 sizeof( oggds_header_t ) - 1 );
372 SetQWLE( &p_stream->p_oggds_header->i_time_unit,
373 I64C(10000000) * p_input->p_fmt->video.i_frame_rate_base /
374 (int64_t)p_input->p_fmt->video.i_frame_rate );
375 SetQWLE( &p_stream->p_oggds_header->i_samples_per_unit, 1 );
376 SetDWLE( &p_stream->p_oggds_header->i_default_len, 1 ); /* ??? */
377 SetDWLE( &p_stream->p_oggds_header->i_buffer_size, 1024*1024 );
378 SetWLE( &p_stream->p_oggds_header->i_bits_per_sample, 0 );
379 SetDWLE( &p_stream->p_oggds_header->header.video.i_width,
380 p_input->p_fmt->video.i_width );
381 SetDWLE( &p_stream->p_oggds_header->header.video.i_height,
382 p_input->p_fmt->video.i_height );
383 msg_Dbg( p_mux, "%4.4s stream", (char *)&p_stream->i_fourcc );
386 case VLC_FOURCC( 't', 'h', 'e', 'o' ):
387 msg_Dbg( p_mux, "theora stream" );
391 FREE( p_input->p_sys );
397 switch( p_stream->i_fourcc )
399 case VLC_FOURCC( 'v', 'o', 'r', 'b' ):
400 msg_Dbg( p_mux, "vorbis stream" );
403 case VLC_FOURCC( 's', 'p', 'x', ' ' ):
404 msg_Dbg( p_mux, "speex stream" );
407 case VLC_FOURCC( 'f', 'l', 'a', 'c' ):
408 msg_Dbg( p_mux, "flac stream" );
412 fourcc_to_wf_tag( p_stream->i_fourcc, &i_tag );
413 if( i_tag == WAVE_FORMAT_UNKNOWN )
415 FREE( p_input->p_sys );
419 p_stream->p_oggds_header =
420 malloc( sizeof(oggds_header_t) + p_input->p_fmt->i_extra );
421 memset( p_stream->p_oggds_header, 0, sizeof(oggds_header_t) );
422 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
424 SetDWLE( &p_stream->p_oggds_header->i_size,
425 sizeof( oggds_header_t ) - 1 + p_input->p_fmt->i_extra );
427 if( p_input->p_fmt->i_extra )
429 memcpy( &p_stream->p_oggds_header[1],
430 p_input->p_fmt->p_extra, p_input->p_fmt->i_extra );
433 memcpy( p_stream->p_oggds_header->stream_type, "audio", 5 );
435 memset( p_stream->p_oggds_header->sub_type, 0, 4 );
436 sprintf( p_stream->p_oggds_header->sub_type, "%-x", i_tag );
438 SetQWLE( &p_stream->p_oggds_header->i_time_unit, I64C(10000000) );
439 SetDWLE( &p_stream->p_oggds_header->i_default_len, 1 );
440 SetDWLE( &p_stream->p_oggds_header->i_buffer_size, 30*1024 );
441 SetQWLE( &p_stream->p_oggds_header->i_samples_per_unit,
442 p_input->p_fmt->audio.i_rate );
443 SetWLE( &p_stream->p_oggds_header->i_bits_per_sample,
444 p_input->p_fmt->audio.i_bitspersample );
445 SetDWLE( &p_stream->p_oggds_header->header.audio.i_channels,
446 p_input->p_fmt->audio.i_channels );
447 SetDWLE( &p_stream->p_oggds_header->header.audio.i_block_align,
448 p_input->p_fmt->audio.i_blockalign );
449 SetDWLE( &p_stream->p_oggds_header->header.audio.i_avgbytespersec,
450 p_input->p_fmt->i_bitrate / 8);
451 msg_Dbg( p_mux, "%4.4s stream", (char *)&p_stream->i_fourcc );
457 switch( p_stream->i_fourcc )
459 case VLC_FOURCC( 's', 'u','b', 't' ):
460 p_stream->p_oggds_header = malloc( sizeof(oggds_header_t) );
461 memset( p_stream->p_oggds_header, 0, sizeof(oggds_header_t) );
462 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
464 memcpy( p_stream->p_oggds_header->stream_type, "text", 4 );
465 msg_Dbg( p_mux, "subtitles stream" );
469 FREE( p_input->p_sys );
474 FREE( p_input->p_sys );
478 p_stream->b_new = VLC_TRUE;
480 p_sys->i_add_streams++;
485 /*****************************************************************************
486 * DelStream: Delete an elementary stream from the muxed stream
487 *****************************************************************************/
488 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
490 sout_mux_sys_t *p_sys = p_mux->p_sys;
491 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
494 msg_Dbg( p_mux, "removing input" );
496 /* flush all remaining data */
499 if( !p_stream->b_new &&
500 ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
502 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
503 sout_AccessOutWrite( p_mux->p_access, p_og );
506 /* move input in delete queue */
507 if( !p_stream->b_new )
509 p_sys->pp_del_streams = realloc( p_sys->pp_del_streams,
510 (p_sys->i_del_streams + 1) *
511 sizeof(ogg_stream_t *) );
512 p_sys->pp_del_streams[p_sys->i_del_streams++] = p_stream;
516 /* Wasn't already added so get rid of it */
517 FREE( p_stream->p_oggds_header );
519 p_sys->i_add_streams--;
523 p_input->p_sys = NULL;
528 /*****************************************************************************
529 * Ogg bitstream manipulation routines
530 *****************************************************************************/
531 static block_t *OggStreamFlush( sout_mux_t *p_mux,
532 ogg_stream_state *p_os, mtime_t i_pts )
534 block_t *p_og, *p_og_first = NULL;
537 while( ogg_stream_flush( p_os, &og ) )
540 p_og = block_New( p_mux, og.header_len + og.body_len );
542 memcpy( p_og->p_buffer, og.header, og.header_len );
543 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
548 i_pts = 0; // write it only once
550 block_ChainAppend( &p_og_first, p_og );
556 static block_t *OggStreamPageOut( sout_mux_t *p_mux,
557 ogg_stream_state *p_os, mtime_t i_pts )
559 block_t *p_og, *p_og_first = NULL;
562 while( ogg_stream_pageout( p_os, &og ) )
565 p_og = block_New( p_mux, og.header_len + og.body_len );
567 memcpy( p_og->p_buffer, og.header, og.header_len );
568 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
573 i_pts = 0; // write them only once
575 block_ChainAppend( &p_og_first, p_og );
581 static block_t *OggCreateHeader( sout_mux_t *p_mux, mtime_t i_dts )
583 block_t *p_hdr = NULL;
584 block_t *p_og = NULL;
589 /* Write header for each stream. All b_o_s (beginning of stream) packets
590 * must appear first in the ogg stream so we take care of them first. */
591 for( i = 0; i < p_mux->i_nb_inputs; i++ )
593 sout_input_t *p_input = p_mux->pp_inputs[i];
594 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
595 p_stream->b_new = VLC_FALSE;
597 msg_Dbg( p_mux, "creating header for %4.4s",
598 (char *)&p_stream->i_fourcc );
600 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
601 p_stream->i_packet_no = 0;
603 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
604 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) ||
605 p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
607 /* First packet in order: vorbis/speex/theora info */
608 p_extra = p_input->p_fmt->p_extra;
609 i_extra = p_input->p_fmt->i_extra;
611 op.bytes = *(p_extra++) << 8;
612 op.bytes |= (*(p_extra++) & 0xFF);
614 i_extra -= (op.bytes + 2);
617 msg_Err( p_mux, "header data corrupted");
624 op.packetno = p_stream->i_packet_no++;
625 ogg_stream_packetin( &p_stream->os, &op );
626 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
628 /* Get keyframe_granule_shift for theora granulepos calculation */
629 if( p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
631 int i_keyframe_frequency_force =
632 1 << ((op.packet[40] << 6 >> 3) | (op.packet[41] >> 5));
634 /* granule_shift = i_log( frequency_force -1 ) */
635 p_stream->i_keyframe_granule_shift = 0;
636 i_keyframe_frequency_force--;
637 while( i_keyframe_frequency_force )
639 p_stream->i_keyframe_granule_shift++;
640 i_keyframe_frequency_force >>= 1;
644 else if( p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
646 /* flac stream marker (yeah, only that in the 1st packet) */
652 op.packetno = p_stream->i_packet_no++;
653 ogg_stream_packetin( &p_stream->os, &op );
654 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
656 else if( p_stream->p_oggds_header )
659 op.packet = (uint8_t*)p_stream->p_oggds_header;
660 op.bytes = p_stream->p_oggds_header->i_size + 1;
664 op.packetno = p_stream->i_packet_no++;
665 ogg_stream_packetin( &p_stream->os, &op );
666 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
669 block_ChainAppend( &p_hdr, p_og );
672 /* Take care of the non b_o_s headers */
673 for( i = 0; i < p_mux->i_nb_inputs; i++ )
675 sout_input_t *p_input = p_mux->pp_inputs[i];
676 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
678 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
679 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) ||
680 p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
682 /* Special case, headers are already there in the incoming stream.
683 * We need to gather them an mark them as headers. */
686 if( p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) ) j = 1;
688 p_extra = p_input->p_fmt->p_extra;
689 i_extra = p_input->p_fmt->i_extra;
692 op.bytes = *(p_extra++) << 8;
693 op.bytes |= (*(p_extra++) & 0xFF);
696 i_extra -= (op.bytes + 2);
700 op.bytes = *(p_extra++) << 8;
701 op.bytes |= (*(p_extra++) & 0xFF);
704 i_extra -= (op.bytes + 2);
707 msg_Err( p_mux, "header data corrupted");
714 op.packetno = p_stream->i_packet_no++;
715 ogg_stream_packetin( &p_stream->os, &op );
717 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
718 block_ChainAppend( &p_hdr, p_og );
721 else if( p_stream->i_fourcc != VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
727 com[0] = PACKET_TYPE_COMMENT;
728 i_com = snprintf( &com[1], 128, PACKAGE_VERSION" stream output" )
735 op.packetno = p_stream->i_packet_no++;
736 ogg_stream_packetin( &p_stream->os, &op );
737 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
738 block_ChainAppend( &p_hdr, p_og );
741 /* Special case for mp4v and flac */
742 if( ( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p', '4', 'v' ) ||
743 p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) ) &&
744 p_input->p_fmt->i_extra )
746 /* Send a packet with the VOL data for mp4v
747 * or STREAMINFO for flac */
748 msg_Dbg( p_mux, "writing extra data" );
749 op.bytes = p_input->p_fmt->i_extra;
750 op.packet = p_input->p_fmt->p_extra;
751 if( p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
753 /* Skip the flac stream marker */
760 op.packetno = p_stream->i_packet_no++;
761 ogg_stream_packetin( &p_stream->os, &op );
762 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
763 block_ChainAppend( &p_hdr, p_og );
767 /* set HEADER flag */
768 for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
770 p_og->i_flags |= BLOCK_FLAG_HEADER;
775 static block_t *OggCreateFooter( sout_mux_t *p_mux, mtime_t i_dts )
777 sout_mux_sys_t *p_sys = p_mux->p_sys;
778 block_t *p_hdr = NULL;
783 /* flush all remaining data */
784 for( i = 0; i < p_mux->i_nb_inputs; i++ )
786 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
788 /* skip newly added streams */
789 if( p_stream->b_new ) continue;
791 if( ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
793 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
794 sout_AccessOutWrite( p_mux->p_access, p_og );
798 /* Write eos packets for each stream. */
799 for( i = 0; i < p_mux->i_nb_inputs; i++ )
801 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
803 /* skip newly added streams */
804 if( p_stream->b_new ) continue;
811 op.packetno = p_stream->i_packet_no++;
812 ogg_stream_packetin( &p_stream->os, &op );
814 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
815 block_ChainAppend( &p_hdr, p_og );
816 ogg_stream_clear( &p_stream->os );
819 for( i = 0; i < p_sys->i_del_streams; i++ )
826 op.packetno = p_sys->pp_del_streams[i]->i_packet_no++;
827 ogg_stream_packetin( &p_sys->pp_del_streams[i]->os, &op );
829 p_og = OggStreamFlush( p_mux, &p_sys->pp_del_streams[i]->os, 0 );
830 block_ChainAppend( &p_hdr, p_og );
831 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
837 static void OggSetDate( block_t *p_og, mtime_t i_dts, mtime_t i_length )
843 for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
847 i_delta = i_length / i_count;
849 for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
851 p_tmp->i_dts = i_dts;
852 p_tmp->i_length = i_delta;
858 /*****************************************************************************
859 * Mux: multiplex available data in input fifos into the Ogg bitstream
860 *****************************************************************************/
861 static int Mux( sout_mux_t *p_mux )
863 sout_mux_sys_t *p_sys = p_mux->p_sys;
864 block_t *p_og = NULL;
868 if( p_sys->i_add_streams || p_sys->i_del_streams )
870 /* Open new ogg stream */
871 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
873 msg_Dbg( p_mux, "waiting for data..." );
877 if( p_sys->i_streams )
879 /* Close current ogg stream */
882 msg_Dbg( p_mux, "writing footer" );
883 block_ChainAppend( &p_og, OggCreateFooter( p_mux, 0 ) );
885 /* Remove deleted logical streams */
886 for( i = 0; i < p_sys->i_del_streams; i++ )
888 FREE( p_sys->pp_del_streams[i]->p_oggds_header );
889 FREE( p_sys->pp_del_streams[i] );
891 FREE( p_sys->pp_del_streams );
892 p_sys->i_streams = 0;
895 msg_Dbg( p_mux, "writing header" );
896 p_sys->i_start_dts = i_dts;
897 p_sys->i_streams = p_mux->i_nb_inputs;
898 p_sys->i_del_streams = 0;
899 p_sys->i_add_streams = 0;
900 block_ChainAppend( &p_og, OggCreateHeader( p_mux, i_dts ) );
902 /* Write header and/or footer */
903 OggSetDate( p_og, i_dts, 0 );
904 sout_AccessOutWrite( p_mux->p_access, p_og );
910 sout_input_t *p_input;
911 ogg_stream_t *p_stream;
915 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
920 p_input = p_mux->pp_inputs[i_stream];
921 p_stream = (ogg_stream_t*)p_input->p_sys;
922 p_data = block_FifoGet( p_input->p_fifo );
924 if( p_stream->i_fourcc != VLC_FOURCC( 'v', 'o', 'r', 'b' ) &&
925 p_stream->i_fourcc != VLC_FOURCC( 'f', 'l', 'a', 'c' ) &&
926 p_stream->i_fourcc != VLC_FOURCC( 's', 'p', 'x', ' ' ) &&
927 p_stream->i_fourcc != VLC_FOURCC( 't', 'h', 'e', 'o' ) )
929 p_data = block_Realloc( p_data, 1, p_data->i_buffer );
930 p_data->p_buffer[0] = PACKET_IS_SYNCPOINT; // FIXME
933 op.packet = p_data->p_buffer;
934 op.bytes = p_data->i_buffer;
937 op.packetno = p_stream->i_packet_no++;
939 if( p_stream->i_cat == AUDIO_ES )
941 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
942 p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) ||
943 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) )
945 /* number of sample from begining + current packet */
947 ( i_dts - p_sys->i_start_dts + p_data->i_length ) *
948 (mtime_t)p_input->p_fmt->audio.i_rate / I64C(1000000);
950 else if( p_stream->p_oggds_header )
952 /* number of sample from begining */
953 op.granulepos = ( i_dts - p_sys->i_start_dts ) *
954 p_stream->p_oggds_header->i_samples_per_unit /
958 else if( p_stream->i_cat == VIDEO_ES )
960 if( p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
962 /* FIXME, we assume only keyframes */
963 op.granulepos = ( ( i_dts - p_sys->i_start_dts ) *
964 p_input->p_fmt->video.i_frame_rate /
965 p_input->p_fmt->video.i_frame_rate_base /
966 I64C(1000000) ) << p_stream->i_keyframe_granule_shift;
968 else if( p_stream->p_oggds_header )
969 op.granulepos = ( i_dts - p_sys->i_start_dts ) * I64C(10) /
970 p_stream->p_oggds_header->i_time_unit;
972 else if( p_stream->i_cat == SPU_ES )
974 /* granulepos is in milisec */
975 op.granulepos = ( i_dts - p_sys->i_start_dts ) / 1000;
978 ogg_stream_packetin( &p_stream->os, &op );
980 if( p_stream->i_cat == SPU_ES ||
981 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) )
983 /* Subtitles or Speex packets are quite small so they
984 * need to be flushed to be sent on time */
985 p_og = OggStreamFlush( p_mux, &p_stream->os, p_data->i_dts );
989 p_og = OggStreamPageOut( p_mux, &p_stream->os, p_data->i_dts );
994 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
995 p_stream->i_dts = -1;
996 p_stream->i_length = 0;
998 sout_AccessOutWrite( p_mux->p_access, p_og );
1002 if( p_stream->i_dts < 0 )
1004 p_stream->i_dts = p_data->i_dts;
1006 p_stream->i_length += p_data->i_length;
1009 block_Release( p_data );