1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: ogg.c,v 1.4 2003/03/31 03:46:11 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 BITMAPINFOHEADER *p_bih;
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->input_format.i_cat;
219 p_stream->i_fourcc = p_input->input_format.i_fourcc;
220 p_stream->i_packet_no = 0;
222 p_stream->header.i_packet_type = PACKET_TYPE_HEADER;
223 switch( p_input->input_format.i_cat )
226 p_bih = (BITMAPINFOHEADER*)p_input->input_format.p_format;
227 switch( p_input->input_format.i_fourcc )
229 case VLC_FOURCC( 'm', 'p','4', 'v' ):
230 case VLC_FOURCC( 'D', 'I','V', '3' ):
231 memcpy( p_stream->header.stream_type,
234 if( p_input->input_format.i_fourcc == VLC_FOURCC( 'm', 'p','4', 'v' ) )
236 memcpy( p_stream->header.sub_type, "XVID", 4 );
238 else if( p_input->input_format.i_fourcc == VLC_FOURCC( 'D', 'I','V', '3' ) )
240 memcpy( p_stream->header.sub_type, "DIV3", 4 );
242 SetDWLE( &p_stream->header.i_size, sizeof( ogg_stream_header_t ) - 1);
243 /* XXX this won't make mplayer happy, but vlc can read that without any problem so...*/
244 SetQWLE( &p_stream->header.i_time_unit, 10*1000 );//(int64_t)10*1000*1000/(int64_t)25 ); // FIXME (25fps)
245 SetQWLE( &p_stream->header.i_samples_per_unit, 1 );
246 SetDWLE( &p_stream->header.i_default_len, 0 ); /* ??? */
247 SetDWLE( &p_stream->header.i_buffer_size, 1024*1024 );
248 SetWLE( &p_stream->header.i_bits_per_sample, 0 );
251 SetDWLE( &p_stream->header.header.video.i_width, p_bih->biWidth );
252 SetDWLE( &p_stream->header.header.video.i_height, p_bih->biHeight );
256 SetDWLE( &p_stream->header.header.video.i_width, 0 );
257 SetDWLE( &p_stream->header.header.video.i_height, 0 );
261 FREE( p_input->p_sys );
262 return( VLC_EGENERIC );
266 p_wf = (WAVEFORMATEX*)p_input->input_format.p_format;
267 switch( p_input->input_format.i_fourcc )
269 case VLC_FOURCC( 'm', 'p','g', 'a' ):
270 case VLC_FOURCC( 'a', '5','2', ' ' ):
271 memcpy( p_stream->header.stream_type,
274 if( p_input->input_format.i_fourcc == VLC_FOURCC( 'm', 'p','g', 'a' ) )
276 memcpy( p_stream->header.sub_type, "55 ", 4 );
278 else if( p_input->input_format.i_fourcc == VLC_FOURCC( 'a', '5','2', ' ' ) )
280 memcpy( p_stream->header.sub_type, "2000", 4 );
282 SetDWLE( &p_stream->header.i_size, sizeof( ogg_stream_header_t ) - 1);
283 SetQWLE( &p_stream->header.i_time_unit, 1000000 ); /* is it used ? */
284 SetDWLE( &p_stream->header.i_default_len, 0 ); /* ??? */
285 SetDWLE( &p_stream->header.i_buffer_size, 30*1024 );
288 SetQWLE( &p_stream->header.i_samples_per_unit, p_wf->nSamplesPerSec );
289 SetWLE( &p_stream->header.i_bits_per_sample, p_wf->wBitsPerSample );
290 SetDWLE( &p_stream->header.header.audio.i_channels, p_wf->nChannels );
291 SetDWLE( &p_stream->header.header.audio.i_block_align, p_wf->nBlockAlign );
292 SetDWLE( &p_stream->header.header.audio.i_avgbytespersec, p_wf->nAvgBytesPerSec );
296 /* perhaps it's better to fail */
297 SetQWLE( &p_stream->header.i_samples_per_unit, 44100 );
298 SetWLE( &p_stream->header.i_bits_per_sample, 0 );
299 SetDWLE( &p_stream->header.header.audio.i_channels, 2 );
300 SetDWLE( &p_stream->header.header.audio.i_block_align, 0 );
301 SetDWLE( &p_stream->header.header.audio.i_avgbytespersec, 0 );
304 case VLC_FOURCC( 'v', 'o', 'r', 'b' ):
306 FREE( p_input->p_sys );
307 return( VLC_EGENERIC );
311 FREE( p_input->p_sys );
312 return( VLC_EGENERIC );
315 ogg_stream_init (&p_stream->os, rand ());
318 return( VLC_SUCCESS );
321 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
323 ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
326 msg_Dbg( p_mux, "removing input" );
328 /* flush all remaining data */
332 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
335 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
337 sout_AccessOutWrite( p_mux->p_access, p_og );
340 ogg_stream_clear( &p_stream->os );
342 FREE( p_input->p_sys );
348 * TODO move this function to src/stream_output.c (used by nearly all muxers)
350 static int MuxGetStream( sout_mux_t *p_mux,
358 for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
362 p_fifo = p_mux->pp_inputs[i]->p_fifo;
364 if( p_fifo->i_depth > 1 )
366 sout_buffer_t *p_buf;
368 p_buf = sout_FifoShow( p_fifo );
369 if( i_stream < 0 || p_buf->i_dts < i_dts )
371 i_dts = p_buf->i_dts;
377 return( -1 ); // wait that all fifo have at least 2 packets
382 *pi_stream = i_stream;
392 static sout_buffer_t *OggStreamFlush( sout_mux_t *p_mux,
393 ogg_stream_state *p_os,
396 sout_buffer_t *p_og, *p_og_first = NULL;
404 if( ( i_result = ogg_stream_flush( p_os, &og ) ) == 0 )
408 i_size = og.header_len + og.body_len;
409 p_og = sout_BufferNew( p_mux->p_sout, i_size);
411 memcpy( p_og->p_buffer,
414 memcpy( p_og->p_buffer + og.header_len,
417 p_og->i_size = i_size;
422 i_pts = 0; // write it only once
424 sout_BufferChain( &p_og_first, p_og );
427 return( p_og_first );
429 static sout_buffer_t *OggStreamPageOut( sout_mux_t *p_mux,
430 ogg_stream_state *p_os,
433 sout_buffer_t *p_og, *p_og_first = NULL;
441 if( ( i_result = ogg_stream_pageout( p_os, &og ) ) == 0 )
445 i_size = og.header_len + og.body_len;
446 p_og = sout_BufferNew( p_mux->p_sout, i_size);
448 memcpy( p_og->p_buffer,
451 memcpy( p_og->p_buffer + og.header_len,
454 p_og->i_size = i_size;
459 i_pts = 0; // write them only once
461 sout_BufferChain( &p_og_first, p_og );
464 return( p_og_first );
466 static sout_buffer_t *OggCreateHeader( sout_mux_t *p_mux, mtime_t i_dts )
468 sout_buffer_t *p_hdr = NULL;
473 /* write header for each stream */
474 for( i = 0; i < p_mux->i_nb_inputs; i++ )
476 ogg_stream_t *p_stream;
478 p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
480 if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
493 op.packet = (uint8_t*)&p_stream->header;
494 op.bytes = sizeof( ogg_stream_t );
498 op.packetno = p_stream->i_packet_no++;
499 ogg_stream_packetin( &p_stream->os, &op );
501 /* I don't know why but this produce broken file */
503 com[0] = PACKET_TYPE_COMMENT;
504 i_com = snprintf( &com[1], 128, "VLC 0.5.x stream output" ) + 1;
510 op.packetno = p_stream->i_packet_no++;
511 ogg_stream_packetin( &p_stream->os, &op );
515 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
516 sout_BufferChain( &p_hdr, p_og );
519 /* set HEADER flag */
520 for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
522 p_og->i_flags |= SOUT_BUFFER_FLAGS_HEADER;
528 static void OggSetDate( sout_buffer_t *p_og, mtime_t i_dts, mtime_t i_length )
531 sout_buffer_t *p_tmp;
534 for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
538 i_delta = i_length / i_count;
540 for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
542 p_tmp->i_dts = i_dts;
543 p_tmp->i_length = i_delta;
549 static int Mux ( sout_mux_t *p_mux )
551 sout_mux_sys_t *p_sys = p_mux->p_sys;
552 sout_buffer_t *p_og = NULL;
556 if( p_sys->b_write_header )
558 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
560 msg_Dbg( p_mux, "waiting data..." );
561 return( VLC_SUCCESS );
563 p_sys->i_start_dts = i_dts;
565 msg_Dbg( p_mux, "writing header" );
566 sout_BufferChain( &p_og, OggCreateHeader( p_mux, i_dts ) );
567 p_sys->b_write_header = VLC_FALSE;
572 sout_input_t *p_input;
573 ogg_stream_t *p_stream;
574 sout_buffer_t *p_data;
577 if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
579 //msg_Dbg( p_mux, "waiting data..." );
580 return( VLC_SUCCESS );
582 //msg_Dbg( p_mux, "doing job" );
584 p_input = p_mux->pp_inputs[i_stream];
585 p_stream = (ogg_stream_t*)p_input->p_sys;
587 p_data = sout_FifoGet( p_input->p_fifo );
589 sout_BufferReallocFromPreHeader( p_mux->p_sout, p_data, 1 );
590 p_data->p_buffer[0] = PACKET_IS_SYNCPOINT; // FIXME
592 op.packet = p_data->p_buffer;
593 op.bytes = p_data->i_size;
596 if( p_stream->i_cat == AUDIO_ES )
598 /* number of sample from begining */
599 op.granulepos = ( i_dts - p_sys->i_start_dts ) *
600 p_stream->header.i_samples_per_unit / (int64_t)1000000;
602 else if( p_stream->i_cat == VIDEO_ES )
604 op.granulepos = ( i_dts - p_sys->i_start_dts ) / 1000;//p_stream->i_packet_no;
606 op.packetno = p_stream->i_packet_no++;
607 ogg_stream_packetin( &p_stream->os, &op );
609 sout_BufferChain( &p_og,
610 OggStreamPageOut( p_mux,
616 OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
617 p_stream->i_dts = -1;
618 p_stream->i_length = 0;
620 msg_Dbg( p_mux, "writing data..." );
621 sout_AccessOutWrite( p_mux->p_access, p_og );
627 if( p_stream->i_dts < 0 )
629 p_stream->i_dts = p_data->i_dts;
631 p_stream->i_length += p_data->i_length;
634 sout_BufferDelete( p_mux->p_sout, p_data );
637 return( VLC_SUCCESS );