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>
40 #include "../demux/xiph.h"
44 /*****************************************************************************
46 *****************************************************************************/
47 static int Open ( vlc_object_t * );
48 static void Close ( vlc_object_t * );
51 set_description( N_("Ogg/OGM muxer") )
52 set_capability( "sout mux", 10 )
53 set_category( CAT_SOUT )
54 set_subcategory( SUBCAT_SOUT_MUX )
55 add_shortcut( "ogg", "ogm" )
56 set_callbacks( Open, Close )
60 /*****************************************************************************
62 *****************************************************************************/
63 static int Control ( sout_mux_t *, int, va_list );
64 static int AddStream( sout_mux_t *, sout_input_t * );
65 static int DelStream( sout_mux_t *, sout_input_t * );
66 static int Mux ( sout_mux_t * );
67 static int MuxBlock ( sout_mux_t *, sout_input_t * );
69 static block_t *OggCreateHeader( sout_mux_t * );
70 static block_t *OggCreateFooter( sout_mux_t * );
72 /*****************************************************************************
74 *****************************************************************************/
76 /* Structures used for OggDS headers used in ogm files */
78 #define PACKET_TYPE_HEADER 0x01
79 #define PACKET_TYPE_COMMENT 0x03
80 #define PACKET_IS_SYNCPOINT 0x08
86 } oggds_header_video_t;
91 int16_t i_block_align;
92 int32_t i_avgbytespersec;
93 } oggds_header_audio_t;
97 uint8_t i_packet_type;
105 int64_t i_samples_per_unit;
106 int32_t i_default_len;
108 int32_t i_buffer_size;
109 int16_t i_bits_per_sample;
111 int16_t i_padding_0; /* Because the original is using MSVC packing style */
115 oggds_header_video_t video;
116 oggds_header_audio_t audio;
119 int32_t i_padding_1; /* Because the original is using MSVC packing style */
123 /*****************************************************************************
124 * Definitions of structures and functions used by this plugins
125 *****************************************************************************/
137 int i_keyframe_granule_shift; /* Theora only */
138 int i_last_keyframe; /* dirac and theora */
139 int i_num_frames; /* Theora only */
140 uint64_t u_last_granulepos; /* Used for correct EOS page */
141 int64_t i_num_keyframes;
144 oggds_header_t *p_oggds_header;
148 struct sout_mux_sys_t
153 int i_next_serial_no;
155 /* number of logical streams pending to be added */
158 /* logical streams pending to be deleted */
160 ogg_stream_t **pp_del_streams;
163 static void OggSetDate( block_t *, mtime_t , mtime_t );
164 static block_t *OggStreamFlush( sout_mux_t *, ogg_stream_state *, mtime_t );
166 /*****************************************************************************
168 *****************************************************************************/
169 static int Open( vlc_object_t *p_this )
171 sout_mux_t *p_mux = (sout_mux_t*)p_this;
172 sout_mux_sys_t *p_sys;
174 msg_Info( p_mux, "Open" );
176 p_sys = malloc( sizeof( sout_mux_sys_t ) );
179 p_sys->i_streams = 0;
180 p_sys->i_add_streams = 0;
181 p_sys->i_del_streams = 0;
182 p_sys->pp_del_streams = 0;
184 p_mux->p_sys = p_sys;
185 p_mux->pf_control = Control;
186 p_mux->pf_addstream = AddStream;
187 p_mux->pf_delstream = DelStream;
190 /* First serial number is random.
191 * (Done like this because on win32 you need to seed the random number
192 * generator once per thread). */
194 vlc_rand_bytes(&r, sizeof(r));
195 p_sys->i_next_serial_no = r & INT_MAX;
200 /*****************************************************************************
201 * Close: Finalize ogg bitstream and close muxer
202 *****************************************************************************/
203 static void Close( vlc_object_t * p_this )
205 sout_mux_t *p_mux = (sout_mux_t*)p_this;
206 sout_mux_sys_t *p_sys = p_mux->p_sys;
208 msg_Info( p_mux, "Close" );
210 if( p_sys->i_del_streams )
212 block_t *p_og = NULL;
213 mtime_t i_dts = p_sys->pp_del_streams[p_sys->i_del_streams - 1]->i_dts;
215 /* Close the current ogg stream */
216 msg_Dbg( p_mux, "writing footer" );
217 block_ChainAppend( &p_og, OggCreateFooter( p_mux ) );
219 /* Remove deleted logical streams */
220 for(int i = 0; i < p_sys->i_del_streams; i++ )
222 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
223 FREENULL( p_sys->pp_del_streams[i]->p_oggds_header );
224 FREENULL( p_sys->pp_del_streams[i] );
226 FREENULL( p_sys->pp_del_streams );
227 p_sys->i_streams -= p_sys->i_del_streams;
230 OggSetDate( p_og, i_dts, 0 );
231 sout_AccessOutWrite( p_mux->p_access, p_og );
237 /*****************************************************************************
239 *****************************************************************************/
240 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
248 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
249 pb_bool = (bool*)va_arg( args, bool * );
253 case MUX_GET_ADD_STREAM_WAIT:
254 pb_bool = (bool*)va_arg( args, bool * );
259 ppsz = (char**)va_arg( args, char ** );
260 *ppsz = strdup( "application/ogg" );
267 /*****************************************************************************
268 * AddStream: Add an elementary stream to the muxed stream
269 *****************************************************************************/
270 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
272 sout_mux_sys_t *p_sys = p_mux->p_sys;
273 ogg_stream_t *p_stream;
276 msg_Dbg( p_mux, "adding input" );
278 p_input->p_sys = p_stream = calloc( 1, sizeof( ogg_stream_t ) );
282 p_stream->i_cat = p_input->p_fmt->i_cat;
283 p_stream->i_fourcc = p_input->p_fmt->i_codec;
284 p_stream->i_serial_no = p_sys->i_next_serial_no++;
285 p_stream->i_packet_no = 0;
286 p_stream->i_last_keyframe = 0;
287 p_stream->i_num_keyframes = 0;
288 p_stream->i_num_frames = 0;
290 p_stream->p_oggds_header = 0;
292 switch( p_input->p_fmt->i_cat )
295 if( !p_input->p_fmt->video.i_frame_rate ||
296 !p_input->p_fmt->video.i_frame_rate_base )
298 msg_Warn( p_mux, "Missing frame rate, assuming 25fps" );
299 p_input->p_fmt->video.i_frame_rate = 25;
300 p_input->p_fmt->video.i_frame_rate_base = 1;
303 switch( p_stream->i_fourcc )
312 p_stream->p_oggds_header = calloc( 1, sizeof(oggds_header_t) );
313 if( !p_stream->p_oggds_header )
318 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
320 memcpy( p_stream->p_oggds_header->stream_type, "video", 5 );
321 if( p_stream->i_fourcc == VLC_CODEC_MP4V )
323 memcpy( p_stream->p_oggds_header->sub_type, "XVID", 4 );
325 else if( p_stream->i_fourcc == VLC_CODEC_DIV3 )
327 memcpy( p_stream->p_oggds_header->sub_type, "DIV3", 4 );
331 memcpy( p_stream->p_oggds_header->sub_type,
332 &p_stream->i_fourcc, 4 );
334 p_stream->p_oggds_header->i_size = 0 ;
335 p_stream->p_oggds_header->i_time_unit =
336 INT64_C(10000000) * p_input->p_fmt->video.i_frame_rate_base /
337 (int64_t)p_input->p_fmt->video.i_frame_rate;
338 p_stream->p_oggds_header->i_samples_per_unit = 1;
339 p_stream->p_oggds_header->i_default_len = 1 ; /* ??? */
340 p_stream->p_oggds_header->i_buffer_size = 1024*1024;
341 p_stream->p_oggds_header->i_bits_per_sample = 0;
342 p_stream->p_oggds_header->header.video.i_width = p_input->p_fmt->video.i_width;
343 p_stream->p_oggds_header->header.video.i_height = p_input->p_fmt->video.i_height;
344 msg_Dbg( p_mux, "%4.4s stream", (char *)&p_stream->i_fourcc );
347 case VLC_CODEC_DIRAC:
348 msg_Dbg( p_mux, "dirac stream" );
351 case VLC_CODEC_THEORA:
352 msg_Dbg( p_mux, "theora stream" );
356 FREENULL( p_input->p_sys );
362 switch( p_stream->i_fourcc )
365 msg_Dbg( p_mux, "opus stream" );
368 case VLC_CODEC_VORBIS:
369 msg_Dbg( p_mux, "vorbis stream" );
372 case VLC_CODEC_SPEEX:
373 msg_Dbg( p_mux, "speex stream" );
377 msg_Dbg( p_mux, "flac stream" );
381 fourcc_to_wf_tag( p_stream->i_fourcc, &i_tag );
382 if( i_tag == WAVE_FORMAT_UNKNOWN )
384 FREENULL( p_input->p_sys );
388 p_stream->p_oggds_header =
389 malloc( sizeof(oggds_header_t) + p_input->p_fmt->i_extra );
390 if( !p_stream->p_oggds_header )
395 memset( p_stream->p_oggds_header, 0, sizeof(oggds_header_t) );
396 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
398 p_stream->p_oggds_header->i_size = p_input->p_fmt->i_extra;
400 if( p_input->p_fmt->i_extra )
402 memcpy( &p_stream->p_oggds_header[1],
403 p_input->p_fmt->p_extra, p_input->p_fmt->i_extra );
406 memcpy( p_stream->p_oggds_header->stream_type, "audio", 5 );
408 memset( p_stream->p_oggds_header->sub_type, 0, 4 );
410 snprintf( buf, sizeof(buf), "%"PRIx16, i_tag );
411 strncpy( p_stream->p_oggds_header->sub_type, buf, 4 );
413 p_stream->p_oggds_header->i_time_unit = INT64_C(10000000);
414 p_stream->p_oggds_header->i_default_len = 1;
415 p_stream->p_oggds_header->i_buffer_size = 30*1024 ;
416 p_stream->p_oggds_header->i_samples_per_unit = p_input->p_fmt->audio.i_rate;
417 p_stream->p_oggds_header->i_bits_per_sample = p_input->p_fmt->audio.i_bitspersample;
418 p_stream->p_oggds_header->header.audio.i_channels = p_input->p_fmt->audio.i_channels;
419 p_stream->p_oggds_header->header.audio.i_block_align = p_input->p_fmt->audio.i_blockalign;
420 p_stream->p_oggds_header->header.audio.i_avgbytespersec = p_input->p_fmt->i_bitrate / 8;
421 msg_Dbg( p_mux, "%4.4s stream", (char *)&p_stream->i_fourcc );
427 switch( p_stream->i_fourcc )
430 p_stream->p_oggds_header = calloc( 1, sizeof(oggds_header_t) );
431 if( !p_stream->p_oggds_header )
436 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
438 memcpy( p_stream->p_oggds_header->stream_type, "text", 4 );
439 msg_Dbg( p_mux, "subtitles stream" );
443 FREENULL( p_input->p_sys );
448 FREENULL( p_input->p_sys );
452 p_stream->b_new = true;
454 p_sys->i_add_streams++;
459 /*****************************************************************************
460 * DelStream: Delete an elementary stream from the muxed stream
461 *****************************************************************************/
462 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
464 sout_mux_sys_t *p_sys = p_mux->p_sys;
465 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
468 msg_Dbg( p_mux, "removing input" );
470 /* flush all remaining data */
473 if( !p_stream->b_new )
475 while( block_FifoCount( p_input->p_fifo ) )
476 MuxBlock( p_mux, p_input );
479 if( !p_stream->b_new &&
480 ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
482 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
483 sout_AccessOutWrite( p_mux->p_access, p_og );
486 /* move input in delete queue */
487 if( !p_stream->b_new )
489 p_sys->pp_del_streams = xrealloc( p_sys->pp_del_streams,
490 (p_sys->i_del_streams + 1) * sizeof(ogg_stream_t *) );
491 p_sys->pp_del_streams[p_sys->i_del_streams++] = p_stream;
495 /* wasn't already added so get rid of it */
496 FREENULL( p_stream->p_oggds_header );
497 FREENULL( p_stream );
498 p_sys->i_add_streams--;
502 p_input->p_sys = NULL;
507 /*****************************************************************************
508 * Ogg bitstream manipulation routines
509 *****************************************************************************/
510 static block_t *OggStreamGetPage( sout_mux_t *p_mux,
511 ogg_stream_state *p_os, mtime_t i_pts,
515 block_t *p_og, *p_og_first = NULL;
517 int (*pager)( ogg_stream_state*, ogg_page* ) = flush ? ogg_stream_flush : ogg_stream_pageout;
519 while( pager( p_os, &og ) )
522 p_og = block_Alloc( og.header_len + og.body_len );
524 memcpy( p_og->p_buffer, og.header, og.header_len );
525 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
530 i_pts = 0; // write it only once
532 block_ChainAppend( &p_og_first, p_og );
538 static block_t *OggStreamFlush( sout_mux_t *p_mux,
539 ogg_stream_state *p_os, mtime_t i_pts )
541 return OggStreamGetPage( p_mux, p_os, i_pts, true );
544 static block_t *OggStreamPageOut( sout_mux_t *p_mux,
545 ogg_stream_state *p_os, mtime_t i_pts )
547 return OggStreamGetPage( p_mux, p_os, i_pts, false );
550 static int32_t OggFillDsHeader( uint8_t *p_buffer, oggds_header_t *p_oggds_header, int i_cat )
553 p_buffer[index] = p_oggds_header->i_packet_type;
555 memcpy( &p_buffer[index], p_oggds_header->stream_type, sizeof(p_oggds_header->stream_type) );
556 index += sizeof(p_oggds_header->stream_type);
557 memcpy(&p_buffer[index], p_oggds_header->sub_type, sizeof(p_oggds_header->sub_type) );
558 index += sizeof(p_oggds_header->sub_type);
560 /* The size is filled at the end */
561 uint8_t *p_isize = &p_buffer[index];
564 SetQWLE( &p_buffer[index], p_oggds_header->i_time_unit );
566 SetQWLE( &p_buffer[index], p_oggds_header->i_samples_per_unit );
568 SetDWLE( &p_buffer[index], p_oggds_header->i_default_len );
570 SetDWLE( &p_buffer[index], p_oggds_header->i_buffer_size );
572 SetWLE( &p_buffer[index], p_oggds_header->i_bits_per_sample );
574 SetWLE( &p_buffer[index], p_oggds_header->i_padding_0 );
580 SetDWLE( &p_buffer[index], p_oggds_header->header.video.i_width );
581 SetDWLE( &p_buffer[index+4], p_oggds_header->header.video.i_height );
584 SetWLE( &p_buffer[index], p_oggds_header->header.audio.i_channels );
585 SetWLE( &p_buffer[index+2], p_oggds_header->header.audio.i_block_align );
586 SetDWLE( &p_buffer[index+4], p_oggds_header->header.audio.i_avgbytespersec );
590 SetDWLE( &p_buffer[index], p_oggds_header->i_padding_1 );
594 if( p_oggds_header->i_size > 0 )
596 memcpy( &p_buffer[index], p_oggds_header + sizeof(*p_oggds_header), p_oggds_header->i_size );
597 index += p_oggds_header->i_size;
600 SetDWLE( p_isize, index-1 );
604 static block_t *OggCreateHeader( sout_mux_t *p_mux )
606 block_t *p_hdr = NULL;
607 block_t *p_og = NULL;
611 /* Write header for each stream. All b_o_s (beginning of stream) packets
612 * must appear first in the ogg stream so we take care of them first. */
613 for( int pass = 0; pass < 2; pass++ )
615 for( i = 0; i < p_mux->i_nb_inputs; i++ )
617 sout_input_t *p_input = p_mux->pp_inputs[i];
618 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
620 bool video = ( p_stream->i_fourcc == VLC_CODEC_THEORA || p_stream->i_fourcc == VLC_CODEC_DIRAC );
621 if( ( ( pass == 0 && !video ) || ( pass == 1 && video ) ) )
624 msg_Dbg( p_mux, "creating header for %4.4s",
625 (char *)&p_stream->i_fourcc );
627 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
628 p_stream->b_new = false;
629 p_stream->i_packet_no = 0;
631 if( p_stream->i_fourcc == VLC_CODEC_VORBIS ||
632 p_stream->i_fourcc == VLC_CODEC_SPEEX ||
633 p_stream->i_fourcc == VLC_CODEC_OPUS ||
634 p_stream->i_fourcc == VLC_CODEC_THEORA )
636 /* First packet in order: vorbis/speex/theora info */
637 unsigned pi_size[XIPH_MAX_HEADER_COUNT];
638 void *pp_data[XIPH_MAX_HEADER_COUNT];
640 if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
641 p_input->p_fmt->i_extra, p_input->p_fmt->p_extra ) )
648 op.bytes = pi_size[0];
649 op.packet = pp_data[0];
650 if( pi_size[0] <= 0 )
651 msg_Err( p_mux, "header data corrupted");
656 op.packetno = p_stream->i_packet_no++;
657 ogg_stream_packetin( &p_stream->os, &op );
658 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
660 /* Get keyframe_granule_shift for theora granulepos calculation */
661 if( p_stream->i_fourcc == VLC_CODEC_THEORA )
663 p_stream->i_keyframe_granule_shift =
664 ( (op.packet[40] & 0x03) << 3 ) | ( (op.packet[41] & 0xe0) >> 5 );
667 for( unsigned i = 0; i < i_count; i++ )
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 = malloc( sizeof(*p_stream->p_oggds_header) + p_stream->p_oggds_header->i_size );
699 op.bytes = OggFillDsHeader( op.packet, p_stream->p_oggds_header, p_stream->i_cat );
703 op.packetno = p_stream->i_packet_no++;
704 ogg_stream_packetin( &p_stream->os, &op );
705 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
709 block_ChainAppend( &p_hdr, p_og );
713 /* Take care of the non b_o_s headers */
714 for( i = 0; i < p_mux->i_nb_inputs; i++ )
716 sout_input_t *p_input = p_mux->pp_inputs[i];
717 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
719 if( p_stream->i_fourcc == VLC_CODEC_VORBIS ||
720 p_stream->i_fourcc == VLC_CODEC_SPEEX ||
721 p_stream->i_fourcc == VLC_CODEC_OPUS ||
722 p_stream->i_fourcc == VLC_CODEC_THEORA )
724 unsigned pi_size[XIPH_MAX_HEADER_COUNT];
725 void *pp_data[XIPH_MAX_HEADER_COUNT];
727 if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
728 p_input->p_fmt->i_extra, p_input->p_fmt->p_extra ) )
731 /* Special case, headers are already there in the incoming stream.
732 * We need to gather them an mark them as headers. */
733 for( unsigned i = 1; i < i_count; i++ )
735 op.bytes = pi_size[i];
736 op.packet = pp_data[i];
737 if( pi_size[i] <= 0 )
738 msg_Err( p_mux, "header data corrupted");
743 op.packetno = p_stream->i_packet_no++;
744 ogg_stream_packetin( &p_stream->os, &op );
746 if( i == i_count - 1 )
747 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
749 p_og = OggStreamPageOut( p_mux, &p_stream->os, 0 );
751 block_ChainAppend( &p_hdr, p_og );
753 for( unsigned i = 0; i < i_count; i++ )
756 else if( p_stream->i_fourcc != VLC_CODEC_FLAC &&
757 p_stream->i_fourcc != VLC_CODEC_DIRAC )
763 com[0] = PACKET_TYPE_COMMENT;
764 i_com = snprintf( (char *)(com+1), 127,
765 PACKAGE_VERSION" stream output" )
772 op.packetno = p_stream->i_packet_no++;
773 ogg_stream_packetin( &p_stream->os, &op );
774 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
775 block_ChainAppend( &p_hdr, p_og );
778 /* Special case for mp4v and flac */
779 if( ( p_stream->i_fourcc == VLC_CODEC_MP4V ||
780 p_stream->i_fourcc == VLC_CODEC_FLAC ) &&
781 p_input->p_fmt->i_extra )
783 /* Send a packet with the VOL data for mp4v
784 * or STREAMINFO for flac */
785 msg_Dbg( p_mux, "writing extra data" );
786 op.bytes = p_input->p_fmt->i_extra;
787 op.packet = p_input->p_fmt->p_extra;
788 uint8_t flac_streaminfo[34 + 4];
789 if( p_stream->i_fourcc == VLC_CODEC_FLAC )
791 if (op.bytes == 42 && !memcmp(op.packet, "fLaC", 4)) {
793 memcpy(flac_streaminfo, op.packet + 4, 38);
794 op.packet = flac_streaminfo;
795 } else if (op.bytes == 34) {
797 memcpy(flac_streaminfo + 4, op.packet, 34);
798 flac_streaminfo[0] = 0x80; /* last block, streaminfo */
799 flac_streaminfo[1] = 0;
800 flac_streaminfo[2] = 0;
801 flac_streaminfo[3] = 34; /* block size */
802 op.packet = flac_streaminfo;
804 msg_Err(p_mux, "Invalid FLAC streaminfo (%ld bytes)",
811 op.packetno = p_stream->i_packet_no++;
812 ogg_stream_packetin( &p_stream->os, &op );
813 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
814 block_ChainAppend( &p_hdr, p_og );
818 /* set HEADER flag */
819 for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
821 p_og->i_flags |= BLOCK_FLAG_HEADER;
826 static block_t *OggCreateFooter( sout_mux_t *p_mux )
828 sout_mux_sys_t *p_sys = p_mux->p_sys;
829 block_t *p_hdr = NULL;
834 /* flush all remaining data */
835 for( i = 0; i < p_mux->i_nb_inputs; i++ )
837 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
839 /* skip newly added streams */
840 if( p_stream->b_new ) continue;
842 if( ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
844 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
845 sout_AccessOutWrite( p_mux->p_access, p_og );
849 /* Write eos packets for each stream. */
850 for( i = 0; i < p_mux->i_nb_inputs; i++ )
852 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
854 /* skip newly added streams */
855 if( p_stream->b_new ) continue;
861 op.granulepos = p_stream->u_last_granulepos;
862 op.packetno = p_stream->i_packet_no++;
863 ogg_stream_packetin( &p_stream->os, &op );
865 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
866 block_ChainAppend( &p_hdr, p_og );
867 ogg_stream_clear( &p_stream->os );
870 for( i = 0; i < p_sys->i_del_streams; i++ )
876 op.granulepos = p_sys->pp_del_streams[i]->u_last_granulepos;
877 op.packetno = p_sys->pp_del_streams[i]->i_packet_no++;
878 ogg_stream_packetin( &p_sys->pp_del_streams[i]->os, &op );
880 p_og = OggStreamFlush( p_mux, &p_sys->pp_del_streams[i]->os, 0 );
881 block_ChainAppend( &p_hdr, p_og );
882 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
888 static void OggSetDate( block_t *p_og, mtime_t i_dts, mtime_t i_length )
894 for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
899 if( i_count == 0 ) return; /* ignore. */
901 i_delta = i_length / i_count;
903 for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
905 p_tmp->i_dts = i_dts;
906 p_tmp->i_length = i_delta;
912 /*****************************************************************************
913 * Mux: multiplex available data in input fifos into the Ogg bitstream
914 *****************************************************************************/
915 static int Mux( sout_mux_t *p_mux )
917 sout_mux_sys_t *p_sys = p_mux->p_sys;
918 block_t *p_og = NULL;
921 if( p_sys->i_add_streams || p_sys->i_del_streams )
923 /* Open new ogg stream */
924 if( sout_MuxGetStream( p_mux, 1, &i_dts) < 0 )
926 msg_Dbg( p_mux, "waiting for data..." );
930 if( p_sys->i_streams )
932 /* Close current ogg stream */
935 msg_Dbg( p_mux, "writing footer" );
936 block_ChainAppend( &p_og, OggCreateFooter( p_mux ) );
938 /* Remove deleted logical streams */
939 for( i = 0; i < p_sys->i_del_streams; i++ )
941 FREENULL( p_sys->pp_del_streams[i]->p_oggds_header );
942 FREENULL( p_sys->pp_del_streams[i] );
944 FREENULL( p_sys->pp_del_streams );
945 p_sys->i_streams = 0;
948 msg_Dbg( p_mux, "writing header" );
949 p_sys->i_start_dts = i_dts;
950 p_sys->i_streams = p_mux->i_nb_inputs;
951 p_sys->i_del_streams = 0;
952 p_sys->i_add_streams = 0;
953 block_t *p_header = OggCreateHeader( p_mux );
956 block_ChainAppend( &p_og, p_header );
958 /* Write header and/or footer */
959 OggSetDate( p_og, i_dts, 0 );
960 sout_AccessOutWrite( p_mux->p_access, p_og );
966 int i_stream = sout_MuxGetStream( p_mux, 1, NULL );
969 MuxBlock( p_mux, p_mux->pp_inputs[i_stream] );
975 static int MuxBlock( sout_mux_t *p_mux, sout_input_t *p_input )
977 sout_mux_sys_t *p_sys = p_mux->p_sys;
978 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
979 block_t *p_data = block_FifoGet( p_input->p_fifo );
980 block_t *p_og = NULL;
983 if( p_stream->i_fourcc != VLC_CODEC_VORBIS &&
984 p_stream->i_fourcc != VLC_CODEC_FLAC &&
985 p_stream->i_fourcc != VLC_CODEC_SPEEX &&
986 p_stream->i_fourcc != VLC_CODEC_OPUS &&
987 p_stream->i_fourcc != VLC_CODEC_THEORA &&
988 p_stream->i_fourcc != VLC_CODEC_DIRAC )
990 p_data = block_Realloc( p_data, 1, p_data->i_buffer );
991 p_data->p_buffer[0] = PACKET_IS_SYNCPOINT; // FIXME
994 op.packet = p_data->p_buffer;
995 op.bytes = p_data->i_buffer;
998 op.packetno = p_stream->i_packet_no++;
1001 if( p_stream->i_cat == AUDIO_ES )
1003 if( p_stream->i_fourcc == VLC_CODEC_VORBIS ||
1004 p_stream->i_fourcc == VLC_CODEC_FLAC ||
1005 p_stream->i_fourcc == VLC_CODEC_OPUS ||
1006 p_stream->i_fourcc == VLC_CODEC_SPEEX )
1008 /* number of sample from begining + current packet */
1010 ( p_data->i_dts - p_sys->i_start_dts + p_data->i_length ) *
1011 (mtime_t)p_input->p_fmt->audio.i_rate / INT64_C(1000000);
1013 else if( p_stream->p_oggds_header )
1015 /* number of sample from begining */
1016 op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) *
1017 p_stream->p_oggds_header->i_samples_per_unit / INT64_C(1000000);
1020 else if( p_stream->i_cat == VIDEO_ES )
1022 if( p_stream->i_fourcc == VLC_CODEC_THEORA )
1024 p_stream->i_num_frames++;
1025 if( p_data->i_flags & BLOCK_FLAG_TYPE_I )
1027 p_stream->i_num_keyframes++;
1028 p_stream->i_last_keyframe = p_stream->i_num_frames;
1031 op.granulepos = (p_stream->i_last_keyframe << p_stream->i_keyframe_granule_shift )
1032 | (p_stream->i_num_frames-p_stream->i_last_keyframe);
1034 else if( p_stream->i_fourcc == VLC_CODEC_DIRAC )
1036 mtime_t dt = (p_data->i_dts - p_sys->i_start_dts + 1)
1037 * p_input->p_fmt->video.i_frame_rate *2
1038 / p_input->p_fmt->video.i_frame_rate_base
1040 mtime_t delay = (p_data->i_pts - p_data->i_dts + 1)
1041 * p_input->p_fmt->video.i_frame_rate *2
1042 / p_input->p_fmt->video.i_frame_rate_base
1044 if( p_data->i_flags & BLOCK_FLAG_TYPE_I )
1045 p_stream->i_last_keyframe = dt;
1046 mtime_t dist = dt - p_stream->i_last_keyframe;
1047 op.granulepos = dt << 31 | (dist&0xff00) << 14
1048 | (delay&0x1fff) << 9 | (dist&0xff);
1050 else if( p_stream->p_oggds_header )
1051 op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) * INT64_C(10) /
1052 p_stream->p_oggds_header->i_time_unit;
1054 else if( p_stream->i_cat == SPU_ES )
1056 /* granulepos is in millisec */
1057 op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) / 1000;
1060 return VLC_EGENERIC;
1062 p_stream->u_last_granulepos = op.granulepos;
1063 ogg_stream_packetin( &p_stream->os, &op );
1065 if( p_stream->i_cat == SPU_ES ||
1066 p_stream->i_fourcc == VLC_CODEC_SPEEX ||
1067 p_stream->i_fourcc == VLC_CODEC_DIRAC )
1069 /* Subtitles or Speex packets are quite small so they
1070 * need to be flushed to be sent on time */
1071 /* The OggDirac mapping suggests ever so strongly that a
1072 * page flush occurs after each OggDirac packet, so to make
1073 * the timestamps unambiguous */
1074 p_og = OggStreamFlush( p_mux, &p_stream->os, p_data->i_dts );
1078 p_og = OggStreamPageOut( p_mux, &p_stream->os, p_data->i_dts );
1083 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
1084 p_stream->i_dts = -1;
1085 p_stream->i_length = 0;
1087 sout_AccessOutWrite( p_mux->p_access, p_og );
1091 if( p_stream->i_dts < 0 )
1093 p_stream->i_dts = p_data->i_dts;
1095 p_stream->i_length += p_data->i_length;
1098 block_Release( p_data );