]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/postprocess.c
string review
[vlc] / modules / codec / ffmpeg / postprocess.c
1 /*****************************************************************************
2  * postprocess.c: video postprocessing using the ffmpeg library
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: postprocess.c,v 1.7 2004/01/25 18:20:12 bigben Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@netcourrier.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 #include <vlc/vlc.h>
26 #include <vlc/vout.h>
27 #include <vlc/decoder.h>
28
29 /* ffmpeg header */
30 #ifdef HAVE_FFMPEG_AVCODEC_H
31 #   include <ffmpeg/avcodec.h>
32 #else
33 #   include <avcodec.h>
34 #endif
35
36 #include "ffmpeg.h"
37
38 #ifdef LIBAVCODEC_PP
39
40 #ifdef HAVE_POSTPROC_POSTPROCESS_H
41 #   include <postproc/postprocess.h>
42 #else
43 #   include <libpostproc/postprocess.h>
44 #endif
45
46 /*****************************************************************************
47  * video_postproc_sys_t : ffmpeg video postprocessing descriptor
48  *****************************************************************************/
49 typedef struct video_postproc_sys_t
50 {
51     pp_context_t *pp_context;
52     pp_mode_t    *pp_mode;
53
54     vlc_bool_t   *pb_pp;
55
56     int i_width;
57     int i_height;
58
59 } video_postproc_sys_t;
60
61 static int PPQCallback( vlc_object_t *p_this, char const *psz_cmd,
62                         vlc_value_t oldval, vlc_value_t newval, void *p_data );
63
64 /*****************************************************************************
65  * OpenPostproc: probe and open the postproc
66  *****************************************************************************/
67 void *E_(OpenPostproc)( decoder_t *p_dec, vlc_bool_t *pb_pp )
68 {
69     video_postproc_sys_t *p_sys;
70     vlc_value_t val, val_orig, text;
71
72     p_sys = malloc( sizeof(video_postproc_sys_t) );
73     p_sys->pp_context = NULL;
74     p_sys->pp_mode = NULL;
75
76     *pb_pp = VLC_FALSE;
77     p_sys->pb_pp = pb_pp;
78
79     /* Create object variable if not already done */
80     if( var_Type( p_dec, "ffmpeg-pp-q" ) == 0 )
81     {
82         var_Create( p_dec, "ffmpeg-pp-q",
83                     VLC_VAR_INTEGER | VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
84         text.psz_string = _("Post processing");
85         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_SETTEXT, &text, NULL );
86
87         var_Get( p_dec, "ffmpeg-pp-q", &val_orig );
88         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_DELCHOICE, &val_orig, NULL );
89
90         val.i_int = 0; text.psz_string = _("Disable");
91         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_ADDCHOICE, &val, &text );
92         val.i_int = 1; text.psz_string = _("1 (Lowest)");
93         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_ADDCHOICE, &val, &text );
94         val.i_int = 2;
95         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_ADDCHOICE, &val, NULL );
96         val.i_int = 3;
97         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_ADDCHOICE, &val, NULL );
98         val.i_int = 4;
99         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_ADDCHOICE, &val, NULL );
100         val.i_int = 5;
101         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_ADDCHOICE, &val, NULL );
102         val.i_int = 6; text.psz_string = _("6 (Highest)");
103         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_ADDCHOICE, &val, &text );
104         var_AddCallback( p_dec, "ffmpeg-pp-q", PPQCallback, p_sys );
105     }
106
107     /* ***** Load post processing if enabled ***** */
108     var_Get( p_dec, "ffmpeg-pp-q", &val );
109     var_Set( p_dec, "ffmpeg-pp-q", val_orig );
110
111     return p_sys;
112 }
113
114 /*****************************************************************************
115  * InitPostproc: 
116  *****************************************************************************/
117 int E_(InitPostproc)( decoder_t *p_dec, void *p_data,
118                       int i_width, int i_height, int pix_fmt )
119 {
120     video_postproc_sys_t *p_sys = (video_postproc_sys_t *)p_data;
121     int32_t i_cpu = p_dec->p_libvlc->i_cpu;
122     int i_flags = 0;
123
124     if( i_cpu & CPU_CAPABILITY_MMX )
125     {
126         i_flags |= PP_CPU_CAPS_MMX;
127     }
128     if( i_cpu & CPU_CAPABILITY_MMXEXT )
129     {
130         i_flags |= PP_CPU_CAPS_MMX2;
131     }
132     if( i_cpu & CPU_CAPABILITY_3DNOW )
133     {
134         i_flags |= PP_CPU_CAPS_3DNOW;
135     }
136
137     switch( pix_fmt )
138     {
139     case PIX_FMT_YUV444P:
140         i_flags |= PP_FORMAT_444;
141         break;
142     case PIX_FMT_YUV422P:
143         i_flags |= PP_FORMAT_422;
144         break;
145     case PIX_FMT_YUV411P:
146         i_flags |= PP_FORMAT_411;
147         break;
148     default:
149         i_flags |= PP_FORMAT_420;
150         break;
151     }
152
153     p_sys->pp_context = pp_get_context( i_width, i_height, i_flags );
154     p_sys->i_width = i_width;
155     p_sys->i_height = i_height;
156
157     return VLC_SUCCESS;
158 }
159
160 /*****************************************************************************
161  * PostprocPict: 
162  *****************************************************************************/
163 int E_(PostprocPict)( decoder_t *p_dec, void *p_data,
164                       picture_t *p_pic, AVFrame *p_ff_pic )
165 {
166     video_postproc_sys_t *p_sys = (video_postproc_sys_t *)p_data;
167
168     uint8_t *src[3], *dst[3];
169     int i_plane, i_src_stride[3], i_dst_stride[3];
170
171     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
172     {
173         src[i_plane] = p_ff_pic->data[i_plane];
174         dst[i_plane] = p_pic->p[i_plane].p_pixels;
175
176         i_src_stride[i_plane] = p_ff_pic->linesize[i_plane];
177         i_dst_stride[i_plane] = p_pic->p[i_plane].i_pitch;
178     }
179
180     pp_postprocess( src, i_src_stride, dst, i_dst_stride,
181                     p_sys->i_width, p_sys->i_height,
182                     p_ff_pic->qscale_table, p_ff_pic->qstride,
183                     p_sys->pp_mode, p_sys->pp_context,
184                     p_ff_pic->pict_type );
185
186     return VLC_SUCCESS;
187 }
188
189 /*****************************************************************************
190  * ClosePostproc: 
191  *****************************************************************************/
192 void E_(ClosePostproc)( decoder_t *p_dec, void *p_data )
193 {
194     video_postproc_sys_t *p_sys = (video_postproc_sys_t *)p_data;
195
196     if( p_sys && p_sys->pp_mode )
197     {
198         pp_free_mode( p_sys->pp_mode );
199         if( p_sys->pp_context ) pp_free_context( p_sys->pp_context );
200     }
201
202     var_DelCallback( p_dec, "ffmpeg-pp-q", PPQCallback, p_sys );
203
204     if( p_sys ) free( p_sys );
205 }
206
207 /*****************************************************************************
208  * object variables callbacks: a bunch of object variables are used by the
209  * interfaces to interact with the decoder.
210  *****************************************************************************/
211 static int PPQCallback( vlc_object_t *p_this, char const *psz_cmd,
212                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
213 {
214     decoder_t *p_dec = (decoder_t *)p_this;
215     video_postproc_sys_t *p_sys = (video_postproc_sys_t *)p_data;
216
217     if( newval.i_int > 0 )
218     {
219         int  i_quality = newval.i_int;
220         char *psz_name = config_GetPsz( p_dec, "ffmpeg-pp-name" );
221         pp_mode_t *pp_mode;
222
223         if( !psz_name )
224         {
225             psz_name = strdup( "default" );
226         }
227         else if( *psz_name == '\0' )
228         {
229             free( psz_name );
230             psz_name = strdup( "default" );
231         }
232
233         pp_mode = pp_get_mode_by_name_and_quality( psz_name, i_quality );
234
235         if( !pp_mode )
236         {
237             msg_Err( p_dec, "failed getting mode for postproc" );
238             newval.i_int = 0;
239         }
240         else
241         {
242             msg_Dbg( p_dec, "postprocessing enabled" );
243         }
244         free( psz_name );
245
246         p_sys->pp_mode = pp_mode;
247     }
248     else
249     {
250         msg_Dbg( p_dec, "postprocessing disabled" );
251     }
252
253     *p_sys->pb_pp = newval.i_int;
254
255     return VLC_SUCCESS;
256 }
257
258 #endif /* LIBAVCODEC_PP */