2 * filter_fieldorder.c -- change field dominance
3 * Copyright (C) 2011 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <framework/mlt.h>
28 static int get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
30 // Get the properties from the frame
31 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
33 // Get the input image, width and height
34 int error = mlt_frame_get_image( frame, image, format, width, height, writable );
36 if ( !error && *image )
38 int tff = mlt_properties_get_int( properties, "consumer_tff" );
40 // Provides a manual override for misreported field order
41 if ( mlt_properties_get( properties, "meta.top_field_first" ) )
42 mlt_properties_set_int( properties, "top_field_first", mlt_properties_get_int( properties, "meta.top_field_first" ) );
43 mlt_log_debug( NULL, "TFF in %d out %d\n", mlt_properties_get_int( properties, "top_field_first" ), tff );
45 if ( mlt_properties_get_int( properties, "meta.swap_fields" ) &&
46 mlt_properties_get( properties, "progressive" ) &&
47 mlt_properties_get_int( properties, "progressive" ) == 0 )
49 // We only work with non-planar formats
50 if ( *format == mlt_image_yuv420p && frame->convert_image )
51 error = frame->convert_image( frame, image, format, mlt_image_yuv422 );
55 int size = mlt_image_format_size( *format, *width, *height, &bpp );
56 uint8_t *new_image = mlt_pool_alloc( size );
57 int stride = *width * bpp;
59 uint8_t *src = *image;
62 mlt_frame_set_image( frame, new_image, size, mlt_pool_release );
67 memcpy( new_image, src + stride * !(i % 2), stride );
69 src += stride * (i % 2) * 2;
73 // Correct field order if needed
75 mlt_properties_get_int( properties, "top_field_first" ) != tff &&
76 mlt_properties_get( properties, "progressive" ) &&
77 mlt_properties_get_int( properties, "progressive" ) == 0 )
79 // We only work with non-planar formats
80 if ( *format == mlt_image_yuv420p )
82 *format = mlt_image_yuv422;
83 mlt_frame_get_image( frame, image, format, width, height, writable );
86 // Shift the entire image down by one line
88 int size = mlt_image_format_size( *format, *width, *height, &bpp );
89 uint8_t *new_image = mlt_pool_alloc( size );
90 uint8_t *ptr = new_image + *width * bpp;
91 memcpy( new_image, *image, *width * bpp );
92 memcpy( ptr, *image, *width * ( *height - 1 ) * bpp );
95 mlt_frame_set_image( frame, new_image, size, mlt_pool_release );
98 // Set the normalised field order
99 mlt_properties_set_int( properties, "top_field_first", tff );
100 mlt_properties_set_int( properties, "meta.top_field_first", tff );
107 static mlt_frame process( mlt_filter filter, mlt_frame frame )
109 mlt_frame_push_get_image( frame, get_image );
113 mlt_filter filter_fieldorder_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
115 mlt_filter filter = calloc( 1, sizeof( *filter ) );
116 if ( mlt_filter_init( filter, NULL ) == 0 )
118 filter->process = process;