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