]> git.sesse.net Git - x264/blob - input/input.c
arm: Don't assume alignment in mbtree_propagate_list_internal where it isn't provided
[x264] / input / input.c
1 /*****************************************************************************
2  * input.c: common input functions
3  *****************************************************************************
4  * Copyright (C) 2010-2015 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_NV21] = { "nv21", 2, { 1,  1 },     { 1, .5 },     2, 2 },
37     [X264_CSP_NV16] = { "nv16", 2, { 1,  1 },     { 1,  1 },     2, 1 },
38     [X264_CSP_BGR]  = { "bgr",  1, { 3 },         { 1 },         1, 1 },
39     [X264_CSP_BGRA] = { "bgra", 1, { 4 },         { 1 },         1, 1 },
40     [X264_CSP_RGB]  = { "rgb",  1, { 3 },         { 1 },         1, 1 },
41 };
42
43 int x264_cli_csp_is_invalid( int csp )
44 {
45     int csp_mask = csp & X264_CSP_MASK;
46     return csp_mask <= X264_CSP_NONE || csp_mask >= X264_CSP_CLI_MAX ||
47            csp_mask == X264_CSP_V210 || csp & X264_CSP_OTHER;
48 }
49
50 int x264_cli_csp_depth_factor( int csp )
51 {
52     if( x264_cli_csp_is_invalid( csp ) )
53         return 0;
54     return (csp & X264_CSP_HIGH_DEPTH) ? 2 : 1;
55 }
56
57 uint64_t x264_cli_pic_plane_size( int csp, int width, int height, int plane )
58 {
59     int csp_mask = csp & X264_CSP_MASK;
60     if( x264_cli_csp_is_invalid( csp ) || plane < 0 || plane >= x264_cli_csps[csp_mask].planes )
61         return 0;
62     uint64_t size = (uint64_t)width * height;
63     size *= x264_cli_csps[csp_mask].width[plane] * x264_cli_csps[csp_mask].height[plane];
64     size *= x264_cli_csp_depth_factor( csp );
65     return size;
66 }
67
68 uint64_t x264_cli_pic_size( int csp, int width, int height )
69 {
70     if( x264_cli_csp_is_invalid( csp ) )
71         return 0;
72     uint64_t size = 0;
73     int csp_mask = csp & X264_CSP_MASK;
74     for( int i = 0; i < x264_cli_csps[csp_mask].planes; i++ )
75         size += x264_cli_pic_plane_size( csp, width, height, i );
76     return size;
77 }
78
79 static int x264_cli_pic_alloc_internal( cli_pic_t *pic, int csp, int width, int height, int align )
80 {
81     memset( pic, 0, sizeof(cli_pic_t) );
82     int csp_mask = csp & X264_CSP_MASK;
83     if( x264_cli_csp_is_invalid( csp ) )
84         pic->img.planes = 0;
85     else
86         pic->img.planes = x264_cli_csps[csp_mask].planes;
87     pic->img.csp    = csp;
88     pic->img.width  = width;
89     pic->img.height = height;
90     for( int i = 0; i < pic->img.planes; i++ )
91     {
92         int stride = width * x264_cli_csps[csp_mask].width[i];
93         stride *= x264_cli_csp_depth_factor( csp );
94         stride = ALIGN( stride, align );
95         uint64_t size = (uint64_t)(height * x264_cli_csps[csp_mask].height[i]) * stride;
96         pic->img.plane[i] = x264_malloc( size );
97         if( !pic->img.plane[i] )
98             return -1;
99         pic->img.stride[i] = stride;
100     }
101
102     return 0;
103 }
104
105 int x264_cli_pic_alloc( cli_pic_t *pic, int csp, int width, int height )
106 {
107     return x264_cli_pic_alloc_internal( pic, csp, width, height, 1 );
108 }
109
110 int x264_cli_pic_alloc_aligned( cli_pic_t *pic, int csp, int width, int height )
111 {
112     return x264_cli_pic_alloc_internal( pic, csp, width, height, NATIVE_ALIGN );
113 }
114
115 void x264_cli_pic_clean( cli_pic_t *pic )
116 {
117     for( int i = 0; i < pic->img.planes; i++ )
118         x264_free( pic->img.plane[i] );
119     memset( pic, 0, sizeof(cli_pic_t) );
120 }
121
122 const x264_cli_csp_t *x264_cli_get_csp( int csp )
123 {
124     if( x264_cli_csp_is_invalid( csp ) )
125         return NULL;
126     return x264_cli_csps + (csp&X264_CSP_MASK);
127 }