[vlc-devel] [RFC PATCH 12/13] lib: Add a picture from attachment constructor

Hugo Beauzée-Luyssen hugo at beauzee.fr
Fri Nov 6 10:22:27 CET 2020


---
 lib/picture.c          | 51 ++++++++++++++++++++++++++++++++++++++++++
 lib/picture_internal.h |  2 ++
 2 files changed, 53 insertions(+)

diff --git a/lib/picture.c b/lib/picture.c
index fc8b769b2a..ddcf1fbc67 100644
--- a/lib/picture.c
+++ b/lib/picture.c
@@ -30,6 +30,8 @@
 #include <vlc_atomic.h>
 #include <vlc_picture.h>
 #include <vlc_block.h>
+#include <vlc_image.h>
+#include <vlc_input.h>
 #include <vlc_fs.h>
 
 #include "picture_internal.h"
@@ -41,6 +43,7 @@ struct libvlc_picture_t
     block_t* converted;
     video_format_t fmt;
     libvlc_time_t time;
+    input_attachment_t* attachment;
 };
 
 libvlc_picture_t* libvlc_picture_new( vlc_object_t* p_obj, picture_t* input,
@@ -54,6 +57,7 @@ libvlc_picture_t* libvlc_picture_new( vlc_object_t* p_obj, picture_t* input,
     vlc_atomic_rc_init( &pic->rc );
     pic->type = type;
     pic->time = MS_FROM_VLC_TICK( input->date );
+    pic->attachment = NULL;
     vlc_fourcc_t format;
     switch ( type )
     {
@@ -79,6 +83,51 @@ libvlc_picture_t* libvlc_picture_new( vlc_object_t* p_obj, picture_t* input,
     return pic;
 }
 
+static void libvlc_picture_block_release( block_t* block )
+{
+    free( block );
+}
+
+static const struct vlc_block_callbacks block_cbs =
+{
+    libvlc_picture_block_release,
+};
+
+libvlc_picture_t* libvlc_picture_from_attachment( input_attachment_t* attachment )
+{
+    vlc_fourcc_t fcc = image_Mime2Fourcc( attachment->psz_mime );
+    if ( !fcc || ( fcc != VLC_CODEC_PNG && fcc != VLC_CODEC_JPEG ) )
+        return NULL;
+    libvlc_picture_t *pic = malloc( sizeof( *pic ) );
+    if ( unlikely( pic == NULL ) )
+        return NULL;
+    pic->converted = malloc( sizeof( *pic->converted ) );
+    if ( unlikely( pic->converted == NULL ) )
+    {
+        free(pic);
+        return NULL;
+    }
+    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 )
+    {
+    case VLC_CODEC_PNG:
+        pic->type = libvlc_picture_Png;
+        break;
+    case VLC_CODEC_JPEG:
+        pic->type = libvlc_picture_Jpg;
+        break;
+    default:
+        vlc_assert_unreachable();
+    }
+
+    return pic;
+}
+
 void libvlc_picture_retain( libvlc_picture_t* pic )
 {
     vlc_atomic_rc_inc( &pic->rc );
@@ -91,6 +140,8 @@ void libvlc_picture_release( libvlc_picture_t* pic )
     video_format_Clean( &pic->fmt );
     if ( pic->converted )
         block_Release( pic->converted );
+    if ( pic->attachment )
+        vlc_input_attachment_Release( pic->attachment );
     free( pic );
 }
 
diff --git a/lib/picture_internal.h b/lib/picture_internal.h
index aeb1e3f505..9f66c554cb 100644
--- a/lib/picture_internal.h
+++ b/lib/picture_internal.h
@@ -44,4 +44,6 @@ libvlc_picture_t* libvlc_picture_new( vlc_object_t* p_obj, picture_t* p_pic,
                                       unsigned int i_width, unsigned int i_height,
                                       bool b_crop );
 
+libvlc_picture_t* libvlc_picture_from_attachment( input_attachment_t* attachment );
+
 #endif /* PICTURE_INTERNAL_H */
-- 
2.28.0



More information about the vlc-devel mailing list