]> git.sesse.net Git - mlt/blobdiff - src/modules/qimage/transition_vqm.cpp
Add support for Qt 5, drop support for Qt 3 and KDE 3.
[mlt] / src / modules / qimage / transition_vqm.cpp
index a75a60abe9c4fdc491218da3638b606974ae1dda..c926b165803c98b3862d300c212883bb9450a50e 100644 (file)
 #include <string.h>
 #include <math.h>
 #include <stdio.h>
+#include <QApplication>
+#include <QImage>
+#include <QColor>
+#include <QLocale>
+#include <QPainter>
+#include <QPalette>
+#include <QFont>
+#include <QString>
+
+static QApplication *app = 0;
 
 static double calc_psnr( const uint8_t *a, const uint8_t *b, int size, int bpp )
 {
@@ -98,6 +108,7 @@ static double calc_ssim( const uint8_t *a, const uint8_t *b, int width, int heig
 static int get_image( mlt_frame a_frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
 {
        mlt_frame b_frame = mlt_frame_pop_frame( a_frame );
+       mlt_properties properties = MLT_FRAME_PROPERTIES( a_frame );
        mlt_transition transition = MLT_TRANSITION( mlt_frame_pop_service( a_frame ) );
        uint8_t *b_image;
        int window_size = mlt_properties_get_int( MLT_TRANSITION_PROPERTIES( transition ), "window_size" );
@@ -113,13 +124,108 @@ static int get_image( mlt_frame a_frame, uint8_t **image, mlt_image_format *form
        ssim[0] = calc_ssim( *image, b_image, *width, *height, window_size, 2 );
        ssim[1] = calc_ssim( *image + 1, b_image + 1, *width / 2, *height, window_size, 4 );
        ssim[2] = calc_ssim( *image + 3, b_image + 3, *width / 2, *height, window_size, 4 );
+       mlt_properties_set_double( properties, "meta.vqm.psnr.y", psnr[0] );
+       mlt_properties_set_double( properties, "meta.vqm.psnr.cb", psnr[1] );
+       mlt_properties_set_double( properties, "meta.vqm.psnr.cr", psnr[2] );
+       mlt_properties_set_double( properties, "meta.vqm.ssim.y", ssim[0] );
+       mlt_properties_set_double( properties, "meta.vqm.ssim.cb", ssim[1] );
+       mlt_properties_set_double( properties, "meta.vqm.ssim.cr", ssim[2] );
        printf( "%05d %05.2f %05.2f %05.2f %5.3f %5.3f %5.3f\n",
                        mlt_frame_get_position( a_frame ), psnr[0], psnr[1], psnr[2],
                        ssim[0], ssim[1], ssim[2] );
 
+       // copy the B frame to the bottom of the A frame for comparison
        window_size = mlt_image_format_size( *format, *width, *height, NULL ) / 2;
        memcpy( *image + window_size, b_image + window_size, window_size );
 
+       if ( !mlt_properties_get_int( MLT_TRANSITION_PROPERTIES( transition ), "render" ) )
+               return 0;
+
+       // get RGBA image for Qt drawing
+       *format = mlt_image_rgb24a;
+       mlt_frame_get_image( a_frame, image, format, width, height, 1 );
+
+       // convert mlt image to qimage
+       QImage img( *width, *height, QImage::Format_ARGB32 );
+       int y = *height + 1;
+       uint8_t *src = *image;
+       while ( --y )
+       {
+               QRgb *dst = (QRgb*) img.scanLine( *height - y );
+               int x = *width + 1;
+               while ( --x )
+               {
+                       *dst++ = qRgba( src[0], src[1], src[2], 255 );
+                       src += 4;
+               }
+       }
+
+       // create QApplication, if needed
+       if ( !app )
+       {
+               if ( qApp )
+               {
+                       app = qApp;
+               }
+               else
+               {
+                       int argc = 1;
+                       char* argv[] = { strdup( "unknown" ) };
+
+                       app = new QApplication( argc, argv );
+                       const char *localename = mlt_properties_get_lcnumeric( MLT_TRANSITION_PROPERTIES(transition) );
+                       QLocale::setDefault( QLocale( localename ) );
+                       free( argv[0] );
+               }
+       }
+
+       // setup Qt drawing
+       QPainter painter;
+       painter.begin( &img );
+       painter.setRenderHints( QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing );
+
+       // draw some stuff with Qt
+       QPalette palette;
+       QFont font;
+       QString s;
+       font.setBold( true );
+       font.setPointSize( 30 * *height / 1080 );
+       painter.setPen( QColor("black") );
+       painter.drawLine( 0, *height/2 + 1, *width, *height/2 );
+       painter.setPen( QColor("white") );
+       painter.drawLine( 0, *height/2 - 1, *width, *height/2 );
+       painter.setFont( font );
+       s.sprintf( "Frame: %05d\nPSNR:   %05.2f (Y) %05.2f (Cb) %05.2f (Cr)\nSSIM:    %5.3f (Y) %5.3f (Cb) %5.3f (Cr)",
+                         mlt_frame_get_position( a_frame ), psnr[0], psnr[1], psnr[2],
+                         ssim[0], ssim[1], ssim[2] );
+       painter.setPen( QColor("black") );
+       painter.drawText( 52, *height * 8 / 10 + 2, *width, *height, 0, s );
+       painter.setPen( QColor("white") );
+       painter.drawText( 50, *height * 8 / 10, *width, *height, 0, s );
+
+       // finish Qt drawing
+       painter.end();
+       window_size = mlt_image_format_size( *format, *width, *height, NULL );
+       uint8_t *dst = (uint8_t *) mlt_pool_alloc( window_size );
+       mlt_properties_set_data( MLT_FRAME_PROPERTIES(a_frame), "image", dst, window_size, mlt_pool_release, NULL );
+       *image = dst;
+
+       // convert qimage to mlt
+       y = *height + 1;
+       while ( --y )
+       {
+               QRgb *src = (QRgb*) img.scanLine( *height - y );
+               int x = *width + 1;
+               while ( --x )
+               {
+                       *dst++ = qRed( *src );
+                       *dst++ = qGreen( *src );
+                       *dst++ = qBlue( *src );
+                       *dst++ = qAlpha( *src );
+                       src++;
+               }
+       }
+
        return 0;
 }