]> git.sesse.net Git - vlc/blob - modules/video_filter/invert.c
4ae18aea9e76b41abcb1e510cda45f4d59a682bf
[vlc] / modules / video_filter / invert.c
1 /*****************************************************************************
2  * invert.c : Invert video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <string.h>
28
29 #include <vlc/vlc.h>
30 #include <vlc_vout.h>
31
32 #include "vlc_filter.h"
33
34 /*****************************************************************************
35  * Local prototypes
36  *****************************************************************************/
37 static int  Create      ( vlc_object_t * );
38 static void Destroy     ( vlc_object_t * );
39
40 static picture_t *Filter( filter_t *, picture_t * );
41
42 /*****************************************************************************
43  * Module descriptor
44  *****************************************************************************/
45 vlc_module_begin();
46     set_description( _("Invert video filter") );
47     set_shortname( _("Color inversion" ));
48     set_category( CAT_VIDEO );
49     set_subcategory( SUBCAT_VIDEO_VFILTER );
50     set_capability( "video filter2", 0 );
51     add_shortcut( "invert" );
52     set_callbacks( Create, Destroy );
53 vlc_module_end();
54
55 /*****************************************************************************
56  * vout_sys_t: Invert video output method descriptor
57  *****************************************************************************
58  * This structure is part of the video output thread descriptor.
59  * It describes the Invert specific properties of an output thread.
60  *****************************************************************************/
61 struct filter_sys_t
62 {
63 };
64
65 /*****************************************************************************
66  * Create: allocates Invert video thread output method
67  *****************************************************************************
68  * This function allocates and initializes a Invert vout method.
69  *****************************************************************************/
70 static int Create( vlc_object_t *p_this )
71 {
72     filter_t *p_filter = (filter_t *)p_this;
73
74     /* Allocate structure */
75     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
76     if( p_filter->p_sys == NULL )
77     {
78         msg_Err( p_filter, "out of memory" );
79         return VLC_ENOMEM;
80     }
81
82     p_filter->pf_video_filter = Filter;
83
84     return VLC_SUCCESS;
85 }
86
87 /*****************************************************************************
88  * Destroy: destroy Invert video thread output method
89  *****************************************************************************
90  * Terminate an output method created by InvertCreateOutputMethod
91  *****************************************************************************/
92 static void Destroy( vlc_object_t *p_this )
93 {
94     filter_t *p_filter = (filter_t *)p_this;
95
96     free( p_filter->p_sys );
97 }
98
99 /*****************************************************************************
100  * Render: displays previously rendered output
101  *****************************************************************************
102  * This function send the currently rendered image to Invert image, waits
103  * until it is displayed and switch the two rendering buffers, preparing next
104  * frame.
105  *****************************************************************************/
106 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
107 {
108     picture_t *p_outpic;
109     int i_index;
110     int i_planes;
111
112     if( !p_pic ) return NULL;
113
114     p_outpic = p_filter->pf_vout_buffer_new( p_filter );
115     if( !p_outpic )
116     {
117         msg_Warn( p_filter, "can't get output picture" );
118         if( p_pic->pf_release )
119             p_pic->pf_release( p_pic );
120         return NULL;
121     }
122
123     if( p_pic->format.i_chroma == VLC_FOURCC('Y','U','V','A') )
124     {
125         /* We don't want to invert the alpha plane */
126         i_planes = p_pic->i_planes - 1;
127         p_filter->p_libvlc->pf_memcpy(
128             p_outpic->p[A_PLANE].p_pixels, p_pic->p[A_PLANE].p_pixels,
129             p_pic->p[A_PLANE].i_pitch *  p_pic->p[A_PLANE].i_lines );
130     }
131     else
132     {
133         i_planes = p_pic->i_planes;
134     }
135
136     for( i_index = 0 ; i_index < i_planes ; i_index++ )
137     {
138         uint8_t *p_in, *p_in_end, *p_line_end, *p_out;
139
140         p_in = p_pic->p[i_index].p_pixels;
141         p_in_end = p_in + p_pic->p[i_index].i_visible_lines
142                            * p_pic->p[i_index].i_pitch;
143
144         p_out = p_outpic->p[i_index].p_pixels;
145
146         for( ; p_in < p_in_end ; )
147         {
148             uint64_t *p_in64, *p_out64;
149
150             p_line_end = p_in + p_pic->p[i_index].i_visible_pitch - 64;
151
152             p_in64 = (uint64_t*)p_in;
153             p_out64 = (uint64_t*)p_out;
154
155             for( ; (ptrdiff_t)p_in64 < (ptrdiff_t)p_line_end ; )
156             {
157                 /* Do 64 pixels at a time */
158                 *p_out64++ = ~*p_in64++; *p_out64++ = ~*p_in64++;
159                 *p_out64++ = ~*p_in64++; *p_out64++ = ~*p_in64++;
160                 *p_out64++ = ~*p_in64++; *p_out64++ = ~*p_in64++;
161                 *p_out64++ = ~*p_in64++; *p_out64++ = ~*p_in64++;
162             }
163
164             p_in = (uint8_t*)p_in64;
165             p_out = (uint8_t*)p_out64;
166             p_line_end += 64;
167
168             for( ; p_in < p_line_end ; )
169             {
170                 *p_out++ = ~( *p_in++ );
171             }
172
173             p_in += p_pic->p[i_index].i_pitch
174                      - p_pic->p[i_index].i_visible_pitch;
175             p_out += p_outpic->p[i_index].i_pitch
176                      - p_outpic->p[i_index].i_visible_pitch;
177         }
178     }
179
180     p_outpic->date = p_pic->date;
181     p_outpic->b_force = p_pic->b_force;
182     p_outpic->i_nb_fields = p_pic->i_nb_fields;
183     p_outpic->b_progressive = p_pic->b_progressive;
184     p_outpic->b_top_field_first = p_pic->b_top_field_first;
185
186     if( p_pic->pf_release )
187         p_pic->pf_release( p_pic );
188
189     return p_outpic;
190 }