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_FFMPEG_SWSCALE_H) || defined(HAVE_LIBSWSCALE_TREE)
36 #include <vlc_filter.h>
40 #ifdef HAVE_FFMPEG_AVCODEC_H
41 # include <ffmpeg/avcodec.h>
48 #if !defined(HAVE_FFMPEG_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 * );
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 *****************************************************************************/
61 int i_src_ffmpeg_chroma;
63 int i_dst_ffmpeg_chroma;
65 ImgReSampleContext *p_rsc;
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 )
75 vout_thread_t *p_vout = (vout_thread_t *)p_this;
76 int i_ffmpeg_chroma[2], i_vlc_chroma[2], i;
79 * Check the source chroma first, then the destination chroma
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++ )
85 i_ffmpeg_chroma[i] = E_(GetFfmpegChroma)( i_vlc_chroma[i] );
86 if( i_ffmpeg_chroma[i] < 0 ) return VLC_EGENERIC;
89 p_vout->chroma.pf_convert = ChromaConversion;
91 p_vout->chroma.p_sys = malloc( sizeof( chroma_sys_t ) );
92 if( p_vout->chroma.p_sys == NULL )
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];
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') ))
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 );
117 msg_Dbg( p_vout, "no resampling" );
118 p_vout->chroma.p_sys->p_rsc = NULL;
121 /* libavcodec needs to be initialized for some chroma conversions */
122 E_(InitLibavcodec)(p_this);
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 )
137 /* Prepare the AVPictures for converion */
138 for( i = 0; i < p_src->i_planes; i++ )
140 src_pic.data[i] = p_src->p[i].p_pixels;
141 src_pic.linesize[i] = p_src->p[i].i_pitch;
143 for( i = 0; i < p_dest->i_planes; i++ )
145 dest_pic.data[i] = p_dest->p[i].p_pixels;
146 dest_pic.linesize[i] = p_dest->p[i].i_pitch;
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') )
154 src_pic.data[1] = p_src->p[2].p_pixels;
155 src_pic.data[2] = p_src->p[1].p_pixels;
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') )
161 dest_pic.data[1] = p_dest->p[2].p_pixels;
162 dest_pic.data[2] = p_dest->p[1].p_pixels;
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;
168 if( p_vout->chroma.p_sys->p_rsc )
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 );
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 );
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 )
192 vout_thread_t *p_vout = (vout_thread_t *)p_this;
193 if( p_vout->chroma.p_sys->p_rsc )
195 img_resample_close( p_vout->chroma.p_sys->p_rsc );
196 avpicture_free( &p_vout->chroma.p_sys->tmp_pic );
198 free( p_vout->chroma.p_sys );
202 static void ChromaConversion( vout_thread_t *, picture_t *, picture_t * );
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 *****************************************************************************/
212 filter_t *p_swscaler;
215 /*****************************************************************************
216 * Video Filter2 functions
217 *****************************************************************************/
218 struct filter_owner_sys_t
220 vout_thread_t *p_vout;
223 static void PictureRelease( picture_t *p_pic )
225 if( p_pic->p_data_orig ) free( p_pic->p_data_orig );
228 static picture_t *video_new_buffer_filter( filter_t *p_filter )
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 )
243 p_picture->pf_release = PictureRelease;
248 static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
250 VLC_UNUSED(p_filter);
253 free( p_pic->p_data_orig );
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 )
265 vout_thread_t *p_vout = (vout_thread_t *)p_this;
266 chroma_sys_t *p_sys = p_vout->chroma.p_sys;
268 p_vout->chroma.p_sys = p_sys = malloc( sizeof( chroma_sys_t ) );
269 if( p_vout->chroma.p_sys == NULL )
273 p_vout->chroma.pf_convert = ChromaConversion;
275 p_sys->p_swscaler = vlc_object_create( p_vout, VLC_OBJECT_FILTER );
276 vlc_object_attach( p_sys->p_swscaler, p_vout );
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;
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;
289 p_sys->p_swscaler->p_module = module_Need( p_sys->p_swscaler,
290 "video filter2", 0, 0 );
292 if( p_sys->p_swscaler->p_module )
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;
300 if( !p_sys->p_swscaler->p_module || !p_sys->p_swscaler->p_owner )
302 vlc_object_detach( p_sys->p_swscaler );
303 vlc_object_destroy( p_sys->p_swscaler );
304 free( p_vout->chroma.p_sys );
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 )
317 chroma_sys_t *p_sys = (chroma_sys_t *) p_vout->chroma.p_sys;
319 if( p_sys && p_src && p_dest &&
320 p_sys->p_swscaler && p_sys->p_swscaler->p_module )
324 p_sys->p_swscaler->fmt_in.video = p_vout->fmt_in;
325 p_sys->p_swscaler->fmt_out.video = p_vout->fmt_out;
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 );
332 p_pic = p_sys->p_swscaler->pf_vout_buffer_new( p_sys->p_swscaler );
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 );
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 )
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 )
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_destroy( p_sys->p_swscaler );
359 p_sys->p_swscaler= NULL;
361 free( p_vout->chroma.p_sys );
364 #endif /* !defined(HAVE_FFMPEG_SWSCALE_H) && !defined(HAVE_LIBSWSCALE_TREE) */