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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  */
19 
20 #include "filter_motion_est.h"
21 #include "arrow_code.h"
22 
23 #include <framework/mlt_frame.h>
24 #include <framework/mlt_profile.h>
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <math.h>
29 #include <string.h>
30 
31 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
32 
paint_arrows(uint8_t * image,struct motion_vector_s * vectors,int w,int h,int mb_w,int mb_h)33 static void paint_arrows( uint8_t *image, struct motion_vector_s *vectors, int w, int h, int mb_w, int mb_h )
34 {
35 	int i, j, x, y;
36 	struct motion_vector_s *p;
37 	for( i = 0; i < w/mb_w; i++ ){
38 		for( j = 0; j < h/mb_h; j++ ){
39 			x = i*mb_w;
40 			y = j*mb_h;
41 			p = vectors + (w/mb_w)*j + i;
42 
43 			if ( p->valid == 1 ) {
44 				//draw_rectangle_outline(image, x-1, y-1, mb_w+1, mb_h+1,100);
45 				//x += mb_w/4;
46 				//y += mb_h/4;
47 				//draw_rectangle_outline(image, x + p->dx, y + p->dy, mb_w, mb_h,100);
48 				x += mb_w/2;
49 				y += mb_h/2;
50 				draw_arrow(image, x, y, x + p->dx, y + p->dy, 100);
51 				//draw_rectangle_fill(image, x + p->dx, y + p->dy, mb_w, mb_h, 100);
52 			}
53 			else if ( p->valid == 2 ) {
54 				draw_rectangle_outline(image, x+1, y+1, mb_w-2, mb_h-2,100);
55 			}
56 			else if ( p->valid == 3 ) {
57 				draw_rectangle_fill(image, x-p->dx, y-p->dy, mb_w, mb_h,0);
58 			}
59 			else if ( p->valid == 4 ) {
60 				draw_line(image, x, y, x + 4, y, 100);
61 				draw_line(image, x, y, x, y + 4, 100);
62 				draw_line(image, x + 4, y, x, y + 4, 100);
63 
64 				draw_line(image, x+mb_w-1, y+mb_h-1, x+mb_w-5, y+mb_h-1, 100);
65 				draw_line(image, x+mb_w-1, y+mb_h-1, x+mb_w-1, y+mb_h-5, 100);
66 				draw_line(image, x+mb_w-5, y+mb_h-1, x+mb_w-1, y+mb_h-5, 100);
67 			}
68 		}
69 	}
70 }
71 
72 // Image stack(able) method
filter_get_image(mlt_frame frame,uint8_t ** image,mlt_image_format * format,int * width,int * height,int writable)73 static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
74 {
75 	// Get the frame properties
76 	mlt_properties properties = MLT_FRAME_PROPERTIES(frame);
77 	mlt_filter filter = mlt_frame_pop_service( frame );
78 	mlt_profile profile = mlt_service_profile(MLT_FILTER_SERVICE(filter));
79 
80 	// Disable consumer scaling
81 	if (profile && profile->width && profile->height) {
82 		*width = profile->width;
83 		*height = profile->height;
84 	}
85 
86 	// Get the new image
87 	*format = mlt_image_yuv422;
88 	int error = mlt_frame_get_image( frame, image, format, width, height, 1 );
89 
90 	if( error != 0 )
91 		mlt_properties_debug( MLT_FRAME_PROPERTIES(frame), "error after mlt_frame_get_image()", stderr );
92 
93 
94 	// Get the size of macroblocks in pixel units
95 	int macroblock_height = mlt_properties_get_int( properties, "motion_est.macroblock_height" );
96 	int macroblock_width = mlt_properties_get_int( properties, "motion_est.macroblock_width" );
97 
98 	// Get the motion vectors
99 	struct motion_vector_s *current_vectors = mlt_properties_get_data( properties, "motion_est.vectors", NULL );
100 
101 	init_arrows( format, *width, *height );
102 
103 	if ( mlt_properties_get_int( properties, "shot_change" ) == 1 )
104 	{
105 		draw_line(*image, 0, 0, *width, *height, 100);
106 		draw_line(*image, 0, *height, *width, 0, 100);
107 	}
108 	if( current_vectors != NULL ) {
109 		paint_arrows( *image, current_vectors, *width, *height, macroblock_width, macroblock_height);
110 	}
111 
112 	return error;
113 }
114 
115 
116 
117 /** Filter processing.
118 */
119 
filter_process(mlt_filter this,mlt_frame frame)120 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
121 {
122 	// Push the frame filter
123 	mlt_frame_push_service( frame, this);
124 	mlt_frame_push_get_image( frame, filter_get_image );
125 
126 
127 	return frame;
128 }
129 
130 /** Constructor for the filter.
131 */
132 
133 
filter_vismv_init(mlt_profile profile,mlt_service_type type,const char * id,char * arg)134 mlt_filter filter_vismv_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
135 {
136 	mlt_filter this = mlt_filter_new( );
137 	if ( this != NULL )
138 	{
139 		this->process = filter_process;
140 
141 	}
142 
143 	return this;
144 }
145 
146 /** This source code will self destruct in 5...4...3...
147 */
148 
149 
150 
151 
152 
153 
154 
155 
156