]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/chroma.c
Remove E_()
[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_LIBSWSCALE_SWSCALE_H)  || defined(HAVE_FFMPEG_SWSCALE_H) || defined(HAVE_LIBSWSCALE_TREE)
36 #include <vlc_filter.h>
37 #endif
38
39 /* ffmpeg header */
40 #ifdef HAVE_LIBAVCODEC_AVCODEC_H
41 #   include <libavcodec/avcodec.h>
42 #elif defined(HAVE_FFMPEG_AVCODEC_H)
43 #   include <ffmpeg/avcodec.h>
44 #else
45 #   include <avcodec.h>
46 #endif
47
48 #include "ffmpeg.h"
49
50 #if !defined(HAVE_LIBSWSCALE_SWSCALE_H)  && !defined(HAVE_FFMPEG_SWSCALE_H) && !defined(HAVE_LIBSWSCALE_TREE)
51 void InitLibavcodec ( vlc_object_t *p_object );
52 static void ChromaConversion( vout_thread_t *, picture_t *, picture_t * );
53
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  *****************************************************************************/
60 struct chroma_sys_t
61 {
62     int i_src_vlc_chroma;
63     int i_src_ffmpeg_chroma;
64     int i_dst_vlc_chroma;
65     int i_dst_ffmpeg_chroma;
66     AVPicture tmp_pic;
67     ImgReSampleContext *p_rsc;
68 };
69
70 /*****************************************************************************
71  * OpenChroma: allocate a chroma function
72  *****************************************************************************
73  * This function allocates and initializes a chroma function
74  *****************************************************************************/
75 int OpenChroma( vlc_object_t *p_this )
76 {
77     vout_thread_t *p_vout = (vout_thread_t *)p_this;
78     int i_ffmpeg_chroma[2], i_vlc_chroma[2], i;
79
80     /*
81      * Check the source chroma first, then the destination chroma
82      */
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++ )
86     {
87         i_ffmpeg_chroma[i] = GetFfmpegChroma( i_vlc_chroma[i] );
88         if( i_ffmpeg_chroma[i] < 0 ) return VLC_EGENERIC;
89     }
90
91     p_vout->chroma.pf_convert = ChromaConversion;
92
93     p_vout->chroma.p_sys = malloc( sizeof( chroma_sys_t ) );
94     if( p_vout->chroma.p_sys == NULL )
95     {
96         return VLC_ENOMEM;
97     }
98
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];
103
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') ))
108     {
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 );
116     }
117     else
118     {
119         msg_Dbg( p_vout, "no resampling" );
120         p_vout->chroma.p_sys->p_rsc = NULL;
121     }
122
123     /* libavcodec needs to be initialized for some chroma conversions */
124     InitLibavcodec(p_this);
125
126     return VLC_SUCCESS;
127 }
128
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 )
134 {
135     AVPicture src_pic;
136     AVPicture dest_pic;
137     int i;
138
139     /* Prepare the AVPictures for converion */
140     for( i = 0; i < p_src->i_planes; i++ )
141     {
142         src_pic.data[i] = p_src->p[i].p_pixels;
143         src_pic.linesize[i] = p_src->p[i].i_pitch;
144     }
145     for( i = 0; i < p_dest->i_planes; i++ )
146     {
147         dest_pic.data[i] = p_dest->p[i].p_pixels;
148         dest_pic.linesize[i] = p_dest->p[i].i_pitch;
149     }
150
151     /* Special cases */
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') )
154     {
155         /* Invert U and V */
156         src_pic.data[1] = p_src->p[2].p_pixels;
157         src_pic.data[2] = p_src->p[1].p_pixels;
158     }
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') )
161     {
162         /* Invert U and V */
163         dest_pic.data[1] = p_dest->p[2].p_pixels;
164         dest_pic.data[2] = p_dest->p[1].p_pixels;
165     }
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;
169
170     if( p_vout->chroma.p_sys->p_rsc )
171     {
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 );
178     }
179     else
180     {
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 );
184     }
185 }
186
187 /*****************************************************************************
188  * CloseChroma: free the chroma function
189  *****************************************************************************
190  * This function frees the previously allocated chroma function
191  *****************************************************************************/
192 void CloseChroma( vlc_object_t *p_this )
193 {
194     vout_thread_t *p_vout = (vout_thread_t *)p_this;
195     if( p_vout->chroma.p_sys->p_rsc )
196     {
197         img_resample_close( p_vout->chroma.p_sys->p_rsc );
198         avpicture_free( &p_vout->chroma.p_sys->tmp_pic );
199     }
200     free( p_vout->chroma.p_sys );
201 }
202 #else
203
204 static void ChromaConversion( vout_thread_t *, picture_t *, picture_t * );
205
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  *****************************************************************************/
212 struct chroma_sys_t
213 {
214     filter_t *p_swscaler;
215 };
216
217 /*****************************************************************************
218  * Video Filter2 functions
219  *****************************************************************************/
220 struct filter_owner_sys_t
221 {
222     vout_thread_t *p_vout;
223 };
224
225 static void PictureRelease( picture_t *p_pic )
226 {
227     free( p_pic->p_data_orig );
228 }
229
230 static picture_t *video_new_buffer_filter( filter_t *p_filter )
231 {
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 )
239         != VLC_SUCCESS )
240     {
241         free( p_picture );
242         return NULL;
243     }
244
245     p_picture->pf_release = PictureRelease;
246
247     return p_picture;
248 }
249
250 static void video_del_buffer_filter( filter_t *p_filter, picture_t *p_pic )
251 {
252     VLC_UNUSED(p_filter);
253     if( p_pic )
254     {
255         free( p_pic->p_data_orig );
256         free( p_pic );
257     }
258 }
259
260 /*****************************************************************************
261  * OpenChroma: allocate a chroma function
262  *****************************************************************************
263  * This function allocates and initializes a chroma function
264  *****************************************************************************/
265 int OpenChroma( vlc_object_t *p_this )
266 {
267     vout_thread_t *p_vout = (vout_thread_t *)p_this;
268     chroma_sys_t  *p_sys = p_vout->chroma.p_sys;
269
270     p_vout->chroma.p_sys = p_sys = malloc( sizeof( chroma_sys_t ) );
271     if( p_vout->chroma.p_sys == NULL )
272     {
273         return VLC_ENOMEM;
274     }
275     p_vout->chroma.pf_convert = ChromaConversion;
276
277     p_sys->p_swscaler = vlc_object_create( p_vout, VLC_OBJECT_FILTER );
278     vlc_object_attach( p_sys->p_swscaler, p_vout );
279
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;
282
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;
290
291     p_sys->p_swscaler->p_module = module_Need( p_sys->p_swscaler,
292                            "video filter2", 0, 0 );
293
294     if( p_sys->p_swscaler->p_module )
295     {
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;
300     }
301
302     if( !p_sys->p_swscaler->p_module || !p_sys->p_swscaler->p_owner )
303     {
304         vlc_object_detach( p_sys->p_swscaler );
305         vlc_object_release( p_sys->p_swscaler );
306         free( p_vout->chroma.p_sys );
307         return VLC_EGENERIC;
308     }
309
310     return VLC_SUCCESS;
311 }
312
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 )
318 {
319     chroma_sys_t *p_sys = (chroma_sys_t *) p_vout->chroma.p_sys;
320
321     if( p_sys && p_src && p_dest && 
322         p_sys->p_swscaler && p_sys->p_swscaler->p_module )
323     {
324         picture_t *p_pic;
325
326         p_sys->p_swscaler->fmt_in.video = p_vout->fmt_in;
327         p_sys->p_swscaler->fmt_out.video = p_vout->fmt_out;
328
329 #if 0
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  );
333 #endif
334         p_pic = p_sys->p_swscaler->pf_vout_buffer_new( p_sys->p_swscaler );
335         if( p_pic )
336         {
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 );
342         }
343     }
344 }
345
346 /*****************************************************************************
347  * CloseChroma: free the chroma function
348  *****************************************************************************
349  * This function frees the previously allocated chroma function
350  *****************************************************************************/
351 void CloseChroma( vlc_object_t *p_this )
352 {
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 )
356     {
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;
362     }
363     free( p_vout->chroma.p_sys );
364 }
365
366 #endif /* !defined(HAVE_LIBSWSCALE_SWSCALE_H)  && !defined(HAVE_FFMPEG_SWSCALE_H) && !defined(HAVE_LIBSWSCALE_TREE) */