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 *****************************************************************************/
31 #ifdef HAVE_FFMPEG_AVCODEC_H
32 # include <ffmpeg/avcodec.h>
39 void E_(InitLibavcodec) ( vlc_object_t *p_object );
40 static void ChromaConversion( vout_thread_t *, picture_t *, picture_t * );
42 /*****************************************************************************
43 * chroma_sys_t: chroma method descriptor
44 *****************************************************************************
45 * This structure is part of the chroma transformation descriptor, it
46 * describes the chroma plugin specific properties.
47 *****************************************************************************/
51 int i_src_ffmpeg_chroma;
53 int i_dst_ffmpeg_chroma;
55 ImgReSampleContext *p_rsc;
58 /*****************************************************************************
59 * OpenChroma: allocate a chroma function
60 *****************************************************************************
61 * This function allocates and initializes a chroma function
62 *****************************************************************************/
63 int E_(OpenChroma)( vlc_object_t *p_this )
65 vout_thread_t *p_vout = (vout_thread_t *)p_this;
66 int i_ffmpeg_chroma[2], i_vlc_chroma[2], i;
69 * Check the source chroma first, then the destination chroma
71 i_vlc_chroma[0] = p_vout->render.i_chroma;
72 i_vlc_chroma[1] = p_vout->output.i_chroma;
73 for( i = 0; i < 2; i++ )
75 i_ffmpeg_chroma[i] = E_(GetFfmpegChroma)( i_vlc_chroma[i] );
76 if( i_ffmpeg_chroma[i] < 0 ) return VLC_EGENERIC;
79 p_vout->chroma.pf_convert = ChromaConversion;
81 p_vout->chroma.p_sys = malloc( sizeof( chroma_sys_t ) );
82 if( p_vout->chroma.p_sys == NULL )
87 p_vout->chroma.p_sys->i_src_vlc_chroma = p_vout->render.i_chroma;
88 p_vout->chroma.p_sys->i_dst_vlc_chroma = p_vout->output.i_chroma;
89 p_vout->chroma.p_sys->i_src_ffmpeg_chroma = i_ffmpeg_chroma[0];
90 p_vout->chroma.p_sys->i_dst_ffmpeg_chroma = i_ffmpeg_chroma[1];
92 if( ( p_vout->render.i_height != p_vout->output.i_height ||
93 p_vout->render.i_width != p_vout->output.i_width ) &&
94 ( p_vout->chroma.p_sys->i_dst_vlc_chroma == VLC_FOURCC('I','4','2','0') ||
95 p_vout->chroma.p_sys->i_dst_vlc_chroma == VLC_FOURCC('Y','V','1','2') ))
98 msg_Dbg( p_vout, "preparing to resample picture" );
99 p_vout->chroma.p_sys->p_rsc =
100 img_resample_init( p_vout->output.i_width, p_vout->output.i_height,
101 p_vout->render.i_width, p_vout->render.i_height );
102 avpicture_alloc( &p_vout->chroma.p_sys->tmp_pic,
103 p_vout->chroma.p_sys->i_dst_ffmpeg_chroma,
104 p_vout->render.i_width, p_vout->render.i_height );
108 msg_Dbg( p_vout, "no resampling" );
109 p_vout->chroma.p_sys->p_rsc = NULL;
112 /* libavcodec needs to be initialized for some chroma conversions */
113 E_(InitLibavcodec)(p_this);
118 /*****************************************************************************
119 * ChromaConversion: actual chroma conversion function
120 *****************************************************************************/
121 static void ChromaConversion( vout_thread_t *p_vout,
122 picture_t *p_src, picture_t *p_dest )
128 /* Prepare the AVPictures for converion */
129 for( i = 0; i < p_src->i_planes; i++ )
131 src_pic.data[i] = p_src->p[i].p_pixels;
132 src_pic.linesize[i] = p_src->p[i].i_pitch;
134 for( i = 0; i < p_dest->i_planes; i++ )
136 dest_pic.data[i] = p_dest->p[i].p_pixels;
137 dest_pic.linesize[i] = p_dest->p[i].i_pitch;
141 if( p_vout->chroma.p_sys->i_src_vlc_chroma == VLC_FOURCC('Y','V','1','2') ||
142 p_vout->chroma.p_sys->i_src_vlc_chroma == VLC_FOURCC('Y','V','U','9') )
145 src_pic.data[1] = p_src->p[2].p_pixels;
146 src_pic.data[2] = p_src->p[1].p_pixels;
148 if( p_vout->chroma.p_sys->i_dst_vlc_chroma == VLC_FOURCC('Y','V','1','2') ||
149 p_vout->chroma.p_sys->i_dst_vlc_chroma == VLC_FOURCC('Y','V','U','9') )
152 dest_pic.data[1] = p_dest->p[2].p_pixels;
153 dest_pic.data[2] = p_dest->p[1].p_pixels;
155 if( p_vout->chroma.p_sys->i_src_ffmpeg_chroma == PIX_FMT_RGB24 )
156 if( p_vout->render.i_bmask == 0x00ff0000 )
157 p_vout->chroma.p_sys->i_src_ffmpeg_chroma = PIX_FMT_BGR24;
159 if( p_vout->chroma.p_sys->p_rsc )
161 img_convert( &p_vout->chroma.p_sys->tmp_pic,
162 p_vout->chroma.p_sys->i_dst_ffmpeg_chroma,
163 &src_pic, p_vout->chroma.p_sys->i_src_ffmpeg_chroma,
164 p_vout->render.i_width, p_vout->render.i_height );
165 img_resample( p_vout->chroma.p_sys->p_rsc, &dest_pic,
166 &p_vout->chroma.p_sys->tmp_pic );
170 img_convert( &dest_pic, p_vout->chroma.p_sys->i_dst_ffmpeg_chroma,
171 &src_pic, p_vout->chroma.p_sys->i_src_ffmpeg_chroma,
172 p_vout->render.i_width, p_vout->render.i_height );
176 /*****************************************************************************
177 * CloseChroma: free the chroma function
178 *****************************************************************************
179 * This function frees the previously allocated chroma function
180 *****************************************************************************/
181 void E_(CloseChroma)( vlc_object_t *p_this )
183 vout_thread_t *p_vout = (vout_thread_t *)p_this;
184 if( p_vout->chroma.p_sys->p_rsc )
186 img_resample_close( p_vout->chroma.p_sys->p_rsc );
187 avpicture_free( &p_vout->chroma.p_sys->tmp_pic );
189 free( p_vout->chroma.p_sys );