]> git.sesse.net Git - vlc/blob - modules/video_filter/puzzle_mgt.c
mediacodec: don't loop in GetOutput
[vlc] / modules / video_filter / puzzle_mgt.c
1 /*****************************************************************************
2  * puzzle_mgt.c : Puzzle game filter - game management
3  *****************************************************************************
4  * Copyright (C) 2005-2009 VLC authors and VideoLAN
5  * Copyright (C) 2013      Vianney Boyer
6  * $Id$
7  *
8  * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
9  *          Vianney Boyer <vlcvboyer -at- gmail -dot- com>
10  *
11  * This program is free software; you can redistribute it and/or modify it
12  * under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation; either version 2.1 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33 #include <math.h>
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_filter.h>
38 #include <vlc_rand.h>
39
40 #include "filter_picture.h"
41
42 #include "puzzle_bezier.h"
43 #include "puzzle_lib.h"
44 #include "puzzle_pce.h"
45 #include "puzzle_mgt.h"
46
47 /*****************************************************************************
48  * puzzle_bake: compute general puzzle data and allocate structures
49  *****************************************************************************/
50 int puzzle_bake( filter_t *p_filter, picture_t *p_pic_out, picture_t *p_pic_in)
51 {
52     filter_sys_t *p_sys = p_filter->p_sys;
53     int i_ret = 0;
54
55     puzzle_free_ps_puzzle_array ( p_filter );
56     puzzle_free_ps_pieces_shapes ( p_filter);
57     puzzle_free_ps_pieces ( p_filter );
58
59     p_sys->s_allocated.i_rows =          p_sys->s_current_param.i_rows;
60     p_sys->s_allocated.i_cols =          p_sys->s_current_param.i_cols;
61     p_sys->s_allocated.i_planes =        p_sys->s_current_param.i_planes;
62     p_sys->s_allocated.i_piece_types =   ((p_sys->s_current_param.b_advanced)?PIECE_TYPE_NBR:0);
63     p_sys->s_allocated.i_pieces_nbr =    p_sys->s_allocated.i_rows * p_sys->s_allocated.i_cols;
64     p_sys->s_allocated.b_preview =       p_sys->s_current_param.b_preview;
65     p_sys->s_allocated.i_preview_size =  p_sys->s_current_param.i_preview_size;
66     p_sys->s_allocated.i_border =        p_sys->s_current_param.i_border;
67     p_sys->s_allocated.b_blackslot =     p_sys->s_current_param.b_blackslot;
68     p_sys->s_allocated.b_near =          p_sys->s_current_param.b_near;
69     p_sys->s_allocated.i_shape_size =    p_sys->s_current_param.i_shape_size;
70     p_sys->s_allocated.b_advanced =      p_sys->s_current_param.b_advanced;
71     p_sys->s_allocated.i_auto_shuffle_speed = p_sys->s_current_param.i_auto_shuffle_speed;
72     p_sys->s_allocated.i_auto_solve_speed =   p_sys->s_current_param.i_auto_solve_speed;
73     p_sys->s_allocated.i_rotate =        p_sys->s_current_param.i_rotate;
74
75     p_sys->ps_puzzle_array = malloc( sizeof( puzzle_array_t** ) * (p_sys->s_allocated.i_rows + 1));
76     if( !p_sys->ps_puzzle_array )
77         return VLC_ENOMEM;
78
79     for (int32_t r=0; r < p_sys->s_allocated.i_rows + 1; r++) {
80         p_sys->ps_puzzle_array[r] = malloc( sizeof( puzzle_array_t* ) * (p_sys->s_allocated.i_cols + 1));
81         if( !p_sys->ps_puzzle_array[r] )
82             return VLC_ENOMEM;
83         for (int32_t c=0; c < p_sys->s_allocated.i_cols + 1; c++) {
84             p_sys->ps_puzzle_array[r][c] = malloc( sizeof( puzzle_array_t ) * p_sys->s_allocated.i_planes);
85             if( !p_sys->ps_puzzle_array[r][c] )
86                 return VLC_ENOMEM;
87         }
88     }
89
90     p_sys->ps_desk_planes = malloc( sizeof( puzzle_plane_t ) * p_sys->s_allocated.i_planes);
91     if( !p_sys->ps_desk_planes )
92         return VLC_ENOMEM;
93     p_sys->ps_pict_planes = malloc( sizeof( puzzle_plane_t ) * p_sys->s_allocated.i_planes);
94     if( !p_sys->ps_pict_planes )
95         return VLC_ENOMEM;
96
97     for (uint8_t i_plane = 0; i_plane < p_sys->s_allocated.i_planes; i_plane++) {
98         p_sys->ps_desk_planes[i_plane].i_lines = p_pic_out->p[i_plane].i_visible_lines;
99         p_sys->ps_desk_planes[i_plane].i_pitch = p_pic_out->p[i_plane].i_pitch;
100         p_sys->ps_desk_planes[i_plane].i_visible_pitch = p_pic_out->p[i_plane].i_visible_pitch;
101         p_sys->ps_desk_planes[i_plane].i_pixel_pitch = p_pic_out->p[i_plane].i_pixel_pitch;
102         p_sys->ps_desk_planes[i_plane].i_width = p_pic_out->p[i_plane].i_visible_pitch / p_pic_out->p[i_plane].i_pixel_pitch;
103
104         p_sys->ps_desk_planes[i_plane].i_preview_width = p_sys->ps_desk_planes[i_plane].i_width * p_sys->s_current_param.i_preview_size / 100;
105         p_sys->ps_desk_planes[i_plane].i_preview_lines = p_sys->ps_desk_planes[i_plane].i_lines * p_sys->s_current_param.i_preview_size / 100;
106
107         p_sys->ps_desk_planes[i_plane].i_border_width = p_sys->ps_desk_planes[i_plane].i_width * p_sys->s_current_param.i_border / 2 / 100;
108         p_sys->ps_desk_planes[i_plane].i_border_lines = p_sys->ps_desk_planes[i_plane].i_lines * p_sys->s_current_param.i_border / 2 / 100;
109
110         p_sys->ps_desk_planes[i_plane].i_pce_max_width = (( p_sys->ps_desk_planes[i_plane].i_width
111                 - 2 * p_sys->ps_desk_planes[i_plane].i_border_width ) + p_sys->s_allocated.i_cols - 1 ) / p_sys->s_allocated.i_cols;
112         p_sys->ps_desk_planes[i_plane].i_pce_max_lines = (( p_sys->ps_desk_planes[i_plane].i_lines
113                 - 2 * p_sys->ps_desk_planes[i_plane].i_border_lines ) + p_sys->s_allocated.i_rows - 1 ) / p_sys->s_allocated.i_rows;
114
115         p_sys->ps_pict_planes[i_plane].i_lines = p_pic_in->p[i_plane].i_visible_lines;
116         p_sys->ps_pict_planes[i_plane].i_pitch = p_pic_in->p[i_plane].i_pitch;
117         p_sys->ps_pict_planes[i_plane].i_visible_pitch = p_pic_in->p[i_plane].i_visible_pitch;
118         p_sys->ps_pict_planes[i_plane].i_pixel_pitch = p_pic_in->p[i_plane].i_pixel_pitch;
119         p_sys->ps_pict_planes[i_plane].i_width = p_pic_in->p[i_plane].i_visible_pitch / p_pic_in->p[i_plane].i_pixel_pitch;
120
121         p_sys->ps_pict_planes[i_plane].i_preview_width = p_sys->ps_pict_planes[i_plane].i_width * p_sys->s_current_param.i_preview_size / 100;
122         p_sys->ps_pict_planes[i_plane].i_preview_lines = p_sys->ps_pict_planes[i_plane].i_lines * p_sys->s_current_param.i_preview_size / 100;
123
124         p_sys->ps_pict_planes[i_plane].i_border_width = p_sys->ps_pict_planes[i_plane].i_width * p_sys->s_current_param.i_border / 2 / 100;
125         p_sys->ps_pict_planes[i_plane].i_border_lines = p_sys->ps_pict_planes[i_plane].i_lines * p_sys->s_current_param.i_border / 2 / 100;
126
127         p_sys->ps_pict_planes[i_plane].i_pce_max_width = (( p_sys->ps_desk_planes[i_plane].i_width
128                 - 2 * p_sys->ps_pict_planes[i_plane].i_border_width ) + p_sys->s_allocated.i_cols - 1 ) / p_sys->s_allocated.i_cols;
129         p_sys->ps_pict_planes[i_plane].i_pce_max_lines = (( p_sys->ps_pict_planes[i_plane].i_lines
130                 - 2 * p_sys->ps_pict_planes[i_plane].i_border_lines ) + p_sys->s_allocated.i_rows - 1 ) / p_sys->s_allocated.i_rows;
131
132         for (int32_t r = 0; r < p_sys->s_allocated.i_rows; r++)
133             for (int32_t c = 0; c < p_sys->s_allocated.i_cols; c++) {
134                 if ( r == 0 )
135                     p_sys->ps_puzzle_array[r][c][i_plane].i_y = p_sys->ps_pict_planes[i_plane].i_border_lines;
136                 if ( c == 0 )
137                     p_sys->ps_puzzle_array[r][c][i_plane].i_x = p_sys->ps_pict_planes[i_plane].i_border_width;
138                 p_sys->ps_puzzle_array[r][c][i_plane].i_width =
139                     (p_sys->ps_pict_planes[i_plane].i_width - p_sys->ps_pict_planes[i_plane].i_border_width
140                     - p_sys->ps_puzzle_array[r][c][i_plane].i_x) / ( p_sys->s_allocated.i_cols - c );
141                 p_sys->ps_puzzle_array[r][c][i_plane].i_lines =
142                     (p_sys->ps_pict_planes[i_plane].i_lines - p_sys->ps_pict_planes[i_plane].i_border_lines
143                     - p_sys->ps_puzzle_array[r][c][i_plane].i_y) / ( p_sys->s_allocated.i_rows - r );
144                 p_sys->ps_puzzle_array[r][c + 1][i_plane].i_x =
145                     p_sys->ps_puzzle_array[r][c][i_plane].i_x + p_sys->ps_puzzle_array[r][c][i_plane].i_width;
146                 p_sys->ps_puzzle_array[r + 1][c][i_plane].i_y =
147                     p_sys->ps_puzzle_array[r][c][i_plane].i_y + p_sys->ps_puzzle_array[r][c][i_plane].i_lines;
148             }
149     }
150
151     p_sys->i_magnet_accuracy = ( p_sys->s_current_param.i_pict_width / 50) + 3;
152
153     if (p_sys->s_current_param.b_advanced && p_sys->s_allocated.i_shape_size != 0) {
154         i_ret = puzzle_bake_pieces_shapes ( p_filter );
155         if (i_ret != VLC_SUCCESS)
156             return i_ret;
157     }
158
159     i_ret = puzzle_bake_piece ( p_filter );
160     if (i_ret != VLC_SUCCESS)
161         return i_ret;
162
163     if ( (p_sys->pi_order != NULL) && (p_sys->ps_desk_planes != NULL) && (p_sys->ps_pict_planes != NULL)
164         && (p_sys->ps_puzzle_array != NULL) && (p_sys->ps_pieces != NULL) )
165         p_sys->b_init = true;
166
167     if ( (p_sys->ps_pieces_shapes == NULL) && (p_sys->s_current_param.b_advanced)
168         && (p_sys->s_current_param.i_shape_size != 0) )
169         p_sys->b_init = false;
170
171     return VLC_SUCCESS;
172 }
173
174 void puzzle_free_ps_puzzle_array( filter_t *p_filter)
175 {
176     filter_sys_t *p_sys = p_filter->p_sys;
177
178     if (p_sys->ps_puzzle_array != NULL) {
179         for (int32_t r=0; r < p_sys->s_allocated.i_rows + 1; r++) {
180             for (int32_t c=0; c < p_sys->s_allocated.i_cols + 1; c++)
181                 free( p_sys->ps_puzzle_array[r][c] );
182             free( p_sys->ps_puzzle_array[r] );
183         }
184         free( p_sys->ps_puzzle_array );
185     }
186     p_sys->ps_puzzle_array = NULL;
187
188     free ( p_sys->ps_desk_planes );
189     p_sys->ps_desk_planes = NULL;
190
191     free ( p_sys->ps_pict_planes );
192     p_sys->ps_pict_planes = NULL;
193
194     return;
195 }
196
197 /*****************************************************************************
198  * puzzle_bake_piece: compute data dedicated to each piece
199  *****************************************************************************/
200 int puzzle_bake_piece( filter_t *p_filter)
201 {
202     int i_ret = puzzle_allocate_ps_pieces( p_filter);
203     if (i_ret != VLC_SUCCESS)
204         return i_ret;
205
206     filter_sys_t *p_sys = p_filter->p_sys;
207
208     /* generates random pi_order array */
209     i_ret = puzzle_shuffle( p_filter );
210     if (i_ret != VLC_SUCCESS)
211         return i_ret;
212
213     int32_t i = 0;
214     for (int32_t row = 0; row < p_sys->s_allocated.i_rows; row++) {
215         for (int32_t col = 0; col < p_sys->s_allocated.i_cols; col++) {
216             int32_t orow = row;
217             int32_t ocol = col;
218
219             if (p_sys->pi_order != NULL) {
220                 orow = p_sys->pi_order[i] / (p_sys->s_allocated.i_cols);
221                 ocol = p_sys->pi_order[i] % (p_sys->s_allocated.i_cols);
222             }
223
224             p_sys->ps_pieces[i].i_original_row = orow;
225             p_sys->ps_pieces[i].i_original_col = ocol;
226
227             /* set bottom and right shapes */
228             p_sys->ps_pieces[i].i_left_shape  = 0;
229             p_sys->ps_pieces[i].i_top_shape   = 2;
230             p_sys->ps_pieces[i].i_btm_shape   = 4;
231             p_sys->ps_pieces[i].i_right_shape = 6;
232
233             if (p_sys->s_allocated.i_shape_size > 0) {
234
235                 if (orow < p_sys->s_allocated.i_rows - 1)
236                     p_sys->ps_pieces[i].i_btm_shape = 4 + 8 + 8*(( (unsigned) vlc_mrand48()) % ( SHAPES_QTY ) ) + (vlc_mrand48() & 0x01);
237
238                 if (ocol < p_sys->s_allocated.i_cols - 1)
239                     p_sys->ps_pieces[i].i_right_shape = 6 + 8 + 8*(( (unsigned) vlc_mrand48()) % ( SHAPES_QTY ) ) + (vlc_mrand48() & 0x01);
240             }
241
242             /* set piece data */
243             p_sys->ps_pieces[i].i_actual_angle   = 0;
244             p_sys->ps_pieces[i].b_overlap        = false;
245             p_sys->ps_pieces[i].i_actual_mirror  = +1;
246             p_sys->ps_pieces[i].b_finished       = ((ocol == col) && (orow == row));
247             p_sys->ps_pieces[i].i_group_ID       = i;
248
249             /* add small random offset to location */
250             int32_t i_rand_x = 0;
251             int32_t i_rand_y = 0;
252             if (p_sys->s_current_param.b_advanced) {
253                 i_rand_x = (( (unsigned) vlc_mrand48()) % ( p_sys->ps_desk_planes[0].i_pce_max_width + 1 ) ) - (int32_t) p_sys->ps_desk_planes[0].i_pce_max_width / 2;
254                 i_rand_y = (( (unsigned) vlc_mrand48()) % ( p_sys->ps_desk_planes[0].i_pce_max_lines + 1 ) ) - (int32_t) p_sys->ps_desk_planes[0].i_pce_max_lines / 2;
255             }
256
257             /* copy related puzzle data to piece data */
258             if (p_sys->ps_puzzle_array != NULL) {
259                 for (uint8_t i_plane = 0; i_plane < p_sys->s_allocated.i_planes; i_plane++) {
260
261                     p_sys->ps_pieces[i].ps_piece_in_plane[i_plane].i_width = p_sys->ps_puzzle_array[row][col][i_plane].i_width;
262                     p_sys->ps_pieces[i].ps_piece_in_plane[i_plane].i_lines = p_sys->ps_puzzle_array[row][col][i_plane].i_lines;
263                     p_sys->ps_pieces[i].ps_piece_in_plane[i_plane].i_original_x = p_sys->ps_puzzle_array[orow][ocol][i_plane].i_x;
264                     p_sys->ps_pieces[i].ps_piece_in_plane[i_plane].i_original_y = p_sys->ps_puzzle_array[orow][ocol][i_plane].i_y;
265                     p_sys->ps_pieces[i].ps_piece_in_plane[i_plane].i_actual_x = p_sys->ps_puzzle_array[row][col][i_plane].i_x + i_rand_x *
266                         p_sys->ps_desk_planes[i_plane].i_width / p_sys->ps_desk_planes[0].i_width;
267                     p_sys->ps_pieces[i].ps_piece_in_plane[i_plane].i_actual_y = p_sys->ps_puzzle_array[row][col][i_plane].i_y + i_rand_y *
268                         p_sys->ps_desk_planes[i_plane].i_lines / p_sys->ps_desk_planes[0].i_lines;
269
270                     if (i_plane == 0) {
271
272                         p_sys->ps_pieces[i].i_OLx = p_sys->ps_pieces[i].ps_piece_in_plane[0].i_original_x;
273                         p_sys->ps_pieces[i].i_OTy = p_sys->ps_pieces[i].ps_piece_in_plane[0].i_original_y;
274                         p_sys->ps_pieces[i].i_ORx = p_sys->ps_pieces[i].ps_piece_in_plane[0].i_original_x + p_sys->ps_pieces[i].ps_piece_in_plane[0].i_width - 1;
275                         p_sys->ps_pieces[i].i_OBy = p_sys->ps_pieces[i].ps_piece_in_plane[0].i_original_y + p_sys->ps_pieces[i].ps_piece_in_plane[0].i_lines - 1;
276
277                         puzzle_calculate_corners( p_filter, i );
278                     }
279                 }
280             }
281             i++;
282         }
283     }
284
285     /* left and top shapes are based on negative right and bottom ones */
286     puzzle_set_left_top_shapes( p_filter);
287
288     /* add random rotation to each piece */
289     puzzle_random_rotate( p_filter);
290
291     return VLC_SUCCESS;
292 }
293
294 /*****************************************************************************
295  * puzzle_set_left_top_shapes: Set the shape ID to be used for top and left
296  *                             edges of each piece. Those ID will be set to
297  *                             be the negative of bottom or right of adjacent
298  *                             piece.
299  *****************************************************************************/
300 void puzzle_set_left_top_shapes( filter_t *p_filter)
301 {
302     filter_sys_t *p_sys = p_filter->p_sys;
303     /* for each piece on the desk... */
304     for (uint16_t i_pce_B=0; i_pce_B < p_sys->s_allocated.i_pieces_nbr; i_pce_B++)
305         /* ...search adjacent piece */
306         for (uint16_t i_pce_A=0;     i_pce_A < p_sys->s_allocated.i_pieces_nbr; i_pce_A++)
307         {
308             if (     ( p_sys->ps_pieces[i_pce_A].i_original_row == p_sys->ps_pieces[i_pce_B].i_original_row )
309                   && ( p_sys->ps_pieces[i_pce_A].i_original_col == p_sys->ps_pieces[i_pce_B].i_original_col-1 ) )
310                 /* left shape is based on negative (invert ID LSB) right one */
311                 p_sys->ps_pieces[i_pce_B].i_left_shape = (p_sys->ps_pieces[i_pce_A].i_right_shape - 6 ) ^ 0x01;
312
313             if (     ( p_sys->ps_pieces[i_pce_A].i_original_row == p_sys->ps_pieces[i_pce_B].i_original_row - 1 )
314                   && ( p_sys->ps_pieces[i_pce_A].i_original_col == p_sys->ps_pieces[i_pce_B].i_original_col ) )
315                 /* top shape is based on negative of bottom one
316                  *                 top ID = bottom ID - 2
317                  *                 negative ID = invert LSB
318                  */
319                 p_sys->ps_pieces[i_pce_B].i_top_shape = (p_sys->ps_pieces[i_pce_A].i_btm_shape - 2 ) ^ 0x01;
320         }
321 }
322
323 void puzzle_random_rotate( filter_t *p_filter)
324 {
325     filter_sys_t *p_sys = p_filter->p_sys;
326     /* add random rotation to each piece */
327     for (uint32_t i = 0; i < p_sys->s_allocated.i_pieces_nbr; i++)
328     {
329         p_sys->ps_pieces[i].i_actual_angle  = 0;
330         p_sys->ps_pieces[i].i_actual_mirror = +1;
331
332         switch ( p_sys->s_current_param.i_rotate )
333         {
334           case 1:
335                 puzzle_rotate_pce( p_filter, i, (( (unsigned) vlc_mrand48()) % ( 2 ) ) * 2, p_sys->ps_pieces[i].i_center_x, p_sys->ps_pieces[i].i_center_y, false );
336             break;
337           case 2:
338                 puzzle_rotate_pce( p_filter, i, (( (unsigned) vlc_mrand48()) % ( 4 ) ), p_sys->ps_pieces[i].i_center_x, p_sys->ps_pieces[i].i_center_y, false );
339             break;
340           case 3:
341                 puzzle_rotate_pce( p_filter, i, (( (unsigned) vlc_mrand48()) % ( 8 ) ), p_sys->ps_pieces[i].i_center_x, p_sys->ps_pieces[i].i_center_y, false );
342             break;
343         }
344         puzzle_calculate_corners( p_filter, i );
345     }
346 }
347
348 void puzzle_free_ps_pieces( filter_t *p_filter)
349 {
350     filter_sys_t *p_sys = p_filter->p_sys;
351
352     if (p_sys->ps_pieces != NULL) {
353         for (uint32_t i_pce = 0; i_pce < p_sys->s_allocated.i_pieces_nbr; i_pce++)
354             free( p_sys->ps_pieces[i_pce].ps_piece_in_plane );
355         free( p_sys->ps_pieces );
356     }
357     p_sys->ps_pieces = NULL;
358
359     free( p_sys->pi_order );
360     p_sys->pi_order = NULL;
361
362     free( p_sys->ps_pieces_tmp );
363     p_sys->ps_pieces_tmp = NULL;
364
365     free( p_sys->pi_group_qty );
366     p_sys->pi_group_qty = NULL;
367
368     return;
369 }
370
371 int puzzle_allocate_ps_pieces( filter_t *p_filter)
372 {
373     filter_sys_t *p_sys = p_filter->p_sys;
374     puzzle_free_ps_pieces(p_filter);
375     p_sys->s_allocated.i_pieces_nbr = p_sys->s_allocated.i_rows * p_sys->s_allocated.i_cols;
376     p_sys->ps_pieces = malloc( sizeof( piece_t) * p_sys->s_allocated.i_pieces_nbr );
377     if( !p_sys->ps_pieces )
378         return VLC_ENOMEM;
379     for (uint32_t p = 0; p < p_sys->s_allocated.i_pieces_nbr; p++) {
380         p_sys->ps_pieces[p].ps_piece_in_plane = malloc( sizeof( piece_in_plane_t) * p_sys->s_allocated.i_planes );
381         if( !p_sys->ps_pieces[p].ps_piece_in_plane ) {
382             for (uint32_t i=0;i<p;i++)
383                 free(p_sys->ps_pieces[i].ps_piece_in_plane);
384             free(p_sys->ps_pieces);
385             p_sys->ps_pieces = NULL;
386             return VLC_ENOMEM;
387         }
388     }
389
390     p_sys->ps_pieces_tmp = malloc( sizeof( piece_t) * p_sys->s_allocated.i_pieces_nbr );
391     if( !p_sys->ps_pieces_tmp ) {
392         for (uint32_t i=0;i<p_sys->s_allocated.i_pieces_nbr;i++)
393             free(p_sys->ps_pieces[i].ps_piece_in_plane);
394         free(p_sys->ps_pieces);
395         p_sys->ps_pieces = NULL;
396         return VLC_ENOMEM;
397     }
398     p_sys->pi_group_qty = malloc( sizeof( int32_t ) * (p_sys->s_allocated.i_pieces_nbr));
399     if( !p_sys->pi_group_qty ) {
400         for (uint32_t i=0;i<p_sys->s_allocated.i_pieces_nbr;i++)
401             free(p_sys->ps_pieces[i].ps_piece_in_plane);
402         free(p_sys->ps_pieces);
403         p_sys->ps_pieces = NULL;
404         free(p_sys->ps_pieces_tmp);
405         p_sys->ps_pieces_tmp = NULL;
406         return VLC_ENOMEM;
407     }
408     return VLC_SUCCESS;
409 }
410
411 bool puzzle_is_valid( filter_sys_t *p_sys, int32_t *pi_pce_lst )
412 {
413     const int32_t i_count = p_sys->s_allocated.i_pieces_nbr;
414
415     if( !p_sys->s_current_param.b_blackslot )
416         return true;
417
418     int32_t d = 0;
419     for( int32_t i = 0; i < i_count; i++ ) {
420         if( pi_pce_lst[i] == i_count - 1 ) {
421             d += i / p_sys->s_allocated.i_cols + 1;
422             continue;
423         }
424         for( int32_t j = i+1; j < i_count; j++ ) {
425             if( pi_pce_lst[j] == i_count - 1 )
426                 continue;
427             if( pi_pce_lst[i] > pi_pce_lst[j] )
428                 d++;
429         }
430     }
431     return (d%2) == 0;
432 }
433
434 int puzzle_shuffle( filter_t *p_filter )
435 {
436     filter_sys_t *p_sys = p_filter->p_sys;
437
438     int32_t i_pieces_nbr = p_sys->s_allocated.i_pieces_nbr;
439
440     do
441     {
442         int i_ret = puzzle_generate_rand_pce_list( p_filter, &p_sys->pi_order );
443         if (i_ret != VLC_SUCCESS)
444             return i_ret;
445     } while( puzzle_is_finished( p_sys, p_sys->pi_order ) || !puzzle_is_valid( p_sys, p_sys->pi_order ) );
446
447
448     if( p_sys->s_current_param.b_blackslot ) {
449         for( int32_t i = 0; i < i_pieces_nbr; i++ )
450             if( p_sys->pi_order[i] == i_pieces_nbr - 1 ) {
451                 p_sys->i_selected = i;
452                 break;
453             }
454     }
455     else {
456         p_sys->i_selected = NO_PCE;
457     }
458
459     p_sys->b_shuffle_rqst = false;
460     p_sys->b_finished = false;
461
462     return VLC_SUCCESS;
463 }
464
465 int puzzle_generate_rand_pce_list( filter_t *p_filter, int32_t **pi_pce_lst )
466 {
467     filter_sys_t *p_sys = p_filter->p_sys;
468     int32_t i_pieces_nbr = p_sys->s_allocated.i_pieces_nbr;
469
470     free( *pi_pce_lst );
471     *pi_pce_lst = calloc( i_pieces_nbr, sizeof(**pi_pce_lst) );
472     if( !*pi_pce_lst )
473         return VLC_ENOMEM;
474
475     for( int32_t i = 0; i < i_pieces_nbr; i++ )
476         (*pi_pce_lst)[i] = NO_PCE;
477
478     for( int32_t c = 0; c < i_pieces_nbr; ) {
479         int32_t i = ((unsigned)vlc_mrand48()) % i_pieces_nbr;
480         if( (*pi_pce_lst)[i] == NO_PCE )
481             (*pi_pce_lst)[i] = c++;
482     }
483     return VLC_SUCCESS;
484 }
485
486 bool puzzle_is_finished( filter_sys_t *p_sys, int32_t *pi_pce_lst )
487 {
488     for( uint32_t i = 0; i < p_sys->s_allocated.i_pieces_nbr; i++ )
489         if( (int32_t)i != pi_pce_lst[i] )
490             return false;
491
492     return true;
493 }
494
495 int puzzle_piece_foreground( filter_t *p_filter, int32_t i_piece) {
496     filter_sys_t *p_sys = p_filter->p_sys;
497     piece_t *ps_pieces_tmp;        /* list [piece] of pieces data. Sort as per layers */
498     uint32_t i_group_ID = p_sys->ps_pieces[i_piece].i_group_ID;
499
500     ps_pieces_tmp = malloc( sizeof( piece_t) * p_sys->s_allocated.i_pieces_nbr );
501     if (!ps_pieces_tmp)
502         return VLC_ENOMEM;
503
504     int32_t j=0;
505
506     memcpy( &ps_pieces_tmp[j], &p_sys->ps_pieces[i_piece], sizeof(piece_t) );
507     j++;
508
509     for (uint32_t i = 0; i < p_sys->s_allocated.i_pieces_nbr; i++) {
510         if ( ( p_sys->ps_pieces[i].i_group_ID == i_group_ID ) && ( (int32_t)i != i_piece ) ) {
511             memcpy( &ps_pieces_tmp[j], &p_sys->ps_pieces[i], sizeof(piece_t));
512             j++;
513         }
514     }
515     for (uint32_t i = 0; i < p_sys->s_allocated.i_pieces_nbr; i++) {
516         if ( p_sys->ps_pieces[i].i_group_ID != i_group_ID ) {
517             memcpy( &ps_pieces_tmp[j], &p_sys->ps_pieces[i], sizeof(piece_t));
518             j++;
519         }
520     }
521
522     free( p_filter->p_sys->ps_pieces );
523     p_filter->p_sys->ps_pieces = ps_pieces_tmp;
524
525     return VLC_SUCCESS;
526 }
527
528 void puzzle_count_pce_group( filter_t *p_filter) { /* count pce in each group */
529     filter_sys_t *p_sys = p_filter->p_sys;
530
531     memset ( p_sys->pi_group_qty, 0, sizeof( int32_t ) * (p_sys->s_allocated.i_pieces_nbr) );
532     for (uint32_t i_pce = 0; i_pce < p_sys->s_allocated.i_pieces_nbr; i_pce++)
533         p_sys->pi_group_qty[p_sys->ps_pieces[i_pce].i_group_ID]++;
534 }
535
536 /*****************************************************************************
537  * puzzle_solve_pces_group: check if pieces are close together
538  *                          and then combine in a group
539  *****************************************************************************/
540 void puzzle_solve_pces_group( filter_t *p_filter) {
541     filter_sys_t *p_sys = p_filter->p_sys;
542     int32_t i_dx, i_dy;
543
544     p_sys->i_solve_grp_loop++;
545     p_sys->i_solve_grp_loop %= p_sys->s_allocated.i_pieces_nbr;
546
547     int32_t i_piece_A = p_sys->i_solve_grp_loop;
548     piece_t *ps_piece_A = &p_sys->ps_pieces[i_piece_A];
549
550     for (uint32_t i_piece_B = 0; i_piece_B < p_sys->s_allocated.i_pieces_nbr; i_piece_B++)
551     {
552         piece_t *ps_piece_B = &p_sys->ps_pieces[i_piece_B];
553
554         if ( ps_piece_A->i_actual_angle != ps_piece_B->i_actual_angle || ps_piece_A->i_actual_mirror != ps_piece_B->i_actual_mirror )
555             continue;
556
557         if ( (ps_piece_B->i_group_ID != p_sys->ps_pieces[i_piece_A].i_group_ID ) )
558         {
559             if ( abs(ps_piece_A->i_OTy - ps_piece_B->i_OTy )<3)
560             {
561                 if (    abs( ps_piece_A->i_ORx - ps_piece_B->i_OLx + 1 ) < 3
562                      && abs( ps_piece_A->i_TRx - ps_piece_B->i_TLx + 1 ) < p_sys->i_magnet_accuracy
563                      && abs( ps_piece_A->i_TRy - ps_piece_B->i_TLy ) < p_sys->i_magnet_accuracy
564                      && abs( ps_piece_A->i_BRx - ps_piece_B->i_BLx + 1 ) < p_sys->i_magnet_accuracy
565                      && abs( ps_piece_A->i_BRy - ps_piece_B->i_BLy ) < p_sys->i_magnet_accuracy )
566                 {
567                     i_dx = ps_piece_A->i_TRx - ps_piece_B->i_TLx + ps_piece_A->i_step_x_x;
568                     i_dy = ps_piece_A->i_TRy - ps_piece_B->i_TLy;
569
570                     if (!ps_piece_B->b_finished)
571                         puzzle_move_group( p_filter, i_piece_B, i_dx, i_dy);
572                     else
573                         puzzle_move_group( p_filter, i_piece_A, -i_dx, -i_dy);
574
575                     uint32_t i_group_ID = ps_piece_B->i_group_ID;
576                     for (uint32_t i_for = 0; i_for < p_sys->s_allocated.i_pieces_nbr; i_for++)
577                         if ( p_sys->ps_pieces[i_for].i_group_ID == i_group_ID)
578                             p_sys->ps_pieces[i_for].i_group_ID = p_sys->ps_pieces[i_piece_A].i_group_ID;
579                 }
580             }
581             else if ( abs(ps_piece_A->i_OLx - ps_piece_B->i_OLx ) < 3 )
582             {
583                 if (    abs(ps_piece_A->i_OBy - ps_piece_B->i_OTy + 1 ) < 3
584                      && abs( ps_piece_B->i_TLx - ps_piece_A->i_BLx ) < p_sys->i_magnet_accuracy
585                      && abs( ps_piece_B->i_TLy - 1 - ps_piece_A->i_BLy ) < p_sys->i_magnet_accuracy
586                      && abs( ps_piece_B->i_TRx - ps_piece_A->i_BRx ) < p_sys->i_magnet_accuracy
587                      && abs( ps_piece_B->i_TRy - 1 - ps_piece_A->i_BRy ) < p_sys->i_magnet_accuracy )
588                 {
589
590                     i_dx = ps_piece_A->i_BLx - ps_piece_B->i_TLx;
591                     i_dy = ps_piece_A->i_BLy - ps_piece_B->i_TLy + ps_piece_A->i_step_y_y;
592
593                     if (!ps_piece_B->b_finished)
594                         puzzle_move_group( p_filter, i_piece_B, i_dx, i_dy);
595                     else
596                         puzzle_move_group( p_filter, i_piece_A, -i_dx, -i_dy);
597
598                     uint32_t i_group_ID = ps_piece_B->i_group_ID;
599                     for (uint32_t i_for = 0; i_for < p_sys->s_allocated.i_pieces_nbr; i_for++)
600                         if ( p_sys->ps_pieces[i_for].i_group_ID == i_group_ID)
601                             p_sys->ps_pieces[i_for].i_group_ID = p_sys->ps_pieces[i_piece_A].i_group_ID;
602                 }
603             }
604         }
605
606         if ( abs( ps_piece_A->i_OTy - ps_piece_B->i_OTy ) < 3 )
607         {
608             if (    abs( ps_piece_A->i_ORx - ps_piece_B->i_OLx + 1 ) < 3
609                  && abs( ps_piece_A->i_TRx - ps_piece_B->i_TLx + 1 ) < p_sys->i_magnet_accuracy
610                  && abs( ps_piece_A->i_TRy - ps_piece_B->i_TLy ) < p_sys->i_magnet_accuracy
611                  && abs( ps_piece_A->i_BRx - ps_piece_B->i_BLx + 1 ) < p_sys->i_magnet_accuracy
612                  && abs( ps_piece_A->i_BRy - ps_piece_B->i_BLy ) < p_sys->i_magnet_accuracy )
613             {
614                 ps_piece_B->i_left_shape = 0;
615                 ps_piece_A->i_right_shape = 6;
616             }
617         }
618         else if ( abs( ps_piece_A->i_OLx - ps_piece_B->i_OLx )<3 )
619         {
620             if (    abs( ps_piece_A->i_OBy - ps_piece_B->i_OTy + 1 )<3
621                  && abs( ps_piece_B->i_TLx - ps_piece_A->i_BLx ) < p_sys->i_magnet_accuracy
622                  && abs( ps_piece_B->i_TLy - 1 - ps_piece_A->i_BLy ) < p_sys->i_magnet_accuracy
623                  && abs( ps_piece_B->i_TRx - ps_piece_A->i_BRx ) < p_sys->i_magnet_accuracy
624                  && abs( ps_piece_B->i_TRy - 1 - ps_piece_A->i_BRy ) < p_sys->i_magnet_accuracy )
625             {
626                 ps_piece_B->i_top_shape = 2;
627                 ps_piece_A->i_btm_shape = 4;
628             }
629         }
630    }
631 }
632
633 /*****************************************************************************
634  * puzzle_solve_pces_accuracy: check if pieces are close to their final location
635  *                             and then adjust position accordingly
636  *****************************************************************************/
637 void puzzle_solve_pces_accuracy( filter_t *p_filter) {
638     filter_sys_t *p_sys = p_filter->p_sys;
639
640     p_sys->i_solve_acc_loop++;
641     if (p_sys->i_solve_acc_loop >= p_sys->s_allocated.i_pieces_nbr) {
642         p_sys->i_done_count = p_sys->i_tmp_done_count;
643         p_sys->i_tmp_done_count = 0;
644         p_sys->i_solve_acc_loop = 0;
645         p_sys->b_finished = (p_sys->i_done_count == p_sys->s_allocated.i_pieces_nbr);
646     }
647
648     piece_t *ps_piece = &p_sys->ps_pieces[p_sys->i_solve_acc_loop];
649
650     ps_piece->b_finished = false;
651     if (    ps_piece->i_actual_mirror == 1
652          && abs( ps_piece->i_TRx - ps_piece->i_ORx )  < p_sys->i_magnet_accuracy
653          && abs( ps_piece->i_TRy - ps_piece->i_OTy )  < p_sys->i_magnet_accuracy
654          && abs( ps_piece->i_TLx - ps_piece->i_OLx )  < p_sys->i_magnet_accuracy
655          && abs( ps_piece->i_TLy - ps_piece->i_OTy )  < p_sys->i_magnet_accuracy )
656     {
657         uint32_t i_group_ID = ps_piece->i_group_ID;
658         p_sys->i_tmp_done_count++;
659
660         for ( uint32_t i = 0; i < p_sys->s_allocated.i_pieces_nbr; i++) {
661             ps_piece = &p_sys->ps_pieces[i];
662             if ( ( ps_piece->i_group_ID == i_group_ID ) && ( !ps_piece->b_finished ) ) {
663                 ps_piece->ps_piece_in_plane[0].i_actual_x = ps_piece->i_OLx;
664                 ps_piece->ps_piece_in_plane[0].i_actual_y = ps_piece->i_OTy;
665                 ps_piece->i_actual_mirror = +1;
666                 puzzle_calculate_corners( p_filter, i );
667                 ps_piece->b_finished = true;
668             }
669         }
670     }
671 }
672
673 /*****************************************************************************
674  * puzzle_sort_layers: sort pieces in order to see in foreground the
675  *                     standalone ones and those are at the wrong place
676  *****************************************************************************/
677 int puzzle_sort_layers( filter_t *p_filter)
678 {
679     filter_sys_t *p_sys = p_filter->p_sys;
680
681     int32_t i_idx = 0;
682     for (uint32_t i_qty = 1; i_qty <= p_sys->s_current_param.i_pieces_nbr; i_qty++) {
683         /* pieces at the wrong place are in foreground */
684         for (uint32_t i_pce_loop = 0; i_pce_loop < p_sys->s_current_param.i_pieces_nbr; i_pce_loop++) {
685             uint32_t i_grp = p_sys->ps_pieces[i_pce_loop].i_group_ID;
686             if ( p_sys->pi_group_qty[i_grp] == (int32_t)i_qty ) {
687                 bool b_check_ok = true;
688                 for (int32_t i_pce_check = 0; i_pce_check < i_idx; i_pce_check++)
689                     if ( p_sys->ps_pieces_tmp[i_pce_check].i_group_ID == i_grp )
690                         b_check_ok = false;
691                 if ( b_check_ok )
692                     for (uint32_t i_pce = i_pce_loop; i_pce < p_sys->s_current_param.i_pieces_nbr; i_pce++)
693                         if (( p_sys->ps_pieces[i_pce].i_group_ID == i_grp ) && !p_sys->ps_pieces[i_pce].b_finished ) {
694                             memcpy( &p_sys->ps_pieces_tmp[i_idx], &p_sys->ps_pieces[i_pce], sizeof(piece_t));
695                             i_idx++;
696                         }
697             }
698         }
699         /* pieces at the final location are in background */
700         for (uint32_t i_pce_loop = 0; i_pce_loop < p_sys->s_current_param.i_pieces_nbr; i_pce_loop++) {
701             uint32_t i_grp = p_sys->ps_pieces[i_pce_loop].i_group_ID;
702             if ( p_sys->pi_group_qty[i_grp] == (int32_t)i_qty ) {
703                 bool b_check_ok = true;
704                 for (int32_t i_pce_check = 0; i_pce_check < i_idx; i_pce_check++)
705                     if ( p_sys->ps_pieces_tmp[i_pce_check].i_group_ID == i_grp && p_sys->ps_pieces_tmp[i_pce_check].b_finished )
706                         b_check_ok = false;
707                 if ( b_check_ok )
708                     for (uint32_t i_pce = i_pce_loop; i_pce < p_sys->s_current_param.i_pieces_nbr; i_pce++)
709                         if (( p_sys->ps_pieces[i_pce].i_group_ID == i_grp ) && p_sys->ps_pieces[i_pce].b_finished ) {
710                             memcpy( &p_sys->ps_pieces_tmp[i_idx], &p_sys->ps_pieces[i_pce], sizeof(piece_t));
711                             i_idx++;
712                         }
713             }
714         }
715     }
716
717     free( p_filter->p_sys->ps_pieces );
718     p_filter->p_sys->ps_pieces = p_sys->ps_pieces_tmp;
719     p_sys->ps_pieces_tmp = malloc( sizeof( piece_t) * p_sys->s_allocated.i_pieces_nbr );
720     if (!p_sys->ps_pieces_tmp)
721         return VLC_ENOMEM;
722
723     return VLC_SUCCESS;
724 }
725
726 /*****************************************************************************
727  * puzzle_auto_solve: solve the puzzle depending on auto_solve_speed parameter
728  *                    = move one piece at the final location each time
729  *                      auto_solve_countdown is < 0
730  *****************************************************************************/
731 void puzzle_auto_solve( filter_t *p_filter)
732 {
733     filter_sys_t *p_sys = p_filter->p_sys;
734
735     if ( p_sys->s_current_param.i_auto_solve_speed < 500 )
736         return;
737
738     if ( --p_sys->i_auto_solve_countdown_val > 0 )
739         return;
740
741     /* delay reached, preset next delay and proceed with puzzle_auto_solve */
742     p_sys->i_auto_solve_countdown_val = init_countdown(p_sys->s_current_param.i_auto_solve_speed);
743
744     /* random piece to be moved */
745     int32_t i_start = ((unsigned)vlc_mrand48()) % p_sys->s_allocated.i_pieces_nbr;
746
747     /* here the computer will help player by placing the piece at the final location */
748     for (uint32_t i_l = 0; i_l < p_sys->s_allocated.i_pieces_nbr; i_l++) {
749         int32_t i = ( i_l + i_start ) % p_sys->s_allocated.i_pieces_nbr;
750         if ( !p_sys->ps_pieces[i].b_finished ) {
751             for (uint32_t j = 0; j < p_sys->s_allocated.i_pieces_nbr; j++) {
752                 if ( p_sys->ps_pieces[j].i_group_ID == p_sys->ps_pieces[i].i_group_ID ) {
753                     p_sys->ps_pieces[j].i_actual_angle   = 0;
754                     p_sys->ps_pieces[j].i_actual_mirror  = +1;
755                     p_sys->ps_pieces[j].ps_piece_in_plane[0].i_actual_x = p_sys->ps_pieces[j].ps_piece_in_plane[0].i_original_x;
756                     p_sys->ps_pieces[j].ps_piece_in_plane[0].i_actual_y = p_sys->ps_pieces[j].ps_piece_in_plane[0].i_original_y;
757                     puzzle_calculate_corners( p_filter, j );
758                 }
759             }
760             break;
761         }
762     }
763 }
764
765 /*****************************************************************************
766  * puzzle_auto_shuffle: shuffle the pieces on the desk depending on
767  *                      auto_shuffle_speed parameter
768  *                    = random move of one piece each time
769  *                      auto_shuffle_countdown is < 0
770  *****************************************************************************/
771 void puzzle_auto_shuffle( filter_t *p_filter)
772 {
773     filter_sys_t *p_sys = p_filter->p_sys;
774
775     if ( p_sys->s_current_param.i_auto_shuffle_speed < 500 )
776         return;
777
778     if ( --p_sys->i_auto_shuffle_countdown_val > 0 )
779         return;
780
781     /* delay reached, preset next delay and proceed with puzzle_auto_shuffle */
782     p_sys->i_auto_shuffle_countdown_val = init_countdown(p_sys->s_current_param.i_auto_shuffle_speed);
783
784     /* random piece to be moved */
785     int32_t i_start = ((unsigned)vlc_mrand48()) % p_sys->s_allocated.i_pieces_nbr;
786
787     for (uint32_t i_l = 0; i_l < p_sys->s_allocated.i_pieces_nbr; i_l++){
788         int32_t i = ( i_l + i_start ) % p_sys->s_allocated.i_pieces_nbr;
789
790         /* find one piece which is part of one group */
791         if ( p_sys->pi_group_qty[p_sys->ps_pieces[i].i_group_ID] > 1 ) {
792             /* find an empty group to be used by this dismantled piece */
793             uint32_t i_new_group;
794             for ( i_new_group = 0 ; i_new_group < p_sys->s_allocated.i_pieces_nbr ; i_new_group ++ )
795                 if ( p_sys->pi_group_qty[i_new_group] == 0 )
796                     break;
797             p_sys->ps_pieces[i].i_group_ID = i_new_group;
798             p_sys->ps_pieces[i].b_finished = false;
799
800             /* random rotate & mirror */
801             switch ( p_sys->s_current_param.i_rotate )
802             {
803               case 1:
804                     puzzle_rotate_pce( p_filter, i, (( (unsigned) vlc_mrand48()) % ( 2 ) ) * 2, p_sys->ps_pieces[i].i_center_x, p_sys->ps_pieces[i].i_center_y, false );
805                 break;
806               case 2:
807                     puzzle_rotate_pce( p_filter, i, (( (unsigned) vlc_mrand48()) % ( 4 ) ), p_sys->ps_pieces[i].i_center_x, p_sys->ps_pieces[i].i_center_y, false );
808                 break;
809               case 3:
810                     puzzle_rotate_pce( p_filter, i, (( (unsigned) vlc_mrand48()) % ( 8 ) ), p_sys->ps_pieces[i].i_center_x, p_sys->ps_pieces[i].i_center_y, false );
811                 break;
812             }
813
814             /* random mvt */
815             p_sys->ps_pieces[i].ps_piece_in_plane[0].i_actual_x =
816                     p_sys->ps_desk_planes[0].i_border_width
817                     + ( (unsigned) vlc_mrand48()) % ( p_sys->ps_desk_planes[0].i_width - 2*p_sys->ps_desk_planes[0].i_border_width - p_sys->ps_pieces[i].ps_piece_in_plane[0].i_width)
818                     + p_sys->ps_pieces[i].ps_piece_in_plane[0].i_width / 2 * ( 1 - p_sys->ps_pieces[i].i_step_x_x )
819                     - (p_sys->ps_pieces[i].ps_piece_in_plane[0].i_lines / 2) * p_sys->ps_pieces[i].i_step_y_x;
820             p_sys->ps_pieces[i].ps_piece_in_plane[0].i_actual_y =
821                     p_sys->ps_desk_planes[0].i_border_lines
822                     + ( (unsigned) vlc_mrand48()) % ( p_sys->ps_desk_planes[0].i_lines - 2*p_sys->ps_desk_planes[0].i_border_lines - p_sys->ps_pieces[i].ps_piece_in_plane[0].i_lines)
823                     + p_sys->ps_pieces[i].ps_piece_in_plane[0].i_lines / 2 * ( 1 - p_sys->ps_pieces[i].i_step_y_y )
824                     - (p_sys->ps_pieces[i].ps_piece_in_plane[0].i_width / 2) * p_sys->ps_pieces[i].i_step_x_y;
825
826             /* redefine shapes */
827             uint32_t i_left_pce  = 0;
828             uint32_t i_right_pce = 6;
829             uint32_t i_top_pce   = 2;
830             uint32_t i_btm_pce   = 4;
831
832             uint32_t i_pce = 0;
833             for (int32_t i_row = 0; i_row < p_sys->s_allocated.i_rows; i_row++)
834                 for (int32_t i_col = 0; i_col < p_sys->s_allocated.i_cols; i_col++) {
835                     if (p_sys->ps_pieces[i].i_original_row == p_sys->ps_pieces[i_pce].i_original_row) {
836                         if (p_sys->ps_pieces[i].i_original_col == p_sys->ps_pieces[i_pce].i_original_col - 1)
837                             i_right_pce = i_pce;
838                         else if (p_sys->ps_pieces[i].i_original_col == p_sys->ps_pieces[i_pce].i_original_col + 1)
839                             i_left_pce = i_pce;
840                     }
841                     else if (p_sys->ps_pieces[i].i_original_col == p_sys->ps_pieces[i_pce].i_original_col) {
842                         if (p_sys->ps_pieces[i].i_original_row == p_sys->ps_pieces[i_pce].i_original_row - 1)
843                             i_btm_pce = i_pce;
844                         else if (p_sys->ps_pieces[i].i_original_row == p_sys->ps_pieces[i_pce].i_original_row + 1)
845                             i_top_pce = i_pce;
846                     }
847                     i_pce++;
848                 }
849
850             if ((p_sys->ps_pieces[i].i_left_shape == 0) && (p_sys->ps_pieces[i].i_original_col != 0)) {
851                 p_sys->ps_pieces[i_left_pce].i_right_shape = 6 + 8 + 8*(( (unsigned) vlc_mrand48()) % ( SHAPES_QTY ) ) + (vlc_mrand48() & 0x01);
852                 p_sys->ps_pieces[i].i_left_shape = (p_sys->ps_pieces[i_left_pce].i_right_shape - 6 ) ^ 0x01;
853             }
854
855             if ((p_sys->ps_pieces[i].i_right_shape == 6) && (p_sys->ps_pieces[i].i_original_col != p_sys->s_allocated.i_cols-1)) {
856                 p_sys->ps_pieces[i].i_right_shape = 6 + 8 + 8*(( (unsigned) vlc_mrand48()) % ( SHAPES_QTY ) ) + (vlc_mrand48() & 0x01);
857                 p_sys->ps_pieces[i_right_pce].i_left_shape = (p_sys->ps_pieces[i].i_right_shape - 6 ) ^ 0x01;
858             }
859
860             if ((p_sys->ps_pieces[i].i_top_shape == 2) && (p_sys->ps_pieces[i].i_original_row != 0)) {
861                 p_sys->ps_pieces[i_top_pce].i_btm_shape = 4 + 8 + 8*(( (unsigned) vlc_mrand48()) % ( SHAPES_QTY ) ) + (vlc_mrand48() & 0x01);
862                 p_sys->ps_pieces[i].i_top_shape = (p_sys->ps_pieces[i_top_pce].i_btm_shape - 2 ) ^ 0x01;
863             }
864
865             if ((p_sys->ps_pieces[i].i_btm_shape == 4) && (p_sys->ps_pieces[i].i_original_row != p_sys->s_allocated.i_rows-1)) {
866                 p_sys->ps_pieces[i].i_btm_shape = 4 + 8 + 8*(( (unsigned) vlc_mrand48()) % ( SHAPES_QTY ) ) + (vlc_mrand48() & 0x01);
867                 p_sys->ps_pieces[i_btm_pce].i_top_shape = (p_sys->ps_pieces[i].i_btm_shape - 2 ) ^ 0x01;
868             }
869
870             puzzle_calculate_corners( p_filter, i );
871             break;
872         }
873     }
874 }
875
876 /*****************************************************************************
877  * puzzle_save: save pieces location in memory
878  *****************************************************************************/
879 save_game_t* puzzle_save(filter_t *p_filter)
880 {
881     filter_sys_t *p_sys = p_filter->p_sys;
882
883     save_game_t *ps_save_game = calloc(1, sizeof(*ps_save_game));
884     if (!ps_save_game)
885         return NULL;
886
887     ps_save_game->i_cols   = p_sys->s_allocated.i_cols;
888     ps_save_game->i_rows   = p_sys->s_allocated.i_rows;
889     ps_save_game->i_rotate = p_sys->s_allocated.i_rotate;
890
891     ps_save_game->ps_pieces = calloc( ps_save_game->i_cols * ps_save_game->i_rows , sizeof(*ps_save_game->ps_pieces));
892     if (!ps_save_game->ps_pieces) {
893         free(ps_save_game);
894         return NULL;
895     }
896
897     int32_t i_border_width = p_sys->ps_desk_planes[0].i_border_width;
898     int32_t i_border_lines = p_sys->ps_desk_planes[0].i_border_lines;
899
900     for (int32_t i_pce = 0; i_pce < ps_save_game->i_cols * ps_save_game->i_rows; i_pce++) {
901         ps_save_game->ps_pieces[i_pce].i_original_row  = p_sys->ps_pieces[i_pce].i_original_row;
902         ps_save_game->ps_pieces[i_pce].i_original_col  = p_sys->ps_pieces[i_pce].i_original_col;
903         ps_save_game->ps_pieces[i_pce].i_top_shape     = p_sys->ps_pieces[i_pce].i_top_shape;
904         ps_save_game->ps_pieces[i_pce].i_btm_shape     = p_sys->ps_pieces[i_pce].i_btm_shape;
905         ps_save_game->ps_pieces[i_pce].i_right_shape   = p_sys->ps_pieces[i_pce].i_right_shape;
906         ps_save_game->ps_pieces[i_pce].i_left_shape    = p_sys->ps_pieces[i_pce].i_left_shape;
907         ps_save_game->ps_pieces[i_pce].f_pos_x         =(p_sys->ps_pieces[i_pce].ps_piece_in_plane[0].i_actual_x - i_border_width ) / ((float)p_sys->ps_desk_planes[0].i_width - 2*i_border_width);
908         ps_save_game->ps_pieces[i_pce].f_pos_y         =(p_sys->ps_pieces[i_pce].ps_piece_in_plane[0].i_actual_y - i_border_lines ) / ((float)p_sys->ps_desk_planes[0].i_lines - 2*i_border_lines);
909         ps_save_game->ps_pieces[i_pce].i_actual_angle  = p_sys->ps_pieces[i_pce].i_actual_angle;
910         ps_save_game->ps_pieces[i_pce].i_actual_mirror = p_sys->ps_pieces[i_pce].i_actual_mirror;
911     }
912
913     return ps_save_game;
914 }
915
916 void puzzle_load( filter_t *p_filter, save_game_t *ps_save_game)
917 {
918     filter_sys_t *p_sys = p_filter->p_sys;
919
920     if (p_sys->s_current_param.i_cols  != ps_save_game->i_cols
921         || p_sys->s_allocated.i_rows   != ps_save_game->i_rows
922         || p_sys->s_allocated.i_rotate != ps_save_game->i_rotate)
923         return;
924
925     int32_t i_border_width = p_sys->ps_desk_planes[0].i_border_width;
926     int32_t i_border_lines = p_sys->ps_desk_planes[0].i_border_lines;
927
928     for (uint32_t i_pce=0; i_pce < p_sys->s_allocated.i_pieces_nbr; i_pce++) {
929        for (uint32_t i=0; i < p_sys->s_allocated.i_pieces_nbr; i++)
930             if   ( p_sys->ps_pieces[i].i_original_row == ps_save_game->ps_pieces[i_pce].i_original_row
931                 && p_sys->ps_pieces[i].i_original_col == ps_save_game->ps_pieces[i_pce].i_original_col  )
932             {
933                 p_sys->ps_pieces[i].ps_piece_in_plane[0].i_actual_x = i_border_width
934                         + ((float)p_sys->ps_desk_planes[0].i_width - 2 * i_border_width)
935                         * ps_save_game->ps_pieces[i_pce].f_pos_x;
936                 p_sys->ps_pieces[i].ps_piece_in_plane[0].i_actual_y = i_border_lines
937                         + ((float)p_sys->ps_desk_planes[0].i_lines - 2 * i_border_lines)
938                         * ps_save_game->ps_pieces[i_pce].f_pos_y;
939
940                 p_sys->ps_pieces[i].i_top_shape     =  ps_save_game->ps_pieces[i_pce].i_top_shape;
941                 p_sys->ps_pieces[i].i_btm_shape     =  ps_save_game->ps_pieces[i_pce].i_btm_shape;
942                 p_sys->ps_pieces[i].i_right_shape   =  ps_save_game->ps_pieces[i_pce].i_right_shape;
943                 p_sys->ps_pieces[i].i_left_shape    =  ps_save_game->ps_pieces[i_pce].i_left_shape;
944                 p_sys->ps_pieces[i].i_actual_angle  =  ps_save_game->ps_pieces[i_pce].i_actual_angle;
945                 p_sys->ps_pieces[i].i_actual_mirror =  ps_save_game->ps_pieces[i_pce].i_actual_mirror;
946                 p_sys->ps_pieces[i].i_group_ID     = i_pce;
947                 p_sys->ps_pieces[i].b_finished     = false;
948
949                 p_sys->ps_pieces[i].ps_piece_in_plane[0].i_actual_x = i_border_width + ((float)p_sys->ps_desk_planes[0].i_width
950                                                                         - 2*i_border_width) * ps_save_game->ps_pieces[i_pce].f_pos_x;
951                 p_sys->ps_pieces[i].ps_piece_in_plane[0].i_actual_y = i_border_lines + ((float)p_sys->ps_desk_planes[0].i_lines
952                                                                         - 2*i_border_lines) * ps_save_game->ps_pieces[i_pce].f_pos_y;
953                 puzzle_calculate_corners( p_filter, i );
954
955                 break;
956             }
957     }
958
959     for (uint32_t i_pce=0; i_pce < p_sys->s_allocated.i_pieces_nbr; i_pce++) {
960         /* redefine shapes */
961         uint32_t i_left_pce  = 0;
962         uint32_t i_right_pce = 6;
963         uint32_t i_top_pce   = 2;
964         uint32_t i_btm_pce   = 4;
965
966         uint32_t i_pce_pair = 0;
967         for (int32_t i_row = 0; i_row < p_sys->s_allocated.i_rows; i_row++)
968             for (int32_t i_col = 0; i_col < p_sys->s_allocated.i_cols; i_col++) {
969                 if (p_sys->ps_pieces[i_pce].i_original_row == p_sys->ps_pieces[i_pce_pair].i_original_row) {
970                     if (p_sys->ps_pieces[i_pce].i_original_col == p_sys->ps_pieces[i_pce_pair].i_original_col - 1)
971                         i_right_pce = i_pce_pair;
972                     else if (p_sys->ps_pieces[i_pce].i_original_col == p_sys->ps_pieces[i_pce_pair].i_original_col + 1)
973                         i_left_pce = i_pce_pair;
974                 }
975                 else if (p_sys->ps_pieces[i_pce].i_original_col == p_sys->ps_pieces[i_pce_pair].i_original_col) {
976                     if (p_sys->ps_pieces[i_pce].i_original_row == p_sys->ps_pieces[i_pce_pair].i_original_row - 1)
977                         i_btm_pce = i_pce_pair;
978                     else if (p_sys->ps_pieces[i_pce].i_original_row == p_sys->ps_pieces[i_pce_pair].i_original_row + 1)
979                         i_top_pce = i_pce_pair;
980                 }
981                 i_pce_pair++;
982             }
983
984         if ((p_sys->ps_pieces[i_pce].i_left_shape == 0) && (p_sys->ps_pieces[i_pce].i_original_col != 0)) {
985             p_sys->ps_pieces[i_left_pce].i_right_shape = 6 + 8 + 8*(( (unsigned) vlc_mrand48()) % ( SHAPES_QTY ) ) + (vlc_mrand48() & 0x01);
986             p_sys->ps_pieces[i_pce].i_left_shape = (p_sys->ps_pieces[i_left_pce].i_right_shape - 6 ) ^ 0x01;
987         }
988
989         if ((p_sys->ps_pieces[i_pce].i_right_shape == 6) && (p_sys->ps_pieces[i_pce].i_original_col != p_sys->s_allocated.i_cols-1)) {
990             p_sys->ps_pieces[i_pce].i_right_shape = 6 + 8 + 8*(( (unsigned) vlc_mrand48()) % ( SHAPES_QTY ) ) + (vlc_mrand48() & 0x01);
991             p_sys->ps_pieces[i_right_pce].i_left_shape = (p_sys->ps_pieces[i_pce].i_right_shape - 6 ) ^ 0x01;
992         }
993
994         if ((p_sys->ps_pieces[i_pce].i_top_shape == 2) && (p_sys->ps_pieces[i_pce].i_original_row != 0)) {
995             p_sys->ps_pieces[i_top_pce].i_btm_shape = 4 + 8 + 8*(( (unsigned) vlc_mrand48()) % ( SHAPES_QTY ) ) + (vlc_mrand48() & 0x01);
996             p_sys->ps_pieces[i_pce].i_top_shape = (p_sys->ps_pieces[i_top_pce].i_btm_shape - 2 ) ^ 0x01;
997         }
998
999         if ((p_sys->ps_pieces[i_pce].i_btm_shape == 4) && (p_sys->ps_pieces[i_pce].i_original_row != p_sys->s_allocated.i_rows-1)) {
1000             p_sys->ps_pieces[i_pce].i_btm_shape = 4 + 8 + 8*(( (unsigned) vlc_mrand48()) % ( SHAPES_QTY ) ) + (vlc_mrand48() & 0x01);
1001             p_sys->ps_pieces[i_btm_pce].i_top_shape = (p_sys->ps_pieces[i_pce].i_btm_shape - 2 ) ^ 0x01;
1002         }
1003
1004     }
1005 }