]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/postprocess.c
375195f6fbcda2bf995e3ad87ca09319e336ec7a
[vlc] / modules / codec / ffmpeg / postprocess.c
1 /*****************************************************************************
2  * postprocess.c: video postprocessing using the ffmpeg library
3  *****************************************************************************
4  * Copyright (C) 1999-2001 the VideoLAN team
5  * $Id$
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include <vlc/vlc.h>
26 #include <vlc_vout.h>
27 #include <vlc_codec.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 HAVE_POSTPROC_POSTPROCESS_H
39 #   include <postproc/postprocess.h>
40 #else
41 #   include <libpostproc/postprocess.h>
42 #endif
43
44 #ifndef PP_CPU_CAPS_ALTIVEC
45 #   define PP_CPU_CAPS_ALTIVEC 0
46 #endif
47
48 /*****************************************************************************
49  * video_postproc_sys_t : ffmpeg video postprocessing descriptor
50  *****************************************************************************/
51 typedef struct video_postproc_sys_t
52 {
53     pp_context_t *pp_context;
54     pp_mode_t    *pp_mode;
55
56     vlc_bool_t   *pb_pp;
57
58     int i_width;
59     int i_height;
60
61 } video_postproc_sys_t;
62
63 static int PPQCallback( vlc_object_t *p_this, char const *psz_cmd,
64                         vlc_value_t oldval, vlc_value_t newval, void *p_data );
65
66 /*****************************************************************************
67  * OpenPostproc: probe and open the postproc
68  *****************************************************************************/
69 void *E_(OpenPostproc)( decoder_t *p_dec, vlc_bool_t *pb_pp )
70 {
71     video_postproc_sys_t *p_sys;
72     vlc_value_t val, val_orig, text;
73
74     p_sys = malloc( sizeof(video_postproc_sys_t) );
75     p_sys->pp_context = NULL;
76     p_sys->pp_mode = NULL;
77
78     *pb_pp = VLC_FALSE;
79     p_sys->pb_pp = pb_pp;
80
81     /* Create object variable if not already done */
82     if( var_Type( p_dec, "ffmpeg-pp-q" ) == 0 )
83     {
84         var_Create( p_dec, "ffmpeg-pp-q",
85                     VLC_VAR_INTEGER | VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
86         text.psz_string = _("Post processing");
87         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_SETTEXT, &text, NULL );
88
89         var_Get( p_dec, "ffmpeg-pp-q", &val_orig );
90         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_DELCHOICE, &val_orig, NULL );
91
92         val.i_int = 0; text.psz_string = _("Disable");
93         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_ADDCHOICE, &val, &text );
94         val.i_int = 1; text.psz_string = _("1 (Lowest)");
95         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_ADDCHOICE, &val, &text );
96         val.i_int = 2;
97         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_ADDCHOICE, &val, NULL );
98         val.i_int = 3;
99         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_ADDCHOICE, &val, NULL );
100         val.i_int = 4;
101         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_ADDCHOICE, &val, NULL );
102         val.i_int = 5;
103         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_ADDCHOICE, &val, NULL );
104         val.i_int = 6; text.psz_string = _("6 (Highest)");
105         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_ADDCHOICE, &val, &text );
106         var_AddCallback( p_dec, "ffmpeg-pp-q", PPQCallback, p_sys );
107     }
108
109     /* ***** Load post processing if enabled ***** */
110     var_Get( p_dec, "ffmpeg-pp-q", &val );
111     var_Set( p_dec, "ffmpeg-pp-q", val_orig );
112     if( val_orig.i_int )
113         *pb_pp = VLC_TRUE;
114
115     return p_sys;
116 }
117
118 /*****************************************************************************
119  * InitPostproc: 
120  *****************************************************************************/
121 int E_(InitPostproc)( decoder_t *p_dec, void *p_data,
122                       int i_width, int i_height, int pix_fmt )
123 {
124     video_postproc_sys_t *p_sys = (video_postproc_sys_t *)p_data;
125     unsigned i_cpu = vlc_CPU();
126     int i_flags = 0;
127
128     /* Set CPU capabilities */
129     if( i_cpu & CPU_CAPABILITY_MMX )
130     {
131         i_flags |= PP_CPU_CAPS_MMX;
132     }
133     if( i_cpu & CPU_CAPABILITY_MMXEXT )
134     {
135         i_flags |= PP_CPU_CAPS_MMX2;
136     }
137     if( i_cpu & CPU_CAPABILITY_3DNOW )
138     {
139         i_flags |= PP_CPU_CAPS_3DNOW;
140     }
141     if( i_cpu & CPU_CAPABILITY_ALTIVEC )
142     {
143         i_flags |= PP_CPU_CAPS_ALTIVEC;
144     }
145
146     switch( pix_fmt )
147     {
148     case PIX_FMT_YUV444P:
149         i_flags |= PP_FORMAT_444;
150         break;
151     case PIX_FMT_YUV422P:
152         i_flags |= PP_FORMAT_422;
153         break;
154     case PIX_FMT_YUV411P:
155         i_flags |= PP_FORMAT_411;
156         break;
157     default:
158         i_flags |= PP_FORMAT_420;
159         break;
160     }
161
162     p_sys->pp_context = pp_get_context( i_width, i_height, i_flags );
163     p_sys->i_width = i_width;
164     p_sys->i_height = i_height;
165
166     return VLC_SUCCESS;
167 }
168
169 /*****************************************************************************
170  * PostprocPict: 
171  *****************************************************************************/
172 int E_(PostprocPict)( decoder_t *p_dec, void *p_data,
173                       picture_t *p_pic, AVFrame *p_ff_pic )
174 {
175     video_postproc_sys_t *p_sys = (video_postproc_sys_t *)p_data;
176
177     uint8_t *src[3], *dst[3];
178     int i_plane, i_src_stride[3], i_dst_stride[3];
179
180     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
181     {
182         src[i_plane] = p_ff_pic->data[i_plane];
183         dst[i_plane] = p_pic->p[i_plane].p_pixels;
184
185         i_src_stride[i_plane] = p_ff_pic->linesize[i_plane];
186         i_dst_stride[i_plane] = p_pic->p[i_plane].i_pitch;
187     }
188
189     pp_postprocess( src, i_src_stride, dst, i_dst_stride,
190                     p_sys->i_width, p_sys->i_height,
191                     p_ff_pic->qscale_table, p_ff_pic->qstride,
192                     p_sys->pp_mode, p_sys->pp_context,
193                     p_ff_pic->pict_type );
194
195     return VLC_SUCCESS;
196 }
197
198 /*****************************************************************************
199  * ClosePostproc: 
200  *****************************************************************************/
201 void E_(ClosePostproc)( decoder_t *p_dec, void *p_data )
202 {
203     video_postproc_sys_t *p_sys = (video_postproc_sys_t *)p_data;
204
205     if( p_sys && p_sys->pp_mode )
206     {
207         pp_free_mode( p_sys->pp_mode );
208         if( p_sys->pp_context ) pp_free_context( p_sys->pp_context );
209     }
210
211     var_DelCallback( p_dec, "ffmpeg-pp-q", PPQCallback, p_sys );
212
213     if( p_sys ) free( p_sys );
214 }
215
216 /*****************************************************************************
217  * object variables callbacks: a bunch of object variables are used by the
218  * interfaces to interact with the decoder.
219  *****************************************************************************/
220 static int PPQCallback( vlc_object_t *p_this, char const *psz_cmd,
221                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
222 {
223     decoder_t *p_dec = (decoder_t *)p_this;
224     video_postproc_sys_t *p_sys = (video_postproc_sys_t *)p_data;
225
226     if( newval.i_int > 0 )
227     {
228         int  i_quality = newval.i_int;
229         char *psz_name = config_GetPsz( p_dec, "ffmpeg-pp-name" );
230         pp_mode_t *pp_mode;
231
232         if( !psz_name )
233         {
234             psz_name = strdup( "default" );
235         }
236         else if( *psz_name == '\0' )
237         {
238             free( psz_name );
239             psz_name = strdup( "default" );
240         }
241
242         pp_mode = pp_get_mode_by_name_and_quality( psz_name, i_quality );
243
244         if( !pp_mode )
245         {
246             msg_Err( p_dec, "failed getting mode for postproc" );
247             newval.i_int = 0;
248         }
249         else
250         {
251             msg_Dbg( p_dec, "postprocessing enabled" );
252         }
253         free( psz_name );
254
255         p_sys->pp_mode = pp_mode;
256     }
257     else
258     {
259         msg_Dbg( p_dec, "postprocessing disabled" );
260     }
261
262     *p_sys->pb_pp = newval.i_int;
263
264     return VLC_SUCCESS;
265 }