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 *****************************************************************************/
35 #if defined(HAVE_LIBSWSCALE_SWSCALE_H) || defined(HAVE_FFMPEG_SWSCALE_H) || defined(HAVE_LIBSWSCALE_TREE)
36 #include <vlc_filter.h>
40 #ifdef HAVE_LIBAVCODEC_AVCODEC_H
41 # include <libavcodec/avcodec.h>
42 #elif defined(HAVE_FFMPEG_AVCODEC_H)
43 # include <ffmpeg/avcodec.h>
50 #if !defined(HAVE_LIBSWSCALE_SWSCALE_H) && !defined(HAVE_FFMPEG_SWSCALE_H) && !defined(HAVE_LIBSWSCALE_TREE)
51 void E_(InitLibavcodec) ( vlc_object_t *p_object );
52 static void ChromaConversion( vout_thread_t *, picture_t *, picture_t * );
54 /*****************************************************************************
55 * chroma_sys_t: chroma method descriptor
56 *****************************************************************************
57 * This structure is part of the chroma transformation descriptor, it
58 * describes the chroma plugin specific properties.
59 *****************************************************************************/
63 int i_src_ffmpeg_chroma;
65 int i_dst_ffmpeg_chroma;
67 ImgReSampleContext *p_rsc;
70 /*****************************************************************************
71 * OpenChroma: allocate a chroma function
72 *****************************************************************************
73 * This function allocates and initializes a chroma function
74 *****************************************************************************/
75 int E_(OpenChroma)( vlc_object_t *p_this )
77 vout_thread_t *p_vout = (vout_thread_t *)p_this;
78 int i_ffmpeg_chroma[2], i_vlc_chroma[2], i;
81 * Check the source chroma first, then the destination chroma
83 i_vlc_chroma[0] = p_vout->render.i_chroma;
84 i_vlc_chroma[1] = p_vout->output.i_chroma;
85 for( i = 0; i < 2; i++ )
87 i_ffmpeg_chroma[i] = E_(GetFfmpegChroma)( i_vlc_chroma[i] );
88 if( i_ffmpeg_chroma[i] < 0 ) return VLC_EGENERIC;
91 p_vout->chroma.pf_convert = ChromaConversion;
93 p_vout->chroma.p_sys = malloc( sizeof( chroma_sys_t ) );
94 if( p_vout->chroma.p_sys == NULL )
99 p_vout->chroma.p_sys->i_src_vlc_chroma = p_vout->render.i_chroma;
100 p_vout->chroma.p_sys->i_dst_vlc_chroma = p_vout->output.i_chroma;
101 p_vout->chroma.p_sys->i_src_ffmpeg_chroma = i_ffmpeg_chroma[0];
102 p_vout->chroma.p_sys->i_dst_ffmpeg_chroma = i_ffmpeg_chroma[1];
104 if( ( p_vout->render.i_height != p_vout->output.i_height ||
105 p_vout->render.i_width != p_vout->output.i_width ) &&
106 ( p_vout->chroma.p_sys->i_dst_vlc_chroma == VLC_FOURCC('I','4','2','0') ||
107 p_vout->chroma.p_sys->i_dst_vlc_chroma == VLC_FOURCC('Y','V','1','2') ))
109 msg_Dbg( p_vout, "preparing to resample picture" );
110 p_vout->chroma.p_sys->p_rsc =
111 img_resample_init( p_vout->output.i_width, p_vout->output.i_height,
112 p_vout->render.i_width, p_vout->render.i_height );
113 avpicture_alloc( &p_vout->chroma.p_sys->tmp_pic,
114 p_vout->chroma.p_sys->i_dst_ffmpeg_chroma,
115 p_vout->render.i_width, p_vout->render.i_height );
119 msg_Dbg( p_vout, "no resampling" );
120 p_vout->chroma.p_sys->p_rsc = NULL;
123 /* libavcodec needs to be initialized for some chroma conversions */
124 E_(InitLibavcodec)(p_this);
129 /*****************************************************************************
130 * ChromaConversion: actual chroma conversion function
131 *****************************************************************************/
132 static void ChromaConversion( vout_thread_t *p_vout,
133 picture_t *p_src, picture_t *p_dest )
139 /* Prepare the AVPictures for converion */
140 for( i = 0; i < p_src->i_planes; i++ )
142 src_pic.data[i] = p_src->p[i].p_pixels;
143 src_pic.linesize[i] = p_src->p[i].i_pitch;
145 for( i = 0; i < p_dest->i_planes; i++ )
147 dest_pic.data[i] = p_dest->p[i].p_pixels;
148 dest_pic.linesize[i] = p_dest->p[i].i_pitch;
152 if( p_vout->chroma.p_sys->i_src_vlc_chroma == VLC_FOURCC('Y','V','1','2') ||
153 p_vout->chroma.p_sys->i_src_vlc_chroma == VLC_FOURCC('Y','V','U','9') )
156 src_pic.data[1] = p_src->p[2].p_pixels;
157 src_pic.data[2] = p_src->p[1].p_pixels;
159 if( p_vout->chroma.p_sys->i_dst_vlc_chroma == VLC_FOURCC('Y','V','1','2') ||
160 p_vout->chroma.p_sys->i_dst_vlc_chroma == VLC_FOURCC('Y','V','U','9') )
163 dest_pic.data[1] = p_dest->p[2].p_pixels;
164 dest_pic.data[2] = p_dest->p[1].p_pixels;
166 if( p_vout->chroma.p_sys->i_src_ffmpeg_chroma == PIX_FMT_RGB24 )
167 if( p_vout->render.i_bmask == 0x00ff0000 )
168 p_vout->chroma.p_sys->i_src_ffmpeg_chroma = PIX_FMT_BGR24;
170 if( p_vout->chroma.p_sys->p_rsc )
172 img_convert( &p_vout->chroma.p_sys->tmp_pic,
173 p_vout->chroma.p_sys->i_dst_ffmpeg_chroma,
174 &src_pic, p_vout->chroma.p_sys->i_src_ffmpeg_chroma,
175 p_vout->render.i_width, p_vout->render.i_height );
176 img_resample( p_vout->chroma.p_sys->p_rsc, &dest_pic,
177 &p_vout->chroma.p_sys->tmp_pic );
181 img_convert( &dest_pic, p_vout->chroma.p_sys->i_dst_ffmpeg_chroma,
182 &src_pic, p_vout->chroma.p_sys->i_src_ffmpeg_chroma,
183 p_vout->render.i_width, p_vout->render.i_height );
187 /*****************************************************************************
188 * CloseChroma: free the chroma function
189 *****************************************************************************
190 * This function frees the previously allocated chroma function
191 *****************************************************************************/
192 void E_(CloseChroma)( vlc_object_t *p_this )
194 vout_thread_t *p_vout = (vout_thread_t *)p_this;
195 if( p_vout->chroma.p_sys->p_rsc )
197 img_resample_close( p_vout->chroma.p_sys->p_rsc );
198 avpicture_free( &p_vout->chroma.p_sys->tmp_pic );
200 free( p_vout->chroma.p_sys );
204 static void ChromaConversion( vout_thread_t *, picture_t *, picture_t * );
206 /*****************************************************************************
207 * chroma_sys_t: chroma method descriptor
208 *****************************************************************************
209 * This structure is part of the chroma transformation descriptor, it
210 * describes the chroma plugin specific properties.
211 *****************************************************************************/
214 filter_t *p_swscaler;
217 /*****************************************************************************
218 * Video Filter2 functions
219 *****************************************************************************/
220 struct filter_owner_sys_t
222 vout_thread_t *p_vout;
225 static void PictureRelease( picture_t *p_pic )
227 if( p_pic->p_data_orig ) free( p_pic->p_data_orig );
230 static picture_t *video_new_buffer_filter( filter_t *p_filter )
232 picture_t *p_picture = malloc( sizeof(picture_t) );
233 if( !p_picture ) return NULL;
234 if( vout_AllocatePicture( p_filter, p_picture,
235 p_filter->fmt_out.video.i_chroma,
236 p_filter->fmt_out.video.i_width,
237 p_filter->fmt_out.video.i_height,
238 p_filter->fmt_out.video.i_aspect )
245 p_picture->pf_release = PictureRelease;
250 static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
252 VLC_UNUSED(p_filter);
255 free( p_pic->p_data_orig );
260 /*****************************************************************************
261 * OpenChroma: allocate a chroma function
262 *****************************************************************************
263 * This function allocates and initializes a chroma function
264 *****************************************************************************/
265 int E_(OpenChroma)( vlc_object_t *p_this )
267 vout_thread_t *p_vout = (vout_thread_t *)p_this;
268 chroma_sys_t *p_sys = p_vout->chroma.p_sys;
270 p_vout->chroma.p_sys = p_sys = malloc( sizeof( chroma_sys_t ) );
271 if( p_vout->chroma.p_sys == NULL )
275 p_vout->chroma.pf_convert = ChromaConversion;
277 p_sys->p_swscaler = vlc_object_create( p_vout, VLC_OBJECT_FILTER );
278 vlc_object_attach( p_sys->p_swscaler, p_vout );
280 p_sys->p_swscaler->pf_vout_buffer_new = video_new_buffer_filter;
281 p_sys->p_swscaler->pf_vout_buffer_del = video_del_buffer_filter;
283 p_sys->p_swscaler->fmt_out.video.i_x_offset =
284 p_sys->p_swscaler->fmt_out.video.i_y_offset = 0;
285 p_sys->p_swscaler->fmt_in.video = p_vout->fmt_in;
286 p_sys->p_swscaler->fmt_out.video = p_vout->fmt_out;
287 p_sys->p_swscaler->fmt_out.video.i_aspect = p_vout->render.i_aspect;
288 p_sys->p_swscaler->fmt_in.video.i_chroma = p_vout->render.i_chroma;
289 p_sys->p_swscaler->fmt_out.video.i_chroma = p_vout->output.i_chroma;
291 p_sys->p_swscaler->p_module = module_Need( p_sys->p_swscaler,
292 "video filter2", 0, 0 );
294 if( p_sys->p_swscaler->p_module )
296 p_sys->p_swscaler->p_owner =
297 malloc( sizeof( filter_owner_sys_t ) );
298 if( p_sys->p_swscaler->p_owner )
299 p_sys->p_swscaler->p_owner->p_vout = p_vout;
302 if( !p_sys->p_swscaler->p_module || !p_sys->p_swscaler->p_owner )
304 vlc_object_detach( p_sys->p_swscaler );
305 vlc_object_release( p_sys->p_swscaler );
306 free( p_vout->chroma.p_sys );
313 /*****************************************************************************
314 * ChromaConversion: actual chroma conversion function
315 *****************************************************************************/
316 static void ChromaConversion( vout_thread_t *p_vout,
317 picture_t *p_src, picture_t *p_dest )
319 chroma_sys_t *p_sys = (chroma_sys_t *) p_vout->chroma.p_sys;
321 if( p_sys && p_src && p_dest &&
322 p_sys->p_swscaler && p_sys->p_swscaler->p_module )
326 p_sys->p_swscaler->fmt_in.video = p_vout->fmt_in;
327 p_sys->p_swscaler->fmt_out.video = p_vout->fmt_out;
330 msg_Dbg( p_vout, "chroma %4.4s (%d) to %4.4s (%d)",
331 (char *)&p_vout->fmt_in.i_chroma, p_src->i_planes,
332 (char *)&p_vout->fmt_out.i_chroma, p_dest->i_planes );
334 p_pic = p_sys->p_swscaler->pf_vout_buffer_new( p_sys->p_swscaler );
337 picture_t *p_dst_pic;
338 vout_CopyPicture( p_vout, p_pic, p_src );
339 p_dst_pic = p_sys->p_swscaler->pf_video_filter( p_sys->p_swscaler, p_pic );
340 vout_CopyPicture( p_vout, p_dest, p_dst_pic );
341 p_dst_pic->pf_release( p_dst_pic );
346 /*****************************************************************************
347 * CloseChroma: free the chroma function
348 *****************************************************************************
349 * This function frees the previously allocated chroma function
350 *****************************************************************************/
351 void E_(CloseChroma)( vlc_object_t *p_this )
353 vout_thread_t *p_vout = (vout_thread_t *)p_this;
354 chroma_sys_t *p_sys = (chroma_sys_t *)p_vout->chroma.p_sys;
355 if( p_sys->p_swscaler && p_sys->p_swscaler->p_module )
357 free( p_sys->p_swscaler->p_owner );
358 module_Unneed( p_sys->p_swscaler, p_sys->p_swscaler->p_module );
359 vlc_object_detach( p_sys->p_swscaler );
360 vlc_object_release( p_sys->p_swscaler );
361 p_sys->p_swscaler= NULL;
363 free( p_vout->chroma.p_sys );
366 #endif /* !defined(HAVE_LIBSWSCALE_SWSCALE_H) && !defined(HAVE_FFMPEG_SWSCALE_H) && !defined(HAVE_LIBSWSCALE_TREE) */