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