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