1 /*****************************************************************************
2 * yuv.c: x264 yuv input module
3 *****************************************************************************
4 * Copyright (C) 2003-2009 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.
22 *****************************************************************************/
33 static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
35 yuv_hnd_t *h = malloc( sizeof(yuv_hnd_t) );
39 if( !opt->resolution )
41 /* try to parse the file name */
42 for( char *p = psz_filename; *p; p++ )
43 if( *p >= '0' && *p <= '9' && sscanf( p, "%ux%u", &info->width, &info->height ) == 2 )
47 sscanf( opt->resolution, "%ux%u", &info->width, &info->height );
48 if( !info->width || !info->height )
50 fprintf( stderr, "yuv [error]: rawyuv input requires a resolution.\n" );
56 h->width = info->width;
57 h->height = info->height;
59 if( !strcmp( psz_filename, "-" ) )
62 h->fh = fopen( psz_filename, "rb" );
70 static int get_frame_total( hnd_t handle )
72 yuv_hnd_t *h = handle;
73 int i_frame_total = 0;
75 if( x264_is_regular_file( h->fh ) )
77 fseek( h->fh, 0, SEEK_END );
78 uint64_t i_size = ftell( h->fh );
79 fseek( h->fh, 0, SEEK_SET );
80 i_frame_total = (int)(i_size / ( h->width * h->height * 3 / 2 ));
86 static int read_frame_internal( x264_picture_t *p_pic, yuv_hnd_t *h )
88 return fread( p_pic->img.plane[0], h->width * h->height, 1, h->fh ) <= 0
89 || fread( p_pic->img.plane[1], h->width * h->height / 4, 1, h->fh ) <= 0
90 || fread( p_pic->img.plane[2], h->width * h->height / 4, 1, h->fh ) <= 0;
93 static int read_frame( x264_picture_t *p_pic, hnd_t handle, int i_frame )
95 yuv_hnd_t *h = handle;
97 if( i_frame > h->next_frame )
99 if( x264_is_regular_file( h->fh ) )
100 fseek( h->fh, (uint64_t)i_frame * h->width * h->height * 3 / 2, SEEK_SET );
102 while( i_frame > h->next_frame )
104 if( read_frame_internal( p_pic, h ) )
110 if( read_frame_internal( p_pic, h ) )
113 h->next_frame = i_frame+1;
117 static int close_file( hnd_t handle )
119 yuv_hnd_t *h = handle;
127 const cli_input_t yuv_input = { open_file, get_frame_total, x264_picture_alloc, read_frame, NULL, x264_picture_clean, close_file };