]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/chroma.c
Update references to ffmpeg header files to match new directory structure. All ffmpe...
[vlc] / modules / codec / ffmpeg / chroma.c
1 /*****************************************************************************
2  * chroma.c: chroma conversion using ffmpeg library
3  *****************************************************************************
4  * Copyright (C) 1999-2001 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.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
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc/vlc.h>
33 #include <vlc_vout.h>
34
35 #if defined(HAVE_LIBSWSCALE_SWSCALE_H) || defined(HAVE_LIBSWSCALE_TREE)
36 #include <vlc_filter.h>
37 #endif
38
39 /* ffmpeg header */
40 #ifdef HAVE_LIBAVCODEC_AVCODEC_H
41 #   include <libavcodec/avcodec.h>
42 #else
43 #   include <avcodec.h>
44 #endif
45
46 #include "ffmpeg.h"
47
48 #if !defined(HAVE_LIBSWSCALE_SWSCALE_H) && !defined(HAVE_LIBSWSCALE_TREE)
49 void E_(InitLibavcodec) ( vlc_object_t *p_object );
50 static void ChromaConversion( vout_thread_t *, picture_t *, picture_t * );
51
52 /*****************************************************************************
53  * chroma_sys_t: chroma method descriptor
54  *****************************************************************************
55  * This structure is part of the chroma transformation descriptor, it
56  * describes the chroma plugin specific properties.
57  *****************************************************************************/
58 struct chroma_sys_t
59 {
60     int i_src_vlc_chroma;
61     int i_src_ffmpeg_chroma;
62     int i_dst_vlc_chroma;
63     int i_dst_ffmpeg_chroma;
64     AVPicture tmp_pic;
65     ImgReSampleContext *p_rsc;
66 };
67
68 /*****************************************************************************
69  * OpenChroma: allocate a chroma function
70  *****************************************************************************
71  * This function allocates and initializes a chroma function
72  *****************************************************************************/
73 int E_(OpenChroma)( vlc_object_t *p_this )
74 {
75     vout_thread_t *p_vout = (vout_thread_t *)p_this;
76     int i_ffmpeg_chroma[2], i_vlc_chroma[2], i;
77
78     /*
79      * Check the source chroma first, then the destination chroma
80      */
81     i_vlc_chroma[0] = p_vout->render.i_chroma;
82     i_vlc_chroma[1] = p_vout->output.i_chroma;
83     for( i = 0; i < 2; i++ )
84     {
85         i_ffmpeg_chroma[i] = E_(GetFfmpegChroma)( i_vlc_chroma[i] );
86         if( i_ffmpeg_chroma[i] < 0 ) return VLC_EGENERIC;
87     }
88
89     p_vout->chroma.pf_convert = ChromaConversion;
90
91     p_vout->chroma.p_sys = malloc( sizeof( chroma_sys_t ) );
92     if( p_vout->chroma.p_sys == NULL )
93     {
94         return VLC_ENOMEM;
95     }
96
97     p_vout->chroma.p_sys->i_src_vlc_chroma = p_vout->render.i_chroma;
98     p_vout->chroma.p_sys->i_dst_vlc_chroma = p_vout->output.i_chroma;
99     p_vout->chroma.p_sys->i_src_ffmpeg_chroma = i_ffmpeg_chroma[0];
100     p_vout->chroma.p_sys->i_dst_ffmpeg_chroma = i_ffmpeg_chroma[1];
101
102     if( ( p_vout->render.i_height != p_vout->output.i_height ||
103           p_vout->render.i_width != p_vout->output.i_width ) &&
104         ( p_vout->chroma.p_sys->i_dst_vlc_chroma == VLC_FOURCC('I','4','2','0') ||
105           p_vout->chroma.p_sys->i_dst_vlc_chroma == VLC_FOURCC('Y','V','1','2') ))
106     {
107         msg_Dbg( p_vout, "preparing to resample picture" );
108         p_vout->chroma.p_sys->p_rsc =
109             img_resample_init( p_vout->output.i_width, p_vout->output.i_height,
110                                p_vout->render.i_width, p_vout->render.i_height );
111         avpicture_alloc( &p_vout->chroma.p_sys->tmp_pic,
112                          p_vout->chroma.p_sys->i_dst_ffmpeg_chroma,
113                          p_vout->render.i_width, p_vout->render.i_height );
114     }
115     else
116     {
117         msg_Dbg( p_vout, "no resampling" );
118         p_vout->chroma.p_sys->p_rsc = NULL;
119     }
120
121     /* libavcodec needs to be initialized for some chroma conversions */
122     E_(InitLibavcodec)(p_this);
123
124     return VLC_SUCCESS;
125 }
126
127 /*****************************************************************************
128  * ChromaConversion: actual chroma conversion function
129  *****************************************************************************/
130 static void ChromaConversion( vout_thread_t *p_vout,
131                               picture_t *p_src, picture_t *p_dest )
132 {
133     AVPicture src_pic;
134     AVPicture dest_pic;
135     int i;
136
137     /* Prepare the AVPictures for converion */
138     for( i = 0; i < p_src->i_planes; i++ )
139     {
140         src_pic.data[i] = p_src->p[i].p_pixels;
141         src_pic.linesize[i] = p_src->p[i].i_pitch;
142     }
143     for( i = 0; i < p_dest->i_planes; i++ )
144     {
145         dest_pic.data[i] = p_dest->p[i].p_pixels;
146         dest_pic.linesize[i] = p_dest->p[i].i_pitch;
147     }
148
149     /* Special cases */
150     if( p_vout->chroma.p_sys->i_src_vlc_chroma == VLC_FOURCC('Y','V','1','2') ||
151         p_vout->chroma.p_sys->i_src_vlc_chroma == VLC_FOURCC('Y','V','U','9') )
152     {
153         /* Invert U and V */
154         src_pic.data[1] = p_src->p[2].p_pixels;
155         src_pic.data[2] = p_src->p[1].p_pixels;
156     }
157     if( p_vout->chroma.p_sys->i_dst_vlc_chroma == VLC_FOURCC('Y','V','1','2') ||
158         p_vout->chroma.p_sys->i_dst_vlc_chroma == VLC_FOURCC('Y','V','U','9') )
159     {
160         /* Invert U and V */
161         dest_pic.data[1] = p_dest->p[2].p_pixels;
162         dest_pic.data[2] = p_dest->p[1].p_pixels;
163     }
164     if( p_vout->chroma.p_sys->i_src_ffmpeg_chroma == PIX_FMT_RGB24 )
165         if( p_vout->render.i_bmask == 0x00ff0000 )
166             p_vout->chroma.p_sys->i_src_ffmpeg_chroma = PIX_FMT_BGR24;
167
168     if( p_vout->chroma.p_sys->p_rsc )
169     {
170         img_convert( &p_vout->chroma.p_sys->tmp_pic,
171                      p_vout->chroma.p_sys->i_dst_ffmpeg_chroma,
172                      &src_pic, p_vout->chroma.p_sys->i_src_ffmpeg_chroma,
173                      p_vout->render.i_width, p_vout->render.i_height );
174         img_resample( p_vout->chroma.p_sys->p_rsc, &dest_pic,
175                       &p_vout->chroma.p_sys->tmp_pic );
176     }
177     else
178     {
179         img_convert( &dest_pic, p_vout->chroma.p_sys->i_dst_ffmpeg_chroma,
180                      &src_pic, p_vout->chroma.p_sys->i_src_ffmpeg_chroma,
181                      p_vout->render.i_width, p_vout->render.i_height );
182     }
183 }
184
185 /*****************************************************************************
186  * CloseChroma: free the chroma function
187  *****************************************************************************
188  * This function frees the previously allocated chroma function
189  *****************************************************************************/
190 void E_(CloseChroma)( vlc_object_t *p_this )
191 {
192     vout_thread_t *p_vout = (vout_thread_t *)p_this;
193     if( p_vout->chroma.p_sys->p_rsc )
194     {
195         img_resample_close( p_vout->chroma.p_sys->p_rsc );
196         avpicture_free( &p_vout->chroma.p_sys->tmp_pic );
197     }
198     free( p_vout->chroma.p_sys );
199 }
200 #else
201
202 static void ChromaConversion( vout_thread_t *, picture_t *, picture_t * );
203
204 /*****************************************************************************
205  * chroma_sys_t: chroma method descriptor
206  *****************************************************************************
207  * This structure is part of the chroma transformation descriptor, it
208  * describes the chroma plugin specific properties.
209  *****************************************************************************/
210 struct chroma_sys_t
211 {
212     filter_t *p_swscaler;
213 };
214
215 /*****************************************************************************
216  * Video Filter2 functions
217  *****************************************************************************/
218 struct filter_owner_sys_t
219 {
220     vout_thread_t *p_vout;
221 };
222
223 static void PictureRelease( picture_t *p_pic )
224 {
225     if( p_pic->p_data_orig ) free( p_pic->p_data_orig );
226 }
227
228 static picture_t *video_new_buffer_filter( filter_t *p_filter )
229 {
230     picture_t *p_picture = malloc( sizeof(picture_t) );
231     if( !p_picture ) return NULL;
232     if( vout_AllocatePicture( p_filter, p_picture,
233                               p_filter->fmt_out.video.i_chroma,
234                               p_filter->fmt_out.video.i_width,
235                               p_filter->fmt_out.video.i_height,
236                               p_filter->fmt_out.video.i_aspect )
237         != VLC_SUCCESS )
238     {
239         free( p_picture );
240         return NULL;
241     }
242
243     p_picture->pf_release = PictureRelease;
244
245     return p_picture;
246 }
247
248 static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
249 {
250     VLC_UNUSED(p_filter);
251     if( p_pic )
252     {
253         free( p_pic->p_data_orig );
254         free( p_pic );
255     }
256 }
257
258 /*****************************************************************************
259  * OpenChroma: allocate a chroma function
260  *****************************************************************************
261  * This function allocates and initializes a chroma function
262  *****************************************************************************/
263 int E_(OpenChroma)( vlc_object_t *p_this )
264 {
265     vout_thread_t *p_vout = (vout_thread_t *)p_this;
266     chroma_sys_t  *p_sys = p_vout->chroma.p_sys;
267
268     p_vout->chroma.p_sys = p_sys = malloc( sizeof( chroma_sys_t ) );
269     if( p_vout->chroma.p_sys == NULL )
270     {
271         return VLC_ENOMEM;
272     }
273     p_vout->chroma.pf_convert = ChromaConversion;
274
275     p_sys->p_swscaler = vlc_object_create( p_vout, VLC_OBJECT_FILTER );
276     vlc_object_attach( p_sys->p_swscaler, p_vout );
277
278     p_sys->p_swscaler->pf_vout_buffer_new = video_new_buffer_filter;
279     p_sys->p_swscaler->pf_vout_buffer_del = video_del_buffer_filter;
280
281     p_sys->p_swscaler->fmt_out.video.i_x_offset =
282         p_sys->p_swscaler->fmt_out.video.i_y_offset = 0;
283     p_sys->p_swscaler->fmt_in.video = p_vout->fmt_in;
284     p_sys->p_swscaler->fmt_out.video = p_vout->fmt_out;
285     p_sys->p_swscaler->fmt_out.video.i_aspect = p_vout->render.i_aspect;
286     p_sys->p_swscaler->fmt_in.video.i_chroma = p_vout->render.i_chroma;
287     p_sys->p_swscaler->fmt_out.video.i_chroma = p_vout->output.i_chroma;
288
289     p_sys->p_swscaler->p_module = module_Need( p_sys->p_swscaler,
290                            "video filter2", 0, 0 );
291
292     if( p_sys->p_swscaler->p_module )
293     {
294         p_sys->p_swscaler->p_owner =
295             malloc( sizeof( filter_owner_sys_t ) );
296         if( p_sys->p_swscaler->p_owner )
297             p_sys->p_swscaler->p_owner->p_vout = p_vout;
298     }
299
300     if( !p_sys->p_swscaler->p_module || !p_sys->p_swscaler->p_owner )
301     {
302         vlc_object_detach( p_sys->p_swscaler );
303         vlc_object_release( p_sys->p_swscaler );
304         free( p_vout->chroma.p_sys );
305         return VLC_EGENERIC;
306     }
307
308     return VLC_SUCCESS;
309 }
310
311 /*****************************************************************************
312  * ChromaConversion: actual chroma conversion function
313  *****************************************************************************/
314 static void ChromaConversion( vout_thread_t *p_vout,
315                               picture_t *p_src, picture_t *p_dest )
316 {
317     chroma_sys_t *p_sys = (chroma_sys_t *) p_vout->chroma.p_sys;
318
319     if( p_sys && p_src && p_dest && 
320         p_sys->p_swscaler && p_sys->p_swscaler->p_module )
321     {
322         picture_t *p_pic;
323
324         p_sys->p_swscaler->fmt_in.video = p_vout->fmt_in;
325         p_sys->p_swscaler->fmt_out.video = p_vout->fmt_out;
326
327 #if 0
328         msg_Dbg( p_vout, "chroma %4.4s (%d) to %4.4s (%d)",
329                  (char *)&p_vout->fmt_in.i_chroma, p_src->i_planes,
330                  (char *)&p_vout->fmt_out.i_chroma, p_dest->i_planes  );
331 #endif
332         p_pic = p_sys->p_swscaler->pf_vout_buffer_new( p_sys->p_swscaler );
333         if( p_pic )
334         {
335             picture_t *p_dst_pic;
336             vout_CopyPicture( p_vout, p_pic, p_src );
337             p_dst_pic = p_sys->p_swscaler->pf_video_filter( p_sys->p_swscaler, p_pic );
338             vout_CopyPicture( p_vout, p_dest, p_dst_pic );
339             p_dst_pic->pf_release( p_dst_pic );
340         }
341     }
342 }
343
344 /*****************************************************************************
345  * CloseChroma: free the chroma function
346  *****************************************************************************
347  * This function frees the previously allocated chroma function
348  *****************************************************************************/
349 void E_(CloseChroma)( vlc_object_t *p_this )
350 {
351     vout_thread_t *p_vout = (vout_thread_t *)p_this;
352     chroma_sys_t  *p_sys  = (chroma_sys_t *)p_vout->chroma.p_sys;
353     if( p_sys->p_swscaler && p_sys->p_swscaler->p_module )
354     {
355         free( p_sys->p_swscaler->p_owner );
356         module_Unneed( p_sys->p_swscaler, p_sys->p_swscaler->p_module );
357         vlc_object_detach( p_sys->p_swscaler );
358         vlc_object_release( p_sys->p_swscaler );
359         p_sys->p_swscaler= NULL;
360     }
361     free( p_vout->chroma.p_sys );
362 }
363
364 #endif /* !defined(HAVE_LIBSWSCALE_SWSCALE_H) && !defined(HAVE_LIBSWSCALE_TREE) */