## crop padd patch version 2 for VLC 0.8.2
##
## by udo_richter(a)gmx.de
## http://urichter.cjb.net/vdr/?h=vlc-croppadd      (German)
## http://urichter.cjb.net/vdr/?h=vlc-croppadd&l=en (English)
##
## apply as patch -p1 < croppadd-2-vlc-0.8.2.diff
##
##
## This patch to vlc-0.8.2 repairs and enhances cropping and padding of the
## transcode stream output of VLC. Brief summary of parameters:
##
## croptop, cropbottom, cropleft, cropright:
##   Cut off pixels from the video source. This is broken in vlc-0.8.2.
##
## paddtop, paddbottom, paddleft, paddright:
##   Add black borders around video output. This adds to the scaled size if
##   width and/or height is specified.
##
## canvas-width, canvas-height:
##  Automatically handle cropping and padding to ensure a target size. The
##  video will be centered on the canvas. Scaling won't happen unless
##  specified.
##
## canvas-aspect:
##  Enforce a video aspect like 4:3 or 16:9 on canvas by shrinking the video
##  in one direction until the aspect is reached. If no canvas is specified,
##  use this as pixel aspect. (use 1:1 for AVI and PC displays)
##
## Possible usage sample:
##   --sout "#transcode{vcodec=mpgv,vb=3000,acodec=mpga,ab=192,
##   width=720,height=576,canvas-width=720,canvas-height=576,
##   canvas-aspect=4:3,fps=25}:std{...}"
## Scales the video up or down to 720x576, then shrink to achieve 4:3 
## display and padd back up to 720x576 for streaming. This will convert 
## every video source to PAL D1 compatible format, and uses letterboxing
## for widescreen sources.
##
##
diff --binary -aur -x .deps -x '*.o' -x '*.dll' -x 'Make*' -x '*.a' vlc-0.8.2-src/modules/codec/ffmpeg/ffmpeg.c vlc-0.8.2-mod/modules/codec/ffmpeg/ffmpeg.c
--- vlc-0.8.2-src/modules/codec/ffmpeg/ffmpeg.c	2005-06-25 15:43:12.000000000 +0200
+++ vlc-0.8.2-mod/modules/codec/ffmpeg/ffmpeg.c	2005-07-03 16:16:04.718573300 +0200
@@ -176,6 +176,12 @@
     set_callbacks( E_(OpenFilter), E_(CloseFilter) );
     set_description( _("ffmpeg video filter") );
 
+    /* crop/padd submodule */
+    add_submodule();
+    set_capability( "crop padd", 10 );
+    set_callbacks( E_(OpenCropPadd), E_(CloseFilter) );
+    set_description( _("ffmpeg crop padd filter") );
+
     /* video filter submodule */
     add_submodule();
     set_capability( "video filter2", 0 );
Only in vlc-0.8.2-mod/modules/codec/ffmpeg: ffmpeg.c.orig
diff --binary -aur -x .deps -x '*.o' -x '*.dll' -x 'Make*' -x '*.a' vlc-0.8.2-src/modules/codec/ffmpeg/ffmpeg.h vlc-0.8.2-mod/modules/codec/ffmpeg/ffmpeg.h
--- vlc-0.8.2-src/modules/codec/ffmpeg/ffmpeg.h	2005-06-25 15:43:12.000000000 +0200
+++ vlc-0.8.2-mod/modules/codec/ffmpeg/ffmpeg.h	2005-07-03 16:16:04.765443500 +0200
@@ -70,6 +70,7 @@
 
 /* Video filter module */
 int  E_(OpenFilter)( vlc_object_t * );
+int  E_(OpenCropPadd)( vlc_object_t * );
 void E_(CloseFilter)( vlc_object_t * );
 int  E_(OpenDeinterlace)( vlc_object_t * );
 void E_(CloseDeinterlace)( vlc_object_t * );
diff --binary -aur -x .deps -x '*.o' -x '*.dll' -x 'Make*' -x '*.a' vlc-0.8.2-src/modules/codec/ffmpeg/video_filter.c vlc-0.8.2-mod/modules/codec/ffmpeg/video_filter.c
--- vlc-0.8.2-src/modules/codec/ffmpeg/video_filter.c	2005-06-25 15:43:12.000000000 +0200
+++ vlc-0.8.2-mod/modules/codec/ffmpeg/video_filter.c	2005-09-14 21:46:22.703750000 +0200
@@ -51,6 +51,7 @@
     vlc_bool_t b_resize;
     vlc_bool_t b_convert;
     vlc_bool_t b_resize_first;
+    vlc_bool_t b_enable_croppadd;
 
     es_format_t fmt_in;
     int i_src_ffmpeg_chroma;
@@ -61,10 +62,11 @@
     ImgReSampleContext *p_rsc;
 };
 
+
 /*****************************************************************************
- * OpenFilter: probe the filter and return score
+ * OpenFilterEx: common code to OpenFilter and OpenCropPadd
  *****************************************************************************/
-int E_(OpenFilter)( vlc_object_t *p_this )
+static int OpenFilterEx( vlc_object_t *p_this, vlc_bool_t b_enable_croppadd )
 {
     filter_t *p_filter = (filter_t*)p_this;
     filter_sys_t *p_sys;
@@ -80,6 +82,20 @@
     b_resize =
         p_filter->fmt_in.video.i_width != p_filter->fmt_out.video.i_width ||
         p_filter->fmt_in.video.i_height != p_filter->fmt_out.video.i_height;
+
+    if ( b_enable_croppadd ) 
+    {
+        b_resize = b_resize ||
+            p_filter->fmt_in.video.i_visible_width != p_filter->fmt_in.video.i_width ||
+            p_filter->fmt_in.video.i_visible_height != p_filter->fmt_in.video.i_height ||
+            p_filter->fmt_in.video.i_x_offset != 0 ||
+            p_filter->fmt_in.video.i_y_offset != 0 ||
+            p_filter->fmt_out.video.i_visible_width != p_filter->fmt_out.video.i_width ||
+            p_filter->fmt_out.video.i_visible_height != p_filter->fmt_out.video.i_height ||
+            p_filter->fmt_out.video.i_x_offset != 0 ||
+            p_filter->fmt_out.video.i_y_offset != 0;
+    }
+
     b_convert =
         p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma;
 
@@ -99,6 +115,7 @@
 
     /* Misc init */
     p_sys->p_rsc = NULL;
+    p_sys->b_enable_croppadd = b_enable_croppadd;
     p_sys->i_src_ffmpeg_chroma =
         E_(GetFfmpegChroma)( p_filter->fmt_in.video.i_chroma );
     p_sys->i_dst_ffmpeg_chroma =
@@ -131,6 +148,23 @@
 }
 
 /*****************************************************************************
+ * OpenFilter: probe the filter and return score
+ *****************************************************************************/
+int E_(OpenFilter)( vlc_object_t *p_this )
+{
+    return OpenFilterEx( p_this, VLC_FALSE );
+}
+
+/*****************************************************************************
+ * OpenCropPadd: probe the filter and return score
+ *****************************************************************************/
+int E_(OpenCropPadd)( vlc_object_t *p_this )
+{
+    return OpenFilterEx( p_this, VLC_TRUE );
+}
+
+
+/*****************************************************************************
  * CloseFilter: clean up the filter
  *****************************************************************************/
 void E_(CloseFilter)( vlc_object_t *p_this )
@@ -151,11 +185,36 @@
 static int CheckInit( filter_t *p_filter )
 {
     filter_sys_t *p_sys = p_filter->p_sys;
+    vlc_bool_t b_change;
+    int i_croptop=0;
+    int i_cropbottom=0;
+    int i_cropleft=0;
+    int i_cropright=0;
+    int i_paddtop=0;
+    int i_paddbottom=0;
+    int i_paddleft=0;
+    int i_paddright=0;
+
 
-    if( p_filter->fmt_in.video.i_width != p_sys->fmt_in.video.i_width ||
+    b_change= p_filter->fmt_in.video.i_width != p_sys->fmt_in.video.i_width ||
         p_filter->fmt_in.video.i_height != p_sys->fmt_in.video.i_height ||
         p_filter->fmt_out.video.i_width != p_sys->fmt_out.video.i_width ||
-        p_filter->fmt_out.video.i_height != p_sys->fmt_out.video.i_height )
+        p_filter->fmt_out.video.i_height != p_sys->fmt_out.video.i_height;
+
+    if ( p_sys->b_enable_croppadd )
+    {
+        b_change = b_change ||
+            p_filter->fmt_in.video.i_y_offset != p_sys->fmt_in.video.i_y_offset ||
+            p_filter->fmt_in.video.i_x_offset != p_sys->fmt_in.video.i_x_offset ||
+            p_filter->fmt_in.video.i_visible_width != p_sys->fmt_in.video.i_visible_width ||
+            p_filter->fmt_in.video.i_visible_height != p_sys->fmt_in.video.i_visible_height ||
+            p_filter->fmt_out.video.i_y_offset != p_sys->fmt_out.video.i_y_offset ||
+            p_filter->fmt_out.video.i_x_offset != p_sys->fmt_out.video.i_x_offset ||
+            p_filter->fmt_out.video.i_visible_width != p_sys->fmt_out.video.i_visible_width ||
+            p_filter->fmt_out.video.i_visible_height != p_sys->fmt_out.video.i_visible_height;
+    }
+
+    if ( b_change )
     {
         if( p_sys->p_rsc ) img_resample_close( p_sys->p_rsc );
         p_sys->p_rsc = 0;
@@ -191,12 +250,67 @@
             p_sys->b_resize_first = VLC_TRUE;
         }
 
+        if ( p_sys->b_enable_croppadd )
+        {
+            p_sys->b_resize = p_sys->b_resize ||
+                p_filter->fmt_in.video.i_visible_width != p_filter->fmt_in.video.i_width ||
+                p_filter->fmt_in.video.i_visible_height != p_filter->fmt_in.video.i_height ||
+                p_filter->fmt_in.video.i_x_offset != 0 ||
+                p_filter->fmt_in.video.i_y_offset != 0 ||
+                p_filter->fmt_out.video.i_visible_width != p_filter->fmt_out.video.i_width ||
+                p_filter->fmt_out.video.i_visible_height != p_filter->fmt_out.video.i_height ||
+                p_filter->fmt_out.video.i_x_offset != 0 ||
+                p_filter->fmt_out.video.i_y_offset != 0;
+        }
+
         if( p_sys->b_resize )
         {
-            p_sys->p_rsc = img_resample_init( p_filter->fmt_out.video.i_width,
+            if ( p_sys->b_enable_croppadd )
+            {
+                i_croptop=p_filter->fmt_in.video.i_y_offset;
+                i_cropbottom=p_filter->fmt_in.video.i_height
+                                       - p_filter->fmt_in.video.i_visible_height
+                                       - p_filter->fmt_in.video.i_y_offset;
+                i_cropleft=p_filter->fmt_in.video.i_x_offset;
+                i_cropright=p_filter->fmt_in.video.i_width
+                                      - p_filter->fmt_in.video.i_visible_width
+                                      - p_filter->fmt_in.video.i_x_offset;
+
+
+                i_paddtop=p_filter->fmt_out.video.i_y_offset;
+                i_paddbottom=p_filter->fmt_out.video.i_height
+                                       - p_filter->fmt_out.video.i_visible_height
+                                       - p_filter->fmt_out.video.i_y_offset;
+                i_paddleft=p_filter->fmt_out.video.i_x_offset;
+                i_paddright=p_filter->fmt_out.video.i_width
+                                      - p_filter->fmt_out.video.i_visible_width
+                                      - p_filter->fmt_out.video.i_x_offset;
+            }
+
+#if LIBAVCODEC_BUILD >= 4708
+            p_sys->p_rsc = img_resample_full_init( 
+                               p_filter->fmt_out.video.i_width,
                                p_filter->fmt_out.video.i_height,
                                p_filter->fmt_in.video.i_width,
-                               p_filter->fmt_in.video.i_height );
+                               p_filter->fmt_in.video.i_height,
+                               i_croptop,i_cropbottom,
+                               i_cropleft,i_cropright,
+                               i_paddtop,i_paddbottom,
+                               i_paddleft,i_paddright );
+#else
+            p_sys->p_rsc = img_resample_full_init( 
+                               p_filter->fmt_out.video.i_width - i_paddleft - i_paddright,
+                               p_filter->fmt_out.video.i_height - i_paddtop - i_paddbottom,
+                               p_filter->fmt_in.video.i_width,
+                               p_filter->fmt_in.video.i_height,
+                               i_croptop,i_cropbottom,
+                               i_cropleft,i_cropright );
+#endif
+            msg_Dbg( p_filter, "input: %ix%i -> %ix%i",
+                p_filter->fmt_out.video.i_width,
+                p_filter->fmt_out.video.i_height,
+                p_filter->fmt_in.video.i_width,
+                p_filter->fmt_in.video.i_height);
 
             if( !p_sys->p_rsc )
             {
@@ -229,6 +343,65 @@
     return VLC_SUCCESS;
 }
 
+/* fill padd code from ffmpeg */
+static int padcolor[3] = { 16, 128, 128 };
+
+/* Expects img to be yuv420 */
+static void fill_pad_region( AVPicture* img, int height, int width,
+        int padtop, int padbottom, int padleft, int padright, int *color ) 
+{
+    int i, y, shift;
+    uint8_t *optr;
+
+    for ( i = 0; i < 3; i++ ) 
+    {
+        shift = ( i == 0 ) ? 0 : 1;
+
+        if ( padtop || padleft ) 
+        {
+            memset( img->data[i], color[i], ( ( ( img->linesize[i] * padtop ) +
+                            padleft ) >> shift) );
+        }
+
+        if ( padleft || padright ) 
+        {
+            optr = img->data[i] + ( img->linesize[i] * ( padtop >> shift ) ) +
+                ( img->linesize[i] - ( padright >> shift ) );
+
+            for ( y = 0; y < ( ( height - ( padtop + padbottom ) ) >> shift ); y++ ) 
+            {
+                memset( optr, color[i], ( padleft + padright ) >> shift );
+                optr += img->linesize[i];
+            }
+        }
+
+        if (padbottom) 
+        {
+            optr = img->data[i] + ( img->linesize[i] * ( ( height - padbottom ) >> shift ) );
+            memset( optr, color[i], ( ( img->linesize[i] * padbottom ) >> shift ) );
+        }
+    }
+}
+
+#if LIBAVCODEC_BUILD < 4708
+
+/* Workaround, because old libavcodec doesnt know how to padd */
+
+static void img_resample_padd( ImgReSampleContext *s, AVPicture *output, 
+        const AVPicture *input, int padtop, int padleft )
+{
+    AVPicture nopadd_pic = *output;
+
+    /* shift out top and left padding for old ffmpeg */
+    nopadd_pic.data[0] += ( nopadd_pic.linesize[0] * padtop + padleft );
+    nopadd_pic.data[1] += ( nopadd_pic.linesize[1] * padtop + padleft ) >> 1;
+    nopadd_pic.data[2] += ( nopadd_pic.linesize[2] * padtop + padleft ) >> 1;
+    img_resample( s, &nopadd_pic, input );
+}
+#endif
+
+
+
 /*****************************************************************************
  * Do the processing here
  *****************************************************************************/
@@ -291,7 +464,43 @@
         if( p_sys->b_resize_first )
         {
             if( p_sys->b_convert ) p_dst = &p_sys->tmp_pic;
+
+#if LIBAVCODEC_BUILD >= 4708
             img_resample( p_sys->p_rsc, p_dst, p_src );
+#else        
+            if ( p_sys->b_enable_croppadd )
+            {
+                img_resample_padd( p_sys->p_rsc, p_dst, p_src, 
+                    p_filter->fmt_out.video.i_y_offset, 
+                    p_filter->fmt_out.video.i_x_offset );
+            } 
+            else 
+            {
+                img_resample( p_sys->p_rsc, p_dst, p_src );
+            }
+#endif
+
+            if (p_sys->b_enable_croppadd)
+            {
+                if (p_filter->fmt_out.video.i_visible_width != p_filter->fmt_out.video.i_width ||
+                    p_filter->fmt_out.video.i_visible_height != p_filter->fmt_out.video.i_height ||
+                    p_filter->fmt_out.video.i_x_offset != 0 ||
+                    p_filter->fmt_out.video.i_y_offset != 0)
+                {
+                    fill_pad_region(p_dst, p_filter->fmt_out.video.i_height,
+                                    p_filter->fmt_out.video.i_width,
+                                    p_filter->fmt_out.video.i_y_offset,
+                                    p_filter->fmt_out.video.i_height
+                                      - p_filter->fmt_out.video.i_visible_height
+                                      - p_filter->fmt_out.video.i_y_offset,
+                                    p_filter->fmt_out.video.i_x_offset,
+                                    p_filter->fmt_out.video.i_width
+                                      - p_filter->fmt_out.video.i_visible_width
+                                      - p_filter->fmt_out.video.i_x_offset,
+                                    padcolor);
+                }
+            }
+
             p_src = p_dst;
         }
     }
@@ -316,7 +525,42 @@
     if( p_sys->b_resize && !p_sys->b_resize_first && p_sys->p_rsc )
     {
         p_dst = &dest_pic;
+
+#if LIBAVCODEC_BUILD >= 4708
         img_resample( p_sys->p_rsc, p_dst, p_src );
+#else
+        if ( p_sys->b_enable_croppadd )
+        {
+            img_resample_padd( p_sys->p_rsc, p_dst, p_src, 
+                p_filter->fmt_out.video.i_y_offset, 
+                p_filter->fmt_out.video.i_x_offset );
+        } 
+        else 
+        {
+            img_resample( p_sys->p_rsc, p_dst, p_src );
+        }
+#endif
+ 
+        if (p_sys->b_enable_croppadd)
+        {
+            if (p_filter->fmt_out.video.i_visible_width != p_filter->fmt_out.video.i_width ||
+                p_filter->fmt_out.video.i_visible_height != p_filter->fmt_out.video.i_height ||
+                p_filter->fmt_out.video.i_x_offset != 0 ||
+                p_filter->fmt_out.video.i_y_offset != 0)
+            {
+                fill_pad_region(p_dst, p_filter->fmt_out.video.i_height,
+                                p_filter->fmt_out.video.i_width,
+                                p_filter->fmt_out.video.i_y_offset,
+                                p_filter->fmt_out.video.i_height
+                                  - p_filter->fmt_out.video.i_visible_height
+                                  - p_filter->fmt_out.video.i_y_offset,
+                                p_filter->fmt_out.video.i_x_offset,
+                                p_filter->fmt_out.video.i_width
+                                  - p_filter->fmt_out.video.i_visible_width
+                                  - p_filter->fmt_out.video.i_x_offset,
+                                padcolor);
+            }
+        }
     }
 
     /* Special case for RV32 -> YUVA */
diff --binary -aur -x .deps -x '*.o' -x '*.dll' -x 'Make*' -x '*.a' vlc-0.8.2-src/modules/stream_out/transcode.c vlc-0.8.2-mod/modules/stream_out/transcode.c
--- vlc-0.8.2-src/modules/stream_out/transcode.c	2005-06-25 15:43:06.000000000 +0200
+++ vlc-0.8.2-mod/modules/stream_out/transcode.c	2005-09-14 21:49:10.485375000 +0200
@@ -85,6 +85,29 @@
 #define CROPRIGHT_LONGTEXT N_( \
     "Allows you to specify the right coordinate for the video cropping." )
 
+#define PADDTOP_TEXT N_("Video padd top")
+#define PADDTOP_LONGTEXT N_( \
+    "Allows you to specify padding of black lines at the top." )
+#define PADDLEFT_TEXT N_("Video padd left")
+#define PADDLEFT_LONGTEXT N_( \
+    "Allows you to specify padding of black lines on the left." )
+#define PADDBOTTOM_TEXT N_("Video padd bottom")
+#define PADDBOTTOM_LONGTEXT N_( \
+    "Allows you to specify padding of black lines at the top." )
+#define PADDRIGHT_TEXT N_("Video padd right")
+#define PADDRIGHT_LONGTEXT N_( \
+    "Allows you to specify padding of black lines on the right." )
+
+#define CANVAS_WIDTH_TEXT N_("Video canvas width")
+#define CANVAS_WIDTH_LONGTEXT N_( \
+    "Allows to padd or crop the frame to a specified width" )
+#define CANVAS_HEIGHT_TEXT N_("Video canvas height")
+#define CANVAS_HEIGHT_LONGTEXT N_( \
+    "Allows to padd or crop the frame to a specified height" )
+#define CANVAS_ASPECT_TEXT N_("Video canvas aspect ratio")
+#define CANVAS_ASPECT_LONGTEXT N_( \
+    "Set aspect (like 4:3) of video canvas and letterbox accordingly" )
+
 #define AENC_TEXT N_("Audio encoder")
 #define AENC_LONGTEXT N_( \
     "Allows you to specify the audio encoder to use and its associated " \
@@ -178,6 +201,22 @@
     add_integer( SOUT_CFG_PREFIX "cropright", 0, NULL, CROPRIGHT_TEXT,
                  CROPRIGHT_LONGTEXT, VLC_TRUE );
 
+    add_integer( SOUT_CFG_PREFIX "paddtop", 0, NULL, PADDTOP_TEXT,
+                 PADDTOP_LONGTEXT, VLC_TRUE );
+    add_integer( SOUT_CFG_PREFIX "paddleft", 0, NULL, PADDLEFT_TEXT,
+                 PADDLEFT_LONGTEXT, VLC_TRUE );
+    add_integer( SOUT_CFG_PREFIX "paddbottom", 0, NULL, PADDBOTTOM_TEXT,
+                 PADDBOTTOM_LONGTEXT, VLC_TRUE );
+    add_integer( SOUT_CFG_PREFIX "paddright", 0, NULL, PADDRIGHT_TEXT,
+                 PADDRIGHT_LONGTEXT, VLC_TRUE );
+
+    add_integer( SOUT_CFG_PREFIX "canvas-width", 0, NULL, CANVAS_WIDTH_TEXT,
+                 CANVAS_WIDTH_LONGTEXT, VLC_TRUE );
+    add_integer( SOUT_CFG_PREFIX "canvas-height", 0, NULL, CANVAS_HEIGHT_TEXT,
+                 CANVAS_HEIGHT_LONGTEXT, VLC_TRUE );
+    add_string( SOUT_CFG_PREFIX "canvas-aspect", NULL, NULL, CANVAS_ASPECT_TEXT,
+                CANVAS_ASPECT_LONGTEXT, VLC_FALSE );
+
     set_section( N_("Audio"), NULL );
     add_string( SOUT_CFG_PREFIX "aenc", NULL, NULL, AENC_TEXT,
                 AENC_LONGTEXT, VLC_FALSE );
@@ -211,6 +250,8 @@
 
 static const char *ppsz_sout_options[] = {
     "venc", "vcodec", "vb", "croptop", "cropbottom", "cropleft", "cropright",
+    "paddtop", "paddbottom", "paddleft", "paddright", 
+    "canvas-width", "canvas-height", "canvas-aspect", 
     "scale", "fps", "width", "height", "deinterlace", "deinterlace-module",
     "threads", "hurry-up", "aenc", "acodec", "ab", "samplerate", "channels",
     "senc", "scodec", "soverlay", "sfilter",
@@ -307,6 +348,26 @@
     int             i_crop_right;
     int             i_crop_left;
 
+    int             i_padd_top;
+    int             i_padd_bottom;
+    int             i_padd_right;
+    int             i_padd_left;
+
+    int             i_canvas_width;
+    int             i_canvas_height;
+    int             i_canvas_aspect;
+    
+    /* Video, calculated */
+    int             i_src_x_offset;
+    int             i_src_y_offset;
+    int             i_crop_width;
+    int             i_crop_height;
+
+    int             i_dst_x_offset;
+    int             i_dst_y_offset;
+    int             i_nopadd_width;
+    int             i_nopadd_height;
+
     /* SPU */
     vlc_fourcc_t    i_scodec;   /* codec spu (0 if not transcode) */
     char            *psz_senc;
@@ -461,6 +522,44 @@
     var_Get( p_stream, SOUT_CFG_PREFIX "cropright", &val );
     p_sys->i_crop_right = val.i_int;
 
+    var_Get( p_stream, SOUT_CFG_PREFIX "paddtop", &val );
+    p_sys->i_padd_top = val.i_int;
+    var_Get( p_stream, SOUT_CFG_PREFIX "paddbottom", &val );
+    p_sys->i_padd_bottom = val.i_int;
+    var_Get( p_stream, SOUT_CFG_PREFIX "paddleft", &val );
+    p_sys->i_padd_left = val.i_int;
+    var_Get( p_stream, SOUT_CFG_PREFIX "paddright", &val );
+    p_sys->i_padd_right = val.i_int;
+    
+    var_Get( p_stream, SOUT_CFG_PREFIX "canvas-width", &val );
+    p_sys->i_canvas_width = val.i_int;
+    var_Get( p_stream, SOUT_CFG_PREFIX "canvas-height", &val );
+    p_sys->i_canvas_height = val.i_int;
+    
+    var_Get( p_stream, SOUT_CFG_PREFIX "canvas-aspect", &val );
+    if ( val.psz_string )
+    {
+        char *psz_parser = strchr( val.psz_string, ':' );
+
+        if( psz_parser )
+        {
+            *psz_parser++ = '\0';
+            p_sys->i_canvas_aspect = atoi( val.psz_string ) * VOUT_ASPECT_FACTOR
+                / atoi( psz_parser );
+        }
+        else
+        {
+            msg_Warn( p_stream, "bad aspect ratio %s", val.psz_string );
+            p_sys->i_canvas_aspect = 0;
+        }
+
+        free( val.psz_string );
+    }
+    else
+    {
+        p_sys->i_canvas_aspect = 0;
+    }
+
     var_Get( p_stream, SOUT_CFG_PREFIX "threads", &val );
     p_sys->i_threads = val.i_int;
 
@@ -717,8 +816,8 @@
 
         /* Complete destination format */
         id->p_encoder->fmt_out.i_codec = p_sys->i_vcodec;
-        id->p_encoder->fmt_out.video.i_width  = p_sys->i_width;
-        id->p_encoder->fmt_out.video.i_height = p_sys->i_height;
+        id->p_encoder->fmt_out.video.i_width  = p_sys->i_width & ~1;
+        id->p_encoder->fmt_out.video.i_height = p_sys->i_height & ~1;
         id->p_encoder->fmt_out.i_bitrate = p_sys->i_vbitrate;
 
         /* Build decoder -> filter -> encoder chain */
@@ -1373,41 +1472,189 @@
     /* Hack because of the copy packetizer which can fail to detect the
      * proper size (which forces us to wait until the 1st frame
      * is decoded) */
-    int i_width = id->p_decoder->fmt_out.video.i_width -
-        p_sys->i_crop_left - p_sys->i_crop_right;
-    int i_height = id->p_decoder->fmt_out.video.i_height -
-        p_sys->i_crop_top - p_sys->i_crop_bottom;
+     
+    /* Calculate scaling, padding, cropping etc. */
+    
+    /* width/height of source */
+    int i_src_width = id->p_decoder->fmt_out.video.i_width;
+    int i_src_height = id->p_decoder->fmt_out.video.i_height;
+
+    /* with/height scaling */
+    float f_scale_width = 1;
+    float f_scale_height = 1;
+
+    /* width/height of output stream */
+    int i_dst_width;
+    int i_dst_height;
+    
+    /* aspect ratio */
+    float f_aspect = (float)id->p_decoder->fmt_out.video.i_aspect / VOUT_ASPECT_FACTOR;
+    msg_Dbg( p_stream, "decoder aspect is %i:%i", id->p_decoder->fmt_out.video.i_aspect, VOUT_ASPECT_FACTOR );
+    
+    /* Change f_aspect from source frame to source pixel */
+    f_aspect = f_aspect * i_src_height / i_src_width;
+    msg_Dbg( p_stream, "source pixel aspect is %f:1", f_aspect );
+
+    
+    /* width/height after cropping */    
+    p_sys->i_src_x_offset = p_sys->i_crop_left & ~1;
+    p_sys->i_src_y_offset = p_sys->i_crop_top & ~1;
+    p_sys->i_crop_width = i_src_width - ( p_sys->i_crop_left & ~1 ) - ( p_sys->i_crop_right & ~1 );
+    p_sys->i_crop_height = i_src_height - ( p_sys->i_crop_top & ~1 ) - ( p_sys->i_crop_bottom & ~1 );
 
+    /* Calculate scaling factor for specified parameters */
     if( id->p_encoder->fmt_out.video.i_width <= 0 &&
         id->p_encoder->fmt_out.video.i_height <= 0 && p_sys->f_scale )
     {
-        /* Apply the scaling */
-        id->p_encoder->fmt_out.video.i_width = i_width * p_sys->f_scale;
-        id->p_encoder->fmt_out.video.i_height = i_height * p_sys->f_scale;
+        /* Apply the scaling. f_scale defaults to 1. */
+        f_scale_width = p_sys->f_scale;
+        f_scale_height = p_sys->f_scale;
     }
     else if( id->p_encoder->fmt_out.video.i_width > 0 &&
              id->p_encoder->fmt_out.video.i_height <= 0 )
     {
-        id->p_encoder->fmt_out.video.i_height =
-            id->p_encoder->fmt_out.video.i_width / (double)i_width * i_height;
+        /* Only width specified */
+        f_scale_width = (float)id->p_encoder->fmt_out.video.i_width / p_sys->i_crop_width;
+        f_scale_height = f_scale_width;
     }
     else if( id->p_encoder->fmt_out.video.i_width <= 0 &&
              id->p_encoder->fmt_out.video.i_height > 0 )
     {
-        id->p_encoder->fmt_out.video.i_width =
-            id->p_encoder->fmt_out.video.i_height / (double)i_height * i_width;
+        /* Only height specified */
+        f_scale_height = (float)id->p_encoder->fmt_out.video.i_height / p_sys->i_crop_height;
+        f_scale_width = f_scale_height;
+    }
+    else if( id->p_encoder->fmt_out.video.i_width > 0 &&
+             id->p_encoder->fmt_out.video.i_height > 0 )
+    {
+        /* Width and height specified */
+        f_scale_width = (float)id->p_encoder->fmt_out.video.i_width / p_sys->i_crop_width;
+        f_scale_height = (float)id->p_encoder->fmt_out.video.i_height / p_sys->i_crop_height;
     }
 
-    /* Make sure the size is at least a multiple of 2 */
-    id->p_encoder->fmt_out.video.i_width =
-        (id->p_encoder->fmt_out.video.i_width + 1) >> 1 << 1;
-    id->p_encoder->fmt_out.video.i_height =
-        (id->p_encoder->fmt_out.video.i_height + 1) >> 1 << 1;
 
-    id->p_encoder->fmt_in.video.i_width =
-        id->p_encoder->fmt_out.video.i_width;
-    id->p_encoder->fmt_in.video.i_height =
-        id->p_encoder->fmt_out.video.i_height;
+    /* Change aspect ratio from source pixel to scaled pixel */
+    f_aspect = f_aspect * f_scale_height / f_scale_width;
+    msg_Dbg( p_stream, "scaled pixel aspect is %f:1", f_aspect );
+
+    /* Correct scaling for target aspect ratio */
+    /* Shrink video if necessary */
+    if ( p_sys->i_canvas_aspect > 0 )
+    {
+        float f_target_aspect = (float)p_sys->i_canvas_aspect / VOUT_ASPECT_FACTOR;
+        
+        if( p_sys->i_canvas_width > 0 && p_sys->i_canvas_height > 0)
+        {
+            /* Calculate pixel aspect of canvas */
+            f_target_aspect = f_target_aspect / p_sys->i_canvas_width * p_sys->i_canvas_height;
+        }
+        
+        if( f_target_aspect > f_aspect ) 
+        {
+            /* Reduce width scale to increase aspect */
+            f_scale_width = f_scale_width * f_aspect / f_target_aspect;
+        }
+        else
+        {
+            /* Reduce height scale to decrease aspect */
+            f_scale_height = f_scale_height * f_target_aspect / f_aspect;
+        }
+        f_aspect = f_target_aspect;
+        msg_Dbg( p_stream, "Canvas scaled pixel aspect is %f:1", f_aspect );
+    }
+    
+    /* f_scale_width and f_scale_height are now final */
+    
+    /* Calculate width, height from scaling */
+    /* Make sure its multiple of 2 */
+    
+    i_dst_width = 2 * (int)( p_sys->i_crop_width * f_scale_width / 2 + 0.5 );
+    i_dst_height = 2 * (int)( p_sys->i_crop_height * f_scale_height / 2 + 0.5 );
+    
+    p_sys->i_nopadd_width = i_dst_width;
+    p_sys->i_nopadd_height = i_dst_height;
+    p_sys->i_dst_x_offset = 0;
+    p_sys->i_dst_y_offset = 0;
+    
+    /* Handle canvas and padding */
+    
+    if( p_sys->i_canvas_width <= 0 )
+    {
+        /* No canvas width set, add explicit padding border */
+        i_dst_width = p_sys->i_nopadd_width + ( p_sys->i_padd_left & ~1 ) + ( p_sys->i_padd_right & ~1 );
+        p_sys->i_dst_x_offset = ( p_sys->i_padd_left & ~1 );
+    }       
+    else    
+    {
+        /* Canvas set, check if we have to padd or crop */
+        
+        if( p_sys->i_canvas_width < p_sys->i_nopadd_width )
+        {
+            /* need to crop more, but keep same scaling */
+            int i_crop = 2 * (int)( ( p_sys->i_canvas_width & ~1 ) / f_scale_width / 2 + 0.5 );
+            
+            p_sys->i_src_x_offset += ( ( p_sys->i_crop_width - i_crop ) / 2 ) & ~1;
+            p_sys->i_crop_width = i_crop;
+            i_dst_width = p_sys->i_canvas_width & ~1;
+            p_sys->i_nopadd_width = i_dst_width;
+        }
+        else if( p_sys->i_canvas_width > p_sys->i_nopadd_width )
+        {
+            /* need to padd */
+            i_dst_width = p_sys->i_canvas_width & ~1;
+            p_sys->i_dst_x_offset = ( i_dst_width - p_sys->i_nopadd_width ) / 2;
+            p_sys->i_dst_x_offset = p_sys->i_dst_x_offset & ~1;
+        }
+    }
+
+    if( p_sys->i_canvas_height <= 0 )
+    {
+        /* No canvas set, add padding border */
+        i_dst_height = p_sys->i_nopadd_height + ( p_sys->i_padd_top & ~1 ) + ( p_sys->i_padd_bottom & ~1 );
+        p_sys->i_dst_y_offset = ( p_sys->i_padd_top & ~1 );
+    }       
+    else    
+    {
+        /* Canvas set, check if we have to padd or crop */
+        
+        if( p_sys->i_canvas_height < p_sys->i_nopadd_height )
+        {
+            /* need to crop more, but keep same scaling */
+            int i_crop = 2 * (int)( ( p_sys->i_canvas_height & ~1 ) / f_scale_height / 2 + 0.5 );
+            
+            p_sys->i_src_y_offset += ( ( p_sys->i_crop_height - i_crop ) / 2 ) & ~1;
+            p_sys->i_crop_height = i_crop;
+            i_dst_height = p_sys->i_canvas_height & ~1;
+            p_sys->i_nopadd_height = i_dst_height;
+        }
+        else if( p_sys->i_canvas_height > p_sys->i_nopadd_height )
+        {
+            /* need to padd */
+            i_dst_height = p_sys->i_canvas_height & ~1;
+            p_sys->i_dst_y_offset = ( i_dst_height - p_sys->i_nopadd_height ) / 2;
+            p_sys->i_dst_y_offset = p_sys->i_dst_y_offset & ~1;
+        }
+    }
+
+    /* Change aspect ratio from scaled pixel to output frame */
+    
+    f_aspect = f_aspect * i_dst_width / i_dst_height;
+    
+    /* Store calculated values */
+    id->p_encoder->fmt_out.video.i_width = i_dst_width;
+    id->p_encoder->fmt_out.video.i_height = i_dst_height;
+
+    id->p_encoder->fmt_in.video.i_width = i_dst_width;
+    id->p_encoder->fmt_in.video.i_height = i_dst_height;
+    
+    msg_Dbg( p_stream, "Source %ix%i, crop %ix%i, destination %ix%i, padd %ix%i", 
+        i_src_width, i_src_height,
+        p_sys->i_crop_width, p_sys->i_crop_height,
+        p_sys->i_nopadd_width, p_sys->i_nopadd_height,
+        i_dst_width, i_dst_height
+    );
+    
+    /* Handle frame rate conversion */
 
     if( !id->p_encoder->fmt_out.video.i_frame_rate ||
         !id->p_encoder->fmt_out.video.i_frame_rate_base )
@@ -1440,12 +1687,13 @@
     /* Check whether a particular aspect ratio was requested */
     if( !id->p_encoder->fmt_out.video.i_aspect )
     {
-        id->p_encoder->fmt_out.video.i_aspect =
-            id->p_decoder->fmt_out.video.i_aspect;
+        id->p_encoder->fmt_out.video.i_aspect = (int)( f_aspect * VOUT_ASPECT_FACTOR + 0.5 );
     }
     id->p_encoder->fmt_in.video.i_aspect =
         id->p_encoder->fmt_out.video.i_aspect;
 
+    msg_Dbg( p_stream, "encoder aspect is %i:%i", id->p_encoder->fmt_out.video.i_aspect, VOUT_ASPECT_FACTOR );
+
     id->p_encoder->p_module =
         module_Need( id->p_encoder, "encoder", p_sys->psz_venc, VLC_TRUE );
     if( !id->p_encoder->p_module )
@@ -1642,12 +1890,14 @@
             /* Check if we need a filter for chroma conversion or resizing */
             if( id->p_decoder->fmt_out.video.i_chroma !=
                 id->p_encoder->fmt_in.video.i_chroma ||
-                id->p_decoder->fmt_out.video.i_width !=
-                id->p_encoder->fmt_out.video.i_width ||
-                id->p_decoder->fmt_out.video.i_height !=
-                id->p_encoder->fmt_out.video.i_height ||
-                p_sys->i_crop_top > 0 || p_sys->i_crop_bottom > 0 ||
-                p_sys->i_crop_left > 0 || p_sys->i_crop_right > 0 )
+                
+                (int)id->p_decoder->fmt_out.video.i_width != p_sys->i_crop_width ||
+                p_sys->i_crop_width != p_sys->i_nopadd_width ||
+                p_sys->i_nopadd_width != (int)id->p_encoder->fmt_out.video.i_width ||
+                
+                (int)id->p_decoder->fmt_out.video.i_height != p_sys->i_crop_height ||
+                p_sys->i_crop_height != p_sys->i_nopadd_height ||
+                p_sys->i_nopadd_height != (int)id->p_encoder->fmt_out.video.i_height)
             {
                 id->pp_filter[id->i_filter] =
                     vlc_object_create( p_stream, VLC_OBJECT_FILTER );
@@ -1661,9 +1911,21 @@
                 id->pp_filter[id->i_filter]->fmt_in = id->p_decoder->fmt_out;
                 id->pp_filter[id->i_filter]->fmt_out = id->p_encoder->fmt_in;
                 id->pp_filter[id->i_filter]->p_cfg = NULL;
+
+
+                id->pp_filter[id->i_filter]->fmt_in.video.i_x_offset = p_sys->i_src_x_offset;
+                id->pp_filter[id->i_filter]->fmt_in.video.i_y_offset = p_sys->i_src_y_offset;
+                id->pp_filter[id->i_filter]->fmt_in.video.i_visible_width = p_sys->i_crop_width;
+                id->pp_filter[id->i_filter]->fmt_in.video.i_visible_height = p_sys->i_crop_height;
+
+                id->pp_filter[id->i_filter]->fmt_out.video.i_x_offset = p_sys->i_dst_x_offset;
+                id->pp_filter[id->i_filter]->fmt_out.video.i_y_offset = p_sys->i_dst_y_offset;
+                id->pp_filter[id->i_filter]->fmt_out.video.i_visible_width = p_sys->i_nopadd_width;
+                id->pp_filter[id->i_filter]->fmt_out.video.i_visible_height = p_sys->i_nopadd_height;
+
                 id->pp_filter[id->i_filter]->p_module =
                     module_Need( id->pp_filter[id->i_filter],
-                                 "video filter2", 0, 0 );
+                                 "crop padd", 0, 0 );
                 if( id->pp_filter[id->i_filter]->p_module )
                 {
                     id->pp_filter[id->i_filter]->p_owner =
