Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
52866 views
1
/*****************************************************************************
2
* input.c: common input functions
3
*****************************************************************************
4
* Copyright (C) 2010-2016 x264 project
5
*
6
* Authors: Steven Walters <[email protected]>
7
* Henrik Gramner <[email protected]>
8
*
9
* This program is free software; you can redistribute it and/or modify
10
* it under the terms of the GNU General Public License as published by
11
* the Free Software Foundation; either version 2 of the License, or
12
* (at your option) any later version.
13
*
14
* This program is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU General Public License for more details.
18
*
19
* You should have received a copy of the GNU General Public License
20
* along with this program; if not, write to the Free Software
21
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
22
*
23
* This program is also available under a commercial proprietary license.
24
* For more information, contact us at [email protected].
25
*****************************************************************************/
26
27
#include "input.h"
28
29
#ifdef _WIN32
30
#include <io.h>
31
#include <windows.h>
32
#elif HAVE_MMAP
33
#include <sys/mman.h>
34
#include <unistd.h>
35
#endif
36
37
const x264_cli_csp_t x264_cli_csps[] = {
38
[X264_CSP_I420] = { "i420", 3, { 1, .5, .5 }, { 1, .5, .5 }, 2, 2 },
39
[X264_CSP_I422] = { "i422", 3, { 1, .5, .5 }, { 1, 1, 1 }, 2, 1 },
40
[X264_CSP_I444] = { "i444", 3, { 1, 1, 1 }, { 1, 1, 1 }, 1, 1 },
41
[X264_CSP_YV12] = { "yv12", 3, { 1, .5, .5 }, { 1, .5, .5 }, 2, 2 },
42
[X264_CSP_YV16] = { "yv16", 3, { 1, .5, .5 }, { 1, 1, 1 }, 2, 1 },
43
[X264_CSP_YV24] = { "yv24", 3, { 1, 1, 1 }, { 1, 1, 1 }, 1, 1 },
44
[X264_CSP_NV12] = { "nv12", 2, { 1, 1 }, { 1, .5 }, 2, 2 },
45
[X264_CSP_NV21] = { "nv21", 2, { 1, 1 }, { 1, .5 }, 2, 2 },
46
[X264_CSP_NV16] = { "nv16", 2, { 1, 1 }, { 1, 1 }, 2, 1 },
47
[X264_CSP_BGR] = { "bgr", 1, { 3 }, { 1 }, 1, 1 },
48
[X264_CSP_BGRA] = { "bgra", 1, { 4 }, { 1 }, 1, 1 },
49
[X264_CSP_RGB] = { "rgb", 1, { 3 }, { 1 }, 1, 1 },
50
};
51
52
int x264_cli_csp_is_invalid( int csp )
53
{
54
int csp_mask = csp & X264_CSP_MASK;
55
return csp_mask <= X264_CSP_NONE || csp_mask >= X264_CSP_CLI_MAX ||
56
csp_mask == X264_CSP_V210 || csp & X264_CSP_OTHER;
57
}
58
59
int x264_cli_csp_depth_factor( int csp )
60
{
61
if( x264_cli_csp_is_invalid( csp ) )
62
return 0;
63
return (csp & X264_CSP_HIGH_DEPTH) ? 2 : 1;
64
}
65
66
uint64_t x264_cli_pic_plane_size( int csp, int width, int height, int plane )
67
{
68
int csp_mask = csp & X264_CSP_MASK;
69
if( x264_cli_csp_is_invalid( csp ) || plane < 0 || plane >= x264_cli_csps[csp_mask].planes )
70
return 0;
71
uint64_t size = (uint64_t)width * height;
72
size *= x264_cli_csps[csp_mask].width[plane] * x264_cli_csps[csp_mask].height[plane];
73
size *= x264_cli_csp_depth_factor( csp );
74
return size;
75
}
76
77
uint64_t x264_cli_pic_size( int csp, int width, int height )
78
{
79
if( x264_cli_csp_is_invalid( csp ) )
80
return 0;
81
uint64_t size = 0;
82
int csp_mask = csp & X264_CSP_MASK;
83
for( int i = 0; i < x264_cli_csps[csp_mask].planes; i++ )
84
size += x264_cli_pic_plane_size( csp, width, height, i );
85
return size;
86
}
87
88
static int x264_cli_pic_init_internal( cli_pic_t *pic, int csp, int width, int height, int align, int alloc )
89
{
90
memset( pic, 0, sizeof(cli_pic_t) );
91
int csp_mask = csp & X264_CSP_MASK;
92
if( x264_cli_csp_is_invalid( csp ) )
93
pic->img.planes = 0;
94
else
95
pic->img.planes = x264_cli_csps[csp_mask].planes;
96
pic->img.csp = csp;
97
pic->img.width = width;
98
pic->img.height = height;
99
for( int i = 0; i < pic->img.planes; i++ )
100
{
101
int stride = width * x264_cli_csps[csp_mask].width[i];
102
stride *= x264_cli_csp_depth_factor( csp );
103
stride = ALIGN( stride, align );
104
pic->img.stride[i] = stride;
105
106
if( alloc )
107
{
108
size_t size = (size_t)(height * x264_cli_csps[csp_mask].height[i]) * stride;
109
pic->img.plane[i] = x264_malloc( size );
110
if( !pic->img.plane[i] )
111
return -1;
112
}
113
}
114
115
return 0;
116
}
117
118
int x264_cli_pic_alloc( cli_pic_t *pic, int csp, int width, int height )
119
{
120
return x264_cli_pic_init_internal( pic, csp, width, height, 1, 1 );
121
}
122
123
int x264_cli_pic_alloc_aligned( cli_pic_t *pic, int csp, int width, int height )
124
{
125
return x264_cli_pic_init_internal( pic, csp, width, height, NATIVE_ALIGN, 1 );
126
}
127
128
int x264_cli_pic_init_noalloc( cli_pic_t *pic, int csp, int width, int height )
129
{
130
return x264_cli_pic_init_internal( pic, csp, width, height, 1, 0 );
131
}
132
133
void x264_cli_pic_clean( cli_pic_t *pic )
134
{
135
for( int i = 0; i < pic->img.planes; i++ )
136
x264_free( pic->img.plane[i] );
137
memset( pic, 0, sizeof(cli_pic_t) );
138
}
139
140
const x264_cli_csp_t *x264_cli_get_csp( int csp )
141
{
142
if( x264_cli_csp_is_invalid( csp ) )
143
return NULL;
144
return x264_cli_csps + (csp&X264_CSP_MASK);
145
}
146
147
/* Functions for handling memory-mapped input frames */
148
int x264_cli_mmap_init( cli_mmap_t *h, FILE *fh )
149
{
150
#ifdef _WIN32
151
HANDLE osfhandle = (HANDLE)_get_osfhandle( _fileno( fh ) );
152
if( osfhandle != INVALID_HANDLE_VALUE )
153
{
154
SYSTEM_INFO si;
155
GetSystemInfo( &si );
156
h->align_mask = si.dwAllocationGranularity - 1;
157
h->map_handle = CreateFileMappingW( osfhandle, NULL, PAGE_READONLY, 0, 0, NULL );
158
return !h->map_handle;
159
}
160
#elif HAVE_MMAP && defined(_SC_PAGESIZE)
161
h->align_mask = sysconf( _SC_PAGESIZE ) - 1;
162
h->fd = fileno( fh );
163
return h->align_mask < 0 || h->fd < 0;
164
#endif
165
return -1;
166
}
167
168
void *x264_cli_mmap( cli_mmap_t *h, int64_t offset, size_t size )
169
{
170
#if defined(_WIN32) || HAVE_MMAP
171
int align = offset & h->align_mask;
172
offset -= align;
173
size += align;
174
#ifdef _WIN32
175
uint8_t *base = MapViewOfFile( h->map_handle, FILE_MAP_READ, offset >> 32, offset, size );
176
/* TODO: Would PrefetchVirtualMemory() (only available on Win8+) be beneficial? */
177
if( base )
178
return base + align;
179
#else
180
uint8_t *base = mmap( NULL, size, PROT_READ, MAP_PRIVATE, h->fd, offset );
181
if( base != MAP_FAILED )
182
{
183
/* Ask the OS to readahead pages. This improves performance whereas
184
* forcing page faults by manually accessing every page does not.
185
* Some systems have implemented madvise() but not posix_madvise()
186
* and vice versa, so check both to see if either is available. */
187
#ifdef MADV_WILLNEED
188
madvise( base, size, MADV_WILLNEED );
189
#elif defined(POSIX_MADV_WILLNEED)
190
posix_madvise( base, size, POSIX_MADV_WILLNEED );
191
#endif
192
return base + align;
193
}
194
#endif
195
#endif
196
return NULL;
197
}
198
199
int x264_cli_munmap( cli_mmap_t *h, void *addr, size_t size )
200
{
201
#if defined(_WIN32) || HAVE_MMAP
202
void *base = (void*)((intptr_t)addr & ~h->align_mask);
203
#ifdef _WIN32
204
return !UnmapViewOfFile( base );
205
#else
206
return munmap( base, size + (intptr_t)addr - (intptr_t)base );
207
#endif
208
#endif
209
return -1;
210
}
211
212
void x264_cli_mmap_close( cli_mmap_t *h )
213
{
214
#ifdef _WIN32
215
CloseHandle( h->map_handle );
216
#endif
217
}
218
219