]> git.sesse.net Git - x264/blob - input/input.c
Add video filtering system to x264cli
[x264] / input / input.c
1 /*****************************************************************************
2  * input.c: x264 input module common
3  *****************************************************************************
4  * Copyright (C) 2010 Steven Walters <kemuri9@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
19  *****************************************************************************/
20
21 #include "input.h"
22
23 const x264_cli_csp_t x264_cli_csps[] = {
24     [X264_CSP_I420] = { "i420", 3, { 1, .5, .5 }, { 1, .5, .5 }, 2, 2 },
25     [X264_CSP_I422] = { "i422", 3, { 1, .5, .5 }, { 1,  1,  1 }, 2, 1 },
26     [X264_CSP_I444] = { "i444", 3, { 1,  1,  1 }, { 1,  1,  1 }, 1, 1 },
27     [X264_CSP_YV12] = { "yv12", 3, { 1, .5, .5 }, { 1, .5, .5 }, 2, 2 },
28     [X264_CSP_BGR]  = { "bgr",  1, { 3 },         { 1 },         1, 1 },
29     [X264_CSP_BGRA] = { "bgra", 1, { 4 },         { 1 },         1, 1 }
30 };
31
32 int x264_cli_csp_is_invalid( int csp )
33 {
34     int csp_mask = csp & X264_CSP_MASK;
35     return csp_mask <= X264_CSP_NONE || csp_mask >= X264_CSP_CLI_MAX || csp & X264_CSP_OTHER;
36 }
37
38 uint64_t x264_cli_pic_plane_size( int csp, int width, int height, int plane )
39 {
40     int csp_mask = csp & X264_CSP_MASK;
41     if( x264_cli_csp_is_invalid( csp ) || plane < 0 || plane >= x264_cli_csps[csp_mask].planes )
42         return 0;
43     uint64_t size = (uint64_t)width * height;
44     size *= x264_cli_csps[csp_mask].width[plane] * x264_cli_csps[csp_mask].height[plane];
45     return size;
46 }
47
48 uint64_t x264_cli_pic_size( int csp, int width, int height )
49 {
50     if( x264_cli_csp_is_invalid( csp ) )
51         return 0;
52     uint64_t size = 0;
53     int csp_mask = csp & X264_CSP_MASK;
54     for( int i = 0; i < x264_cli_csps[csp_mask].planes; i++ )
55         size += x264_cli_pic_plane_size( csp, width, height, i );
56     return size;
57 }
58
59 int x264_cli_pic_alloc( cli_pic_t *pic, int csp, int width, int height )
60 {
61     memset( pic, 0, sizeof(cli_pic_t) );
62     int csp_mask = csp & X264_CSP_MASK;
63     if( x264_cli_csp_is_invalid( csp ) )
64         pic->img.planes = 0;
65     else
66         pic->img.planes = x264_cli_csps[csp_mask].planes;
67     pic->img.csp    = csp;
68     pic->img.width  = width;
69     pic->img.height = height;
70     for( int i = 0; i < pic->img.planes; i++ )
71     {
72          pic->img.plane[i] = x264_malloc( x264_cli_pic_plane_size( csp, width, height, i ) );
73          if( !pic->img.plane[i] )
74              return -1;
75          pic->img.stride[i] = width * x264_cli_csps[csp_mask].width[i];
76     }
77
78     return 0;
79 }
80
81 void x264_cli_pic_clean( cli_pic_t *pic )
82 {
83     for( int i = 0; i < pic->img.planes; i++ )
84         x264_free( pic->img.plane[i] );
85     memset( pic, 0, sizeof(cli_pic_t) );
86 }
87
88 const x264_cli_csp_t *x264_cli_get_csp( int csp )
89 {
90     if( x264_cli_csp_is_invalid( csp ) )
91         return NULL;
92     return x264_cli_csps + (csp&X264_CSP_MASK);
93 }