Qt/EPG: Remove the update button. Improve the window layout.
[vlc/gmpfix.git] / modules / mux / ogg.c
bloba2de8607e32aba42a985b140998c510cd0833f76
1 /*****************************************************************************
2 * ogg.c: ogg muxer module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001, 2002, 2006 the VideoLAN team
5 * $Id$
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 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_sout.h>
36 #include <vlc_block.h>
37 #include <vlc_codecs.h>
38 #include <limits.h>
39 #include <vlc_rand.h>
40 #include "../demux/xiph.h"
42 #include <ogg/ogg.h>
44 /*****************************************************************************
45 * Module descriptor
46 *****************************************************************************/
47 static int Open ( vlc_object_t * );
48 static void Close ( vlc_object_t * );
50 vlc_module_begin ()
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 )
57 vlc_module_end ()
60 /*****************************************************************************
61 * Exported prototypes
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 /*****************************************************************************
73 * Misc declarations
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
82 typedef struct
83 #ifdef HAVE_ATTRIBUTE_PACKED
84 __attribute__((__packed__))
85 #endif
87 int32_t i_width;
88 int32_t i_height;
89 } oggds_header_video_t;
91 typedef struct
92 #ifdef HAVE_ATTRIBUTE_PACKED
93 __attribute__((__packed__))
94 #endif
96 int16_t i_channels;
97 int16_t i_block_align;
98 int32_t i_avgbytespersec;
99 } oggds_header_audio_t;
101 typedef struct
102 #ifdef HAVE_ATTRIBUTE_PACKED
103 __attribute__((__packed__))
104 #endif
106 uint8_t i_packet_type;
108 char stream_type[8];
109 char sub_type[4];
111 int32_t i_size;
113 int64_t i_time_unit;
114 int64_t i_samples_per_unit;
115 int32_t i_default_len;
117 int32_t i_buffer_size;
118 int16_t i_bits_per_sample;
120 int16_t i_padding_0; /* Because the original is using MSVC packing style */
122 union
124 oggds_header_video_t video;
125 oggds_header_audio_t audio;
126 } header;
128 int32_t i_padding_1; /* Because the original is using MSVC packing style */
130 } oggds_header_t;
132 /*****************************************************************************
133 * Definitions of structures and functions used by this plugins
134 *****************************************************************************/
135 typedef struct
137 int i_cat;
138 int i_fourcc;
140 int b_new;
142 mtime_t i_dts;
143 mtime_t i_length;
144 int i_packet_no;
145 int i_serial_no;
146 int i_keyframe_granule_shift; /* Theora only */
147 int i_last_keyframe; /* dirac and theora */
148 int i_num_frames; /* Theora only */
149 uint64_t u_last_granulepos; /* Used for correct EOS page */
150 int64_t i_num_keyframes;
151 ogg_stream_state os;
153 oggds_header_t *p_oggds_header;
155 } ogg_stream_t;
157 struct sout_mux_sys_t
159 int i_streams;
161 mtime_t i_start_dts;
162 int i_next_serial_no;
164 /* number of logical streams pending to be added */
165 int i_add_streams;
167 /* logical streams pending to be deleted */
168 int i_del_streams;
169 ogg_stream_t **pp_del_streams;
172 static void OggSetDate( block_t *, mtime_t , mtime_t );
173 static block_t *OggStreamFlush( sout_mux_t *, ogg_stream_state *, mtime_t );
175 /*****************************************************************************
176 * Open: Open muxer
177 *****************************************************************************/
178 static int Open( vlc_object_t *p_this )
180 sout_mux_t *p_mux = (sout_mux_t*)p_this;
181 sout_mux_sys_t *p_sys;
183 msg_Info( p_mux, "Open" );
185 p_sys = malloc( sizeof( sout_mux_sys_t ) );
186 if( !p_sys )
187 return VLC_ENOMEM;
188 p_sys->i_streams = 0;
189 p_sys->i_add_streams = 0;
190 p_sys->i_del_streams = 0;
191 p_sys->pp_del_streams = 0;
193 p_mux->p_sys = p_sys;
194 p_mux->pf_control = Control;
195 p_mux->pf_addstream = AddStream;
196 p_mux->pf_delstream = DelStream;
197 p_mux->pf_mux = Mux;
199 /* First serial number is random.
200 * (Done like this because on win32 you need to seed the random number
201 * generator once per thread). */
202 uint32_t r;
203 vlc_rand_bytes(&r, sizeof(r));
204 p_sys->i_next_serial_no = r & INT_MAX;
206 return VLC_SUCCESS;
209 /*****************************************************************************
210 * Close: Finalize ogg bitstream and close muxer
211 *****************************************************************************/
212 static void Close( vlc_object_t * p_this )
214 sout_mux_t *p_mux = (sout_mux_t*)p_this;
215 sout_mux_sys_t *p_sys = p_mux->p_sys;
217 msg_Info( p_mux, "Close" );
219 if( p_sys->i_del_streams )
221 block_t *p_og = NULL;
222 mtime_t i_dts = p_sys->pp_del_streams[p_sys->i_del_streams - 1]->i_dts;
224 /* Close the current ogg stream */
225 msg_Dbg( p_mux, "writing footer" );
226 block_ChainAppend( &p_og, OggCreateFooter( p_mux ) );
228 /* Remove deleted logical streams */
229 for(int i = 0; i < p_sys->i_del_streams; i++ )
231 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
232 FREENULL( p_sys->pp_del_streams[i]->p_oggds_header );
233 FREENULL( p_sys->pp_del_streams[i] );
235 FREENULL( p_sys->pp_del_streams );
236 p_sys->i_streams -= p_sys->i_del_streams;
238 /* Write footer */
239 OggSetDate( p_og, i_dts, 0 );
240 sout_AccessOutWrite( p_mux->p_access, p_og );
243 free( p_sys );
246 /*****************************************************************************
247 * Control:
248 *****************************************************************************/
249 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
251 VLC_UNUSED(p_mux);
252 bool *pb_bool;
253 char **ppsz;
255 switch( i_query )
257 case MUX_CAN_ADD_STREAM_WHILE_MUXING:
258 pb_bool = (bool*)va_arg( args, bool * );
259 *pb_bool = true;
260 return VLC_SUCCESS;
262 case MUX_GET_ADD_STREAM_WAIT:
263 pb_bool = (bool*)va_arg( args, bool * );
264 *pb_bool = true;
265 return VLC_SUCCESS;
267 case MUX_GET_MIME:
268 ppsz = (char**)va_arg( args, char ** );
269 *ppsz = strdup( "application/ogg" );
270 return VLC_SUCCESS;
272 default:
273 return VLC_EGENERIC;
276 /*****************************************************************************
277 * AddStream: Add an elementary stream to the muxed stream
278 *****************************************************************************/
279 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
281 sout_mux_sys_t *p_sys = p_mux->p_sys;
282 ogg_stream_t *p_stream;
283 uint16_t i_tag;
285 msg_Dbg( p_mux, "adding input" );
287 p_input->p_sys = p_stream = calloc( 1, sizeof( ogg_stream_t ) );
288 if( !p_stream )
289 return VLC_ENOMEM;
291 p_stream->i_cat = p_input->p_fmt->i_cat;
292 p_stream->i_fourcc = p_input->p_fmt->i_codec;
293 p_stream->i_serial_no = p_sys->i_next_serial_no++;
294 p_stream->i_packet_no = 0;
295 p_stream->i_last_keyframe = 0;
296 p_stream->i_num_keyframes = 0;
297 p_stream->i_num_frames = 0;
299 p_stream->p_oggds_header = 0;
301 switch( p_input->p_fmt->i_cat )
303 case VIDEO_ES:
304 if( !p_input->p_fmt->video.i_frame_rate ||
305 !p_input->p_fmt->video.i_frame_rate_base )
307 msg_Warn( p_mux, "Missing frame rate, assuming 25fps" );
308 p_input->p_fmt->video.i_frame_rate = 25;
309 p_input->p_fmt->video.i_frame_rate_base = 1;
312 switch( p_stream->i_fourcc )
314 case VLC_CODEC_MP4V:
315 case VLC_CODEC_MPGV:
316 case VLC_CODEC_DIV3:
317 case VLC_CODEC_MJPG:
318 case VLC_CODEC_WMV1:
319 case VLC_CODEC_WMV2:
320 case VLC_CODEC_WMV3:
321 case VLC_CODEC_SNOW:
322 p_stream->p_oggds_header = calloc( 1, sizeof(oggds_header_t) );
323 if( !p_stream->p_oggds_header )
325 free( p_stream );
326 return VLC_ENOMEM;
328 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
330 memcpy( p_stream->p_oggds_header->stream_type, "video", 5 );
331 if( p_stream->i_fourcc == VLC_CODEC_MP4V )
333 memcpy( p_stream->p_oggds_header->sub_type, "XVID", 4 );
335 else if( p_stream->i_fourcc == VLC_CODEC_DIV3 )
337 memcpy( p_stream->p_oggds_header->sub_type, "DIV3", 4 );
339 else
341 memcpy( p_stream->p_oggds_header->sub_type,
342 &p_stream->i_fourcc, 4 );
344 SetDWLE( &p_stream->p_oggds_header->i_size,
345 sizeof( oggds_header_t ) - 1 );
346 SetQWLE( &p_stream->p_oggds_header->i_time_unit,
347 INT64_C(10000000) * p_input->p_fmt->video.i_frame_rate_base /
348 (int64_t)p_input->p_fmt->video.i_frame_rate );
349 SetQWLE( &p_stream->p_oggds_header->i_samples_per_unit, 1 );
350 SetDWLE( &p_stream->p_oggds_header->i_default_len, 1 ); /* ??? */
351 SetDWLE( &p_stream->p_oggds_header->i_buffer_size, 1024*1024 );
352 SetWLE( &p_stream->p_oggds_header->i_bits_per_sample, 0 );
353 SetDWLE( &p_stream->p_oggds_header->header.video.i_width,
354 p_input->p_fmt->video.i_width );
355 SetDWLE( &p_stream->p_oggds_header->header.video.i_height,
356 p_input->p_fmt->video.i_height );
357 msg_Dbg( p_mux, "%4.4s stream", (char *)&p_stream->i_fourcc );
358 break;
360 case VLC_CODEC_DIRAC:
361 msg_Dbg( p_mux, "dirac stream" );
362 break;
364 case VLC_CODEC_THEORA:
365 msg_Dbg( p_mux, "theora stream" );
366 break;
368 default:
369 FREENULL( p_input->p_sys );
370 return VLC_EGENERIC;
372 break;
374 case AUDIO_ES:
375 switch( p_stream->i_fourcc )
377 case VLC_CODEC_VORBIS:
378 msg_Dbg( p_mux, "vorbis stream" );
379 break;
381 case VLC_CODEC_SPEEX:
382 msg_Dbg( p_mux, "speex stream" );
383 break;
385 case VLC_CODEC_FLAC:
386 msg_Dbg( p_mux, "flac stream" );
387 break;
389 default:
390 fourcc_to_wf_tag( p_stream->i_fourcc, &i_tag );
391 if( i_tag == WAVE_FORMAT_UNKNOWN )
393 FREENULL( p_input->p_sys );
394 return VLC_EGENERIC;
397 p_stream->p_oggds_header =
398 malloc( sizeof(oggds_header_t) + p_input->p_fmt->i_extra );
399 if( !p_stream->p_oggds_header )
401 free( p_stream );
402 return VLC_ENOMEM;
404 memset( p_stream->p_oggds_header, 0, sizeof(oggds_header_t) );
405 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
407 SetDWLE( &p_stream->p_oggds_header->i_size,
408 sizeof( oggds_header_t ) - 1 + p_input->p_fmt->i_extra );
410 if( p_input->p_fmt->i_extra )
412 memcpy( &p_stream->p_oggds_header[1],
413 p_input->p_fmt->p_extra, p_input->p_fmt->i_extra );
416 memcpy( p_stream->p_oggds_header->stream_type, "audio", 5 );
418 memset( p_stream->p_oggds_header->sub_type, 0, 4 );
419 sprintf( p_stream->p_oggds_header->sub_type, "%-x", i_tag );
421 SetQWLE( &p_stream->p_oggds_header->i_time_unit, INT64_C(10000000) );
422 SetDWLE( &p_stream->p_oggds_header->i_default_len, 1 );
423 SetDWLE( &p_stream->p_oggds_header->i_buffer_size, 30*1024 );
424 SetQWLE( &p_stream->p_oggds_header->i_samples_per_unit,
425 p_input->p_fmt->audio.i_rate );
426 SetWLE( &p_stream->p_oggds_header->i_bits_per_sample,
427 p_input->p_fmt->audio.i_bitspersample );
428 SetDWLE( &p_stream->p_oggds_header->header.audio.i_channels,
429 p_input->p_fmt->audio.i_channels );
430 SetDWLE( &p_stream->p_oggds_header->header.audio.i_block_align,
431 p_input->p_fmt->audio.i_blockalign );
432 SetDWLE( &p_stream->p_oggds_header->header.audio.i_avgbytespersec,
433 p_input->p_fmt->i_bitrate / 8);
434 msg_Dbg( p_mux, "%4.4s stream", (char *)&p_stream->i_fourcc );
435 break;
437 break;
439 case SPU_ES:
440 switch( p_stream->i_fourcc )
442 case VLC_CODEC_SUBT:
443 p_stream->p_oggds_header = calloc( 1, sizeof(oggds_header_t) );
444 if( !p_stream->p_oggds_header )
446 free( p_stream );
447 return VLC_ENOMEM;
449 p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
451 memcpy( p_stream->p_oggds_header->stream_type, "text", 4 );
452 msg_Dbg( p_mux, "subtitles stream" );
453 break;
455 default:
456 FREENULL( p_input->p_sys );
457 return VLC_EGENERIC;
459 break;
460 default:
461 FREENULL( p_input->p_sys );
462 return VLC_EGENERIC;
465 p_stream->b_new = true;
467 p_sys->i_add_streams++;
469 return VLC_SUCCESS;
472 /*****************************************************************************
473 * DelStream: Delete an elementary stream from the muxed stream
474 *****************************************************************************/
475 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
477 sout_mux_sys_t *p_sys = p_mux->p_sys;
478 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
479 block_t *p_og;
481 msg_Dbg( p_mux, "removing input" );
483 /* flush all remaining data */
484 if( p_input->p_sys )
486 if( !p_stream->b_new )
488 while( block_FifoCount( p_input->p_fifo ) )
489 MuxBlock( p_mux, p_input );
492 if( !p_stream->b_new &&
493 ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
495 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
496 sout_AccessOutWrite( p_mux->p_access, p_og );
499 /* move input in delete queue */
500 if( !p_stream->b_new )
502 p_sys->pp_del_streams = xrealloc( p_sys->pp_del_streams,
503 (p_sys->i_del_streams + 1) * sizeof(ogg_stream_t *) );
504 p_sys->pp_del_streams[p_sys->i_del_streams++] = p_stream;
506 else
508 /* wasn't already added so get rid of it */
509 FREENULL( p_stream->p_oggds_header );
510 FREENULL( p_stream );
511 p_sys->i_add_streams--;
515 p_input->p_sys = NULL;
517 return 0;
520 /*****************************************************************************
521 * Ogg bitstream manipulation routines
522 *****************************************************************************/
523 static block_t *OggStreamGetPage( sout_mux_t *p_mux,
524 ogg_stream_state *p_os, mtime_t i_pts,
525 bool flush )
527 (void)p_mux;
528 block_t *p_og, *p_og_first = NULL;
529 ogg_page og;
530 int (*pager)( ogg_stream_state*, ogg_page* ) = flush ? ogg_stream_flush : ogg_stream_pageout;
532 while( pager( p_os, &og ) )
534 /* Flush all data */
535 p_og = block_New( p_mux, og.header_len + og.body_len );
537 memcpy( p_og->p_buffer, og.header, og.header_len );
538 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
539 p_og->i_dts = 0;
540 p_og->i_pts = i_pts;
541 p_og->i_length = 0;
543 i_pts = 0; // write it only once
545 block_ChainAppend( &p_og_first, p_og );
548 return p_og_first;
551 static block_t *OggStreamFlush( sout_mux_t *p_mux,
552 ogg_stream_state *p_os, mtime_t i_pts )
554 return OggStreamGetPage( p_mux, p_os, i_pts, true );
557 static block_t *OggStreamPageOut( sout_mux_t *p_mux,
558 ogg_stream_state *p_os, mtime_t i_pts )
560 return OggStreamGetPage( p_mux, p_os, i_pts, false );
563 static block_t *OggCreateHeader( sout_mux_t *p_mux )
565 block_t *p_hdr = NULL;
566 block_t *p_og = NULL;
567 ogg_packet op;
568 int i;
570 /* Write header for each stream. All b_o_s (beginning of stream) packets
571 * must appear first in the ogg stream so we take care of them first. */
572 for( int pass = 0; pass < 2; pass++ )
574 for( i = 0; i < p_mux->i_nb_inputs; i++ )
576 sout_input_t *p_input = p_mux->pp_inputs[i];
577 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
579 bool video = ( p_stream->i_fourcc == VLC_CODEC_THEORA || p_stream->i_fourcc == VLC_CODEC_DIRAC );
580 if( ( ( pass == 0 && !video ) || ( pass == 1 && video ) ) )
581 continue;
583 msg_Dbg( p_mux, "creating header for %4.4s",
584 (char *)&p_stream->i_fourcc );
586 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
587 p_stream->b_new = false;
588 p_stream->i_packet_no = 0;
590 if( p_stream->i_fourcc == VLC_CODEC_VORBIS ||
591 p_stream->i_fourcc == VLC_CODEC_SPEEX ||
592 p_stream->i_fourcc == VLC_CODEC_THEORA )
594 /* First packet in order: vorbis/speex/theora info */
595 unsigned pi_size[XIPH_MAX_HEADER_COUNT];
596 void *pp_data[XIPH_MAX_HEADER_COUNT];
597 unsigned i_count;
598 if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
599 p_input->p_fmt->i_extra, p_input->p_fmt->p_extra ) )
601 i_count = 0;
602 pi_size[0] = 0;
603 pp_data[0] = NULL;
606 op.bytes = pi_size[0];
607 op.packet = pp_data[0];
608 if( pi_size[0] <= 0 )
609 msg_Err( p_mux, "header data corrupted");
611 op.b_o_s = 1;
612 op.e_o_s = 0;
613 op.granulepos = 0;
614 op.packetno = p_stream->i_packet_no++;
615 ogg_stream_packetin( &p_stream->os, &op );
616 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
618 /* Get keyframe_granule_shift for theora granulepos calculation */
619 if( p_stream->i_fourcc == VLC_CODEC_THEORA )
621 p_stream->i_keyframe_granule_shift =
622 ( (op.packet[40] & 0x03) << 3 ) | ( (op.packet[41] & 0xe0) >> 5 );
625 for( unsigned i = 0; i < i_count; i++ )
626 free( pp_data[i] );
628 else if( p_stream->i_fourcc == VLC_CODEC_DIRAC )
630 op.packet = p_input->p_fmt->p_extra;
631 op.bytes = p_input->p_fmt->i_extra;
632 op.b_o_s = 1;
633 op.e_o_s = 0;
634 op.granulepos = ~0;
635 op.packetno = p_stream->i_packet_no++;
636 ogg_stream_packetin( &p_stream->os, &op );
637 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
639 else if( p_stream->i_fourcc == VLC_CODEC_FLAC )
641 /* flac stream marker (yeah, only that in the 1st packet) */
642 op.packet = (unsigned char *)"fLaC";
643 op.bytes = 4;
644 op.b_o_s = 1;
645 op.e_o_s = 0;
646 op.granulepos = 0;
647 op.packetno = p_stream->i_packet_no++;
648 ogg_stream_packetin( &p_stream->os, &op );
649 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
651 else if( p_stream->p_oggds_header )
653 /* ds header */
654 op.packet = (uint8_t*)p_stream->p_oggds_header;
655 op.bytes = p_stream->p_oggds_header->i_size + 1;
656 op.b_o_s = 1;
657 op.e_o_s = 0;
658 op.granulepos = 0;
659 op.packetno = p_stream->i_packet_no++;
660 ogg_stream_packetin( &p_stream->os, &op );
661 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
664 block_ChainAppend( &p_hdr, p_og );
668 /* Take care of the non b_o_s headers */
669 for( i = 0; i < p_mux->i_nb_inputs; i++ )
671 sout_input_t *p_input = p_mux->pp_inputs[i];
672 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
674 if( p_stream->i_fourcc == VLC_CODEC_VORBIS ||
675 p_stream->i_fourcc == VLC_CODEC_SPEEX ||
676 p_stream->i_fourcc == VLC_CODEC_THEORA )
678 unsigned pi_size[XIPH_MAX_HEADER_COUNT];
679 void *pp_data[XIPH_MAX_HEADER_COUNT];
680 unsigned i_count;
681 if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
682 p_input->p_fmt->i_extra, p_input->p_fmt->p_extra ) )
683 i_count = 0;
685 /* Special case, headers are already there in the incoming stream.
686 * We need to gather them an mark them as headers. */
687 for( unsigned i = 1; i < i_count; i++ )
689 op.bytes = pi_size[i];
690 op.packet = pp_data[i];
691 if( pi_size[i] <= 0 )
692 msg_Err( p_mux, "header data corrupted");
694 op.b_o_s = 0;
695 op.e_o_s = 0;
696 op.granulepos = 0;
697 op.packetno = p_stream->i_packet_no++;
698 ogg_stream_packetin( &p_stream->os, &op );
700 if( i == i_count - 1 )
701 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
702 else
703 p_og = OggStreamPageOut( p_mux, &p_stream->os, 0 );
704 if( p_og )
705 block_ChainAppend( &p_hdr, p_og );
707 for( unsigned i = 0; i < i_count; i++ )
708 free( pp_data[i] );
710 else if( p_stream->i_fourcc != VLC_CODEC_FLAC &&
711 p_stream->i_fourcc != VLC_CODEC_DIRAC )
713 uint8_t com[128];
714 int i_com;
716 /* comment */
717 com[0] = PACKET_TYPE_COMMENT;
718 i_com = snprintf( (char *)(com+1), 127,
719 PACKAGE_VERSION" stream output" )
720 + 1;
721 op.packet = com;
722 op.bytes = i_com;
723 op.b_o_s = 0;
724 op.e_o_s = 0;
725 op.granulepos = 0;
726 op.packetno = p_stream->i_packet_no++;
727 ogg_stream_packetin( &p_stream->os, &op );
728 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
729 block_ChainAppend( &p_hdr, p_og );
732 /* Special case for mp4v and flac */
733 if( ( p_stream->i_fourcc == VLC_CODEC_MP4V ||
734 p_stream->i_fourcc == VLC_CODEC_FLAC ) &&
735 p_input->p_fmt->i_extra )
737 /* Send a packet with the VOL data for mp4v
738 * or STREAMINFO for flac */
739 msg_Dbg( p_mux, "writing extra data" );
740 op.bytes = p_input->p_fmt->i_extra;
741 op.packet = p_input->p_fmt->p_extra;
742 if( p_stream->i_fourcc == VLC_CODEC_FLAC )
744 /* Skip the flac stream marker */
745 op.bytes -= 4;
746 op.packet+= 4;
748 op.b_o_s = 0;
749 op.e_o_s = 0;
750 op.granulepos = 0;
751 op.packetno = p_stream->i_packet_no++;
752 ogg_stream_packetin( &p_stream->os, &op );
753 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
754 block_ChainAppend( &p_hdr, p_og );
758 /* set HEADER flag */
759 for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
761 p_og->i_flags |= BLOCK_FLAG_HEADER;
763 return p_hdr;
766 static block_t *OggCreateFooter( sout_mux_t *p_mux )
768 sout_mux_sys_t *p_sys = p_mux->p_sys;
769 block_t *p_hdr = NULL;
770 block_t *p_og;
771 ogg_packet op;
772 int i;
774 /* flush all remaining data */
775 for( i = 0; i < p_mux->i_nb_inputs; i++ )
777 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
779 /* skip newly added streams */
780 if( p_stream->b_new ) continue;
782 if( ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
784 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
785 sout_AccessOutWrite( p_mux->p_access, p_og );
789 /* Write eos packets for each stream. */
790 for( i = 0; i < p_mux->i_nb_inputs; i++ )
792 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
794 /* skip newly added streams */
795 if( p_stream->b_new ) continue;
797 op.packet = NULL;
798 op.bytes = 0;
799 op.b_o_s = 0;
800 op.e_o_s = 1;
801 op.granulepos = p_stream->u_last_granulepos;
802 op.packetno = p_stream->i_packet_no++;
803 ogg_stream_packetin( &p_stream->os, &op );
805 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
806 block_ChainAppend( &p_hdr, p_og );
807 ogg_stream_clear( &p_stream->os );
810 for( i = 0; i < p_sys->i_del_streams; i++ )
812 op.packet = NULL;
813 op.bytes = 0;
814 op.b_o_s = 0;
815 op.e_o_s = 1;
816 op.granulepos = p_sys->pp_del_streams[i]->u_last_granulepos;
817 op.packetno = p_sys->pp_del_streams[i]->i_packet_no++;
818 ogg_stream_packetin( &p_sys->pp_del_streams[i]->os, &op );
820 p_og = OggStreamFlush( p_mux, &p_sys->pp_del_streams[i]->os, 0 );
821 block_ChainAppend( &p_hdr, p_og );
822 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
825 return p_hdr;
828 static void OggSetDate( block_t *p_og, mtime_t i_dts, mtime_t i_length )
830 int i_count;
831 block_t *p_tmp;
832 mtime_t i_delta;
834 for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
836 i_count++;
839 if( i_count == 0 ) return; /* ignore. */
841 i_delta = i_length / i_count;
843 for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
845 p_tmp->i_dts = i_dts;
846 p_tmp->i_length = i_delta;
848 i_dts += i_delta;
852 /*****************************************************************************
853 * Mux: multiplex available data in input fifos into the Ogg bitstream
854 *****************************************************************************/
855 static int Mux( sout_mux_t *p_mux )
857 sout_mux_sys_t *p_sys = p_mux->p_sys;
858 block_t *p_og = NULL;
859 mtime_t i_dts;
861 if( p_sys->i_add_streams || p_sys->i_del_streams )
863 /* Open new ogg stream */
864 if( sout_MuxGetStream( p_mux, 1, &i_dts) < 0 )
866 msg_Dbg( p_mux, "waiting for data..." );
867 return VLC_SUCCESS;
870 if( p_sys->i_streams )
872 /* Close current ogg stream */
873 int i;
875 msg_Dbg( p_mux, "writing footer" );
876 block_ChainAppend( &p_og, OggCreateFooter( p_mux ) );
878 /* Remove deleted logical streams */
879 for( i = 0; i < p_sys->i_del_streams; i++ )
881 FREENULL( p_sys->pp_del_streams[i]->p_oggds_header );
882 FREENULL( p_sys->pp_del_streams[i] );
884 FREENULL( p_sys->pp_del_streams );
885 p_sys->i_streams = 0;
888 msg_Dbg( p_mux, "writing header" );
889 p_sys->i_start_dts = i_dts;
890 p_sys->i_streams = p_mux->i_nb_inputs;
891 p_sys->i_del_streams = 0;
892 p_sys->i_add_streams = 0;
893 block_ChainAppend( &p_og, OggCreateHeader( p_mux ) );
895 /* Write header and/or footer */
896 OggSetDate( p_og, i_dts, 0 );
897 sout_AccessOutWrite( p_mux->p_access, p_og );
898 p_og = NULL;
901 for( ;; )
903 int i_stream = sout_MuxGetStream( p_mux, 1, NULL );
904 if( i_stream < 0 )
905 return VLC_SUCCESS;
906 MuxBlock( p_mux, p_mux->pp_inputs[i_stream] );
909 return VLC_SUCCESS;
912 static int MuxBlock( sout_mux_t *p_mux, sout_input_t *p_input )
914 sout_mux_sys_t *p_sys = p_mux->p_sys;
915 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
916 block_t *p_data = block_FifoGet( p_input->p_fifo );
917 block_t *p_og = NULL;
918 ogg_packet op;
920 if( p_stream->i_fourcc != VLC_CODEC_VORBIS &&
921 p_stream->i_fourcc != VLC_CODEC_FLAC &&
922 p_stream->i_fourcc != VLC_CODEC_SPEEX &&
923 p_stream->i_fourcc != VLC_CODEC_THEORA &&
924 p_stream->i_fourcc != VLC_CODEC_DIRAC )
926 p_data = block_Realloc( p_data, 1, p_data->i_buffer );
927 p_data->p_buffer[0] = PACKET_IS_SYNCPOINT; // FIXME
930 op.packet = p_data->p_buffer;
931 op.bytes = p_data->i_buffer;
932 op.b_o_s = 0;
933 op.e_o_s = 0;
934 op.packetno = p_stream->i_packet_no++;
936 if( p_stream->i_cat == AUDIO_ES )
938 if( p_stream->i_fourcc == VLC_CODEC_VORBIS ||
939 p_stream->i_fourcc == VLC_CODEC_FLAC ||
940 p_stream->i_fourcc == VLC_CODEC_SPEEX )
942 /* number of sample from begining + current packet */
943 op.granulepos =
944 ( p_data->i_dts - p_sys->i_start_dts + p_data->i_length ) *
945 (mtime_t)p_input->p_fmt->audio.i_rate / INT64_C(1000000);
947 else if( p_stream->p_oggds_header )
949 /* number of sample from begining */
950 op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) *
951 p_stream->p_oggds_header->i_samples_per_unit / INT64_C(1000000);
954 else if( p_stream->i_cat == VIDEO_ES )
956 if( p_stream->i_fourcc == VLC_CODEC_THEORA )
958 p_stream->i_num_frames++;
959 if( p_data->i_flags & BLOCK_FLAG_TYPE_I )
961 p_stream->i_num_keyframes++;
962 p_stream->i_last_keyframe = p_stream->i_num_frames;
965 op.granulepos = (p_stream->i_last_keyframe << p_stream->i_keyframe_granule_shift )
966 | (p_stream->i_num_frames-p_stream->i_last_keyframe);
968 else if( p_stream->i_fourcc == VLC_CODEC_DIRAC )
970 mtime_t dt = (p_data->i_dts - p_sys->i_start_dts + 1)
971 * p_input->p_fmt->video.i_frame_rate *2
972 / p_input->p_fmt->video.i_frame_rate_base
973 / INT64_C(1000000);
974 mtime_t delay = (p_data->i_pts - p_data->i_dts + 1)
975 * p_input->p_fmt->video.i_frame_rate *2
976 / p_input->p_fmt->video.i_frame_rate_base
977 / INT64_C(1000000);
978 if( p_data->i_flags & BLOCK_FLAG_TYPE_I )
979 p_stream->i_last_keyframe = dt;
980 mtime_t dist = dt - p_stream->i_last_keyframe;
981 op.granulepos = dt << 31 | (dist&0xff00) << 14
982 | (delay&0x1fff) << 9 | (dist&0xff);
984 else if( p_stream->p_oggds_header )
985 op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) * INT64_C(10) /
986 p_stream->p_oggds_header->i_time_unit;
988 else if( p_stream->i_cat == SPU_ES )
990 /* granulepos is in millisec */
991 op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) / 1000;
994 p_stream->u_last_granulepos = op.granulepos;
995 ogg_stream_packetin( &p_stream->os, &op );
997 if( p_stream->i_cat == SPU_ES ||
998 p_stream->i_fourcc == VLC_CODEC_SPEEX ||
999 p_stream->i_fourcc == VLC_CODEC_DIRAC )
1001 /* Subtitles or Speex packets are quite small so they
1002 * need to be flushed to be sent on time */
1003 /* The OggDirac mapping suggests ever so strongly that a
1004 * page flush occurs after each OggDirac packet, so to make
1005 * the timestamps unambiguous */
1006 p_og = OggStreamFlush( p_mux, &p_stream->os, p_data->i_dts );
1008 else
1010 p_og = OggStreamPageOut( p_mux, &p_stream->os, p_data->i_dts );
1013 if( p_og )
1015 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
1016 p_stream->i_dts = -1;
1017 p_stream->i_length = 0;
1019 sout_AccessOutWrite( p_mux->p_access, p_og );
1021 else
1023 if( p_stream->i_dts < 0 )
1025 p_stream->i_dts = p_data->i_dts;
1027 p_stream->i_length += p_data->i_length;
1030 block_Release( p_data );
1031 return VLC_SUCCESS;