]> git.sesse.net Git - x264/blob - input/input.c
Bump dates to 2013
[x264] / input / input.c
1 /*****************************************************************************
2  * input.c: common input functions
3  *****************************************************************************
4  * Copyright (C) 2010-2013 x264 project
5  *
6  * Authors: Steven Walters <kemuri9@gmail.com>
7  *
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.
12  *
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.
17  *
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.
21  *
22  * This program is also available under a commercial proprietary license.
23  * For more information, contact us at licensing@x264.com.
24  *****************************************************************************/
25
26 #include "input.h"
27
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_YV16] = { "yv16", 3, { 1, .5, .5 }, { 1,  1,  1 }, 2, 1 },
34     [X264_CSP_YV24] = { "yv24", 3, { 1,  1,  1 }, { 1,  1,  1 }, 1, 1 },
35     [X264_CSP_NV12] = { "nv12", 2, { 1,  1 },     { 1, .5 },     2, 2 },
36     [X264_CSP_NV16] = { "nv16", 2, { 1,  1 },     { 1,  1 },     2, 1 },
37     [X264_CSP_BGR]  = { "bgr",  1, { 3 },         { 1 },         1, 1 },
38     [X264_CSP_BGRA] = { "bgra", 1, { 4 },         { 1 },         1, 1 },
39     [X264_CSP_RGB]  = { "rgb",  1, { 3 },         { 1 },         1, 1 },
40 };
41
42 int x264_cli_csp_is_invalid( int csp )
43 {
44     int csp_mask = csp & X264_CSP_MASK;
45     return csp_mask <= X264_CSP_NONE || csp_mask >= X264_CSP_CLI_MAX || csp & X264_CSP_OTHER;
46 }
47
48 int x264_cli_csp_depth_factor( int csp )
49 {
50     if( x264_cli_csp_is_invalid( csp ) )
51         return 0;
52     return (csp & X264_CSP_HIGH_DEPTH) ? 2 : 1;
53 }
54
55 uint64_t x264_cli_pic_plane_size( int csp, int width, int height, int plane )
56 {
57     int csp_mask = csp & X264_CSP_MASK;
58     if( x264_cli_csp_is_invalid( csp ) || plane < 0 || plane >= x264_cli_csps[csp_mask].planes )
59         return 0;
60     uint64_t size = (uint64_t)width * height;
61     size *= x264_cli_csps[csp_mask].width[plane] * x264_cli_csps[csp_mask].height[plane];
62     size *= x264_cli_csp_depth_factor( csp );
63     return size;
64 }
65
66 uint64_t x264_cli_pic_size( int csp, int width, int height )
67 {
68     if( x264_cli_csp_is_invalid( csp ) )
69         return 0;
70     uint64_t size = 0;
71     int csp_mask = csp & X264_CSP_MASK;
72     for( int i = 0; i < x264_cli_csps[csp_mask].planes; i++ )
73         size += x264_cli_pic_plane_size( csp, width, height, i );
74     return size;
75 }
76
77 int x264_cli_pic_alloc( cli_pic_t *pic, int csp, int width, int height )
78 {
79     memset( pic, 0, sizeof(cli_pic_t) );
80     int csp_mask = csp & X264_CSP_MASK;
81     if( x264_cli_csp_is_invalid( csp ) )
82         pic->img.planes = 0;
83     else
84         pic->img.planes = x264_cli_csps[csp_mask].planes;
85     pic->img.csp    = csp;
86     pic->img.width  = width;
87     pic->img.height = height;
88     for( int i = 0; i < pic->img.planes; i++ )
89     {
90          pic->img.plane[i] = x264_malloc( x264_cli_pic_plane_size( csp, width, height, i ) );
91          if( !pic->img.plane[i] )
92              return -1;
93          pic->img.stride[i] = width * x264_cli_csps[csp_mask].width[i] * x264_cli_csp_depth_factor( csp );
94     }
95
96     return 0;
97 }
98
99 void x264_cli_pic_clean( cli_pic_t *pic )
100 {
101     for( int i = 0; i < pic->img.planes; i++ )
102         x264_free( pic->img.plane[i] );
103     memset( pic, 0, sizeof(cli_pic_t) );
104 }
105
106 const x264_cli_csp_t *x264_cli_get_csp( int csp )
107 {
108     if( x264_cli_csp_is_invalid( csp ) )
109         return NULL;
110     return x264_cli_csps + (csp&X264_CSP_MASK);
111 }