Initial import of the motion estimation filter.
[melted] / src / modules / motion_est / filter_vismv.c
1 /*
2 * /brief Draw motion vectors
3 * /author Zachary Drew, Copyright 2004
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 #include "filter_motion_est.h"
21 #include "arrow_code.h"
22
23 #include <framework/mlt_frame.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <math.h>
28 #include <string.h>
29
30 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
31
32 static void paint_arrows( uint8_t *image, struct motion_vector_s *vectors, int w, int h, int mb_w, int mb_h )
33 {
34 int i, j, x, y;
35 struct motion_vector_s *p;
36 for( i = 0; i < w/mb_w; i++ ){
37 for( j = 0; j < h/mb_h; j++ ){
38 x = i*mb_w;
39 y = j*mb_h;
40 p = vectors + (w/mb_w)*j + i;
41 #if 0
42 if( p->color == 0 )
43 continue;
44 if( p->quality > 10 ){
45 draw_line(image, x, y, x + mb_w, y, 100);
46 draw_line(image, x, y, x, y + mb_h, 100);
47 draw_line(image, x + mb_w, y, x + mb_w, y + mb_h, 100);
48 draw_line(image, x + mb_w, y + mb_h, x, y + mb_w, 100);
49 }
50 else if ( p->color == 18 ) {
51 draw_line(image, x, y, x + mb_w, y + mb_h, 100);
52 draw_line(image, x, y + mb_h, x + mb_w, y, 100);
53 continue;
54 }
55 else if( p->vert_dev < 150 ){
56 x += mb_w/2;
57 draw_line(image, x, y, x, y + mb_h, 100);
58 continue;
59 }
60 else if( p->horiz_dev < 150 ){
61 y += mb_w/2;
62 draw_line(image, x, y, x+mb_w, y, 100);
63 continue;
64 }
65 else
66 #endif
67 /*if ( p->valid == 1 ){
68 x += mb_w/2;
69 y += mb_h/2;
70 draw_arrow(image, x + p->dx, y + p->dy, x, y, 100);
71 } else */
72 if ( p->valid == 3 ) {
73 draw_rectangle_fill(image, x, y, mb_w, mb_h,0);
74 }
75 if ( p->valid == 1 ) {
76 //draw_rectangle_outline(image, x, y, mb_w, mb_h,100);
77 //x += mb_w/4;
78 //y += mb_h/4;
79 //draw_rectangle_outline(image, x + p->dx, y + p->dy, mb_w, mb_h,100);
80 x += mb_w/2;
81 y += mb_h/2;
82 draw_arrow(image, x, y, x + p->dx, y + p->dy, 100);
83 //draw_rectangle_fill(image, x + p->dx, y + p->dy, mb_w, mb_h, 100);
84 }
85 }
86 }
87 //if (count > 300)
88 // fprintf(stderr, "%d mbs above %d\n", count, mb_w * mb_h * 12);
89 }
90
91 #if 0
92 static void paint_mbs( uint8_t *image, struct motion_vector_s *vectors, int w, int h, int mb_w, int mb_h, int xstep, int ystep )
93 {
94 int i, j, x, y;
95 struct motion_vector_s *p;
96 for( i = 0; i < w/mb_w; i++ ){
97 for( j = 0; j < h/mb_h; j++ ){
98 x = i * mb_w;
99 y = j * mb_h;
100 p = vectors + (w/mb_w)*j + i;
101 if( p->color == 0 )
102 continue;
103 draw_line(image, x, y, x + mb_w, y, 100);
104 draw_line(image, x, y, x, y + mb_h, 100);
105 draw_line(image, x + mb_w, y, x + mb_w, y + mb_h, 100);
106 draw_line(image, x + mb_w, y + mb_h, x, y + mb_w, 100);
107 }
108 }
109 }
110 #endif
111
112 // Image stack(able) method
113 static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
114 {
115 // Get the frame properties
116 mlt_properties properties = MLT_FRAME_PROPERTIES(frame);
117
118 // Get the new image
119 int error = mlt_frame_get_image( frame, image, format, width, height, 1 );
120
121 if( error != 0 )
122 mlt_properties_debug( MLT_FRAME_PROPERTIES(frame), "error after mlt_frame_get_image()", stderr );
123
124
125 // Get the size of macroblocks in pixel units
126 int macroblock_height = mlt_properties_get_int( properties, "motion_est.macroblock_height" );
127 int macroblock_width = mlt_properties_get_int( properties, "motion_est.macroblock_width" );
128
129 // Get the motion vectors
130 struct motion_vectors_s *current_vectors = mlt_properties_get_data( properties, "motion_est.vectors", NULL );
131
132 init_arrows( format, *width, *height );
133
134 if ( mlt_properties_get_int( properties, "shot_change" ) == 1 )
135 {
136 draw_line(*image, 0, 0, *width, *height, 100);
137 draw_line(*image, 0, *height, *width, 0, 100);
138 }
139 if( current_vectors != NULL ) {
140 paint_arrows( *image, current_vectors, *width, *height, macroblock_width, macroblock_height);
141 //paint_mbs( *image, current_vectors, *width, *height, macroblock_width, macroblock_height, xstep, ystep);
142 }
143
144 return error;
145 }
146
147
148
149 /** Filter processing.
150 */
151
152 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
153 {
154 // Push the frame filter
155 mlt_frame_push_get_image( frame, filter_get_image );
156
157
158 return frame;
159 }
160
161 /** Constructor for the filter.
162 */
163
164
165 mlt_filter filter_vismv_init( char *arg )
166 {
167 mlt_filter this = mlt_filter_new( );
168 if ( this != NULL )
169 {
170 this->process = filter_process;
171
172 }
173
174 return this;
175 }
176
177 /** This source code will self destruct in 5...4...3...
178 */
179
180
181
182
183
184
185
186