]> git.sesse.net Git - x264/blob - input/y4m.c
f32cc9b6704d923e5407463b440e1b36be230248
[x264] / input / y4m.c
1 /*****************************************************************************
2  * y4m.c: y4m input
3  *****************************************************************************
4  * Copyright (C) 2003-2015 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 #define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, "y4m", __VA_ARGS__ )
29
30 typedef struct
31 {
32     FILE *fh;
33     int next_frame;
34     int seq_header_len;
35     int frame_header_len;
36     uint64_t frame_size;
37     uint64_t plane_size[3];
38     int bit_depth;
39 } y4m_hnd_t;
40
41 #define Y4M_MAGIC "YUV4MPEG2"
42 #define MAX_YUV4_HEADER 80
43 #define Y4M_FRAME_MAGIC "FRAME"
44 #define MAX_FRAME_HEADER 80
45
46 static int parse_csp_and_depth( char *csp_name, int *bit_depth )
47 {
48     int csp    = X264_CSP_MAX;
49
50     /* Set colorspace from known variants */
51     if( !strncmp( "420", csp_name, 3 ) )
52         csp = X264_CSP_I420;
53     else if( !strncmp( "422", csp_name, 3 ) )
54         csp = X264_CSP_I422;
55     else if( !strncmp( "444", csp_name, 3 ) && strncmp( "444alpha", csp_name, 8 ) ) // only accept alphaless 4:4:4
56         csp = X264_CSP_I444;
57
58     /* Set high bit depth from known extensions */
59     if( sscanf( csp_name, "%*d%*[pP]%d", bit_depth ) != 1 )
60         *bit_depth = 8;
61
62     return csp;
63 }
64
65 static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
66 {
67     y4m_hnd_t *h = malloc( sizeof(y4m_hnd_t) );
68     int i;
69     uint32_t n, d;
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;
75     if( !h )
76         return -1;
77
78     h->next_frame = 0;
79     info->vfr = 0;
80
81     if( !strcmp( psz_filename, "-" ) )
82         h->fh = stdin;
83     else
84         h->fh = x264_fopen(psz_filename, "rb");
85     if( h->fh == NULL )
86         return -1;
87
88     h->frame_header_len = strlen( Y4M_FRAME_MAGIC )+1;
89
90     /* Read header */
91     for( i = 0; i < MAX_YUV4_HEADER; i++ )
92     {
93         header[i] = fgetc( h->fh );
94         if( header[i] == '\n' )
95         {
96             /* Add a space after last option. Makes parsing "444" vs
97                "444alpha" easier. */
98             header[i+1] = 0x20;
99             header[i+2] = 0;
100             break;
101         }
102     }
103     if( i == MAX_YUV4_HEADER || strncmp( header, Y4M_MAGIC, strlen( Y4M_MAGIC ) ) )
104         return -1;
105
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++ )
110     {
111         if( *tokstart == 0x20 )
112             continue;
113         switch( *tokstart++ )
114         {
115             case 'W': /* Width. Required. */
116                 info->width = strtol( tokstart, &tokend, 10 );
117                 tokstart=tokend;
118                 break;
119             case 'H': /* Height. Required. */
120                 info->height = strtol( tokstart, &tokend, 10 );
121                 tokstart=tokend;
122                 break;
123             case 'C': /* Color space */
124                 colorspace = parse_csp_and_depth( tokstart, &h->bit_depth );
125                 tokstart = strchr( tokstart, 0x20 );
126                 break;
127             case 'I': /* Interlace type */
128                 switch( *tokstart++ )
129                 {
130                     case 't':
131                         info->interlaced = 1;
132                         info->tff = 1;
133                         break;
134                     case 'b':
135                         info->interlaced = 1;
136                         info->tff = 0;
137                         break;
138                     case 'm':
139                         info->interlaced = 1;
140                         break;
141                     //case '?':
142                     //case 'p':
143                     default:
144                         break;
145                 }
146                 break;
147             case 'F': /* Frame rate - 0:0 if unknown */
148                 if( sscanf( tokstart, "%u:%u", &n, &d ) == 2 && n && d )
149                 {
150                     x264_reduce_fraction( &n, &d );
151                     info->fps_num = n;
152                     info->fps_den = d;
153                 }
154                 tokstart = strchr( tokstart, 0x20 );
155                 break;
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 )
159                 {
160                     x264_reduce_fraction( &n, &d );
161                     info->sar_width  = n;
162                     info->sar_height = d;
163                 }
164                 tokstart = strchr( tokstart, 0x20 );
165                 break;
166             case 'X': /* Vendor extensions */
167                 if( !strncmp( "YSCSS=", tokstart, 6 ) )
168                 {
169                     /* Older nonstandard pixel format representation */
170                     tokstart += 6;
171                     alt_colorspace = parse_csp_and_depth( tokstart, &alt_bit_depth );
172                 }
173                 tokstart = strchr( tokstart, 0x20 );
174                 break;
175         }
176     }
177
178     if( colorspace == X264_CSP_NONE )
179     {
180         colorspace   = alt_colorspace;
181         h->bit_depth = alt_bit_depth;
182     }
183
184     // default to 8bit 4:2:0 if nothing is specified
185     if( colorspace == X264_CSP_NONE )
186     {
187         colorspace    = X264_CSP_I420;
188         h->bit_depth  = 8;
189     }
190
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 );
193
194     info->thread_safe = 1;
195     info->num_frames  = 0;
196     info->csp         = colorspace;
197     h->frame_size     = h->frame_header_len;
198
199     if( h->bit_depth > 8 )
200         info->csp |= X264_CSP_HIGH_DEPTH;
201
202     const x264_cli_csp_t *csp = x264_cli_get_csp( info->csp );
203
204     for( i = 0; i < csp->planes; i++ )
205     {
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 );
210     }
211
212     /* Most common case: frame_header = "FRAME" */
213     if( x264_is_regular_file( h->fh ) )
214     {
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;
220     }
221
222     *p_handle = h;
223     return 0;
224 }
225
226 static int read_frame_internal( cli_pic_t *pic, y4m_hnd_t *h, int bit_depth_uc )
227 {
228     size_t slen = strlen( Y4M_FRAME_MAGIC );
229     int pixel_depth = x264_cli_csp_depth_factor( pic->img.csp );
230     int i = 0;
231     char header[16];
232
233     /* Read frame header - without terminating '\n' */
234     if( fread( header, 1, slen, h->fh ) != slen )
235         return -1;
236
237     header[slen] = 0;
238     FAIL_IF_ERROR( strncmp( header, Y4M_FRAME_MAGIC, slen ), "bad header magic (%"PRIx32" <=> %s)\n",
239                    M32(header), header )
240
241     /* Skip most of it */
242     while( i < MAX_FRAME_HEADER && fgetc( h->fh ) != '\n' )
243         i++;
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;
247
248     int error = 0;
249     for( i = 0; i < pic->img.planes && !error; i++ )
250     {
251         error |= fread( pic->img.plane[i], pixel_depth, h->plane_size[i], h->fh ) != h->plane_size[i];
252         if( bit_depth_uc )
253         {
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;
261         }
262     }
263     return error;
264 }
265
266 static int read_frame( cli_pic_t *pic, hnd_t handle, int i_frame )
267 {
268     y4m_hnd_t *h = handle;
269
270     if( i_frame > h->next_frame )
271     {
272         if( x264_is_regular_file( h->fh ) )
273             fseek( h->fh, h->frame_size * i_frame + h->seq_header_len, SEEK_SET );
274         else
275             while( i_frame > h->next_frame )
276             {
277                 if( read_frame_internal( pic, h, 0 ) )
278                     return -1;
279                 h->next_frame++;
280             }
281     }
282
283     if( read_frame_internal( pic, h, h->bit_depth & 7 ) )
284         return -1;
285
286     h->next_frame = i_frame+1;
287     return 0;
288 }
289
290 static int close_file( hnd_t handle )
291 {
292     y4m_hnd_t *h = handle;
293     if( !h || !h->fh )
294         return 0;
295     fclose( h->fh );
296     free( h );
297     return 0;
298 }
299
300 const cli_input_t y4m_input = { open_file, x264_cli_pic_alloc, read_frame, NULL, x264_cli_pic_clean, close_file };