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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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 BlendYUVPacked( 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 BlendPalYUVPacked( 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('U','Y','V','Y') &&
88 p_filter->fmt_out.video.i_chroma != VLC_FOURCC('R','V','1','6') &&
89 p_filter->fmt_out.video.i_chroma != VLC_FOURCC('R','V','2','4') &&
90 p_filter->fmt_out.video.i_chroma != VLC_FOURCC('R','V','3','2') ) )
95 /* Allocate the memory needed to store the decoder's structure */
96 if( ( p_filter->p_sys = p_sys =
97 (filter_sys_t *)malloc(sizeof(filter_sys_t)) ) == NULL )
99 msg_Err( p_filter, "out of memory" );
104 p_filter->pf_video_blend = Blend;
106 msg_Dbg( p_filter, "chroma: %4.4s -> %4.4s",
107 (char *)&p_filter->fmt_in.video.i_chroma,
108 (char *)&p_filter->fmt_out.video.i_chroma );
114 /****************************************************************************
115 * Blend: the whole thing
116 ****************************************************************************
117 * This function is called just after the thread is launched.
118 ****************************************************************************/
119 static void Blend( filter_t *p_filter, picture_t *p_dst,
120 picture_t *p_dst_orig, picture_t *p_src,
121 int i_x_offset, int i_y_offset, int i_alpha )
123 int i_width, i_height;
125 i_width = __MIN((int)p_filter->fmt_out.video.i_visible_width - i_x_offset,
126 (int)p_filter->fmt_in.video.i_visible_width);
128 i_height = __MIN((int)p_filter->fmt_out.video.i_visible_height -i_y_offset,
129 (int)p_filter->fmt_in.video.i_visible_height);
131 if( i_width <= 0 || i_height <= 0 ) return;
133 if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','A') &&
134 ( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('I','4','2','0') ||
135 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','V','1','2') ) )
137 BlendI420( p_filter, p_dst, p_dst_orig, p_src,
138 i_x_offset, i_y_offset, i_width, i_height, i_alpha );
141 if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','A') &&
142 ( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','U','Y','2') ||
143 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('U','Y','V','Y') ) )
145 BlendYUVPacked( 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') ||
174 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('U','Y','V','Y') ) )
176 BlendPalYUVPacked( p_filter, p_dst, p_dst_orig, p_src,
177 i_x_offset, i_y_offset, i_width, i_height, i_alpha );
180 if( p_filter->fmt_in.video.i_chroma == VLC_FOURCC('Y','U','V','P') &&
181 ( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','1','6') ||
182 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','2','4') ||
183 p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','3','2') ) )
185 BlendPalRV( p_filter, p_dst, p_dst_orig, p_src,
186 i_x_offset, i_y_offset, i_width, i_height, i_alpha );
190 msg_Dbg( p_filter, "no matching alpha blending routine" );
193 static void BlendI420( filter_t *p_filter, picture_t *p_dst,
194 picture_t *p_dst_orig, picture_t *p_src,
195 int i_x_offset, int i_y_offset,
196 int i_width, int i_height, int i_alpha )
198 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
199 uint8_t *p_src1_y, *p_src2_y, *p_dst_y;
200 uint8_t *p_src1_u, *p_src2_u, *p_dst_u;
201 uint8_t *p_src1_v, *p_src2_v, *p_dst_v;
203 int i_x, i_y, i_trans;
204 vlc_bool_t b_even_scanline = i_y_offset % 2;
206 i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
207 p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
208 p_filter->fmt_out.video.i_x_offset +
209 p_dst->p[Y_PLANE].i_pitch *
210 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
211 p_dst_u = p_dst->p[U_PLANE].p_pixels + i_x_offset/2 +
212 p_filter->fmt_out.video.i_x_offset/2 +
213 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
214 p_dst->p[U_PLANE].i_pitch;
215 p_dst_v = p_dst->p[V_PLANE].p_pixels + i_x_offset/2 +
216 p_filter->fmt_out.video.i_x_offset/2 +
217 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
218 p_dst->p[V_PLANE].i_pitch;
220 i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
221 p_src1_y = p_dst_orig->p[Y_PLANE].p_pixels + i_x_offset +
222 p_filter->fmt_out.video.i_x_offset +
223 p_dst_orig->p[Y_PLANE].i_pitch *
224 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
225 p_src1_u = p_dst_orig->p[U_PLANE].p_pixels + i_x_offset/2 +
226 p_filter->fmt_out.video.i_x_offset/2 +
227 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
228 p_dst_orig->p[U_PLANE].i_pitch;
229 p_src1_v = p_dst_orig->p[V_PLANE].p_pixels + i_x_offset/2 +
230 p_filter->fmt_out.video.i_x_offset/2 +
231 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
232 p_dst_orig->p[V_PLANE].i_pitch;
234 i_src2_pitch = p_src->p[Y_PLANE].i_pitch;
235 p_src2_y = p_src->p[Y_PLANE].p_pixels +
236 p_filter->fmt_in.video.i_x_offset +
237 p_src->p[Y_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
238 p_src2_u = p_src->p[U_PLANE].p_pixels +
239 p_filter->fmt_in.video.i_x_offset/2 +
240 p_src->p[U_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
241 p_src2_v = p_src->p[V_PLANE].p_pixels +
242 p_filter->fmt_in.video.i_x_offset/2 +
243 p_src->p[V_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
245 p_trans = p_src->p[A_PLANE].p_pixels +
246 p_filter->fmt_in.video.i_x_offset +
247 p_src->p[A_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
249 #define MAX_TRANS 255
252 /* Draw until we reach the bottom of the subtitle */
253 for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
254 p_dst_y += i_dst_pitch, p_src1_y += i_src1_pitch,
255 p_src2_y += i_src2_pitch,
256 p_dst_u += b_even_scanline ? i_dst_pitch/2 : 0,
257 p_src1_u += b_even_scanline ? i_src1_pitch/2 : 0,
258 p_src2_u += i_src2_pitch,
259 p_dst_v += b_even_scanline ? i_dst_pitch/2 : 0,
260 p_src1_v += b_even_scanline ? i_src1_pitch/2 : 0,
261 p_src2_v += i_src2_pitch )
263 b_even_scanline = !b_even_scanline;
265 /* Draw until we reach the end of the line */
266 for( i_x = 0; i_x < i_width; i_x++ )
268 i_trans = ( p_trans[i_x] * i_alpha ) / 255;
271 /* Completely transparent. Don't change pixel */
274 else if( i_trans == MAX_TRANS )
276 /* Completely opaque. Completely overwrite underlying pixel */
277 p_dst_y[i_x] = p_src2_y[i_x];
279 if( b_even_scanline && i_x % 2 == 0 )
281 p_dst_u[i_x/2] = p_src2_u[i_x];
282 p_dst_v[i_x/2] = p_src2_v[i_x];
288 p_dst_y[i_x] = ( (uint16_t)p_src2_y[i_x] * i_trans +
289 (uint16_t)p_src1_y[i_x] * (MAX_TRANS - i_trans) )
292 if( b_even_scanline && i_x % 2 == 0 )
294 p_dst_u[i_x/2] = ( (uint16_t)p_src2_u[i_x] * i_trans +
295 (uint16_t)p_src1_u[i_x/2] * (MAX_TRANS - i_trans) )
297 p_dst_v[i_x/2] = ( (uint16_t)p_src2_v[i_x] * i_trans +
298 (uint16_t)p_src1_v[i_x/2] * (MAX_TRANS - i_trans) )
310 static inline void yuv_to_rgb( int *r, int *g, int *b,
311 uint8_t y1, uint8_t u1, uint8_t v1 )
313 /* macros used for YUV pixel conversions */
314 # define SCALEBITS 10
315 # define ONE_HALF (1 << (SCALEBITS - 1))
316 # define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
317 # define CLAMP( x ) (((x) > 255) ? 255 : ((x) < 0) ? 0 : (x));
319 int y, cb, cr, r_add, g_add, b_add;
323 r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;
324 g_add = - FIX(0.34414*255.0/224.0) * cb
325 - FIX(0.71414*255.0/224.0) * cr + ONE_HALF;
326 b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;
327 y = (y1 - 16) * FIX(255.0/219.0);
328 *r = CLAMP((y + r_add) >> SCALEBITS);
329 *g = CLAMP((y + g_add) >> SCALEBITS);
330 *b = CLAMP((y + b_add) >> SCALEBITS);
333 static void BlendR16( filter_t *p_filter, picture_t *p_dst_pic,
334 picture_t *p_dst_orig, picture_t *p_src,
335 int i_x_offset, int i_y_offset,
336 int i_width, int i_height, int i_alpha )
338 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
339 uint8_t *p_dst, *p_src1, *p_src2_y;
340 uint8_t *p_src2_u, *p_src2_v;
342 int i_x, i_y, i_pix_pitch;
345 i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
346 i_dst_pitch = p_dst_pic->p->i_pitch;
347 p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
348 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
349 p_dst_pic->p->i_pitch *
350 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
352 i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
353 p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
354 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
355 p_dst_orig->p->i_pitch *
356 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
358 i_src2_pitch = p_src->p[Y_PLANE].i_pitch;
359 p_src2_y = p_src->p[Y_PLANE].p_pixels +
360 p_filter->fmt_in.video.i_x_offset +
361 p_src->p[Y_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
362 p_src2_u = p_src->p[U_PLANE].p_pixels +
363 p_filter->fmt_in.video.i_x_offset/2 +
364 p_src->p[U_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
365 p_src2_v = p_src->p[V_PLANE].p_pixels +
366 p_filter->fmt_in.video.i_x_offset/2 +
367 p_src->p[V_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
369 p_trans = p_src->p[A_PLANE].p_pixels +
370 p_filter->fmt_in.video.i_x_offset +
371 p_src->p[A_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
373 #define MAX_TRANS 255
376 /* Draw until we reach the bottom of the subtitle */
377 for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
378 p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
379 p_src2_y += i_src2_pitch, p_src2_u += i_src2_pitch,
380 p_src2_v += i_src2_pitch )
382 /* Draw until we reach the end of the line */
383 for( i_x = 0; i_x < i_width; i_x++ )
387 /* Completely transparent. Don't change pixel */
390 else if( p_trans[i_x] == MAX_TRANS )
392 /* Completely opaque. Completely overwrite underlying pixel */
393 yuv_to_rgb( &r, &g, &b,
394 p_src2_y[i_x], p_src2_u[i_x], p_src2_v[i_x] );
396 ((uint16_t *)(&p_dst[i_x * i_pix_pitch]))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
401 yuv_to_rgb( &r, &g, &b,
402 p_src2_y[i_x], p_src2_u[i_x], p_src2_v[i_x] );
404 ((uint16_t *)(&p_dst[i_x * i_pix_pitch]))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
414 static void BlendR24( filter_t *p_filter, picture_t *p_dst_pic,
415 picture_t *p_dst_orig, picture_t *p_src,
416 int i_x_offset, int i_y_offset,
417 int i_width, int i_height, int i_alpha )
419 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
420 uint8_t *p_dst, *p_src1, *p_src2_y;
421 uint8_t *p_src2_u, *p_src2_v;
423 int i_x, i_y, i_pix_pitch, i_trans;
426 i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
427 i_dst_pitch = p_dst_pic->p->i_pitch;
428 p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
429 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
430 p_dst_pic->p->i_pitch *
431 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
433 i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
434 p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
435 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
436 p_dst_orig->p->i_pitch *
437 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
439 i_src2_pitch = p_src->p[Y_PLANE].i_pitch;
440 p_src2_y = p_src->p[Y_PLANE].p_pixels +
441 p_filter->fmt_in.video.i_x_offset +
442 p_src->p[Y_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
443 p_src2_u = p_src->p[U_PLANE].p_pixels +
444 p_filter->fmt_in.video.i_x_offset/2 +
445 p_src->p[U_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
446 p_src2_v = p_src->p[V_PLANE].p_pixels +
447 p_filter->fmt_in.video.i_x_offset/2 +
448 p_src->p[V_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
450 p_trans = p_src->p[A_PLANE].p_pixels +
451 p_filter->fmt_in.video.i_x_offset +
452 p_src->p[A_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
454 #define MAX_TRANS 255
457 /* Draw until we reach the bottom of the subtitle */
458 for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
459 p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
460 p_src2_y += i_src2_pitch, p_src2_u += i_src2_pitch,
461 p_src2_v += i_src2_pitch )
463 /* Draw until we reach the end of the line */
464 for( i_x = 0; i_x < i_width; i_x++ )
466 i_trans = ( p_trans[i_x] * i_alpha ) / 255;
469 /* Completely transparent. Don't change pixel */
472 else if( i_trans == MAX_TRANS )
474 /* Completely opaque. Completely overwrite underlying pixel */
475 yuv_to_rgb( &r, &g, &b,
476 p_src2_y[i_x], p_src2_u[i_x], p_src2_v[i_x] );
478 p_dst[i_x * i_pix_pitch] = r;
479 p_dst[i_x * i_pix_pitch + 1] = g;
480 p_dst[i_x * i_pix_pitch + 2] = b;
485 yuv_to_rgb( &r, &g, &b,
486 p_src2_y[i_x], p_src2_u[i_x], p_src2_v[i_x] );
488 p_dst[i_x * i_pix_pitch] = ( r * i_trans +
489 (uint16_t)p_src1[i_x * i_pix_pitch] *
490 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
491 p_dst[i_x * i_pix_pitch + 1] = ( g * i_trans +
492 (uint16_t)p_src1[i_x * i_pix_pitch + 1] *
493 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
494 p_dst[i_x * i_pix_pitch + 2] = ( b * i_trans +
495 (uint16_t)p_src1[i_x * i_pix_pitch + 2] *
496 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
506 static void BlendYUVPacked( filter_t *p_filter, picture_t *p_dst_pic,
507 picture_t *p_dst_orig, picture_t *p_src,
508 int i_x_offset, int i_y_offset,
509 int i_width, int i_height, int i_alpha )
511 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
512 uint8_t *p_dst, *p_src1, *p_src2_y;
513 uint8_t *p_src2_u, *p_src2_v;
515 int i_x, i_y, i_pix_pitch, i_trans;
516 vlc_bool_t b_even = !((i_x_offset + p_filter->fmt_out.video.i_x_offset)%2);
517 int i_u_offset, i_v_offset;
519 if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','U','Y','2') )
524 else if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('U','Y','V','Y') )
531 i_dst_pitch = p_dst_pic->p->i_pitch;
532 p_dst = p_dst_pic->p->p_pixels + i_x_offset * i_pix_pitch +
533 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
534 p_dst_pic->p->i_pitch *
535 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
537 i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
538 p_src1 = p_dst_orig->p->p_pixels + i_x_offset * i_pix_pitch +
539 p_filter->fmt_out.video.i_x_offset * i_pix_pitch +
540 p_dst_orig->p->i_pitch *
541 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
543 i_src2_pitch = p_src->p[Y_PLANE].i_pitch;
544 p_src2_y = p_src->p[Y_PLANE].p_pixels +
545 p_filter->fmt_in.video.i_x_offset +
546 p_src->p[Y_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
547 p_src2_u = p_src->p[U_PLANE].p_pixels +
548 p_filter->fmt_in.video.i_x_offset/2 +
549 p_src->p[U_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
550 p_src2_v = p_src->p[V_PLANE].p_pixels +
551 p_filter->fmt_in.video.i_x_offset/2 +
552 p_src->p[V_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset/2;
554 p_trans = p_src->p[A_PLANE].p_pixels +
555 p_filter->fmt_in.video.i_x_offset +
556 p_src->p[A_PLANE].i_pitch * p_filter->fmt_in.video.i_y_offset;
558 i_width = (i_width >> 1) << 1; /* Needs to be a multiple of 2 */
560 #define MAX_TRANS 255
563 /* Draw until we reach the bottom of the subtitle */
564 for( i_y = 0; i_y < i_height; i_y++, p_trans += i_src2_pitch,
565 p_dst += i_dst_pitch, p_src1 += i_src1_pitch,
566 p_src2_y += i_src2_pitch, p_src2_u += i_src2_pitch,
567 p_src2_v += i_src2_pitch )
569 /* Draw until we reach the end of the line */
570 for( i_x = 0; i_x < i_width; i_x++, b_even = !b_even )
572 i_trans = ( p_trans[i_x] * i_alpha ) / 255;
575 /* Completely transparent. Don't change pixel */
577 else if( i_trans == MAX_TRANS )
579 /* Completely opaque. Completely overwrite underlying pixel */
580 p_dst[i_x * 2] = p_src2_y[i_x];
584 p_dst[i_x * 2 + i_u_offset] = p_src2_u[i_x];
585 p_dst[i_x * 2 + i_v_offset] = p_src2_v[i_x];
591 p_dst[i_x * 2] = ( (uint16_t)p_src2_y[i_x] * i_trans +
592 (uint16_t)p_src1[i_x * 2] * (MAX_TRANS - i_trans) )
597 p_dst[i_x * 2 + i_u_offset] = ( (uint16_t)p_src2_u[i_x] * i_trans +
598 (uint16_t)p_src1[i_x * 2 + i_u_offset] * (MAX_TRANS - i_trans) )
600 p_dst[i_x * 2 + i_v_offset] = ( (uint16_t)p_src2_v[i_x] * i_trans +
601 (uint16_t)p_src1[i_x * 2 + i_v_offset] * (MAX_TRANS - i_trans) )
614 static void BlendPalI420( filter_t *p_filter, picture_t *p_dst,
615 picture_t *p_dst_orig, picture_t *p_src,
616 int i_x_offset, int i_y_offset,
617 int i_width, int i_height, int i_alpha )
619 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
620 uint8_t *p_src1_y, *p_src2, *p_dst_y;
621 uint8_t *p_src1_u, *p_dst_u;
622 uint8_t *p_src1_v, *p_dst_v;
623 int i_x, i_y, i_trans;
624 vlc_bool_t b_even_scanline = i_y_offset % 2;
626 i_dst_pitch = p_dst->p[Y_PLANE].i_pitch;
627 p_dst_y = p_dst->p[Y_PLANE].p_pixels + i_x_offset +
628 p_filter->fmt_out.video.i_x_offset +
629 p_dst->p[Y_PLANE].i_pitch *
630 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
631 p_dst_u = p_dst->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->p[U_PLANE].i_pitch;
635 p_dst_v = p_dst->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->p[V_PLANE].i_pitch;
640 i_src1_pitch = p_dst_orig->p[Y_PLANE].i_pitch;
641 p_src1_y = p_dst_orig->p[Y_PLANE].p_pixels + i_x_offset +
642 p_filter->fmt_out.video.i_x_offset +
643 p_dst_orig->p[Y_PLANE].i_pitch *
644 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
645 p_src1_u = p_dst_orig->p[U_PLANE].p_pixels + i_x_offset/2 +
646 p_filter->fmt_out.video.i_x_offset/2 +
647 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
648 p_dst_orig->p[U_PLANE].i_pitch;
649 p_src1_v = p_dst_orig->p[V_PLANE].p_pixels + i_x_offset/2 +
650 p_filter->fmt_out.video.i_x_offset/2 +
651 ( i_y_offset + p_filter->fmt_out.video.i_y_offset ) / 2 *
652 p_dst_orig->p[V_PLANE].i_pitch;
654 i_src2_pitch = p_src->p->i_pitch;
655 p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
656 i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
658 #define MAX_TRANS 255
660 #define p_trans p_src2
661 #define p_pal p_filter->fmt_in.video.p_palette->palette
663 /* Draw until we reach the bottom of the subtitle */
664 for( i_y = 0; i_y < i_height; i_y++,
665 p_dst_y += i_dst_pitch, p_src1_y += i_src1_pitch,
666 p_src2 += i_src2_pitch,
667 p_dst_u += b_even_scanline ? i_dst_pitch/2 : 0,
668 p_src1_u += b_even_scanline ? i_src1_pitch/2 : 0,
669 p_dst_v += b_even_scanline ? i_dst_pitch/2 : 0,
670 p_src1_v += b_even_scanline ? i_src1_pitch/2 : 0 )
672 b_even_scanline = !b_even_scanline;
674 /* Draw until we reach the end of the line */
675 for( i_x = 0; i_x < i_width; i_x++ )
677 i_trans = ( p_pal[p_trans[i_x]][3] * i_alpha ) / 255;
680 /* Completely transparent. Don't change pixel */
683 else if( i_trans == MAX_TRANS )
685 /* Completely opaque. Completely overwrite underlying pixel */
686 p_dst_y[i_x] = p_pal[p_src2[i_x]][0];
688 if( b_even_scanline && i_x % 2 == 0 )
690 p_dst_u[i_x/2] = p_pal[p_src2[i_x]][1];
691 p_dst_v[i_x/2] = p_pal[p_src2[i_x]][2];
697 p_dst_y[i_x] = ( (uint16_t)p_pal[p_src2[i_x]][0] * i_trans +
698 (uint16_t)p_src1_y[i_x] * (MAX_TRANS - i_trans) )
701 if( b_even_scanline && i_x % 2 == 0 )
703 p_dst_u[i_x/2] = ( (uint16_t)p_pal[p_src2[i_x]][1] * i_trans +
704 (uint16_t)p_src1_u[i_x/2] * (MAX_TRANS - i_trans) )
706 p_dst_v[i_x/2] = ( (uint16_t)p_pal[p_src2[i_x]][2] * i_trans +
707 (uint16_t)p_src1_v[i_x/2] * (MAX_TRANS - i_trans) )
721 static void BlendPalYUVPacked( filter_t *p_filter, picture_t *p_dst_pic,
722 picture_t *p_dst_orig, picture_t *p_src,
723 int i_x_offset, int i_y_offset,
724 int i_width, int i_height, int i_alpha )
726 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
727 uint8_t *p_src1, *p_src2, *p_dst;
728 int i_x, i_y, i_pix_pitch, i_trans;
729 vlc_bool_t b_even = !((i_x_offset + p_filter->fmt_out.video.i_x_offset)%2);
730 int i_u_offset, i_v_offset;
732 if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('Y','U','Y','2') )
737 else if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('U','Y','V','Y') )
744 i_dst_pitch = p_dst_pic->p->i_pitch;
745 p_dst = p_dst_pic->p->p_pixels + i_pix_pitch * (i_x_offset +
746 p_filter->fmt_out.video.i_x_offset) + p_dst_pic->p->i_pitch *
747 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
749 i_src1_pitch = p_dst_orig->p->i_pitch;
750 p_src1 = p_dst_orig->p->p_pixels + i_pix_pitch * (i_x_offset +
751 p_filter->fmt_out.video.i_x_offset) + p_dst_orig->p->i_pitch *
752 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
754 i_src2_pitch = p_src->p->i_pitch;
755 p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
756 i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
758 i_width = (i_width >> 1) << 1; /* Needs to be a multiple of 2 */
760 #define MAX_TRANS 255
762 #define p_trans p_src2
763 #define p_pal p_filter->fmt_in.video.p_palette->palette
765 /* Draw until we reach the bottom of the subtitle */
766 for( i_y = 0; i_y < i_height; i_y++,
767 p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_pitch )
769 /* Draw until we reach the end of the line */
770 for( i_x = 0; i_x < i_width; i_x++, b_even = !b_even )
772 i_trans = ( p_pal[p_trans[i_x]][3] * i_alpha ) / 255;
775 /* Completely transparent. Don't change pixel */
777 else if( i_trans == MAX_TRANS )
779 /* Completely opaque. Completely overwrite underlying pixel */
780 p_dst[i_x * 2] = p_pal[p_src2[i_x]][0];
784 p_dst[i_x * 2 + i_u_offset] = p_pal[p_src2[i_x]][1];
785 p_dst[i_x * 2 + i_v_offset] = p_pal[p_src2[i_x]][2];
791 p_dst[i_x * 2] = ( (uint16_t)p_pal[p_src2[i_x]][0] *
792 i_trans + (uint16_t)p_src1[i_x * 2] *
793 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
797 p_dst[i_x * 2 + i_u_offset] = ( (uint16_t)p_pal[p_src2[i_x]][1] *
798 i_trans + (uint16_t)p_src1[i_x * 2 + i_u_offset] *
799 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
800 p_dst[i_x * 2 + i_v_offset] = ( (uint16_t)p_pal[p_src2[i_x]][2] *
801 i_trans + (uint16_t)p_src1[i_x * 2 + i_v_offset] *
802 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
816 static void BlendPalRV( filter_t *p_filter, picture_t *p_dst_pic,
817 picture_t *p_dst_orig, picture_t *p_src,
818 int i_x_offset, int i_y_offset,
819 int i_width, int i_height, int i_alpha )
821 int i_src1_pitch, i_src2_pitch, i_dst_pitch;
822 uint8_t *p_src1, *p_src2, *p_dst;
823 int i_x, i_y, i_pix_pitch, i_trans;
825 video_palette_t rgbpalette;
827 i_pix_pitch = p_dst_pic->p->i_pixel_pitch;
828 i_dst_pitch = p_dst_pic->p->i_pitch;
829 p_dst = p_dst_pic->p->p_pixels + i_pix_pitch * (i_x_offset +
830 p_filter->fmt_out.video.i_x_offset) + p_dst_pic->p->i_pitch *
831 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
833 i_src1_pitch = p_dst_orig->p->i_pitch;
834 p_src1 = p_dst_orig->p->p_pixels + i_pix_pitch * (i_x_offset +
835 p_filter->fmt_out.video.i_x_offset) + p_dst_orig->p->i_pitch *
836 ( i_y_offset + p_filter->fmt_out.video.i_y_offset );
838 i_src2_pitch = p_src->p->i_pitch;
839 p_src2 = p_src->p->p_pixels + p_filter->fmt_in.video.i_x_offset +
840 i_src2_pitch * p_filter->fmt_in.video.i_y_offset;
842 #define MAX_TRANS 255
844 #define p_trans p_src2
845 #define p_pal p_filter->fmt_in.video.p_palette->palette
846 #define rgbpal rgbpalette.palette
848 /* Convert palette first */
849 for( i_y = 0; i_y < p_filter->fmt_in.video.p_palette->i_entries &&
852 yuv_to_rgb( &r, &g, &b, p_pal[i_y][0], p_pal[i_y][1], p_pal[i_y][2] );
854 if( p_filter->fmt_out.video.i_chroma == VLC_FOURCC('R','V','1','6') )
856 *(uint16_t *)rgbpal[i_y] =
857 ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
861 rgbpal[i_y][0] = r; rgbpal[i_y][1] = g; rgbpal[i_y][2] = b;
865 /* Draw until we reach the bottom of the subtitle */
866 for( i_y = 0; i_y < i_height; i_y++,
867 p_dst += i_dst_pitch, p_src1 += i_src1_pitch, p_src2 += i_src2_pitch )
869 /* Draw until we reach the end of the line */
870 for( i_x = 0; i_x < i_width; i_x++ )
872 i_trans = ( p_pal[p_trans[i_x]][3] * i_alpha ) / 255;
875 /* Completely transparent. Don't change pixel */
878 else if( i_trans == MAX_TRANS ||
879 p_filter->fmt_out.video.i_chroma ==
880 VLC_FOURCC('R','V','1','6') )
882 /* Completely opaque. Completely overwrite underlying pixel */
883 p_dst[i_x * i_pix_pitch] = rgbpal[p_src2[i_x]][0];
884 p_dst[i_x * i_pix_pitch + 1] = rgbpal[p_src2[i_x]][1];
885 if( p_filter->fmt_out.video.i_chroma !=
886 VLC_FOURCC('R','V','1','6') )
887 p_dst[i_x * i_pix_pitch + 2] = rgbpal[p_src2[i_x]][2];
892 p_dst[i_x * i_pix_pitch] = ( (uint16_t)rgbpal[p_src2[i_x]][0] *
893 i_trans + (uint16_t)p_src1[i_x * i_pix_pitch] *
894 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
895 p_dst[i_x * i_pix_pitch + 1] = ( (uint16_t)rgbpal[p_src2[i_x]][1] *
896 i_trans + (uint16_t)p_src1[i_x * i_pix_pitch + 1] *
897 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
898 p_dst[i_x * i_pix_pitch + 2] = ( (uint16_t)rgbpal[p_src2[i_x]][2] *
899 i_trans + (uint16_t)p_src1[i_x * i_pix_pitch + 2] *
900 (MAX_TRANS - i_trans) ) >> TRANS_BITS;
913 /*****************************************************************************
914 * CloseFilter: clean up the filter
915 *****************************************************************************/
916 static void CloseFilter( vlc_object_t *p_this )
918 filter_t *p_filter = (filter_t*)p_this;
919 filter_sys_t *p_sys = p_filter->p_sys;