]> git.sesse.net Git - vlc/blobdiff - src/misc/picture.c
block: Fix buffer total size in block_Alloc()
[vlc] / src / misc / picture.c
index 87876b0896d910b52f9c6875d44f83bf60f1cba2..b175849d17c632e744609529010dda9f72057dee 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.
  *****************************************************************************/
 
 /*****************************************************************************
@@ -37,6 +37,7 @@
 #include <vlc_picture.h>
 #include <vlc_image.h>
 #include <vlc_block.h>
+#include <vlc_atomic.h>
 
 /**
  * Allocate a new picture in the heap.
  * 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,
+                            vlc_fourcc_t i_chroma,
+                            int i_width, int i_height,
+                            int i_sar_num, int i_sar_den )
 {
     /* Make sure the real dimensions are a multiple of 16 */
     if( picture_Setup( p_pic, i_chroma, i_width, i_height,
@@ -92,11 +93,16 @@ static int vout_AllocatePicture( picture_t *p_pic,
 /*****************************************************************************
  *
  *****************************************************************************/
-static void PictureReleaseCallback( picture_t *p_picture )
+static void PictureDestroy( picture_t *p_picture )
 {
-    if( --p_picture->i_refcount > 0 )
-        return;
-    picture_Delete( p_picture );
+    assert( p_picture &&
+            vlc_atomic_get( &p_picture->gc.refcount ) == 0 &&
+            p_picture->gc.p_sys == NULL );
+
+    free( p_picture->p_q );
+    vlc_free( p_picture->p_data_orig );
+    free( p_picture->p_sys );
+    free( p_picture );
 }
 
 /*****************************************************************************
@@ -110,7 +116,11 @@ void picture_Reset( picture_t *p_picture )
     p_picture->b_progressive = false;
     p_picture->i_nb_fields = 2;
     p_picture->b_top_field_first = false;
-    picture_CleanupQuant( p_picture );
+
+    free( p_picture->p_q );
+    p_picture->p_q = NULL;
+    p_picture->i_qstride = 0;
+    p_picture->i_qtype = 0;
 }
 
 /*****************************************************************************
@@ -133,9 +143,9 @@ 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;
+    vlc_atomic_set( &p_picture->gc.refcount, 0 );
+    p_picture->gc.pf_destroy = NULL;
+    p_picture->gc.p_sys = NULL;
 
     p_picture->i_nb_fields = 2;
 
@@ -167,6 +177,7 @@ 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;
@@ -229,9 +240,9 @@ 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,
+                             fmt.i_chroma, fmt.i_width, fmt.i_height,
+                             fmt.i_sar_num, fmt.i_sar_den ) )
         {
             free( p_picture );
             return NULL;
@@ -239,8 +250,10 @@ picture_t *picture_NewFromResource( const video_format_t *p_fmt, const picture_r
     }
     /* */
     p_picture->format = fmt;
-    p_picture->i_refcount = 1;
-    p_picture->pf_release = PictureReleaseCallback;
+
+    vlc_atomic_set( &p_picture->gc.refcount, 1 );
+    p_picture->gc.pf_destroy = PictureDestroy;
+    p_picture->gc.p_sys = NULL;
 
     return p_picture;
 }
@@ -262,28 +275,28 @@ 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 );
-    vlc_free( p_picture->p_data_orig );
-    free( p_picture->p_sys );
-    free( p_picture );
+picture_t *picture_Hold( picture_t *p_picture )
+{
+    vlc_atomic_inc( &p_picture->gc.refcount );
+    return p_picture;
 }
 
-/*****************************************************************************
- *
- *****************************************************************************/
-void picture_CopyPixels( picture_t *p_dst, const picture_t *p_src )
+void picture_Release( picture_t *p_picture )
 {
-    int i;
+    if( vlc_atomic_dec( &p_picture->gc.refcount ) == 0 &&
+        p_picture->gc.pf_destroy )
+        p_picture->gc.pf_destroy( p_picture );
+}
 
-    for( i = 0; i < p_src->i_planes ; i++ )
-        plane_CopyPixels( p_dst->p+i, p_src->p+i );
+bool picture_IsReferenced( picture_t *p_picture )
+{
+    return vlc_atomic_get( &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,
@@ -321,6 +334,33 @@ void plane_CopyPixels( plane_t *p_dst, const plane_t *p_src )
     }
 }
 
+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;
+
+    /* FIXME: copy ->p_q and ->p_qstride */
+}
+
+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 );
+}
+
+
 /*****************************************************************************
  *
  *****************************************************************************/
@@ -400,7 +440,6 @@ int picture_Export( vlc_object_t *p_obj,
 void picture_BlendSubpicture(picture_t *dst,
                              filter_t *blend, subpicture_t *src)
 {
-    assert(blend && dst && blend->fmt_out.video.i_chroma == dst->format.i_chroma);
     assert(src && !src->b_fade && src->b_absolute);
 
     for (subpicture_region_t *r = src->p_region; r != NULL; r = r->p_next) {