Update Changelogs
[vlc.git] / lib / picture.c
blob5f55aeaa666ffe1c79d50f50946f707ad0065093
1 /*****************************************************************************
2 * picture.c: libvlc API picture management
3 *****************************************************************************
4 * Copyright (C) 2018 VLC authors and VideoLAN
6 * Authors: Hugo Beauzée-Luyssen <hugo@beauzee.fr>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
27 #include <vlc/libvlc.h>
28 #include <vlc/libvlc_picture.h>
30 #include <vlc_atomic.h>
31 #include <vlc_picture.h>
32 #include <vlc_block.h>
33 #include <vlc_image.h>
34 #include <vlc_input.h>
35 #include <vlc_fs.h>
37 #include "picture_internal.h"
39 struct libvlc_picture_t
41 vlc_atomic_rc_t rc;
42 libvlc_picture_type_t type;
43 block_t* converted;
44 video_format_t fmt;
45 libvlc_time_t time;
46 input_attachment_t* attachment;
49 struct libvlc_picture_list_t
51 size_t count;
52 libvlc_picture_t* pictures[];
55 libvlc_picture_t* libvlc_picture_new( vlc_object_t* p_obj, picture_t* input,
56 libvlc_picture_type_t type,
57 unsigned int width, unsigned int height,
58 bool crop )
60 libvlc_picture_t *pic = malloc( sizeof( *pic ) );
61 if ( unlikely( pic == NULL ) )
62 return NULL;
63 vlc_atomic_rc_init( &pic->rc );
64 pic->type = type;
65 pic->time = MS_FROM_VLC_TICK( input->date );
66 pic->attachment = NULL;
67 vlc_fourcc_t format;
68 switch ( type )
70 case libvlc_picture_Argb:
71 format = VLC_CODEC_ARGB;
72 break;
73 case libvlc_picture_Jpg:
74 format = VLC_CODEC_JPEG;
75 break;
76 case libvlc_picture_Png:
77 format = VLC_CODEC_PNG;
78 break;
79 default:
80 vlc_assert_unreachable();
82 if ( picture_Export( p_obj, &pic->converted, &pic->fmt,
83 input, format, width, height, crop ) != VLC_SUCCESS )
85 free( pic );
86 return NULL;
89 return pic;
92 static void libvlc_picture_block_release( block_t* block )
94 free( block );
97 static const struct vlc_block_callbacks block_cbs =
99 libvlc_picture_block_release,
102 static libvlc_picture_t* libvlc_picture_from_attachment( input_attachment_t* attachment )
104 vlc_fourcc_t fcc = image_Mime2Fourcc( attachment->psz_mime );
105 if ( fcc != VLC_CODEC_PNG && fcc != VLC_CODEC_JPEG )
106 return NULL;
107 libvlc_picture_t *pic = malloc( sizeof( *pic ) );
108 if ( unlikely( pic == NULL ) )
109 return NULL;
110 pic->converted = malloc( sizeof( *pic->converted ) );
111 if ( unlikely( pic->converted == NULL ) )
113 free(pic);
114 return NULL;
116 vlc_atomic_rc_init( &pic->rc );
117 pic->attachment = vlc_input_attachment_Hold( attachment );
118 pic->time = VLC_TICK_INVALID;
119 block_Init( pic->converted, &block_cbs, attachment->p_data,
120 attachment->i_data);
121 video_format_Init( &pic->fmt, fcc );
122 switch ( fcc )
124 case VLC_CODEC_PNG:
125 pic->type = libvlc_picture_Png;
126 break;
127 case VLC_CODEC_JPEG:
128 pic->type = libvlc_picture_Jpg;
129 break;
130 default:
131 vlc_assert_unreachable();
134 return pic;
137 void libvlc_picture_retain( libvlc_picture_t* pic )
139 vlc_atomic_rc_inc( &pic->rc );
142 void libvlc_picture_release( libvlc_picture_t* pic )
144 if ( vlc_atomic_rc_dec( &pic->rc ) == false )
145 return;
146 video_format_Clean( &pic->fmt );
147 if ( pic->converted )
148 block_Release( pic->converted );
149 if ( pic->attachment )
150 vlc_input_attachment_Release( pic->attachment );
151 free( pic );
154 int libvlc_picture_save( const libvlc_picture_t* pic, const char* path )
156 FILE* file = vlc_fopen( path, "wb" );
157 if ( !file )
158 return -1;
159 size_t res = fwrite( pic->converted->p_buffer,
160 pic->converted->i_buffer, 1, file );
161 fclose( file );
162 return res == 1 ? 0 : -1;
165 const unsigned char* libvlc_picture_get_buffer( const libvlc_picture_t* pic,
166 size_t *size )
168 assert( size != NULL );
169 *size = pic->converted->i_buffer;
170 return pic->converted->p_buffer;
173 libvlc_picture_type_t libvlc_picture_type( const libvlc_picture_t* pic )
175 return pic->type;
178 unsigned int libvlc_picture_get_stride( const libvlc_picture_t *pic )
180 assert( pic->type == libvlc_picture_Argb );
181 return pic->fmt.i_width * pic->fmt.i_bits_per_pixel / 8;
184 unsigned int libvlc_picture_get_width( const libvlc_picture_t* pic )
186 return pic->fmt.i_visible_width;
189 unsigned int libvlc_picture_get_height( const libvlc_picture_t* pic )
191 return pic->fmt.i_visible_height;
194 libvlc_time_t libvlc_picture_get_time( const libvlc_picture_t* pic )
196 return pic->time;
199 libvlc_picture_list_t* libvlc_picture_list_from_attachments( input_attachment_t** attachments,
200 size_t nb_attachments )
202 size_t size = 0;
203 libvlc_picture_list_t* list;
204 if ( mul_overflow( nb_attachments, sizeof( libvlc_picture_t* ), &size ) )
205 return NULL;
206 if ( add_overflow( size, sizeof( *list ), &size ) )
207 return NULL;
209 list = malloc( size );
210 if ( !list )
211 return NULL;
212 list->count = 0;
213 for ( size_t i = 0; i < nb_attachments; ++i )
215 input_attachment_t* a = attachments[i];
216 libvlc_picture_t *pic = libvlc_picture_from_attachment( a );
217 if( !pic )
218 continue;
219 list->pictures[list->count] = pic;
220 list->count++;
222 return list;
225 size_t libvlc_picture_list_count( const libvlc_picture_list_t* list )
227 assert( list );
228 return list->count;
231 libvlc_picture_t* libvlc_picture_list_at( const libvlc_picture_list_t* list,
232 size_t index )
234 assert( list );
235 return list->pictures[index];
238 void libvlc_picture_list_destroy( libvlc_picture_list_t* list )
240 if ( !list )
241 return;
242 for ( size_t i = 0; i < list->count; ++i )
243 libvlc_picture_release( list->pictures[i] );
244 free( list );