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_common.h>
36 #include <vlc_plugin.h>
38 #include <vlc_block.h>
39 #include <vlc_codecs.h>
40 #include <vlc_memory.h>
44 /*****************************************************************************
46 *****************************************************************************/
47 static int Open ( vlc_object_t * );
48 static void Close ( vlc_object_t * );
51 set_description( N_("Ogg/OGM muxer") )
52 set_capability( "sout mux", 10 )
53 set_category( CAT_SOUT )
54 set_subcategory( SUBCAT_SOUT_MUX )
57 set_callbacks( Open, Close )
61 /*****************************************************************************
63 *****************************************************************************/
64 static int Control ( sout_mux_t *, int, va_list );
65 static int AddStream( sout_mux_t *, sout_input_t * );
66 static int DelStream( sout_mux_t *, sout_input_t * );
67 static int Mux ( sout_mux_t * );
68 static int MuxBlock ( sout_mux_t *, sout_input_t * );
70 static block_t *OggCreateHeader( sout_mux_t * );
71 static block_t *OggCreateFooter( sout_mux_t * );
73 /*****************************************************************************
75 *****************************************************************************/
77 /* Structures used for OggDS headers used in ogm files */
79 #define PACKET_TYPE_HEADER 0x01
80 #define PACKET_TYPE_COMMENT 0x03
81 #define PACKET_IS_SYNCPOINT 0x08
84 #ifdef HAVE_ATTRIBUTE_PACKED
85 __attribute__((__packed__))
90 } oggds_header_video_t;
93 #ifdef HAVE_ATTRIBUTE_PACKED
94 __attribute__((__packed__))
98 int16_t i_block_align;
99 int32_t i_avgbytespersec;
100 } oggds_header_audio_t;
103 #ifdef HAVE_ATTRIBUTE_PACKED
104 __attribute__((__packed__))
107 uint8_t i_packet_type;
115 int64_t i_samples_per_unit;
116 int32_t i_default_len;
118 int32_t i_buffer_size;
119 int16_t i_bits_per_sample;
121 int16_t i_padding_0; /* Because the original is using MSVC packing style */
125 oggds_header_video_t video;
126 oggds_header_audio_t audio;
129 int32_t i_padding_1; /* Because the original is using MSVC packing style */
134 * TODO move this function to src/stream_output.c (used by nearly all muxers)
136 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
141 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
143 block_fifo_t *p_fifo;
145 p_fifo = p_mux->pp_inputs[i]->p_fifo;
147 /* We don't really need to have anything in the SPU fifo */
148 if( p_mux->pp_inputs[i]->p_fmt->i_cat == SPU_ES &&
149 block_FifoCount( p_fifo ) == 0 ) continue;
151 if( block_FifoCount( p_fifo ) )
155 p_buf = block_FifoShow( p_fifo );
156 if( i_stream < 0 || p_buf->i_dts < i_dts )
158 i_dts = p_buf->i_dts;
165 if( pi_stream ) *pi_stream = i_stream;
166 if( pi_dts ) *pi_dts = i_dts;
170 /*****************************************************************************
171 * Definitions of structures and functions used by this plugins
172 *****************************************************************************/
184 int i_keyframe_granule_shift; /* Theora only */
185 int i_last_keyframe; /* dirac and theora */
186 int i_num_frames; /* Theora only */
187 uint64_t u_last_granulepos; /* Used for correct EOS page */
188 int64_t i_num_keyframes;
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 ) );
226 p_sys->i_streams = 0;
227 p_sys->i_add_streams = 0;
228 p_sys->i_del_streams = 0;
229 p_sys->pp_del_streams = 0;
231 p_mux->p_sys = p_sys;
232 p_mux->pf_control = Control;
233 p_mux->pf_addstream = AddStream;
234 p_mux->pf_delstream = DelStream;
237 /* First serial number is random.
238 * (Done like this because on win32 you need to seed the random number
239 * generator once per thread). */
240 srand( (unsigned int)time( NULL ) );
241 p_sys->i_next_serial_no = rand();
246 /*****************************************************************************
247 * Close: Finalize ogg bitstream and close muxer
248 *****************************************************************************/
249 static void Close( vlc_object_t * p_this )
251 sout_mux_t *p_mux = (sout_mux_t*)p_this;
252 sout_mux_sys_t *p_sys = p_mux->p_sys;
254 msg_Info( p_mux, "Close" );
256 if( p_sys->i_del_streams )
258 block_t *p_og = NULL;
262 /* Close the current ogg stream */
263 msg_Dbg( p_mux, "writing footer" );
264 block_ChainAppend( &p_og, OggCreateFooter( p_mux ) );
266 /* Remove deleted logical streams */
267 for( i = 0; i < p_sys->i_del_streams; i++ )
269 i_dts = p_sys->pp_del_streams[i]->i_dts;
270 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
271 FREENULL( p_sys->pp_del_streams[i]->p_oggds_header );
272 FREENULL( p_sys->pp_del_streams[i] );
274 FREENULL( p_sys->pp_del_streams );
275 p_sys->i_streams -= p_sys->i_del_streams;
278 OggSetDate( p_og, i_dts, 0 );
279 sout_AccessOutWrite( p_mux->p_access, p_og );
285 /*****************************************************************************
287 *****************************************************************************/
288 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
296 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
297 pb_bool = (bool*)va_arg( args, bool * );
301 case MUX_GET_ADD_STREAM_WAIT:
302 pb_bool = (bool*)va_arg( args, bool * );
307 ppsz = (char**)va_arg( args, char ** );
308 *ppsz = strdup( "application/ogg" );
315 /*****************************************************************************
316 * AddStream: Add an elementary stream to the muxed stream
317 *****************************************************************************/
318 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
320 sout_mux_sys_t *p_sys = p_mux->p_sys;
321 ogg_stream_t *p_stream;
324 msg_Dbg( p_mux, "adding input" );
326 p_input->p_sys = p_stream = calloc( 1, sizeof( ogg_stream_t ) );
330 p_stream->i_cat = p_input->p_fmt->i_cat;
331 p_stream->i_fourcc = p_input->p_fmt->i_codec;
332 p_stream->i_serial_no = p_sys->i_next_serial_no++;
333 p_stream->i_packet_no = 0;
334 p_stream->i_last_keyframe = 0;
335 p_stream->i_num_keyframes = 0;
336 p_stream->i_num_frames = 0;
338 p_stream->p_oggds_header = 0;
340 switch( p_input->p_fmt->i_cat )
343 if( !p_input->p_fmt->video.i_frame_rate ||
344 !p_input->p_fmt->video.i_frame_rate_base )
346 msg_Warn( p_mux, "Missing frame rate, assuming 25fps" );
347 p_input->p_fmt->video.i_frame_rate = 25;
348 p_input->p_fmt->video.i_frame_rate_base = 1;
351 switch( p_stream->i_fourcc )
361 p_stream->p_oggds_header = calloc( 1, sizeof(oggds_header_t) );
362 if( !p_stream->p_oggds_header )
367 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
369 memcpy( p_stream->p_oggds_header->stream_type, "video", 5 );
370 if( p_stream->i_fourcc == VLC_CODEC_MP4V )
372 memcpy( p_stream->p_oggds_header->sub_type, "XVID", 4 );
374 else if( p_stream->i_fourcc == VLC_CODEC_DIV3 )
376 memcpy( p_stream->p_oggds_header->sub_type, "DIV3", 4 );
380 memcpy( p_stream->p_oggds_header->sub_type,
381 &p_stream->i_fourcc, 4 );
383 SetDWLE( &p_stream->p_oggds_header->i_size,
384 sizeof( oggds_header_t ) - 1 );
385 SetQWLE( &p_stream->p_oggds_header->i_time_unit,
386 INT64_C(10000000) * p_input->p_fmt->video.i_frame_rate_base /
387 (int64_t)p_input->p_fmt->video.i_frame_rate );
388 SetQWLE( &p_stream->p_oggds_header->i_samples_per_unit, 1 );
389 SetDWLE( &p_stream->p_oggds_header->i_default_len, 1 ); /* ??? */
390 SetDWLE( &p_stream->p_oggds_header->i_buffer_size, 1024*1024 );
391 SetWLE( &p_stream->p_oggds_header->i_bits_per_sample, 0 );
392 SetDWLE( &p_stream->p_oggds_header->header.video.i_width,
393 p_input->p_fmt->video.i_width );
394 SetDWLE( &p_stream->p_oggds_header->header.video.i_height,
395 p_input->p_fmt->video.i_height );
396 msg_Dbg( p_mux, "%4.4s stream", (char *)&p_stream->i_fourcc );
399 case VLC_CODEC_DIRAC:
400 msg_Dbg( p_mux, "dirac stream" );
403 case VLC_CODEC_THEORA:
404 msg_Dbg( p_mux, "theora stream" );
408 FREENULL( p_input->p_sys );
414 switch( p_stream->i_fourcc )
416 case VLC_CODEC_VORBIS:
417 msg_Dbg( p_mux, "vorbis stream" );
420 case VLC_CODEC_SPEEX:
421 msg_Dbg( p_mux, "speex stream" );
425 msg_Dbg( p_mux, "flac stream" );
429 fourcc_to_wf_tag( p_stream->i_fourcc, &i_tag );
430 if( i_tag == WAVE_FORMAT_UNKNOWN )
432 FREENULL( p_input->p_sys );
436 p_stream->p_oggds_header =
437 malloc( sizeof(oggds_header_t) + p_input->p_fmt->i_extra );
438 if( !p_stream->p_oggds_header )
443 memset( p_stream->p_oggds_header, 0, sizeof(oggds_header_t) );
444 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
446 SetDWLE( &p_stream->p_oggds_header->i_size,
447 sizeof( oggds_header_t ) - 1 + p_input->p_fmt->i_extra );
449 if( p_input->p_fmt->i_extra )
451 memcpy( &p_stream->p_oggds_header[1],
452 p_input->p_fmt->p_extra, p_input->p_fmt->i_extra );
455 memcpy( p_stream->p_oggds_header->stream_type, "audio", 5 );
457 memset( p_stream->p_oggds_header->sub_type, 0, 4 );
458 sprintf( p_stream->p_oggds_header->sub_type, "%-x", i_tag );
460 SetQWLE( &p_stream->p_oggds_header->i_time_unit, INT64_C(10000000) );
461 SetDWLE( &p_stream->p_oggds_header->i_default_len, 1 );
462 SetDWLE( &p_stream->p_oggds_header->i_buffer_size, 30*1024 );
463 SetQWLE( &p_stream->p_oggds_header->i_samples_per_unit,
464 p_input->p_fmt->audio.i_rate );
465 SetWLE( &p_stream->p_oggds_header->i_bits_per_sample,
466 p_input->p_fmt->audio.i_bitspersample );
467 SetDWLE( &p_stream->p_oggds_header->header.audio.i_channels,
468 p_input->p_fmt->audio.i_channels );
469 SetDWLE( &p_stream->p_oggds_header->header.audio.i_block_align,
470 p_input->p_fmt->audio.i_blockalign );
471 SetDWLE( &p_stream->p_oggds_header->header.audio.i_avgbytespersec,
472 p_input->p_fmt->i_bitrate / 8);
473 msg_Dbg( p_mux, "%4.4s stream", (char *)&p_stream->i_fourcc );
479 switch( p_stream->i_fourcc )
482 p_stream->p_oggds_header = calloc( 1, sizeof(oggds_header_t) );
483 if( !p_stream->p_oggds_header )
488 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
490 memcpy( p_stream->p_oggds_header->stream_type, "text", 4 );
491 msg_Dbg( p_mux, "subtitles stream" );
495 FREENULL( p_input->p_sys );
500 FREENULL( p_input->p_sys );
504 p_stream->b_new = true;
506 p_sys->i_add_streams++;
511 /*****************************************************************************
512 * DelStream: Delete an elementary stream from the muxed stream
513 *****************************************************************************/
514 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
516 sout_mux_sys_t *p_sys = p_mux->p_sys;
517 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
520 msg_Dbg( p_mux, "removing input" );
522 /* flush all remaining data */
525 if( !p_stream->b_new )
527 while( block_FifoCount( p_input->p_fifo ) )
528 MuxBlock( p_mux, p_input );
531 if( !p_stream->b_new &&
532 ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
534 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
535 sout_AccessOutWrite( p_mux->p_access, p_og );
538 /* move input in delete queue */
539 if( !p_stream->b_new )
541 p_sys->pp_del_streams = realloc_or_free( p_sys->pp_del_streams,
542 (p_sys->i_del_streams + 1) * sizeof(ogg_stream_t *) );
543 assert( p_sys->pp_del_streams );
544 p_sys->pp_del_streams[p_sys->i_del_streams++] = p_stream;
548 /* wasn't already added so get rid of it */
549 FREENULL( p_stream->p_oggds_header );
550 FREENULL( p_stream );
551 p_sys->i_add_streams--;
555 p_input->p_sys = NULL;
560 /*****************************************************************************
561 * Ogg bitstream manipulation routines
562 *****************************************************************************/
563 static block_t *OggStreamGetPage( sout_mux_t *p_mux,
564 ogg_stream_state *p_os, mtime_t i_pts,
568 block_t *p_og, *p_og_first = NULL;
570 int (*pager)( ogg_stream_state*, ogg_page* ) = flush ? ogg_stream_flush : ogg_stream_pageout;
572 while( pager( p_os, &og ) )
575 p_og = block_New( p_mux, og.header_len + og.body_len );
577 memcpy( p_og->p_buffer, og.header, og.header_len );
578 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
583 i_pts = 0; // write it only once
585 block_ChainAppend( &p_og_first, p_og );
591 static block_t *OggStreamFlush( sout_mux_t *p_mux,
592 ogg_stream_state *p_os, mtime_t i_pts )
594 return OggStreamGetPage( p_mux, p_os, i_pts, true );
597 static block_t *OggStreamPageOut( sout_mux_t *p_mux,
598 ogg_stream_state *p_os, mtime_t i_pts )
600 return OggStreamGetPage( p_mux, p_os, i_pts, false );
603 static block_t *OggCreateHeader( sout_mux_t *p_mux )
605 block_t *p_hdr = NULL;
606 block_t *p_og = NULL;
611 /* Write header for each stream. All b_o_s (beginning of stream) packets
612 * must appear first in the ogg stream so we take care of them first. */
613 for( int pass = 0; pass < 2; pass++ )
615 for( i = 0; i < p_mux->i_nb_inputs; i++ )
617 sout_input_t *p_input = p_mux->pp_inputs[i];
618 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
620 bool video = ( p_stream->i_fourcc == VLC_CODEC_THEORA || p_stream->i_fourcc == VLC_CODEC_DIRAC );
621 if( ( ( pass == 0 && !video ) || ( pass == 1 && video ) ) )
624 msg_Dbg( p_mux, "creating header for %4.4s",
625 (char *)&p_stream->i_fourcc );
627 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
628 p_stream->b_new = false;
629 p_stream->i_packet_no = 0;
631 if( p_stream->i_fourcc == VLC_CODEC_VORBIS ||
632 p_stream->i_fourcc == VLC_CODEC_SPEEX ||
633 p_stream->i_fourcc == VLC_CODEC_THEORA )
635 /* First packet in order: vorbis/speex/theora info */
636 p_extra = p_input->p_fmt->p_extra;
637 i_extra = p_input->p_fmt->i_extra;
639 op.bytes = *(p_extra++) << 8;
640 op.bytes |= (*(p_extra++) & 0xFF);
642 i_extra -= (op.bytes + 2);
645 msg_Err( p_mux, "header data corrupted");
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 /* Get keyframe_granule_shift for theora granulepos calculation */
657 if( p_stream->i_fourcc == VLC_CODEC_THEORA )
659 p_stream->i_keyframe_granule_shift =
660 ( (op.packet[40] & 0x03) << 3 ) | ( (op.packet[41] & 0xe0) >> 5 );
663 else if( p_stream->i_fourcc == VLC_CODEC_DIRAC )
665 op.packet = p_input->p_fmt->p_extra;
666 op.bytes = p_input->p_fmt->i_extra;
670 op.packetno = p_stream->i_packet_no++;
671 ogg_stream_packetin( &p_stream->os, &op );
672 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
674 else if( p_stream->i_fourcc == VLC_CODEC_FLAC )
676 /* flac stream marker (yeah, only that in the 1st packet) */
677 op.packet = (unsigned char *)"fLaC";
682 op.packetno = p_stream->i_packet_no++;
683 ogg_stream_packetin( &p_stream->os, &op );
684 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
686 else if( p_stream->p_oggds_header )
689 op.packet = (uint8_t*)p_stream->p_oggds_header;
690 op.bytes = p_stream->p_oggds_header->i_size + 1;
694 op.packetno = p_stream->i_packet_no++;
695 ogg_stream_packetin( &p_stream->os, &op );
696 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
699 block_ChainAppend( &p_hdr, p_og );
703 /* Take care of the non b_o_s headers */
704 for( i = 0; i < p_mux->i_nb_inputs; i++ )
706 sout_input_t *p_input = p_mux->pp_inputs[i];
707 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
709 if( p_stream->i_fourcc == VLC_CODEC_VORBIS ||
710 p_stream->i_fourcc == VLC_CODEC_SPEEX ||
711 p_stream->i_fourcc == VLC_CODEC_THEORA )
713 /* Special case, headers are already there in the incoming stream.
714 * We need to gather them an mark them as headers. */
717 if( p_stream->i_fourcc == VLC_CODEC_SPEEX ) j = 1;
719 p_extra = p_input->p_fmt->p_extra;
720 i_extra = p_input->p_fmt->i_extra;
723 op.bytes = *(p_extra++) << 8;
724 op.bytes |= (*(p_extra++) & 0xFF);
727 i_extra -= (op.bytes + 2);
731 op.bytes = *(p_extra++) << 8;
732 op.bytes |= (*(p_extra++) & 0xFF);
735 i_extra -= (op.bytes + 2);
738 msg_Err( p_mux, "header data corrupted");
745 op.packetno = p_stream->i_packet_no++;
746 ogg_stream_packetin( &p_stream->os, &op );
749 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
751 p_og = OggStreamPageOut( p_mux, &p_stream->os, 0 );
753 block_ChainAppend( &p_hdr, p_og );
756 else if( p_stream->i_fourcc != VLC_CODEC_FLAC &&
757 p_stream->i_fourcc != VLC_CODEC_DIRAC )
763 com[0] = PACKET_TYPE_COMMENT;
764 i_com = snprintf( (char *)(com+1), 127,
765 PACKAGE_VERSION" stream output" )
772 op.packetno = p_stream->i_packet_no++;
773 ogg_stream_packetin( &p_stream->os, &op );
774 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
775 block_ChainAppend( &p_hdr, p_og );
778 /* Special case for mp4v and flac */
779 if( ( p_stream->i_fourcc == VLC_CODEC_MP4V ||
780 p_stream->i_fourcc == VLC_CODEC_FLAC ) &&
781 p_input->p_fmt->i_extra )
783 /* Send a packet with the VOL data for mp4v
784 * or STREAMINFO for flac */
785 msg_Dbg( p_mux, "writing extra data" );
786 op.bytes = p_input->p_fmt->i_extra;
787 op.packet = p_input->p_fmt->p_extra;
788 if( p_stream->i_fourcc == VLC_CODEC_FLAC )
790 /* Skip the flac stream marker */
797 op.packetno = p_stream->i_packet_no++;
798 ogg_stream_packetin( &p_stream->os, &op );
799 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
800 block_ChainAppend( &p_hdr, p_og );
804 /* set HEADER flag */
805 for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
807 p_og->i_flags |= BLOCK_FLAG_HEADER;
812 static block_t *OggCreateFooter( sout_mux_t *p_mux )
814 sout_mux_sys_t *p_sys = p_mux->p_sys;
815 block_t *p_hdr = NULL;
820 /* flush all remaining data */
821 for( i = 0; i < p_mux->i_nb_inputs; i++ )
823 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
825 /* skip newly added streams */
826 if( p_stream->b_new ) continue;
828 if( ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
830 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
831 sout_AccessOutWrite( p_mux->p_access, p_og );
835 /* Write eos packets for each stream. */
836 for( i = 0; i < p_mux->i_nb_inputs; i++ )
838 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
840 /* skip newly added streams */
841 if( p_stream->b_new ) continue;
847 op.granulepos = p_stream->u_last_granulepos;
848 op.packetno = p_stream->i_packet_no++;
849 ogg_stream_packetin( &p_stream->os, &op );
851 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
852 block_ChainAppend( &p_hdr, p_og );
853 ogg_stream_clear( &p_stream->os );
856 for( i = 0; i < p_sys->i_del_streams; i++ )
862 op.granulepos = p_sys->pp_del_streams[i]->u_last_granulepos;
863 op.packetno = p_sys->pp_del_streams[i]->i_packet_no++;
864 ogg_stream_packetin( &p_sys->pp_del_streams[i]->os, &op );
866 p_og = OggStreamFlush( p_mux, &p_sys->pp_del_streams[i]->os, 0 );
867 block_ChainAppend( &p_hdr, p_og );
868 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
874 static void OggSetDate( block_t *p_og, mtime_t i_dts, mtime_t i_length )
880 for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
885 if( i_count == 0 ) return; /* ignore. */
887 i_delta = i_length / i_count;
889 for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
891 p_tmp->i_dts = i_dts;
892 p_tmp->i_length = i_delta;
898 /*****************************************************************************
899 * Mux: multiplex available data in input fifos into the Ogg bitstream
900 *****************************************************************************/
901 static int Mux( sout_mux_t *p_mux )
903 sout_mux_sys_t *p_sys = p_mux->p_sys;
904 block_t *p_og = NULL;
908 if( p_sys->i_add_streams || p_sys->i_del_streams )
910 /* Open new ogg stream */
911 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
913 msg_Dbg( p_mux, "waiting for data..." );
917 if( p_sys->i_streams )
919 /* Close current ogg stream */
922 msg_Dbg( p_mux, "writing footer" );
923 block_ChainAppend( &p_og, OggCreateFooter( p_mux ) );
925 /* Remove deleted logical streams */
926 for( i = 0; i < p_sys->i_del_streams; i++ )
928 FREENULL( p_sys->pp_del_streams[i]->p_oggds_header );
929 FREENULL( p_sys->pp_del_streams[i] );
931 FREENULL( p_sys->pp_del_streams );
932 p_sys->i_streams = 0;
935 msg_Dbg( p_mux, "writing header" );
936 p_sys->i_start_dts = i_dts;
937 p_sys->i_streams = p_mux->i_nb_inputs;
938 p_sys->i_del_streams = 0;
939 p_sys->i_add_streams = 0;
940 block_ChainAppend( &p_og, OggCreateHeader( p_mux ) );
942 /* Write header and/or footer */
943 OggSetDate( p_og, i_dts, 0 );
944 sout_AccessOutWrite( p_mux->p_access, p_og );
950 if( MuxGetStream( p_mux, &i_stream, 0 ) < 0 ) return VLC_SUCCESS;
951 MuxBlock( p_mux, p_mux->pp_inputs[i_stream] );
957 static int MuxBlock( sout_mux_t *p_mux, sout_input_t *p_input )
959 sout_mux_sys_t *p_sys = p_mux->p_sys;
960 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
961 block_t *p_data = block_FifoGet( p_input->p_fifo );
962 block_t *p_og = NULL;
965 if( p_stream->i_fourcc != VLC_CODEC_VORBIS &&
966 p_stream->i_fourcc != VLC_CODEC_FLAC &&
967 p_stream->i_fourcc != VLC_CODEC_SPEEX &&
968 p_stream->i_fourcc != VLC_CODEC_THEORA &&
969 p_stream->i_fourcc != VLC_CODEC_DIRAC )
971 p_data = block_Realloc( p_data, 1, p_data->i_buffer );
972 p_data->p_buffer[0] = PACKET_IS_SYNCPOINT; // FIXME
975 op.packet = p_data->p_buffer;
976 op.bytes = p_data->i_buffer;
979 op.packetno = p_stream->i_packet_no++;
981 if( p_stream->i_cat == AUDIO_ES )
983 if( p_stream->i_fourcc == VLC_CODEC_VORBIS ||
984 p_stream->i_fourcc == VLC_CODEC_FLAC ||
985 p_stream->i_fourcc == VLC_CODEC_SPEEX )
987 /* number of sample from begining + current packet */
989 ( p_data->i_dts - p_sys->i_start_dts + p_data->i_length ) *
990 (mtime_t)p_input->p_fmt->audio.i_rate / INT64_C(1000000);
992 else if( p_stream->p_oggds_header )
994 /* number of sample from begining */
995 op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) *
996 p_stream->p_oggds_header->i_samples_per_unit / INT64_C(1000000);
999 else if( p_stream->i_cat == VIDEO_ES )
1001 if( p_stream->i_fourcc == VLC_CODEC_THEORA )
1003 p_stream->i_num_frames++;
1004 if( p_data->i_flags & BLOCK_FLAG_TYPE_I )
1006 p_stream->i_num_keyframes++;
1007 p_stream->i_last_keyframe = p_stream->i_num_frames;
1010 op.granulepos = (p_stream->i_last_keyframe << p_stream->i_keyframe_granule_shift )
1011 | (p_stream->i_num_frames-p_stream->i_last_keyframe);
1013 else if( p_stream->i_fourcc == VLC_CODEC_DIRAC )
1015 mtime_t dt = (p_data->i_dts - p_sys->i_start_dts + 1)
1016 * p_input->p_fmt->video.i_frame_rate *2
1017 / p_input->p_fmt->video.i_frame_rate_base
1019 mtime_t delay = (p_data->i_pts - p_data->i_dts + 1)
1020 * p_input->p_fmt->video.i_frame_rate *2
1021 / p_input->p_fmt->video.i_frame_rate_base
1023 if( p_data->i_flags & BLOCK_FLAG_TYPE_I )
1024 p_stream->i_last_keyframe = dt;
1025 mtime_t dist = dt - p_stream->i_last_keyframe;
1026 op.granulepos = dt << 31 | (dist&0xff00) << 14
1027 | (delay&0x1fff) << 9 | (dist&0xff);
1029 else if( p_stream->p_oggds_header )
1030 op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) * INT64_C(10) /
1031 p_stream->p_oggds_header->i_time_unit;
1033 else if( p_stream->i_cat == SPU_ES )
1035 /* granulepos is in millisec */
1036 op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) / 1000;
1039 p_stream->u_last_granulepos = op.granulepos;
1040 ogg_stream_packetin( &p_stream->os, &op );
1042 if( p_stream->i_cat == SPU_ES ||
1043 p_stream->i_fourcc == VLC_CODEC_SPEEX ||
1044 p_stream->i_fourcc == VLC_CODEC_DIRAC )
1046 /* Subtitles or Speex packets are quite small so they
1047 * need to be flushed to be sent on time */
1048 /* The OggDirac mapping suggests ever so strongly that a
1049 * page flush occurs after each OggDirac packet, so to make
1050 * the timestamps unambiguous */
1051 p_og = OggStreamFlush( p_mux, &p_stream->os, p_data->i_dts );
1055 p_og = OggStreamPageOut( p_mux, &p_stream->os, p_data->i_dts );
1060 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
1061 p_stream->i_dts = -1;
1062 p_stream->i_length = 0;
1064 sout_AccessOutWrite( p_mux->p_access, p_og );
1068 if( p_stream->i_dts < 0 )
1070 p_stream->i_dts = p_data->i_dts;
1072 p_stream->i_length += p_data->i_length;
1075 block_Release( p_data );