* all: reworked stream output. Now it handles mutliples outputs from
[vlc.git] / modules / mux / ogg.c
1 /*****************************************************************************
2  * ogg.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: ogg.c,v 1.3 2003/03/11 19:02:30 fenrir Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/types.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/input.h>
33 #include <vlc/sout.h>
34
35 #include "codecs.h"
36
37 #include <ogg/ogg.h>
38
39 /*****************************************************************************
40  * Exported prototypes
41  *****************************************************************************/
42 static int     Open   ( vlc_object_t * );
43 static void    Close  ( vlc_object_t * );
44
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 * );
49
50 /*****************************************************************************
51  * Module descriptor
52  *****************************************************************************/
53 vlc_module_begin();
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 );
59 vlc_module_end();
60
61 /*****************************************************************************
62  *
63  *****************************************************************************/
64 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
65
66 #define PACKET_TYPE_HEADER   0x01
67 #define PACKET_TYPE_COMMENT  0x03
68
69 #define PACKET_IS_SYNCPOINT      0x08
70
71 typedef struct __attribute__((__packed__))
72 {
73     int32_t i_width;
74     int32_t i_height;
75 } ogg_stream_header_video_t;
76
77 typedef struct __attribute__((__packed__))
78 {
79     int16_t i_channels;
80     int16_t i_block_align;
81     int32_t i_avgbytespersec;
82 } ogg_stream_header_audio_t;
83
84 typedef struct __attribute__((__packed__))
85 {
86     uint8_t i_packet_type;
87
88     char stream_type[8];
89     char sub_type[4];
90
91     int32_t i_size;
92
93     int64_t i_time_unit;
94     int64_t i_samples_per_unit;
95     int32_t i_default_len;
96
97     int32_t i_buffer_size;
98     int16_t i_bits_per_sample;
99     int16_t i_padding_0;            // hum hum
100     union
101     {
102         ogg_stream_header_video_t video;
103         ogg_stream_header_audio_t audio;
104     } header;
105
106 } ogg_stream_header_t;
107
108
109 typedef struct
110 {
111     int i_cat;
112     int i_fourcc;
113
114     ogg_stream_header_t header;
115
116     int i_packet_no;
117
118     mtime_t             i_dts;
119     mtime_t             i_length;
120     ogg_stream_state    os;
121
122 } ogg_stream_t;
123
124 struct sout_mux_sys_t
125 {
126     int b_write_header;
127     int i_streams;
128
129 };
130
131 #define SetWLE( p, v ) _SetWLE( (uint8_t*)p, v)
132 static void _SetWLE( uint8_t *p, uint16_t i_dw )
133 {
134     p[1] = ( i_dw >>  8 )&0xff;
135     p[0] = ( i_dw       )&0xff;
136 }
137
138 #define SetDWLE( p, v ) _SetDWLE( (uint8_t*)p, v)
139 static void _SetDWLE( uint8_t *p, uint32_t i_dw )
140 {
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;
145 }
146 #define SetQWLE( p, v ) _SetQWLE( (uint8_t*)p, v)
147 static void _SetQWLE( uint8_t *p, uint64_t i_qw )
148 {
149     SetDWLE( p,   i_qw&0xffffffff );
150     SetDWLE( p+4, ( i_qw >> 32)&0xffffffff );
151 }
152
153 static void OggSetDate( sout_buffer_t *, mtime_t , mtime_t  );
154 static sout_buffer_t *OggStreamFlush( sout_mux_t *, ogg_stream_state *, mtime_t );
155
156 /*****************************************************************************
157  * Open:
158  *****************************************************************************/
159 static int Open( vlc_object_t *p_this )
160 {
161     sout_mux_t      *p_mux = (sout_mux_t*)p_this;
162     sout_mux_sys_t  *p_sys;
163
164     msg_Info( p_mux, "Open" );
165
166     p_sys                 = malloc( sizeof( sout_mux_sys_t ) );
167     p_sys->i_streams      = 0;
168     p_sys->b_write_header = VLC_TRUE;
169
170     p_mux->p_sys        = p_sys;
171     p_mux->pf_capacity  = Capability;
172     p_mux->pf_addstream = AddStream;
173     p_mux->pf_delstream = DelStream;
174     p_mux->pf_mux       = Mux;
175     p_mux->i_preheader  = 1;
176
177     return VLC_SUCCESS;
178 }
179
180 /*****************************************************************************
181  * Close:
182  *****************************************************************************/
183
184 static void Close( vlc_object_t * p_this )
185 {
186     sout_mux_t      *p_mux = (sout_mux_t*)p_this;
187     sout_mux_sys_t  *p_sys = p_mux->p_sys;
188
189     msg_Info( p_mux, "Close" );
190
191     free( p_sys );
192 }
193
194 static int Capability( sout_mux_t *p_mux, int i_query, void *p_args, void *p_answer )
195 {
196    switch( i_query )
197    {
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 );
201         default:
202             return( SOUT_MUX_CAP_ERR_UNIMPLEMENTED );
203    }
204 }
205
206 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
207 {
208     sout_mux_sys_t  *p_sys = p_mux->p_sys;
209     ogg_stream_t        *p_stream;
210
211     BITMAPINFOHEADER    *p_bih;
212     WAVEFORMATEX        *p_wf;
213
214     msg_Dbg( p_mux, "adding input" );
215     p_input->p_sys = (void*)p_stream = malloc( sizeof( ogg_stream_t ) );
216
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;
220
221     p_stream->header.i_packet_type = PACKET_TYPE_HEADER;
222     switch( p_input->input_format.i_cat )
223     {
224         case VIDEO_ES:
225             p_bih = (BITMAPINFOHEADER*)p_input->input_format.p_format;
226             switch( p_input->input_format.i_fourcc )
227             {
228                 case VLC_FOURCC( 'm', 'p','4', 'v' ):
229                 case VLC_FOURCC( 'D', 'I','V', '3' ):
230                     memcpy( p_stream->header.stream_type,
231                             "video    ",
232                             8 );
233                     if( p_input->input_format.i_fourcc == VLC_FOURCC( 'm', 'p','4', 'v' ) )
234                     {
235                         memcpy( p_stream->header.sub_type, "XVID", 4 );
236                     }
237                     else if( p_input->input_format.i_fourcc == VLC_FOURCC( 'D', 'I','V', '3' ) )
238                     {
239                         memcpy( p_stream->header.sub_type, "DIV3", 4 );
240                     }
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 );
248                     if( p_bih )
249                     {
250                         SetDWLE( &p_stream->header.header.video.i_width, p_bih->biWidth );
251                         SetDWLE( &p_stream->header.header.video.i_height, p_bih->biHeight );
252                     }
253                     else
254                     {
255                         SetDWLE( &p_stream->header.header.video.i_width,  0 );
256                         SetDWLE( &p_stream->header.header.video.i_height, 0 );
257                     }
258                     break;
259                 default:
260                     FREE( p_input->p_sys );
261                     return( VLC_EGENERIC );
262             }
263             break;
264         case AUDIO_ES:
265             p_wf = (WAVEFORMATEX*)p_input->input_format.p_format;
266             switch( p_input->input_format.i_fourcc )
267             {
268                 case VLC_FOURCC( 'm', 'p','g', 'a' ):
269                 case VLC_FOURCC( 'a', '5','2', ' ' ):
270                     memcpy( p_stream->header.stream_type,
271                             "audio    ",
272                             8 );
273                     if( p_input->input_format.i_fourcc == VLC_FOURCC( 'm', 'p','g', 'a' ) )
274                     {
275                         memcpy( p_stream->header.sub_type, "55  ", 4 );
276                     }
277                     else if( p_input->input_format.i_fourcc == VLC_FOURCC( 'a', '5','2', ' ' ) )
278                     {
279                         memcpy( p_stream->header.sub_type, "2000", 4 );
280                     }
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 );
285                     if( p_wf )
286                     {
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 );
292                     }
293                     else
294                     {
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 );
301                     }
302                     break;
303                 case VLC_FOURCC( 'v', 'o', 'r', 'b' ):
304                 default:
305                     FREE( p_input->p_sys );
306                     return( VLC_EGENERIC );
307             }
308             break;
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     p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
330     if( p_og )
331     {
332         OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
333
334         sout_AccessOutWrite( p_mux->p_access, p_og );
335     }
336
337     ogg_stream_clear( &p_stream->os );
338
339     FREE( p_input->p_sys );
340     return( 0 );
341 }
342
343 /*
344  * TODO  move this function to src/stream_output.c (used by nearly all muxers)
345  */
346 static int MuxGetStream( sout_mux_t *p_mux,
347                          int        *pi_stream,
348                          mtime_t    *pi_dts )
349 {
350     mtime_t i_dts;
351     int     i_stream;
352     int     i;
353
354     for( i = 0, i_dts = 0, i_stream = -1; i < p_mux->i_nb_inputs; i++ )
355     {
356         sout_fifo_t  *p_fifo;
357
358         p_fifo = p_mux->pp_inputs[i]->p_fifo;
359
360         if( p_fifo->i_depth > 1 )
361         {
362             sout_buffer_t *p_buf;
363
364             p_buf = sout_FifoShow( p_fifo );
365             if( i_stream < 0 || p_buf->i_dts < i_dts )
366             {
367                 i_dts = p_buf->i_dts;
368                 i_stream = i;
369             }
370         }
371         else
372         {
373             return( -1 ); // wait that all fifo have at least 2 packets
374         }
375     }
376     if( pi_stream )
377     {
378         *pi_stream = i_stream;
379     }
380     if( pi_dts )
381     {
382         *pi_dts = i_dts;
383     }
384     return( i_stream );
385 }
386
387
388 static sout_buffer_t *OggStreamFlush( sout_mux_t *p_mux,
389                                       ogg_stream_state *p_os,
390                                       mtime_t i_pts )
391 {
392     sout_buffer_t       *p_og, *p_og_first = NULL;
393     ogg_page            og;
394
395     for( ;; )
396     {
397         /* flush all data */
398         int i_result;
399         int i_size;
400         if( ( i_result = ogg_stream_flush( p_os, &og ) ) == 0 )
401         {
402             break;
403         }
404         i_size = og.header_len + og.body_len;
405         p_og = sout_BufferNew( p_mux->p_sout, i_size);
406
407         memcpy( p_og->p_buffer,
408                 og.header,
409                 og.header_len );
410         memcpy( p_og->p_buffer + og.header_len,
411                 og.body,
412                 og.body_len );
413         p_og->i_size    = i_size;
414         p_og->i_dts     = 0;
415         p_og->i_pts     = i_pts;
416         p_og->i_length  = 0;
417
418         i_pts   = 0; // write it only once
419
420         sout_BufferChain( &p_og_first, p_og );
421     }
422
423     return( p_og_first );
424 }
425 static sout_buffer_t *OggStreamPageOut( sout_mux_t *p_mux,
426                                         ogg_stream_state *p_os,
427                                         mtime_t i_pts )
428 {
429     sout_buffer_t       *p_og, *p_og_first = NULL;
430     ogg_page            og;
431
432     for( ;; )
433     {
434         /* flush all data */
435         int i_result;
436         int i_size;
437         if( ( i_result = ogg_stream_pageout( p_os, &og ) ) == 0 )
438         {
439             break;
440         }
441         i_size = og.header_len + og.body_len;
442         p_og = sout_BufferNew( p_mux->p_sout, i_size);
443
444         memcpy( p_og->p_buffer,
445                 og.header,
446                 og.header_len );
447         memcpy( p_og->p_buffer + og.header_len,
448                 og.body,
449                 og.body_len );
450         p_og->i_size    = i_size;
451         p_og->i_dts     = 0;
452         p_og->i_pts     = i_pts;
453         p_og->i_length  = 0;
454
455         i_pts   = 0; // write them only once
456
457         sout_BufferChain( &p_og_first, p_og );
458     }
459
460     return( p_og_first );
461 }
462 static sout_buffer_t *OggCreateHeader( sout_mux_t *p_mux, mtime_t i_dts )
463 {
464     sout_buffer_t *p_hdr = NULL;
465     sout_buffer_t *p_og;
466     ogg_packet    op;
467     int i;
468
469     /* write header for each stream */
470     for( i = 0; i < p_mux->i_nb_inputs; i++ )
471     {
472         ogg_stream_t *p_stream;
473
474         p_stream = (ogg_stream_t*)p_mux->pp_inputs[i]->p_sys;
475
476         if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
477         {
478             /* special case */
479         }
480         else
481         {
482             /* ds header */
483 #if 0
484             uint8_t com[128];
485             int     i_com;
486 #endif
487
488             /* header */
489             op.packet = (uint8_t*)&p_stream->header;
490             op.bytes  = sizeof( ogg_stream_t );
491             op.b_o_s  = 1;
492             op.e_o_s  = 0;
493             op.granulepos = 0;
494             op.packetno = p_stream->i_packet_no++;
495             ogg_stream_packetin( &p_stream->os, &op );
496 #if 0
497             /* I don't know why but this produce broken file */
498             /* comment */
499             com[0] = PACKET_TYPE_COMMENT;
500             i_com = snprintf( &com[1], 128, "VLC 0.5.x stream output" ) + 1;
501             op.packet = com;
502             op.bytes  = i_com;
503             op.b_o_s  = 0;
504             op.e_o_s  = 0;
505             op.granulepos = 0;
506             op.packetno = p_stream->i_packet_no++;
507             ogg_stream_packetin( &p_stream->os, &op );
508 #endif
509         }
510
511         p_og = OggStreamFlush( p_mux, &p_stream->os, 0 );
512         sout_BufferChain( &p_hdr, p_og );
513     }
514
515     /* set HEADER flag */
516     for( p_og = p_hdr; p_og != NULL; p_og = p_og->p_next )
517     {
518         p_og->i_flags |= SOUT_BUFFER_FLAGS_HEADER;
519     }
520     return( p_hdr );
521 }
522
523
524 static void OggSetDate( sout_buffer_t *p_og, mtime_t i_dts, mtime_t i_length )
525 {
526     int i_count;
527     sout_buffer_t *p_tmp;
528     mtime_t i_delta;
529
530     for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
531     {
532         i_count++;
533     }
534     i_delta = i_length / i_count;
535
536     for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
537     {
538         p_tmp->i_dts    = i_dts;
539         p_tmp->i_length = i_delta;
540
541         i_dts += i_delta;
542     }
543 }
544
545 static int Mux      ( sout_mux_t *p_mux )
546 {
547     sout_mux_sys_t      *p_sys  = p_mux->p_sys;
548     sout_buffer_t       *p_og = NULL;
549     int i_stream;
550     mtime_t             i_dts;
551
552     if( p_sys->b_write_header )
553     {
554         if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
555         {
556             return( VLC_SUCCESS );
557         }
558         sout_BufferChain( &p_og, OggCreateHeader( p_mux, i_dts ) );
559         p_sys->b_write_header = VLC_FALSE;
560     }
561
562     for( ;; )
563     {
564         sout_input_t    *p_input;
565         ogg_stream_t    *p_stream;
566         sout_buffer_t   *p_data;
567         ogg_packet          op;
568
569         if( MuxGetStream( p_mux, &i_stream, &i_dts) < 0 )
570         {
571             return( VLC_SUCCESS );
572         }
573
574         p_input  = p_mux->pp_inputs[i_stream];
575         p_stream = (ogg_stream_t*)p_input->p_sys;
576
577         p_data  = sout_FifoGet( p_input->p_fifo );
578
579         sout_BufferReallocFromPreHeader( p_mux->p_sout, p_data, 1 );
580         p_data->p_buffer[0] = PACKET_IS_SYNCPOINT;      // FIXME
581
582         op.packet = p_data->p_buffer;
583         op.bytes  = p_data->i_size;
584         op.b_o_s  = 0;
585         op.e_o_s  = 0;
586         if( p_stream->i_cat == AUDIO_ES )
587         {
588             /* number of sample from begining */
589             op.granulepos = i_dts * p_stream->header.i_samples_per_unit / (int64_t)1000000;
590         }
591         else if( p_stream->i_cat == VIDEO_ES )
592         {
593             op.granulepos = i_dts/1000;//p_stream->i_packet_no;
594         }
595         op.packetno = p_stream->i_packet_no++;
596         ogg_stream_packetin( &p_stream->os, &op );
597
598         sout_BufferChain( &p_og,
599                           OggStreamPageOut( p_mux,
600                                             &p_stream->os,
601                                             p_data->i_pts ) );
602
603         if( p_og )
604         {
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_mux->p_access, p_og );
609
610             p_og = NULL;
611         }
612         else
613         {
614             if( p_stream->i_dts < 0 )
615             {
616                 p_stream->i_dts     = p_data->i_dts;
617             }
618             p_stream->i_length += p_data->i_length;
619         }
620
621         sout_BufferDelete( p_mux->p_sout, p_data );
622     }
623
624     return( VLC_SUCCESS );
625 }
626