1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003-2011 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];
40 #define Y4M_MAGIC "YUV4MPEG2"
41 #define MAX_YUV4_HEADER 80
42 #define Y4M_FRAME_MAGIC "FRAME"
43 #define MAX_FRAME_HEADER 80
45 static int csp_string_to_int( char *csp_name )
47 int csp = X264_CSP_MAX;
48 if( !strncmp( "420", csp_name, 3 ) )
50 else if( !strncmp( "422", csp_name, 3 ) )
52 else if( !strncmp( "444", csp_name, 3 ) && strncmp( "444alpha", csp_name, 8 ) ) // only accept alphaless 4:4:4
57 static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
59 y4m_hnd_t *h = malloc( sizeof(y4m_hnd_t) );
62 char header[MAX_YUV4_HEADER+10];
63 char *tokend, *header_end;
64 int colorspace = X264_CSP_NONE;
65 int alt_colorspace = X264_CSP_NONE;
72 if( !strcmp( psz_filename, "-" ) )
75 h->fh = fopen(psz_filename, "rb");
79 h->frame_header_len = strlen( Y4M_FRAME_MAGIC )+1;
82 for( i = 0; i < MAX_YUV4_HEADER; i++ )
84 header[i] = fgetc( h->fh );
85 if( header[i] == '\n' )
87 /* Add a space after last option. Makes parsing "444" vs
94 if( i == MAX_YUV4_HEADER || strncmp( header, Y4M_MAGIC, strlen( Y4M_MAGIC ) ) )
98 header_end = &header[i+1]; /* Include space */
99 h->seq_header_len = i+1;
100 for( char *tokstart = &header[strlen( Y4M_MAGIC )+1]; tokstart < header_end; tokstart++ )
102 if( *tokstart == 0x20 )
104 switch( *tokstart++ )
106 case 'W': /* Width. Required. */
107 info->width = strtol( tokstart, &tokend, 10 );
110 case 'H': /* Height. Required. */
111 info->height = strtol( tokstart, &tokend, 10 );
114 case 'C': /* Color space */
115 colorspace = csp_string_to_int( tokstart );
116 tokstart = strchr( tokstart, 0x20 );
118 case 'I': /* Interlace type */
119 switch( *tokstart++ )
122 info->interlaced = 1;
126 info->interlaced = 1;
130 info->interlaced = 1;
138 case 'F': /* Frame rate - 0:0 if unknown */
139 if( sscanf( tokstart, "%d:%d", &n, &d ) == 2 && n && d )
141 x264_reduce_fraction( &n, &d );
145 tokstart = strchr( tokstart, 0x20 );
147 case 'A': /* Pixel aspect - 0:0 if unknown */
148 /* Don't override the aspect ratio if sar has been explicitly set on the commandline. */
149 if( sscanf( tokstart, "%d:%d", &n, &d ) == 2 && n && d )
151 x264_reduce_fraction( &n, &d );
153 info->sar_height = d;
155 tokstart = strchr( tokstart, 0x20 );
157 case 'X': /* Vendor extensions */
158 if( !strncmp( "YSCSS=", tokstart, 6 ) )
160 /* Older nonstandard pixel format representation */
162 alt_colorspace = csp_string_to_int( tokstart );
164 tokstart = strchr( tokstart, 0x20 );
169 if( colorspace == X264_CSP_NONE )
170 colorspace = alt_colorspace;
172 // default to 4:2:0 if nothing is specified
173 if( colorspace == X264_CSP_NONE )
174 colorspace = X264_CSP_I420;
176 FAIL_IF_ERROR( colorspace <= X264_CSP_NONE && colorspace >= X264_CSP_MAX, "colorspace unhandled\n" )
178 info->thread_safe = 1;
179 info->num_frames = 0;
180 info->csp = colorspace;
181 h->frame_size = h->frame_header_len;
182 for( i = 0; i < x264_cli_csps[info->csp].planes; i++ )
184 h->plane_size[i] = x264_cli_pic_plane_size( info->csp, info->width, info->height, i );
185 h->frame_size += h->plane_size[i];
188 /* Most common case: frame_header = "FRAME" */
189 if( x264_is_regular_file( h->fh ) )
191 uint64_t init_pos = ftell( h->fh );
192 fseek( h->fh, 0, SEEK_END );
193 uint64_t i_size = ftell( h->fh );
194 fseek( h->fh, init_pos, SEEK_SET );
195 info->num_frames = (i_size - h->seq_header_len) / h->frame_size;
202 static int read_frame_internal( cli_pic_t *pic, y4m_hnd_t *h )
204 int slen = strlen( Y4M_FRAME_MAGIC );
208 /* Read frame header - without terminating '\n' */
209 if( fread( header, 1, slen, h->fh ) != slen )
213 FAIL_IF_ERROR( strncmp( header, Y4M_FRAME_MAGIC, slen ), "bad header magic (%"PRIx32" <=> %s)\n",
214 M32(header), header )
216 /* Skip most of it */
217 while( i < MAX_FRAME_HEADER && fgetc( h->fh ) != '\n' )
219 FAIL_IF_ERROR( i == MAX_FRAME_HEADER, "bad frame header!\n" )
220 h->frame_size = h->frame_size - h->frame_header_len + i+slen+1;
221 h->frame_header_len = i+slen+1;
224 for( i = 0; i < pic->img.planes && !error; i++ )
225 error |= fread( pic->img.plane[i], h->plane_size[i], 1, h->fh ) <= 0;
229 static int read_frame( cli_pic_t *pic, hnd_t handle, int i_frame )
231 y4m_hnd_t *h = handle;
233 if( i_frame > h->next_frame )
235 if( x264_is_regular_file( h->fh ) )
236 fseek( h->fh, h->frame_size * i_frame + h->seq_header_len, SEEK_SET );
238 while( i_frame > h->next_frame )
240 if( read_frame_internal( pic, h ) )
246 if( read_frame_internal( pic, h ) )
249 h->next_frame = i_frame+1;
253 static int close_file( hnd_t handle )
255 y4m_hnd_t *h = handle;
263 const cli_input_t y4m_input = { open_file, x264_cli_pic_alloc, read_frame, NULL, x264_cli_pic_clean, close_file };