[vlc-commits] [Git][videolan/vlc][master] 6 commits: frame: remove unused label

Jean-Baptiste Kempf (@jbk) gitlab at videolan.org
Tue Apr 4 13:14:08 UTC 2023



Jean-Baptiste Kempf pushed to branch master at VideoLAN / VLC


Commits:
6dad92bf by Rémi Denis-Courmont at 2023-04-04T12:03:15+00:00
frame: remove unused label

- - - - -
cbe2dfa7 by Rémi Denis-Courmont at 2023-04-04T12:03:15+00:00
frame: use vlc_frame_heap_Alloc() for vlc_frame_Alloc()

There are no particular reasons to allocate the frame structure and the
frame buffer together. In fact, this prevents the effective use of
aligned allocation functions for the buffer, and reduces the
effectiveness of the address sanitiser.

This is also no longer necessary for vlc_frame_Realloc() to work; the
function now works optimally regardless of how the frame was allocated.

The ad-hoc overflow check is also dropped, as it is unnecessary whence
size is always smaller than 2^28.

- - - - -
d9bf20cf by Rémi Denis-Courmont at 2023-04-04T12:03:15+00:00
frame: use aligned_alloc() where available

- - - - -
b98765d5 by Rémi Denis-Courmont at 2023-04-04T12:03:15+00:00
frame: add vlc_frame_New()

- - - - -
0c3bc60e by Rémi Denis-Courmont at 2023-04-04T12:03:15+00:00
lib: use block_New()

- - - - -
9ae9734d by Rémi Denis-Courmont at 2023-04-04T12:03:15+00:00
frame: use vlc_frame_New()

- - - - -


5 changed files:

- include/vlc_block.h
- include/vlc_frame.h
- lib/picture.c
- src/libvlccore.sym
- src/misc/frame.c


Changes:

=====================================
include/vlc_block.h
=====================================
@@ -79,6 +79,7 @@
 #define vlc_block_callbacks vlc_frame_callbacks
 
 #define block_Init vlc_frame_Init
+#define block_New vlc_frame_New
 #define block_Alloc vlc_frame_Alloc
 #define block_TryRealloc vlc_frame_TryRealloc
 #define block_Realloc vlc_frame_Realloc


=====================================
include/vlc_frame.h
=====================================
@@ -159,6 +159,24 @@ VLC_API vlc_frame_t *vlc_frame_Init(vlc_frame_t *frame,
                                     const struct vlc_frame_callbacks *cbs,
                                     void *base, size_t length);
 
+/**
+ * Creates a custom frame.
+ *
+ * This function initialize a frame of timed data allocated by custom means.
+ * This allows passing data without copying even if the data has been allocated
+ * with unusual means or outside of LibVLC.
+ *
+ * Normally, frames are allocated and initialized by vlc_frame_Alloc() instead.
+ *
+ * @param cbs structure of custom callbacks to handle the frame [IN]
+ * @param base start address of the frame data
+ * @param length byte length of the frame data
+ *
+ * @return the created frame, or NULL on memory error.
+ */
+VLC_API vlc_frame_t *vlc_frame_New(const struct vlc_frame_callbacks *cbs,
+                                   void *base, size_t length);
+
 /**
  * Allocates a frame.
  *


=====================================
lib/picture.c
=====================================
@@ -108,7 +108,8 @@ static libvlc_picture_t* libvlc_picture_from_attachment( input_attachment_t* att
     libvlc_picture_t *pic = malloc( sizeof( *pic ) );
     if ( unlikely( pic == NULL ) )
         return NULL;
-    pic->converted = malloc( sizeof( *pic->converted ) );
+    pic->converted = block_New(&block_cbs, attachment->p_data,
+                               attachment->i_data);
     if ( unlikely( pic->converted == NULL ) )
     {
         free(pic);
@@ -117,8 +118,6 @@ static libvlc_picture_t* libvlc_picture_from_attachment( input_attachment_t* att
     vlc_atomic_rc_init( &pic->rc );
     pic->attachment = vlc_input_attachment_Hold( attachment );
     pic->time = VLC_TICK_INVALID;
-    block_Init( pic->converted, &block_cbs, attachment->p_data,
-                attachment->i_data);
     video_format_Init( &pic->fmt, fcc );
     switch ( fcc )
     {


=====================================
src/libvlccore.sym
=====================================
@@ -53,6 +53,7 @@ vlc_frame_GetAncillary
 vlc_frame_heap_Alloc
 vlc_frame_Init
 vlc_frame_mmap_Alloc
+vlc_frame_New
 vlc_frame_shm_Alloc
 vlc_frame_Realloc
 vlc_frame_Release


=====================================
src/misc/frame.c
=====================================
@@ -72,6 +72,16 @@ void vlc_frame_CopyProperties(vlc_frame_t *restrict dst, const vlc_frame_t *src)
     dst->i_length  = src->i_length;
 }
 
+vlc_frame_t *vlc_frame_New(const struct vlc_frame_callbacks *cbs,
+                           void *buf, size_t size)
+{
+    vlc_frame_t *f = malloc(sizeof (*f));
+    if (unlikely(f == NULL))
+        return NULL;
+
+    return vlc_frame_Init(f, cbs, buf, size);
+}
+
 vlc_frame_t *vlc_frame_Init(vlc_frame_t *restrict f, const struct vlc_frame_callbacks *cbs,
                             void *buf, size_t size)
 {
@@ -91,18 +101,6 @@ vlc_frame_t *vlc_frame_Init(vlc_frame_t *restrict f, const struct vlc_frame_call
     return f;
 }
 
-static void vlc_frame_generic_Release (vlc_frame_t *frame)
-{
-    /* That is always true for frames allocated with vlc_frame_Alloc(). */
-    assert (frame->p_start == (unsigned char *)(frame + 1));
-    free (frame);
-}
-
-static const struct vlc_frame_callbacks vlc_frame_generic_cbs =
-{
-    vlc_frame_generic_Release,
-};
-
 /** Initial memory alignment of data frame.
  * @note This must be a multiple of sizeof(void*) and a power of two.
  * libavcodec AVX optimizations require at least 32-bytes. */
@@ -119,22 +117,34 @@ vlc_frame_t *vlc_frame_Alloc (size_t size)
         return NULL;
     }
 
+    static_assert ((VLC_FRAME_PADDING % VLC_FRAME_ALIGN) == 0,
+                   "VLC_FRAME_PADDING must be a multiple of VLC_FRAME_ALIGN");
+
     /* 2 * VLC_FRAME_PADDING: pre + post padding */
-    const size_t alloc = sizeof (vlc_frame_t) + VLC_FRAME_ALIGN + (2 * VLC_FRAME_PADDING)
-                       + size;
-    if (unlikely(alloc <= size))
+    size_t capacity = (2 * VLC_FRAME_PADDING) + size;
+    unsigned char *buf;
+#ifdef HAVE_ALIGNED_ALLOC
+    capacity += (-size) % VLC_FRAME_ALIGN;
+    buf = aligned_alloc(VLC_FRAME_ALIGN, capacity);
+#else
+    capacity += VLC_FRAME_ALIGN;
+    buf = malloc(capacity);
+#endif
+    if (unlikely(buf == NULL))
         return NULL;
 
-    vlc_frame_t *f = malloc (alloc);
-    if (unlikely(f == NULL))
-        return NULL;
+    vlc_frame_t *f = vlc_frame_heap_Alloc(buf, capacity);
+    if (likely(f != NULL)) {
+#ifndef HAVE_ALIGNED_ALLOC
+        /* Alignment */
+        buf += (-(uintptr_t)(void *)buf) % (uintptr_t)VLC_FRAME_ALIGN;
+#endif
+        /* Header reserve */
+        buf += VLC_FRAME_PADDING;
+        f->p_buffer = buf;
+        f->i_buffer = size;
+    }
 
-    vlc_frame_Init(f, &vlc_frame_generic_cbs, f + 1, alloc - sizeof (*f));
-    static_assert ((VLC_FRAME_PADDING % VLC_FRAME_ALIGN) == 0,
-                   "VLC_FRAME_PADDING must be a multiple of VLC_FRAME_ALIGN");
-    f->p_buffer += VLC_FRAME_PADDING + VLC_FRAME_ALIGN - 1;
-    f->p_buffer = (void *)(((uintptr_t)f->p_buffer) & ~(VLC_FRAME_ALIGN - 1));
-    f->i_buffer = size;
     return f;
 }
 
@@ -261,14 +271,10 @@ static const struct vlc_frame_callbacks vlc_frame_heap_cbs =
 
 vlc_frame_t *vlc_frame_heap_Alloc (void *addr, size_t length)
 {
-    vlc_frame_t *frame = malloc (sizeof (*frame));
-    if (frame == NULL)
-    {
-        free (addr);
-        return NULL;
-    }
-
-    return vlc_frame_Init(frame, &vlc_frame_heap_cbs, addr, length);
+    vlc_frame_t *frame = vlc_frame_New(&vlc_frame_heap_cbs, addr, length);
+    if (unlikely(frame == NULL))
+        free(addr);
+    return frame;
 }
 
 #ifdef HAVE_MMAP
@@ -293,18 +299,14 @@ vlc_frame_t *vlc_frame_mmap_Alloc (void *addr, size_t length)
     long page_mask = sysconf(_SC_PAGESIZE) - 1;
     size_t left = ((uintptr_t)addr) & page_mask;
     size_t right = (-length) & page_mask;
-
-    vlc_frame_t *frame = malloc (sizeof (*frame));
-    if (frame == NULL)
-    {
-        munmap (addr, length);
-        return NULL;
-    }
-
-    vlc_frame_Init(frame, &vlc_frame_mmap_cbs,
-               ((char *)addr) - left, left + length + right);
-    frame->p_buffer = addr;
-    frame->i_buffer = length;
+    vlc_frame_t *frame = vlc_frame_New(&vlc_frame_mmap_cbs,
+                                       ((char *)addr) - left,
+                                       left + length + right);
+    if (likely(frame != NULL)) {
+        frame->p_buffer = addr;
+        frame->i_buffer = length;
+    } else
+        munmap(addr, length);
     return frame;
 }
 #else
@@ -367,14 +369,11 @@ static const struct vlc_frame_callbacks vlc_frame_shm_cbs =
 
 vlc_frame_t *vlc_frame_shm_Alloc (void *addr, size_t length)
 {
-    vlc_frame_t *frame = malloc (sizeof (*frame));
+    vlc_frame_t *frame = vlc_frame_New(&vlc_frame_shm_cbs, (uint8_t *)addr,
+                                       length);
     if (unlikely(frame == NULL))
-    {
-        shmdt (addr);
-        return NULL;
-    }
-
-    return vlc_frame_Init(frame, &vlc_frame_shm_cbs, (uint8_t *)addr, length);
+        shmdt(addr);
+    return frame;
 }
 #else
 vlc_frame_t *vlc_frame_shm_Alloc (void *addr, size_t length)
@@ -521,8 +520,8 @@ vlc_frame_t *vlc_frame_File(int fd, bool write)
         vlc_frame_Release (frame);
         frame = NULL;
         errno = EIO;
-        goto done;
     }
+done:
 #else // !_WIN32
     for (size_t i = 0; i < length;)
     {
@@ -536,7 +535,6 @@ vlc_frame_t *vlc_frame_File(int fd, bool write)
         i += len;
     }
 #endif // !_WIN32
-done:
     vlc_cleanup_pop ();
     return frame;
 }



View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/d65736cd202d620a6832a1f06d7144037de6b95f...9ae9734db433605b540afa8a56ce26a672235015

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/d65736cd202d620a6832a1f06d7144037de6b95f...9ae9734db433605b540afa8a56ce26a672235015
You're receiving this email because of your account on code.videolan.org.


VideoLAN code repository instance


More information about the vlc-commits mailing list