2 * filter_crop.c -- cropping filter
3 * Copyright (C) 2009 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_filter.h>
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_log.h>
30 static void crop( uint8_t *src
, uint8_t *dest
, int bpp
, int width
, int height
, int left
, int right
, int top
, int bottom
)
32 int stride
= ( width
- left
- right
) * bpp
+ 1;
33 int y
= height
- top
- bottom
+ 1;
34 uint8_t *s
= &src
[ ( ( top
* width
) + left
) * bpp
];
41 s
+= ( right
+ left
) * bpp
;
48 static int filter_get_image( mlt_frame
this, uint8_t **image
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
52 // Get the properties from the frame
53 mlt_properties properties
= MLT_FRAME_PROPERTIES( this );
55 // Correct Width/height if necessary
56 if ( *width
== 0 || *height
== 0 )
58 *width
= mlt_properties_get_int( properties
, "normalised_width" );
59 *height
= mlt_properties_get_int( properties
, "normalised_height" );
63 error
= mlt_frame_get_image( this, image
, format
, width
, height
, writable
);
65 int left
= mlt_properties_get_int( properties
, "crop.left" );
66 int right
= mlt_properties_get_int( properties
, "crop.right" );
67 int top
= mlt_properties_get_int( properties
, "crop.top" );
68 int bottom
= mlt_properties_get_int( properties
, "crop.bottom" );
69 int owidth
= *width
- left
- right
;
70 int oheight
= *height
- top
- bottom
;
72 // We only know how to process yuv422 at the moment
73 if ( ( owidth
!= *width
|| oheight
!= *height
) &&
74 error
== 0 && *format
== mlt_image_yuv422
&& *image
!= NULL
&& owidth
> 0 && oheight
> 0 )
76 // Provides a manual override for misreported field order
77 if ( mlt_properties_get( properties
, "meta.top_field_first" ) )
79 mlt_properties_set_int( properties
, "top_field_first", mlt_properties_get_int( properties
, "meta.top_field_first" ) );
80 mlt_properties_set_int( properties
, "meta.top_field_first", 0 );
84 mlt_properties_set_int( properties
, "top_field_first", !mlt_properties_get_int( properties
, "top_field_first" ) );
87 owidth
= *width
- left
- right
;
89 // Create the output image
90 uint8_t *output
= mlt_pool_alloc( owidth
* ( oheight
+ 1 ) * 2 );
93 // Call the generic resize
94 crop( *image
, output
, 2, *width
, *height
, left
, right
, top
, bottom
);
96 // Now update the frame
98 mlt_properties_set_data( properties
, "image", output
, owidth
* ( oheight
+ 1 ) * 2, ( mlt_destructor
)mlt_pool_release
, NULL
);
99 mlt_properties_set_int( properties
, "width", owidth
);
100 mlt_properties_set_int( properties
, "height", oheight
);
103 // We should resize the alpha too
104 uint8_t *alpha
= mlt_frame_get_alpha_mask( this );
107 uint8_t *newalpha
= mlt_pool_alloc( owidth
* oheight
);
110 crop( alpha
, newalpha
, 1, *width
, *height
, left
, right
, top
, bottom
);
111 mlt_properties_set_data( properties
, "alpha", newalpha
, owidth
* oheight
, ( mlt_destructor
)mlt_pool_release
, NULL
);
112 this->get_alpha_mask
= NULL
;
122 /** Filter processing.
125 static mlt_frame
filter_process( mlt_filter
this, mlt_frame frame
)
127 if ( mlt_properties_get_int( MLT_FILTER_PROPERTIES( this ), "active" ) )
129 // Push the get_image method on to the stack
130 mlt_frame_push_get_image( frame
, filter_get_image
);
134 mlt_properties filter_props
= MLT_FILTER_PROPERTIES( this );
135 mlt_properties frame_props
= MLT_FRAME_PROPERTIES( frame
);
136 int left
= mlt_properties_get_int( filter_props
, "left" );
137 int right
= mlt_properties_get_int( filter_props
, "right" );
138 int top
= mlt_properties_get_int( filter_props
, "top" );
139 int bottom
= mlt_properties_get_int( filter_props
, "bottom" );
140 int width
= mlt_properties_get_int( frame_props
, "real_width" );
141 int height
= mlt_properties_get_int( frame_props
, "real_height" );
143 mlt_properties_set_int( frame_props
, "crop.left", left
);
144 mlt_properties_set_int( frame_props
, "crop.right", right
);
145 mlt_properties_set_int( frame_props
, "crop.top", top
);
146 mlt_properties_set_int( frame_props
, "crop.bottom", bottom
);
147 mlt_properties_set_int( frame_props
, "real_width", width
- left
- right
);
148 mlt_properties_set_int( frame_props
, "real_height", height
- top
- bottom
);
153 /** Constructor for the filter.
156 mlt_filter
filter_crop_init( mlt_profile profile
, mlt_service_type type
, const char *id
, char *arg
)
158 mlt_filter
this = calloc( sizeof( struct mlt_filter_s
), 1 );
159 if ( mlt_filter_init( this, this ) == 0 )
161 this->process
= filter_process
;
163 mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "active", atoi( arg
) );