]> git.sesse.net Git - vlc/blobdiff - src/misc/picture.c
build: create a macro to check for, and replace, possibly-inline functions.
[vlc] / src / misc / picture.c
index d13a0d1b4050a14e2dac57d541fd87e3902a7425..6aa5b056a9105d491f66a1d026d3df2b6b627043 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * picture.c : picture management functions
  *****************************************************************************
- * Copyright (C) 2000-2010 the VideoLAN team
+ * Copyright (C) 2000-2010 VLC authors and VideoLAN
  * Copyright (C) 2009-2010 Laurent Aimar
  * $Id$
  *
@@ -9,19 +9,19 @@
  *          Samuel Hocevar <sam@zoy.org>
  *          Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
  *
- * 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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.
  *****************************************************************************/
 
 /*****************************************************************************
  * used exactly like a video buffer. The video output thread then manages
  * how it gets displayed.
  */
-static int vout_AllocatePicture( picture_t *p_pic,
-                                 vlc_fourcc_t i_chroma,
-                                 int i_width, int i_height,
-                                 int i_sar_num, int i_sar_den )
+static int AllocatePicture( picture_t *p_pic )
 {
-    /* Make sure the real dimensions are a multiple of 16 */
-    if( picture_Setup( p_pic, i_chroma, i_width, i_height,
-                       i_sar_num, i_sar_den ) != VLC_SUCCESS )
-        return VLC_EGENERIC;
-
     /* Calculate how big the new image should be */
     size_t i_bytes = 0;
     for( int i = 0; i < p_pic->i_planes; i++ )
     {
         const plane_t *p = &p_pic->p[i];
 
-        if( p->i_pitch <= 0 || p->i_lines <= 0 ||
-            p->i_pitch > (SIZE_MAX - i_bytes)/p->i_lines )
+        if( p->i_pitch < 0 || p->i_lines <= 0 ||
+            (size_t)p->i_pitch > (SIZE_MAX - i_bytes)/p->i_lines )
         {
             p_pic->i_planes = 0;
             return VLC_ENOMEM;
@@ -70,8 +62,8 @@ static int vout_AllocatePicture( picture_t *p_pic,
         i_bytes += p->i_pitch * p->i_lines;
     }
 
-    uint8_t *p_data = vlc_memalign( &p_pic->p_data_orig, 16, i_bytes );
-    if( !p_data )
+    uint8_t *p_data = vlc_memalign( 16, i_bytes );
+    if( i_bytes > 0 && p_data == NULL )
     {
         p_pic->i_planes = 0;
         return VLC_EGENERIC;
@@ -91,11 +83,36 @@ static int vout_AllocatePicture( picture_t *p_pic,
 /*****************************************************************************
  *
  *****************************************************************************/
-static void PictureReleaseCallback( picture_t *p_picture )
+
+static void PictureDestroyContext( picture_t *p_picture )
 {
-    if( --p_picture->i_refcount > 0 )
-        return;
-    picture_Delete( p_picture );
+    void (**context)( void * ) = p_picture->context;
+    if( context != NULL )
+    {
+        void (*context_destroy)( void * ) = *context;
+        context_destroy( context );
+        p_picture->context = NULL;
+    }
+}
+
+/**
+ * Destroys a picture allocated by picture_NewFromResource() but without
+ * a custom destruction callback.
+ */
+static void picture_DestroyFromResource( picture_t *p_picture )
+{
+    free( p_picture->p_sys );
+    free( p_picture );
+}
+
+/**
+ * Destroys a picture allocated with picture_NewFromFormat()
+ * (and thus AllocatePicture()).
+ */
+static void picture_Destroy( picture_t *p_picture )
+{
+    vlc_free( p_picture->p[0].p_pixels );
+    free( p_picture );
 }
 
 /*****************************************************************************
@@ -107,9 +124,9 @@ void picture_Reset( picture_t *p_picture )
     p_picture->date = VLC_TS_INVALID;
     p_picture->b_force = false;
     p_picture->b_progressive = false;
-    p_picture->i_nb_fields = 0;
+    p_picture->i_nb_fields = 2;
     p_picture->b_top_field_first = false;
-    picture_CleanupQuant( p_picture );
+    PictureDestroyContext( p_picture );
 }
 
 /*****************************************************************************
@@ -120,8 +137,7 @@ static int LCM( int a, int b )
     return a * b / GCD( a, b );
 }
 
-int picture_Setup( picture_t *p_picture, vlc_fourcc_t i_chroma,
-                   int i_width, int i_height, int i_sar_num, int i_sar_den )
+int picture_Setup( picture_t *p_picture, const video_format_t *restrict fmt )
 {
     /* Store default values */
     p_picture->i_planes = 0;
@@ -132,16 +148,14 @@ int picture_Setup( picture_t *p_picture, vlc_fourcc_t i_chroma,
         p->i_pixel_pitch = 0;
     }
 
-    p_picture->pf_release = NULL;
-    p_picture->p_release_sys = NULL;
-    p_picture->i_refcount = 0;
+    atomic_init( &p_picture->gc.refcount, 0 );
+    p_picture->gc.p_sys = NULL;
 
-    p_picture->i_qtype = QTYPE_NONE;
-    p_picture->i_qstride = 0;
-    p_picture->p_q = NULL;
+    p_picture->i_nb_fields = 2;
 
-    video_format_Setup( &p_picture->format, i_chroma, i_width, i_height,
-                        i_sar_num, i_sar_den );
+    video_format_Setup( &p_picture->format, fmt->i_chroma, fmt->i_width, fmt->i_height,
+                        fmt->i_visible_width, fmt->i_visible_height,
+                        fmt->i_sar_num, fmt->i_sar_den );
 
     const vlc_chroma_description_t *p_dsc =
         vlc_fourcc_GetChromaDescription( p_picture->format.i_chroma );
@@ -156,7 +170,7 @@ int picture_Setup( picture_t *p_picture, vlc_fourcc_t i_chroma,
     */
     int i_modulo_w = 1;
     int i_modulo_h = 1;
-    int i_ratio_h  = 1;
+    unsigned int i_ratio_h  = 1;
     for( unsigned i = 0; i < p_dsc->plane_count; i++ )
     {
         i_modulo_w = LCM( i_modulo_w, 16 * p_dsc->p[i].w.den );
@@ -164,18 +178,19 @@ int picture_Setup( picture_t *p_picture, vlc_fourcc_t i_chroma,
         if( i_ratio_h < p_dsc->p[i].h.den )
             i_ratio_h = p_dsc->p[i].h.den;
     }
+    i_modulo_h = LCM( i_modulo_h, 32 );
 
-    const int i_width_aligned  = ( i_width  + i_modulo_w - 1 ) / i_modulo_w * i_modulo_w;
-    const int i_height_aligned = ( i_height + i_modulo_h - 1 ) / i_modulo_h * i_modulo_h;
+    const int i_width_aligned  = ( fmt->i_width  + i_modulo_w - 1 ) / i_modulo_w * i_modulo_w;
+    const int i_height_aligned = ( fmt->i_height + i_modulo_h - 1 ) / i_modulo_h * i_modulo_h;
     const int i_height_extra   = 2 * i_ratio_h; /* This one is a hack for some ASM functions */
     for( unsigned i = 0; i < p_dsc->plane_count; i++ )
     {
         plane_t *p = &p_picture->p[i];
 
         p->i_lines         = (i_height_aligned + i_height_extra ) * p_dsc->p[i].h.num / p_dsc->p[i].h.den;
-        p->i_visible_lines = i_height * p_dsc->p[i].h.num / p_dsc->p[i].h.den;
+        p->i_visible_lines = fmt->i_visible_height * p_dsc->p[i].h.num / p_dsc->p[i].h.den;
         p->i_pitch         = i_width_aligned * p_dsc->p[i].w.num / p_dsc->p[i].w.den * p_dsc->pixel_size;
-        p->i_visible_pitch = i_width * p_dsc->p[i].w.num / p_dsc->p[i].w.den * p_dsc->pixel_size;
+        p->i_visible_pitch = fmt->i_visible_width * p_dsc->p[i].w.num / p_dsc->p[i].w.den * p_dsc->pixel_size;
         p->i_pixel_pitch   = p_dsc->pixel_size;
 
         assert( (p->i_pitch % 16) == 0 );
@@ -195,22 +210,32 @@ picture_t *picture_NewFromResource( const video_format_t *p_fmt, const picture_r
     /* It is needed to be sure all information are filled */
     video_format_Setup( &fmt, p_fmt->i_chroma,
                               p_fmt->i_width, p_fmt->i_height,
+                              p_fmt->i_visible_width, p_fmt->i_visible_height,
                               p_fmt->i_sar_num, p_fmt->i_sar_den );
+    if( p_fmt->i_x_offset < p_fmt->i_width &&
+        p_fmt->i_y_offset < p_fmt->i_height &&
+        p_fmt->i_visible_width  > 0 && p_fmt->i_x_offset + p_fmt->i_visible_width  <= p_fmt->i_width &&
+        p_fmt->i_visible_height > 0 && p_fmt->i_y_offset + p_fmt->i_visible_height <= p_fmt->i_height )
+        video_format_CopyCrop( &fmt, p_fmt );
 
     /* */
     picture_t *p_picture = calloc( 1, sizeof(*p_picture) );
     if( !p_picture )
         return NULL;
 
+    /* Make sure the real dimensions are a multiple of 16 */
+    if( picture_Setup( p_picture, &fmt ) )
+    {
+        free( p_picture );
+        return NULL;
+    }
+
     if( p_resource )
     {
-        if( picture_Setup( p_picture, fmt.i_chroma, fmt.i_width, fmt.i_height,
-                           fmt.i_sar_num, fmt.i_sar_den ) )
-        {
-            free( p_picture );
-            return NULL;
-        }
         p_picture->p_sys = p_resource->p_sys;
+        p_picture->gc.pf_destroy = p_resource->pf_destroy;
+        if( p_picture->gc.pf_destroy == NULL )
+            p_picture->gc.pf_destroy = picture_DestroyFromResource;
 
         for( int i = 0; i < p_picture->i_planes; i++ )
         {
@@ -221,32 +246,34 @@ picture_t *picture_NewFromResource( const video_format_t *p_fmt, const picture_r
     }
     else
     {
-        if( vout_AllocatePicture( p_picture,
-                                  fmt.i_chroma, fmt.i_width, fmt.i_height,
-                                  fmt.i_sar_num, fmt.i_sar_den ) )
+        if( AllocatePicture( p_picture ) )
         {
             free( p_picture );
             return NULL;
         }
+        p_picture->gc.pf_destroy = picture_Destroy;
     }
+
     /* */
     p_picture->format = fmt;
-    p_picture->i_refcount = 1;
-    p_picture->pf_release = PictureReleaseCallback;
+
+    atomic_init( &p_picture->gc.refcount, 1 );
 
     return p_picture;
 }
+
 picture_t *picture_NewFromFormat( const video_format_t *p_fmt )
 {
     return picture_NewFromResource( p_fmt, NULL );
 }
+
 picture_t *picture_New( vlc_fourcc_t i_chroma, int i_width, int i_height, int i_sar_num, int i_sar_den )
 {
     video_format_t fmt;
 
     memset( &fmt, 0, sizeof(fmt) );
     video_format_Setup( &fmt, i_chroma, i_width, i_height,
-                        i_sar_num, i_sar_den );
+                        i_width, i_height, i_sar_num, i_sar_den );
 
     return picture_NewFromFormat( &fmt );
 }
@@ -254,28 +281,36 @@ picture_t *picture_New( vlc_fourcc_t i_chroma, int i_width, int i_height, int i_
 /*****************************************************************************
  *
  *****************************************************************************/
-void picture_Delete( picture_t *p_picture )
-{
-    assert( p_picture && p_picture->i_refcount == 0 );
-    assert( p_picture->p_release_sys == NULL );
 
-    free( p_picture->p_q );
-    free( p_picture->p_data_orig );
-    free( p_picture->p_sys );
-    free( p_picture );
+picture_t *picture_Hold( picture_t *p_picture )
+{
+    uintptr_t refs = atomic_fetch_add( &p_picture->gc.refcount, 1 );
+    assert( refs > 0 );
+    return p_picture;
 }
 
-/*****************************************************************************
- *
- *****************************************************************************/
-void picture_CopyPixels( picture_t *p_dst, const picture_t *p_src )
+void picture_Release( picture_t *p_picture )
 {
-    int i;
+    assert( p_picture != NULL );
 
-    for( i = 0; i < p_src->i_planes ; i++ )
-        plane_CopyPixels( p_dst->p+i, p_src->p+i );
+    uintptr_t refs = atomic_fetch_sub( &p_picture->gc.refcount, 1 );
+    assert( refs != 0 );
+    if( refs > 1 )
+        return;
+
+    PictureDestroyContext( p_picture );
+    assert( p_picture->gc.pf_destroy != NULL );
+    p_picture->gc.pf_destroy( p_picture );
 }
 
+bool picture_IsReferenced( picture_t *p_picture )
+{
+    return atomic_load( &p_picture->gc.refcount ) > 1;
+}
+
+/*****************************************************************************
+ *
+ *****************************************************************************/
 void plane_CopyPixels( plane_t *p_dst, const plane_t *p_src )
 {
     const unsigned i_width  = __MIN( p_dst->i_visible_pitch,
@@ -283,10 +318,15 @@ void plane_CopyPixels( plane_t *p_dst, const plane_t *p_src )
     const unsigned i_height = __MIN( p_dst->i_visible_lines,
                                      p_src->i_visible_lines );
 
-    if( p_src->i_pitch == p_dst->i_pitch )
+    /* The 2x visible pitch check does two things:
+       1) Makes field plane_t's work correctly (see the deinterlacer module)
+       2) Moves less data if the pitch and visible pitch differ much.
+    */
+    if( p_src->i_pitch == p_dst->i_pitch  &&
+        p_src->i_pitch < 2*p_src->i_visible_pitch )
     {
         /* There are margins, but with the same width : perfect ! */
-        vlc_memcpy( p_dst->p_pixels, p_src->p_pixels,
+        memcpy( p_dst->p_pixels, p_src->p_pixels,
                     p_src->i_pitch * i_height );
     }
     else
@@ -301,13 +341,38 @@ void plane_CopyPixels( plane_t *p_dst, const plane_t *p_src )
 
         for( i_line = i_height; i_line--; )
         {
-            vlc_memcpy( p_out, p_in, i_width );
+            memcpy( p_out, p_in, i_width );
             p_in += p_src->i_pitch;
             p_out += p_dst->i_pitch;
         }
     }
 }
 
+void picture_CopyProperties( picture_t *p_dst, const picture_t *p_src )
+{
+    p_dst->date = p_src->date;
+    p_dst->b_force = p_src->b_force;
+
+    p_dst->b_progressive = p_src->b_progressive;
+    p_dst->i_nb_fields = p_src->i_nb_fields;
+    p_dst->b_top_field_first = p_src->b_top_field_first;
+}
+
+void picture_CopyPixels( picture_t *p_dst, const picture_t *p_src )
+{
+    int i;
+
+    for( i = 0; i < p_src->i_planes ; i++ )
+        plane_CopyPixels( p_dst->p+i, p_src->p+i );
+}
+
+void picture_Copy( picture_t *p_dst, const picture_t *p_src )
+{
+    picture_CopyPixels( p_dst, p_src );
+    picture_CopyProperties( p_dst, p_src );
+}
+
+
 /*****************************************************************************
  *
  *****************************************************************************/
@@ -349,9 +414,9 @@ int picture_Export( vlc_object_t *p_obj,
 
     /* */
     fmt_out.i_width  = ( i_override_width < 0 ) ?
-                       i_original_width : i_override_width;
+                       i_original_width : (unsigned)i_override_width;
     fmt_out.i_height = ( i_override_height < 0 ) ?
-                       i_original_height : i_override_height;
+                       i_original_height : (unsigned)i_override_height;
 
     /* scale if only one direction is provided */
     if( fmt_out.i_height == 0 && fmt_out.i_width > 0 )
@@ -384,3 +449,24 @@ int picture_Export( vlc_object_t *p_obj,
     return VLC_SUCCESS;
 }
 
+unsigned picture_BlendSubpicture(picture_t *dst,
+                                 filter_t *blend, subpicture_t *src)
+{
+    unsigned done = 0;
+
+    assert(src && !src->b_fade && src->b_absolute);
+
+    for (subpicture_region_t *r = src->p_region; r != NULL; r = r->p_next) {
+        assert(r->p_picture && r->i_align == 0);
+        if (filter_ConfigureBlend(blend, dst->format.i_width,
+                                  dst->format.i_height,  &r->fmt)
+         || filter_Blend(blend, dst, r->i_x, r->i_y, r->p_picture,
+                         src->i_alpha * r->i_alpha / 255))
+            msg_Err(blend, "blending %4.4s to %4.4s failed",
+                    (char *)&blend->fmt_in.video.i_chroma,
+                    (char *)&blend->fmt_out.video.i_chroma );
+        else
+            done++;
+    }
+    return done;
+}