]> git.sesse.net Git - mlt/commitdiff
Cleanup + fix crashes when used in Kdenlive
authorj-b-m <jb@kdenlive.org>
Sun, 26 Jul 2009 22:56:51 +0000 (00:56 +0200)
committerj-b-m <jb@kdenlive.org>
Sun, 26 Jul 2009 22:56:51 +0000 (00:56 +0200)
modified:   kdenlivetitle_wrapper.cpp
modified:   kdenlivetitle_wrapper.h
modified:   producer_kdenlivetitle.c

src/modules/qimage/kdenlivetitle_wrapper.cpp
src/modules/qimage/kdenlivetitle_wrapper.h
src/modules/qimage/producer_kdenlivetitle.c

index 679d8de3f9558b9f8b32fc608ffe70c93cba67b3..77373e12adf15c09dfd9eacfa10780413366c63e 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
+#include "kdenlivetitle_wrapper.h"
+#include <framework/mlt_producer.h>
+
 #include <QtGui/QImage>
 #include <QtGui/QPainter>
-#include <QtCore/QCoreApplication>
+#include <QtCore/QDebug>
 #include <QtGui/QApplication>
 #include <QtCore/QFile>
 #include <QtGui/QGraphicsScene>
 #include <QtGui/QGraphicsTextItem>
-#include <QtGui/QGraphicsPolygonItem>
 #include <QtGui/QTextCursor>
-#include "kdenlivetitle_wrapper.h"
-#include <framework/mlt_producer.h>
 
-static Title* titleclass;
-static QApplication *app;
+static Title* titleclass = NULL;
+static QApplication *app = NULL;
 
 extern "C"
 {
        void init_qt( const char* c )
        {
-               titleclass=new Title( QString( c ) );
+               titleclass=new Title( c );
        }
 
        void close_qt()
@@ -46,60 +46,60 @@ extern "C"
        
        void refresh_kdenlivetitle( uint8_t* buffer, int width, int height , double position, char *templatexml, char *templatetext, int force_refresh )
        {
-               if (force_refresh) titleclass->reloadXml(templatexml, templatetext);
+               if (force_refresh) titleclass->loadFromXml( templatexml, QString( templatetext ) );
                titleclass->drawKdenliveTitle( buffer, width, height, position, templatexml, templatetext );
        }
 }
 
-Title::Title( const QString& filename ):m_filename( filename ), m_scene( NULL )
-{
-}
 
-Title::~Title()
+Title::Title( const char *filename ):m_filename( filename ), m_scene( NULL )
 {
-       if (m_scene) delete m_scene;
-       if (app) delete app;
 }
 
-void Title::reloadXml(char *templatexml, char *templatetext)
+Title::~Title()
 {
-       if (m_scene == NULL) return;
-       loadDocument( m_filename, QString( templatexml ), QString( templatetext ) );
+       if (m_scene) {
+           m_scene->clear();
+           delete m_scene;
+       }
 }
 
-void Title::drawKdenliveTitle( uint8_t * buffer, int width, int height, double position, char *templatexml, char *templatetext )
+void Title::drawKdenliveTitle( uint8_t * buffer, int width, int height, double position, const char *templatexml, const char *templatetext )
 {
        if ( m_scene == NULL )
        {
-               int argc=0;
+               int argc = 1;
                char* argv[1];
-               argv[0]="xxx";
-               if ( ! QApplication::activeWindow() )
-                       //if (!app)
-                       app=new QApplication( argc,argv );
-               m_scene = new QGraphicsScene(app);
-               loadDocument( m_filename, QString( templatexml ), QString( templatetext ) );
+               argv[0] = "xxx";
+               if (qApp) {
+                   app = qApp;
+               }
+               else {
+                   app=new QApplication( argc,argv );
+               }
+               m_scene = new QGraphicsScene(0, 0, (qreal) width, (qreal) height, app);
+               loadFromXml( templatexml, QString( templatetext ) );
        }
        //must be extracted from kdenlive title
-
-       QImage *img=new QImage( width,height,QImage::Format_ARGB32 );
+       QImage *img = new QImage( width,height,QImage::Format_ARGB32 );
        img->fill( 0 );
        QPainter p1;
        p1.begin( img );
        p1.setRenderHints( QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing );//|QPainter::SmoothPixmapTransform );
-       
        if (m_start.isNull() && m_end.isNull()) {
-           m_scene->render( &p1,QRect( 0, 0, width, height ) );
+           m_scene->render( &p1 );
        }
        else {
            QPointF topleft=m_start.topLeft()+( m_end.topLeft()-m_start.topLeft() )*position;
            QPointF bottomRight=m_start.bottomRight()+( m_end.bottomRight()-m_start.bottomRight() )*position;
-           m_scene->render( &p1,QRect( 0,0,width,height ),QRectF( topleft,bottomRight ) );
+           const QRectF r1( 0, 0, width, height );
+           const QRectF r2( topleft, bottomRight );
+           m_scene->render( &p1, r1, r2 );
        }
        p1.end();
        uint8_t *pointer=img->bits();
        QRgb* src = ( QRgb* ) pointer;
-       for ( int i=0;i<width*height*4;i+=4 )
+       for ( int i = 0; i < width * height * 4; i += 4 )
        {
                *buffer++=qRed( *src );
                *buffer++=qGreen( *src );
@@ -110,29 +110,13 @@ void Title::drawKdenliveTitle( uint8_t * buffer, int width, int height, double p
        delete img;
 }
 
-int Title::loadDocument( const QString& url, const QString templateXml, const QString templateText )
-{
-       if (m_scene == NULL) return 0;
-       QDomDocument doc;
-       if ( !templateXml.isEmpty() )
-       {
-               doc.setContent( templateXml );
-       }
-       else
-       {
-               QFile file( url );
-               if ( file.open( QIODevice::ReadOnly ) )
-               {
-                       doc.setContent( &file, false );
-                       file.close();
-               }
-       }
-       return loadFromXml( doc, templateText );
-}
-
-int Title::loadFromXml( QDomDocument doc, const QString templateText )
+void Title::loadFromXml( const char *templateXml, const QString templateText )
 {
+       if (m_scene == NULL) return;
        m_scene->clear();
+       QDomDocument doc;
+       QString data(templateXml);
+       doc.setContent(data);
        QDomNodeList titles = doc.elementsByTagName( "kdenlivetitle" );
        int maxZValue = 0;
        if ( titles.size() )
@@ -260,7 +244,7 @@ int Title::loadFromXml( QDomDocument doc, const QString templateText )
                        }
                }
        }
-       return maxZValue;
+       return;
 }
 
 QString Title::colorToString( const QColor& c )
index 2453526c706376bf073e4e2a3956b56ccb8ac2cb..d446cb72a09ac91e91d9c12a6b7ed007eba0f31d 100644 (file)
 #include <QtCore/QString>
 #include <framework/mlt_frame.h>
 #include <QtXml/QDomElement>
+#include <QtCore/QRectF>
+#include <QtGui/QColor>
+#include <QtGui/QWidget>
 
-class QGraphicsPolygonItem;
-class QCoreApplication;
 class QApplication;
-class Title;
 class QGraphicsScene;
+class QTransform;
 
 class Title: public QObject
 {
 
 public:
-       Title( const QString & );
+       Title( const char *filename);
        virtual ~Title();
-       void drawKdenliveTitle( uint8_t*, int, int, double, char*, char* );
-       void reloadXml(char *templatexml, char *templatetext);
+       void drawKdenliveTitle( uint8_t*, int, int, double, const char*, const char* );
+       void loadFromXml( const char *templateXml, const QString templateText );
+
 private:
        QString m_filename;
-       int loadDocument( const QString& url, const QString templateXml, const QString templateText );
-       int  loadFromXml( QDomDocument doc, const QString templateText );
        QGraphicsScene *m_scene;
        QRectF m_start, m_end;
        QString colorToString( const QColor& );
index 125d36640e11df2881abec94f251e9493c19f689..72fa625a286126917d523137199fda3c27155d90 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 
+
 extern void init_qt();
 extern void close_qt();
 extern void refresh_kdenlivetitle( uint8_t*, int, int, double, char*, char*, int );
 
+void read_xml(mlt_properties properties)
+{
+       FILE *f = fopen( mlt_properties_get( properties, "resource" ), "r");
+       if ( f != NULL )
+       {
+               int size = 0;
+               long lSize;
+               fseek (f , 0 , SEEK_END);
+               lSize = ftell (f);
+               rewind (f);
+
+               char *infile = (char*) mlt_pool_alloc(lSize);
+               size=fread(infile,1,lSize,f);
+               infile[size] = '\0';
+               fclose(f);
+               mlt_properties_set(properties, "xmldata", infile);
+               mlt_pool_release( infile );
+       }
+}
+
 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
 {
-       // Obtain properties of frame
+       /* Obtain properties of frame */
        mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
 
-       // Obtain the producer for this frame
+       /* Obtain the producer for this frame */
        mlt_producer producer = mlt_properties_get_data( properties, "producer_kdenlivetitle", NULL );
 
-       // Obtain properties of producer
+       /* Obtain properties of producer */
        mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
        
-       // Allocate the image
+       /* Allocate the image */
        int size = *width * ( *height ) * 4;
 
        *buffer = mlt_pool_alloc( size );
-       // Update the frame
+
        mlt_properties_set_int( properties, "width", *width );
        mlt_properties_set_int( properties, "height", *height );
 
-       // Update the frame
-       mlt_properties_set_data( properties, "image", *buffer, size, mlt_pool_release, NULL );
-       //cache later ??
+
+
+       /* cache later ?? */
 
        if ( 1 )
        {
-               // Allocate the image
+               /* Allocate the image */
                *format = mlt_image_rgb24a;
                mlt_position in = mlt_producer_get_in( producer );
                mlt_position out = mlt_producer_get_out( producer );
                mlt_position time = mlt_producer_position( producer );
                double position = ( double )( time - in ) / ( double )( out - in + 1 );
                if ( mlt_properties_get_int( producer_props, "force_reload" ) ) {
-                       refresh_kdenlivetitle( *buffer, *width, *height, position, mlt_properties_get( producer_props, "xmldata" ), mlt_properties_get( producer_props, "templatetext" ),  1);
+                       read_xml(producer_props);
                        mlt_properties_set_int( producer_props, "force_reload", 0 );
                }
-               else refresh_kdenlivetitle( *buffer, *width, *height, position, mlt_properties_get( producer_props, "xmldata" ), mlt_properties_get( producer_props, "templatetext" ),  0);
+               refresh_kdenlivetitle( *buffer, *width, *height, position, mlt_properties_get( producer_props, "xmldata" ), mlt_properties_get( producer_props, "templatetext" ),  0);
+               /* Update the frame */
+               mlt_properties_set_data( properties, "image", *buffer, size, mlt_pool_release, NULL );
+               
                mlt_log_debug( MLT_PRODUCER_SERVICE( producer ), "width:%d height:%d %s\n", *width, *height, mlt_image_format_name( *format ) );
        }
 
        return 0;
 }
 
-int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
+static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
 
 {
 
-       // Generate a frame
+       /* Generate a frame */
        *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
 
        if ( *frame != NULL )
        {
-               // Obtain properties of frame and producer
+               /* Obtain properties of frame and producer */
                mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
 
-               // Obtain properties of producer
+               /* Obtain properties of producer */
                mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
 
-               // Set the producer on the frame properties
+               /* Set the producer on the frame properties */
                mlt_properties_set_data( properties, "producer_kdenlivetitle", producer, 0, NULL, NULL );
 
-               // Update timecode on the frame we're creating
+               /* Update timecode on the frame we're creating */
                mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
 
-               // Set producer-specific frame properties
+               /* Set producer-specific frame properties */
                mlt_properties_set_int( properties, "progressive", 1 );
                mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_props, "aspect_ratio" ) );
 
-               // Push the get_image method
+               /* Push the get_image method */
                mlt_frame_push_get_image( *frame, producer_get_image );
        }
 
-       // Calculate the next timecode
+       /* Calculate the next timecode */
        mlt_producer_prepare_next( producer );
 
        return 0;
 }
 
-void producer_close( mlt_producer producer )
+static void producer_close( mlt_producer producer )
 {
+       fprintf(stderr, "000000000000000000000000  CLOSING\n");
        producer->close = NULL;
-       mlt_producer_close( producer );
        close_qt();
+       mlt_producer_close( producer );
        free( producer );
 }
 
-mlt_producer producer_kdenlivetitle_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
-{
-       // Create a new producer object
-       mlt_producer this = mlt_producer_new( );
 
-       // Initialise the producer
+mlt_producer producer_kdenlivetitle_init( mlt_profile profile, mlt_service_type type, const char *id, char *filename )
+{
+       fprintf(stderr, "\n\n,,,,,,,,, CREATE 1 TITLE\n");
+       /* Create a new producer object */
 
-       if ( this != NULL )
+       mlt_producer producer = calloc( 1, sizeof( struct mlt_producer_s ) );
+       if ( producer != NULL && mlt_producer_init( producer, NULL ) == 0 )
        {
-               init_qt( arg );
-               // Callback registration
-               this->get_frame = producer_get_frame;
-               this->close = ( mlt_destructor ) producer_close;
-               mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
-               mlt_properties_set( properties, "resource", arg );
+               /* Get the properties interface */
+               mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
+               /* Callback registration */
+               producer->get_frame = producer_get_frame;
+               producer->close = ( mlt_destructor )producer_close;
+               mlt_properties_set( properties, "resource", filename );
+               mlt_properties_set_int( properties, "aspect_ratio", 1 );
+               read_xml(properties);
+               init_qt( filename );
+               return producer;
        }
-
-       return this;
+       free( producer );
+       return NULL;
 }