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>
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Open ( vlc_object_t * );
45 static void Close ( vlc_object_t * );
48 set_description( N_("Ogg/OGM muxer") )
49 set_capability( "sout mux", 10 )
50 set_category( CAT_SOUT )
51 set_subcategory( SUBCAT_SOUT_MUX )
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 * );
65 static int MuxBlock ( sout_mux_t *, sout_input_t * );
67 static block_t *OggCreateHeader( sout_mux_t * );
68 static block_t *OggCreateFooter( sout_mux_t * );
70 /*****************************************************************************
72 *****************************************************************************/
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 )
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 */
182 int i_last_keyframe; /* dirac and theora */
183 int i_num_frames; /* Theora only */
184 uint64_t u_last_granulepos; /* Used for correct EOS page */
185 int64_t i_num_keyframes;
188 oggds_header_t *p_oggds_header;
192 struct sout_mux_sys_t
197 int i_next_serial_no;
199 /* number of logical streams pending to be added */
202 /* logical streams pending to be deleted */
204 ogg_stream_t **pp_del_streams;
207 static void OggSetDate( block_t *, mtime_t , mtime_t );
208 static block_t *OggStreamFlush( sout_mux_t *, ogg_stream_state *, mtime_t );
210 /*****************************************************************************
212 *****************************************************************************/
213 static int Open( vlc_object_t *p_this )
215 sout_mux_t *p_mux = (sout_mux_t*)p_this;
216 sout_mux_sys_t *p_sys;
218 msg_Info( p_mux, "Open" );
220 p_sys = malloc( sizeof( sout_mux_sys_t ) );
223 p_sys->i_streams = 0;
224 p_sys->i_add_streams = 0;
225 p_sys->i_del_streams = 0;
226 p_sys->pp_del_streams = 0;
228 p_mux->p_sys = p_sys;
229 p_mux->pf_control = Control;
230 p_mux->pf_addstream = AddStream;
231 p_mux->pf_delstream = DelStream;
234 /* First serial number is random.
235 * (Done like this because on win32 you need to seed the random number
236 * generator once per thread). */
237 srand( (unsigned int)time( NULL ) );
238 p_sys->i_next_serial_no = rand();
243 /*****************************************************************************
244 * Close: Finalize ogg bitstream and close muxer
245 *****************************************************************************/
246 static void Close( vlc_object_t * p_this )
248 sout_mux_t *p_mux = (sout_mux_t*)p_this;
249 sout_mux_sys_t *p_sys = p_mux->p_sys;
251 msg_Info( p_mux, "Close" );
253 if( p_sys->i_del_streams )
255 block_t *p_og = NULL;
259 /* Close the current ogg stream */
260 msg_Dbg( p_mux, "writing footer" );
261 block_ChainAppend( &p_og, OggCreateFooter( p_mux ) );
263 /* Remove deleted logical streams */
264 for( i = 0; i < p_sys->i_del_streams; i++ )
266 i_dts = p_sys->pp_del_streams[i]->i_dts;
267 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
268 FREENULL( p_sys->pp_del_streams[i]->p_oggds_header );
269 FREENULL( p_sys->pp_del_streams[i] );
271 FREENULL( p_sys->pp_del_streams );
272 p_sys->i_streams -= p_sys->i_del_streams;
275 OggSetDate( p_og, i_dts, 0 );
276 sout_AccessOutWrite( p_mux->p_access, p_og );
282 /*****************************************************************************
284 *****************************************************************************/
285 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 = (bool*)va_arg( args, bool * );
298 case MUX_GET_ADD_STREAM_WAIT:
299 pb_bool = (bool*)va_arg( args, bool * );
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 = calloc( 1, sizeof( ogg_stream_t ) );
327 p_stream->i_cat = p_input->p_fmt->i_cat;
328 p_stream->i_fourcc = p_input->p_fmt->i_codec;
329 p_stream->i_serial_no = p_sys->i_next_serial_no++;
330 p_stream->i_packet_no = 0;
331 p_stream->i_last_keyframe = 0;
332 p_stream->i_num_keyframes = 0;
333 p_stream->i_num_frames = 0;
335 p_stream->p_oggds_header = 0;
337 switch( p_input->p_fmt->i_cat )
340 if( !p_input->p_fmt->video.i_frame_rate ||
341 !p_input->p_fmt->video.i_frame_rate_base )
343 msg_Warn( p_mux, "Missing frame rate, assuming 25fps" );
344 p_input->p_fmt->video.i_frame_rate = 25;
345 p_input->p_fmt->video.i_frame_rate_base = 1;
348 switch( p_stream->i_fourcc )
358 p_stream->p_oggds_header = calloc( 1, sizeof(oggds_header_t) );
359 if( !p_stream->p_oggds_header )
364 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
366 memcpy( p_stream->p_oggds_header->stream_type, "video", 5 );
367 if( p_stream->i_fourcc == VLC_CODEC_MP4V )
369 memcpy( p_stream->p_oggds_header->sub_type, "XVID", 4 );
371 else if( p_stream->i_fourcc == VLC_CODEC_DIV3 )
373 memcpy( p_stream->p_oggds_header->sub_type, "DIV3", 4 );
377 memcpy( p_stream->p_oggds_header->sub_type,
378 &p_stream->i_fourcc, 4 );
380 SetDWLE( &p_stream->p_oggds_header->i_size,
381 sizeof( oggds_header_t ) - 1 );
382 SetQWLE( &p_stream->p_oggds_header->i_time_unit,
383 INT64_C(10000000) * p_input->p_fmt->video.i_frame_rate_base /
384 (int64_t)p_input->p_fmt->video.i_frame_rate );
385 SetQWLE( &p_stream->p_oggds_header->i_samples_per_unit, 1 );
386 SetDWLE( &p_stream->p_oggds_header->i_default_len, 1 ); /* ??? */
387 SetDWLE( &p_stream->p_oggds_header->i_buffer_size, 1024*1024 );
388 SetWLE( &p_stream->p_oggds_header->i_bits_per_sample, 0 );
389 SetDWLE( &p_stream->p_oggds_header->header.video.i_width,
390 p_input->p_fmt->video.i_width );
391 SetDWLE( &p_stream->p_oggds_header->header.video.i_height,
392 p_input->p_fmt->video.i_height );
393 msg_Dbg( p_mux, "%4.4s stream", (char *)&p_stream->i_fourcc );
396 case VLC_CODEC_DIRAC:
397 msg_Dbg( p_mux, "dirac stream" );
400 case VLC_CODEC_THEORA:
401 msg_Dbg( p_mux, "theora stream" );
405 FREENULL( p_input->p_sys );
411 switch( p_stream->i_fourcc )
413 case VLC_CODEC_VORBIS:
414 msg_Dbg( p_mux, "vorbis stream" );
417 case VLC_CODEC_SPEEX:
418 msg_Dbg( p_mux, "speex stream" );
422 msg_Dbg( p_mux, "flac stream" );
426 fourcc_to_wf_tag( p_stream->i_fourcc, &i_tag );
427 if( i_tag == WAVE_FORMAT_UNKNOWN )
429 FREENULL( p_input->p_sys );
433 p_stream->p_oggds_header =
434 malloc( sizeof(oggds_header_t) + p_input->p_fmt->i_extra );
435 if( !p_stream->p_oggds_header )
440 memset( p_stream->p_oggds_header, 0, sizeof(oggds_header_t) );
441 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
443 SetDWLE( &p_stream->p_oggds_header->i_size,
444 sizeof( oggds_header_t ) - 1 + p_input->p_fmt->i_extra );
446 if( p_input->p_fmt->i_extra )
448 memcpy( &p_stream->p_oggds_header[1],
449 p_input->p_fmt->p_extra, p_input->p_fmt->i_extra );
452 memcpy( p_stream->p_oggds_header->stream_type, "audio", 5 );
454 memset( p_stream->p_oggds_header->sub_type, 0, 4 );
455 sprintf( p_stream->p_oggds_header->sub_type, "%-x", i_tag );
457 SetQWLE( &p_stream->p_oggds_header->i_time_unit, INT64_C(10000000) );
458 SetDWLE( &p_stream->p_oggds_header->i_default_len, 1 );
459 SetDWLE( &p_stream->p_oggds_header->i_buffer_size, 30*1024 );
460 SetQWLE( &p_stream->p_oggds_header->i_samples_per_unit,
461 p_input->p_fmt->audio.i_rate );
462 SetWLE( &p_stream->p_oggds_header->i_bits_per_sample,
463 p_input->p_fmt->audio.i_bitspersample );
464 SetDWLE( &p_stream->p_oggds_header->header.audio.i_channels,
465 p_input->p_fmt->audio.i_channels );
466 SetDWLE( &p_stream->p_oggds_header->header.audio.i_block_align,
467 p_input->p_fmt->audio.i_blockalign );
468 SetDWLE( &p_stream->p_oggds_header->header.audio.i_avgbytespersec,
469 p_input->p_fmt->i_bitrate / 8);
470 msg_Dbg( p_mux, "%4.4s stream", (char *)&p_stream->i_fourcc );
476 switch( p_stream->i_fourcc )
479 p_stream->p_oggds_header = calloc( 1, sizeof(oggds_header_t) );
480 if( !p_stream->p_oggds_header )
485 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
487 memcpy( p_stream->p_oggds_header->stream_type, "text", 4 );
488 msg_Dbg( p_mux, "subtitles stream" );
492 FREENULL( p_input->p_sys );
497 FREENULL( p_input->p_sys );
501 p_stream->b_new = true;
503 p_sys->i_add_streams++;
508 /*****************************************************************************
509 * DelStream: Delete an elementary stream from the muxed stream
510 *****************************************************************************/
511 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
513 sout_mux_sys_t *p_sys = p_mux->p_sys;
514 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
517 msg_Dbg( p_mux, "removing input" );
519 /* flush all remaining data */
522 if( !p_stream->b_new )
524 while( block_FifoCount( p_input->p_fifo ) )
525 MuxBlock( p_mux, p_input );
528 if( !p_stream->b_new &&
529 ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
531 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
532 sout_AccessOutWrite( p_mux->p_access, p_og );
535 /* move input in delete queue */
536 if( !p_stream->b_new )
538 p_sys->pp_del_streams = realloc( p_sys->pp_del_streams,
539 (p_sys->i_del_streams + 1) *
540 sizeof(ogg_stream_t *) );
541 p_sys->pp_del_streams[p_sys->i_del_streams++] = p_stream;
545 /* wasn't already added so get rid of it */
546 FREENULL( p_stream->p_oggds_header );
547 FREENULL( p_stream );
548 p_sys->i_add_streams--;
552 p_input->p_sys = NULL;
557 /*****************************************************************************
558 * Ogg bitstream manipulation routines
559 *****************************************************************************/
560 static block_t *OggStreamGetPage( sout_mux_t *p_mux,
561 ogg_stream_state *p_os, mtime_t i_pts,
565 block_t *p_og, *p_og_first = NULL;
567 int (*pager)( ogg_stream_state*, ogg_page* ) = flush ? ogg_stream_flush : ogg_stream_pageout;
569 while( pager( p_os, &og ) )
572 p_og = block_New( p_mux, og.header_len + og.body_len );
574 memcpy( p_og->p_buffer, og.header, og.header_len );
575 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
580 i_pts = 0; // write it only once
582 block_ChainAppend( &p_og_first, p_og );
588 static block_t *OggStreamFlush( sout_mux_t *p_mux,
589 ogg_stream_state *p_os, mtime_t i_pts )
591 return OggStreamGetPage( p_mux, p_os, i_pts, true );
594 static block_t *OggStreamPageOut( sout_mux_t *p_mux,
595 ogg_stream_state *p_os, mtime_t i_pts )
597 return OggStreamGetPage( p_mux, p_os, i_pts, false );
600 static block_t *OggCreateHeader( sout_mux_t *p_mux )
602 block_t *p_hdr = NULL;
603 block_t *p_og = NULL;
608 /* Write header for each stream. All b_o_s (beginning of stream) packets
609 * must appear first in the ogg stream so we take care of them first. */
610 for( int pass = 0; pass < 2; pass++ )
612 for( i = 0; i < p_mux->i_nb_inputs; i++ )
614 sout_input_t *p_input = p_mux->pp_inputs[i];
615 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
617 bool video = ( p_stream->i_fourcc == VLC_CODEC_THEORA || p_stream->i_fourcc == VLC_CODEC_DIRAC );
618 if( ( ( pass == 0 && !video ) || ( pass == 1 && video ) ) )
621 msg_Dbg( p_mux, "creating header for %4.4s",
622 (char *)&p_stream->i_fourcc );
624 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
625 p_stream->b_new = false;
626 p_stream->i_packet_no = 0;
628 if( p_stream->i_fourcc == VLC_CODEC_VORBIS ||
629 p_stream->i_fourcc == VLC_CODEC_SPEEX ||
630 p_stream->i_fourcc == VLC_CODEC_THEORA )
632 /* First packet in order: vorbis/speex/theora info */
633 p_extra = p_input->p_fmt->p_extra;
634 i_extra = p_input->p_fmt->i_extra;
636 op.bytes = *(p_extra++) << 8;
637 op.bytes |= (*(p_extra++) & 0xFF);
639 i_extra -= (op.bytes + 2);
642 msg_Err( p_mux, "header data corrupted");
649 op.packetno = p_stream->i_packet_no++;
650 ogg_stream_packetin( &p_stream->os, &op );
651 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
653 /* Get keyframe_granule_shift for theora granulepos calculation */
654 if( p_stream->i_fourcc == VLC_CODEC_THEORA )
656 p_stream->i_keyframe_granule_shift =
657 ( (op.packet[40] & 0x03) << 3 ) | ( (op.packet[41] & 0xe0) >> 5 );
660 else if( p_stream->i_fourcc == VLC_CODEC_DIRAC )
662 op.packet = p_input->p_fmt->p_extra;
663 op.bytes = p_input->p_fmt->i_extra;
667 op.packetno = p_stream->i_packet_no++;
668 ogg_stream_packetin( &p_stream->os, &op );
669 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
671 else if( p_stream->i_fourcc == VLC_CODEC_FLAC )
673 /* flac stream marker (yeah, only that in the 1st packet) */
674 op.packet = (unsigned char *)"fLaC";
679 op.packetno = p_stream->i_packet_no++;
680 ogg_stream_packetin( &p_stream->os, &op );
681 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
683 else if( p_stream->p_oggds_header )
686 op.packet = (uint8_t*)p_stream->p_oggds_header;
687 op.bytes = p_stream->p_oggds_header->i_size + 1;
691 op.packetno = p_stream->i_packet_no++;
692 ogg_stream_packetin( &p_stream->os, &op );
693 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
696 block_ChainAppend( &p_hdr, p_og );
700 /* Take care of the non b_o_s headers */
701 for( i = 0; i < p_mux->i_nb_inputs; i++ )
703 sout_input_t *p_input = p_mux->pp_inputs[i];
704 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
706 if( p_stream->i_fourcc == VLC_CODEC_VORBIS ||
707 p_stream->i_fourcc == VLC_CODEC_SPEEX ||
708 p_stream->i_fourcc == VLC_CODEC_THEORA )
710 /* Special case, headers are already there in the incoming stream.
711 * We need to gather them an mark them as headers. */
714 if( p_stream->i_fourcc == VLC_CODEC_SPEEX ) j = 1;
716 p_extra = p_input->p_fmt->p_extra;
717 i_extra = p_input->p_fmt->i_extra;
720 op.bytes = *(p_extra++) << 8;
721 op.bytes |= (*(p_extra++) & 0xFF);
724 i_extra -= (op.bytes + 2);
728 op.bytes = *(p_extra++) << 8;
729 op.bytes |= (*(p_extra++) & 0xFF);
732 i_extra -= (op.bytes + 2);
735 msg_Err( p_mux, "header data corrupted");
742 op.packetno = p_stream->i_packet_no++;
743 ogg_stream_packetin( &p_stream->os, &op );
746 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
748 p_og = OggStreamPageOut( p_mux, &p_stream->os, 0 );
750 block_ChainAppend( &p_hdr, p_og );
753 else if( p_stream->i_fourcc != VLC_CODEC_FLAC &&
754 p_stream->i_fourcc != VLC_CODEC_DIRAC )
760 com[0] = PACKET_TYPE_COMMENT;
761 i_com = snprintf( (char *)(com+1), 127,
762 PACKAGE_VERSION" stream output" )
769 op.packetno = p_stream->i_packet_no++;
770 ogg_stream_packetin( &p_stream->os, &op );
771 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
772 block_ChainAppend( &p_hdr, p_og );
775 /* Special case for mp4v and flac */
776 if( ( p_stream->i_fourcc == VLC_CODEC_MP4V ||
777 p_stream->i_fourcc == VLC_CODEC_FLAC ) &&
778 p_input->p_fmt->i_extra )
780 /* Send a packet with the VOL data for mp4v
781 * or STREAMINFO for flac */
782 msg_Dbg( p_mux, "writing extra data" );
783 op.bytes = p_input->p_fmt->i_extra;
784 op.packet = p_input->p_fmt->p_extra;
785 if( p_stream->i_fourcc == VLC_CODEC_FLAC )
787 /* Skip the flac stream marker */
794 op.packetno = p_stream->i_packet_no++;
795 ogg_stream_packetin( &p_stream->os, &op );
796 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
797 block_ChainAppend( &p_hdr, p_og );
801 /* set HEADER flag */
802 for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
804 p_og->i_flags |= BLOCK_FLAG_HEADER;
809 static block_t *OggCreateFooter( sout_mux_t *p_mux )
811 sout_mux_sys_t *p_sys = p_mux->p_sys;
812 block_t *p_hdr = NULL;
817 /* flush all remaining data */
818 for( i = 0; i < p_mux->i_nb_inputs; i++ )
820 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
822 /* skip newly added streams */
823 if( p_stream->b_new ) continue;
825 if( ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
827 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
828 sout_AccessOutWrite( p_mux->p_access, p_og );
832 /* Write eos packets for each stream. */
833 for( i = 0; i < p_mux->i_nb_inputs; i++ )
835 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
837 /* skip newly added streams */
838 if( p_stream->b_new ) continue;
844 op.granulepos = p_stream->u_last_granulepos;
845 op.packetno = p_stream->i_packet_no++;
846 ogg_stream_packetin( &p_stream->os, &op );
848 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
849 block_ChainAppend( &p_hdr, p_og );
850 ogg_stream_clear( &p_stream->os );
853 for( i = 0; i < p_sys->i_del_streams; i++ )
859 op.granulepos = p_sys->pp_del_streams[i]->u_last_granulepos;
860 op.packetno = p_sys->pp_del_streams[i]->i_packet_no++;
861 ogg_stream_packetin( &p_sys->pp_del_streams[i]->os, &op );
863 p_og = OggStreamFlush( p_mux, &p_sys->pp_del_streams[i]->os, 0 );
864 block_ChainAppend( &p_hdr, p_og );
865 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
871 static void OggSetDate( block_t *p_og, mtime_t i_dts, mtime_t i_length )
877 for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
882 if( i_count == 0 ) return; /* ignore. */
884 i_delta = i_length / i_count;
886 for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
888 p_tmp->i_dts = i_dts;
889 p_tmp->i_length = i_delta;
895 /*****************************************************************************
896 * Mux: multiplex available data in input fifos into the Ogg bitstream
897 *****************************************************************************/
898 static int Mux( sout_mux_t *p_mux )
900 sout_mux_sys_t *p_sys = p_mux->p_sys;
901 block_t *p_og = NULL;
905 if( p_sys->i_add_streams || p_sys->i_del_streams )
907 /* Open new ogg stream */
908 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
910 msg_Dbg( p_mux, "waiting for data..." );
914 if( p_sys->i_streams )
916 /* Close current ogg stream */
919 msg_Dbg( p_mux, "writing footer" );
920 block_ChainAppend( &p_og, OggCreateFooter( p_mux ) );
922 /* Remove deleted logical streams */
923 for( i = 0; i < p_sys->i_del_streams; i++ )
925 FREENULL( p_sys->pp_del_streams[i]->p_oggds_header );
926 FREENULL( p_sys->pp_del_streams[i] );
928 FREENULL( p_sys->pp_del_streams );
929 p_sys->i_streams = 0;
932 msg_Dbg( p_mux, "writing header" );
933 p_sys->i_start_dts = i_dts;
934 p_sys->i_streams = p_mux->i_nb_inputs;
935 p_sys->i_del_streams = 0;
936 p_sys->i_add_streams = 0;
937 block_ChainAppend( &p_og, OggCreateHeader( p_mux ) );
939 /* Write header and/or footer */
940 OggSetDate( p_og, i_dts, 0 );
941 sout_AccessOutWrite( p_mux->p_access, p_og );
947 if( MuxGetStream( p_mux, &i_stream, 0 ) < 0 ) return VLC_SUCCESS;
948 MuxBlock( p_mux, p_mux->pp_inputs[i_stream] );
954 static int MuxBlock( sout_mux_t *p_mux, sout_input_t *p_input )
956 sout_mux_sys_t *p_sys = p_mux->p_sys;
957 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
958 block_t *p_data = block_FifoGet( p_input->p_fifo );
959 block_t *p_og = NULL;
962 if( p_stream->i_fourcc != VLC_CODEC_VORBIS &&
963 p_stream->i_fourcc != VLC_CODEC_FLAC &&
964 p_stream->i_fourcc != VLC_CODEC_SPEEX &&
965 p_stream->i_fourcc != VLC_CODEC_THEORA &&
966 p_stream->i_fourcc != VLC_CODEC_DIRAC )
968 p_data = block_Realloc( p_data, 1, p_data->i_buffer );
969 p_data->p_buffer[0] = PACKET_IS_SYNCPOINT; // FIXME
972 op.packet = p_data->p_buffer;
973 op.bytes = p_data->i_buffer;
976 op.packetno = p_stream->i_packet_no++;
978 if( p_stream->i_cat == AUDIO_ES )
980 if( p_stream->i_fourcc == VLC_CODEC_VORBIS ||
981 p_stream->i_fourcc == VLC_CODEC_FLAC ||
982 p_stream->i_fourcc == VLC_CODEC_SPEEX )
984 /* number of sample from begining + current packet */
986 ( p_data->i_dts - p_sys->i_start_dts + p_data->i_length ) *
987 (mtime_t)p_input->p_fmt->audio.i_rate / INT64_C(1000000);
989 else if( p_stream->p_oggds_header )
991 /* number of sample from begining */
992 op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) *
993 p_stream->p_oggds_header->i_samples_per_unit / INT64_C(1000000);
996 else if( p_stream->i_cat == VIDEO_ES )
998 if( p_stream->i_fourcc == VLC_CODEC_THEORA )
1000 p_stream->i_num_frames++;
1001 if( p_data->i_flags & BLOCK_FLAG_TYPE_I )
1003 p_stream->i_num_keyframes++;
1004 p_stream->i_last_keyframe = p_stream->i_num_frames;
1007 op.granulepos = (p_stream->i_last_keyframe << p_stream->i_keyframe_granule_shift )
1008 | (p_stream->i_num_frames-p_stream->i_last_keyframe);
1010 else if( p_stream->i_fourcc == VLC_CODEC_DIRAC )
1012 mtime_t dt = (p_data->i_dts - p_sys->i_start_dts + 1)
1013 * p_input->p_fmt->video.i_frame_rate *2
1014 / p_input->p_fmt->video.i_frame_rate_base
1016 mtime_t delay = (p_data->i_pts - p_data->i_dts + 1)
1017 * p_input->p_fmt->video.i_frame_rate *2
1018 / p_input->p_fmt->video.i_frame_rate_base
1020 if( p_data->i_flags & BLOCK_FLAG_TYPE_I )
1021 p_stream->i_last_keyframe = dt;
1022 mtime_t dist = dt - p_stream->i_last_keyframe;
1023 op.granulepos = dt << 31 | (dist&0xff00) << 14
1024 | (delay&0x1fff) << 9 | (dist&0xff);
1026 else if( p_stream->p_oggds_header )
1027 op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) * INT64_C(10) /
1028 p_stream->p_oggds_header->i_time_unit;
1030 else if( p_stream->i_cat == SPU_ES )
1032 /* granulepos is in millisec */
1033 op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) / 1000;
1036 p_stream->u_last_granulepos = op.granulepos;
1037 ogg_stream_packetin( &p_stream->os, &op );
1039 if( p_stream->i_cat == SPU_ES ||
1040 p_stream->i_fourcc == VLC_CODEC_SPEEX ||
1041 p_stream->i_fourcc == VLC_CODEC_DIRAC )
1043 /* Subtitles or Speex packets are quite small so they
1044 * need to be flushed to be sent on time */
1045 /* The OggDirac mapping suggests ever so strongly that a
1046 * page flush occurs after each OggDirac packet, so to make
1047 * the timestamps unambiguous */
1048 p_og = OggStreamFlush( p_mux, &p_stream->os, p_data->i_dts );
1052 p_og = OggStreamPageOut( p_mux, &p_stream->os, p_data->i_dts );
1057 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
1058 p_stream->i_dts = -1;
1059 p_stream->i_length = 0;
1061 sout_AccessOutWrite( p_mux->p_access, p_og );
1065 if( p_stream->i_dts < 0 )
1067 p_stream->i_dts = p_data->i_dts;
1069 p_stream->i_length += p_data->i_length;
1072 block_Release( p_data );