forked from videolan/vlc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
picture.c
140 lines (124 loc) · 4.01 KB
/
picture.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*****************************************************************************
* picture.c: libvlc API picture management
*****************************************************************************
* Copyright (C) 2018 VLC authors and VideoLAN
*
* Authors: Hugo Beauzée-Luyssen <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <vlc/libvlc.h>
#include <vlc/libvlc_picture.h>
#include <vlc_atomic.h>
#include <vlc_picture.h>
#include <vlc_block.h>
#include <vlc_fs.h>
#include "picture_internal.h"
struct libvlc_picture_t
{
vlc_atomic_rc_t rc;
libvlc_picture_type_t type;
block_t* converted;
video_format_t fmt;
libvlc_time_t time;
};
libvlc_picture_t* libvlc_picture_new( vlc_object_t* p_obj, picture_t* input,
libvlc_picture_type_t type,
unsigned int width, unsigned int height,
bool crop )
{
libvlc_picture_t *pic = malloc( sizeof( *pic ) );
if ( unlikely( pic == NULL ) )
return NULL;
vlc_atomic_rc_init( &pic->rc );
pic->type = type;
pic->time = MS_FROM_VLC_TICK( input->date );
vlc_fourcc_t format;
switch ( type )
{
case libvlc_picture_Argb:
format = VLC_CODEC_ARGB;
break;
case libvlc_picture_Jpg:
format = VLC_CODEC_JPEG;
break;
case libvlc_picture_Png:
format = VLC_CODEC_PNG;
break;
default:
vlc_assert_unreachable();
}
if ( picture_Export( p_obj, &pic->converted, &pic->fmt,
input, format, width, height, crop ) != VLC_SUCCESS )
{
free( pic );
return NULL;
}
return pic;
}
void libvlc_picture_retain( libvlc_picture_t* pic )
{
vlc_atomic_rc_inc( &pic->rc );
}
void libvlc_picture_release( libvlc_picture_t* pic )
{
if ( vlc_atomic_rc_dec( &pic->rc ) == false )
return;
video_format_Clean( &pic->fmt );
if ( pic->converted )
block_Release( pic->converted );
free( pic );
}
int libvlc_picture_save( const libvlc_picture_t* pic, const char* path )
{
FILE* file = vlc_fopen( path, "wb" );
if ( !file )
return -1;
size_t res = fwrite( pic->converted->p_buffer,
pic->converted->i_buffer, 1, file );
fclose( file );
return res == 1 ? 0 : -1;
}
const unsigned char* libvlc_picture_get_buffer( const libvlc_picture_t* pic,
size_t *size )
{
assert( size != NULL );
*size = pic->converted->i_buffer;
return pic->converted->p_buffer;
}
libvlc_picture_type_t libvlc_picture_type( const libvlc_picture_t* pic )
{
return pic->type;
}
unsigned int libvlc_picture_get_stride( const libvlc_picture_t *pic )
{
assert( pic->type == libvlc_picture_Argb );
return pic->fmt.i_width * pic->fmt.i_bits_per_pixel / 8;
}
unsigned int libvlc_picture_get_width( const libvlc_picture_t* pic )
{
return pic->fmt.i_visible_width;
}
unsigned int libvlc_picture_get_height( const libvlc_picture_t* pic )
{
return pic->fmt.i_visible_height;
}
libvlc_time_t libvlc_picture_get_time( const libvlc_picture_t* pic )
{
return pic->time;
}