kwallet: use vlc_memstream
[vlc.git] / modules / video_filter / oldmovie.c
blob0f1d22aa7f7339cc4165b149c0a37e91d0d809cb
1 /*****************************************************************************
2 * oldmovie.c : Old movie effect video filter
3 *****************************************************************************
4 * Copyright (C) 2013 Vianney Boyer
5 * $Id$
7 * Authors: Vianney Boyer <vlcvboyer -at- gmail -dot- com>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <math.h>
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_filter.h>
37 #include <vlc_rand.h>
38 #include <vlc_mtime.h>
40 #include "filter_picture.h"
42 static inline int64_t MOD(int64_t a, int64_t b) {
43 return ( ( a % b ) + b ) % b; }
45 #define SUB_MIN(val, sub_val, min) val = \
46 ((val-(int32_t)sub_val)<min?min:val-sub_val)
47 #define ADD_MAX(val, add_val, max) val = \
48 ((val+(int32_t)add_val)>max?max:val+add_val)
50 static inline int32_t PIX_OFS(int32_t i_x, int32_t i_y, plane_t *ps_plane) {
51 return i_x * ps_plane->i_pixel_pitch + i_y * ps_plane->i_pitch; }
53 #define CHECK_PIX_OFS(i_x, i_y, ps_plane) ( \
54 (i_x) >= 0 && (i_y) >= 0 && \
55 (i_x) * ps_plane->i_pixel_pitch < ps_plane->i_visible_pitch && \
56 (i_y) < ps_plane->i_visible_lines \
59 static inline void DARKEN_PIXEL(int32_t i_x, int32_t i_y,
60 int16_t intensity, plane_t *ps_plane) {
61 SUB_MIN( ps_plane->p_pixels[ PIX_OFS(i_x, i_y, ps_plane) ],
62 intensity, 0 );
65 static inline void LIGHTEN_PIXEL(int32_t i_x, int32_t i_y,
66 int16_t intensity, plane_t *ps_plane) {
67 ADD_MAX( ps_plane->p_pixels[ PIX_OFS(i_x, i_y, ps_plane) ],
68 intensity, 0xFF );
71 static inline void CHECK_N_DARKEN_PIXEL(int32_t i_x, int32_t i_y,
72 int16_t intensity, plane_t *ps_plane) {
73 if ( likely( CHECK_PIX_OFS(i_x, i_y, ps_plane) ) )
74 DARKEN_PIXEL(i_x, i_y, intensity, ps_plane);
77 static inline void CHECK_N_LIGHTEN_PIXEL(int32_t i_x, int32_t i_y,
78 int16_t intensity, plane_t *ps_plane) {
79 if ( likely( CHECK_PIX_OFS(i_x, i_y, ps_plane) ) )
80 LIGHTEN_PIXEL(i_x, i_y, intensity, ps_plane);
83 #define MAX_SCRATCH 20
84 #define MAX_HAIR 10
85 #define MAX_DUST 10
87 typedef struct {
88 int32_t i_offset;
89 int32_t i_width;
90 uint16_t i_intensity;
91 mtime_t i_stop_trigger;
92 } scratch_t;
94 typedef struct {
95 int32_t i_x, i_y;
96 uint8_t i_rotation;
97 int32_t i_width;
98 int32_t i_length;
99 int32_t i_curve;
100 uint16_t i_intensity;
101 mtime_t i_stop_trigger;
102 } hair_t;
104 typedef struct {
105 int32_t i_x, i_y;
106 int32_t i_width;
107 uint16_t i_intensity;
108 mtime_t i_stop_trigger;
109 } dust_t;
111 struct filter_sys_t {
113 /* general data */
114 bool b_init;
115 int32_t i_planes;
116 int32_t *i_height;
117 int32_t *i_width;
118 int32_t *i_visible_pitch;
119 mtime_t i_start_time;
120 mtime_t i_last_time;
121 mtime_t i_cur_time;
123 /* sliding & offset effect */
124 mtime_t i_offset_trigger;
125 mtime_t i_sliding_trigger;
126 mtime_t i_sliding_stop_trig;
127 int32_t i_offset_ofs;
128 int32_t i_sliding_ofs;
129 int32_t i_sliding_speed;
131 /* scratch on film */
132 mtime_t i_scratch_trigger;
133 scratch_t *p_scratch[MAX_SCRATCH];
135 /* hair on lens */
136 mtime_t i_hair_trigger;
137 hair_t *p_hair[MAX_HAIR];
139 /* blotch on film */
140 mtime_t i_blotch_trigger;
142 /* dust on lens */
143 mtime_t i_dust_trigger;
144 dust_t *p_dust[MAX_DUST];
147 /*****************************************************************************
148 * Prototypes
149 *****************************************************************************/
151 static picture_t *Filter( filter_t *, picture_t * );
153 static int oldmovie_allocate_data( filter_t *, picture_t * );
154 static void oldmovie_free_allocated_data( filter_t * );
156 static void oldmovie_shutter_effect( filter_t *, picture_t * );
157 static int oldmovie_sliding_offset_effect( filter_t *, picture_t * );
158 static void oldmovie_black_n_white_effect( picture_t * );
159 static int oldmovie_dark_border_effect( filter_t *, picture_t * );
160 static int oldmovie_film_scratch_effect( filter_t *, picture_t * );
161 static void oldmovie_film_blotch_effect( filter_t *, picture_t * );
162 static void oldmovie_film_dust_effect( filter_t *, picture_t * );
163 static int oldmovie_lens_hair_effect( filter_t *, picture_t * );
164 static int oldmovie_lens_dust_effect( filter_t *, picture_t * );
166 static void oldmovie_define_hair_location( filter_t *p_filter, hair_t* ps_hair );
167 static void oldmovie_define_dust_location( filter_t *p_filter, dust_t* ps_dust );
168 static int oldmovie_sliding_offset_apply( filter_t *p_filter, picture_t *p_pic_out );
170 /*****************************************************************************
171 * Module descriptor
172 *****************************************************************************/
174 static int Open ( vlc_object_t * );
175 static void Close( vlc_object_t * );
177 vlc_module_begin()
178 set_description( N_("Old movie effect video filter") )
179 set_shortname( N_( "Old movie" ))
180 set_capability( "video filter", 0 )
181 set_category( CAT_VIDEO )
182 set_subcategory( SUBCAT_VIDEO_VFILTER )
184 set_callbacks( Open, Close )
185 vlc_module_end()
188 * Open the filter
190 static int Open( vlc_object_t *p_this ) {
191 filter_t *p_filter = (filter_t *)p_this;
192 filter_sys_t *p_sys;
194 /* Assert video in match with video out */
195 if( !es_format_IsSimilar( &p_filter->fmt_in, &p_filter->fmt_out ) ) {
196 msg_Err( p_filter, "Input and output format does not match" );
197 return VLC_EGENERIC;
200 /* Reject 0 bpp and unsupported chroma */
201 const vlc_fourcc_t fourcc = p_filter->fmt_in.video.i_chroma;
202 const vlc_chroma_description_t *p_chroma =
203 vlc_fourcc_GetChromaDescription( p_filter->fmt_in.video.i_chroma );
204 if( !p_chroma || p_chroma->pixel_size == 0
205 || p_chroma->plane_count < 3 || p_chroma->pixel_size > 1
206 || !vlc_fourcc_IsYUV( fourcc ) ) {
208 msg_Err( p_filter, "Unsupported chroma (%4.4s)", (char*)&fourcc );
209 return VLC_EGENERIC;
212 /* Allocate structure */
213 p_filter->p_sys = p_sys = calloc( 1, sizeof(*p_sys) );
214 if( unlikely( !p_sys ) )
215 return VLC_ENOMEM;
217 /* init data */
218 p_filter->pf_video_filter = Filter;
219 p_sys->i_start_time = p_sys->i_cur_time = p_sys->i_last_time = mdate();
221 return VLC_SUCCESS;
225 * Close the filter
227 static void Close( vlc_object_t *p_this ) {
228 filter_t *p_filter = (filter_t *)p_this;
229 filter_sys_t *p_sys = p_filter->p_sys;
231 /* Free allocated memory */
232 oldmovie_free_allocated_data( p_filter );
233 free( p_sys );
237 * Filter a picture
239 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic_in ) {
240 if( unlikely( !p_pic_in || !p_filter ) )
241 return NULL;
243 filter_sys_t *p_sys = p_filter->p_sys;
245 picture_t *p_pic_out = filter_NewPicture( p_filter );
246 if( unlikely( !p_pic_out ) ) {
247 picture_Release( p_pic_in );
248 return NULL;
252 * manage time
254 p_sys->i_last_time = p_sys->i_cur_time;
255 p_sys->i_cur_time = mdate();
258 * allocate data
260 if ( unlikely( !p_sys->b_init ) )
261 if ( unlikely( oldmovie_allocate_data( p_filter, p_pic_in ) != VLC_SUCCESS ) ) {
262 picture_Release( p_pic_in );
263 return NULL;
265 p_sys->b_init = true;
268 * preset output pic: raw copy src to dst
270 picture_CopyPixels(p_pic_out, p_pic_in);
273 * apply several effects on picture
275 oldmovie_black_n_white_effect( p_pic_out );
277 /* simulates projector shutter blinking effect */
278 oldmovie_shutter_effect(p_filter, p_pic_out);
280 if ( unlikely( oldmovie_sliding_offset_effect( p_filter, p_pic_out ) != VLC_SUCCESS ) )
281 return CopyInfoAndRelease( p_pic_out, p_pic_in );
283 oldmovie_dark_border_effect( p_filter, p_pic_out );
285 if ( unlikely( oldmovie_film_scratch_effect(p_filter, p_pic_out) != VLC_SUCCESS ) )
286 return CopyInfoAndRelease( p_pic_out, p_pic_in );
288 oldmovie_film_blotch_effect(p_filter, p_pic_out);
290 if ( unlikely( oldmovie_lens_hair_effect( p_filter, p_pic_out ) != VLC_SUCCESS ) )
291 return CopyInfoAndRelease( p_pic_out, p_pic_in );
293 if ( unlikely( oldmovie_lens_dust_effect( p_filter, p_pic_out ) != VLC_SUCCESS ) )
294 return CopyInfoAndRelease( p_pic_out, p_pic_in );
296 oldmovie_film_dust_effect( p_filter, p_pic_out );
298 return CopyInfoAndRelease( p_pic_out, p_pic_in );
302 * Allocate data
304 static int oldmovie_allocate_data( filter_t *p_filter, picture_t *p_pic_in ) {
305 filter_sys_t *p_sys = p_filter->p_sys;
307 oldmovie_free_allocated_data( p_filter );
310 * take into account different characteristics for each plane
312 p_sys->i_planes = p_pic_in->i_planes;
313 p_sys->i_height = calloc( p_sys->i_planes, sizeof(int32_t) );
314 p_sys->i_width = calloc( p_sys->i_planes, sizeof(int32_t) );
315 p_sys->i_visible_pitch = calloc( p_sys->i_planes, sizeof(int32_t) );
317 if( unlikely( !p_sys->i_height || !p_sys->i_width || !p_sys->i_visible_pitch ) ) {
318 oldmovie_free_allocated_data( p_filter );
319 return VLC_ENOMEM;
322 for (int32_t i_p=0; i_p < p_sys->i_planes; i_p++) {
323 p_sys->i_visible_pitch [i_p] = (int) p_pic_in->p[i_p].i_visible_pitch;
324 p_sys->i_height[i_p] = (int) p_pic_in->p[i_p].i_visible_lines;
325 p_sys->i_width [i_p] = (int) p_pic_in->p[i_p].i_visible_pitch
326 / p_pic_in->p[i_p].i_pixel_pitch;
328 return VLC_SUCCESS;
332 * Free allocated data
334 static void oldmovie_free_allocated_data( filter_t *p_filter ) {
335 filter_sys_t *p_sys = p_filter->p_sys;
337 for ( uint32_t i_s = 0; i_s < MAX_SCRATCH; i_s++ )
338 FREENULL(p_sys->p_scratch[i_s]);
340 for ( uint32_t i_h = 0; i_h < MAX_HAIR; i_h++ )
341 FREENULL(p_sys->p_hair[i_h]);
343 for ( uint32_t i_d = 0; i_d < MAX_DUST; i_d++ )
344 FREENULL(p_sys->p_dust[i_d]);
346 p_sys->i_planes = 0;
347 FREENULL( p_sys->i_height );
348 FREENULL( p_sys->i_width );
349 FREENULL( p_sys->i_visible_pitch );
353 * Projector shutter effect
355 static void oldmovie_shutter_effect( filter_t *p_filter, picture_t *p_pic_out ) {
356 filter_sys_t *p_sys = p_filter->p_sys;
358 #define SHUTTER_FREQ 2
359 #define SHUTTER_SPEED 25
360 #define SHUTTER_HEIGHT 1.5
362 #define SHUTTER_INTENSITY 50
364 #define SUB_FRAME (p_sys->i_cur_time % (CLOCK_FREQ / SHUTTER_FREQ))
367 * depending on current time: define shutter location on picture
369 int32_t i_shutter_sup = VLC_CLIP((int64_t)SUB_FRAME
370 * p_pic_out->p[Y_PLANE].i_visible_lines
371 * SHUTTER_SPEED / CLOCK_FREQ,
372 0, p_pic_out->p[Y_PLANE].i_visible_lines);
374 int32_t i_shutter_inf = VLC_CLIP((int64_t)SUB_FRAME
375 * p_pic_out->p[Y_PLANE].i_visible_lines
376 * SHUTTER_SPEED / CLOCK_FREQ
377 - SHUTTER_HEIGHT * p_pic_out->p[Y_PLANE].i_visible_lines,
378 0, p_pic_out->p[Y_PLANE].i_visible_lines);
380 int32_t i_width = p_pic_out->p[Y_PLANE].i_visible_pitch
381 / p_pic_out->p[Y_PLANE].i_pixel_pitch;
384 * darken pixels currently occulted by shutter
386 for ( int32_t i_y = i_shutter_inf; i_y < i_shutter_sup; i_y++ )
387 for ( int32_t i_x = 0; i_x < i_width; i_x++ )
388 DARKEN_PIXEL( i_x, i_y, SHUTTER_INTENSITY, &p_pic_out->p[Y_PLANE] );
392 * sliding & offset effect
394 static int oldmovie_sliding_offset_effect( filter_t *p_filter, picture_t *p_pic_out ) {
395 filter_sys_t *p_sys = p_filter->p_sys;
399 * one shot offset section
402 #define OFFSET_AVERAGE_PERIOD (10 * CLOCK_FREQ)
404 /* start trigger to be (re)initialized */
405 if ( p_sys->i_offset_trigger == 0
406 || p_sys->i_sliding_speed != 0 ) { /* do not mix sliding and offset */
407 /* random trigger for offset effect */
408 p_sys->i_offset_trigger = p_sys->i_cur_time
409 + ( (uint64_t) vlc_mrand48() ) % OFFSET_AVERAGE_PERIOD
410 + OFFSET_AVERAGE_PERIOD / 2;
411 p_sys->i_offset_ofs = 0;
412 } else if ( p_sys->i_offset_trigger <= p_sys->i_cur_time ) {
413 /* trigger for offset effect */
414 p_sys->i_offset_trigger = 0;
415 p_sys->i_offset_ofs = MOD( ( (uint32_t)vlc_mrand48() ),
416 p_sys->i_height[Y_PLANE] ) * 100;
417 } else
418 p_sys->i_offset_ofs = 0;
422 * sliding section
425 #define SLIDING_AVERAGE_PERIOD (20 * CLOCK_FREQ)
426 #define SLIDING_AVERAGE_DURATION ( 3 * CLOCK_FREQ)
428 /* start trigger to be (re)initialized */
429 if ( ( p_sys->i_sliding_stop_trig == 0 )
430 && ( p_sys->i_sliding_trigger == 0 )
431 && ( p_sys->i_sliding_speed == 0 ) ) {
432 /* random trigger which enable sliding effect */
433 p_sys->i_sliding_trigger = p_sys->i_cur_time
434 + ( (uint64_t) vlc_mrand48() ) % SLIDING_AVERAGE_PERIOD
435 + SLIDING_AVERAGE_PERIOD / 2;
437 /* start trigger just occurs */
438 else if ( ( p_sys->i_sliding_stop_trig == 0 )
439 && ( p_sys->i_sliding_trigger <= p_sys->i_cur_time )
440 && ( p_sys->i_sliding_speed == 0 ) ) {
441 /* init sliding parameters */
442 p_sys->i_sliding_trigger = 0;
443 p_sys->i_sliding_stop_trig = p_sys->i_cur_time
444 + ((uint64_t) vlc_mrand48() ) % SLIDING_AVERAGE_DURATION
445 + SLIDING_AVERAGE_DURATION / 2;
446 p_sys->i_sliding_ofs = 0;
447 /* note: sliding speed unit = image per 100 s */
448 p_sys->i_sliding_speed = MOD(((int32_t) vlc_mrand48() ), 201) - 100;
450 /* stop trigger disabling sliding effect */
451 else if ( ( p_sys->i_sliding_stop_trig <= p_sys->i_cur_time )
452 && ( p_sys->i_sliding_trigger == 0 ) ) {
454 /* first increase speed to ensure we will come back to stable image */
455 if ( abs(p_sys->i_sliding_speed) < 50 )
456 p_sys->i_sliding_speed += 5;
458 /* check if offset is close to 0 and then ready to stop */
459 if ( abs( p_sys->i_sliding_ofs ) < abs( p_sys->i_sliding_speed
460 * p_sys->i_height[Y_PLANE]
461 * ( p_sys->i_cur_time - p_sys->i_last_time ) / CLOCK_FREQ )
462 || abs( p_sys->i_sliding_ofs ) < p_sys->i_height[Y_PLANE] * 100 / 20 ) {
464 /* reset sliding parameters */
465 p_sys->i_sliding_ofs = p_sys->i_sliding_speed = 0;
466 p_sys->i_sliding_trigger = p_sys->i_sliding_stop_trig = 0;
470 /* update offset */
471 p_sys->i_sliding_ofs += p_sys->i_sliding_speed * p_sys->i_height[Y_PLANE]
472 * ( p_sys->i_cur_time - p_sys->i_last_time )
473 / CLOCK_FREQ;
475 p_sys->i_sliding_ofs = MOD( p_sys->i_sliding_ofs,
476 p_sys->i_height[Y_PLANE] * 100 );
478 /* apply offset */
479 return oldmovie_sliding_offset_apply( p_filter, p_pic_out );
483 * apply both sliding and offset effect
485 static int oldmovie_sliding_offset_apply( filter_t *p_filter, picture_t *p_pic_out )
487 filter_sys_t *p_sys = p_filter->p_sys;
489 for ( uint8_t i_p = 0; i_p < p_pic_out->i_planes; i_p++ ) {
490 /* first allocate temporary buffer for swap operation */
491 uint8_t *p_temp_buf = calloc( p_pic_out->p[i_p].i_lines * p_pic_out->p[i_p].i_pitch,
492 sizeof(uint8_t) );
493 if ( unlikely( !p_temp_buf ) )
494 return VLC_ENOMEM;
495 memcpy( p_temp_buf,p_pic_out->p[i_p].p_pixels,
496 p_pic_out->p[i_p].i_lines * p_pic_out->p[i_p].i_pitch );
498 /* copy lines to output_pic */
499 for ( int32_t i_y = 0; i_y < p_pic_out->p[i_p].i_visible_lines; i_y++ ) {
500 int32_t i_ofs = MOD( ( p_sys->i_offset_ofs + p_sys->i_sliding_ofs )
501 /100,
502 p_sys->i_height[Y_PLANE] );
503 i_ofs *= p_pic_out->p[i_p].i_visible_lines;
504 i_ofs /= p_sys->i_height[Y_PLANE];
506 memcpy( &p_pic_out->p[i_p].p_pixels[ i_y * p_pic_out->p[i_p].i_pitch ],
507 &p_temp_buf[ ( ( i_y + i_ofs ) % p_pic_out->p[i_p].i_visible_lines ) * p_pic_out->p[i_p].i_pitch ],
508 p_pic_out->p[i_p].i_visible_pitch);
510 free( p_temp_buf );
513 return VLC_SUCCESS;
517 * Black and white transform including a touch of sepia effect
519 static void oldmovie_black_n_white_effect( picture_t *p_pic_out )
521 for ( int32_t i_y = 0; i_y < p_pic_out->p[Y_PLANE].i_visible_lines; i_y++ )
522 for ( int32_t i_x = 0; i_x < p_pic_out->p[Y_PLANE].i_visible_pitch;
523 i_x += p_pic_out->p[Y_PLANE].i_pixel_pitch ) {
525 uint32_t i_pix_ofs = i_x+i_y*p_pic_out->p[Y_PLANE].i_pitch;
526 p_pic_out->p[Y_PLANE].p_pixels[i_pix_ofs] -= p_pic_out->p[Y_PLANE].p_pixels[i_pix_ofs] >> 2;
527 p_pic_out->p[Y_PLANE].p_pixels[i_pix_ofs] += 30;
530 memset( p_pic_out->p[U_PLANE].p_pixels, 122,
531 p_pic_out->p[U_PLANE].i_lines * p_pic_out->p[U_PLANE].i_pitch );
532 memset( p_pic_out->p[V_PLANE].p_pixels, 132,
533 p_pic_out->p[V_PLANE].i_lines * p_pic_out->p[V_PLANE].i_pitch );
537 * Smooth darker borders effect
539 static int oldmovie_dark_border_effect( filter_t *p_filter, picture_t *p_pic_out )
541 filter_sys_t *p_sys = p_filter->p_sys;
543 #define BORDER_DIST 20
545 for ( int32_t i_y = 0; i_y < p_sys->i_height[Y_PLANE]; i_y++ )
546 for ( int32_t i_x = 0; i_x < p_sys->i_width[Y_PLANE]; i_x++ ) {
548 int32_t i_x_border_dist = __MIN( i_x, p_sys->i_width[Y_PLANE] - i_x);
549 int32_t i_y_border_dist = __MIN( i_y, p_sys->i_height[Y_PLANE] - i_y);
551 int32_t i_border_dist = __MAX(BORDER_DIST - i_x_border_dist,0)
552 + __MAX(BORDER_DIST - i_y_border_dist,0);
554 i_border_dist = __MIN(BORDER_DIST, i_border_dist);
556 if ( i_border_dist == 0 )
557 continue;
559 uint32_t i_pix_ofs = i_x * p_pic_out->p[Y_PLANE].i_pixel_pitch
560 + i_y * p_pic_out->p[Y_PLANE].i_pitch;
562 SUB_MIN( p_pic_out->p[Y_PLANE].p_pixels[i_pix_ofs],
563 i_border_dist * 255 / BORDER_DIST, 0 );
566 return VLC_SUCCESS;
570 * Vertical scratch random management and effect
572 static int oldmovie_film_scratch_effect( filter_t *p_filter, picture_t *p_pic_out )
574 filter_sys_t *p_sys = p_filter->p_sys;
576 #define SCRATCH_GENERATOR_PERIOD ( CLOCK_FREQ * 2 )
577 #define SCRATCH_DURATION ( CLOCK_FREQ * 1 / 2)
579 /* generate new scratch */
580 if ( p_sys->i_scratch_trigger <= p_sys->i_cur_time ) {
581 for ( uint32_t i_s = 0; i_s < MAX_SCRATCH; i_s++ )
582 if ( p_sys->p_scratch[i_s] == NULL ) {
583 /* allocate data */
584 p_sys->p_scratch[i_s] = calloc( 1, sizeof(scratch_t) );
585 if ( unlikely( !p_sys->p_scratch[i_s] ) )
586 return VLC_ENOMEM;
588 /* set random parameters */
589 p_sys->p_scratch[i_s]->i_offset = ( ( (unsigned)vlc_mrand48() )
590 % __MAX( p_sys->i_width[Y_PLANE] - 10, 1 ) )
591 + 5;
592 p_sys->p_scratch[i_s]->i_width = ( ( (unsigned)vlc_mrand48() )
593 % __MAX( p_sys->i_width[Y_PLANE] / 500, 1 ) )
594 + 1;
595 p_sys->p_scratch[i_s]->i_intensity = (unsigned) vlc_mrand48() % 50 + 10;
596 p_sys->p_scratch[i_s]->i_stop_trigger = p_sys->i_cur_time
597 + (uint64_t) vlc_mrand48() % SCRATCH_DURATION
598 + SCRATCH_DURATION / 2;
600 break;
602 p_sys->i_scratch_trigger = p_sys->i_cur_time
603 + ( (uint64_t)vlc_mrand48() ) % SCRATCH_GENERATOR_PERIOD
604 + SCRATCH_GENERATOR_PERIOD / 2;
607 /* manage and apply current scratch */
608 for ( uint32_t i_s = 0; i_s < MAX_SCRATCH; i_s++ )
609 if ( p_sys->p_scratch[i_s] ) {
610 /* remove outdated scratch */
611 if ( p_sys->p_scratch[i_s]->i_stop_trigger <= p_sys->i_cur_time ) {
612 FREENULL( p_sys->p_scratch[i_s] );
613 continue;
616 /* otherwise apply scratch */
617 for ( int32_t i_y = 0; i_y < p_pic_out->p[Y_PLANE].i_visible_lines; i_y++ )
618 for ( int32_t i_x = p_sys->p_scratch[i_s]->i_offset;
619 i_x < __MIN(p_sys->p_scratch[i_s]->i_offset
620 + p_sys->p_scratch[i_s]->i_width, p_sys->i_width[Y_PLANE] );
621 i_x++ )
622 DARKEN_PIXEL( i_x, i_y, p_sys->p_scratch[i_s]->i_intensity,
623 &p_pic_out->p[Y_PLANE] );
626 return VLC_SUCCESS;
630 * Blotch addition
631 * bigger than dust but only during one frame (due to a local film damage)
633 static void oldmovie_film_blotch_effect( filter_t *p_filter, picture_t *p_pic_out )
635 filter_sys_t *p_sys = p_filter->p_sys;
637 #define BLOTCH_GENERATOR_PERIOD ( CLOCK_FREQ * 5 )
639 /* generate blotch */
640 if ( p_sys->i_blotch_trigger <= p_sys->i_cur_time ) {
641 /* set random parameters */
642 int32_t i_bx = (unsigned)vlc_mrand48() % p_sys->i_width[Y_PLANE];
643 int32_t i_by = (unsigned)vlc_mrand48() % p_sys->i_height[Y_PLANE];
644 int32_t i_width = (unsigned)vlc_mrand48() % __MAX( 1, p_sys->i_width[Y_PLANE] / 10 ) + 1;
645 int32_t i_intensity = (unsigned)vlc_mrand48() % 50 + 20;
647 if ( (unsigned)vlc_mrand48() & 0x01 ) {
648 /* draw dark blotch */
649 for ( int32_t i_y = -i_width + 1; i_y < i_width; i_y++ )
650 for ( int32_t i_x = -i_width + 1; i_x < i_width; i_x++ )
651 if ( i_x * i_x + i_y * i_y <= i_width * i_width )
652 CHECK_N_DARKEN_PIXEL( i_x + i_bx, i_y + i_by,
653 i_intensity, &p_pic_out->p[Y_PLANE] );
654 } else {
655 /* draw light blotch */
656 for ( int32_t i_y = -i_width+1; i_y < i_width; i_y++ )
657 for ( int32_t i_x = -i_width+1; i_x < i_width; i_x++ )
658 if ( i_x * i_x + i_y * i_y <= i_width * i_width )
659 CHECK_N_LIGHTEN_PIXEL( i_x + i_bx, i_y + i_by,
660 i_intensity, &p_pic_out->p[Y_PLANE] );
663 p_sys->i_blotch_trigger = p_sys->i_cur_time
664 + (uint64_t)vlc_mrand48() % BLOTCH_GENERATOR_PERIOD
665 + BLOTCH_GENERATOR_PERIOD / 2;
670 * Dust dots addition, visible during one frame only (film damages)
672 static void oldmovie_film_dust_effect( filter_t *p_filter, picture_t *p_pic_out ) {
673 #define ONESHOT_DUST_RATIO 1000
675 filter_sys_t *p_sys = p_filter->p_sys;
677 for ( int32_t i_dust = 0;
678 i_dust < p_sys->i_width[Y_PLANE] * p_sys->i_height[Y_PLANE] / ONESHOT_DUST_RATIO;
679 i_dust++)
680 if ( (unsigned)vlc_mrand48() % 5 < 3 )
681 DARKEN_PIXEL( (unsigned)vlc_mrand48() % p_sys->i_width[Y_PLANE],
682 (unsigned)vlc_mrand48() % p_sys->i_height[Y_PLANE],
683 150, &p_pic_out->p[Y_PLANE] );
684 else
685 LIGHTEN_PIXEL( (unsigned)vlc_mrand48() % p_sys->i_width[Y_PLANE],
686 (unsigned)vlc_mrand48() % p_sys->i_height[Y_PLANE],
687 50, &p_pic_out->p[Y_PLANE] );
691 * Hair and dust on projector lens
694 #define HAIR_GENERATOR_PERIOD ( CLOCK_FREQ * 50 )
695 #define HAIR_DURATION ( CLOCK_FREQ * 50 )
696 #define DUST_GENERATOR_PERIOD ( CLOCK_FREQ * 100 )
697 #define DUST_DURATION ( CLOCK_FREQ * 4 )
700 * Define hair location on the lens and timeout
703 static void oldmovie_define_hair_location( filter_t *p_filter, hair_t* ps_hair ) {
704 filter_sys_t *p_sys = p_filter->p_sys;
706 ps_hair->i_x = (unsigned)vlc_mrand48() % p_sys->i_width[Y_PLANE];
707 ps_hair->i_y = (unsigned)vlc_mrand48() % p_sys->i_height[Y_PLANE];
708 ps_hair->i_rotation = (unsigned)vlc_mrand48() % 200;
710 ps_hair->i_stop_trigger = p_sys->i_cur_time
711 + (uint64_t)vlc_mrand48() % HAIR_DURATION
712 + HAIR_DURATION / 2;
716 * Show black hair on the screen
717 * after random duration it is removed or re-located
719 static int oldmovie_lens_hair_effect( filter_t *p_filter, picture_t *p_pic_out ) {
720 filter_sys_t *p_sys = p_filter->p_sys;
722 /* generate new hair */
723 if ( p_sys->i_hair_trigger <= p_sys->i_cur_time ) {
724 for ( uint32_t i_h = 0; i_h < MAX_HAIR; i_h++ )
725 if ( p_sys->p_hair[i_h] == NULL ) {
726 /* allocate data */
727 p_sys->p_hair[i_h] = calloc( 1, sizeof(hair_t) );
728 if ( unlikely( !p_sys->p_hair[i_h] ) )
729 return VLC_ENOMEM;
731 /* set random parameters */
732 p_sys->p_hair[i_h]->i_length = (unsigned)vlc_mrand48()
733 % ( p_sys->i_width[Y_PLANE] / 3 ) + 5;
734 p_sys->p_hair[i_h]->i_curve = MOD( (int32_t)vlc_mrand48(), 80 ) - 40;
735 p_sys->p_hair[i_h]->i_width = (unsigned)vlc_mrand48()
736 % __MAX( 1, p_sys->i_width[Y_PLANE] / 1500 ) + 1;
737 p_sys->p_hair[i_h]->i_intensity = (unsigned)vlc_mrand48() % 50 + 20;
739 oldmovie_define_hair_location( p_filter, p_sys->p_hair[i_h] );
741 break;
743 p_sys->i_hair_trigger = p_sys->i_cur_time
744 + (uint64_t)vlc_mrand48() % HAIR_GENERATOR_PERIOD
745 + HAIR_GENERATOR_PERIOD / 2;
748 /* manage and apply current hair */
749 for ( uint32_t i_h = 0; i_h < MAX_HAIR; i_h++ )
750 if ( p_sys->p_hair[i_h] ) {
751 /* remove outdated ones */
752 if ( p_sys->p_hair[i_h]->i_stop_trigger <= p_sys->i_cur_time ) {
753 /* select between moving or removing hair */
754 if ( (unsigned)vlc_mrand48() % 2 == 0 )
755 /* move hair */
756 oldmovie_define_hair_location( p_filter, p_sys->p_hair[i_h] );
757 else {
758 /* remove hair */
759 FREENULL( p_sys->p_hair[i_h] );
760 continue;
764 /* draw hair */
765 double f_base_x = (double)p_sys->p_hair[i_h]->i_x;
766 double f_base_y = (double)p_sys->p_hair[i_h]->i_y;
768 for ( int32_t i_l = 0; i_l < p_sys->p_hair[i_h]->i_length; i_l++ ) {
769 uint32_t i_current_rot = p_sys->p_hair[i_h]->i_rotation
770 + p_sys->p_hair[i_h]->i_curve * i_l / 100;
771 f_base_x += cos( (double)i_current_rot / 128.0 * M_PI );
772 f_base_y += sin( (double)i_current_rot / 128.0 * M_PI );
773 double f_current_x = f_base_x;
774 double f_current_y = f_base_y;
775 for ( int32_t i_w = 0; i_w < p_sys->p_hair[i_h]->i_width; i_w++ ) {
776 f_current_x += sin( (double)i_current_rot / 128.0 * M_PI );
777 f_current_y += cos( (double)i_current_rot / 128.0 * M_PI );
778 CHECK_N_DARKEN_PIXEL( (int32_t) f_current_x,
779 (int32_t) f_current_y,
780 p_sys->p_hair[i_h]->i_intensity,
781 &p_pic_out->p[Y_PLANE] );
786 return VLC_SUCCESS;
790 * Define dust location on the lens and timeout
793 static void oldmovie_define_dust_location( filter_t *p_filter, dust_t* ps_dust ) {
794 filter_sys_t *p_sys = p_filter->p_sys;
796 ps_dust->i_x = (unsigned)vlc_mrand48() % p_sys->i_width[Y_PLANE];
797 ps_dust->i_y = (unsigned)vlc_mrand48() % p_sys->i_height[Y_PLANE];
799 ps_dust->i_stop_trigger = p_sys->i_cur_time
800 + (uint64_t)vlc_mrand48() % HAIR_DURATION
801 + HAIR_DURATION / 2;
804 ps_dust->i_x = MOD( (int32_t)vlc_mrand48(), p_sys->i_width[Y_PLANE] );
805 ps_dust->i_y = MOD( (int32_t)vlc_mrand48(), p_sys->i_height[Y_PLANE] );
807 ps_dust->i_stop_trigger = p_sys->i_cur_time
808 + (uint64_t)vlc_mrand48() % DUST_DURATION
809 + DUST_DURATION / 2;
813 * Dust addition
814 * smaller than blotch but will remain on the screen for long time
816 static int oldmovie_lens_dust_effect( filter_t *p_filter, picture_t *p_pic_out ) {
817 filter_sys_t *p_sys = p_filter->p_sys;
819 /* generate new dust */
820 if ( p_sys->i_dust_trigger <= p_sys->i_cur_time ) {
821 for ( uint32_t i_d = 0; i_d < MAX_DUST; i_d++ )
822 if ( p_sys->p_dust[i_d] == NULL ) {
823 /* allocate data */
824 p_sys->p_dust[i_d] = calloc( 1, sizeof(dust_t) );
825 if ( unlikely( !p_sys->p_dust[i_d] ) )
826 return VLC_ENOMEM;
828 /* set random parameters */
829 oldmovie_define_dust_location( p_filter, p_sys->p_dust[i_d] );
830 p_sys->p_dust[i_d]->i_width = MOD( (int32_t)vlc_mrand48(), 5 ) + 1;
831 p_sys->p_dust[i_d]->i_intensity = (unsigned)vlc_mrand48() % 30 + 30;
833 break;
835 p_sys->i_dust_trigger = p_sys->i_cur_time
836 + (uint64_t)vlc_mrand48() % DUST_GENERATOR_PERIOD
837 + DUST_GENERATOR_PERIOD / 2;
840 /* manage and apply current dust */
841 for ( uint32_t i_d = 0; i_d < MAX_DUST; i_d++ )
842 if ( p_sys->p_dust[i_d] ) {
843 /* remove outdated ones */
844 if ( p_sys->p_dust[i_d]->i_stop_trigger <= p_sys->i_cur_time ) {
845 /* select between moving or removing dust */
846 if ( (unsigned)vlc_mrand48() % 2 == 0 )
847 /* move dust */
848 oldmovie_define_dust_location( p_filter, p_sys->p_dust[i_d] );
849 else {
850 /* remove dust */
851 FREENULL( p_sys->p_dust[i_d] );
852 continue;
856 /* draw dust */
857 for ( int32_t i_y = -p_sys->p_dust[i_d]->i_width + 1; i_y < p_sys->p_dust[i_d]->i_width; i_y++ )
858 for ( int32_t i_x = -p_sys->p_dust[i_d]->i_width + 1; i_x < p_sys->p_dust[i_d]->i_width; i_x++ )
859 if ( i_x * i_x + i_y * i_y <= p_sys->p_dust[i_d]->i_width * p_sys->p_dust[i_d]->i_width )
860 CHECK_N_DARKEN_PIXEL( i_x + p_sys->p_dust[i_d]->i_x,
861 i_y + p_sys->p_dust[i_d]->i_y,
862 p_sys->p_dust[i_d]->i_intensity,
863 &p_pic_out->p[Y_PLANE] );
866 return VLC_SUCCESS;