]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/chroma.c
30bbae02b6a15bc8e41d3f3a53ab3f986d8fc505
[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_FFMPEG_SWSCALE_H) || defined(HAVE_LIBSWSCALE_TREE)
36 #include <vlc_filter.h>
37 #endif
38
39 /* ffmpeg header */
40 #ifdef HAVE_FFMPEG_AVCODEC_H
41 #   include <ffmpeg/avcodec.h>
42 #else
43 #   include <avcodec.h>
44 #endif
45
46 #include "ffmpeg.h"
47
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 * );
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     (void)p_filter;
251     if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
252     if( p_pic ) free( p_pic );
253 }
254
255 /*****************************************************************************
256  * OpenChroma: allocate a chroma function
257  *****************************************************************************
258  * This function allocates and initializes a chroma function
259  *****************************************************************************/
260 int E_(OpenChroma)( vlc_object_t *p_this )
261 {
262     vout_thread_t *p_vout = (vout_thread_t *)p_this;
263     chroma_sys_t  *p_sys = p_vout->chroma.p_sys;
264
265     p_vout->chroma.p_sys = p_sys = malloc( sizeof( chroma_sys_t ) );
266     if( p_vout->chroma.p_sys == NULL )
267     {
268         return VLC_ENOMEM;
269     }
270     p_vout->chroma.pf_convert = ChromaConversion;
271
272     p_sys->p_swscaler = vlc_object_create( p_vout, VLC_OBJECT_FILTER );
273     vlc_object_attach( p_sys->p_swscaler, p_vout );
274
275     p_sys->p_swscaler->pf_vout_buffer_new = video_new_buffer_filter;
276     p_sys->p_swscaler->pf_vout_buffer_del = video_del_buffer_filter;
277
278     p_sys->p_swscaler->fmt_out.video.i_x_offset =
279         p_sys->p_swscaler->fmt_out.video.i_y_offset = 0;
280     p_sys->p_swscaler->fmt_in.video = p_vout->fmt_in;
281     p_sys->p_swscaler->fmt_out.video = p_vout->fmt_out;
282     p_sys->p_swscaler->fmt_out.video.i_aspect = p_vout->render.i_aspect;
283     p_sys->p_swscaler->fmt_in.video.i_chroma = p_vout->render.i_chroma;
284     p_sys->p_swscaler->fmt_out.video.i_chroma = p_vout->output.i_chroma;
285
286     p_sys->p_swscaler->p_module = module_Need( p_sys->p_swscaler,
287                            "video filter2", 0, 0 );
288
289     if( p_sys->p_swscaler->p_module )
290     {
291         p_sys->p_swscaler->p_owner =
292             malloc( sizeof( filter_owner_sys_t ) );
293         if( p_sys->p_swscaler->p_owner )
294             p_sys->p_swscaler->p_owner->p_vout = p_vout;
295     }
296
297     if( !p_sys->p_swscaler->p_module || !p_sys->p_swscaler->p_owner )
298     {
299         vlc_object_detach( p_sys->p_swscaler );
300         vlc_object_destroy( p_sys->p_swscaler );
301         free( p_vout->chroma.p_sys );
302         return VLC_EGENERIC;
303     }
304
305     return VLC_SUCCESS;
306 }
307
308 /*****************************************************************************
309  * ChromaConversion: actual chroma conversion function
310  *****************************************************************************/
311 static void ChromaConversion( vout_thread_t *p_vout,
312                               picture_t *p_src, picture_t *p_dest )
313 {
314     chroma_sys_t *p_sys = (chroma_sys_t *) p_vout->chroma.p_sys;
315
316     if( p_sys && p_src && p_dest && 
317         p_sys->p_swscaler && p_sys->p_swscaler->p_module )
318     {
319         picture_t *p_pic;
320
321         p_sys->p_swscaler->fmt_in.video = p_vout->fmt_in;
322         p_sys->p_swscaler->fmt_out.video = p_vout->fmt_out;
323
324 #if 0
325         msg_Dbg( p_vout, "chroma %4.4s (%d) to %4.4s (%d)",
326                  (char *)&p_vout->fmt_in.i_chroma, p_src->i_planes,
327                  (char *)&p_vout->fmt_out.i_chroma, p_dest->i_planes  );
328 #endif
329         p_pic = p_sys->p_swscaler->pf_vout_buffer_new( p_sys->p_swscaler );
330         if( p_pic )
331         {
332             picture_t *p_dst_pic;
333             vout_CopyPicture( p_vout, p_pic, p_src );
334             p_dst_pic = p_sys->p_swscaler->pf_video_filter( p_sys->p_swscaler, p_pic );
335             vout_CopyPicture( p_vout, p_dest, p_dst_pic );
336             p_dst_pic->pf_release( p_dst_pic );
337         }
338     }
339 }
340
341 /*****************************************************************************
342  * CloseChroma: free the chroma function
343  *****************************************************************************
344  * This function frees the previously allocated chroma function
345  *****************************************************************************/
346 void E_(CloseChroma)( vlc_object_t *p_this )
347 {
348     vout_thread_t *p_vout = (vout_thread_t *)p_this;
349     chroma_sys_t  *p_sys  = (chroma_sys_t *)p_vout->chroma.p_sys;
350     if( p_sys->p_swscaler && p_sys->p_swscaler->p_module )
351     {
352         free( p_sys->p_swscaler->p_owner );
353         module_Unneed( p_sys->p_swscaler, p_sys->p_swscaler->p_module );
354         vlc_object_detach( p_sys->p_swscaler );
355         vlc_object_destroy( p_sys->p_swscaler );
356         p_sys->p_swscaler= NULL;
357     }
358     free( p_vout->chroma.p_sys );
359 }
360
361 #endif /* !defined(HAVE_FFMPEG_SWSCALE_H) && !defined(HAVE_LIBSWSCALE_TREE) */