]> git.sesse.net Git - vlc/blob - modules/mux/ogg.c
* configure.ac: Added a compiler check for __attribute__((__packed__)).
[vlc] / modules / mux / ogg.c
1 /*****************************************************************************
2  * ogg.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: ogg.c,v 1.8 2003/07/01 17:14:58 sam Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@netcourrier.com>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/types.h>
31
32 #include <vlc/vlc.h>
33 #include <vlc/input.h>
34 #include <vlc/sout.h>
35
36 #include "codecs.h"
37
38 #include <ogg/ogg.h>
39
40 /*****************************************************************************
41  * Exported prototypes
42  *****************************************************************************/
43 static int     Open   ( vlc_object_t * );
44 static void    Close  ( vlc_object_t * );
45
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 * );
50
51 /*****************************************************************************
52  * Module descriptor
53  *****************************************************************************/
54 vlc_module_begin();
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 );
60 vlc_module_end();
61
62 /*****************************************************************************
63  *
64  *****************************************************************************/
65 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
66
67 #define PACKET_TYPE_HEADER   0x01
68 #define PACKET_TYPE_COMMENT  0x03
69
70 #define PACKET_IS_SYNCPOINT      0x08
71
72 typedef struct
73 #ifdef HAVE_ATTRIBUTE_PACKED
74     __attribute__((__packed__))
75 #endif
76 {
77     int32_t i_width;
78     int32_t i_height;
79 } ogg_stream_header_video_t;
80
81 typedef struct
82 #ifdef HAVE_ATTRIBUTE_PACKED
83     __attribute__((__packed__))
84 #endif
85 {
86     int16_t i_channels;
87     int16_t i_block_align;
88     int32_t i_avgbytespersec;
89 } ogg_stream_header_audio_t;
90
91 typedef struct
92 #ifdef HAVE_ATTRIBUTE_PACKED
93     __attribute__((__packed__))
94 #endif
95 {
96     uint8_t i_packet_type;
97
98     char stream_type[8];
99     char sub_type[4];
100
101     int32_t i_size;
102
103     int64_t i_time_unit;
104     int64_t i_samples_per_unit;
105     int32_t i_default_len;
106
107     int32_t i_buffer_size;
108     int16_t i_bits_per_sample;
109     int16_t i_padding_0;            // hum hum
110     union
111     {
112         ogg_stream_header_video_t video;
113         ogg_stream_header_audio_t audio;
114     } header;
115
116 } ogg_stream_header_t;
117
118
119 typedef struct
120 {
121     int i_cat;
122     int i_fourcc;
123
124     ogg_stream_header_t header;
125
126     int i_packet_no;
127
128     mtime_t             i_dts;
129     mtime_t             i_length;
130     ogg_stream_state    os;
131
132 } ogg_stream_t;
133
134 struct sout_mux_sys_t
135 {
136     int     b_write_header;
137     int     i_streams;
138
139     mtime_t i_start_dts;
140 };
141
142 #define SetWLE( p, v ) _SetWLE( (uint8_t*)p, v)
143 static void _SetWLE( uint8_t *p, uint16_t i_dw )
144 {
145     p[1] = ( i_dw >>  8 )&0xff;
146     p[0] = ( i_dw       )&0xff;
147 }
148
149 #define SetDWLE( p, v ) _SetDWLE( (uint8_t*)p, v)
150 static void _SetDWLE( uint8_t *p, uint32_t i_dw )
151 {
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;
156 }
157 #define SetQWLE( p, v ) _SetQWLE( (uint8_t*)p, v)
158 static void _SetQWLE( uint8_t *p, uint64_t i_qw )
159 {
160     SetDWLE( p,   i_qw&0xffffffff );
161     SetDWLE( p+4, ( i_qw >> 32)&0xffffffff );
162 }
163
164 static void OggSetDate( sout_buffer_t *, mtime_t , mtime_t  );
165 static sout_buffer_t *OggStreamFlush( sout_mux_t *, ogg_stream_state *,
166                                       mtime_t );
167
168 /*****************************************************************************
169  * Open:
170  *****************************************************************************/
171 static int Open( vlc_object_t *p_this )
172 {
173     sout_mux_t      *p_mux = (sout_mux_t*)p_this;
174     sout_mux_sys_t  *p_sys;
175
176     msg_Info( p_mux, "Open" );
177
178     p_sys                 = malloc( sizeof( sout_mux_sys_t ) );
179     p_sys->i_streams      = 0;
180     p_sys->b_write_header = VLC_TRUE;
181
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;
186     p_mux->pf_mux       = Mux;
187     p_mux->i_preheader  = 1;
188
189     return VLC_SUCCESS;
190 }
191
192 /*****************************************************************************
193  * Close:
194  *****************************************************************************/
195
196 static void Close( vlc_object_t * p_this )
197 {
198     sout_mux_t      *p_mux = (sout_mux_t*)p_this;
199     sout_mux_sys_t  *p_sys = p_mux->p_sys;
200
201     msg_Info( p_mux, "Close" );
202
203     free( p_sys );
204 }
205
206 static int Capability( sout_mux_t *p_mux, int i_query, void *p_args,
207                        void *p_answer )
208 {
209    switch( i_query )
210    {
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 );
214         default:
215             return( SOUT_MUX_CAP_ERR_UNIMPLEMENTED );
216    }
217 }
218
219 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
220 {
221     sout_mux_sys_t  *p_sys = p_mux->p_sys;
222     ogg_stream_t        *p_stream;
223
224     msg_Dbg( p_mux, "adding input" );
225     p_input->p_sys = (void*)p_stream = malloc( sizeof( ogg_stream_t ) );
226
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;
230
231     p_stream->header.i_packet_type = PACKET_TYPE_HEADER;
232     switch( p_input->p_fmt->i_cat )
233     {
234     case VIDEO_ES:
235         switch( p_input->p_fmt->i_fourcc )
236         {
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' ) )
241             {
242                 memcpy( p_stream->header.sub_type, "XVID", 4 );
243             }
244             else if( p_input->p_fmt->i_fourcc ==
245                      VLC_FOURCC( 'D', 'I','V', '3' ) )
246             {
247                 memcpy( p_stream->header.sub_type, "DIV3", 4 );
248             }
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 );
263             break;
264
265         default:
266             FREE( p_input->p_sys );
267             return( VLC_EGENERIC );
268         }
269         break;
270     case AUDIO_ES:
271         switch( p_input->p_fmt->i_fourcc )
272         {
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' ) )
277             {
278                 memcpy( p_stream->header.sub_type, "55  ", 4 );
279             }
280             else if( p_input->p_fmt->i_fourcc ==
281                      VLC_FOURCC( 'a', '5','2', ' ' ) )
282             {
283                 memcpy( p_stream->header.sub_type, "2000", 4 );
284             }
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 );
298             break;
299
300         case VLC_FOURCC( 'v', 'o', 'r', 'b' ):
301           msg_Dbg( p_mux, "vorbis stream" );
302           break;
303         default:
304             FREE( p_input->p_sys );
305             return( VLC_EGENERIC );
306         }
307         break;
308
309     default:
310         FREE( p_input->p_sys );
311         return( VLC_EGENERIC );
312     }
313
314     ogg_stream_init( &p_stream->os, rand () );
315
316     p_sys->i_streams++;
317     return( VLC_SUCCESS );
318 }
319
320 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
321 {
322     ogg_stream_t        *p_stream = (ogg_stream_t*)p_input->p_sys;
323     sout_buffer_t       *p_og;
324
325     msg_Dbg( p_mux, "removing input" );
326
327     /* flush all remaining data */
328
329     if( p_input->p_sys )
330     {
331         p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
332         if( p_og )
333         {
334             OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
335
336             sout_AccessOutWrite( p_mux->p_access, p_og );
337         }
338
339         ogg_stream_clear( &p_stream->os );
340
341         FREE( p_input->p_sys );
342     }
343     return( 0 );
344 }
345
346 /*
347  * TODO  move this function to src/stream_output.c (used by nearly all muxers)
348  */
349 static int MuxGetStream( sout_mux_t *p_mux, int *pi_stream, mtime_t *pi_dts )
350 {
351     mtime_t i_dts;
352     int     i_stream;
353     int     i;
354
355     for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
356     {
357         sout_fifo_t  *p_fifo;
358
359         p_fifo = p_mux->pp_inputs[i]->p_fifo;
360
361         if( p_fifo->i_depth > 1 )
362         {
363             sout_buffer_t *p_buf;
364
365             p_buf = sout_FifoShow( p_fifo );
366             if( i_stream < 0 || p_buf->i_dts < i_dts )
367             {
368                 i_dts = p_buf->i_dts;
369                 i_stream = i;
370             }
371         }
372         else
373         {
374             return( -1 ); // wait that all fifo have at least 2 packets
375         }
376     }
377     if( pi_stream )
378     {
379         *pi_stream = i_stream;
380     }
381     if( pi_dts )
382     {
383         *pi_dts = i_dts;
384     }
385     return( i_stream );
386 }
387
388 static sout_buffer_t *OggStreamFlush( sout_mux_t *p_mux,
389                                       ogg_stream_state *p_os, mtime_t i_pts )
390 {
391     sout_buffer_t *p_og, *p_og_first = NULL;
392     ogg_page      og;
393
394     for( ;; )
395     {
396         /* flush all data */
397         int i_result;
398         int i_size;
399         if( ( i_result = ogg_stream_flush( p_os, &og ) ) == 0 )
400         {
401             break;
402         }
403         i_size = og.header_len + og.body_len;
404         p_og = sout_BufferNew( p_mux->p_sout, i_size);
405
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;
409         p_og->i_dts     = 0;
410         p_og->i_pts     = i_pts;
411         p_og->i_length  = 0;
412
413         i_pts   = 0; // write it only once
414
415         sout_BufferChain( &p_og_first, p_og );
416     }
417
418     return( p_og_first );
419 }
420
421 static sout_buffer_t *OggStreamPageOut( sout_mux_t *p_mux,
422                                         ogg_stream_state *p_os, mtime_t i_pts )
423 {
424     sout_buffer_t *p_og, *p_og_first = NULL;
425     ogg_page      og;
426
427     for( ;; )
428     {
429         /* flush all data */
430         int i_result;
431         int i_size;
432         if( ( i_result = ogg_stream_pageout( p_os, &og ) ) == 0 )
433         {
434             break;
435         }
436         i_size = og.header_len + og.body_len;
437         p_og = sout_BufferNew( p_mux->p_sout, i_size);
438
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;
442         p_og->i_dts     = 0;
443         p_og->i_pts     = i_pts;
444         p_og->i_length  = 0;
445
446         i_pts   = 0; // write them only once
447
448         sout_BufferChain( &p_og_first, p_og );
449     }
450
451     return( p_og_first );
452 }
453
454 static sout_buffer_t *OggCreateHeader( sout_mux_t *p_mux, mtime_t i_dts )
455 {
456     sout_buffer_t *p_hdr = NULL;
457     sout_buffer_t *p_og;
458     ogg_packet    op;
459     int i;
460
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++ )
464     {
465         ogg_stream_t *p_stream;
466
467         p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
468
469         if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
470         {
471             /* Special case, headers are already there in the
472              * incoming stream */
473
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;
478             op.b_o_s  = 1;
479             op.e_o_s  = 0;
480             op.granulepos = 0;
481             op.packetno = p_stream->i_packet_no++;
482             ogg_stream_packetin( &p_stream->os, &op );
483         }
484         else
485         {
486             /* ds header */
487             op.packet = (uint8_t*)&p_stream->header;
488             op.bytes  = sizeof( ogg_stream_t );
489             op.b_o_s  = 1;
490             op.e_o_s  = 0;
491             op.granulepos = 0;
492             op.packetno = p_stream->i_packet_no++;
493             ogg_stream_packetin( &p_stream->os, &op );
494         }
495
496         p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
497         sout_BufferChain( &p_hdr, p_og );
498     }
499
500     /* Take care of the non b_o_s headers */
501     for( i = 0; i < p_mux->i_nb_inputs; i++ )
502     {
503         ogg_stream_t *p_stream;
504
505         p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
506
507         if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
508         {
509             /* Special case, headers are already there in the incoming stream.
510              * We need to gather them an mark them as headers. */
511             int j;
512             for( j = 0; j < 2; j++ )
513             {
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;
518                 op.b_o_s  = 0;
519                 op.e_o_s  = 0;
520                 op.granulepos = 0;
521                 op.packetno = p_stream->i_packet_no++;
522                 ogg_stream_packetin( &p_stream->os, &op );
523             }
524         }
525         else
526         {
527             uint8_t com[128];
528             int     i_com;
529
530             /* comment */
531             com[0] = PACKET_TYPE_COMMENT;
532             i_com = snprintf( &com[1], 128, "VLC 0.5.x stream output" ) + 1;
533             op.packet = com;
534             op.bytes  = i_com;
535             op.b_o_s  = 0;
536             op.e_o_s  = 0;
537             op.granulepos = 0;
538             op.packetno = p_stream->i_packet_no++;
539             ogg_stream_packetin( &p_stream->os, &op );
540         }
541
542         p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
543         sout_BufferChain( &p_hdr, p_og );
544     }
545
546     /* set HEADER flag */
547     for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
548     {
549         p_og->i_flags |= SOUT_BUFFER_FLAGS_HEADER;
550     }
551     return( p_hdr );
552 }
553
554 static void OggSetDate( sout_buffer_t *p_og, mtime_t i_dts, mtime_t i_length )
555 {
556     int i_count;
557     sout_buffer_t *p_tmp;
558     mtime_t i_delta;
559
560     for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
561     {
562         i_count++;
563     }
564     i_delta = i_length / i_count;
565
566     for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
567     {
568         p_tmp->i_dts    = i_dts;
569         p_tmp->i_length = i_delta;
570
571         i_dts += i_delta;
572     }
573 }
574
575 static int Mux( sout_mux_t *p_mux )
576 {
577     sout_mux_sys_t      *p_sys  = p_mux->p_sys;
578     sout_buffer_t       *p_og = NULL;
579     int                 i_stream;
580     mtime_t             i_dts;
581
582     if( p_sys->b_write_header )
583     {
584         if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
585         {
586             msg_Dbg( p_mux, "waiting data..." );
587             return( VLC_SUCCESS );
588         }
589         p_sys->i_start_dts = i_dts;
590
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;
594     }
595
596     for( ;; )
597     {
598         sout_input_t    *p_input;
599         ogg_stream_t    *p_stream;
600         sout_buffer_t   *p_data;
601         ogg_packet          op;
602
603         if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
604         {
605             //msg_Dbg( p_mux, "waiting data..." );
606             return( VLC_SUCCESS );
607         }
608         //msg_Dbg( p_mux, "doing job" );
609
610         if( p_sys->i_start_dts <= 0 ) p_sys->i_start_dts = i_dts;
611
612         p_input  = p_mux->pp_inputs[i_stream];
613         p_stream = (ogg_stream_t*)p_input->p_sys;
614
615         p_data  = sout_FifoGet( p_input->p_fifo );
616
617         if( p_stream->i_fourcc != VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
618         {
619             sout_BufferReallocFromPreHeader( p_mux->p_sout, p_data, 1 );
620             p_data->p_buffer[0] = PACKET_IS_SYNCPOINT;      // FIXME
621         }
622
623         op.packet   = p_data->p_buffer;
624         op.bytes    = p_data->i_size;
625         op.b_o_s    = 0;
626         op.e_o_s    = 0;
627         op.packetno = p_stream->i_packet_no++;
628
629         if( p_stream->i_cat == AUDIO_ES )
630         {
631             if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
632             {
633                 /* number of sample from begining + current packet */
634                 op.granulepos =
635                     ( i_dts + p_data->i_length - p_sys->i_start_dts ) *
636                     p_input->p_fmt->i_sample_rate / (int64_t)1000000;
637             }
638             else
639             {
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;
643             }
644         }
645         else if( p_stream->i_cat == VIDEO_ES )
646         {
647             op.granulepos = ( i_dts - p_sys->i_start_dts ) / 1000;
648         }
649
650         ogg_stream_packetin( &p_stream->os, &op );
651
652         sout_BufferChain( &p_og, OggStreamPageOut( p_mux, &p_stream->os,
653                                                    p_data->i_dts ) );
654
655         if( p_og )
656         {
657             OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
658             p_stream->i_dts = -1;
659             p_stream->i_length = 0;
660
661             sout_AccessOutWrite( p_mux->p_access, p_og );
662
663             p_og = NULL;
664         }
665         else
666         {
667             if( p_stream->i_dts < 0 )
668             {
669                 p_stream->i_dts = p_data->i_dts;
670             }
671             p_stream->i_length += p_data->i_length;
672         }
673
674         sout_BufferDelete( p_mux->p_sout, p_data );
675     }
676
677     return( VLC_SUCCESS );
678 }