]> git.sesse.net Git - vlc/blobdiff - modules/codec/ffmpeg/video_filter.c
* modules/codec/ffmpeg/audio.c: reduce memory usage a bit.
[vlc] / modules / codec / ffmpeg / video_filter.c
index a3178691f3b7d59fc9d04417504440f02235a8e0..c3d538874f60ab2b383270b741098bf09c98d52e 100644 (file)
@@ -2,7 +2,7 @@
  * video filter: video filter doing chroma conversion and resizing
  *               using the ffmpeg library
  *****************************************************************************
- * Copyright (C) 1999-2001 VideoLAN
+ * Copyright (C) 1999-2001 the VideoLAN team
  * $Id$
  *
  * Authors: Gildas Bazin <gbazin@videolan.org>
@@ -19,7 +19,7 @@
  *
  * 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.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
@@ -50,6 +50,8 @@ struct filter_sys_t
 {
     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;
@@ -60,10 +62,11 @@ struct filter_sys_t
     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;
@@ -79,6 +82,20 @@ int E_(OpenFilter)( vlc_object_t *p_this )
     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;
 
@@ -98,7 +115,7 @@ int E_(OpenFilter)( vlc_object_t *p_this )
 
     /* Misc init */
     p_sys->p_rsc = NULL;
-    p_sys->b_convert = b_convert;
+    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 =
@@ -107,31 +124,17 @@ int E_(OpenFilter)( vlc_object_t *p_this )
     es_format_Init( &p_sys->fmt_in, 0, 0 );
     es_format_Init( &p_sys->fmt_out, 0, 0 );
 
+    /* Dummy alloc, will be reallocated in CheckInit */
+    avpicture_alloc( &p_sys->tmp_pic, p_sys->i_src_ffmpeg_chroma,
+                     p_filter->fmt_out.video.i_width,
+                     p_filter->fmt_out.video.i_height );
+
     if( CheckInit( p_filter ) != VLC_SUCCESS )
     {
         free( p_sys );
         return VLC_EGENERIC;
     }
 
-    if( p_sys->b_resize && p_sys->b_convert )
-    {
-        if ( p_filter->fmt_in.video.i_width * p_filter->fmt_in.video.i_height >
-             p_filter->fmt_out.video.i_width * p_filter->fmt_out.video.i_height )
-        {
-            /* Resizing then conversion */
-            avpicture_alloc( &p_sys->tmp_pic, p_sys->i_src_ffmpeg_chroma,
-                             p_filter->fmt_out.video.i_width,
-                             p_filter->fmt_out.video.i_height );
-        }
-        else
-        {
-            /* Conversion then resizing */
-            avpicture_alloc( &p_sys->tmp_pic, p_sys->i_dst_ffmpeg_chroma,
-                             p_filter->fmt_in.video.i_width,
-                             p_filter->fmt_in.video.i_height );
-        }
-    }
-
     msg_Dbg( p_filter, "input: %ix%i %4.4s -> %ix%i %4.4s",
              p_filter->fmt_in.video.i_width, p_filter->fmt_in.video.i_height,
              (char *)&p_filter->fmt_in.video.i_chroma,
@@ -144,6 +147,23 @@ int E_(OpenFilter)( vlc_object_t *p_this )
     return VLC_SUCCESS;
 }
 
+/*****************************************************************************
+ * 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
  *****************************************************************************/
@@ -165,25 +185,132 @@ void E_(CloseFilter)( vlc_object_t *p_this )
 static int CheckInit( filter_t *p_filter )
 {
     filter_sys_t *p_sys = p_filter->p_sys;
-
-    if( p_filter->fmt_in.video.i_width != p_sys->fmt_in.video.i_width ||
+    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;
+
+
+    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;
 
+        p_sys->b_convert =
+          p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma;
+
         p_sys->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;
 
+        p_sys->b_resize_first =
+          p_filter->fmt_in.video.i_width * p_filter->fmt_in.video.i_height >
+          p_filter->fmt_out.video.i_width * p_filter->fmt_out.video.i_height;
+
+        if( p_sys->b_resize &&
+            p_sys->i_src_ffmpeg_chroma != PIX_FMT_YUV420P &&
+            p_sys->i_src_ffmpeg_chroma != PIX_FMT_YUVJ420P &&
+            p_sys->i_dst_ffmpeg_chroma != PIX_FMT_YUV420P &&
+            p_sys->i_dst_ffmpeg_chroma != PIX_FMT_YUVJ420P )
+        {
+            msg_Err( p_filter, "img_resample_init only deals with I420" );
+            return VLC_EGENERIC;
+        }
+        else if( p_sys->i_src_ffmpeg_chroma != PIX_FMT_YUV420P &&
+                 p_sys->i_src_ffmpeg_chroma != PIX_FMT_YUVJ420P )
+        {
+            p_sys->b_resize_first = VLC_FALSE;
+        }
+        else if( p_sys->i_dst_ffmpeg_chroma != PIX_FMT_YUV420P &&
+                 p_sys->i_dst_ffmpeg_chroma != PIX_FMT_YUVJ420P )
+        {
+            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 )
             {
@@ -192,6 +319,23 @@ static int CheckInit( filter_t *p_filter )
             }
         }
 
+        avpicture_free( &p_sys->tmp_pic );
+
+        if( p_sys->b_resize_first )
+        {
+            /* Resizing then conversion */
+            avpicture_alloc( &p_sys->tmp_pic, p_sys->i_src_ffmpeg_chroma,
+                             p_filter->fmt_out.video.i_width,
+                             p_filter->fmt_out.video.i_height );
+        }
+        else
+        {
+            /* Conversion then resizing */
+            avpicture_alloc( &p_sys->tmp_pic, p_sys->i_dst_ffmpeg_chroma,
+                             p_filter->fmt_in.video.i_width,
+                             p_filter->fmt_in.video.i_height );
+        }
+
         p_sys->fmt_in = p_filter->fmt_in;
         p_sys->fmt_out = p_filter->fmt_out;
     }
@@ -199,16 +343,74 @@ static int CheckInit( filter_t *p_filter )
     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
  *****************************************************************************/
 static picture_t *Process( filter_t *p_filter, picture_t *p_pic )
 {
     filter_sys_t *p_sys = p_filter->p_sys;
-    AVPicture src_pic, dest_pic, inter_pic;
+    AVPicture src_pic, dest_pic;
     AVPicture *p_src, *p_dst;
     picture_t *p_pic_dst;
-    vlc_bool_t b_resize = p_sys->b_resize;
     int i;
 
     /* Check if format properties changed */
@@ -256,15 +458,49 @@ static picture_t *Process( filter_t *p_filter, picture_t *p_pic )
 
     p_src = &src_pic;
 
-    if( b_resize && p_sys->p_rsc )
+    if( p_sys->b_resize && p_sys->p_rsc )
     {
         p_dst = &dest_pic;
-        if ( p_filter->fmt_in.video.i_width * p_filter->fmt_in.video.i_height >
-             p_filter->fmt_out.video.i_width * p_filter->fmt_out.video.i_height )
+        if( p_sys->b_resize_first )
         {
-            if ( p_sys->b_convert ) p_dst = &p_sys->tmp_pic;
+            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 );
-            b_resize = VLC_FALSE;
+#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;
         }
     }
@@ -273,7 +509,7 @@ static picture_t *Process( filter_t *p_filter, picture_t *p_pic )
     {
         video_format_t *p_fmt = &p_filter->fmt_out.video;
         p_dst = &dest_pic;
-        if( b_resize )
+        if( p_sys->b_resize && !p_sys->b_resize_first )
         {
             p_dst = &p_sys->tmp_pic;
             p_fmt = &p_filter->fmt_in.video;
@@ -286,10 +522,45 @@ static picture_t *Process( filter_t *p_filter, picture_t *p_pic )
         p_src = p_dst;
     }
 
-    if( b_resize && p_sys->p_rsc )
+    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 */
@@ -301,9 +572,9 @@ static picture_t *Process( filter_t *p_filter, picture_t *p_pic )
         int i_src_pitch = p_pic->p[0].i_pitch;
         uint8_t *p_dst = p_pic_dst->p[3].p_pixels;
         int i_dst_pitch = p_pic_dst->p[3].i_pitch;
-        int j;
+        uint32_t l,j;
 
-        for( i = 0; i < p_filter->fmt_out.video.i_height; i++ )
+        for( l = 0; l < p_filter->fmt_out.video.i_height; l++ )
         {
             for( j = 0; j < p_filter->fmt_out.video.i_width; j++ )
             {