]> git.sesse.net Git - mlt/blob - src/modules/core/filter_fieldorder.c
79223757323d84cd9e10801aa1313a353f0b7285
[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 )
51                         {
52                                 *format = mlt_image_yuv422;
53                                 mlt_frame_get_image( frame, image, format, width, height, writable );
54                         }
55
56                         // Make a new image
57                         int bpp;
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;
61                         int i = *height + 1;
62                         uint8_t *src = *image;
63
64                         // Set the new image
65                         mlt_frame_set_image( frame, new_image, size, mlt_pool_release );
66                         *image = new_image;
67
68                         while ( --i )
69                         {
70                                 memcpy( new_image, src + stride * !(i % 2), stride );
71                                 new_image += stride;
72                                 src += stride * (i % 2) * 2;
73                         }
74                 }
75
76                 // Correct field order if needed
77                 if ( tff != -1 &&
78                      mlt_properties_get_int( properties, "top_field_first" ) != tff &&
79                      mlt_properties_get( properties, "progressive" ) &&
80                      mlt_properties_get_int( properties, "progressive" ) == 0 )
81                 {
82                         // We only work with non-planar formats
83                         if ( *format == mlt_image_yuv420p )
84                         {
85                                 *format = mlt_image_yuv422;
86                                 mlt_frame_get_image( frame, image, format, width, height, writable );
87                         }
88
89                         // Shift the entire image down by one line
90                         int bpp;
91                         int size = mlt_image_format_size( *format, *width, *height, &bpp );
92                         uint8_t *new_image = mlt_pool_alloc( size );
93                         uint8_t *ptr = new_image + *width * bpp;
94                         memcpy( new_image, *image, *width * bpp );
95                         memcpy( ptr, *image, *width * ( *height - 1 ) * bpp );
96
97                         // Set the new image
98                         mlt_frame_set_image( frame, new_image, size, mlt_pool_release );
99                         *image = new_image;
100
101                         // Set the normalised field order
102                         mlt_properties_set_int( properties, "top_field_first", tff );
103                         mlt_properties_set_int( properties, "meta.top_field_first", tff );
104                 }
105         }
106
107         return error;
108 }
109
110 static mlt_frame process( mlt_filter filter, mlt_frame frame )
111 {
112         mlt_frame_push_get_image( frame, get_image );
113         return frame;
114 }
115
116 mlt_filter filter_fieldorder_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
117 {
118         mlt_filter filter = calloc( 1, sizeof( *filter ) );
119         if ( mlt_filter_init( filter, NULL ) == 0 )
120         {
121                 filter->process = process;
122         }
123         return filter;
124 }