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