demux: mp4: avoid audio cuts on seek
[vlc.git] / modules / video_chroma / yuvp.c
blobea702f5cc8feb471bf92da9613c443291d68bb52
1 /*****************************************************************************
2 * yuvp.c: YUVP to YUVA/RGBA chroma converter
3 *****************************************************************************
4 * Copyright (C) 2008 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Laurent Aimar < fenrir @ videolan.org >
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_filter.h>
34 #include <vlc_picture.h>
35 #include <assert.h>
37 /* TODO:
38 * Add anti-aliasing support (specially for DVD where only 4 colors are used)
41 /*****************************************************************************
42 * Module descriptor
43 *****************************************************************************/
44 static int Open ( vlc_object_t * );
45 static void Close( vlc_object_t * );
47 vlc_module_begin ()
48 set_description( N_("YUVP converter") )
49 set_capability( "video converter", 10 )
50 set_callbacks( Open, Close )
51 vlc_module_end ()
53 /****************************************************************************
54 * Local prototypes
55 ****************************************************************************/
56 static picture_t *Filter( filter_t *, picture_t * );
57 static void Yuv2Rgb( uint8_t *r, uint8_t *g, uint8_t *b, int y1, int u1, int v1 );
59 /*****************************************************************************
60 * Open: probe the filter and return score
61 *****************************************************************************/
62 static int Open( vlc_object_t *p_this )
64 filter_t *p_filter = (filter_t*)p_this;
66 /* It only supports YUVP to YUVA/RGBA without scaling
67 * (if scaling is required another filter can do it) */
68 if( p_filter->fmt_in.video.i_chroma != VLC_CODEC_YUVP ||
69 ( p_filter->fmt_out.video.i_chroma != VLC_CODEC_YUVA &&
70 p_filter->fmt_out.video.i_chroma != VLC_CODEC_RGBA &&
71 p_filter->fmt_out.video.i_chroma != VLC_CODEC_ARGB ) ||
72 p_filter->fmt_in.video.i_width != p_filter->fmt_out.video.i_width ||
73 p_filter->fmt_in.video.i_height != p_filter->fmt_out.video.i_height ||
74 p_filter->fmt_in.video.orientation != p_filter->fmt_out.video.orientation )
76 return VLC_EGENERIC;
79 p_filter->pf_video_filter = Filter;
81 msg_Dbg( p_filter, "YUVP to %4.4s converter",
82 (const char*)&p_filter->fmt_out.video.i_chroma );
84 return VLC_SUCCESS;
87 /*****************************************************************************
88 * Close: clean up the filter
89 *****************************************************************************/
90 static void Close( vlc_object_t *p_this )
92 VLC_UNUSED(p_this );
95 /****************************************************************************
96 * Filter: the whole thing
97 ****************************************************************************/
98 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
100 picture_t *p_out;
102 if( !p_pic )
103 return NULL;
105 const video_palette_t *p_yuvp = p_filter->fmt_in.video.p_palette;
107 assert( p_yuvp != NULL );
108 assert( p_filter->fmt_in.video.i_chroma == VLC_CODEC_YUVP );
109 assert( p_filter->fmt_in.video.i_width == p_filter->fmt_out.video.i_width );
110 assert( p_filter->fmt_in.video.i_height == p_filter->fmt_out.video.i_height );
112 /* Request output picture */
113 p_out = filter_NewPicture( p_filter );
114 if( !p_out )
116 picture_Release( p_pic );
117 return NULL;
120 if( p_filter->fmt_out.video.i_chroma == VLC_CODEC_YUVA )
122 for( unsigned int y = 0; y < p_filter->fmt_in.video.i_height; y++ )
124 const uint8_t *p_line = &p_pic->p->p_pixels[y*p_pic->p->i_pitch];
125 uint8_t *p_y = &p_out->Y_PIXELS[y*p_out->Y_PITCH];
126 uint8_t *p_u = &p_out->U_PIXELS[y*p_out->U_PITCH];
127 uint8_t *p_v = &p_out->V_PIXELS[y*p_out->V_PITCH];
128 uint8_t *p_a = &p_out->A_PIXELS[y*p_out->A_PITCH];
130 for( unsigned int x = 0; x < p_filter->fmt_in.video.i_width; x++ )
132 const int v = p_line[x];
134 if( v > p_yuvp->i_entries ) /* maybe assert ? */
135 continue;
137 p_y[x] = p_yuvp->palette[v][0];
138 p_u[x] = p_yuvp->palette[v][1];
139 p_v[x] = p_yuvp->palette[v][2];
140 p_a[x] = p_yuvp->palette[v][3];
144 else
146 video_palette_t rgbp;
148 assert( p_filter->fmt_out.video.i_chroma == VLC_CODEC_ARGB ||
149 p_filter->fmt_out.video.i_chroma == VLC_CODEC_RGBA );
150 /* Create a RGBA palette */
151 rgbp.i_entries = p_yuvp->i_entries;
152 const uint8_t r = p_filter->fmt_out.video.i_chroma == VLC_CODEC_ARGB ? 1 : 0;
153 const uint8_t g = p_filter->fmt_out.video.i_chroma == VLC_CODEC_ARGB ? 2 : 1;
154 const uint8_t b = p_filter->fmt_out.video.i_chroma == VLC_CODEC_ARGB ? 3 : 2;
155 const uint8_t a = p_filter->fmt_out.video.i_chroma == VLC_CODEC_ARGB ? 0 : 3;
156 for( int i = 0; i < p_yuvp->i_entries; i++ )
158 if( p_yuvp->palette[i][3] == 0 )
160 memset( rgbp.palette[i], 0, sizeof( rgbp.palette[i] ) );
161 continue;
163 Yuv2Rgb( &rgbp.palette[i][r], &rgbp.palette[i][g], &rgbp.palette[i][b],
164 p_yuvp->palette[i][0], p_yuvp->palette[i][1], p_yuvp->palette[i][2] );
165 rgbp.palette[i][a] = p_yuvp->palette[i][3];
168 for( unsigned int y = 0; y < p_filter->fmt_in.video.i_height; y++ )
170 const uint8_t *p_line = &p_pic->p->p_pixels[y*p_pic->p->i_pitch];
171 uint8_t *p_pixels = &p_out->p->p_pixels[y*p_out->p->i_pitch];
173 for( unsigned int x = 0; x < p_filter->fmt_in.video.i_width; x++ )
175 const int v = p_line[x];
177 if( v >= rgbp.i_entries ) /* maybe assert ? */
178 continue;
180 p_pixels[4*x+0] = rgbp.palette[v][0];
181 p_pixels[4*x+1] = rgbp.palette[v][1];
182 p_pixels[4*x+2] = rgbp.palette[v][2];
183 p_pixels[4*x+3] = rgbp.palette[v][3];
189 picture_CopyProperties( p_out, p_pic );
190 picture_Release( p_pic );
191 return p_out;
194 /* FIXME copied from blend.c */
195 static inline uint8_t vlc_uint8( int v )
197 if( v > 255 )
198 return 255;
199 else if( v < 0 )
200 return 0;
201 return v;
203 static void Yuv2Rgb( uint8_t *r, uint8_t *g, uint8_t *b, int y1, int u1, int v1 )
205 /* macros used for YUV pixel conversions */
206 # define SCALEBITS 10
207 # define ONE_HALF (1 << (SCALEBITS - 1))
208 # define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
210 int y, cb, cr, r_add, g_add, b_add;
212 cb = u1 - 128;
213 cr = v1 - 128;
214 r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;
215 g_add = - FIX(0.34414*255.0/224.0) * cb
216 - FIX(0.71414*255.0/224.0) * cr + ONE_HALF;
217 b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;
218 y = (y1 - 16) * FIX(255.0/219.0);
219 *r = vlc_uint8( (y + r_add) >> SCALEBITS );
220 *g = vlc_uint8( (y + g_add) >> SCALEBITS );
221 *b = vlc_uint8( (y + b_add) >> SCALEBITS );
222 #undef FIX