1 /*****************************************************************************
2 * raw.c: x264 raw input module
3 *****************************************************************************
4 * Copyright (C) 2003-2010 x264 project
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7 * Loren Merritt <lorenm@u.washington.edu>
8 * Steven Walters <kemuri9@gmail.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
23 *****************************************************************************/
26 #define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, "raw", __VA_ARGS__ )
32 uint64_t plane_size[4];
36 static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
38 raw_hnd_t *h = malloc( sizeof(raw_hnd_t) );
42 if( !opt->resolution )
44 /* try to parse the file name */
45 for( char *p = psz_filename; *p; p++ )
46 if( *p >= '0' && *p <= '9' && sscanf( p, "%ux%u", &info->width, &info->height ) == 2 )
50 sscanf( opt->resolution, "%ux%u", &info->width, &info->height );
51 FAIL_IF_ERROR( !info->width || !info->height, "raw input requires a resolution.\n" )
54 for( info->csp = X264_CSP_CLI_MAX-1; x264_cli_csps[info->csp].name && strcasecmp( x264_cli_csps[info->csp].name, opt->colorspace ); )
56 FAIL_IF_ERROR( info->csp == X264_CSP_NONE, "unsupported colorspace `%s'\n", opt->colorspace );
59 info->csp = X264_CSP_I420;
64 if( !strcmp( psz_filename, "-" ) )
67 h->fh = fopen( psz_filename, "rb" );
71 info->thread_safe = 1;
74 for( int i = 0; i < x264_cli_csps[info->csp].planes; i++ )
76 h->plane_size[i] = x264_cli_pic_plane_size( info->csp, info->width, info->height, i );
77 h->frame_size += h->plane_size[i];
80 if( x264_is_regular_file( h->fh ) )
82 fseek( h->fh, 0, SEEK_END );
83 uint64_t size = ftell( h->fh );
84 fseek( h->fh, 0, SEEK_SET );
85 info->num_frames = size / h->frame_size;
92 static int read_frame_internal( cli_pic_t *pic, raw_hnd_t *h )
95 for( int i = 0; i < pic->img.planes && !error; i++ )
96 error |= fread( pic->img.plane[i], h->plane_size[i], 1, h->fh ) <= 0;
100 static int read_frame( cli_pic_t *pic, hnd_t handle, int i_frame )
102 raw_hnd_t *h = handle;
104 if( i_frame > h->next_frame )
106 if( x264_is_regular_file( h->fh ) )
107 fseek( h->fh, i_frame * h->frame_size, SEEK_SET );
109 while( i_frame > h->next_frame )
111 if( read_frame_internal( pic, h ) )
117 if( read_frame_internal( pic, h ) )
120 h->next_frame = i_frame+1;
124 static int close_file( hnd_t handle )
126 raw_hnd_t *h = handle;
134 const cli_input_t raw_input = { open_file, x264_cli_pic_alloc, read_frame, NULL, x264_cli_pic_clean, close_file };