1 /*****************************************************************************
2 * ogg.c: ogg muxer module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001, 2002, 2006 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
35 #include <vlc_block.h>
36 #include <vlc_codecs.h>
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Open ( vlc_object_t * );
44 static void Close ( vlc_object_t * );
47 set_description( _("Ogg/OGM muxer") );
48 set_capability( "sout mux", 10 );
49 set_category( CAT_SOUT );
50 set_subcategory( SUBCAT_SOUT_MUX );
51 add_shortcut( "ogg" );
52 add_shortcut( "ogm" );
53 set_callbacks( Open, Close );
57 /*****************************************************************************
59 *****************************************************************************/
60 static int Control ( sout_mux_t *, int, va_list );
61 static int AddStream( sout_mux_t *, sout_input_t * );
62 static int DelStream( sout_mux_t *, sout_input_t * );
63 static int Mux ( sout_mux_t * );
64 static int MuxBlock ( sout_mux_t *, sout_input_t * );
66 static block_t *OggCreateHeader( sout_mux_t *, mtime_t );
67 static block_t *OggCreateFooter( sout_mux_t *, mtime_t );
69 /*****************************************************************************
71 *****************************************************************************/
73 /* Structures used for OggDS headers used in ogm files */
75 #define PACKET_TYPE_HEADER 0x01
76 #define PACKET_TYPE_COMMENT 0x03
77 #define PACKET_IS_SYNCPOINT 0x08
80 #ifdef HAVE_ATTRIBUTE_PACKED
81 __attribute__((__packed__))
86 } oggds_header_video_t;
89 #ifdef HAVE_ATTRIBUTE_PACKED
90 __attribute__((__packed__))
94 int16_t i_block_align;
95 int32_t i_avgbytespersec;
96 } oggds_header_audio_t;
99 #ifdef HAVE_ATTRIBUTE_PACKED
100 __attribute__((__packed__))
103 uint8_t i_packet_type;
111 int64_t i_samples_per_unit;
112 int32_t i_default_len;
114 int32_t i_buffer_size;
115 int16_t i_bits_per_sample;
117 int16_t i_padding_0; /* Because the original is using MSVC packing style */
121 oggds_header_video_t video;
122 oggds_header_audio_t audio;
125 int32_t i_padding_1; /* Because the original is using MSVC packing style */
130 * TODO move this function to src/stream_output.c (used by nearly all muxers)
132 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
138 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
140 block_fifo_t *p_fifo;
142 p_fifo = p_mux->pp_inputs[i]->p_fifo;
144 /* We don't really need to have anything in the SPU fifo */
145 if( p_mux->pp_inputs[i]->p_fmt->i_cat == SPU_ES &&
146 block_FifoCount( p_fifo ) == 0 ) continue;
148 if( block_FifoCount( p_fifo ) )
152 p_buf = block_FifoShow( p_fifo );
153 if( i_stream < 0 || p_buf->i_dts < i_dts )
155 i_dts = p_buf->i_dts;
162 if( pi_stream ) *pi_stream = i_stream;
163 if( pi_dts ) *pi_dts = i_dts;
167 /*****************************************************************************
168 * Definitions of structures and functions used by this plugins
169 *****************************************************************************/
181 int i_keyframe_granule_shift; /* Theora only */
184 oggds_header_t *p_oggds_header;
188 struct sout_mux_sys_t
193 int i_next_serial_no;
195 /* number of logical streams pending to be added */
198 /* logical streams pending to be deleted */
200 ogg_stream_t **pp_del_streams;
203 static void OggSetDate( block_t *, mtime_t , mtime_t );
204 static block_t *OggStreamFlush( sout_mux_t *, ogg_stream_state *, mtime_t );
206 /*****************************************************************************
208 *****************************************************************************/
209 static int Open( vlc_object_t *p_this )
211 sout_mux_t *p_mux = (sout_mux_t*)p_this;
212 sout_mux_sys_t *p_sys;
214 msg_Info( p_mux, "Open" );
216 p_sys = malloc( sizeof( sout_mux_sys_t ) );
217 p_sys->i_streams = 0;
218 p_sys->i_add_streams = 0;
219 p_sys->i_del_streams = 0;
220 p_sys->pp_del_streams = 0;
222 p_mux->p_sys = p_sys;
223 p_mux->pf_control = Control;
224 p_mux->pf_addstream = AddStream;
225 p_mux->pf_delstream = DelStream;
228 /* First serial number is random.
229 * (Done like this because on win32 you need to seed the random number
230 * generator once per thread). */
231 srand( (unsigned int)time( NULL ) );
232 p_sys->i_next_serial_no = rand();
237 /*****************************************************************************
238 * Close: Finalize ogg bitstream and close muxer
239 *****************************************************************************/
240 static void Close( vlc_object_t * p_this )
242 sout_mux_t *p_mux = (sout_mux_t*)p_this;
243 sout_mux_sys_t *p_sys = p_mux->p_sys;
245 msg_Info( p_mux, "Close" );
247 if( p_sys->i_del_streams )
249 block_t *p_og = NULL;
253 /* Close the current ogg stream */
254 msg_Dbg( p_mux, "writing footer" );
255 block_ChainAppend( &p_og, OggCreateFooter( p_mux, 0 ) );
257 /* Remove deleted logical streams */
258 for( i = 0; i < p_sys->i_del_streams; i++ )
260 i_dts = p_sys->pp_del_streams[i]->i_dts;
261 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
262 FREENULL( p_sys->pp_del_streams[i]->p_oggds_header );
263 FREENULL( p_sys->pp_del_streams[i] );
265 FREENULL( p_sys->pp_del_streams );
266 p_sys->i_streams -= p_sys->i_del_streams;
269 OggSetDate( p_og, i_dts, 0 );
270 sout_AccessOutWrite( p_mux->p_access, p_og );
276 /*****************************************************************************
278 *****************************************************************************/
279 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
286 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
287 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
291 case MUX_GET_ADD_STREAM_WAIT:
292 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
297 ppsz = (char**)va_arg( args, char ** );
298 *ppsz = strdup( "application/ogg" );
305 /*****************************************************************************
306 * AddStream: Add an elementary stream to the muxed stream
307 *****************************************************************************/
308 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
310 sout_mux_sys_t *p_sys = p_mux->p_sys;
311 ogg_stream_t *p_stream;
314 msg_Dbg( p_mux, "adding input" );
316 p_input->p_sys = p_stream = malloc( sizeof( ogg_stream_t ) );
318 p_stream->i_cat = p_input->p_fmt->i_cat;
319 p_stream->i_fourcc = p_input->p_fmt->i_codec;
320 p_stream->i_serial_no = p_sys->i_next_serial_no++;
321 p_stream->i_packet_no = 0;
323 p_stream->p_oggds_header = 0;
325 switch( p_input->p_fmt->i_cat )
328 if( !p_input->p_fmt->video.i_frame_rate ||
329 !p_input->p_fmt->video.i_frame_rate_base )
331 msg_Warn( p_mux, "Missing frame rate, assuming 25fps" );
332 p_input->p_fmt->video.i_frame_rate = 25;
333 p_input->p_fmt->video.i_frame_rate_base = 1;
336 switch( p_stream->i_fourcc )
338 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
339 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
340 case VLC_FOURCC( 'D', 'I', 'V', '3' ):
341 case VLC_FOURCC( 'M', 'J', 'P', 'G' ):
342 case VLC_FOURCC( 'W', 'M', 'V', '1' ):
343 case VLC_FOURCC( 'W', 'M', 'V', '2' ):
344 case VLC_FOURCC( 'W', 'M', 'V', '3' ):
345 case VLC_FOURCC( 'S', 'N', 'O', 'W' ):
346 case VLC_FOURCC( 'd', 'r', 'a', 'c' ):
347 p_stream->p_oggds_header = malloc( sizeof(oggds_header_t) );
348 memset( p_stream->p_oggds_header, 0, sizeof(oggds_header_t) );
349 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
351 memcpy( p_stream->p_oggds_header->stream_type, "video", 5 );
352 if( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p', '4', 'v' ) )
354 memcpy( p_stream->p_oggds_header->sub_type, "XVID", 4 );
356 else if( p_stream->i_fourcc == VLC_FOURCC( 'D', 'I', 'V', '3' ) )
358 memcpy( p_stream->p_oggds_header->sub_type, "DIV3", 4 );
362 memcpy( p_stream->p_oggds_header->sub_type,
363 &p_stream->i_fourcc, 4 );
365 SetDWLE( &p_stream->p_oggds_header->i_size,
366 sizeof( oggds_header_t ) - 1 );
367 SetQWLE( &p_stream->p_oggds_header->i_time_unit,
368 I64C(10000000) * p_input->p_fmt->video.i_frame_rate_base /
369 (int64_t)p_input->p_fmt->video.i_frame_rate );
370 SetQWLE( &p_stream->p_oggds_header->i_samples_per_unit, 1 );
371 SetDWLE( &p_stream->p_oggds_header->i_default_len, 1 ); /* ??? */
372 SetDWLE( &p_stream->p_oggds_header->i_buffer_size, 1024*1024 );
373 SetWLE( &p_stream->p_oggds_header->i_bits_per_sample, 0 );
374 SetDWLE( &p_stream->p_oggds_header->header.video.i_width,
375 p_input->p_fmt->video.i_width );
376 SetDWLE( &p_stream->p_oggds_header->header.video.i_height,
377 p_input->p_fmt->video.i_height );
378 msg_Dbg( p_mux, "%4.4s stream", (char *)&p_stream->i_fourcc );
381 case VLC_FOURCC( 't', 'h', 'e', 'o' ):
382 msg_Dbg( p_mux, "theora stream" );
386 FREENULL( p_input->p_sys );
392 switch( p_stream->i_fourcc )
394 case VLC_FOURCC( 'v', 'o', 'r', 'b' ):
395 msg_Dbg( p_mux, "vorbis stream" );
398 case VLC_FOURCC( 's', 'p', 'x', ' ' ):
399 msg_Dbg( p_mux, "speex stream" );
402 case VLC_FOURCC( 'f', 'l', 'a', 'c' ):
403 msg_Dbg( p_mux, "flac stream" );
407 fourcc_to_wf_tag( p_stream->i_fourcc, &i_tag );
408 if( i_tag == WAVE_FORMAT_UNKNOWN )
410 FREENULL( p_input->p_sys );
414 p_stream->p_oggds_header =
415 malloc( sizeof(oggds_header_t) + p_input->p_fmt->i_extra );
416 memset( p_stream->p_oggds_header, 0, sizeof(oggds_header_t) );
417 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
419 SetDWLE( &p_stream->p_oggds_header->i_size,
420 sizeof( oggds_header_t ) - 1 + p_input->p_fmt->i_extra );
422 if( p_input->p_fmt->i_extra )
424 memcpy( &p_stream->p_oggds_header[1],
425 p_input->p_fmt->p_extra, p_input->p_fmt->i_extra );
428 memcpy( p_stream->p_oggds_header->stream_type, "audio", 5 );
430 memset( p_stream->p_oggds_header->sub_type, 0, 4 );
431 sprintf( p_stream->p_oggds_header->sub_type, "%-x", i_tag );
433 SetQWLE( &p_stream->p_oggds_header->i_time_unit, I64C(10000000) );
434 SetDWLE( &p_stream->p_oggds_header->i_default_len, 1 );
435 SetDWLE( &p_stream->p_oggds_header->i_buffer_size, 30*1024 );
436 SetQWLE( &p_stream->p_oggds_header->i_samples_per_unit,
437 p_input->p_fmt->audio.i_rate );
438 SetWLE( &p_stream->p_oggds_header->i_bits_per_sample,
439 p_input->p_fmt->audio.i_bitspersample );
440 SetDWLE( &p_stream->p_oggds_header->header.audio.i_channels,
441 p_input->p_fmt->audio.i_channels );
442 SetDWLE( &p_stream->p_oggds_header->header.audio.i_block_align,
443 p_input->p_fmt->audio.i_blockalign );
444 SetDWLE( &p_stream->p_oggds_header->header.audio.i_avgbytespersec,
445 p_input->p_fmt->i_bitrate / 8);
446 msg_Dbg( p_mux, "%4.4s stream", (char *)&p_stream->i_fourcc );
452 switch( p_stream->i_fourcc )
454 case VLC_FOURCC( 's', 'u','b', 't' ):
455 p_stream->p_oggds_header = malloc( sizeof(oggds_header_t) );
456 memset( p_stream->p_oggds_header, 0, sizeof(oggds_header_t) );
457 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
459 memcpy( p_stream->p_oggds_header->stream_type, "text", 4 );
460 msg_Dbg( p_mux, "subtitles stream" );
464 FREENULL( p_input->p_sys );
469 FREENULL( p_input->p_sys );
473 p_stream->b_new = VLC_TRUE;
475 p_sys->i_add_streams++;
480 /*****************************************************************************
481 * DelStream: Delete an elementary stream from the muxed stream
482 *****************************************************************************/
483 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
485 sout_mux_sys_t *p_sys = p_mux->p_sys;
486 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
489 msg_Dbg( p_mux, "removing input" );
491 /* flush all remaining data */
494 if( !p_stream->b_new )
496 while( block_FifoCount( p_input->p_fifo ) )
497 MuxBlock( p_mux, p_input );
500 if( !p_stream->b_new &&
501 ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
503 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
504 sout_AccessOutWrite( p_mux->p_access, p_og );
507 /* move input in delete queue */
508 if( !p_stream->b_new )
510 p_sys->pp_del_streams = realloc( p_sys->pp_del_streams,
511 (p_sys->i_del_streams + 1) *
512 sizeof(ogg_stream_t *) );
513 p_sys->pp_del_streams[p_sys->i_del_streams++] = p_stream;
517 /* wasn't already added so get rid of it */
518 FREENULL( p_stream->p_oggds_header );
519 FREENULL( p_stream );
520 p_sys->i_add_streams--;
524 p_input->p_sys = NULL;
529 /*****************************************************************************
530 * Ogg bitstream manipulation routines
531 *****************************************************************************/
532 static block_t *OggStreamFlush( sout_mux_t *p_mux,
533 ogg_stream_state *p_os, mtime_t i_pts )
535 block_t *p_og, *p_og_first = NULL;
538 while( ogg_stream_flush( p_os, &og ) )
541 p_og = block_New( p_mux, og.header_len + og.body_len );
543 memcpy( p_og->p_buffer, og.header, og.header_len );
544 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
549 i_pts = 0; // write it only once
551 block_ChainAppend( &p_og_first, p_og );
557 static block_t *OggStreamPageOut( sout_mux_t *p_mux,
558 ogg_stream_state *p_os, mtime_t i_pts )
560 block_t *p_og, *p_og_first = NULL;
563 while( ogg_stream_pageout( p_os, &og ) )
566 p_og = block_New( p_mux, og.header_len + og.body_len );
568 memcpy( p_og->p_buffer, og.header, og.header_len );
569 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
574 i_pts = 0; // write them only once
576 block_ChainAppend( &p_og_first, p_og );
582 static block_t *OggCreateHeader( sout_mux_t *p_mux, mtime_t i_dts )
584 block_t *p_hdr = NULL;
585 block_t *p_og = NULL;
590 /* Write header for each stream. All b_o_s (beginning of stream) packets
591 * must appear first in the ogg stream so we take care of them first. */
592 for( i = 0; i < p_mux->i_nb_inputs; i++ )
594 sout_input_t *p_input = p_mux->pp_inputs[i];
595 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
596 p_stream->b_new = VLC_FALSE;
598 msg_Dbg( p_mux, "creating header for %4.4s",
599 (char *)&p_stream->i_fourcc );
601 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
602 p_stream->i_packet_no = 0;
604 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
605 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) ||
606 p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
608 /* First packet in order: vorbis/speex/theora info */
609 p_extra = p_input->p_fmt->p_extra;
610 i_extra = p_input->p_fmt->i_extra;
612 op.bytes = *(p_extra++) << 8;
613 op.bytes |= (*(p_extra++) & 0xFF);
615 i_extra -= (op.bytes + 2);
618 msg_Err( p_mux, "header data corrupted");
625 op.packetno = p_stream->i_packet_no++;
626 ogg_stream_packetin( &p_stream->os, &op );
627 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
629 /* Get keyframe_granule_shift for theora granulepos calculation */
630 if( p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
632 int i_keyframe_frequency_force =
633 1 << ((op.packet[40] << 6 >> 3) | (op.packet[41] >> 5));
635 /* granule_shift = i_log( frequency_force -1 ) */
636 p_stream->i_keyframe_granule_shift = 0;
637 i_keyframe_frequency_force--;
638 while( i_keyframe_frequency_force )
640 p_stream->i_keyframe_granule_shift++;
641 i_keyframe_frequency_force >>= 1;
645 else if( p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
647 /* flac stream marker (yeah, only that in the 1st packet) */
648 op.packet = (unsigned char *)"fLaC";
653 op.packetno = p_stream->i_packet_no++;
654 ogg_stream_packetin( &p_stream->os, &op );
655 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
657 else if( p_stream->p_oggds_header )
660 op.packet = (uint8_t*)p_stream->p_oggds_header;
661 op.bytes = p_stream->p_oggds_header->i_size + 1;
665 op.packetno = p_stream->i_packet_no++;
666 ogg_stream_packetin( &p_stream->os, &op );
667 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
670 block_ChainAppend( &p_hdr, p_og );
673 /* Take care of the non b_o_s headers */
674 for( i = 0; i < p_mux->i_nb_inputs; i++ )
676 sout_input_t *p_input = p_mux->pp_inputs[i];
677 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
679 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
680 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) ||
681 p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
683 /* Special case, headers are already there in the incoming stream.
684 * We need to gather them an mark them as headers. */
687 if( p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) ) j = 1;
689 p_extra = p_input->p_fmt->p_extra;
690 i_extra = p_input->p_fmt->i_extra;
693 op.bytes = *(p_extra++) << 8;
694 op.bytes |= (*(p_extra++) & 0xFF);
697 i_extra -= (op.bytes + 2);
701 op.bytes = *(p_extra++) << 8;
702 op.bytes |= (*(p_extra++) & 0xFF);
705 i_extra -= (op.bytes + 2);
708 msg_Err( p_mux, "header data corrupted");
715 op.packetno = p_stream->i_packet_no++;
716 ogg_stream_packetin( &p_stream->os, &op );
718 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
719 block_ChainAppend( &p_hdr, p_og );
722 else if( p_stream->i_fourcc != VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
728 com[0] = PACKET_TYPE_COMMENT;
729 i_com = snprintf( (char *)(com+1), 127,
730 PACKAGE_VERSION" stream output" )
737 op.packetno = p_stream->i_packet_no++;
738 ogg_stream_packetin( &p_stream->os, &op );
739 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
740 block_ChainAppend( &p_hdr, p_og );
743 /* Special case for mp4v and flac */
744 if( ( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p', '4', 'v' ) ||
745 p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) ) &&
746 p_input->p_fmt->i_extra )
748 /* Send a packet with the VOL data for mp4v
749 * or STREAMINFO for flac */
750 msg_Dbg( p_mux, "writing extra data" );
751 op.bytes = p_input->p_fmt->i_extra;
752 op.packet = p_input->p_fmt->p_extra;
753 if( p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
755 /* Skip the flac stream marker */
762 op.packetno = p_stream->i_packet_no++;
763 ogg_stream_packetin( &p_stream->os, &op );
764 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
765 block_ChainAppend( &p_hdr, p_og );
769 /* set HEADER flag */
770 for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
772 p_og->i_flags |= BLOCK_FLAG_HEADER;
777 static block_t *OggCreateFooter( sout_mux_t *p_mux, mtime_t i_dts )
779 sout_mux_sys_t *p_sys = p_mux->p_sys;
780 block_t *p_hdr = NULL;
785 /* flush all remaining data */
786 for( i = 0; i < p_mux->i_nb_inputs; i++ )
788 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
790 /* skip newly added streams */
791 if( p_stream->b_new ) continue;
793 if( ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
795 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
796 sout_AccessOutWrite( p_mux->p_access, p_og );
800 /* Write eos packets for each stream. */
801 for( i = 0; i < p_mux->i_nb_inputs; i++ )
803 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
805 /* skip newly added streams */
806 if( p_stream->b_new ) continue;
813 op.packetno = p_stream->i_packet_no++;
814 ogg_stream_packetin( &p_stream->os, &op );
816 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
817 block_ChainAppend( &p_hdr, p_og );
818 ogg_stream_clear( &p_stream->os );
821 for( i = 0; i < p_sys->i_del_streams; i++ )
828 op.packetno = p_sys->pp_del_streams[i]->i_packet_no++;
829 ogg_stream_packetin( &p_sys->pp_del_streams[i]->os, &op );
831 p_og = OggStreamFlush( p_mux, &p_sys->pp_del_streams[i]->os, 0 );
832 block_ChainAppend( &p_hdr, p_og );
833 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
839 static void OggSetDate( block_t *p_og, mtime_t i_dts, mtime_t i_length )
845 for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
849 i_delta = i_length / i_count;
851 for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
853 p_tmp->i_dts = i_dts;
854 p_tmp->i_length = i_delta;
860 /*****************************************************************************
861 * Mux: multiplex available data in input fifos into the Ogg bitstream
862 *****************************************************************************/
863 static int Mux( sout_mux_t *p_mux )
865 sout_mux_sys_t *p_sys = p_mux->p_sys;
866 block_t *p_og = NULL;
870 if( p_sys->i_add_streams || p_sys->i_del_streams )
872 /* Open new ogg stream */
873 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
875 msg_Dbg( p_mux, "waiting for data..." );
879 if( p_sys->i_streams )
881 /* Close current ogg stream */
884 msg_Dbg( p_mux, "writing footer" );
885 block_ChainAppend( &p_og, OggCreateFooter( p_mux, 0 ) );
887 /* Remove deleted logical streams */
888 for( i = 0; i < p_sys->i_del_streams; i++ )
890 FREENULL( p_sys->pp_del_streams[i]->p_oggds_header );
891 FREENULL( p_sys->pp_del_streams[i] );
893 FREENULL( p_sys->pp_del_streams );
894 p_sys->i_streams = 0;
897 msg_Dbg( p_mux, "writing header" );
898 p_sys->i_start_dts = i_dts;
899 p_sys->i_streams = p_mux->i_nb_inputs;
900 p_sys->i_del_streams = 0;
901 p_sys->i_add_streams = 0;
902 block_ChainAppend( &p_og, OggCreateHeader( p_mux, i_dts ) );
904 /* Write header and/or footer */
905 OggSetDate( p_og, i_dts, 0 );
906 sout_AccessOutWrite( p_mux->p_access, p_og );
912 if( MuxGetStream( p_mux, &i_stream, 0 ) < 0 ) return VLC_SUCCESS;
913 MuxBlock( p_mux, p_mux->pp_inputs[i_stream] );
919 static int MuxBlock( sout_mux_t *p_mux, sout_input_t *p_input )
921 sout_mux_sys_t *p_sys = p_mux->p_sys;
922 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
923 block_t *p_data = block_FifoGet( p_input->p_fifo );
924 block_t *p_og = NULL;
927 if( p_stream->i_fourcc != VLC_FOURCC( 'v', 'o', 'r', 'b' ) &&
928 p_stream->i_fourcc != VLC_FOURCC( 'f', 'l', 'a', 'c' ) &&
929 p_stream->i_fourcc != VLC_FOURCC( 's', 'p', 'x', ' ' ) &&
930 p_stream->i_fourcc != VLC_FOURCC( 't', 'h', 'e', 'o' ) )
932 p_data = block_Realloc( p_data, 1, p_data->i_buffer );
933 p_data->p_buffer[0] = PACKET_IS_SYNCPOINT; // FIXME
936 op.packet = p_data->p_buffer;
937 op.bytes = p_data->i_buffer;
940 op.packetno = p_stream->i_packet_no++;
942 if( p_stream->i_cat == AUDIO_ES )
944 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
945 p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) ||
946 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) )
948 /* number of sample from begining + current packet */
950 ( p_data->i_dts - p_sys->i_start_dts + p_data->i_length ) *
951 (mtime_t)p_input->p_fmt->audio.i_rate / I64C(1000000);
953 else if( p_stream->p_oggds_header )
955 /* number of sample from begining */
956 op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) *
957 p_stream->p_oggds_header->i_samples_per_unit / I64C(1000000);
960 else if( p_stream->i_cat == VIDEO_ES )
962 if( p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
964 /* FIXME, we assume only keyframes */
965 op.granulepos = ( ( p_data->i_dts - p_sys->i_start_dts ) *
966 p_input->p_fmt->video.i_frame_rate /
967 p_input->p_fmt->video.i_frame_rate_base /
968 I64C(1000000) ) << p_stream->i_keyframe_granule_shift;
970 else if( p_stream->p_oggds_header )
971 op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) * I64C(10) /
972 p_stream->p_oggds_header->i_time_unit;
974 else if( p_stream->i_cat == SPU_ES )
976 /* granulepos is in milisec */
977 op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) / 1000;
980 ogg_stream_packetin( &p_stream->os, &op );
982 if( p_stream->i_cat == SPU_ES ||
983 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) )
985 /* Subtitles or Speex packets are quite small so they
986 * need to be flushed to be sent on time */
987 p_og = OggStreamFlush( p_mux, &p_stream->os, p_data->i_dts );
991 p_og = OggStreamPageOut( p_mux, &p_stream->os, p_data->i_dts );
996 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
997 p_stream->i_dts = -1;
998 p_stream->i_length = 0;
1000 sout_AccessOutWrite( p_mux->p_access, p_og );
1004 if( p_stream->i_dts < 0 )
1006 p_stream->i_dts = p_data->i_dts;
1008 p_stream->i_length += p_data->i_length;
1011 block_Release( p_data );