]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/deinterlace.c
db46f77a950a3bd39ae097009ad2c37a9d642e94
[vlc] / modules / codec / ffmpeg / deinterlace.c
1 /*****************************************************************************
2  * video filter: video filter doing chroma conversion and resizing
3  *               using the ffmpeg library
4  *****************************************************************************
5  * Copyright (C) 1999-2001 the VideoLAN team
6  * $Id$
7  *
8  * Authors: Gildas Bazin <gbazin@videolan.org>
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 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc/vlc.h>
33 #include <vlc_codec.h>
34 #include <vlc_vout.h>
35 #include <vlc_filter.h>
36
37 /* ffmpeg header */
38 #ifdef HAVE_FFMPEG_AVCODEC_H
39 #   include <ffmpeg/avcodec.h>
40 #else
41 #   include <avcodec.h>
42 #endif
43
44 #include "ffmpeg.h"
45
46 static picture_t *Deinterlace( filter_t *p_filter, picture_t *p_pic );
47
48 /*****************************************************************************
49  * filter_sys_t : filter descriptor
50  *****************************************************************************/
51 struct filter_sys_t
52 {
53     vlc_bool_t b_resize;
54     vlc_bool_t b_convert;
55     vlc_bool_t b_resize_first;
56     vlc_bool_t b_enable_croppadd;
57
58     es_format_t fmt_in;
59     int i_src_ffmpeg_chroma;
60     es_format_t fmt_out;
61     int i_dst_ffmpeg_chroma;
62
63     AVPicture tmp_pic;
64 };
65
66 /*****************************************************************************
67  * OpenDeinterlace: probe the filter and return score
68  *****************************************************************************/
69 int E_(OpenDeinterlace)( vlc_object_t *p_this )
70 {
71     filter_t *p_filter = (filter_t*)p_this;
72     filter_sys_t *p_sys;
73
74     /* Check if we can handle that formats */
75     if( E_(GetFfmpegChroma)( p_filter->fmt_in.video.i_chroma ) < 0 )
76     {
77         return VLC_EGENERIC;
78     }
79
80     /* Allocate the memory needed to store the decoder's structure */
81     if( ( p_filter->p_sys = p_sys =
82           (filter_sys_t *)malloc(sizeof(filter_sys_t)) ) == NULL )
83     {
84         msg_Err( p_filter, "out of memory" );
85         return VLC_EGENERIC;
86     }
87
88     /* Misc init */
89     p_sys->i_src_ffmpeg_chroma =
90         E_(GetFfmpegChroma)( p_filter->fmt_in.video.i_chroma );
91     p_filter->pf_video_filter = Deinterlace;
92
93     msg_Dbg( p_filter, "deinterlacing" );
94
95     /* libavcodec needs to be initialized for some chroma conversions */
96     E_(InitLibavcodec)(p_this);
97
98     return VLC_SUCCESS;
99 }
100
101 /*****************************************************************************
102  * CloseDeinterlace: clean up the filter
103  *****************************************************************************/
104 void E_(CloseDeinterlace)( vlc_object_t *p_this )
105 {
106     filter_t *p_filter = (filter_t*)p_this;
107     filter_sys_t *p_sys = p_filter->p_sys;
108
109     free( p_sys );
110 }
111
112 /*****************************************************************************
113  * Do the processing here
114  *****************************************************************************/
115 static picture_t *Deinterlace( filter_t *p_filter, picture_t *p_pic )
116 {
117     filter_sys_t *p_sys = p_filter->p_sys;
118     AVPicture src_pic, dest_pic;
119     picture_t *p_pic_dst;
120     int i;
121
122     /* Request output picture */
123     p_pic_dst = p_filter->pf_vout_buffer_new( p_filter );
124     if( !p_pic_dst )
125     {
126         msg_Warn( p_filter, "can't get output picture" );
127         return NULL;
128     }
129
130     /* Prepare the AVPictures for the conversion */
131     for( i = 0; i < p_pic->i_planes; i++ )
132     {
133         src_pic.data[i] = p_pic->p[i].p_pixels;
134         src_pic.linesize[i] = p_pic->p[i].i_pitch;
135     }
136     for( i = 0; i < p_pic_dst->i_planes; i++ )
137     {
138         dest_pic.data[i] = p_pic_dst->p[i].p_pixels;
139         dest_pic.linesize[i] = p_pic_dst->p[i].i_pitch;
140     }
141
142     avpicture_deinterlace( &dest_pic, &src_pic, p_sys->i_src_ffmpeg_chroma,
143                            p_filter->fmt_in.video.i_width,
144                            p_filter->fmt_in.video.i_height );
145
146     p_pic_dst->date = p_pic->date;
147     p_pic_dst->b_force = p_pic->b_force;
148     p_pic_dst->i_nb_fields = p_pic->i_nb_fields;
149     p_pic_dst->b_progressive = VLC_TRUE;
150     p_pic_dst->b_top_field_first = p_pic->b_top_field_first;
151
152     p_pic->pf_release( p_pic );
153     return p_pic_dst;
154 }