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 *****************************************************************************/
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>
37 #include "picture_internal.h"
39 struct libvlc_picture_t
42 libvlc_picture_type_t type
;
46 input_attachment_t
* attachment
;
49 struct libvlc_picture_list_t
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
,
60 libvlc_picture_t
*pic
= malloc( sizeof( *pic
) );
61 if ( unlikely( pic
== NULL
) )
63 vlc_atomic_rc_init( &pic
->rc
);
65 pic
->time
= MS_FROM_VLC_TICK( input
->date
);
66 pic
->attachment
= NULL
;
70 case libvlc_picture_Argb
:
71 format
= VLC_CODEC_ARGB
;
73 case libvlc_picture_Jpg
:
74 format
= VLC_CODEC_JPEG
;
76 case libvlc_picture_Png
:
77 format
= VLC_CODEC_PNG
;
80 vlc_assert_unreachable();
82 if ( picture_Export( p_obj
, &pic
->converted
, &pic
->fmt
,
83 input
, format
, width
, height
, crop
) != VLC_SUCCESS
)
92 static void libvlc_picture_block_release( block_t
* 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
)
107 libvlc_picture_t
*pic
= malloc( sizeof( *pic
) );
108 if ( unlikely( pic
== NULL
) )
110 pic
->converted
= malloc( sizeof( *pic
->converted
) );
111 if ( unlikely( pic
->converted
== 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
,
121 video_format_Init( &pic
->fmt
, fcc
);
125 pic
->type
= libvlc_picture_Png
;
128 pic
->type
= libvlc_picture_Jpg
;
131 vlc_assert_unreachable();
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 )
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
);
154 int libvlc_picture_save( const libvlc_picture_t
* pic
, const char* path
)
156 FILE* file
= vlc_fopen( path
, "wb" );
159 size_t res
= fwrite( pic
->converted
->p_buffer
,
160 pic
->converted
->i_buffer
, 1, file
);
162 return res
== 1 ? 0 : -1;
165 const unsigned char* libvlc_picture_get_buffer( const libvlc_picture_t
* pic
,
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
)
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
)
199 libvlc_picture_list_t
* libvlc_picture_list_from_attachments( input_attachment_t
** attachments
,
200 size_t nb_attachments
)
203 libvlc_picture_list_t
* list
;
204 if ( mul_overflow( nb_attachments
, sizeof( libvlc_picture_t
* ), &size
) )
206 if ( add_overflow( size
, sizeof( *list
), &size
) )
209 list
= malloc( size
);
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
);
219 list
->pictures
[list
->count
] = pic
;
225 size_t libvlc_picture_list_count( const libvlc_picture_list_t
* list
)
231 libvlc_picture_t
* libvlc_picture_list_at( const libvlc_picture_list_t
* list
,
235 return list
->pictures
[index
];
238 void libvlc_picture_list_destroy( libvlc_picture_list_t
* list
)
242 for ( size_t i
= 0; i
< list
->count
; ++i
)
243 libvlc_picture_release( list
->pictures
[i
] );