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