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 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
36 #include <vlc_block.h>
37 #include <vlc_codecs.h>
38 #include "../demux/xiph.h"
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Open ( vlc_object_t * );
46 static void Close ( vlc_object_t * );
49 set_description( N_("Ogg/OGM muxer") )
50 set_capability( "sout mux", 10 )
51 set_category( CAT_SOUT )
52 set_subcategory( SUBCAT_SOUT_MUX )
55 set_callbacks( Open, Close )
59 /*****************************************************************************
61 *****************************************************************************/
62 static int Control ( sout_mux_t *, int, va_list );
63 static int AddStream( sout_mux_t *, sout_input_t * );
64 static int DelStream( sout_mux_t *, sout_input_t * );
65 static int Mux ( sout_mux_t * );
66 static int MuxBlock ( sout_mux_t *, sout_input_t * );
68 static block_t *OggCreateHeader( sout_mux_t * );
69 static block_t *OggCreateFooter( sout_mux_t * );
71 /*****************************************************************************
73 *****************************************************************************/
75 /* Structures used for OggDS headers used in ogm files */
77 #define PACKET_TYPE_HEADER 0x01
78 #define PACKET_TYPE_COMMENT 0x03
79 #define PACKET_IS_SYNCPOINT 0x08
82 #ifdef HAVE_ATTRIBUTE_PACKED
83 __attribute__((__packed__))
88 } oggds_header_video_t;
91 #ifdef HAVE_ATTRIBUTE_PACKED
92 __attribute__((__packed__))
96 int16_t i_block_align;
97 int32_t i_avgbytespersec;
98 } oggds_header_audio_t;
101 #ifdef HAVE_ATTRIBUTE_PACKED
102 __attribute__((__packed__))
105 uint8_t i_packet_type;
113 int64_t i_samples_per_unit;
114 int32_t i_default_len;
116 int32_t i_buffer_size;
117 int16_t i_bits_per_sample;
119 int16_t i_padding_0; /* Because the original is using MSVC packing style */
123 oggds_header_video_t video;
124 oggds_header_audio_t audio;
127 int32_t i_padding_1; /* Because the original is using MSVC packing style */
131 /*****************************************************************************
132 * Definitions of structures and functions used by this plugins
133 *****************************************************************************/
145 int i_keyframe_granule_shift; /* Theora only */
146 int i_last_keyframe; /* dirac and theora */
147 int i_num_frames; /* Theora only */
148 uint64_t u_last_granulepos; /* Used for correct EOS page */
149 int64_t i_num_keyframes;
152 oggds_header_t *p_oggds_header;
156 struct sout_mux_sys_t
161 int i_next_serial_no;
163 /* number of logical streams pending to be added */
166 /* logical streams pending to be deleted */
168 ogg_stream_t **pp_del_streams;
171 static void OggSetDate( block_t *, mtime_t , mtime_t );
172 static block_t *OggStreamFlush( sout_mux_t *, ogg_stream_state *, mtime_t );
174 /*****************************************************************************
176 *****************************************************************************/
177 static int Open( vlc_object_t *p_this )
179 sout_mux_t *p_mux = (sout_mux_t*)p_this;
180 sout_mux_sys_t *p_sys;
182 msg_Info( p_mux, "Open" );
184 p_sys = malloc( sizeof( sout_mux_sys_t ) );
187 p_sys->i_streams = 0;
188 p_sys->i_add_streams = 0;
189 p_sys->i_del_streams = 0;
190 p_sys->pp_del_streams = 0;
192 p_mux->p_sys = p_sys;
193 p_mux->pf_control = Control;
194 p_mux->pf_addstream = AddStream;
195 p_mux->pf_delstream = DelStream;
198 /* First serial number is random.
199 * (Done like this because on win32 you need to seed the random number
200 * generator once per thread). */
201 srand( (unsigned int)time( NULL ) );
202 p_sys->i_next_serial_no = rand();
207 /*****************************************************************************
208 * Close: Finalize ogg bitstream and close muxer
209 *****************************************************************************/
210 static void Close( vlc_object_t * p_this )
212 sout_mux_t *p_mux = (sout_mux_t*)p_this;
213 sout_mux_sys_t *p_sys = p_mux->p_sys;
215 msg_Info( p_mux, "Close" );
217 if( p_sys->i_del_streams )
219 block_t *p_og = NULL;
220 mtime_t i_dts = p_sys->pp_del_streams[p_sys->i_del_streams - 1]->i_dts;
222 /* Close the current ogg stream */
223 msg_Dbg( p_mux, "writing footer" );
224 block_ChainAppend( &p_og, OggCreateFooter( p_mux ) );
226 /* Remove deleted logical streams */
227 for(int i = 0; i < p_sys->i_del_streams; i++ )
229 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
230 FREENULL( p_sys->pp_del_streams[i]->p_oggds_header );
231 FREENULL( p_sys->pp_del_streams[i] );
233 FREENULL( p_sys->pp_del_streams );
234 p_sys->i_streams -= p_sys->i_del_streams;
237 OggSetDate( p_og, i_dts, 0 );
238 sout_AccessOutWrite( p_mux->p_access, p_og );
244 /*****************************************************************************
246 *****************************************************************************/
247 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
255 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
256 pb_bool = (bool*)va_arg( args, bool * );
260 case MUX_GET_ADD_STREAM_WAIT:
261 pb_bool = (bool*)va_arg( args, bool * );
266 ppsz = (char**)va_arg( args, char ** );
267 *ppsz = strdup( "application/ogg" );
274 /*****************************************************************************
275 * AddStream: Add an elementary stream to the muxed stream
276 *****************************************************************************/
277 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
279 sout_mux_sys_t *p_sys = p_mux->p_sys;
280 ogg_stream_t *p_stream;
283 msg_Dbg( p_mux, "adding input" );
285 p_input->p_sys = p_stream = calloc( 1, sizeof( ogg_stream_t ) );
289 p_stream->i_cat = p_input->p_fmt->i_cat;
290 p_stream->i_fourcc = p_input->p_fmt->i_codec;
291 p_stream->i_serial_no = p_sys->i_next_serial_no++;
292 p_stream->i_packet_no = 0;
293 p_stream->i_last_keyframe = 0;
294 p_stream->i_num_keyframes = 0;
295 p_stream->i_num_frames = 0;
297 p_stream->p_oggds_header = 0;
299 switch( p_input->p_fmt->i_cat )
302 if( !p_input->p_fmt->video.i_frame_rate ||
303 !p_input->p_fmt->video.i_frame_rate_base )
305 msg_Warn( p_mux, "Missing frame rate, assuming 25fps" );
306 p_input->p_fmt->video.i_frame_rate = 25;
307 p_input->p_fmt->video.i_frame_rate_base = 1;
310 switch( p_stream->i_fourcc )
320 p_stream->p_oggds_header = calloc( 1, sizeof(oggds_header_t) );
321 if( !p_stream->p_oggds_header )
326 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
328 memcpy( p_stream->p_oggds_header->stream_type, "video", 5 );
329 if( p_stream->i_fourcc == VLC_CODEC_MP4V )
331 memcpy( p_stream->p_oggds_header->sub_type, "XVID", 4 );
333 else if( p_stream->i_fourcc == VLC_CODEC_DIV3 )
335 memcpy( p_stream->p_oggds_header->sub_type, "DIV3", 4 );
339 memcpy( p_stream->p_oggds_header->sub_type,
340 &p_stream->i_fourcc, 4 );
342 SetDWLE( &p_stream->p_oggds_header->i_size,
343 sizeof( oggds_header_t ) - 1 );
344 SetQWLE( &p_stream->p_oggds_header->i_time_unit,
345 INT64_C(10000000) * p_input->p_fmt->video.i_frame_rate_base /
346 (int64_t)p_input->p_fmt->video.i_frame_rate );
347 SetQWLE( &p_stream->p_oggds_header->i_samples_per_unit, 1 );
348 SetDWLE( &p_stream->p_oggds_header->i_default_len, 1 ); /* ??? */
349 SetDWLE( &p_stream->p_oggds_header->i_buffer_size, 1024*1024 );
350 SetWLE( &p_stream->p_oggds_header->i_bits_per_sample, 0 );
351 SetDWLE( &p_stream->p_oggds_header->header.video.i_width,
352 p_input->p_fmt->video.i_width );
353 SetDWLE( &p_stream->p_oggds_header->header.video.i_height,
354 p_input->p_fmt->video.i_height );
355 msg_Dbg( p_mux, "%4.4s stream", (char *)&p_stream->i_fourcc );
358 case VLC_CODEC_DIRAC:
359 msg_Dbg( p_mux, "dirac stream" );
362 case VLC_CODEC_THEORA:
363 msg_Dbg( p_mux, "theora stream" );
367 FREENULL( p_input->p_sys );
373 switch( p_stream->i_fourcc )
375 case VLC_CODEC_VORBIS:
376 msg_Dbg( p_mux, "vorbis stream" );
379 case VLC_CODEC_SPEEX:
380 msg_Dbg( p_mux, "speex stream" );
384 msg_Dbg( p_mux, "flac stream" );
388 fourcc_to_wf_tag( p_stream->i_fourcc, &i_tag );
389 if( i_tag == WAVE_FORMAT_UNKNOWN )
391 FREENULL( p_input->p_sys );
395 p_stream->p_oggds_header =
396 malloc( sizeof(oggds_header_t) + p_input->p_fmt->i_extra );
397 if( !p_stream->p_oggds_header )
402 memset( p_stream->p_oggds_header, 0, sizeof(oggds_header_t) );
403 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
405 SetDWLE( &p_stream->p_oggds_header->i_size,
406 sizeof( oggds_header_t ) - 1 + p_input->p_fmt->i_extra );
408 if( p_input->p_fmt->i_extra )
410 memcpy( &p_stream->p_oggds_header[1],
411 p_input->p_fmt->p_extra, p_input->p_fmt->i_extra );
414 memcpy( p_stream->p_oggds_header->stream_type, "audio", 5 );
416 memset( p_stream->p_oggds_header->sub_type, 0, 4 );
417 sprintf( p_stream->p_oggds_header->sub_type, "%-x", i_tag );
419 SetQWLE( &p_stream->p_oggds_header->i_time_unit, INT64_C(10000000) );
420 SetDWLE( &p_stream->p_oggds_header->i_default_len, 1 );
421 SetDWLE( &p_stream->p_oggds_header->i_buffer_size, 30*1024 );
422 SetQWLE( &p_stream->p_oggds_header->i_samples_per_unit,
423 p_input->p_fmt->audio.i_rate );
424 SetWLE( &p_stream->p_oggds_header->i_bits_per_sample,
425 p_input->p_fmt->audio.i_bitspersample );
426 SetDWLE( &p_stream->p_oggds_header->header.audio.i_channels,
427 p_input->p_fmt->audio.i_channels );
428 SetDWLE( &p_stream->p_oggds_header->header.audio.i_block_align,
429 p_input->p_fmt->audio.i_blockalign );
430 SetDWLE( &p_stream->p_oggds_header->header.audio.i_avgbytespersec,
431 p_input->p_fmt->i_bitrate / 8);
432 msg_Dbg( p_mux, "%4.4s stream", (char *)&p_stream->i_fourcc );
438 switch( p_stream->i_fourcc )
441 p_stream->p_oggds_header = calloc( 1, sizeof(oggds_header_t) );
442 if( !p_stream->p_oggds_header )
447 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
449 memcpy( p_stream->p_oggds_header->stream_type, "text", 4 );
450 msg_Dbg( p_mux, "subtitles stream" );
454 FREENULL( p_input->p_sys );
459 FREENULL( p_input->p_sys );
463 p_stream->b_new = true;
465 p_sys->i_add_streams++;
470 /*****************************************************************************
471 * DelStream: Delete an elementary stream from the muxed stream
472 *****************************************************************************/
473 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
475 sout_mux_sys_t *p_sys = p_mux->p_sys;
476 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
479 msg_Dbg( p_mux, "removing input" );
481 /* flush all remaining data */
484 if( !p_stream->b_new )
486 while( block_FifoCount( p_input->p_fifo ) )
487 MuxBlock( p_mux, p_input );
490 if( !p_stream->b_new &&
491 ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
493 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
494 sout_AccessOutWrite( p_mux->p_access, p_og );
497 /* move input in delete queue */
498 if( !p_stream->b_new )
500 p_sys->pp_del_streams = xrealloc( p_sys->pp_del_streams,
501 (p_sys->i_del_streams + 1) * sizeof(ogg_stream_t *) );
502 p_sys->pp_del_streams[p_sys->i_del_streams++] = p_stream;
506 /* wasn't already added so get rid of it */
507 FREENULL( p_stream->p_oggds_header );
508 FREENULL( p_stream );
509 p_sys->i_add_streams--;
513 p_input->p_sys = NULL;
518 /*****************************************************************************
519 * Ogg bitstream manipulation routines
520 *****************************************************************************/
521 static block_t *OggStreamGetPage( sout_mux_t *p_mux,
522 ogg_stream_state *p_os, mtime_t i_pts,
526 block_t *p_og, *p_og_first = NULL;
528 int (*pager)( ogg_stream_state*, ogg_page* ) = flush ? ogg_stream_flush : ogg_stream_pageout;
530 while( pager( p_os, &og ) )
533 p_og = block_New( p_mux, og.header_len + og.body_len );
535 memcpy( p_og->p_buffer, og.header, og.header_len );
536 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
541 i_pts = 0; // write it only once
543 block_ChainAppend( &p_og_first, p_og );
549 static block_t *OggStreamFlush( sout_mux_t *p_mux,
550 ogg_stream_state *p_os, mtime_t i_pts )
552 return OggStreamGetPage( p_mux, p_os, i_pts, true );
555 static block_t *OggStreamPageOut( sout_mux_t *p_mux,
556 ogg_stream_state *p_os, mtime_t i_pts )
558 return OggStreamGetPage( p_mux, p_os, i_pts, false );
561 static block_t *OggCreateHeader( sout_mux_t *p_mux )
563 block_t *p_hdr = NULL;
564 block_t *p_og = NULL;
568 /* Write header for each stream. All b_o_s (beginning of stream) packets
569 * must appear first in the ogg stream so we take care of them first. */
570 for( int pass = 0; pass < 2; pass++ )
572 for( i = 0; i < p_mux->i_nb_inputs; i++ )
574 sout_input_t *p_input = p_mux->pp_inputs[i];
575 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
577 bool video = ( p_stream->i_fourcc == VLC_CODEC_THEORA || p_stream->i_fourcc == VLC_CODEC_DIRAC );
578 if( ( ( pass == 0 && !video ) || ( pass == 1 && video ) ) )
581 msg_Dbg( p_mux, "creating header for %4.4s",
582 (char *)&p_stream->i_fourcc );
584 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
585 p_stream->b_new = false;
586 p_stream->i_packet_no = 0;
588 if( p_stream->i_fourcc == VLC_CODEC_VORBIS ||
589 p_stream->i_fourcc == VLC_CODEC_SPEEX ||
590 p_stream->i_fourcc == VLC_CODEC_THEORA )
592 /* First packet in order: vorbis/speex/theora info */
593 unsigned pi_size[XIPH_MAX_HEADER_COUNT];
594 void *pp_data[XIPH_MAX_HEADER_COUNT];
596 if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
597 p_input->p_fmt->i_extra, p_input->p_fmt->p_extra ) )
604 op.bytes = pi_size[0];
605 op.packet = pp_data[0];
606 if( pi_size[0] <= 0 )
607 msg_Err( p_mux, "header data corrupted");
612 op.packetno = p_stream->i_packet_no++;
613 ogg_stream_packetin( &p_stream->os, &op );
614 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
616 /* Get keyframe_granule_shift for theora granulepos calculation */
617 if( p_stream->i_fourcc == VLC_CODEC_THEORA )
619 p_stream->i_keyframe_granule_shift =
620 ( (op.packet[40] & 0x03) << 3 ) | ( (op.packet[41] & 0xe0) >> 5 );
623 for( unsigned i = 0; i < i_count; i++ )
626 else if( p_stream->i_fourcc == VLC_CODEC_DIRAC )
628 op.packet = p_input->p_fmt->p_extra;
629 op.bytes = p_input->p_fmt->i_extra;
633 op.packetno = p_stream->i_packet_no++;
634 ogg_stream_packetin( &p_stream->os, &op );
635 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
637 else if( p_stream->i_fourcc == VLC_CODEC_FLAC )
639 /* flac stream marker (yeah, only that in the 1st packet) */
640 op.packet = (unsigned char *)"fLaC";
645 op.packetno = p_stream->i_packet_no++;
646 ogg_stream_packetin( &p_stream->os, &op );
647 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
649 else if( p_stream->p_oggds_header )
652 op.packet = (uint8_t*)p_stream->p_oggds_header;
653 op.bytes = p_stream->p_oggds_header->i_size + 1;
657 op.packetno = p_stream->i_packet_no++;
658 ogg_stream_packetin( &p_stream->os, &op );
659 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
662 block_ChainAppend( &p_hdr, p_og );
666 /* Take care of the non b_o_s headers */
667 for( i = 0; i < p_mux->i_nb_inputs; i++ )
669 sout_input_t *p_input = p_mux->pp_inputs[i];
670 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
672 if( p_stream->i_fourcc == VLC_CODEC_VORBIS ||
673 p_stream->i_fourcc == VLC_CODEC_SPEEX ||
674 p_stream->i_fourcc == VLC_CODEC_THEORA )
676 unsigned pi_size[XIPH_MAX_HEADER_COUNT];
677 void *pp_data[XIPH_MAX_HEADER_COUNT];
679 if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
680 p_input->p_fmt->i_extra, p_input->p_fmt->p_extra ) )
683 /* Special case, headers are already there in the incoming stream.
684 * We need to gather them an mark them as headers. */
685 for( unsigned i = 1; i < i_count; i++ )
687 op.bytes = pi_size[i];
688 op.packet = pp_data[i];
689 if( pi_size[i] <= 0 )
690 msg_Err( p_mux, "header data corrupted");
695 op.packetno = p_stream->i_packet_no++;
696 ogg_stream_packetin( &p_stream->os, &op );
698 if( i == i_count - 1 )
699 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
701 p_og = OggStreamPageOut( p_mux, &p_stream->os, 0 );
703 block_ChainAppend( &p_hdr, p_og );
705 for( unsigned i = 0; i < i_count; i++ )
708 else if( p_stream->i_fourcc != VLC_CODEC_FLAC &&
709 p_stream->i_fourcc != VLC_CODEC_DIRAC )
715 com[0] = PACKET_TYPE_COMMENT;
716 i_com = snprintf( (char *)(com+1), 127,
717 PACKAGE_VERSION" stream output" )
724 op.packetno = p_stream->i_packet_no++;
725 ogg_stream_packetin( &p_stream->os, &op );
726 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
727 block_ChainAppend( &p_hdr, p_og );
730 /* Special case for mp4v and flac */
731 if( ( p_stream->i_fourcc == VLC_CODEC_MP4V ||
732 p_stream->i_fourcc == VLC_CODEC_FLAC ) &&
733 p_input->p_fmt->i_extra )
735 /* Send a packet with the VOL data for mp4v
736 * or STREAMINFO for flac */
737 msg_Dbg( p_mux, "writing extra data" );
738 op.bytes = p_input->p_fmt->i_extra;
739 op.packet = p_input->p_fmt->p_extra;
740 if( p_stream->i_fourcc == VLC_CODEC_FLAC )
742 /* Skip the flac stream marker */
749 op.packetno = p_stream->i_packet_no++;
750 ogg_stream_packetin( &p_stream->os, &op );
751 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
752 block_ChainAppend( &p_hdr, p_og );
756 /* set HEADER flag */
757 for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
759 p_og->i_flags |= BLOCK_FLAG_HEADER;
764 static block_t *OggCreateFooter( sout_mux_t *p_mux )
766 sout_mux_sys_t *p_sys = p_mux->p_sys;
767 block_t *p_hdr = NULL;
772 /* flush all remaining data */
773 for( i = 0; i < p_mux->i_nb_inputs; i++ )
775 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
777 /* skip newly added streams */
778 if( p_stream->b_new ) continue;
780 if( ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
782 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
783 sout_AccessOutWrite( p_mux->p_access, p_og );
787 /* Write eos packets for each stream. */
788 for( i = 0; i < p_mux->i_nb_inputs; i++ )
790 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
792 /* skip newly added streams */
793 if( p_stream->b_new ) continue;
799 op.granulepos = p_stream->u_last_granulepos;
800 op.packetno = p_stream->i_packet_no++;
801 ogg_stream_packetin( &p_stream->os, &op );
803 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
804 block_ChainAppend( &p_hdr, p_og );
805 ogg_stream_clear( &p_stream->os );
808 for( i = 0; i < p_sys->i_del_streams; i++ )
814 op.granulepos = p_sys->pp_del_streams[i]->u_last_granulepos;
815 op.packetno = p_sys->pp_del_streams[i]->i_packet_no++;
816 ogg_stream_packetin( &p_sys->pp_del_streams[i]->os, &op );
818 p_og = OggStreamFlush( p_mux, &p_sys->pp_del_streams[i]->os, 0 );
819 block_ChainAppend( &p_hdr, p_og );
820 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
826 static void OggSetDate( block_t *p_og, mtime_t i_dts, mtime_t i_length )
832 for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
837 if( i_count == 0 ) return; /* ignore. */
839 i_delta = i_length / i_count;
841 for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
843 p_tmp->i_dts = i_dts;
844 p_tmp->i_length = i_delta;
850 /*****************************************************************************
851 * Mux: multiplex available data in input fifos into the Ogg bitstream
852 *****************************************************************************/
853 static int Mux( sout_mux_t *p_mux )
855 sout_mux_sys_t *p_sys = p_mux->p_sys;
856 block_t *p_og = NULL;
859 if( p_sys->i_add_streams || p_sys->i_del_streams )
861 /* Open new ogg stream */
862 if( sout_MuxGetStream( p_mux, 1, &i_dts) < 0 )
864 msg_Dbg( p_mux, "waiting for data..." );
868 if( p_sys->i_streams )
870 /* Close current ogg stream */
873 msg_Dbg( p_mux, "writing footer" );
874 block_ChainAppend( &p_og, OggCreateFooter( p_mux ) );
876 /* Remove deleted logical streams */
877 for( i = 0; i < p_sys->i_del_streams; i++ )
879 FREENULL( p_sys->pp_del_streams[i]->p_oggds_header );
880 FREENULL( p_sys->pp_del_streams[i] );
882 FREENULL( p_sys->pp_del_streams );
883 p_sys->i_streams = 0;
886 msg_Dbg( p_mux, "writing header" );
887 p_sys->i_start_dts = i_dts;
888 p_sys->i_streams = p_mux->i_nb_inputs;
889 p_sys->i_del_streams = 0;
890 p_sys->i_add_streams = 0;
891 block_ChainAppend( &p_og, OggCreateHeader( p_mux ) );
893 /* Write header and/or footer */
894 OggSetDate( p_og, i_dts, 0 );
895 sout_AccessOutWrite( p_mux->p_access, p_og );
901 int i_stream = sout_MuxGetStream( p_mux, 1, NULL );
904 MuxBlock( p_mux, p_mux->pp_inputs[i_stream] );
910 static int MuxBlock( sout_mux_t *p_mux, sout_input_t *p_input )
912 sout_mux_sys_t *p_sys = p_mux->p_sys;
913 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
914 block_t *p_data = block_FifoGet( p_input->p_fifo );
915 block_t *p_og = NULL;
918 if( p_stream->i_fourcc != VLC_CODEC_VORBIS &&
919 p_stream->i_fourcc != VLC_CODEC_FLAC &&
920 p_stream->i_fourcc != VLC_CODEC_SPEEX &&
921 p_stream->i_fourcc != VLC_CODEC_THEORA &&
922 p_stream->i_fourcc != VLC_CODEC_DIRAC )
924 p_data = block_Realloc( p_data, 1, p_data->i_buffer );
925 p_data->p_buffer[0] = PACKET_IS_SYNCPOINT; // FIXME
928 op.packet = p_data->p_buffer;
929 op.bytes = p_data->i_buffer;
932 op.packetno = p_stream->i_packet_no++;
934 if( p_stream->i_cat == AUDIO_ES )
936 if( p_stream->i_fourcc == VLC_CODEC_VORBIS ||
937 p_stream->i_fourcc == VLC_CODEC_FLAC ||
938 p_stream->i_fourcc == VLC_CODEC_SPEEX )
940 /* number of sample from begining + current packet */
942 ( p_data->i_dts - p_sys->i_start_dts + p_data->i_length ) *
943 (mtime_t)p_input->p_fmt->audio.i_rate / INT64_C(1000000);
945 else if( p_stream->p_oggds_header )
947 /* number of sample from begining */
948 op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) *
949 p_stream->p_oggds_header->i_samples_per_unit / INT64_C(1000000);
952 else if( p_stream->i_cat == VIDEO_ES )
954 if( p_stream->i_fourcc == VLC_CODEC_THEORA )
956 p_stream->i_num_frames++;
957 if( p_data->i_flags & BLOCK_FLAG_TYPE_I )
959 p_stream->i_num_keyframes++;
960 p_stream->i_last_keyframe = p_stream->i_num_frames;
963 op.granulepos = (p_stream->i_last_keyframe << p_stream->i_keyframe_granule_shift )
964 | (p_stream->i_num_frames-p_stream->i_last_keyframe);
966 else if( p_stream->i_fourcc == VLC_CODEC_DIRAC )
968 mtime_t dt = (p_data->i_dts - p_sys->i_start_dts + 1)
969 * p_input->p_fmt->video.i_frame_rate *2
970 / p_input->p_fmt->video.i_frame_rate_base
972 mtime_t delay = (p_data->i_pts - p_data->i_dts + 1)
973 * p_input->p_fmt->video.i_frame_rate *2
974 / p_input->p_fmt->video.i_frame_rate_base
976 if( p_data->i_flags & BLOCK_FLAG_TYPE_I )
977 p_stream->i_last_keyframe = dt;
978 mtime_t dist = dt - p_stream->i_last_keyframe;
979 op.granulepos = dt << 31 | (dist&0xff00) << 14
980 | (delay&0x1fff) << 9 | (dist&0xff);
982 else if( p_stream->p_oggds_header )
983 op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) * INT64_C(10) /
984 p_stream->p_oggds_header->i_time_unit;
986 else if( p_stream->i_cat == SPU_ES )
988 /* granulepos is in millisec */
989 op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) / 1000;
992 p_stream->u_last_granulepos = op.granulepos;
993 ogg_stream_packetin( &p_stream->os, &op );
995 if( p_stream->i_cat == SPU_ES ||
996 p_stream->i_fourcc == VLC_CODEC_SPEEX ||
997 p_stream->i_fourcc == VLC_CODEC_DIRAC )
999 /* Subtitles or Speex packets are quite small so they
1000 * need to be flushed to be sent on time */
1001 /* The OggDirac mapping suggests ever so strongly that a
1002 * page flush occurs after each OggDirac packet, so to make
1003 * the timestamps unambiguous */
1004 p_og = OggStreamFlush( p_mux, &p_stream->os, p_data->i_dts );
1008 p_og = OggStreamPageOut( p_mux, &p_stream->os, p_data->i_dts );
1013 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
1014 p_stream->i_dts = -1;
1015 p_stream->i_length = 0;
1017 sout_AccessOutWrite( p_mux->p_access, p_og );
1021 if( p_stream->i_dts < 0 )
1023 p_stream->i_dts = p_data->i_dts;
1025 p_stream->i_length += p_data->i_length;
1028 block_Release( p_data );