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_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;
513 vlc_bool_t b_even = !((i_x_offset + p_filter->fmt_out.video.i_x_offset)%2);
516 i_dst_pitch = p_dst_pic->p->i_pitch;
517 p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
518 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
519 p_dst_pic->p->i_pitch *
520 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
522 i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
523 p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
524 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
525 p_dst_orig->p->i_pitch *
526 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
528 i_src2_pitch = p_src->p[Y_PLANE].i_pitch;
529 p_src2_y = p_src->p[Y_PLANE].p_pixels +
530 p_filter->fmt_in.video.i_x_offset +
531 p_src->p[Y_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
532 p_src2_u = p_src->p[U_PLANE].p_pixels +
533 p_filter->fmt_in.video.i_x_offset/2 +
534 p_src->p[U_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
535 p_src2_v = p_src->p[V_PLANE].p_pixels +
536 p_filter->fmt_in.video.i_x_offset/2 +
537 p_src->p[V_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
539 p_trans = p_src->p[A_PLANE].p_pixels +
540 p_filter->fmt_in.video.i_x_offset +
541 p_src->p[A_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
543 i_width = (i_width >> 1) << 1; /* Needs to be a multiple of 2 */
545 #define MAX_TRANS 255
548 /* Draw until we reach the bottom of the subtitle */
549 for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
550 p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
551 p_src2_y += i_src2_pitch, p_src2_u += i_src2_pitch,
552 p_src2_v += i_src2_pitch )
554 /* Draw until we reach the end of the line */
555 for( i_x = 0; i_x < i_width; i_x++, b_even = !b_even )
557 i_trans = ( p_trans[i_x] * i_alpha ) / 255;
560 /* Completely transparent. Don't change pixel */
562 else if( i_trans == MAX_TRANS )
564 /* Completely opaque. Completely overwrite underlying pixel */
565 p_dst[i_x * 2] = p_src2_y[i_x];
569 p_dst[i_x * 2 + 1] = p_src2_u[i_x];
570 p_dst[i_x * 2 + 3] = p_src2_v[i_x];
576 p_dst[i_x * 2] = ( (uint16_t)p_src2_y[i_x] * i_trans +
577 (uint16_t)p_src1[i_x * 2] * (MAX_TRANS - i_trans) )
582 p_dst[i_x * 2 + 1] = ( (uint16_t)p_src2_u[i_x] * i_trans +
583 (uint16_t)p_src1[i_x * 2 + 1] * (MAX_TRANS - i_trans) )
585 p_dst[i_x * 2 + 3] = ( (uint16_t)p_src2_v[i_x] * i_trans +
586 (uint16_t)p_src1[i_x * 2 + 3] * (MAX_TRANS - i_trans) )
599 static void BlendPalI420( filter_t *p_filter, picture_t *p_dst,
600 picture_t *p_dst_orig, picture_t *p_src,
601 int i_x_offset, int i_y_offset,
602 int i_width, int i_height, int i_alpha )
604 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
605 uint8_t *p_src1_y, *p_src2, *p_dst_y;
606 uint8_t *p_src1_u, *p_dst_u;
607 uint8_t *p_src1_v, *p_dst_v;
608 int i_x, i_y, i_trans;
609 vlc_bool_t b_even_scanline = i_y_offset % 2;
611 i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
612 p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
613 p_filter->fmt_out.video.i_x_offset +
614 p_dst->p[Y_PLANE].i_pitch *
615 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
616 p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
617 p_filter->fmt_out.video.i_x_offset/2 +
618 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
619 p_dst->p[U_PLANE].i_pitch;
620 p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
621 p_filter->fmt_out.video.i_x_offset/2 +
622 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
623 p_dst->p[V_PLANE].i_pitch;
625 i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
626 p_src1_y = p_dst_orig->p[Y_PLANE].p_pixels + i_x_offset +
627 p_filter->fmt_out.video.i_x_offset +
628 p_dst_orig->p[Y_PLANE].i_pitch *
629 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
630 p_src1_u = p_dst_orig->p[U_PLANE].p_pixels + i_x_offset/2 +
631 p_filter->fmt_out.video.i_x_offset/2 +
632 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
633 p_dst_orig->p[U_PLANE].i_pitch;
634 p_src1_v = p_dst_orig->p[V_PLANE].p_pixels + i_x_offset/2 +
635 p_filter->fmt_out.video.i_x_offset/2 +
636 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
637 p_dst_orig->p[V_PLANE].i_pitch;
639 i_src2_pitch = p_src->p->i_pitch;
640 p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
641 i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
643 #define MAX_TRANS 255
645 #define p_trans p_src2
646 #define p_pal p_filter->fmt_in.video.p_palette->palette
648 /* Draw until we reach the bottom of the subtitle */
649 for( i_y = 0; i_y < i_height; i_y++,
650 p_dst_y += i_dst_pitch, p_src1_y += i_src1_pitch,
651 p_src2 += i_src2_pitch,
652 p_dst_u += b_even_scanline ? i_dst_pitch/2 : 0,
653 p_src1_u += b_even_scanline ? i_src1_pitch/2 : 0,
654 p_dst_v += b_even_scanline ? i_dst_pitch/2 : 0,
655 p_src1_v += b_even_scanline ? i_src1_pitch/2 : 0 )
657 b_even_scanline = !b_even_scanline;
659 /* Draw until we reach the end of the line */
660 for( i_x = 0; i_x < i_width; i_x++ )
662 i_trans = ( p_pal[p_trans[i_x]][3] * i_alpha ) / 255;
665 /* Completely transparent. Don't change pixel */
668 else if( i_trans == MAX_TRANS )
670 /* Completely opaque. Completely overwrite underlying pixel */
671 p_dst_y[i_x] = p_pal[p_src2[i_x]][0];
673 if( b_even_scanline && i_x % 2 == 0 )
675 p_dst_u[i_x/2] = p_pal[p_src2[i_x]][1];
676 p_dst_v[i_x/2] = p_pal[p_src2[i_x]][2];
682 p_dst_y[i_x] = ( (uint16_t)p_pal[p_src2[i_x]][0] * i_trans +
683 (uint16_t)p_src1_y[i_x] * (MAX_TRANS - i_trans) )
686 if( b_even_scanline && i_x % 2 == 0 )
688 p_dst_u[i_x/2] = ( (uint16_t)p_pal[p_src2[i_x]][1] * i_trans +
689 (uint16_t)p_src1_u[i_x/2] * (MAX_TRANS - i_trans) )
691 p_dst_v[i_x/2] = ( (uint16_t)p_pal[p_src2[i_x]][2] * i_trans +
692 (uint16_t)p_src1_v[i_x/2] * (MAX_TRANS - i_trans) )
706 static void BlendPalYUY2( filter_t *p_filter, picture_t *p_dst_pic,
707 picture_t *p_dst_orig, picture_t *p_src,
708 int i_x_offset, int i_y_offset,
709 int i_width, int i_height, int i_alpha )
711 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
712 uint8_t *p_src1, *p_src2, *p_dst;
713 int i_x, i_y, i_pix_pitch, i_trans;
714 vlc_bool_t b_even = !((i_x_offset + p_filter->fmt_out.video.i_x_offset)%2);
717 i_dst_pitch = p_dst_pic->p->i_pitch;
718 p_dst = p_dst_pic->p->p_pixels + i_pix_pitch * (i_x_offset +
719 p_filter->fmt_out.video.i_x_offset) + p_dst_pic->p->i_pitch *
720 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
722 i_src1_pitch = p_dst_orig->p->i_pitch;
723 p_src1 = p_dst_orig->p->p_pixels + i_pix_pitch * (i_x_offset +
724 p_filter->fmt_out.video.i_x_offset) + p_dst_orig->p->i_pitch *
725 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
727 i_src2_pitch = p_src->p->i_pitch;
728 p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
729 i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
731 i_width = (i_width >> 1) << 1; /* Needs to be a multiple of 2 */
733 #define MAX_TRANS 255
735 #define p_trans p_src2
736 #define p_pal p_filter->fmt_in.video.p_palette->palette
738 /* Draw until we reach the bottom of the subtitle */
739 for( i_y = 0; i_y < i_height; i_y++,
740 p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_pitch )
742 /* Draw until we reach the end of the line */
743 for( i_x = 0; i_x < i_width; i_x++, b_even = !b_even )
745 i_trans = ( p_pal[p_trans[i_x]][3] * i_alpha ) / 255;
748 /* Completely transparent. Don't change pixel */
750 else if( i_trans == MAX_TRANS )
752 /* Completely opaque. Completely overwrite underlying pixel */
753 p_dst[i_x * 2] = p_pal[p_src2[i_x]][0];
757 p_dst[i_x * 2 + 1] = p_pal[p_src2[i_x]][1];
758 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;
770 p_dst[i_x * 2 + 1] = ( (uint16_t)p_pal[p_src2[i_x]][1] *
771 i_trans + (uint16_t)p_src1[i_x * 2 + 1] *
772 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
773 p_dst[i_x * 2 + 3] = ( (uint16_t)p_pal[p_src2[i_x]][2] *
774 i_trans + (uint16_t)p_src1[i_x * 2 + 3] *
775 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
789 static void BlendPalRV( filter_t *p_filter, picture_t *p_dst_pic,
790 picture_t *p_dst_orig, picture_t *p_src,
791 int i_x_offset, int i_y_offset,
792 int i_width, int i_height, int i_alpha )
794 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
795 uint8_t *p_src1, *p_src2, *p_dst;
796 int i_x, i_y, i_pix_pitch, i_trans;
798 video_palette_t rgbpalette;
800 i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
801 i_dst_pitch = p_dst_pic->p->i_pitch;
802 p_dst = p_dst_pic->p->p_pixels + i_pix_pitch * (i_x_offset +
803 p_filter->fmt_out.video.i_x_offset) + p_dst_pic->p->i_pitch *
804 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
806 i_src1_pitch = p_dst_orig->p->i_pitch;
807 p_src1 = p_dst_orig->p->p_pixels + i_pix_pitch * (i_x_offset +
808 p_filter->fmt_out.video.i_x_offset) + p_dst_orig->p->i_pitch *
809 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
811 i_src2_pitch = p_src->p->i_pitch;
812 p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
813 i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
815 #define MAX_TRANS 255
817 #define p_trans p_src2
818 #define p_pal p_filter->fmt_in.video.p_palette->palette
819 #define rgbpal rgbpalette.palette
821 /* Convert palette first */
822 for( i_y = 0; i_y < p_filter->fmt_in.video.p_palette->i_entries &&
825 yuv_to_rgb( &r, &g, &b, p_pal[i_y][0], p_pal[i_y][1], p_pal[i_y][2] );
827 if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','1','6') )
829 *(uint16_t *)rgbpal[i_y] =
830 ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
834 rgbpal[i_y][0] = r; rgbpal[i_y][1] = g; rgbpal[i_y][2] = b;
838 /* Draw until we reach the bottom of the subtitle */
839 for( i_y = 0; i_y < i_height; i_y++,
840 p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_pitch )
842 /* Draw until we reach the end of the line */
843 for( i_x = 0; i_x < i_width; i_x++ )
845 i_trans = ( p_pal[p_trans[i_x]][3] * i_alpha ) / 255;
848 /* Completely transparent. Don't change pixel */
851 else if( i_trans == MAX_TRANS ||
852 p_filter->fmt_out.video.i_chroma ==
853 VLC_FOURCC('R','V','1','6') )
855 /* Completely opaque. Completely overwrite underlying pixel */
856 p_dst[i_x * i_pix_pitch] = rgbpal[p_src2[i_x]][0];
857 p_dst[i_x * i_pix_pitch + 1] = rgbpal[p_src2[i_x]][1];
858 if( p_filter->fmt_out.video.i_chroma !=
859 VLC_FOURCC('R','V','1','6') )
860 p_dst[i_x * i_pix_pitch + 2] = rgbpal[p_src2[i_x]][2];
865 p_dst[i_x * i_pix_pitch] = ( (uint16_t)rgbpal[p_src2[i_x]][0] *
866 i_trans + (uint16_t)p_src1[i_x * i_pix_pitch] *
867 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
868 p_dst[i_x * i_pix_pitch + 1] = ( (uint16_t)rgbpal[p_src2[i_x]][1] *
869 i_trans + (uint16_t)p_src1[i_x * i_pix_pitch + 1] *
870 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
871 p_dst[i_x * i_pix_pitch + 2] = ( (uint16_t)rgbpal[p_src2[i_x]][2] *
872 i_trans + (uint16_t)p_src1[i_x * i_pix_pitch + 2] *
873 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
886 /*****************************************************************************
887 * CloseFilter: clean up the filter
888 *****************************************************************************/
889 static void CloseFilter( vlc_object_t *p_this )
891 filter_t *p_filter = (filter_t*)p_this;
892 filter_sys_t *p_sys = p_filter->p_sys;