]> git.sesse.net Git - x264/blob - input/y4m.c
Add speedcontrol file.
[x264] / input / y4m.c
1 /*****************************************************************************
2  * y4m.c: y4m input
3  *****************************************************************************
4  * Copyright (C) 2003-2016 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     cli_mmap_t mmap;
40     int use_mmap;
41 } y4m_hnd_t;
42
43 #define Y4M_MAGIC "YUV4MPEG2"
44 #define MAX_YUV4_HEADER 80
45 #define Y4M_FRAME_MAGIC "FRAME"
46 #define MAX_FRAME_HEADER 80
47
48 static int parse_csp_and_depth( char *csp_name, int *bit_depth )
49 {
50     int csp    = X264_CSP_MAX;
51
52     /* Set colorspace from known variants */
53     if( !strncmp( "420", csp_name, 3 ) )
54         csp = X264_CSP_I420;
55     else if( !strncmp( "422", csp_name, 3 ) )
56         csp = X264_CSP_I422;
57     else if( !strncmp( "444", csp_name, 3 ) && strncmp( "444alpha", csp_name, 8 ) ) // only accept alphaless 4:4:4
58         csp = X264_CSP_I444;
59
60     /* Set high bit depth from known extensions */
61     if( sscanf( csp_name, "%*d%*[pP]%d", bit_depth ) != 1 )
62         *bit_depth = 8;
63
64     return csp;
65 }
66
67 static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
68 {
69     y4m_hnd_t *h = calloc( 1, sizeof(y4m_hnd_t) );
70     int i;
71     uint32_t n, d;
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;
77     if( !h )
78         return -1;
79
80     info->vfr = 0;
81
82     if( !strcmp( psz_filename, "-" ) )
83         h->fh = stdin;
84     else
85         h->fh = x264_fopen(psz_filename, "rb");
86     if( h->fh == NULL )
87         return -1;
88
89     /* Read header */
90     for( i = 0; i < MAX_YUV4_HEADER; i++ )
91     {
92         header[i] = fgetc( h->fh );
93         if( header[i] == '\n' )
94         {
95             /* Add a space after last option. Makes parsing "444" vs
96                "444alpha" easier. */
97             header[i+1] = 0x20;
98             header[i+2] = 0;
99             break;
100         }
101     }
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" )
104
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++ )
109     {
110         if( *tokstart == 0x20 )
111             continue;
112         switch( *tokstart++ )
113         {
114             case 'W': /* Width. Required. */
115                 info->width = strtol( tokstart, &tokend, 10 );
116                 tokstart=tokend;
117                 break;
118             case 'H': /* Height. Required. */
119                 info->height = strtol( tokstart, &tokend, 10 );
120                 tokstart=tokend;
121                 break;
122             case 'C': /* Color space */
123                 colorspace = parse_csp_and_depth( tokstart, &h->bit_depth );
124                 tokstart = strchr( tokstart, 0x20 );
125                 break;
126             case 'I': /* Interlace type */
127                 switch( *tokstart++ )
128                 {
129                     case 't':
130                         info->interlaced = 1;
131                         info->tff = 1;
132                         break;
133                     case 'b':
134                         info->interlaced = 1;
135                         info->tff = 0;
136                         break;
137                     case 'm':
138                         info->interlaced = 1;
139                         break;
140                     //case '?':
141                     //case 'p':
142                     default:
143                         break;
144                 }
145                 break;
146             case 'F': /* Frame rate - 0:0 if unknown */
147                 if( sscanf( tokstart, "%u:%u", &n, &d ) == 2 && n && d )
148                 {
149                     x264_reduce_fraction( &n, &d );
150                     info->fps_num = n;
151                     info->fps_den = d;
152                 }
153                 tokstart = strchr( tokstart, 0x20 );
154                 break;
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 )
158                 {
159                     x264_reduce_fraction( &n, &d );
160                     info->sar_width  = n;
161                     info->sar_height = d;
162                 }
163                 tokstart = strchr( tokstart, 0x20 );
164                 break;
165             case 'X': /* Vendor extensions */
166                 if( !strncmp( "YSCSS=", tokstart, 6 ) )
167                 {
168                     /* Older nonstandard pixel format representation */
169                     tokstart += 6;
170                     alt_colorspace = parse_csp_and_depth( tokstart, &alt_bit_depth );
171                 }
172                 tokstart = strchr( tokstart, 0x20 );
173                 break;
174         }
175     }
176
177     if( colorspace == X264_CSP_NONE )
178     {
179         colorspace   = alt_colorspace;
180         h->bit_depth = alt_bit_depth;
181     }
182
183     // default to 8bit 4:2:0 if nothing is specified
184     if( colorspace == X264_CSP_NONE )
185     {
186         colorspace    = X264_CSP_I420;
187         h->bit_depth  = 8;
188     }
189
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 );
192
193     info->thread_safe = 1;
194     info->num_frames  = 0;
195     info->csp         = colorspace;
196
197     if( h->bit_depth > 8 )
198         info->csp |= X264_CSP_HIGH_DEPTH;
199
200     const x264_cli_csp_t *csp = x264_cli_get_csp( info->csp );
201
202     for( i = 0; i < csp->planes; i++ )
203     {
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 );
208     }
209
210     if( x264_is_regular_file( h->fh ) )
211     {
212         uint64_t init_pos = ftell( h->fh );
213
214         /* Find out the length of the frame header */
215         int len = 1;
216         while( len <= MAX_FRAME_HEADER && fgetc( h->fh ) != '\n' )
217             len++;
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;
221
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;
226
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 );
230     }
231
232     *p_handle = h;
233     return 0;
234 }
235
236 static int read_frame_internal( cli_pic_t *pic, y4m_hnd_t *h, int bit_depth_uc )
237 {
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);
241     char header_buf[16];
242     char *header;
243
244     /* Verify that the frame header is valid */
245     if( h->use_mmap )
246     {
247         header = (char*)pic->img.plane[0];
248         pic->img.plane[0] += h->frame_header_len;
249
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' )
254             i++;
255         FAIL_IF_ERROR( i != h->frame_header_len, "bad frame header length\n" )
256     }
257     else
258     {
259         header = header_buf;
260         if( fread( header, 1, slen, h->fh ) != slen )
261             return -1;
262         while( i <= MAX_FRAME_HEADER && fgetc( h->fh ) != '\n' )
263             i++;
264         FAIL_IF_ERROR( i > MAX_FRAME_HEADER, "bad frame header length\n" )
265     }
266     FAIL_IF_ERROR( memcmp( header, Y4M_FRAME_MAGIC, slen ), "bad frame header magic\n" )
267
268     for( i = 0; i < pic->img.planes; i++ )
269     {
270         if( h->use_mmap )
271         {
272             if( i )
273                 pic->img.plane[i] = pic->img.plane[i-1] + pixel_depth * h->plane_size[i-1];
274         }
275         else if( fread( pic->img.plane[i], pixel_depth, h->plane_size[i], h->fh ) != h->plane_size[i] )
276             return -1;
277
278         if( bit_depth_uc )
279         {
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;
287         }
288     }
289     return 0;
290 }
291
292 static int read_frame( cli_pic_t *pic, hnd_t handle, int i_frame )
293 {
294     y4m_hnd_t *h = handle;
295
296     if( h->use_mmap )
297     {
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] )
300             return -1;
301     }
302     else if( i_frame > h->next_frame )
303     {
304         if( x264_is_regular_file( h->fh ) )
305             fseek( h->fh, h->frame_size * i_frame + h->seq_header_len, SEEK_SET );
306         else
307             while( i_frame > h->next_frame )
308             {
309                 if( read_frame_internal( pic, h, 0 ) )
310                     return -1;
311                 h->next_frame++;
312             }
313     }
314
315     if( read_frame_internal( pic, h, h->bit_depth & 7 ) )
316         return -1;
317
318     h->next_frame = i_frame+1;
319     return 0;
320 }
321
322 static int release_frame( cli_pic_t *pic, hnd_t handle )
323 {
324     y4m_hnd_t *h = handle;
325     if( h->use_mmap )
326         return x264_cli_munmap( &h->mmap, pic->img.plane[0] - h->frame_header_len, h->frame_size );
327     return 0;
328 }
329
330 static int picture_alloc( cli_pic_t *pic, hnd_t handle, int csp, int width, int height )
331 {
332     y4m_hnd_t *h = handle;
333     return (h->use_mmap ? x264_cli_pic_init_noalloc : x264_cli_pic_alloc)( pic, csp, width, height );
334 }
335
336 static void picture_clean( cli_pic_t *pic, hnd_t handle )
337 {
338     y4m_hnd_t *h = handle;
339     if( h->use_mmap )
340         memset( pic, 0, sizeof(cli_pic_t) );
341     else
342         x264_cli_pic_clean( pic );
343 }
344
345 static int close_file( hnd_t handle )
346 {
347     y4m_hnd_t *h = handle;
348     if( !h || !h->fh )
349         return 0;
350     if( h->use_mmap )
351         x264_cli_mmap_close( &h->mmap );
352     fclose( h->fh );
353     free( h );
354     return 0;
355 }
356
357 const cli_input_t y4m_input = { open_file, picture_alloc, read_frame, release_frame, picture_clean, close_file };