]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/chroma.c
* modules/codec/ffmpeg/encoder.c: some option names renaming butchery.
[vlc] / modules / codec / ffmpeg / chroma.c
1 /*****************************************************************************
2  * chroma.c: chroma conversion using ffmpeg library
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: chroma.c,v 1.7 2004/02/27 14:02:05 fenrir Exp $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc/vout.h>
29
30 /* ffmpeg header */
31 #ifdef HAVE_FFMPEG_AVCODEC_H
32 #   include <ffmpeg/avcodec.h>
33 #else
34 #   include <avcodec.h>
35 #endif
36
37 #include "ffmpeg.h"
38
39 void E_(InitLibavcodec) ( vlc_object_t *p_object );
40 static void ChromaConversion( vout_thread_t *, picture_t *, picture_t * );
41
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  *****************************************************************************/
48 struct chroma_sys_t
49 {
50     int i_src_vlc_chroma;
51     int i_src_ffmpeg_chroma;
52     int i_dst_vlc_chroma;
53     int i_dst_ffmpeg_chroma;
54 };
55
56 /*****************************************************************************
57  * OpenChroma: allocate a chroma function
58  *****************************************************************************
59  * This function allocates and initializes a chroma function
60  *****************************************************************************/
61 int E_(OpenChroma)( vlc_object_t *p_this )
62 {
63     vout_thread_t *p_vout = (vout_thread_t *)p_this;
64     int i_ffmpeg_chroma[2], i_vlc_chroma[2], i;
65
66     /*
67      * Check the source chroma first, then the destination chroma
68      */
69     i_vlc_chroma[0] = p_vout->render.i_chroma;
70     i_vlc_chroma[1] = p_vout->output.i_chroma;
71     for( i = 0; i < 2; i++ )
72     {
73         switch( i_vlc_chroma[i] )
74         {
75
76         /* Planar YUV formats */
77         case VLC_FOURCC('I','4','4','4'):
78             i_ffmpeg_chroma[i] = PIX_FMT_YUV444P;
79             break;
80
81         case VLC_FOURCC('I','4','2','2'):
82             i_ffmpeg_chroma[i] = PIX_FMT_YUV422P;
83             break;
84
85         case VLC_FOURCC('Y','V','1','2'):
86         case VLC_FOURCC('I','4','2','0'):
87         case VLC_FOURCC('I','Y','U','V'):
88             i_ffmpeg_chroma[i] = PIX_FMT_YUV420P;
89             break;
90
91         case VLC_FOURCC('I','4','1','1'):
92             i_ffmpeg_chroma[i] = PIX_FMT_YUV411P;
93             break;
94
95         case VLC_FOURCC('I','4','1','0'):
96         case VLC_FOURCC('Y','V','U','9'):
97             i_ffmpeg_chroma[i] = PIX_FMT_YUV410P;
98             break;
99
100         /* Packed YUV formats */
101
102         case VLC_FOURCC('Y','U','Y','2'):
103         case VLC_FOURCC('U','Y','V','Y'):
104             i_ffmpeg_chroma[i] = PIX_FMT_YUV422;
105             break;
106
107         /* Packed RGB formats */
108
109         case VLC_FOURCC('R','V','3','2'):
110             i_ffmpeg_chroma[i] = PIX_FMT_RGBA32;
111             break;
112
113         case VLC_FOURCC('R','V','2','4'):
114             i_ffmpeg_chroma[i] = PIX_FMT_RGB24;
115             //i_ffmpeg_chroma[i] = PIX_FMT_BGR24;
116             break;
117
118         case VLC_FOURCC('R','V','1','6'):
119             i_ffmpeg_chroma[i] = PIX_FMT_RGB565;
120             break;
121
122         case VLC_FOURCC('R','V','1','5'):
123             i_ffmpeg_chroma[i] = PIX_FMT_RGB555;
124             break;
125
126         case VLC_FOURCC('R','G','B','2'):
127             i_ffmpeg_chroma[i] = PIX_FMT_GRAY8;
128             break;
129
130         default:
131             return VLC_EGENERIC;
132             break;
133         }
134     }
135
136     p_vout->chroma.pf_convert = ChromaConversion;
137
138     p_vout->chroma.p_sys = malloc( sizeof( chroma_sys_t ) );
139     if( p_vout->chroma.p_sys == NULL )
140     {
141         return VLC_EGENERIC;
142     }
143
144     p_vout->chroma.p_sys->i_src_vlc_chroma = p_vout->render.i_chroma;
145     p_vout->chroma.p_sys->i_dst_vlc_chroma = p_vout->output.i_chroma;
146     p_vout->chroma.p_sys->i_src_ffmpeg_chroma = i_ffmpeg_chroma[0];
147     p_vout->chroma.p_sys->i_dst_ffmpeg_chroma = i_ffmpeg_chroma[1];
148
149     /* libavcodec needs to be initialized for some chroma conversions */
150     E_(InitLibavcodec)(p_this);
151
152     return VLC_SUCCESS;
153 }
154
155 /*****************************************************************************
156  * ChromaConversion: actual chroma conversion function
157  *****************************************************************************/
158 static void ChromaConversion( vout_thread_t *p_vout,
159                               picture_t *p_src, picture_t *p_dest )
160 {
161     AVPicture src_pic;
162     AVPicture dest_pic;
163     int i;
164
165     /* Prepare the AVPictures for converion */
166     for( i = 0; i < p_src->i_planes; i++ )
167     {
168         src_pic.data[i] = p_src->p[i].p_pixels;
169         src_pic.linesize[i] = p_src->p[i].i_pitch;
170     }
171     for( i = 0; i < p_dest->i_planes; i++ )
172     {
173         dest_pic.data[i] = p_dest->p[i].p_pixels;
174         dest_pic.linesize[i] = p_dest->p[i].i_pitch;
175     }
176
177     /* Special cases */
178     if( p_vout->chroma.p_sys->i_src_vlc_chroma == VLC_FOURCC('Y','V','1','2') ||
179         p_vout->chroma.p_sys->i_src_vlc_chroma == VLC_FOURCC('Y','V','U','9') )
180     {
181         /* Invert U and V */
182         src_pic.data[1] = p_src->p[2].p_pixels;
183         src_pic.data[2] = p_src->p[1].p_pixels;
184     }
185     if( p_vout->chroma.p_sys->i_dst_vlc_chroma == VLC_FOURCC('Y','V','1','2') ||
186         p_vout->chroma.p_sys->i_dst_vlc_chroma == VLC_FOURCC('Y','V','U','9') )
187     {
188         /* Invert U and V */
189         dest_pic.data[1] = p_dest->p[2].p_pixels;
190         dest_pic.data[2] = p_dest->p[1].p_pixels;
191     }
192
193     img_convert( &dest_pic, p_vout->chroma.p_sys->i_dst_ffmpeg_chroma,
194                  &src_pic, p_vout->chroma.p_sys->i_src_ffmpeg_chroma,
195                  p_vout->render.i_width, p_vout->render.i_height );
196 }
197
198 /*****************************************************************************
199  * CloseChroma: free the chroma function
200  *****************************************************************************
201  * This function frees the previously allocated chroma function
202  *****************************************************************************/
203 void E_(CloseChroma)( vlc_object_t *p_this )
204 {
205     vout_thread_t *p_vout = (vout_thread_t *)p_this;
206
207     free( p_vout->chroma.p_sys );
208 }