3c7c99ff8618b1d07cac869fb601634969d5da59
[vlc.git] / modules / mux / avi.c
1 /*****************************************************************************
2  * avi.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: avi.c,v 1.2 2003/01/13 02:33:13 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 <sys/types.h>
29 #include <sys/stat.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <fcntl.h>
33
34 #include <vlc/vlc.h>
35 #include <vlc/input.h>
36 #include <vlc/sout.h>
37
38 #ifdef HAVE_UNISTD_H
39 #   include <unistd.h>
40 #elif defined( _MSC_VER ) && defined( _WIN32 ) && !defined( UNDER_CE )
41 #   include <io.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 /*****************************************************************************
55  * Exported prototypes
56  *****************************************************************************/
57 static int     Open   ( vlc_object_t * );
58 static void    Close  ( vlc_object_t * );
59
60 static int AddStream( sout_instance_t *, sout_input_t * );
61 static int DelStream( sout_instance_t *, sout_input_t * );
62 static int Mux      ( sout_instance_t * );
63
64 static sout_buffer_t *avi_HeaderCreateRIFF( sout_instance_t *p_sout );
65
66 static void SetFCC( uint8_t *p, char *fcc )
67 {
68     p[0] = fcc[0];
69     p[1] = fcc[1];
70     p[2] = fcc[2];
71     p[3] = fcc[3];
72 }
73
74 static void SetDWLE( uint8_t *p, uint32_t i_dw )
75 {
76     p[3] = ( i_dw >> 24 )&0xff;
77     p[2] = ( i_dw >> 16 )&0xff;
78     p[1] = ( i_dw >>  8 )&0xff;
79     p[0] = ( i_dw       )&0xff;
80 }
81
82 /*****************************************************************************
83  * Module descriptor
84  *****************************************************************************/
85 vlc_module_begin();
86     set_description( _("Avi muxer") );
87     set_capability( "sout mux", 5 );
88     add_shortcut( "avi" );
89     set_callbacks( Open, Close );
90 vlc_module_end();
91
92 // FIXME FIXME
93 #define HDR_SIZE 10240
94
95 typedef struct avi_stream_s
96 {
97     int i_cat;
98
99     char fcc[4];
100
101     mtime_t i_duration;       // in ┬Ás
102
103     int     i_frames;        // total frame count
104     int64_t i_totalsize;    // total stream size
105
106     float   f_fps;
107     int     i_bitrate;
108
109     BITMAPINFOHEADER    *p_bih;
110     WAVEFORMATEX        *p_wf;
111
112 } avi_stream_t;
113
114 typedef struct sout_mux_s
115 {
116     int i_streams;
117     int i_stream_video;
118
119     off_t i_movi_size;
120     avi_stream_t stream[100];
121
122 } sout_mux_t;
123
124 /*****************************************************************************
125  * Open:
126  *****************************************************************************/
127 static int Open( vlc_object_t *p_this )
128 {
129     sout_instance_t     *p_sout = (sout_instance_t*)p_this;
130     sout_mux_t          *p_mux;
131     sout_buffer_t       *p_hdr;
132
133     p_mux = malloc( sizeof( sout_mux_t ) );
134     p_mux->i_streams = 0;
135     p_mux->i_stream_video = -1;
136     p_mux->i_movi_size = 0;
137
138     msg_Info( p_sout, "Open" );
139
140     p_sout->pf_mux_addstream = AddStream;
141     p_sout->pf_mux_delstream = DelStream;
142     p_sout->pf_mux           = Mux;
143     p_sout->p_mux_data = (void*)p_mux;
144     p_sout->i_mux_preheader  = 8; // (fourcc,length) header
145
146     /* room to add header at the end */
147     p_hdr = sout_BufferNew( p_sout, HDR_SIZE );
148     memset( p_hdr->p_buffer, 0, HDR_SIZE );
149     p_sout->pf_write( p_sout, p_hdr );
150
151     return VLC_SUCCESS;
152 }
153
154 /*****************************************************************************
155  * Close:
156  *****************************************************************************/
157
158 static void Close( vlc_object_t * p_this )
159 {
160     sout_instance_t     *p_sout = (sout_instance_t*)p_this;
161     sout_mux_t          *p_mux = (sout_mux_t*)p_sout->p_mux_data;
162     sout_buffer_t       *p_hdr;
163     int                 i_stream;
164
165     msg_Info( p_sout, "Close" );
166
167     for( i_stream = 0; i_stream < p_mux->i_streams; i_stream++ )
168     {
169         avi_stream_t *p_stream;
170
171         p_stream = &p_mux->stream[i_stream];
172
173         p_stream->f_fps = 25;
174         if( p_stream->i_duration > 0 )
175         {
176             p_stream->f_fps = (float)p_stream->i_frames /
177                               ( (float)p_stream->i_duration /
178                                 (float)1000000 );
179         }
180         p_stream->i_bitrate = 128 * 1024;
181         if( p_stream->i_duration > 0 )
182         {
183             p_stream->i_bitrate =
184                 8 * (uint64_t)1000000 *
185                     (uint64_t)p_stream->i_totalsize /
186                     (uint64_t)p_stream->i_duration;
187         }
188         msg_Err( p_sout,"stream[%d] duration:%lld totalsize:%lld frames:%d fps:%f kb/s:%d",
189                 i_stream,
190                 p_stream->i_duration/1000000, p_stream->i_totalsize,
191                 p_stream->i_frames,
192                 p_stream->f_fps, p_stream->i_bitrate/1024 );
193     }
194
195     p_hdr = avi_HeaderCreateRIFF( p_sout );
196     p_sout->pf_seek( p_sout, 0 );
197     p_sout->pf_write( p_sout, p_hdr );
198 }
199
200
201 static int AddStream( sout_instance_t *p_sout, sout_input_t *p_input )
202 {
203     sout_mux_t          *p_mux = (sout_mux_t*)p_sout->p_mux_data;
204     avi_stream_t        *p_stream;
205
206     if( p_mux->i_streams >= 100 )
207     {
208         msg_Err( p_sout, "too many streams" );
209         return( -1 );
210     }
211     if( p_input->input_format.p_format == NULL )
212     {
213         msg_Err( p_sout, "stream descriptor missing" );
214         return( -1 );
215     }
216
217     msg_Dbg( p_sout, "adding input" );
218     p_input->p_mux_data = malloc( sizeof( int ) );
219
220     *((int*)p_input->p_mux_data) = p_mux->i_streams;
221     p_stream = &p_mux->stream[p_mux->i_streams];
222
223     switch( p_input->input_format.i_cat )
224     {
225         case AUDIO_ES:
226             {
227                 WAVEFORMATEX *p_wf =
228                     (WAVEFORMATEX*)p_input->input_format.p_format;
229
230                 p_stream->i_cat = AUDIO_ES;
231                 p_stream->fcc[0] = '0' + p_mux->i_streams / 10;
232                 p_stream->fcc[1] = '0' + p_mux->i_streams % 10;
233                 p_stream->fcc[2] = 'w';
234                 p_stream->fcc[3] = 'b';
235
236                 p_stream->p_bih = NULL;
237                 p_stream->p_wf  = malloc( sizeof( WAVEFORMATEX ) + p_wf->cbSize );
238                 memcpy( p_stream->p_wf,
239                         p_wf,
240                         sizeof( WAVEFORMATEX ) + p_wf->cbSize);
241             }
242             break;
243         case VIDEO_ES:
244             {
245                 BITMAPINFOHEADER *p_bih =
246                     (BITMAPINFOHEADER*)p_input->input_format.p_format;;
247
248                 p_stream->i_cat = VIDEO_ES;
249                 p_stream->fcc[0] = '0' + p_mux->i_streams / 10;
250                 p_stream->fcc[1] = '0' + p_mux->i_streams % 10;
251                 p_stream->fcc[2] = 'd';
252                 p_stream->fcc[3] = 'c';
253                 if( p_mux->i_stream_video < 0 )
254                 {
255                     p_mux->i_stream_video = p_mux->i_streams;
256                 }
257                 p_stream->p_wf  = NULL;
258                 p_stream->p_bih = malloc( p_bih->biSize );
259                 memcpy( p_stream->p_bih,
260                         p_bih,
261                         p_bih->biSize );
262             }
263             break;
264         default:
265             return( -1 );
266     }
267     p_stream->i_totalsize = 0;
268     p_stream->i_frames     = 0;
269     p_stream->i_duration  = 0;
270
271     p_mux->i_streams++;
272     return( 0 );
273 }
274
275 static int DelStream( sout_instance_t *p_sout, sout_input_t *p_input )
276 {
277 //    sout_mux_t          *p_mux = (sout_mux_t*)p_sout->p_mux_data;
278
279     msg_Dbg( p_sout, "removing input" );
280
281     free( p_input->p_mux_data ); p_input->p_mux_data = NULL;
282
283     return( 0 );
284 }
285
286 static int Mux      ( sout_instance_t *p_sout )
287 {
288     sout_mux_t          *p_mux = (sout_mux_t*)p_sout->p_mux_data;
289     avi_stream_t        *p_stream;
290     int i_stream;
291     int i;
292
293     for( i = 0; i < p_sout->i_nb_inputs; i++ )
294     {
295         int i_count;
296         sout_fifo_t *p_fifo;
297
298         i_stream = *((int*)p_sout->pp_inputs[i]->p_mux_data );
299         p_stream = &p_mux->stream[i_stream];
300
301         p_fifo = p_sout->pp_inputs[i]->p_fifo;
302         i_count = p_fifo->i_depth;
303         while( i_count > 0 )
304         {
305             sout_buffer_t *p_data;
306
307             p_data = sout_FifoGet( p_fifo );
308
309             p_stream->i_frames++;
310             p_stream->i_duration  += p_data->i_length;
311             p_stream->i_totalsize += p_data->i_size;
312
313             if( sout_BufferReallocFromPreHeader( p_sout, p_data, 8 ) )
314             {
315                 /* there isn't enough data in preheader */
316                 sout_buffer_t *p_hdr;
317
318                 p_hdr = sout_BufferNew( p_sout, 8 );
319                 SetFCC( p_hdr->p_buffer, p_stream->fcc );
320                 SetDWLE( p_hdr->p_buffer + 4, p_data->i_size );
321
322                 p_sout->pf_write( p_sout, p_hdr );
323                 p_mux->i_movi_size += p_hdr->i_size;
324
325             }
326             else
327             {
328                 SetFCC( p_data->p_buffer, p_stream->fcc );
329                 SetDWLE( p_data->p_buffer + 4, p_data->i_size - 8 );
330             }
331
332             if( p_data->i_size & 0x01 )
333             {
334                 sout_BufferRealloc( p_sout, p_data, p_data->i_size + 1 );
335                 p_data->i_size += 1;
336             }
337
338             p_sout->pf_write( p_sout, p_data );
339             p_mux->i_movi_size += p_data->i_size;
340
341             i_count--;
342         }
343
344     }
345     return( 0 );
346 }
347
348 /****************************************************************************/
349 /****************************************************************************/
350 /****************************************************************************/
351 /****************************************************************************/
352
353 typedef struct buffer_out_s
354 {
355     int      i_buffer_size;
356     int      i_buffer;
357     uint8_t  *p_buffer;
358
359 } buffer_out_t;
360
361 static void bo_Init( buffer_out_t *p_bo, int i_size, uint8_t *p_buffer )
362 {
363     p_bo->i_buffer_size = i_size;
364     p_bo->i_buffer = 0;
365     p_bo->p_buffer = p_buffer;
366 }
367 static void bo_AddByte( buffer_out_t *p_bo, uint8_t i )
368 {
369     if( p_bo->i_buffer < p_bo->i_buffer_size )
370     {
371         p_bo->p_buffer[p_bo->i_buffer] = i;
372     }
373     p_bo->i_buffer++;
374 }
375 static void bo_AddWordLE( buffer_out_t *p_bo, uint16_t i )
376 {
377     bo_AddByte( p_bo, i &0xff );
378     bo_AddByte( p_bo, ( ( i >> 8) &0xff ) );
379 }
380 static void bo_AddWordBE( buffer_out_t *p_bo, uint16_t i )
381 {
382     bo_AddByte( p_bo, ( ( i >> 8) &0xff ) );
383     bo_AddByte( p_bo, i &0xff );
384 }
385 static void bo_AddDWordLE( buffer_out_t *p_bo, uint32_t i )
386 {
387     bo_AddWordLE( p_bo, i &0xffff );
388     bo_AddWordLE( p_bo, ( ( i >> 16) &0xffff ) );
389 }
390 static void bo_AddDWordBE( buffer_out_t *p_bo, uint32_t i )
391 {
392     bo_AddWordLE( p_bo, ( ( i >> 16) &0xffff ) );
393     bo_AddWordLE( p_bo, i &0xffff );
394 }
395 #if 0
396 static void bo_AddLWordLE( buffer_out_t *p_bo, uint64_t i )
397 {
398     bo_AddDWordLE( p_bo, i &0xffffffff );
399     bo_AddDWordLE( p_bo, ( ( i >> 32) &0xffffffff ) );
400 }
401 static void bo_AddLWordBE( buffer_out_t *p_bo, uint64_t i )
402 {
403     bo_AddDWordBE( p_bo, ( ( i >> 32) &0xffffffff ) );
404     bo_AddDWordBE( p_bo, i &0xffffffff );
405 }
406 #endif
407
408 static void bo_AddFCC( buffer_out_t *p_bo, char *fcc )
409 {
410     bo_AddByte( p_bo, fcc[0] );
411     bo_AddByte( p_bo, fcc[1] );
412     bo_AddByte( p_bo, fcc[2] );
413     bo_AddByte( p_bo, fcc[3] );
414 }
415
416 static void bo_AddMem( buffer_out_t *p_bo, int i_size, uint8_t *p_mem )
417 {
418     int i;
419
420     for( i = 0; i < i_size; i++ )
421     {
422         bo_AddByte( p_bo, p_mem[i] );
423     }
424 }
425
426 /****************************************************************************
427  ****************************************************************************
428  **
429  ** avi header generation
430  **
431  ****************************************************************************
432  ****************************************************************************/
433 #define AVI_BOX_ENTER( fcc ) \
434     buffer_out_t _bo_sav_; \
435     bo_AddFCC( p_bo, fcc ); \
436     _bo_sav_ = *p_bo; \
437     bo_AddDWordLE( p_bo, 0 )
438
439 #define AVI_BOX_ENTER_LIST( fcc ) \
440     AVI_BOX_ENTER( "LIST" ); \
441     bo_AddFCC( p_bo, fcc )
442
443 #define AVI_BOX_EXIT( i_err ) \
444     if( p_bo->i_buffer&0x01 ) bo_AddByte( p_bo, 0 ); \
445     bo_AddDWordLE( &_bo_sav_, p_bo->i_buffer - _bo_sav_.i_buffer - 4 ); \
446     return( i_err );
447
448 static int avi_HeaderAdd_avih( sout_instance_t *p_sout,
449                                buffer_out_t *p_bo )
450 {
451     sout_mux_t *p_mux = (sout_mux_t*)p_sout->p_mux_data;
452     avi_stream_t *p_video = NULL;
453     int         i_stream;
454     uint32_t    i_microsecperframe;
455     int         i_maxbytespersec;
456     int         i_totalframes;
457     AVI_BOX_ENTER( "avih" );
458
459     if( p_mux->i_stream_video >= 0 )
460     {
461         p_video = &p_mux->stream[p_mux->i_stream_video];
462         if( p_video->i_frames <= 0 )
463         {
464             p_video = NULL;
465         }
466     }
467
468     if( p_video )
469     {
470         i_microsecperframe =
471             (uint32_t)( (float)1000000 /
472                         (float)p_mux->stream[p_mux->i_stream_video].f_fps );
473         i_totalframes = p_mux->stream[p_mux->i_stream_video].i_frames;
474     }
475     else
476     {
477         msg_Warn( p_sout, "avi file without audio video track isn't a good idea..." );
478         i_microsecperframe = 0;
479         i_totalframes = 0;
480     }
481
482     for( i_stream = 0,i_maxbytespersec = 0; i_stream < p_mux->i_streams; i_stream++ )
483     {
484         if( p_mux->stream[p_mux->i_stream_video].i_duration > 0 )
485         {
486             i_maxbytespersec +=
487                 p_mux->stream[p_mux->i_stream_video].i_totalsize /
488                 p_mux->stream[p_mux->i_stream_video].i_duration;
489         }
490     }
491
492     bo_AddDWordLE( p_bo, i_microsecperframe );
493     bo_AddDWordLE( p_bo, i_maxbytespersec );
494     bo_AddDWordLE( p_bo, 0 );                   /* padding */
495     bo_AddDWordLE( p_bo, AVIF_TRUSTCKTYPE |
496                          AVIF_HASINDEX |
497                          AVIF_ISINTERLEAVED );  /* flags */
498     bo_AddDWordLE( p_bo, i_totalframes );
499     bo_AddDWordLE( p_bo, 0 );                   /* initial frame */
500     bo_AddDWordLE( p_bo, p_mux->i_streams );    /* streams count */
501     bo_AddDWordLE( p_bo, 1024 * 1024 );         /* suggested buffer size */
502     if( p_video )
503     {
504         bo_AddDWordLE( p_bo, p_video->p_bih->biWidth );
505         bo_AddDWordLE( p_bo, p_video->p_bih->biHeight );
506     }
507     else
508     {
509         bo_AddDWordLE( p_bo, 0 );
510         bo_AddDWordLE( p_bo, 0 );
511     }
512     bo_AddDWordLE( p_bo, 0 );                   /* ???? */
513     bo_AddDWordLE( p_bo, 0 );                   /* ???? */
514     bo_AddDWordLE( p_bo, 0 );                   /* ???? */
515     bo_AddDWordLE( p_bo, 0 );                   /* ???? */
516
517     AVI_BOX_EXIT( 0 );
518 }
519 static int avi_HeaderAdd_strh( sout_instance_t *p_sout,
520                                buffer_out_t *p_bo,
521                                avi_stream_t *p_stream )
522 {
523 //    sout_mux_t *p_mux = (sout_mux_t*)p_sout->p_mux_data;
524     AVI_BOX_ENTER( "strh" );
525
526     switch( p_stream->i_cat )
527     {
528         case VIDEO_ES:
529             {
530                 bo_AddFCC( p_bo, "vids" );
531                 bo_AddDWordBE( p_bo, p_stream->p_bih->biCompression );
532                 bo_AddDWordLE( p_bo, 0 );   /* flags */
533                 bo_AddWordLE(  p_bo, 0 );   /* priority */
534                 bo_AddWordLE(  p_bo, 0 );   /* langage */
535                 bo_AddDWordLE( p_bo, 0 );   /* initial frame */
536                 bo_AddDWordLE( p_bo, 1000 );/* scale */
537                 bo_AddDWordLE( p_bo, (uint32_t)( 1000 * p_stream->f_fps ));
538                 bo_AddDWordLE( p_bo, 0 );   /* start */
539                 bo_AddDWordLE( p_bo, p_stream->i_frames );
540                 bo_AddDWordLE( p_bo, 1024 * 1024 );
541                 bo_AddDWordLE( p_bo, -1 );  /* quality */
542                 bo_AddDWordLE( p_bo, 0 );   /* samplesize */
543                 bo_AddWordLE(  p_bo, 0 );   /* ??? */
544                 bo_AddWordLE(  p_bo, 0 );   /* ??? */
545                 bo_AddWordLE(  p_bo, p_stream->p_bih->biWidth );
546                 bo_AddWordLE(  p_bo, p_stream->p_bih->biHeight );
547             }
548             break;
549         case AUDIO_ES:
550             {
551                 int i_rate, i_scale, i_samplesize;
552
553                 i_samplesize = p_stream->p_wf->nBlockAlign;
554                 if( i_samplesize > 1 )
555                 {
556                     i_scale = i_samplesize;
557                     i_rate = i_scale * p_stream->i_bitrate / 8;
558                 }
559                 else
560                 {
561                     i_samplesize = 1;
562                     i_scale = 1000;
563                     i_rate = 1000 * p_stream->i_bitrate / 8;
564                 }
565                 bo_AddFCC( p_bo, "auds" );
566                 bo_AddDWordLE( p_bo, 1 );   /* tag */
567                 bo_AddDWordLE( p_bo, 0 );   /* flags */
568                 bo_AddWordLE(  p_bo, 0 );   /* priority */
569                 bo_AddWordLE(  p_bo, 0 );   /* langage */
570                 bo_AddDWordLE( p_bo, 0 );   /* initial frame */
571                 bo_AddDWordLE( p_bo, i_scale );/* scale */
572                 bo_AddDWordLE( p_bo, i_rate );
573                 bo_AddDWordLE( p_bo, 0 );   /* start */
574                 bo_AddDWordLE( p_bo, p_stream->i_frames );
575                 bo_AddDWordLE( p_bo, 10 * 1024 );
576                 bo_AddDWordLE( p_bo, -1 );  /* quality */
577                 bo_AddDWordLE( p_bo, i_samplesize );
578                 bo_AddWordLE(  p_bo, 0 );   /* ??? */
579                 bo_AddWordLE(  p_bo, 0 );   /* ??? */
580                 bo_AddWordLE(  p_bo, 0 );
581                 bo_AddWordLE(  p_bo, 0 );
582             }
583             break;
584     }
585
586     AVI_BOX_EXIT( 0 );
587 }
588
589 static int avi_HeaderAdd_strf( sout_instance_t *p_sout,
590                                buffer_out_t *p_bo,
591                                avi_stream_t *p_stream )
592 {
593 //    sout_mux_t *p_mux = (sout_mux_t*)p_sout->p_mux_data;
594     AVI_BOX_ENTER( "strf" );
595
596     switch( p_stream->i_cat )
597     {
598         case AUDIO_ES:
599             bo_AddWordLE( p_bo, p_stream->p_wf->wFormatTag );
600             bo_AddWordLE( p_bo, p_stream->p_wf->nChannels );
601             bo_AddDWordLE( p_bo, p_stream->p_wf->nSamplesPerSec );
602             bo_AddDWordLE( p_bo, p_stream->p_wf->nAvgBytesPerSec );
603             bo_AddWordLE( p_bo, p_stream->p_wf->nBlockAlign );
604             bo_AddWordLE( p_bo, p_stream->p_wf->wBitsPerSample );
605             bo_AddWordLE( p_bo, p_stream->p_wf->cbSize );
606             bo_AddMem( p_bo, p_stream->p_wf->cbSize, (uint8_t*)&p_stream->p_wf[1] );
607             break;
608         case VIDEO_ES:
609             bo_AddDWordLE( p_bo, p_stream->p_bih->biSize );
610             bo_AddDWordLE( p_bo, p_stream->p_bih->biWidth );
611             bo_AddDWordLE( p_bo, p_stream->p_bih->biHeight );
612             bo_AddWordLE( p_bo, p_stream->p_bih->biPlanes );
613             bo_AddWordLE( p_bo, p_stream->p_bih->biBitCount );
614             bo_AddDWordLE( p_bo, p_stream->p_bih->biCompression );
615             bo_AddDWordLE( p_bo, p_stream->p_bih->biSizeImage );
616             bo_AddDWordLE( p_bo, p_stream->p_bih->biXPelsPerMeter );
617             bo_AddDWordLE( p_bo, p_stream->p_bih->biYPelsPerMeter );
618             bo_AddDWordLE( p_bo, p_stream->p_bih->biClrUsed );
619             bo_AddDWordLE( p_bo, p_stream->p_bih->biClrImportant );
620             bo_AddMem( p_bo,
621                        p_stream->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
622                        (uint8_t*)&p_stream->p_bih[1] );
623             break;
624     }
625
626     AVI_BOX_EXIT( 0 );
627 }
628
629 static int avi_HeaderAdd_strl( sout_instance_t *p_sout,
630                                buffer_out_t *p_bo,
631                                avi_stream_t *p_stream )
632 {
633 //    sout_mux_t *p_mux = (sout_mux_t*)p_sout->p_mux_data;
634     AVI_BOX_ENTER_LIST( "strl" );
635
636     avi_HeaderAdd_strh( p_sout, p_bo, p_stream );
637     avi_HeaderAdd_strf( p_sout, p_bo, p_stream );
638
639     AVI_BOX_EXIT( 0 );
640 }
641
642 static sout_buffer_t *avi_HeaderCreateRIFF( sout_instance_t *p_sout )
643 {
644     sout_mux_t          *p_mux = (sout_mux_t*)p_sout->p_mux_data;
645     sout_buffer_t       *p_hdr;
646     int                 i_stream;
647     int                 i_maxbytespersec;
648     int                 i_junk;
649     buffer_out_t        bo;
650
651     p_hdr = sout_BufferNew( p_sout, HDR_SIZE );
652     memset( p_hdr->p_buffer, 0, HDR_SIZE );
653
654     bo_Init( &bo, HDR_SIZE, p_hdr->p_buffer );
655
656     bo_AddFCC( &bo, "RIFF" );
657     bo_AddDWordLE( &bo, p_mux->i_movi_size + HDR_SIZE - 8 );
658     bo_AddFCC( &bo, "AVI " );
659
660     bo_AddFCC( &bo, "LIST" );
661     bo_AddDWordLE( &bo, HDR_SIZE - 8);
662     bo_AddFCC( &bo, "hdrl" );
663
664     avi_HeaderAdd_avih( p_sout, &bo );
665     for( i_stream = 0,i_maxbytespersec = 0; i_stream < p_mux->i_streams; i_stream++ )
666     {
667         avi_HeaderAdd_strl( p_sout, &bo, &p_mux->stream[i_stream] );
668     }
669
670     i_junk = HDR_SIZE - bo.i_buffer - 8 - 12;
671     bo_AddFCC( &bo, "JUNK" );
672     bo_AddDWordLE( &bo, i_junk );
673
674     bo.i_buffer += i_junk;
675     bo_AddFCC( &bo, "LIST" );
676     bo_AddDWordLE( &bo, p_mux->i_movi_size);
677     bo_AddFCC( &bo, "movi" );
678
679     return( p_hdr );
680 }