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 *****************************************************************************/
39 #include <vlc_block.h>
40 #include <vlc_codecs.h>
44 /*****************************************************************************
46 *****************************************************************************/
47 static int Open ( vlc_object_t * );
48 static void Close ( vlc_object_t * );
51 set_description( _("Ogg/OGM muxer") );
52 set_capability( "sout mux", 10 );
53 set_category( CAT_SOUT );
54 set_subcategory( SUBCAT_SOUT_MUX );
55 add_shortcut( "ogg" );
56 add_shortcut( "ogm" );
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 */
187 oggds_header_t *p_oggds_header;
191 struct sout_mux_sys_t
196 int i_next_serial_no;
198 /* number of logical streams pending to be added */
201 /* logical streams pending to be deleted */
203 ogg_stream_t **pp_del_streams;
206 static void OggSetDate( block_t *, mtime_t , mtime_t );
207 static block_t *OggStreamFlush( sout_mux_t *, ogg_stream_state *, mtime_t );
209 /*****************************************************************************
211 *****************************************************************************/
212 static int Open( vlc_object_t *p_this )
214 sout_mux_t *p_mux = (sout_mux_t*)p_this;
215 sout_mux_sys_t *p_sys;
217 msg_Info( p_mux, "Open" );
219 p_sys = malloc( sizeof( sout_mux_sys_t ) );
220 p_sys->i_streams = 0;
221 p_sys->i_add_streams = 0;
222 p_sys->i_del_streams = 0;
223 p_sys->pp_del_streams = 0;
225 p_mux->p_sys = p_sys;
226 p_mux->pf_control = Control;
227 p_mux->pf_addstream = AddStream;
228 p_mux->pf_delstream = DelStream;
231 /* First serial number is random.
232 * (Done like this because on win32 you need to seed the random number
233 * generator once per thread). */
234 srand( (unsigned int)time( NULL ) );
235 p_sys->i_next_serial_no = rand();
240 /*****************************************************************************
241 * Close: Finalize ogg bitstream and close muxer
242 *****************************************************************************/
243 static void Close( vlc_object_t * p_this )
245 sout_mux_t *p_mux = (sout_mux_t*)p_this;
246 sout_mux_sys_t *p_sys = p_mux->p_sys;
248 msg_Info( p_mux, "Close" );
250 if( p_sys->i_del_streams )
252 block_t *p_og = NULL;
256 /* Close the current ogg stream */
257 msg_Dbg( p_mux, "writing footer" );
258 block_ChainAppend( &p_og, OggCreateFooter( p_mux ) );
260 /* Remove deleted logical streams */
261 for( i = 0; i < p_sys->i_del_streams; i++ )
263 i_dts = p_sys->pp_del_streams[i]->i_dts;
264 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
265 FREENULL( p_sys->pp_del_streams[i]->p_oggds_header );
266 FREENULL( p_sys->pp_del_streams[i] );
268 FREENULL( p_sys->pp_del_streams );
269 p_sys->i_streams -= p_sys->i_del_streams;
272 OggSetDate( p_og, i_dts, 0 );
273 sout_AccessOutWrite( p_mux->p_access, p_og );
279 /*****************************************************************************
281 *****************************************************************************/
282 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
290 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
291 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
295 case MUX_GET_ADD_STREAM_WAIT:
296 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
301 ppsz = (char**)va_arg( args, char ** );
302 *ppsz = strdup( "application/ogg" );
309 /*****************************************************************************
310 * AddStream: Add an elementary stream to the muxed stream
311 *****************************************************************************/
312 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
314 sout_mux_sys_t *p_sys = p_mux->p_sys;
315 ogg_stream_t *p_stream;
318 msg_Dbg( p_mux, "adding input" );
320 p_input->p_sys = p_stream = malloc( sizeof( ogg_stream_t ) );
322 p_stream->i_cat = p_input->p_fmt->i_cat;
323 p_stream->i_fourcc = p_input->p_fmt->i_codec;
324 p_stream->i_serial_no = p_sys->i_next_serial_no++;
325 p_stream->i_packet_no = 0;
327 p_stream->p_oggds_header = 0;
329 switch( p_input->p_fmt->i_cat )
332 if( !p_input->p_fmt->video.i_frame_rate ||
333 !p_input->p_fmt->video.i_frame_rate_base )
335 msg_Warn( p_mux, "Missing frame rate, assuming 25fps" );
336 p_input->p_fmt->video.i_frame_rate = 25;
337 p_input->p_fmt->video.i_frame_rate_base = 1;
340 switch( p_stream->i_fourcc )
342 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
343 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
344 case VLC_FOURCC( 'D', 'I', 'V', '3' ):
345 case VLC_FOURCC( 'M', 'J', 'P', 'G' ):
346 case VLC_FOURCC( 'W', 'M', 'V', '1' ):
347 case VLC_FOURCC( 'W', 'M', 'V', '2' ):
348 case VLC_FOURCC( 'W', 'M', 'V', '3' ):
349 case VLC_FOURCC( 'S', 'N', 'O', 'W' ):
350 case VLC_FOURCC( 'd', 'r', 'a', 'c' ):
351 p_stream->p_oggds_header = malloc( sizeof(oggds_header_t) );
352 memset( p_stream->p_oggds_header, 0, sizeof(oggds_header_t) );
353 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
355 memcpy( p_stream->p_oggds_header->stream_type, "video", 5 );
356 if( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p', '4', 'v' ) )
358 memcpy( p_stream->p_oggds_header->sub_type, "XVID", 4 );
360 else if( p_stream->i_fourcc == VLC_FOURCC( 'D', 'I', 'V', '3' ) )
362 memcpy( p_stream->p_oggds_header->sub_type, "DIV3", 4 );
366 memcpy( p_stream->p_oggds_header->sub_type,
367 &p_stream->i_fourcc, 4 );
369 SetDWLE( &p_stream->p_oggds_header->i_size,
370 sizeof( oggds_header_t ) - 1 );
371 SetQWLE( &p_stream->p_oggds_header->i_time_unit,
372 I64C(10000000) * p_input->p_fmt->video.i_frame_rate_base /
373 (int64_t)p_input->p_fmt->video.i_frame_rate );
374 SetQWLE( &p_stream->p_oggds_header->i_samples_per_unit, 1 );
375 SetDWLE( &p_stream->p_oggds_header->i_default_len, 1 ); /* ??? */
376 SetDWLE( &p_stream->p_oggds_header->i_buffer_size, 1024*1024 );
377 SetWLE( &p_stream->p_oggds_header->i_bits_per_sample, 0 );
378 SetDWLE( &p_stream->p_oggds_header->header.video.i_width,
379 p_input->p_fmt->video.i_width );
380 SetDWLE( &p_stream->p_oggds_header->header.video.i_height,
381 p_input->p_fmt->video.i_height );
382 msg_Dbg( p_mux, "%4.4s stream", (char *)&p_stream->i_fourcc );
385 case VLC_FOURCC( 't', 'h', 'e', 'o' ):
386 msg_Dbg( p_mux, "theora stream" );
390 FREENULL( p_input->p_sys );
396 switch( p_stream->i_fourcc )
398 case VLC_FOURCC( 'v', 'o', 'r', 'b' ):
399 msg_Dbg( p_mux, "vorbis stream" );
402 case VLC_FOURCC( 's', 'p', 'x', ' ' ):
403 msg_Dbg( p_mux, "speex stream" );
406 case VLC_FOURCC( 'f', 'l', 'a', 'c' ):
407 msg_Dbg( p_mux, "flac stream" );
411 fourcc_to_wf_tag( p_stream->i_fourcc, &i_tag );
412 if( i_tag == WAVE_FORMAT_UNKNOWN )
414 FREENULL( p_input->p_sys );
418 p_stream->p_oggds_header =
419 malloc( sizeof(oggds_header_t) + p_input->p_fmt->i_extra );
420 memset( p_stream->p_oggds_header, 0, sizeof(oggds_header_t) );
421 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
423 SetDWLE( &p_stream->p_oggds_header->i_size,
424 sizeof( oggds_header_t ) - 1 + p_input->p_fmt->i_extra );
426 if( p_input->p_fmt->i_extra )
428 memcpy( &p_stream->p_oggds_header[1],
429 p_input->p_fmt->p_extra, p_input->p_fmt->i_extra );
432 memcpy( p_stream->p_oggds_header->stream_type, "audio", 5 );
434 memset( p_stream->p_oggds_header->sub_type, 0, 4 );
435 sprintf( p_stream->p_oggds_header->sub_type, "%-x", i_tag );
437 SetQWLE( &p_stream->p_oggds_header->i_time_unit, I64C(10000000) );
438 SetDWLE( &p_stream->p_oggds_header->i_default_len, 1 );
439 SetDWLE( &p_stream->p_oggds_header->i_buffer_size, 30*1024 );
440 SetQWLE( &p_stream->p_oggds_header->i_samples_per_unit,
441 p_input->p_fmt->audio.i_rate );
442 SetWLE( &p_stream->p_oggds_header->i_bits_per_sample,
443 p_input->p_fmt->audio.i_bitspersample );
444 SetDWLE( &p_stream->p_oggds_header->header.audio.i_channels,
445 p_input->p_fmt->audio.i_channels );
446 SetDWLE( &p_stream->p_oggds_header->header.audio.i_block_align,
447 p_input->p_fmt->audio.i_blockalign );
448 SetDWLE( &p_stream->p_oggds_header->header.audio.i_avgbytespersec,
449 p_input->p_fmt->i_bitrate / 8);
450 msg_Dbg( p_mux, "%4.4s stream", (char *)&p_stream->i_fourcc );
456 switch( p_stream->i_fourcc )
458 case VLC_FOURCC( 's', 'u','b', 't' ):
459 p_stream->p_oggds_header = malloc( sizeof(oggds_header_t) );
460 memset( p_stream->p_oggds_header, 0, sizeof(oggds_header_t) );
461 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
463 memcpy( p_stream->p_oggds_header->stream_type, "text", 4 );
464 msg_Dbg( p_mux, "subtitles stream" );
468 FREENULL( p_input->p_sys );
473 FREENULL( p_input->p_sys );
477 p_stream->b_new = VLC_TRUE;
479 p_sys->i_add_streams++;
484 /*****************************************************************************
485 * DelStream: Delete an elementary stream from the muxed stream
486 *****************************************************************************/
487 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
489 sout_mux_sys_t *p_sys = p_mux->p_sys;
490 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
493 msg_Dbg( p_mux, "removing input" );
495 /* flush all remaining data */
498 if( !p_stream->b_new )
500 while( block_FifoCount( p_input->p_fifo ) )
501 MuxBlock( p_mux, p_input );
504 if( !p_stream->b_new &&
505 ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
507 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
508 sout_AccessOutWrite( p_mux->p_access, p_og );
511 /* move input in delete queue */
512 if( !p_stream->b_new )
514 p_sys->pp_del_streams = realloc( p_sys->pp_del_streams,
515 (p_sys->i_del_streams + 1) *
516 sizeof(ogg_stream_t *) );
517 p_sys->pp_del_streams[p_sys->i_del_streams++] = p_stream;
521 /* wasn't already added so get rid of it */
522 FREENULL( p_stream->p_oggds_header );
523 FREENULL( p_stream );
524 p_sys->i_add_streams--;
528 p_input->p_sys = NULL;
533 /*****************************************************************************
534 * Ogg bitstream manipulation routines
535 *****************************************************************************/
536 static block_t *OggStreamFlush( sout_mux_t *p_mux,
537 ogg_stream_state *p_os, mtime_t i_pts )
539 block_t *p_og, *p_og_first = NULL;
542 while( ogg_stream_flush( p_os, &og ) )
545 p_og = block_New( p_mux, og.header_len + og.body_len );
547 memcpy( p_og->p_buffer, og.header, og.header_len );
548 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
553 i_pts = 0; // write it only once
555 block_ChainAppend( &p_og_first, p_og );
561 static block_t *OggStreamPageOut( sout_mux_t *p_mux,
562 ogg_stream_state *p_os, mtime_t i_pts )
564 block_t *p_og, *p_og_first = NULL;
567 while( ogg_stream_pageout( p_os, &og ) )
570 p_og = block_New( p_mux, og.header_len + og.body_len );
572 memcpy( p_og->p_buffer, og.header, og.header_len );
573 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
578 i_pts = 0; // write them only once
580 block_ChainAppend( &p_og_first, p_og );
586 static block_t *OggCreateHeader( sout_mux_t *p_mux )
588 block_t *p_hdr = NULL;
589 block_t *p_og = NULL;
594 /* Write header for each stream. All b_o_s (beginning of stream) packets
595 * must appear first in the ogg stream so we take care of them first. */
596 for( i = 0; i < p_mux->i_nb_inputs; i++ )
598 sout_input_t *p_input = p_mux->pp_inputs[i];
599 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
600 p_stream->b_new = VLC_FALSE;
602 msg_Dbg( p_mux, "creating header for %4.4s",
603 (char *)&p_stream->i_fourcc );
605 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
606 p_stream->i_packet_no = 0;
608 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
609 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) ||
610 p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
612 /* First packet in order: vorbis/speex/theora info */
613 p_extra = p_input->p_fmt->p_extra;
614 i_extra = p_input->p_fmt->i_extra;
616 op.bytes = *(p_extra++) << 8;
617 op.bytes |= (*(p_extra++) & 0xFF);
619 i_extra -= (op.bytes + 2);
622 msg_Err( p_mux, "header data corrupted");
629 op.packetno = p_stream->i_packet_no++;
630 ogg_stream_packetin( &p_stream->os, &op );
631 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
633 /* Get keyframe_granule_shift for theora granulepos calculation */
634 if( p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
636 int i_keyframe_frequency_force =
637 1 << ((op.packet[40] << 6 >> 3) | (op.packet[41] >> 5));
639 /* granule_shift = i_log( frequency_force -1 ) */
640 p_stream->i_keyframe_granule_shift = 0;
641 i_keyframe_frequency_force--;
642 while( i_keyframe_frequency_force )
644 p_stream->i_keyframe_granule_shift++;
645 i_keyframe_frequency_force >>= 1;
649 else if( p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
651 /* flac stream marker (yeah, only that in the 1st packet) */
652 op.packet = (unsigned char *)"fLaC";
657 op.packetno = p_stream->i_packet_no++;
658 ogg_stream_packetin( &p_stream->os, &op );
659 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
661 else if( p_stream->p_oggds_header )
664 op.packet = (uint8_t*)p_stream->p_oggds_header;
665 op.bytes = p_stream->p_oggds_header->i_size + 1;
669 op.packetno = p_stream->i_packet_no++;
670 ogg_stream_packetin( &p_stream->os, &op );
671 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
674 block_ChainAppend( &p_hdr, p_og );
677 /* Take care of the non b_o_s headers */
678 for( i = 0; i < p_mux->i_nb_inputs; i++ )
680 sout_input_t *p_input = p_mux->pp_inputs[i];
681 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
683 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
684 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) ||
685 p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
687 /* Special case, headers are already there in the incoming stream.
688 * We need to gather them an mark them as headers. */
691 if( p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) ) j = 1;
693 p_extra = p_input->p_fmt->p_extra;
694 i_extra = p_input->p_fmt->i_extra;
697 op.bytes = *(p_extra++) << 8;
698 op.bytes |= (*(p_extra++) & 0xFF);
701 i_extra -= (op.bytes + 2);
705 op.bytes = *(p_extra++) << 8;
706 op.bytes |= (*(p_extra++) & 0xFF);
709 i_extra -= (op.bytes + 2);
712 msg_Err( p_mux, "header data corrupted");
719 op.packetno = p_stream->i_packet_no++;
720 ogg_stream_packetin( &p_stream->os, &op );
722 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
723 block_ChainAppend( &p_hdr, p_og );
726 else if( p_stream->i_fourcc != VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
732 com[0] = PACKET_TYPE_COMMENT;
733 i_com = snprintf( (char *)(com+1), 127,
734 PACKAGE_VERSION" stream output" )
741 op.packetno = p_stream->i_packet_no++;
742 ogg_stream_packetin( &p_stream->os, &op );
743 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
744 block_ChainAppend( &p_hdr, p_og );
747 /* Special case for mp4v and flac */
748 if( ( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p', '4', 'v' ) ||
749 p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) ) &&
750 p_input->p_fmt->i_extra )
752 /* Send a packet with the VOL data for mp4v
753 * or STREAMINFO for flac */
754 msg_Dbg( p_mux, "writing extra data" );
755 op.bytes = p_input->p_fmt->i_extra;
756 op.packet = p_input->p_fmt->p_extra;
757 if( p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
759 /* Skip the flac stream marker */
766 op.packetno = p_stream->i_packet_no++;
767 ogg_stream_packetin( &p_stream->os, &op );
768 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
769 block_ChainAppend( &p_hdr, p_og );
773 /* set HEADER flag */
774 for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
776 p_og->i_flags |= BLOCK_FLAG_HEADER;
781 static block_t *OggCreateFooter( sout_mux_t *p_mux )
783 sout_mux_sys_t *p_sys = p_mux->p_sys;
784 block_t *p_hdr = NULL;
789 /* flush all remaining data */
790 for( i = 0; i < p_mux->i_nb_inputs; i++ )
792 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
794 /* skip newly added streams */
795 if( p_stream->b_new ) continue;
797 if( ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
799 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
800 sout_AccessOutWrite( p_mux->p_access, p_og );
804 /* Write eos packets for each stream. */
805 for( i = 0; i < p_mux->i_nb_inputs; i++ )
807 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
809 /* skip newly added streams */
810 if( p_stream->b_new ) continue;
817 op.packetno = p_stream->i_packet_no++;
818 ogg_stream_packetin( &p_stream->os, &op );
820 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
821 block_ChainAppend( &p_hdr, p_og );
822 ogg_stream_clear( &p_stream->os );
825 for( i = 0; i < p_sys->i_del_streams; i++ )
832 op.packetno = p_sys->pp_del_streams[i]->i_packet_no++;
833 ogg_stream_packetin( &p_sys->pp_del_streams[i]->os, &op );
835 p_og = OggStreamFlush( p_mux, &p_sys->pp_del_streams[i]->os, 0 );
836 block_ChainAppend( &p_hdr, p_og );
837 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
843 static void OggSetDate( block_t *p_og, mtime_t i_dts, mtime_t i_length )
849 for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
853 i_delta = i_length / i_count;
855 for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
857 p_tmp->i_dts = i_dts;
858 p_tmp->i_length = i_delta;
864 /*****************************************************************************
865 * Mux: multiplex available data in input fifos into the Ogg bitstream
866 *****************************************************************************/
867 static int Mux( sout_mux_t *p_mux )
869 sout_mux_sys_t *p_sys = p_mux->p_sys;
870 block_t *p_og = NULL;
874 if( p_sys->i_add_streams || p_sys->i_del_streams )
876 /* Open new ogg stream */
877 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
879 msg_Dbg( p_mux, "waiting for data..." );
883 if( p_sys->i_streams )
885 /* Close current ogg stream */
888 msg_Dbg( p_mux, "writing footer" );
889 block_ChainAppend( &p_og, OggCreateFooter( p_mux ) );
891 /* Remove deleted logical streams */
892 for( i = 0; i < p_sys->i_del_streams; i++ )
894 FREENULL( p_sys->pp_del_streams[i]->p_oggds_header );
895 FREENULL( p_sys->pp_del_streams[i] );
897 FREENULL( p_sys->pp_del_streams );
898 p_sys->i_streams = 0;
901 msg_Dbg( p_mux, "writing header" );
902 p_sys->i_start_dts = i_dts;
903 p_sys->i_streams = p_mux->i_nb_inputs;
904 p_sys->i_del_streams = 0;
905 p_sys->i_add_streams = 0;
906 block_ChainAppend( &p_og, OggCreateHeader( p_mux ) );
908 /* Write header and/or footer */
909 OggSetDate( p_og, i_dts, 0 );
910 sout_AccessOutWrite( p_mux->p_access, p_og );
916 if( MuxGetStream( p_mux, &i_stream, 0 ) < 0 ) return VLC_SUCCESS;
917 MuxBlock( p_mux, p_mux->pp_inputs[i_stream] );
923 static int MuxBlock( sout_mux_t *p_mux, sout_input_t *p_input )
925 sout_mux_sys_t *p_sys = p_mux->p_sys;
926 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
927 block_t *p_data = block_FifoGet( p_input->p_fifo );
928 block_t *p_og = NULL;
931 if( p_stream->i_fourcc != VLC_FOURCC( 'v', 'o', 'r', 'b' ) &&
932 p_stream->i_fourcc != VLC_FOURCC( 'f', 'l', 'a', 'c' ) &&
933 p_stream->i_fourcc != VLC_FOURCC( 's', 'p', 'x', ' ' ) &&
934 p_stream->i_fourcc != VLC_FOURCC( 't', 'h', 'e', 'o' ) )
936 p_data = block_Realloc( p_data, 1, p_data->i_buffer );
937 p_data->p_buffer[0] = PACKET_IS_SYNCPOINT; // FIXME
940 op.packet = p_data->p_buffer;
941 op.bytes = p_data->i_buffer;
944 op.packetno = p_stream->i_packet_no++;
946 if( p_stream->i_cat == AUDIO_ES )
948 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
949 p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) ||
950 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) )
952 /* number of sample from begining + current packet */
954 ( p_data->i_dts - p_sys->i_start_dts + p_data->i_length ) *
955 (mtime_t)p_input->p_fmt->audio.i_rate / I64C(1000000);
957 else if( p_stream->p_oggds_header )
959 /* number of sample from begining */
960 op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) *
961 p_stream->p_oggds_header->i_samples_per_unit / I64C(1000000);
964 else if( p_stream->i_cat == VIDEO_ES )
966 if( p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
968 /* FIXME, we assume only keyframes */
969 op.granulepos = ( ( p_data->i_dts - p_sys->i_start_dts ) *
970 p_input->p_fmt->video.i_frame_rate /
971 p_input->p_fmt->video.i_frame_rate_base /
972 I64C(1000000) ) << p_stream->i_keyframe_granule_shift;
974 else if( p_stream->p_oggds_header )
975 op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) * I64C(10) /
976 p_stream->p_oggds_header->i_time_unit;
978 else if( p_stream->i_cat == SPU_ES )
980 /* granulepos is in milisec */
981 op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) / 1000;
984 ogg_stream_packetin( &p_stream->os, &op );
986 if( p_stream->i_cat == SPU_ES ||
987 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) )
989 /* Subtitles or Speex packets are quite small so they
990 * need to be flushed to be sent on time */
991 p_og = OggStreamFlush( p_mux, &p_stream->os, p_data->i_dts );
995 p_og = OggStreamPageOut( p_mux, &p_stream->os, p_data->i_dts );
1000 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
1001 p_stream->i_dts = -1;
1002 p_stream->i_length = 0;
1004 sout_AccessOutWrite( p_mux->p_access, p_og );
1008 if( p_stream->i_dts < 0 )
1010 p_stream->i_dts = p_data->i_dts;
1012 p_stream->i_length += p_data->i_length;
1015 block_Release( p_data );