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_capability( "video blending", 100 );
69 set_callbacks( OpenFilter, CloseFilter );
72 /*****************************************************************************
73 * OpenFilter: probe the filter and return score
74 *****************************************************************************/
75 static int OpenFilter( vlc_object_t *p_this )
77 filter_t *p_filter = (filter_t*)p_this;
80 /* Check if we can handle that format.
81 * We could try to use a chroma filter if we can't. */
82 if( ( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','A') &&
83 p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','P') ) ||
84 ( p_filter->fmt_out.video.i_chroma != VLC_FOURCC('I','4','2','0') &&
85 p_filter->fmt_out.video.i_chroma != VLC_FOURCC('Y','U','Y','2') &&
86 p_filter->fmt_out.video.i_chroma != VLC_FOURCC('Y','V','1','2') &&
87 p_filter->fmt_out.video.i_chroma != VLC_FOURCC('R','V','1','6') &&
88 p_filter->fmt_out.video.i_chroma != VLC_FOURCC('R','V','2','4') &&
89 p_filter->fmt_out.video.i_chroma != VLC_FOURCC('R','V','3','2') ) )
94 /* Allocate the memory needed to store the decoder's structure */
95 if( ( p_filter->p_sys = p_sys =
96 (filter_sys_t *)malloc(sizeof(filter_sys_t)) ) == NULL )
98 msg_Err( p_filter, "out of memory" );
103 p_filter->pf_video_blend = Blend;
105 msg_Dbg( p_filter, "chroma: %4.4s -> %4.4s",
106 (char *)&p_filter->fmt_in.video.i_chroma,
107 (char *)&p_filter->fmt_out.video.i_chroma );
113 /****************************************************************************
114 * Blend: the whole thing
115 ****************************************************************************
116 * This function is called just after the thread is launched.
117 ****************************************************************************/
118 static void Blend( filter_t *p_filter, picture_t *p_dst,
119 picture_t *p_dst_orig, picture_t *p_src,
120 int i_x_offset, int i_y_offset, int i_alpha )
122 int i_width, i_height;
124 i_width = __MIN((int)p_filter->fmt_out.video.i_visible_width - i_x_offset,
125 (int)p_filter->fmt_in.video.i_visible_width);
127 i_height = __MIN((int)p_filter->fmt_out.video.i_visible_height -i_y_offset,
128 (int)p_filter->fmt_in.video.i_visible_height);
130 if( i_width <= 0 || i_height <= 0 ) return;
132 if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','A') &&
133 ( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('I','4','2','0') ||
134 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','V','1','2') ) )
136 BlendI420( p_filter, p_dst, p_dst_orig, p_src,
137 i_x_offset, i_y_offset, i_width, i_height, i_alpha );
140 if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','A') &&
141 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','U','Y','2') )
143 BlendYUY2( p_filter, p_dst, p_dst_orig, p_src,
144 i_x_offset, i_y_offset, i_width, i_height, i_alpha );
147 if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','A') &&
148 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','1','6') )
150 BlendR16( p_filter, p_dst, p_dst_orig, p_src,
151 i_x_offset, i_y_offset, i_width, i_height, i_alpha );
154 if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','A') &&
155 ( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','2','4') ||
156 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','3','2') ) )
158 BlendR24( p_filter, p_dst, p_dst_orig, p_src,
159 i_x_offset, i_y_offset, i_width, i_height, i_alpha );
162 if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','P') &&
163 ( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('I','4','2','0') ||
164 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','V','1','2') ) )
166 BlendPalI420( p_filter, p_dst, p_dst_orig, p_src,
167 i_x_offset, i_y_offset, i_width, i_height, i_alpha );
170 if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','P') &&
171 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','U','Y','2') )
173 BlendPalYUY2( p_filter, p_dst, p_dst_orig, p_src,
174 i_x_offset, i_y_offset, i_width, i_height, i_alpha );
177 if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','P') &&
178 ( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','1','6') ||
179 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','2','4') ||
180 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','3','2') ) )
182 BlendPalRV( p_filter, p_dst, p_dst_orig, p_src,
183 i_x_offset, i_y_offset, i_width, i_height, i_alpha );
187 msg_Dbg( p_filter, "no matching alpha blending routine" );
190 static void BlendI420( filter_t *p_filter, picture_t *p_dst,
191 picture_t *p_dst_orig, picture_t *p_src,
192 int i_x_offset, int i_y_offset,
193 int i_width, int i_height, int i_alpha )
195 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
196 uint8_t *p_src1_y, *p_src2_y, *p_dst_y;
197 uint8_t *p_src1_u, *p_src2_u, *p_dst_u;
198 uint8_t *p_src1_v, *p_src2_v, *p_dst_v;
200 int i_x, i_y, i_trans;
201 vlc_bool_t b_even_scanline = i_y_offset % 2;
203 i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
204 p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
205 p_filter->fmt_out.video.i_x_offset +
206 p_dst->p[Y_PLANE].i_pitch *
207 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
208 p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
209 p_filter->fmt_out.video.i_x_offset/2 +
210 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
211 p_dst->p[U_PLANE].i_pitch;
212 p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
213 p_filter->fmt_out.video.i_x_offset/2 +
214 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
215 p_dst->p[V_PLANE].i_pitch;
217 i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
218 p_src1_y = p_dst_orig->p[Y_PLANE].p_pixels + i_x_offset +
219 p_filter->fmt_out.video.i_x_offset +
220 p_dst_orig->p[Y_PLANE].i_pitch *
221 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
222 p_src1_u = p_dst_orig->p[U_PLANE].p_pixels + i_x_offset/2 +
223 p_filter->fmt_out.video.i_x_offset/2 +
224 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
225 p_dst_orig->p[U_PLANE].i_pitch;
226 p_src1_v = p_dst_orig->p[V_PLANE].p_pixels + i_x_offset/2 +
227 p_filter->fmt_out.video.i_x_offset/2 +
228 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
229 p_dst_orig->p[V_PLANE].i_pitch;
231 i_src2_pitch = p_src->p[Y_PLANE].i_pitch;
232 p_src2_y = p_src->p[Y_PLANE].p_pixels +
233 p_filter->fmt_in.video.i_x_offset +
234 p_src->p[Y_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
235 p_src2_u = p_src->p[U_PLANE].p_pixels +
236 p_filter->fmt_in.video.i_x_offset/2 +
237 p_src->p[U_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
238 p_src2_v = p_src->p[V_PLANE].p_pixels +
239 p_filter->fmt_in.video.i_x_offset/2 +
240 p_src->p[V_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
242 p_trans = p_src->p[A_PLANE].p_pixels +
243 p_filter->fmt_in.video.i_x_offset +
244 p_src->p[A_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
246 #define MAX_TRANS 255
249 /* Draw until we reach the bottom of the subtitle */
250 for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
251 p_dst_y += i_dst_pitch, p_src1_y += i_src1_pitch,
252 p_src2_y += i_src2_pitch,
253 p_dst_u += b_even_scanline ? i_dst_pitch/2 : 0,
254 p_src1_u += b_even_scanline ? i_src1_pitch/2 : 0,
255 p_src2_u += i_src2_pitch,
256 p_dst_v += b_even_scanline ? i_dst_pitch/2 : 0,
257 p_src1_v += b_even_scanline ? i_src1_pitch/2 : 0,
258 p_src2_v += i_src2_pitch )
260 b_even_scanline = !b_even_scanline;
262 /* Draw until we reach the end of the line */
263 for( i_x = 0; i_x < i_width; i_x++ )
265 i_trans = ( p_trans[i_x] * i_alpha ) / 255;
268 /* Completely transparent. Don't change pixel */
271 else if( i_trans == MAX_TRANS )
273 /* Completely opaque. Completely overwrite underlying pixel */
274 p_dst_y[i_x] = p_src2_y[i_x];
276 if( b_even_scanline && i_x % 2 == 0 )
278 p_dst_u[i_x/2] = p_src2_u[i_x];
279 p_dst_v[i_x/2] = p_src2_v[i_x];
285 p_dst_y[i_x] = ( (uint16_t)p_src2_y[i_x] * i_trans +
286 (uint16_t)p_src1_y[i_x] * (MAX_TRANS - i_trans) )
289 if( b_even_scanline && i_x % 2 == 0 )
291 p_dst_u[i_x/2] = ( (uint16_t)p_src2_u[i_x] * i_trans +
292 (uint16_t)p_src1_u[i_x/2] * (MAX_TRANS - i_trans) )
294 p_dst_v[i_x/2] = ( (uint16_t)p_src2_v[i_x] * i_trans +
295 (uint16_t)p_src1_v[i_x/2] * (MAX_TRANS - i_trans) )
307 static inline void yuv_to_rgb( int *r, int *g, int *b,
308 uint8_t y1, uint8_t u1, uint8_t v1 )
310 /* macros used for YUV pixel conversions */
311 # define SCALEBITS 10
312 # define ONE_HALF (1 << (SCALEBITS - 1))
313 # define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
314 # define CLAMP( x ) (((x) > 255) ? 255 : ((x) < 0) ? 0 : (x));
316 int y, cb, cr, r_add, g_add, b_add;
320 r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;
321 g_add = - FIX(0.34414*255.0/224.0) * cb
322 - FIX(0.71414*255.0/224.0) * cr + ONE_HALF;
323 b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;
324 y = (y1 - 16) * FIX(255.0/219.0);
325 *r = CLAMP((y + r_add) >> SCALEBITS);
326 *g = CLAMP((y + g_add) >> SCALEBITS);
327 *b = CLAMP((y + b_add) >> SCALEBITS);
330 static void BlendR16( filter_t *p_filter, picture_t *p_dst_pic,
331 picture_t *p_dst_orig, picture_t *p_src,
332 int i_x_offset, int i_y_offset,
333 int i_width, int i_height, int i_alpha )
335 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
336 uint8_t *p_dst, *p_src1, *p_src2_y;
337 uint8_t *p_src2_u, *p_src2_v;
339 int i_x, i_y, i_pix_pitch;
342 i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
343 i_dst_pitch = p_dst_pic->p->i_pitch;
344 p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
345 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
346 p_dst_pic->p->i_pitch *
347 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
349 i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
350 p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
351 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
352 p_dst_orig->p->i_pitch *
353 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
355 i_src2_pitch = p_src->p[Y_PLANE].i_pitch;
356 p_src2_y = p_src->p[Y_PLANE].p_pixels +
357 p_filter->fmt_in.video.i_x_offset +
358 p_src->p[Y_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
359 p_src2_u = p_src->p[U_PLANE].p_pixels +
360 p_filter->fmt_in.video.i_x_offset/2 +
361 p_src->p[U_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
362 p_src2_v = p_src->p[V_PLANE].p_pixels +
363 p_filter->fmt_in.video.i_x_offset/2 +
364 p_src->p[V_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
366 p_trans = p_src->p[A_PLANE].p_pixels +
367 p_filter->fmt_in.video.i_x_offset +
368 p_src->p[A_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
370 #define MAX_TRANS 255
373 /* Draw until we reach the bottom of the subtitle */
374 for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
375 p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
376 p_src2_y += i_src2_pitch, p_src2_u += i_src2_pitch,
377 p_src2_v += i_src2_pitch )
379 /* Draw until we reach the end of the line */
380 for( i_x = 0; i_x < i_width; i_x++ )
384 /* Completely transparent. Don't change pixel */
387 else if( p_trans[i_x] == MAX_TRANS )
389 /* Completely opaque. Completely overwrite underlying pixel */
390 yuv_to_rgb( &r, &g, &b,
391 p_src2_y[i_x], p_src2_u[i_x], p_src2_v[i_x] );
393 ((uint16_t *)(&p_dst[i_x * i_pix_pitch]))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
398 yuv_to_rgb( &r, &g, &b,
399 p_src2_y[i_x], p_src2_u[i_x], p_src2_v[i_x] );
401 ((uint16_t *)(&p_dst[i_x * i_pix_pitch]))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
411 static void BlendR24( filter_t *p_filter, picture_t *p_dst_pic,
412 picture_t *p_dst_orig, picture_t *p_src,
413 int i_x_offset, int i_y_offset,
414 int i_width, int i_height, int i_alpha )
416 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
417 uint8_t *p_dst, *p_src1, *p_src2_y;
418 uint8_t *p_src2_u, *p_src2_v;
420 int i_x, i_y, i_pix_pitch, i_trans;
423 i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
424 i_dst_pitch = p_dst_pic->p->i_pitch;
425 p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
426 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
427 p_dst_pic->p->i_pitch *
428 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
430 i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
431 p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
432 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
433 p_dst_orig->p->i_pitch *
434 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
436 i_src2_pitch = p_src->p[Y_PLANE].i_pitch;
437 p_src2_y = p_src->p[Y_PLANE].p_pixels +
438 p_filter->fmt_in.video.i_x_offset +
439 p_src->p[Y_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
440 p_src2_u = p_src->p[U_PLANE].p_pixels +
441 p_filter->fmt_in.video.i_x_offset/2 +
442 p_src->p[U_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
443 p_src2_v = p_src->p[V_PLANE].p_pixels +
444 p_filter->fmt_in.video.i_x_offset/2 +
445 p_src->p[V_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
447 p_trans = p_src->p[A_PLANE].p_pixels +
448 p_filter->fmt_in.video.i_x_offset +
449 p_src->p[A_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
451 #define MAX_TRANS 255
454 /* Draw until we reach the bottom of the subtitle */
455 for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
456 p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
457 p_src2_y += i_src2_pitch, p_src2_u += i_src2_pitch,
458 p_src2_v += i_src2_pitch )
460 /* Draw until we reach the end of the line */
461 for( i_x = 0; i_x < i_width; i_x++ )
463 i_trans = ( p_trans[i_x] * i_alpha ) / 255;
466 /* Completely transparent. Don't change pixel */
469 else if( i_trans == MAX_TRANS )
471 /* Completely opaque. Completely overwrite underlying pixel */
472 yuv_to_rgb( &r, &g, &b,
473 p_src2_y[i_x], p_src2_u[i_x], p_src2_v[i_x] );
475 p_dst[i_x * i_pix_pitch] = r;
476 p_dst[i_x * i_pix_pitch + 1] = g;
477 p_dst[i_x * i_pix_pitch + 2] = b;
482 yuv_to_rgb( &r, &g, &b,
483 p_src2_y[i_x], p_src2_u[i_x], p_src2_v[i_x] );
485 p_dst[i_x * i_pix_pitch] = ( r * i_trans +
486 (uint16_t)p_src1[i_x * i_pix_pitch] *
487 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
488 p_dst[i_x * i_pix_pitch + 1] = ( g * i_trans +
489 (uint16_t)p_src1[i_x * i_pix_pitch + 1] *
490 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
491 p_dst[i_x * i_pix_pitch + 2] = ( b * i_trans +
492 (uint16_t)p_src1[i_x * i_pix_pitch + 2] *
493 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
503 static void BlendYUY2( filter_t *p_filter, picture_t *p_dst_pic,
504 picture_t *p_dst_orig, picture_t *p_src,
505 int i_x_offset, int i_y_offset,
506 int i_width, int i_height, int i_alpha )
508 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
509 uint8_t *p_dst, *p_src1, *p_src2_y;
510 uint8_t *p_src2_u, *p_src2_v;
512 int i_x, i_y, i_pix_pitch, i_trans;
515 i_dst_pitch = p_dst_pic->p->i_pitch;
516 p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
517 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
518 p_dst_pic->p->i_pitch *
519 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
521 i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
522 p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
523 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
524 p_dst_orig->p->i_pitch *
525 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
527 i_src2_pitch = p_src->p[Y_PLANE].i_pitch;
528 p_src2_y = p_src->p[Y_PLANE].p_pixels +
529 p_filter->fmt_in.video.i_x_offset +
530 p_src->p[Y_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
531 p_src2_u = p_src->p[U_PLANE].p_pixels +
532 p_filter->fmt_in.video.i_x_offset/2 +
533 p_src->p[U_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
534 p_src2_v = p_src->p[V_PLANE].p_pixels +
535 p_filter->fmt_in.video.i_x_offset/2 +
536 p_src->p[V_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
538 p_trans = p_src->p[A_PLANE].p_pixels +
539 p_filter->fmt_in.video.i_x_offset +
540 p_src->p[A_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
542 #define MAX_TRANS 255
545 /* Draw until we reach the bottom of the subtitle */
546 for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
547 p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
548 p_src2_y += i_src2_pitch, p_src2_u += i_src2_pitch,
549 p_src2_v += i_src2_pitch )
551 /* Draw until we reach the end of the line */
552 for( i_x = 0; i_x < i_width; i_x += 2 )
554 i_trans = ( p_trans[i_x] * i_alpha ) / 255;
557 /* Completely transparent. Don't change pixel */
559 else if( i_trans == MAX_TRANS )
561 /* Completely opaque. Completely overwrite underlying pixel */
562 p_dst[i_x * 2] = p_src2_y[i_x];
563 p_dst[i_x * 2 + 1] = p_src2_u[i_x];
564 p_dst[i_x * 2 + 3] = p_src2_v[i_x];
569 p_dst[i_x * 2] = ( (uint16_t)p_src2_y[i_x] * i_trans +
570 (uint16_t)p_src1[i_x * 2] * (MAX_TRANS - i_trans) )
572 p_dst[i_x * 2 + 1] = ( (uint16_t)p_src2_u[i_x] * i_trans +
573 (uint16_t)p_src1[i_x * 2 + 1] * (MAX_TRANS - i_trans) )
575 p_dst[i_x * 2 + 3] = ( (uint16_t)p_src2_v[i_x] * i_trans +
576 (uint16_t)p_src1[i_x * 2 + 3] * (MAX_TRANS - i_trans) )
580 i_trans = ( p_trans[i_x+1] * i_alpha ) / 255;
583 /* Completely transparent. Don't change pixel */
585 else if( i_trans == MAX_TRANS )
587 /* Completely opaque. Completely overwrite underlying pixel */
588 p_dst[i_x * 2 + 2] = p_src2_y[i_x + 1];
593 p_dst[i_x * 2 + 2] = ( (uint16_t)p_src2_y[i_x+1] * i_trans +
594 (uint16_t)p_src1[i_x * 2 + 2] * (MAX_TRANS - i_trans) )
606 static void BlendPalI420( filter_t *p_filter, picture_t *p_dst,
607 picture_t *p_dst_orig, picture_t *p_src,
608 int i_x_offset, int i_y_offset,
609 int i_width, int i_height, int i_alpha )
611 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
612 uint8_t *p_src1_y, *p_src2, *p_dst_y;
613 uint8_t *p_src1_u, *p_dst_u;
614 uint8_t *p_src1_v, *p_dst_v;
615 int i_x, i_y, i_trans;
616 vlc_bool_t b_even_scanline = i_y_offset % 2;
618 i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
619 p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
620 p_filter->fmt_out.video.i_x_offset +
621 p_dst->p[Y_PLANE].i_pitch *
622 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
623 p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
624 p_filter->fmt_out.video.i_x_offset/2 +
625 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
626 p_dst->p[U_PLANE].i_pitch;
627 p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
628 p_filter->fmt_out.video.i_x_offset/2 +
629 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
630 p_dst->p[V_PLANE].i_pitch;
632 i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
633 p_src1_y = p_dst_orig->p[Y_PLANE].p_pixels + i_x_offset +
634 p_filter->fmt_out.video.i_x_offset +
635 p_dst_orig->p[Y_PLANE].i_pitch *
636 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
637 p_src1_u = p_dst_orig->p[U_PLANE].p_pixels + i_x_offset/2 +
638 p_filter->fmt_out.video.i_x_offset/2 +
639 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
640 p_dst_orig->p[U_PLANE].i_pitch;
641 p_src1_v = p_dst_orig->p[V_PLANE].p_pixels + i_x_offset/2 +
642 p_filter->fmt_out.video.i_x_offset/2 +
643 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
644 p_dst_orig->p[V_PLANE].i_pitch;
646 i_src2_pitch = p_src->p->i_pitch;
647 p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
648 i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
650 #define MAX_TRANS 255
652 #define p_trans p_src2
653 #define p_pal p_filter->fmt_in.video.p_palette->palette
655 /* Draw until we reach the bottom of the subtitle */
656 for( i_y = 0; i_y < i_height; i_y++,
657 p_dst_y += i_dst_pitch, p_src1_y += i_src1_pitch,
658 p_src2 += i_src2_pitch,
659 p_dst_u += b_even_scanline ? i_dst_pitch/2 : 0,
660 p_src1_u += b_even_scanline ? i_src1_pitch/2 : 0,
661 p_dst_v += b_even_scanline ? i_dst_pitch/2 : 0,
662 p_src1_v += b_even_scanline ? i_src1_pitch/2 : 0 )
664 b_even_scanline = !b_even_scanline;
666 /* Draw until we reach the end of the line */
667 for( i_x = 0; i_x < i_width; i_x++ )
669 i_trans = ( p_pal[p_trans[i_x]][3] * i_alpha ) / 255;
672 /* Completely transparent. Don't change pixel */
675 else if( i_trans == MAX_TRANS )
677 /* Completely opaque. Completely overwrite underlying pixel */
678 p_dst_y[i_x] = p_pal[p_src2[i_x]][0];
680 if( b_even_scanline && i_x % 2 == 0 )
682 p_dst_u[i_x/2] = p_pal[p_src2[i_x]][1];
683 p_dst_v[i_x/2] = p_pal[p_src2[i_x]][2];
689 p_dst_y[i_x] = ( (uint16_t)p_pal[p_src2[i_x]][0] * i_trans +
690 (uint16_t)p_src1_y[i_x] * (MAX_TRANS - i_trans) )
693 if( b_even_scanline && i_x % 2 == 0 )
695 p_dst_u[i_x/2] = ( (uint16_t)p_pal[p_src2[i_x]][1] * i_trans +
696 (uint16_t)p_src1_u[i_x/2] * (MAX_TRANS - i_trans) )
698 p_dst_v[i_x/2] = ( (uint16_t)p_pal[p_src2[i_x]][2] * i_trans +
699 (uint16_t)p_src1_v[i_x/2] * (MAX_TRANS - i_trans) )
713 static void BlendPalYUY2( filter_t *p_filter, picture_t *p_dst_pic,
714 picture_t *p_dst_orig, picture_t *p_src,
715 int i_x_offset, int i_y_offset,
716 int i_width, int i_height, int i_alpha )
718 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
719 uint8_t *p_src1, *p_src2, *p_dst;
720 int i_x, i_y, i_pix_pitch, i_trans;
723 i_dst_pitch = p_dst_pic->p->i_pitch;
724 p_dst = p_dst_pic->p->p_pixels + i_pix_pitch * (i_x_offset +
725 p_filter->fmt_out.video.i_x_offset) + p_dst_pic->p->i_pitch *
726 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
728 i_src1_pitch = p_dst_orig->p->i_pitch;
729 p_src1 = p_dst_orig->p->p_pixels + i_pix_pitch * (i_x_offset +
730 p_filter->fmt_out.video.i_x_offset) + p_dst_orig->p->i_pitch *
731 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
733 i_src2_pitch = p_src->p->i_pitch;
734 p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
735 i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
737 #define MAX_TRANS 255
739 #define p_trans p_src2
740 #define p_pal p_filter->fmt_in.video.p_palette->palette
742 /* Draw until we reach the bottom of the subtitle */
743 for( i_y = 0; i_y < i_height; i_y++,
744 p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_pitch )
746 /* Draw until we reach the end of the line */
747 for( i_x = 0; i_x < i_width; i_x += 2 )
749 i_trans = ( p_pal[p_trans[i_x]][3] * i_alpha ) / 255;
752 /* Completely transparent. Don't change pixel */
754 else if( i_trans == MAX_TRANS )
756 /* Completely opaque. Completely overwrite underlying pixel */
757 p_dst[i_x * 2] = p_pal[p_src2[i_x]][0];
758 p_dst[i_x * 2 + 1] = p_pal[p_src2[i_x]][1];
759 p_dst[i_x * 2 + 3] = p_pal[p_src2[i_x]][2];
764 p_dst[i_x * 2] = ( (uint16_t)p_pal[p_src2[i_x]][0] *
765 i_trans + (uint16_t)p_src1[i_x * 2] *
766 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
767 p_dst[i_x * 2 + 1] = ( (uint16_t)p_pal[p_src2[i_x]][1] *
768 i_trans + (uint16_t)p_src1[i_x * 2 + 1] *
769 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
770 p_dst[i_x * 2 + 3] = ( (uint16_t)p_pal[p_src2[i_x]][2] *
771 i_trans + (uint16_t)p_src1[i_x * 2 + 3] *
772 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
775 i_trans = ( p_pal[p_trans[i_x+1]][3] * i_alpha ) / 255;
778 /* Completely transparent. Don't change pixel */
780 else if( i_trans == MAX_TRANS )
782 /* Completely opaque. Completely overwrite underlying pixel */
783 p_dst[i_x * 2 + 2] = p_pal[p_src2[i_x + 1]][0];
788 p_dst[i_x * 2 + 2] = ( (uint16_t)p_pal[p_src2[i_x+1]][0] *
789 i_trans + (uint16_t)p_src1[i_x * 2 + 2] *
790 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
803 static void BlendPalRV( filter_t *p_filter, picture_t *p_dst_pic,
804 picture_t *p_dst_orig, picture_t *p_src,
805 int i_x_offset, int i_y_offset,
806 int i_width, int i_height, int i_alpha )
808 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
809 uint8_t *p_src1, *p_src2, *p_dst;
810 int i_x, i_y, i_pix_pitch, i_trans;
812 video_palette_t rgbpalette;
814 i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
815 i_dst_pitch = p_dst_pic->p->i_pitch;
816 p_dst = p_dst_pic->p->p_pixels + i_pix_pitch * (i_x_offset +
817 p_filter->fmt_out.video.i_x_offset) + p_dst_pic->p->i_pitch *
818 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
820 i_src1_pitch = p_dst_orig->p->i_pitch;
821 p_src1 = p_dst_orig->p->p_pixels + i_pix_pitch * (i_x_offset +
822 p_filter->fmt_out.video.i_x_offset) + p_dst_orig->p->i_pitch *
823 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
825 i_src2_pitch = p_src->p->i_pitch;
826 p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
827 i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
829 #define MAX_TRANS 255
831 #define p_trans p_src2
832 #define p_pal p_filter->fmt_in.video.p_palette->palette
833 #define rgbpal rgbpalette.palette
835 /* Convert palette first */
836 for( i_y = 0; i_y < p_filter->fmt_in.video.p_palette->i_entries &&
839 yuv_to_rgb( &r, &g, &b, p_pal[i_y][0], p_pal[i_y][1], p_pal[i_y][2] );
841 if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','1','6') )
843 *(uint16_t *)rgbpal[i_y] =
844 ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
848 rgbpal[i_y][0] = r; rgbpal[i_y][1] = g; rgbpal[i_y][2] = b;
852 /* Draw until we reach the bottom of the subtitle */
853 for( i_y = 0; i_y < i_height; i_y++,
854 p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_pitch )
856 /* Draw until we reach the end of the line */
857 for( i_x = 0; i_x < i_width; i_x++ )
859 i_trans = ( p_pal[p_trans[i_x]][3] * i_alpha ) / 255;
862 /* Completely transparent. Don't change pixel */
865 else if( i_trans == MAX_TRANS ||
866 p_filter->fmt_out.video.i_chroma ==
867 VLC_FOURCC('R','V','1','6') )
869 /* Completely opaque. Completely overwrite underlying pixel */
870 p_dst[i_x * i_pix_pitch] = rgbpal[p_src2[i_x]][0];
871 p_dst[i_x * i_pix_pitch + 1] = rgbpal[p_src2[i_x]][1];
872 if( p_filter->fmt_out.video.i_chroma !=
873 VLC_FOURCC('R','V','1','6') )
874 p_dst[i_x * i_pix_pitch + 2] = rgbpal[p_src2[i_x]][2];
879 p_dst[i_x * i_pix_pitch] = ( (uint16_t)rgbpal[p_src2[i_x]][0] *
880 i_trans + (uint16_t)p_src1[i_x * i_pix_pitch] *
881 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
882 p_dst[i_x * i_pix_pitch + 1] = ( (uint16_t)rgbpal[p_src2[i_x]][1] *
883 i_trans + (uint16_t)p_src1[i_x * i_pix_pitch + 1] *
884 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
885 p_dst[i_x * i_pix_pitch + 2] = ( (uint16_t)rgbpal[p_src2[i_x]][2] *
886 i_trans + (uint16_t)p_src1[i_x * i_pix_pitch + 2] *
887 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
900 /*****************************************************************************
901 * CloseFilter: clean up the filter
902 *****************************************************************************/
903 static void CloseFilter( vlc_object_t *p_this )
905 filter_t *p_filter = (filter_t*)p_this;
906 filter_sys_t *p_sys = p_filter->p_sys;