]> git.sesse.net Git - mlt/blobdiff - src/modules/xine/filter_deinterlace.c
make mlt_position type double
[mlt] / src / modules / xine / filter_deinterlace.c
index 8916647b144fba4e1432980a8ca7bfdc7e71d7cf..70457e96dcc084eb2863f30d52f5f1d2972bc6f1 100644 (file)
  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  */
 
-#include "filter_deinterlace.h"
+#include <framework/mlt_filter.h>
+#include <framework/mlt_log.h>
+#include <framework/mlt_producer.h>
+#include <framework/mlt_events.h>
 #include "deinterlace.h"
+#include "yadif.h"
 
 #include <framework/mlt_frame.h>
 
 #include <string.h>
 #include <stdlib.h>
 
-/* Linear Blend filter - C version contributed by Rogerio Brito.
-   This algorithm has the same interface as the other functions.
+#define YADIF_MODE_TEMPORAL_SPATIAL (0)
+#define YADIF_MODE_TEMPORAL (2)
 
-   The destination "screen" (pdst) is constructed from the source
-   screen (psrc[0]) line by line.
+static yadif_filter *init_yadif( int width, int height )
+{
+       yadif_filter *yadif = mlt_pool_alloc( sizeof( *yadif ) );
 
-   The i-th line of the destination screen is the average of 3 lines
-   from the source screen: the (i-1)-th, i-th and (i+1)-th lines, with
-   the i-th line having weight 2 in the computation.
+       yadif->cpu = 0; // Pure C
+#ifdef USE_SSE
+       yadif->cpu |= AVS_CPU_INTEGER_SSE;
+#endif
+#ifdef USE_SSE2
+       yadif->cpu |= AVS_CPU_SSE2;
+#endif
+       // Create intermediate planar planes
+       yadif->yheight = height;
+       yadif->ywidth  = width;
+       yadif->uvwidth = yadif->ywidth / 2;
+       yadif->ypitch  = ( yadif->ywidth +  15 ) / 16 * 16;
+       yadif->uvpitch = ( yadif->uvwidth + 15 ) / 16 * 16;
+       yadif->ysrc  = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->ypitch );
+       yadif->usrc  = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->uvpitch);
+       yadif->vsrc  = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->uvpitch );
+       yadif->yprev = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->ypitch );
+       yadif->uprev = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->uvpitch );
+       yadif->vprev = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->uvpitch );
+       yadif->ynext = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->ypitch );
+       yadif->unext = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->uvpitch );
+       yadif->vnext = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->uvpitch );
+       yadif->ydest = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->ypitch );
+       yadif->udest = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->uvpitch );
+       yadif->vdest = (unsigned char *) mlt_pool_alloc( yadif->yheight * yadif->uvpitch );
 
-   Remarks:
-   * each line on pdst doesn't depend on previous lines;
-   * due to the way the algorithm is defined, the first & last lines of the
-     screen aren't deinterlaced.
+       return yadif;
+}
 
-*/
-#if 0
-static void deinterlace_yuv( uint8_t *pdst, uint8_t *psrc, int width, int height )
+static void close_yadif(yadif_filter *yadif)
+{
+       mlt_pool_release( yadif->ysrc );
+       mlt_pool_release( yadif->usrc );
+       mlt_pool_release( yadif->vsrc );
+       mlt_pool_release( yadif->yprev );
+       mlt_pool_release( yadif->uprev );
+       mlt_pool_release( yadif->vprev );
+       mlt_pool_release( yadif->ynext );
+       mlt_pool_release( yadif->unext );
+       mlt_pool_release( yadif->vnext );
+       mlt_pool_release( yadif->ydest );
+       mlt_pool_release( yadif->udest );
+       mlt_pool_release( yadif->vdest );
+       mlt_pool_release( yadif );
+
+#if defined(__GNUC__) && !defined(PIC)
+       // Set SSSE3 bit to cpu
+       asm (\
+       "mov $1, %%eax \n\t"\
+       "push %%ebx \n\t"\
+       "cpuid \n\t"\
+       "pop %%ebx \n\t"\
+       "mov %%ecx, %%edx \n\t"\
+       "shr $9, %%edx \n\t"\
+       "and $1, %%edx \n\t"\
+       "shl $9, %%edx \n\t"\
+       "and $511, %%ebx \n\t"\
+       "or %%edx, %%ebx \n\t"\
+       : "=b"(yadif->cpu) : "p"(yadif->cpu) : "%eax", "%ecx", "%edx");
+#endif
+}
+
+static int deinterlace_yadif( mlt_frame frame, mlt_filter filter, uint8_t **image, mlt_image_format *format, int *width, int *height, int mode )
 {
-       register int x, y;
-       register uint8_t *l0, *l1, *l2, *l3;
+       mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
+       mlt_frame previous_frame = mlt_properties_get_data( properties, "previous frame", NULL );
+       uint8_t* previous_image = NULL;
+       int previous_width = *width;
+       int previous_height = *height;
+       mlt_frame next_frame = mlt_properties_get_data( properties, "next frame", NULL );
+       uint8_t* next_image = NULL;
+       int next_width = *width;
+       int next_height = *height;
+       
+       mlt_log_debug( MLT_FILTER_SERVICE(filter), "previous " MLT_POSITION_FMT " current " MLT_POSITION_FMT " next " MLT_POSITION_FMT "\n",
+               previous_frame? mlt_frame_original_position(previous_frame) : -1,
+               mlt_frame_original_position(frame),
+               next_frame?  mlt_frame_original_position(next_frame) : -1);
 
-       l0 = pdst;                      // target line
-       l1 = psrc;                      // 1st source line
-       l2 = l1 + width;        // 2nd source line = line that follows l1
-       l3 = l2 + width;        // 3rd source line = line that follows l2
+       if ( !previous_frame || !next_frame )
+               return 1;
 
-       // Copy the first line
-       memcpy(l0, l1, width);
-       l0 += width;
+       // Get the preceding frame's image
+       int error = mlt_frame_get_image( previous_frame, &previous_image, format, &previous_width, &previous_height, 0 );
+       int progressive = mlt_properties_get_int( MLT_FRAME_PROPERTIES( previous_frame ), "progressive" );
 
-       for (y = 1; y < height-1; ++y) 
+       // Check that we aren't already progressive
+       if ( !error && previous_image && !progressive )
        {
-               // computes avg of: l1 + 2*l2 + l3
-               for (x = 0; x < width; ++x)
-                       l0[x] = (l1[x] + (l2[x]<<1) + l3[x]) >> 2;
+               // OK, now we know we have work to do and can request the image in our format
+               frame->convert_image( previous_frame, &previous_image, format, mlt_image_yuv422 );
 
-               // updates the line pointers
-               l1 = l2; l2 = l3; l3 += width;
-               l0 += width;
-       }
+               // Get the current frame's image
+               *format = mlt_image_yuv422;
+               error = mlt_frame_get_image( frame, image, format, width, height, 0 );
 
-       // Copy the last line
-       memcpy(l0, l1, width);
+               if ( !error && *image && *format == mlt_image_yuv422 )
+               {
+                       // Get the following frame's image
+                       error = mlt_frame_get_image( next_frame, &next_image, format, &next_width, &next_height, 0 );
+               
+                       if ( !error && next_image && *format == mlt_image_yuv422 )
+                       {
+                               yadif_filter *yadif = init_yadif( *width, *height );
+                               if ( yadif )
+                               {
+                                       const int order = mlt_properties_get_int( properties, "top_field_first" );
+                                       const int pitch = *width << 1;
+                                       const int parity = 0;
+
+                                       // Convert packed to planar
+                                       YUY2ToPlanes( *image, pitch, *width, *height, yadif->ysrc,
+                                               yadif->ypitch, yadif->usrc, yadif->vsrc, yadif->uvpitch, yadif->cpu );
+                                       YUY2ToPlanes( previous_image, pitch, *width, *height, yadif->yprev,
+                                               yadif->ypitch, yadif->uprev, yadif->vprev, yadif->uvpitch, yadif->cpu );
+                                       YUY2ToPlanes( next_image, pitch, *width, *height, yadif->ynext,
+                                               yadif->ypitch, yadif->unext, yadif->vnext, yadif->uvpitch, yadif->cpu );
+
+                                       // Deinterlace each plane
+                                       filter_plane( mode, yadif->ydest, yadif->ypitch, yadif->yprev, yadif->ysrc,
+                                               yadif->ynext, yadif->ypitch, *width, *height, parity, order, yadif->cpu);
+                                       filter_plane( mode, yadif->udest, yadif->uvpitch,yadif->uprev, yadif->usrc,
+                                               yadif->unext, yadif->uvpitch, *width >> 1, *height, parity, order, yadif->cpu);
+                                       filter_plane( mode, yadif->vdest, yadif->uvpitch, yadif->vprev, yadif->vsrc,
+                                               yadif->vnext, yadif->uvpitch, *width >> 1, *height, parity, order, yadif->cpu);
+
+                                       // Convert planar to packed
+                                       YUY2FromPlanes( *image, pitch, *width, *height, yadif->ydest,
+                                               yadif->ypitch, yadif->udest, yadif->vdest, yadif->uvpitch, yadif->cpu);
+
+                                       close_yadif( yadif );
+                               }
+                       }
+               }
+       }
+       else
+       {
+               // Get the current frame's image
+               error = mlt_frame_get_image( frame, image, format, width, height, 0 );
+       }
+       return error;
 }
-#endif
 
 /** Do it :-).
 */
 
-static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
+static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
 {
        int error = 0;
-       int deinterlace = mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "consumer_deinterlace" );
-       
-       // Pop the service off the stack
-       mlt_filter filter = mlt_frame_pop_service( this );
-
-       // Determine if we need a writable version or not
-       if ( deinterlace && !writable )
-                writable = !mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "progressive" );
+       mlt_filter filter = mlt_frame_pop_service( frame );
+       mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
+       int deinterlace = mlt_properties_get_int( properties, "consumer_deinterlace" );
+       // The progressive var should only represent the frame's original state and not the state
+       // as modified by this filter!
+       int progressive = mlt_properties_get_int( properties, "progressive" );
+       // At this point - before image was requested - (progressive == 0) cannot be trusted because
+       // some producers (avformat) do not yet know.
 
-       // Get the input image
-       error = mlt_frame_get_image( this, image, format, width, height, writable );
-
-       // Check that we want progressive and we aren't already progressive
-       if ( deinterlace && *format == mlt_image_yuv422 && *image != NULL )
+       if ( deinterlace && !mlt_properties_get_int( properties, "test_image" ) )
        {
                // Determine deinterlace method
                char *method_str = mlt_properties_get( MLT_FILTER_PROPERTIES( filter ), "method" );
-               int method = DEINTERLACE_LINEARBLEND;
-               char *frame_method_str = mlt_properties_get( MLT_FRAME_PROPERTIES( this ), "deinterlace_method" );
+               int method = DEINTERLACE_NONE;
+               char *frame_method_str = mlt_properties_get( properties, "deinterlace_method" );
                
-               if ( frame_method_str != NULL )
+               if ( frame_method_str )
                        method_str = frame_method_str;
                
-               if ( method_str == NULL )
+               if ( !method_str || strcmp( method_str, "yadif" ) == 0 )
+                       method = DEINTERLACE_YADIF;
+               else if ( strcmp( method_str, "yadif-nospatial" ) == 0 )
+                       method = DEINTERLACE_YADIF_NOSPATIAL;
+               else if ( strcmp( method_str, "onefield" ) == 0 )
+                       method = DEINTERLACE_ONEFIELD;
+               else if ( strcmp( method_str, "linearblend" ) == 0 )
                        method = DEINTERLACE_LINEARBLEND;
                else if ( strcmp( method_str, "bob" ) == 0 )
                        method = DEINTERLACE_BOB;
@@ -110,14 +219,95 @@ static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *
                        method = DEINTERLACE_BOB;
                else if ( strcmp( method_str, "greedy" ) == 0 )
                        method = DEINTERLACE_GREEDY;
-               else if ( strcmp( method_str, "onefield" ) == 0 )
-                       method = DEINTERLACE_ONEFIELD;
+
+               // Some producers like pixbuf want rescale_width & _height, but will not get them if you request
+               // the previous image first. So, on the first iteration, we use linearblend.
+               if ( ( method == DEINTERLACE_YADIF || method == DEINTERLACE_YADIF_NOSPATIAL ) &&
+                       !mlt_properties_get_int( MLT_FILTER_PROPERTIES(filter), "_notfirst" ) )
+               {
+                       mlt_properties_set_int( MLT_FILTER_PROPERTIES(filter), "_notfirst", 1 );
+                       method = DEINTERLACE_LINEARBLEND;
+                       error = 1;
+               }
+
+               if ( method == DEINTERLACE_YADIF )
+               {
+                       error = deinterlace_yadif( frame, filter, image, format, width, height, YADIF_MODE_TEMPORAL_SPATIAL );
+               }
+               else if ( method == DEINTERLACE_YADIF_NOSPATIAL )
+               {
+                       error = deinterlace_yadif( frame, filter, image, format, width, height, YADIF_MODE_TEMPORAL );
+               }
+               if ( error || ( method > DEINTERLACE_NONE && method < DEINTERLACE_YADIF ) )
+               {
+                       mlt_service service = mlt_properties_get_data( MLT_FILTER_PROPERTIES(filter), "service", NULL );
+
+                       // Get the current frame's image
+                       int error2 = mlt_frame_get_image( frame, image, format, width, height, writable );
+                       progressive = mlt_properties_get_int( properties, "progressive" );
+
+                       if ( error )
+                       {
+                               method = DEINTERLACE_LINEARBLEND;
+                               // If YADIF requested, prev/next cancelled because some previous frames were progressive,
+                               // but new frames are interlaced, then turn prev/next frames back on.
+                               if ( !progressive )
+                                       mlt_properties_set_int( MLT_SERVICE_PROPERTIES(service), "_need_previous_next", 1 );
+                       }
+                       else
+                       {
+                               // Signal that we no longer need previous and next frames
+                               mlt_properties_set_int( MLT_SERVICE_PROPERTIES(service), "_need_previous_next", 0 );
+                       }
+                       error = error2;
                        
-               // Deinterlace the image
-               deinterlace_yuv( *image, image, *width * 2, *height, method );
-               
-               // Make sure that others know the frame is deinterlaced
-               mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "progressive", 1 );
+                       if ( !error && !progressive )
+                       {
+                               // OK, now we know we have work to do and can request the image in our format
+                               error = frame->convert_image( frame, image, format, mlt_image_yuv422 );
+
+                               // Check that we aren't already progressive
+                               if ( !error && *image && *format == mlt_image_yuv422 )
+                               {
+                                       // Deinterlace the image using one of the Xine deinterlacers
+                                       int image_size = *width * *height * 2;
+                                       uint8_t *new_image = mlt_pool_alloc( image_size );
+
+                                       deinterlace_yuv( new_image, image, *width * 2, *height, method );
+                                       mlt_frame_set_image( frame, new_image, image_size, mlt_pool_release );
+                                       *image = new_image;
+                               }
+                       }
+               }
+               else if ( method == DEINTERLACE_NONE )
+               {
+                       error = mlt_frame_get_image( frame, image, format, width, height, writable );
+               }
+
+               // update progressive flag after having obtained image
+               progressive = mlt_properties_get_int( properties, "progressive" );
+
+               mlt_log_debug( MLT_FILTER_SERVICE( filter ), "error %d deint %d prog %d fmt %s method %s\n",
+                       error, deinterlace, progressive, mlt_image_format_name( *format ), method_str ? method_str : "yadif" );
+
+               if ( !error )
+               {
+                       // Make sure that others know the frame is deinterlaced
+                       mlt_properties_set_int( properties, "progressive", 1 );
+               }
+       }
+       else
+       {
+               // Pass through
+               error = mlt_frame_get_image( frame, image, format, width, height, writable );
+       }
+
+       if ( !deinterlace || progressive )
+       {
+               // Signal that we no longer need previous and next frames
+               mlt_service service = mlt_properties_get_data( MLT_FILTER_PROPERTIES(filter), "service", NULL );
+               if ( service )
+                       mlt_properties_set_int( MLT_SERVICE_PROPERTIES(service), "_need_previous_next", 0 );
        }
 
        return error;
@@ -126,10 +316,10 @@ static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *
 /** Deinterlace filter processing - this should be lazy evaluation here...
 */
 
-static mlt_frame deinterlace_process( mlt_filter this, mlt_frame frame )
+static mlt_frame deinterlace_process( mlt_filter filter, mlt_frame frame )
 {
-       // Push this on to the service stack
-       mlt_frame_push_service( frame, this );
+       // Push filter on to the service stack
+       mlt_frame_push_service( frame, filter );
 
        // Push the get_image method on to the stack
        mlt_frame_push_get_image( frame, filter_get_image );
@@ -137,17 +327,24 @@ static mlt_frame deinterlace_process( mlt_filter this, mlt_frame frame )
        return frame;
 }
 
+static void on_service_changed( mlt_service owner, mlt_service filter )
+{
+       mlt_service service = mlt_properties_get_data( MLT_SERVICE_PROPERTIES(filter), "service", NULL );
+       mlt_properties_set_int( MLT_SERVICE_PROPERTIES(service), "_need_previous_next", 1 );
+}
+
 /** Constructor for the filter.
 */
 
-mlt_filter filter_deinterlace_init( void *arg )
+mlt_filter filter_deinterlace_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
 {
-       mlt_filter this = mlt_filter_new( );
-       if ( this != NULL )
+       mlt_filter filter = mlt_filter_new( );
+       if ( filter != NULL )
        {
-               this->process = deinterlace_process;
-               mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "method", arg );
+               filter->process = deinterlace_process;
+               mlt_properties_set( MLT_FILTER_PROPERTIES( filter ), "method", arg );
+               mlt_events_listen( MLT_FILTER_PROPERTIES( filter ), filter, "service-changed", (mlt_listener) on_service_changed );
        }
-       return this;
+       return filter;
 }