1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: ogg.c,v 1.8 2003/07/01 17:14:58 sam 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 );
266 FREE( p_input->p_sys );
267 return( VLC_EGENERIC );
271 switch( p_input->p_fmt->i_fourcc )
273 case VLC_FOURCC( 'm', 'p','g', 'a' ):
274 case VLC_FOURCC( 'a', '5','2', ' ' ):
275 memcpy( p_stream->header.stream_type, "audio ", 8 );
276 if( p_input->p_fmt->i_fourcc == VLC_FOURCC( 'm', 'p','g', 'a' ) )
278 memcpy( p_stream->header.sub_type, "55 ", 4 );
280 else if( p_input->p_fmt->i_fourcc ==
281 VLC_FOURCC( 'a', '5','2', ' ' ) )
283 memcpy( p_stream->header.sub_type, "2000", 4 );
285 SetDWLE( &p_stream->header.i_size,
286 sizeof( ogg_stream_header_t ) - 1);
287 SetQWLE( &p_stream->header.i_time_unit, 1000000 ); /* is it used ? */
288 SetDWLE( &p_stream->header.i_default_len, 0 ); /* ??? */
289 SetDWLE( &p_stream->header.i_buffer_size, 30*1024 );
290 SetQWLE( &p_stream->header.i_samples_per_unit,
291 p_input->p_fmt->i_sample_rate );
292 SetWLE( &p_stream->header.i_bits_per_sample, 0 );
293 SetDWLE( &p_stream->header.header.audio.i_channels,
294 p_input->p_fmt->i_channels );
295 SetDWLE( &p_stream->header.header.audio.i_block_align,
296 p_input->p_fmt->i_block_align );
297 SetDWLE( &p_stream->header.header.audio.i_avgbytespersec, 0 );
300 case VLC_FOURCC( 'v', 'o', 'r', 'b' ):
301 msg_Dbg( p_mux, "vorbis stream" );
304 FREE( p_input->p_sys );
305 return( VLC_EGENERIC );
310 FREE( p_input->p_sys );
311 return( VLC_EGENERIC );
314 ogg_stream_init( &p_stream->os, rand () );
317 return( VLC_SUCCESS );
320 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
322 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
325 msg_Dbg( p_mux, "removing input" );
327 /* flush all remaining data */
331 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
334 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
336 sout_AccessOutWrite( p_mux->p_access, p_og );
339 ogg_stream_clear( &p_stream->os );
341 FREE( p_input->p_sys );
347 * TODO move this function to src/stream_output.c (used by nearly all muxers)
349 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
355 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
359 p_fifo = p_mux->pp_inputs[i]->p_fifo;
361 if( p_fifo->i_depth > 1 )
363 sout_buffer_t *p_buf;
365 p_buf = sout_FifoShow( p_fifo );
366 if( i_stream < 0 || p_buf->i_dts < i_dts )
368 i_dts = p_buf->i_dts;
374 return( -1 ); // wait that all fifo have at least 2 packets
379 *pi_stream = i_stream;
388 static sout_buffer_t *OggStreamFlush( sout_mux_t *p_mux,
389 ogg_stream_state *p_os, mtime_t i_pts )
391 sout_buffer_t *p_og, *p_og_first = NULL;
399 if( ( i_result = ogg_stream_flush( p_os, &og ) ) == 0 )
403 i_size = og.header_len + og.body_len;
404 p_og = sout_BufferNew( p_mux->p_sout, i_size);
406 memcpy( p_og->p_buffer, og.header, og.header_len );
407 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
408 p_og->i_size = i_size;
413 i_pts = 0; // write it only once
415 sout_BufferChain( &p_og_first, p_og );
418 return( p_og_first );
421 static sout_buffer_t *OggStreamPageOut( sout_mux_t *p_mux,
422 ogg_stream_state *p_os, mtime_t i_pts )
424 sout_buffer_t *p_og, *p_og_first = NULL;
432 if( ( i_result = ogg_stream_pageout( p_os, &og ) ) == 0 )
436 i_size = og.header_len + og.body_len;
437 p_og = sout_BufferNew( p_mux->p_sout, i_size);
439 memcpy( p_og->p_buffer, og.header, og.header_len );
440 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
441 p_og->i_size = i_size;
446 i_pts = 0; // write them only once
448 sout_BufferChain( &p_og_first, p_og );
451 return( p_og_first );
454 static sout_buffer_t *OggCreateHeader( sout_mux_t *p_mux, mtime_t i_dts )
456 sout_buffer_t *p_hdr = NULL;
461 /* Write header for each stream. All b_o_s (beginning of stream) packets
462 * must appear first in the ogg stream so we take care of them first. */
463 for( i = 0; i < p_mux->i_nb_inputs; i++ )
465 ogg_stream_t *p_stream;
467 p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
469 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
471 /* Special case, headers are already there in the
474 /* first packet in order: vorbis info */
475 p_og = sout_FifoGet( p_mux->pp_inputs[i]->p_fifo );
476 op.packet = p_og->p_buffer;
477 op.bytes = p_og->i_size;
481 op.packetno = p_stream->i_packet_no++;
482 ogg_stream_packetin( &p_stream->os, &op );
487 op.packet = (uint8_t*)&p_stream->header;
488 op.bytes = sizeof( ogg_stream_t );
492 op.packetno = p_stream->i_packet_no++;
493 ogg_stream_packetin( &p_stream->os, &op );
496 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
497 sout_BufferChain( &p_hdr, p_og );
500 /* Take care of the non b_o_s headers */
501 for( i = 0; i < p_mux->i_nb_inputs; i++ )
503 ogg_stream_t *p_stream;
505 p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
507 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
509 /* Special case, headers are already there in the incoming stream.
510 * We need to gather them an mark them as headers. */
512 for( j = 0; j < 2; j++ )
514 /* next packets in order: comments and codebooks */
515 p_og = sout_FifoGet( p_mux->pp_inputs[i]->p_fifo );
516 op.packet = p_og->p_buffer;
517 op.bytes = p_og->i_size;
521 op.packetno = p_stream->i_packet_no++;
522 ogg_stream_packetin( &p_stream->os, &op );
531 com[0] = PACKET_TYPE_COMMENT;
532 i_com = snprintf( &com[1], 128, "VLC 0.5.x stream output" ) + 1;
538 op.packetno = p_stream->i_packet_no++;
539 ogg_stream_packetin( &p_stream->os, &op );
542 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
543 sout_BufferChain( &p_hdr, p_og );
546 /* set HEADER flag */
547 for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
549 p_og->i_flags |= SOUT_BUFFER_FLAGS_HEADER;
554 static void OggSetDate( sout_buffer_t *p_og, mtime_t i_dts, mtime_t i_length )
557 sout_buffer_t *p_tmp;
560 for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
564 i_delta = i_length / i_count;
566 for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
568 p_tmp->i_dts = i_dts;
569 p_tmp->i_length = i_delta;
575 static int Mux( sout_mux_t *p_mux )
577 sout_mux_sys_t *p_sys = p_mux->p_sys;
578 sout_buffer_t *p_og = NULL;
582 if( p_sys->b_write_header )
584 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
586 msg_Dbg( p_mux, "waiting data..." );
587 return( VLC_SUCCESS );
589 p_sys->i_start_dts = i_dts;
591 msg_Dbg( p_mux, "writing header" );
592 sout_BufferChain( &p_og, OggCreateHeader( p_mux, i_dts ) );
593 p_sys->b_write_header = VLC_FALSE;
598 sout_input_t *p_input;
599 ogg_stream_t *p_stream;
600 sout_buffer_t *p_data;
603 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
605 //msg_Dbg( p_mux, "waiting data..." );
606 return( VLC_SUCCESS );
608 //msg_Dbg( p_mux, "doing job" );
610 if( p_sys->i_start_dts <= 0 ) p_sys->i_start_dts = i_dts;
612 p_input = p_mux->pp_inputs[i_stream];
613 p_stream = (ogg_stream_t*)p_input->p_sys;
615 p_data = sout_FifoGet( p_input->p_fifo );
617 if( p_stream->i_fourcc != VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
619 sout_BufferReallocFromPreHeader( p_mux->p_sout, p_data, 1 );
620 p_data->p_buffer[0] = PACKET_IS_SYNCPOINT; // FIXME
623 op.packet = p_data->p_buffer;
624 op.bytes = p_data->i_size;
627 op.packetno = p_stream->i_packet_no++;
629 if( p_stream->i_cat == AUDIO_ES )
631 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
633 /* number of sample from begining + current packet */
635 ( i_dts + p_data->i_length - p_sys->i_start_dts ) *
636 p_input->p_fmt->i_sample_rate / (int64_t)1000000;
640 /* number of sample from begining */
641 op.granulepos = ( i_dts - p_sys->i_start_dts ) *
642 p_stream->header.i_samples_per_unit / (int64_t)1000000;
645 else if( p_stream->i_cat == VIDEO_ES )
647 op.granulepos = ( i_dts - p_sys->i_start_dts ) / 1000;
650 ogg_stream_packetin( &p_stream->os, &op );
652 sout_BufferChain( &p_og, OggStreamPageOut( p_mux, &p_stream->os,
657 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
658 p_stream->i_dts = -1;
659 p_stream->i_length = 0;
661 sout_AccessOutWrite( p_mux->p_access, p_og );
667 if( p_stream->i_dts < 0 )
669 p_stream->i_dts = p_data->i_dts;
671 p_stream->i_length += p_data->i_length;
674 sout_BufferDelete( p_mux->p_sout, p_data );
677 return( VLC_SUCCESS );