1 /*****************************************************************
2  * gavl - a general purpose audio/video processing library
3  *
4  * Copyright (c) 2001 - 2011 Members of the Gmerlin project
5  * gmerlin-general@lists.sourceforge.net
6  * http://gmerlin.sourceforge.net
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  * *****************************************************************/
21 
22 #include <string.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <math.h>
26 
27 #include <gavl/gavl.h>
28 #include <video.h>
29 
30 #include <deinterlace.h>
31 #include <accel.h>
32 
gavl_video_deinterlacer_create()33 gavl_video_deinterlacer_t * gavl_video_deinterlacer_create()
34   {
35   gavl_video_deinterlacer_t * ret;
36   ret = calloc(1, sizeof(*ret));
37   gavl_video_options_set_defaults(&ret->opt);
38 
39   ret->src_field = gavl_video_frame_create(NULL);
40   ret->dst_field = gavl_video_frame_create(NULL);
41   return ret;
42   }
43 
gavl_video_deinterlacer_destroy(gavl_video_deinterlacer_t * d)44 void gavl_video_deinterlacer_destroy(gavl_video_deinterlacer_t * d)
45   {
46   gavl_video_frame_destroy(d->src_field);
47   gavl_video_frame_destroy(d->dst_field);
48 
49   if(d->scaler)
50     gavl_video_scaler_destroy(d->scaler);
51 
52   free(d);
53   }
54 
55 gavl_video_options_t *
gavl_video_deinterlacer_get_options(gavl_video_deinterlacer_t * d)56 gavl_video_deinterlacer_get_options(gavl_video_deinterlacer_t * d)
57   {
58   return &d->opt;
59   }
60 
61 
gavl_video_deinterlacer_init(gavl_video_deinterlacer_t * d,const gavl_video_format_t * src_format)62 int gavl_video_deinterlacer_init(gavl_video_deinterlacer_t * d,
63                                  const gavl_video_format_t * src_format)
64   {
65 
66   gavl_video_format_copy(&d->format, src_format);
67   gavl_video_format_copy(&d->half_height_format, src_format);
68 
69   if((d->format.interlace_mode == GAVL_INTERLACE_MIXED) ||
70      (d->format.interlace_mode == GAVL_INTERLACE_MIXED_TOP) ||
71      (d->format.interlace_mode == GAVL_INTERLACE_MIXED_BOTTOM))
72     d->mixed = 1;
73   else
74     d->mixed = 0;
75 
76   d->half_height_format.image_height /= 2;
77   d->half_height_format.frame_height /= 2;
78 
79   d->num_planes = gavl_pixelformat_num_planes(d->format.pixelformat);
80   gavl_pixelformat_chroma_sub(d->format.pixelformat, &d->sub_h, &d->sub_v);
81 
82   switch(d->opt.deinterlace_mode)
83     {
84     case GAVL_DEINTERLACE_NONE:
85       break;
86     case GAVL_DEINTERLACE_COPY:
87       gavl_deinterlacer_init_copy(d);
88       break;
89     case GAVL_DEINTERLACE_SCALE:
90       gavl_deinterlacer_init_scale(d);
91       break;
92     case GAVL_DEINTERLACE_BLEND:
93       if(!gavl_deinterlacer_init_blend(d))
94         return 0;
95       break;
96     }
97   return 1;
98   }
99 
gavl_video_deinterlacer_deinterlace(gavl_video_deinterlacer_t * d,const gavl_video_frame_t * input_frame,gavl_video_frame_t * output_frame)100 void gavl_video_deinterlacer_deinterlace(gavl_video_deinterlacer_t * d,
101                                          const gavl_video_frame_t * input_frame,
102                                          gavl_video_frame_t * output_frame)
103   {
104   if(d->mixed)
105     {
106     if((input_frame->interlace_mode != GAVL_INTERLACE_NONE) ||
107        (d->opt.conversion_flags & GAVL_FORCE_DEINTERLACE))
108       d->func(d, input_frame, output_frame);
109     else
110       gavl_video_frame_copy(&d->format, output_frame, input_frame);
111     }
112   else
113     d->func(d, input_frame, output_frame);
114   }
115 
116