1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: ogg.c,v 1.6 2003/06/23 23:51:31 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
72 typedef struct __attribute__((__packed__))
76 } ogg_stream_header_video_t;
78 typedef struct __attribute__((__packed__))
81 int16_t i_block_align;
82 int32_t i_avgbytespersec;
83 } ogg_stream_header_audio_t;
85 typedef struct __attribute__((__packed__))
87 uint8_t i_packet_type;
95 int64_t i_samples_per_unit;
96 int32_t i_default_len;
98 int32_t i_buffer_size;
99 int16_t i_bits_per_sample;
100 int16_t i_padding_0; // hum hum
103 ogg_stream_header_video_t video;
104 ogg_stream_header_audio_t audio;
107 } ogg_stream_header_t;
115 ogg_stream_header_t header;
125 struct sout_mux_sys_t
133 #define SetWLE( p, v ) _SetWLE( (uint8_t*)p, v)
134 static void _SetWLE( uint8_t *p, uint16_t i_dw )
136 p[1] = ( i_dw >> 8 )&0xff;
137 p[0] = ( i_dw )&0xff;
140 #define SetDWLE( p, v ) _SetDWLE( (uint8_t*)p, v)
141 static void _SetDWLE( uint8_t *p, uint32_t i_dw )
143 p[3] = ( i_dw >> 24 )&0xff;
144 p[2] = ( i_dw >> 16 )&0xff;
145 p[1] = ( i_dw >> 8 )&0xff;
146 p[0] = ( i_dw )&0xff;
148 #define SetQWLE( p, v ) _SetQWLE( (uint8_t*)p, v)
149 static void _SetQWLE( uint8_t *p, uint64_t i_qw )
151 SetDWLE( p, i_qw&0xffffffff );
152 SetDWLE( p+4, ( i_qw >> 32)&0xffffffff );
155 static void OggSetDate( sout_buffer_t *, mtime_t , mtime_t );
156 static sout_buffer_t *OggStreamFlush( sout_mux_t *, ogg_stream_state *,
159 /*****************************************************************************
161 *****************************************************************************/
162 static int Open( vlc_object_t *p_this )
164 sout_mux_t *p_mux = (sout_mux_t*)p_this;
165 sout_mux_sys_t *p_sys;
167 msg_Info( p_mux, "Open" );
169 p_sys = malloc( sizeof( sout_mux_sys_t ) );
170 p_sys->i_streams = 0;
171 p_sys->b_write_header = VLC_TRUE;
173 p_mux->p_sys = p_sys;
174 p_mux->pf_capacity = Capability;
175 p_mux->pf_addstream = AddStream;
176 p_mux->pf_delstream = DelStream;
178 p_mux->i_preheader = 1;
183 /*****************************************************************************
185 *****************************************************************************/
187 static void Close( vlc_object_t * p_this )
189 sout_mux_t *p_mux = (sout_mux_t*)p_this;
190 sout_mux_sys_t *p_sys = p_mux->p_sys;
192 msg_Info( p_mux, "Close" );
197 static int Capability( sout_mux_t *p_mux, int i_query, void *p_args,
202 case SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME:
203 *(vlc_bool_t*)p_answer = VLC_FALSE;
204 return( SOUT_MUX_CAP_ERR_OK );
206 return( SOUT_MUX_CAP_ERR_UNIMPLEMENTED );
210 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
212 sout_mux_sys_t *p_sys = p_mux->p_sys;
213 ogg_stream_t *p_stream;
215 msg_Dbg( p_mux, "adding input" );
216 p_input->p_sys = (void*)p_stream = malloc( sizeof( ogg_stream_t ) );
218 p_stream->i_cat = p_input->p_fmt->i_cat;
219 p_stream->i_fourcc = p_input->p_fmt->i_fourcc;
220 p_stream->i_packet_no = 0;
222 p_stream->header.i_packet_type = PACKET_TYPE_HEADER;
223 switch( p_input->p_fmt->i_cat )
226 switch( p_input->p_fmt->i_fourcc )
228 case VLC_FOURCC( 'm', 'p','4', 'v' ):
229 case VLC_FOURCC( 'D', 'I','V', '3' ):
230 memcpy( p_stream->header.stream_type, "video ", 8 );
231 if( p_input->p_fmt->i_fourcc == VLC_FOURCC( 'm', 'p','4', 'v' ) )
233 memcpy( p_stream->header.sub_type, "XVID", 4 );
235 else if( p_input->p_fmt->i_fourcc ==
236 VLC_FOURCC( 'D', 'I','V', '3' ) )
238 memcpy( p_stream->header.sub_type, "DIV3", 4 );
240 SetDWLE( &p_stream->header.i_size,
241 sizeof( ogg_stream_header_t ) - 1);
242 /* XXX this won't make mplayer happy,
243 * but vlc can read that without any problem so...*/
244 SetQWLE( &p_stream->header.i_time_unit, 10*1000 );
245 //(int64_t)10*1000*1000/(int64_t)25 ); // FIXME (25fps)
246 SetQWLE( &p_stream->header.i_samples_per_unit, 1 );
247 SetDWLE( &p_stream->header.i_default_len, 0 ); /* ??? */
248 SetDWLE( &p_stream->header.i_buffer_size, 1024*1024 );
249 SetWLE( &p_stream->header.i_bits_per_sample, 0 );
250 SetDWLE( &p_stream->header.header.video.i_width,
251 p_input->p_fmt->i_width );
252 SetDWLE( &p_stream->header.header.video.i_height,
253 p_input->p_fmt->i_height );
257 FREE( p_input->p_sys );
258 return( VLC_EGENERIC );
262 switch( p_input->p_fmt->i_fourcc )
264 case VLC_FOURCC( 'm', 'p','g', 'a' ):
265 case VLC_FOURCC( 'a', '5','2', ' ' ):
266 memcpy( p_stream->header.stream_type, "audio ", 8 );
267 if( p_input->p_fmt->i_fourcc == VLC_FOURCC( 'm', 'p','g', 'a' ) )
269 memcpy( p_stream->header.sub_type, "55 ", 4 );
271 else if( p_input->p_fmt->i_fourcc ==
272 VLC_FOURCC( 'a', '5','2', ' ' ) )
274 memcpy( p_stream->header.sub_type, "2000", 4 );
276 SetDWLE( &p_stream->header.i_size,
277 sizeof( ogg_stream_header_t ) - 1);
278 SetQWLE( &p_stream->header.i_time_unit, 1000000 ); /* is it used ? */
279 SetDWLE( &p_stream->header.i_default_len, 0 ); /* ??? */
280 SetDWLE( &p_stream->header.i_buffer_size, 30*1024 );
281 SetQWLE( &p_stream->header.i_samples_per_unit,
282 p_input->p_fmt->i_sample_rate );
283 SetWLE( &p_stream->header.i_bits_per_sample, 0 );
284 SetDWLE( &p_stream->header.header.audio.i_channels,
285 p_input->p_fmt->i_channels );
286 SetDWLE( &p_stream->header.header.audio.i_block_align,
287 p_input->p_fmt->i_block_align );
288 SetDWLE( &p_stream->header.header.audio.i_avgbytespersec, 0 );
291 case VLC_FOURCC( 'v', 'o', 'r', 'b' ):
292 msg_Dbg( p_mux, "vorbis stream" );
295 FREE( p_input->p_sys );
296 return( VLC_EGENERIC );
301 FREE( p_input->p_sys );
302 return( VLC_EGENERIC );
305 ogg_stream_init( &p_stream->os, rand () );
308 return( VLC_SUCCESS );
311 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
313 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
316 msg_Dbg( p_mux, "removing input" );
318 /* flush all remaining data */
322 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
325 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
327 sout_AccessOutWrite( p_mux->p_access, p_og );
330 ogg_stream_clear( &p_stream->os );
332 FREE( p_input->p_sys );
338 * TODO move this function to src/stream_output.c (used by nearly all muxers)
340 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
346 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
350 p_fifo = p_mux->pp_inputs[i]->p_fifo;
352 if( p_fifo->i_depth > 1 )
354 sout_buffer_t *p_buf;
356 p_buf = sout_FifoShow( p_fifo );
357 if( i_stream < 0 || p_buf->i_dts < i_dts )
359 i_dts = p_buf->i_dts;
365 return( -1 ); // wait that all fifo have at least 2 packets
370 *pi_stream = i_stream;
379 static sout_buffer_t *OggStreamFlush( sout_mux_t *p_mux,
380 ogg_stream_state *p_os, mtime_t i_pts )
382 sout_buffer_t *p_og, *p_og_first = NULL;
390 if( ( i_result = ogg_stream_flush( p_os, &og ) ) == 0 )
394 i_size = og.header_len + og.body_len;
395 p_og = sout_BufferNew( p_mux->p_sout, i_size);
397 memcpy( p_og->p_buffer, og.header, og.header_len );
398 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
399 p_og->i_size = i_size;
404 i_pts = 0; // write it only once
406 sout_BufferChain( &p_og_first, p_og );
409 return( p_og_first );
412 static sout_buffer_t *OggStreamPageOut( sout_mux_t *p_mux,
413 ogg_stream_state *p_os, mtime_t i_pts )
415 sout_buffer_t *p_og, *p_og_first = NULL;
423 if( ( i_result = ogg_stream_pageout( p_os, &og ) ) == 0 )
427 i_size = og.header_len + og.body_len;
428 p_og = sout_BufferNew( p_mux->p_sout, i_size);
430 memcpy( p_og->p_buffer, og.header, og.header_len );
431 memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
432 p_og->i_size = i_size;
437 i_pts = 0; // write them only once
439 sout_BufferChain( &p_og_first, p_og );
442 return( p_og_first );
445 static sout_buffer_t *OggCreateHeader( sout_mux_t *p_mux, mtime_t i_dts )
447 sout_buffer_t *p_hdr = NULL;
452 /* Write header for each stream. All b_o_s (beginning of stream) packets
453 * must appear first in the ogg stream so we take care of them first. */
454 for( i = 0; i < p_mux->i_nb_inputs; i++ )
456 ogg_stream_t *p_stream;
458 p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
460 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
462 /* Special case, headers are already there in the
465 /* first packet in order: vorbis info */
466 p_og = sout_FifoGet( p_mux->pp_inputs[i]->p_fifo );
467 op.packet = p_og->p_buffer;
468 op.bytes = p_og->i_size;
472 op.packetno = p_stream->i_packet_no++;
473 ogg_stream_packetin( &p_stream->os, &op );
478 op.packet = (uint8_t*)&p_stream->header;
479 op.bytes = sizeof( ogg_stream_t );
483 op.packetno = p_stream->i_packet_no++;
484 ogg_stream_packetin( &p_stream->os, &op );
487 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
488 sout_BufferChain( &p_hdr, p_og );
491 /* Take care of the non b_o_s headers */
492 for( i = 0; i < p_mux->i_nb_inputs; i++ )
494 ogg_stream_t *p_stream;
496 p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
498 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
500 /* Special case, headers are already there in the incoming stream.
501 * We need to gather them an mark them as headers. */
503 for( j = 0; j < 2; j++ )
505 /* next packets in order: comments and codebooks */
506 p_og = sout_FifoGet( p_mux->pp_inputs[i]->p_fifo );
507 op.packet = p_og->p_buffer;
508 op.bytes = p_og->i_size;
512 op.packetno = p_stream->i_packet_no++;
513 ogg_stream_packetin( &p_stream->os, &op );
522 com[0] = PACKET_TYPE_COMMENT;
523 i_com = snprintf( &com[1], 128, "VLC 0.5.x stream output" ) + 1;
529 op.packetno = p_stream->i_packet_no++;
530 ogg_stream_packetin( &p_stream->os, &op );
533 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
534 sout_BufferChain( &p_hdr, p_og );
537 /* set HEADER flag */
538 for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
540 p_og->i_flags |= SOUT_BUFFER_FLAGS_HEADER;
545 static void OggSetDate( sout_buffer_t *p_og, mtime_t i_dts, mtime_t i_length )
548 sout_buffer_t *p_tmp;
551 for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
555 i_delta = i_length / i_count;
557 for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
559 p_tmp->i_dts = i_dts;
560 p_tmp->i_length = i_delta;
566 static int Mux( sout_mux_t *p_mux )
568 sout_mux_sys_t *p_sys = p_mux->p_sys;
569 sout_buffer_t *p_og = NULL;
573 if( p_sys->b_write_header )
575 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
577 msg_Dbg( p_mux, "waiting data..." );
578 return( VLC_SUCCESS );
580 p_sys->i_start_dts = i_dts;
582 msg_Dbg( p_mux, "writing header" );
583 sout_BufferChain( &p_og, OggCreateHeader( p_mux, i_dts ) );
584 p_sys->b_write_header = VLC_FALSE;
589 sout_input_t *p_input;
590 ogg_stream_t *p_stream;
591 sout_buffer_t *p_data;
594 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
596 //msg_Dbg( p_mux, "waiting data..." );
597 return( VLC_SUCCESS );
599 //msg_Dbg( p_mux, "doing job" );
601 if( p_sys->i_start_dts <= 0 ) p_sys->i_start_dts = i_dts;
603 p_input = p_mux->pp_inputs[i_stream];
604 p_stream = (ogg_stream_t*)p_input->p_sys;
606 p_data = sout_FifoGet( p_input->p_fifo );
608 if( p_stream->i_fourcc != VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
610 sout_BufferReallocFromPreHeader( p_mux->p_sout, p_data, 1 );
611 p_data->p_buffer[0] = PACKET_IS_SYNCPOINT; // FIXME
614 op.packet = p_data->p_buffer;
615 op.bytes = p_data->i_size;
618 op.packetno = p_stream->i_packet_no++;
620 if( p_stream->i_cat == AUDIO_ES )
622 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
624 /* number of sample from begining + current packet */
626 ( i_dts + p_data->i_length - p_sys->i_start_dts ) *
627 p_input->p_fmt->i_sample_rate / (int64_t)1000000;
631 /* number of sample from begining */
632 op.granulepos = ( i_dts - p_sys->i_start_dts ) *
633 p_stream->header.i_samples_per_unit / (int64_t)1000000;
636 else if( p_stream->i_cat == VIDEO_ES )
638 op.granulepos = ( i_dts - p_sys->i_start_dts ) / 1000;
641 ogg_stream_packetin( &p_stream->os, &op );
643 sout_BufferChain( &p_og, OggStreamPageOut( p_mux, &p_stream->os,
648 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
649 p_stream->i_dts = -1;
650 p_stream->i_length = 0;
652 msg_Dbg( p_mux, "writing data..." );
653 sout_AccessOutWrite( p_mux->p_access, p_og );
659 if( p_stream->i_dts < 0 )
661 p_stream->i_dts = p_data->i_dts;
663 p_stream->i_length += p_data->i_length;
666 sout_BufferDelete( p_mux->p_sout, p_data );
669 return( VLC_SUCCESS );