File: blend.c

package info (click to toggle)
gavl 1.4.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 14,912 kB
  • sloc: ansic: 437,319; sh: 10,487; makefile: 238
file content (201 lines) | stat: -rw-r--r-- 5,737 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*****************************************************************
 * gavl - a general purpose audio/video processing library
 *
 * Copyright (c) 2001 - 2011 Members of the Gmerlin project
 * gmerlin-general@lists.sourceforge.net
 * http://gmerlin.sourceforge.net
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * *****************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <gavl/gavl.h>
#include <video.h>
#include <blend.h>

gavl_overlay_blend_context_t * gavl_overlay_blend_context_create()
  {
  gavl_overlay_blend_context_t * ret;
  ret = calloc(1, sizeof(*ret));

  ret->ovl_win = gavl_video_frame_create(NULL);
  ret->dst_win = gavl_video_frame_create(NULL);
  
  gavl_video_options_set_defaults(&ret->opt);
  
  return ret;
  }

void gavl_overlay_blend_context_destroy(gavl_overlay_blend_context_t * ctx)
  {
  gavl_video_frame_null(ctx->dst_win);
  gavl_video_frame_destroy(ctx->dst_win);
  
  if(ctx->ovl_win)
    {
    gavl_video_frame_null(ctx->ovl_win);
    gavl_video_frame_destroy(ctx->ovl_win);
    }
  
  free(ctx);
  }

gavl_video_options_t *
gavl_overlay_blend_context_get_options(gavl_overlay_blend_context_t * ctx)
  {
  return &ctx->opt;
  }

int
gavl_overlay_blend_context_init(gavl_overlay_blend_context_t * ctx,
                                const gavl_video_format_t * dst_format,
                                gavl_video_format_t * ovl_format)
  {
  /* Clean up from previous initializations */

  if(ctx->ovl_win)
    {
    gavl_video_frame_null(ctx->ovl_win);
    gavl_video_frame_destroy(ctx->ovl_win);
    ctx->ovl_win = NULL;
    ctx->has_overlay = 0;
    }
  
  /* Check for non alpha capable overlay format */

  if(!gavl_pixelformat_has_alpha(ovl_format->pixelformat))
    return 0;
  
  /* Copy formats */
  gavl_video_format_copy(&ctx->dst_format, dst_format);
  gavl_video_format_copy(&ctx->ovl_format, ovl_format);

  /* Get chroma subsampling of the destination */
  gavl_pixelformat_chroma_sub(dst_format->pixelformat,
                              &ctx->dst_sub_h, &ctx->dst_sub_v);

  /* Get blend function */

  ctx->func = 
    gavl_find_blend_func_c(ctx,
                           dst_format->pixelformat,
                           &ctx->ovl_format.pixelformat);
  
  ctx->ovl_win = gavl_video_frame_create(NULL);
  gavl_video_format_copy(ovl_format, &ctx->ovl_format);
  return 1;
  }

void gavl_overlay_blend_context_set_overlay(gavl_overlay_blend_context_t * ctx,
                                            gavl_overlay_t * ovl)
  {
  int diff;
  /* Save overlay */

  if(!ovl)
    {
    ctx->has_overlay = 0;
    return;
    }
  ctx->has_overlay = 1;
  memcpy(&ctx->ovl, ovl, sizeof(ctx->ovl));

  /* Crop rectangle to destination format */

  if(ctx->ovl.dst_x < 0)
    {
    ctx->ovl.ovl_rect.w += ctx->ovl.dst_x;
    ctx->ovl.ovl_rect.x -= ctx->ovl.dst_x;
    ctx->ovl.dst_x = 0;
    }

  if(ctx->ovl.dst_y < 0)
    {
    ctx->ovl.ovl_rect.h += ctx->ovl.dst_y;
    ctx->ovl.ovl_rect.y -= ctx->ovl.dst_y;
    ctx->ovl.dst_y = 0;
    }
  
  diff = ctx->ovl.dst_x + ctx->ovl.ovl_rect.w - ctx->dst_format.image_width;
  if(diff > 0)
    ctx->ovl.ovl_rect.w -= diff;

  diff = ctx->ovl.dst_y + ctx->ovl.ovl_rect.h - ctx->dst_format.image_height;
  if(diff > 0)
    ctx->ovl.ovl_rect.h -= diff;

  /* Crop rectangle to source format */

  if(ctx->ovl.ovl_rect.x < 0)
    {
    ctx->ovl.ovl_rect.w += ctx->ovl.ovl_rect.x;
    ctx->ovl.dst_x -= ctx->ovl.ovl_rect.x;
    ctx->ovl.ovl_rect.x = 0;
    }

  if(ctx->ovl.ovl_rect.y < 0)
    {
    ctx->ovl.ovl_rect.h += ctx->ovl.ovl_rect.y;
    ctx->ovl.dst_y -= ctx->ovl.ovl_rect.y;
    ctx->ovl.ovl_rect.y = 0;
    }

  diff = ctx->ovl.ovl_rect.x + ctx->ovl.ovl_rect.w - ctx->ovl_format.image_width;
  if(diff > 0)
    ctx->ovl.ovl_rect.w -= diff;

  diff = ctx->ovl.ovl_rect.y + ctx->ovl.ovl_rect.h - ctx->ovl_format.image_height;
  if(diff > 0)
    ctx->ovl.ovl_rect.h -= diff;

  /* Align rectangle */

  ctx->ovl.ovl_rect.w -= ctx->ovl.ovl_rect.w % ctx->dst_sub_h;
  ctx->ovl.ovl_rect.h -= ctx->ovl.ovl_rect.h % ctx->dst_sub_v;
  ctx->ovl.dst_x      -= ctx->ovl.dst_x % ctx->dst_sub_h;
  ctx->ovl.dst_y      -= ctx->ovl.dst_y % ctx->dst_sub_v;

  /* Set destination rectangle for getting the subframe later on */

  ctx->dst_rect.x = ctx->ovl.dst_x;
  ctx->dst_rect.y = ctx->ovl.dst_y;

  ctx->dst_rect.w = ctx->ovl.ovl_rect.w;
  ctx->dst_rect.h = ctx->ovl.ovl_rect.h;
    
  gavl_video_frame_get_subframe(ctx->ovl_format.pixelformat,
                                ovl->frame,
                                ctx->ovl_win,
                                &ctx->ovl.ovl_rect);
  }

void gavl_overlay_blend(gavl_overlay_blend_context_t * ctx,
                        gavl_video_frame_t * dst_frame)
  {
  if(!ctx->has_overlay)
    return;
  /* Get subframe from destination */
  
  gavl_video_frame_get_subframe(ctx->dst_format.pixelformat,
                                dst_frame,
                                ctx->dst_win,
                                &ctx->dst_rect);
  /* Fire up blender */

  ctx->func(ctx, ctx->dst_win, ctx->ovl_win);
  }