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