]> git.sesse.net Git - mlt/blob - src/modules/vid.stab/common.c
Add support for more image formats to vid.stab
[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 VSPixelFormat mltimage_to_vsimage( mlt_image_format mlt_format, int width, int height, uint8_t* mlt_img, uint8_t** vs_img )
43 {
44         switch( mlt_format )
45         {
46         case mlt_image_rgb24:
47                 // Convert RGB24 to YUV444 because it is the only planar
48                 // format with comparable bit depth.
49                 {
50                         *vs_img = mlt_pool_alloc( width * height * 3 );
51                         int y, u, v, r, g, b;
52                         int total = width * height + 1;
53                         uint8_t* yp = *vs_img;
54                         uint8_t* up = yp + ( width * height );
55                         uint8_t* vp = up + ( width * height );
56
57                         while( --total )
58                         {
59                                 r = *mlt_img++;
60                                 g = *mlt_img++;
61                                 b = *mlt_img++;
62                                 RGB2YUV_601_SCALED(r, g, b, y, u, v);
63                                 *yp++ = y;
64                                 *up++ = u;
65                                 *vp++ = v;
66                         }
67
68                         return PF_YUV444P;
69                 }
70         case mlt_image_yuv420p:
71                 // This format maps with no conversion
72                 {
73                         *vs_img = mlt_img;
74                         return PF_YUV420P;
75                 }
76         case mlt_image_yuv422:
77                 // Convert packed to planar
78                 {
79                         *vs_img = mlt_pool_alloc( width * height * 2 );
80                         uint8_t* yp = *vs_img;
81                         uint8_t* up = yp + ( width * height );
82                         uint8_t* vp = up + ( width * height / 2 );
83                         int total = ( width * height / 2 ) + 1;
84
85                         while( --total )
86                         {
87                                 *yp++ = *mlt_img++;
88                                 *up++ = *mlt_img++;
89                                 *yp++ = *mlt_img++;
90                                 *vp++ = *mlt_img++;
91                         }
92
93                         return PF_YUV422P;
94                 }
95         default:
96                 return PF_NONE;
97         }
98 }
99
100 void vsimage_to_mltimage( uint8_t* vs_img, uint8_t* mlt_img, mlt_image_format mlt_format, int width, int height )
101 {
102         switch( mlt_format )
103         {
104         case mlt_image_rgb24:
105                 // Convert YUV444 to RGB24.
106                 {
107                         int y, u, v, r, g, b;
108                         int total = width * height + 1;
109                         uint8_t* yp = vs_img;
110                         uint8_t* up = yp + ( width * height );
111                         uint8_t* vp = up + ( width * height );
112
113                         while( --total )
114                         {
115                                 y = *yp++;
116                                 u = *up++;
117                                 v = *vp++;
118                                 YUV2RGB_601_SCALED( y, u, v, r, g, b );
119                                 *mlt_img++ = r;
120                                 *mlt_img++ = g;
121                                 *mlt_img++ = b;
122                         }
123                 }
124                 break;
125         case mlt_image_yuv420p:
126                 // This format was never converted
127                 break;
128         case mlt_image_yuv422:
129                 // Convert planar to packed
130                 {
131                         uint8_t* yp = vs_img;
132                         uint8_t* up = yp + ( width * height );
133                         uint8_t* vp = up + ( width * height / 2 );
134                         int total = ( width * height / 2 ) + 1;
135
136                         while( --total )
137                         {
138                                 *mlt_img++ = *yp++;
139                                 *mlt_img++ = *up++;
140                                 *mlt_img++ = *yp++;
141                                 *mlt_img++ = *vp++;
142                         }
143                 }
144                 break;
145         default:
146                 return PF_NONE;
147         }
148 }
149
150 void free_vsimage( uint8_t* vs_img, VSPixelFormat format )
151 {
152         if( format != PF_YUV420P )
153         {
154                 mlt_pool_release( vs_img );
155         }
156 }