]> git.sesse.net Git - vlc/blob - modules/video_filter/seamcarving.c
edaf74284bdc947b496feddb23eb8f3b10d77ecb
[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_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_sout.h>
36 #include <vlc_vout.h>
37
38 #include "vlc_filter.h"
39 #include "filter_picture.h"
40
41 #include <assert.h>
42
43 /*****************************************************************************
44  * Local prototypes
45  *****************************************************************************/
46 static int  Create    ( vlc_object_t * );
47 static void Destroy   ( vlc_object_t * );
48
49 static picture_t *Filter( filter_t *, picture_t * );
50 static int CropCallback( vlc_object_t *, char const *,
51                          vlc_value_t, vlc_value_t,
52                          void * );
53
54 static void FilterSeamCarving( filter_t *, picture_t *, picture_t * );
55
56 /*****************************************************************************
57  * Module descriptor
58  *****************************************************************************/
59
60 #define FILTER_PREFIX "seamcarving-"
61
62 vlc_module_begin();
63     set_description( N_("Seam Carving video filter") );
64     set_shortname( N_( "Seam Carving" ));
65     set_capability( "video filter2", 0 );
66     set_category( CAT_VIDEO );
67     set_subcategory( SUBCAT_VIDEO_VFILTER );
68
69     set_callbacks( Create, Destroy );
70 vlc_module_end();
71
72 struct filter_sys_t
73 {
74     int *p_energy;
75     int *p_grad;
76
77     int i_crop;
78 };
79
80 static int Create( vlc_object_t *p_this )
81 {
82     filter_t *p_filter = (filter_t *)p_this;
83
84     /* Allocate structure */
85     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
86     if( p_filter->p_sys == NULL )
87     {
88         msg_Err( p_filter, "out of memory" );
89         return VLC_ENOMEM;
90     }
91
92     p_filter->pf_video_filter = Filter;
93     p_filter->p_sys->p_energy = NULL;
94     p_filter->p_sys->p_grad = NULL;
95     p_filter->p_sys->i_crop = 0;
96
97     var_Create( p_filter, "crop", VLC_VAR_INTEGER|VLC_VAR_ISCOMMAND );
98     var_AddCallback( p_filter, "crop", CropCallback, p_filter->p_sys );
99
100     return VLC_SUCCESS;
101 }
102
103 static void Destroy( vlc_object_t *p_this )
104 {
105     filter_t *p_filter = (filter_t *)p_this;
106
107     free( p_filter->p_sys->p_energy );
108     free( p_filter->p_sys->p_grad );
109
110     free( p_filter->p_sys );
111 }
112
113 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
114 {
115     picture_t *p_outpic;
116
117     if( !p_pic ) return NULL;
118
119     p_outpic = p_filter->pf_vout_buffer_new( p_filter );
120     if( !p_outpic )
121     {
122         msg_Warn( p_filter, "can't get output picture" );
123         if( p_pic->pf_release )
124             p_pic->pf_release( p_pic );
125         return NULL;
126     }
127
128     FilterSeamCarving( p_filter, p_pic, p_outpic );
129
130     return CopyInfoAndRelease( p_outpic, p_pic );
131 }
132
133 static inline int my_min3( int a, int b, int c );
134 static inline int my_min( int a, int b );
135 static int RemoveVerticalSeam( filter_t *p_filter, picture_t *p_inpic, picture_t *p_outpic, int i_src_visible );
136
137 //#define DRAW_GRADIENT
138 //#define DRAW_ENERGY
139 //#define DRAW_SEAM
140
141 static void FilterSeamCarving( filter_t *p_filter, picture_t *p_inpic,
142                                                 picture_t *p_outpic )
143 {
144     const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
145     const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;
146
147     int i_src_visible = p_inpic->p[Y_PLANE].i_visible_pitch;
148
149     if( !p_filter->p_sys->p_energy )
150         p_filter->p_sys->p_energy = (int*)malloc(i_src_pitch * i_num_lines * sizeof(int));
151     if( !p_filter->p_sys->p_grad )
152         p_filter->p_sys->p_grad = (int*)malloc(i_src_pitch * i_num_lines * sizeof(int));
153
154 //#if defined( DRAW_GRADIENT ) || defined( DRAW_ENERGY ) || defined( DRAW_SEAM )
155     vlc_memcpy( p_outpic->p[Y_PLANE].p_pixels, p_inpic->p[Y_PLANE].p_pixels,
156         p_outpic->p[Y_PLANE].i_lines * p_outpic->p[Y_PLANE].i_pitch );
157 //#else
158 //    vlc_memset( p_outpix, 0x80,
159 //        p_outpic->p[Y_PLANE].i_lines * p_outpic->p[Y_PLANE].i_pitch );
160 //#endif
161     vlc_memset( p_outpic->p[U_PLANE].p_pixels, 0x80,
162         p_outpic->p[U_PLANE].i_lines * p_outpic->p[U_PLANE].i_pitch );
163     vlc_memset( p_outpic->p[V_PLANE].p_pixels, 0x80,
164         p_outpic->p[V_PLANE].i_lines * p_outpic->p[V_PLANE].i_pitch );
165
166 #if defined( DRAW_GRADIENT ) || defined( DRAW_ENERGY ) || defined( DRAW_SEAM )
167     i_src_visible = RemoveVerticalSeam( p_filter, p_outpic, p_outpic, i_src_visible );
168 #else
169     static int j = 1;
170     static int k = 1;
171     int i;
172     if( p_filter->p_sys->i_crop != 0 )
173         j = p_filter->p_sys->i_crop;
174     for( i = 0; i < j; i++ )
175     i_src_visible = RemoveVerticalSeam( p_filter, p_outpic, p_outpic, i_src_visible );
176     int y;
177     for( y = 0; y < p_outpic->p[Y_PLANE].i_lines; y++ )
178         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 );
179     j += k;
180     if( j == 100 ) k = -1;
181     if( j == 1 ) k = 1;
182 #endif
183 }
184
185 static int ComputeGradient( filter_t *p_filter, picture_t *p_inpic, int i_src_visible )
186 {
187     int x, y;
188     const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
189     const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;
190
191     const uint8_t *p_inpix = p_inpic->p[Y_PLANE].p_pixels;
192     int *p_grad = p_filter->p_sys->p_grad;
193
194     for( y = 1; y < i_num_lines - 1;
195          y++, p_grad += i_src_pitch )
196     {
197         /* Compute line y's gradient */
198 #define GRADx( x ) \
199             ( \
200               abs( \
201                  ( p_inpix[(y-1)*i_src_pitch+x-1] \
202                    - p_inpix[(y+1)*i_src_pitch+x-1] ) \
203                + ( ( p_inpix[(y-1)*i_src_pitch+x] \
204                     - p_inpix[(y+1)*i_src_pitch+x] ) <<1 ) \
205                + ( p_inpix[(y-1)*i_src_pitch+x+1] \
206                    - p_inpix[(y+1)*i_src_pitch+x+1] ) \
207               ) \
208             + \
209               abs( \
210                  ( p_inpix[(y-1)*i_src_pitch+x-1] \
211                    - p_inpix[(y-1)*i_src_pitch+x+1] ) \
212                + ( ( p_inpix[y*i_src_pitch+x-1] \
213                     - p_inpix[y*i_src_pitch+x+1] ) <<1 ) \
214                + ( p_inpix[(y+1)*i_src_pitch+x-1] \
215                    - p_inpix[(y+1)*i_src_pitch+x+1] ) \
216               ) \
217             )
218         for( x = 1; x < i_src_visible - 1; x++ )
219         {
220             p_grad[x] = GRADx( x );
221 #ifdef DRAW_GRADIENT
222             p_outpix[y*i_src_pitch+x] = p_grad[x]>>3;
223 #endif
224         }
225     }
226     return 0;
227 }
228
229 static int RemoveVerticalSeam( filter_t *p_filter, picture_t *p_inpic, picture_t *p_outpic, int i_src_visible )
230 {
231     int x, y;
232     const int i_src_pitch = p_inpic->p[Y_PLANE].i_pitch;
233     const int i_num_lines = p_inpic->p[Y_PLANE].i_visible_lines;
234
235     uint8_t *p_outpix = p_outpic->p[Y_PLANE].p_pixels;
236
237     int *p_energy = p_filter->p_sys->p_energy;
238     int *p_grad = p_filter->p_sys->p_grad;
239
240     ComputeGradient( p_filter, p_inpic, i_src_visible );
241
242     /** Compute the image's energy (using a sobel gradient as the base energy
243      ** function) */
244     /* Set the first energy line to 0 */
245     memset( p_energy, 0, i_src_pitch*sizeof(int));
246
247     int *p_energy_prev = p_energy;
248     for( y = 1; y < i_num_lines - 1;
249          y++, p_energy_prev = p_energy, p_energy += i_src_pitch,
250          p_grad += i_src_pitch )
251     {
252         /* Compute line y's minimum energy value for paths ending on
253          * each x */
254         x = 1;
255         p_energy[x] = my_min( p_energy_prev[x  ]+p_grad[x  ],
256                            p_energy_prev[x+1]+p_grad[x+1] );
257         for( x = 2; x < i_src_visible - 2; x++ )
258         {
259             p_energy[x] = my_min3( p_energy_prev[x-1]+p_grad[x-1],
260                                 p_energy_prev[x  ]+p_grad[x  ],
261                                 p_energy_prev[x+1]+p_grad[x+1] );
262         }
263         p_energy[x] = my_min( p_energy_prev[x-1]+p_grad[x-1],
264                            p_energy_prev[x  ]+p_grad[x  ] );
265
266 #ifdef DRAW_ENERGY
267         int max = p_energy[1];
268         for( x = 1; x < i_src_visible - 1; x++ )
269             if( p_energy[x] > max ) max = p_energy[x];
270         for( x = 1; x < i_src_visible - 1; x++ )
271             p_outpix[y*i_src_pitch+x] = p_energy[x]*0xff/max;
272 #endif
273     }
274
275     /* Find the minimum energy point on the last line */
276     y--;
277     p_energy -= i_src_pitch;
278     p_grad -= i_src_pitch;
279
280     int m = p_energy[1];
281     int xmin = 1;
282     for( x = 1; x < i_src_visible - 1; x++ )
283     {
284         if( p_energy[x] < m )
285         {
286             m = p_energy[x];
287             xmin = x;
288         }
289     }
290
291 #ifdef DRAW_SEAM
292     p_outpix[y*i_src_pitch+xmin] = 0xff;
293     p_outpix[(y+1)*i_src_pitch+xmin] = 0xff;
294 #else
295     memmove( p_outpix+y*i_src_pitch+xmin, p_outpix+y*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
296     memmove( p_outpix+(y+1)*i_src_pitch+xmin, p_outpix+(y+1)*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
297 #endif
298
299     p_energy -= i_src_pitch;
300     for( ; y>1; y--, p_energy -= i_src_pitch, p_grad -= i_src_pitch )
301     {
302         if( m != p_energy[xmin]+p_grad[xmin] )
303         {
304             if( xmin > 1 && m == p_energy[xmin-1]+p_grad[xmin-1] )
305             {
306                 xmin--;
307             }
308             else if( xmin < i_src_visible - 2 && m == p_energy[xmin+1]+p_grad[xmin+1] )
309             {
310                 xmin++;
311             }
312             else
313             {
314                 printf("Alarm! %d\n" ,y);
315                 //assert( 0 );
316             }
317         }
318         m = p_energy[xmin];
319 #ifdef DRAW_SEAM
320         p_outpix[y*i_src_pitch+xmin] = 0xff;
321 #else
322         memmove( p_outpix+y*i_src_pitch+xmin, p_outpix+y*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
323 #endif
324     }
325 #ifdef DRAW_SEAM
326     p_outpix[y*i_src_pitch+xmin] = 0xff;
327 #else
328     memmove( p_outpix+y*i_src_pitch+xmin, p_outpix+y*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
329 #endif
330     y--;
331 #ifdef DRAW_SEAM
332     p_outpix[y*i_src_pitch+xmin] = 0xff;
333 #else
334     memmove( p_outpix+y*i_src_pitch+xmin, p_outpix+y*i_src_pitch+xmin+1, i_src_pitch-(xmin+1) );
335 #endif
336
337 #if defined( DRAW_SEAM )
338     return i_src_visible;
339 #else
340     return i_src_visible-1;
341 #endif
342 }
343
344 static inline int my_min3( int a, int b, int c )
345 {
346     if( a < b )
347     {
348         if( a < c ) return a;
349         return c;
350     }
351     if( b < c ) return b;
352     return c;
353 }
354 static inline int my_min( int a, int b )
355 {
356     return a < b ? a : b;
357 }
358
359 static int CropCallback( vlc_object_t *p_this, char const *psz_var,
360                                 vlc_value_t oldval, vlc_value_t newval,
361                                 void *p_data )
362 {
363     VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
364     filter_sys_t *p_sys = (filter_sys_t *)p_data;
365     p_sys->i_crop = newval.i_int;
366     return VLC_SUCCESS;
367 }