y4m.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*****************************************************************************
  2. * y4m.c: y4m input
  3. *****************************************************************************
  4. * Copyright (C) 2003-2018 x264 project
  5. *
  6. * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  7. * Loren Merritt <lorenm@u.washington.edu>
  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. #define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, "y4m", __VA_ARGS__ )
  28. typedef struct
  29. {
  30. FILE *fh;
  31. int next_frame;
  32. int seq_header_len;
  33. int frame_header_len;
  34. uint64_t frame_size;
  35. uint64_t plane_size[3];
  36. int bit_depth;
  37. cli_mmap_t mmap;
  38. int use_mmap;
  39. } y4m_hnd_t;
  40. #define Y4M_MAGIC "YUV4MPEG2"
  41. #define MAX_YUV4_HEADER 80
  42. #define Y4M_FRAME_MAGIC "FRAME"
  43. #define MAX_FRAME_HEADER 80
  44. static int parse_csp_and_depth( char *csp_name, int *bit_depth )
  45. {
  46. int csp = X264_CSP_MAX;
  47. /* Set colorspace from known variants */
  48. if( !strncmp( "mono", csp_name, 4 ) )
  49. csp = X264_CSP_I400;
  50. else if( !strncmp( "420", csp_name, 3 ) )
  51. csp = X264_CSP_I420;
  52. else if( !strncmp( "422", csp_name, 3 ) )
  53. csp = X264_CSP_I422;
  54. else if( !strncmp( "444", csp_name, 3 ) && strncmp( "444alpha", csp_name, 8 ) ) // only accept alphaless 4:4:4
  55. csp = X264_CSP_I444;
  56. /* Set high bit depth from known extensions */
  57. if( sscanf( csp_name, "mono%d", bit_depth ) != 1 &&
  58. sscanf( csp_name, "%*d%*[pP]%d", bit_depth ) != 1 )
  59. *bit_depth = 8;
  60. return csp;
  61. }
  62. static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
  63. {
  64. y4m_hnd_t *h = calloc( 1, sizeof(y4m_hnd_t) );
  65. int i;
  66. uint32_t n, d;
  67. char header[MAX_YUV4_HEADER+10];
  68. char *tokend, *header_end;
  69. int colorspace = X264_CSP_NONE;
  70. int alt_colorspace = X264_CSP_NONE;
  71. int alt_bit_depth = 8;
  72. if( !h )
  73. return -1;
  74. info->vfr = 0;
  75. if( !strcmp( psz_filename, "-" ) )
  76. h->fh = stdin;
  77. else
  78. h->fh = x264_fopen(psz_filename, "rb");
  79. if( h->fh == NULL )
  80. return -1;
  81. /* Read header */
  82. for( i = 0; i < MAX_YUV4_HEADER; i++ )
  83. {
  84. header[i] = fgetc( h->fh );
  85. if( header[i] == '\n' )
  86. {
  87. /* Add a space after last option. Makes parsing "444" vs
  88. "444alpha" easier. */
  89. header[i+1] = 0x20;
  90. header[i+2] = 0;
  91. break;
  92. }
  93. }
  94. FAIL_IF_ERROR( strncmp( header, Y4M_MAGIC, sizeof(Y4M_MAGIC)-1 ), "bad sequence header magic\n" );
  95. FAIL_IF_ERROR( i == MAX_YUV4_HEADER, "bad sequence header length\n" );
  96. /* Scan properties */
  97. header_end = &header[i+1]; /* Include space */
  98. h->seq_header_len = i+1;
  99. for( char *tokstart = header + sizeof(Y4M_MAGIC); tokstart < header_end; tokstart++ )
  100. {
  101. if( *tokstart == 0x20 )
  102. continue;
  103. switch( *tokstart++ )
  104. {
  105. case 'W': /* Width. Required. */
  106. info->width = strtol( tokstart, &tokend, 10 );
  107. tokstart=tokend;
  108. break;
  109. case 'H': /* Height. Required. */
  110. info->height = strtol( tokstart, &tokend, 10 );
  111. tokstart=tokend;
  112. break;
  113. case 'C': /* Color space */
  114. colorspace = parse_csp_and_depth( tokstart, &h->bit_depth );
  115. tokstart = strchr( tokstart, 0x20 );
  116. break;
  117. case 'I': /* Interlace type */
  118. switch( *tokstart++ )
  119. {
  120. case 't':
  121. info->interlaced = 1;
  122. info->tff = 1;
  123. break;
  124. case 'b':
  125. info->interlaced = 1;
  126. info->tff = 0;
  127. break;
  128. case 'm':
  129. info->interlaced = 1;
  130. break;
  131. //case '?':
  132. //case 'p':
  133. default:
  134. break;
  135. }
  136. break;
  137. case 'F': /* Frame rate - 0:0 if unknown */
  138. if( sscanf( tokstart, "%u:%u", &n, &d ) == 2 && n && d )
  139. {
  140. x264_reduce_fraction( &n, &d );
  141. info->fps_num = n;
  142. info->fps_den = d;
  143. }
  144. tokstart = strchr( tokstart, 0x20 );
  145. break;
  146. case 'A': /* Pixel aspect - 0:0 if unknown */
  147. /* Don't override the aspect ratio if sar has been explicitly set on the commandline. */
  148. if( sscanf( tokstart, "%u:%u", &n, &d ) == 2 && n && d )
  149. {
  150. x264_reduce_fraction( &n, &d );
  151. info->sar_width = n;
  152. info->sar_height = d;
  153. }
  154. tokstart = strchr( tokstart, 0x20 );
  155. break;
  156. case 'X': /* Vendor extensions */
  157. if( !strncmp( "YSCSS=", tokstart, 6 ) )
  158. {
  159. /* Older nonstandard pixel format representation */
  160. tokstart += 6;
  161. alt_colorspace = parse_csp_and_depth( tokstart, &alt_bit_depth );
  162. }
  163. tokstart = strchr( tokstart, 0x20 );
  164. break;
  165. }
  166. }
  167. if( colorspace == X264_CSP_NONE )
  168. {
  169. colorspace = alt_colorspace;
  170. h->bit_depth = alt_bit_depth;
  171. }
  172. // default to 8bit 4:2:0 if nothing is specified
  173. if( colorspace == X264_CSP_NONE )
  174. {
  175. colorspace = X264_CSP_I420;
  176. h->bit_depth = 8;
  177. }
  178. FAIL_IF_ERROR( colorspace <= X264_CSP_NONE || colorspace >= X264_CSP_MAX, "colorspace unhandled\n" );
  179. FAIL_IF_ERROR( h->bit_depth < 8 || h->bit_depth > 16, "unsupported bit depth `%d'\n", h->bit_depth );
  180. info->thread_safe = 1;
  181. info->num_frames = 0;
  182. info->csp = colorspace;
  183. if( h->bit_depth > 8 )
  184. info->csp |= X264_CSP_HIGH_DEPTH;
  185. const x264_cli_csp_t *csp = x264_cli_get_csp( info->csp );
  186. for( i = 0; i < csp->planes; i++ )
  187. {
  188. h->plane_size[i] = x264_cli_pic_plane_size( info->csp, info->width, info->height, i );
  189. h->frame_size += h->plane_size[i];
  190. /* x264_cli_pic_plane_size returns the size in bytes, we need the value in pixels from here on */
  191. h->plane_size[i] /= x264_cli_csp_depth_factor( info->csp );
  192. }
  193. if( x264_is_regular_file( h->fh ) )
  194. {
  195. uint64_t init_pos = ftell( h->fh );
  196. /* Find out the length of the frame header */
  197. int len = 1;
  198. while( len <= MAX_FRAME_HEADER && fgetc( h->fh ) != '\n' )
  199. len++;
  200. FAIL_IF_ERROR( len > MAX_FRAME_HEADER || len < sizeof(Y4M_FRAME_MAGIC), "bad frame header length\n" );
  201. h->frame_header_len = len;
  202. h->frame_size += len;
  203. fseek( h->fh, 0, SEEK_END );
  204. uint64_t i_size = ftell( h->fh );
  205. fseek( h->fh, init_pos, SEEK_SET );
  206. info->num_frames = (i_size - h->seq_header_len) / h->frame_size;
  207. FAIL_IF_ERROR( !info->num_frames, "empty input file\n" );
  208. /* Attempt to use memory-mapped input frames if possible */
  209. if( !(h->bit_depth & 7) )
  210. h->use_mmap = !x264_cli_mmap_init( &h->mmap, h->fh );
  211. }
  212. *p_handle = h;
  213. return 0;
  214. }
  215. static int read_frame_internal( cli_pic_t *pic, y4m_hnd_t *h, int bit_depth_uc )
  216. {
  217. static const size_t slen = sizeof(Y4M_FRAME_MAGIC)-1;
  218. int pixel_depth = x264_cli_csp_depth_factor( pic->img.csp );
  219. int i = sizeof(Y4M_FRAME_MAGIC);
  220. char header_buf[16];
  221. char *header;
  222. /* Verify that the frame header is valid */
  223. if( h->use_mmap )
  224. {
  225. header = (char*)pic->img.plane[0];
  226. pic->img.plane[0] += h->frame_header_len;
  227. /* If the header length has changed between frames the size of the mapping will be invalid.
  228. * It might be possible to work around it, but I'm not aware of any tool beside fuzzers that
  229. * produces y4m files with variable-length frame headers so just error out if that happens. */
  230. while( i <= h->frame_header_len && header[i-1] != '\n' )
  231. i++;
  232. FAIL_IF_ERROR( i != h->frame_header_len, "bad frame header length\n" );
  233. }
  234. else
  235. {
  236. header = header_buf;
  237. if( fread( header, 1, slen, h->fh ) != slen )
  238. return -1;
  239. while( i <= MAX_FRAME_HEADER && fgetc( h->fh ) != '\n' )
  240. i++;
  241. FAIL_IF_ERROR( i > MAX_FRAME_HEADER, "bad frame header length\n" );
  242. }
  243. FAIL_IF_ERROR( memcmp( header, Y4M_FRAME_MAGIC, slen ), "bad frame header magic\n" );
  244. for( i = 0; i < pic->img.planes; i++ )
  245. {
  246. if( h->use_mmap )
  247. {
  248. if( i )
  249. pic->img.plane[i] = pic->img.plane[i-1] + pixel_depth * h->plane_size[i-1];
  250. }
  251. else if( fread( pic->img.plane[i], pixel_depth, h->plane_size[i], h->fh ) != h->plane_size[i] )
  252. return -1;
  253. if( bit_depth_uc )
  254. {
  255. /* upconvert non 16bit high depth planes to 16bit using the same
  256. * algorithm as used in the depth filter. */
  257. uint16_t *plane = (uint16_t*)pic->img.plane[i];
  258. uint64_t pixel_count = h->plane_size[i];
  259. int lshift = 16 - h->bit_depth;
  260. for( uint64_t j = 0; j < pixel_count; j++ )
  261. plane[j] = plane[j] << lshift;
  262. }
  263. }
  264. return 0;
  265. }
  266. static int read_frame( cli_pic_t *pic, hnd_t handle, int i_frame )
  267. {
  268. y4m_hnd_t *h = handle;
  269. if( h->use_mmap )
  270. {
  271. pic->img.plane[0] = x264_cli_mmap( &h->mmap, h->frame_size * i_frame + h->seq_header_len, h->frame_size );
  272. if( !pic->img.plane[0] )
  273. return -1;
  274. }
  275. else if( i_frame > h->next_frame )
  276. {
  277. if( x264_is_regular_file( h->fh ) )
  278. fseek( h->fh, h->frame_size * i_frame + h->seq_header_len, SEEK_SET );
  279. else
  280. while( i_frame > h->next_frame )
  281. {
  282. if( read_frame_internal( pic, h, 0 ) )
  283. return -1;
  284. h->next_frame++;
  285. }
  286. }
  287. if( read_frame_internal( pic, h, h->bit_depth & 7 ) )
  288. return -1;
  289. h->next_frame = i_frame+1;
  290. return 0;
  291. }
  292. static int release_frame( cli_pic_t *pic, hnd_t handle )
  293. {
  294. y4m_hnd_t *h = handle;
  295. if( h->use_mmap )
  296. return x264_cli_munmap( &h->mmap, pic->img.plane[0] - h->frame_header_len, h->frame_size );
  297. return 0;
  298. }
  299. static int picture_alloc( cli_pic_t *pic, hnd_t handle, int csp, int width, int height )
  300. {
  301. y4m_hnd_t *h = handle;
  302. return (h->use_mmap ? x264_cli_pic_init_noalloc : x264_cli_pic_alloc)( pic, csp, width, height );
  303. }
  304. static void picture_clean( cli_pic_t *pic, hnd_t handle )
  305. {
  306. y4m_hnd_t *h = handle;
  307. if( h->use_mmap )
  308. memset( pic, 0, sizeof(cli_pic_t) );
  309. else
  310. x264_cli_pic_clean( pic );
  311. }
  312. static int close_file( hnd_t handle )
  313. {
  314. y4m_hnd_t *h = handle;
  315. if( !h || !h->fh )
  316. return 0;
  317. if( h->use_mmap )
  318. x264_cli_mmap_close( &h->mmap );
  319. fclose( h->fh );
  320. free( h );
  321. return 0;
  322. }
  323. const cli_input_t y4m_input = { open_file, picture_alloc, read_frame, release_frame, picture_clean, close_file };