1 /*****************************************************************************
2 * ogg.c: ogg muxer module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@netcourrier.com>
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
36 #include <vlc/input.h>
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Open ( vlc_object_t * );
47 static void Close ( vlc_object_t * );
50 set_description( _("Ogg/ogm muxer") );
51 set_capability( "sout mux", 10 );
52 add_shortcut( "ogg" );
53 add_shortcut( "ogm" );
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 * );
66 static block_t *OggCreateHeader( sout_mux_t *, mtime_t );
67 static block_t *OggCreateFooter( sout_mux_t *, mtime_t );
69 /*****************************************************************************
71 *****************************************************************************/
72 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
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 )
139 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
141 block_fifo_t *p_fifo;
143 p_fifo = p_mux->pp_inputs[i]->p_fifo;
145 /* We don't really need to have anything in the SPU fifo */
146 if( p_mux->pp_inputs[i]->p_fmt->i_cat == SPU_ES &&
147 p_fifo->i_depth == 0 ) continue;
149 if( p_fifo->i_depth > 2 ||
150 /* Special case for SPUs */
151 ( p_mux->pp_inputs[i]->p_fmt->i_cat == SPU_ES &&
152 p_fifo->i_depth > 0 ) )
156 p_buf = block_FifoShow( p_fifo );
157 if( i_stream < 0 || p_buf->i_dts < i_dts )
159 i_dts = p_buf->i_dts;
165 // wait that all fifo have at least 3 packets (3 vorbis headers)
171 *pi_stream = i_stream;
180 /*****************************************************************************
181 * Definitions of structures and functions used by this plugins
182 *****************************************************************************/
194 int i_keyframe_granule_shift; /* Theora only */
197 oggds_header_t *p_oggds_header;
199 block_t *pp_sout_headers[3];
204 struct sout_mux_sys_t
209 int i_next_serial_no;
211 /* number of logical streams pending to be added */
214 /* logical streams pending to be deleted */
216 ogg_stream_t **pp_del_streams;
219 static void OggSetDate( block_t *, mtime_t , mtime_t );
220 static block_t *OggStreamFlush( sout_mux_t *, ogg_stream_state *, mtime_t );
222 /*****************************************************************************
224 *****************************************************************************/
225 static int Open( vlc_object_t *p_this )
227 sout_mux_t *p_mux = (sout_mux_t*)p_this;
228 sout_mux_sys_t *p_sys;
230 msg_Info( p_mux, "Open" );
232 p_sys = malloc( sizeof( sout_mux_sys_t ) );
233 p_sys->i_streams = 0;
234 p_sys->i_add_streams = 0;
235 p_sys->i_del_streams = 0;
236 p_sys->pp_del_streams = 0;
238 p_mux->p_sys = p_sys;
239 p_mux->pf_control = Control;
240 p_mux->pf_addstream = AddStream;
241 p_mux->pf_delstream = DelStream;
244 /* First serial number is random.
245 * (Done like this because on win32 you need to seed the random number
246 * generator once per thread). */
247 srand( (unsigned int)time( NULL ) );
248 p_sys->i_next_serial_no = rand();
253 /*****************************************************************************
254 * Close: Finalize ogg bitstream and close muxer
255 *****************************************************************************/
256 static void Close( vlc_object_t * p_this )
258 sout_mux_t *p_mux = (sout_mux_t*)p_this;
259 sout_mux_sys_t *p_sys = p_mux->p_sys;
261 msg_Info( p_mux, "Close" );
263 if( p_sys->i_del_streams )
265 block_t *p_og = NULL;
269 /* Close the current ogg stream */
270 msg_Dbg( p_mux, "writing footer" );
271 block_ChainAppend( &p_og, OggCreateFooter( p_mux, 0 ) );
273 /* Remove deleted logical streams */
274 for( i = 0; i < p_sys->i_del_streams; i++ )
276 i_dts = p_sys->pp_del_streams[i]->i_dts;
277 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
278 FREE( p_sys->pp_del_streams[i]->p_oggds_header );
279 FREE( p_sys->pp_del_streams[i] );
281 FREE( p_sys->pp_del_streams );
282 p_sys->i_streams -= p_sys->i_del_streams;
285 OggSetDate( p_og, i_dts, 0 );
286 sout_AccessOutWrite( p_mux->p_access, p_og );
292 /*****************************************************************************
294 *****************************************************************************/
295 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
302 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
303 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
307 case MUX_GET_ADD_STREAM_WAIT:
308 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
313 ppsz = (char**)va_arg( args, char ** );
314 *ppsz = strdup( "application/ogg" );
321 /*****************************************************************************
322 * AddStream: Add an elementary stream to the muxed stream
323 *****************************************************************************/
324 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
326 sout_mux_sys_t *p_sys = p_mux->p_sys;
327 ogg_stream_t *p_stream;
330 msg_Dbg( p_mux, "adding input" );
332 p_input->p_sys = p_stream = malloc( sizeof( ogg_stream_t ) );
334 p_stream->i_cat = p_input->p_fmt->i_cat;
335 p_stream->i_fourcc = p_input->p_fmt->i_codec;
336 p_stream->i_serial_no = p_sys->i_next_serial_no++;
337 p_stream->i_packet_no = 0;
339 p_stream->i_sout_headers = 0;
340 p_stream->p_oggds_header = 0;
342 switch( p_input->p_fmt->i_cat )
345 if( !p_input->p_fmt->video.i_frame_rate ||
346 !p_input->p_fmt->video.i_frame_rate_base )
348 msg_Warn( p_mux, "Missing frame rate, assuming 25fps" );
349 p_input->p_fmt->video.i_frame_rate = 25;
350 p_input->p_fmt->video.i_frame_rate_base = 1;
353 switch( p_stream->i_fourcc )
355 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
356 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
357 case VLC_FOURCC( 'D', 'I', 'V', '3' ):
358 case VLC_FOURCC( 'M', 'J', 'P', 'G' ):
359 case VLC_FOURCC( 'W', 'M', 'V', '1' ):
360 case VLC_FOURCC( 'W', 'M', 'V', '2' ):
361 case VLC_FOURCC( 'W', 'M', 'V', '3' ):
362 p_stream->p_oggds_header = malloc( sizeof(oggds_header_t) );
363 memset( p_stream->p_oggds_header, 0, sizeof(oggds_header_t) );
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_FOURCC( 'm', 'p', '4', 'v' ) )
369 memcpy( p_stream->p_oggds_header->sub_type, "XVID", 4 );
371 else if( p_stream->i_fourcc == VLC_FOURCC( 'D', 'I', 'V', '3' ) )
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 I64C(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_FOURCC( 't', 'h', 'e', 'o' ):
397 msg_Dbg( p_mux, "theora stream" );
401 FREE( p_input->p_sys );
407 switch( p_stream->i_fourcc )
409 case VLC_FOURCC( 'v', 'o', 'r', 'b' ):
410 msg_Dbg( p_mux, "vorbis stream" );
413 case VLC_FOURCC( 's', 'p', 'x', ' ' ):
414 msg_Dbg( p_mux, "speex stream" );
417 case VLC_FOURCC( 'f', 'l', 'a', 'c' ):
418 msg_Dbg( p_mux, "flac stream" );
422 fourcc_to_wf_tag( p_stream->i_fourcc, &i_tag );
423 if( i_tag == WAVE_FORMAT_UNKNOWN )
425 FREE( p_input->p_sys );
429 p_stream->p_oggds_header =
430 malloc( sizeof(oggds_header_t) + p_input->p_fmt->i_extra );
431 memset( p_stream->p_oggds_header, 0, sizeof(oggds_header_t) );
432 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
434 SetDWLE( &p_stream->p_oggds_header->i_size,
435 sizeof( oggds_header_t ) - 1 + p_input->p_fmt->i_extra );
437 if( p_input->p_fmt->i_extra )
439 memcpy( &p_stream->p_oggds_header[1],
440 p_input->p_fmt->p_extra, p_input->p_fmt->i_extra );
443 memcpy( p_stream->p_oggds_header->stream_type, "audio", 5 );
445 memset( p_stream->p_oggds_header->sub_type, 0, 4 );
446 sprintf( p_stream->p_oggds_header->sub_type, "%-x", i_tag );
448 SetQWLE( &p_stream->p_oggds_header->i_time_unit, I64C(10000000) );
449 SetDWLE( &p_stream->p_oggds_header->i_default_len, 1 );
450 SetDWLE( &p_stream->p_oggds_header->i_buffer_size, 30*1024 );
451 SetQWLE( &p_stream->p_oggds_header->i_samples_per_unit,
452 p_input->p_fmt->audio.i_rate );
453 SetWLE( &p_stream->p_oggds_header->i_bits_per_sample,
454 p_input->p_fmt->audio.i_bitspersample );
455 SetDWLE( &p_stream->p_oggds_header->header.audio.i_channels,
456 p_input->p_fmt->audio.i_channels );
457 SetDWLE( &p_stream->p_oggds_header->header.audio.i_block_align,
458 p_input->p_fmt->audio.i_blockalign );
459 SetDWLE( &p_stream->p_oggds_header->header.audio.i_avgbytespersec,
460 p_input->p_fmt->i_bitrate / 8);
461 msg_Dbg( p_mux, "%4.4s stream", (char *)&p_stream->i_fourcc );
467 switch( p_stream->i_fourcc )
469 case VLC_FOURCC( 's', 'u','b', 't' ):
470 p_stream->p_oggds_header = malloc( sizeof(oggds_header_t) );
471 memset( p_stream->p_oggds_header, 0, sizeof(oggds_header_t) );
472 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
474 memcpy( p_stream->p_oggds_header->stream_type, "text", 4 );
475 msg_Dbg( p_mux, "subtitles stream" );
479 FREE( p_input->p_sys );
484 FREE( p_input->p_sys );
488 p_stream->b_new = VLC_TRUE;
490 p_sys->i_add_streams++;
495 /*****************************************************************************
496 * DelStream: Delete an elementary stream from the muxed stream
497 *****************************************************************************/
498 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
500 sout_mux_sys_t *p_sys = p_mux->p_sys;
501 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
504 msg_Dbg( p_mux, "removing input" );
506 /* flush all remaining data */
511 if( !p_stream->b_new &&
512 ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
514 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
515 sout_AccessOutWrite( p_mux->p_access, p_og );
518 for( i = 0; i < p_stream->i_sout_headers; i++ )
520 block_Release( p_stream->pp_sout_headers[i] );
521 p_stream->i_sout_headers = 0;
524 /* move input in delete queue */
525 if( !p_stream->b_new )
527 p_sys->pp_del_streams = realloc( p_sys->pp_del_streams,
528 (p_sys->i_del_streams + 1) *
529 sizeof(ogg_stream_t *) );
530 p_sys->pp_del_streams[p_sys->i_del_streams++] = p_stream;
534 /* Wasn't already added so get rid of it */
535 FREE( p_stream->p_oggds_header );
537 p_sys->i_add_streams--;
541 p_input->p_sys = NULL;
546 /*****************************************************************************
547 * Ogg bitstream manipulation routines
548 *****************************************************************************/
549 static block_t *OggStreamFlush( sout_mux_t *p_mux,
550 ogg_stream_state *p_os, mtime_t i_pts )
552 block_t *p_og, *p_og_first = NULL;
555 while( ogg_stream_flush( p_os, &og ) )
558 p_og = block_New( p_mux, og.header_len + og.body_len );
560 memcpy( p_og->p_buffer, og.header, og.header_len );
561 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
566 i_pts = 0; // write it only once
568 block_ChainAppend( &p_og_first, p_og );
574 static block_t *OggStreamPageOut( sout_mux_t *p_mux,
575 ogg_stream_state *p_os, mtime_t i_pts )
577 block_t *p_og, *p_og_first = NULL;
580 while( ogg_stream_pageout( p_os, &og ) )
583 p_og = block_New( p_mux, og.header_len + og.body_len );
585 memcpy( p_og->p_buffer, og.header, og.header_len );
586 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
591 i_pts = 0; // write them only once
593 block_ChainAppend( &p_og_first, p_og );
599 static block_t *OggCreateHeader( sout_mux_t *p_mux, mtime_t i_dts )
601 block_t *p_hdr = NULL;
602 block_t *p_og = NULL;
606 /* Write header for each stream. All b_o_s (beginning of stream) packets
607 * must appear first in the ogg stream so we take care of them first. */
608 for( i = 0; i < p_mux->i_nb_inputs; i++ )
610 ogg_stream_t *p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
611 p_stream->b_new = VLC_FALSE;
613 msg_Dbg( p_mux, "creating header for %4.4s",
614 (char *)&p_stream->i_fourcc );
616 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
617 p_stream->i_packet_no = 0;
619 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
620 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) ||
621 p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
623 /* Special case, headers are already there in the
624 * incoming stream or we backed them up earlier */
626 /* first packet in order: vorbis/speex/theora info */
627 if( !p_stream->i_sout_headers )
629 p_og = block_FifoGet( p_mux->pp_inputs[i]->p_fifo );
630 op.packet = p_og->p_buffer;
631 op.bytes = p_og->i_buffer;
635 op.packetno = p_stream->i_packet_no++;
636 ogg_stream_packetin( &p_stream->os, &op );
637 p_stream->pp_sout_headers[0] =
638 OggStreamFlush( p_mux, &p_stream->os, 0 );
639 p_stream->i_sout_headers++;
641 p_og = block_Duplicate( p_stream->pp_sout_headers[0] );
643 /* Get keyframe_granule_shift for theora granulepos calculation */
644 if( p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
646 int i_keyframe_frequency_force =
647 1 << ((op.packet[40] << 6 >> 3) | (op.packet[41] >> 5));
649 /* granule_shift = i_log( frequency_force -1 ) */
650 p_stream->i_keyframe_granule_shift = 0;
651 i_keyframe_frequency_force--;
652 while( i_keyframe_frequency_force )
654 p_stream->i_keyframe_granule_shift++;
655 i_keyframe_frequency_force >>= 1;
659 else if( p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
661 /* flac stream marker (yeah, only that in the 1st packet) */
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->p_oggds_header )
674 op.packet = (uint8_t*)p_stream->p_oggds_header;
675 op.bytes = p_stream->p_oggds_header->i_size + 1;
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 );
684 block_ChainAppend( &p_hdr, p_og );
687 /* Take care of the non b_o_s headers */
688 for( i = 0; i < p_mux->i_nb_inputs; i++ )
690 ogg_stream_t *p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
692 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
693 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) ||
694 p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
696 /* Special case, headers are already there in the incoming stream.
697 * We need to gather them an mark them as headers. */
699 for( j = 0; j < 2; j++ )
701 if( p_stream->i_sout_headers < j + 2 )
703 /* next packets in order: comments and codebooks */
704 p_og = block_FifoGet( p_mux->pp_inputs[i]->p_fifo );
705 op.packet = p_og->p_buffer;
706 op.bytes = p_og->i_buffer;
710 op.packetno = p_stream->i_packet_no++;
711 ogg_stream_packetin( &p_stream->os, &op );
712 p_stream->pp_sout_headers[j+1] =
713 OggStreamFlush( p_mux, &p_stream->os, 0 );
714 p_stream->i_sout_headers++;
717 p_og = block_Duplicate( p_stream->pp_sout_headers[j+1] );
718 block_ChainAppend( &p_hdr, p_og );
721 else if( p_stream->i_fourcc != VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
727 com[0] = PACKET_TYPE_COMMENT;
728 i_com = snprintf( &com[1], 128, VERSION" stream output" ) + 1;
734 op.packetno = p_stream->i_packet_no++;
735 ogg_stream_packetin( &p_stream->os, &op );
736 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
737 block_ChainAppend( &p_hdr, p_og );
740 /* Special case for mp4v and flac */
741 if( ( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p', '4', 'v' ) ||
742 p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) ) &&
743 p_mux->pp_inputs[i]->p_fmt->i_extra )
745 /* Send a packet with the VOL data for mp4v
746 * or STREAMINFO for flac */
747 msg_Dbg( p_mux, "writing extra data" );
748 op.bytes = p_mux->pp_inputs[i]->p_fmt->i_extra;
749 op.packet = p_mux->pp_inputs[i]->p_fmt->p_extra;
750 if( p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) )
752 /* Skip the flac stream marker */
759 op.packetno = p_stream->i_packet_no++;
760 ogg_stream_packetin( &p_stream->os, &op );
761 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
762 block_ChainAppend( &p_hdr, p_og );
766 /* set HEADER flag */
767 for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
769 p_og->i_flags |= BLOCK_FLAG_HEADER;
774 static block_t *OggCreateFooter( sout_mux_t *p_mux, mtime_t i_dts )
776 sout_mux_sys_t *p_sys = p_mux->p_sys;
777 block_t *p_hdr = NULL;
782 /* flush all remaining data */
783 for( i = 0; i < p_mux->i_nb_inputs; i++ )
785 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
787 /* skip newly added streams */
788 if( p_stream->b_new ) continue;
790 if( ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
792 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
793 sout_AccessOutWrite( p_mux->p_access, p_og );
797 /* Write eos packets for each stream. */
798 for( i = 0; i < p_mux->i_nb_inputs; i++ )
800 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
802 /* skip newly added streams */
803 if( p_stream->b_new ) continue;
810 op.packetno = p_stream->i_packet_no++;
811 ogg_stream_packetin( &p_stream->os, &op );
813 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
814 block_ChainAppend( &p_hdr, p_og );
815 ogg_stream_clear( &p_stream->os );
818 for( i = 0; i < p_sys->i_del_streams; i++ )
825 op.packetno = p_sys->pp_del_streams[i]->i_packet_no++;
826 ogg_stream_packetin( &p_sys->pp_del_streams[i]->os, &op );
828 p_og = OggStreamFlush( p_mux, &p_sys->pp_del_streams[i]->os, 0 );
829 block_ChainAppend( &p_hdr, p_og );
830 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
836 static void OggSetDate( block_t *p_og, mtime_t i_dts, mtime_t i_length )
842 for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
846 i_delta = i_length / i_count;
848 for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
850 p_tmp->i_dts = i_dts;
851 p_tmp->i_length = i_delta;
857 /*****************************************************************************
858 * Mux: multiplex available data in input fifos into the Ogg bitstream
859 *****************************************************************************/
860 static int Mux( sout_mux_t *p_mux )
862 sout_mux_sys_t *p_sys = p_mux->p_sys;
863 block_t *p_og = NULL;
867 if( p_sys->i_add_streams || p_sys->i_del_streams )
869 /* Open new ogg stream */
870 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
872 msg_Dbg( p_mux, "waiting for data..." );
876 if( p_sys->i_streams )
878 /* Close current ogg stream */
881 msg_Dbg( p_mux, "writing footer" );
882 block_ChainAppend( &p_og, OggCreateFooter( p_mux, 0 ) );
884 /* Remove deleted logical streams */
885 for( i = 0; i < p_sys->i_del_streams; i++ )
887 FREE( p_sys->pp_del_streams[i]->p_oggds_header );
888 FREE( p_sys->pp_del_streams[i] );
890 FREE( p_sys->pp_del_streams );
891 p_sys->i_streams = 0;
894 msg_Dbg( p_mux, "writing header" );
895 p_sys->i_start_dts = i_dts;
896 p_sys->i_streams = p_mux->i_nb_inputs;
897 p_sys->i_del_streams = 0;
898 p_sys->i_add_streams = 0;
899 block_ChainAppend( &p_og, OggCreateHeader( p_mux, i_dts ) );
901 /* Write header and/or footer */
902 OggSetDate( p_og, i_dts, 0 );
903 sout_AccessOutWrite( p_mux->p_access, p_og );
909 sout_input_t *p_input;
910 ogg_stream_t *p_stream;
914 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
919 p_input = p_mux->pp_inputs[i_stream];
920 p_stream = (ogg_stream_t*)p_input->p_sys;
921 p_data = block_FifoGet( p_input->p_fifo );
923 if( p_stream->i_fourcc != VLC_FOURCC( 'v', 'o', 'r', 'b' ) &&
924 p_stream->i_fourcc != VLC_FOURCC( 'f', 'l', 'a', 'c' ) &&
925 p_stream->i_fourcc != VLC_FOURCC( 's', 'p', 'x', ' ' ) &&
926 p_stream->i_fourcc != VLC_FOURCC( 't', 'h', 'e', 'o' ) )
928 p_data = block_Realloc( p_data, 1, p_data->i_buffer );
929 p_data->p_buffer[0] = PACKET_IS_SYNCPOINT; // FIXME
932 op.packet = p_data->p_buffer;
933 op.bytes = p_data->i_buffer;
936 op.packetno = p_stream->i_packet_no++;
938 if( p_stream->i_cat == AUDIO_ES )
940 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
941 p_stream->i_fourcc == VLC_FOURCC( 'f', 'l', 'a', 'c' ) ||
942 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) )
944 /* number of sample from begining + current packet */
946 ( i_dts - p_sys->i_start_dts + p_data->i_length ) *
947 (mtime_t)p_input->p_fmt->audio.i_rate / I64C(1000000);
949 else if( p_stream->p_oggds_header )
951 /* number of sample from begining */
952 op.granulepos = ( i_dts - p_sys->i_start_dts ) *
953 p_stream->p_oggds_header->i_samples_per_unit /
957 else if( p_stream->i_cat == VIDEO_ES )
959 if( p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
961 /* FIXME, we assume only keyframes */
962 op.granulepos = ( ( i_dts - p_sys->i_start_dts ) *
963 p_input->p_fmt->video.i_frame_rate /
964 p_input->p_fmt->video.i_frame_rate_base /
965 I64C(1000000) ) << p_stream->i_keyframe_granule_shift;
967 else if( p_stream->p_oggds_header )
968 op.granulepos = ( i_dts - p_sys->i_start_dts ) * I64C(10) /
969 p_stream->p_oggds_header->i_time_unit;
971 else if( p_stream->i_cat == SPU_ES )
973 /* granulepos is in milisec */
974 op.granulepos = ( i_dts - p_sys->i_start_dts ) / 1000;
977 ogg_stream_packetin( &p_stream->os, &op );
979 if( p_stream->i_cat == SPU_ES ||
980 p_stream->i_fourcc == VLC_FOURCC( 's', 'p', 'x', ' ' ) )
982 /* Subtitles or Speex packets are quite small so they
983 * need to be flushed to be sent on time */
984 p_og = OggStreamFlush( p_mux, &p_stream->os, p_data->i_dts );
988 p_og = OggStreamPageOut( p_mux, &p_stream->os, p_data->i_dts );
993 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
994 p_stream->i_dts = -1;
995 p_stream->i_length = 0;
997 sout_AccessOutWrite( p_mux->p_access, p_og );
1001 if( p_stream->i_dts < 0 )
1003 p_stream->i_dts = p_data->i_dts;
1005 p_stream->i_length += p_data->i_length;
1008 block_Release( p_data );