1 /*****************************************************************************
2  * input.c: common input functions
3  *****************************************************************************
4  * Copyright (C) 2010-2021 x264 project
5  *
6  * Authors: Steven Walters <kemuri9@gmail.com>
7  *          Henrik Gramner <henrik@gramner.com>
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 licensing@x264.com.
25  *****************************************************************************/
26 
27 #include "input.h"
28 
29 #ifdef _WIN32
30 #include <io.h>
31 #elif HAVE_MMAP
32 #include <sys/mman.h>
33 #include <unistd.h>
34 #endif
35 
36 const x264_cli_csp_t x264_cli_csps[] = {
37     [X264_CSP_I400] = { "i400", 1, { 1 },         { 1 },         1, 1 },
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_YUYV] = { "yuyv", 1, { 2 },         { 1 },         2, 1 },
48     [X264_CSP_UYVY] = { "uyvy", 1, { 2 },         { 1 },         2, 1 },
49     [X264_CSP_BGR]  = { "bgr",  1, { 3 },         { 1 },         1, 1 },
50     [X264_CSP_BGRA] = { "bgra", 1, { 4 },         { 1 },         1, 1 },
51     [X264_CSP_RGB]  = { "rgb",  1, { 3 },         { 1 },         1, 1 },
52 };
53 
x264_cli_csp_is_invalid(int csp)54 int x264_cli_csp_is_invalid( int csp )
55 {
56     int csp_mask = csp & X264_CSP_MASK;
57     return csp_mask <= X264_CSP_NONE || csp_mask >= X264_CSP_CLI_MAX ||
58            csp_mask == X264_CSP_V210 || csp & X264_CSP_OTHER;
59 }
60 
x264_cli_csp_depth_factor(int csp)61 int x264_cli_csp_depth_factor( int csp )
62 {
63     if( x264_cli_csp_is_invalid( csp ) )
64         return 0;
65     return (csp & X264_CSP_HIGH_DEPTH) ? 2 : 1;
66 }
67 
x264_cli_pic_plane_size(int csp,int width,int height,int plane)68 int64_t x264_cli_pic_plane_size( int csp, int width, int height, int plane )
69 {
70     int csp_mask = csp & X264_CSP_MASK;
71     if( x264_cli_csp_is_invalid( csp ) || plane < 0 || plane >= x264_cli_csps[csp_mask].planes )
72         return 0;
73     int64_t size = (int64_t)width * height;
74     size *= x264_cli_csps[csp_mask].width[plane] * x264_cli_csps[csp_mask].height[plane];
75     size *= x264_cli_csp_depth_factor( csp );
76     return size;
77 }
78 
x264_cli_pic_size(int csp,int width,int height)79 int64_t x264_cli_pic_size( int csp, int width, int height )
80 {
81     if( x264_cli_csp_is_invalid( csp ) )
82         return 0;
83     int64_t size = 0;
84     int csp_mask = csp & X264_CSP_MASK;
85     for( int i = 0; i < x264_cli_csps[csp_mask].planes; i++ )
86         size += x264_cli_pic_plane_size( csp, width, height, i );
87     return size;
88 }
89 
cli_pic_init_internal(cli_pic_t * pic,int csp,int width,int height,int align,int alloc)90 static int cli_pic_init_internal( cli_pic_t *pic, int csp, int width, int height, int align, int alloc )
91 {
92     memset( pic, 0, sizeof(cli_pic_t) );
93     int csp_mask = csp & X264_CSP_MASK;
94     if( x264_cli_csp_is_invalid( csp ) )
95         pic->img.planes = 0;
96     else
97         pic->img.planes = x264_cli_csps[csp_mask].planes;
98     pic->img.csp    = csp;
99     pic->img.width  = width;
100     pic->img.height = height;
101     for( int i = 0; i < pic->img.planes; i++ )
102     {
103         int stride = width * x264_cli_csps[csp_mask].width[i];
104         stride *= x264_cli_csp_depth_factor( csp );
105         stride = ALIGN( stride, align );
106         pic->img.stride[i] = stride;
107 
108         if( alloc )
109         {
110             int64_t size = (int64_t)(height * x264_cli_csps[csp_mask].height[i]) * stride;
111             pic->img.plane[i] = x264_malloc( size );
112             if( !pic->img.plane[i] )
113                 return -1;
114         }
115     }
116 
117     return 0;
118 }
119 
x264_cli_pic_alloc(cli_pic_t * pic,int csp,int width,int height)120 int x264_cli_pic_alloc( cli_pic_t *pic, int csp, int width, int height )
121 {
122     return cli_pic_init_internal( pic, csp, width, height, 1, 1 );
123 }
124 
x264_cli_pic_alloc_aligned(cli_pic_t * pic,int csp,int width,int height)125 int x264_cli_pic_alloc_aligned( cli_pic_t *pic, int csp, int width, int height )
126 {
127     return cli_pic_init_internal( pic, csp, width, height, NATIVE_ALIGN, 1 );
128 }
129 
x264_cli_pic_init_noalloc(cli_pic_t * pic,int csp,int width,int height)130 int x264_cli_pic_init_noalloc( cli_pic_t *pic, int csp, int width, int height )
131 {
132     return cli_pic_init_internal( pic, csp, width, height, 1, 0 );
133 }
134 
x264_cli_pic_clean(cli_pic_t * pic)135 void x264_cli_pic_clean( cli_pic_t *pic )
136 {
137     for( int i = 0; i < pic->img.planes; i++ )
138         x264_free( pic->img.plane[i] );
139     memset( pic, 0, sizeof(cli_pic_t) );
140 }
141 
x264_cli_get_csp(int csp)142 const x264_cli_csp_t *x264_cli_get_csp( int csp )
143 {
144     if( x264_cli_csp_is_invalid( csp ) )
145         return NULL;
146     return x264_cli_csps + (csp&X264_CSP_MASK);
147 }
148 
149 /* Functions for handling memory-mapped input frames */
x264_cli_mmap_init(cli_mmap_t * h,FILE * fh)150 int x264_cli_mmap_init( cli_mmap_t *h, FILE *fh )
151 {
152 #if defined(_WIN32) || HAVE_MMAP
153     int fd = fileno( fh );
154     x264_struct_stat file_stat;
155     if( !x264_fstat( fd, &file_stat ) )
156     {
157         h->file_size = file_stat.st_size;
158 #ifdef _WIN32
159         HANDLE osfhandle = (HANDLE)_get_osfhandle( fd );
160         if( osfhandle != INVALID_HANDLE_VALUE )
161         {
162             SYSTEM_INFO si;
163             GetSystemInfo( &si );
164             h->page_mask = si.dwPageSize - 1;
165             h->align_mask = si.dwAllocationGranularity - 1;
166             h->prefetch_virtual_memory = (void*)GetProcAddress( GetModuleHandleW( L"kernel32.dll" ), "PrefetchVirtualMemory" );
167             h->process_handle = GetCurrentProcess();
168             h->map_handle = CreateFileMappingW( osfhandle, NULL, PAGE_READONLY, 0, 0, NULL );
169             return !h->map_handle;
170         }
171 #elif HAVE_MMAP && defined(_SC_PAGESIZE)
172         h->align_mask = sysconf( _SC_PAGESIZE ) - 1;
173         h->fd = fd;
174         return h->align_mask < 0 || fd < 0;
175 #endif
176     }
177 #endif
178     return -1;
179 }
180 
181 /* Third-party filters such as swscale can overread the input buffer which may result
182  * in segfaults. We have to pad the buffer size as a workaround to avoid that. */
183 #define MMAP_PADDING 64
184 
x264_cli_mmap(cli_mmap_t * h,int64_t offset,int64_t size)185 void *x264_cli_mmap( cli_mmap_t *h, int64_t offset, int64_t size )
186 {
187 #if defined(_WIN32) || HAVE_MMAP
188     uint8_t *base;
189     int align = offset & h->align_mask;
190     if( offset < 0 || size < 0 || (uint64_t)size > (SIZE_MAX - MMAP_PADDING - align) )
191         return NULL;
192     offset -= align;
193     size   += align;
194 #ifdef _WIN32
195     /* If the padding crosses a page boundary we need to increase the mapping size. */
196     size_t padded_size = (-size & h->page_mask) < MMAP_PADDING ? size + MMAP_PADDING : size;
197     if( (uint64_t)offset + padded_size > (uint64_t)h->file_size )
198     {
199         /* It's not possible to do the POSIX mmap() remapping trick on Windows, so if the padding crosses a
200          * page boundary past the end of the file we have to copy the entire frame into a padded buffer. */
201         if( (base = MapViewOfFile( h->map_handle, FILE_MAP_READ, (uint64_t)offset >> 32, offset, size )) )
202         {
203             uint8_t *buf = NULL;
204             HANDLE anon_map = CreateFileMappingW( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, (uint64_t)padded_size >> 32, padded_size, NULL );
205             if( anon_map )
206             {
207                 if( (buf = MapViewOfFile( anon_map, FILE_MAP_WRITE, 0, 0, 0 )) )
208                 {
209                     buf += align;
210                     memcpy( buf, base + align, size - align );
211                 }
212                 CloseHandle( anon_map );
213             }
214             UnmapViewOfFile( base );
215             return buf;
216         }
217     }
218     else if( (base = MapViewOfFile( h->map_handle, FILE_MAP_READ, (uint64_t)offset >> 32, offset, padded_size )) )
219     {
220         /* PrefetchVirtualMemory() is only available on Windows 8 and newer. */
221         if( h->prefetch_virtual_memory )
222         {
223             struct { void *addr; size_t size; } mem_range = { base, size };
224             h->prefetch_virtual_memory( h->process_handle, 1, &mem_range, 0 );
225         }
226         return base + align;
227     }
228 #else
229     size_t padded_size = size + MMAP_PADDING;
230     if( (base = mmap( NULL, padded_size, PROT_READ, MAP_PRIVATE, h->fd, offset )) != MAP_FAILED )
231     {
232         /* Ask the OS to readahead pages. This improves performance whereas
233          * forcing page faults by manually accessing every page does not.
234          * Some systems have implemented madvise() but not posix_madvise()
235          * and vice versa, so check both to see if either is available. */
236 #ifdef MADV_WILLNEED
237         madvise( base, size, MADV_WILLNEED );
238 #elif defined(POSIX_MADV_WILLNEED)
239         posix_madvise( base, size, POSIX_MADV_WILLNEED );
240 #endif
241         /* Remap the file mapping of any padding that crosses a page boundary past the end of
242          * the file into a copy of the last valid page to prevent reads from invalid memory. */
243         size_t aligned_size = (padded_size - 1) & ~h->align_mask;
244         if( offset + aligned_size >= h->file_size )
245             mmap( base + aligned_size, padded_size - aligned_size, PROT_READ, MAP_PRIVATE|MAP_FIXED, h->fd, (offset + size - 1) & ~h->align_mask );
246 
247         return base + align;
248     }
249 #endif
250 #endif
251     return NULL;
252 }
253 
x264_cli_munmap(cli_mmap_t * h,void * addr,int64_t size)254 int x264_cli_munmap( cli_mmap_t *h, void *addr, int64_t size )
255 {
256 #if defined(_WIN32) || HAVE_MMAP
257     void *base = (void*)((intptr_t)addr & ~h->align_mask);
258 #ifdef _WIN32
259     return !UnmapViewOfFile( base );
260 #else
261     if( size < 0 || size > (SIZE_MAX - MMAP_PADDING - ((intptr_t)addr - (intptr_t)base)) )
262         return -1;
263     return munmap( base, size + MMAP_PADDING + (intptr_t)addr - (intptr_t)base );
264 #endif
265 #endif
266     return -1;
267 }
268 
x264_cli_mmap_close(cli_mmap_t * h)269 void x264_cli_mmap_close( cli_mmap_t *h )
270 {
271 #ifdef _WIN32
272     CloseHandle( h->map_handle );
273 #endif
274 }
275