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 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
|
/*
* Zapping TV viewer
*
* Copyright (C) 2004 Michael H. Schimek
*
* 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, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* $Id: main.c,v 1.11 2006/04/12 01:42:25 mschimek Exp $ */
#include "site_def.h"
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "common/intl-priv.h"
#include "libtv/image_format.h" /* tv_memcpy() */
#include "src/properties.h"
#include "src/plugin_common.h"
#include "src/zgconf.h"
#include "libtv/cpu.h"
#include "preferences.h"
#include "main.h"
#define GCONF_DIR "/apps/zapping/plugins/deinterlace"
#ifndef DI_MAIN_HEIGHT_DIV
/* For tests. My CPU is too slow to run
all methods at full height. */
# define DI_MAIN_HEIGHT_DIV 1
#endif
DEINTERLACE_METHOD * deinterlace_methods[30];
static zf_consumer consumer;
static gint capture_format_id = -1;
static gboolean capturing;
static gboolean active;
static gboolean reverse_fields;
typedef struct {
TPicture tpicture;
zf_buffer * buffer;
} picture;
static picture pictures[MAX_PICTURE_HISTORY];
static TDeinterlaceInfo info;
static guint queue_len2;
static guint field_count;
static guint capture_width;
static guint capture_height;
static DEINTERLACE_METHOD * method;
DEINTERLACE_METHOD *
deinterlace_find_method (const gchar * name)
{
guint i;
g_return_val_if_fail (NULL != name, NULL);
for (i = 0; i < G_N_ELEMENTS (deinterlace_methods); ++i)
if ((method = deinterlace_methods[i]))
if (0 == g_ascii_strcasecmp (name, method->szName))
{
return method;
}
return NULL;
}
static guint
deinterlace (zimage * dst0,
zimage * dst1)
{
const tv_video_standard *std;
gboolean bottom_first;
zf_buffer *b1;
zf_buffer *b2;
TPicture *tp;
picture *p;
capture_frame *cf;
const zimage *src;
guint n_frames;
if (0)
{
fputc ('=', stderr);
fflush (stderr);
}
std = tv_cur_video_standard (zapping->info);
bottom_first = (std && (std->videostd_set & TV_VIDEOSTD_SET_NTSC));
bottom_first ^= reverse_fields;
b1 = NULL;
b2 = NULL;
for (;;)
{
if (b2)
{
zf_send_empty_buffer (&consumer, b2);
}
b2 = b1;
if (!(b1 = zf_recv_full_buffer (&consumer)))
break;
}
if (!b2)
return 0;
cf = PARENT (b2, capture_frame, b);
src = retrieve_frame (cf, TV_PIXFMT_YUYV, /* copy */ FALSE);
if (!src)
{
zf_send_empty_buffer (&consumer, b2);
return 0;
}
n_frames = 0;
/* First field. */
tp = info.PictureHistory[queue_len2 - 1];
p = PARENT (tp, picture, tpicture);
if (p->buffer)
{
zf_send_empty_buffer (&consumer, p->buffer);
p->buffer = NULL;
}
memmove (info.PictureHistory + 1,
info.PictureHistory + 0,
((G_N_ELEMENTS (info.PictureHistory) - 1)
* sizeof (info.PictureHistory[0])));
info.PictureHistory[0] = tp;
p->buffer = b2;
if (bottom_first)
{
tp->pData = ((uint8_t *) src->img) + info.FrameWidth * 2;
tp->Flags = PICTURE_INTERLACED_ODD; /* sic */
}
else
{
tp->pData = src->img;
tp->Flags = PICTURE_INTERLACED_EVEN; /* sic */
}
tp->pData = ((uint8_t *) tp->pData)
+ (info.FrameWidth * capture_height * 2
* (DI_MAIN_HEIGHT_DIV - 1) / DI_MAIN_HEIGHT_DIV);
tp->IsFirstInSeries = (0 == field_count);
++field_count;
info.Overlay = dst0->img;
if (field_count >= (guint) method->nFieldsRequired)
{
if (method->pfnAlgorithm (&info))
n_frames = 1;
}
/* Second field. */
tp = info.PictureHistory[queue_len2 - 1];
p = PARENT (tp, picture, tpicture);
memmove (info.PictureHistory + 1,
info.PictureHistory + 0,
((G_N_ELEMENTS (info.PictureHistory) - 1)
* sizeof (info.PictureHistory[0])));
info.PictureHistory[0] = tp;
if (bottom_first)
{
tp->pData = src->img;
tp->Flags = PICTURE_INTERLACED_EVEN; /* sic */
}
else
{
tp->pData = ((uint8_t *) src->img) + info.FrameWidth * 2;
tp->Flags = PICTURE_INTERLACED_ODD; /* sic */
}
tp->pData = ((uint8_t *) tp->pData)
+ (info.FrameWidth * capture_height * 2
* (DI_MAIN_HEIGHT_DIV - 1) / DI_MAIN_HEIGHT_DIV);
tp->IsFirstInSeries = FALSE;
++field_count;
if (n_frames > 0)
info.Overlay = dst1->img;
if (field_count >= (guint) method->nFieldsRequired)
{
if (method->pfnAlgorithm (&info))
++n_frames;
}
return n_frames;
}
static gboolean
stop_thread (void)
{
zf_buffer *b;
guint i;
for (i = 0; i < G_N_ELEMENTS (pictures); ++i)
if (pictures[i].buffer)
{
zf_send_empty_buffer (&consumer, pictures[i].buffer);
pictures[i].buffer = NULL;
}
field_count = 0;
while ((b = zf_recv_full_buffer (&consumer)))
zf_send_empty_buffer (&consumer, b);
zf_rem_consumer (&consumer);
if (-1 != capture_format_id)
release_capture_format (capture_format_id);
capture_format_id = -1;
remove_display_filter (deinterlace);
return TRUE;
}
static gboolean
start_thread1 (void)
{
gchar *s;
capture_mode old_mode;
guint resolution;
const tv_video_standard *std;
guint display_height;
guint i;
if (0)
fprintf (stderr, "Start deinterlace thread\n");
s = NULL;
z_gconf_get_string (&s, GCONF_DIR "/method");
if (!s)
return FALSE;
method = deinterlace_find_method (s);
g_free (s);
s = NULL;
if (!method)
return FALSE;
assert (NULL != method->pfnAlgorithm);
s = NULL;
z_gconf_get_string (&s, GCONF_DIR "/resolution");
resolution = 4;
if (s)
{
gint i;
/* Error ignored. */
gconf_string_to_enum (resolution_enum, s, &i);
resolution = SATURATE (i, 1, 4);
}
if (0)
fprintf (stderr, "Method %s (%s), resolution %u\n",
_(method->szName), method->szName, resolution);
old_mode = tv_get_capture_mode (zapping->info);
if (CAPTURE_MODE_READ != old_mode)
if (-1 == zmisc_switch_mode (zapping->display_mode,
CAPTURE_MODE_READ,
zapping->info, /* warnings */ TRUE))
{
goto failure;
}
if ((std = tv_cur_video_standard (zapping->info)))
{
capture_width = (std->frame_width * resolution) >> 2;
capture_height = std->frame_height;
}
else
{
capture_width = (768 * resolution) >> 2;
capture_height = 576;
}
display_height = capture_height;
if (method->bIsHalfHeight)
display_height >>= 1;
if (!add_display_filter (deinterlace,
TV_PIXFMT_YUYV,
capture_width,
display_height / DI_MAIN_HEIGHT_DIV))
{
g_warning ("Cannot add display filter\n");
goto failure1;
}
capture_format_id = request_capture_format (zapping->info,
capture_width,
capture_height,
TV_PIXFMT_SET (TV_PIXFMT_YUYV),
REQ_SIZE | REQ_PIXFMT);
if (-1 == capture_format_id)
{
z_show_non_modal_message_dialog
(/* parent */ NULL,
GTK_MESSAGE_ERROR,
_("Could not start deinterlace plugin"),
_("Could not switch to capture format YUYV %u x %u."),
capture_width, capture_height);
goto failure2;
}
zf_add_consumer (&capture_fifo, &consumer);
CLEAR (info);
info.Version = DEINTERLACE_INFO_CURRENT_VERSION;
for (i = 0; i < MAX_PICTURE_HISTORY; ++i)
info.PictureHistory[i] = &pictures[i].tpicture;
info.Overlay = NULL;
info.OverlayPitch = capture_width * 2;
info.LineLength = capture_width * 2;
info.FrameWidth = capture_width;
info.FrameHeight = capture_height / DI_MAIN_HEIGHT_DIV;
info.FieldHeight = capture_height / 2 / DI_MAIN_HEIGHT_DIV;
info.CpuFeatureFlags = 0;
info.InputPitch = capture_width * 2 * 2;
info.pMemcpy = (void *) tv_memcpy;
/* XXX Not implemented yet. */
assert (!method->bNeedFieldDiff);
assert (!method->bNeedCombFactor);
queue_len2 = (method->nFieldsRequired + 1) & -2;
g_assert (queue_len2 <= MAX_PICTURE_HISTORY);
field_count = 0;
return TRUE;
failure2:
remove_display_filter (deinterlace);
failure1:
/* Error ignored. */
zmisc_switch_mode (zapping->display_mode,
old_mode, zapping->info, /* warnings */ FALSE);
failure:
return FALSE;
}
static void
plugin_capture_stop (void)
{
if (active)
stop_thread ();
active = FALSE;
capturing = FALSE;
}
static void
plugin_capture_start (void)
{
capturing = TRUE;
if (!active)
active = start_thread1 ();
}
static void
notify (GConfClient * client _unused_,
guint cnxn_id _unused_,
GConfEntry * entry _unused_,
gpointer user_data _unused_)
{
if (0)
fprintf(stderr, "Notify %d\n", capturing);
if (capturing)
{
if (active)
stop_thread ();
active = start_thread1 ();
}
}
static void
plugin_close (void)
{
}
static void
properties_add (GtkDialog * dialog)
{
static SidebarEntry se = {
.label = N_("Deinterlace"),
.icon_name = "interlace48.png",
.create = deinterlace_prefs_new,
.cancel = (void (*)(GtkWidget *)) deinterlace_prefs_cancel,
.help_link_id = "zapping-settings-deinterlace",
};
static const SidebarGroup sg = {
N_("Plugins"), &se, 1
};
standard_properties_add (dialog, &sg, 1, /* glade */ NULL);
}
extern int GreedyTestMode;
static gboolean
plugin_init (PluginBridge bridge _unused_,
tveng_device_info * info _unused_)
{
static const property_handler ph = {
.add = properties_add,
};
append_property_handler (&ph);
D();
#undef GET
#define GET(x, y) \
do { \
DEINTERLACE_METHOD *method = DI_##y##_GetDeinterlacePluginInfo (); \
/* NULL if we have no scalar of cpu_features optimized implem. */ \
deinterlace_methods[INDEX_##x] = method; \
} while (0)
GET (VIDEO_BOB, VideoBob);
GET (VIDEO_WEAVE, VideoWeave);
GET (VIDEO_2FRAME, TwoFrame);
GET (WEAVE, Weave);
GET (BOB, Bob);
GET (SCALER_BOB, ScalerBob);
GET (EVEN_ONLY, EvenOnly);
GET (ODD_ONLY, OddOnly);
/* No longer supported GET (BLENDED_CLIP, BlendedClip); */
/* To do GET (ADAPTIVE, Adaptive); */
GET (VIDEO_GREEDY, Greedy);
GET (VIDEO_GREEDY2FRAME, Greedy2Frame);
GET (VIDEO_GREEDYH, GreedyH);
/* To do - bNeedCombFactor
GET (OLD_GAME, OldGame); */
GET (VIDEO_TOMSMOCOMP, TomsMoComp);
GET (VIDEO_MOCOMP2, MoComp2);
D();
z_gconf_notify_add (GCONF_DIR "/method", notify, NULL);
z_gconf_notify_add (GCONF_DIR "/resolution", notify, NULL);
z_gconf_auto_update_bool (&reverse_fields, GCONF_DIR "/reverse_fields");
z_gconf_auto_update_bool (&GreedyTestMode,
GCONF_DIR "/options/Deinterlace/GreedyTestMode");
return TRUE;
}
static void
plugin_get_info (const gchar ** canonical_name,
const gchar ** descriptive_name,
const gchar ** description,
const gchar ** short_description,
const gchar ** author,
const gchar ** version)
{
if (canonical_name)
*canonical_name = "deinterlace";
if (descriptive_name)
*descriptive_name = N_("Deinterlace plugin");
if (description)
*description = "";
if (short_description)
*short_description = "";
if (author)
*author = "";
if (version)
*version = "1.0";
}
static struct plugin_misc_info *
plugin_get_misc_info (void)
{
static struct plugin_misc_info returned_struct = {
sizeof (struct plugin_misc_info),
6, /* plugin priority */
0 /* category */
};
return &returned_struct;
}
gboolean
plugin_get_symbol (const gchar * name,
gint hash,
gpointer * ptr)
{
static const struct plugin_exported_symbol symbols [] = {
SYMBOL (plugin_close, 0x1234),
SYMBOL (plugin_capture_stop, 0x1234),
SYMBOL (plugin_capture_start, 0x1234),
SYMBOL (plugin_get_info, 0x1234),
SYMBOL (plugin_get_misc_info, 0x1234),
SYMBOL (plugin_init, 0x1234),
};
guint i;
for (i = 0; i < N_ELEMENTS (symbols); ++i)
if (0 == strcmp (symbols[i].symbol, name))
{
if (symbols[i].hash != hash)
{
if (ptr)
*ptr = GINT_TO_POINTER(0x3); /* hash collision code */
g_warning("Check error: \"%s\" in plugin %s "
"has hash 0x%x vs. 0x%x",
name, "teletext", symbols[i].hash, hash);
return FALSE;
}
if (ptr)
*ptr = symbols[i].ptr;
return TRUE;
}
if (ptr)
*ptr = GINT_TO_POINTER(0x2);
return FALSE;
}
gint
plugin_get_protocol (void)
{
return PLUGIN_PROTOCOL;
}
|