1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003-2014 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];
41 #define Y4M_MAGIC "YUV4MPEG2"
42 #define MAX_YUV4_HEADER 80
43 #define Y4M_FRAME_MAGIC "FRAME"
44 #define MAX_FRAME_HEADER 80
46 static int parse_csp_and_depth( char *csp_name, int *bit_depth )
48 int csp = X264_CSP_MAX;
50 /* Set colorspace from known variants */
51 if( !strncmp( "420", csp_name, 3 ) )
53 else if( !strncmp( "422", csp_name, 3 ) )
55 else if( !strncmp( "444", csp_name, 3 ) && strncmp( "444alpha", csp_name, 8 ) ) // only accept alphaless 4:4:4
58 /* Set high bit depth from known extensions */
59 if( sscanf( csp_name, "%*d%*[pP]%d", bit_depth ) != 1 )
65 static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
67 y4m_hnd_t *h = malloc( sizeof(y4m_hnd_t) );
70 char header[MAX_YUV4_HEADER+10];
71 char *tokend, *header_end;
72 int colorspace = X264_CSP_NONE;
73 int alt_colorspace = X264_CSP_NONE;
74 int alt_bit_depth = 8;
81 if( !strcmp( psz_filename, "-" ) )
84 h->fh = x264_fopen(psz_filename, "rb");
88 h->frame_header_len = strlen( Y4M_FRAME_MAGIC )+1;
91 for( i = 0; i < MAX_YUV4_HEADER; i++ )
93 header[i] = fgetc( h->fh );
94 if( header[i] == '\n' )
96 /* Add a space after last option. Makes parsing "444" vs
103 if( i == MAX_YUV4_HEADER || strncmp( header, Y4M_MAGIC, strlen( Y4M_MAGIC ) ) )
106 /* Scan properties */
107 header_end = &header[i+1]; /* Include space */
108 h->seq_header_len = i+1;
109 for( char *tokstart = &header[strlen( Y4M_MAGIC )+1]; tokstart < header_end; tokstart++ )
111 if( *tokstart == 0x20 )
113 switch( *tokstart++ )
115 case 'W': /* Width. Required. */
116 info->width = strtol( tokstart, &tokend, 10 );
119 case 'H': /* Height. Required. */
120 info->height = strtol( tokstart, &tokend, 10 );
123 case 'C': /* Color space */
124 colorspace = parse_csp_and_depth( tokstart, &h->bit_depth );
125 tokstart = strchr( tokstart, 0x20 );
127 case 'I': /* Interlace type */
128 switch( *tokstart++ )
131 info->interlaced = 1;
135 info->interlaced = 1;
139 info->interlaced = 1;
147 case 'F': /* Frame rate - 0:0 if unknown */
148 if( sscanf( tokstart, "%u:%u", &n, &d ) == 2 && n && d )
150 x264_reduce_fraction( &n, &d );
154 tokstart = strchr( tokstart, 0x20 );
156 case 'A': /* Pixel aspect - 0:0 if unknown */
157 /* Don't override the aspect ratio if sar has been explicitly set on the commandline. */
158 if( sscanf( tokstart, "%u:%u", &n, &d ) == 2 && n && d )
160 x264_reduce_fraction( &n, &d );
162 info->sar_height = d;
164 tokstart = strchr( tokstart, 0x20 );
166 case 'X': /* Vendor extensions */
167 if( !strncmp( "YSCSS=", tokstart, 6 ) )
169 /* Older nonstandard pixel format representation */
171 alt_colorspace = parse_csp_and_depth( tokstart, &alt_bit_depth );
173 tokstart = strchr( tokstart, 0x20 );
178 if( colorspace == X264_CSP_NONE )
180 colorspace = alt_colorspace;
181 h->bit_depth = alt_bit_depth;
184 // default to 8bit 4:2:0 if nothing is specified
185 if( colorspace == X264_CSP_NONE )
187 colorspace = X264_CSP_I420;
191 FAIL_IF_ERROR( colorspace <= X264_CSP_NONE || colorspace >= X264_CSP_MAX, "colorspace unhandled\n" )
192 FAIL_IF_ERROR( h->bit_depth < 8 || h->bit_depth > 16, "unsupported bit depth `%d'\n", h->bit_depth );
194 info->thread_safe = 1;
195 info->num_frames = 0;
196 info->csp = colorspace;
197 h->frame_size = h->frame_header_len;
199 if( h->bit_depth > 8 )
200 info->csp |= X264_CSP_HIGH_DEPTH;
202 const x264_cli_csp_t *csp = x264_cli_get_csp( info->csp );
204 for( i = 0; i < csp->planes; i++ )
206 h->plane_size[i] = x264_cli_pic_plane_size( info->csp, info->width, info->height, i );
207 h->frame_size += h->plane_size[i];
208 /* x264_cli_pic_plane_size returns the size in bytes, we need the value in pixels from here on */
209 h->plane_size[i] /= x264_cli_csp_depth_factor( info->csp );
212 /* Most common case: frame_header = "FRAME" */
213 if( x264_is_regular_file( h->fh ) )
215 uint64_t init_pos = ftell( h->fh );
216 fseek( h->fh, 0, SEEK_END );
217 uint64_t i_size = ftell( h->fh );
218 fseek( h->fh, init_pos, SEEK_SET );
219 info->num_frames = (i_size - h->seq_header_len) / h->frame_size;
226 static int read_frame_internal( cli_pic_t *pic, y4m_hnd_t *h, int bit_depth_uc )
228 size_t slen = strlen( Y4M_FRAME_MAGIC );
229 int pixel_depth = x264_cli_csp_depth_factor( pic->img.csp );
233 /* Read frame header - without terminating '\n' */
234 if( fread( header, 1, slen, h->fh ) != slen )
238 FAIL_IF_ERROR( strncmp( header, Y4M_FRAME_MAGIC, slen ), "bad header magic (%"PRIx32" <=> %s)\n",
239 M32(header), header )
241 /* Skip most of it */
242 while( i < MAX_FRAME_HEADER && fgetc( h->fh ) != '\n' )
244 FAIL_IF_ERROR( i == MAX_FRAME_HEADER, "bad frame header!\n" )
245 h->frame_size = h->frame_size - h->frame_header_len + i+slen+1;
246 h->frame_header_len = i+slen+1;
249 for( i = 0; i < pic->img.planes && !error; i++ )
251 error |= fread( pic->img.plane[i], pixel_depth, h->plane_size[i], h->fh ) != h->plane_size[i];
254 /* upconvert non 16bit high depth planes to 16bit using the same
255 * algorithm as used in the depth filter. */
256 uint16_t *plane = (uint16_t*)pic->img.plane[i];
257 uint64_t pixel_count = h->plane_size[i];
258 int lshift = 16 - h->bit_depth;
259 for( uint64_t j = 0; j < pixel_count; j++ )
260 plane[j] = plane[j] << lshift;
266 static int read_frame( cli_pic_t *pic, hnd_t handle, int i_frame )
268 y4m_hnd_t *h = handle;
270 if( i_frame > h->next_frame )
272 if( x264_is_regular_file( h->fh ) )
273 fseek( h->fh, h->frame_size * i_frame + h->seq_header_len, SEEK_SET );
275 while( i_frame > h->next_frame )
277 if( read_frame_internal( pic, h, 0 ) )
283 if( read_frame_internal( pic, h, h->bit_depth & 7 ) )
286 h->next_frame = i_frame+1;
290 static int close_file( hnd_t handle )
292 y4m_hnd_t *h = handle;
300 const cli_input_t y4m_input = { open_file, x264_cli_pic_alloc, read_frame, NULL, x264_cli_pic_clean, close_file };