24d9e4940434d5c09115cdc4c751ff29e9545daf
[vlc.git] / modules / mux / avi.c
1 /*****************************************************************************
2  * avi.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: avi.c,v 1.9 2003/03/03 14:21:08 gbazin Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 /* TODO: add OpenDML write support */
28
29 #include <stdlib.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <fcntl.h>
35
36 #include <vlc/vlc.h>
37 #include <vlc/input.h>
38 #include <vlc/sout.h>
39
40 #ifdef HAVE_UNISTD_H
41 #   include <unistd.h>
42 #endif
43
44 #include "codecs.h"
45
46
47 #define AVIF_HASINDEX       0x00000010  // Index at end of file?
48 #define AVIF_MUSTUSEINDEX   0x00000020
49 #define AVIF_ISINTERLEAVED  0x00000100
50 #define AVIF_TRUSTCKTYPE    0x00000800  // Use CKType to find key frames?
51 #define AVIF_WASCAPTUREFILE 0x00010000
52 #define AVIF_COPYRIGHTED    0x00020000
53
54 /* Flags for index */
55 #define AVIIF_LIST          0x00000001L /* chunk is a 'LIST' */
56 #define AVIIF_KEYFRAME      0x00000010L /* this frame is a key frame.*/
57 #define AVIIF_NOTIME        0x00000100L /* this frame doesn't take any time */
58 #define AVIIF_COMPUSE       0x0FFF0000L /* these bits are for compressor use */
59
60 /*****************************************************************************
61  * Exported prototypes
62  *****************************************************************************/
63 static int     Open   ( vlc_object_t * );
64 static void    Close  ( vlc_object_t * );
65
66 static int Capability(sout_instance_t *, int , void *, void * );
67 static int AddStream( sout_instance_t *, sout_input_t * );
68 static int DelStream( sout_instance_t *, sout_input_t * );
69 static int Mux      ( sout_instance_t * );
70
71 static sout_buffer_t *avi_HeaderCreateRIFF( sout_instance_t *p_sout );
72 static sout_buffer_t *avi_HeaderCreateidx1( sout_instance_t *p_sout );
73
74 static void SetFCC( uint8_t *p, char *fcc )
75 {
76     p[0] = fcc[0];
77     p[1] = fcc[1];
78     p[2] = fcc[2];
79     p[3] = fcc[3];
80 }
81
82 static void SetDWLE( uint8_t *p, uint32_t i_dw )
83 {
84     p[3] = ( i_dw >> 24 )&0xff;
85     p[2] = ( i_dw >> 16 )&0xff;
86     p[1] = ( i_dw >>  8 )&0xff;
87     p[0] = ( i_dw       )&0xff;
88 }
89
90 /*****************************************************************************
91  * Module descriptor
92  *****************************************************************************/
93 vlc_module_begin();
94     set_description( _("Avi muxer") );
95     set_capability( "sout mux", 5 );
96     add_shortcut( "avi" );
97     set_callbacks( Open, Close );
98 vlc_module_end();
99
100 // FIXME FIXME
101 #define HDR_SIZE 10240
102
103 typedef struct avi_stream_s
104 {
105     int i_cat;
106
107     char fcc[4];
108
109     mtime_t i_duration;       // in ┬Ás
110
111     int     i_frames;        // total frame count
112     int64_t i_totalsize;    // total stream size
113
114     float   f_fps;
115     int     i_bitrate;
116
117     BITMAPINFOHEADER    *p_bih;
118     WAVEFORMATEX        *p_wf;
119
120 } avi_stream_t;
121
122 typedef struct avi_idx1_entry_s
123 {
124     char     fcc[4];
125     uint32_t i_flags;
126     uint32_t i_pos;
127     uint32_t i_length;
128
129 } avi_idx1_entry_t;
130
131 typedef struct avi_idx1_s
132 {
133     unsigned int i_entry_count;
134     unsigned int i_entry_max;
135
136     avi_idx1_entry_t *entry;
137 } avi_idx1_t;
138
139 typedef struct sout_mux_s
140 {
141     int i_streams;
142     int i_stream_video;
143
144     off_t i_movi_size;
145     avi_stream_t stream[100];
146
147     avi_idx1_t idx1;
148     off_t i_idx1_size;
149
150 } sout_mux_t;
151
152 /*****************************************************************************
153  * Open:
154  *****************************************************************************/
155 static int Open( vlc_object_t *p_this )
156 {
157     sout_instance_t     *p_sout = (sout_instance_t*)p_this;
158     sout_mux_t          *p_mux;
159     sout_buffer_t       *p_hdr;
160
161     p_mux = malloc( sizeof( sout_mux_t ) );
162     p_mux->i_streams = 0;
163     p_mux->i_stream_video = -1;
164     p_mux->i_movi_size = 0;
165
166     p_mux->idx1.i_entry_count = 0;
167     p_mux->idx1.i_entry_max = 10000;
168     p_mux->idx1.entry = calloc( p_mux->idx1.i_entry_max, sizeof( avi_idx1_entry_t ) );
169
170     msg_Info( p_sout, "Open" );
171
172     p_sout->pf_mux_capacity  = Capability;
173     p_sout->pf_mux_addstream = AddStream;
174     p_sout->pf_mux_delstream = DelStream;
175     p_sout->pf_mux           = Mux;
176     p_sout->p_mux_data = (void*)p_mux;
177     p_sout->i_mux_preheader  = 8; // (fourcc,length) header
178
179     /* room to add header at the end */
180     p_hdr = sout_BufferNew( p_sout, HDR_SIZE );
181     memset( p_hdr->p_buffer, 0, HDR_SIZE );
182     sout_AccessOutWrite( p_sout->p_access, p_hdr );
183
184     return VLC_SUCCESS;
185 }
186
187 /*****************************************************************************
188  * Close:
189  *****************************************************************************/
190
191 static void Close( vlc_object_t * p_this )
192 {
193     sout_instance_t     *p_sout = (sout_instance_t*)p_this;
194     sout_mux_t          *p_mux = (sout_mux_t*)p_sout->p_mux_data;
195     sout_buffer_t       *p_hdr, *p_idx1;
196     int                 i_stream;
197
198     msg_Info( p_sout, "Close" );
199
200     /* first create idx1 chunk (write at the end of the stream */
201     p_idx1 = avi_HeaderCreateidx1( p_sout );
202     p_mux->i_idx1_size = p_idx1->i_size;
203     sout_AccessOutWrite( p_sout->p_access, p_idx1 );
204
205     /* calculate some value for headers creations */
206     for( i_stream = 0; i_stream < p_mux->i_streams; i_stream++ )
207     {
208         avi_stream_t *p_stream;
209
210         p_stream = &p_mux->stream[i_stream];
211
212         p_stream->f_fps = 25;
213         if( p_stream->i_duration > 0 )
214         {
215             p_stream->f_fps = (float)p_stream->i_frames /
216                               ( (float)p_stream->i_duration /
217                                 (float)1000000 );
218         }
219         p_stream->i_bitrate = 128 * 1024;
220         if( p_stream->i_duration > 0 )
221         {
222             p_stream->i_bitrate =
223                 8 * (uint64_t)1000000 *
224                     (uint64_t)p_stream->i_totalsize /
225                     (uint64_t)p_stream->i_duration;
226         }
227         msg_Err( p_sout,"stream[%d] duration:%lld totalsize:%lld frames:%d fps:%f kb/s:%d",
228                 i_stream,
229                 p_stream->i_duration/1000000, p_stream->i_totalsize,
230                 p_stream->i_frames,
231                 p_stream->f_fps, p_stream->i_bitrate/1024 );
232     }
233
234     p_hdr = avi_HeaderCreateRIFF( p_sout );
235     sout_AccessOutSeek( p_sout->p_access, 0 );
236     sout_AccessOutWrite( p_sout->p_access, p_hdr );
237 }
238
239 static int Capability( sout_instance_t *p_sout, int i_query, void *p_args, void *p_answer )
240 {
241    switch( i_query )
242    {
243         case SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME:
244             *(vlc_bool_t*)p_answer = VLC_TRUE;
245             return( SOUT_MUX_CAP_ERR_OK );
246         default:
247             return( SOUT_MUX_CAP_ERR_UNIMPLEMENTED );
248    }
249 }
250
251 static int AddStream( sout_instance_t *p_sout, sout_input_t *p_input )
252 {
253     sout_mux_t          *p_mux = (sout_mux_t*)p_sout->p_mux_data;
254     avi_stream_t        *p_stream;
255
256     if( p_mux->i_streams >= 100 )
257     {
258         msg_Err( p_sout, "too many streams" );
259         return( -1 );
260     }
261     if( p_input->input_format.p_format == NULL )
262     {
263         msg_Err( p_sout, "stream descriptor missing" );
264         return( -1 );
265     }
266
267     msg_Dbg( p_sout, "adding input" );
268     p_input->p_mux_data = malloc( sizeof( int ) );
269
270     *((int*)p_input->p_mux_data) = p_mux->i_streams;
271     p_stream = &p_mux->stream[p_mux->i_streams];
272
273     switch( p_input->input_format.i_cat )
274     {
275         case AUDIO_ES:
276             {
277                 WAVEFORMATEX *p_wf =
278                     (WAVEFORMATEX*)p_input->input_format.p_format;
279
280                 p_stream->i_cat = AUDIO_ES;
281                 p_stream->fcc[0] = '0' + p_mux->i_streams / 10;
282                 p_stream->fcc[1] = '0' + p_mux->i_streams % 10;
283                 p_stream->fcc[2] = 'w';
284                 p_stream->fcc[3] = 'b';
285
286                 p_stream->p_bih = NULL;
287                 p_stream->p_wf  = malloc( sizeof( WAVEFORMATEX ) + p_wf->cbSize );
288                 memcpy( p_stream->p_wf,
289                         p_wf,
290                         sizeof( WAVEFORMATEX ) + p_wf->cbSize);
291             }
292             break;
293         case VIDEO_ES:
294             {
295                 BITMAPINFOHEADER *p_bih =
296                     (BITMAPINFOHEADER*)p_input->input_format.p_format;;
297
298                 p_stream->i_cat = VIDEO_ES;
299                 p_stream->fcc[0] = '0' + p_mux->i_streams / 10;
300                 p_stream->fcc[1] = '0' + p_mux->i_streams % 10;
301                 p_stream->fcc[2] = 'd';
302                 p_stream->fcc[3] = 'c';
303                 if( p_mux->i_stream_video < 0 )
304                 {
305                     p_mux->i_stream_video = p_mux->i_streams;
306                 }
307                 p_stream->p_wf  = NULL;
308                 p_stream->p_bih = malloc( p_bih->biSize );
309                 memcpy( p_stream->p_bih,
310                         p_bih,
311                         p_bih->biSize );
312             }
313             break;
314         default:
315             return( -1 );
316     }
317     p_stream->i_totalsize = 0;
318     p_stream->i_frames     = 0;
319     p_stream->i_duration  = 0;
320
321     p_mux->i_streams++;
322     return( 0 );
323 }
324
325 static int DelStream( sout_instance_t *p_sout, sout_input_t *p_input )
326 {
327 //    sout_mux_t          *p_mux = (sout_mux_t*)p_sout->p_mux_data;
328
329     msg_Dbg( p_sout, "removing input" );
330
331     free( p_input->p_mux_data ); p_input->p_mux_data = NULL;
332
333     return( 0 );
334 }
335
336 static int Mux      ( sout_instance_t *p_sout )
337 {
338     sout_mux_t          *p_mux = (sout_mux_t*)p_sout->p_mux_data;
339     avi_stream_t        *p_stream;
340     int i_stream;
341     int i;
342
343     for( i = 0; i < p_sout->i_nb_inputs; i++ )
344     {
345         int i_count;
346         sout_fifo_t *p_fifo;
347
348         i_stream = *((int*)p_sout->pp_inputs[i]->p_mux_data );
349         p_stream = &p_mux->stream[i_stream];
350
351         p_fifo = p_sout->pp_inputs[i]->p_fifo;
352         i_count = p_fifo->i_depth;
353         while( i_count > 0 )
354         {
355             avi_idx1_entry_t *p_idx;
356             sout_buffer_t *p_data;
357
358             p_data = sout_FifoGet( p_fifo );
359
360             p_stream->i_frames++;
361             p_stream->i_duration  += p_data->i_length;
362             p_stream->i_totalsize += p_data->i_size;
363
364             /* add idx1 entry for this frame */
365             p_idx = &p_mux->idx1.entry[p_mux->idx1.i_entry_count];
366             memcpy( p_idx->fcc, p_stream->fcc, 4 );
367             p_idx->i_flags = AVIIF_KEYFRAME;
368             p_idx->i_pos   = p_mux->i_movi_size + 4;
369             p_idx->i_length= p_data->i_size;
370             p_mux->idx1.i_entry_count++;
371             if( p_mux->idx1.i_entry_count >= p_mux->idx1.i_entry_max )
372             {
373                 p_mux->idx1.i_entry_max += 10000;
374                 p_mux->idx1.entry = realloc( p_mux->idx1.entry,
375                                              p_mux->idx1.i_entry_max * sizeof( avi_idx1_entry_t ) );
376             }
377
378
379             if( sout_BufferReallocFromPreHeader( p_sout, p_data, 8 ) )
380             {
381                 /* there isn't enough data in preheader */
382                 sout_buffer_t *p_hdr;
383
384                 p_hdr = sout_BufferNew( p_sout, 8 );
385                 SetFCC( p_hdr->p_buffer, p_stream->fcc );
386                 SetDWLE( p_hdr->p_buffer + 4, p_data->i_size );
387
388                 sout_AccessOutWrite( p_sout->p_access, p_hdr );
389                 p_mux->i_movi_size += p_hdr->i_size;
390
391             }
392             else
393             {
394                 SetFCC( p_data->p_buffer, p_stream->fcc );
395                 SetDWLE( p_data->p_buffer + 4, p_data->i_size - 8 );
396             }
397
398             if( p_data->i_size & 0x01 )
399             {
400                 sout_BufferRealloc( p_sout, p_data, p_data->i_size + 1 );
401                 p_data->i_size += 1;
402             }
403
404             sout_AccessOutWrite( p_sout->p_access, p_data );
405             p_mux->i_movi_size += p_data->i_size;
406
407             i_count--;
408         }
409
410     }
411     return( 0 );
412 }
413
414 /****************************************************************************/
415 /****************************************************************************/
416 /****************************************************************************/
417 /****************************************************************************/
418
419 typedef struct buffer_out_s
420 {
421     int      i_buffer_size;
422     int      i_buffer;
423     uint8_t  *p_buffer;
424
425 } buffer_out_t;
426
427 static void bo_Init( buffer_out_t *p_bo, int i_size, uint8_t *p_buffer )
428 {
429     p_bo->i_buffer_size = i_size;
430     p_bo->i_buffer = 0;
431     p_bo->p_buffer = p_buffer;
432 }
433 static void bo_AddByte( buffer_out_t *p_bo, uint8_t i )
434 {
435     if( p_bo->i_buffer < p_bo->i_buffer_size )
436     {
437         p_bo->p_buffer[p_bo->i_buffer] = i;
438     }
439     p_bo->i_buffer++;
440 }
441 static void bo_AddWordLE( buffer_out_t *p_bo, uint16_t i )
442 {
443     bo_AddByte( p_bo, i &0xff );
444     bo_AddByte( p_bo, ( ( i >> 8) &0xff ) );
445 }
446 static void bo_AddWordBE( buffer_out_t *p_bo, uint16_t i )
447 {
448     bo_AddByte( p_bo, ( ( i >> 8) &0xff ) );
449     bo_AddByte( p_bo, i &0xff );
450 }
451 static void bo_AddDWordLE( buffer_out_t *p_bo, uint32_t i )
452 {
453     bo_AddWordLE( p_bo, i &0xffff );
454     bo_AddWordLE( p_bo, ( ( i >> 16) &0xffff ) );
455 }
456 static void bo_AddDWordBE( buffer_out_t *p_bo, uint32_t i )
457 {
458     bo_AddWordBE( p_bo, ( ( i >> 16) &0xffff ) );
459     bo_AddWordBE( p_bo, i &0xffff );
460 }
461 #if 0
462 static void bo_AddLWordLE( buffer_out_t *p_bo, uint64_t i )
463 {
464     bo_AddDWordLE( p_bo, i &0xffffffff );
465     bo_AddDWordLE( p_bo, ( ( i >> 32) &0xffffffff ) );
466 }
467 static void bo_AddLWordBE( buffer_out_t *p_bo, uint64_t i )
468 {
469     bo_AddDWordBE( p_bo, ( ( i >> 32) &0xffffffff ) );
470     bo_AddDWordBE( p_bo, i &0xffffffff );
471 }
472 #endif
473
474 static void bo_AddFCC( buffer_out_t *p_bo, char *fcc )
475 {
476     bo_AddByte( p_bo, fcc[0] );
477     bo_AddByte( p_bo, fcc[1] );
478     bo_AddByte( p_bo, fcc[2] );
479     bo_AddByte( p_bo, fcc[3] );
480 }
481
482 static void bo_AddMem( buffer_out_t *p_bo, int i_size, uint8_t *p_mem )
483 {
484     int i;
485
486     for( i = 0; i < i_size; i++ )
487     {
488         bo_AddByte( p_bo, p_mem[i] );
489     }
490 }
491
492 /****************************************************************************
493  ****************************************************************************
494  **
495  ** avi header generation
496  **
497  ****************************************************************************
498  ****************************************************************************/
499 #define AVI_BOX_ENTER( fcc ) \
500     buffer_out_t _bo_sav_; \
501     bo_AddFCC( p_bo, fcc ); \
502     _bo_sav_ = *p_bo; \
503     bo_AddDWordLE( p_bo, 0 )
504
505 #define AVI_BOX_ENTER_LIST( fcc ) \
506     AVI_BOX_ENTER( "LIST" ); \
507     bo_AddFCC( p_bo, fcc )
508
509 #define AVI_BOX_EXIT( i_err ) \
510     if( p_bo->i_buffer&0x01 ) bo_AddByte( p_bo, 0 ); \
511     bo_AddDWordLE( &_bo_sav_, p_bo->i_buffer - _bo_sav_.i_buffer - 4 ); \
512     return( i_err );
513
514 static int avi_HeaderAdd_avih( sout_instance_t *p_sout,
515                                buffer_out_t *p_bo )
516 {
517     sout_mux_t *p_mux = (sout_mux_t*)p_sout->p_mux_data;
518     avi_stream_t *p_video = NULL;
519     int         i_stream;
520     uint32_t    i_microsecperframe;
521     int         i_maxbytespersec;
522     int         i_totalframes;
523     AVI_BOX_ENTER( "avih" );
524
525     if( p_mux->i_stream_video >= 0 )
526     {
527         p_video = &p_mux->stream[p_mux->i_stream_video];
528         if( p_video->i_frames <= 0 )
529         {
530             p_video = NULL;
531         }
532     }
533
534     if( p_video )
535     {
536         i_microsecperframe =
537             (uint32_t)( (float)1000000 /
538                         (float)p_mux->stream[p_mux->i_stream_video].f_fps );
539         i_totalframes = p_mux->stream[p_mux->i_stream_video].i_frames;
540     }
541     else
542     {
543         msg_Warn( p_sout, "avi file without audio video track isn't a good idea..." );
544         i_microsecperframe = 0;
545         i_totalframes = 0;
546     }
547
548     for( i_stream = 0,i_maxbytespersec = 0; i_stream < p_mux->i_streams; i_stream++ )
549     {
550         if( p_mux->stream[p_mux->i_stream_video].i_duration > 0 )
551         {
552             i_maxbytespersec +=
553                 p_mux->stream[p_mux->i_stream_video].i_totalsize /
554                 p_mux->stream[p_mux->i_stream_video].i_duration;
555         }
556     }
557
558     bo_AddDWordLE( p_bo, i_microsecperframe );
559     bo_AddDWordLE( p_bo, i_maxbytespersec );
560     bo_AddDWordLE( p_bo, 0 );                   /* padding */
561     bo_AddDWordLE( p_bo, AVIF_TRUSTCKTYPE |
562                          AVIF_HASINDEX |
563                          AVIF_ISINTERLEAVED );  /* flags */
564     bo_AddDWordLE( p_bo, i_totalframes );
565     bo_AddDWordLE( p_bo, 0 );                   /* initial frame */
566     bo_AddDWordLE( p_bo, p_mux->i_streams );    /* streams count */
567     bo_AddDWordLE( p_bo, 1024 * 1024 );         /* suggested buffer size */
568     if( p_video )
569     {
570         bo_AddDWordLE( p_bo, p_video->p_bih->biWidth );
571         bo_AddDWordLE( p_bo, p_video->p_bih->biHeight );
572     }
573     else
574     {
575         bo_AddDWordLE( p_bo, 0 );
576         bo_AddDWordLE( p_bo, 0 );
577     }
578     bo_AddDWordLE( p_bo, 0 );                   /* ???? */
579     bo_AddDWordLE( p_bo, 0 );                   /* ???? */
580     bo_AddDWordLE( p_bo, 0 );                   /* ???? */
581     bo_AddDWordLE( p_bo, 0 );                   /* ???? */
582
583     AVI_BOX_EXIT( 0 );
584 }
585 static int avi_HeaderAdd_strh( sout_instance_t *p_sout,
586                                buffer_out_t *p_bo,
587                                avi_stream_t *p_stream )
588 {
589 //    sout_mux_t *p_mux = (sout_mux_t*)p_sout->p_mux_data;
590     AVI_BOX_ENTER( "strh" );
591
592     switch( p_stream->i_cat )
593     {
594         case VIDEO_ES:
595             {
596                 bo_AddFCC( p_bo, "vids" );
597                 bo_AddDWordBE( p_bo, p_stream->p_bih->biCompression );
598                 bo_AddDWordLE( p_bo, 0 );   /* flags */
599                 bo_AddWordLE(  p_bo, 0 );   /* priority */
600                 bo_AddWordLE(  p_bo, 0 );   /* langage */
601                 bo_AddDWordLE( p_bo, 0 );   /* initial frame */
602                 bo_AddDWordLE( p_bo, 1000 );/* scale */
603                 bo_AddDWordLE( p_bo, (uint32_t)( 1000 * p_stream->f_fps ));
604                 bo_AddDWordLE( p_bo, 0 );   /* start */
605                 bo_AddDWordLE( p_bo, p_stream->i_frames );
606                 bo_AddDWordLE( p_bo, 1024 * 1024 );
607                 bo_AddDWordLE( p_bo, -1 );  /* quality */
608                 bo_AddDWordLE( p_bo, 0 );   /* samplesize */
609                 bo_AddWordLE(  p_bo, 0 );   /* ??? */
610                 bo_AddWordLE(  p_bo, 0 );   /* ??? */
611                 bo_AddWordLE(  p_bo, p_stream->p_bih->biWidth );
612                 bo_AddWordLE(  p_bo, p_stream->p_bih->biHeight );
613             }
614             break;
615         case AUDIO_ES:
616             {
617                 int i_rate, i_scale, i_samplesize;
618
619                 i_samplesize = p_stream->p_wf->nBlockAlign;
620                 if( i_samplesize > 1 )
621                 {
622                     i_scale = i_samplesize;
623                     i_rate = i_scale * p_stream->i_bitrate / 8;
624                 }
625                 else
626                 {
627                     i_samplesize = 1;
628                     i_scale = 1000;
629                     i_rate = 1000 * p_stream->i_bitrate / 8;
630                 }
631                 bo_AddFCC( p_bo, "auds" );
632                 bo_AddDWordLE( p_bo, 0 );   /* tag */
633                 bo_AddDWordLE( p_bo, 0 );   /* flags */
634                 bo_AddWordLE(  p_bo, 0 );   /* priority */
635                 bo_AddWordLE(  p_bo, 0 );   /* langage */
636                 bo_AddDWordLE( p_bo, 0 );   /* initial frame */
637                 bo_AddDWordLE( p_bo, i_scale );/* scale */
638                 bo_AddDWordLE( p_bo, i_rate );
639                 bo_AddDWordLE( p_bo, 0 );   /* start */
640                 bo_AddDWordLE( p_bo, p_stream->i_frames );
641                 bo_AddDWordLE( p_bo, 10 * 1024 );
642                 bo_AddDWordLE( p_bo, -1 );  /* quality */
643                 bo_AddDWordLE( p_bo, i_samplesize );
644                 bo_AddWordLE(  p_bo, 0 );   /* ??? */
645                 bo_AddWordLE(  p_bo, 0 );   /* ??? */
646                 bo_AddWordLE(  p_bo, 0 );
647                 bo_AddWordLE(  p_bo, 0 );
648             }
649             break;
650     }
651
652     AVI_BOX_EXIT( 0 );
653 }
654
655 static int avi_HeaderAdd_strf( sout_instance_t *p_sout,
656                                buffer_out_t *p_bo,
657                                avi_stream_t *p_stream )
658 {
659 //    sout_mux_t *p_mux = (sout_mux_t*)p_sout->p_mux_data;
660     AVI_BOX_ENTER( "strf" );
661
662     switch( p_stream->i_cat )
663     {
664         case AUDIO_ES:
665             bo_AddWordLE( p_bo, p_stream->p_wf->wFormatTag );
666             bo_AddWordLE( p_bo, p_stream->p_wf->nChannels );
667             bo_AddDWordLE( p_bo, p_stream->p_wf->nSamplesPerSec );
668             bo_AddDWordLE( p_bo, p_stream->p_wf->nAvgBytesPerSec );
669             bo_AddWordLE( p_bo, p_stream->p_wf->nBlockAlign );
670             bo_AddWordLE( p_bo, p_stream->p_wf->wBitsPerSample );
671             bo_AddWordLE( p_bo, p_stream->p_wf->cbSize );
672             bo_AddMem( p_bo, p_stream->p_wf->cbSize, (uint8_t*)&p_stream->p_wf[1] );
673             break;
674         case VIDEO_ES:
675             bo_AddDWordLE( p_bo, p_stream->p_bih->biSize );
676             bo_AddDWordLE( p_bo, p_stream->p_bih->biWidth );
677             bo_AddDWordLE( p_bo, p_stream->p_bih->biHeight );
678             bo_AddWordLE( p_bo, p_stream->p_bih->biPlanes );
679             bo_AddWordLE( p_bo, p_stream->p_bih->biBitCount );
680             if( VLC_FOURCC( 0, 0, 0, 1 ) == 0x00000001 )
681             {
682                 bo_AddDWordBE( p_bo, p_stream->p_bih->biCompression );
683             }
684             else
685             {
686                 bo_AddDWordLE( p_bo, p_stream->p_bih->biCompression );
687             }
688             bo_AddDWordLE( p_bo, p_stream->p_bih->biSizeImage );
689             bo_AddDWordLE( p_bo, p_stream->p_bih->biXPelsPerMeter );
690             bo_AddDWordLE( p_bo, p_stream->p_bih->biYPelsPerMeter );
691             bo_AddDWordLE( p_bo, p_stream->p_bih->biClrUsed );
692             bo_AddDWordLE( p_bo, p_stream->p_bih->biClrImportant );
693             bo_AddMem( p_bo,
694                        p_stream->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
695                        (uint8_t*)&p_stream->p_bih[1] );
696             break;
697     }
698
699     AVI_BOX_EXIT( 0 );
700 }
701
702 static int avi_HeaderAdd_strl( sout_instance_t *p_sout,
703                                buffer_out_t *p_bo,
704                                avi_stream_t *p_stream )
705 {
706 //    sout_mux_t *p_mux = (sout_mux_t*)p_sout->p_mux_data;
707     AVI_BOX_ENTER_LIST( "strl" );
708
709     avi_HeaderAdd_strh( p_sout, p_bo, p_stream );
710     avi_HeaderAdd_strf( p_sout, p_bo, p_stream );
711
712     AVI_BOX_EXIT( 0 );
713 }
714
715 static sout_buffer_t *avi_HeaderCreateRIFF( sout_instance_t *p_sout )
716 {
717     sout_mux_t          *p_mux = (sout_mux_t*)p_sout->p_mux_data;
718     sout_buffer_t       *p_hdr;
719     int                 i_stream;
720     int                 i_maxbytespersec;
721     int                 i_junk;
722     buffer_out_t        bo;
723
724     p_hdr = sout_BufferNew( p_sout, HDR_SIZE );
725     memset( p_hdr->p_buffer, 0, HDR_SIZE );
726
727     bo_Init( &bo, HDR_SIZE, p_hdr->p_buffer );
728
729     bo_AddFCC( &bo, "RIFF" );
730     bo_AddDWordLE( &bo, p_mux->i_movi_size + HDR_SIZE - 8 + p_mux->i_idx1_size );
731     bo_AddFCC( &bo, "AVI " );
732
733     bo_AddFCC( &bo, "LIST" );
734     bo_AddDWordLE( &bo, HDR_SIZE - 8);
735     bo_AddFCC( &bo, "hdrl" );
736
737     avi_HeaderAdd_avih( p_sout, &bo );
738     for( i_stream = 0,i_maxbytespersec = 0; i_stream < p_mux->i_streams; i_stream++ )
739     {
740         avi_HeaderAdd_strl( p_sout, &bo, &p_mux->stream[i_stream] );
741     }
742
743     i_junk = HDR_SIZE - bo.i_buffer - 8 - 12;
744     bo_AddFCC( &bo, "JUNK" );
745     bo_AddDWordLE( &bo, i_junk );
746
747     bo.i_buffer += i_junk;
748     bo_AddFCC( &bo, "LIST" );
749     bo_AddDWordLE( &bo, p_mux->i_movi_size + 4 );
750     bo_AddFCC( &bo, "movi" );
751
752     return( p_hdr );
753 }
754
755 static sout_buffer_t * avi_HeaderCreateidx1( sout_instance_t *p_sout )
756 {
757     sout_mux_t          *p_mux = (sout_mux_t*)p_sout->p_mux_data;
758     sout_buffer_t       *p_idx1;
759     uint32_t            i_idx1_size;
760     unsigned int        i;
761     buffer_out_t        bo;
762
763     i_idx1_size = 16 * p_mux->idx1.i_entry_count;
764
765     p_idx1 = sout_BufferNew( p_sout, i_idx1_size + 8 );
766     memset( p_idx1->p_buffer, 0, i_idx1_size );
767
768     bo_Init( &bo, i_idx1_size, p_idx1->p_buffer );
769     bo_AddFCC( &bo, "idx1" );
770     bo_AddDWordLE( &bo, i_idx1_size );
771
772     for( i = 0; i < p_mux->idx1.i_entry_count; i++ )
773     {
774         bo_AddFCC( &bo, p_mux->idx1.entry[i].fcc );
775         bo_AddDWordLE( &bo, p_mux->idx1.entry[i].i_flags );
776         bo_AddDWordLE( &bo, p_mux->idx1.entry[i].i_pos );
777         bo_AddDWordLE( &bo, p_mux->idx1.entry[i].i_length );
778     }
779
780     return( p_idx1 );
781 }
782
783