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 *****************************************************************************/
38 /****************************************************************************
40 ****************************************************************************/
41 static int OpenFilter ( vlc_object_t * );
42 static void CloseFilter( vlc_object_t * );
44 static void Blend( filter_t *, picture_t *, picture_t *, picture_t *,
46 static void BlendI420( filter_t *, picture_t *, picture_t *, picture_t *,
48 static void BlendR16( filter_t *, picture_t *, picture_t *, picture_t *,
50 static void BlendR24( filter_t *, picture_t *, picture_t *, picture_t *,
52 static void BlendYUY2( filter_t *, picture_t *, picture_t *, picture_t *,
54 static void BlendPalI420( filter_t *, picture_t *, picture_t *, picture_t *,
56 static void BlendPalYUY2( filter_t *, picture_t *, picture_t *, picture_t *,
58 static void BlendPalRV( filter_t *, picture_t *, picture_t *, picture_t *,
61 /*****************************************************************************
63 *****************************************************************************/
65 set_description( _("Video pictures blending") );
66 set_capability( "video blending", 100 );
67 set_callbacks( OpenFilter, CloseFilter );
70 /*****************************************************************************
71 * OpenFilter: probe the filter and return score
72 *****************************************************************************/
73 static int OpenFilter( vlc_object_t *p_this )
75 filter_t *p_filter = (filter_t*)p_this;
78 /* Check if we can handle that format.
79 * We could try to use a chroma filter if we can't. */
80 if( ( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','A') &&
81 p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','P') ) ||
82 ( p_filter->fmt_out.video.i_chroma != VLC_FOURCC('I','4','2','0') &&
83 p_filter->fmt_out.video.i_chroma != VLC_FOURCC('Y','U','Y','2') &&
84 p_filter->fmt_out.video.i_chroma != VLC_FOURCC('Y','V','1','2') &&
85 p_filter->fmt_out.video.i_chroma != VLC_FOURCC('R','V','1','6') &&
86 p_filter->fmt_out.video.i_chroma != VLC_FOURCC('R','V','2','4') &&
87 p_filter->fmt_out.video.i_chroma != VLC_FOURCC('R','V','3','2') ) )
92 /* Allocate the memory needed to store the decoder's structure */
93 if( ( p_filter->p_sys = p_sys =
94 (filter_sys_t *)malloc(sizeof(filter_sys_t)) ) == NULL )
96 msg_Err( p_filter, "out of memory" );
101 p_filter->pf_video_blend = Blend;
103 msg_Dbg( p_filter, "chroma: %4.4s -> %4.4s",
104 (char *)&p_filter->fmt_in.video.i_chroma,
105 (char *)&p_filter->fmt_out.video.i_chroma );
111 /****************************************************************************
112 * Blend: the whole thing
113 ****************************************************************************
114 * This function is called just after the thread is launched.
115 ****************************************************************************/
116 static void Blend( filter_t *p_filter, picture_t *p_dst,
117 picture_t *p_dst_orig, picture_t *p_src,
118 int i_x_offset, int i_y_offset )
120 int i_width, i_height;
122 i_width = __MIN((int)p_filter->fmt_out.video.i_visible_width - i_x_offset,
123 (int)p_filter->fmt_in.video.i_visible_width);
125 i_height = __MIN((int)p_filter->fmt_out.video.i_visible_height -i_y_offset,
126 (int)p_filter->fmt_in.video.i_visible_height);
128 if( i_width <= 0 || i_height <= 0 ) return;
130 if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','A') &&
131 ( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('I','4','2','0') ||
132 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','V','1','2') ) )
134 BlendI420( p_filter, p_dst, p_dst_orig, p_src,
135 i_x_offset, i_y_offset, i_width, i_height );
138 if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','A') &&
139 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','U','Y','2') )
141 BlendYUY2( p_filter, p_dst, p_dst_orig, p_src,
142 i_x_offset, i_y_offset, i_width, i_height );
145 if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','A') &&
146 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','1','6') )
148 BlendR16( p_filter, p_dst, p_dst_orig, p_src,
149 i_x_offset, i_y_offset, i_width, i_height );
152 if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','A') &&
153 ( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','2','4') ||
154 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','3','2') ) )
156 BlendR24( p_filter, p_dst, p_dst_orig, p_src,
157 i_x_offset, i_y_offset, i_width, i_height );
160 if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','P') &&
161 ( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('I','4','2','0') ||
162 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','V','1','2') ) )
164 BlendPalI420( p_filter, p_dst, p_dst_orig, p_src,
165 i_x_offset, i_y_offset, i_width, i_height );
168 if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','P') &&
169 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','U','Y','2') )
171 BlendPalYUY2( p_filter, p_dst, p_dst_orig, p_src,
172 i_x_offset, i_y_offset, i_width, i_height );
175 if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','P') &&
176 ( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','1','6') ||
177 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','2','4') ||
178 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','3','2') ) )
180 BlendPalRV( p_filter, p_dst, p_dst_orig, p_src,
181 i_x_offset, i_y_offset, i_width, i_height );
185 msg_Dbg( p_filter, "no matching alpha blending routine" );
188 static void BlendI420( filter_t *p_filter, picture_t *p_dst,
189 picture_t *p_dst_orig, picture_t *p_src,
190 int i_x_offset, int i_y_offset,
191 int i_width, int i_height )
193 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
194 uint8_t *p_src1_y, *p_src2_y, *p_dst_y;
195 uint8_t *p_src1_u, *p_src2_u, *p_dst_u;
196 uint8_t *p_src1_v, *p_src2_v, *p_dst_v;
199 vlc_bool_t b_even_scanline = i_y_offset % 2;
201 i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
202 p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
203 p_filter->fmt_out.video.i_x_offset +
204 p_dst->p[Y_PLANE].i_pitch *
205 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
206 p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
207 p_filter->fmt_out.video.i_x_offset/2 +
208 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
209 p_dst->p[U_PLANE].i_pitch;
210 p_dst_v = p_dst->p[V_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[V_PLANE].i_pitch;
215 i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
216 p_src1_y = p_dst_orig->p[Y_PLANE].p_pixels + i_x_offset +
217 p_filter->fmt_out.video.i_x_offset +
218 p_dst_orig->p[Y_PLANE].i_pitch *
219 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
220 p_src1_u = p_dst_orig->p[U_PLANE].p_pixels + i_x_offset/2 +
221 p_filter->fmt_out.video.i_x_offset/2 +
222 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
223 p_dst_orig->p[U_PLANE].i_pitch;
224 p_src1_v = p_dst_orig->p[V_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[V_PLANE].i_pitch;
229 i_src2_pitch = p_src->p[Y_PLANE].i_pitch;
230 p_src2_y = p_src->p[Y_PLANE].p_pixels +
231 p_filter->fmt_in.video.i_x_offset +
232 p_src->p[Y_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
233 p_src2_u = p_src->p[U_PLANE].p_pixels +
234 p_filter->fmt_in.video.i_x_offset/2 +
235 p_src->p[U_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
236 p_src2_v = p_src->p[V_PLANE].p_pixels +
237 p_filter->fmt_in.video.i_x_offset/2 +
238 p_src->p[V_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
240 p_trans = p_src->p[A_PLANE].p_pixels +
241 p_filter->fmt_in.video.i_x_offset +
242 p_src->p[A_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
244 #define MAX_TRANS 255
247 /* Draw until we reach the bottom of the subtitle */
248 for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
249 p_dst_y += i_dst_pitch, p_src1_y += i_src1_pitch,
250 p_src2_y += i_src2_pitch,
251 p_dst_u += b_even_scanline ? i_dst_pitch/2 : 0,
252 p_src1_u += b_even_scanline ? i_src1_pitch/2 : 0,
253 p_src2_u += i_src2_pitch,
254 p_dst_v += b_even_scanline ? i_dst_pitch/2 : 0,
255 p_src1_v += b_even_scanline ? i_src1_pitch/2 : 0,
256 p_src2_v += i_src2_pitch )
258 b_even_scanline = !b_even_scanline;
260 /* Draw until we reach the end of the line */
261 for( i_x = 0; i_x < i_width; i_x++ )
265 /* Completely transparent. Don't change pixel */
268 else if( p_trans[i_x] == MAX_TRANS )
270 /* Completely opaque. Completely overwrite underlying pixel */
271 p_dst_y[i_x] = p_src2_y[i_x];
273 if( b_even_scanline && i_x % 2 == 0 )
275 p_dst_u[i_x/2] = p_src2_u[i_x];
276 p_dst_v[i_x/2] = p_src2_v[i_x];
282 p_dst_y[i_x] = ( (uint16_t)p_src2_y[i_x] * p_trans[i_x] +
283 (uint16_t)p_src1_y[i_x] * (MAX_TRANS - p_trans[i_x]) )
286 if( b_even_scanline && i_x % 2 == 0 )
288 p_dst_u[i_x/2] = ( (uint16_t)p_src2_u[i_x] * p_trans[i_x] +
289 (uint16_t)p_src1_u[i_x/2] * (MAX_TRANS - p_trans[i_x]) )
291 p_dst_v[i_x/2] = ( (uint16_t)p_src2_v[i_x] * p_trans[i_x] +
292 (uint16_t)p_src1_v[i_x/2] * (MAX_TRANS - p_trans[i_x]) )
304 static inline void yuv_to_rgb( int *r, int *g, int *b,
305 uint8_t y1, uint8_t u1, uint8_t v1 )
307 /* macros used for YUV pixel conversions */
308 # define SCALEBITS 10
309 # define ONE_HALF (1 << (SCALEBITS - 1))
310 # define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
311 # define CLAMP( x ) (((x) > 255) ? 255 : ((x) < 0) ? 0 : (x));
313 int y, cb, cr, r_add, g_add, b_add;
317 r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;
318 g_add = - FIX(0.34414*255.0/224.0) * cb
319 - FIX(0.71414*255.0/224.0) * cr + ONE_HALF;
320 b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;
321 y = (y1 - 16) * FIX(255.0/219.0);
322 *r = CLAMP((y + r_add) >> SCALEBITS);
323 *g = CLAMP((y + g_add) >> SCALEBITS);
324 *b = CLAMP((y + b_add) >> SCALEBITS);
327 static void BlendR16( filter_t *p_filter, picture_t *p_dst_pic,
328 picture_t *p_dst_orig, picture_t *p_src,
329 int i_x_offset, int i_y_offset,
330 int i_width, int i_height )
332 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
333 uint8_t *p_dst, *p_src1, *p_src2_y;
334 uint8_t *p_src2_u, *p_src2_v;
336 int i_x, i_y, i_pix_pitch;
339 i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
340 i_dst_pitch = p_dst_pic->p->i_pitch;
341 p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
342 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
343 p_dst_pic->p->i_pitch *
344 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
346 i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
347 p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
348 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
349 p_dst_orig->p->i_pitch *
350 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
352 i_src2_pitch = p_src->p[Y_PLANE].i_pitch;
353 p_src2_y = p_src->p[Y_PLANE].p_pixels +
354 p_filter->fmt_in.video.i_x_offset +
355 p_src->p[Y_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
356 p_src2_u = p_src->p[U_PLANE].p_pixels +
357 p_filter->fmt_in.video.i_x_offset/2 +
358 p_src->p[U_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
359 p_src2_v = p_src->p[V_PLANE].p_pixels +
360 p_filter->fmt_in.video.i_x_offset/2 +
361 p_src->p[V_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
363 p_trans = p_src->p[A_PLANE].p_pixels +
364 p_filter->fmt_in.video.i_x_offset +
365 p_src->p[A_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
367 #define MAX_TRANS 255
370 /* Draw until we reach the bottom of the subtitle */
371 for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
372 p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
373 p_src2_y += i_src2_pitch, p_src2_u += i_src2_pitch,
374 p_src2_v += i_src2_pitch )
376 /* Draw until we reach the end of the line */
377 for( i_x = 0; i_x < i_width; i_x++ )
381 /* Completely transparent. Don't change pixel */
384 else if( p_trans[i_x] == MAX_TRANS )
386 /* Completely opaque. Completely overwrite underlying pixel */
387 yuv_to_rgb( &r, &g, &b,
388 p_src2_y[i_x], p_src2_u[i_x], p_src2_v[i_x] );
390 ((uint16_t *)(&p_dst[i_x * i_pix_pitch]))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
395 yuv_to_rgb( &r, &g, &b,
396 p_src2_y[i_x], p_src2_u[i_x], p_src2_v[i_x] );
398 ((uint16_t *)(&p_dst[i_x * i_pix_pitch]))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
408 static void BlendR24( filter_t *p_filter, picture_t *p_dst_pic,
409 picture_t *p_dst_orig, picture_t *p_src,
410 int i_x_offset, int i_y_offset,
411 int i_width, int i_height )
413 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
414 uint8_t *p_dst, *p_src1, *p_src2_y;
415 uint8_t *p_src2_u, *p_src2_v;
417 int i_x, i_y, i_pix_pitch;
420 i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
421 i_dst_pitch = p_dst_pic->p->i_pitch;
422 p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
423 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
424 p_dst_pic->p->i_pitch *
425 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
427 i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
428 p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
429 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
430 p_dst_orig->p->i_pitch *
431 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
433 i_src2_pitch = p_src->p[Y_PLANE].i_pitch;
434 p_src2_y = p_src->p[Y_PLANE].p_pixels +
435 p_filter->fmt_in.video.i_x_offset +
436 p_src->p[Y_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
437 p_src2_u = p_src->p[U_PLANE].p_pixels +
438 p_filter->fmt_in.video.i_x_offset/2 +
439 p_src->p[U_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
440 p_src2_v = p_src->p[V_PLANE].p_pixels +
441 p_filter->fmt_in.video.i_x_offset/2 +
442 p_src->p[V_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
444 p_trans = p_src->p[A_PLANE].p_pixels +
445 p_filter->fmt_in.video.i_x_offset +
446 p_src->p[A_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
448 #define MAX_TRANS 255
451 /* Draw until we reach the bottom of the subtitle */
452 for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
453 p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
454 p_src2_y += i_src2_pitch, p_src2_u += i_src2_pitch,
455 p_src2_v += i_src2_pitch )
457 /* Draw until we reach the end of the line */
458 for( i_x = 0; i_x < i_width; i_x++ )
462 /* Completely transparent. Don't change pixel */
465 else if( p_trans[i_x] == MAX_TRANS )
467 /* Completely opaque. Completely overwrite underlying pixel */
468 yuv_to_rgb( &r, &g, &b,
469 p_src2_y[i_x], p_src2_u[i_x], p_src2_v[i_x] );
471 p_dst[i_x * i_pix_pitch] = r;
472 p_dst[i_x * i_pix_pitch + 1] = g;
473 p_dst[i_x * i_pix_pitch + 2] = b;
478 yuv_to_rgb( &r, &g, &b,
479 p_src2_y[i_x], p_src2_u[i_x], p_src2_v[i_x] );
481 p_dst[i_x * i_pix_pitch] = ( r * p_trans[i_x] +
482 (uint16_t)p_src1[i_x * i_pix_pitch] *
483 (MAX_TRANS - p_trans[i_x]) ) >> TRANS_BITS;
484 p_dst[i_x * i_pix_pitch + 1] = ( g * p_trans[i_x] +
485 (uint16_t)p_src1[i_x * i_pix_pitch + 1] *
486 (MAX_TRANS - p_trans[i_x]) ) >> TRANS_BITS;
487 p_dst[i_x * i_pix_pitch + 2] = ( b * p_trans[i_x] +
488 (uint16_t)p_src1[i_x * i_pix_pitch + 2] *
489 (MAX_TRANS - p_trans[i_x]) ) >> TRANS_BITS;
499 static void BlendYUY2( filter_t *p_filter, picture_t *p_dst_pic,
500 picture_t *p_dst_orig, picture_t *p_src,
501 int i_x_offset, int i_y_offset,
502 int i_width, int i_height )
504 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
505 uint8_t *p_dst, *p_src1, *p_src2_y;
506 uint8_t *p_src2_u, *p_src2_v;
508 int i_x, i_y, i_pix_pitch;
511 i_dst_pitch = p_dst_pic->p->i_pitch;
512 p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
513 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
514 p_dst_pic->p->i_pitch *
515 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
517 i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
518 p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
519 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
520 p_dst_orig->p->i_pitch *
521 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
523 i_src2_pitch = p_src->p[Y_PLANE].i_pitch;
524 p_src2_y = p_src->p[Y_PLANE].p_pixels +
525 p_filter->fmt_in.video.i_x_offset +
526 p_src->p[Y_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
527 p_src2_u = p_src->p[U_PLANE].p_pixels +
528 p_filter->fmt_in.video.i_x_offset/2 +
529 p_src->p[U_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
530 p_src2_v = p_src->p[V_PLANE].p_pixels +
531 p_filter->fmt_in.video.i_x_offset/2 +
532 p_src->p[V_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
534 p_trans = p_src->p[A_PLANE].p_pixels +
535 p_filter->fmt_in.video.i_x_offset +
536 p_src->p[A_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
538 #define MAX_TRANS 255
541 /* Draw until we reach the bottom of the subtitle */
542 for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
543 p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
544 p_src2_y += i_src2_pitch, p_src2_u += i_src2_pitch,
545 p_src2_v += i_src2_pitch )
547 /* Draw until we reach the end of the line */
548 for( i_x = 0; i_x < i_width; i_x += 2 )
552 /* Completely transparent. Don't change pixel */
554 else if( p_trans[i_x] == MAX_TRANS )
556 /* Completely opaque. Completely overwrite underlying pixel */
557 p_dst[i_x * 2] = p_src2_y[i_x];
558 p_dst[i_x * 2 + 1] = p_src2_u[i_x];
559 p_dst[i_x * 2 + 3] = p_src2_v[i_x];
564 p_dst[i_x * 2] = ( (uint16_t)p_src2_y[i_x] * p_trans[i_x] +
565 (uint16_t)p_src1[i_x * 2] *
566 (MAX_TRANS - p_trans[i_x]) ) >> TRANS_BITS;
567 p_dst[i_x * 2 + 1] = ( (uint16_t)p_src2_u[i_x] * p_trans[i_x] +
568 (uint16_t)p_src1[i_x * 2 + 1] *
569 (MAX_TRANS - p_trans[i_x]) ) >> TRANS_BITS;
570 p_dst[i_x * 2 + 3] = ( (uint16_t)p_src2_v[i_x] * p_trans[i_x] +
571 (uint16_t)p_src1[i_x * 2 + 3] *
572 (MAX_TRANS - p_trans[i_x]) ) >> TRANS_BITS;
575 if( !p_trans[i_x+1] )
577 /* Completely transparent. Don't change pixel */
579 else if( p_trans[i_x+1] == MAX_TRANS )
581 /* Completely opaque. Completely overwrite underlying pixel */
582 p_dst[i_x * 2 + 2] = p_src2_y[i_x + 1];
587 p_dst[i_x * 2 + 2] = ( (uint16_t)p_src2_y[i_x+1] *
588 p_trans[i_x+1] + (uint16_t)p_src1[i_x * 2 + 2] *
589 (MAX_TRANS - p_trans[i_x+1]) ) >> TRANS_BITS;
600 static void BlendPalI420( filter_t *p_filter, picture_t *p_dst,
601 picture_t *p_dst_orig, picture_t *p_src,
602 int i_x_offset, int i_y_offset,
603 int i_width, int i_height )
605 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
606 uint8_t *p_src1_y, *p_src2, *p_dst_y;
607 uint8_t *p_src1_u, *p_dst_u;
608 uint8_t *p_src1_v, *p_dst_v;
610 vlc_bool_t b_even_scanline = i_y_offset % 2;
612 i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
613 p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
614 p_filter->fmt_out.video.i_x_offset +
615 p_dst->p[Y_PLANE].i_pitch *
616 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
617 p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
618 p_filter->fmt_out.video.i_x_offset/2 +
619 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
620 p_dst->p[U_PLANE].i_pitch;
621 p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
622 p_filter->fmt_out.video.i_x_offset/2 +
623 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
624 p_dst->p[V_PLANE].i_pitch;
626 i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
627 p_src1_y = p_dst_orig->p[Y_PLANE].p_pixels + i_x_offset +
628 p_filter->fmt_out.video.i_x_offset +
629 p_dst_orig->p[Y_PLANE].i_pitch *
630 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
631 p_src1_u = p_dst_orig->p[U_PLANE].p_pixels + i_x_offset/2 +
632 p_filter->fmt_out.video.i_x_offset/2 +
633 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
634 p_dst_orig->p[U_PLANE].i_pitch;
635 p_src1_v = p_dst_orig->p[V_PLANE].p_pixels + i_x_offset/2 +
636 p_filter->fmt_out.video.i_x_offset/2 +
637 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
638 p_dst_orig->p[V_PLANE].i_pitch;
640 i_src2_pitch = p_src->p->i_pitch;
641 p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
642 i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
644 #define MAX_TRANS 255
646 #define p_trans p_src2
647 #define p_pal p_filter->fmt_in.video.p_palette->palette
649 /* Draw until we reach the bottom of the subtitle */
650 for( i_y = 0; i_y < i_height; i_y++,
651 p_dst_y += i_dst_pitch, p_src1_y += i_src1_pitch,
652 p_src2 += i_src2_pitch,
653 p_dst_u += b_even_scanline ? i_dst_pitch/2 : 0,
654 p_src1_u += b_even_scanline ? i_src1_pitch/2 : 0,
655 p_dst_v += b_even_scanline ? i_dst_pitch/2 : 0,
656 p_src1_v += b_even_scanline ? i_src1_pitch/2 : 0 )
658 b_even_scanline = !b_even_scanline;
660 /* Draw until we reach the end of the line */
661 for( i_x = 0; i_x < i_width; i_x++ )
663 if( !p_pal[p_trans[i_x]][3] )
665 /* Completely transparent. Don't change pixel */
668 else if( p_pal[p_trans[i_x]][3] == 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] *
683 p_pal[p_trans[i_x]][3] + (uint16_t)p_src1_y[i_x] *
684 (MAX_TRANS - p_pal[p_trans[i_x]][3]) ) >> TRANS_BITS;
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] *
689 p_pal[p_trans[i_x]][3] + (uint16_t)p_src1_u[i_x/2] *
690 (MAX_TRANS - p_pal[p_trans[i_x]][3]) ) >> TRANS_BITS;
691 p_dst_v[i_x/2] = ( (uint16_t)p_pal[p_src2[i_x]][2] *
692 p_pal[p_trans[i_x]][3] + (uint16_t)p_src1_v[i_x/2] *
693 (MAX_TRANS - p_pal[p_trans[i_x]][3]) ) >> TRANS_BITS;
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 )
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;
716 i_dst_pitch = p_dst_pic->p->i_pitch;
717 p_dst = p_dst_pic->p->p_pixels + i_pix_pitch * (i_x_offset +
718 p_filter->fmt_out.video.i_x_offset) + p_dst_pic->p->i_pitch *
719 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
721 i_src1_pitch = p_dst_orig->p->i_pitch;
722 p_src1 = p_dst_orig->p->p_pixels + i_pix_pitch * (i_x_offset +
723 p_filter->fmt_out.video.i_x_offset) + p_dst_orig->p->i_pitch *
724 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
726 i_src2_pitch = p_src->p->i_pitch;
727 p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
728 i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
730 #define MAX_TRANS 255
732 #define p_trans p_src2
733 #define p_pal p_filter->fmt_in.video.p_palette->palette
735 /* Draw until we reach the bottom of the subtitle */
736 for( i_y = 0; i_y < i_height; i_y++,
737 p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_pitch )
739 /* Draw until we reach the end of the line */
740 for( i_x = 0; i_x < i_width; i_x += 2 )
742 if( !p_pal[p_trans[i_x]][3] )
744 /* Completely transparent. Don't change pixel */
746 else if( p_pal[p_trans[i_x]][3] == MAX_TRANS )
748 /* Completely opaque. Completely overwrite underlying pixel */
749 p_dst[i_x * 2] = p_pal[p_src2[i_x]][0];
750 p_dst[i_x * 2 + 1] = p_pal[p_src2[i_x]][1];
751 p_dst[i_x * 2 + 3] = p_pal[p_src2[i_x]][2];
756 p_dst[i_x * 2] = ( (uint16_t)p_pal[p_src2[i_x]][0] *
757 p_pal[p_trans[i_x]][3] + (uint16_t)p_src1[i_x * 2] *
758 (MAX_TRANS - p_pal[p_trans[i_x]][3]) ) >> TRANS_BITS;
759 p_dst[i_x * 2 + 1] = ( (uint16_t)p_pal[p_src2[i_x]][1] *
760 p_pal[p_trans[i_x]][3] + (uint16_t)p_src1[i_x * 2 + 1] *
761 (MAX_TRANS - p_pal[p_trans[i_x]][3]) ) >> TRANS_BITS;
762 p_dst[i_x * 2 + 3] = ( (uint16_t)p_pal[p_src2[i_x]][2] *
763 p_pal[p_trans[i_x]][3] + (uint16_t)p_src1[i_x * 2 + 3] *
764 (MAX_TRANS - p_pal[p_trans[i_x]][3]) ) >> TRANS_BITS;
767 if( !p_pal[p_trans[i_x+1]][3] )
769 /* Completely transparent. Don't change pixel */
771 else if( p_pal[p_trans[i_x+1]][3] == MAX_TRANS )
773 /* Completely opaque. Completely overwrite underlying pixel */
774 p_dst[i_x * 2 + 2] = p_pal[p_src2[i_x + 1]][0];
779 p_dst[i_x * 2 + 2] = ( (uint16_t)p_pal[p_src2[i_x+1]][0] *
780 p_pal[p_trans[i_x+1]][3] + (uint16_t)p_src1[i_x * 2 + 2] *
781 (MAX_TRANS - p_pal[p_trans[i_x+1]][3]) ) >> TRANS_BITS;
794 static void BlendPalRV( filter_t *p_filter, picture_t *p_dst_pic,
795 picture_t *p_dst_orig, picture_t *p_src,
796 int i_x_offset, int i_y_offset,
797 int i_width, int i_height )
799 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
800 uint8_t *p_src1, *p_src2, *p_dst;
801 int i_x, i_y, i_pix_pitch;
803 video_palette_t rgbpalette;
805 i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
806 i_dst_pitch = p_dst_pic->p->i_pitch;
807 p_dst = p_dst_pic->p->p_pixels + i_pix_pitch * (i_x_offset +
808 p_filter->fmt_out.video.i_x_offset) + p_dst_pic->p->i_pitch *
809 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
811 i_src1_pitch = p_dst_orig->p->i_pitch;
812 p_src1 = p_dst_orig->p->p_pixels + i_pix_pitch * (i_x_offset +
813 p_filter->fmt_out.video.i_x_offset) + p_dst_orig->p->i_pitch *
814 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
816 i_src2_pitch = p_src->p->i_pitch;
817 p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
818 i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
820 #define MAX_TRANS 255
822 #define p_trans p_src2
823 #define p_pal p_filter->fmt_in.video.p_palette->palette
824 #define rgbpal rgbpalette.palette
826 /* Convert palette first */
827 for( i_y = 0; i_y < p_filter->fmt_in.video.p_palette->i_entries &&
830 yuv_to_rgb( &r, &g, &b, p_pal[i_y][0], p_pal[i_y][1], p_pal[i_y][2] );
832 if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','1','6') )
834 *(uint16_t *)rgbpal[i_y] =
835 ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
839 rgbpal[i_y][0] = r; rgbpal[i_y][1] = g; rgbpal[i_y][2] = b;
843 /* Draw until we reach the bottom of the subtitle */
844 for( i_y = 0; i_y < i_height; i_y++,
845 p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_pitch )
847 /* Draw until we reach the end of the line */
848 for( i_x = 0; i_x < i_width; i_x++ )
850 if( !p_pal[p_trans[i_x]][3] )
852 /* Completely transparent. Don't change pixel */
855 else if( p_pal[p_trans[i_x]][3] == MAX_TRANS ||
856 p_filter->fmt_out.video.i_chroma ==
857 VLC_FOURCC('R','V','1','6') )
859 /* Completely opaque. Completely overwrite underlying pixel */
860 p_dst[i_x * i_pix_pitch] = rgbpal[p_src2[i_x]][0];
861 p_dst[i_x * i_pix_pitch + 1] = rgbpal[p_src2[i_x]][1];
862 if( p_filter->fmt_out.video.i_chroma !=
863 VLC_FOURCC('R','V','1','6') )
864 p_dst[i_x * i_pix_pitch + 2] = rgbpal[p_src2[i_x]][2];
869 p_dst[i_x * i_pix_pitch] = ( (uint16_t)rgbpal[p_src2[i_x]][0] *
870 p_pal[p_trans[i_x]][3] +
871 (uint16_t)p_src1[i_x * i_pix_pitch] *
872 (MAX_TRANS - p_pal[p_trans[i_x]][3]) ) >> TRANS_BITS;
873 p_dst[i_x * i_pix_pitch + 1] = ( (uint16_t)rgbpal[p_src2[i_x]][1] *
874 p_pal[p_trans[i_x]][3] +
875 (uint16_t)p_src1[i_x * i_pix_pitch + 1] *
876 (MAX_TRANS - p_pal[p_trans[i_x]][3]) ) >> TRANS_BITS;
877 p_dst[i_x * i_pix_pitch + 2] = ( (uint16_t)rgbpal[p_src2[i_x]][2] *
878 p_pal[p_trans[i_x]][3] +
879 (uint16_t)p_src1[i_x * i_pix_pitch + 2] *
880 (MAX_TRANS - p_pal[p_trans[i_x]][3]) ) >> TRANS_BITS;
893 /*****************************************************************************
894 * CloseFilter: clean up the filter
895 *****************************************************************************/
896 static void CloseFilter( vlc_object_t *p_this )
898 filter_t *p_filter = (filter_t*)p_this;
899 filter_sys_t *p_sys = p_filter->p_sys;