1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: ogg.c,v 1.2 2003/02/25 17:17:43 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_instance_t *, int, void *, void * );
46 static int AddStream( sout_instance_t *, sout_input_t * );
47 static int DelStream( sout_instance_t *, sout_input_t * );
48 static int Mux ( sout_instance_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 typedef struct sout_mux_s
131 #define SetWLE( p, v ) _SetWLE( (uint8_t*)p, v)
132 static void _SetWLE( uint8_t *p, uint16_t i_dw )
134 p[1] = ( i_dw >> 8 )&0xff;
135 p[0] = ( i_dw )&0xff;
138 #define SetDWLE( p, v ) _SetDWLE( (uint8_t*)p, v)
139 static void _SetDWLE( uint8_t *p, uint32_t i_dw )
141 p[3] = ( i_dw >> 24 )&0xff;
142 p[2] = ( i_dw >> 16 )&0xff;
143 p[1] = ( i_dw >> 8 )&0xff;
144 p[0] = ( i_dw )&0xff;
146 #define SetQWLE( p, v ) _SetQWLE( (uint8_t*)p, v)
147 static void _SetQWLE( uint8_t *p, uint64_t i_qw )
149 SetDWLE( p, i_qw&0xffffffff );
150 SetDWLE( p+4, ( i_qw >> 32)&0xffffffff );
153 static void OggSetDate( sout_buffer_t *, mtime_t , mtime_t );
154 static sout_buffer_t *OggStreamFlush( sout_instance_t *, ogg_stream_state *, mtime_t );
156 /*****************************************************************************
158 *****************************************************************************/
159 static int Open( vlc_object_t *p_this )
161 sout_instance_t *p_sout = (sout_instance_t*)p_this;
164 msg_Info( p_sout, "Open" );
166 p_mux = malloc( sizeof( sout_mux_t ) );
167 p_mux->i_streams = 0;
168 p_mux->b_write_header = VLC_TRUE;
170 p_sout->p_mux_data = (void*)p_mux;
171 p_sout->pf_mux_capacity = Capability;
172 p_sout->pf_mux_addstream = AddStream;
173 p_sout->pf_mux_delstream = DelStream;
174 p_sout->pf_mux = Mux;
175 p_sout->i_mux_preheader = 1;
180 /*****************************************************************************
182 *****************************************************************************/
184 static void Close( vlc_object_t * p_this )
186 sout_instance_t *p_sout = (sout_instance_t*)p_this;
187 sout_mux_t *p_mux = (sout_mux_t*)p_sout->p_mux_data;
189 msg_Info( p_sout, "Close" );
194 static int Capability( sout_instance_t *p_sout, int i_query, void *p_args, void *p_answer )
198 case SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME:
199 *(vlc_bool_t*)p_answer = VLC_FALSE;
200 return( SOUT_MUX_CAP_ERR_OK );
202 return( SOUT_MUX_CAP_ERR_UNIMPLEMENTED );
206 static int AddStream( sout_instance_t *p_sout, sout_input_t *p_input )
208 sout_mux_t *p_mux = (sout_mux_t*)p_sout->p_mux_data;
209 ogg_stream_t *p_stream;
211 BITMAPINFOHEADER *p_bih;
214 msg_Dbg( p_sout, "adding input" );
215 p_input->p_mux_data = (void*)p_stream = malloc( sizeof( ogg_stream_t ) );
217 p_stream->i_cat = p_input->input_format.i_cat;
218 p_stream->i_fourcc = p_input->input_format.i_fourcc;
219 p_stream->i_packet_no = 0;
221 p_stream->header.i_packet_type = PACKET_TYPE_HEADER;
222 switch( p_input->input_format.i_cat )
225 p_bih = (BITMAPINFOHEADER*)p_input->input_format.p_format;
226 switch( p_input->input_format.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,
233 if( p_input->input_format.i_fourcc == VLC_FOURCC( 'm', 'p','4', 'v' ) )
235 memcpy( p_stream->header.sub_type, "XVID", 4 );
237 else if( p_input->input_format.i_fourcc == VLC_FOURCC( 'D', 'I','V', '3' ) )
239 memcpy( p_stream->header.sub_type, "DIV3", 4 );
241 SetDWLE( &p_stream->header.i_size, sizeof( ogg_stream_header_t ) - 1);
242 /* XXX this won't make mplayer happy, but vlc can read that without any problem so...*/
243 SetQWLE( &p_stream->header.i_time_unit, 10*1000 );//(int64_t)10*1000*1000/(int64_t)25 ); // FIXME (25fps)
244 SetQWLE( &p_stream->header.i_samples_per_unit, 1 );
245 SetDWLE( &p_stream->header.i_default_len, 0 ); /* ??? */
246 SetDWLE( &p_stream->header.i_buffer_size, 1024*1024 );
247 SetWLE( &p_stream->header.i_bits_per_sample, 0 );
250 SetDWLE( &p_stream->header.header.video.i_width, p_bih->biWidth );
251 SetDWLE( &p_stream->header.header.video.i_height, p_bih->biHeight );
255 SetDWLE( &p_stream->header.header.video.i_width, 0 );
256 SetDWLE( &p_stream->header.header.video.i_height, 0 );
260 FREE( p_input->p_mux_data );
261 return( VLC_EGENERIC );
265 p_wf = (WAVEFORMATEX*)p_input->input_format.p_format;
266 switch( p_input->input_format.i_fourcc )
268 case VLC_FOURCC( 'm', 'p','g', 'a' ):
269 case VLC_FOURCC( 'a', '5','2', ' ' ):
270 memcpy( p_stream->header.stream_type,
273 if( p_input->input_format.i_fourcc == VLC_FOURCC( 'm', 'p','g', 'a' ) )
275 memcpy( p_stream->header.sub_type, "55 ", 4 );
277 else if( p_input->input_format.i_fourcc == VLC_FOURCC( 'a', '5','2', ' ' ) )
279 memcpy( p_stream->header.sub_type, "2000", 4 );
281 SetDWLE( &p_stream->header.i_size, sizeof( ogg_stream_header_t ) - 1);
282 SetQWLE( &p_stream->header.i_time_unit, 1000000 ); /* is it used ? */
283 SetDWLE( &p_stream->header.i_default_len, 0 ); /* ??? */
284 SetDWLE( &p_stream->header.i_buffer_size, 30*1024 );
287 SetQWLE( &p_stream->header.i_samples_per_unit, p_wf->nSamplesPerSec );
288 SetWLE( &p_stream->header.i_bits_per_sample, p_wf->wBitsPerSample );
289 SetDWLE( &p_stream->header.header.audio.i_channels, p_wf->nChannels );
290 SetDWLE( &p_stream->header.header.audio.i_block_align, p_wf->nBlockAlign );
291 SetDWLE( &p_stream->header.header.audio.i_avgbytespersec, p_wf->nAvgBytesPerSec );
295 /* perhaps it's better to fail */
296 SetQWLE( &p_stream->header.i_samples_per_unit, 44100 );
297 SetWLE( &p_stream->header.i_bits_per_sample, 0 );
298 SetDWLE( &p_stream->header.header.audio.i_channels, 2 );
299 SetDWLE( &p_stream->header.header.audio.i_block_align, 0 );
300 SetDWLE( &p_stream->header.header.audio.i_avgbytespersec, 0 );
303 case VLC_FOURCC( 'v', 'o', 'r', 'b' ):
305 FREE( p_input->p_mux_data );
306 return( VLC_EGENERIC );
310 FREE( p_input->p_mux_data );
311 return( VLC_EGENERIC );
314 ogg_stream_init (&p_stream->os, rand ());
317 return( VLC_SUCCESS );
320 static int DelStream( sout_instance_t *p_sout, sout_input_t *p_input )
322 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_mux_data;
325 msg_Dbg( p_sout, "removing input" );
327 /* flush all remaining data */
329 p_og = OggStreamFlush( p_sout, &p_stream->os, 0 );
332 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
334 sout_AccessOutWrite( p_sout->p_access, p_og );
337 ogg_stream_clear( &p_stream->os );
339 FREE( p_input->p_mux_data );
344 * TODO move this function to src/stream_output.c (used by nearly all muxers)
346 static int MuxGetStream( sout_instance_t *p_sout,
354 for( i = 0, i_dts = 0, i_stream = -1; i < p_sout->i_nb_inputs; i++ )
358 p_fifo = p_sout->pp_inputs[i]->p_fifo;
360 if( p_fifo->i_depth > 1 )
362 sout_buffer_t *p_buf;
364 p_buf = sout_FifoShow( p_fifo );
365 if( i_stream < 0 || p_buf->i_dts < i_dts )
367 i_dts = p_buf->i_dts;
373 return( -1 ); // wait that all fifo have at least 2 packets
378 *pi_stream = i_stream;
388 static sout_buffer_t *OggStreamFlush( sout_instance_t *p_sout,
389 ogg_stream_state *p_os,
392 sout_buffer_t *p_og, *p_og_first = NULL;
400 if( ( i_result = ogg_stream_flush( p_os, &og ) ) == 0 )
404 i_size = og.header_len + og.body_len;
405 p_og = sout_BufferNew( p_sout, i_size);
407 memcpy( p_og->p_buffer,
410 memcpy( p_og->p_buffer + og.header_len,
413 p_og->i_size = i_size;
418 i_pts = 0; // write it only once
420 sout_BufferChain( &p_og_first, p_og );
423 return( p_og_first );
425 static sout_buffer_t *OggStreamPageOut( sout_instance_t *p_sout,
426 ogg_stream_state *p_os,
429 sout_buffer_t *p_og, *p_og_first = NULL;
437 if( ( i_result = ogg_stream_pageout( p_os, &og ) ) == 0 )
441 i_size = og.header_len + og.body_len;
442 p_og = sout_BufferNew( p_sout, i_size);
444 memcpy( p_og->p_buffer,
447 memcpy( p_og->p_buffer + og.header_len,
450 p_og->i_size = i_size;
455 i_pts = 0; // write them only once
457 sout_BufferChain( &p_og_first, p_og );
460 return( p_og_first );
462 static sout_buffer_t *OggCreateHeader( sout_instance_t *p_sout, mtime_t i_dts )
464 sout_buffer_t *p_hdr = NULL;
469 /* write header for each stream */
470 for( i = 0; i < p_sout->i_nb_inputs; i++ )
472 ogg_stream_t *p_stream;
474 p_stream = (ogg_stream_t*)p_sout->pp_inputs[i]->p_mux_data;
476 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
489 op.packet = (uint8_t*)&p_stream->header;
490 op.bytes = sizeof( ogg_stream_t );
494 op.packetno = p_stream->i_packet_no++;
495 ogg_stream_packetin( &p_stream->os, &op );
497 /* I don't know why but this produce broken file */
499 com[0] = PACKET_TYPE_COMMENT;
500 i_com = snprintf( &com[1], 128, "VLC 0.5.x stream output" ) + 1;
506 op.packetno = p_stream->i_packet_no++;
507 ogg_stream_packetin( &p_stream->os, &op );
511 p_og = OggStreamFlush( p_sout, &p_stream->os, 0 );
512 sout_BufferChain( &p_hdr, p_og );
515 /* set HEADER flag */
516 for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
518 p_og->i_flags |= SOUT_BUFFER_FLAGS_HEADER;
524 static void OggSetDate( sout_buffer_t *p_og, mtime_t i_dts, mtime_t i_length )
527 sout_buffer_t *p_tmp;
530 for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
534 i_delta = i_length / i_count;
536 for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
538 p_tmp->i_dts = i_dts;
539 p_tmp->i_length = i_delta;
545 static int Mux ( sout_instance_t *p_sout )
547 sout_mux_t *p_mux = (sout_mux_t*)p_sout->p_mux_data;
548 sout_buffer_t *p_og = NULL;
552 if( p_mux->b_write_header )
554 if( MuxGetStream( p_sout, &i_stream, &i_dts) < 0 )
556 return( VLC_SUCCESS );
558 sout_BufferChain( &p_og, OggCreateHeader( p_sout, i_dts ) );
559 p_mux->b_write_header = VLC_FALSE;
564 sout_input_t *p_input;
565 ogg_stream_t *p_stream;
566 sout_buffer_t *p_data;
569 if( MuxGetStream( p_sout, &i_stream, &i_dts) < 0 )
571 return( VLC_SUCCESS );
574 p_input = p_sout->pp_inputs[i_stream];
575 p_stream = (ogg_stream_t*)p_input->p_mux_data;
577 p_data = sout_FifoGet( p_input->p_fifo );
579 sout_BufferReallocFromPreHeader( p_sout, p_data, 1 );
580 p_data->p_buffer[0] = PACKET_IS_SYNCPOINT; // FIXME
582 op.packet = p_data->p_buffer;
583 op.bytes = p_data->i_size;
586 if( p_stream->i_cat == AUDIO_ES )
588 /* number of sample from begining */
589 op.granulepos = i_dts * p_stream->header.i_samples_per_unit / (int64_t)1000000;
591 else if( p_stream->i_cat == VIDEO_ES )
593 op.granulepos = i_dts/1000;//p_stream->i_packet_no;
595 op.packetno = p_stream->i_packet_no++;
596 ogg_stream_packetin( &p_stream->os, &op );
598 sout_BufferChain( &p_og,
599 OggStreamPageOut( p_sout,
605 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
606 p_stream->i_dts = -1;
607 p_stream->i_length = 0;;
608 sout_AccessOutWrite( p_sout->p_access, p_og );
614 if( p_stream->i_dts < 0 )
616 p_stream->i_dts = p_data->i_dts;
618 p_stream->i_length += p_data->i_length;
621 sout_BufferDelete( p_sout, p_data );
624 return( VLC_SUCCESS );