1 /*****************************************************************************
2 * chroma.c: chroma conversion using ffmpeg library
3 *****************************************************************************
4 * Copyright (C) 1999-2001 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
30 #ifdef HAVE_FFMPEG_SWSCALE_H
31 #include <vlc_filter.h>
35 #ifdef HAVE_FFMPEG_AVCODEC_H
36 # include <ffmpeg/avcodec.h>
43 #if !defined(HAVE_FFMPEG_SWSCALE_H) && !defined(HAVE_LIBSWSCALE_TREE)
44 void E_(InitLibavcodec) ( vlc_object_t *p_object );
45 static void ChromaConversion( vout_thread_t *, picture_t *, picture_t * );
47 /*****************************************************************************
48 * chroma_sys_t: chroma method descriptor
49 *****************************************************************************
50 * This structure is part of the chroma transformation descriptor, it
51 * describes the chroma plugin specific properties.
52 *****************************************************************************/
56 int i_src_ffmpeg_chroma;
58 int i_dst_ffmpeg_chroma;
60 ImgReSampleContext *p_rsc;
63 /*****************************************************************************
64 * OpenChroma: allocate a chroma function
65 *****************************************************************************
66 * This function allocates and initializes a chroma function
67 *****************************************************************************/
68 int E_(OpenChroma)( vlc_object_t *p_this )
70 vout_thread_t *p_vout = (vout_thread_t *)p_this;
71 int i_ffmpeg_chroma[2], i_vlc_chroma[2], i;
74 * Check the source chroma first, then the destination chroma
76 i_vlc_chroma[0] = p_vout->render.i_chroma;
77 i_vlc_chroma[1] = p_vout->output.i_chroma;
78 for( i = 0; i < 2; i++ )
80 i_ffmpeg_chroma[i] = E_(GetFfmpegChroma)( i_vlc_chroma[i] );
81 if( i_ffmpeg_chroma[i] < 0 ) return VLC_EGENERIC;
84 p_vout->chroma.pf_convert = ChromaConversion;
86 p_vout->chroma.p_sys = malloc( sizeof( chroma_sys_t ) );
87 if( p_vout->chroma.p_sys == NULL )
92 p_vout->chroma.p_sys->i_src_vlc_chroma = p_vout->render.i_chroma;
93 p_vout->chroma.p_sys->i_dst_vlc_chroma = p_vout->output.i_chroma;
94 p_vout->chroma.p_sys->i_src_ffmpeg_chroma = i_ffmpeg_chroma[0];
95 p_vout->chroma.p_sys->i_dst_ffmpeg_chroma = i_ffmpeg_chroma[1];
97 if( ( p_vout->render.i_height != p_vout->output.i_height ||
98 p_vout->render.i_width != p_vout->output.i_width ) &&
99 ( p_vout->chroma.p_sys->i_dst_vlc_chroma == VLC_FOURCC('I','4','2','0') ||
100 p_vout->chroma.p_sys->i_dst_vlc_chroma == VLC_FOURCC('Y','V','1','2') ))
102 msg_Dbg( p_vout, "preparing to resample picture" );
103 p_vout->chroma.p_sys->p_rsc =
104 img_resample_init( p_vout->output.i_width, p_vout->output.i_height,
105 p_vout->render.i_width, p_vout->render.i_height );
106 avpicture_alloc( &p_vout->chroma.p_sys->tmp_pic,
107 p_vout->chroma.p_sys->i_dst_ffmpeg_chroma,
108 p_vout->render.i_width, p_vout->render.i_height );
112 msg_Dbg( p_vout, "no resampling" );
113 p_vout->chroma.p_sys->p_rsc = NULL;
116 /* libavcodec needs to be initialized for some chroma conversions */
117 E_(InitLibavcodec)(p_this);
122 /*****************************************************************************
123 * ChromaConversion: actual chroma conversion function
124 *****************************************************************************/
125 static void ChromaConversion( vout_thread_t *p_vout,
126 picture_t *p_src, picture_t *p_dest )
132 /* Prepare the AVPictures for converion */
133 for( i = 0; i < p_src->i_planes; i++ )
135 src_pic.data[i] = p_src->p[i].p_pixels;
136 src_pic.linesize[i] = p_src->p[i].i_pitch;
138 for( i = 0; i < p_dest->i_planes; i++ )
140 dest_pic.data[i] = p_dest->p[i].p_pixels;
141 dest_pic.linesize[i] = p_dest->p[i].i_pitch;
145 if( p_vout->chroma.p_sys->i_src_vlc_chroma == VLC_FOURCC('Y','V','1','2') ||
146 p_vout->chroma.p_sys->i_src_vlc_chroma == VLC_FOURCC('Y','V','U','9') )
149 src_pic.data[1] = p_src->p[2].p_pixels;
150 src_pic.data[2] = p_src->p[1].p_pixels;
152 if( p_vout->chroma.p_sys->i_dst_vlc_chroma == VLC_FOURCC('Y','V','1','2') ||
153 p_vout->chroma.p_sys->i_dst_vlc_chroma == VLC_FOURCC('Y','V','U','9') )
156 dest_pic.data[1] = p_dest->p[2].p_pixels;
157 dest_pic.data[2] = p_dest->p[1].p_pixels;
159 if( p_vout->chroma.p_sys->i_src_ffmpeg_chroma == PIX_FMT_RGB24 )
160 if( p_vout->render.i_bmask == 0x00ff0000 )
161 p_vout->chroma.p_sys->i_src_ffmpeg_chroma = PIX_FMT_BGR24;
163 if( p_vout->chroma.p_sys->p_rsc )
165 img_convert( &p_vout->chroma.p_sys->tmp_pic,
166 p_vout->chroma.p_sys->i_dst_ffmpeg_chroma,
167 &src_pic, p_vout->chroma.p_sys->i_src_ffmpeg_chroma,
168 p_vout->render.i_width, p_vout->render.i_height );
169 img_resample( p_vout->chroma.p_sys->p_rsc, &dest_pic,
170 &p_vout->chroma.p_sys->tmp_pic );
174 img_convert( &dest_pic, p_vout->chroma.p_sys->i_dst_ffmpeg_chroma,
175 &src_pic, p_vout->chroma.p_sys->i_src_ffmpeg_chroma,
176 p_vout->render.i_width, p_vout->render.i_height );
180 /*****************************************************************************
181 * CloseChroma: free the chroma function
182 *****************************************************************************
183 * This function frees the previously allocated chroma function
184 *****************************************************************************/
185 void E_(CloseChroma)( vlc_object_t *p_this )
187 vout_thread_t *p_vout = (vout_thread_t *)p_this;
188 if( p_vout->chroma.p_sys->p_rsc )
190 img_resample_close( p_vout->chroma.p_sys->p_rsc );
191 avpicture_free( &p_vout->chroma.p_sys->tmp_pic );
193 free( p_vout->chroma.p_sys );
197 static void ChromaConversion( vout_thread_t *, picture_t *, picture_t * );
199 /*****************************************************************************
200 * chroma_sys_t: chroma method descriptor
201 *****************************************************************************
202 * This structure is part of the chroma transformation descriptor, it
203 * describes the chroma plugin specific properties.
204 *****************************************************************************/
207 filter_t *p_swscaler;
210 /*****************************************************************************
211 * Video Filter2 functions
212 *****************************************************************************/
213 struct filter_owner_sys_t
215 vout_thread_t *p_vout;
218 static picture_t *video_new_buffer_filter( filter_t *p_filter )
220 picture_t *p_picture;
221 vout_thread_t *p_vout = p_filter->p_owner->p_vout;
223 p_picture = vout_CreatePicture( p_vout, 0, 0, 0 );
228 static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
230 vout_DestroyPicture( p_filter->p_owner->p_vout, p_pic );
233 /*****************************************************************************
234 * OpenChroma: allocate a chroma function
235 *****************************************************************************
236 * This function allocates and initializes a chroma function
237 *****************************************************************************/
238 int E_(OpenChroma)( vlc_object_t *p_this )
240 vout_thread_t *p_vout = (vout_thread_t *)p_this;
241 chroma_sys_t *p_sys = p_vout->chroma.p_sys;
243 p_vout->chroma.p_sys = p_sys = malloc( sizeof( chroma_sys_t ) );
244 if( p_vout->chroma.p_sys == NULL )
248 p_vout->chroma.pf_convert = ChromaConversion;
250 p_sys->p_swscaler = vlc_object_create( p_vout, VLC_OBJECT_FILTER );
251 vlc_object_attach( p_sys->p_swscaler, p_vout );
253 p_sys->p_swscaler->pf_vout_buffer_new = video_new_buffer_filter;
254 p_sys->p_swscaler->pf_vout_buffer_del = video_del_buffer_filter;
256 p_sys->p_swscaler->fmt_out.video.i_x_offset =
257 p_sys->p_swscaler->fmt_out.video.i_y_offset = 0;
258 p_sys->p_swscaler->fmt_in.video = p_vout->fmt_in;
259 p_sys->p_swscaler->fmt_out.video = p_vout->fmt_out;
260 p_sys->p_swscaler->fmt_out.video.i_aspect = p_vout->render.i_aspect;
261 p_sys->p_swscaler->fmt_in.video.i_chroma = p_vout->render.i_chroma;
262 p_sys->p_swscaler->fmt_out.video.i_chroma = p_vout->output.i_chroma;
264 p_sys->p_swscaler->p_module = module_Need( p_sys->p_swscaler,
265 "video filter2", 0, 0 );
267 if( p_sys->p_swscaler->p_module )
269 p_sys->p_swscaler->p_owner =
270 malloc( sizeof( filter_owner_sys_t ) );
271 if( p_sys->p_swscaler->p_owner )
272 p_sys->p_swscaler->p_owner->p_vout = p_vout;
275 if( !p_sys->p_swscaler->p_module || !p_sys->p_swscaler->p_owner )
277 vlc_object_detach( p_sys->p_swscaler );
278 vlc_object_destroy( p_sys->p_swscaler );
279 free( p_vout->chroma.p_sys );
286 /*****************************************************************************
287 * ChromaConversion: actual chroma conversion function
288 *****************************************************************************/
289 static void ChromaConversion( vout_thread_t *p_vout,
290 picture_t *p_src, picture_t *p_dest )
292 chroma_sys_t *p_sys = (chroma_sys_t *) p_vout->chroma.p_sys;
294 if( p_sys && p_src && p_dest &&
295 p_sys->p_swscaler && p_sys->p_swscaler->p_module )
299 p_pic = (picture_t *) malloc( sizeof( picture_t ) );
302 memset( p_pic, 0, sizeof( picture_t ) );
303 vout_AllocatePicture( VLC_OBJECT(p_vout),
304 p_pic, p_vout->fmt_in.i_chroma,
305 p_vout->fmt_in.i_width,
306 p_vout->fmt_in.i_height,
307 p_vout->fmt_in.i_aspect );
308 if( !p_pic->i_planes )
310 free( p_pic->p_data_orig );
315 vout_CopyPicture( p_vout, p_pic, p_src );
316 p_sys->p_swscaler->fmt_in.video = p_vout->fmt_in;
317 p_sys->p_swscaler->fmt_out.video = p_vout->fmt_out;
318 p_pic = p_sys->p_swscaler->pf_video_filter( p_sys->p_swscaler, p_pic );
321 vout_CopyPicture( p_vout, p_dest, p_pic );
322 free( p_pic->p_data_orig );
323 if( p_pic->pf_release ) p_pic->pf_release( p_pic );
328 p_sys->p_swscaler->fmt_in.video = p_vout->fmt_in;
329 p_sys->p_swscaler->fmt_out.video = p_vout->fmt_out;
331 msg_Dbg( p_vout, "chroma %4.4s (%d) to %4.4s (%d)",
332 (char *)&p_vout->fmt_in.i_chroma, p_src->i_planes,
333 (char *)&p_vout->fmt_out.i_chroma, p_dest->i_planes );
334 p_pic = p_sys->p_swscaler->pf_video_filter( (filter_t*)p_sys->p_swscaler, p_src );
337 vout_CopyPicture( p_vout, p_dest, p_pic );
338 free( p_pic->p_data_orig );
339 if( p_pic->pf_release ) p_pic->pf_release( p_pic );
345 /*****************************************************************************
346 * CloseChroma: free the chroma function
347 *****************************************************************************
348 * This function frees the previously allocated chroma function
349 *****************************************************************************/
350 void E_(CloseChroma)( vlc_object_t *p_this )
352 vout_thread_t *p_vout = (vout_thread_t *)p_this;
353 chroma_sys_t *p_sys = (chroma_sys_t *)p_vout->chroma.p_sys;
354 if( p_sys->p_swscaler && p_sys->p_swscaler->p_module )
356 free( p_sys->p_swscaler->p_owner );
357 module_Unneed( p_sys->p_swscaler, p_sys->p_swscaler->p_module );
358 vlc_object_detach( p_sys->p_swscaler );
359 vlc_object_destroy( p_sys->p_swscaler );
360 p_sys->p_swscaler= NULL;
362 free( p_vout->chroma.p_sys );
365 #endif /* !defined(HAVE_FFMPEG_SWSCALE_H) && !defined(HAVE_LIBSWSCALE_TREE) */