]> git.sesse.net Git - x264/blob - input/y4m.c
y4m: Support extended frame headers when seeking
[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 = calloc( 1, 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     info->vfr = 0;
79
80     if( !strcmp( psz_filename, "-" ) )
81         h->fh = stdin;
82     else
83         h->fh = x264_fopen(psz_filename, "rb");
84     if( h->fh == NULL )
85         return -1;
86
87     /* Read header */
88     for( i = 0; i < MAX_YUV4_HEADER; i++ )
89     {
90         header[i] = fgetc( h->fh );
91         if( header[i] == '\n' )
92         {
93             /* Add a space after last option. Makes parsing "444" vs
94                "444alpha" easier. */
95             header[i+1] = 0x20;
96             header[i+2] = 0;
97             break;
98         }
99     }
100     if( i == MAX_YUV4_HEADER || strncmp( header, Y4M_MAGIC, sizeof(Y4M_MAGIC)-1 ) )
101         return -1;
102
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++ )
107     {
108         if( *tokstart == 0x20 )
109             continue;
110         switch( *tokstart++ )
111         {
112             case 'W': /* Width. Required. */
113                 info->width = strtol( tokstart, &tokend, 10 );
114                 tokstart=tokend;
115                 break;
116             case 'H': /* Height. Required. */
117                 info->height = strtol( tokstart, &tokend, 10 );
118                 tokstart=tokend;
119                 break;
120             case 'C': /* Color space */
121                 colorspace = parse_csp_and_depth( tokstart, &h->bit_depth );
122                 tokstart = strchr( tokstart, 0x20 );
123                 break;
124             case 'I': /* Interlace type */
125                 switch( *tokstart++ )
126                 {
127                     case 't':
128                         info->interlaced = 1;
129                         info->tff = 1;
130                         break;
131                     case 'b':
132                         info->interlaced = 1;
133                         info->tff = 0;
134                         break;
135                     case 'm':
136                         info->interlaced = 1;
137                         break;
138                     //case '?':
139                     //case 'p':
140                     default:
141                         break;
142                 }
143                 break;
144             case 'F': /* Frame rate - 0:0 if unknown */
145                 if( sscanf( tokstart, "%u:%u", &n, &d ) == 2 && n && d )
146                 {
147                     x264_reduce_fraction( &n, &d );
148                     info->fps_num = n;
149                     info->fps_den = d;
150                 }
151                 tokstart = strchr( tokstart, 0x20 );
152                 break;
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 )
156                 {
157                     x264_reduce_fraction( &n, &d );
158                     info->sar_width  = n;
159                     info->sar_height = d;
160                 }
161                 tokstart = strchr( tokstart, 0x20 );
162                 break;
163             case 'X': /* Vendor extensions */
164                 if( !strncmp( "YSCSS=", tokstart, 6 ) )
165                 {
166                     /* Older nonstandard pixel format representation */
167                     tokstart += 6;
168                     alt_colorspace = parse_csp_and_depth( tokstart, &alt_bit_depth );
169                 }
170                 tokstart = strchr( tokstart, 0x20 );
171                 break;
172         }
173     }
174
175     if( colorspace == X264_CSP_NONE )
176     {
177         colorspace   = alt_colorspace;
178         h->bit_depth = alt_bit_depth;
179     }
180
181     // default to 8bit 4:2:0 if nothing is specified
182     if( colorspace == X264_CSP_NONE )
183     {
184         colorspace    = X264_CSP_I420;
185         h->bit_depth  = 8;
186     }
187
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 );
190
191     info->thread_safe = 1;
192     info->num_frames  = 0;
193     info->csp         = colorspace;
194
195     if( h->bit_depth > 8 )
196         info->csp |= X264_CSP_HIGH_DEPTH;
197
198     const x264_cli_csp_t *csp = x264_cli_get_csp( info->csp );
199
200     for( i = 0; i < csp->planes; i++ )
201     {
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 );
206     }
207
208     if( x264_is_regular_file( h->fh ) )
209     {
210         uint64_t init_pos = ftell( h->fh );
211
212         /* Find out the length of the frame header */
213         int len = 1;
214         while( len <= MAX_FRAME_HEADER && fgetc( h->fh ) != '\n' )
215             len++;
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;
219
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;
224     }
225
226     *p_handle = h;
227     return 0;
228 }
229
230 static int read_frame_internal( cli_pic_t *pic, y4m_hnd_t *h, int bit_depth_uc )
231 {
232     static const size_t slen = sizeof(Y4M_FRAME_MAGIC)-1;
233     int pixel_depth = x264_cli_csp_depth_factor( pic->img.csp );
234     int i = 0;
235     char header[16];
236
237     /* Read frame header - without terminating '\n' */
238     if( fread( header, 1, slen, h->fh ) != slen )
239         return -1;
240
241     header[slen] = 0;
242     FAIL_IF_ERROR( strncmp( header, Y4M_FRAME_MAGIC, slen ), "bad header magic (%"PRIx32" <=> %s)\n",
243                    M32(header), header )
244
245     /* Skip most of it */
246     while( i < MAX_FRAME_HEADER && fgetc( h->fh ) != '\n' )
247         i++;
248     FAIL_IF_ERROR( i == MAX_FRAME_HEADER, "bad frame header!\n" )
249
250     int error = 0;
251     for( i = 0; i < pic->img.planes && !error; i++ )
252     {
253         error |= fread( pic->img.plane[i], pixel_depth, h->plane_size[i], h->fh ) != h->plane_size[i];
254         if( bit_depth_uc )
255         {
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;
263         }
264     }
265     return error;
266 }
267
268 static int read_frame( cli_pic_t *pic, hnd_t handle, int i_frame )
269 {
270     y4m_hnd_t *h = handle;
271
272     if( i_frame > h->next_frame )
273     {
274         if( x264_is_regular_file( h->fh ) )
275             fseek( h->fh, h->frame_size * i_frame + h->seq_header_len, SEEK_SET );
276         else
277             while( i_frame > h->next_frame )
278             {
279                 if( read_frame_internal( pic, h, 0 ) )
280                     return -1;
281                 h->next_frame++;
282             }
283     }
284
285     if( read_frame_internal( pic, h, h->bit_depth & 7 ) )
286         return -1;
287
288     h->next_frame = i_frame+1;
289     return 0;
290 }
291
292 static int close_file( hnd_t handle )
293 {
294     y4m_hnd_t *h = handle;
295     if( !h || !h->fh )
296         return 0;
297     fclose( h->fh );
298     free( h );
299     return 0;
300 }
301
302 const cli_input_t y4m_input = { open_file, x264_cli_pic_alloc, read_frame, NULL, x264_cli_pic_clean, close_file };