]> git.sesse.net Git - vlc/blobdiff - modules/mux/avi.c
avcodec: pass AVCodecContext to VA destroy callback
[vlc] / modules / mux / avi.c
index d86b688aa4e02defb6298ce7396eafaca2681386..bc72fc1f93df4b929910f1f168a7995e177d8748 100644 (file)
@@ -1,24 +1,24 @@
 /*****************************************************************************
  * avi.c
  *****************************************************************************
- * Copyright (C) 2001, 2002 VideoLAN
- * $Id: avi.c,v 1.18 2004/02/06 23:43:32 gbazin Exp $
+ * Copyright (C) 2001-2009 VLC authors and VideoLAN
+ * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
  *****************************************************************************/
 /* TODO: add OpenDML write support */
 
-#include <stdlib.h>
 
-#include <vlc/vlc.h>
-#include <vlc/input.h>
-#include <vlc/sout.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
 
-#include "codecs.h"
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_sout.h>
+#include <vlc_block.h>
+#include <vlc_codecs.h>
 
 /*****************************************************************************
  * Module descriptor
 static int  Open   ( vlc_object_t * );
 static void Close  ( vlc_object_t * );
 
-vlc_module_begin();
-    set_description( _("AVI muxer") );
-    set_capability( "sout mux", 5 );
-    add_shortcut( "avi" );
-    set_callbacks( Open, Close );
-vlc_module_end();
+#define SOUT_CFG_PREFIX "sout-avi-"
+
+#define CFG_ARTIST_TEXT     N_("Artist")
+#define CFG_DATE_TEXT       N_("Date")
+#define CFG_GENRE_TEXT      N_("Genre")
+#define CFG_COPYRIGHT_TEXT  N_("Copyright")
+#define CFG_COMMENT_TEXT    N_("Comment")
+#define CFG_NAME_TEXT       N_("Name")
+#define CFG_SUBJECT_TEXT    N_("Subject")
+#define CFG_ENCODER_TEXT    N_("Encoder")
+#define CFG_KEYWORDS_TEXT   N_("Keywords")
+
+vlc_module_begin ()
+    set_description( N_("AVI muxer") )
+    set_category( CAT_SOUT )
+    set_subcategory( SUBCAT_SOUT_MUX )
+    set_capability( "sout mux", 5 )
+    add_shortcut( "avi" )
+
+    add_string( SOUT_CFG_PREFIX "artist", NULL,    CFG_ARTIST_TEXT, NULL, true )
+    add_string( SOUT_CFG_PREFIX "date",   NULL,    CFG_DATE_TEXT, NULL, true )
+    add_string( SOUT_CFG_PREFIX "genre",  NULL,    CFG_GENRE_TEXT, NULL, true )
+    add_string( SOUT_CFG_PREFIX "copyright", NULL, CFG_COPYRIGHT_TEXT, NULL, true )
+    add_string( SOUT_CFG_PREFIX "comment", NULL,   CFG_COMMENT_TEXT, NULL, true )
+    add_string( SOUT_CFG_PREFIX "name", NULL,      CFG_NAME_TEXT, NULL, true )
+    add_string( SOUT_CFG_PREFIX "subject", NULL,   CFG_SUBJECT_TEXT, NULL, true )
+    add_string( SOUT_CFG_PREFIX "encoder",
+                "VLC Media Player - " VERSION_MESSAGE,
+                                                   CFG_ENCODER_TEXT, NULL, true )
+    add_string( SOUT_CFG_PREFIX "keywords", NULL,  CFG_KEYWORDS_TEXT, NULL, true )
+
+    set_callbacks( Open, Close )
+vlc_module_end ()
 
 
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
-static int  Capability(sout_mux_t *, int , void *, void * );
-static int  AddStream( sout_mux_t *, sout_input_t * );
-static int  DelStream( sout_mux_t *, sout_input_t * );
-static int  Mux      ( sout_mux_t * );
+static int Control( sout_mux_t *, int, va_list );
+static int AddStream( sout_mux_t *, sout_input_t * );
+static int DelStream( sout_mux_t *, sout_input_t * );
+static int Mux      ( sout_mux_t * );
 
 typedef struct avi_stream_s
 {
@@ -62,7 +92,7 @@ typedef struct avi_stream_s
 
     char fcc[4];
 
-    mtime_t i_duration;       // in µs
+    mtime_t i_duration;       // in Âµs
 
     int     i_frames;        // total frame count
     int64_t i_totalsize;    // total stream size
@@ -70,8 +100,8 @@ typedef struct avi_stream_s
     float   f_fps;
     int     i_bitrate;
 
-    BITMAPINFOHEADER    *p_bih;
-    WAVEFORMATEX        *p_wf;
+    VLC_BITMAPINFOHEADER    *p_bih;
+    WAVEFORMATEX            *p_wf;
 
 } avi_stream_t;
 
@@ -94,7 +124,7 @@ typedef struct avi_idx1_s
 
 struct sout_mux_sys_t
 {
-    vlc_bool_t b_write_header;
+    bool b_write_header;
 
     int i_streams;
     int i_stream_video;
@@ -107,8 +137,7 @@ struct sout_mux_sys_t
 
 };
 
-// FIXME FIXME
-#define HDR_SIZE 10240
+#define HDR_BASE_SIZE 512 /* single video&audio ~ 400 bytes header */
 
 /* Flags in avih */
 #define AVIF_HASINDEX       0x00000010  // Index at end of file?
@@ -119,8 +148,8 @@ struct sout_mux_sys_t
 #define AVIIF_KEYFRAME      0x00000010L /* this frame is a key frame.*/
 
 
-static sout_buffer_t *avi_HeaderCreateRIFF( sout_mux_t * );
-static sout_buffer_t *avi_HeaderCreateidx1( sout_mux_t * );
+static block_t *avi_HeaderCreateRIFF( sout_mux_t * );
+static block_t *avi_HeaderCreateidx1( sout_mux_t * );
 
 static void SetFCC( uint8_t *p, char *fcc )
 {
@@ -133,11 +162,13 @@ static void SetFCC( uint8_t *p, char *fcc )
 static int Open( vlc_object_t *p_this )
 {
     sout_mux_t      *p_mux = (sout_mux_t*)p_this;
-    sout_mux_sys_t  *p_sys = p_mux->p_sys;
+    sout_mux_sys_t  *p_sys;
 
     msg_Dbg( p_mux, "AVI muxer opened" );
 
     p_sys = malloc( sizeof( sout_mux_sys_t ) );
+    if( !p_sys )
+        return VLC_ENOMEM;
     p_sys->i_streams = 0;
     p_sys->i_stream_video = -1;
     p_sys->i_movi_size = 0;
@@ -146,15 +177,19 @@ static int Open( vlc_object_t *p_this )
     p_sys->idx1.i_entry_max = 10000;
     p_sys->idx1.entry = calloc( p_sys->idx1.i_entry_max,
                                 sizeof( avi_idx1_entry_t ) );
-    p_sys->b_write_header = VLC_TRUE;
+    if( !p_sys->idx1.entry )
+    {
+        free( p_sys );
+        return VLC_ENOMEM;
+    }
+    p_sys->b_write_header = true;
 
 
-    p_mux->pf_capacity  = Capability;
+    p_mux->pf_control   = Control;
     p_mux->pf_addstream = AddStream;
     p_mux->pf_delstream = DelStream;
     p_mux->pf_mux       = Mux;
     p_mux->p_sys        = p_sys;
-    p_mux->i_preheader  = 8;    /* (fourcc,length) header */
 
     return VLC_SUCCESS;
 }
@@ -167,14 +202,14 @@ static void Close( vlc_object_t * p_this )
     sout_mux_t      *p_mux = (sout_mux_t*)p_this;
     sout_mux_sys_t  *p_sys = p_mux->p_sys;
 
-    sout_buffer_t       *p_hdr, *p_idx1;
+    block_t       *p_hdr, *p_idx1;
     int                 i_stream;
 
     msg_Dbg( p_mux, "AVI muxer closed" );
 
     /* first create idx1 chunk (write at the end of the stream */
     p_idx1 = avi_HeaderCreateidx1( p_mux );
-    p_sys->i_idx1_size = p_idx1->i_size;
+    p_sys->i_idx1_size = p_idx1->i_buffer;
     sout_AccessOutWrite( p_mux->p_access, p_idx1 );
 
     /* calculate some value for headers creations */
@@ -199,8 +234,8 @@ static void Close( vlc_object_t * p_this )
                     (uint64_t)p_stream->i_totalsize /
                     (uint64_t)p_stream->i_duration;
         }
-        msg_Info( p_mux, "stream[%d] duration:"I64Fd" totalsize:"I64Fd
-                  " frames:%d fps:%f kb/s:%d",
+        msg_Info( p_mux, "stream[%d] duration:%"PRId64" totalsize:%"PRId64
+                  " frames:%d fps:%f KiB/s:%d",
                   i_stream,
                   (int64_t)p_stream->i_duration / (int64_t)1000000,
                   p_stream->i_totalsize,
@@ -211,18 +246,43 @@ static void Close( vlc_object_t * p_this )
     p_hdr = avi_HeaderCreateRIFF( p_mux );
     sout_AccessOutSeek( p_mux->p_access, 0 );
     sout_AccessOutWrite( p_mux->p_access, p_hdr );
+
+    for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
+    {
+        avi_stream_t *p_stream;
+        p_stream = &p_sys->stream[i_stream];
+        free( p_stream->p_bih );
+        free( p_stream->p_wf );
+    }
+    free( p_sys->idx1.entry );
+    free( p_sys );
 }
 
-static int Capability( sout_mux_t *p_mux, int i_query,
-                       void *p_args, void *p_answer )
+static int Control( sout_mux_t *p_mux, int i_query, va_list args )
 {
+    VLC_UNUSED(p_mux);
+    bool *pb_bool;
+    char **ppsz;
+
    switch( i_query )
    {
-        case SOUT_MUX_CAP_GET_ADD_STREAM_ANY_TIME:
-            *(vlc_bool_t*)p_answer = VLC_FALSE;
-            return( SOUT_MUX_CAP_ERR_OK );
+       case MUX_CAN_ADD_STREAM_WHILE_MUXING:
+           pb_bool = (bool*)va_arg( args, bool * );
+           *pb_bool = false;
+           return VLC_SUCCESS;
+
+       case MUX_GET_ADD_STREAM_WAIT:
+           pb_bool = (bool*)va_arg( args, bool * );
+           *pb_bool = true;
+           return VLC_SUCCESS;
+
+       case MUX_GET_MIME:
+           ppsz = (char**)va_arg( args, char ** );
+           *ppsz = strdup( "video/avi" );
+           return VLC_SUCCESS;
+
         default:
-            return( SOUT_MUX_CAP_ERR_UNIMPLEMENTED );
+            return VLC_EGENERIC;
    }
 }
 
@@ -234,11 +294,13 @@ static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
     if( p_sys->i_streams >= 100 )
     {
         msg_Err( p_mux, "too many streams" );
-        return( -1 );
+        return VLC_EGENERIC;
     }
 
     msg_Dbg( p_mux, "adding input" );
     p_input->p_sys = malloc( sizeof( int ) );
+    if( !p_input->p_sys )
+        return VLC_ENOMEM;
 
     *((int*)p_input->p_sys) = p_sys->i_streams;
     p_stream = &p_sys->stream[p_sys->i_streams];
@@ -256,6 +318,11 @@ static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
 
             p_stream->p_wf  = malloc( sizeof( WAVEFORMATEX ) +
                                       p_input->p_fmt->i_extra );
+            if( !p_stream->p_wf )
+            {
+                free( p_input->p_sys );
+                return VLC_ENOMEM;
+            }
 #define p_wf p_stream->p_wf
             p_wf->cbSize = p_input->p_fmt->i_extra;
             if( p_wf->cbSize > 0 )
@@ -272,41 +339,54 @@ static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
 
             switch( p_input->p_fmt->i_codec )
             {
-                case VLC_FOURCC( 'a', '5', '2', ' ' ):
+                case VLC_CODEC_A52:
                     p_wf->wFormatTag = WAVE_FORMAT_A52;
+                    p_wf->nBlockAlign= 1;
                     break;
-                case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
+                case VLC_CODEC_MP3:
                     p_wf->wFormatTag = WAVE_FORMAT_MPEGLAYER3;
+                    p_wf->nBlockAlign= 1;
                     break;
-                case VLC_FOURCC( 'w', 'm', 'a', '1' ):
+                case VLC_CODEC_WMA1:
                     p_wf->wFormatTag = WAVE_FORMAT_WMA1;
                     break;
-                case VLC_FOURCC( 'w', 'm', 'a', '2' ):
+                case VLC_CODEC_WMA2:
                     p_wf->wFormatTag = WAVE_FORMAT_WMA2;
                     break;
-                case VLC_FOURCC( 'w', 'm', 'a', '3' ):
-                    p_wf->wFormatTag = WAVE_FORMAT_WMA3;
+                case VLC_CODEC_WMAP:
+                    p_wf->wFormatTag = WAVE_FORMAT_WMAP;
+                    break;
+                case VLC_CODEC_WMAL:
+                    p_wf->wFormatTag = WAVE_FORMAT_WMAL;
                     break;
                     /* raw codec */
-                case VLC_FOURCC( 'u', '8', ' ', ' ' ):
+                case VLC_CODEC_U8:
                     p_wf->wFormatTag = WAVE_FORMAT_PCM;
                     p_wf->nBlockAlign= p_wf->nChannels;
                     p_wf->wBitsPerSample = 8;
+                    p_wf->nAvgBytesPerSec = (p_wf->wBitsPerSample/8) *
+                                      p_wf->nSamplesPerSec * p_wf->nChannels;
                     break;
-                case VLC_FOURCC( 's', '1', '6', 'l' ):
+                case VLC_CODEC_S16L:
                     p_wf->wFormatTag = WAVE_FORMAT_PCM;
                     p_wf->nBlockAlign= 2 * p_wf->nChannels;
                     p_wf->wBitsPerSample = 16;
+                    p_wf->nAvgBytesPerSec = (p_wf->wBitsPerSample/8) *
+                                      p_wf->nSamplesPerSec * p_wf->nChannels;
                     break;
-                case VLC_FOURCC( 's', '2', '4', 'l' ):
+                case VLC_CODEC_S24L:
                     p_wf->wFormatTag = WAVE_FORMAT_PCM;
                     p_wf->nBlockAlign= 3 * p_wf->nChannels;
                     p_wf->wBitsPerSample = 24;
+                    p_wf->nAvgBytesPerSec = (p_wf->wBitsPerSample/8) *
+                                      p_wf->nSamplesPerSec * p_wf->nChannels;
                     break;
-                case VLC_FOURCC( 's', '3', '2', 'l' ):
+                case VLC_CODEC_S32L:
                     p_wf->wFormatTag = WAVE_FORMAT_PCM;
                     p_wf->nBlockAlign= 4 * p_wf->nChannels;
                     p_wf->wBitsPerSample = 32;
+                    p_wf->nAvgBytesPerSec = (p_wf->wBitsPerSample/8) *
+                                      p_wf->nSamplesPerSec * p_wf->nChannels;
                     break;
                 default:
                     return VLC_EGENERIC;
@@ -324,10 +404,15 @@ static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
                 p_sys->i_stream_video = p_sys->i_streams;
             }
             p_stream->p_wf  = NULL;
-            p_stream->p_bih = malloc( sizeof( BITMAPINFOHEADER ) +
+            p_stream->p_bih = malloc( sizeof( VLC_BITMAPINFOHEADER ) +
                                       p_input->p_fmt->i_extra );
+            if( !p_stream->p_bih )
+            {
+                free( p_input->p_sys );
+                return VLC_ENOMEM;
+            }
 #define p_bih p_stream->p_bih
-            p_bih->biSize  = sizeof( BITMAPINFOHEADER ) +
+            p_bih->biSize  = sizeof( VLC_BITMAPINFOHEADER ) +
                              p_input->p_fmt->i_extra;
             if( p_input->p_fmt->i_extra > 0 )
             {
@@ -346,11 +431,11 @@ static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
             p_bih->biClrImportant   = 0;
             switch( p_input->p_fmt->i_codec )
             {
-                case VLC_FOURCC( 'm', 'p', '4', 'v' ):
+                case VLC_CODEC_MP4V:
                     p_bih->biCompression = VLC_FOURCC( 'X', 'V', 'I', 'D' );
                     break;
                 default:
-                    p_bih->biCompression = p_input->p_fmt->i_codec;
+                    p_bih->biCompression = p_input->p_fmt->i_original_fourcc ?: p_input->p_fmt->i_codec;
                     break;
             }
 #undef p_bih
@@ -372,102 +457,112 @@ static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
 
 static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
 {
-
     msg_Dbg( p_mux, "removing input" );
 
     free( p_input->p_sys );
-    return( 0 );
+    return 0;
 }
 
 static int Mux      ( sout_mux_t *p_mux )
 {
     sout_mux_sys_t  *p_sys = p_mux->p_sys;
     avi_stream_t    *p_stream;
-    int i_stream;
-    int i;
+    int i_stream, i;
 
     if( p_sys->b_write_header )
     {
-        sout_buffer_t *p_hdr;
+        block_t *p_hdr;
 
         msg_Dbg( p_mux, "writing header" );
 
         p_hdr = avi_HeaderCreateRIFF( p_mux );
         sout_AccessOutWrite( p_mux->p_access, p_hdr );
 
-        p_sys->b_write_header = VLC_FALSE;
+        p_sys->b_write_header = false;
     }
 
     for( i = 0; i < p_mux->i_nb_inputs; i++ )
     {
         int i_count;
-        sout_fifo_t *p_fifo;
+        block_fifo_t *p_fifo;
 
         i_stream = *((int*)p_mux->pp_inputs[i]->p_sys );
         p_stream = &p_sys->stream[i_stream];
 
         p_fifo = p_mux->pp_inputs[i]->p_fifo;
-        i_count = p_fifo->i_depth;
-        while( i_count > 0 )
+        i_count = block_FifoCount(  p_fifo );
+        while( i_count > 1 )
         {
             avi_idx1_entry_t *p_idx;
-            sout_buffer_t *p_data;
+            block_t *p_data;
+
+            p_data = block_FifoGet( p_fifo );
+            if( block_FifoCount( p_fifo ) > 0 )
+            {
+                block_t *p_next = block_FifoShow( p_fifo );
+                p_data->i_length = p_next->i_dts - p_data->i_dts;
+            }
 
-            p_data = sout_FifoGet( p_fifo );
+
+            if( p_stream->i_frames == 0 &&p_stream->i_cat == VIDEO_ES )
+            {
+               /* Add header present at the end of BITMAP info header
+                  to first frame in case of XVID */
+               if( p_stream->p_bih->biCompression
+                               == VLC_FOURCC( 'X', 'V', 'I', 'D' ) )
+               {
+                   int i_header_length =
+                       p_stream->p_bih->biSize - sizeof(VLC_BITMAPINFOHEADER);
+                   p_data = block_Realloc( p_data,
+                                   i_header_length, p_data->i_buffer );
+                   if( !p_data)
+                       return VLC_ENOMEM;
+                   memcpy(p_data->p_buffer,&p_stream->p_bih[1], i_header_length);
+               }
+            }
 
             p_stream->i_frames++;
             if( p_data->i_length < 0 )
             {
                 msg_Warn( p_mux, "argg length < 0 l" );
-                sout_BufferDelete( p_mux->p_sout, p_data );
+                block_Release( p_data );
                 i_count--;
                 continue;
             }
             p_stream->i_duration  += p_data->i_length;
-            p_stream->i_totalsize += p_data->i_size;
+            p_stream->i_totalsize += p_data->i_buffer;
 
             /* add idx1 entry for this frame */
             p_idx = &p_sys->idx1.entry[p_sys->idx1.i_entry_count];
             memcpy( p_idx->fcc, p_stream->fcc, 4 );
-            p_idx->i_flags = AVIIF_KEYFRAME;
+            p_idx->i_flags = 0;
+            if( ( p_data->i_flags & BLOCK_FLAG_TYPE_MASK ) == 0 || ( p_data->i_flags & BLOCK_FLAG_TYPE_I ) )
+                p_idx->i_flags = AVIIF_KEYFRAME;
             p_idx->i_pos   = p_sys->i_movi_size + 4;
-            p_idx->i_length= p_data->i_size;
+            p_idx->i_length= p_data->i_buffer;
             p_sys->idx1.i_entry_count++;
             if( p_sys->idx1.i_entry_count >= p_sys->idx1.i_entry_max )
             {
                 p_sys->idx1.i_entry_max += 10000;
-                p_sys->idx1.entry = realloc( p_sys->idx1.entry,
-                                             p_sys->idx1.i_entry_max * sizeof( avi_idx1_entry_t ) );
+                p_sys->idx1.entry = xrealloc( p_sys->idx1.entry,
+                       p_sys->idx1.i_entry_max * sizeof( avi_idx1_entry_t ) );
             }
 
-
-            if( sout_BufferReallocFromPreHeader( p_mux->p_sout, p_data, 8 ) )
-            {
-                /* there isn't enough data in preheader */
-                sout_buffer_t *p_hdr;
-
-                p_hdr = sout_BufferNew( p_mux->p_sout, 8 );
-                SetFCC( p_hdr->p_buffer, p_stream->fcc );
-                SetDWLE( p_hdr->p_buffer + 4, p_data->i_size );
-
-                sout_AccessOutWrite( p_mux->p_access, p_hdr );
-                p_sys->i_movi_size += p_hdr->i_size;
-
-            }
-            else
+            p_data = block_Realloc( p_data, 8, p_data->i_buffer );
+            if( p_data )
             {
                 SetFCC( p_data->p_buffer, p_stream->fcc );
-                SetDWLE( p_data->p_buffer + 4, p_data->i_size - 8 );
-            }
+                SetDWLE( p_data->p_buffer + 4, p_data->i_buffer - 8 );
 
-            if( p_data->i_size & 0x01 )
-            {
-                sout_BufferRealloc( p_mux->p_sout, p_data, p_data->i_size + 1 );
-                p_data->i_size += 1;
-            }
+                if( p_data->i_buffer & 0x01 )
+                {
+                    p_data = block_Realloc( p_data, 0, p_data->i_buffer + 1 );
+                    p_data->p_buffer[ p_data->i_buffer - 1 ] = '\0';
+                }
 
-            p_sys->i_movi_size += p_data->i_size;
-            sout_AccessOutWrite( p_mux->p_access, p_data );
+                p_sys->i_movi_size += p_data->i_buffer;
+                sout_AccessOutWrite( p_mux->p_access, p_data );
+            }
 
             i_count--;
         }
@@ -484,24 +579,41 @@ static int Mux      ( sout_mux_t *p_mux )
 typedef struct buffer_out_s
 {
     int      i_buffer_size;
-    int      i_buffer;
-    uint8_t  *p_buffer;
-
+    block_t  *p_block;
 } buffer_out_t;
 
-static void bo_Init( buffer_out_t *p_bo, int i_size, uint8_t *p_buffer )
+static void bo_Init( buffer_out_t *p_bo, int i_size )
 {
+    p_bo->p_block = block_Alloc( i_size );
+    p_bo->p_block->i_buffer = 0;
     p_bo->i_buffer_size = i_size;
-    p_bo->i_buffer = 0;
-    p_bo->p_buffer = p_buffer;
 }
-static void bo_AddByte( buffer_out_t *p_bo, uint8_t i )
+
+static void bo_SetByte( buffer_out_t *p_bo, int i_offset, uint8_t i )
 {
-    if( p_bo->i_buffer < p_bo->i_buffer_size )
+    if( i_offset >= p_bo->i_buffer_size )
     {
-        p_bo->p_buffer[p_bo->i_buffer] = i;
+        int i_growth = HDR_BASE_SIZE;
+        while( i_offset >= p_bo->i_buffer_size + i_growth )
+        {
+            i_growth += HDR_BASE_SIZE;
+        }
+        int i = p_bo->p_block->i_buffer; /* Realloc would set payload size == buffer size */
+        p_bo->p_block = block_Realloc( p_bo->p_block, 0, p_bo->i_buffer_size + i_growth );
+        p_bo->p_block->i_buffer = i;
+        p_bo->i_buffer_size += i_growth;
     }
-    p_bo->i_buffer++;
+    p_bo->p_block->p_buffer[i_offset] = i;
+}
+static void bo_AddByte( buffer_out_t *p_bo, uint8_t i )
+{
+    bo_SetByte( p_bo, p_bo->p_block->i_buffer, i );
+    p_bo->p_block->i_buffer++;
+}
+static void bo_SetWordLE( buffer_out_t *p_bo, int i_offset, uint16_t i )
+{
+    bo_SetByte( p_bo, i_offset, i &0xff );
+    bo_SetByte( p_bo, i_offset + 1, ( ( i >> 8) &0xff ) );
 }
 static void bo_AddWordLE( buffer_out_t *p_bo, uint16_t i )
 {
@@ -513,6 +625,11 @@ static void bo_AddWordBE( buffer_out_t *p_bo, uint16_t i )
     bo_AddByte( p_bo, ( ( i >> 8) &0xff ) );
     bo_AddByte( p_bo, i &0xff );
 }
+static void bo_SetDWordLE( buffer_out_t *p_bo, int i_offset, uint32_t i )
+{
+    bo_SetWordLE( p_bo, i_offset, i &0xffff );
+    bo_SetWordLE( p_bo, i_offset + 2, ( ( i >> 16) &0xffff ) );
+}
 static void bo_AddDWordLE( buffer_out_t *p_bo, uint32_t i )
 {
     bo_AddWordLE( p_bo, i &0xffff );
@@ -536,7 +653,7 @@ static void bo_AddLWordBE( buffer_out_t *p_bo, uint64_t i )
 }
 #endif
 
-static void bo_AddFCC( buffer_out_t *p_bo, char *fcc )
+static void bo_AddFCC( buffer_out_t *p_bo, const char *fcc )
 {
     bo_AddByte( p_bo, fcc[0] );
     bo_AddByte( p_bo, fcc[1] );
@@ -562,9 +679,9 @@ static void bo_AddMem( buffer_out_t *p_bo, int i_size, uint8_t *p_mem )
  ****************************************************************************
  ****************************************************************************/
 #define AVI_BOX_ENTER( fcc ) \
-    buffer_out_t _bo_sav_; \
+    int i_datasize_offset; \
     bo_AddFCC( p_bo, fcc ); \
-    _bo_sav_ = *p_bo; \
+    i_datasize_offset = p_bo->p_block->i_buffer; \
     bo_AddDWordLE( p_bo, 0 )
 
 #define AVI_BOX_ENTER_LIST( fcc ) \
@@ -572,8 +689,8 @@ static void bo_AddMem( buffer_out_t *p_bo, int i_size, uint8_t *p_mem )
     bo_AddFCC( p_bo, fcc )
 
 #define AVI_BOX_EXIT( i_err ) \
-    if( p_bo->i_buffer&0x01 ) bo_AddByte( p_bo, 0 ); \
-    bo_AddDWordLE( &_bo_sav_, p_bo->i_buffer - _bo_sav_.i_buffer - 4 ); \
+    if( p_bo->p_block->i_buffer&0x01 ) bo_AddByte( p_bo, 0 ); \
+    bo_SetDWordLE( p_bo, i_datasize_offset, p_bo->p_block->i_buffer - i_datasize_offset - 4 ); \
     return( i_err );
 
 static int avi_HeaderAdd_avih( sout_mux_t *p_mux,
@@ -615,8 +732,8 @@ static int avi_HeaderAdd_avih( sout_mux_t *p_mux,
         if( p_sys->stream[i_stream].i_duration > 0 )
         {
             i_maxbytespersec +=
-                p_sys->stream[p_sys->i_stream_video].i_totalsize /
-                p_sys->stream[p_sys->i_stream_video].i_duration;
+                p_sys->stream[i_stream].i_totalsize /
+                p_sys->stream[i_stream].i_duration;
         }
     }
 
@@ -647,9 +764,7 @@ static int avi_HeaderAdd_avih( sout_mux_t *p_mux,
 
     AVI_BOX_EXIT( 0 );
 }
-static int avi_HeaderAdd_strh( sout_mux_t   *p_mux,
-                               buffer_out_t *p_bo,
-                               avi_stream_t *p_stream )
+static int avi_HeaderAdd_strh( buffer_out_t *p_bo, avi_stream_t *p_stream )
 {
     AVI_BOX_ENTER( "strh" );
 
@@ -716,9 +831,7 @@ static int avi_HeaderAdd_strh( sout_mux_t   *p_mux,
     AVI_BOX_EXIT( 0 );
 }
 
-static int avi_HeaderAdd_strf( sout_mux_t *p_mux,
-                               buffer_out_t *p_bo,
-                               avi_stream_t *p_stream )
+static int avi_HeaderAdd_strf( buffer_out_t *p_bo, avi_stream_t *p_stream )
 {
     AVI_BOX_ENTER( "strf" );
 
@@ -754,7 +867,7 @@ static int avi_HeaderAdd_strf( sout_mux_t *p_mux,
             bo_AddDWordLE( p_bo, p_stream->p_bih->biClrUsed );
             bo_AddDWordLE( p_bo, p_stream->p_bih->biClrImportant );
             bo_AddMem( p_bo,
-                       p_stream->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
+                       p_stream->p_bih->biSize - sizeof( VLC_BITMAPINFOHEADER ),
                        (uint8_t*)&p_stream->p_bih[1] );
             break;
     }
@@ -762,76 +875,137 @@ static int avi_HeaderAdd_strf( sout_mux_t *p_mux,
     AVI_BOX_EXIT( 0 );
 }
 
-static int avi_HeaderAdd_strl( sout_mux_t *p_mux,
-                               buffer_out_t *p_bo,
-                               avi_stream_t *p_stream )
+static int avi_HeaderAdd_strl( buffer_out_t *p_bo, avi_stream_t *p_stream )
 {
     AVI_BOX_ENTER_LIST( "strl" );
 
-    avi_HeaderAdd_strh( p_mux, p_bo, p_stream );
-    avi_HeaderAdd_strf( p_mux, p_bo, p_stream );
+    avi_HeaderAdd_strh( p_bo, p_stream );
+    avi_HeaderAdd_strf( p_bo, p_stream );
 
     AVI_BOX_EXIT( 0 );
 }
 
-static sout_buffer_t *avi_HeaderCreateRIFF( sout_mux_t *p_mux )
+static int avi_HeaderAdd_meta( buffer_out_t *p_bo, const char psz_meta[4],
+                               const char *psz_data )
+{
+    if ( psz_data == NULL ) return 1;
+    const char *psz = psz_data;
+    AVI_BOX_ENTER( psz_meta );
+    while (*psz) bo_AddByte( p_bo, *psz++ );
+    bo_AddByte( p_bo, 0 );
+    AVI_BOX_EXIT( 0 );
+}
+
+static int avi_HeaderAdd_INFO( sout_mux_t *p_mux, buffer_out_t *p_bo )
+{
+    char *psz;
+
+#define APPLY_META(var, fourcc) \
+    psz = var_InheritString( p_mux, SOUT_CFG_PREFIX var );\
+    if ( psz )\
+    {\
+        avi_HeaderAdd_meta( p_bo, fourcc, psz );\
+        free( psz );\
+    }
+
+    AVI_BOX_ENTER_LIST( "INFO" );
+
+    APPLY_META( "artist",   "IART")
+    APPLY_META( "comment",  "ICMT")
+    APPLY_META( "copyright","ICOP")
+    APPLY_META( "date",     "ICRD")
+    APPLY_META( "genre",    "IGNR")
+    APPLY_META( "name",     "INAM")
+    APPLY_META( "keywords", "IKEY")
+    APPLY_META( "subject",  "ISBJ")
+    APPLY_META( "encoder",  "ISFT")
+    /* Some are missing, but are they really useful ?? */
+
+#undef APPLY_META
+
+    AVI_BOX_EXIT( 0 );
+}
+
+static block_t *avi_HeaderCreateRIFF( sout_mux_t *p_mux )
 {
     sout_mux_sys_t      *p_sys = p_mux->p_sys;
-    sout_buffer_t       *p_hdr;
     int                 i_stream;
-    int                 i_maxbytespersec;
     int                 i_junk;
     buffer_out_t        bo;
 
-    p_hdr = sout_BufferNew( p_mux->p_sout, HDR_SIZE );
-    memset( p_hdr->p_buffer, 0, HDR_SIZE );
+    struct
+    {
+        int i_riffsize;
+        int i_hdrllistsize;
+        int i_hdrldatastart;
+    } offsets;
 
-    bo_Init( &bo, HDR_SIZE, p_hdr->p_buffer );
+    bo_Init( &bo, HDR_BASE_SIZE );
 
     bo_AddFCC( &bo, "RIFF" );
-    bo_AddDWordLE( &bo, p_sys->i_movi_size + HDR_SIZE - 8 + p_sys->i_idx1_size );
+    offsets.i_riffsize = bo.p_block->i_buffer;
+    bo_AddDWordLE( &bo, 0xEFBEADDE );
     bo_AddFCC( &bo, "AVI " );
 
     bo_AddFCC( &bo, "LIST" );
-    bo_AddDWordLE( &bo, HDR_SIZE - 8);
+    /* HDRL List size should exclude following data in HDR buffer
+     *  -12 (RIFF, RIFF size, 'AVI ' tag),
+     *  - 8 (hdr1 LIST tag and its size)
+     *  - 12 (movi LIST tag, size, 'movi' listType )
+     */
+    offsets.i_hdrllistsize = bo.p_block->i_buffer;
+    bo_AddDWordLE( &bo, 0xEFBEADDE );
     bo_AddFCC( &bo, "hdrl" );
+    offsets.i_hdrldatastart = bo.p_block->i_buffer;
 
     avi_HeaderAdd_avih( p_mux, &bo );
-    for( i_stream = 0,i_maxbytespersec = 0; i_stream < p_sys->i_streams; i_stream++ )
+    for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
     {
-        avi_HeaderAdd_strl( p_mux, &bo, &p_sys->stream[i_stream] );
+        avi_HeaderAdd_strl( &bo, &p_sys->stream[i_stream] );
     }
 
-    i_junk = HDR_SIZE - bo.i_buffer - 8 - 12;
+    /* align on 16 bytes */
+    int i_align = ( ( bo.p_block->i_buffer + 12 + 0xE ) & ~ 0xF );
+    i_junk = i_align - bo.p_block->i_buffer;
     bo_AddFCC( &bo, "JUNK" );
     bo_AddDWordLE( &bo, i_junk );
+    for( int i=0; i< i_junk; i++ )
+    {
+        bo_AddByte( &bo, 0 );
+    }
+
+    /* Now set hdrl size */
+    bo_SetDWordLE( &bo, offsets.i_hdrllistsize,
+                   bo.p_block->i_buffer - offsets.i_hdrldatastart );
+
+    avi_HeaderAdd_INFO( p_mux, &bo );
 
-    bo.i_buffer += i_junk;
     bo_AddFCC( &bo, "LIST" );
     bo_AddDWordLE( &bo, p_sys->i_movi_size + 4 );
     bo_AddFCC( &bo, "movi" );
 
-    return( p_hdr );
+    /* Now set RIFF size */
+    bo_SetDWordLE( &bo, offsets.i_riffsize, bo.p_block->i_buffer - 8
+                   + p_sys->i_movi_size + p_sys->i_idx1_size );
+
+    return( bo.p_block );
 }
 
-static sout_buffer_t * avi_HeaderCreateidx1( sout_mux_t *p_mux )
+static block_t * avi_HeaderCreateidx1( sout_mux_t *p_mux )
 {
     sout_mux_sys_t      *p_sys = p_mux->p_sys;
-    sout_buffer_t       *p_idx1;
     uint32_t            i_idx1_size;
-    unsigned int        i;
     buffer_out_t        bo;
 
-    i_idx1_size = 16 * p_sys->idx1.i_entry_count;
+    i_idx1_size = 16 * p_sys->idx1.i_entry_count + 8;
 
-    p_idx1 = sout_BufferNew( p_mux->p_sout, i_idx1_size + 8 );
-    memset( p_idx1->p_buffer, 0, i_idx1_size );
+    bo_Init( &bo, i_idx1_size );
+    memset( bo.p_block->p_buffer, 0, i_idx1_size);
 
-    bo_Init( &bo, i_idx1_size, p_idx1->p_buffer );
     bo_AddFCC( &bo, "idx1" );
-    bo_AddDWordLE( &bo, i_idx1_size );
+    bo_AddDWordLE( &bo, i_idx1_size - 8);
 
-    for( i = 0; i < p_sys->idx1.i_entry_count; i++ )
+    for( unsigned i = 0; i < p_sys->idx1.i_entry_count; i++ )
     {
         bo_AddFCC( &bo, p_sys->idx1.entry[i].fcc );
         bo_AddDWordLE( &bo, p_sys->idx1.entry[i].i_flags );
@@ -839,6 +1013,5 @@ static sout_buffer_t * avi_HeaderCreateidx1( sout_mux_t *p_mux )
         bo_AddDWordLE( &bo, p_sys->idx1.entry[i].i_length );
     }
 
-    return( p_idx1 );
+    return( bo.p_block );
 }
-