]> git.sesse.net Git - mlt/blob - src/modules/core/filter_fieldorder.c
Remove unused string.h include
[mlt] / src / modules / core / filter_fieldorder.c
1 /*
2  * filter_fieldorder.c -- change field dominance
3  * Copyright (C) 2011 Ushodaya Enterprises Limited
4  * Author: Dan Dennedy <dan@dennedy.org>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <framework/mlt.h>
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <math.h>
27
28 static int get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
29 {
30         // Get the properties from the frame
31         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
32
33         // Get the input image, width and height
34         int error = mlt_frame_get_image( frame, image, format, width, height, writable );
35
36         if ( !error && *image )
37         {
38                 int tff = mlt_properties_get_int( properties, "consumer_tff" );
39
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 );
44
45                 if ( mlt_properties_get_int( properties, "meta.swap_fields" ) &&
46                         mlt_properties_get( properties, "progressive" ) &&
47                         mlt_properties_get_int( properties, "progressive" ) == 0 )
48                 {
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 );
52
53                         // Make a new image
54                         int bpp;
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;
58                         int i = *height + 1;
59                         uint8_t *src = *image;
60
61                         // Set the new image
62                         mlt_frame_set_image( frame, new_image, size, mlt_pool_release );
63                         *image = new_image;
64
65                         while ( --i )
66                         {
67                                 memcpy( new_image, src + stride * !(i % 2), stride );
68                                 new_image += stride;
69                                 src += stride * (i % 2) * 2;
70                         }
71                 }
72
73                 // Correct field order if needed
74                 if ( tff != -1 &&
75                      mlt_properties_get_int( properties, "top_field_first" ) != tff &&
76                      mlt_properties_get( properties, "progressive" ) &&
77                      mlt_properties_get_int( properties, "progressive" ) == 0 )
78                 {
79                         // We only work with non-planar formats
80                         if ( *format == mlt_image_yuv420p )
81                         {
82                                 *format = mlt_image_yuv422;
83                                 mlt_frame_get_image( frame, image, format, width, height, writable );
84                         }
85
86                         // Shift the entire image down by one line
87                         int bpp;
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 );
93
94                         // Set the new image
95                         mlt_frame_set_image( frame, new_image, size, mlt_pool_release );
96                         *image = new_image;
97
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 );
101                 }
102         }
103
104         return error;
105 }
106
107 static mlt_frame process( mlt_filter filter, mlt_frame frame )
108 {
109         mlt_frame_push_get_image( frame, get_image );
110         return frame;
111 }
112
113 mlt_filter filter_fieldorder_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
114 {
115         mlt_filter filter = calloc( 1, sizeof( *filter ) );
116         if ( mlt_filter_init( filter, NULL ) == 0 )
117         {
118                 filter->process = process;
119         }
120         return filter;
121 }