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