]> git.sesse.net Git - mlt/blob - src/modules/qt/transition_vqm.cpp
A little debugging.
[mlt] / src / modules / qt / transition_vqm.cpp
1 /*
2  * transition_vqm.c -- video quality measurement
3  * Copyright (c) 2012 Dan Dennedy <dan@dennedy.org>
4  * Core psnr and ssim routines based on code from
5  *   qsnr (C) 2010 E. Oriani, ema <AT> fastwebnet <DOT> it
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>
19  */
20
21 #include "common.h"
22 #include <framework/mlt.h>
23 #include <string.h>
24 #include <math.h>
25 #include <stdio.h>
26 #include <QImage>
27 #include <QColor>
28 #include <QPainter>
29 #include <QPalette>
30 #include <QFont>
31 #include <QString>
32
33 static double calc_psnr( const uint8_t *a, const uint8_t *b, int size, int bpp )
34 {
35         double mse = 0.0;
36         int n = size + 1;
37
38         while ( --n )
39         {
40                 int diff = *a - *b;
41                 mse += diff * diff;
42                 a += bpp;
43                 b += bpp;
44         }
45
46         return 10.0 * log10( 255.0 * 255.0 / ( mse == 0 ? 1e-10 : mse/size ) );
47 }
48
49 static double calc_ssim( const uint8_t *a, const uint8_t *b, int width, int height, int window_size, int bpp )
50 {
51         int     windows_x = width / window_size;
52         int windows_y = height / window_size;
53         double  avg = 0.0;
54
55         if ( !windows_x || !windows_y )
56                 return 0.0;
57
58         // for each window
59         for ( int y = 0; y < windows_y; ++y )
60                 for ( int x = 0; x < windows_x; ++x )
61                 {
62                         int     base_offset = x * window_size + y * window_size * width;
63                         double  ref_acc = 0.0,
64                                         ref_acc_2 = 0.0,
65                                         cmp_acc = 0.0,
66                                         cmp_acc_2 = 0.0,
67                                         ref_cmp_acc = 0.0;
68
69                         // accumulate the pixel values for this window
70                         for ( int j = 0; j < window_size; ++j )
71                                 for ( int i = 0; i < window_size; ++i )
72                                 {
73                                         uint8_t c_a = a[bpp * (base_offset + j * width + i)];
74                                         uint8_t c_b = b[bpp * (base_offset + j * width + i)];
75                                         ref_acc += c_a;
76                                         ref_acc_2 += c_a * c_a;
77                                         cmp_acc += c_b;
78                                         cmp_acc_2 += c_b * c_b;
79                                         ref_cmp_acc += c_a * c_b;
80                                 }
81
82                         // compute the SSIM for this window
83                         // http://en.wikipedia.org/wiki/SSIM
84                         // http://en.wikipedia.org/wiki/Variance
85                         // http://en.wikipedia.org/wiki/Covariance
86                         double n_samples = window_size * window_size,
87                                         ref_avg = ref_acc / n_samples,
88                                         ref_var = ref_acc_2 / n_samples - ref_avg * ref_avg,
89                                         cmp_avg = cmp_acc / n_samples,
90                                         cmp_var = cmp_acc_2 / n_samples - cmp_avg * cmp_avg,
91                                         ref_cmp_cov = ref_cmp_acc / n_samples - ref_avg * cmp_avg,
92                                         c1 = 6.5025, // (0.01*255.0)^2
93                                         c2 = 58.5225, // (0.03*255)^2
94                                         ssim_num = (2.0 * ref_avg * cmp_avg + c1) * (2.0 * ref_cmp_cov + c2),
95                                         ssim_den = (ref_avg * ref_avg + cmp_avg * cmp_avg + c1) * (ref_var + cmp_var + c2);
96
97                         // accumulate the SSIM
98                         avg += ssim_num / ssim_den;
99                 }
100
101         // return the average SSIM
102         return avg / windows_x / windows_y;
103 }
104
105 static int get_image( mlt_frame a_frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
106 {
107         mlt_frame b_frame = mlt_frame_pop_frame( a_frame );
108         mlt_properties properties = MLT_FRAME_PROPERTIES( a_frame );
109         mlt_transition transition = MLT_TRANSITION( mlt_frame_pop_service( a_frame ) );
110         uint8_t *b_image;
111         int window_size = mlt_properties_get_int( MLT_TRANSITION_PROPERTIES( transition ), "window_size" );
112         double psnr[3], ssim[3];
113
114         *format = mlt_image_yuv422;
115         mlt_frame_get_image( b_frame, &b_image, format, width, height, writable );
116         mlt_frame_get_image( a_frame, image, format, width, height, writable );
117
118         psnr[0] = calc_psnr( *image, b_image, *width * *height, 2 );
119         psnr[1] = calc_psnr( *image + 1, b_image + 1, *width * *height / 2, 4 );
120         psnr[2] = calc_psnr( *image + 3, b_image + 3, *width * *height / 2, 4 );
121         ssim[0] = calc_ssim( *image, b_image, *width, *height, window_size, 2 );
122         ssim[1] = calc_ssim( *image + 1, b_image + 1, *width / 2, *height, window_size, 4 );
123         ssim[2] = calc_ssim( *image + 3, b_image + 3, *width / 2, *height, window_size, 4 );
124         mlt_properties_set_double( properties, "meta.vqm.psnr.y", psnr[0] );
125         mlt_properties_set_double( properties, "meta.vqm.psnr.cb", psnr[1] );
126         mlt_properties_set_double( properties, "meta.vqm.psnr.cr", psnr[2] );
127         mlt_properties_set_double( properties, "meta.vqm.ssim.y", ssim[0] );
128         mlt_properties_set_double( properties, "meta.vqm.ssim.cb", ssim[1] );
129         mlt_properties_set_double( properties, "meta.vqm.ssim.cr", ssim[2] );
130         printf( "%05d %05.2f %05.2f %05.2f %5.3f %5.3f %5.3f\n",
131                         mlt_frame_get_position( a_frame ), psnr[0], psnr[1], psnr[2],
132                         ssim[0], ssim[1], ssim[2] );
133
134         // copy the B frame to the bottom of the A frame for comparison
135         window_size = mlt_image_format_size( *format, *width, *height, NULL ) / 2;
136         memcpy( *image + window_size, b_image + window_size, window_size );
137
138         if ( !mlt_properties_get_int( MLT_TRANSITION_PROPERTIES( transition ), "render" ) )
139                 return 0;
140
141         // get RGBA image for Qt drawing
142         *format = mlt_image_rgb24a;
143         mlt_frame_get_image( a_frame, image, format, width, height, 1 );
144
145         // convert mlt image to qimage
146         QImage img( *width, *height, QImage::Format_ARGB32 );
147         int y = *height + 1;
148         uint8_t *src = *image;
149         while ( --y )
150         {
151                 QRgb *dst = (QRgb*) img.scanLine( *height - y );
152                 int x = *width + 1;
153                 while ( --x )
154                 {
155                         *dst++ = qRgba( src[0], src[1], src[2], 255 );
156                         src += 4;
157                 }
158         }
159
160         // setup Qt drawing
161         QPainter painter;
162         painter.begin( &img );
163         painter.setRenderHints( QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing );
164
165         // draw some stuff with Qt
166         QPalette palette;
167         QFont font;
168         QString s;
169         font.setBold( true );
170         font.setPointSize( 30 * *height / 1080 );
171         painter.setPen( QColor("black") );
172         painter.drawLine( 0, *height/2 + 1, *width, *height/2 );
173         painter.setPen( QColor("white") );
174         painter.drawLine( 0, *height/2 - 1, *width, *height/2 );
175         painter.setFont( font );
176         s.sprintf( "Frame: %05d\nPSNR:   %05.2f (Y) %05.2f (Cb) %05.2f (Cr)\nSSIM:    %5.3f (Y) %5.3f (Cb) %5.3f (Cr)",
177                           mlt_frame_get_position( a_frame ), psnr[0], psnr[1], psnr[2],
178                           ssim[0], ssim[1], ssim[2] );
179         painter.setPen( QColor("black") );
180         painter.drawText( 52, *height * 8 / 10 + 2, *width, *height, 0, s );
181         painter.setPen( QColor("white") );
182         painter.drawText( 50, *height * 8 / 10, *width, *height, 0, s );
183
184         // finish Qt drawing
185         painter.end();
186         window_size = mlt_image_format_size( *format, *width, *height, NULL );
187         uint8_t *dst = (uint8_t *) mlt_pool_alloc( window_size );
188         mlt_properties_set_data( MLT_FRAME_PROPERTIES(a_frame), "image", dst, window_size, mlt_pool_release, NULL );
189         *image = dst;
190
191         // convert qimage to mlt
192         y = *height + 1;
193         while ( --y )
194         {
195                 QRgb *src = (QRgb*) img.scanLine( *height - y );
196                 int x = *width + 1;
197                 while ( --x )
198                 {
199                         *dst++ = qRed( *src );
200                         *dst++ = qGreen( *src );
201                         *dst++ = qBlue( *src );
202                         *dst++ = qAlpha( *src );
203                         src++;
204                 }
205         }
206
207         return 0;
208 }
209
210 static mlt_frame process( mlt_transition transition, mlt_frame a_frame, mlt_frame b_frame )
211 {
212         mlt_frame_push_service( a_frame, transition );
213         mlt_frame_push_frame( a_frame, b_frame );
214         mlt_frame_push_get_image( a_frame, get_image );
215
216         return a_frame;
217 }
218
219 extern "C" {
220
221 mlt_transition transition_vqm_init( mlt_profile profile, mlt_service_type type, const char *id, void *arg )
222 {
223         mlt_transition transition = mlt_transition_new();
224
225         if ( transition )
226         {
227                 mlt_properties properties = MLT_TRANSITION_PROPERTIES( transition );
228
229                 if ( !createQApplicationIfNeeded( MLT_TRANSITION_SERVICE(transition) ) )
230                 {
231                         mlt_transition_close( transition );
232                         return NULL;
233                 }
234                 transition->process = process;
235                 mlt_properties_set_int( properties, "_transition_type", 1 ); // video only
236                 mlt_properties_set_int( properties, "window_size", 8 );
237                 printf( "frame psnr[Y] psnr[Cb] psnr[Cr] ssim[Y] ssim[Cb] ssim[Cr]\n" );
238         }
239
240         return transition;
241 }
242
243 } // extern "C"