1 /*****************************************************************************
2 * ogg.c: ogg muxer module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: ogg.c,v 1.9 2003/09/25 23:09:41 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 /*****************************************************************************
53 *****************************************************************************/
55 set_description( _("Ogg/ogm muxer") );
56 set_capability( "sout mux", 10 );
57 add_shortcut( "ogg" );
58 add_shortcut( "ogm" );
59 set_callbacks( Open, Close );
62 /*****************************************************************************
64 *****************************************************************************/
65 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
67 #define PACKET_TYPE_HEADER 0x01
68 #define PACKET_TYPE_COMMENT 0x03
70 #define PACKET_IS_SYNCPOINT 0x08
73 #ifdef HAVE_ATTRIBUTE_PACKED
74 __attribute__((__packed__))
79 } ogg_stream_header_video_t;
82 #ifdef HAVE_ATTRIBUTE_PACKED
83 __attribute__((__packed__))
87 int16_t i_block_align;
88 int32_t i_avgbytespersec;
89 } ogg_stream_header_audio_t;
92 #ifdef HAVE_ATTRIBUTE_PACKED
93 __attribute__((__packed__))
96 uint8_t i_packet_type;
104 int64_t i_samples_per_unit;
105 int32_t i_default_len;
107 int32_t i_buffer_size;
108 int16_t i_bits_per_sample;
109 int16_t i_padding_0; // hum hum
112 ogg_stream_header_video_t video;
113 ogg_stream_header_audio_t audio;
116 } ogg_stream_header_t;
124 ogg_stream_header_t header;
134 struct sout_mux_sys_t
142 #define SetWLE( p, v ) _SetWLE( (uint8_t*)p, v)
143 static void _SetWLE( uint8_t *p, uint16_t i_dw )
145 p[1] = ( i_dw >> 8 )&0xff;
146 p[0] = ( i_dw )&0xff;
149 #define SetDWLE( p, v ) _SetDWLE( (uint8_t*)p, v)
150 static void _SetDWLE( uint8_t *p, uint32_t i_dw )
152 p[3] = ( i_dw >> 24 )&0xff;
153 p[2] = ( i_dw >> 16 )&0xff;
154 p[1] = ( i_dw >> 8 )&0xff;
155 p[0] = ( i_dw )&0xff;
157 #define SetQWLE( p, v ) _SetQWLE( (uint8_t*)p, v)
158 static void _SetQWLE( uint8_t *p, uint64_t i_qw )
160 SetDWLE( p, i_qw&0xffffffff );
161 SetDWLE( p+4, ( i_qw >> 32)&0xffffffff );
164 static void OggSetDate( sout_buffer_t *, mtime_t , mtime_t );
165 static sout_buffer_t *OggStreamFlush( sout_mux_t *, ogg_stream_state *,
168 /*****************************************************************************
170 *****************************************************************************/
171 static int Open( vlc_object_t *p_this )
173 sout_mux_t *p_mux = (sout_mux_t*)p_this;
174 sout_mux_sys_t *p_sys;
176 msg_Info( p_mux, "Open" );
178 p_sys = malloc( sizeof( sout_mux_sys_t ) );
179 p_sys->i_streams = 0;
180 p_sys->b_write_header = VLC_TRUE;
182 p_mux->p_sys = p_sys;
183 p_mux->pf_capacity = Capability;
184 p_mux->pf_addstream = AddStream;
185 p_mux->pf_delstream = DelStream;
187 p_mux->i_preheader = 1;
192 /*****************************************************************************
194 *****************************************************************************/
196 static void Close( vlc_object_t * p_this )
198 sout_mux_t *p_mux = (sout_mux_t*)p_this;
199 sout_mux_sys_t *p_sys = p_mux->p_sys;
201 msg_Info( p_mux, "Close" );
206 static int Capability( sout_mux_t *p_mux, int i_query, void *p_args,
211 case SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME:
212 *(vlc_bool_t*)p_answer = VLC_FALSE;
213 return( SOUT_MUX_CAP_ERR_OK );
215 return( SOUT_MUX_CAP_ERR_UNIMPLEMENTED );
219 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
221 sout_mux_sys_t *p_sys = p_mux->p_sys;
222 ogg_stream_t *p_stream;
224 msg_Dbg( p_mux, "adding input" );
225 p_input->p_sys = (void*)p_stream = malloc( sizeof( ogg_stream_t ) );
227 p_stream->i_cat = p_input->p_fmt->i_cat;
228 p_stream->i_fourcc = p_input->p_fmt->i_fourcc;
229 p_stream->i_packet_no = 0;
231 p_stream->header.i_packet_type = PACKET_TYPE_HEADER;
232 switch( p_input->p_fmt->i_cat )
235 switch( p_input->p_fmt->i_fourcc )
237 case VLC_FOURCC( 'm', 'p','4', 'v' ):
238 case VLC_FOURCC( 'D', 'I','V', '3' ):
239 memcpy( p_stream->header.stream_type, "video ", 8 );
240 if( p_input->p_fmt->i_fourcc == VLC_FOURCC( 'm', 'p','4', 'v' ) )
242 memcpy( p_stream->header.sub_type, "XVID", 4 );
244 else if( p_input->p_fmt->i_fourcc ==
245 VLC_FOURCC( 'D', 'I','V', '3' ) )
247 memcpy( p_stream->header.sub_type, "DIV3", 4 );
249 SetDWLE( &p_stream->header.i_size,
250 sizeof( ogg_stream_header_t ) - 1);
251 /* XXX this won't make mplayer happy,
252 * but vlc can read that without any problem so...*/
253 SetQWLE( &p_stream->header.i_time_unit, 10*1000 );
254 //(int64_t)10*1000*1000/(int64_t)25 ); // FIXME (25fps)
255 SetQWLE( &p_stream->header.i_samples_per_unit, 1 );
256 SetDWLE( &p_stream->header.i_default_len, 0 ); /* ??? */
257 SetDWLE( &p_stream->header.i_buffer_size, 1024*1024 );
258 SetWLE( &p_stream->header.i_bits_per_sample, 0 );
259 SetDWLE( &p_stream->header.header.video.i_width,
260 p_input->p_fmt->i_width );
261 SetDWLE( &p_stream->header.header.video.i_height,
262 p_input->p_fmt->i_height );
265 case VLC_FOURCC( 't', 'h', 'e', 'o' ):
266 msg_Dbg( p_mux, "theora stream" );
270 FREE( p_input->p_sys );
271 return( VLC_EGENERIC );
276 switch( p_input->p_fmt->i_fourcc )
278 case VLC_FOURCC( 'm', 'p','g', 'a' ):
279 case VLC_FOURCC( 'a', '5','2', ' ' ):
280 memcpy( p_stream->header.stream_type, "audio ", 8 );
281 if( p_input->p_fmt->i_fourcc == VLC_FOURCC( 'm', 'p','g', 'a' ) )
283 memcpy( p_stream->header.sub_type, "55 ", 4 );
285 else if( p_input->p_fmt->i_fourcc ==
286 VLC_FOURCC( 'a', '5','2', ' ' ) )
288 memcpy( p_stream->header.sub_type, "2000", 4 );
290 SetDWLE( &p_stream->header.i_size,
291 sizeof( ogg_stream_header_t ) - 1);
292 SetQWLE( &p_stream->header.i_time_unit, 1000000 ); /* is it used ? */
293 SetDWLE( &p_stream->header.i_default_len, 0 ); /* ??? */
294 SetDWLE( &p_stream->header.i_buffer_size, 30*1024 );
295 SetQWLE( &p_stream->header.i_samples_per_unit,
296 p_input->p_fmt->i_sample_rate );
297 SetWLE( &p_stream->header.i_bits_per_sample, 0 );
298 SetDWLE( &p_stream->header.header.audio.i_channels,
299 p_input->p_fmt->i_channels );
300 SetDWLE( &p_stream->header.header.audio.i_block_align,
301 p_input->p_fmt->i_block_align );
302 SetDWLE( &p_stream->header.header.audio.i_avgbytespersec, 0 );
305 case VLC_FOURCC( 'v', 'o', 'r', 'b' ):
306 msg_Dbg( p_mux, "vorbis stream" );
310 FREE( p_input->p_sys );
311 return( VLC_EGENERIC );
316 FREE( p_input->p_sys );
317 return( VLC_EGENERIC );
320 ogg_stream_init( &p_stream->os, rand () );
323 return( VLC_SUCCESS );
326 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
328 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
331 msg_Dbg( p_mux, "removing input" );
333 /* flush all remaining data */
337 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
340 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
342 sout_AccessOutWrite( p_mux->p_access, p_og );
345 ogg_stream_clear( &p_stream->os );
347 FREE( p_input->p_sys );
353 * TODO move this function to src/stream_output.c (used by nearly all muxers)
355 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
361 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
365 p_fifo = p_mux->pp_inputs[i]->p_fifo;
367 if( p_fifo->i_depth > 1 )
369 sout_buffer_t *p_buf;
371 p_buf = sout_FifoShow( p_fifo );
372 if( i_stream < 0 || p_buf->i_dts < i_dts )
374 i_dts = p_buf->i_dts;
380 return( -1 ); // wait that all fifo have at least 2 packets
385 *pi_stream = i_stream;
394 static sout_buffer_t *OggStreamFlush( sout_mux_t *p_mux,
395 ogg_stream_state *p_os, mtime_t i_pts )
397 sout_buffer_t *p_og, *p_og_first = NULL;
405 if( ( i_result = ogg_stream_flush( p_os, &og ) ) == 0 )
409 i_size = og.header_len + og.body_len;
410 p_og = sout_BufferNew( p_mux->p_sout, i_size);
412 memcpy( p_og->p_buffer, og.header, og.header_len );
413 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
414 p_og->i_size = i_size;
419 i_pts = 0; // write it only once
421 sout_BufferChain( &p_og_first, p_og );
424 return( p_og_first );
427 static sout_buffer_t *OggStreamPageOut( sout_mux_t *p_mux,
428 ogg_stream_state *p_os, mtime_t i_pts )
430 sout_buffer_t *p_og, *p_og_first = NULL;
438 if( ( i_result = ogg_stream_pageout( p_os, &og ) ) == 0 )
442 i_size = og.header_len + og.body_len;
443 p_og = sout_BufferNew( p_mux->p_sout, i_size);
445 memcpy( p_og->p_buffer, og.header, og.header_len );
446 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
447 p_og->i_size = i_size;
452 i_pts = 0; // write them only once
454 sout_BufferChain( &p_og_first, p_og );
457 return( p_og_first );
460 static sout_buffer_t *OggCreateHeader( sout_mux_t *p_mux, mtime_t i_dts )
462 sout_buffer_t *p_hdr = NULL;
467 /* Write header for each stream. All b_o_s (beginning of stream) packets
468 * must appear first in the ogg stream so we take care of them first. */
469 for( i = 0; i < p_mux->i_nb_inputs; i++ )
471 ogg_stream_t *p_stream;
473 p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
475 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
476 p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
478 /* Special case, headers are already there in the
481 /* first packet in order: vorbis info */
482 p_og = sout_FifoGet( p_mux->pp_inputs[i]->p_fifo );
483 op.packet = p_og->p_buffer;
484 op.bytes = p_og->i_size;
488 op.packetno = p_stream->i_packet_no++;
489 ogg_stream_packetin( &p_stream->os, &op );
494 op.packet = (uint8_t*)&p_stream->header;
495 op.bytes = sizeof( ogg_stream_t );
499 op.packetno = p_stream->i_packet_no++;
500 ogg_stream_packetin( &p_stream->os, &op );
503 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
504 sout_BufferChain( &p_hdr, p_og );
507 /* Take care of the non b_o_s headers */
508 for( i = 0; i < p_mux->i_nb_inputs; i++ )
510 ogg_stream_t *p_stream;
512 p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
514 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) ||
515 p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
517 /* Special case, headers are already there in the incoming stream.
518 * We need to gather them an mark them as headers. */
520 for( j = 0; j < 2; j++ )
522 /* next packets in order: comments and codebooks */
523 p_og = sout_FifoGet( p_mux->pp_inputs[i]->p_fifo );
524 op.packet = p_og->p_buffer;
525 op.bytes = p_og->i_size;
529 op.packetno = p_stream->i_packet_no++;
530 ogg_stream_packetin( &p_stream->os, &op );
539 com[0] = PACKET_TYPE_COMMENT;
540 i_com = snprintf( &com[1], 128, "VLC 0.5.x stream output" ) + 1;
546 op.packetno = p_stream->i_packet_no++;
547 ogg_stream_packetin( &p_stream->os, &op );
550 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
551 sout_BufferChain( &p_hdr, p_og );
554 /* set HEADER flag */
555 for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
557 p_og->i_flags |= SOUT_BUFFER_FLAGS_HEADER;
562 static void OggSetDate( sout_buffer_t *p_og, mtime_t i_dts, mtime_t i_length )
565 sout_buffer_t *p_tmp;
568 for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
572 i_delta = i_length / i_count;
574 for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
576 p_tmp->i_dts = i_dts;
577 p_tmp->i_length = i_delta;
583 static int Mux( sout_mux_t *p_mux )
585 sout_mux_sys_t *p_sys = p_mux->p_sys;
586 sout_buffer_t *p_og = NULL;
590 if( p_sys->b_write_header )
592 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
594 msg_Dbg( p_mux, "waiting data..." );
595 return( VLC_SUCCESS );
597 p_sys->i_start_dts = i_dts;
599 msg_Dbg( p_mux, "writing header" );
600 sout_BufferChain( &p_og, OggCreateHeader( p_mux, i_dts ) );
601 p_sys->b_write_header = VLC_FALSE;
606 sout_input_t *p_input;
607 ogg_stream_t *p_stream;
608 sout_buffer_t *p_data;
611 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
613 //msg_Dbg( p_mux, "waiting data..." );
614 return( VLC_SUCCESS );
616 //msg_Dbg( p_mux, "doing job" );
618 if( p_sys->i_start_dts <= 0 ) p_sys->i_start_dts = i_dts;
620 p_input = p_mux->pp_inputs[i_stream];
621 p_stream = (ogg_stream_t*)p_input->p_sys;
623 p_data = sout_FifoGet( p_input->p_fifo );
625 if( p_stream->i_fourcc != VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
627 sout_BufferReallocFromPreHeader( p_mux->p_sout, p_data, 1 );
628 p_data->p_buffer[0] = PACKET_IS_SYNCPOINT; // FIXME
631 op.packet = p_data->p_buffer;
632 op.bytes = p_data->i_size;
635 op.packetno = p_stream->i_packet_no++;
637 if( p_stream->i_cat == AUDIO_ES )
639 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
641 /* number of sample from begining + current packet */
643 ( i_dts + p_data->i_length - p_sys->i_start_dts ) *
644 p_input->p_fmt->i_sample_rate / (int64_t)1000000;
648 /* number of sample from begining */
649 op.granulepos = ( i_dts - p_sys->i_start_dts ) *
650 p_stream->header.i_samples_per_unit / (int64_t)1000000;
653 else if( p_stream->i_cat == VIDEO_ES )
655 if( p_stream->i_fourcc == VLC_FOURCC( 't', 'h', 'e', 'o' ) )
657 op.granulepos = op.packetno; /* FIXME */
660 op.granulepos = ( i_dts - p_sys->i_start_dts ) / 1000;
663 ogg_stream_packetin( &p_stream->os, &op );
665 sout_BufferChain( &p_og, OggStreamPageOut( p_mux, &p_stream->os,
670 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
671 p_stream->i_dts = -1;
672 p_stream->i_length = 0;
674 sout_AccessOutWrite( p_mux->p_access, p_og );
680 if( p_stream->i_dts < 0 )
682 p_stream->i_dts = p_data->i_dts;
684 p_stream->i_length += p_data->i_length;
687 sout_BufferDelete( p_mux->p_sout, p_data );
690 return( VLC_SUCCESS );