1 /*****************************************************************************
2 * blend.c: alpha blend 2 pictures together
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
7 * Author: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 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 General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
28 #include <vlc/decoder.h>
29 #include "vlc_filter.h"
31 /*****************************************************************************
32 * filter_sys_t : filter descriptor
33 *****************************************************************************/
39 /****************************************************************************
41 ****************************************************************************/
42 static int OpenFilter ( vlc_object_t * );
43 static void CloseFilter( vlc_object_t * );
45 /* TODO i_alpha support for BlendR16 */
46 static void Blend( filter_t *, picture_t *, picture_t *, picture_t *,
48 static void BlendI420( filter_t *, picture_t *, picture_t *, picture_t *,
49 int, int, int, int, int );
50 static void BlendR16( filter_t *, picture_t *, picture_t *, picture_t *,
51 int, int, int, int, int );
52 static void BlendR24( filter_t *, picture_t *, picture_t *, picture_t *,
53 int, int, int, int, int );
54 static void BlendYUY2( filter_t *, picture_t *, picture_t *, picture_t *,
55 int, int, int, int, int );
56 static void BlendPalI420( filter_t *, picture_t *, picture_t *, picture_t *,
57 int, int, int, int, int );
58 static void BlendPalYUY2( filter_t *, picture_t *, picture_t *, picture_t *,
59 int, int, int, int, int );
60 static void BlendPalRV( filter_t *, picture_t *, picture_t *, picture_t *,
61 int, int, int, int, int );
63 /*****************************************************************************
65 *****************************************************************************/
67 set_description( _("Video pictures blending") );
68 set_category( CAT_VIDEO );
69 set_subcategory( SUBCAT_VIDEO_VFILTER );
70 set_capability( "video blending", 100 );
71 set_callbacks( OpenFilter, CloseFilter );
74 /*****************************************************************************
75 * OpenFilter: probe the filter and return score
76 *****************************************************************************/
77 static int OpenFilter( vlc_object_t *p_this )
79 filter_t *p_filter = (filter_t*)p_this;
82 /* Check if we can handle that format.
83 * We could try to use a chroma filter if we can't. */
84 if( ( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','A') &&
85 p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','P') ) ||
86 ( p_filter->fmt_out.video.i_chroma != VLC_FOURCC('I','4','2','0') &&
87 p_filter->fmt_out.video.i_chroma != VLC_FOURCC('Y','U','Y','2') &&
88 p_filter->fmt_out.video.i_chroma != VLC_FOURCC('Y','V','1','2') &&
89 p_filter->fmt_out.video.i_chroma != VLC_FOURCC('R','V','1','6') &&
90 p_filter->fmt_out.video.i_chroma != VLC_FOURCC('R','V','2','4') &&
91 p_filter->fmt_out.video.i_chroma != VLC_FOURCC('R','V','3','2') ) )
96 /* Allocate the memory needed to store the decoder's structure */
97 if( ( p_filter->p_sys = p_sys =
98 (filter_sys_t *)malloc(sizeof(filter_sys_t)) ) == NULL )
100 msg_Err( p_filter, "out of memory" );
105 p_filter->pf_video_blend = Blend;
107 msg_Dbg( p_filter, "chroma: %4.4s -> %4.4s",
108 (char *)&p_filter->fmt_in.video.i_chroma,
109 (char *)&p_filter->fmt_out.video.i_chroma );
115 /****************************************************************************
116 * Blend: the whole thing
117 ****************************************************************************
118 * This function is called just after the thread is launched.
119 ****************************************************************************/
120 static void Blend( filter_t *p_filter, picture_t *p_dst,
121 picture_t *p_dst_orig, picture_t *p_src,
122 int i_x_offset, int i_y_offset, int i_alpha )
124 int i_width, i_height;
126 i_width = __MIN((int)p_filter->fmt_out.video.i_visible_width - i_x_offset,
127 (int)p_filter->fmt_in.video.i_visible_width);
129 i_height = __MIN((int)p_filter->fmt_out.video.i_visible_height -i_y_offset,
130 (int)p_filter->fmt_in.video.i_visible_height);
132 if( i_width <= 0 || i_height <= 0 ) return;
134 if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','A') &&
135 ( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('I','4','2','0') ||
136 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','V','1','2') ) )
138 BlendI420( p_filter, p_dst, p_dst_orig, p_src,
139 i_x_offset, i_y_offset, i_width, i_height, i_alpha );
142 if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','A') &&
143 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','U','Y','2') )
145 BlendYUY2( p_filter, p_dst, p_dst_orig, p_src,
146 i_x_offset, i_y_offset, i_width, i_height, i_alpha );
149 if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','A') &&
150 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','1','6') )
152 BlendR16( p_filter, p_dst, p_dst_orig, p_src,
153 i_x_offset, i_y_offset, i_width, i_height, i_alpha );
156 if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','A') &&
157 ( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','2','4') ||
158 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','3','2') ) )
160 BlendR24( p_filter, p_dst, p_dst_orig, p_src,
161 i_x_offset, i_y_offset, i_width, i_height, i_alpha );
164 if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','P') &&
165 ( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('I','4','2','0') ||
166 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','V','1','2') ) )
168 BlendPalI420( p_filter, p_dst, p_dst_orig, p_src,
169 i_x_offset, i_y_offset, i_width, i_height, i_alpha );
172 if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','P') &&
173 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','U','Y','2') )
175 BlendPalYUY2( p_filter, p_dst, p_dst_orig, p_src,
176 i_x_offset, i_y_offset, i_width, i_height, i_alpha );
179 if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','P') &&
180 ( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','1','6') ||
181 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','2','4') ||
182 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','3','2') ) )
184 BlendPalRV( p_filter, p_dst, p_dst_orig, p_src,
185 i_x_offset, i_y_offset, i_width, i_height, i_alpha );
189 msg_Dbg( p_filter, "no matching alpha blending routine" );
192 static void BlendI420( filter_t *p_filter, picture_t *p_dst,
193 picture_t *p_dst_orig, picture_t *p_src,
194 int i_x_offset, int i_y_offset,
195 int i_width, int i_height, int i_alpha )
197 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
198 uint8_t *p_src1_y, *p_src2_y, *p_dst_y;
199 uint8_t *p_src1_u, *p_src2_u, *p_dst_u;
200 uint8_t *p_src1_v, *p_src2_v, *p_dst_v;
202 int i_x, i_y, i_trans;
203 vlc_bool_t b_even_scanline = i_y_offset % 2;
205 i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
206 p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
207 p_filter->fmt_out.video.i_x_offset +
208 p_dst->p[Y_PLANE].i_pitch *
209 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
210 p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
211 p_filter->fmt_out.video.i_x_offset/2 +
212 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
213 p_dst->p[U_PLANE].i_pitch;
214 p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
215 p_filter->fmt_out.video.i_x_offset/2 +
216 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
217 p_dst->p[V_PLANE].i_pitch;
219 i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
220 p_src1_y = p_dst_orig->p[Y_PLANE].p_pixels + i_x_offset +
221 p_filter->fmt_out.video.i_x_offset +
222 p_dst_orig->p[Y_PLANE].i_pitch *
223 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
224 p_src1_u = p_dst_orig->p[U_PLANE].p_pixels + i_x_offset/2 +
225 p_filter->fmt_out.video.i_x_offset/2 +
226 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
227 p_dst_orig->p[U_PLANE].i_pitch;
228 p_src1_v = p_dst_orig->p[V_PLANE].p_pixels + i_x_offset/2 +
229 p_filter->fmt_out.video.i_x_offset/2 +
230 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
231 p_dst_orig->p[V_PLANE].i_pitch;
233 i_src2_pitch = p_src->p[Y_PLANE].i_pitch;
234 p_src2_y = p_src->p[Y_PLANE].p_pixels +
235 p_filter->fmt_in.video.i_x_offset +
236 p_src->p[Y_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
237 p_src2_u = p_src->p[U_PLANE].p_pixels +
238 p_filter->fmt_in.video.i_x_offset/2 +
239 p_src->p[U_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
240 p_src2_v = p_src->p[V_PLANE].p_pixels +
241 p_filter->fmt_in.video.i_x_offset/2 +
242 p_src->p[V_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
244 p_trans = p_src->p[A_PLANE].p_pixels +
245 p_filter->fmt_in.video.i_x_offset +
246 p_src->p[A_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
248 #define MAX_TRANS 255
251 /* Draw until we reach the bottom of the subtitle */
252 for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
253 p_dst_y += i_dst_pitch, p_src1_y += i_src1_pitch,
254 p_src2_y += i_src2_pitch,
255 p_dst_u += b_even_scanline ? i_dst_pitch/2 : 0,
256 p_src1_u += b_even_scanline ? i_src1_pitch/2 : 0,
257 p_src2_u += i_src2_pitch,
258 p_dst_v += b_even_scanline ? i_dst_pitch/2 : 0,
259 p_src1_v += b_even_scanline ? i_src1_pitch/2 : 0,
260 p_src2_v += i_src2_pitch )
262 b_even_scanline = !b_even_scanline;
264 /* Draw until we reach the end of the line */
265 for( i_x = 0; i_x < i_width; i_x++ )
267 i_trans = ( p_trans[i_x] * i_alpha ) / 255;
270 /* Completely transparent. Don't change pixel */
273 else if( i_trans == MAX_TRANS )
275 /* Completely opaque. Completely overwrite underlying pixel */
276 p_dst_y[i_x] = p_src2_y[i_x];
278 if( b_even_scanline && i_x % 2 == 0 )
280 p_dst_u[i_x/2] = p_src2_u[i_x];
281 p_dst_v[i_x/2] = p_src2_v[i_x];
287 p_dst_y[i_x] = ( (uint16_t)p_src2_y[i_x] * i_trans +
288 (uint16_t)p_src1_y[i_x] * (MAX_TRANS - i_trans) )
291 if( b_even_scanline && i_x % 2 == 0 )
293 p_dst_u[i_x/2] = ( (uint16_t)p_src2_u[i_x] * i_trans +
294 (uint16_t)p_src1_u[i_x/2] * (MAX_TRANS - i_trans) )
296 p_dst_v[i_x/2] = ( (uint16_t)p_src2_v[i_x] * i_trans +
297 (uint16_t)p_src1_v[i_x/2] * (MAX_TRANS - i_trans) )
309 static inline void yuv_to_rgb( int *r, int *g, int *b,
310 uint8_t y1, uint8_t u1, uint8_t v1 )
312 /* macros used for YUV pixel conversions */
313 # define SCALEBITS 10
314 # define ONE_HALF (1 << (SCALEBITS - 1))
315 # define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
316 # define CLAMP( x ) (((x) > 255) ? 255 : ((x) < 0) ? 0 : (x));
318 int y, cb, cr, r_add, g_add, b_add;
322 r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;
323 g_add = - FIX(0.34414*255.0/224.0) * cb
324 - FIX(0.71414*255.0/224.0) * cr + ONE_HALF;
325 b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;
326 y = (y1 - 16) * FIX(255.0/219.0);
327 *r = CLAMP((y + r_add) >> SCALEBITS);
328 *g = CLAMP((y + g_add) >> SCALEBITS);
329 *b = CLAMP((y + b_add) >> SCALEBITS);
332 static void BlendR16( filter_t *p_filter, picture_t *p_dst_pic,
333 picture_t *p_dst_orig, picture_t *p_src,
334 int i_x_offset, int i_y_offset,
335 int i_width, int i_height, int i_alpha )
337 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
338 uint8_t *p_dst, *p_src1, *p_src2_y;
339 uint8_t *p_src2_u, *p_src2_v;
341 int i_x, i_y, i_pix_pitch;
344 i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
345 i_dst_pitch = p_dst_pic->p->i_pitch;
346 p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
347 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
348 p_dst_pic->p->i_pitch *
349 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
351 i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
352 p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
353 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
354 p_dst_orig->p->i_pitch *
355 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
357 i_src2_pitch = p_src->p[Y_PLANE].i_pitch;
358 p_src2_y = p_src->p[Y_PLANE].p_pixels +
359 p_filter->fmt_in.video.i_x_offset +
360 p_src->p[Y_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
361 p_src2_u = p_src->p[U_PLANE].p_pixels +
362 p_filter->fmt_in.video.i_x_offset/2 +
363 p_src->p[U_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
364 p_src2_v = p_src->p[V_PLANE].p_pixels +
365 p_filter->fmt_in.video.i_x_offset/2 +
366 p_src->p[V_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
368 p_trans = p_src->p[A_PLANE].p_pixels +
369 p_filter->fmt_in.video.i_x_offset +
370 p_src->p[A_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
372 #define MAX_TRANS 255
375 /* Draw until we reach the bottom of the subtitle */
376 for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
377 p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
378 p_src2_y += i_src2_pitch, p_src2_u += i_src2_pitch,
379 p_src2_v += i_src2_pitch )
381 /* Draw until we reach the end of the line */
382 for( i_x = 0; i_x < i_width; i_x++ )
386 /* Completely transparent. Don't change pixel */
389 else if( p_trans[i_x] == MAX_TRANS )
391 /* Completely opaque. Completely overwrite underlying pixel */
392 yuv_to_rgb( &r, &g, &b,
393 p_src2_y[i_x], p_src2_u[i_x], p_src2_v[i_x] );
395 ((uint16_t *)(&p_dst[i_x * i_pix_pitch]))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
400 yuv_to_rgb( &r, &g, &b,
401 p_src2_y[i_x], p_src2_u[i_x], p_src2_v[i_x] );
403 ((uint16_t *)(&p_dst[i_x * i_pix_pitch]))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
413 static void BlendR24( filter_t *p_filter, picture_t *p_dst_pic,
414 picture_t *p_dst_orig, picture_t *p_src,
415 int i_x_offset, int i_y_offset,
416 int i_width, int i_height, int i_alpha )
418 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
419 uint8_t *p_dst, *p_src1, *p_src2_y;
420 uint8_t *p_src2_u, *p_src2_v;
422 int i_x, i_y, i_pix_pitch, i_trans;
425 i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
426 i_dst_pitch = p_dst_pic->p->i_pitch;
427 p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
428 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
429 p_dst_pic->p->i_pitch *
430 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
432 i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
433 p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
434 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
435 p_dst_orig->p->i_pitch *
436 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
438 i_src2_pitch = p_src->p[Y_PLANE].i_pitch;
439 p_src2_y = p_src->p[Y_PLANE].p_pixels +
440 p_filter->fmt_in.video.i_x_offset +
441 p_src->p[Y_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
442 p_src2_u = p_src->p[U_PLANE].p_pixels +
443 p_filter->fmt_in.video.i_x_offset/2 +
444 p_src->p[U_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
445 p_src2_v = p_src->p[V_PLANE].p_pixels +
446 p_filter->fmt_in.video.i_x_offset/2 +
447 p_src->p[V_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
449 p_trans = p_src->p[A_PLANE].p_pixels +
450 p_filter->fmt_in.video.i_x_offset +
451 p_src->p[A_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
453 #define MAX_TRANS 255
456 /* Draw until we reach the bottom of the subtitle */
457 for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
458 p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
459 p_src2_y += i_src2_pitch, p_src2_u += i_src2_pitch,
460 p_src2_v += i_src2_pitch )
462 /* Draw until we reach the end of the line */
463 for( i_x = 0; i_x < i_width; i_x++ )
465 i_trans = ( p_trans[i_x] * i_alpha ) / 255;
468 /* Completely transparent. Don't change pixel */
471 else if( i_trans == MAX_TRANS )
473 /* Completely opaque. Completely overwrite underlying pixel */
474 yuv_to_rgb( &r, &g, &b,
475 p_src2_y[i_x], p_src2_u[i_x], p_src2_v[i_x] );
477 p_dst[i_x * i_pix_pitch] = r;
478 p_dst[i_x * i_pix_pitch + 1] = g;
479 p_dst[i_x * i_pix_pitch + 2] = b;
484 yuv_to_rgb( &r, &g, &b,
485 p_src2_y[i_x], p_src2_u[i_x], p_src2_v[i_x] );
487 p_dst[i_x * i_pix_pitch] = ( r * i_trans +
488 (uint16_t)p_src1[i_x * i_pix_pitch] *
489 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
490 p_dst[i_x * i_pix_pitch + 1] = ( g * i_trans +
491 (uint16_t)p_src1[i_x * i_pix_pitch + 1] *
492 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
493 p_dst[i_x * i_pix_pitch + 2] = ( b * i_trans +
494 (uint16_t)p_src1[i_x * i_pix_pitch + 2] *
495 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
505 static void BlendYUY2( filter_t *p_filter, picture_t *p_dst_pic,
506 picture_t *p_dst_orig, picture_t *p_src,
507 int i_x_offset, int i_y_offset,
508 int i_width, int i_height, int i_alpha )
510 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
511 uint8_t *p_dst, *p_src1, *p_src2_y;
512 uint8_t *p_src2_u, *p_src2_v;
514 int i_x, i_y, i_pix_pitch, i_trans;
517 i_dst_pitch = p_dst_pic->p->i_pitch;
518 p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
519 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
520 p_dst_pic->p->i_pitch *
521 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
523 i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
524 p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
525 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
526 p_dst_orig->p->i_pitch *
527 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
529 i_src2_pitch = p_src->p[Y_PLANE].i_pitch;
530 p_src2_y = p_src->p[Y_PLANE].p_pixels +
531 p_filter->fmt_in.video.i_x_offset +
532 p_src->p[Y_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
533 p_src2_u = p_src->p[U_PLANE].p_pixels +
534 p_filter->fmt_in.video.i_x_offset/2 +
535 p_src->p[U_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
536 p_src2_v = p_src->p[V_PLANE].p_pixels +
537 p_filter->fmt_in.video.i_x_offset/2 +
538 p_src->p[V_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
540 p_trans = p_src->p[A_PLANE].p_pixels +
541 p_filter->fmt_in.video.i_x_offset +
542 p_src->p[A_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
544 #define MAX_TRANS 255
547 /* Draw until we reach the bottom of the subtitle */
548 for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
549 p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
550 p_src2_y += i_src2_pitch, p_src2_u += i_src2_pitch,
551 p_src2_v += i_src2_pitch )
553 /* Draw until we reach the end of the line */
554 for( i_x = 0; i_x < i_width; i_x += 2 )
556 i_trans = ( p_trans[i_x] * i_alpha ) / 255;
559 /* Completely transparent. Don't change pixel */
561 else if( i_trans == MAX_TRANS )
563 /* Completely opaque. Completely overwrite underlying pixel */
564 p_dst[i_x * 2] = p_src2_y[i_x];
565 p_dst[i_x * 2 + 1] = p_src2_u[i_x];
566 p_dst[i_x * 2 + 3] = p_src2_v[i_x];
571 p_dst[i_x * 2] = ( (uint16_t)p_src2_y[i_x] * i_trans +
572 (uint16_t)p_src1[i_x * 2] * (MAX_TRANS - i_trans) )
574 p_dst[i_x * 2 + 1] = ( (uint16_t)p_src2_u[i_x] * i_trans +
575 (uint16_t)p_src1[i_x * 2 + 1] * (MAX_TRANS - i_trans) )
577 p_dst[i_x * 2 + 3] = ( (uint16_t)p_src2_v[i_x] * i_trans +
578 (uint16_t)p_src1[i_x * 2 + 3] * (MAX_TRANS - i_trans) )
582 i_trans = ( p_trans[i_x+1] * i_alpha ) / 255;
585 /* Completely transparent. Don't change pixel */
587 else if( i_trans == MAX_TRANS )
589 /* Completely opaque. Completely overwrite underlying pixel */
590 p_dst[i_x * 2 + 2] = p_src2_y[i_x + 1];
595 p_dst[i_x * 2 + 2] = ( (uint16_t)p_src2_y[i_x+1] * i_trans +
596 (uint16_t)p_src1[i_x * 2 + 2] * (MAX_TRANS - i_trans) )
608 static void BlendPalI420( filter_t *p_filter, picture_t *p_dst,
609 picture_t *p_dst_orig, picture_t *p_src,
610 int i_x_offset, int i_y_offset,
611 int i_width, int i_height, int i_alpha )
613 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
614 uint8_t *p_src1_y, *p_src2, *p_dst_y;
615 uint8_t *p_src1_u, *p_dst_u;
616 uint8_t *p_src1_v, *p_dst_v;
617 int i_x, i_y, i_trans;
618 vlc_bool_t b_even_scanline = i_y_offset % 2;
620 i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
621 p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
622 p_filter->fmt_out.video.i_x_offset +
623 p_dst->p[Y_PLANE].i_pitch *
624 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
625 p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
626 p_filter->fmt_out.video.i_x_offset/2 +
627 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
628 p_dst->p[U_PLANE].i_pitch;
629 p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
630 p_filter->fmt_out.video.i_x_offset/2 +
631 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
632 p_dst->p[V_PLANE].i_pitch;
634 i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
635 p_src1_y = p_dst_orig->p[Y_PLANE].p_pixels + i_x_offset +
636 p_filter->fmt_out.video.i_x_offset +
637 p_dst_orig->p[Y_PLANE].i_pitch *
638 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
639 p_src1_u = p_dst_orig->p[U_PLANE].p_pixels + i_x_offset/2 +
640 p_filter->fmt_out.video.i_x_offset/2 +
641 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
642 p_dst_orig->p[U_PLANE].i_pitch;
643 p_src1_v = p_dst_orig->p[V_PLANE].p_pixels + i_x_offset/2 +
644 p_filter->fmt_out.video.i_x_offset/2 +
645 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
646 p_dst_orig->p[V_PLANE].i_pitch;
648 i_src2_pitch = p_src->p->i_pitch;
649 p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
650 i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
652 #define MAX_TRANS 255
654 #define p_trans p_src2
655 #define p_pal p_filter->fmt_in.video.p_palette->palette
657 /* Draw until we reach the bottom of the subtitle */
658 for( i_y = 0; i_y < i_height; i_y++,
659 p_dst_y += i_dst_pitch, p_src1_y += i_src1_pitch,
660 p_src2 += i_src2_pitch,
661 p_dst_u += b_even_scanline ? i_dst_pitch/2 : 0,
662 p_src1_u += b_even_scanline ? i_src1_pitch/2 : 0,
663 p_dst_v += b_even_scanline ? i_dst_pitch/2 : 0,
664 p_src1_v += b_even_scanline ? i_src1_pitch/2 : 0 )
666 b_even_scanline = !b_even_scanline;
668 /* Draw until we reach the end of the line */
669 for( i_x = 0; i_x < i_width; i_x++ )
671 i_trans = ( p_pal[p_trans[i_x]][3] * i_alpha ) / 255;
674 /* Completely transparent. Don't change pixel */
677 else if( i_trans == MAX_TRANS )
679 /* Completely opaque. Completely overwrite underlying pixel */
680 p_dst_y[i_x] = p_pal[p_src2[i_x]][0];
682 if( b_even_scanline && i_x % 2 == 0 )
684 p_dst_u[i_x/2] = p_pal[p_src2[i_x]][1];
685 p_dst_v[i_x/2] = p_pal[p_src2[i_x]][2];
691 p_dst_y[i_x] = ( (uint16_t)p_pal[p_src2[i_x]][0] * i_trans +
692 (uint16_t)p_src1_y[i_x] * (MAX_TRANS - i_trans) )
695 if( b_even_scanline && i_x % 2 == 0 )
697 p_dst_u[i_x/2] = ( (uint16_t)p_pal[p_src2[i_x]][1] * i_trans +
698 (uint16_t)p_src1_u[i_x/2] * (MAX_TRANS - i_trans) )
700 p_dst_v[i_x/2] = ( (uint16_t)p_pal[p_src2[i_x]][2] * i_trans +
701 (uint16_t)p_src1_v[i_x/2] * (MAX_TRANS - i_trans) )
715 static void BlendPalYUY2( filter_t *p_filter, picture_t *p_dst_pic,
716 picture_t *p_dst_orig, picture_t *p_src,
717 int i_x_offset, int i_y_offset,
718 int i_width, int i_height, int i_alpha )
720 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
721 uint8_t *p_src1, *p_src2, *p_dst;
722 int i_x, i_y, i_pix_pitch, i_trans;
725 i_dst_pitch = p_dst_pic->p->i_pitch;
726 p_dst = p_dst_pic->p->p_pixels + i_pix_pitch * (i_x_offset +
727 p_filter->fmt_out.video.i_x_offset) + p_dst_pic->p->i_pitch *
728 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
730 i_src1_pitch = p_dst_orig->p->i_pitch;
731 p_src1 = p_dst_orig->p->p_pixels + i_pix_pitch * (i_x_offset +
732 p_filter->fmt_out.video.i_x_offset) + p_dst_orig->p->i_pitch *
733 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
735 i_src2_pitch = p_src->p->i_pitch;
736 p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
737 i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
739 #define MAX_TRANS 255
741 #define p_trans p_src2
742 #define p_pal p_filter->fmt_in.video.p_palette->palette
744 /* Draw until we reach the bottom of the subtitle */
745 for( i_y = 0; i_y < i_height; i_y++,
746 p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_pitch )
748 /* Draw until we reach the end of the line */
749 for( i_x = 0; i_x < i_width; i_x += 2 )
751 i_trans = ( p_pal[p_trans[i_x]][3] * i_alpha ) / 255;
754 /* Completely transparent. Don't change pixel */
756 else if( i_trans == MAX_TRANS )
758 /* Completely opaque. Completely overwrite underlying pixel */
759 p_dst[i_x * 2] = p_pal[p_src2[i_x]][0];
760 p_dst[i_x * 2 + 1] = p_pal[p_src2[i_x]][1];
761 p_dst[i_x * 2 + 3] = p_pal[p_src2[i_x]][2];
766 p_dst[i_x * 2] = ( (uint16_t)p_pal[p_src2[i_x]][0] *
767 i_trans + (uint16_t)p_src1[i_x * 2] *
768 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
769 p_dst[i_x * 2 + 1] = ( (uint16_t)p_pal[p_src2[i_x]][1] *
770 i_trans + (uint16_t)p_src1[i_x * 2 + 1] *
771 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
772 p_dst[i_x * 2 + 3] = ( (uint16_t)p_pal[p_src2[i_x]][2] *
773 i_trans + (uint16_t)p_src1[i_x * 2 + 3] *
774 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
777 i_trans = ( p_pal[p_trans[i_x+1]][3] * i_alpha ) / 255;
780 /* Completely transparent. Don't change pixel */
782 else if( i_trans == MAX_TRANS )
784 /* Completely opaque. Completely overwrite underlying pixel */
785 p_dst[i_x * 2 + 2] = p_pal[p_src2[i_x + 1]][0];
790 p_dst[i_x * 2 + 2] = ( (uint16_t)p_pal[p_src2[i_x+1]][0] *
791 i_trans + (uint16_t)p_src1[i_x * 2 + 2] *
792 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
805 static void BlendPalRV( filter_t *p_filter, picture_t *p_dst_pic,
806 picture_t *p_dst_orig, picture_t *p_src,
807 int i_x_offset, int i_y_offset,
808 int i_width, int i_height, int i_alpha )
810 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
811 uint8_t *p_src1, *p_src2, *p_dst;
812 int i_x, i_y, i_pix_pitch, i_trans;
814 video_palette_t rgbpalette;
816 i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
817 i_dst_pitch = p_dst_pic->p->i_pitch;
818 p_dst = p_dst_pic->p->p_pixels + i_pix_pitch * (i_x_offset +
819 p_filter->fmt_out.video.i_x_offset) + p_dst_pic->p->i_pitch *
820 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
822 i_src1_pitch = p_dst_orig->p->i_pitch;
823 p_src1 = p_dst_orig->p->p_pixels + i_pix_pitch * (i_x_offset +
824 p_filter->fmt_out.video.i_x_offset) + p_dst_orig->p->i_pitch *
825 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
827 i_src2_pitch = p_src->p->i_pitch;
828 p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
829 i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
831 #define MAX_TRANS 255
833 #define p_trans p_src2
834 #define p_pal p_filter->fmt_in.video.p_palette->palette
835 #define rgbpal rgbpalette.palette
837 /* Convert palette first */
838 for( i_y = 0; i_y < p_filter->fmt_in.video.p_palette->i_entries &&
841 yuv_to_rgb( &r, &g, &b, p_pal[i_y][0], p_pal[i_y][1], p_pal[i_y][2] );
843 if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','1','6') )
845 *(uint16_t *)rgbpal[i_y] =
846 ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
850 rgbpal[i_y][0] = r; rgbpal[i_y][1] = g; rgbpal[i_y][2] = b;
854 /* Draw until we reach the bottom of the subtitle */
855 for( i_y = 0; i_y < i_height; i_y++,
856 p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_pitch )
858 /* Draw until we reach the end of the line */
859 for( i_x = 0; i_x < i_width; i_x++ )
861 i_trans = ( p_pal[p_trans[i_x]][3] * i_alpha ) / 255;
864 /* Completely transparent. Don't change pixel */
867 else if( i_trans == MAX_TRANS ||
868 p_filter->fmt_out.video.i_chroma ==
869 VLC_FOURCC('R','V','1','6') )
871 /* Completely opaque. Completely overwrite underlying pixel */
872 p_dst[i_x * i_pix_pitch] = rgbpal[p_src2[i_x]][0];
873 p_dst[i_x * i_pix_pitch + 1] = rgbpal[p_src2[i_x]][1];
874 if( p_filter->fmt_out.video.i_chroma !=
875 VLC_FOURCC('R','V','1','6') )
876 p_dst[i_x * i_pix_pitch + 2] = rgbpal[p_src2[i_x]][2];
881 p_dst[i_x * i_pix_pitch] = ( (uint16_t)rgbpal[p_src2[i_x]][0] *
882 i_trans + (uint16_t)p_src1[i_x * i_pix_pitch] *
883 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
884 p_dst[i_x * i_pix_pitch + 1] = ( (uint16_t)rgbpal[p_src2[i_x]][1] *
885 i_trans + (uint16_t)p_src1[i_x * i_pix_pitch + 1] *
886 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
887 p_dst[i_x * i_pix_pitch + 2] = ( (uint16_t)rgbpal[p_src2[i_x]][2] *
888 i_trans + (uint16_t)p_src1[i_x * i_pix_pitch + 2] *
889 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
902 /*****************************************************************************
903 * CloseFilter: clean up the filter
904 *****************************************************************************/
905 static void CloseFilter( vlc_object_t *p_this )
907 filter_t *p_filter = (filter_t*)p_this;
908 filter_sys_t *p_sys = p_filter->p_sys;