input.c 9.8 KB

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