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