1 /*****************************************************************************
2 * ogg.c: ogg muxer module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001, 2002, 2006 VLC authors and VideoLAN
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 it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * 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 #define INDEXINTVL_TEXT N_("Index interval")
48 #define INDEXINTVL_LONGTEXT N_("Minimal index interval, in microseconds. " \
49 "Set to 0 to disable index creation.")
50 #define INDEXRATIO_TEXT N_("Index size ratio")
51 #define INDEXRATIO_LONGTEXT N_(\
52 "Set index size ratio. Alters default (60min content) or estimated size." )
54 static int Open ( vlc_object_t * );
55 static void Close ( vlc_object_t * );
57 #define SOUT_CFG_PREFIX "sout-ogg-"
60 set_description( N_("Ogg/OGM muxer") )
61 set_capability( "sout mux", 10 )
62 set_category( CAT_SOUT )
63 set_subcategory( SUBCAT_SOUT_MUX )
64 add_shortcut( "ogg", "ogm" )
65 add_integer_with_range( SOUT_CFG_PREFIX "indexintvl", 1000, 0, INT_MAX,
66 INDEXINTVL_TEXT, INDEXINTVL_LONGTEXT, true )
67 add_float_with_range( SOUT_CFG_PREFIX "indexratio", 1.0, 1.0, 1000,
68 INDEXRATIO_TEXT, INDEXRATIO_LONGTEXT, true )
69 set_callbacks( Open, Close )
73 /*****************************************************************************
75 *****************************************************************************/
76 static int Control ( sout_mux_t *, int, va_list );
77 static int AddStream( sout_mux_t *, sout_input_t * );
78 static int DelStream( sout_mux_t *, sout_input_t * );
79 static int Mux ( sout_mux_t * );
80 static int MuxBlock ( sout_mux_t *, sout_input_t * );
82 static bool OggCreateHeaders( sout_mux_t * );
83 static void OggFillSkeletonFishead( uint8_t *p_buffer, sout_mux_t *p_mux );
85 /*****************************************************************************
87 *****************************************************************************/
90 #define FISBONE_BASE_SIZE 52
91 #define FISBONE_BASE_OFFSET 44
92 #define INDEX_BASE_SIZE 42
94 /* Structures used for OggDS headers used in ogm files */
96 #define PACKET_TYPE_HEADER 0x01
97 #define PACKET_TYPE_COMMENT 0x03
98 #define PACKET_IS_SYNCPOINT 0x08
104 } oggds_header_video_t;
109 int16_t i_block_align;
110 int32_t i_avgbytespersec;
111 } oggds_header_audio_t;
115 uint8_t i_packet_type;
123 int64_t i_samples_per_unit;
124 int32_t i_default_len;
126 int32_t i_buffer_size;
127 int16_t i_bits_per_sample;
129 int16_t i_padding_0; /* Because the original is using MSVC packing style */
133 oggds_header_video_t video;
134 oggds_header_audio_t audio;
137 int32_t i_padding_1; /* Because the original is using MSVC packing style */
141 /*****************************************************************************
142 * Definitions of structures and functions used by this plugins
143 *****************************************************************************/
147 vlc_fourcc_t i_fourcc;
155 int i_keyframe_granule_shift; /* Theora only */
156 int i_last_keyframe; /* dirac and theora */
157 int i_num_frames; /* Theora only */
158 uint64_t u_last_granulepos; /* Used for correct EOS page */
159 int64_t i_num_keyframes;
162 oggds_header_t *p_oggds_header;
170 /* Skeleton index storage */
171 unsigned char *p_index;
172 uint64_t i_index_size;
173 uint64_t i_index_payload; /* real index size */
174 uint64_t i_index_count;
175 /* backup values for rewriting index page later */
176 uint64_t i_index_offset; /* sout offset of the index page */
177 int64_t i_index_packetno; /* index packet no */
179 /* index creation tracking values */
180 uint64_t i_last_keyframe_pos;
181 uint64_t i_last_keyframe_time;
186 mtime_t i_baseptsdelay;
190 struct sout_mux_sys_t
195 int i_next_serial_no;
197 /* number of logical streams pending to be added */
199 bool b_can_add_streams;
201 /* logical streams pending to be deleted */
203 ogg_stream_t **pp_del_streams;
213 /* backup values for rewriting fishead page later */
214 uint64_t i_fishead_offset; /* sout offset of the fishead page */
219 /* access position */
221 ssize_t i_data_start;
222 ssize_t i_segment_start;
225 static void OggSetDate( block_t *, mtime_t , mtime_t );
226 static block_t *OggStreamFlush( sout_mux_t *, ogg_stream_state *, mtime_t );
227 static void OggCreateStreamFooter( sout_mux_t *p_mux, ogg_stream_t *p_stream );
228 static void OggRewriteFisheadPage( sout_mux_t *p_mux );
229 static bool AllocateIndex( sout_mux_t *p_mux, sout_input_t *p_input );
231 /*****************************************************************************
233 *****************************************************************************/
234 static int Open( vlc_object_t *p_this )
236 sout_mux_t *p_mux = (sout_mux_t*)p_this;
237 sout_mux_sys_t *p_sys;
239 msg_Info( p_mux, "Open" );
241 p_sys = malloc( sizeof( sout_mux_sys_t ) );
244 p_sys->i_streams = 0;
245 p_sys->i_add_streams = 0;
246 p_sys->b_can_add_streams = true;
247 p_sys->i_del_streams = 0;
248 p_sys->pp_del_streams = 0;
250 p_sys->skeleton.b_create = false;
251 p_sys->skeleton.b_head_done = false;
252 p_sys->skeleton.i_index_intvl =
253 var_InheritInteger( p_this, SOUT_CFG_PREFIX "indexintvl" );
254 p_sys->skeleton.i_index_ratio =
255 var_InheritFloat( p_this, SOUT_CFG_PREFIX "indexratio" );
256 p_sys->i_data_start = 0;
257 p_sys->i_segment_start = 0;
258 p_mux->p_sys = p_sys;
259 p_mux->pf_control = Control;
260 p_mux->pf_addstream = AddStream;
261 p_mux->pf_delstream = DelStream;
264 /* First serial number is random.
265 * (Done like this because on win32 you need to seed the random number
266 * generator once per thread). */
268 vlc_rand_bytes(&r, sizeof(r));
269 p_sys->i_next_serial_no = r & INT_MAX;
274 /*****************************************************************************
275 * Close: Finalize ogg bitstream and close muxer
276 *****************************************************************************/
277 static void Close( vlc_object_t * p_this )
279 sout_mux_t *p_mux = (sout_mux_t*)p_this;
280 sout_mux_sys_t *p_sys = p_mux->p_sys;
281 ogg_stream_t *p_stream;
283 msg_Info( p_mux, "Close" );
285 if( p_sys->i_del_streams )
287 /* Close the current ogg stream */
288 msg_Dbg( p_mux, "writing footers" );
290 for(int i = 0; i < p_mux->i_nb_inputs; i++ )
292 p_stream = (ogg_stream_t *) p_mux->pp_inputs[i]->p_sys;
293 OggCreateStreamFooter( p_mux, p_stream );
294 free( p_stream->skeleton.p_index );
297 /* Remove deleted logical streams */
298 for(int i = 0; i < p_sys->i_del_streams; i++ )
300 OggCreateStreamFooter( p_mux, p_sys->pp_del_streams[i] );
301 free( p_sys->pp_del_streams[i]->p_oggds_header );
302 free( p_sys->pp_del_streams[i]->skeleton.p_index );
303 free( p_sys->pp_del_streams[i] );
305 free( p_sys->pp_del_streams );
306 p_sys->i_streams -= p_sys->i_del_streams;
309 /* rewrite fishead with final values */
310 if ( p_sys->skeleton.b_create && p_sys->skeleton.b_head_done )
312 OggRewriteFisheadPage( p_mux );
318 /*****************************************************************************
320 *****************************************************************************/
321 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
329 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
330 pb_bool = (bool*)va_arg( args, bool * );
334 case MUX_GET_ADD_STREAM_WAIT:
335 pb_bool = (bool*)va_arg( args, bool * );
340 ppsz = (char**)va_arg( args, char ** );
341 *ppsz = strdup( "application/ogg" );
348 /*****************************************************************************
349 * AddStream: Add an elementary stream to the muxed stream
350 *****************************************************************************/
351 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
353 sout_mux_sys_t *p_sys = p_mux->p_sys;
354 ogg_stream_t *p_stream;
357 msg_Dbg( p_mux, "adding input" );
359 p_input->p_sys = p_stream = calloc( 1, sizeof( ogg_stream_t ) );
363 p_stream->i_cat = p_input->p_fmt->i_cat;
364 p_stream->i_fourcc = p_input->p_fmt->i_codec;
365 p_stream->i_serial_no = p_sys->i_next_serial_no++;
366 p_stream->i_packet_no = 0;
367 p_stream->i_last_keyframe = 0;
368 p_stream->i_num_keyframes = 0;
369 p_stream->i_num_frames = 0;
371 p_stream->p_oggds_header = 0;
373 p_stream->i_baseptsdelay = -1;
374 p_stream->i_dirac_last_pt = -1;
375 p_stream->i_dirac_last_dt = -1;
377 switch( p_input->p_fmt->i_cat )
380 if( !p_input->p_fmt->video.i_frame_rate ||
381 !p_input->p_fmt->video.i_frame_rate_base )
383 msg_Warn( p_mux, "Missing frame rate, assuming 25fps" );
384 p_input->p_fmt->video.i_frame_rate = 25;
385 p_input->p_fmt->video.i_frame_rate_base = 1;
388 switch( p_stream->i_fourcc )
397 p_stream->p_oggds_header = calloc( 1, sizeof(oggds_header_t) );
398 if( !p_stream->p_oggds_header )
403 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
405 memcpy( p_stream->p_oggds_header->stream_type, "video", 5 );
406 if( p_stream->i_fourcc == VLC_CODEC_MP4V )
408 memcpy( p_stream->p_oggds_header->sub_type, "XVID", 4 );
410 else if( p_stream->i_fourcc == VLC_CODEC_DIV3 )
412 memcpy( p_stream->p_oggds_header->sub_type, "DIV3", 4 );
416 memcpy( p_stream->p_oggds_header->sub_type,
417 &p_stream->i_fourcc, 4 );
419 p_stream->p_oggds_header->i_size = 0 ;
420 p_stream->p_oggds_header->i_time_unit =
421 INT64_C(10000000) * p_input->p_fmt->video.i_frame_rate_base /
422 (int64_t)p_input->p_fmt->video.i_frame_rate;
423 p_stream->p_oggds_header->i_samples_per_unit = 1;
424 p_stream->p_oggds_header->i_default_len = 1 ; /* ??? */
425 p_stream->p_oggds_header->i_buffer_size = 1024*1024;
426 p_stream->p_oggds_header->i_bits_per_sample = 0;
427 p_stream->p_oggds_header->header.video.i_width = p_input->p_fmt->video.i_width;
428 p_stream->p_oggds_header->header.video.i_height = p_input->p_fmt->video.i_height;
429 msg_Dbg( p_mux, "%4.4s stream", (char *)&p_stream->i_fourcc );
432 case VLC_CODEC_DIRAC:
433 msg_Dbg( p_mux, "dirac stream" );
436 case VLC_CODEC_THEORA:
437 msg_Dbg( p_mux, "theora stream" );
441 msg_Dbg( p_mux, "VP8 stream" );
445 FREENULL( p_input->p_sys );
451 switch( p_stream->i_fourcc )
454 msg_Dbg( p_mux, "opus stream" );
457 case VLC_CODEC_VORBIS:
458 msg_Dbg( p_mux, "vorbis stream" );
461 case VLC_CODEC_SPEEX:
462 msg_Dbg( p_mux, "speex stream" );
466 msg_Dbg( p_mux, "flac stream" );
470 fourcc_to_wf_tag( p_stream->i_fourcc, &i_tag );
471 if( i_tag == WAVE_FORMAT_UNKNOWN )
473 FREENULL( p_input->p_sys );
477 p_stream->p_oggds_header =
478 malloc( sizeof(oggds_header_t) + p_input->p_fmt->i_extra );
479 if( !p_stream->p_oggds_header )
484 memset( p_stream->p_oggds_header, 0, sizeof(oggds_header_t) );
485 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
487 p_stream->p_oggds_header->i_size = p_input->p_fmt->i_extra;
489 if( p_input->p_fmt->i_extra )
491 memcpy( &p_stream->p_oggds_header[1],
492 p_input->p_fmt->p_extra, p_input->p_fmt->i_extra );
495 memcpy( p_stream->p_oggds_header->stream_type, "audio", 5 );
497 memset( p_stream->p_oggds_header->sub_type, 0, 4 );
499 snprintf( buf, sizeof(buf), "%"PRIx16, i_tag );
500 strncpy( p_stream->p_oggds_header->sub_type, buf, 4 );
502 p_stream->p_oggds_header->i_time_unit = INT64_C(10000000);
503 p_stream->p_oggds_header->i_default_len = 1;
504 p_stream->p_oggds_header->i_buffer_size = 30*1024 ;
505 p_stream->p_oggds_header->i_samples_per_unit = p_input->p_fmt->audio.i_rate;
506 p_stream->p_oggds_header->i_bits_per_sample = p_input->p_fmt->audio.i_bitspersample;
507 p_stream->p_oggds_header->header.audio.i_channels = p_input->p_fmt->audio.i_channels;
508 p_stream->p_oggds_header->header.audio.i_block_align = p_input->p_fmt->audio.i_blockalign;
509 p_stream->p_oggds_header->header.audio.i_avgbytespersec = p_input->p_fmt->i_bitrate / 8;
510 msg_Dbg( p_mux, "%4.4s stream", (char *)&p_stream->i_fourcc );
516 switch( p_stream->i_fourcc )
519 p_stream->p_oggds_header = calloc( 1, sizeof(oggds_header_t) );
520 if( !p_stream->p_oggds_header )
525 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
527 memcpy( p_stream->p_oggds_header->stream_type, "text", 4 );
528 msg_Dbg( p_mux, "subtitles stream" );
532 FREENULL( p_input->p_sys );
537 FREENULL( p_input->p_sys );
541 p_stream->b_new = true;
543 p_sys->i_add_streams++;
548 /*****************************************************************************
549 * DelStream: Delete an elementary stream from the muxed stream
550 *****************************************************************************/
551 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
553 sout_mux_sys_t *p_sys = p_mux->p_sys;
554 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
557 msg_Dbg( p_mux, "removing input" );
559 /* flush all remaining data */
562 if( !p_stream->b_new )
564 while( block_FifoCount( p_input->p_fifo ) )
565 MuxBlock( p_mux, p_input );
568 if( !p_stream->b_new &&
569 ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
571 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
572 p_mux->p_sys->i_pos += sout_AccessOutWrite( p_mux->p_access, p_og );
575 /* move input in delete queue */
576 if( !p_stream->b_new )
578 p_sys->pp_del_streams = xrealloc( p_sys->pp_del_streams,
579 (p_sys->i_del_streams + 1) * sizeof(ogg_stream_t *) );
580 p_sys->pp_del_streams[p_sys->i_del_streams++] = p_stream;
584 /* wasn't already added so get rid of it */
585 FREENULL( p_stream->p_oggds_header );
586 FREENULL( p_stream );
587 p_sys->i_add_streams--;
591 p_input->p_sys = NULL;
596 /*****************************************************************************
597 * Ogg Skeleton helpers
598 *****************************************************************************/
599 static int WriteQWVariableLE( uint64_t i_64, uint64_t i_offset,
600 uint8_t *p_buffer, int i_buffer_size )
602 uint8_t *p_dest = p_buffer + i_offset;
607 if ( p_dest - p_buffer >= i_buffer_size ) return -1;
609 *p_dest = (uint8_t) ( i_64 & 0x7F );
623 static bool AddIndexEntry( sout_mux_t *p_mux, uint64_t i_time, sout_input_t *p_input )
625 sout_mux_sys_t *p_sys = p_mux->p_sys;
626 ogg_stream_t *p_stream = (ogg_stream_t *) p_input->p_sys;
628 uint64_t i_timedelta;
629 if ( !p_sys->skeleton.b_create || p_mux->p_sys->skeleton.i_index_intvl == 0
630 || !p_stream->skeleton.p_index )
633 if ( p_stream->skeleton.i_last_keyframe_pos == 0 )
634 p_stream->skeleton.i_last_keyframe_pos = p_sys->i_segment_start;
635 i_posdelta = p_sys->i_pos - p_stream->skeleton.i_last_keyframe_pos;
636 i_timedelta = i_time - p_stream->skeleton.i_last_keyframe_time;
638 if ( i_timedelta <= ( (uint64_t) p_mux->p_sys->skeleton.i_index_intvl * 1000 )
639 || i_posdelta <= 0xFFFF )
644 if ( !p_stream->skeleton.p_index ) return false;
645 uint64_t i_offset = p_stream->skeleton.i_index_payload;
646 i_ret = WriteQWVariableLE( i_posdelta, i_offset, p_stream->skeleton.p_index,
647 p_stream->skeleton.i_index_size );
648 if ( i_ret == -1 ) return false;
650 i_ret = WriteQWVariableLE( i_timedelta, i_offset, p_stream->skeleton.p_index,
651 p_stream->skeleton.i_index_size );
652 if ( i_ret == -1 ) return false;
653 p_stream->skeleton.i_index_payload = i_offset + i_ret;
654 p_stream->skeleton.i_index_count++;
656 /* update diff points */
657 p_stream->skeleton.i_last_keyframe_pos = p_sys->i_pos;
658 p_stream->skeleton.i_last_keyframe_time = i_time;
659 msg_Dbg( p_mux, "Added index on stream %d entry %zd %"PRIu64,
660 p_stream->i_serial_no, p_sys->i_pos - p_sys->i_segment_start, i_time );
665 /*****************************************************************************
666 * Ogg bitstream manipulation routines
667 *****************************************************************************/
668 static block_t *OggStreamGetPage( sout_mux_t *p_mux,
669 ogg_stream_state *p_os, mtime_t i_pts,
673 block_t *p_og, *p_og_first = NULL;
675 int (*pager)( ogg_stream_state*, ogg_page* ) = flush ? ogg_stream_flush : ogg_stream_pageout;
677 while( pager( p_os, &og ) )
680 p_og = block_Alloc( og.header_len + og.body_len );
682 memcpy( p_og->p_buffer, og.header, og.header_len );
683 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
688 i_pts = 0; // write it only once
690 block_ChainAppend( &p_og_first, p_og );
696 static block_t *OggStreamFlush( sout_mux_t *p_mux,
697 ogg_stream_state *p_os, mtime_t i_pts )
699 return OggStreamGetPage( p_mux, p_os, i_pts, true );
702 static block_t *OggStreamPageOut( sout_mux_t *p_mux,
703 ogg_stream_state *p_os, mtime_t i_pts )
705 return OggStreamGetPage( p_mux, p_os, i_pts, false );
708 static void OggGetSkeletonIndex( uint8_t **pp_buffer, long *pi_size, ogg_stream_t *p_stream )
710 uint8_t *p_buffer = calloc( INDEX_BASE_SIZE + p_stream->skeleton.i_index_size, sizeof(uint8_t) );
711 if ( !p_buffer ) return;
712 *pp_buffer = p_buffer;
714 memcpy( p_buffer, "index", 6 );
715 SetDWLE( &p_buffer[6], p_stream->i_serial_no );
716 SetQWLE( &p_buffer[10], p_stream->skeleton.i_index_count ); /* num keypoints */
717 SetQWLE( &p_buffer[18], 1000000 );
718 SetQWLE( &p_buffer[34], p_stream->i_length );
719 memcpy( p_buffer + INDEX_BASE_SIZE, p_stream->skeleton.p_index, p_stream->skeleton.i_index_payload );
720 *pi_size = INDEX_BASE_SIZE + p_stream->skeleton.i_index_size;
723 static void OggGetSkeletonFisbone( uint8_t **pp_buffer, long *pi_size,
724 sout_input_t *p_input, sout_mux_t *p_mux )
728 const char *psz_value = NULL;
729 ogg_stream_t *p_stream = (ogg_stream_t *) p_input->p_sys;
732 char *psz_content_type;
735 unsigned int i_count;
736 } headers = { NULL, NULL, 0, 0 };
739 switch( p_stream->i_fourcc )
741 case VLC_CODEC_VORBIS:
742 psz_value = "audio/vorbis";
744 case VLC_CODEC_THEORA:
745 psz_value = "video/theora";
747 case VLC_CODEC_SPEEX:
748 psz_value = "audio/speex";
751 psz_value = "audio/flac";
754 psz_value = "text/cmml";
757 psz_value = "application/kate";
760 psz_value = "video/x-vp8";
763 psz_value = "application/octet-stream";
764 msg_Warn( p_mux, "Unkown fourcc for stream %s, setting Content-Type to %s",
765 vlc_fourcc_GetDescription( p_stream->i_cat, p_stream->i_fourcc ),
769 /* Content Type Header */
770 if ( asprintf( &headers.psz_content_type, "Content-Type: %s\r\n", psz_value ) != -1 )
772 headers.i_size += strlen( headers.psz_content_type );
776 /* Set Role Header */
777 if ( p_input->p_fmt->i_priority > ES_PRIORITY_NOT_SELECTABLE )
779 int i_max_prio = ES_PRIORITY_MIN;
780 for ( int i=0; i< p_mux->i_nb_inputs; i++ )
782 if ( p_mux->pp_inputs[i]->p_fmt->i_cat != p_input->p_fmt->i_cat ) continue;
783 i_max_prio = __MAX( p_mux->pp_inputs[i]->p_fmt->i_priority, i_max_prio );
787 if ( p_input->p_fmt->i_cat == AUDIO_ES || p_input->p_fmt->i_cat == VIDEO_ES )
789 if ( p_input->p_fmt->i_priority == i_max_prio && i_max_prio >= ES_PRIORITY_SELECTABLE_MIN )
790 psz_value = ( p_input->p_fmt->i_cat == VIDEO_ES ) ?
791 "video/main" : "audio/main";
793 psz_value = ( p_input->p_fmt->i_cat == VIDEO_ES ) ?
794 "video/alternate" : "audio/alternate";
796 else if ( p_input->p_fmt->i_cat == SPU_ES )
798 psz_value = ( p_input->p_fmt->i_codec == VLC_CODEC_KATE ) ?
799 "text/karaoke" : "text/subtitle";
802 if ( psz_value && asprintf( &headers.psz_role, "Role: %s\r\n", psz_value ) != -1 )
804 headers.i_size += strlen( headers.psz_role );
809 *pp_buffer = calloc( FISBONE_BASE_SIZE + headers.i_size, sizeof(uint8_t) );
810 if ( !*pp_buffer ) return;
811 p_buffer = *pp_buffer;
813 memcpy( p_buffer, "fisbone", 8 );
814 SetDWLE( &p_buffer[8], FISBONE_BASE_OFFSET ); /* offset to message headers */
815 SetDWLE( &p_buffer[12], p_stream->i_serial_no );
816 SetDWLE( &p_buffer[16], headers.i_count );
818 /* granulerate den */
819 switch ( p_input->p_fmt->i_cat )
822 SetQWLE( &(*pp_buffer)[20], p_input->p_fmt->video.i_frame_rate );
823 SetQWLE( &(*pp_buffer)[28], p_input->p_fmt->video.i_frame_rate_base );
826 SetQWLE( &(*pp_buffer)[20], p_input->p_fmt->audio.i_rate );
827 SetQWLE( &(*pp_buffer)[28], 1 );
830 SetQWLE( &(*pp_buffer)[20], 1000 );
831 SetQWLE( &(*pp_buffer)[28], 1 );
835 if ( p_input->p_fmt->p_extra )
836 SetDWLE( &(*pp_buffer)[44], xiph_CountHeaders( p_input->p_fmt->p_extra, p_input->p_fmt->i_extra ) );
838 psz_header = *pp_buffer + FISBONE_BASE_SIZE;
839 memcpy( psz_header, headers.psz_content_type, strlen( headers.psz_content_type ) );
840 psz_header += strlen( headers.psz_content_type );
841 if ( headers.psz_role )
842 memcpy( psz_header, headers.psz_role, strlen( headers.psz_role ) );
844 *pi_size = FISBONE_BASE_SIZE + headers.i_size;
846 free( headers.psz_content_type );
847 free( headers.psz_role );
850 static void OggFillSkeletonFishead( uint8_t *p_buffer, sout_mux_t *p_mux )
852 memcpy( p_buffer, "fishead", 8 );
853 SetWLE( &p_buffer[8], 4 );
854 SetWLE( &p_buffer[10], 0 );
855 SetQWLE( &p_buffer[20], 1000 );
856 SetQWLE( &p_buffer[36], 1000 );
857 SetQWLE( &p_buffer[64], p_mux->p_sys->i_pos - p_mux->p_sys->i_segment_start ); /* segment length */
858 SetQWLE( &p_buffer[72], p_mux->p_sys->i_data_start - p_mux->p_sys->i_segment_start ); /* data start offset */
861 static int32_t OggFillDsHeader( uint8_t *p_buffer, oggds_header_t *p_oggds_header, int i_cat )
864 p_buffer[index] = p_oggds_header->i_packet_type;
866 memcpy( &p_buffer[index], p_oggds_header->stream_type, sizeof(p_oggds_header->stream_type) );
867 index += sizeof(p_oggds_header->stream_type);
868 memcpy(&p_buffer[index], p_oggds_header->sub_type, sizeof(p_oggds_header->sub_type) );
869 index += sizeof(p_oggds_header->sub_type);
871 /* The size is filled at the end */
872 uint8_t *p_isize = &p_buffer[index];
875 SetQWLE( &p_buffer[index], p_oggds_header->i_time_unit );
877 SetQWLE( &p_buffer[index], p_oggds_header->i_samples_per_unit );
879 SetDWLE( &p_buffer[index], p_oggds_header->i_default_len );
881 SetDWLE( &p_buffer[index], p_oggds_header->i_buffer_size );
883 SetWLE( &p_buffer[index], p_oggds_header->i_bits_per_sample );
885 SetWLE( &p_buffer[index], p_oggds_header->i_padding_0 );
891 SetDWLE( &p_buffer[index], p_oggds_header->header.video.i_width );
892 SetDWLE( &p_buffer[index+4], p_oggds_header->header.video.i_height );
895 SetWLE( &p_buffer[index], p_oggds_header->header.audio.i_channels );
896 SetWLE( &p_buffer[index+2], p_oggds_header->header.audio.i_block_align );
897 SetDWLE( &p_buffer[index+4], p_oggds_header->header.audio.i_avgbytespersec );
901 SetDWLE( &p_buffer[index], p_oggds_header->i_padding_1 );
905 if( p_oggds_header->i_size > 0 )
907 memcpy( &p_buffer[index], p_oggds_header + sizeof(*p_oggds_header), p_oggds_header->i_size );
908 index += p_oggds_header->i_size;
911 SetDWLE( p_isize, index-1 );
915 static void OggFillVP8Header( uint8_t *p_buffer, sout_input_t *p_input )
917 memcpy( p_buffer, "OVP80\x01\x01\x00", 8 );
918 SetWBE( &p_buffer[8], p_input->p_fmt->video.i_width );
919 SetDWBE( &p_buffer[14], p_input->p_fmt->video.i_sar_den );/* 24 bits, 15~ */
920 SetDWBE( &p_buffer[11], p_input->p_fmt->video.i_sar_num );/* 24 bits, 12~ */
921 SetWBE( &p_buffer[10], p_input->p_fmt->video.i_height );
922 SetDWBE( &p_buffer[18], p_input->p_fmt->video.i_frame_rate );
923 SetDWBE( &p_buffer[22], p_input->p_fmt->video.i_frame_rate_base );
926 static bool OggCreateHeaders( sout_mux_t *p_mux )
928 block_t *p_hdr = NULL;
929 block_t *p_og = NULL;
931 ogg_stream_t *p_stream;
932 sout_mux_sys_t *p_sys = p_mux->p_sys;
935 if( sout_AccessOutControl( p_mux->p_access,
937 &p_sys->skeleton.b_create ) )
939 p_sys->skeleton.b_create = false;
942 p_sys->skeleton.b_create &= !! p_mux->i_nb_inputs;
944 /* no skeleton for solo vorbis/speex/opus tracks */
945 if ( p_mux->i_nb_inputs == 1 && p_mux->pp_inputs[0]->p_fmt->i_cat == AUDIO_ES )
947 p_sys->skeleton.b_create = false;
951 for ( int i=0; i< p_mux->i_nb_inputs; i++ )
953 p_stream = (ogg_stream_t*) p_mux->pp_inputs[i]->p_sys;
954 if ( p_stream->p_oggds_header )
956 /* We don't want skeleton for OggDS */
957 p_sys->skeleton.b_create = false;
963 /* Skeleton's Fishead must be the first page of the stream */
964 if ( p_sys->skeleton.b_create && !p_sys->skeleton.b_head_done )
966 msg_Dbg( p_mux, "creating header for skeleton" );
967 p_sys->skeleton.i_serial_no = p_sys->i_next_serial_no++;
968 ogg_stream_init( &p_sys->skeleton.os, p_sys->skeleton.i_serial_no );
970 op.packet = calloc( 1, op.bytes );
971 if ( op.packet == NULL ) return false;
976 OggFillSkeletonFishead( op.packet, p_mux );
977 ogg_stream_packetin( &p_sys->skeleton.os, &op );
978 p_og = OggStreamFlush( p_mux, &p_sys->skeleton.os, 0 );
979 block_ChainAppend( &p_hdr, p_og );
980 p_sys->skeleton.b_head_done = true;
981 p_sys->skeleton.i_fishead_offset = p_sys->i_pos;
984 /* Write header for each stream. All b_o_s (beginning of stream) packets
985 * must appear first in the ogg stream so we take care of them first. */
986 for( int pass = 0; pass < 2; pass++ )
988 for( i = 0; i < p_mux->i_nb_inputs; i++ )
990 sout_input_t *p_input = p_mux->pp_inputs[i];
991 p_stream = (ogg_stream_t*)p_input->p_sys;
993 bool video = ( p_stream->i_fourcc == VLC_CODEC_THEORA || p_stream->i_fourcc == VLC_CODEC_DIRAC );
994 if( ( ( pass == 0 && !video ) || ( pass == 1 && video ) ) )
997 msg_Dbg( p_mux, "creating header for %4.4s",
998 (char *)&p_stream->i_fourcc );
1000 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
1001 p_stream->b_new = false;
1002 p_stream->i_packet_no = 0;
1003 p_stream->b_started = true;
1005 if( p_stream->i_fourcc == VLC_CODEC_VORBIS ||
1006 p_stream->i_fourcc == VLC_CODEC_SPEEX ||
1007 p_stream->i_fourcc == VLC_CODEC_OPUS ||
1008 p_stream->i_fourcc == VLC_CODEC_THEORA )
1010 /* First packet in order: vorbis/speex/theora info */
1011 unsigned pi_size[XIPH_MAX_HEADER_COUNT];
1012 void *pp_data[XIPH_MAX_HEADER_COUNT];
1014 if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
1015 p_input->p_fmt->i_extra, p_input->p_fmt->p_extra ) )
1022 op.bytes = pi_size[0];
1023 op.packet = pp_data[0];
1024 if( pi_size[0] <= 0 )
1025 msg_Err( p_mux, "header data corrupted");
1030 op.packetno = p_stream->i_packet_no++;
1031 ogg_stream_packetin( &p_stream->os, &op );
1032 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
1034 /* Get keyframe_granule_shift for theora granulepos calculation */
1035 if( p_stream->i_fourcc == VLC_CODEC_THEORA )
1037 p_stream->i_keyframe_granule_shift =
1038 ( (op.packet[40] & 0x03) << 3 ) | ( (op.packet[41] & 0xe0) >> 5 );
1041 else if( p_stream->i_fourcc == VLC_CODEC_DIRAC )
1043 op.packet = p_input->p_fmt->p_extra;
1044 op.bytes = p_input->p_fmt->i_extra;
1048 op.packetno = p_stream->i_packet_no++;
1049 ogg_stream_packetin( &p_stream->os, &op );
1050 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
1052 else if( p_stream->i_fourcc == VLC_CODEC_FLAC )
1054 /* flac stream marker (yeah, only that in the 1st packet) */
1055 op.packet = (unsigned char *)"fLaC";
1060 op.packetno = p_stream->i_packet_no++;
1061 ogg_stream_packetin( &p_stream->os, &op );
1062 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
1064 else if( p_stream->i_fourcc == VLC_CODEC_VP8 )
1067 op.packet = malloc( 26 );
1071 OggFillVP8Header( op.packet, p_input );
1075 op.packetno = p_stream->i_packet_no++;
1076 ogg_stream_packetin( &p_stream->os, &op );
1077 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
1080 else if( p_stream->p_oggds_header )
1083 op.packet = malloc( sizeof(*p_stream->p_oggds_header) + p_stream->p_oggds_header->i_size );
1086 op.bytes = OggFillDsHeader( op.packet, p_stream->p_oggds_header, p_stream->i_cat );
1090 op.packetno = p_stream->i_packet_no++;
1091 ogg_stream_packetin( &p_stream->os, &op );
1092 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
1096 block_ChainAppend( &p_hdr, p_og );
1100 /* Create fisbones if any */
1101 if ( p_sys->skeleton.b_create )
1103 for( i = 0; i < p_mux->i_nb_inputs; i++ )
1105 sout_input_t *p_input = p_mux->pp_inputs[i];
1106 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
1107 if ( p_stream->skeleton.b_fisbone_done ) continue;
1108 OggGetSkeletonFisbone( &op.packet, &op.bytes, p_input, p_mux );
1109 if ( op.packet == NULL ) return false;
1113 op.packetno = p_sys->skeleton.i_packet_no++;
1114 ogg_stream_packetin( &p_sys->skeleton.os, &op );
1115 p_og = OggStreamFlush( p_mux, &p_sys->skeleton.os, 0 );
1116 block_ChainAppend( &p_hdr, p_og );
1117 p_stream->skeleton.b_fisbone_done = true;
1121 /* Write previous headers */
1122 for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
1124 /* flag headers to be resent for streaming clients */
1125 p_og->i_flags |= BLOCK_FLAG_HEADER;
1127 p_mux->p_sys->i_pos += sout_AccessOutWrite( p_mux->p_access, p_hdr );
1130 /* Create indexes if any */
1131 for( i = 0; i < p_mux->i_nb_inputs; i++ )
1133 sout_input_t *p_input = p_mux->pp_inputs[i];
1134 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
1135 /* flush stream && save offset */
1136 if ( p_sys->skeleton.b_create && !p_stream->skeleton.b_index_done )
1138 if ( !p_stream->skeleton.p_index ) AllocateIndex( p_mux, p_input );
1139 if ( p_stream->skeleton.p_index )
1141 msg_Dbg( p_mux, "Creating index for stream %d", p_stream->i_serial_no );
1142 OggGetSkeletonIndex( &op.packet, &op.bytes, p_stream );
1143 if ( op.packet == NULL ) return false;
1147 op.packetno = p_sys->skeleton.i_packet_no++;
1149 /* backup some values */
1150 p_stream->skeleton.i_index_offset = p_mux->p_sys->i_pos;
1151 p_stream->skeleton.i_index_packetno = p_sys->skeleton.os.packetno;
1152 p_stream->skeleton.i_index_pageno = p_sys->skeleton.os.pageno;
1154 ogg_stream_packetin( &p_sys->skeleton.os, &op );
1155 p_og = OggStreamFlush( p_mux, &p_sys->skeleton.os, 0 );
1156 p_mux->p_sys->i_pos += sout_AccessOutWrite( p_mux->p_access, p_og );
1158 p_stream->skeleton.b_index_done = true;
1162 /* Take care of the non b_o_s headers */
1163 for( i = 0; i < p_mux->i_nb_inputs; i++ )
1165 sout_input_t *p_input = p_mux->pp_inputs[i];
1166 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
1168 if( p_stream->i_fourcc == VLC_CODEC_VORBIS ||
1169 p_stream->i_fourcc == VLC_CODEC_SPEEX ||
1170 p_stream->i_fourcc == VLC_CODEC_OPUS ||
1171 p_stream->i_fourcc == VLC_CODEC_THEORA )
1173 unsigned pi_size[XIPH_MAX_HEADER_COUNT];
1174 void *pp_data[XIPH_MAX_HEADER_COUNT];
1176 if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
1177 p_input->p_fmt->i_extra, p_input->p_fmt->p_extra ) )
1180 /* Special case, headers are already there in the incoming stream.
1181 * We need to gather them an mark them as headers. */
1182 for( unsigned i = 1; i < i_count; i++ )
1184 op.bytes = pi_size[i];
1185 op.packet = pp_data[i];
1186 if( pi_size[i] <= 0 )
1187 msg_Err( p_mux, "header data corrupted");
1192 op.packetno = p_stream->i_packet_no++;
1193 ogg_stream_packetin( &p_stream->os, &op );
1194 msg_Dbg( p_mux, "adding non bos, secondary header" );
1195 if( i == i_count - 1 )
1196 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
1198 p_og = OggStreamPageOut( p_mux, &p_stream->os, 0 );
1200 block_ChainAppend( &p_hdr, p_og );
1203 else if( p_stream->i_fourcc != VLC_CODEC_FLAC &&
1204 p_stream->i_fourcc != VLC_CODEC_DIRAC )
1210 com[0] = PACKET_TYPE_COMMENT;
1211 i_com = snprintf( (char *)(com+1), 127,
1212 PACKAGE_VERSION" stream output" )
1219 op.packetno = p_stream->i_packet_no++;
1220 ogg_stream_packetin( &p_stream->os, &op );
1221 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
1222 block_ChainAppend( &p_hdr, p_og );
1225 /* Special case for mp4v and flac */
1226 if( ( p_stream->i_fourcc == VLC_CODEC_MP4V ||
1227 p_stream->i_fourcc == VLC_CODEC_FLAC ) &&
1228 p_input->p_fmt->i_extra )
1230 /* Send a packet with the VOL data for mp4v
1231 * or STREAMINFO for flac */
1232 msg_Dbg( p_mux, "writing extra data" );
1233 op.bytes = p_input->p_fmt->i_extra;
1234 op.packet = p_input->p_fmt->p_extra;
1235 uint8_t flac_streaminfo[34 + 4];
1236 if( p_stream->i_fourcc == VLC_CODEC_FLAC )
1238 if (op.bytes == 42 && !memcmp(op.packet, "fLaC", 4)) {
1240 memcpy(flac_streaminfo, op.packet + 4, 38);
1241 op.packet = flac_streaminfo;
1242 } else if (op.bytes == 34) {
1244 memcpy(flac_streaminfo + 4, op.packet, 34);
1245 flac_streaminfo[0] = 0x80; /* last block, streaminfo */
1246 flac_streaminfo[1] = 0;
1247 flac_streaminfo[2] = 0;
1248 flac_streaminfo[3] = 34; /* block size */
1249 op.packet = flac_streaminfo;
1251 msg_Err(p_mux, "Invalid FLAC streaminfo (%ld bytes)",
1258 op.packetno = p_stream->i_packet_no++;
1259 ogg_stream_packetin( &p_stream->os, &op );
1260 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
1261 block_ChainAppend( &p_hdr, p_og );
1265 if ( p_sys->skeleton.b_create )
1267 msg_Dbg( p_mux, "ending skeleton" );
1273 op.packetno = p_sys->skeleton.i_packet_no++;
1274 ogg_stream_packetin( &p_sys->skeleton.os, &op );
1275 p_og = OggStreamFlush( p_mux, &p_sys->skeleton.os, 0 );
1276 block_ChainAppend( &p_hdr, p_og );
1279 /* set HEADER flag */
1280 /* flag headers to be resent for streaming clients */
1281 for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
1283 p_og->i_flags |= BLOCK_FLAG_HEADER;
1286 /* Write previous headers */
1287 p_mux->p_sys->i_pos += sout_AccessOutWrite( p_mux->p_access, p_hdr );
1292 static void OggCreateStreamFooter( sout_mux_t *p_mux, ogg_stream_t *p_stream )
1296 sout_mux_sys_t *p_sys = p_mux->p_sys;
1298 /* as stream is finished, overwrite the index, if there was any */
1299 if ( p_sys->skeleton.b_create && p_stream->skeleton.p_index
1300 && p_stream->skeleton.i_index_payload )
1302 sout_AccessOutSeek( p_mux->p_access, p_stream->skeleton.i_index_offset );
1303 OggGetSkeletonIndex( &op.packet, &op.bytes, p_stream );
1304 if ( op.packet != NULL )
1306 msg_Dbg(p_mux, "Rewriting index at %"PRId64, p_stream->skeleton.i_index_offset );
1307 ogg_stream_reset_serialno( &p_sys->skeleton.os, p_sys->skeleton.i_serial_no );
1311 op.packetno = p_stream->skeleton.i_index_packetno + 1;
1312 /* fake our stream state */
1313 p_sys->skeleton.os.pageno = p_stream->skeleton.i_index_pageno;
1314 p_sys->skeleton.os.packetno = p_stream->skeleton.i_index_packetno;
1315 p_sys->skeleton.os.granulepos = 0;
1316 p_sys->skeleton.os.b_o_s = 1;
1317 p_sys->skeleton.os.e_o_s = 0;
1318 ogg_stream_packetin( &p_sys->skeleton.os, &op );
1319 p_og = OggStreamFlush( p_mux, &p_sys->skeleton.os, 0 );
1320 sout_AccessOutWrite( p_mux->p_access, p_og );
1322 sout_AccessOutSeek( p_mux->p_access, p_sys->i_pos );
1325 /* clear skeleton */
1326 p_stream->skeleton.b_fisbone_done = false;
1327 p_stream->skeleton.b_index_done = false;
1328 p_stream->skeleton.i_index_offset = 0;
1329 p_stream->skeleton.i_index_payload = 0;
1330 p_stream->skeleton.i_last_keyframe_pos = 0;
1331 p_stream->skeleton.i_last_keyframe_time = 0;
1332 /* clear accounting */
1333 p_stream->i_num_frames = 0;
1334 p_stream->i_num_keyframes = 0;
1336 /* Write eos packet for stream. */
1341 op.granulepos = p_stream->u_last_granulepos;
1342 op.packetno = p_stream->i_packet_no++;
1343 ogg_stream_packetin( &p_stream->os, &op );
1345 /* flush it with all remaining data */
1346 if( ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
1349 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
1350 p_mux->p_sys->i_pos += sout_AccessOutWrite( p_mux->p_access, p_og );
1353 ogg_stream_clear( &p_stream->os );
1356 static void OggSetDate( block_t *p_og, mtime_t i_dts, mtime_t i_length )
1362 for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
1367 if( i_count == 0 ) return; /* ignore. */
1369 i_delta = i_length / i_count;
1371 for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
1373 p_tmp->i_dts = i_dts;
1374 p_tmp->i_length = i_delta;
1380 static void OggRewriteFisheadPage( sout_mux_t *p_mux )
1382 sout_mux_sys_t *p_sys = p_mux->p_sys;
1385 op.packet = calloc( 1, op.bytes );
1386 if ( op.packet != NULL )
1392 ogg_stream_reset_serialno( &p_sys->skeleton.os, p_sys->skeleton.i_serial_no );
1393 OggFillSkeletonFishead( op.packet, p_mux );
1394 ogg_stream_packetin( &p_sys->skeleton.os, &op );
1395 msg_Dbg( p_mux, "rewriting fishead at %"PRId64, p_mux->p_sys->skeleton.i_fishead_offset );
1396 sout_AccessOutSeek( p_mux->p_access, p_mux->p_sys->skeleton.i_fishead_offset );
1397 sout_AccessOutWrite( p_mux->p_access,
1398 OggStreamFlush( p_mux, &p_sys->skeleton.os, 0 ) );
1399 sout_AccessOutSeek( p_mux->p_access, p_mux->p_sys->i_pos );
1403 static bool AllocateIndex( sout_mux_t *p_mux, sout_input_t *p_input )
1405 ogg_stream_t *p_stream = (ogg_stream_t *) p_input->p_sys;
1408 if ( p_stream->i_length )
1410 uint64_t i_interval = (uint64_t)p_mux->p_sys->skeleton.i_index_intvl * 1000;
1413 if( p_input->p_fmt->i_cat == VIDEO_ES &&
1414 p_input->p_fmt->video.i_frame_rate )
1416 /* optimize for fps < 1 */
1417 i_interval= __MAX( p_mux->p_sys->skeleton.i_index_intvl * 1000,
1419 p_input->p_fmt->video.i_frame_rate_base /
1420 p_input->p_fmt->video.i_frame_rate );
1423 size_t i_tuple_size = 0;
1424 /* estimate length of pos value */
1425 if ( p_input->p_fmt->i_bitrate )
1427 i = i_interval * p_input->p_fmt->i_bitrate / 1000000;
1428 while ( i <<= 1 ) i_tuple_size++;
1432 /* Likely 64KB<<keyframe interval<<16MB */
1433 /* We can't really guess due to muxing */
1434 i_tuple_size = 24 / 8;
1437 /* add length of interval value */
1439 while ( i <<= 1 ) i_tuple_size++;
1441 i_size = i_tuple_size * ( p_stream->i_length / i_interval + 2 );
1445 i_size = ( INT64_C(3600) * 11.2 * 1000 / p_mux->p_sys->skeleton.i_index_intvl )
1446 * p_mux->p_sys->skeleton.i_index_ratio;
1447 msg_Dbg( p_mux, "No stream length, using default allocation for index" );
1449 i_size *= ( 8.0 / 7 ); /* 7bits encoding overhead */
1450 msg_Dbg( p_mux, "allocating %zu bytes for index", i_size );
1451 p_stream->skeleton.p_index = calloc( i_size, sizeof(uint8_t) );
1452 if ( !p_stream->skeleton.p_index ) return false;
1453 p_stream->skeleton.i_index_size = i_size;
1454 p_stream->skeleton.i_index_payload = 0;
1458 /*****************************************************************************
1459 * Mux: multiplex available data in input fifos into the Ogg bitstream
1460 *****************************************************************************/
1461 static int Mux( sout_mux_t *p_mux )
1463 sout_mux_sys_t *p_sys = p_mux->p_sys;
1466 /* End any stream that ends in that group */
1467 if ( p_sys->i_del_streams )
1469 /* Remove deleted logical streams */
1470 for( int i = 0; i < p_sys->i_del_streams; i++ )
1472 OggCreateStreamFooter( p_mux, p_sys->pp_del_streams[i] );
1473 FREENULL( p_sys->pp_del_streams[i]->p_oggds_header );
1474 FREENULL( p_sys->pp_del_streams[i] );
1476 FREENULL( p_sys->pp_del_streams );
1477 p_sys->i_del_streams = 0;
1480 if ( p_sys->i_streams == 0 )
1482 /* All streams have been deleted, or none have ever been created
1483 From this point, we are allowed to start a new group of logical streams */
1484 p_sys->skeleton.b_head_done = false;
1485 p_sys->b_can_add_streams = true;
1486 p_sys->i_segment_start = p_sys->i_pos;
1489 if ( p_sys->i_add_streams )
1491 if ( !p_sys->b_can_add_streams )
1493 msg_Warn( p_mux, "Can't add new stream %d/%d: Considerer increasing sout-mux-caching variable", p_sys->i_del_streams, p_mux->p_sys->i_streams);
1494 msg_Warn( p_mux, "Resetting and setting new identity to current streams");
1496 /* resetting all active streams */
1497 for ( int i=0; i < p_mux->p_sys->i_streams; i++ )
1499 ogg_stream_t * p_stream = (ogg_stream_t *) p_mux->pp_inputs[i]->p_sys;
1500 if ( p_stream->b_finished || !p_stream->b_started ) continue;
1501 OggCreateStreamFooter( p_mux, p_stream );
1502 p_stream->i_serial_no = p_sys->i_next_serial_no++;
1503 p_stream->i_packet_no = 0;
1504 p_stream->b_finished = true;
1507 /* rewrite fishead with final values */
1508 if ( p_sys->skeleton.b_head_done )
1510 OggRewriteFisheadPage( p_mux );
1513 p_sys->b_can_add_streams = true;
1514 p_sys->skeleton.b_head_done = false;
1515 p_sys->i_segment_start = p_sys->i_pos;
1518 /* Open new ogg stream */
1519 if( sout_MuxGetStream( p_mux, 1, &i_dts) < 0 )
1521 msg_Dbg( p_mux, "waiting for data..." );
1524 msg_Dbg( p_mux, "writing streams headers" );
1525 p_sys->i_start_dts = i_dts;
1526 p_sys->i_streams = p_mux->i_nb_inputs;
1527 p_sys->i_del_streams = 0;
1528 p_sys->i_add_streams = 0;
1529 p_sys->skeleton.b_create = true;
1531 if ( ! OggCreateHeaders( p_mux ) )
1534 /* If we're switching to end of headers, then that's data start */
1535 if ( p_sys->b_can_add_streams )
1537 msg_Dbg( p_mux, "data starts from %zu", p_sys->i_pos );
1538 p_sys->i_data_start = p_sys->i_pos;
1541 /* Since we started sending secondaryheader or data pages,
1542 * we're no longer allowed to create new streams, until all streams end */
1543 p_sys->b_can_add_streams = false;
1546 /* Do the regular data mux thing */
1549 int i_stream = sout_MuxGetStream( p_mux, 1, NULL );
1552 MuxBlock( p_mux, p_mux->pp_inputs[i_stream] );
1558 static int MuxBlock( sout_mux_t *p_mux, sout_input_t *p_input )
1560 sout_mux_sys_t *p_sys = p_mux->p_sys;
1561 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
1562 block_t *p_data = block_FifoGet( p_input->p_fifo );
1563 block_t *p_og = NULL;
1567 if( p_stream->i_fourcc != VLC_CODEC_VORBIS &&
1568 p_stream->i_fourcc != VLC_CODEC_FLAC &&
1569 p_stream->i_fourcc != VLC_CODEC_SPEEX &&
1570 p_stream->i_fourcc != VLC_CODEC_OPUS &&
1571 p_stream->i_fourcc != VLC_CODEC_THEORA &&
1572 p_stream->i_fourcc != VLC_CODEC_VP8 &&
1573 p_stream->i_fourcc != VLC_CODEC_DIRAC )
1575 p_data = block_Realloc( p_data, 1, p_data->i_buffer );
1576 p_data->p_buffer[0] = PACKET_IS_SYNCPOINT; // FIXME
1579 if ( p_stream->i_fourcc == VLC_CODEC_DIRAC && p_stream->i_baseptsdelay < 0 )
1580 p_stream->i_baseptsdelay = p_data->i_pts - p_data->i_dts;
1582 op.packet = p_data->p_buffer;
1583 op.bytes = p_data->i_buffer;
1586 op.packetno = p_stream->i_packet_no++;
1589 if( p_stream->i_cat == AUDIO_ES )
1591 if( p_stream->i_fourcc == VLC_CODEC_VORBIS ||
1592 p_stream->i_fourcc == VLC_CODEC_FLAC ||
1593 p_stream->i_fourcc == VLC_CODEC_OPUS ||
1594 p_stream->i_fourcc == VLC_CODEC_SPEEX )
1596 /* number of sample from begining + current packet */
1598 ( p_data->i_dts - p_sys->i_start_dts + p_data->i_length ) *
1599 (mtime_t)p_input->p_fmt->audio.i_rate / CLOCK_FREQ;
1601 i_time = p_data->i_dts - p_sys->i_start_dts;
1602 AddIndexEntry( p_mux, i_time, p_input );
1604 else if( p_stream->p_oggds_header )
1606 /* number of sample from begining */
1607 op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) *
1608 p_stream->p_oggds_header->i_samples_per_unit / CLOCK_FREQ;
1611 else if( p_stream->i_cat == VIDEO_ES )
1613 if( p_stream->i_fourcc == VLC_CODEC_THEORA )
1615 p_stream->i_num_frames++;
1616 if( p_data->i_flags & BLOCK_FLAG_TYPE_I )
1618 p_stream->i_num_keyframes++;
1619 p_stream->i_last_keyframe = p_stream->i_num_frames;
1621 /* presentation time */
1622 i_time = CLOCK_FREQ * ( p_stream->i_num_frames - 1 ) *
1623 p_input->p_fmt->video.i_frame_rate_base / p_input->p_fmt->video.i_frame_rate;
1624 AddIndexEntry( p_mux, i_time, p_input );
1627 op.granulepos = (p_stream->i_last_keyframe << p_stream->i_keyframe_granule_shift )
1628 | (p_stream->i_num_frames-p_stream->i_last_keyframe);
1630 else if( p_stream->i_fourcc == VLC_CODEC_DIRAC )
1633 #define FRAME_ROUND(a) \
1634 if ( ( a + 5000 / CLOCK_FREQ ) > ( a / CLOCK_FREQ ) )\
1638 mtime_t dt = (p_data->i_dts - p_sys->i_start_dts) * p_input->p_fmt->video.i_frame_rate / p_input->p_fmt->video.i_frame_rate_base;
1641 mtime_t pt = (p_data->i_pts - p_sys->i_start_dts - p_stream->i_baseptsdelay ) * p_input->p_fmt->video.i_frame_rate / p_input->p_fmt->video.i_frame_rate_base;
1644 /* (shro) some PTS could be repeated within 1st frames */
1645 if ( pt == p_stream->i_dirac_last_pt )
1648 p_stream->i_dirac_last_pt = pt;
1650 /* (shro) some DTS could be repeated within 1st frames */
1651 if ( dt == p_stream->i_dirac_last_dt )
1654 p_stream->i_dirac_last_dt = dt;
1656 if( p_data->i_flags & BLOCK_FLAG_TYPE_I )
1657 p_stream->i_last_keyframe = dt;
1658 mtime_t dist = dt - p_stream->i_last_keyframe;
1660 /* Everything increments by two for progressive */
1667 mtime_t delay = pt - dt;
1668 if ( delay < 0 ) delay *= -1;
1670 op.granulepos = (pt - delay) << 31 | (dist&0xff00) << 14
1671 | (delay&0x1fff) << 9 | (dist&0xff);
1673 msg_Dbg( p_mux, "dts %"PRId64" pts %"PRId64" dt %"PRId64" pt %"PRId64" delay %"PRId64" granule %"PRId64,
1674 (p_data->i_dts - p_sys->i_start_dts),
1675 (p_data->i_pts - p_sys->i_start_dts ),
1676 dt, pt, delay, op.granulepos );
1679 AddIndexEntry( p_mux, dt, p_input );
1681 else if( p_stream->i_fourcc == VLC_CODEC_VP8 )
1683 p_stream->i_num_frames++;
1684 if( p_data->i_flags & BLOCK_FLAG_TYPE_I )
1686 p_stream->i_num_keyframes++;
1687 p_stream->i_last_keyframe = p_stream->i_num_frames;
1689 /* presentation time */
1690 i_time = CLOCK_FREQ * ( p_stream->i_num_frames - 1 ) *
1691 p_input->p_fmt->video.i_frame_rate_base / p_input->p_fmt->video.i_frame_rate;
1692 AddIndexEntry( p_mux, i_time, p_input );
1694 op.granulepos = ( ((int64_t)p_stream->i_num_frames) << 32 ) |
1695 ( ( ( p_stream->i_num_frames - p_stream->i_last_keyframe ) & 0x07FFFFFF ) << 3 );
1697 else if( p_stream->p_oggds_header )
1698 op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) * INT64_C(10) /
1699 p_stream->p_oggds_header->i_time_unit;
1701 else if( p_stream->i_cat == SPU_ES )
1703 /* granulepos is in millisec */
1704 op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) / 1000;
1707 return VLC_EGENERIC;
1709 p_stream->u_last_granulepos = op.granulepos;
1710 ogg_stream_packetin( &p_stream->os, &op );
1712 if( p_stream->i_cat == SPU_ES ||
1713 p_stream->i_fourcc == VLC_CODEC_SPEEX ||
1714 p_stream->i_fourcc == VLC_CODEC_DIRAC )
1716 /* Subtitles or Speex packets are quite small so they
1717 * need to be flushed to be sent on time */
1718 /* The OggDirac mapping suggests ever so strongly that a
1719 * page flush occurs after each OggDirac packet, so to make
1720 * the timestamps unambiguous */
1721 p_og = OggStreamFlush( p_mux, &p_stream->os, p_data->i_dts );
1725 p_og = OggStreamPageOut( p_mux, &p_stream->os, p_data->i_dts );
1730 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
1731 p_stream->i_dts = -1;
1732 p_stream->i_length = 0;
1733 p_mux->p_sys->i_pos += sout_AccessOutWrite( p_mux->p_access, p_og );
1737 if( p_stream->i_dts < 0 )
1739 p_stream->i_dts = p_data->i_dts;
1741 p_stream->i_length += p_data->i_length;
1744 block_Release( p_data );