]> git.sesse.net Git - vlc/blob - modules/video_filter/seamcarving.c
Use vlc_memset/vlc_memcpy
[vlc] / modules / video_filter / seamcarving.c
1 /*****************************************************************************
2  * seamcarving.c: "Seam Carving for Content-Aware Image Resizing"
3  * Based on paper by Shai Avidan and Ariel Shamir.
4  *****************************************************************************
5  * Copyright (C) 2007 the VideoLAN team
6  * $Id$
7  *
8  * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc/vlc.h>
34 #include <vlc_sout.h>
35 #include <vlc_vout.h>
36
37 #include "vlc_filter.h"
38
39 #include <assert.h>
40
41 /*****************************************************************************
42  * Local prototypes
43  *****************************************************************************/
44 static int  Create    ( vlc_object_t * );
45 static void Destroy   ( vlc_object_t * );
46
47 static picture_t *Filter( filter_t *, picture_t * );
48 static int CropCallback( vlc_object_t *, char const *,
49                          vlc_value_t, vlc_value_t,
50                          void * );
51
52 static void FilterSeamCarving( filter_t *, picture_t *, picture_t * );
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57
58 #define FILTER_PREFIX "seamcarving-"
59
60 vlc_module_begin();
61     set_description( _("Seam Carving video filter") );
62     set_shortname( _( "Seam Carving" ));
63     set_capability( "video filter2", 0 );
64     set_category( CAT_VIDEO );
65     set_subcategory( SUBCAT_VIDEO_VFILTER );
66
67     set_callbacks( Create, Destroy );
68 vlc_module_end();
69
70 static const char *ppsz_filter_options[] = {
71     NULL
72 };
73
74 struct filter_sys_t
75 {
76     int *p_energy;
77     int *p_grad;
78
79     int i_crop;
80 };
81
82 static int Create( vlc_object_t *p_this )
83 {
84     filter_t *p_filter = (filter_t *)p_this;
85
86     /* Allocate structure */
87     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
88     if( p_filter->p_sys == NULL )
89     {
90         msg_Err( p_filter, "out of memory" );
91         return VLC_ENOMEM;
92     }
93
94     p_filter->pf_video_filter = Filter;
95     p_filter->p_sys->p_energy = NULL;
96     p_filter->p_sys->p_grad = NULL;
97     p_filter->p_sys->i_crop = 0;
98
99     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
100                    p_filter->p_cfg );
101
102     var_Create( p_filter, "crop", VLC_VAR_INTEGER|VLC_VAR_ISCOMMAND );
103     var_AddCallback( p_filter, "crop", CropCallback, p_filter->p_sys );
104
105     return VLC_SUCCESS;
106 }
107
108 static void Destroy( vlc_object_t *p_this )
109 {
110     filter_t *p_filter = (filter_t *)p_this;
111
112     free( p_filter->p_sys->p_energy );
113     free( p_filter->p_sys->p_grad );
114
115     free( p_filter->p_sys );
116 }
117
118 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
119 {
120     picture_t *p_outpic;
121
122     if( !p_pic ) return NULL;
123
124     p_outpic = p_filter->pf_vout_buffer_new( p_filter );
125     if( !p_outpic )
126     {
127         msg_Warn( p_filter, "can't get output picture" );
128         if( p_pic->pf_release )
129             p_pic->pf_release( p_pic );
130         return NULL;
131     }
132
133     FilterSeamCarving( p_filter, p_pic, p_outpic );
134
135     p_outpic->date = p_pic->date;
136     p_outpic->b_force = p_pic->b_force;
137     p_outpic->i_nb_fields = p_pic->i_nb_fields;
138     p_outpic->b_progressive = p_pic->b_progressive;
139     p_outpic->b_top_field_first = p_pic->b_top_field_first;
140
141     if( p_pic->pf_release )
142         p_pic->pf_release( p_pic );
143
144     return p_outpic;
145 }
146
147 static inline int my_min3( int a, int b, int c );
148 static inline int my_min( int a, int b );
149 static int RemoveVerticalSeam( filter_t *p_filter, picture_t *p_inpic, picture_t *p_outpic, int i_src_visible );
150
151 //#define DRAW_GRADIENT
152 //#define DRAW_ENERGY
153 //#define DRAW_SEAM
154
155 static void FilterSeamCarving( filter_t *p_filter, picture_t *p_inpic,
156                                                 picture_t *p_outpic )
157 {
158     const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
159     const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;
160
161     int i_src_visible = p_inpic->p[Y_PLANE].i_visible_pitch;
162
163     if( !p_filter->p_sys->p_energy )
164         p_filter->p_sys->p_energy = (int*)malloc(i_src_pitch * i_num_lines * sizeof(int));
165     if( !p_filter->p_sys->p_grad )
166         p_filter->p_sys->p_grad = (int*)malloc(i_src_pitch * i_num_lines * sizeof(int));
167
168 //#if defined( DRAW_GRADIENT ) || defined( DRAW_ENERGY ) || defined( DRAW_SEAM )
169     vlc_memcpy( p_outpic->p[Y_PLANE].p_pixels, p_inpic->p[Y_PLANE].p_pixels,
170         p_outpic->p[Y_PLANE].i_lines * p_outpic->p[Y_PLANE].i_pitch );
171 //#else
172 //    vlc_memset( p_outpix, 0x80,
173 //        p_outpic->p[Y_PLANE].i_lines * p_outpic->p[Y_PLANE].i_pitch );
174 //#endif
175     vlc_memset( p_outpic->p[U_PLANE].p_pixels, 0x80,
176         p_outpic->p[U_PLANE].i_lines * p_outpic->p[U_PLANE].i_pitch );
177     vlc_memset( p_outpic->p[V_PLANE].p_pixels, 0x80,
178         p_outpic->p[V_PLANE].i_lines * p_outpic->p[V_PLANE].i_pitch );
179
180 #if defined( DRAW_GRADIENT ) || defined( DRAW_ENERGY ) || defined( DRAW_SEAM )
181     i_src_visible = RemoveVerticalSeam( p_filter, p_outpic, p_outpic, i_src_visible );
182 #else
183     static int j = 1;
184     static int k = 1;
185     int i;
186     if( p_filter->p_sys->i_crop != 0 )
187         j = p_filter->p_sys->i_crop;
188     for( i = 0; i < j; i++ )
189     i_src_visible = RemoveVerticalSeam( p_filter, p_outpic, p_outpic, i_src_visible );
190     int y;
191     for( y = 0; y < p_outpic->p[Y_PLANE].i_lines; y++ )
192         vlc_memset( p_outpic->p[Y_PLANE].p_pixels + y*p_outpic->p[Y_PLANE].i_pitch + i_src_visible, 0x00, p_outpic->p[Y_PLANE].i_pitch - i_src_visible );
193     j += k;
194     if( j == 100 ) k = -1;
195     if( j == 1 ) k = 1;
196 #endif
197 }
198
199 static int ComputeGradient( filter_t *p_filter, picture_t *p_inpic, int i_src_visible )
200 {
201     int x, y;
202     const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
203     const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;
204
205     const uint8_t *p_inpix = p_inpic->p[Y_PLANE].p_pixels;
206     int *p_grad = p_filter->p_sys->p_grad;
207
208     for( y = 1; y < i_num_lines - 1;
209          y++, p_grad += i_src_pitch )
210     {
211         /* Compute line y's gradient */
212 #define GRADx( x ) \
213             ( \
214               abs( \
215                  ( p_inpix[(y-1)*i_src_pitch+x-1] \
216                    - p_inpix[(y+1)*i_src_pitch+x-1] ) \
217                + ( ( p_inpix[(y-1)*i_src_pitch+x] \
218                     - p_inpix[(y+1)*i_src_pitch+x] ) <<1 ) \
219                + ( p_inpix[(y-1)*i_src_pitch+x+1] \
220                    - p_inpix[(y+1)*i_src_pitch+x+1] ) \
221               ) \
222             + \
223               abs( \
224                  ( p_inpix[(y-1)*i_src_pitch+x-1] \
225                    - p_inpix[(y-1)*i_src_pitch+x+1] ) \
226                + ( ( p_inpix[y*i_src_pitch+x-1] \
227                     - p_inpix[y*i_src_pitch+x+1] ) <<1 ) \
228                + ( p_inpix[(y+1)*i_src_pitch+x-1] \
229                    - p_inpix[(y+1)*i_src_pitch+x+1] ) \
230               ) \
231             )
232         for( x = 1; x < i_src_visible - 1; x++ )
233         {
234             p_grad[x] = GRADx( x );
235 #ifdef DRAW_GRADIENT
236             p_outpix[y*i_src_pitch+x] = p_grad[x]>>3;
237 #endif
238         }
239     }
240     return 0;
241 }
242
243 static int RemoveVerticalSeam( filter_t *p_filter, picture_t *p_inpic, picture_t *p_outpic, int i_src_visible )
244 {
245     int x, y;
246     const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
247     const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;
248
249     uint8_t *p_outpix = p_outpic->p[Y_PLANE].p_pixels;
250
251     int *p_energy = p_filter->p_sys->p_energy;
252     int *p_grad = p_filter->p_sys->p_grad;
253
254     ComputeGradient( p_filter, p_inpic, i_src_visible );
255
256     /** Compute the image's energy (using a sobel gradient as the base energy
257      ** function) */
258     /* Set the first energy line to 0 */
259     memset( p_energy, 0, i_src_pitch*sizeof(int));
260
261     int *p_energy_prev = p_energy;
262     for( y = 1; y < i_num_lines - 1;
263          y++, p_energy_prev = p_energy, p_energy += i_src_pitch,
264          p_grad += i_src_pitch )
265     {
266         /* Compute line y's minimum energy value for paths ending on
267          * each x */
268         x = 1;
269         p_energy[x] = my_min( p_energy_prev[x  ]+p_grad[x  ],
270                            p_energy_prev[x+1]+p_grad[x+1] );
271         for( x = 2; x < i_src_visible - 2; x++ )
272         {
273             p_energy[x] = my_min3( p_energy_prev[x-1]+p_grad[x-1],
274                                 p_energy_prev[x  ]+p_grad[x  ],
275                                 p_energy_prev[x+1]+p_grad[x+1] );
276         }
277         p_energy[x] = my_min( p_energy_prev[x-1]+p_grad[x-1],
278                            p_energy_prev[x  ]+p_grad[x  ] );
279
280 #ifdef DRAW_ENERGY
281         int max = p_energy[1];
282         for( x = 1; x < i_src_visible - 1; x++ )
283             if( p_energy[x] > max ) max = p_energy[x];
284         for( x = 1; x < i_src_visible - 1; x++ )
285             p_outpix[y*i_src_pitch+x] = p_energy[x]*0xff/max;
286 #endif
287     }
288
289     /* Find the minimum energy point on the last line */
290     y--;
291     p_energy -= i_src_pitch;
292     p_grad -= i_src_pitch;
293
294     int m = p_energy[1];
295     int xmin = 1;
296     for( x = 1; x < i_src_visible - 1; x++ )
297     {
298         if( p_energy[x] < m )
299         {
300             m = p_energy[x];
301             xmin = x;
302         }
303     }
304
305 #ifdef DRAW_SEAM
306     p_outpix[y*i_src_pitch+xmin] = 0xff;
307     p_outpix[(y+1)*i_src_pitch+xmin] = 0xff;
308 #else
309     memmove( p_outpix+y*i_src_pitch+xmin, p_outpix+y*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
310     memmove( p_outpix+(y+1)*i_src_pitch+xmin, p_outpix+(y+1)*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
311 #endif
312
313     p_energy -= i_src_pitch;
314     for( ; y>1; y--, p_energy -= i_src_pitch, p_grad -= i_src_pitch )
315     {
316         if( m != p_energy[xmin]+p_grad[xmin] )
317         {
318             if( xmin > 1 && m == p_energy[xmin-1]+p_grad[xmin-1] )
319             {
320                 xmin--;
321             }
322             else if( xmin < i_src_visible - 2 && m == p_energy[xmin+1]+p_grad[xmin+1] )
323             {
324                 xmin++;
325             }
326             else
327             {
328                 printf("Alarm! %d\n" ,y);
329                 //assert( 0 );
330             }
331         }
332         m = p_energy[xmin];
333 #ifdef DRAW_SEAM
334         p_outpix[y*i_src_pitch+xmin] = 0xff;
335 #else
336         memmove( p_outpix+y*i_src_pitch+xmin, p_outpix+y*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
337 #endif
338     }
339 #ifdef DRAW_SEAM
340     p_outpix[y*i_src_pitch+xmin] = 0xff;
341 #else
342     memmove( p_outpix+y*i_src_pitch+xmin, p_outpix+y*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
343 #endif
344     y--;
345 #ifdef DRAW_SEAM
346     p_outpix[y*i_src_pitch+xmin] = 0xff;
347 #else
348     memmove( p_outpix+y*i_src_pitch+xmin, p_outpix+y*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
349 #endif
350
351 #if defined( DRAW_SEAM )
352     return i_src_visible;
353 #else
354     return i_src_visible-1;
355 #endif
356 }
357
358 static inline int my_min3( int a, int b, int c )
359 {
360     if( a < b )
361     {
362         if( a < c ) return a;
363         return c;
364     }
365     if( b < c ) return b;
366     return c;
367 }
368 static inline int my_min( int a, int b )
369 {
370     return a < b ? a : b;
371 }
372
373 static int CropCallback( vlc_object_t *p_this, char const *psz_var,
374                                 vlc_value_t oldval, vlc_value_t newval,
375                                 void *p_data )
376 {
377     VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
378     filter_sys_t *p_sys = (filter_sys_t *)p_data;
379     p_sys->i_crop = newval.i_int;
380     return VLC_SUCCESS;
381 }