Cleanup license declarations and remove dv1394d references.
[melted] / src / modules / inigo / producer_inigo.c
index 2bb3ccf..95e99b2 100644 (file)
@@ -3,19 +3,19 @@
  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
  * Author: Charles Yates <charles.yates@pandora.be>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
  *
- * This program is distributed in the hope that it will be useful,
+ * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
 #include "producer_inigo.h"
@@ -47,7 +47,7 @@ mlt_producer producer_inigo_file_init( char *file )
 
        if ( result != NULL )
        {
-               mlt_properties properties = mlt_producer_properties( result );
+               mlt_properties properties = MLT_PRODUCER_PROPERTIES( result );
                mlt_properties_set( properties, "resource", file );
        }
 
@@ -79,26 +79,30 @@ static mlt_producer create_producer( mlt_field field, char *file )
 
 static mlt_filter create_attach( mlt_field field, char *id, int track )
 {
-       char *arg = strchr( id, ':' );
+       char *temp = strdup( id );
+       char *arg = strchr( temp, ':' );
        if ( arg != NULL )
                *arg ++ = '\0';
-       mlt_filter filter = mlt_factory_filter( id, arg );
+       mlt_filter filter = mlt_factory_filter( temp, arg );
        if ( filter != NULL )
                track_service( field, filter, ( mlt_destructor )mlt_filter_close );
+       free( temp );
        return filter;
 }
 
 static mlt_filter create_filter( mlt_field field, char *id, int track )
 {
-       char *arg = strchr( id, ':' );
+       char *temp = strdup( id );
+       char *arg = strchr( temp, ':' );
        if ( arg != NULL )
                *arg ++ = '\0';
-       mlt_filter filter = mlt_factory_filter( id, arg );
+       mlt_filter filter = mlt_factory_filter( temp, arg );
        if ( filter != NULL )
        {
                mlt_field_plant_filter( field, filter, track );
                track_service( field, filter, ( mlt_destructor )mlt_filter_close );
        }
+       free( temp );
        return filter;
 }
 
@@ -124,13 +128,16 @@ mlt_producer producer_inigo_init( char **argv )
        mlt_tractor mix = NULL;
        mlt_playlist playlist = mlt_playlist_init( );
        mlt_properties group = mlt_properties_new( );
-       mlt_properties properties = group;
        mlt_tractor tractor = mlt_tractor_new( );
+       mlt_properties properties = MLT_TRACTOR_PROPERTIES( tractor );
        mlt_field field = mlt_tractor_field( tractor );
        mlt_properties field_properties = mlt_field_properties( field );
        mlt_multitrack multitrack = mlt_tractor_multitrack( tractor );
        char *title = NULL;
 
+       // Assistance for template construction (allows -track usage to specify the first track)
+       mlt_properties_set_int( MLT_PLAYLIST_PROPERTIES( playlist ), "_inigo_first", 1 );
+
        // We need to track the number of registered filters
        mlt_properties_set_int( field_properties, "registered", 0 );
 
@@ -147,9 +154,14 @@ mlt_producer producer_inigo_init( char **argv )
                        if ( group != NULL )
                                properties = group;
                }
-               else if ( !strcmp( argv[ i ], "-attach" ) || !strcmp( argv[ i ], "-chain" ) )
+               else if ( !strcmp( argv[ i ], "-attach" ) || 
+                                 !strcmp( argv[ i ], "-attach-cut" ) ||
+                                 !strcmp( argv[ i ], "-attach-track" ) ||
+                                 !strcmp( argv[ i ], "-attach-clip" ) )
                {
-                       int type = !strcmp( argv[ i ], "-attach" ) ? 0 : 1;
+                       int type = !strcmp( argv[ i ], "-attach" ) ? 0 : 
+                                          !strcmp( argv[ i ], "-attach-cut" ) ? 1 : 
+                                          !strcmp( argv[ i ], "-attach-track" ) ? 2 : 3;
                        mlt_filter filter = create_attach( field, argv[ ++ i ], track );
                        if ( producer != NULL && !mlt_producer_is_cut( producer ) )
                        {
@@ -157,17 +169,33 @@ mlt_producer producer_inigo_init( char **argv )
                                mlt_playlist_append( playlist, producer );
                                mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
                                producer = info.cut;
-                               properties = mlt_producer_properties( producer );
+                       }
+
+                       if ( type == 1 || type == 2 )
+                       {
+                               mlt_playlist_clip_info info;
+                               mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
+                               producer = info.cut;
                        }
 
                        if ( filter != NULL && mlt_playlist_count( playlist ) > 0 )
                        {
                                if ( type == 0 )
                                        mlt_service_attach( ( mlt_service )properties, filter );
-                               else
+                               else if ( type == 1 )
                                        mlt_service_attach( ( mlt_service )producer, filter );
+                               else if ( type == 2 )
+                                       mlt_service_attach( ( mlt_service )playlist, filter );
+                               else if ( type == 3 )
+                                       mlt_service_attach( ( mlt_service )mlt_producer_cut_parent( producer ), filter );
 
-                               properties = mlt_filter_properties( filter );
+                               properties = MLT_FILTER_PROPERTIES( filter );
+                               mlt_properties_inherit( properties, group );
+                       }
+                       else if ( filter != NULL )
+                       {
+                               mlt_service_attach( ( mlt_service )playlist, filter );
+                               properties = MLT_FILTER_PROPERTIES( filter );
                                mlt_properties_inherit( properties, group );
                        }
                }
@@ -183,7 +211,7 @@ mlt_producer producer_inigo_init( char **argv )
                                mlt_playlist_repeat_clip( playlist, mlt_playlist_count( playlist ) - 1, repeat );
                                mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
                                producer = info.cut;
-                               properties = mlt_producer_properties( producer );
+                               properties = MLT_PRODUCER_PROPERTIES( producer );
                        }
                }
                else if ( !strcmp( argv[ i ], "-split" ) )
@@ -200,7 +228,7 @@ mlt_producer producer_inigo_init( char **argv )
                                mlt_playlist_split( playlist, mlt_playlist_count( playlist ) - 1, split );
                                mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
                                producer = info.cut;
-                               properties = mlt_producer_properties( producer );
+                               properties = MLT_PRODUCER_PROPERTIES( producer );
                        }
                }
                else if ( !strcmp( argv[ i ], "-swap" ) )
@@ -214,7 +242,7 @@ mlt_producer producer_inigo_init( char **argv )
                                mlt_playlist_move( playlist, mlt_playlist_count( playlist ) - 2, mlt_playlist_count( playlist ) - 1 );
                                mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
                                producer = info.cut;
-                               properties = mlt_producer_properties( producer );
+                               properties = MLT_PRODUCER_PROPERTIES( producer );
                        }
                }
                else if ( !strcmp( argv[ i ], "-join" ) )
@@ -226,10 +254,14 @@ mlt_producer producer_inigo_init( char **argv )
                        if ( mlt_playlist_count( playlist ) > 0 )
                        {
                                mlt_playlist_clip_info info;
-                               mlt_playlist_join( playlist, mlt_playlist_count( playlist ) - clips - 1, clips, 0 );
+                               int clip = clips <= 0 ? 0 : mlt_playlist_count( playlist ) - clips - 1;
+                               if ( clip < 0 ) clip = 0;
+                               if ( clip >= mlt_playlist_count( playlist ) ) clip = mlt_playlist_count( playlist ) - 2;
+                               if ( clips < 0 ) clips =  mlt_playlist_count( playlist ) - 1;
+                               mlt_playlist_join( playlist, clip, clips, 0 );
                                mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
                                producer = info.cut;
-                               properties = mlt_producer_properties( producer );
+                               properties = MLT_PRODUCER_PROPERTIES( producer );
                        }
                }
                else if ( !strcmp( argv[ i ], "-remove" ) )
@@ -243,7 +275,7 @@ mlt_producer producer_inigo_init( char **argv )
                                mlt_playlist_remove( playlist, mlt_playlist_count( playlist ) - 1 );
                                mlt_playlist_get_clip_info( playlist, &info, mlt_playlist_count( playlist ) - 1 );
                                producer = info.cut;
-                               properties = mlt_producer_properties( producer );
+                               properties = MLT_PRODUCER_PROPERTIES( producer );
                        }
                }
                else if ( !strcmp( argv[ i ], "-mix" ) )
@@ -286,7 +318,7 @@ mlt_producer producer_inigo_init( char **argv )
                                transition = mlt_factory_transition( id, arg );
                                if ( transition != NULL )
                                {
-                                       properties = mlt_transition_properties( transition );
+                                       properties = MLT_TRANSITION_PROPERTIES( transition );
                                        mlt_properties_inherit( properties, group );
                                        mlt_field_plant_transition( field, transition, 0, 1 );
                                        mlt_properties_set_position( properties, "in", 0 );
@@ -305,7 +337,7 @@ mlt_producer producer_inigo_init( char **argv )
                        mlt_filter filter = create_filter( field, argv[ ++ i ], track );
                        if ( filter != NULL )
                        {
-                               properties = mlt_filter_properties( filter );
+                               properties = MLT_FILTER_PROPERTIES( filter );
                                mlt_properties_inherit( properties, group );
                        }
                }
@@ -314,7 +346,7 @@ mlt_producer producer_inigo_init( char **argv )
                        mlt_transition transition = create_transition( field, argv[ ++ i ], track - 1 );
                        if ( transition != NULL )
                        {
-                               properties = mlt_transition_properties( transition );
+                               properties = MLT_TRANSITION_PROPERTIES( transition );
                                mlt_properties_inherit( properties, group );
                        }
                }
@@ -326,6 +358,9 @@ mlt_producer producer_inigo_init( char **argv )
                        mlt_playlist_blank( playlist, atof( argv[ ++ i ] ) );
                }
                else if ( !strcmp( argv[ i ], "-track" ) ||
+                                 !strcmp( argv[ i ], "-null-track" ) ||
+                                 !strcmp( argv[ i ], "-video-track" ) ||
+                                 !strcmp( argv[ i ], "-audio-track" ) ||
                                  !strcmp( argv[ i ], "-hide-track" ) ||
                                  !strcmp( argv[ i ], "-hide-video" ) ||
                                  !strcmp( argv[ i ], "-hide-audio" ) )
@@ -333,21 +368,25 @@ mlt_producer producer_inigo_init( char **argv )
                        if ( producer != NULL && !mlt_producer_is_cut( producer ) )
                                mlt_playlist_append( playlist, producer );
                        producer = NULL;
-                       mlt_multitrack_connect( multitrack, mlt_playlist_producer( playlist ), track ++ );
-                       track_service( field, playlist, ( mlt_destructor )mlt_playlist_close );
-                       playlist = mlt_playlist_init( );
+                       if ( !mlt_properties_get_int( MLT_PLAYLIST_PROPERTIES( playlist ), "_inigo_first" ) || 
+                                 mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( playlist ) ) > 0 )
+                       {
+                               mlt_multitrack_connect( multitrack, MLT_PLAYLIST_PRODUCER( playlist ), track ++ );
+                               track_service( field, playlist, ( mlt_destructor )mlt_playlist_close );
+                               playlist = mlt_playlist_init( );
+                       }
                        if ( playlist != NULL )
                        {
-                               properties = mlt_playlist_properties( playlist );
-                               if ( !strcmp( argv[ i ], "-hide-track" ) )
+                               properties = MLT_PLAYLIST_PROPERTIES( playlist );
+                               if ( !strcmp( argv[ i ], "-null-track" ) || !strcmp( argv[ i ], "-hide-track" ) )
                                        mlt_properties_set_int( properties, "hide", 3 );
-                               else if ( !strcmp( argv[ i ], "-hide-video" ) )
+                               else if ( !strcmp( argv[ i ], "-audio-track" ) || !strcmp( argv[ i ], "-hide-video" ) )
                                        mlt_properties_set_int( properties, "hide", 1 );
-                               else if ( !strcmp( argv[ i ], "-hide-audio" ) )
+                               else if ( !strcmp( argv[ i ], "-video-track" ) || !strcmp( argv[ i ], "-hide-audio" ) )
                                        mlt_properties_set_int( properties, "hide", 2 );
                        }
                }
-               else if ( strchr( argv[ i ], '=' ) )
+               else if ( strchr( argv[ i ], '=' ) && strstr( argv[ i ], "<?xml" ) != argv[ i ] )
                {
                        mlt_properties_parse( properties, argv[ i ] );
                }
@@ -355,12 +394,12 @@ mlt_producer producer_inigo_init( char **argv )
                {
                        if ( producer != NULL && !mlt_producer_is_cut( producer ) )
                                mlt_playlist_append( playlist, producer );
-                       if ( title == NULL )
+                       if ( title == NULL && strstr( argv[ i ], "<?xml" ) != argv[ i ] )
                                title = argv[ i ];
                        producer = create_producer( field, argv[ i ] );
                        if ( producer != NULL )
                        {
-                               properties = mlt_producer_properties( producer );
+                               properties = MLT_PRODUCER_PROPERTIES( producer );
                                mlt_properties_inherit( properties, group );
                        }
                }
@@ -386,15 +425,17 @@ mlt_producer producer_inigo_init( char **argv )
        track_service( field, playlist, ( mlt_destructor )mlt_playlist_close );
 
        // We must have a playlist to connect
-       if ( mlt_playlist_count( playlist ) > 0 )
-               mlt_multitrack_connect( multitrack, mlt_playlist_producer( playlist ), track );
+       if ( !mlt_properties_get_int( MLT_PLAYLIST_PROPERTIES( playlist ), "_inigo_first" ) || 
+                 mlt_producer_get_playtime( MLT_PLAYLIST_PRODUCER( playlist ) ) > 0 )
+               mlt_multitrack_connect( multitrack, MLT_PLAYLIST_PRODUCER( playlist ), track );
 
-       mlt_producer prod = mlt_tractor_producer( tractor );
-       mlt_properties props = mlt_tractor_properties( tractor );
+       mlt_producer prod = MLT_TRACTOR_PRODUCER( tractor );
+       mlt_producer_optimise( prod );
+       mlt_properties props = MLT_TRACTOR_PROPERTIES( tractor );
        mlt_properties_set_data( props, "group", group, 0, ( mlt_destructor )mlt_properties_close, NULL );
-       mlt_properties_set_position( props, "length", mlt_producer_get_out( mlt_multitrack_producer( multitrack ) ) + 1 );
-       mlt_producer_set_in_and_out( prod, 0, mlt_producer_get_out( mlt_multitrack_producer( multitrack ) ) );
-       mlt_properties_set_double( props, "fps", mlt_producer_get_fps( mlt_multitrack_producer( multitrack ) ) );
+       mlt_properties_set_position( props, "length", mlt_producer_get_out( MLT_MULTITRACK_PRODUCER( multitrack ) ) + 1 );
+       mlt_producer_set_in_and_out( prod, 0, mlt_producer_get_out( MLT_MULTITRACK_PRODUCER( multitrack ) ) );
+       mlt_properties_set_double( props, "fps", mlt_producer_get_fps( MLT_MULTITRACK_PRODUCER( multitrack ) ) );
        if ( title != NULL )
                mlt_properties_set( props, "title", strchr( title, '/' ) ? strrchr( title, '/' ) + 1 : title );