1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003-2016 x264 project
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7 * Loren Merritt <lorenm@u.washington.edu>
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 *****************************************************************************/
28 #define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, "y4m", __VA_ARGS__ )
37 uint64_t plane_size[3];
43 #define Y4M_MAGIC "YUV4MPEG2"
44 #define MAX_YUV4_HEADER 80
45 #define Y4M_FRAME_MAGIC "FRAME"
46 #define MAX_FRAME_HEADER 80
48 static int parse_csp_and_depth( char *csp_name, int *bit_depth )
50 int csp = X264_CSP_MAX;
52 /* Set colorspace from known variants */
53 if( !strncmp( "420", csp_name, 3 ) )
55 else if( !strncmp( "422", csp_name, 3 ) )
57 else if( !strncmp( "444", csp_name, 3 ) && strncmp( "444alpha", csp_name, 8 ) ) // only accept alphaless 4:4:4
60 /* Set high bit depth from known extensions */
61 if( sscanf( csp_name, "%*d%*[pP]%d", bit_depth ) != 1 )
67 static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
69 y4m_hnd_t *h = calloc( 1, sizeof(y4m_hnd_t) );
72 char header[MAX_YUV4_HEADER+10];
73 char *tokend, *header_end;
74 int colorspace = X264_CSP_NONE;
75 int alt_colorspace = X264_CSP_NONE;
76 int alt_bit_depth = 8;
82 if( !strcmp( psz_filename, "-" ) )
85 h->fh = x264_fopen(psz_filename, "rb");
90 for( i = 0; i < MAX_YUV4_HEADER; i++ )
92 header[i] = fgetc( h->fh );
93 if( header[i] == '\n' )
95 /* Add a space after last option. Makes parsing "444" vs
102 FAIL_IF_ERROR( strncmp( header, Y4M_MAGIC, sizeof(Y4M_MAGIC)-1 ), "bad sequence header magic\n" )
103 FAIL_IF_ERROR( i == MAX_YUV4_HEADER, "bad sequence header length\n" )
105 /* Scan properties */
106 header_end = &header[i+1]; /* Include space */
107 h->seq_header_len = i+1;
108 for( char *tokstart = header + sizeof(Y4M_MAGIC); tokstart < header_end; tokstart++ )
110 if( *tokstart == 0x20 )
112 switch( *tokstart++ )
114 case 'W': /* Width. Required. */
115 info->width = strtol( tokstart, &tokend, 10 );
118 case 'H': /* Height. Required. */
119 info->height = strtol( tokstart, &tokend, 10 );
122 case 'C': /* Color space */
123 colorspace = parse_csp_and_depth( tokstart, &h->bit_depth );
124 tokstart = strchr( tokstart, 0x20 );
126 case 'I': /* Interlace type */
127 switch( *tokstart++ )
130 info->interlaced = 1;
134 info->interlaced = 1;
138 info->interlaced = 1;
146 case 'F': /* Frame rate - 0:0 if unknown */
147 if( sscanf( tokstart, "%u:%u", &n, &d ) == 2 && n && d )
149 x264_reduce_fraction( &n, &d );
153 tokstart = strchr( tokstart, 0x20 );
155 case 'A': /* Pixel aspect - 0:0 if unknown */
156 /* Don't override the aspect ratio if sar has been explicitly set on the commandline. */
157 if( sscanf( tokstart, "%u:%u", &n, &d ) == 2 && n && d )
159 x264_reduce_fraction( &n, &d );
161 info->sar_height = d;
163 tokstart = strchr( tokstart, 0x20 );
165 case 'X': /* Vendor extensions */
166 if( !strncmp( "YSCSS=", tokstart, 6 ) )
168 /* Older nonstandard pixel format representation */
170 alt_colorspace = parse_csp_and_depth( tokstart, &alt_bit_depth );
172 tokstart = strchr( tokstart, 0x20 );
177 if( colorspace == X264_CSP_NONE )
179 colorspace = alt_colorspace;
180 h->bit_depth = alt_bit_depth;
183 // default to 8bit 4:2:0 if nothing is specified
184 if( colorspace == X264_CSP_NONE )
186 colorspace = X264_CSP_I420;
190 FAIL_IF_ERROR( colorspace <= X264_CSP_NONE || colorspace >= X264_CSP_MAX, "colorspace unhandled\n" )
191 FAIL_IF_ERROR( h->bit_depth < 8 || h->bit_depth > 16, "unsupported bit depth `%d'\n", h->bit_depth );
193 info->thread_safe = 1;
194 info->num_frames = 0;
195 info->csp = colorspace;
197 if( h->bit_depth > 8 )
198 info->csp |= X264_CSP_HIGH_DEPTH;
200 const x264_cli_csp_t *csp = x264_cli_get_csp( info->csp );
202 for( i = 0; i < csp->planes; i++ )
204 h->plane_size[i] = x264_cli_pic_plane_size( info->csp, info->width, info->height, i );
205 h->frame_size += h->plane_size[i];
206 /* x264_cli_pic_plane_size returns the size in bytes, we need the value in pixels from here on */
207 h->plane_size[i] /= x264_cli_csp_depth_factor( info->csp );
210 if( x264_is_regular_file( h->fh ) )
212 uint64_t init_pos = ftell( h->fh );
214 /* Find out the length of the frame header */
216 while( len <= MAX_FRAME_HEADER && fgetc( h->fh ) != '\n' )
218 FAIL_IF_ERROR( len > MAX_FRAME_HEADER || len < sizeof(Y4M_FRAME_MAGIC), "bad frame header length\n" )
219 h->frame_header_len = len;
220 h->frame_size += len;
222 fseek( h->fh, 0, SEEK_END );
223 uint64_t i_size = ftell( h->fh );
224 fseek( h->fh, init_pos, SEEK_SET );
225 info->num_frames = (i_size - h->seq_header_len) / h->frame_size;
227 /* Attempt to use memory-mapped input frames if possible */
228 if( !(h->bit_depth & 7) )
229 h->use_mmap = !x264_cli_mmap_init( &h->mmap, h->fh );
236 static int read_frame_internal( cli_pic_t *pic, y4m_hnd_t *h, int bit_depth_uc )
238 static const size_t slen = sizeof(Y4M_FRAME_MAGIC)-1;
239 int pixel_depth = x264_cli_csp_depth_factor( pic->img.csp );
240 int i = sizeof(Y4M_FRAME_MAGIC);
244 /* Verify that the frame header is valid */
247 header = (char*)pic->img.plane[0];
248 pic->img.plane[0] += h->frame_header_len;
250 /* If the header length has changed between frames the size of the mapping will be invalid.
251 * It might be possible to work around it, but I'm not aware of any tool beside fuzzers that
252 * produces y4m files with variable-length frame headers so just error out if that happens. */
253 while( i <= h->frame_header_len && header[i-1] != '\n' )
255 FAIL_IF_ERROR( i != h->frame_header_len, "bad frame header length\n" )
260 if( fread( header, 1, slen, h->fh ) != slen )
262 while( i <= MAX_FRAME_HEADER && fgetc( h->fh ) != '\n' )
264 FAIL_IF_ERROR( i > MAX_FRAME_HEADER, "bad frame header length\n" )
266 FAIL_IF_ERROR( memcmp( header, Y4M_FRAME_MAGIC, slen ), "bad frame header magic\n" )
268 for( i = 0; i < pic->img.planes; i++ )
273 pic->img.plane[i] = pic->img.plane[i-1] + pixel_depth * h->plane_size[i-1];
275 else if( fread( pic->img.plane[i], pixel_depth, h->plane_size[i], h->fh ) != h->plane_size[i] )
280 /* upconvert non 16bit high depth planes to 16bit using the same
281 * algorithm as used in the depth filter. */
282 uint16_t *plane = (uint16_t*)pic->img.plane[i];
283 uint64_t pixel_count = h->plane_size[i];
284 int lshift = 16 - h->bit_depth;
285 for( uint64_t j = 0; j < pixel_count; j++ )
286 plane[j] = plane[j] << lshift;
292 static int read_frame( cli_pic_t *pic, hnd_t handle, int i_frame )
294 y4m_hnd_t *h = handle;
298 pic->img.plane[0] = x264_cli_mmap( &h->mmap, h->frame_size * i_frame + h->seq_header_len, h->frame_size );
299 if( !pic->img.plane[0] )
302 else if( i_frame > h->next_frame )
304 if( x264_is_regular_file( h->fh ) )
305 fseek( h->fh, h->frame_size * i_frame + h->seq_header_len, SEEK_SET );
307 while( i_frame > h->next_frame )
309 if( read_frame_internal( pic, h, 0 ) )
315 if( read_frame_internal( pic, h, h->bit_depth & 7 ) )
318 h->next_frame = i_frame+1;
322 static int release_frame( cli_pic_t *pic, hnd_t handle )
324 y4m_hnd_t *h = handle;
326 return x264_cli_munmap( &h->mmap, pic->img.plane[0] - h->frame_header_len, h->frame_size );
330 static int picture_alloc( cli_pic_t *pic, hnd_t handle, int csp, int width, int height )
332 y4m_hnd_t *h = handle;
333 return (h->use_mmap ? x264_cli_pic_init_noalloc : x264_cli_pic_alloc)( pic, csp, width, height );
336 static void picture_clean( cli_pic_t *pic, hnd_t handle )
338 y4m_hnd_t *h = handle;
340 memset( pic, 0, sizeof(cli_pic_t) );
342 x264_cli_pic_clean( pic );
345 static int close_file( hnd_t handle )
347 y4m_hnd_t *h = handle;
351 x264_cli_mmap_close( &h->mmap );
357 const cli_input_t y4m_input = { open_file, picture_alloc, read_frame, release_frame, picture_clean, close_file };