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