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 *****************************************************************************/
37 #include <vlc_block.h>
38 #include <vlc_codecs.h>
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Open ( vlc_object_t * );
46 static void Close ( vlc_object_t * );
49 set_description( _("Ogg/OGM muxer") );
50 set_capability( "sout mux", 10 );
51 set_category( CAT_SOUT );
52 set_subcategory( SUBCAT_SOUT_MUX );
53 add_shortcut( "ogg" );
54 add_shortcut( "ogm" );
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 *, mtime_t );
69 static block_t *OggCreateFooter( sout_mux_t *, mtime_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 */
132 * TODO move this function to src/stream_output.c (used by nearly all muxers)
134 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
140 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
142 block_fifo_t *p_fifo;
144 p_fifo = p_mux->pp_inputs[i]->p_fifo;
146 /* We don't really need to have anything in the SPU fifo */
147 if( p_mux->pp_inputs[i]->p_fmt->i_cat == SPU_ES &&
148 p_fifo->i_depth == 0 ) continue;
150 if( p_fifo->i_depth )
154 p_buf = block_FifoShow( p_fifo );
155 if( i_stream < 0 || p_buf->i_dts < i_dts )
157 i_dts = p_buf->i_dts;
164 if( pi_stream ) *pi_stream = i_stream;
165 if( pi_dts ) *pi_dts = i_dts;
169 /*****************************************************************************
170 * Definitions of structures and functions used by this plugins
171 *****************************************************************************/
183 int i_keyframe_granule_shift; /* Theora only */
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 ) );
219 p_sys->i_streams = 0;
220 p_sys->i_add_streams = 0;
221 p_sys->i_del_streams = 0;
222 p_sys->pp_del_streams = 0;
224 p_mux->p_sys = p_sys;
225 p_mux->pf_control = Control;
226 p_mux->pf_addstream = AddStream;
227 p_mux->pf_delstream = DelStream;
230 /* First serial number is random.
231 * (Done like this because on win32 you need to seed the random number
232 * generator once per thread). */
233 srand( (unsigned int)time( NULL ) );
234 p_sys->i_next_serial_no = rand();
239 /*****************************************************************************
240 * Close: Finalize ogg bitstream and close muxer
241 *****************************************************************************/
242 static void Close( vlc_object_t * p_this )
244 sout_mux_t *p_mux = (sout_mux_t*)p_this;
245 sout_mux_sys_t *p_sys = p_mux->p_sys;
247 msg_Info( p_mux, "Close" );
249 if( p_sys->i_del_streams )
251 block_t *p_og = NULL;
255 /* Close the current ogg stream */
256 msg_Dbg( p_mux, "writing footer" );
257 block_ChainAppend( &p_og, OggCreateFooter( p_mux, 0 ) );
259 /* Remove deleted logical streams */
260 for( i = 0; i < p_sys->i_del_streams; i++ )
262 i_dts = p_sys->pp_del_streams[i]->i_dts;
263 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
264 FREENULL( p_sys->pp_del_streams[i]->p_oggds_header );
265 FREENULL( p_sys->pp_del_streams[i] );
267 FREENULL( p_sys->pp_del_streams );
268 p_sys->i_streams -= p_sys->i_del_streams;
271 OggSetDate( p_og, i_dts, 0 );
272 sout_AccessOutWrite( p_mux->p_access, p_og );
278 /*****************************************************************************
280 *****************************************************************************/
281 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
288 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
289 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
293 case MUX_GET_ADD_STREAM_WAIT:
294 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
299 ppsz = (char**)va_arg( args, char ** );
300 *ppsz = strdup( "application/ogg" );
307 /*****************************************************************************
308 * AddStream: Add an elementary stream to the muxed stream
309 *****************************************************************************/
310 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
312 sout_mux_sys_t *p_sys = p_mux->p_sys;
313 ogg_stream_t *p_stream;
316 msg_Dbg( p_mux, "adding input" );
318 p_input->p_sys = p_stream = malloc( sizeof( ogg_stream_t ) );
320 p_stream->i_cat = p_input->p_fmt->i_cat;
321 p_stream->i_fourcc = p_input->p_fmt->i_codec;
322 p_stream->i_serial_no = p_sys->i_next_serial_no++;
323 p_stream->i_packet_no = 0;
325 p_stream->p_oggds_header = 0;
327 switch( p_input->p_fmt->i_cat )
330 if( !p_input->p_fmt->video.i_frame_rate ||
331 !p_input->p_fmt->video.i_frame_rate_base )
333 msg_Warn( p_mux, "Missing frame rate, assuming 25fps" );
334 p_input->p_fmt->video.i_frame_rate = 25;
335 p_input->p_fmt->video.i_frame_rate_base = 1;
338 switch( p_stream->i_fourcc )
340 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
341 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
342 case VLC_FOURCC( 'D', 'I', 'V', '3' ):
343 case VLC_FOURCC( 'M', 'J', 'P', 'G' ):
344 case VLC_FOURCC( 'W', 'M', 'V', '1' ):
345 case VLC_FOURCC( 'W', 'M', 'V', '2' ):
346 case VLC_FOURCC( 'W', 'M', 'V', '3' ):
347 case VLC_FOURCC( 'S', 'N', 'O', 'W' ):
348 case VLC_FOURCC( 'd', 'r', 'a', 'c' ):
349 p_stream->p_oggds_header = malloc( sizeof(oggds_header_t) );
350 memset( p_stream->p_oggds_header, 0, sizeof(oggds_header_t) );
351 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
353 memcpy( p_stream->p_oggds_header->stream_type, "video", 5 );
354 if( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p', '4', 'v' ) )
356 memcpy( p_stream->p_oggds_header->sub_type, "XVID", 4 );
358 else if( p_stream->i_fourcc == VLC_FOURCC( 'D', 'I', 'V', '3' ) )
360 memcpy( p_stream->p_oggds_header->sub_type, "DIV3", 4 );
364 memcpy( p_stream->p_oggds_header->sub_type,
365 &p_stream->i_fourcc, 4 );
367 SetDWLE( &p_stream->p_oggds_header->i_size,
368 sizeof( oggds_header_t ) - 1 );
369 SetQWLE( &p_stream->p_oggds_header->i_time_unit,
370 I64C(10000000) * p_input->p_fmt->video.i_frame_rate_base /
371 (int64_t)p_input->p_fmt->video.i_frame_rate );
372 SetQWLE( &p_stream->p_oggds_header->i_samples_per_unit, 1 );
373 SetDWLE( &p_stream->p_oggds_header->i_default_len, 1 ); /* ??? */
374 SetDWLE( &p_stream->p_oggds_header->i_buffer_size, 1024*1024 );
375 SetWLE( &p_stream->p_oggds_header->i_bits_per_sample, 0 );
376 SetDWLE( &p_stream->p_oggds_header->header.video.i_width,
377 p_input->p_fmt->video.i_width );
378 SetDWLE( &p_stream->p_oggds_header->header.video.i_height,
379 p_input->p_fmt->video.i_height );
380 msg_Dbg( p_mux, "%4.4s stream", (char *)&p_stream->i_fourcc );
383 case VLC_FOURCC( 't', 'h', 'e', 'o' ):
384 msg_Dbg( p_mux, "theora stream" );
388 FREENULL( p_input->p_sys );
394 switch( p_stream->i_fourcc )
396 case VLC_FOURCC( 'v', 'o', 'r', 'b' ):
397 msg_Dbg( p_mux, "vorbis stream" );
400 case VLC_FOURCC( 's', 'p', 'x', ' ' ):
401 msg_Dbg( p_mux, "speex stream" );
404 case VLC_FOURCC( 'f', 'l', 'a', 'c' ):
405 msg_Dbg( p_mux, "flac stream" );
409 fourcc_to_wf_tag( p_stream->i_fourcc, &i_tag );
410 if( i_tag == WAVE_FORMAT_UNKNOWN )
412 FREENULL( p_input->p_sys );
416 p_stream->p_oggds_header =
417 malloc( sizeof(oggds_header_t) + p_input->p_fmt->i_extra );
418 memset( p_stream->p_oggds_header, 0, sizeof(oggds_header_t) );
419 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
421 SetDWLE( &p_stream->p_oggds_header->i_size,
422 sizeof( oggds_header_t ) - 1 + p_input->p_fmt->i_extra );
424 if( p_input->p_fmt->i_extra )
426 memcpy( &p_stream->p_oggds_header[1],
427 p_input->p_fmt->p_extra, p_input->p_fmt->i_extra );
430 memcpy( p_stream->p_oggds_header->stream_type, "audio", 5 );
432 memset( p_stream->p_oggds_header->sub_type, 0, 4 );
433 sprintf( p_stream->p_oggds_header->sub_type, "%-x", i_tag );
435 SetQWLE( &p_stream->p_oggds_header->i_time_unit, I64C(10000000) );
436 SetDWLE( &p_stream->p_oggds_header->i_default_len, 1 );
437 SetDWLE( &p_stream->p_oggds_header->i_buffer_size, 30*1024 );
438 SetQWLE( &p_stream->p_oggds_header->i_samples_per_unit,
439 p_input->p_fmt->audio.i_rate );
440 SetWLE( &p_stream->p_oggds_header->i_bits_per_sample,
441 p_input->p_fmt->audio.i_bitspersample );
442 SetDWLE( &p_stream->p_oggds_header->header.audio.i_channels,
443 p_input->p_fmt->audio.i_channels );
444 SetDWLE( &p_stream->p_oggds_header->header.audio.i_block_align,
445 p_input->p_fmt->audio.i_blockalign );
446 SetDWLE( &p_stream->p_oggds_header->header.audio.i_avgbytespersec,
447 p_input->p_fmt->i_bitrate / 8);
448 msg_Dbg( p_mux, "%4.4s stream", (char *)&p_stream->i_fourcc );
454 switch( p_stream->i_fourcc )
456 case VLC_FOURCC( 's', 'u','b', 't' ):
457 p_stream->p_oggds_header = malloc( sizeof(oggds_header_t) );
458 memset( p_stream->p_oggds_header, 0, sizeof(oggds_header_t) );
459 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
461 memcpy( p_stream->p_oggds_header->stream_type, "text", 4 );
462 msg_Dbg( p_mux, "subtitles stream" );
466 FREENULL( p_input->p_sys );
471 FREENULL( p_input->p_sys );
475 p_stream->b_new = VLC_TRUE;
477 p_sys->i_add_streams++;
482 /*****************************************************************************
483 * DelStream: Delete an elementary stream from the muxed stream
484 *****************************************************************************/
485 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
487 sout_mux_sys_t *p_sys = p_mux->p_sys;
488 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
491 msg_Dbg( p_mux, "removing input" );
493 /* flush all remaining data */
496 if( !p_stream->b_new )
498 while( p_input->p_fifo->i_depth ) MuxBlock( p_mux, p_input );
501 if( !p_stream->b_new &&
502 ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
504 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
505 sout_AccessOutWrite( p_mux->p_access, p_og );
508 /* move input in delete queue */
509 if( !p_stream->b_new )
511 p_sys->pp_del_streams = realloc( p_sys->pp_del_streams,
512 (p_sys->i_del_streams + 1) *
513 sizeof(ogg_stream_t *) );
514 p_sys->pp_del_streams[p_sys->i_del_streams++] = p_stream;
518 /* wasn't already added so get rid of it */
519 FREENULL( p_stream->p_oggds_header );
520 FREENULL( p_stream );
521 p_sys->i_add_streams--;
525 p_input->p_sys = NULL;
530 /*****************************************************************************
531 * Ogg bitstream manipulation routines
532 *****************************************************************************/
533 static block_t *OggStreamFlush( sout_mux_t *p_mux,
534 ogg_stream_state *p_os, mtime_t i_pts )
536 block_t *p_og, *p_og_first = NULL;
539 while( ogg_stream_flush( p_os, &og ) )
542 p_og = block_New( p_mux, og.header_len + og.body_len );
544 memcpy( p_og->p_buffer, og.header, og.header_len );
545 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
550 i_pts = 0; // write it only once
552 block_ChainAppend( &p_og_first, p_og );
558 static block_t *OggStreamPageOut( sout_mux_t *p_mux,
559 ogg_stream_state *p_os, mtime_t i_pts )
561 block_t *p_og, *p_og_first = NULL;
564 while( ogg_stream_pageout( p_os, &og ) )
567 p_og = block_New( p_mux, og.header_len + og.body_len );
569 memcpy( p_og->p_buffer, og.header, og.header_len );
570 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
575 i_pts = 0; // write them only once
577 block_ChainAppend( &p_og_first, p_og );
583 static block_t *OggCreateHeader( sout_mux_t *p_mux, mtime_t i_dts )
585 block_t *p_hdr = NULL;
586 block_t *p_og = NULL;
591 /* Write header for each stream. All b_o_s (beginning of stream) packets
592 * must appear first in the ogg stream so we take care of them first. */
593 for( i = 0; i < p_mux->i_nb_inputs; i++ )
595 sout_input_t *p_input = p_mux->pp_inputs[i];
596 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
597 p_stream->b_new = VLC_FALSE;
599 msg_Dbg( p_mux, "creating header for %4.4s",
600 (char *)&p_stream->i_fourcc );
602 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
603 p_stream->i_packet_no = 0;
605 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
606 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) ||
607 p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
609 /* First packet in order: vorbis/speex/theora info */
610 p_extra = p_input->p_fmt->p_extra;
611 i_extra = p_input->p_fmt->i_extra;
613 op.bytes = *(p_extra++) << 8;
614 op.bytes |= (*(p_extra++) & 0xFF);
616 i_extra -= (op.bytes + 2);
619 msg_Err( p_mux, "header data corrupted");
626 op.packetno = p_stream->i_packet_no++;
627 ogg_stream_packetin( &p_stream->os, &op );
628 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
630 /* Get keyframe_granule_shift for theora granulepos calculation */
631 if( p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
633 int i_keyframe_frequency_force =
634 1 << ((op.packet[40] << 6 >> 3) | (op.packet[41] >> 5));
636 /* granule_shift = i_log( frequency_force -1 ) */
637 p_stream->i_keyframe_granule_shift = 0;
638 i_keyframe_frequency_force--;
639 while( i_keyframe_frequency_force )
641 p_stream->i_keyframe_granule_shift++;
642 i_keyframe_frequency_force >>= 1;
646 else if( p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
648 /* flac stream marker (yeah, only that in the 1st packet) */
649 op.packet = (unsigned char *)"fLaC";
654 op.packetno = p_stream->i_packet_no++;
655 ogg_stream_packetin( &p_stream->os, &op );
656 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
658 else if( p_stream->p_oggds_header )
661 op.packet = (uint8_t*)p_stream->p_oggds_header;
662 op.bytes = p_stream->p_oggds_header->i_size + 1;
666 op.packetno = p_stream->i_packet_no++;
667 ogg_stream_packetin( &p_stream->os, &op );
668 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
671 block_ChainAppend( &p_hdr, p_og );
674 /* Take care of the non b_o_s headers */
675 for( i = 0; i < p_mux->i_nb_inputs; i++ )
677 sout_input_t *p_input = p_mux->pp_inputs[i];
678 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
680 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
681 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) ||
682 p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
684 /* Special case, headers are already there in the incoming stream.
685 * We need to gather them an mark them as headers. */
688 if( p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) ) j = 1;
690 p_extra = p_input->p_fmt->p_extra;
691 i_extra = p_input->p_fmt->i_extra;
694 op.bytes = *(p_extra++) << 8;
695 op.bytes |= (*(p_extra++) & 0xFF);
698 i_extra -= (op.bytes + 2);
702 op.bytes = *(p_extra++) << 8;
703 op.bytes |= (*(p_extra++) & 0xFF);
706 i_extra -= (op.bytes + 2);
709 msg_Err( p_mux, "header data corrupted");
716 op.packetno = p_stream->i_packet_no++;
717 ogg_stream_packetin( &p_stream->os, &op );
719 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
720 block_ChainAppend( &p_hdr, p_og );
723 else if( p_stream->i_fourcc != VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
729 com[0] = PACKET_TYPE_COMMENT;
730 i_com = snprintf( (char *)(com+1), 127,
731 PACKAGE_VERSION" stream output" )
738 op.packetno = p_stream->i_packet_no++;
739 ogg_stream_packetin( &p_stream->os, &op );
740 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
741 block_ChainAppend( &p_hdr, p_og );
744 /* Special case for mp4v and flac */
745 if( ( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p', '4', 'v' ) ||
746 p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) ) &&
747 p_input->p_fmt->i_extra )
749 /* Send a packet with the VOL data for mp4v
750 * or STREAMINFO for flac */
751 msg_Dbg( p_mux, "writing extra data" );
752 op.bytes = p_input->p_fmt->i_extra;
753 op.packet = p_input->p_fmt->p_extra;
754 if( p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
756 /* Skip the flac stream marker */
763 op.packetno = p_stream->i_packet_no++;
764 ogg_stream_packetin( &p_stream->os, &op );
765 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
766 block_ChainAppend( &p_hdr, p_og );
770 /* set HEADER flag */
771 for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
773 p_og->i_flags |= BLOCK_FLAG_HEADER;
778 static block_t *OggCreateFooter( sout_mux_t *p_mux, mtime_t i_dts )
780 sout_mux_sys_t *p_sys = p_mux->p_sys;
781 block_t *p_hdr = NULL;
786 /* flush all remaining data */
787 for( i = 0; i < p_mux->i_nb_inputs; i++ )
789 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
791 /* skip newly added streams */
792 if( p_stream->b_new ) continue;
794 if( ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
796 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
797 sout_AccessOutWrite( p_mux->p_access, p_og );
801 /* Write eos packets for each stream. */
802 for( i = 0; i < p_mux->i_nb_inputs; i++ )
804 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
806 /* skip newly added streams */
807 if( p_stream->b_new ) continue;
814 op.packetno = p_stream->i_packet_no++;
815 ogg_stream_packetin( &p_stream->os, &op );
817 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
818 block_ChainAppend( &p_hdr, p_og );
819 ogg_stream_clear( &p_stream->os );
822 for( i = 0; i < p_sys->i_del_streams; i++ )
829 op.packetno = p_sys->pp_del_streams[i]->i_packet_no++;
830 ogg_stream_packetin( &p_sys->pp_del_streams[i]->os, &op );
832 p_og = OggStreamFlush( p_mux, &p_sys->pp_del_streams[i]->os, 0 );
833 block_ChainAppend( &p_hdr, p_og );
834 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
840 static void OggSetDate( block_t *p_og, mtime_t i_dts, mtime_t i_length )
846 for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
850 i_delta = i_length / i_count;
852 for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
854 p_tmp->i_dts = i_dts;
855 p_tmp->i_length = i_delta;
861 /*****************************************************************************
862 * Mux: multiplex available data in input fifos into the Ogg bitstream
863 *****************************************************************************/
864 static int Mux( sout_mux_t *p_mux )
866 sout_mux_sys_t *p_sys = p_mux->p_sys;
867 block_t *p_og = NULL;
871 if( p_sys->i_add_streams || p_sys->i_del_streams )
873 /* Open new ogg stream */
874 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
876 msg_Dbg( p_mux, "waiting for data..." );
880 if( p_sys->i_streams )
882 /* Close current ogg stream */
885 msg_Dbg( p_mux, "writing footer" );
886 block_ChainAppend( &p_og, OggCreateFooter( p_mux, 0 ) );
888 /* Remove deleted logical streams */
889 for( i = 0; i < p_sys->i_del_streams; i++ )
891 FREENULL( p_sys->pp_del_streams[i]->p_oggds_header );
892 FREENULL( p_sys->pp_del_streams[i] );
894 FREENULL( p_sys->pp_del_streams );
895 p_sys->i_streams = 0;
898 msg_Dbg( p_mux, "writing header" );
899 p_sys->i_start_dts = i_dts;
900 p_sys->i_streams = p_mux->i_nb_inputs;
901 p_sys->i_del_streams = 0;
902 p_sys->i_add_streams = 0;
903 block_ChainAppend( &p_og, OggCreateHeader( p_mux, i_dts ) );
905 /* Write header and/or footer */
906 OggSetDate( p_og, i_dts, 0 );
907 sout_AccessOutWrite( p_mux->p_access, p_og );
913 if( MuxGetStream( p_mux, &i_stream, 0 ) < 0 ) return VLC_SUCCESS;
914 MuxBlock( p_mux, p_mux->pp_inputs[i_stream] );
920 static int MuxBlock( sout_mux_t *p_mux, sout_input_t *p_input )
922 sout_mux_sys_t *p_sys = p_mux->p_sys;
923 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
924 block_t *p_data = block_FifoGet( p_input->p_fifo );
925 block_t *p_og = NULL;
928 if( p_stream->i_fourcc != VLC_FOURCC( 'v', 'o', 'r', 'b' ) &&
929 p_stream->i_fourcc != VLC_FOURCC( 'f', 'l', 'a', 'c' ) &&
930 p_stream->i_fourcc != VLC_FOURCC( 's', 'p', 'x', ' ' ) &&
931 p_stream->i_fourcc != VLC_FOURCC( 't', 'h', 'e', 'o' ) )
933 p_data = block_Realloc( p_data, 1, p_data->i_buffer );
934 p_data->p_buffer[0] = PACKET_IS_SYNCPOINT; // FIXME
937 op.packet = p_data->p_buffer;
938 op.bytes = p_data->i_buffer;
941 op.packetno = p_stream->i_packet_no++;
943 if( p_stream->i_cat == AUDIO_ES )
945 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
946 p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) ||
947 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) )
949 /* number of sample from begining + current packet */
951 ( p_data->i_dts - p_sys->i_start_dts + p_data->i_length ) *
952 (mtime_t)p_input->p_fmt->audio.i_rate / I64C(1000000);
954 else if( p_stream->p_oggds_header )
956 /* number of sample from begining */
957 op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) *
958 p_stream->p_oggds_header->i_samples_per_unit / I64C(1000000);
961 else if( p_stream->i_cat == VIDEO_ES )
963 if( p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
965 /* FIXME, we assume only keyframes */
966 op.granulepos = ( ( p_data->i_dts - p_sys->i_start_dts ) *
967 p_input->p_fmt->video.i_frame_rate /
968 p_input->p_fmt->video.i_frame_rate_base /
969 I64C(1000000) ) << p_stream->i_keyframe_granule_shift;
971 else if( p_stream->p_oggds_header )
972 op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) * I64C(10) /
973 p_stream->p_oggds_header->i_time_unit;
975 else if( p_stream->i_cat == SPU_ES )
977 /* granulepos is in milisec */
978 op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) / 1000;
981 ogg_stream_packetin( &p_stream->os, &op );
983 if( p_stream->i_cat == SPU_ES ||
984 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) )
986 /* Subtitles or Speex packets are quite small so they
987 * need to be flushed to be sent on time */
988 p_og = OggStreamFlush( p_mux, &p_stream->os, p_data->i_dts );
992 p_og = OggStreamPageOut( p_mux, &p_stream->os, p_data->i_dts );
997 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
998 p_stream->i_dts = -1;
999 p_stream->i_length = 0;
1001 sout_AccessOutWrite( p_mux->p_access, p_og );
1005 if( p_stream->i_dts < 0 )
1007 p_stream->i_dts = p_data->i_dts;
1009 p_stream->i_length += p_data->i_length;
1012 block_Release( p_data );