1 /*****************************************************************************
2 * input.c: common input functions
3 *****************************************************************************
4 * Copyright (C) 2010-2011 x264 project
6 * Authors: Steven Walters <kemuri9@gmail.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
22 * This program is also available under a commercial proprietary license.
23 * For more information, contact us at licensing@x264.com.
24 *****************************************************************************/
28 const x264_cli_csp_t x264_cli_csps[] = {
29 [X264_CSP_I420] = { "i420", 3, { 1, .5, .5 }, { 1, .5, .5 }, 2, 2 },
30 [X264_CSP_I422] = { "i422", 3, { 1, .5, .5 }, { 1, 1, 1 }, 2, 1 },
31 [X264_CSP_I444] = { "i444", 3, { 1, 1, 1 }, { 1, 1, 1 }, 1, 1 },
32 [X264_CSP_YV12] = { "yv12", 3, { 1, .5, .5 }, { 1, .5, .5 }, 2, 2 },
33 [X264_CSP_NV12] = { "nv12", 2, { 1, 1 }, { 1, .5 }, 2, 2 },
34 [X264_CSP_BGR] = { "bgr", 1, { 3 }, { 1 }, 1, 1 },
35 [X264_CSP_BGRA] = { "bgra", 1, { 4 }, { 1 }, 1, 1 },
36 [X264_CSP_RGB] = { "rgb", 1, { 3 }, { 1 }, 1, 1 },
39 int x264_cli_csp_is_invalid( int csp )
41 int csp_mask = csp & X264_CSP_MASK;
42 return csp_mask <= X264_CSP_NONE || csp_mask >= X264_CSP_CLI_MAX || csp & X264_CSP_OTHER;
45 int x264_cli_csp_depth_factor( int csp )
47 if( x264_cli_csp_is_invalid( csp ) )
49 return (csp & X264_CSP_HIGH_DEPTH) ? 2 : 1;
52 uint64_t x264_cli_pic_plane_size( int csp, int width, int height, int plane )
54 int csp_mask = csp & X264_CSP_MASK;
55 if( x264_cli_csp_is_invalid( csp ) || plane < 0 || plane >= x264_cli_csps[csp_mask].planes )
57 uint64_t size = (uint64_t)width * height;
58 size *= x264_cli_csps[csp_mask].width[plane] * x264_cli_csps[csp_mask].height[plane];
59 size *= x264_cli_csp_depth_factor( csp );
63 uint64_t x264_cli_pic_size( int csp, int width, int height )
65 if( x264_cli_csp_is_invalid( csp ) )
68 int csp_mask = csp & X264_CSP_MASK;
69 for( int i = 0; i < x264_cli_csps[csp_mask].planes; i++ )
70 size += x264_cli_pic_plane_size( csp, width, height, i );
74 int x264_cli_pic_alloc( cli_pic_t *pic, int csp, int width, int height )
76 memset( pic, 0, sizeof(cli_pic_t) );
77 int csp_mask = csp & X264_CSP_MASK;
78 if( x264_cli_csp_is_invalid( csp ) )
81 pic->img.planes = x264_cli_csps[csp_mask].planes;
83 pic->img.width = width;
84 pic->img.height = height;
85 for( int i = 0; i < pic->img.planes; i++ )
87 pic->img.plane[i] = x264_malloc( x264_cli_pic_plane_size( csp, width, height, i ) );
88 if( !pic->img.plane[i] )
90 pic->img.stride[i] = width * x264_cli_csps[csp_mask].width[i] * x264_cli_csp_depth_factor( csp );
96 void x264_cli_pic_clean( cli_pic_t *pic )
98 for( int i = 0; i < pic->img.planes; i++ )
99 x264_free( pic->img.plane[i] );
100 memset( pic, 0, sizeof(cli_pic_t) );
103 const x264_cli_csp_t *x264_cli_get_csp( int csp )
105 if( x264_cli_csp_is_invalid( csp ) )
107 return x264_cli_csps + (csp&X264_CSP_MASK);