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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
|
/*
* vo_osdscaler.c: OSD scaling video-out post plugin
*
* See the main source file 'xineliboutput.c' for copyright information and
* how to reach the author.
*
* $Id: vo_osdscaler.c,v 1.4 2009-05-27 08:37:06 phintuka Exp $
*
*/
#include <stdlib.h>
#include <xine/video_out.h>
#ifdef SWBLEND
# include <xine/alphablend.h>
#endif
#define LOG_MODULENAME "[osdscaler] "
#include "../logdefs.h"
#include "vo_props.h"
#include "vo_hook.h"
/*#define LOGOSD(x...) LOGMSG(x)*/
#define LOGOSD(x...)
typedef rle_elem_t xine_rle_elem_t;
#include "../tools/rle.h"
/* Make sure our properties won't overlap with xine's properties */
#if VO_NUM_PROPERTIES > VO_PROP_OSD_SCALING
# error VO_NUM_PROPERTIES > VO_PROP_OSD_SCALING
#endif
#undef ABS
#define ABS(x) ((x)<0?-(x):(x))
/*
* osd_data_t
*
* - cache scaled OSD data
*/
typedef struct osd_data_s osd_data_t;
struct osd_data_s {
/* original source */
vo_overlay_t *source;
/* scaled overlay */
uint8_t scaled : 1;
vo_overlay_t ovl;
/* for what output resolution overlay was scaled */
uint16_t output_width;
uint16_t output_height;
/* linked list */
osd_data_t *next;
};
/*
* osd_data_dispose()
*
* - free() osd_data_t and all allocated memory
*/
static void osd_data_dispose(osd_data_t *data)
{
if (data->scaled)
free(data->ovl.rle);
free(data);
}
/*
* osd_data_clear()
*
* - free() whole linked list
*/
static void osd_data_clear(osd_data_t *data)
{
if (data) {
if (data->next)
osd_data_clear(data->next);
osd_data_dispose(data);
}
}
/*
* osd_data_remove()
*
* - remove (and free) specific osd_data_t item from linked list
*/
static void osd_data_remove(osd_data_t **list, osd_data_t *data)
{
if (!list || !*list)
return;
/* special handling for list head */
if (data == *list) {
*list = data->next;
free(data);
return;
}
osd_data_t *it = *list;
while (it) {
if (data == it->next) {
it->next = data->next;
free(data);
return;
}
it = it->next;
}
}
/*
* osd_data_init()
*
* - allocate and fill new osd_data_t
*
*/
static osd_data_t *osd_data_init(vo_overlay_t *ovl, osd_data_t *next,
uint32_t factor_x, uint32_t factor_y)
{
osd_data_t *data = calloc(1, sizeof(osd_data_t));
data->source = ovl;
data->next = next;
memcpy(&data->ovl, ovl, sizeof(vo_overlay_t));
int num_rle = data->ovl.num_rle;
/* new position and size */
int x2 = ovl->x + ovl->width + 1;
int y2 = ovl->y + ovl->height + 1;
x2 = ((x2+1) * factor_x) >> 16;
y2 = ((y2+1) * factor_y) >> 16;
data->ovl.x = (ovl->x * factor_x) >> 16;
data->ovl.y = (ovl->y * factor_y) >> 16;
data->ovl.width = x2 - data->ovl.x - 1;
data->ovl.height = y2 - data->ovl.y - 1;
data->ovl.rle = (rle_elem_t*)
rle_scale_nearest((struct xine_rle_elem_s*)ovl->rle, &num_rle,
ovl->width, ovl->height,
data->ovl.width, data->ovl.height);
data->ovl.num_rle = num_rle;
data->scaled = 1;
LOGOSD("I: %d,%d %dx%d", ovl->x, ovl->y, ovl->width, ovl->height);
LOGOSD("O: %d,%d %dx%d", data->ovl.x, data->ovl.y, data->ovl.width, data->ovl.height);
return data;
}
/*
* osdscaler_hook_t
*/
typedef struct {
vo_driver_hook_t h;
/* configuration */
uint8_t enable;
uint8_t unscaled_supported;
uint8_t custom_extent_supported;
uint8_t argb_supported;
/* current output */
uint16_t output_width;
uint16_t output_height;
uint32_t factor_x; /* scaling factor << 16 */
uint32_t factor_y;
uint16_t x_move; /* OSD displacement */
uint16_t y_move;
/* currently showing OSDs - pre-scaled data */
osd_data_t *active_osds;
} osdscaler_hook_t;
/*
*
*/
/*
* override overlay_begin()
*/
static void osdscaler_overlay_begin (vo_driver_t *self, vo_frame_t *frame, int changed)
{
osdscaler_hook_t *this = (osdscaler_hook_t*)self;
/* assume we're wired when called */
if (!this->h.orig_driver) {
LOGMSG("osdscaler_overlay_begin: assertion this->h.orig_driver failed !");
abort();
}
/* re-scale all if OSD changed */
if (changed) {
LOGOSD("osdscaler_overlay_begin: changed = 1");
osd_data_clear(this->active_osds);
this->active_osds = NULL;
this->unscaled_supported = (vo_def_get_capabilities(self) & VO_CAP_UNSCALED_OVERLAY);
/* VO_CAP_OSDSCALING == VO_CAP_CUSTOM_EXTENT_OVERLAY */
this->custom_extent_supported = (vo_def_get_capabilities(self) & VO_CAP_OSDSCALING);
/* VO_CAP_ARGB == VO_CAP_ARGB_LAYER_OVERLAY */
this->argb_supported = (vo_def_get_capabilities(self) & VO_CAP_ARGB);
}
/* redirect */
if (this->h.orig_driver->overlay_begin)
this->h.orig_driver->overlay_begin(this->h.orig_driver, frame, changed);
}
static int check_for_scaling(osdscaler_hook_t *this, vo_frame_t *frame, vo_overlay_t *overlay)
{
this->x_move = this->y_move = 0;
if (!this->enable)
return 0;
/* check for VDR OSD */
if (overlay->hili_rgb_clut != VDR_OSD_MAGIC /* not from vdr */) {
LOGOSD("overlay: source not VDR");
return 0;
}
/* VDR input plugin stores some control data in hili clut area */
vdr_osd_extradata_t *data = (vdr_osd_extradata_t *)overlay->hili_color;
int extent_width = data->extent_width;
int extent_height = data->extent_height;
if (!data->scaling)
return 0;
#if 0
if (this->custom_extent_supported) {
/* let the "real" video driver do scaling */
return 0;
}
#else
# ifdef VO_CAP_CUSTOM_EXTENT_OVERLAY
/* disable VDPAU HW scaler */
overlay->extent_width = 0;
overlay->extent_height = 0;
# endif
#endif
/* detect output size */
if (overlay->unscaled && this->unscaled_supported) {
this->output_width = vo_def_get_property((vo_driver_t*)this, VO_PROP_WINDOW_WIDTH);
this->output_height = vo_def_get_property((vo_driver_t*)this, VO_PROP_WINDOW_HEIGHT);
} else {
this->output_width = frame->width;
this->output_height = frame->height;
/* check cropping */
if (frame->crop_top > 0) this->output_height -= frame->crop_top;
if (frame->crop_bottom > 0) this->output_height -= frame->crop_bottom;
if (frame->crop_left > 0) this->output_width -= frame->crop_left;
if (frame->crop_right > 0) this->output_width -= frame->crop_right;
}
/* check if scaling should be done */
if (ABS(this->output_width - extent_width) > extent_width /20 ||
ABS(this->output_height - extent_height) > extent_height/20 ) {
LOGOSD("scaling required");
this->factor_x = 0x10000 * this->output_width / extent_width;
this->factor_y = 0x10000 * this->output_height / extent_height;
return 1;
}
/* if no scaling was required, we may still need to re-center OSD */
if(this->output_width != extent_width)
this->x_move = (this->output_width - extent_width)/2;
if(this->output_height != extent_height)
this->y_move = (this->output_height - extent_height)/2;
return 0;
}
static vo_overlay_t *scale_overlay(osdscaler_hook_t *this, vo_frame_t *frame, vo_overlay_t *overlay)
{
if (check_for_scaling(this, frame, overlay)) {
/* find old scaled OSD */
osd_data_t *scaled = this->active_osds;
while (scaled && scaled->source != overlay)
scaled = scaled->next;
/* output size changed since last scaling (need to re-scale) ? */
if (scaled &&
(scaled->output_width != this->output_width ||
scaled->output_height != this->output_height)) {
LOGOSD("re-scaling required: output size changed %dx%d -> %dx%d",
scaled->output_width, scaled->output_height, this->output_width, this->output_height);
osd_data_remove(&this->active_osds, scaled);
scaled = NULL;
}
/* not scaled found ? */
if (!scaled) {
LOGOSD("scaling OSD");
scaled = this->active_osds = osd_data_init(overlay, this->active_osds,
this->factor_x, this->factor_y);
scaled->output_width = this->output_width;
scaled->output_height = this->output_height;
}
/* override */
overlay = &scaled->ovl;
}
return overlay;
}
/*
* interface
*/
/*
* override overlay_blend()
*/
static void osdscaler_overlay_blend (vo_driver_t *self, vo_frame_t *frame, vo_overlay_t *overlay)
{
osdscaler_hook_t *this = (osdscaler_hook_t*)self;
overlay = scale_overlay(this, frame, overlay);
/* redirect */
if (this->h.orig_driver->overlay_blend)
this->h.orig_driver->overlay_blend(this->h.orig_driver, frame, overlay);
}
/*
* override overlay_end()
*/
static void osdscaler_overlay_end (vo_driver_t *self, vo_frame_t *vo_img)
{
osdscaler_hook_t *this = (osdscaler_hook_t*)self;
/* redirect */
if (this->h.orig_driver->overlay_end)
this->h.orig_driver->overlay_end(this->h.orig_driver, vo_img);
}
/*
* Management interface
*/
/*
* override get_capabilities()
*/
static uint32_t osdscaler_get_capabilities(vo_driver_t *self)
{
return vo_def_get_capabilities(self) |
VO_CAP_OSDSCALING;
}
/*
* override get_property()
*/
static int osdscaler_get_property(vo_driver_t *self, int prop)
{
osdscaler_hook_t *this = (osdscaler_hook_t*)self;
switch (prop) {
case VO_PROP_OSD_SCALING: return this->enable;
default:;
}
return vo_def_get_property(self, prop);
}
/*
* override set_property()
*/
static int osdscaler_set_property(vo_driver_t *self, int prop, int val)
{
osdscaler_hook_t *this = (osdscaler_hook_t*)self;
switch (prop) {
case VO_PROP_OSD_SCALING:
if (this->enable != val) {
LOGOSD("osdscaler_set_property: enable %d->%d", this->enable, val);
this->enable = val?1:0;
}
return this->enable;
}
return vo_def_set_property(self, prop, val);
}
/*
* dispose()
*/
static void osdscaler_dispose(vo_driver_t *self)
{
osdscaler_hook_t *this = (osdscaler_hook_t *) self;
osd_data_clear(this->active_osds);
vo_def_dispose(self);
}
/*
* init()
*/
vo_driver_t *osdscaler_init(void)
{
osdscaler_hook_t *this = calloc(1, sizeof(osdscaler_hook_t));
/* OSD interface */
this->h.vo.overlay_begin = osdscaler_overlay_begin;
this->h.vo.overlay_blend = osdscaler_overlay_blend;
this->h.vo.overlay_end = osdscaler_overlay_end;
/* management interface */
this->h.vo.get_capabilities = osdscaler_get_capabilities;
this->h.vo.get_property = osdscaler_get_property;
this->h.vo.set_property = osdscaler_set_property;
this->h.vo.dispose = osdscaler_dispose;
/* initialize data */
this->enable = 0;
return &this->h.vo;
}
|