]> git.sesse.net Git - mlt/blob - src/modules/vid.stab/common.c
Updates to vid.stab module.
[mlt] / src / modules / vid.stab / common.c
1 /*
2  * common.c
3  * Copyright (C) 2014 Brian Matherly <pez4brian@yahoo.com>
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 #include <framework/mlt.h>
20 #include <vid.stab/libvidstab.h>
21 #include "common.h"
22
23 mlt_image_format validate_format( mlt_image_format format )
24 {
25         switch( format )
26         {
27         case mlt_image_rgb24a:
28         case mlt_image_rgb24:
29                 return mlt_image_rgb24;
30         case mlt_image_yuv420p:
31                 return mlt_image_yuv420p;
32         default:
33         case mlt_image_none:
34         case mlt_image_yuv422:
35         case mlt_image_opengl:
36         case mlt_image_glsl:
37         case mlt_image_glsl_texture:
38                 return mlt_image_yuv422;
39         }
40 }
41
42 /** Convert an MLT image to one that can be used by VS.
43  * Use free_vsimage() when done with the resulting image.
44  */
45
46 VSPixelFormat mltimage_to_vsimage( mlt_image_format mlt_format, int width, int height, uint8_t* mlt_img, uint8_t** vs_img )
47 {
48         switch( mlt_format )
49         {
50         case mlt_image_rgb24:
51                 // Convert RGB24 to YUV444 because it is the only planar
52                 // format with comparable bit depth.
53                 {
54                         *vs_img = mlt_pool_alloc( width * height * 3 );
55                         int y, u, v, r, g, b;
56                         int total = width * height + 1;
57                         uint8_t* yp = *vs_img;
58                         uint8_t* up = yp + ( width * height );
59                         uint8_t* vp = up + ( width * height );
60
61                         while( --total )
62                         {
63                                 r = *mlt_img++;
64                                 g = *mlt_img++;
65                                 b = *mlt_img++;
66                                 RGB2YUV_601_SCALED(r, g, b, y, u, v);
67                                 *yp++ = y;
68                                 *up++ = u;
69                                 *vp++ = v;
70                         }
71
72                         return PF_YUV444P;
73                 }
74         case mlt_image_yuv420p:
75                 // This format maps with no conversion
76                 {
77                         *vs_img = mlt_img;
78                         return PF_YUV420P;
79                 }
80         case mlt_image_yuv422:
81                 // Convert packed to planar
82                 {
83                         *vs_img = mlt_pool_alloc( width * height * 2 );
84                         uint8_t* yp = *vs_img;
85                         uint8_t* up = yp + ( width * height );
86                         uint8_t* vp = up + ( width * height / 2 );
87                         int total = ( width * height / 2 ) + 1;
88
89                         while( --total )
90                         {
91                                 *yp++ = *mlt_img++;
92                                 *up++ = *mlt_img++;
93                                 *yp++ = *mlt_img++;
94                                 *vp++ = *mlt_img++;
95                         }
96
97                         return PF_YUV422P;
98                 }
99         default:
100                 return PF_NONE;
101         }
102 }
103
104 /** Convert a VS image back to the MLT image it originally came from in mltimage_to_vsimage().
105  */
106
107 void vsimage_to_mltimage( uint8_t* vs_img, uint8_t* mlt_img, mlt_image_format mlt_format, int width, int height )
108 {
109         switch( mlt_format )
110         {
111         case mlt_image_rgb24:
112                 // Convert YUV444 to RGB24.
113                 {
114                         int y, u, v, r, g, b;
115                         int total = width * height + 1;
116                         uint8_t* yp = vs_img;
117                         uint8_t* up = yp + ( width * height );
118                         uint8_t* vp = up + ( width * height );
119
120                         while( --total )
121                         {
122                                 y = *yp++;
123                                 u = *up++;
124                                 v = *vp++;
125                                 YUV2RGB_601_SCALED( y, u, v, r, g, b );
126                                 *mlt_img++ = r;
127                                 *mlt_img++ = g;
128                                 *mlt_img++ = b;
129                         }
130                 }
131                 break;
132         case mlt_image_yuv420p:
133                 // This format was never converted
134                 break;
135         case mlt_image_yuv422:
136                 // Convert planar to packed
137                 {
138                         uint8_t* yp = vs_img;
139                         uint8_t* up = yp + ( width * height );
140                         uint8_t* vp = up + ( width * height / 2 );
141                         int total = ( width * height / 2 ) + 1;
142
143                         while( --total )
144                         {
145                                 *mlt_img++ = *yp++;
146                                 *mlt_img++ = *up++;
147                                 *mlt_img++ = *yp++;
148                                 *mlt_img++ = *vp++;
149                         }
150                 }
151                 break;
152         default:
153                 break;
154         }
155 }
156
157 /** Free an image allocated by mltimage_to_vsimage().
158  */
159
160 void free_vsimage( uint8_t* vs_img, VSPixelFormat format )
161 {
162         if( format != PF_YUV420P )
163         {
164                 mlt_pool_release( vs_img );
165         }
166 }
167
168 /** Compare two VSMotionDetectConfig structures.
169  * Return 1 if they are different. 0 if they are the same.
170  */
171
172 int compare_motion_config( VSMotionDetectConfig* a, VSMotionDetectConfig* b )
173 {
174         if( a->shakiness != b->shakiness ||
175                 a->accuracy != b->accuracy ||
176                 a->stepSize != b->stepSize ||
177                 // Skip: Deprecated
178                 // a->algo != b->algo ||
179                 a->virtualTripod != b->virtualTripod ||
180                 a->show != b->show ||
181                 // Skip: inconsequential?
182                 // a->modName != b->modName ||
183                 a->contrastThreshold != b->contrastThreshold )
184         {
185                 return 1;
186         }
187         return 0;
188 }
189
190 /** Compare two VSTransformConfig structures.
191  * Return 1 if they are different. 0 if they are the same.
192  */
193
194 int compare_transform_config( VSTransformConfig* a, VSTransformConfig* b )
195 {
196         if( a->relative != b->relative ||
197                 a->smoothing != b->smoothing ||
198                 a->crop != b->crop ||
199                 a->invert != b->invert ||
200                 a->zoom != b->zoom ||
201                 a->optZoom != b->optZoom ||
202                 a->zoomSpeed != b->zoomSpeed ||
203                 a->interpolType != b->interpolType ||
204                 a->maxShift != b->maxShift ||
205                 a->maxAngle != b->maxAngle ||
206                 // Skip: inconsequential?
207                 // a->modName != b->modName ||
208                 // Skip: unused?
209                 // a->verbose != b->verbose ||
210                 a->simpleMotionCalculation != b->simpleMotionCalculation ||
211                 // Skip: unused?
212                 // a->storeTransforms != b->storeTransforms ||
213                 a->smoothZoom != b->smoothZoom ||
214                 a->camPathAlgo != b->camPathAlgo )
215         {
216                 return 1;
217         }
218         return 0;
219 }