1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: ogg.c,v 1.5 2003/04/13 20:00:21 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
29 #include <sys/types.h>
32 #include <vlc/input.h>
39 /*****************************************************************************
41 *****************************************************************************/
42 static int Open ( vlc_object_t * );
43 static void Close ( vlc_object_t * );
45 static int Capability(sout_mux_t *, int, void *, void * );
46 static int AddStream( sout_mux_t *, sout_input_t * );
47 static int DelStream( sout_mux_t *, sout_input_t * );
48 static int Mux ( sout_mux_t * );
50 /*****************************************************************************
52 *****************************************************************************/
54 set_description( _("Ogg/ogm muxer") );
55 set_capability( "sout mux", 10 );
56 add_shortcut( "ogg" );
57 add_shortcut( "ogm" );
58 set_callbacks( Open, Close );
61 /*****************************************************************************
63 *****************************************************************************/
64 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
66 #define PACKET_TYPE_HEADER 0x01
67 #define PACKET_TYPE_COMMENT 0x03
69 #define PACKET_IS_SYNCPOINT 0x08
71 typedef struct __attribute__((__packed__))
75 } ogg_stream_header_video_t;
77 typedef struct __attribute__((__packed__))
80 int16_t i_block_align;
81 int32_t i_avgbytespersec;
82 } ogg_stream_header_audio_t;
84 typedef struct __attribute__((__packed__))
86 uint8_t i_packet_type;
94 int64_t i_samples_per_unit;
95 int32_t i_default_len;
97 int32_t i_buffer_size;
98 int16_t i_bits_per_sample;
99 int16_t i_padding_0; // hum hum
102 ogg_stream_header_video_t video;
103 ogg_stream_header_audio_t audio;
106 } ogg_stream_header_t;
114 ogg_stream_header_t header;
124 struct sout_mux_sys_t
132 #define SetWLE( p, v ) _SetWLE( (uint8_t*)p, v)
133 static void _SetWLE( uint8_t *p, uint16_t i_dw )
135 p[1] = ( i_dw >> 8 )&0xff;
136 p[0] = ( i_dw )&0xff;
139 #define SetDWLE( p, v ) _SetDWLE( (uint8_t*)p, v)
140 static void _SetDWLE( uint8_t *p, uint32_t i_dw )
142 p[3] = ( i_dw >> 24 )&0xff;
143 p[2] = ( i_dw >> 16 )&0xff;
144 p[1] = ( i_dw >> 8 )&0xff;
145 p[0] = ( i_dw )&0xff;
147 #define SetQWLE( p, v ) _SetQWLE( (uint8_t*)p, v)
148 static void _SetQWLE( uint8_t *p, uint64_t i_qw )
150 SetDWLE( p, i_qw&0xffffffff );
151 SetDWLE( p+4, ( i_qw >> 32)&0xffffffff );
154 static void OggSetDate( sout_buffer_t *, mtime_t , mtime_t );
155 static sout_buffer_t *OggStreamFlush( sout_mux_t *, ogg_stream_state *, mtime_t );
157 /*****************************************************************************
159 *****************************************************************************/
160 static int Open( vlc_object_t *p_this )
162 sout_mux_t *p_mux = (sout_mux_t*)p_this;
163 sout_mux_sys_t *p_sys;
165 msg_Info( p_mux, "Open" );
167 p_sys = malloc( sizeof( sout_mux_sys_t ) );
168 p_sys->i_streams = 0;
169 p_sys->b_write_header = VLC_TRUE;
171 p_mux->p_sys = p_sys;
172 p_mux->pf_capacity = Capability;
173 p_mux->pf_addstream = AddStream;
174 p_mux->pf_delstream = DelStream;
176 p_mux->i_preheader = 1;
181 /*****************************************************************************
183 *****************************************************************************/
185 static void Close( vlc_object_t * p_this )
187 sout_mux_t *p_mux = (sout_mux_t*)p_this;
188 sout_mux_sys_t *p_sys = p_mux->p_sys;
190 msg_Info( p_mux, "Close" );
195 static int Capability( sout_mux_t *p_mux, int i_query, void *p_args, void *p_answer )
199 case SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME:
200 *(vlc_bool_t*)p_answer = VLC_FALSE;
201 return( SOUT_MUX_CAP_ERR_OK );
203 return( SOUT_MUX_CAP_ERR_UNIMPLEMENTED );
207 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
209 sout_mux_sys_t *p_sys = p_mux->p_sys;
210 ogg_stream_t *p_stream;
212 msg_Dbg( p_mux, "adding input" );
213 p_input->p_sys = (void*)p_stream = malloc( sizeof( ogg_stream_t ) );
215 p_stream->i_cat = p_input->p_fmt->i_cat;
216 p_stream->i_fourcc = p_input->p_fmt->i_fourcc;
217 p_stream->i_packet_no = 0;
219 p_stream->header.i_packet_type = PACKET_TYPE_HEADER;
220 switch( p_input->p_fmt->i_cat )
223 switch( p_input->p_fmt->i_fourcc )
225 case VLC_FOURCC( 'm', 'p','4', 'v' ):
226 case VLC_FOURCC( 'D', 'I','V', '3' ):
227 memcpy( p_stream->header.stream_type,
230 if( p_input->p_fmt->i_fourcc == VLC_FOURCC( 'm', 'p','4', 'v' ) )
232 memcpy( p_stream->header.sub_type, "XVID", 4 );
234 else if( p_input->p_fmt->i_fourcc == VLC_FOURCC( 'D', 'I','V', '3' ) )
236 memcpy( p_stream->header.sub_type, "DIV3", 4 );
238 SetDWLE( &p_stream->header.i_size, sizeof( ogg_stream_header_t ) - 1);
239 /* XXX this won't make mplayer happy, but vlc can read that without any problem so...*/
240 SetQWLE( &p_stream->header.i_time_unit, 10*1000 );//(int64_t)10*1000*1000/(int64_t)25 ); // FIXME (25fps)
241 SetQWLE( &p_stream->header.i_samples_per_unit, 1 );
242 SetDWLE( &p_stream->header.i_default_len, 0 ); /* ??? */
243 SetDWLE( &p_stream->header.i_buffer_size, 1024*1024 );
244 SetWLE( &p_stream->header.i_bits_per_sample, 0 );
245 SetDWLE( &p_stream->header.header.video.i_width, p_input->p_fmt->i_width );
246 SetDWLE( &p_stream->header.header.video.i_height, p_input->p_fmt->i_height );
249 FREE( p_input->p_sys );
250 return( VLC_EGENERIC );
254 switch( p_input->p_fmt->i_fourcc )
256 case VLC_FOURCC( 'm', 'p','g', 'a' ):
257 case VLC_FOURCC( 'a', '5','2', ' ' ):
258 memcpy( p_stream->header.stream_type,
261 if( p_input->p_fmt->i_fourcc == VLC_FOURCC( 'm', 'p','g', 'a' ) )
263 memcpy( p_stream->header.sub_type, "55 ", 4 );
265 else if( p_input->p_fmt->i_fourcc == VLC_FOURCC( 'a', '5','2', ' ' ) )
267 memcpy( p_stream->header.sub_type, "2000", 4 );
269 SetDWLE( &p_stream->header.i_size, sizeof( ogg_stream_header_t ) - 1);
270 SetQWLE( &p_stream->header.i_time_unit, 1000000 ); /* is it used ? */
271 SetDWLE( &p_stream->header.i_default_len, 0 ); /* ??? */
272 SetDWLE( &p_stream->header.i_buffer_size, 30*1024 );
273 SetQWLE( &p_stream->header.i_samples_per_unit, p_input->p_fmt->i_sample_rate );
274 SetWLE( &p_stream->header.i_bits_per_sample, 0 );
275 SetDWLE( &p_stream->header.header.audio.i_channels, p_input->p_fmt->i_channels );
276 SetDWLE( &p_stream->header.header.audio.i_block_align, p_input->p_fmt->i_block_align );
277 SetDWLE( &p_stream->header.header.audio.i_avgbytespersec, 0 );
279 case VLC_FOURCC( 'v', 'o', 'r', 'b' ):
281 FREE( p_input->p_sys );
282 return( VLC_EGENERIC );
286 FREE( p_input->p_sys );
287 return( VLC_EGENERIC );
290 ogg_stream_init (&p_stream->os, rand ());
293 return( VLC_SUCCESS );
296 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
298 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
301 msg_Dbg( p_mux, "removing input" );
303 /* flush all remaining data */
307 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
310 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
312 sout_AccessOutWrite( p_mux->p_access, p_og );
315 ogg_stream_clear( &p_stream->os );
317 FREE( p_input->p_sys );
323 * TODO move this function to src/stream_output.c (used by nearly all muxers)
325 static int MuxGetStream( sout_mux_t *p_mux,
333 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
337 p_fifo = p_mux->pp_inputs[i]->p_fifo;
339 if( p_fifo->i_depth > 1 )
341 sout_buffer_t *p_buf;
343 p_buf = sout_FifoShow( p_fifo );
344 if( i_stream < 0 || p_buf->i_dts < i_dts )
346 i_dts = p_buf->i_dts;
352 return( -1 ); // wait that all fifo have at least 2 packets
357 *pi_stream = i_stream;
367 static sout_buffer_t *OggStreamFlush( sout_mux_t *p_mux,
368 ogg_stream_state *p_os,
371 sout_buffer_t *p_og, *p_og_first = NULL;
379 if( ( i_result = ogg_stream_flush( p_os, &og ) ) == 0 )
383 i_size = og.header_len + og.body_len;
384 p_og = sout_BufferNew( p_mux->p_sout, i_size);
386 memcpy( p_og->p_buffer,
389 memcpy( p_og->p_buffer + og.header_len,
392 p_og->i_size = i_size;
397 i_pts = 0; // write it only once
399 sout_BufferChain( &p_og_first, p_og );
402 return( p_og_first );
404 static sout_buffer_t *OggStreamPageOut( sout_mux_t *p_mux,
405 ogg_stream_state *p_os,
408 sout_buffer_t *p_og, *p_og_first = NULL;
416 if( ( i_result = ogg_stream_pageout( p_os, &og ) ) == 0 )
420 i_size = og.header_len + og.body_len;
421 p_og = sout_BufferNew( p_mux->p_sout, i_size);
423 memcpy( p_og->p_buffer,
426 memcpy( p_og->p_buffer + og.header_len,
429 p_og->i_size = i_size;
434 i_pts = 0; // write them only once
436 sout_BufferChain( &p_og_first, p_og );
439 return( p_og_first );
441 static sout_buffer_t *OggCreateHeader( sout_mux_t *p_mux, mtime_t i_dts )
443 sout_buffer_t *p_hdr = NULL;
448 /* write header for each stream */
449 for( i = 0; i < p_mux->i_nb_inputs; i++ )
451 ogg_stream_t *p_stream;
453 p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
455 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
468 op.packet = (uint8_t*)&p_stream->header;
469 op.bytes = sizeof( ogg_stream_t );
473 op.packetno = p_stream->i_packet_no++;
474 ogg_stream_packetin( &p_stream->os, &op );
476 /* I don't know why but this produce broken file */
478 com[0] = PACKET_TYPE_COMMENT;
479 i_com = snprintf( &com[1], 128, "VLC 0.5.x stream output" ) + 1;
485 op.packetno = p_stream->i_packet_no++;
486 ogg_stream_packetin( &p_stream->os, &op );
490 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
491 sout_BufferChain( &p_hdr, p_og );
494 /* set HEADER flag */
495 for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
497 p_og->i_flags |= SOUT_BUFFER_FLAGS_HEADER;
503 static void OggSetDate( sout_buffer_t *p_og, mtime_t i_dts, mtime_t i_length )
506 sout_buffer_t *p_tmp;
509 for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
513 i_delta = i_length / i_count;
515 for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
517 p_tmp->i_dts = i_dts;
518 p_tmp->i_length = i_delta;
524 static int Mux ( sout_mux_t *p_mux )
526 sout_mux_sys_t *p_sys = p_mux->p_sys;
527 sout_buffer_t *p_og = NULL;
531 if( p_sys->b_write_header )
533 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
535 msg_Dbg( p_mux, "waiting data..." );
536 return( VLC_SUCCESS );
538 p_sys->i_start_dts = i_dts;
540 msg_Dbg( p_mux, "writing header" );
541 sout_BufferChain( &p_og, OggCreateHeader( p_mux, i_dts ) );
542 p_sys->b_write_header = VLC_FALSE;
547 sout_input_t *p_input;
548 ogg_stream_t *p_stream;
549 sout_buffer_t *p_data;
552 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
554 //msg_Dbg( p_mux, "waiting data..." );
555 return( VLC_SUCCESS );
557 //msg_Dbg( p_mux, "doing job" );
559 p_input = p_mux->pp_inputs[i_stream];
560 p_stream = (ogg_stream_t*)p_input->p_sys;
562 p_data = sout_FifoGet( p_input->p_fifo );
564 sout_BufferReallocFromPreHeader( p_mux->p_sout, p_data, 1 );
565 p_data->p_buffer[0] = PACKET_IS_SYNCPOINT; // FIXME
567 op.packet = p_data->p_buffer;
568 op.bytes = p_data->i_size;
571 if( p_stream->i_cat == AUDIO_ES )
573 /* number of sample from begining */
574 op.granulepos = ( i_dts - p_sys->i_start_dts ) *
575 p_stream->header.i_samples_per_unit / (int64_t)1000000;
577 else if( p_stream->i_cat == VIDEO_ES )
579 op.granulepos = ( i_dts - p_sys->i_start_dts ) / 1000;//p_stream->i_packet_no;
581 op.packetno = p_stream->i_packet_no++;
582 ogg_stream_packetin( &p_stream->os, &op );
584 sout_BufferChain( &p_og,
585 OggStreamPageOut( p_mux,
591 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
592 p_stream->i_dts = -1;
593 p_stream->i_length = 0;
595 msg_Dbg( p_mux, "writing data..." );
596 sout_AccessOutWrite( p_mux->p_access, p_og );
602 if( p_stream->i_dts < 0 )
604 p_stream->i_dts = p_data->i_dts;
606 p_stream->i_length += p_data->i_length;
609 sout_BufferDelete( p_mux->p_sout, p_data );
612 return( VLC_SUCCESS );