* all: new sout scheme. Now a chain of module are created that can
[vlc.git] / modules / mux / avi.c
1 /*****************************************************************************
2  * avi.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: avi.c,v 1.12 2003/04/13 20:00:21 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 /* TODO: add OpenDML write support */
28
29 #include <stdlib.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <fcntl.h>
35
36 #include <vlc/vlc.h>
37 #include <vlc/input.h>
38 #include <vlc/sout.h>
39
40 #ifdef HAVE_UNISTD_H
41 #   include <unistd.h>
42 #endif
43
44 #include "codecs.h"
45
46
47 #define AVIF_HASINDEX       0x00000010  // Index at end of file?
48 #define AVIF_MUSTUSEINDEX   0x00000020
49 #define AVIF_ISINTERLEAVED  0x00000100
50 #define AVIF_TRUSTCKTYPE    0x00000800  // Use CKType to find key frames?
51 #define AVIF_WASCAPTUREFILE 0x00010000
52 #define AVIF_COPYRIGHTED    0x00020000
53
54 /* Flags for index */
55 #define AVIIF_LIST          0x00000001L /* chunk is a 'LIST' */
56 #define AVIIF_KEYFRAME      0x00000010L /* this frame is a key frame.*/
57 #define AVIIF_NOTIME        0x00000100L /* this frame doesn't take any time */
58 #define AVIIF_COMPUSE       0x0FFF0000L /* these bits are for compressor use */
59
60 /*****************************************************************************
61  * Exported prototypes
62  *****************************************************************************/
63 static int     Open   ( vlc_object_t * );
64 static void    Close  ( vlc_object_t * );
65
66 static int Capability(sout_mux_t *, int , void *, void * );
67 static int AddStream( sout_mux_t *, sout_input_t * );
68 static int DelStream( sout_mux_t *, sout_input_t * );
69 static int Mux      ( sout_mux_t * );
70
71 static sout_buffer_t *avi_HeaderCreateRIFF( sout_mux_t * );
72 static sout_buffer_t *avi_HeaderCreateidx1( sout_mux_t * );
73
74 static void SetFCC( uint8_t *p, char *fcc )
75 {
76     p[0] = fcc[0];
77     p[1] = fcc[1];
78     p[2] = fcc[2];
79     p[3] = fcc[3];
80 }
81
82 static void SetDWLE( uint8_t *p, uint32_t i_dw )
83 {
84     p[3] = ( i_dw >> 24 )&0xff;
85     p[2] = ( i_dw >> 16 )&0xff;
86     p[1] = ( i_dw >>  8 )&0xff;
87     p[0] = ( i_dw       )&0xff;
88 }
89
90 /*****************************************************************************
91  * Module descriptor
92  *****************************************************************************/
93 vlc_module_begin();
94     set_description( _("Avi muxer") );
95     set_capability( "sout mux", 5 );
96     add_shortcut( "avi" );
97     set_callbacks( Open, Close );
98 vlc_module_end();
99
100 // FIXME FIXME
101 #define HDR_SIZE 10240
102
103 typedef struct avi_stream_s
104 {
105     int i_cat;
106
107     char fcc[4];
108
109     //mtime_t i_first_pts;
110     mtime_t i_duration;       // in ┬Ás
111
112     int     i_frames;        // total frame count
113     int64_t i_totalsize;    // total stream size
114
115     float   f_fps;
116     int     i_bitrate;
117
118     BITMAPINFOHEADER    *p_bih;
119     WAVEFORMATEX        *p_wf;
120
121 } avi_stream_t;
122
123 typedef struct avi_idx1_entry_s
124 {
125     char     fcc[4];
126     uint32_t i_flags;
127     uint32_t i_pos;
128     uint32_t i_length;
129
130 } avi_idx1_entry_t;
131
132 typedef struct avi_idx1_s
133 {
134     unsigned int i_entry_count;
135     unsigned int i_entry_max;
136
137     avi_idx1_entry_t *entry;
138 } avi_idx1_t;
139
140 struct sout_mux_sys_t
141 {
142     vlc_bool_t b_write_header;
143
144     int i_streams;
145     int i_stream_video;
146
147     off_t i_movi_size;
148     avi_stream_t stream[100];
149
150     avi_idx1_t idx1;
151     off_t i_idx1_size;
152
153 };
154
155 /*****************************************************************************
156  * Open:
157  *****************************************************************************/
158 static int Open( vlc_object_t *p_this )
159 {
160     sout_mux_t      *p_mux = (sout_mux_t*)p_this;
161     sout_mux_sys_t  *p_sys = p_mux->p_sys;
162     //sout_buffer_t   *p_hdr;
163
164     p_sys = malloc( sizeof( sout_mux_sys_t ) );
165     p_sys->i_streams = 0;
166     p_sys->i_stream_video = -1;
167     p_sys->i_movi_size = 0;
168
169     p_sys->idx1.i_entry_count = 0;
170     p_sys->idx1.i_entry_max = 10000;
171     p_sys->idx1.entry = calloc( p_sys->idx1.i_entry_max, sizeof( avi_idx1_entry_t ) );
172     p_sys->b_write_header = VLC_TRUE;
173
174     msg_Info( p_mux, "Open" );
175
176     p_mux->pf_capacity  = Capability;
177     p_mux->pf_addstream = AddStream;
178     p_mux->pf_delstream = DelStream;
179     p_mux->pf_mux       = Mux;
180     p_mux->p_sys        = p_sys;
181     p_mux->i_preheader  = 8; // (fourcc,length) header
182
183 #if 0
184     /* room to add header at the end */
185     p_hdr = sout_BufferNew( p_mux->p_sout, HDR_SIZE );
186     memset( p_hdr->p_buffer, 0, HDR_SIZE );
187     sout_AccessOutWrite( p_mux->p_access, p_hdr );
188 #endif
189
190     return VLC_SUCCESS;
191 }
192
193 /*****************************************************************************
194  * Close:
195  *****************************************************************************/
196
197 static void Close( vlc_object_t * p_this )
198 {
199     sout_mux_t      *p_mux = (sout_mux_t*)p_this;
200     sout_mux_sys_t  *p_sys = p_mux->p_sys;
201
202     sout_buffer_t       *p_hdr, *p_idx1;
203     int                 i_stream;
204
205     msg_Info( p_mux, "Close" );
206
207     /* first create idx1 chunk (write at the end of the stream */
208     p_idx1 = avi_HeaderCreateidx1( p_mux );
209     p_sys->i_idx1_size = p_idx1->i_size;
210     sout_AccessOutWrite( p_mux->p_access, p_idx1 );
211
212     /* calculate some value for headers creations */
213     for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
214     {
215         avi_stream_t *p_stream;
216
217         p_stream = &p_sys->stream[i_stream];
218
219         p_stream->f_fps = 25;
220         if( p_stream->i_duration > 0 )
221         {
222             p_stream->f_fps = (float)p_stream->i_frames /
223                               ( (float)p_stream->i_duration /
224                                 (float)1000000 );
225         }
226         p_stream->i_bitrate = 128 * 1024;
227         if( p_stream->i_duration > 0 )
228         {
229             p_stream->i_bitrate =
230                 8 * (uint64_t)1000000 *
231                     (uint64_t)p_stream->i_totalsize /
232                     (uint64_t)p_stream->i_duration;
233         }
234         msg_Info( p_mux, "stream[%d] duration:%lld totalsize:%lld frames:%d fps:%f kb/s:%d",
235                   i_stream,
236                   (int64_t)p_stream->i_duration / (int64_t)1000000,
237                   p_stream->i_totalsize,
238                   p_stream->i_frames,
239                   p_stream->f_fps, p_stream->i_bitrate/1024 );
240     }
241
242     p_hdr = avi_HeaderCreateRIFF( p_mux );
243     sout_AccessOutSeek( p_mux->p_access, 0 );
244     sout_AccessOutWrite( p_mux->p_access, p_hdr );
245 }
246
247 static int Capability( sout_mux_t *p_mux, int i_query, void *p_args, void *p_answer )
248 {
249    switch( i_query )
250    {
251         case SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME:
252             *(vlc_bool_t*)p_answer = VLC_FALSE;
253             return( SOUT_MUX_CAP_ERR_OK );
254         default:
255             return( SOUT_MUX_CAP_ERR_UNIMPLEMENTED );
256    }
257 }
258
259 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
260 {
261     sout_mux_sys_t  *p_sys = p_mux->p_sys;
262     avi_stream_t    *p_stream;
263
264     if( p_sys->i_streams >= 100 )
265     {
266         msg_Err( p_mux, "too many streams" );
267         return( -1 );
268     }
269
270     msg_Dbg( p_mux, "adding input" );
271     p_input->p_sys = malloc( sizeof( int ) );
272
273     *((int*)p_input->p_sys) = p_sys->i_streams;
274     p_stream = &p_sys->stream[p_sys->i_streams];
275
276     switch( p_input->p_fmt->i_cat )
277     {
278         case AUDIO_ES:
279             p_stream->i_cat = AUDIO_ES;
280             p_stream->fcc[0] = '0' + p_sys->i_streams / 10;
281             p_stream->fcc[1] = '0' + p_sys->i_streams % 10;
282             p_stream->fcc[2] = 'w';
283             p_stream->fcc[3] = 'b';
284
285             p_stream->p_bih = NULL;
286
287             p_stream->p_wf  = malloc( sizeof( WAVEFORMATEX ) + p_input->p_fmt->i_extra_data );
288 #define p_wf p_stream->p_wf
289             p_wf->cbSize = p_input->p_fmt->i_extra_data;
290             if( p_wf->cbSize > 0 )
291             {
292                 memcpy( &p_wf[1],
293                         p_input->p_fmt->p_extra_data,
294                         p_input->p_fmt->i_extra_data );
295             }
296             p_wf->nChannels      = p_input->p_fmt->i_channels;
297             p_wf->nSamplesPerSec = p_input->p_fmt->i_sample_rate;
298             p_wf->nBlockAlign    = p_input->p_fmt->i_block_align;
299             p_wf->nAvgBytesPerSec= p_input->p_fmt->i_bitrate / 8;
300             p_wf->wBitsPerSample = 0;
301
302             switch( p_input->p_fmt->i_fourcc )
303             {
304                 case VLC_FOURCC( 'a', '5', '2', ' ' ):
305                     p_wf->wFormatTag = WAVE_FORMAT_A52;
306                     break;
307                 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
308                     p_wf->wFormatTag = WAVE_FORMAT_MPEGLAYER3;
309                     break;
310                 case VLC_FOURCC( 'w', 'm', 'a', '1' ):
311                     p_wf->wFormatTag = WAVE_FORMAT_WMA1;
312                     break;
313                 case VLC_FOURCC( 'w', 'm', 'a', '2' ):
314                     p_wf->wFormatTag = WAVE_FORMAT_WMA2;
315                     break;
316                 case VLC_FOURCC( 'w', 'm', 'a', '3' ):
317                     p_wf->wFormatTag = WAVE_FORMAT_WMA3;
318                     break;
319                 default:
320                     return VLC_EGENERIC;
321             }
322 #undef p_wf
323             break;
324         case VIDEO_ES:
325             p_stream->i_cat = VIDEO_ES;
326             p_stream->fcc[0] = '0' + p_sys->i_streams / 10;
327             p_stream->fcc[1] = '0' + p_sys->i_streams % 10;
328             p_stream->fcc[2] = 'd';
329             p_stream->fcc[3] = 'c';
330             if( p_sys->i_stream_video < 0 )
331             {
332                 p_sys->i_stream_video = p_sys->i_streams;
333             }
334             p_stream->p_wf  = NULL;
335             p_stream->p_bih = malloc( sizeof( BITMAPINFOHEADER ) + p_input->p_fmt->i_extra_data );
336 #define p_bih p_stream->p_bih
337             p_bih->biSize  = sizeof( BITMAPINFOHEADER ) + p_input->p_fmt->i_extra_data;
338             if( p_input->p_fmt->i_extra_data > 0 )
339             {
340                 memcpy( &p_bih[1],
341                         p_input->p_fmt->p_extra_data,
342                         p_input->p_fmt->i_extra_data );
343             }
344             p_bih->biWidth = p_input->p_fmt->i_width;
345             p_bih->biHeight= p_input->p_fmt->i_height;
346             p_bih->biPlanes= 1;
347             p_bih->biBitCount       = 24;
348             p_bih->biSizeImage      = 0;
349             p_bih->biXPelsPerMeter  = 0;
350             p_bih->biYPelsPerMeter  = 0;
351             p_bih->biClrUsed        = 0;
352             p_bih->biClrImportant   = 0;
353             switch( p_input->p_fmt->i_fourcc )
354             {
355                 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
356                     p_bih->biCompression = VLC_FOURCC( 'X', 'V', 'I', 'D' );
357                     break;
358                 default:
359                     p_bih->biCompression = p_input->p_fmt->i_fourcc;
360                     break;
361             }
362 #undef p_bih
363             break;
364         default:
365             return( VLC_EGENERIC );
366     }
367     p_stream->i_totalsize = 0;
368     p_stream->i_frames    = 0;
369     p_stream->i_duration  = 0;
370     //p_stream->i_first_pts = 0;
371
372     /* fixed later */
373     p_stream->f_fps = 25;
374     p_stream->i_bitrate = 128 * 1024;
375
376
377     p_sys->i_streams++;
378     return( VLC_SUCCESS );
379 }
380
381 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
382 {
383
384     msg_Dbg( p_mux, "removing input" );
385
386     free( p_input->p_sys ); p_input->p_sys = NULL;
387
388     return( 0 );
389 }
390
391 static int Mux      ( sout_mux_t *p_mux )
392 {
393     sout_mux_sys_t  *p_sys = p_mux->p_sys;
394     avi_stream_t    *p_stream;
395     int i_stream;
396     int i;
397
398     if( p_sys->b_write_header )
399     {
400         sout_buffer_t *p_hdr;
401
402         msg_Dbg( p_mux, "writing header" );
403
404         p_hdr = avi_HeaderCreateRIFF( p_mux );
405         sout_AccessOutWrite( p_mux->p_access, p_hdr );
406
407         p_sys->b_write_header = VLC_FALSE;
408     }
409
410     for( i = 0; i < p_mux->i_nb_inputs; i++ )
411     {
412         int i_count;
413         sout_fifo_t *p_fifo;
414
415         i_stream = *((int*)p_mux->pp_inputs[i]->p_sys );
416         p_stream = &p_sys->stream[i_stream];
417
418         p_fifo = p_mux->pp_inputs[i]->p_fifo;
419         i_count = p_fifo->i_depth;
420         while( i_count > 0 )
421         {
422             avi_idx1_entry_t *p_idx;
423             sout_buffer_t *p_data;
424
425             p_data = sout_FifoGet( p_fifo );
426
427             p_stream->i_frames++;
428             if( p_data->i_length < 0 )
429             {
430                 msg_Warn( p_mux, "argg length < 0 l" );
431                 p_data->i_length = 0;
432             }
433             p_stream->i_duration  += p_data->i_length;
434             p_stream->i_totalsize += p_data->i_size;
435
436             /* add idx1 entry for this frame */
437             p_idx = &p_sys->idx1.entry[p_sys->idx1.i_entry_count];
438             memcpy( p_idx->fcc, p_stream->fcc, 4 );
439             p_idx->i_flags = AVIIF_KEYFRAME;
440             p_idx->i_pos   = p_sys->i_movi_size + 4;
441             p_idx->i_length= p_data->i_size;
442             p_sys->idx1.i_entry_count++;
443             if( p_sys->idx1.i_entry_count >= p_sys->idx1.i_entry_max )
444             {
445                 p_sys->idx1.i_entry_max += 10000;
446                 p_sys->idx1.entry = realloc( p_sys->idx1.entry,
447                                              p_sys->idx1.i_entry_max * sizeof( avi_idx1_entry_t ) );
448             }
449
450
451             if( sout_BufferReallocFromPreHeader( p_mux->p_sout, p_data, 8 ) )
452             {
453                 /* there isn't enough data in preheader */
454                 sout_buffer_t *p_hdr;
455
456                 p_hdr = sout_BufferNew( p_mux->p_sout, 8 );
457                 SetFCC( p_hdr->p_buffer, p_stream->fcc );
458                 SetDWLE( p_hdr->p_buffer + 4, p_data->i_size );
459
460                 sout_AccessOutWrite( p_mux->p_access, p_hdr );
461                 p_sys->i_movi_size += p_hdr->i_size;
462
463             }
464             else
465             {
466                 SetFCC( p_data->p_buffer, p_stream->fcc );
467                 SetDWLE( p_data->p_buffer + 4, p_data->i_size - 8 );
468             }
469
470             if( p_data->i_size & 0x01 )
471             {
472                 sout_BufferRealloc( p_mux->p_sout, p_data, p_data->i_size + 1 );
473                 p_data->i_size += 1;
474             }
475
476             p_sys->i_movi_size += p_data->i_size;
477             sout_AccessOutWrite( p_mux->p_access, p_data );
478
479             i_count--;
480         }
481
482     }
483     return( 0 );
484 }
485
486 /****************************************************************************/
487 /****************************************************************************/
488 /****************************************************************************/
489 /****************************************************************************/
490
491 typedef struct buffer_out_s
492 {
493     int      i_buffer_size;
494     int      i_buffer;
495     uint8_t  *p_buffer;
496
497 } buffer_out_t;
498
499 static void bo_Init( buffer_out_t *p_bo, int i_size, uint8_t *p_buffer )
500 {
501     p_bo->i_buffer_size = i_size;
502     p_bo->i_buffer = 0;
503     p_bo->p_buffer = p_buffer;
504 }
505 static void bo_AddByte( buffer_out_t *p_bo, uint8_t i )
506 {
507     if( p_bo->i_buffer < p_bo->i_buffer_size )
508     {
509         p_bo->p_buffer[p_bo->i_buffer] = i;
510     }
511     p_bo->i_buffer++;
512 }
513 static void bo_AddWordLE( buffer_out_t *p_bo, uint16_t i )
514 {
515     bo_AddByte( p_bo, i &0xff );
516     bo_AddByte( p_bo, ( ( i >> 8) &0xff ) );
517 }
518 static void bo_AddWordBE( buffer_out_t *p_bo, uint16_t i )
519 {
520     bo_AddByte( p_bo, ( ( i >> 8) &0xff ) );
521     bo_AddByte( p_bo, i &0xff );
522 }
523 static void bo_AddDWordLE( buffer_out_t *p_bo, uint32_t i )
524 {
525     bo_AddWordLE( p_bo, i &0xffff );
526     bo_AddWordLE( p_bo, ( ( i >> 16) &0xffff ) );
527 }
528 static void bo_AddDWordBE( buffer_out_t *p_bo, uint32_t i )
529 {
530     bo_AddWordBE( p_bo, ( ( i >> 16) &0xffff ) );
531     bo_AddWordBE( p_bo, i &0xffff );
532 }
533 #if 0
534 static void bo_AddLWordLE( buffer_out_t *p_bo, uint64_t i )
535 {
536     bo_AddDWordLE( p_bo, i &0xffffffff );
537     bo_AddDWordLE( p_bo, ( ( i >> 32) &0xffffffff ) );
538 }
539 static void bo_AddLWordBE( buffer_out_t *p_bo, uint64_t i )
540 {
541     bo_AddDWordBE( p_bo, ( ( i >> 32) &0xffffffff ) );
542     bo_AddDWordBE( p_bo, i &0xffffffff );
543 }
544 #endif
545
546 static void bo_AddFCC( buffer_out_t *p_bo, char *fcc )
547 {
548     bo_AddByte( p_bo, fcc[0] );
549     bo_AddByte( p_bo, fcc[1] );
550     bo_AddByte( p_bo, fcc[2] );
551     bo_AddByte( p_bo, fcc[3] );
552 }
553
554 static void bo_AddMem( buffer_out_t *p_bo, int i_size, uint8_t *p_mem )
555 {
556     int i;
557
558     for( i = 0; i < i_size; i++ )
559     {
560         bo_AddByte( p_bo, p_mem[i] );
561     }
562 }
563
564 /****************************************************************************
565  ****************************************************************************
566  **
567  ** avi header generation
568  **
569  ****************************************************************************
570  ****************************************************************************/
571 #define AVI_BOX_ENTER( fcc ) \
572     buffer_out_t _bo_sav_; \
573     bo_AddFCC( p_bo, fcc ); \
574     _bo_sav_ = *p_bo; \
575     bo_AddDWordLE( p_bo, 0 )
576
577 #define AVI_BOX_ENTER_LIST( fcc ) \
578     AVI_BOX_ENTER( "LIST" ); \
579     bo_AddFCC( p_bo, fcc )
580
581 #define AVI_BOX_EXIT( i_err ) \
582     if( p_bo->i_buffer&0x01 ) bo_AddByte( p_bo, 0 ); \
583     bo_AddDWordLE( &_bo_sav_, p_bo->i_buffer - _bo_sav_.i_buffer - 4 ); \
584     return( i_err );
585
586 static int avi_HeaderAdd_avih( sout_mux_t *p_mux,
587                                buffer_out_t *p_bo )
588 {
589     sout_mux_sys_t  *p_sys = p_mux->p_sys;
590     avi_stream_t    *p_video = NULL;
591     int         i_stream;
592     uint32_t    i_microsecperframe;
593     int         i_maxbytespersec;
594     int         i_totalframes;
595     AVI_BOX_ENTER( "avih" );
596
597     if( p_sys->i_stream_video >= 0 )
598     {
599         p_video = &p_sys->stream[p_sys->i_stream_video];
600         if( p_video->i_frames <= 0 )
601         {
602         //    p_video = NULL;
603         }
604     }
605
606     if( p_video )
607     {
608         i_microsecperframe =
609             (uint32_t)( (float)1000000 /
610                         (float)p_sys->stream[p_sys->i_stream_video].f_fps );
611         i_totalframes = p_sys->stream[p_sys->i_stream_video].i_frames;
612     }
613     else
614     {
615         msg_Warn( p_mux, "avi file without video track isn't a good idea..." );
616         i_microsecperframe = 0;
617         i_totalframes = 0;
618     }
619
620     for( i_stream = 0,i_maxbytespersec = 0; i_stream < p_sys->i_streams; i_stream++ )
621     {
622         if( p_sys->stream[i_stream].i_duration > 0 )
623         {
624             i_maxbytespersec +=
625                 p_sys->stream[p_sys->i_stream_video].i_totalsize /
626                 p_sys->stream[p_sys->i_stream_video].i_duration;
627         }
628     }
629
630     bo_AddDWordLE( p_bo, i_microsecperframe );
631     bo_AddDWordLE( p_bo, i_maxbytespersec );
632     bo_AddDWordLE( p_bo, 0 );                   /* padding */
633     bo_AddDWordLE( p_bo, AVIF_TRUSTCKTYPE |
634                          AVIF_HASINDEX |
635                          AVIF_ISINTERLEAVED );  /* flags */
636     bo_AddDWordLE( p_bo, i_totalframes );
637     bo_AddDWordLE( p_bo, 0 );                   /* initial frame */
638     bo_AddDWordLE( p_bo, p_sys->i_streams );    /* streams count */
639     bo_AddDWordLE( p_bo, 1024 * 1024 );         /* suggested buffer size */
640     if( p_video )
641     {
642         bo_AddDWordLE( p_bo, p_video->p_bih->biWidth );
643         bo_AddDWordLE( p_bo, p_video->p_bih->biHeight );
644     }
645     else
646     {
647         bo_AddDWordLE( p_bo, 0 );
648         bo_AddDWordLE( p_bo, 0 );
649     }
650     bo_AddDWordLE( p_bo, 0 );                   /* ???? */
651     bo_AddDWordLE( p_bo, 0 );                   /* ???? */
652     bo_AddDWordLE( p_bo, 0 );                   /* ???? */
653     bo_AddDWordLE( p_bo, 0 );                   /* ???? */
654
655     AVI_BOX_EXIT( 0 );
656 }
657 static int avi_HeaderAdd_strh( sout_mux_t   *p_mux,
658                                buffer_out_t *p_bo,
659                                avi_stream_t *p_stream )
660 {
661     AVI_BOX_ENTER( "strh" );
662
663     switch( p_stream->i_cat )
664     {
665         case VIDEO_ES:
666             {
667                 bo_AddFCC( p_bo, "vids" );
668                 bo_AddDWordBE( p_bo, p_stream->p_bih->biCompression );
669                 bo_AddDWordLE( p_bo, 0 );   /* flags */
670                 bo_AddWordLE(  p_bo, 0 );   /* priority */
671                 bo_AddWordLE(  p_bo, 0 );   /* langage */
672                 bo_AddDWordLE( p_bo, 0 );   /* initial frame */
673                 bo_AddDWordLE( p_bo, 1000 );/* scale */
674                 bo_AddDWordLE( p_bo, (uint32_t)( 1000 * p_stream->f_fps ));
675                 bo_AddDWordLE( p_bo, 0 );   /* start */
676                 bo_AddDWordLE( p_bo, p_stream->i_frames );
677                 bo_AddDWordLE( p_bo, 1024 * 1024 );
678                 bo_AddDWordLE( p_bo, -1 );  /* quality */
679                 bo_AddDWordLE( p_bo, 0 );   /* samplesize */
680                 bo_AddWordLE(  p_bo, 0 );   /* ??? */
681                 bo_AddWordLE(  p_bo, 0 );   /* ??? */
682                 bo_AddWordLE(  p_bo, p_stream->p_bih->biWidth );
683                 bo_AddWordLE(  p_bo, p_stream->p_bih->biHeight );
684             }
685             break;
686         case AUDIO_ES:
687             {
688                 int i_rate, i_scale, i_samplesize;
689
690                 i_samplesize = p_stream->p_wf->nBlockAlign;
691                 if( i_samplesize > 1 )
692                 {
693                     i_scale = i_samplesize;
694                     i_rate = /*i_scale **/ p_stream->i_bitrate / 8;
695                 }
696                 else
697                 {
698                     i_samplesize = 1;
699                     i_scale = 1000;
700                     i_rate = 1000 * p_stream->i_bitrate / 8;
701                 }
702                 bo_AddFCC( p_bo, "auds" );
703                 bo_AddDWordLE( p_bo, 0 );   /* tag */
704                 bo_AddDWordLE( p_bo, 0 );   /* flags */
705                 bo_AddWordLE(  p_bo, 0 );   /* priority */
706                 bo_AddWordLE(  p_bo, 0 );   /* langage */
707                 bo_AddDWordLE( p_bo, 0 );   /* initial frame */
708                 bo_AddDWordLE( p_bo, i_scale );/* scale */
709                 bo_AddDWordLE( p_bo, i_rate );
710                 bo_AddDWordLE( p_bo, 0 );   /* start */
711                 bo_AddDWordLE( p_bo, p_stream->i_frames );
712                 bo_AddDWordLE( p_bo, 10 * 1024 );
713                 bo_AddDWordLE( p_bo, -1 );  /* quality */
714                 bo_AddDWordLE( p_bo, i_samplesize );
715                 bo_AddWordLE(  p_bo, 0 );   /* ??? */
716                 bo_AddWordLE(  p_bo, 0 );   /* ??? */
717                 bo_AddWordLE(  p_bo, 0 );
718                 bo_AddWordLE(  p_bo, 0 );
719             }
720             break;
721     }
722
723     AVI_BOX_EXIT( 0 );
724 }
725
726 static int avi_HeaderAdd_strf( sout_mux_t *p_mux,
727                                buffer_out_t *p_bo,
728                                avi_stream_t *p_stream )
729 {
730     AVI_BOX_ENTER( "strf" );
731
732     switch( p_stream->i_cat )
733     {
734         case AUDIO_ES:
735             bo_AddWordLE( p_bo, p_stream->p_wf->wFormatTag );
736             bo_AddWordLE( p_bo, p_stream->p_wf->nChannels );
737             bo_AddDWordLE( p_bo, p_stream->p_wf->nSamplesPerSec );
738             bo_AddDWordLE( p_bo, p_stream->p_wf->nAvgBytesPerSec );
739             bo_AddWordLE( p_bo, p_stream->p_wf->nBlockAlign );
740             bo_AddWordLE( p_bo, p_stream->p_wf->wBitsPerSample );
741             bo_AddWordLE( p_bo, p_stream->p_wf->cbSize );
742             bo_AddMem( p_bo, p_stream->p_wf->cbSize, (uint8_t*)&p_stream->p_wf[1] );
743             break;
744         case VIDEO_ES:
745             bo_AddDWordLE( p_bo, p_stream->p_bih->biSize );
746             bo_AddDWordLE( p_bo, p_stream->p_bih->biWidth );
747             bo_AddDWordLE( p_bo, p_stream->p_bih->biHeight );
748             bo_AddWordLE( p_bo, p_stream->p_bih->biPlanes );
749             bo_AddWordLE( p_bo, p_stream->p_bih->biBitCount );
750             if( VLC_FOURCC( 0, 0, 0, 1 ) == 0x00000001 )
751             {
752                 bo_AddDWordBE( p_bo, p_stream->p_bih->biCompression );
753             }
754             else
755             {
756                 bo_AddDWordLE( p_bo, p_stream->p_bih->biCompression );
757             }
758             bo_AddDWordLE( p_bo, p_stream->p_bih->biSizeImage );
759             bo_AddDWordLE( p_bo, p_stream->p_bih->biXPelsPerMeter );
760             bo_AddDWordLE( p_bo, p_stream->p_bih->biYPelsPerMeter );
761             bo_AddDWordLE( p_bo, p_stream->p_bih->biClrUsed );
762             bo_AddDWordLE( p_bo, p_stream->p_bih->biClrImportant );
763             bo_AddMem( p_bo,
764                        p_stream->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
765                        (uint8_t*)&p_stream->p_bih[1] );
766             break;
767     }
768
769     AVI_BOX_EXIT( 0 );
770 }
771
772 static int avi_HeaderAdd_strl( sout_mux_t *p_mux,
773                                buffer_out_t *p_bo,
774                                avi_stream_t *p_stream )
775 {
776     AVI_BOX_ENTER_LIST( "strl" );
777
778     avi_HeaderAdd_strh( p_mux, p_bo, p_stream );
779     avi_HeaderAdd_strf( p_mux, p_bo, p_stream );
780
781     AVI_BOX_EXIT( 0 );
782 }
783
784 static sout_buffer_t *avi_HeaderCreateRIFF( sout_mux_t *p_mux )
785 {
786     sout_mux_sys_t      *p_sys = p_mux->p_sys;
787     sout_buffer_t       *p_hdr;
788     int                 i_stream;
789     int                 i_maxbytespersec;
790     int                 i_junk;
791     buffer_out_t        bo;
792
793     p_hdr = sout_BufferNew( p_mux->p_sout, HDR_SIZE );
794     memset( p_hdr->p_buffer, 0, HDR_SIZE );
795
796     bo_Init( &bo, HDR_SIZE, p_hdr->p_buffer );
797
798     bo_AddFCC( &bo, "RIFF" );
799     bo_AddDWordLE( &bo, p_sys->i_movi_size + HDR_SIZE - 8 + p_sys->i_idx1_size );
800     bo_AddFCC( &bo, "AVI " );
801
802     bo_AddFCC( &bo, "LIST" );
803     bo_AddDWordLE( &bo, HDR_SIZE - 8);
804     bo_AddFCC( &bo, "hdrl" );
805
806     avi_HeaderAdd_avih( p_mux, &bo );
807     for( i_stream = 0,i_maxbytespersec = 0; i_stream < p_sys->i_streams; i_stream++ )
808     {
809         avi_HeaderAdd_strl( p_mux, &bo, &p_sys->stream[i_stream] );
810     }
811
812     i_junk = HDR_SIZE - bo.i_buffer - 8 - 12;
813     bo_AddFCC( &bo, "JUNK" );
814     bo_AddDWordLE( &bo, i_junk );
815
816     bo.i_buffer += i_junk;
817     bo_AddFCC( &bo, "LIST" );
818     bo_AddDWordLE( &bo, p_sys->i_movi_size + 4 );
819     bo_AddFCC( &bo, "movi" );
820
821     return( p_hdr );
822 }
823
824 static sout_buffer_t * avi_HeaderCreateidx1( sout_mux_t *p_mux )
825 {
826     sout_mux_sys_t      *p_sys = p_mux->p_sys;
827     sout_buffer_t       *p_idx1;
828     uint32_t            i_idx1_size;
829     unsigned int        i;
830     buffer_out_t        bo;
831
832     i_idx1_size = 16 * p_sys->idx1.i_entry_count;
833
834     p_idx1 = sout_BufferNew( p_mux->p_sout, i_idx1_size + 8 );
835     memset( p_idx1->p_buffer, 0, i_idx1_size );
836
837     bo_Init( &bo, i_idx1_size, p_idx1->p_buffer );
838     bo_AddFCC( &bo, "idx1" );
839     bo_AddDWordLE( &bo, i_idx1_size );
840
841     for( i = 0; i < p_sys->idx1.i_entry_count; i++ )
842     {
843         bo_AddFCC( &bo, p_sys->idx1.entry[i].fcc );
844         bo_AddDWordLE( &bo, p_sys->idx1.entry[i].i_flags );
845         bo_AddDWordLE( &bo, p_sys->idx1.entry[i].i_pos );
846         bo_AddDWordLE( &bo, p_sys->idx1.entry[i].i_length );
847     }
848
849     return( p_idx1 );
850 }
851
852