westley/libxml2 mods, mcdv/mpeg release integration
[melted] / src / modules / dv / consumer_libdv.c
index 07bd6e6..de64f46 100644 (file)
@@ -34,6 +34,9 @@
 #include <libdv/dv.h>
 
 // Forward references.
+static int consumer_start( mlt_consumer this );
+static int consumer_stop( mlt_consumer this );
+static int consumer_is_stopped( mlt_consumer this );
 static int consumer_encode_video( mlt_consumer this, uint8_t *dv_frame, mlt_frame frame );
 static void consumer_encode_audio( mlt_consumer this, uint8_t *dv_frame, mlt_frame frame );
 static void consumer_output( mlt_consumer this, uint8_t *dv_frame, int size, mlt_frame frame );
@@ -54,26 +57,24 @@ mlt_consumer consumer_libdv_init( char *arg )
                // Get properties from the consumer
                mlt_properties properties = mlt_consumer_properties( this );
 
-               // Allocate a thread
-               pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
-
                // Assign close callback
                this->close = consumer_close;
 
-               // Assign all properties
+               // Interpret the constructor argument
                if ( arg == NULL || !strcmp( arg, "PAL" ) )
                        mlt_properties_set_double( properties, "fps", 25 );
                else
                        mlt_properties_set_double( properties, "fps", 29.97 );
 
-               mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
+               // Set the encode and output handling method
                mlt_properties_set_data( properties, "video", consumer_encode_video, 0, NULL, NULL );
                mlt_properties_set_data( properties, "audio", consumer_encode_audio, 0, NULL, NULL );
                mlt_properties_set_data( properties, "output", consumer_output, 0, NULL, NULL );
 
-               // Create the thread (this should not happen immediately)
-               mlt_properties_set_int( properties, "running", 1 );
-               pthread_create( thread, NULL, consumer_thread, this );
+               // Set up start/stop/terminated callbacks
+               this->start = consumer_start;
+               this->stop = consumer_stop;
+               this->is_stopped = consumer_is_stopped;
        }
        else
        {
@@ -86,6 +87,66 @@ mlt_consumer consumer_libdv_init( char *arg )
        return this;
 }
 
+/** Start the consumer.
+*/
+
+static int consumer_start( mlt_consumer this )
+{
+       // Get the properties
+       mlt_properties properties = mlt_consumer_properties( this );
+
+       // Check that we're not already running
+       if ( !mlt_properties_get_int( properties, "running" ) )
+       {
+               // Allocate a thread
+               pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
+
+               // Assign the thread to properties
+               mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
+
+               // Set the running state
+               mlt_properties_set_int( properties, "running", 1 );
+
+               // Create the thread
+               pthread_create( thread, NULL, consumer_thread, this );
+       }
+       return 0;
+}
+
+/** Stop the consumer.
+*/
+
+static int consumer_stop( mlt_consumer this )
+{
+       // Get the properties
+       mlt_properties properties = mlt_consumer_properties( this );
+
+       // Check that we're running
+       if ( mlt_properties_get_int( properties, "running" ) )
+       {
+               // Get the thread
+               pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
+
+               // Stop the thread
+               mlt_properties_set_int( properties, "running", 0 );
+
+               // Wait for termination
+               pthread_join( *thread, NULL );
+       }
+
+       return 0;
+}
+
+/** Determine if the consumer is stopped.
+*/
+
+static int consumer_is_stopped( mlt_consumer this )
+{
+       // Get the properties
+       mlt_properties properties = mlt_consumer_properties( this );
+       return !mlt_properties_get_int( properties, "running" );
+}
+
 /** Get or create a new libdv encoder.
 */
 
@@ -104,10 +165,10 @@ static dv_encoder_t *libdv_get_encoder( mlt_consumer this, mlt_frame frame )
                double fps = mlt_properties_get_double( this_properties, "fps" );
 
                // Create the encoder
-               encoder = dv_encoder_new( fps != 25, 0, 0 );
+               encoder = dv_encoder_new( 0, 0, 0 );
 
                // Encoder settings
-               encoder->isPAL = fps = 25;
+               encoder->isPAL = fps == 25;
                encoder->is16x9 = 0;
                encoder->vlc_encode_passes = 1;
                encoder->static_qno = 0;
@@ -140,6 +201,9 @@ static int consumer_encode_video( mlt_consumer this, uint8_t *dv_frame, mlt_fram
        // This will hold the size of the dv frame
        int size = 0;
 
+       // determine if this a test card
+       int is_test = mlt_frame_is_test_card( frame );
+
        // If we get an encoder, then encode the image
        if ( encoder != NULL )
        {
@@ -168,10 +232,13 @@ static int consumer_encode_video( mlt_consumer this, uint8_t *dv_frame, mlt_fram
                }
 
                // Process the frame
-               if ( size != 0 )
+               if ( size != 0 && !( mlt_properties_get_int( this_properties, "was_test_card" ) && is_test ) )
                {
                        // Encode the image
                        dv_encode_full_frame( encoder, &image, e_dv_color_yuv, dv_frame );
+
+                       // Note test card status
+                       mlt_properties_set_int( this_properties, "was_test_card", is_test );
                }
        }
        
@@ -282,18 +349,26 @@ static void *consumer_thread( void *arg )
                // Get the frame
                if ( mlt_service_get_frame( service, &frame, 0 ) == 0 )
                {
-                       // Encode the image
-                       int size = video( this, dv_frame, frame );
-
-                       // Encode the audio
-                       if ( size > 0 )
-                               audio( this, dv_frame, frame );
-
-                       // Output the frame
-                       output( this, dv_frame, size, frame );
-
-                       // Close the frame
-                       mlt_frame_close( frame );
+                       // Obtain the dv_encoder
+                       if ( libdv_get_encoder( this, frame ) != NULL )
+                       {
+                               // Encode the image
+                               int size = video( this, dv_frame, frame );
+
+                               // Encode the audio
+                               if ( size > 0 )
+                                       audio( this, dv_frame, frame );
+
+                               // Output the frame
+                               output( this, dv_frame, size, frame );
+
+                               // Close the frame
+                               mlt_frame_close( frame );
+                       }
+                       else
+                       {
+                               fprintf( stderr, "Unable to obtain dv encoder.\n" );
+                       }
                }
        }
 
@@ -308,17 +383,8 @@ static void *consumer_thread( void *arg )
 
 static void consumer_close( mlt_consumer this )
 {
-       // Get the properties
-       mlt_properties properties = mlt_consumer_properties( this );
-
-       // Get the thread
-       pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
-
-       // Stop the thread
-       mlt_properties_set_int( properties, "running", 0 );
-
-       // Wait for termination
-       pthread_join( *thread, NULL );
+       // Stop the consumer
+       mlt_consumer_stop( this );
 
        // Close the parent
        mlt_consumer_close( this );