]> git.sesse.net Git - vlc/blob - modules/mux/avi.c
* extras/MacOSX/Resources/English.lproj/MainMenu.nib/*
[vlc] / modules / mux / avi.c
1 /*****************************************************************************
2  * avi.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: avi.c,v 1.10 2003/03/11 19:02:30 fenrir 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_mux_t *, int , void *, void * );
67 static int AddStream( sout_mux_t *, sout_input_t * );
68 static int DelStream( sout_mux_t *, sout_input_t * );
69 static int Mux      ( sout_mux_t * );
70
71 static sout_buffer_t *avi_HeaderCreateRIFF( sout_mux_t * );
72 static sout_buffer_t *avi_HeaderCreateidx1( sout_mux_t * );
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 struct sout_mux_sys_t
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 };
151
152 /*****************************************************************************
153  * Open:
154  *****************************************************************************/
155 static int Open( vlc_object_t *p_this )
156 {
157     sout_mux_t      *p_mux = (sout_mux_t*)p_this;
158     sout_mux_sys_t  *p_sys = p_mux->p_sys;
159     sout_buffer_t   *p_hdr;
160
161     p_sys = malloc( sizeof( sout_mux_sys_t ) );
162     p_sys->i_streams = 0;
163     p_sys->i_stream_video = -1;
164     p_sys->i_movi_size = 0;
165
166     p_sys->idx1.i_entry_count = 0;
167     p_sys->idx1.i_entry_max = 10000;
168     p_sys->idx1.entry = calloc( p_sys->idx1.i_entry_max, sizeof( avi_idx1_entry_t ) );
169
170     msg_Info( p_mux, "Open" );
171
172     p_mux->pf_capacity  = Capability;
173     p_mux->pf_addstream = AddStream;
174     p_mux->pf_delstream = DelStream;
175     p_mux->pf_mux       = Mux;
176     p_mux->p_sys        = p_sys;
177     p_mux->i_preheader  = 8; // (fourcc,length) header
178
179     /* room to add header at the end */
180     p_hdr = sout_BufferNew( p_mux->p_sout, HDR_SIZE );
181     memset( p_hdr->p_buffer, 0, HDR_SIZE );
182     sout_AccessOutWrite( p_mux->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_mux_t      *p_mux = (sout_mux_t*)p_this;
194     sout_mux_sys_t  *p_sys = p_mux->p_sys;
195
196     sout_buffer_t       *p_hdr, *p_idx1;
197     int                 i_stream;
198
199     msg_Info( p_mux, "Close" );
200
201     /* first create idx1 chunk (write at the end of the stream */
202     p_idx1 = avi_HeaderCreateidx1( p_mux );
203     p_sys->i_idx1_size = p_idx1->i_size;
204     sout_AccessOutWrite( p_mux->p_access, p_idx1 );
205
206     /* calculate some value for headers creations */
207     for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
208     {
209         avi_stream_t *p_stream;
210
211         p_stream = &p_sys->stream[i_stream];
212
213         p_stream->f_fps = 25;
214         if( p_stream->i_duration > 0 )
215         {
216             p_stream->f_fps = (float)p_stream->i_frames /
217                               ( (float)p_stream->i_duration /
218                                 (float)1000000 );
219         }
220         p_stream->i_bitrate = 128 * 1024;
221         if( p_stream->i_duration > 0 )
222         {
223             p_stream->i_bitrate =
224                 8 * (uint64_t)1000000 *
225                     (uint64_t)p_stream->i_totalsize /
226                     (uint64_t)p_stream->i_duration;
227         }
228         msg_Err( p_mux, "stream[%d] duration:%lld totalsize:%lld frames:%d fps:%f kb/s:%d",
229                  i_stream,
230                  p_stream->i_duration/1000000, p_stream->i_totalsize,
231                  p_stream->i_frames,
232                  p_stream->f_fps, p_stream->i_bitrate/1024 );
233     }
234
235     p_hdr = avi_HeaderCreateRIFF( p_mux );
236     sout_AccessOutSeek( p_mux->p_access, 0 );
237     sout_AccessOutWrite( p_mux->p_access, p_hdr );
238 }
239
240 static int Capability( sout_mux_t *p_mux, int i_query, void *p_args, void *p_answer )
241 {
242    switch( i_query )
243    {
244         case SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME:
245             *(vlc_bool_t*)p_answer = VLC_TRUE;
246             return( SOUT_MUX_CAP_ERR_OK );
247         default:
248             return( SOUT_MUX_CAP_ERR_UNIMPLEMENTED );
249    }
250 }
251
252 static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
253 {
254     sout_mux_sys_t  *p_sys = p_mux->p_sys;
255     avi_stream_t    *p_stream;
256
257     if( p_sys->i_streams >= 100 )
258     {
259         msg_Err( p_mux, "too many streams" );
260         return( -1 );
261     }
262     if( p_input->input_format.p_format == NULL )
263     {
264         msg_Err( p_mux, "stream descriptor missing" );
265         return( -1 );
266     }
267
268     msg_Dbg( p_mux, "adding input" );
269     p_input->p_sys = malloc( sizeof( int ) );
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->input_format.i_cat )
275     {
276         case AUDIO_ES:
277             {
278                 WAVEFORMATEX *p_wf =
279                     (WAVEFORMATEX*)p_input->input_format.p_format;
280
281                 p_stream->i_cat = AUDIO_ES;
282                 p_stream->fcc[0] = '0' + p_sys->i_streams / 10;
283                 p_stream->fcc[1] = '0' + p_sys->i_streams % 10;
284                 p_stream->fcc[2] = 'w';
285                 p_stream->fcc[3] = 'b';
286
287                 p_stream->p_bih = NULL;
288                 p_stream->p_wf  = malloc( sizeof( WAVEFORMATEX ) + p_wf->cbSize );
289                 memcpy( p_stream->p_wf,
290                         p_wf,
291                         sizeof( WAVEFORMATEX ) + p_wf->cbSize);
292             }
293             break;
294         case VIDEO_ES:
295             {
296                 BITMAPINFOHEADER *p_bih =
297                     (BITMAPINFOHEADER*)p_input->input_format.p_format;;
298
299                 p_stream->i_cat = VIDEO_ES;
300                 p_stream->fcc[0] = '0' + p_sys->i_streams / 10;
301                 p_stream->fcc[1] = '0' + p_sys->i_streams % 10;
302                 p_stream->fcc[2] = 'd';
303                 p_stream->fcc[3] = 'c';
304                 if( p_sys->i_stream_video < 0 )
305                 {
306                     p_sys->i_stream_video = p_sys->i_streams;
307                 }
308                 p_stream->p_wf  = NULL;
309                 p_stream->p_bih = malloc( p_bih->biSize );
310                 memcpy( p_stream->p_bih,
311                         p_bih,
312                         p_bih->biSize );
313             }
314             break;
315         default:
316             return( -1 );
317     }
318     p_stream->i_totalsize = 0;
319     p_stream->i_frames     = 0;
320     p_stream->i_duration  = 0;
321
322     p_sys->i_streams++;
323     return( 0 );
324 }
325
326 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
327 {
328
329     msg_Dbg( p_mux, "removing input" );
330
331     free( p_input->p_sys ); p_input->p_sys = NULL;
332
333     return( 0 );
334 }
335
336 static int Mux      ( sout_mux_t *p_mux )
337 {
338     sout_mux_sys_t  *p_sys = p_mux->p_sys;
339     avi_stream_t    *p_stream;
340     int i_stream;
341     int i;
342
343     for( i = 0; i < p_mux->i_nb_inputs; i++ )
344     {
345         int i_count;
346         sout_fifo_t *p_fifo;
347
348         i_stream = *((int*)p_mux->pp_inputs[i]->p_sys );
349         p_stream = &p_sys->stream[i_stream];
350
351         p_fifo = p_mux->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_sys->idx1.entry[p_sys->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_sys->i_movi_size + 4;
369             p_idx->i_length= p_data->i_size;
370             p_sys->idx1.i_entry_count++;
371             if( p_sys->idx1.i_entry_count >= p_sys->idx1.i_entry_max )
372             {
373                 p_sys->idx1.i_entry_max += 10000;
374                 p_sys->idx1.entry = realloc( p_sys->idx1.entry,
375                                              p_sys->idx1.i_entry_max * sizeof( avi_idx1_entry_t ) );
376             }
377
378
379             if( sout_BufferReallocFromPreHeader( p_mux->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_mux->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_mux->p_access, p_hdr );
389                 p_sys->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_mux->p_sout, p_data, p_data->i_size + 1 );
401                 p_data->i_size += 1;
402             }
403
404             sout_AccessOutWrite( p_mux->p_access, p_data );
405             p_sys->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_mux_t *p_mux,
515                                buffer_out_t *p_bo )
516 {
517     sout_mux_sys_t  *p_sys = p_mux->p_sys;
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_sys->i_stream_video >= 0 )
526     {
527         p_video = &p_sys->stream[p_sys->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_sys->stream[p_sys->i_stream_video].f_fps );
539         i_totalframes = p_sys->stream[p_sys->i_stream_video].i_frames;
540     }
541     else
542     {
543         msg_Warn( p_mux, "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_sys->i_streams; i_stream++ )
549     {
550         if( p_sys->stream[p_sys->i_stream_video].i_duration > 0 )
551         {
552             i_maxbytespersec +=
553                 p_sys->stream[p_sys->i_stream_video].i_totalsize /
554                 p_sys->stream[p_sys->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_sys->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_mux_t   *p_mux,
586                                buffer_out_t *p_bo,
587                                avi_stream_t *p_stream )
588 {
589     AVI_BOX_ENTER( "strh" );
590
591     switch( p_stream->i_cat )
592     {
593         case VIDEO_ES:
594             {
595                 bo_AddFCC( p_bo, "vids" );
596                 bo_AddDWordBE( p_bo, p_stream->p_bih->biCompression );
597                 bo_AddDWordLE( p_bo, 0 );   /* flags */
598                 bo_AddWordLE(  p_bo, 0 );   /* priority */
599                 bo_AddWordLE(  p_bo, 0 );   /* langage */
600                 bo_AddDWordLE( p_bo, 0 );   /* initial frame */
601                 bo_AddDWordLE( p_bo, 1000 );/* scale */
602                 bo_AddDWordLE( p_bo, (uint32_t)( 1000 * p_stream->f_fps ));
603                 bo_AddDWordLE( p_bo, 0 );   /* start */
604                 bo_AddDWordLE( p_bo, p_stream->i_frames );
605                 bo_AddDWordLE( p_bo, 1024 * 1024 );
606                 bo_AddDWordLE( p_bo, -1 );  /* quality */
607                 bo_AddDWordLE( p_bo, 0 );   /* samplesize */
608                 bo_AddWordLE(  p_bo, 0 );   /* ??? */
609                 bo_AddWordLE(  p_bo, 0 );   /* ??? */
610                 bo_AddWordLE(  p_bo, p_stream->p_bih->biWidth );
611                 bo_AddWordLE(  p_bo, p_stream->p_bih->biHeight );
612             }
613             break;
614         case AUDIO_ES:
615             {
616                 int i_rate, i_scale, i_samplesize;
617
618                 i_samplesize = p_stream->p_wf->nBlockAlign;
619                 if( i_samplesize > 1 )
620                 {
621                     i_scale = i_samplesize;
622                     i_rate = i_scale * p_stream->i_bitrate / 8;
623                 }
624                 else
625                 {
626                     i_samplesize = 1;
627                     i_scale = 1000;
628                     i_rate = 1000 * p_stream->i_bitrate / 8;
629                 }
630                 bo_AddFCC( p_bo, "auds" );
631                 bo_AddDWordLE( p_bo, 0 );   /* tag */
632                 bo_AddDWordLE( p_bo, 0 );   /* flags */
633                 bo_AddWordLE(  p_bo, 0 );   /* priority */
634                 bo_AddWordLE(  p_bo, 0 );   /* langage */
635                 bo_AddDWordLE( p_bo, 0 );   /* initial frame */
636                 bo_AddDWordLE( p_bo, i_scale );/* scale */
637                 bo_AddDWordLE( p_bo, i_rate );
638                 bo_AddDWordLE( p_bo, 0 );   /* start */
639                 bo_AddDWordLE( p_bo, p_stream->i_frames );
640                 bo_AddDWordLE( p_bo, 10 * 1024 );
641                 bo_AddDWordLE( p_bo, -1 );  /* quality */
642                 bo_AddDWordLE( p_bo, i_samplesize );
643                 bo_AddWordLE(  p_bo, 0 );   /* ??? */
644                 bo_AddWordLE(  p_bo, 0 );   /* ??? */
645                 bo_AddWordLE(  p_bo, 0 );
646                 bo_AddWordLE(  p_bo, 0 );
647             }
648             break;
649     }
650
651     AVI_BOX_EXIT( 0 );
652 }
653
654 static int avi_HeaderAdd_strf( sout_mux_t *p_mux,
655                                buffer_out_t *p_bo,
656                                avi_stream_t *p_stream )
657 {
658     AVI_BOX_ENTER( "strf" );
659
660     switch( p_stream->i_cat )
661     {
662         case AUDIO_ES:
663             bo_AddWordLE( p_bo, p_stream->p_wf->wFormatTag );
664             bo_AddWordLE( p_bo, p_stream->p_wf->nChannels );
665             bo_AddDWordLE( p_bo, p_stream->p_wf->nSamplesPerSec );
666             bo_AddDWordLE( p_bo, p_stream->p_wf->nAvgBytesPerSec );
667             bo_AddWordLE( p_bo, p_stream->p_wf->nBlockAlign );
668             bo_AddWordLE( p_bo, p_stream->p_wf->wBitsPerSample );
669             bo_AddWordLE( p_bo, p_stream->p_wf->cbSize );
670             bo_AddMem( p_bo, p_stream->p_wf->cbSize, (uint8_t*)&p_stream->p_wf[1] );
671             break;
672         case VIDEO_ES:
673             bo_AddDWordLE( p_bo, p_stream->p_bih->biSize );
674             bo_AddDWordLE( p_bo, p_stream->p_bih->biWidth );
675             bo_AddDWordLE( p_bo, p_stream->p_bih->biHeight );
676             bo_AddWordLE( p_bo, p_stream->p_bih->biPlanes );
677             bo_AddWordLE( p_bo, p_stream->p_bih->biBitCount );
678             if( VLC_FOURCC( 0, 0, 0, 1 ) == 0x00000001 )
679             {
680                 bo_AddDWordBE( p_bo, p_stream->p_bih->biCompression );
681             }
682             else
683             {
684                 bo_AddDWordLE( p_bo, p_stream->p_bih->biCompression );
685             }
686             bo_AddDWordLE( p_bo, p_stream->p_bih->biSizeImage );
687             bo_AddDWordLE( p_bo, p_stream->p_bih->biXPelsPerMeter );
688             bo_AddDWordLE( p_bo, p_stream->p_bih->biYPelsPerMeter );
689             bo_AddDWordLE( p_bo, p_stream->p_bih->biClrUsed );
690             bo_AddDWordLE( p_bo, p_stream->p_bih->biClrImportant );
691             bo_AddMem( p_bo,
692                        p_stream->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
693                        (uint8_t*)&p_stream->p_bih[1] );
694             break;
695     }
696
697     AVI_BOX_EXIT( 0 );
698 }
699
700 static int avi_HeaderAdd_strl( sout_mux_t *p_mux,
701                                buffer_out_t *p_bo,
702                                avi_stream_t *p_stream )
703 {
704     AVI_BOX_ENTER_LIST( "strl" );
705
706     avi_HeaderAdd_strh( p_mux, p_bo, p_stream );
707     avi_HeaderAdd_strf( p_mux, p_bo, p_stream );
708
709     AVI_BOX_EXIT( 0 );
710 }
711
712 static sout_buffer_t *avi_HeaderCreateRIFF( sout_mux_t *p_mux )
713 {
714     sout_mux_sys_t      *p_sys = p_mux->p_sys;
715     sout_buffer_t       *p_hdr;
716     int                 i_stream;
717     int                 i_maxbytespersec;
718     int                 i_junk;
719     buffer_out_t        bo;
720
721     p_hdr = sout_BufferNew( p_mux->p_sout, HDR_SIZE );
722     memset( p_hdr->p_buffer, 0, HDR_SIZE );
723
724     bo_Init( &bo, HDR_SIZE, p_hdr->p_buffer );
725
726     bo_AddFCC( &bo, "RIFF" );
727     bo_AddDWordLE( &bo, p_sys->i_movi_size + HDR_SIZE - 8 + p_sys->i_idx1_size );
728     bo_AddFCC( &bo, "AVI " );
729
730     bo_AddFCC( &bo, "LIST" );
731     bo_AddDWordLE( &bo, HDR_SIZE - 8);
732     bo_AddFCC( &bo, "hdrl" );
733
734     avi_HeaderAdd_avih( p_mux, &bo );
735     for( i_stream = 0,i_maxbytespersec = 0; i_stream < p_sys->i_streams; i_stream++ )
736     {
737         avi_HeaderAdd_strl( p_mux, &bo, &p_sys->stream[i_stream] );
738     }
739
740     i_junk = HDR_SIZE - bo.i_buffer - 8 - 12;
741     bo_AddFCC( &bo, "JUNK" );
742     bo_AddDWordLE( &bo, i_junk );
743
744     bo.i_buffer += i_junk;
745     bo_AddFCC( &bo, "LIST" );
746     bo_AddDWordLE( &bo, p_sys->i_movi_size + 4 );
747     bo_AddFCC( &bo, "movi" );
748
749     return( p_hdr );
750 }
751
752 static sout_buffer_t * avi_HeaderCreateidx1( sout_mux_t *p_mux )
753 {
754     sout_mux_sys_t      *p_sys = p_mux->p_sys;
755     sout_buffer_t       *p_idx1;
756     uint32_t            i_idx1_size;
757     unsigned int        i;
758     buffer_out_t        bo;
759
760     i_idx1_size = 16 * p_sys->idx1.i_entry_count;
761
762     p_idx1 = sout_BufferNew( p_mux->p_sout, i_idx1_size + 8 );
763     memset( p_idx1->p_buffer, 0, i_idx1_size );
764
765     bo_Init( &bo, i_idx1_size, p_idx1->p_buffer );
766     bo_AddFCC( &bo, "idx1" );
767     bo_AddDWordLE( &bo, i_idx1_size );
768
769     for( i = 0; i < p_sys->idx1.i_entry_count; i++ )
770     {
771         bo_AddFCC( &bo, p_sys->idx1.entry[i].fcc );
772         bo_AddDWordLE( &bo, p_sys->idx1.entry[i].i_flags );
773         bo_AddDWordLE( &bo, p_sys->idx1.entry[i].i_pos );
774         bo_AddDWordLE( &bo, p_sys->idx1.entry[i].i_length );
775     }
776
777     return( p_idx1 );
778 }
779
780