motiondetect.c 17 KB
Newer Older
1
/*****************************************************************************
2
 * motiondetec.c : Second version of a motion detection plugin.
3
 *****************************************************************************
4
 * Copyright (C) 2000-2006 the VideoLAN team
5
 * $Id$
6
 *
7
 * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
8
9
10
11
12
13
14
15
16
17
18
19
20
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22
23
24
25
26
27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

28
29
30
31
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

32
#include <vlc/vlc.h>
33
#include <vlc_plugin.h>
34
#include <vlc_sout.h>
zorglub's avatar
zorglub committed
35
#include <vlc_vout.h>
36

37
#include "vlc_filter.h"
38
#include "filter_picture.h"
39
40
41
42
43
44
45

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  Create    ( vlc_object_t * );
static void Destroy   ( vlc_object_t * );

46
47
static picture_t *Filter( filter_t *, picture_t * );
static void GaussianConvolution( uint32_t *, uint32_t *, int, int, int );
48
49
50
51

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
52
53

#define FILTER_PREFIX "motiondetect-"
54
55

vlc_module_begin();
56
57
    set_description( N_("Motion detect video filter") );
    set_shortname( N_( "Motion Detect" ));
58
    set_capability( "video filter2", 0 );
59
60
    set_category( CAT_VIDEO );
    set_subcategory( SUBCAT_VIDEO_VFILTER );
61

62
    add_shortcut( "motion" );
63
64
65
    set_callbacks( Create, Destroy );
vlc_module_end();

66
struct filter_sys_t
67
{
68
69
70
71
72
73
74
    uint8_t *p_oldpix;
    uint8_t *p_oldpix_u;
    uint8_t *p_oldpix_v;
    uint32_t *p_buf;
    uint32_t *p_buf2;
    vlc_mutex_t lock;
};
75
76

/*****************************************************************************
77
 * Create
78
79
80
 *****************************************************************************/
static int Create( vlc_object_t *p_this )
{
81
    filter_t *p_filter = (filter_t *)p_this;
82

83
84
85
86
87
88
89
90
91
92
93
94
95
    switch( p_filter->fmt_in.video.i_chroma )
    {
        CASE_PLANAR_YUV
            break;

        CASE_PACKED_YUV_422
            msg_Err( p_filter, "FIXME ... this should be easy." );
        default:
            msg_Err( p_filter, "Unsupported input chroma (%4s)",
                     (char*)&(p_filter->fmt_in.video.i_chroma) );
            return VLC_EGENERIC;
    }

96
    /* Allocate structure */
97
98
    p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
    if( p_filter->p_sys == NULL )
99
    {
100
        msg_Err( p_filter, "out of memory" );
101
102
103
        return VLC_ENOMEM;
    }

104
    p_filter->pf_video_filter = Filter;
105

106
107
    p_filter->p_sys->p_oldpix = NULL;
    p_filter->p_sys->p_buf = NULL;
108

109
    vlc_mutex_init( &p_filter->p_sys->lock );
110
111
112
113
114

    return VLC_SUCCESS;
}

/*****************************************************************************
115
 * Destroy
116
 *****************************************************************************/
117
static void Destroy( vlc_object_t *p_this )
118
{
119
    filter_t *p_filter = (filter_t *)p_this;
120

121
122
    free( p_filter->p_sys->p_oldpix );
    free( p_filter->p_sys->p_buf );
123

124
    vlc_mutex_destroy( &p_filter->p_sys->lock );
125

126
127
    free( p_filter->p_sys );
}
128

129
130
131
132
133
134
135
/*****************************************************************************
 * Render
 *****************************************************************************/
static picture_t *Filter( filter_t *p_filter, picture_t *p_inpic )
{
    picture_t *p_outpic;
    filter_sys_t *p_sys = p_filter->p_sys;
136

137
138
139
140
    const uint8_t *p_inpix = p_inpic->p[Y_PLANE].p_pixels;
    const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
    const int i_src_visible = p_inpic->p[Y_PLANE].i_visible_pitch;
    const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;
141

142
143
144
145
    const uint8_t *p_inpix_u = p_inpic->p[U_PLANE].p_pixels;
    const uint8_t *p_inpix_v = p_inpic->p[V_PLANE].p_pixels;
    const int i_src_pitch_u = p_inpic->p[U_PLANE].i_pitch;
    const int i_num_lines_u = p_inpic->p[U_PLANE].i_visible_lines;
146

147
148
149
150
151
152
    uint8_t *p_oldpix;
    uint8_t *p_oldpix_u;
    uint8_t *p_oldpix_v;
    uint8_t *p_outpix;
    uint32_t *p_buf;
    uint32_t *p_buf2;
153

154
155
    int i,j;
    int last;
156

157
    if( !p_inpic ) return NULL;
158

159
160
    p_outpic = p_filter->pf_vout_buffer_new( p_filter );
    if( !p_outpic )
161
    {
162
163
164
165
        msg_Warn( p_filter, "can't get output picture" );
        if( p_inpic->pf_release )
            p_inpic->pf_release( p_inpic );
        return NULL;
166
167
    }

168
    p_outpix = p_outpic->p[Y_PLANE].p_pixels;
169
    vlc_memcpy( p_outpic->p[U_PLANE].p_pixels, p_inpic->p[U_PLANE].p_pixels,
170
        p_inpic->p[U_PLANE].i_pitch * p_inpic->p[U_PLANE].i_visible_lines );
171
    vlc_memcpy( p_outpic->p[V_PLANE].p_pixels, p_inpic->p[V_PLANE].p_pixels,
172
173
174
        p_inpic->p[V_PLANE].i_pitch * p_inpic->p[V_PLANE].i_visible_lines );

    if( !p_sys->p_oldpix || !p_sys->p_buf )
175
    {
176
177
178
179
180
181
182
183
        free( p_sys->p_oldpix );
        free( p_sys->p_buf );
        p_sys->p_oldpix = malloc( i_src_pitch * i_num_lines );
        p_sys->p_oldpix_u = malloc( i_src_pitch_u * i_num_lines_u );
        p_sys->p_oldpix_v = malloc( i_src_pitch_u * i_num_lines_u );
        p_sys->p_buf = malloc( sizeof( uint32_t ) * i_src_pitch * i_num_lines );
        p_sys->p_buf2 = malloc( sizeof( uint32_t ) * i_src_pitch * i_num_lines);
        return p_inpic;
184
    }
185
186
187
188
189
190
191
192
193
194
195
196
    p_oldpix = p_sys->p_oldpix;
    p_oldpix_u = p_sys->p_oldpix_u;
    p_oldpix_v = p_sys->p_oldpix_v;
    p_buf = p_sys->p_buf;
    p_buf2 = p_sys->p_buf2;

    vlc_mutex_lock( &p_filter->p_sys->lock );

    /**
     * Substract Y planes
     */
    for( i = 0; i < i_src_pitch * i_num_lines; i++ )
197
    {
198
199
200
201
202
203
204
205
        if( p_inpix[i] > p_oldpix[i] )
        {
            p_buf2[i] = p_inpix[i] - p_oldpix[i];
        }
        else
        {
            p_buf2[i] = p_oldpix[i] - p_inpix[i];
        }
206
    }
207
208
209
210
211
212
213
214
215
216
217
    int line;
    int col;
    int format;
    switch( p_inpic->format.i_chroma )
    {
        case VLC_FOURCC('I','4','2','0'):
        case VLC_FOURCC('I','Y','U','V'):
        case VLC_FOURCC('J','4','2','0'):
        case VLC_FOURCC('Y','V','1','2'):
            format = 1;
            break;
218

219
220
221
222
        case VLC_FOURCC('I','4','2','2'):
        case VLC_FOURCC('J','4','2','2'):
            format = 2;
            break;
223

224
225
226
227
228
        default:
            format = 0;
            msg_Warn( p_filter, "Not taking chroma into account" );
            break;
    }
dionoea's avatar
dionoea committed
229
230

    //format = 0;
231
    if( format )
232
    {
233
        for( line = 0; line < i_num_lines_u; line++ )
234
        {
235
236
237
238
239
240
241
242
243
244
245
246
            for( col = 0; col < i_src_pitch_u; col ++ )
            {
                int diff;
                i = line * i_src_pitch_u + col;
                if( p_inpix_u[i] > p_oldpix_u[i] )
                {
                    diff = p_inpix_u[i] - p_oldpix_u[i];
                }
                else
                {
                    diff = p_oldpix_u[i] - p_inpix_u[i];
                }
247
248
249
250
251
252
253
254
                if( p_inpix_v[i] > p_oldpix_v[i] )
                {
                    diff += p_inpix_v[i] - p_oldpix_v[i];
                }
                else
                {
                    diff += p_oldpix_v[i] - p_inpix_v[i];
                }
255
256
257
258
259
260
261
262
263
                switch( format )
                {
                    case 1:
                        p_buf2[2*line*i_src_pitch+2*col] += diff;
                        p_buf2[2*line*i_src_pitch+2*col+1] += diff;
                        p_buf2[(2*line+1)*i_src_pitch+2*col] += diff;
                        p_buf2[(2*line+1)*i_src_pitch+2*col+1] += diff;
                        break;

zorglub's avatar
zorglub committed
264
                    case 2:
265
266
267
268
269
                        p_buf2[line*i_src_pitch+2*col] += diff;
                        p_buf2[line*i_src_pitch+2*col+1] += diff;
                        break;
                }
            }
270
271
272
        }
    }

273
274
275
276
277
278
279
280
281
    /**
     * Apply some smoothing to remove noise
     */
    GaussianConvolution( p_buf2, p_buf, i_src_pitch, i_num_lines, i_src_visible );

    /**
     * Copy luminance plane
     */
    for( i = 0; i < i_src_pitch * i_num_lines; i++ )
282
    {
283
        p_outpix[i] = p_inpix[i];
284
    }
285

286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
    /**
     * Label the shapes ans build the labels dependencies list
     */
    last = 1;
    int colors[5000];
    int color_x_min[5000];
    int color_x_max[5000];
    int color_y_min[5000];
    int color_y_max[5000];

    for( j = 0; j < i_src_pitch; j++ )
    {
        p_buf[j] = 0;
        p_buf[(i_num_lines-1)*i_src_pitch+j] = 0;
    }
    for( i = 1; i < i_num_lines-1; i++ )
    {
        p_buf[i*i_src_pitch] = 0;
        for( j = 1; j < i_src_pitch-1; j++ )
        {
            if( p_buf[i*i_src_pitch+j] > 15 )
            {
                if( p_buf[(i-1)*i_src_pitch+j-1] )
                {
                    p_buf[i*i_src_pitch+j] = p_buf[(i-1)*i_src_pitch+j-1];
                }
                else if( p_buf[(i-1)*i_src_pitch+j] )
                    p_buf[i*i_src_pitch+j] = p_buf[(i-1)*i_src_pitch+j];
                else if( p_buf[i*i_src_pitch+j-1] )
                    p_buf[i*i_src_pitch+j] = p_buf[i*i_src_pitch+j-1];
                else
                {
                    p_buf[i*i_src_pitch+j] = last;
                    colors[last] = last;
                    last++;
                }
                #define CHECK( A ) \
                if( p_buf[A] && p_buf[A] != p_buf[i*i_src_pitch+j] ) \
                { \
                    if( p_buf[A] < p_buf[i*i_src_pitch+j] ) \
                        colors[p_buf[i*i_src_pitch+j]] = p_buf[A]; \
                    else \
                        colors[p_buf[A]] = p_buf[i*i_src_pitch+j]; \
                }
                CHECK( i*i_src_pitch+j-1 );
                CHECK( (i-1)*i_src_pitch+j-1 );
                CHECK( (i-1)*i_src_pitch+j );
                CHECK( (i-1)*i_src_pitch+j+1 );
            }
            else
            {
                p_buf[i*i_src_pitch+j] = 0;
            }
        }
        p_buf[i*i_src_pitch+j] = 0;
    }
342

343
344
345
346
347
348
349
350
351
352
    /**
     * Initialise empty rectangle list
     */
    for( i = 1; i < last; i++ )
    {
        color_x_min[i] = -1;
        color_x_max[i] = -1;
        color_y_min[i] = -1;
        color_y_max[i] = -1;
    }
353

354
355
356
357
358
359
    /**
     * Compute rectangle coordinates
     */
    for( i = 0; i < i_src_pitch * i_num_lines; i++ )
    {
        if( p_buf[i] )
360
        {
361
362
363
364
365
366
367
368
369
370
            while( colors[p_buf[i]] != p_buf[i] )
                p_buf[i] = colors[p_buf[i]];
            if( color_x_min[p_buf[i]] == -1 )
            {
                color_x_min[p_buf[i]] =
                color_x_max[p_buf[i]] = i % i_src_pitch;
                color_y_min[p_buf[i]] =
                color_y_max[p_buf[i]] = i / i_src_pitch;
            }
            else
371
            {
372
373
374
375
376
377
378
379
380
381
382
383
                int x = i % i_src_pitch, y = i / i_src_pitch;
                if( x < color_x_min[p_buf[i]] )
                    color_x_min[p_buf[i]] = x;
                if( x > color_x_max[p_buf[i]] )
                    color_x_max[p_buf[i]] = x;
                if( y < color_y_min[p_buf[i]] )
                    color_y_min[p_buf[i]] = y;
                if( y > color_y_max[p_buf[i]] )
                    color_y_max[p_buf[i]] = y;
            }
        }
    }
384

385
386
387
388
389
390
391
392
393
394
395
    /**
     * Merge overlaping rectangles
     */
    for( i = 1; i < last; i++ )
    {
        if( colors[i] != i ) continue;
        if( color_x_min[i] == -1 ) continue;
        for( j = i+1; j < last; j++ )
        {
            if( colors[j] != j ) continue;
            if( color_x_min[j] == -1 ) continue;
396
397
            if( __MAX( color_x_min[i], color_x_min[j] ) < __MIN( color_x_max[i], color_x_max[j] ) &&
                __MAX( color_y_min[i], color_y_min[j] ) < __MIN( color_y_max[i], color_y_max[j] ) )
398
            {
399
400
401
402
                color_x_min[i] = __MIN( color_x_min[i], color_x_min[j] );
                color_x_max[i] = __MAX( color_x_max[i], color_x_max[j] );
                color_y_min[i] = __MIN( color_y_min[i], color_y_min[j] );
                color_y_max[i] = __MAX( color_y_max[i], color_y_max[j] );
403
404
                color_x_min[j] = -1;
                j = 0;
405
            }
406
407
        }
    }
408

409
410
411
412
413
414
415
416
417
418
419
420
421
422
    /**
     * Count final number of shapes
     * Draw rectangles (there can be more than 1 moving shape in 1 rectangle)
     */
    j = 0;
    for( i = 1; i < last; i++ )
    {
        if( colors[i] == i && color_x_min[i] != -1 )
        {
            if( ( color_y_max[i] - color_y_min[i] ) * ( color_x_max[i] - color_x_min[i] ) < 16 ) continue;
            j++;
            int x, y;
            y = color_y_min[i];
            for( x = color_x_min[i]; x <= color_x_max[i]; x++ )
423
            {
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
                p_outpix[y*i_src_pitch+x] = 0xff;
            }
            y = color_y_max[i];
            for( x = color_x_min[i]; x <= color_x_max[i]; x++ )
            {
                p_outpix[y*i_src_pitch+x] = 0xff;
            }
            x = color_x_min[i];
            for( y = color_y_min[i]; y <= color_y_max[i]; y++ )
            {
                p_outpix[y*i_src_pitch+x] = 0xff;
            }
            x = color_x_max[i];
            for( y = color_y_min[i]; y <= color_y_max[i]; y++ )
            {
                p_outpix[y*i_src_pitch+x] = 0xff;
            }
441
442
        }
    }
443
    msg_Dbg( p_filter, "Counted %d moving shapes.", j);
444
445

    /**
446
     * We're done. Lets keep a copy of the picture
447
     */
448
449
450
    vlc_memcpy( p_oldpix, p_inpix, i_src_pitch * i_num_lines );
    vlc_memcpy( p_oldpix_u, p_inpix_u, i_src_pitch_u * i_num_lines_u );
    vlc_memcpy( p_oldpix_v, p_inpix_v, i_src_pitch_u * i_num_lines_u );
451
452
453
454
455
456
457
458
459
460
461
462
463
464

    vlc_mutex_unlock( &p_filter->p_sys->lock );

    /* misc stuff */
    p_outpic->date = p_inpic->date;
    p_outpic->b_force = p_inpic->b_force;
    p_outpic->i_nb_fields = p_inpic->i_nb_fields;
    p_outpic->b_progressive = p_inpic->b_progressive;
    p_outpic->b_top_field_first = p_inpic->b_top_field_first;

    if( p_inpic->pf_release )
        p_inpic->pf_release( p_inpic );

    return p_outpic;
465
466
467
468
}


/*****************************************************************************
469
470
471
472
473
474
475
476
477
 * Gaussian Convolution
 *****************************************************************************
 *    Gaussian convolution ( sigma == 1.4 )
 *
 *    |  2  4  5  4  2  |   |  2  4  4  4  2 |
 *    |  4  9 12  9  4  |   |  4  8 12  8  4 |
 *    |  5 12 15 12  5  | ~ |  4 12 16 12  4 |
 *    |  4  9 12  9  4  |   |  4  8 12  8  4 |
 *    |  2  4  5  4  2  |   |  2  4  4  4  2 |
478
 *****************************************************************************/
479
480
481
static void GaussianConvolution( uint32_t *p_inpix, uint32_t *p_smooth,
                                 int i_src_pitch, int i_num_lines,
                                 int i_src_visible )
482
{
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*    const uint8_t *p_inpix = p_inpic->p[Y_PLANE].p_pixels;
    const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
    const int i_src_visible = p_inpic->p[Y_PLANE].i_visible_pitch;
    const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;*/

    int x,y;
    for( y = 2; y < i_num_lines - 2; y++ )
    {
        for( x = 2; x < i_src_visible - 2; x++ )
        {
            p_smooth[y*i_src_visible+x] = (uint32_t)(
              /* 2 rows up */
                ( p_inpix[(y-2)*i_src_pitch+x-2] )
              + ((p_inpix[(y-2)*i_src_pitch+x-1]
              +   p_inpix[(y-2)*i_src_pitch+x]
              +   p_inpix[(y-2)*i_src_pitch+x+1])<<1 )
              + ( p_inpix[(y-2)*i_src_pitch+x+2] )
              /* 1 row up */
              + ((p_inpix[(y-1)*i_src_pitch+x-2]
              + ( p_inpix[(y-1)*i_src_pitch+x-1]<<1 )
              + ( p_inpix[(y-1)*i_src_pitch+x]*3 )
              + ( p_inpix[(y-1)*i_src_pitch+x+1]<<1 )
              +   p_inpix[(y-1)*i_src_pitch+x+2]
              /* */
              +   p_inpix[y*i_src_pitch+x-2]
              + ( p_inpix[y*i_src_pitch+x-1]*3 )
              + ( p_inpix[y*i_src_pitch+x]<<2 )
              + ( p_inpix[y*i_src_pitch+x+1]*3 )
              +   p_inpix[y*i_src_pitch+x+2]
              /* 1 row down */
              +   p_inpix[(y+1)*i_src_pitch+x-2]
              + ( p_inpix[(y+1)*i_src_pitch+x-1]<<1 )
              + ( p_inpix[(y+1)*i_src_pitch+x]*3 )
              + ( p_inpix[(y+1)*i_src_pitch+x+1]<<1 )
              +   p_inpix[(y+1)*i_src_pitch+x+2] )<<1 )
              /* 2 rows down */
              + ( p_inpix[(y+2)*i_src_pitch+x-2] )
              + ((p_inpix[(y+2)*i_src_pitch+x-1]
              +   p_inpix[(y+2)*i_src_pitch+x]
              +   p_inpix[(y+2)*i_src_pitch+x+1])<<1 )
              + ( p_inpix[(y+2)*i_src_pitch+x+2] )
              ) >> 6 /* 115 */;
        }
    }
527
}