]> git.sesse.net Git - vlc/blob - modules/mux/ogg.c
I422_YUY2: clobber lists for MMX and SSE2
[vlc] / modules / mux / ogg.c
1 /*****************************************************************************
2  * ogg.c: ogg muxer module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001, 2002, 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_sout.h>
36 #include <vlc_block.h>
37 #include <vlc_codecs.h>
38 #include <limits.h>
39 #include <vlc_rand.h>
40 #include "../demux/xiph.h"
41
42 #include <ogg/ogg.h>
43
44 /*****************************************************************************
45  * Module descriptor
46  *****************************************************************************/
47 static int  Open   ( vlc_object_t * );
48 static void Close  ( vlc_object_t * );
49
50 vlc_module_begin ()
51     set_description( N_("Ogg/OGM muxer") )
52     set_capability( "sout mux", 10 )
53     set_category( CAT_SOUT )
54     set_subcategory( SUBCAT_SOUT_MUX )
55     add_shortcut( "ogg", "ogm" )
56     set_callbacks( Open, Close )
57 vlc_module_end ()
58
59
60 /*****************************************************************************
61  * Exported prototypes
62  *****************************************************************************/
63 static int Control  ( sout_mux_t *, int, va_list );
64 static int AddStream( sout_mux_t *, sout_input_t * );
65 static int DelStream( sout_mux_t *, sout_input_t * );
66 static int Mux      ( sout_mux_t * );
67 static int MuxBlock ( sout_mux_t *, sout_input_t * );
68
69 static block_t *OggCreateHeader( sout_mux_t * );
70 static block_t *OggCreateFooter( sout_mux_t * );
71
72 /*****************************************************************************
73  * Misc declarations
74  *****************************************************************************/
75
76 /* Structures used for OggDS headers used in ogm files */
77
78 #define PACKET_TYPE_HEADER   0x01
79 #define PACKET_TYPE_COMMENT  0x03
80 #define PACKET_IS_SYNCPOINT  0x08
81
82 typedef struct
83 #ifdef HAVE_ATTRIBUTE_PACKED
84     __attribute__((__packed__))
85 #endif
86 {
87     int32_t i_width;
88     int32_t i_height;
89 } oggds_header_video_t;
90
91 typedef struct
92 #ifdef HAVE_ATTRIBUTE_PACKED
93     __attribute__((__packed__))
94 #endif
95 {
96     int16_t i_channels;
97     int16_t i_block_align;
98     int32_t i_avgbytespersec;
99 } oggds_header_audio_t;
100
101 typedef struct
102 #ifdef HAVE_ATTRIBUTE_PACKED
103     __attribute__((__packed__))
104 #endif
105 {
106     uint8_t i_packet_type;
107
108     char stream_type[8];
109     char sub_type[4];
110
111     int32_t i_size;
112
113     int64_t i_time_unit;
114     int64_t i_samples_per_unit;
115     int32_t i_default_len;
116
117     int32_t i_buffer_size;
118     int16_t i_bits_per_sample;
119
120     int16_t i_padding_0; /* Because the original is using MSVC packing style */
121
122     union
123     {
124         oggds_header_video_t video;
125         oggds_header_audio_t audio;
126     } header;
127
128     int32_t i_padding_1; /* Because the original is using MSVC packing style */
129
130 } oggds_header_t;
131
132 /*****************************************************************************
133  * Definitions of structures and functions used by this plugins
134  *****************************************************************************/
135 typedef struct
136 {
137     int i_cat;
138     int i_fourcc;
139
140     int b_new;
141
142     mtime_t i_dts;
143     mtime_t i_length;
144     int     i_packet_no;
145     int     i_serial_no;
146     int     i_keyframe_granule_shift; /* Theora only */
147     int     i_last_keyframe; /* dirac and theora */
148     int     i_num_frames; /* Theora only */
149     uint64_t u_last_granulepos; /* Used for correct EOS page */
150     int64_t i_num_keyframes;
151     ogg_stream_state os;
152
153     oggds_header_t *p_oggds_header;
154
155 } ogg_stream_t;
156
157 struct sout_mux_sys_t
158 {
159     int     i_streams;
160
161     mtime_t i_start_dts;
162     int     i_next_serial_no;
163
164     /* number of logical streams pending to be added */
165     int i_add_streams;
166
167     /* logical streams pending to be deleted */
168     int i_del_streams;
169     ogg_stream_t **pp_del_streams;
170 };
171
172 static void OggSetDate( block_t *, mtime_t , mtime_t  );
173 static block_t *OggStreamFlush( sout_mux_t *, ogg_stream_state *, mtime_t );
174
175 /*****************************************************************************
176  * Open: Open muxer
177  *****************************************************************************/
178 static int Open( vlc_object_t *p_this )
179 {
180     sout_mux_t      *p_mux = (sout_mux_t*)p_this;
181     sout_mux_sys_t  *p_sys;
182
183     msg_Info( p_mux, "Open" );
184
185     p_sys                 = malloc( sizeof( sout_mux_sys_t ) );
186     if( !p_sys )
187         return VLC_ENOMEM;
188     p_sys->i_streams      = 0;
189     p_sys->i_add_streams  = 0;
190     p_sys->i_del_streams  = 0;
191     p_sys->pp_del_streams = 0;
192
193     p_mux->p_sys        = p_sys;
194     p_mux->pf_control   = Control;
195     p_mux->pf_addstream = AddStream;
196     p_mux->pf_delstream = DelStream;
197     p_mux->pf_mux       = Mux;
198
199     /* First serial number is random.
200      * (Done like this because on win32 you need to seed the random number
201      *  generator once per thread). */
202     uint32_t r;
203     vlc_rand_bytes(&r, sizeof(r));
204     p_sys->i_next_serial_no = r & INT_MAX;
205
206     return VLC_SUCCESS;
207 }
208
209 /*****************************************************************************
210  * Close: Finalize ogg bitstream and close muxer
211  *****************************************************************************/
212 static void Close( vlc_object_t * p_this )
213 {
214     sout_mux_t     *p_mux = (sout_mux_t*)p_this;
215     sout_mux_sys_t *p_sys = p_mux->p_sys;
216
217     msg_Info( p_mux, "Close" );
218
219     if( p_sys->i_del_streams )
220     {
221         block_t *p_og = NULL;
222         mtime_t i_dts = p_sys->pp_del_streams[p_sys->i_del_streams - 1]->i_dts;
223
224         /* Close the current ogg stream */
225         msg_Dbg( p_mux, "writing footer" );
226         block_ChainAppend( &p_og, OggCreateFooter( p_mux ) );
227
228         /* Remove deleted logical streams */
229         for(int i = 0; i < p_sys->i_del_streams; i++ )
230         {
231             ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
232             FREENULL( p_sys->pp_del_streams[i]->p_oggds_header );
233             FREENULL( p_sys->pp_del_streams[i] );
234         }
235         FREENULL( p_sys->pp_del_streams );
236         p_sys->i_streams -= p_sys->i_del_streams;
237
238         /* Write footer */
239         OggSetDate( p_og, i_dts, 0 );
240         sout_AccessOutWrite( p_mux->p_access, p_og );
241     }
242
243     free( p_sys );
244 }
245
246 /*****************************************************************************
247  * Control:
248  *****************************************************************************/
249 static int Control( sout_mux_t *p_mux, int i_query, va_list args )
250 {
251     VLC_UNUSED(p_mux);
252     bool *pb_bool;
253     char **ppsz;
254
255    switch( i_query )
256    {
257        case MUX_CAN_ADD_STREAM_WHILE_MUXING:
258            pb_bool = (bool*)va_arg( args, bool * );
259            *pb_bool = true;
260            return VLC_SUCCESS;
261
262        case MUX_GET_ADD_STREAM_WAIT:
263            pb_bool = (bool*)va_arg( args, bool * );
264            *pb_bool = true;
265            return VLC_SUCCESS;
266
267        case MUX_GET_MIME:
268            ppsz = (char**)va_arg( args, char ** );
269            *ppsz = strdup( "application/ogg" );
270            return VLC_SUCCESS;
271
272         default:
273             return VLC_EGENERIC;
274    }
275 }
276 /*****************************************************************************
277  * AddStream: Add an elementary stream to the muxed stream
278  *****************************************************************************/
279 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
280 {
281     sout_mux_sys_t *p_sys = p_mux->p_sys;
282     ogg_stream_t   *p_stream;
283     uint16_t i_tag;
284
285     msg_Dbg( p_mux, "adding input" );
286
287     p_input->p_sys = p_stream = calloc( 1, sizeof( ogg_stream_t ) );
288     if( !p_stream )
289         return VLC_ENOMEM;
290
291     p_stream->i_cat       = p_input->p_fmt->i_cat;
292     p_stream->i_fourcc    = p_input->p_fmt->i_codec;
293     p_stream->i_serial_no = p_sys->i_next_serial_no++;
294     p_stream->i_packet_no = 0;
295     p_stream->i_last_keyframe = 0;
296     p_stream->i_num_keyframes = 0;
297     p_stream->i_num_frames = 0;
298
299     p_stream->p_oggds_header = 0;
300
301     switch( p_input->p_fmt->i_cat )
302     {
303     case VIDEO_ES:
304         if( !p_input->p_fmt->video.i_frame_rate ||
305             !p_input->p_fmt->video.i_frame_rate_base )
306         {
307             msg_Warn( p_mux, "Missing frame rate, assuming 25fps" );
308             p_input->p_fmt->video.i_frame_rate = 25;
309             p_input->p_fmt->video.i_frame_rate_base = 1;
310         }
311
312         switch( p_stream->i_fourcc )
313         {
314         case VLC_CODEC_MP4V:
315         case VLC_CODEC_MPGV:
316         case VLC_CODEC_DIV3:
317         case VLC_CODEC_MJPG:
318         case VLC_CODEC_WMV1:
319         case VLC_CODEC_WMV2:
320         case VLC_CODEC_WMV3:
321         case VLC_CODEC_SNOW:
322             p_stream->p_oggds_header = calloc( 1, sizeof(oggds_header_t) );
323             if( !p_stream->p_oggds_header )
324             {
325                 free( p_stream );
326                 return VLC_ENOMEM;
327             }
328             p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
329
330             memcpy( p_stream->p_oggds_header->stream_type, "video", 5 );
331             if( p_stream->i_fourcc == VLC_CODEC_MP4V )
332             {
333                 memcpy( p_stream->p_oggds_header->sub_type, "XVID", 4 );
334             }
335             else if( p_stream->i_fourcc == VLC_CODEC_DIV3 )
336             {
337                 memcpy( p_stream->p_oggds_header->sub_type, "DIV3", 4 );
338             }
339             else
340             {
341                 memcpy( p_stream->p_oggds_header->sub_type,
342                         &p_stream->i_fourcc, 4 );
343             }
344             SetDWLE( &p_stream->p_oggds_header->i_size,
345                      sizeof( oggds_header_t ) - 1 );
346             SetQWLE( &p_stream->p_oggds_header->i_time_unit,
347                      INT64_C(10000000) * p_input->p_fmt->video.i_frame_rate_base /
348                      (int64_t)p_input->p_fmt->video.i_frame_rate );
349             SetQWLE( &p_stream->p_oggds_header->i_samples_per_unit, 1 );
350             SetDWLE( &p_stream->p_oggds_header->i_default_len, 1 ); /* ??? */
351             SetDWLE( &p_stream->p_oggds_header->i_buffer_size, 1024*1024 );
352             SetWLE( &p_stream->p_oggds_header->i_bits_per_sample, 0 );
353             SetDWLE( &p_stream->p_oggds_header->header.video.i_width,
354                      p_input->p_fmt->video.i_width );
355             SetDWLE( &p_stream->p_oggds_header->header.video.i_height,
356                      p_input->p_fmt->video.i_height );
357             msg_Dbg( p_mux, "%4.4s stream", (char *)&p_stream->i_fourcc );
358             break;
359
360         case VLC_CODEC_DIRAC:
361             msg_Dbg( p_mux, "dirac stream" );
362             break;
363
364         case VLC_CODEC_THEORA:
365             msg_Dbg( p_mux, "theora stream" );
366             break;
367
368         default:
369             FREENULL( p_input->p_sys );
370             return VLC_EGENERIC;
371         }
372         break;
373
374     case AUDIO_ES:
375         switch( p_stream->i_fourcc )
376         {
377         case VLC_CODEC_VORBIS:
378             msg_Dbg( p_mux, "vorbis stream" );
379             break;
380
381         case VLC_CODEC_SPEEX:
382             msg_Dbg( p_mux, "speex stream" );
383             break;
384
385         case VLC_CODEC_FLAC:
386             msg_Dbg( p_mux, "flac stream" );
387             break;
388
389         default:
390             fourcc_to_wf_tag( p_stream->i_fourcc, &i_tag );
391             if( i_tag == WAVE_FORMAT_UNKNOWN )
392             {
393                 FREENULL( p_input->p_sys );
394                 return VLC_EGENERIC;
395             }
396
397             p_stream->p_oggds_header =
398                 malloc( sizeof(oggds_header_t) + p_input->p_fmt->i_extra );
399             if( !p_stream->p_oggds_header )
400             {
401                 free( p_stream );
402                 return VLC_ENOMEM;
403             }
404             memset( p_stream->p_oggds_header, 0, sizeof(oggds_header_t) );
405             p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
406
407             SetDWLE( &p_stream->p_oggds_header->i_size,
408                      sizeof( oggds_header_t ) - 1 + p_input->p_fmt->i_extra );
409
410             if( p_input->p_fmt->i_extra )
411             {
412                 memcpy( &p_stream->p_oggds_header[1],
413                         p_input->p_fmt->p_extra, p_input->p_fmt->i_extra );
414             }
415
416             memcpy( p_stream->p_oggds_header->stream_type, "audio", 5 );
417
418             memset( p_stream->p_oggds_header->sub_type, 0, 4 );
419             char buf[5];
420             snprintf( buf, sizeof(buf), "%"PRIx16, i_tag );
421             strncpy( p_stream->p_oggds_header->sub_type, buf, 4 );
422
423             SetQWLE( &p_stream->p_oggds_header->i_time_unit, INT64_C(10000000) );
424             SetDWLE( &p_stream->p_oggds_header->i_default_len, 1 );
425             SetDWLE( &p_stream->p_oggds_header->i_buffer_size, 30*1024 );
426             SetQWLE( &p_stream->p_oggds_header->i_samples_per_unit,
427                      p_input->p_fmt->audio.i_rate );
428             SetWLE( &p_stream->p_oggds_header->i_bits_per_sample,
429                     p_input->p_fmt->audio.i_bitspersample );
430             SetDWLE( &p_stream->p_oggds_header->header.audio.i_channels,
431                      p_input->p_fmt->audio.i_channels );
432             SetDWLE( &p_stream->p_oggds_header->header.audio.i_block_align,
433                      p_input->p_fmt->audio.i_blockalign );
434             SetDWLE( &p_stream->p_oggds_header->header.audio.i_avgbytespersec,
435                      p_input->p_fmt->i_bitrate / 8);
436             msg_Dbg( p_mux, "%4.4s stream", (char *)&p_stream->i_fourcc );
437             break;
438         }
439         break;
440
441     case SPU_ES:
442         switch( p_stream->i_fourcc )
443         {
444         case VLC_CODEC_SUBT:
445             p_stream->p_oggds_header = calloc( 1, sizeof(oggds_header_t) );
446             if( !p_stream->p_oggds_header )
447             {
448                 free( p_stream );
449                 return VLC_ENOMEM;
450             }
451             p_stream->p_oggds_header->i_packet_type = PACKET_TYPE_HEADER;
452
453             memcpy( p_stream->p_oggds_header->stream_type, "text", 4 );
454             msg_Dbg( p_mux, "subtitles stream" );
455             break;
456
457         default:
458             FREENULL( p_input->p_sys );
459             return VLC_EGENERIC;
460         }
461         break;
462     default:
463         FREENULL( p_input->p_sys );
464         return VLC_EGENERIC;
465     }
466
467     p_stream->b_new = true;
468
469     p_sys->i_add_streams++;
470
471     return VLC_SUCCESS;
472 }
473
474 /*****************************************************************************
475  * DelStream: Delete an elementary stream from the muxed stream
476  *****************************************************************************/
477 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
478 {
479     sout_mux_sys_t *p_sys  = p_mux->p_sys;
480     ogg_stream_t   *p_stream = (ogg_stream_t*)p_input->p_sys;
481     block_t *p_og;
482
483     msg_Dbg( p_mux, "removing input" );
484
485     /* flush all remaining data */
486     if( p_input->p_sys )
487     {
488         if( !p_stream->b_new )
489         {
490             while( block_FifoCount( p_input->p_fifo ) )
491                 MuxBlock( p_mux, p_input );
492         }
493
494         if( !p_stream->b_new &&
495             ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
496         {
497             OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
498             sout_AccessOutWrite( p_mux->p_access, p_og );
499         }
500
501         /* move input in delete queue */
502         if( !p_stream->b_new )
503         {
504             p_sys->pp_del_streams = xrealloc( p_sys->pp_del_streams,
505                         (p_sys->i_del_streams + 1) * sizeof(ogg_stream_t *) );
506             p_sys->pp_del_streams[p_sys->i_del_streams++] = p_stream;
507         }
508         else
509         {
510             /* wasn't already added so get rid of it */
511             FREENULL( p_stream->p_oggds_header );
512             FREENULL( p_stream );
513             p_sys->i_add_streams--;
514         }
515     }
516
517     p_input->p_sys = NULL;
518
519     return 0;
520 }
521
522 /*****************************************************************************
523  * Ogg bitstream manipulation routines
524  *****************************************************************************/
525 static block_t *OggStreamGetPage( sout_mux_t *p_mux,
526                                   ogg_stream_state *p_os, mtime_t i_pts,
527                                   bool flush )
528 {
529     (void)p_mux;
530     block_t *p_og, *p_og_first = NULL;
531     ogg_page og;
532     int (*pager)( ogg_stream_state*, ogg_page* ) = flush ? ogg_stream_flush : ogg_stream_pageout;
533
534     while( pager( p_os, &og ) )
535     {
536         /* Flush all data */
537         p_og = block_New( p_mux, og.header_len + og.body_len );
538
539         memcpy( p_og->p_buffer, og.header, og.header_len );
540         memcpy( p_og->p_buffer + og.header_len, og.body, og.body_len );
541         p_og->i_dts     = 0;
542         p_og->i_pts     = i_pts;
543         p_og->i_length  = 0;
544
545         i_pts = 0; // write it only once
546
547         block_ChainAppend( &p_og_first, p_og );
548     }
549
550     return p_og_first;
551 }
552
553 static block_t *OggStreamFlush( sout_mux_t *p_mux,
554                                 ogg_stream_state *p_os, mtime_t i_pts )
555 {
556     return OggStreamGetPage( p_mux, p_os, i_pts, true );
557 }
558
559 static block_t *OggStreamPageOut( sout_mux_t *p_mux,
560                                   ogg_stream_state *p_os, mtime_t i_pts )
561 {
562     return OggStreamGetPage( p_mux, p_os, i_pts, false );
563 }
564
565 static block_t *OggCreateHeader( sout_mux_t *p_mux )
566 {
567     block_t *p_hdr = NULL;
568     block_t *p_og = NULL;
569     ogg_packet op;
570     int i;
571
572     /* Write header for each stream. All b_o_s (beginning of stream) packets
573      * must appear first in the ogg stream so we take care of them first. */
574     for( int pass = 0; pass < 2; pass++ )
575     {
576         for( i = 0; i < p_mux->i_nb_inputs; i++ )
577         {
578             sout_input_t *p_input = p_mux->pp_inputs[i];
579             ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
580
581             bool video = ( p_stream->i_fourcc == VLC_CODEC_THEORA || p_stream->i_fourcc == VLC_CODEC_DIRAC );
582             if( ( ( pass == 0 && !video ) || ( pass == 1 && video ) ) )
583                 continue;
584
585             msg_Dbg( p_mux, "creating header for %4.4s",
586                      (char *)&p_stream->i_fourcc );
587
588             ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
589             p_stream->b_new = false;
590             p_stream->i_packet_no = 0;
591
592             if( p_stream->i_fourcc == VLC_CODEC_VORBIS ||
593                 p_stream->i_fourcc == VLC_CODEC_SPEEX ||
594                 p_stream->i_fourcc == VLC_CODEC_THEORA )
595             {
596                 /* First packet in order: vorbis/speex/theora info */
597                 unsigned pi_size[XIPH_MAX_HEADER_COUNT];
598                 void     *pp_data[XIPH_MAX_HEADER_COUNT];
599                 unsigned i_count;
600                 if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
601                                        p_input->p_fmt->i_extra, p_input->p_fmt->p_extra ) )
602                 {
603                     i_count = 0;
604                     pi_size[0] = 0;
605                     pp_data[0] = NULL;
606                 }
607
608                 op.bytes  = pi_size[0];
609                 op.packet = pp_data[0];
610                 if( pi_size[0] <= 0 )
611                     msg_Err( p_mux, "header data corrupted");
612
613                 op.b_o_s  = 1;
614                 op.e_o_s  = 0;
615                 op.granulepos = 0;
616                 op.packetno = p_stream->i_packet_no++;
617                 ogg_stream_packetin( &p_stream->os, &op );
618                 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
619
620                 /* Get keyframe_granule_shift for theora granulepos calculation */
621                 if( p_stream->i_fourcc == VLC_CODEC_THEORA )
622                 {
623                     p_stream->i_keyframe_granule_shift =
624                         ( (op.packet[40] & 0x03) << 3 ) | ( (op.packet[41] & 0xe0) >> 5 );
625                 }
626
627                 for( unsigned i = 0; i < i_count; i++ )
628                     free( pp_data[i] );
629             }
630             else if( p_stream->i_fourcc == VLC_CODEC_DIRAC )
631             {
632                 op.packet = p_input->p_fmt->p_extra;
633                 op.bytes  = p_input->p_fmt->i_extra;
634                 op.b_o_s  = 1;
635                 op.e_o_s  = 0;
636                 op.granulepos = ~0;
637                 op.packetno = p_stream->i_packet_no++;
638                 ogg_stream_packetin( &p_stream->os, &op );
639                 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
640             }
641             else if( p_stream->i_fourcc == VLC_CODEC_FLAC )
642             {
643                 /* flac stream marker (yeah, only that in the 1st packet) */
644                 op.packet = (unsigned char *)"fLaC";
645                 op.bytes  = 4;
646                 op.b_o_s  = 1;
647                 op.e_o_s  = 0;
648                 op.granulepos = 0;
649                 op.packetno = p_stream->i_packet_no++;
650                 ogg_stream_packetin( &p_stream->os, &op );
651                 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
652             }
653             else if( p_stream->p_oggds_header )
654             {
655                 /* ds header */
656                 op.packet = (uint8_t*)p_stream->p_oggds_header;
657                 op.bytes  = p_stream->p_oggds_header->i_size + 1;
658                 op.b_o_s  = 1;
659                 op.e_o_s  = 0;
660                 op.granulepos = 0;
661                 op.packetno = p_stream->i_packet_no++;
662                 ogg_stream_packetin( &p_stream->os, &op );
663                 p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
664             }
665
666             block_ChainAppend( &p_hdr, p_og );
667         }
668     }
669
670     /* Take care of the non b_o_s headers */
671     for( i = 0; i < p_mux->i_nb_inputs; i++ )
672     {
673         sout_input_t *p_input = p_mux->pp_inputs[i];
674         ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
675
676         if( p_stream->i_fourcc == VLC_CODEC_VORBIS ||
677             p_stream->i_fourcc == VLC_CODEC_SPEEX ||
678             p_stream->i_fourcc == VLC_CODEC_THEORA )
679         {
680             unsigned pi_size[XIPH_MAX_HEADER_COUNT];
681             void     *pp_data[XIPH_MAX_HEADER_COUNT];
682             unsigned i_count;
683             if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
684                                    p_input->p_fmt->i_extra, p_input->p_fmt->p_extra ) )
685                 i_count = 0;
686
687             /* Special case, headers are already there in the incoming stream.
688              * We need to gather them an mark them as headers. */
689             for( unsigned i = 1; i < i_count; i++ )
690             {
691                 op.bytes  = pi_size[i];
692                 op.packet = pp_data[i];
693                 if( pi_size[i] <= 0 )
694                     msg_Err( p_mux, "header data corrupted");
695
696                 op.b_o_s  = 0;
697                 op.e_o_s  = 0;
698                 op.granulepos = 0;
699                 op.packetno = p_stream->i_packet_no++;
700                 ogg_stream_packetin( &p_stream->os, &op );
701
702                 if( i == i_count - 1 )
703                     p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
704                 else
705                     p_og = OggStreamPageOut( p_mux, &p_stream->os, 0 );
706                 if( p_og )
707                     block_ChainAppend( &p_hdr, p_og );
708             }
709             for( unsigned i = 0; i < i_count; i++ )
710                 free( pp_data[i] );
711         }
712         else if( p_stream->i_fourcc != VLC_CODEC_FLAC &&
713                  p_stream->i_fourcc != VLC_CODEC_DIRAC )
714         {
715             uint8_t com[128];
716             int     i_com;
717
718             /* comment */
719             com[0] = PACKET_TYPE_COMMENT;
720             i_com = snprintf( (char *)(com+1), 127,
721                               PACKAGE_VERSION" stream output" )
722                      + 1;
723             op.packet = com;
724             op.bytes  = i_com;
725             op.b_o_s  = 0;
726             op.e_o_s  = 0;
727             op.granulepos = 0;
728             op.packetno = p_stream->i_packet_no++;
729             ogg_stream_packetin( &p_stream->os, &op );
730             p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
731             block_ChainAppend( &p_hdr, p_og );
732         }
733
734         /* Special case for mp4v and flac */
735         if( ( p_stream->i_fourcc == VLC_CODEC_MP4V ||
736               p_stream->i_fourcc == VLC_CODEC_FLAC ) &&
737             p_input->p_fmt->i_extra )
738         {
739             /* Send a packet with the VOL data for mp4v
740              * or STREAMINFO for flac */
741             msg_Dbg( p_mux, "writing extra data" );
742             op.bytes  = p_input->p_fmt->i_extra;
743             op.packet = p_input->p_fmt->p_extra;
744             if( p_stream->i_fourcc == VLC_CODEC_FLAC )
745             {
746                 /* Skip the flac stream marker */
747                 op.bytes -= 4;
748                 op.packet+= 4;
749             }
750             op.b_o_s  = 0;
751             op.e_o_s  = 0;
752             op.granulepos = 0;
753             op.packetno = p_stream->i_packet_no++;
754             ogg_stream_packetin( &p_stream->os, &op );
755             p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
756             block_ChainAppend( &p_hdr, p_og );
757         }
758     }
759
760     /* set HEADER flag */
761     for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
762     {
763         p_og->i_flags |= BLOCK_FLAG_HEADER;
764     }
765     return p_hdr;
766 }
767
768 static block_t *OggCreateFooter( sout_mux_t *p_mux )
769 {
770     sout_mux_sys_t *p_sys = p_mux->p_sys;
771     block_t *p_hdr = NULL;
772     block_t *p_og;
773     ogg_packet    op;
774     int     i;
775
776     /* flush all remaining data */
777     for( i = 0; i < p_mux->i_nb_inputs; i++ )
778     {
779         ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
780
781         /* skip newly added streams */
782         if( p_stream->b_new ) continue;
783
784         if( ( p_og = OggStreamFlush( p_mux, &p_stream->os, 0 ) ) )
785         {
786             OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
787             sout_AccessOutWrite( p_mux->p_access, p_og );
788         }
789     }
790
791     /* Write eos packets for each stream. */
792     for( i = 0; i < p_mux->i_nb_inputs; i++ )
793     {
794         ogg_stream_t *p_stream = p_mux->pp_inputs[i]->p_sys;
795
796         /* skip newly added streams */
797         if( p_stream->b_new ) continue;
798
799         op.packet = NULL;
800         op.bytes  = 0;
801         op.b_o_s  = 0;
802         op.e_o_s  = 1;
803         op.granulepos = p_stream->u_last_granulepos;
804         op.packetno = p_stream->i_packet_no++;
805         ogg_stream_packetin( &p_stream->os, &op );
806
807         p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
808         block_ChainAppend( &p_hdr, p_og );
809         ogg_stream_clear( &p_stream->os );
810     }
811
812     for( i = 0; i < p_sys->i_del_streams; i++ )
813     {
814         op.packet = NULL;
815         op.bytes  = 0;
816         op.b_o_s  = 0;
817         op.e_o_s  = 1;
818         op.granulepos = p_sys->pp_del_streams[i]->u_last_granulepos;
819         op.packetno = p_sys->pp_del_streams[i]->i_packet_no++;
820         ogg_stream_packetin( &p_sys->pp_del_streams[i]->os, &op );
821
822         p_og = OggStreamFlush( p_mux, &p_sys->pp_del_streams[i]->os, 0 );
823         block_ChainAppend( &p_hdr, p_og );
824         ogg_stream_clear( &p_sys->pp_del_streams[i]->os );
825     }
826
827     return p_hdr;
828 }
829
830 static void OggSetDate( block_t *p_og, mtime_t i_dts, mtime_t i_length )
831 {
832     int i_count;
833     block_t *p_tmp;
834     mtime_t i_delta;
835
836     for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
837     {
838         i_count++;
839     }
840
841     if( i_count == 0 ) return; /* ignore. */
842
843     i_delta = i_length / i_count;
844
845     for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
846     {
847         p_tmp->i_dts    = i_dts;
848         p_tmp->i_length = i_delta;
849
850         i_dts += i_delta;
851     }
852 }
853
854 /*****************************************************************************
855  * Mux: multiplex available data in input fifos into the Ogg bitstream
856  *****************************************************************************/
857 static int Mux( sout_mux_t *p_mux )
858 {
859     sout_mux_sys_t *p_sys = p_mux->p_sys;
860     block_t        *p_og = NULL;
861     mtime_t        i_dts;
862
863     if( p_sys->i_add_streams || p_sys->i_del_streams )
864     {
865         /* Open new ogg stream */
866         if( sout_MuxGetStream( p_mux, 1, &i_dts) < 0 )
867         {
868             msg_Dbg( p_mux, "waiting for data..." );
869             return VLC_SUCCESS;
870         }
871
872         if( p_sys->i_streams )
873         {
874             /* Close current ogg stream */
875             int i;
876
877             msg_Dbg( p_mux, "writing footer" );
878             block_ChainAppend( &p_og, OggCreateFooter( p_mux ) );
879
880             /* Remove deleted logical streams */
881             for( i = 0; i < p_sys->i_del_streams; i++ )
882             {
883                 FREENULL( p_sys->pp_del_streams[i]->p_oggds_header );
884                 FREENULL( p_sys->pp_del_streams[i] );
885             }
886             FREENULL( p_sys->pp_del_streams );
887             p_sys->i_streams = 0;
888         }
889
890         msg_Dbg( p_mux, "writing header" );
891         p_sys->i_start_dts = i_dts;
892         p_sys->i_streams = p_mux->i_nb_inputs;
893         p_sys->i_del_streams = 0;
894         p_sys->i_add_streams = 0;
895         block_ChainAppend( &p_og, OggCreateHeader( p_mux ) );
896
897         /* Write header and/or footer */
898         OggSetDate( p_og, i_dts, 0 );
899         sout_AccessOutWrite( p_mux->p_access, p_og );
900         p_og = NULL;
901     }
902
903     for( ;; )
904     {
905         int i_stream = sout_MuxGetStream( p_mux, 1, NULL );
906         if( i_stream < 0 )
907             return VLC_SUCCESS;
908         MuxBlock( p_mux, p_mux->pp_inputs[i_stream] );
909     }
910
911     return VLC_SUCCESS;
912 }
913
914 static int MuxBlock( sout_mux_t *p_mux, sout_input_t *p_input )
915 {
916     sout_mux_sys_t *p_sys = p_mux->p_sys;
917     ogg_stream_t *p_stream = (ogg_stream_t*)p_input->p_sys;
918     block_t *p_data = block_FifoGet( p_input->p_fifo );
919     block_t *p_og = NULL;
920     ogg_packet op;
921
922     if( p_stream->i_fourcc != VLC_CODEC_VORBIS &&
923         p_stream->i_fourcc != VLC_CODEC_FLAC &&
924         p_stream->i_fourcc != VLC_CODEC_SPEEX &&
925         p_stream->i_fourcc != VLC_CODEC_THEORA &&
926         p_stream->i_fourcc != VLC_CODEC_DIRAC )
927     {
928         p_data = block_Realloc( p_data, 1, p_data->i_buffer );
929         p_data->p_buffer[0] = PACKET_IS_SYNCPOINT;      // FIXME
930     }
931
932     op.packet   = p_data->p_buffer;
933     op.bytes    = p_data->i_buffer;
934     op.b_o_s    = 0;
935     op.e_o_s    = 0;
936     op.packetno = p_stream->i_packet_no++;
937
938     if( p_stream->i_cat == AUDIO_ES )
939     {
940         if( p_stream->i_fourcc == VLC_CODEC_VORBIS ||
941             p_stream->i_fourcc == VLC_CODEC_FLAC ||
942             p_stream->i_fourcc == VLC_CODEC_SPEEX )
943         {
944             /* number of sample from begining + current packet */
945             op.granulepos =
946                 ( p_data->i_dts - p_sys->i_start_dts + p_data->i_length ) *
947                 (mtime_t)p_input->p_fmt->audio.i_rate / INT64_C(1000000);
948         }
949         else if( p_stream->p_oggds_header )
950         {
951             /* number of sample from begining */
952             op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) *
953                 p_stream->p_oggds_header->i_samples_per_unit / INT64_C(1000000);
954         }
955     }
956     else if( p_stream->i_cat == VIDEO_ES )
957     {
958         if( p_stream->i_fourcc == VLC_CODEC_THEORA )
959         {
960             p_stream->i_num_frames++;
961             if( p_data->i_flags & BLOCK_FLAG_TYPE_I )
962             {
963                 p_stream->i_num_keyframes++;
964                 p_stream->i_last_keyframe = p_stream->i_num_frames;
965             }
966
967             op.granulepos = (p_stream->i_last_keyframe << p_stream->i_keyframe_granule_shift )
968                           | (p_stream->i_num_frames-p_stream->i_last_keyframe);
969         }
970         else if( p_stream->i_fourcc == VLC_CODEC_DIRAC )
971         {
972             mtime_t dt = (p_data->i_dts - p_sys->i_start_dts + 1)
973                        * p_input->p_fmt->video.i_frame_rate *2
974                        / p_input->p_fmt->video.i_frame_rate_base
975                        / INT64_C(1000000);
976             mtime_t delay = (p_data->i_pts - p_data->i_dts + 1)
977                           * p_input->p_fmt->video.i_frame_rate *2
978                           / p_input->p_fmt->video.i_frame_rate_base
979                           / INT64_C(1000000);
980             if( p_data->i_flags & BLOCK_FLAG_TYPE_I )
981                 p_stream->i_last_keyframe = dt;
982             mtime_t dist = dt - p_stream->i_last_keyframe;
983             op.granulepos = dt << 31 | (dist&0xff00) << 14
984                           | (delay&0x1fff) << 9 | (dist&0xff);
985         }
986         else if( p_stream->p_oggds_header )
987             op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) * INT64_C(10) /
988                 p_stream->p_oggds_header->i_time_unit;
989     }
990     else if( p_stream->i_cat == SPU_ES )
991     {
992         /* granulepos is in millisec */
993         op.granulepos = ( p_data->i_dts - p_sys->i_start_dts ) / 1000;
994     }
995     else
996         return VLC_EGENERIC;
997
998     p_stream->u_last_granulepos = op.granulepos;
999     ogg_stream_packetin( &p_stream->os, &op );
1000
1001     if( p_stream->i_cat == SPU_ES ||
1002         p_stream->i_fourcc == VLC_CODEC_SPEEX ||
1003         p_stream->i_fourcc == VLC_CODEC_DIRAC )
1004     {
1005         /* Subtitles or Speex packets are quite small so they
1006          * need to be flushed to be sent on time */
1007         /* The OggDirac mapping suggests ever so strongly that a
1008          * page flush occurs after each OggDirac packet, so to make
1009          * the timestamps unambiguous */
1010         p_og = OggStreamFlush( p_mux, &p_stream->os, p_data->i_dts );
1011     }
1012     else
1013     {
1014         p_og = OggStreamPageOut( p_mux, &p_stream->os, p_data->i_dts );
1015     }
1016
1017     if( p_og )
1018     {
1019         OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
1020         p_stream->i_dts = -1;
1021         p_stream->i_length = 0;
1022
1023         sout_AccessOutWrite( p_mux->p_access, p_og );
1024     }
1025     else
1026     {
1027         if( p_stream->i_dts < 0 )
1028         {
1029             p_stream->i_dts = p_data->i_dts;
1030         }
1031         p_stream->i_length += p_data->i_length;
1032     }
1033
1034     block_Release( p_data );
1035     return VLC_SUCCESS;
1036 }