]> git.sesse.net Git - vlc/blob - modules/codec/avcodec/deinterlace.c
avcodec: print picture format name when unsupported (refs #7994)
[vlc] / modules / codec / avcodec / deinterlace.c
1 /*****************************************************************************
2  * deinterlace.c: video filter doing chroma conversion and resizing
3  *               using the libavcodec library
4  *****************************************************************************
5  * Copyright (C) 1999-2001 VLC authors and VideoLAN
6  * $Id$
7  *
8  * Authors: Gildas Bazin <gbazin@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * 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_common.h>
33 #include <vlc_codec.h>
34 #include <vlc_filter.h>
35
36 #include <libavcodec/avcodec.h>
37
38 #include "avcodec.h"
39 #include "avcommon.h"
40
41 static picture_t *Deinterlace( filter_t *p_filter, picture_t *p_pic );
42
43 /*****************************************************************************
44  * filter_sys_t : filter descriptor
45  *****************************************************************************/
46 struct filter_sys_t
47 {
48     bool b_resize;
49     bool b_convert;
50     bool b_resize_first;
51     bool b_enable_croppadd;
52
53     es_format_t fmt_in;
54     int i_src_ffmpeg_chroma;
55     es_format_t fmt_out;
56     int i_dst_ffmpeg_chroma;
57
58     AVPicture tmp_pic;
59 };
60
61 /*****************************************************************************
62  * OpenDeinterlace: probe the filter and return score
63  *****************************************************************************/
64 int OpenDeinterlace( vlc_object_t *p_this )
65 {
66     filter_t *p_filter = (filter_t*)p_this;
67     filter_sys_t *p_sys;
68
69     /* libavcodec needs to be initialized for some chroma conversions */
70     vlc_init_avcodec();
71
72     /* Check if we can handle that formats */
73     if( TestFfmpegChroma( -1, p_filter->fmt_in.i_codec  ) != VLC_SUCCESS )
74     {
75         msg_Err( p_filter, "Failed to match chroma type" );
76         return VLC_EGENERIC;
77     }
78
79     /* Allocate the memory needed to store the decoder's structure */
80     if( ( p_filter->p_sys = p_sys =
81           (filter_sys_t *)malloc(sizeof(filter_sys_t)) ) == NULL )
82     {
83         return VLC_EGENERIC;
84     }
85
86     /* Misc init */
87     p_filter->fmt_in.video.i_chroma = p_filter->fmt_in.i_codec;
88     if( GetFfmpegChroma( &p_sys->i_src_ffmpeg_chroma, p_filter->fmt_in.video ) != VLC_SUCCESS )
89     {
90         msg_Err( p_filter, "Failed to match chroma type" );
91         return VLC_EGENERIC;
92     }
93     p_filter->pf_video_filter = Deinterlace;
94
95     msg_Dbg( p_filter, "deinterlacing" );
96
97     return VLC_SUCCESS;
98 }
99
100 /*****************************************************************************
101  * CloseDeinterlace: clean up the filter
102  *****************************************************************************/
103 void CloseDeinterlace( vlc_object_t *p_this )
104 {
105     filter_t *p_filter = (filter_t*)p_this;
106     filter_sys_t *p_sys = p_filter->p_sys;
107
108     free( p_sys );
109 }
110
111 /*****************************************************************************
112  * Do the processing here
113  *****************************************************************************/
114 static picture_t *Deinterlace( filter_t *p_filter, picture_t *p_pic )
115 {
116     filter_sys_t *p_sys = p_filter->p_sys;
117     AVPicture src_pic, dest_pic;
118     picture_t *p_pic_dst;
119     int i, i_res = -1;
120
121     /* Request output picture */
122     p_pic_dst = filter_NewPicture( p_filter );
123     if( !p_pic_dst )
124     {
125         picture_Release( p_pic );
126         return NULL;
127     }
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         filter_DeletePicture( p_filter, p_pic_dst );
148         picture_Release( p_pic );
149         return NULL;
150     }
151
152     picture_CopyProperties( p_pic_dst, p_pic );
153     p_pic_dst->b_progressive = true;
154     picture_Release( p_pic );
155     return p_pic_dst;
156 }