]> git.sesse.net Git - x264/blob - input/raw.c
Disable progress for FFMS input with --no-progress
[x264] / input / raw.c
1 /*****************************************************************************
2  * raw.c: raw input
3  *****************************************************************************
4  * Copyright (C) 2003-2011 x264 project
5  *
6  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7  *          Loren Merritt <lorenm@u.washington.edu>
8  *          Steven Walters <kemuri9@gmail.com>
9  *
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.
14  *
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.
19  *
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  *
24  * This program is also available under a commercial proprietary license.
25  * For more information, contact us at licensing@x264.com.
26  *****************************************************************************/
27
28 #include "input.h"
29 #define FAIL_IF_ERROR( cond, ... ) FAIL_IF_ERR( cond, "raw", __VA_ARGS__ )
30
31 typedef struct
32 {
33     FILE *fh;
34     int next_frame;
35     uint64_t plane_size[4];
36     uint64_t frame_size;
37     int bit_depth;
38 } raw_hnd_t;
39
40 static int open_file( char *psz_filename, hnd_t *p_handle, video_info_t *info, cli_input_opt_t *opt )
41 {
42     raw_hnd_t *h = calloc( 1, sizeof(raw_hnd_t) );
43     if( !h )
44         return -1;
45
46     if( !opt->resolution )
47     {
48         /* try to parse the file name */
49         for( char *p = psz_filename; *p; p++ )
50             if( *p >= '0' && *p <= '9' && sscanf( p, "%dx%d", &info->width, &info->height ) == 2 )
51                 break;
52     }
53     else
54         sscanf( opt->resolution, "%dx%d", &info->width, &info->height );
55     FAIL_IF_ERROR( !info->width || !info->height, "raw input requires a resolution.\n" )
56     if( opt->colorspace )
57     {
58         for( info->csp = X264_CSP_CLI_MAX-1; x264_cli_csps[info->csp].name && strcasecmp( x264_cli_csps[info->csp].name, opt->colorspace ); )
59             info->csp--;
60         FAIL_IF_ERROR( info->csp == X264_CSP_NONE, "unsupported colorspace `%s'\n", opt->colorspace );
61     }
62     else /* default */
63         info->csp = X264_CSP_I420;
64
65     h->bit_depth = opt->bit_depth;
66     FAIL_IF_ERROR( h->bit_depth < 8 || h->bit_depth > 16, "unsupported bit depth `%d'\n", h->bit_depth );
67     if( h->bit_depth > 8 )
68         info->csp |= X264_CSP_HIGH_DEPTH;
69
70     if( !strcmp( psz_filename, "-" ) )
71         h->fh = stdin;
72     else
73         h->fh = fopen( psz_filename, "rb" );
74     if( h->fh == NULL )
75         return -1;
76
77     info->thread_safe = 1;
78     info->num_frames  = 0;
79     info->vfr         = 0;
80
81     const x264_cli_csp_t *csp = x264_cli_get_csp( info->csp );
82     for( int i = 0; i < csp->planes; i++ )
83     {
84         h->plane_size[i] = x264_cli_pic_plane_size( info->csp, info->width, info->height, i );
85         h->frame_size += h->plane_size[i];
86         /* x264_cli_pic_plane_size returns the size in bytes, we need the value in pixels from here on */
87         h->plane_size[i] /= x264_cli_csp_depth_factor( info->csp );
88     }
89
90     if( x264_is_regular_file( h->fh ) )
91     {
92         fseek( h->fh, 0, SEEK_END );
93         uint64_t size = ftell( h->fh );
94         fseek( h->fh, 0, SEEK_SET );
95         info->num_frames = size / h->frame_size;
96     }
97
98     *p_handle = h;
99     return 0;
100 }
101
102 static int read_frame_internal( cli_pic_t *pic, raw_hnd_t *h )
103 {
104     int error = 0;
105     int pixel_depth = x264_cli_csp_depth_factor( pic->img.csp );
106     for( int i = 0; i < pic->img.planes && !error; i++ )
107     {
108         error |= fread( pic->img.plane[i], pixel_depth, h->plane_size[i], h->fh ) != h->plane_size[i];
109         if( h->bit_depth & 7 )
110         {
111             /* upconvert non 16bit high depth planes to 16bit using the same
112              * algorithm as used in the depth filter. */
113             uint16_t *plane = (uint16_t*)pic->img.plane[i];
114             uint64_t pixel_count = h->plane_size[i];
115             int lshift = 16 - h->bit_depth;
116             int rshift = 2*h->bit_depth - 16;
117             for( uint64_t j = 0; j < pixel_count; j++ )
118                 plane[j] = (plane[j] << lshift) + (plane[j] >> rshift);
119         }
120     }
121     return error;
122 }
123
124 static int read_frame( cli_pic_t *pic, hnd_t handle, int i_frame )
125 {
126     raw_hnd_t *h = handle;
127
128     if( i_frame > h->next_frame )
129     {
130         if( x264_is_regular_file( h->fh ) )
131             fseek( h->fh, i_frame * h->frame_size, SEEK_SET );
132         else
133             while( i_frame > h->next_frame )
134             {
135                 if( read_frame_internal( pic, h ) )
136                     return -1;
137                 h->next_frame++;
138             }
139     }
140
141     if( read_frame_internal( pic, h ) )
142         return -1;
143
144     h->next_frame = i_frame+1;
145     return 0;
146 }
147
148 static int close_file( hnd_t handle )
149 {
150     raw_hnd_t *h = handle;
151     if( !h || !h->fh )
152         return 0;
153     fclose( h->fh );
154     free( h );
155     return 0;
156 }
157
158 const cli_input_t raw_input = { open_file, x264_cli_pic_alloc, read_frame, NULL, x264_cli_pic_clean, close_file };