Fortunes about QT and "hearing" video codecs
[vlc.git] / lib / picture.c
blob0a4541baf76d98f64e2b070ee0cd68b59883829a
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_fs.h>
35 #include "picture_internal.h"
37 struct libvlc_picture_t
39 vlc_atomic_rc_t rc;
40 libvlc_picture_type_t type;
41 block_t* converted;
42 video_format_t fmt;
43 libvlc_time_t time;
46 libvlc_picture_t* libvlc_picture_new( vlc_object_t* p_obj, picture_t* input,
47 libvlc_picture_type_t type,
48 unsigned int width, unsigned int height )
50 libvlc_picture_t *pic = malloc( sizeof( *pic ) );
51 if ( unlikely( pic == NULL ) )
52 return NULL;
53 vlc_atomic_rc_init( &pic->rc );
54 pic->type = type;
55 pic->time = MS_FROM_VLC_TICK( input->date );
56 vlc_fourcc_t format;
57 switch ( type )
59 case libvlc_picture_Argb:
60 format = VLC_CODEC_ARGB;
61 break;
62 case libvlc_picture_Jpg:
63 format = VLC_CODEC_JPEG;
64 break;
65 case libvlc_picture_Png:
66 format = VLC_CODEC_PNG;
67 break;
68 default:
69 vlc_assert_unreachable();
71 if ( picture_Export( p_obj, &pic->converted, &pic->fmt,
72 input, format, width, height ) != VLC_SUCCESS )
74 free( pic );
75 return NULL;
78 return pic;
81 void libvlc_picture_retain( libvlc_picture_t* pic )
83 vlc_atomic_rc_inc( &pic->rc );
86 void libvlc_picture_release( libvlc_picture_t* pic )
88 if ( vlc_atomic_rc_dec( &pic->rc ) == false )
89 return;
90 video_format_Clean( &pic->fmt );
91 if ( pic->converted )
92 block_Release( pic->converted );
93 free( pic );
96 int libvlc_picture_save( const libvlc_picture_t* pic, const char* path )
98 FILE* file = vlc_fopen( path, "wb" );
99 if ( !file )
100 return -1;
101 size_t res = fwrite( pic->converted->p_buffer,
102 pic->converted->i_buffer, 1, file );
103 fclose( file );
104 return res == 1 ? 0 : -1;
107 const unsigned char* libvlc_picture_get_buffer( const libvlc_picture_t* pic,
108 size_t *size )
110 assert( size != NULL );
111 *size = pic->converted->i_buffer;
112 return pic->converted->p_buffer;
115 libvlc_picture_type_t libvlc_picture_type( const libvlc_picture_t* pic )
117 return pic->type;
120 unsigned int libvlc_picture_get_stride( const libvlc_picture_t *pic )
122 assert( pic->type == libvlc_picture_Argb );
123 return pic->fmt.i_width * pic->fmt.i_bits_per_pixel / 8;
126 unsigned int libvlc_picture_get_width( const libvlc_picture_t* pic )
128 return pic->fmt.i_visible_width;
131 unsigned int libvlc_picture_get_height( const libvlc_picture_t* pic )
133 return pic->fmt.i_visible_height;
136 libvlc_time_t libvlc_picture_get_time( const libvlc_picture_t* pic )
138 return pic->time;