1 /*****************************************************************************
2 * blend.c: alpha blend 2 pictures together
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
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;
515 vlc_bool_t b_even = !((i_x_offset + p_filter->fmt_out.video.i_x_offset)%2);
518 i_dst_pitch = p_dst_pic->p->i_pitch;
519 p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
520 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
521 p_dst_pic->p->i_pitch *
522 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
524 i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
525 p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
526 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
527 p_dst_orig->p->i_pitch *
528 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
530 i_src2_pitch = p_src->p[Y_PLANE].i_pitch;
531 p_src2_y = p_src->p[Y_PLANE].p_pixels +
532 p_filter->fmt_in.video.i_x_offset +
533 p_src->p[Y_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
534 p_src2_u = p_src->p[U_PLANE].p_pixels +
535 p_filter->fmt_in.video.i_x_offset/2 +
536 p_src->p[U_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
537 p_src2_v = p_src->p[V_PLANE].p_pixels +
538 p_filter->fmt_in.video.i_x_offset/2 +
539 p_src->p[V_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
541 p_trans = p_src->p[A_PLANE].p_pixels +
542 p_filter->fmt_in.video.i_x_offset +
543 p_src->p[A_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
545 i_width = (i_width >> 1) << 1; /* Needs to be a multiple of 2 */
547 #define MAX_TRANS 255
550 /* Draw until we reach the bottom of the subtitle */
551 for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
552 p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
553 p_src2_y += i_src2_pitch, p_src2_u += i_src2_pitch,
554 p_src2_v += i_src2_pitch )
556 /* Draw until we reach the end of the line */
557 for( i_x = 0; i_x < i_width; i_x++, b_even = !b_even )
559 i_trans = ( p_trans[i_x] * i_alpha ) / 255;
562 /* Completely transparent. Don't change pixel */
564 else if( i_trans == MAX_TRANS )
566 /* Completely opaque. Completely overwrite underlying pixel */
567 p_dst[i_x * 2] = p_src2_y[i_x];
571 p_dst[i_x * 2 + 1] = p_src2_u[i_x];
572 p_dst[i_x * 2 + 3] = p_src2_v[i_x];
578 p_dst[i_x * 2] = ( (uint16_t)p_src2_y[i_x] * i_trans +
579 (uint16_t)p_src1[i_x * 2] * (MAX_TRANS - i_trans) )
584 p_dst[i_x * 2 + 1] = ( (uint16_t)p_src2_u[i_x] * i_trans +
585 (uint16_t)p_src1[i_x * 2 + 1] * (MAX_TRANS - i_trans) )
587 p_dst[i_x * 2 + 3] = ( (uint16_t)p_src2_v[i_x] * i_trans +
588 (uint16_t)p_src1[i_x * 2 + 3] * (MAX_TRANS - i_trans) )
601 static void BlendPalI420( filter_t *p_filter, picture_t *p_dst,
602 picture_t *p_dst_orig, picture_t *p_src,
603 int i_x_offset, int i_y_offset,
604 int i_width, int i_height, int i_alpha )
606 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
607 uint8_t *p_src1_y, *p_src2, *p_dst_y;
608 uint8_t *p_src1_u, *p_dst_u;
609 uint8_t *p_src1_v, *p_dst_v;
610 int i_x, i_y, i_trans;
611 vlc_bool_t b_even_scanline = i_y_offset % 2;
613 i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
614 p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
615 p_filter->fmt_out.video.i_x_offset +
616 p_dst->p[Y_PLANE].i_pitch *
617 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
618 p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
619 p_filter->fmt_out.video.i_x_offset/2 +
620 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
621 p_dst->p[U_PLANE].i_pitch;
622 p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
623 p_filter->fmt_out.video.i_x_offset/2 +
624 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
625 p_dst->p[V_PLANE].i_pitch;
627 i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
628 p_src1_y = p_dst_orig->p[Y_PLANE].p_pixels + i_x_offset +
629 p_filter->fmt_out.video.i_x_offset +
630 p_dst_orig->p[Y_PLANE].i_pitch *
631 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
632 p_src1_u = p_dst_orig->p[U_PLANE].p_pixels + i_x_offset/2 +
633 p_filter->fmt_out.video.i_x_offset/2 +
634 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
635 p_dst_orig->p[U_PLANE].i_pitch;
636 p_src1_v = p_dst_orig->p[V_PLANE].p_pixels + i_x_offset/2 +
637 p_filter->fmt_out.video.i_x_offset/2 +
638 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
639 p_dst_orig->p[V_PLANE].i_pitch;
641 i_src2_pitch = p_src->p->i_pitch;
642 p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
643 i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
645 #define MAX_TRANS 255
647 #define p_trans p_src2
648 #define p_pal p_filter->fmt_in.video.p_palette->palette
650 /* Draw until we reach the bottom of the subtitle */
651 for( i_y = 0; i_y < i_height; i_y++,
652 p_dst_y += i_dst_pitch, p_src1_y += i_src1_pitch,
653 p_src2 += i_src2_pitch,
654 p_dst_u += b_even_scanline ? i_dst_pitch/2 : 0,
655 p_src1_u += b_even_scanline ? i_src1_pitch/2 : 0,
656 p_dst_v += b_even_scanline ? i_dst_pitch/2 : 0,
657 p_src1_v += b_even_scanline ? i_src1_pitch/2 : 0 )
659 b_even_scanline = !b_even_scanline;
661 /* Draw until we reach the end of the line */
662 for( i_x = 0; i_x < i_width; i_x++ )
664 i_trans = ( p_pal[p_trans[i_x]][3] * i_alpha ) / 255;
667 /* Completely transparent. Don't change pixel */
670 else if( i_trans == MAX_TRANS )
672 /* Completely opaque. Completely overwrite underlying pixel */
673 p_dst_y[i_x] = p_pal[p_src2[i_x]][0];
675 if( b_even_scanline && i_x % 2 == 0 )
677 p_dst_u[i_x/2] = p_pal[p_src2[i_x]][1];
678 p_dst_v[i_x/2] = p_pal[p_src2[i_x]][2];
684 p_dst_y[i_x] = ( (uint16_t)p_pal[p_src2[i_x]][0] * i_trans +
685 (uint16_t)p_src1_y[i_x] * (MAX_TRANS - i_trans) )
688 if( b_even_scanline && i_x % 2 == 0 )
690 p_dst_u[i_x/2] = ( (uint16_t)p_pal[p_src2[i_x]][1] * i_trans +
691 (uint16_t)p_src1_u[i_x/2] * (MAX_TRANS - i_trans) )
693 p_dst_v[i_x/2] = ( (uint16_t)p_pal[p_src2[i_x]][2] * i_trans +
694 (uint16_t)p_src1_v[i_x/2] * (MAX_TRANS - i_trans) )
708 static void BlendPalYUY2( filter_t *p_filter, picture_t *p_dst_pic,
709 picture_t *p_dst_orig, picture_t *p_src,
710 int i_x_offset, int i_y_offset,
711 int i_width, int i_height, int i_alpha )
713 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
714 uint8_t *p_src1, *p_src2, *p_dst;
715 int i_x, i_y, i_pix_pitch, i_trans;
716 vlc_bool_t b_even = !((i_x_offset + p_filter->fmt_out.video.i_x_offset)%2);
719 i_dst_pitch = p_dst_pic->p->i_pitch;
720 p_dst = p_dst_pic->p->p_pixels + i_pix_pitch * (i_x_offset +
721 p_filter->fmt_out.video.i_x_offset) + p_dst_pic->p->i_pitch *
722 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
724 i_src1_pitch = p_dst_orig->p->i_pitch;
725 p_src1 = p_dst_orig->p->p_pixels + i_pix_pitch * (i_x_offset +
726 p_filter->fmt_out.video.i_x_offset) + p_dst_orig->p->i_pitch *
727 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
729 i_src2_pitch = p_src->p->i_pitch;
730 p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
731 i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
733 i_width = (i_width >> 1) << 1; /* Needs to be a multiple of 2 */
735 #define MAX_TRANS 255
737 #define p_trans p_src2
738 #define p_pal p_filter->fmt_in.video.p_palette->palette
740 /* Draw until we reach the bottom of the subtitle */
741 for( i_y = 0; i_y < i_height; i_y++,
742 p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_pitch )
744 /* Draw until we reach the end of the line */
745 for( i_x = 0; i_x < i_width; i_x++, b_even = !b_even )
747 i_trans = ( p_pal[p_trans[i_x]][3] * i_alpha ) / 255;
750 /* Completely transparent. Don't change pixel */
752 else if( i_trans == MAX_TRANS )
754 /* Completely opaque. Completely overwrite underlying pixel */
755 p_dst[i_x * 2] = p_pal[p_src2[i_x]][0];
759 p_dst[i_x * 2 + 1] = p_pal[p_src2[i_x]][1];
760 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;
772 p_dst[i_x * 2 + 1] = ( (uint16_t)p_pal[p_src2[i_x]][1] *
773 i_trans + (uint16_t)p_src1[i_x * 2 + 1] *
774 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
775 p_dst[i_x * 2 + 3] = ( (uint16_t)p_pal[p_src2[i_x]][2] *
776 i_trans + (uint16_t)p_src1[i_x * 2 + 3] *
777 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
791 static void BlendPalRV( filter_t *p_filter, picture_t *p_dst_pic,
792 picture_t *p_dst_orig, picture_t *p_src,
793 int i_x_offset, int i_y_offset,
794 int i_width, int i_height, int i_alpha )
796 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
797 uint8_t *p_src1, *p_src2, *p_dst;
798 int i_x, i_y, i_pix_pitch, i_trans;
800 video_palette_t rgbpalette;
802 i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
803 i_dst_pitch = p_dst_pic->p->i_pitch;
804 p_dst = p_dst_pic->p->p_pixels + i_pix_pitch * (i_x_offset +
805 p_filter->fmt_out.video.i_x_offset) + p_dst_pic->p->i_pitch *
806 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
808 i_src1_pitch = p_dst_orig->p->i_pitch;
809 p_src1 = p_dst_orig->p->p_pixels + i_pix_pitch * (i_x_offset +
810 p_filter->fmt_out.video.i_x_offset) + p_dst_orig->p->i_pitch *
811 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
813 i_src2_pitch = p_src->p->i_pitch;
814 p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
815 i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
817 #define MAX_TRANS 255
819 #define p_trans p_src2
820 #define p_pal p_filter->fmt_in.video.p_palette->palette
821 #define rgbpal rgbpalette.palette
823 /* Convert palette first */
824 for( i_y = 0; i_y < p_filter->fmt_in.video.p_palette->i_entries &&
827 yuv_to_rgb( &r, &g, &b, p_pal[i_y][0], p_pal[i_y][1], p_pal[i_y][2] );
829 if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','1','6') )
831 *(uint16_t *)rgbpal[i_y] =
832 ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
836 rgbpal[i_y][0] = r; rgbpal[i_y][1] = g; rgbpal[i_y][2] = b;
840 /* Draw until we reach the bottom of the subtitle */
841 for( i_y = 0; i_y < i_height; i_y++,
842 p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_pitch )
844 /* Draw until we reach the end of the line */
845 for( i_x = 0; i_x < i_width; i_x++ )
847 i_trans = ( p_pal[p_trans[i_x]][3] * i_alpha ) / 255;
850 /* Completely transparent. Don't change pixel */
853 else if( i_trans == MAX_TRANS ||
854 p_filter->fmt_out.video.i_chroma ==
855 VLC_FOURCC('R','V','1','6') )
857 /* Completely opaque. Completely overwrite underlying pixel */
858 p_dst[i_x * i_pix_pitch] = rgbpal[p_src2[i_x]][0];
859 p_dst[i_x * i_pix_pitch + 1] = rgbpal[p_src2[i_x]][1];
860 if( p_filter->fmt_out.video.i_chroma !=
861 VLC_FOURCC('R','V','1','6') )
862 p_dst[i_x * i_pix_pitch + 2] = rgbpal[p_src2[i_x]][2];
867 p_dst[i_x * i_pix_pitch] = ( (uint16_t)rgbpal[p_src2[i_x]][0] *
868 i_trans + (uint16_t)p_src1[i_x * i_pix_pitch] *
869 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
870 p_dst[i_x * i_pix_pitch + 1] = ( (uint16_t)rgbpal[p_src2[i_x]][1] *
871 i_trans + (uint16_t)p_src1[i_x * i_pix_pitch + 1] *
872 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
873 p_dst[i_x * i_pix_pitch + 2] = ( (uint16_t)rgbpal[p_src2[i_x]][2] *
874 i_trans + (uint16_t)p_src1[i_x * i_pix_pitch + 2] *
875 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
888 /*****************************************************************************
889 * CloseFilter: clean up the filter
890 *****************************************************************************/
891 static void CloseFilter( vlc_object_t *p_this )
893 filter_t *p_filter = (filter_t*)p_this;
894 filter_sys_t *p_sys = p_filter->p_sys;