* all : ogg/ogm muxer. Yet very limited as it accepts only mp3/a52 audio
[vlc.git] / modules / mux / ogg.c
1 /*****************************************************************************
2  * ogg.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: ogg.c,v 1.1 2003/02/24 23:27:20 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_instance_t *, int, void *, void * );
46 static int AddStream( sout_instance_t *, sout_input_t * );
47 static int DelStream( sout_instance_t *, sout_input_t * );
48 static int Mux      ( sout_instance_t * );
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 typedef struct sout_mux_s
125 {
126     int b_write_header;
127     int i_streams;
128
129 } sout_mux_t;
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_instance_t *, ogg_stream_state *, mtime_t );
155
156 /*****************************************************************************
157  * Open:
158  *****************************************************************************/
159 static int Open( vlc_object_t *p_this )
160 {
161     sout_instance_t     *p_sout = (sout_instance_t*)p_this;
162     sout_mux_t          *p_mux;
163
164     msg_Info( p_sout, "Open" );
165
166     p_mux                 = malloc( sizeof( sout_mux_t ) );
167     p_mux->i_streams      = 0;
168     p_mux->b_write_header = VLC_TRUE;
169
170     p_sout->p_mux_data       = (void*)p_mux;
171     p_sout->pf_mux_capacity  = Capability;
172     p_sout->pf_mux_addstream = AddStream;
173     p_sout->pf_mux_delstream = DelStream;
174     p_sout->pf_mux           = Mux;
175     p_sout->i_mux_preheader  = 1;
176
177     return VLC_SUCCESS;
178 }
179
180 /*****************************************************************************
181  * Close:
182  *****************************************************************************/
183
184 static void Close( vlc_object_t * p_this )
185 {
186     sout_instance_t     *p_sout = (sout_instance_t*)p_this;
187     sout_mux_t          *p_mux  = (sout_mux_t*)p_sout->p_mux_data;
188
189     msg_Info( p_sout, "Close" );
190
191     free( p_mux );
192 }
193
194 static int Capability( sout_instance_t *p_sout, 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_instance_t *p_sout, sout_input_t *p_input )
207 {
208     sout_mux_t          *p_mux  = (sout_mux_t*)p_sout->p_mux_data;
209     ogg_stream_t        *p_stream;
210
211     BITMAPINFOHEADER    *p_bih;
212     WAVEFORMATEX        *p_wf;
213
214     msg_Dbg( p_sout, "adding input" );
215     p_input->p_mux_data = (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_mux_data );
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_mux_data );
306                     return( VLC_EGENERIC );
307             }
308             break;
309         default:
310             FREE( p_input->p_mux_data );
311             return( VLC_EGENERIC );
312     }
313
314     ogg_stream_init (&p_stream->os, rand ());
315
316     p_mux->i_streams++;
317     return( VLC_SUCCESS );
318 }
319
320 static int DelStream( sout_instance_t *p_sout, sout_input_t *p_input )
321 {
322     ogg_stream_t        *p_stream = (ogg_stream_t*)p_input->p_mux_data;
323     sout_buffer_t       *p_og;
324
325     msg_Dbg( p_sout, "removing input" );
326
327     /* flush all remaining data */
328
329     p_og = OggStreamFlush( p_sout, &p_stream->os, 0 );
330     OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
331
332     sout_AccessOutWrite( p_sout->p_access, p_og );
333
334     ogg_stream_clear( &p_stream->os );
335
336     FREE( p_input->p_mux_data );
337     return( 0 );
338 }
339
340 /*
341  * TODO  move this function to src/stream_output.c (used by nearly all muxers)
342  */
343 static int MuxGetStream( sout_instance_t *p_sout,
344                          int        *pi_stream,
345                          mtime_t    *pi_dts )
346 {
347     mtime_t i_dts;
348     int     i_stream;
349     int     i;
350
351     for( i = 0, i_dts = 0, i_stream = -1; i < p_sout->i_nb_inputs; i++ )
352     {
353         sout_fifo_t  *p_fifo;
354
355         p_fifo = p_sout->pp_inputs[i]->p_fifo;
356
357         if( p_fifo->i_depth > 1 )
358         {
359             sout_buffer_t *p_buf;
360
361             p_buf = sout_FifoShow( p_fifo );
362             if( i_stream < 0 || p_buf->i_dts < i_dts )
363             {
364                 i_dts = p_buf->i_dts;
365                 i_stream = i;
366             }
367         }
368         else
369         {
370             return( -1 ); // wait that all fifo have at least 2 packets
371         }
372     }
373     if( pi_stream )
374     {
375         *pi_stream = i_stream;
376     }
377     if( pi_dts )
378     {
379         *pi_dts = i_dts;
380     }
381     return( i_stream );
382 }
383
384
385 static sout_buffer_t *OggStreamFlush( sout_instance_t *p_sout,
386                                       ogg_stream_state *p_os,
387                                       mtime_t i_pts )
388 {
389     sout_buffer_t       *p_og, *p_og_first = NULL;
390     ogg_page            og;
391
392     for( ;; )
393     {
394         /* flush all data */
395         int i_result;
396         int i_size;
397         if( ( i_result = ogg_stream_flush( p_os, &og ) ) == 0 )
398         {
399             break;
400         }
401         i_size = og.header_len + og.body_len;
402         p_og = sout_BufferNew( p_sout, i_size);
403
404         memcpy( p_og->p_buffer,
405                 og.header,
406                 og.header_len );
407         memcpy( p_og->p_buffer + og.header_len,
408                 og.body,
409                 og.body_len );
410         p_og->i_size    = i_size;
411         p_og->i_dts     = 0;
412         p_og->i_pts     = i_pts;
413         p_og->i_length  = 0;
414
415         i_pts   = 0; // write it only once
416
417         sout_BufferChain( &p_og_first, p_og );
418     }
419
420     return( p_og_first );
421 }
422 static sout_buffer_t *OggStreamPageOut( sout_instance_t *p_sout,
423                                         ogg_stream_state *p_os,
424                                         mtime_t i_pts )
425 {
426     sout_buffer_t       *p_og, *p_og_first = NULL;
427     ogg_page            og;
428
429     for( ;; )
430     {
431         /* flush all data */
432         int i_result;
433         int i_size;
434         if( ( i_result = ogg_stream_pageout( p_os, &og ) ) == 0 )
435         {
436             break;
437         }
438         i_size = og.header_len + og.body_len;
439         p_og = sout_BufferNew( p_sout, i_size);
440
441         memcpy( p_og->p_buffer,
442                 og.header,
443                 og.header_len );
444         memcpy( p_og->p_buffer + og.header_len,
445                 og.body,
446                 og.body_len );
447         p_og->i_size    = i_size;
448         p_og->i_dts     = 0;
449         p_og->i_pts     = i_pts;
450         p_og->i_length  = 0;
451
452         i_pts   = 0; // write them only once
453
454         sout_BufferChain( &p_og_first, p_og );
455     }
456
457     return( p_og_first );
458 }
459 static sout_buffer_t *OggCreateHeader( sout_instance_t *p_sout, mtime_t i_dts )
460 {
461     sout_buffer_t *p_hdr = NULL;
462     sout_buffer_t *p_og;
463     ogg_packet    op;
464     int i;
465
466     /* write header for each stream */
467     for( i = 0; i < p_sout->i_nb_inputs; i++ )
468     {
469         ogg_stream_t *p_stream;
470
471         p_stream = (ogg_stream_t*)p_sout->pp_inputs[i]->p_mux_data;
472
473         if( p_stream->i_fourcc == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
474         {
475             /* special case */
476         }
477         else
478         {
479             /* ds header */
480 #if 0
481             uint8_t com[128];
482             int     i_com;
483 #endif
484
485             /* header */
486             op.packet = (uint8_t*)&p_stream->header;
487             op.bytes  = sizeof( ogg_stream_t );
488             op.b_o_s  = 1;
489             op.e_o_s  = 0;
490             op.granulepos = 0;
491             op.packetno = p_stream->i_packet_no++;
492             ogg_stream_packetin( &p_stream->os, &op );
493 #if 0
494             /* I don't know why but this produce broken file */
495             /* comment */
496             com[0] = PACKET_TYPE_COMMENT;
497             i_com = snprintf( &com[1], 128, "VLC 0.5.x stream output" ) + 1;
498             op.packet = com;
499             op.bytes  = i_com;
500             op.b_o_s  = 0;
501             op.e_o_s  = 0;
502             op.granulepos = 0;
503             op.packetno = p_stream->i_packet_no++;
504             ogg_stream_packetin( &p_stream->os, &op );
505 #endif
506         }
507
508         p_og = OggStreamFlush( p_sout, &p_stream->os, 0 );
509         sout_BufferChain( &p_hdr, p_og );
510     }
511
512     return( p_hdr );
513 }
514
515
516 static void OggSetDate( sout_buffer_t *p_og, mtime_t i_dts, mtime_t i_length )
517 {
518     int i_count;
519     sout_buffer_t *p_tmp;
520     mtime_t i_delta;
521
522     for( p_tmp = p_og, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->p_next )
523     {
524         i_count++;
525     }
526     i_delta = i_length / i_count;
527
528     for( p_tmp = p_og; p_tmp != NULL; p_tmp = p_tmp->p_next )
529     {
530         p_tmp->i_dts    = i_dts;
531         p_tmp->i_length = i_delta;
532
533         i_dts += i_delta;
534     }
535 }
536
537 static int Mux      ( sout_instance_t *p_sout )
538 {
539     sout_mux_t          *p_mux  = (sout_mux_t*)p_sout->p_mux_data;
540     sout_buffer_t       *p_og = NULL;
541     int i_stream;
542     mtime_t             i_dts;
543
544     if( p_mux->b_write_header )
545     {
546         if( MuxGetStream( p_sout, &i_stream, &i_dts) < 0 )
547         {
548             return( VLC_SUCCESS );
549         }
550         sout_BufferChain( &p_og, OggCreateHeader( p_sout, i_dts ) );
551         p_mux->b_write_header = VLC_FALSE;
552     }
553
554     for( ;; )
555     {
556         sout_input_t    *p_input;
557         ogg_stream_t    *p_stream;
558         sout_buffer_t   *p_data;
559         ogg_packet          op;
560
561         if( MuxGetStream( p_sout, &i_stream, &i_dts) < 0 )
562         {
563             return( VLC_SUCCESS );
564         }
565
566         p_input  = p_sout->pp_inputs[i_stream];
567         p_stream = (ogg_stream_t*)p_input->p_mux_data;
568
569         p_data  = sout_FifoGet( p_input->p_fifo );
570
571         sout_BufferReallocFromPreHeader( p_sout, p_data, 1 );
572         p_data->p_buffer[0] = PACKET_IS_SYNCPOINT;      // FIXME
573
574         op.packet = p_data->p_buffer;
575         op.bytes  = p_data->i_size;
576         op.b_o_s  = 0;
577         op.e_o_s  = 0;
578         if( p_stream->i_cat == AUDIO_ES )
579         {
580             /* number of sample from begining */
581             op.granulepos = i_dts * p_stream->header.i_samples_per_unit / (int64_t)1000000;
582         }
583         else if( p_stream->i_cat == VIDEO_ES )
584         {
585             op.granulepos = i_dts/1000;//p_stream->i_packet_no;
586         }
587         op.packetno = p_stream->i_packet_no++;
588         ogg_stream_packetin( &p_stream->os, &op );
589
590         sout_BufferChain( &p_og,
591                           OggStreamPageOut( p_sout,
592                                             &p_stream->os,
593                                             p_data->i_pts ) );
594
595         if( p_og )
596         {
597             OggSetDate( p_og, p_stream->i_dts, p_stream->i_length );
598             p_stream->i_dts = -1;
599             p_stream->i_length = 0;;
600             sout_AccessOutWrite( p_sout->p_access, p_og );
601
602             p_og = NULL;
603         }
604         else
605         {
606             if( p_stream->i_dts < 0 )
607             {
608                 p_stream->i_dts     = p_data->i_dts;
609             }
610             p_stream->i_length += p_data->i_length;
611         }
612
613         sout_BufferDelete( p_sout, p_data );
614     }
615
616     return( VLC_SUCCESS );
617 }
618