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 )
52 *format = mlt_image_yuv422;
53 mlt_frame_get_image( frame, image, format, width, height, writable );
58 int size = mlt_image_format_size( *format, *width, *height, &bpp );
59 uint8_t *new_image = mlt_pool_alloc( size );
60 int stride = *width * bpp;
62 uint8_t *src = *image;
65 mlt_frame_set_image( frame, new_image, size, mlt_pool_release );
70 memcpy( new_image, src + stride * !(i % 2), stride );
72 src += stride * (i % 2) * 2;
76 // Correct field order if needed
77 if ( mlt_properties_get_int( properties, "top_field_first" ) != tff &&
78 mlt_properties_get( properties, "progressive" ) &&
79 mlt_properties_get_int( properties, "progressive" ) == 0 )
81 // We only work with non-planar formats
82 if ( *format == mlt_image_yuv420p )
84 *format = mlt_image_yuv422;
85 mlt_frame_get_image( frame, image, format, width, height, writable );
88 // Shift the entire image down by one line
90 int size = mlt_image_format_size( *format, *width, *height, &bpp );
91 uint8_t *new_image = mlt_pool_alloc( size );
92 uint8_t *ptr = new_image + *width * bpp;
93 memcpy( new_image, *image, *width * bpp );
94 memcpy( ptr, *image, *width * ( *height - 1 ) * bpp );
97 mlt_frame_set_image( frame, new_image, size, mlt_pool_release );
100 // Set the normalised field order
101 mlt_properties_set_int( properties, "top_field_first", tff );
102 mlt_properties_set_int( properties, "meta.top_field_first", tff );
109 static mlt_frame process( mlt_filter filter, mlt_frame frame )
111 mlt_frame_push_get_image( frame, get_image );
115 mlt_filter filter_fieldorder_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
117 mlt_filter filter = calloc( 1, sizeof( *filter ) );
118 if ( mlt_filter_init( filter, NULL ) == 0 )
120 filter->process = process;