1 /*****************************************************************************
2 * ogg.c: ogg muxer module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: ogg.c,v 1.10 2003/09/28 16:50:04 gbazin Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@netcourrier.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
30 #include <sys/types.h>
33 #include <vlc/input.h>
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Open ( vlc_object_t * );
44 static void Close ( vlc_object_t * );
46 static int Capability(sout_mux_t *, int, void *, void * );
47 static int AddStream( sout_mux_t *, sout_input_t * );
48 static int DelStream( sout_mux_t *, sout_input_t * );
49 static int Mux ( sout_mux_t * );
51 static sout_buffer_t *OggCreateHeader( sout_mux_t *, mtime_t );
52 static sout_buffer_t *OggCreateFooter( sout_mux_t *, mtime_t );
54 /*****************************************************************************
56 *****************************************************************************/
58 set_description( _("Ogg/ogm muxer") );
59 set_capability( "sout mux", 10 );
60 add_shortcut( "ogg" );
61 add_shortcut( "ogm" );
62 set_callbacks( Open, Close );
65 /*****************************************************************************
67 *****************************************************************************/
68 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
70 /* Structures used for OggDS headers used in ogm files */
72 #define PACKET_TYPE_HEADER 0x01
73 #define PACKET_TYPE_COMMENT 0x03
74 #define PACKET_IS_SYNCPOINT 0x08
77 #ifdef HAVE_ATTRIBUTE_PACKED
78 __attribute__((__packed__))
83 } ogg_stream_header_video_t;
86 #ifdef HAVE_ATTRIBUTE_PACKED
87 __attribute__((__packed__))
91 int16_t i_block_align;
92 int32_t i_avgbytespersec;
93 } ogg_stream_header_audio_t;
96 #ifdef HAVE_ATTRIBUTE_PACKED
97 __attribute__((__packed__))
100 uint8_t i_packet_type;
108 int64_t i_samples_per_unit;
109 int32_t i_default_len;
111 int32_t i_buffer_size;
112 int16_t i_bits_per_sample;
113 int16_t i_padding_0; // hum hum
116 ogg_stream_header_video_t video;
117 ogg_stream_header_audio_t audio;
120 } ogg_stream_header_t;
122 /* Helper writer functions */
124 #define SetWLE( p, v ) _SetWLE( (uint8_t*)p, v)
125 static void _SetWLE( uint8_t *p, uint16_t i_dw )
127 p[1] = ( i_dw >> 8 )&0xff;
128 p[0] = ( i_dw )&0xff;
131 #define SetDWLE( p, v ) _SetDWLE( (uint8_t*)p, v)
132 static void _SetDWLE( uint8_t *p, uint32_t i_dw )
134 p[3] = ( i_dw >> 24 )&0xff;
135 p[2] = ( i_dw >> 16 )&0xff;
136 p[1] = ( i_dw >> 8 )&0xff;
137 p[0] = ( i_dw )&0xff;
139 #define SetQWLE( p, v ) _SetQWLE( (uint8_t*)p, v)
140 static void _SetQWLE( uint8_t *p, uint64_t i_qw )
142 SetDWLE( p, i_qw&0xffffffff );
143 SetDWLE( p+4, ( i_qw >> 32)&0xffffffff );
147 * TODO move this function to src/stream_output.c (used by nearly all muxers)
149 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
155 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
159 p_fifo = p_mux->pp_inputs[i]->p_fifo;
161 if( p_fifo->i_depth > 2 )
163 sout_buffer_t *p_buf;
165 p_buf = sout_FifoShow( p_fifo );
166 if( i_stream < 0 || p_buf->i_dts < i_dts )
168 i_dts = p_buf->i_dts;
174 // wait that all fifo have at least 3 packets (3 vorbis headers)
180 *pi_stream = i_stream;
189 /*****************************************************************************
190 * Definitions of structures and functions used by this plugins
191 *****************************************************************************/
204 ogg_stream_header_t header;
208 struct sout_mux_sys_t
214 /* number of logical streams pending to be added */
217 /* logical streams pending to be deleted */
219 ogg_stream_t **pp_del_streams;
222 static void OggSetDate( sout_buffer_t *, mtime_t , mtime_t );
223 static sout_buffer_t *OggStreamFlush( sout_mux_t *, ogg_stream_state *,
226 /*****************************************************************************
228 *****************************************************************************/
229 static int Open( vlc_object_t *p_this )
231 sout_mux_t *p_mux = (sout_mux_t*)p_this;
232 sout_mux_sys_t *p_sys;
234 msg_Info( p_mux, "Open" );
236 p_sys = malloc( sizeof( sout_mux_sys_t ) );
237 p_sys->i_streams = 0;
238 p_sys->i_add_streams = 0;
239 p_sys->i_del_streams = 0;
240 p_sys->pp_del_streams = 0;
242 p_mux->p_sys = p_sys;
243 p_mux->pf_capacity = Capability;
244 p_mux->pf_addstream = AddStream;
245 p_mux->pf_delstream = DelStream;
247 p_mux->i_preheader = 1;
252 /*****************************************************************************
253 * Close: Finalize ogg bitstream and close muxer
254 *****************************************************************************/
255 static void Close( vlc_object_t * p_this )
257 sout_mux_t *p_mux = (sout_mux_t*)p_this;
258 sout_mux_sys_t *p_sys = p_mux->p_sys;
260 msg_Info( p_mux, "Close" );
262 if( p_sys->i_del_streams )
264 sout_buffer_t *p_og = NULL;
268 /* Close the current ogg stream */
269 msg_Dbg( p_mux, "writing footer" );
270 sout_BufferChain( &p_og, OggCreateFooter( p_mux, 0 ) );
272 /* Remove deleted logical streams */
273 for( i = 0; i < p_sys->i_del_streams; i++ )
275 i_dts = p_sys->pp_del_streams[i]->i_dts;
276 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
277 FREE( p_sys->pp_del_streams[i] );
279 FREE( p_sys->pp_del_streams );
280 p_sys->i_streams -= p_sys->i_del_streams;
283 OggSetDate( p_og, i_dts, 0 );
284 sout_AccessOutWrite( p_mux->p_access, p_og );
290 static int Capability( sout_mux_t *p_mux, int i_query, void *p_args,
295 case SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME:
296 *(vlc_bool_t*)p_answer = VLC_TRUE;
297 return( SOUT_MUX_CAP_ERR_OK );
299 return( SOUT_MUX_CAP_ERR_UNIMPLEMENTED );
303 /*****************************************************************************
304 * AddStream: Add an elementary stream to the muxed stream
305 *****************************************************************************/
306 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
308 sout_mux_sys_t *p_sys = p_mux->p_sys;
309 ogg_stream_t *p_stream;
311 msg_Dbg( p_mux, "adding input" );
313 p_input->p_sys = (void *)p_stream = malloc( sizeof( ogg_stream_t ) );
315 p_stream->i_cat = p_input->p_fmt->i_cat;
316 p_stream->i_fourcc = p_input->p_fmt->i_fourcc;
317 p_stream->i_packet_no = 0;
319 p_stream->header.i_packet_type = PACKET_TYPE_HEADER;
320 switch( p_input->p_fmt->i_cat )
323 switch( p_stream->i_fourcc )
325 case VLC_FOURCC( 'm', 'p','4', 'v' ):
326 case VLC_FOURCC( 'D', 'I','V', '3' ):
327 memcpy( p_stream->header.stream_type, "video ", 8 );
328 if( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p','4', 'v' ) )
330 memcpy( p_stream->header.sub_type, "XVID", 4 );
332 else if( p_stream->i_fourcc == VLC_FOURCC( 'D', 'I','V', '3' ) )
334 memcpy( p_stream->header.sub_type, "DIV3", 4 );
336 SetDWLE( &p_stream->header.i_size,
337 sizeof( ogg_stream_header_t ) - 1);
338 /* XXX this won't make mplayer happy,
339 * but vlc can read that without any problem so...*/
340 SetQWLE( &p_stream->header.i_time_unit, 10*1000 );
341 //(int64_t)10*1000*1000/(int64_t)25 ); // FIXME (25fps)
342 SetQWLE( &p_stream->header.i_samples_per_unit, 1 );
343 SetDWLE( &p_stream->header.i_default_len, 0 ); /* ??? */
344 SetDWLE( &p_stream->header.i_buffer_size, 1024*1024 );
345 SetWLE( &p_stream->header.i_bits_per_sample, 0 );
346 SetDWLE( &p_stream->header.header.video.i_width,
347 p_input->p_fmt->i_width );
348 SetDWLE( &p_stream->header.header.video.i_height,
349 p_input->p_fmt->i_height );
352 case VLC_FOURCC( 't', 'h', 'e', 'o' ):
353 msg_Dbg( p_mux, "theora stream" );
357 FREE( p_input->p_sys );
358 return( VLC_EGENERIC );
363 switch( p_stream->i_fourcc )
365 case VLC_FOURCC( 'm', 'p','g', 'a' ):
366 case VLC_FOURCC( 'a', '5','2', ' ' ):
367 memcpy( p_stream->header.stream_type, "audio ", 8 );
368 if( p_stream->i_fourcc == VLC_FOURCC( 'm', 'p','g', 'a' ) )
370 memcpy( p_stream->header.sub_type, "55 ", 4 );
372 else if( p_stream->i_fourcc == VLC_FOURCC( 'a', '5','2', ' ' ) )
374 memcpy( p_stream->header.sub_type, "2000", 4 );
376 SetDWLE( &p_stream->header.i_size,
377 sizeof( ogg_stream_header_t ) - 1);
378 SetQWLE( &p_stream->header.i_time_unit, 1000000 ); /*is it used ?*/
379 SetDWLE( &p_stream->header.i_default_len, 0 ); /* ??? */
380 SetDWLE( &p_stream->header.i_buffer_size, 30*1024 );
381 SetQWLE( &p_stream->header.i_samples_per_unit,
382 p_input->p_fmt->i_sample_rate );
383 SetWLE( &p_stream->header.i_bits_per_sample, 0 );
384 SetDWLE( &p_stream->header.header.audio.i_channels,
385 p_input->p_fmt->i_channels );
386 SetDWLE( &p_stream->header.header.audio.i_block_align,
387 p_input->p_fmt->i_block_align );
388 SetDWLE( &p_stream->header.header.audio.i_avgbytespersec, 0 );
391 case VLC_FOURCC( 'v', 'o', 'r', 'b' ):
392 msg_Dbg( p_mux, "vorbis stream" );
396 FREE( p_input->p_sys );
397 return( VLC_EGENERIC );
402 switch( p_stream->i_fourcc )
404 case VLC_FOURCC( 's', 'u','b', 't' ):
405 memcpy( p_stream->header.stream_type, "text ", 8 );
409 FREE( p_input->p_sys );
410 return( VLC_EGENERIC );
414 FREE( p_input->p_sys );
415 return( VLC_EGENERIC );
418 ogg_stream_init( &p_stream->os, rand () );
419 p_stream->b_new = VLC_TRUE;
421 p_sys->i_add_streams++;
423 return( VLC_SUCCESS );
426 /*****************************************************************************
427 * DelStream: Delete an elementary stream from the muxed stream
428 *****************************************************************************/
429 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
431 sout_mux_sys_t *p_sys = p_mux->p_sys;
432 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
435 msg_Dbg( p_mux, "removing input" );
437 /* flush all remaining data */
440 if( ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
442 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
443 sout_AccessOutWrite( p_mux->p_access, p_og );
446 /* move input in delete queue */
447 if( !p_stream->b_new )
449 p_sys->pp_del_streams = realloc( p_sys->pp_del_streams,
450 (p_sys->i_del_streams + 1) *
451 sizeof(ogg_stream_t *) );
452 p_sys->pp_del_streams[p_sys->i_del_streams++] = p_stream;
456 /* Wasn't already added so get rid of it */
457 ogg_stream_clear( &p_stream->os );
459 p_sys->i_add_streams--;
463 p_input->p_sys = NULL;
468 /*****************************************************************************
469 * Ogg bitstream manipulation routines
470 *****************************************************************************/
471 static sout_buffer_t *OggStreamFlush( sout_mux_t *p_mux,
472 ogg_stream_state *p_os, mtime_t i_pts )
474 sout_buffer_t *p_og, *p_og_first = NULL;
482 if( ( i_result = ogg_stream_flush( p_os, &og ) ) == 0 )
486 i_size = og.header_len + og.body_len;
487 p_og = sout_BufferNew( p_mux->p_sout, i_size);
489 memcpy( p_og->p_buffer, og.header, og.header_len );
490 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
491 p_og->i_size = i_size;
496 i_pts = 0; // write it only once
498 sout_BufferChain( &p_og_first, p_og );
501 return( p_og_first );
504 static sout_buffer_t *OggStreamPageOut( sout_mux_t *p_mux,
505 ogg_stream_state *p_os, mtime_t i_pts )
507 sout_buffer_t *p_og, *p_og_first = NULL;
515 if( ( i_result = ogg_stream_pageout( p_os, &og ) ) == 0 )
519 i_size = og.header_len + og.body_len;
520 p_og = sout_BufferNew( p_mux->p_sout, i_size);
522 memcpy( p_og->p_buffer, og.header, og.header_len );
523 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
524 p_og->i_size = i_size;
529 i_pts = 0; // write them only once
531 sout_BufferChain( &p_og_first, p_og );
534 return( p_og_first );
537 static sout_buffer_t *OggCreateHeader( sout_mux_t *p_mux, mtime_t i_dts )
539 sout_buffer_t *p_hdr = NULL;
544 /* Write header for each stream. All b_o_s (beginning of stream) packets
545 * must appear first in the ogg stream so we take care of them first. */
546 for( i = 0; i < p_mux->i_nb_inputs; i++ )
548 ogg_stream_t *p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
549 p_stream->b_new = VLC_FALSE;
551 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
552 p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
554 /* Special case, headers are already there in the
557 /* first packet in order: vorbis info */
558 p_og = sout_FifoGet( p_mux->pp_inputs[i]->p_fifo );
559 op.packet = p_og->p_buffer;
560 op.bytes = p_og->i_size;
564 op.packetno = p_stream->i_packet_no++;
565 ogg_stream_packetin( &p_stream->os, &op );
570 op.packet = (uint8_t*)&p_stream->header;
571 op.bytes = sizeof( ogg_stream_header_t );
575 op.packetno = p_stream->i_packet_no++;
576 ogg_stream_packetin( &p_stream->os, &op );
579 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
580 sout_BufferChain( &p_hdr, p_og );
583 /* Take care of the non b_o_s headers */
584 for( i = 0; i < p_mux->i_nb_inputs; i++ )
586 ogg_stream_t *p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
588 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
589 p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
591 /* Special case, headers are already there in the incoming stream.
592 * We need to gather them an mark them as headers. */
594 for( j = 0; j < 2; j++ )
596 /* next packets in order: comments and codebooks */
597 p_og = sout_FifoGet( p_mux->pp_inputs[i]->p_fifo );
598 op.packet = p_og->p_buffer;
599 op.bytes = p_og->i_size;
603 op.packetno = p_stream->i_packet_no++;
604 ogg_stream_packetin( &p_stream->os, &op );
613 com[0] = PACKET_TYPE_COMMENT;
614 i_com = snprintf( &com[1], 128, VERSION" stream output" ) + 1;
620 op.packetno = p_stream->i_packet_no++;
621 ogg_stream_packetin( &p_stream->os, &op );
624 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
625 sout_BufferChain( &p_hdr, p_og );
628 /* set HEADER flag */
629 for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
631 p_og->i_flags |= SOUT_BUFFER_FLAGS_HEADER;
636 static sout_buffer_t *OggCreateFooter( sout_mux_t *p_mux, mtime_t i_dts )
638 sout_mux_sys_t *p_sys = p_mux->p_sys;
639 sout_buffer_t *p_hdr = NULL;
644 /* Write eos packets for each stream. */
645 for( i = 0; i < p_mux->i_nb_inputs; i++ )
647 ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
649 /* skip newly added streams */
650 if( p_stream->b_new ) continue;
657 op.packetno = p_stream->i_packet_no++;
658 ogg_stream_packetin( &p_stream->os, &op );
660 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
661 sout_BufferChain( &p_hdr, p_og );
664 for( i = 0; i < p_sys->i_del_streams; i++ )
671 op.packetno = p_sys->pp_del_streams[i]->i_packet_no++;
672 ogg_stream_packetin( &p_sys->pp_del_streams[i]->os, &op );
674 p_og = OggStreamFlush( p_mux, &p_sys->pp_del_streams[i]->os, 0 );
675 sout_BufferChain( &p_hdr, p_og );
681 static void OggSetDate( sout_buffer_t *p_og, mtime_t i_dts, mtime_t i_length )
684 sout_buffer_t *p_tmp;
687 for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
691 i_delta = i_length / i_count;
693 for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
695 p_tmp->i_dts = i_dts;
696 p_tmp->i_length = i_delta;
702 /*****************************************************************************
703 * Mux: multiplex available data in input fifos into the Ogg bitstream
704 *****************************************************************************/
705 static int Mux( sout_mux_t *p_mux )
707 sout_mux_sys_t *p_sys = p_mux->p_sys;
708 sout_buffer_t *p_og = NULL;
712 if( p_sys->i_add_streams || p_sys->i_del_streams )
714 /* Open new ogg stream */
715 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
717 msg_Dbg( p_mux, "waiting for data..." );
718 return( VLC_SUCCESS );
721 if( p_sys->i_streams )
723 /* Close current ogg stream */
726 msg_Dbg( p_mux, "writing footer" );
727 sout_BufferChain( &p_og, OggCreateFooter( p_mux, 0 ) );
729 /* Remove deleted logical streams */
730 for( i = 0; i < p_sys->i_del_streams; i++ )
732 ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
733 FREE( p_sys->pp_del_streams[i] );
735 FREE( p_sys->pp_del_streams );
736 p_sys->i_streams = 0;
739 msg_Dbg( p_mux, "writing header" );
740 p_sys->i_start_dts = i_dts;
741 p_sys->i_streams = p_mux->i_nb_inputs;
742 p_sys->i_del_streams = 0;
743 p_sys->i_add_streams = 0;
744 sout_BufferChain( &p_og, OggCreateHeader( p_mux, i_dts ) );
746 /* Write header and/or footer */
747 OggSetDate( p_og, i_dts, 0 );
748 sout_AccessOutWrite( p_mux->p_access, p_og );
754 sout_input_t *p_input;
755 ogg_stream_t *p_stream;
756 sout_buffer_t *p_data;
759 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
761 return( VLC_SUCCESS );
764 p_input = p_mux->pp_inputs[i_stream];
765 p_stream = (ogg_stream_t*)p_input->p_sys;
766 p_data = sout_FifoGet( p_input->p_fifo );
768 if( p_stream->i_fourcc != VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
770 sout_BufferReallocFromPreHeader( p_mux->p_sout, p_data, 1 );
771 p_data->p_buffer[0] = PACKET_IS_SYNCPOINT; // FIXME
774 op.packet = p_data->p_buffer;
775 op.bytes = p_data->i_size;
778 op.packetno = p_stream->i_packet_no++;
780 if( p_stream->i_cat == AUDIO_ES )
782 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
784 /* number of sample from begining + current packet */
786 ( i_dts + p_data->i_length - p_sys->i_start_dts ) *
787 p_input->p_fmt->i_sample_rate / (int64_t)1000000;
791 /* number of sample from begining */
792 op.granulepos = ( i_dts - p_sys->i_start_dts ) *
793 p_stream->header.i_samples_per_unit / (int64_t)1000000;
796 else if( p_stream->i_cat == VIDEO_ES )
798 if( p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
800 op.granulepos = op.packetno; /* FIXME */
803 op.granulepos = ( i_dts - p_sys->i_start_dts ) / 1000;
805 else if( p_stream->i_cat == SPU_ES )
807 /* granulepos is in milisec */
808 op.granulepos = ( i_dts - p_sys->i_start_dts ) / 1000;
811 ogg_stream_packetin( &p_stream->os, &op );
813 sout_BufferChain( &p_og, OggStreamPageOut( p_mux, &p_stream->os,
818 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
819 p_stream->i_dts = -1;
820 p_stream->i_length = 0;
822 sout_AccessOutWrite( p_mux->p_access, p_og );
828 if( p_stream->i_dts < 0 )
830 p_stream->i_dts = p_data->i_dts;
832 p_stream->i_length += p_data->i_length;
835 sout_BufferDelete( p_mux->p_sout, p_data );
838 return( VLC_SUCCESS );