1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003-2015 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 = calloc( 1, 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;
80 if( !strcmp( psz_filename, "-" ) )
83 h->fh = x264_fopen(psz_filename, "rb");
88 for( i = 0; i < MAX_YUV4_HEADER; i++ )
90 header[i] = fgetc( h->fh );
91 if( header[i] == '\n' )
93 /* Add a space after last option. Makes parsing "444" vs
100 if( i == MAX_YUV4_HEADER || strncmp( header, Y4M_MAGIC, sizeof(Y4M_MAGIC)-1 ) )
103 /* Scan properties */
104 header_end = &header[i+1]; /* Include space */
105 h->seq_header_len = i+1;
106 for( char *tokstart = header + sizeof(Y4M_MAGIC); tokstart < header_end; tokstart++ )
108 if( *tokstart == 0x20 )
110 switch( *tokstart++ )
112 case 'W': /* Width. Required. */
113 info->width = strtol( tokstart, &tokend, 10 );
116 case 'H': /* Height. Required. */
117 info->height = strtol( tokstart, &tokend, 10 );
120 case 'C': /* Color space */
121 colorspace = parse_csp_and_depth( tokstart, &h->bit_depth );
122 tokstart = strchr( tokstart, 0x20 );
124 case 'I': /* Interlace type */
125 switch( *tokstart++ )
128 info->interlaced = 1;
132 info->interlaced = 1;
136 info->interlaced = 1;
144 case 'F': /* Frame rate - 0:0 if unknown */
145 if( sscanf( tokstart, "%u:%u", &n, &d ) == 2 && n && d )
147 x264_reduce_fraction( &n, &d );
151 tokstart = strchr( tokstart, 0x20 );
153 case 'A': /* Pixel aspect - 0:0 if unknown */
154 /* Don't override the aspect ratio if sar has been explicitly set on the commandline. */
155 if( sscanf( tokstart, "%u:%u", &n, &d ) == 2 && n && d )
157 x264_reduce_fraction( &n, &d );
159 info->sar_height = d;
161 tokstart = strchr( tokstart, 0x20 );
163 case 'X': /* Vendor extensions */
164 if( !strncmp( "YSCSS=", tokstart, 6 ) )
166 /* Older nonstandard pixel format representation */
168 alt_colorspace = parse_csp_and_depth( tokstart, &alt_bit_depth );
170 tokstart = strchr( tokstart, 0x20 );
175 if( colorspace == X264_CSP_NONE )
177 colorspace = alt_colorspace;
178 h->bit_depth = alt_bit_depth;
181 // default to 8bit 4:2:0 if nothing is specified
182 if( colorspace == X264_CSP_NONE )
184 colorspace = X264_CSP_I420;
188 FAIL_IF_ERROR( colorspace <= X264_CSP_NONE || colorspace >= X264_CSP_MAX, "colorspace unhandled\n" )
189 FAIL_IF_ERROR( h->bit_depth < 8 || h->bit_depth > 16, "unsupported bit depth `%d'\n", h->bit_depth );
191 info->thread_safe = 1;
192 info->num_frames = 0;
193 info->csp = colorspace;
195 if( h->bit_depth > 8 )
196 info->csp |= X264_CSP_HIGH_DEPTH;
198 const x264_cli_csp_t *csp = x264_cli_get_csp( info->csp );
200 for( i = 0; i < csp->planes; i++ )
202 h->plane_size[i] = x264_cli_pic_plane_size( info->csp, info->width, info->height, i );
203 h->frame_size += h->plane_size[i];
204 /* x264_cli_pic_plane_size returns the size in bytes, we need the value in pixels from here on */
205 h->plane_size[i] /= x264_cli_csp_depth_factor( info->csp );
208 if( x264_is_regular_file( h->fh ) )
210 uint64_t init_pos = ftell( h->fh );
212 /* Find out the length of the frame header */
214 while( len <= MAX_FRAME_HEADER && fgetc( h->fh ) != '\n' )
216 FAIL_IF_ERROR( len > MAX_FRAME_HEADER || len < sizeof(Y4M_FRAME_MAGIC), "bad frame header length\n" )
217 h->frame_header_len = len;
218 h->frame_size += len;
220 fseek( h->fh, 0, SEEK_END );
221 uint64_t i_size = ftell( h->fh );
222 fseek( h->fh, init_pos, SEEK_SET );
223 info->num_frames = (i_size - h->seq_header_len) / h->frame_size;
230 static int read_frame_internal( cli_pic_t *pic, y4m_hnd_t *h, int bit_depth_uc )
232 static const size_t slen = sizeof(Y4M_FRAME_MAGIC)-1;
233 int pixel_depth = x264_cli_csp_depth_factor( pic->img.csp );
237 /* Read frame header - without terminating '\n' */
238 if( fread( header, 1, slen, h->fh ) != slen )
242 FAIL_IF_ERROR( strncmp( header, Y4M_FRAME_MAGIC, slen ), "bad header magic (%"PRIx32" <=> %s)\n",
243 M32(header), header )
245 /* Skip most of it */
246 while( i < MAX_FRAME_HEADER && fgetc( h->fh ) != '\n' )
248 FAIL_IF_ERROR( i == MAX_FRAME_HEADER, "bad frame header!\n" )
251 for( i = 0; i < pic->img.planes && !error; i++ )
253 error |= fread( pic->img.plane[i], pixel_depth, h->plane_size[i], h->fh ) != h->plane_size[i];
256 /* upconvert non 16bit high depth planes to 16bit using the same
257 * algorithm as used in the depth filter. */
258 uint16_t *plane = (uint16_t*)pic->img.plane[i];
259 uint64_t pixel_count = h->plane_size[i];
260 int lshift = 16 - h->bit_depth;
261 for( uint64_t j = 0; j < pixel_count; j++ )
262 plane[j] = plane[j] << lshift;
268 static int read_frame( cli_pic_t *pic, hnd_t handle, int i_frame )
270 y4m_hnd_t *h = handle;
272 if( i_frame > h->next_frame )
274 if( x264_is_regular_file( h->fh ) )
275 fseek( h->fh, h->frame_size * i_frame + h->seq_header_len, SEEK_SET );
277 while( i_frame > h->next_frame )
279 if( read_frame_internal( pic, h, 0 ) )
285 if( read_frame_internal( pic, h, h->bit_depth & 7 ) )
288 h->next_frame = i_frame+1;
292 static int close_file( hnd_t handle )
294 y4m_hnd_t *h = handle;
302 const cli_input_t y4m_input = { open_file, x264_cli_pic_alloc, read_frame, NULL, x264_cli_pic_clean, close_file };