]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/postprocess.c
* all: only include header that are needed (and no more stdlib.h, string.h
[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.3 2003/11/22 23:39:14 fenrir 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     int i_width;
55     int i_height;
56
57 } video_postproc_sys_t;
58
59 /*****************************************************************************
60  * OpenPostproc: probe and open the postproc
61  *****************************************************************************/
62 int E_(OpenPostproc)( decoder_t *p_dec, void **pp_data )
63 {
64     video_postproc_sys_t **pp_sys = (video_postproc_sys_t **)pp_data;
65     pp_mode_t *pp_mode;
66     vlc_value_t val;
67
68     /* ***** Load post processing if enabled ***** */
69     var_Create( p_dec, "ffmpeg-pp-q", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
70     var_Get( p_dec, "ffmpeg-pp-q", &val );
71     if( val.i_int > 0 )
72     {
73         int  i_quality = val.i_int;
74         char *psz_name = config_GetPsz( p_dec, "ffmpeg-pp-name" );
75
76         if( !psz_name )
77         {
78             psz_name = strdup( "default" );
79         }
80         else if( *psz_name == '\0' )
81         {
82             free( psz_name );
83             psz_name = strdup( "default" );
84         }
85
86         pp_mode = pp_get_mode_by_name_and_quality( psz_name, i_quality );
87
88         if( !pp_mode )
89         {
90             msg_Err( p_dec, "failed geting mode for postproc" );
91         }
92         else
93         {
94             msg_Info( p_dec, "postprocessing activated" );
95         }
96         free( psz_name );
97
98         *pp_sys = malloc( sizeof(video_postproc_sys_t) );
99         (*pp_sys)->pp_context = NULL;
100         (*pp_sys)->pp_mode    = pp_mode;
101
102         return VLC_SUCCESS;
103     }
104     else
105     {
106         msg_Dbg( p_dec, "no postprocessing enabled" );
107         return VLC_EGENERIC;
108     }
109 }
110
111 /*****************************************************************************
112  * InitPostproc: 
113  *****************************************************************************/
114 int E_(InitPostproc)( decoder_t *p_dec, void *p_data,
115                       int i_width, int i_height, int pix_fmt )
116 {
117     video_postproc_sys_t *p_sys = (video_postproc_sys_t *)p_data;
118     int32_t i_cpu = p_dec->p_libvlc->i_cpu;
119     int i_flags = 0;
120
121     if( i_cpu & CPU_CAPABILITY_MMX )
122     {
123         i_flags |= PP_CPU_CAPS_MMX;
124     }
125     if( i_cpu & CPU_CAPABILITY_MMXEXT )
126     {
127         i_flags |= PP_CPU_CAPS_MMX2;
128     }
129     if( i_cpu & CPU_CAPABILITY_3DNOW )
130     {
131         i_flags |= PP_CPU_CAPS_3DNOW;
132     }
133
134     switch( pix_fmt )
135     {
136     case PIX_FMT_YUV444P:
137         i_flags |= PP_FORMAT_444;
138         break;
139     case PIX_FMT_YUV422P:
140         i_flags |= PP_FORMAT_422;
141         break;
142     case PIX_FMT_YUV411P:
143         i_flags |= PP_FORMAT_411;
144         break;
145     default:
146         i_flags |= PP_FORMAT_420;
147         break;
148     }
149
150     p_sys->pp_context = pp_get_context( i_width, i_height, i_flags );
151     p_sys->i_width = i_width;
152     p_sys->i_height = i_height;
153
154     return VLC_SUCCESS;
155 }
156
157 /*****************************************************************************
158  * PostprocPict: 
159  *****************************************************************************/
160 int E_(PostprocPict)( decoder_t *p_dec, void *p_data,
161                       picture_t *p_pic, AVFrame *p_ff_pic )
162 {
163     video_postproc_sys_t *p_sys = (video_postproc_sys_t *)p_data;
164
165     uint8_t *src[3], *dst[3];
166     int i_plane, i_src_stride[3], i_dst_stride[3];
167
168     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
169     {
170         src[i_plane] = p_ff_pic->data[i_plane];
171         dst[i_plane] = p_pic->p[i_plane].p_pixels;
172
173         i_src_stride[i_plane] = p_ff_pic->linesize[i_plane];
174         i_dst_stride[i_plane] = p_pic->p[i_plane].i_pitch;
175     }
176
177     pp_postprocess( src, i_src_stride, dst, i_dst_stride,
178                     p_sys->i_width, p_sys->i_height,
179                     p_ff_pic->qscale_table, p_ff_pic->qstride,
180                     p_sys->pp_mode, p_sys->pp_context,
181                     p_ff_pic->pict_type );
182
183     return VLC_SUCCESS;
184 }
185
186 /*****************************************************************************
187  * ClosePostproc: 
188  *****************************************************************************/
189 void E_(ClosePostproc)( decoder_t *p_dec, void *p_data )
190 {
191     video_postproc_sys_t *p_sys = (video_postproc_sys_t *)p_data;
192
193     if( p_sys && p_sys->pp_mode )
194     {
195         pp_free_mode( p_sys->pp_mode );
196         if( p_sys->pp_context ) pp_free_context( p_sys->pp_context );
197     }
198 }
199
200 #endif /* LIBAVCODEC_PP */