1 /*****************************************************************************
2 * input.c: common input functions
3 *****************************************************************************
4 * Copyright (C) 2010-2016 x264 project
6 * Authors: Steven Walters <kemuri9@gmail.com>
7 * Henrik Gramner <henrik@gramner.com>
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.
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.
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.
23 * This program is also available under a commercial proprietary license.
24 * For more information, contact us at licensing@x264.com.
25 *****************************************************************************/
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 },
52 int x264_cli_csp_is_invalid( int csp )
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;
59 int x264_cli_csp_depth_factor( int csp )
61 if( x264_cli_csp_is_invalid( csp ) )
63 return (csp & X264_CSP_HIGH_DEPTH) ? 2 : 1;
66 uint64_t x264_cli_pic_plane_size( int csp, int width, int height, int plane )
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 )
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 );
77 uint64_t x264_cli_pic_size( int csp, int width, int height )
79 if( x264_cli_csp_is_invalid( csp ) )
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 );
88 static int x264_cli_pic_init_internal( cli_pic_t *pic, int csp, int width, int height, int align, int alloc )
90 memset( pic, 0, sizeof(cli_pic_t) );
91 int csp_mask = csp & X264_CSP_MASK;
92 if( x264_cli_csp_is_invalid( csp ) )
95 pic->img.planes = x264_cli_csps[csp_mask].planes;
97 pic->img.width = width;
98 pic->img.height = height;
99 for( int i = 0; i < pic->img.planes; i++ )
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;
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] )
118 int x264_cli_pic_alloc( cli_pic_t *pic, int csp, int width, int height )
120 return x264_cli_pic_init_internal( pic, csp, width, height, 1, 1 );
123 int x264_cli_pic_alloc_aligned( cli_pic_t *pic, int csp, int width, int height )
125 return x264_cli_pic_init_internal( pic, csp, width, height, NATIVE_ALIGN, 1 );
128 int x264_cli_pic_init_noalloc( cli_pic_t *pic, int csp, int width, int height )
130 return x264_cli_pic_init_internal( pic, csp, width, height, 1, 0 );
133 void x264_cli_pic_clean( cli_pic_t *pic )
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) );
140 const x264_cli_csp_t *x264_cli_get_csp( int csp )
142 if( x264_cli_csp_is_invalid( csp ) )
144 return x264_cli_csps + (csp&X264_CSP_MASK);
147 /* Functions for handling memory-mapped input frames */
148 int x264_cli_mmap_init( cli_mmap_t *h, FILE *fh )
151 HANDLE osfhandle = (HANDLE)_get_osfhandle( _fileno( fh ) );
152 if( osfhandle != INVALID_HANDLE_VALUE )
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;
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;
168 void *x264_cli_mmap( cli_mmap_t *h, int64_t offset, size_t size )
170 #if defined(_WIN32) || HAVE_MMAP
171 int align = offset & h->align_mask;
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? */
180 uint8_t *base = mmap( NULL, size, PROT_READ, MAP_PRIVATE, h->fd, offset );
181 if( base != MAP_FAILED )
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. */
188 madvise( base, size, MADV_WILLNEED );
189 #elif defined(POSIX_MADV_WILLNEED)
190 posix_madvise( base, size, POSIX_MADV_WILLNEED );
199 int x264_cli_munmap( cli_mmap_t *h, void *addr, size_t size )
201 #if defined(_WIN32) || HAVE_MMAP
202 void *base = (void*)((intptr_t)addr & ~h->align_mask);
204 return !UnmapViewOfFile( base );
206 return munmap( base, size + (intptr_t)addr - (intptr_t)base );
212 void x264_cli_mmap_close( cli_mmap_t *h )
215 CloseHandle( h->map_handle );