]> git.sesse.net Git - vlc/blob - modules/video_filter/yuvp.c
mediacodec: don't loop in GetOutput
[vlc] / modules / video_filter / yuvp.c
1 /*****************************************************************************
2  * yuvp.c: YUVP to YUVA/RGBA chroma converter
3  *****************************************************************************
4  * Copyright (C) 2008 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar < fenrir @ videolan.org >
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_filter.h>
34 #include <assert.h>
35
36 /* TODO:
37  *  Add anti-aliasing support (specially for DVD where only 4 colors are used)
38  */
39
40 /*****************************************************************************
41  * Module descriptor
42  *****************************************************************************/
43 static int  Open ( vlc_object_t * );
44 static void Close( vlc_object_t * );
45
46 vlc_module_begin ()
47     set_description( N_("YUVP converter") )
48     set_capability( "video filter2", 10 )
49     set_callbacks( Open, Close )
50 vlc_module_end ()
51
52 /****************************************************************************
53  * Local prototypes
54  ****************************************************************************/
55 static picture_t *Filter( filter_t *, picture_t * );
56 static void Yuv2Rgb( uint8_t *r, uint8_t *g, uint8_t *b, int y1, int u1, int v1 );
57
58 /*****************************************************************************
59  * Open: probe the filter and return score
60  *****************************************************************************/
61 static int Open( vlc_object_t *p_this )
62 {
63     filter_t *p_filter = (filter_t*)p_this;
64
65     /* It only supports YUVP to YUVA/RGBA without scaling
66      * (if scaling is required another filter can do it) */
67     if( p_filter->fmt_in.video.i_chroma != VLC_CODEC_YUVP ||
68         ( p_filter->fmt_out.video.i_chroma != VLC_CODEC_YUVA &&
69           p_filter->fmt_out.video.i_chroma != VLC_CODEC_RGBA &&
70           p_filter->fmt_out.video.i_chroma != VLC_CODEC_ARGB ) ||
71         p_filter->fmt_in.video.i_width  != p_filter->fmt_out.video.i_width ||
72         p_filter->fmt_in.video.i_height != p_filter->fmt_out.video.i_height ||
73         p_filter->fmt_in.video.orientation != p_filter->fmt_out.video.orientation )
74     {
75         return VLC_EGENERIC;
76     }
77
78     p_filter->pf_video_filter = Filter;
79
80     msg_Dbg( p_filter, "YUVP to %4.4s converter",
81              (const char*)&p_filter->fmt_out.video.i_chroma );
82
83     return VLC_SUCCESS;
84 }
85
86 /*****************************************************************************
87  * Close: clean up the filter
88  *****************************************************************************/
89 static void Close( vlc_object_t *p_this )
90 {
91     VLC_UNUSED(p_this );
92 }
93
94 /****************************************************************************
95  * Filter: the whole thing
96  ****************************************************************************/
97 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
98 {
99     picture_t *p_out;
100
101     if( !p_pic )
102         return NULL;
103
104     const video_palette_t *p_yuvp = p_filter->fmt_in.video.p_palette;
105
106     assert( p_yuvp != NULL );
107     assert( p_filter->fmt_in.video.i_chroma == VLC_CODEC_YUVP );
108     assert( p_filter->fmt_in.video.i_width == p_filter->fmt_out.video.i_width );
109     assert( p_filter->fmt_in.video.i_height == p_filter->fmt_out.video.i_height );
110
111     /* Request output picture */
112     p_out = filter_NewPicture( p_filter );
113     if( !p_out )
114     {
115         picture_Release( p_pic );
116         return NULL;
117     }
118
119     if( p_filter->fmt_out.video.i_chroma == VLC_CODEC_YUVA )
120     {
121         for( unsigned int y = 0; y < p_filter->fmt_in.video.i_height; y++ )
122         {
123             const uint8_t *p_line = &p_pic->p->p_pixels[y*p_pic->p->i_pitch];
124             uint8_t *p_y = &p_out->Y_PIXELS[y*p_out->Y_PITCH];
125             uint8_t *p_u = &p_out->U_PIXELS[y*p_out->U_PITCH];
126             uint8_t *p_v = &p_out->V_PIXELS[y*p_out->V_PITCH];
127             uint8_t *p_a = &p_out->A_PIXELS[y*p_out->A_PITCH];
128
129             for( unsigned int x = 0; x < p_filter->fmt_in.video.i_width; x++ )
130             {
131                 const int v = p_line[x];
132
133                 if( v > p_yuvp->i_entries )  /* maybe assert ? */
134                     continue;
135
136                 p_y[x] = p_yuvp->palette[v][0];
137                 p_u[x] = p_yuvp->palette[v][1];
138                 p_v[x] = p_yuvp->palette[v][2];
139                 p_a[x] = p_yuvp->palette[v][3];
140             }
141         }
142     }
143     else if( p_filter->fmt_out.video.i_chroma == VLC_CODEC_RGBA )
144     {
145         video_palette_t rgbp;
146
147         /* Create a RGBA palette */
148         rgbp.i_entries = p_yuvp->i_entries;
149         for( int i = 0; i < p_yuvp->i_entries; i++ )
150         {
151             Yuv2Rgb( &rgbp.palette[i][0], &rgbp.palette[i][1], &rgbp.palette[i][2],
152                      p_yuvp->palette[i][0], p_yuvp->palette[i][1], p_yuvp->palette[i][2] );
153             rgbp.palette[i][3] = p_yuvp->palette[i][3];
154         }
155
156         for( unsigned int y = 0; y < p_filter->fmt_in.video.i_height; y++ )
157         {
158             const uint8_t *p_line = &p_pic->p->p_pixels[y*p_pic->p->i_pitch];
159             uint8_t *p_rgba = &p_out->p->p_pixels[y*p_out->p->i_pitch];
160
161             for( unsigned int x = 0; x < p_filter->fmt_in.video.i_width; x++ )
162             {
163                 const int v = p_line[x];
164
165                 if( v >= rgbp.i_entries )  /* maybe assert ? */
166                     continue;
167
168                 p_rgba[4*x+0] = rgbp.palette[v][0];
169                 p_rgba[4*x+1] = rgbp.palette[v][1];
170                 p_rgba[4*x+2] = rgbp.palette[v][2];
171                 p_rgba[4*x+3] = rgbp.palette[v][3];
172             }
173         }
174     }
175     else
176     {
177         video_palette_t rgbp;
178
179         assert( p_filter->fmt_out.video.i_chroma == VLC_CODEC_ARGB );
180         /* Create a RGBA palette */
181         rgbp.i_entries = p_yuvp->i_entries;
182         for( int i = 0; i < p_yuvp->i_entries; i++ )
183         {
184             Yuv2Rgb( &rgbp.palette[i][1], &rgbp.palette[i][2], &rgbp.palette[i][3],
185                      p_yuvp->palette[i][0], p_yuvp->palette[i][1], p_yuvp->palette[i][2] );
186             rgbp.palette[i][0] = p_yuvp->palette[i][3];
187         }
188
189         for( unsigned int y = 0; y < p_filter->fmt_in.video.i_height; y++ )
190         {
191             const uint8_t *p_line = &p_pic->p->p_pixels[y*p_pic->p->i_pitch];
192             uint8_t *p_rgba = &p_out->p->p_pixels[y*p_out->p->i_pitch];
193
194             for( unsigned int x = 0; x < p_filter->fmt_in.video.i_width; x++ )
195             {
196                 const int v = p_line[x];
197
198                 if( v >= rgbp.i_entries )  /* maybe assert ? */
199                     continue;
200
201                 p_rgba[4*x+0] = rgbp.palette[v][0];
202                 p_rgba[4*x+1] = rgbp.palette[v][1];
203                 p_rgba[4*x+2] = rgbp.palette[v][2];
204                 p_rgba[4*x+3] = rgbp.palette[v][3];
205             }
206         }
207
208     }
209
210     picture_CopyProperties( p_out, p_pic );
211     picture_Release( p_pic );
212     return p_out;
213 }
214
215 /* FIXME copied from blend.c */
216 static inline uint8_t vlc_uint8( int v )
217 {
218     if( v > 255 )
219         return 255;
220     else if( v < 0 )
221         return 0;
222     return v;
223 }
224 static void Yuv2Rgb( uint8_t *r, uint8_t *g, uint8_t *b, int y1, int u1, int v1 )
225 {
226     /* macros used for YUV pixel conversions */
227 #   define SCALEBITS 10
228 #   define ONE_HALF  (1 << (SCALEBITS - 1))
229 #   define FIX(x)    ((int) ((x) * (1<<SCALEBITS) + 0.5))
230
231     int y, cb, cr, r_add, g_add, b_add;
232
233     cb = u1 - 128;
234     cr = v1 - 128;
235     r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;
236     g_add = - FIX(0.34414*255.0/224.0) * cb
237             - FIX(0.71414*255.0/224.0) * cr + ONE_HALF;
238     b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;
239     y = (y1 - 16) * FIX(255.0/219.0);
240     *r = vlc_uint8( (y + r_add) >> SCALEBITS );
241     *g = vlc_uint8( (y + g_add) >> SCALEBITS );
242     *b = vlc_uint8( (y + b_add) >> SCALEBITS );
243 #undef FIX
244 }