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 eventually theora */
183 uint64_t u_last_granulepos; /* Used for correct EOS page */
186 oggds_header_t *p_oggds_header;
190 struct sout_mux_sys_t
195 int i_next_serial_no;
197 /* number of logical streams pending to be added */
200 /* logical streams pending to be deleted */
202 ogg_stream_t **pp_del_streams;
205 static void OggSetDate( block_t *, mtime_t , mtime_t );
206 static block_t *OggStreamFlush( sout_mux_t *, ogg_stream_state *, mtime_t );
208 /*****************************************************************************
210 *****************************************************************************/
211 static int Open( vlc_object_t *p_this )
213 sout_mux_t *p_mux = (sout_mux_t*)p_this;
214 sout_mux_sys_t *p_sys;
216 msg_Info( p_mux, "Open" );
218 p_sys = malloc( sizeof( sout_mux_sys_t ) );
221 p_sys->i_streams = 0;
222 p_sys->i_add_streams = 0;
223 p_sys->i_del_streams = 0;
224 p_sys->pp_del_streams = 0;
226 p_mux->p_sys = p_sys;
227 p_mux->pf_control = Control;
228 p_mux->pf_addstream = AddStream;
229 p_mux->pf_delstream = DelStream;
232 /* First serial number is random.
233 * (Done like this because on win32 you need to seed the random number
234 * generator once per thread). */
235 srand( (unsigned int)time( NULL ) );
236 p_sys->i_next_serial_no = rand();
241 /*****************************************************************************
242 * Close: Finalize ogg bitstream and close muxer
243 *****************************************************************************/
244 static void Close( vlc_object_t * p_this )
246 sout_mux_t *p_mux = (sout_mux_t*)p_this;
247 sout_mux_sys_t *p_sys = p_mux->p_sys;
249 msg_Info( p_mux, "Close" );
251 if( p_sys->i_del_streams )
253 block_t *p_og = NULL;
257 /* Close the current ogg stream */
258 msg_Dbg( p_mux, "writing footer" );
259 block_ChainAppend( &p_og, OggCreateFooter( p_mux ) );
261 /* Remove deleted logical streams */
262 for( i = 0; i < p_sys->i_del_streams; i++ )
264 i_dts = p_sys->pp_del_streams[i]->i_dts;
265 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
266 FREENULL( p_sys->pp_del_streams[i]->p_oggds_header );
267 FREENULL( p_sys->pp_del_streams[i] );
269 FREENULL( p_sys->pp_del_streams );
270 p_sys->i_streams -= p_sys->i_del_streams;
273 OggSetDate( p_og, i_dts, 0 );
274 sout_AccessOutWrite( p_mux->p_access, p_og );
280 /*****************************************************************************
282 *****************************************************************************/
283 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
291 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
292 pb_bool = (bool*)va_arg( args, bool * );
296 case MUX_GET_ADD_STREAM_WAIT:
297 pb_bool = (bool*)va_arg( args, bool * );
302 ppsz = (char**)va_arg( args, char ** );
303 *ppsz = strdup( "application/ogg" );
310 /*****************************************************************************
311 * AddStream: Add an elementary stream to the muxed stream
312 *****************************************************************************/
313 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
315 sout_mux_sys_t *p_sys = p_mux->p_sys;
316 ogg_stream_t *p_stream;
319 msg_Dbg( p_mux, "adding input" );
321 p_input->p_sys = p_stream = calloc( 1, sizeof( ogg_stream_t ) );
325 p_stream->i_cat = p_input->p_fmt->i_cat;
326 p_stream->i_fourcc = p_input->p_fmt->i_codec;
327 p_stream->i_serial_no = p_sys->i_next_serial_no++;
328 p_stream->i_packet_no = 0;
330 p_stream->p_oggds_header = 0;
332 switch( p_input->p_fmt->i_cat )
335 if( !p_input->p_fmt->video.i_frame_rate ||
336 !p_input->p_fmt->video.i_frame_rate_base )
338 msg_Warn( p_mux, "Missing frame rate, assuming 25fps" );
339 p_input->p_fmt->video.i_frame_rate = 25;
340 p_input->p_fmt->video.i_frame_rate_base = 1;
343 switch( p_stream->i_fourcc )
353 p_stream->p_oggds_header = calloc( 1, sizeof(oggds_header_t) );
354 if( !p_stream->p_oggds_header )
359 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
361 memcpy( p_stream->p_oggds_header->stream_type, "video", 5 );
362 if( p_stream->i_fourcc == VLC_CODEC_MP4V )
364 memcpy( p_stream->p_oggds_header->sub_type, "XVID", 4 );
366 else if( p_stream->i_fourcc == VLC_CODEC_DIV3 )
368 memcpy( p_stream->p_oggds_header->sub_type, "DIV3", 4 );
372 memcpy( p_stream->p_oggds_header->sub_type,
373 &p_stream->i_fourcc, 4 );
375 SetDWLE( &p_stream->p_oggds_header->i_size,
376 sizeof( oggds_header_t ) - 1 );
377 SetQWLE( &p_stream->p_oggds_header->i_time_unit,
378 INT64_C(10000000) * p_input->p_fmt->video.i_frame_rate_base /
379 (int64_t)p_input->p_fmt->video.i_frame_rate );
380 SetQWLE( &p_stream->p_oggds_header->i_samples_per_unit, 1 );
381 SetDWLE( &p_stream->p_oggds_header->i_default_len, 1 ); /* ??? */
382 SetDWLE( &p_stream->p_oggds_header->i_buffer_size, 1024*1024 );
383 SetWLE( &p_stream->p_oggds_header->i_bits_per_sample, 0 );
384 SetDWLE( &p_stream->p_oggds_header->header.video.i_width,
385 p_input->p_fmt->video.i_width );
386 SetDWLE( &p_stream->p_oggds_header->header.video.i_height,
387 p_input->p_fmt->video.i_height );
388 msg_Dbg( p_mux, "%4.4s stream", (char *)&p_stream->i_fourcc );
391 case VLC_CODEC_DIRAC:
392 msg_Dbg( p_mux, "dirac stream" );
395 case VLC_CODEC_THEORA:
396 msg_Dbg( p_mux, "theora stream" );
400 FREENULL( p_input->p_sys );
406 switch( p_stream->i_fourcc )
408 case VLC_CODEC_VORBIS:
409 msg_Dbg( p_mux, "vorbis stream" );
412 case VLC_CODEC_SPEEX:
413 msg_Dbg( p_mux, "speex stream" );
417 msg_Dbg( p_mux, "flac stream" );
421 fourcc_to_wf_tag( p_stream->i_fourcc, &i_tag );
422 if( i_tag == WAVE_FORMAT_UNKNOWN )
424 FREENULL( p_input->p_sys );
428 p_stream->p_oggds_header =
429 malloc( sizeof(oggds_header_t) + p_input->p_fmt->i_extra );
430 if( !p_stream->p_oggds_header )
435 memset( p_stream->p_oggds_header, 0, sizeof(oggds_header_t) );
436 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
438 SetDWLE( &p_stream->p_oggds_header->i_size,
439 sizeof( oggds_header_t ) - 1 + p_input->p_fmt->i_extra );
441 if( p_input->p_fmt->i_extra )
443 memcpy( &p_stream->p_oggds_header[1],
444 p_input->p_fmt->p_extra, p_input->p_fmt->i_extra );
447 memcpy( p_stream->p_oggds_header->stream_type, "audio", 5 );
449 memset( p_stream->p_oggds_header->sub_type, 0, 4 );
450 sprintf( p_stream->p_oggds_header->sub_type, "%-x", i_tag );
452 SetQWLE( &p_stream->p_oggds_header->i_time_unit, INT64_C(10000000) );
453 SetDWLE( &p_stream->p_oggds_header->i_default_len, 1 );
454 SetDWLE( &p_stream->p_oggds_header->i_buffer_size, 30*1024 );
455 SetQWLE( &p_stream->p_oggds_header->i_samples_per_unit,
456 p_input->p_fmt->audio.i_rate );
457 SetWLE( &p_stream->p_oggds_header->i_bits_per_sample,
458 p_input->p_fmt->audio.i_bitspersample );
459 SetDWLE( &p_stream->p_oggds_header->header.audio.i_channels,
460 p_input->p_fmt->audio.i_channels );
461 SetDWLE( &p_stream->p_oggds_header->header.audio.i_block_align,
462 p_input->p_fmt->audio.i_blockalign );
463 SetDWLE( &p_stream->p_oggds_header->header.audio.i_avgbytespersec,
464 p_input->p_fmt->i_bitrate / 8);
465 msg_Dbg( p_mux, "%4.4s stream", (char *)&p_stream->i_fourcc );
471 switch( p_stream->i_fourcc )
474 p_stream->p_oggds_header = calloc( 1, sizeof(oggds_header_t) );
475 if( !p_stream->p_oggds_header )
480 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
482 memcpy( p_stream->p_oggds_header->stream_type, "text", 4 );
483 msg_Dbg( p_mux, "subtitles stream" );
487 FREENULL( p_input->p_sys );
492 FREENULL( p_input->p_sys );
496 p_stream->b_new = true;
498 p_sys->i_add_streams++;
503 /*****************************************************************************
504 * DelStream: Delete an elementary stream from the muxed stream
505 *****************************************************************************/
506 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
508 sout_mux_sys_t *p_sys = p_mux->p_sys;
509 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
512 msg_Dbg( p_mux, "removing input" );
514 /* flush all remaining data */
517 if( !p_stream->b_new )
519 while( block_FifoCount( p_input->p_fifo ) )
520 MuxBlock( p_mux, p_input );
523 if( !p_stream->b_new &&
524 ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
526 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
527 sout_AccessOutWrite( p_mux->p_access, p_og );
530 /* move input in delete queue */
531 if( !p_stream->b_new )
533 p_sys->pp_del_streams = realloc( p_sys->pp_del_streams,
534 (p_sys->i_del_streams + 1) *
535 sizeof(ogg_stream_t *) );
536 p_sys->pp_del_streams[p_sys->i_del_streams++] = p_stream;
540 /* wasn't already added so get rid of it */
541 FREENULL( p_stream->p_oggds_header );
542 FREENULL( p_stream );
543 p_sys->i_add_streams--;
547 p_input->p_sys = NULL;
552 /*****************************************************************************
553 * Ogg bitstream manipulation routines
554 *****************************************************************************/
555 static block_t *OggStreamFlush( sout_mux_t *p_mux,
556 ogg_stream_state *p_os, mtime_t i_pts )
559 block_t *p_og, *p_og_first = NULL;
562 while( ogg_stream_flush( p_os, &og ) )
565 p_og = block_New( p_mux, og.header_len + og.body_len );
567 memcpy( p_og->p_buffer, og.header, og.header_len );
568 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
573 i_pts = 0; // write it only once
575 block_ChainAppend( &p_og_first, p_og );
581 static block_t *OggStreamPageOut( sout_mux_t *p_mux,
582 ogg_stream_state *p_os, mtime_t i_pts )
585 block_t *p_og, *p_og_first = NULL;
588 while( ogg_stream_pageout( p_os, &og ) )
591 p_og = block_New( p_mux, og.header_len + og.body_len );
593 memcpy( p_og->p_buffer, og.header, og.header_len );
594 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
599 i_pts = 0; // write them only once
601 block_ChainAppend( &p_og_first, p_og );
607 static block_t *OggCreateHeader( sout_mux_t *p_mux )
609 block_t *p_hdr = NULL;
610 block_t *p_og = NULL;
615 /* Write header for each stream. All b_o_s (beginning of stream) packets
616 * must appear first in the ogg stream so we take care of them first. */
617 for( i = 0; i < p_mux->i_nb_inputs; i++ )
619 sout_input_t *p_input = p_mux->pp_inputs[i];
620 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
621 p_stream->b_new = false;
623 msg_Dbg( p_mux, "creating header for %4.4s",
624 (char *)&p_stream->i_fourcc );
626 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
627 p_stream->i_packet_no = 0;
629 if( p_stream->i_fourcc == VLC_CODEC_VORBIS ||
630 p_stream->i_fourcc == VLC_CODEC_SPEEX ||
631 p_stream->i_fourcc == VLC_CODEC_THEORA )
633 /* First packet in order: vorbis/speex/theora info */
634 p_extra = p_input->p_fmt->p_extra;
635 i_extra = p_input->p_fmt->i_extra;
637 op.bytes = *(p_extra++) << 8;
638 op.bytes |= (*(p_extra++) & 0xFF);
640 i_extra -= (op.bytes + 2);
643 msg_Err( p_mux, "header data corrupted");
650 op.packetno = p_stream->i_packet_no++;
651 ogg_stream_packetin( &p_stream->os, &op );
652 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
654 /* Get keyframe_granule_shift for theora granulepos calculation */
655 if( p_stream->i_fourcc == VLC_CODEC_THEORA )
657 int i_keyframe_frequency_force =
658 1 << ((op.packet[40] << 6 >> 3) | (op.packet[41] >> 5));
660 /* granule_shift = i_log( frequency_force -1 ) */
661 p_stream->i_keyframe_granule_shift = 0;
662 i_keyframe_frequency_force--;
663 while( i_keyframe_frequency_force )
665 p_stream->i_keyframe_granule_shift++;
666 i_keyframe_frequency_force >>= 1;
670 else if( p_stream->i_fourcc == VLC_CODEC_DIRAC )
672 op.packet = p_input->p_fmt->p_extra;
673 op.bytes = p_input->p_fmt->i_extra;
677 op.packetno = p_stream->i_packet_no++;
678 ogg_stream_packetin( &p_stream->os, &op );
679 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
681 else if( p_stream->i_fourcc == VLC_CODEC_FLAC )
683 /* flac stream marker (yeah, only that in the 1st packet) */
684 op.packet = (unsigned char *)"fLaC";
689 op.packetno = p_stream->i_packet_no++;
690 ogg_stream_packetin( &p_stream->os, &op );
691 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
693 else if( p_stream->p_oggds_header )
696 op.packet = (uint8_t*)p_stream->p_oggds_header;
697 op.bytes = p_stream->p_oggds_header->i_size + 1;
701 op.packetno = p_stream->i_packet_no++;
702 ogg_stream_packetin( &p_stream->os, &op );
703 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
706 block_ChainAppend( &p_hdr, p_og );
709 /* Take care of the non b_o_s headers */
710 for( i = 0; i < p_mux->i_nb_inputs; i++ )
712 sout_input_t *p_input = p_mux->pp_inputs[i];
713 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
715 if( p_stream->i_fourcc == VLC_CODEC_VORBIS ||
716 p_stream->i_fourcc == VLC_CODEC_SPEEX ||
717 p_stream->i_fourcc == VLC_CODEC_THEORA )
719 /* Special case, headers are already there in the incoming stream.
720 * We need to gather them an mark them as headers. */
723 if( p_stream->i_fourcc == VLC_CODEC_SPEEX ) j = 1;
725 p_extra = p_input->p_fmt->p_extra;
726 i_extra = p_input->p_fmt->i_extra;
729 op.bytes = *(p_extra++) << 8;
730 op.bytes |= (*(p_extra++) & 0xFF);
733 i_extra -= (op.bytes + 2);
737 op.bytes = *(p_extra++) << 8;
738 op.bytes |= (*(p_extra++) & 0xFF);
741 i_extra -= (op.bytes + 2);
744 msg_Err( p_mux, "header data corrupted");
751 op.packetno = p_stream->i_packet_no++;
752 ogg_stream_packetin( &p_stream->os, &op );
754 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
755 block_ChainAppend( &p_hdr, p_og );
758 else if( p_stream->i_fourcc != VLC_CODEC_FLAC &&
759 p_stream->i_fourcc != VLC_CODEC_DIRAC )
765 com[0] = PACKET_TYPE_COMMENT;
766 i_com = snprintf( (char *)(com+1), 127,
767 PACKAGE_VERSION" stream output" )
774 op.packetno = p_stream->i_packet_no++;
775 ogg_stream_packetin( &p_stream->os, &op );
776 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
777 block_ChainAppend( &p_hdr, p_og );
780 /* Special case for mp4v and flac */
781 if( ( p_stream->i_fourcc == VLC_CODEC_MP4V ||
782 p_stream->i_fourcc == VLC_CODEC_FLAC ) &&
783 p_input->p_fmt->i_extra )
785 /* Send a packet with the VOL data for mp4v
786 * or STREAMINFO for flac */
787 msg_Dbg( p_mux, "writing extra data" );
788 op.bytes = p_input->p_fmt->i_extra;
789 op.packet = p_input->p_fmt->p_extra;
790 if( p_stream->i_fourcc == VLC_CODEC_FLAC )
792 /* Skip the flac stream marker */
799 op.packetno = p_stream->i_packet_no++;
800 ogg_stream_packetin( &p_stream->os, &op );
801 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
802 block_ChainAppend( &p_hdr, p_og );
806 /* set HEADER flag */
807 for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
809 p_og->i_flags |= BLOCK_FLAG_HEADER;
814 static block_t *OggCreateFooter( sout_mux_t *p_mux )
816 sout_mux_sys_t *p_sys = p_mux->p_sys;
817 block_t *p_hdr = NULL;
822 /* flush all remaining data */
823 for( i = 0; i < p_mux->i_nb_inputs; i++ )
825 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
827 /* skip newly added streams */
828 if( p_stream->b_new ) continue;
830 if( ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
832 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
833 sout_AccessOutWrite( p_mux->p_access, p_og );
837 /* Write eos packets for each stream. */
838 for( i = 0; i < p_mux->i_nb_inputs; i++ )
840 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
842 /* skip newly added streams */
843 if( p_stream->b_new ) continue;
849 op.granulepos = p_stream->u_last_granulepos;
850 op.packetno = p_stream->i_packet_no++;
851 ogg_stream_packetin( &p_stream->os, &op );
853 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
854 block_ChainAppend( &p_hdr, p_og );
855 ogg_stream_clear( &p_stream->os );
858 for( i = 0; i < p_sys->i_del_streams; i++ )
864 op.granulepos = p_sys->pp_del_streams[i]->u_last_granulepos;
865 op.packetno = p_sys->pp_del_streams[i]->i_packet_no++;
866 ogg_stream_packetin( &p_sys->pp_del_streams[i]->os, &op );
868 p_og = OggStreamFlush( p_mux, &p_sys->pp_del_streams[i]->os, 0 );
869 block_ChainAppend( &p_hdr, p_og );
870 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
876 static void OggSetDate( block_t *p_og, mtime_t i_dts, mtime_t i_length )
882 for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
887 if( i_count == 0 ) return; /* ignore. */
889 i_delta = i_length / i_count;
891 for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
893 p_tmp->i_dts = i_dts;
894 p_tmp->i_length = i_delta;
900 /*****************************************************************************
901 * Mux: multiplex available data in input fifos into the Ogg bitstream
902 *****************************************************************************/
903 static int Mux( sout_mux_t *p_mux )
905 sout_mux_sys_t *p_sys = p_mux->p_sys;
906 block_t *p_og = NULL;
910 if( p_sys->i_add_streams || p_sys->i_del_streams )
912 /* Open new ogg stream */
913 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
915 msg_Dbg( p_mux, "waiting for data..." );
919 if( p_sys->i_streams )
921 /* Close current ogg stream */
924 msg_Dbg( p_mux, "writing footer" );
925 block_ChainAppend( &p_og, OggCreateFooter( p_mux ) );
927 /* Remove deleted logical streams */
928 for( i = 0; i < p_sys->i_del_streams; i++ )
930 FREENULL( p_sys->pp_del_streams[i]->p_oggds_header );
931 FREENULL( p_sys->pp_del_streams[i] );
933 FREENULL( p_sys->pp_del_streams );
934 p_sys->i_streams = 0;
937 msg_Dbg( p_mux, "writing header" );
938 p_sys->i_start_dts = i_dts;
939 p_sys->i_streams = p_mux->i_nb_inputs;
940 p_sys->i_del_streams = 0;
941 p_sys->i_add_streams = 0;
942 block_ChainAppend( &p_og, OggCreateHeader( p_mux ) );
944 /* Write header and/or footer */
945 OggSetDate( p_og, i_dts, 0 );
946 sout_AccessOutWrite( p_mux->p_access, p_og );
952 if( MuxGetStream( p_mux, &i_stream, 0 ) < 0 ) return VLC_SUCCESS;
953 MuxBlock( p_mux, p_mux->pp_inputs[i_stream] );
959 static int MuxBlock( sout_mux_t *p_mux, sout_input_t *p_input )
961 sout_mux_sys_t *p_sys = p_mux->p_sys;
962 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
963 block_t *p_data = block_FifoGet( p_input->p_fifo );
964 block_t *p_og = NULL;
967 if( p_stream->i_fourcc != VLC_CODEC_VORBIS &&
968 p_stream->i_fourcc != VLC_CODEC_FLAC &&
969 p_stream->i_fourcc != VLC_CODEC_SPEEX &&
970 p_stream->i_fourcc != VLC_CODEC_THEORA &&
971 p_stream->i_fourcc != VLC_CODEC_DIRAC )
973 p_data = block_Realloc( p_data, 1, p_data->i_buffer );
974 p_data->p_buffer[0] = PACKET_IS_SYNCPOINT; // FIXME
977 op.packet = p_data->p_buffer;
978 op.bytes = p_data->i_buffer;
981 op.packetno = p_stream->i_packet_no++;
983 if( p_stream->i_cat == AUDIO_ES )
985 if( p_stream->i_fourcc == VLC_CODEC_VORBIS ||
986 p_stream->i_fourcc == VLC_CODEC_FLAC ||
987 p_stream->i_fourcc == VLC_CODEC_SPEEX )
989 /* number of sample from begining + current packet */
991 ( p_data->i_dts - p_sys->i_start_dts + p_data->i_length ) *
992 (mtime_t)p_input->p_fmt->audio.i_rate / INT64_C(1000000);
994 else if( p_stream->p_oggds_header )
996 /* number of sample from begining */
997 op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) *
998 p_stream->p_oggds_header->i_samples_per_unit / INT64_C(1000000);
1001 else if( p_stream->i_cat == VIDEO_ES )
1003 if( p_stream->i_fourcc == VLC_CODEC_THEORA )
1005 /* FIXME, we assume only keyframes */
1006 op.granulepos = ( ( p_data->i_dts - p_sys->i_start_dts ) *
1007 p_input->p_fmt->video.i_frame_rate /
1008 p_input->p_fmt->video.i_frame_rate_base /
1009 INT64_C(1000000) ) << p_stream->i_keyframe_granule_shift;
1011 else if( p_stream->i_fourcc == VLC_CODEC_DIRAC )
1013 mtime_t dt = (p_data->i_dts - p_sys->i_start_dts + 1)
1014 * p_input->p_fmt->video.i_frame_rate *2
1015 / p_input->p_fmt->video.i_frame_rate_base
1017 mtime_t delay = (p_data->i_pts - p_data->i_dts + 1)
1018 * p_input->p_fmt->video.i_frame_rate *2
1019 / p_input->p_fmt->video.i_frame_rate_base
1021 if( p_data->i_flags & BLOCK_FLAG_TYPE_I )
1022 p_stream->i_last_keyframe = dt;
1023 mtime_t dist = dt - p_stream->i_last_keyframe;
1024 op.granulepos = dt << 31 | (dist&0xff00) << 14
1025 | (delay&0x1fff) << 9 | (dist&0xff);
1027 else if( p_stream->p_oggds_header )
1028 op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) * INT64_C(10) /
1029 p_stream->p_oggds_header->i_time_unit;
1031 else if( p_stream->i_cat == SPU_ES )
1033 /* granulepos is in millisec */
1034 op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) / 1000;
1037 p_stream->u_last_granulepos = op.granulepos;
1038 ogg_stream_packetin( &p_stream->os, &op );
1040 if( p_stream->i_cat == SPU_ES ||
1041 p_stream->i_fourcc == VLC_CODEC_SPEEX ||
1042 p_stream->i_fourcc == VLC_CODEC_DIRAC )
1044 /* Subtitles or Speex packets are quite small so they
1045 * need to be flushed to be sent on time */
1046 /* The OggDirac mapping suggests ever so strongly that a
1047 * page flush occurs after each OggDirac packet, so to make
1048 * the timestamps unambiguous */
1049 p_og = OggStreamFlush( p_mux, &p_stream->os, p_data->i_dts );
1053 p_og = OggStreamPageOut( p_mux, &p_stream->os, p_data->i_dts );
1058 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
1059 p_stream->i_dts = -1;
1060 p_stream->i_length = 0;
1062 sout_AccessOutWrite( p_mux->p_access, p_og );
1066 if( p_stream->i_dts < 0 )
1068 p_stream->i_dts = p_data->i_dts;
1070 p_stream->i_length += p_data->i_length;
1073 block_Release( p_data );