Debian Codegraph @

plugins/actor/oinksie/actor_oinksie.c

raw
1 /* Libvisual-plugins - Standard plugins for libvisual
2 *
3 * Copyright (C) 2002, 2003, 2004, 2005, 2006 Dennis Smit <[email protected]>
4 *
5 * Authors: Dennis Smit <[email protected]>
6 *
7 * $Id: actor_oinksie.c,v 1.36 2006/01/27 20:19:17 synap Exp $
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24 #include <config.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <string.h>
31 #include <gettext.h>
32
33 #include "oinksie.h"
34
35 typedef struct {
36 OinksiePrivate priv1;
37 OinksiePrivate priv2;
38
39 int color_mode;
40
41 int depth;
42 uint8_t *tbuf1;
43 uint8_t *tbuf2;
44 uint8_t *buf1;
45 uint8_t *buf2;
46
47 VisVideoCustomCompositeFunc currentcomp;
48 } OinksiePrivContainer;
49
50 static int composite_blend1_32_c (VisVideo *dest, VisVideo *src);
51 static int composite_blend2_32_c (VisVideo *dest, VisVideo *src);
52 static int composite_blend3_32_c (VisVideo *dest, VisVideo *src);
53 static int composite_blend4_32_c (VisVideo *dest, VisVideo *src);
54 static int composite_blend5_32_c (VisVideo *dest, VisVideo *src);
55
56 int act_oinksie_init (VisPluginData *plugin);
57 int act_oinksie_cleanup (VisPluginData *plugin);
58 int act_oinksie_requisition (VisPluginData *plugin, int *width, int *height);
59 int act_oinksie_dimension (VisPluginData *plugin, VisVideo *video, int width, int height);
60 int act_oinksie_events (VisPluginData *plugin, VisEventQueue *events);
61 VisPalette *act_oinksie_palette (VisPluginData *plugin);
62 int act_oinksie_render (VisPluginData *plugin, VisVideo *video, VisAudio *audio);
63
64 VISUAL_PLUGIN_API_VERSION_VALIDATOR
65
66 const VisPluginInfo *get_plugin_info (int *count)
67 {
68 static VisActorPlugin actor[] = {{
69 .requisition = act_oinksie_requisition,
70 .palette = act_oinksie_palette,
71 .render = act_oinksie_render,
72 .vidoptions.depth =
73 VISUAL_VIDEO_DEPTH_8BIT |
74 VISUAL_VIDEO_DEPTH_32BIT
75 }};
76
77 static VisPluginInfo info[] = {{
78 .type = VISUAL_PLUGIN_TYPE_ACTOR,
79
80 .plugname = "oinksie",
81 .name = "oinksie plugin",
82 .author = "Dennis Smit <[email protected]>",
83 .version = "0.1",
84 .about = N_("Libvisual Oinksie visual plugin"),
85 .help = N_("This is the libvisual plugin for the Oinksie visual"),
86 .license = VISUAL_PLUGIN_LICENSE_LGPL,
87
88 .init = act_oinksie_init,
89 .cleanup = act_oinksie_cleanup,
90 .events = act_oinksie_events,
91
92 .plugin = VISUAL_OBJECT (&actor[0])
93 }};
94
95 *count = sizeof (info) / sizeof (*info);
96
97 return info;
98 }
99
100 int act_oinksie_init (VisPluginData *plugin)
101 {
102 OinksiePrivContainer *priv;
103 VisRandomContext *rcontext;
104 VisParamContainer *paramcontainer = visual_plugin_get_params (plugin);
105
106 static VisParamEntry params[] = {
107 VISUAL_PARAM_LIST_ENTRY_INTEGER ("color mode", 1),
108 VISUAL_PARAM_LIST_ENTRY_INTEGER ("acid palette", 0),
109 VISUAL_PARAM_LIST_END
110 };
111
112 static VisParamEntry cmodeparamchoices[] = {
113 VISUAL_PARAM_LIST_ENTRY_INTEGER ("Fair blended", 0),
114 VISUAL_PARAM_LIST_ENTRY_INTEGER ("Turbelent temperature", 1),
115 VISUAL_PARAM_LIST_ENTRY_INTEGER ("Acid summer", 2),
116 VISUAL_PARAM_LIST_ENTRY_INTEGER ("Perfect match", 3),
117 VISUAL_PARAM_LIST_ENTRY_INTEGER ("Sanity edge", 4),
118 VISUAL_PARAM_LIST_END
119 };
120
121 VisUIWidget *hbox;
122 VisUIWidget *label;
123 VisUIWidget *popup;
124
125 /* FIXME: add UI to access the acid palette parameter */
126
127 #if ENABLE_NLS
128 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
129 #endif
130
131 priv = visual_mem_new0 (OinksiePrivContainer, 1);
132 visual_object_set_private (VISUAL_OBJECT (plugin), priv);
133
134 visual_param_container_add_many (paramcontainer, params);
135
136 hbox = visual_ui_box_new (VISUAL_ORIENT_TYPE_HORIZONTAL);
137
138 label = visual_ui_label_new (_("Color mode:"), FALSE);
139
140 popup = visual_ui_popup_new ();
141 visual_ui_widget_set_tooltip (popup, _("The coloring method (only works when the plugin is in 32 bits mode)"));
142 visual_ui_mutator_set_param (VISUAL_UI_MUTATOR (popup), visual_param_container_get (paramcontainer, "color mode"));
143 visual_ui_choice_add_many (VISUAL_UI_CHOICE (popup), cmodeparamchoices);
144
145 visual_ui_box_pack (VISUAL_UI_BOX (hbox), label);
146 visual_ui_box_pack (VISUAL_UI_BOX (hbox), popup);
147
148 visual_plugin_set_userinterface (plugin, hbox);
149
150 visual_palette_allocate_colors (&priv->priv1.pal_cur, 256);
151 visual_palette_allocate_colors (&priv->priv1.pal_old, 256);
152
153 visual_palette_allocate_colors (&priv->priv2.pal_cur, 256);
154 visual_palette_allocate_colors (&priv->priv2.pal_old, 256);
155
156 rcontext = visual_plugin_get_random_context (plugin);
157 priv->priv1.rcontext = rcontext;
158 priv->priv2.rcontext = rcontext;
159
160 oinksie_init (&priv->priv1, 64, 64);
161 oinksie_init (&priv->priv2, 64, 64);
162
163 return 0;
164 }
165
166 int act_oinksie_cleanup (VisPluginData *plugin)
167 {
168 OinksiePrivContainer *priv = visual_object_get_private (VISUAL_OBJECT (plugin));
169
170 oinksie_quit (&priv->priv1);
171 oinksie_quit (&priv->priv2);
172
173 if (priv->depth != VISUAL_VIDEO_DEPTH_8BIT) {
174 if (priv->buf1)
175 visual_mem_free (priv->buf1);
176
177 if (priv->buf2)
178 visual_mem_free (priv->buf2);
179
180 if (priv->tbuf1)
181 visual_mem_free (priv->tbuf1);
182
183 if (priv->tbuf2)
184 visual_mem_free (priv->tbuf2);
185 }
186
187 visual_palette_free_colors (&priv->priv1.pal_cur);
188 visual_palette_free_colors (&priv->priv1.pal_old);
189
190 visual_palette_free_colors (&priv->priv2.pal_cur);
191 visual_palette_free_colors (&priv->priv2.pal_old);
192
193 visual_mem_free (priv);
194
195 return 0;
196 }
197
198 int act_oinksie_requisition (VisPluginData *plugin, int *width, int *height)
199 {
200 int reqw, reqh;
201
202 reqw = *width;
203 reqh = *height;
204
205 while (reqw % 4)
206 reqw--;
207
208 while (reqh % 4)
209 reqh--;
210
211 if (reqw < 32)
212 reqw = 32;
213
214 if (reqh < 32)
215 reqh = 32;
216
217 *width = reqw;
218 *height = reqh;
219
220 return 0;
221 }
222
223 int act_oinksie_dimension (VisPluginData *plugin, VisVideo *video, int width, int height)
224 {
225 OinksiePrivContainer *priv = visual_object_get_private (VISUAL_OBJECT (plugin));
226
227 visual_video_set_dimension (video, width, height);
228
229 oinksie_size_set (&priv->priv1, video->width, video->height);
230 oinksie_size_set (&priv->priv2, video->width, video->height);
231
232 priv->depth = video->depth;
233 if (priv->depth != VISUAL_VIDEO_DEPTH_8BIT) {
234 if (priv->buf1)
235 visual_mem_free (priv->buf1);
236
237 if (priv->buf2)
238 visual_mem_free (priv->buf2);
239
240 if (priv->tbuf1)
241 visual_mem_free (priv->tbuf1);
242
243 if (priv->tbuf2)
244 visual_mem_free (priv->tbuf2);
245
246 priv->buf1 = visual_mem_malloc0 (visual_video_get_size (video));
247 priv->buf2 = visual_mem_malloc0 (visual_video_get_size (video));
248 priv->tbuf1 = visual_mem_malloc0 (visual_video_get_size (video));
249 priv->tbuf2 = visual_mem_malloc0 (visual_video_get_size (video));
250 }
251
252 return 0;
253 }
254
255 int act_oinksie_events (VisPluginData *plugin, VisEventQueue *events)
256 {
257 OinksiePrivContainer *priv = visual_object_get_private (VISUAL_OBJECT (plugin));
258 VisEvent ev;
259 VisParamEntry *param;
260
261 while (visual_event_queue_poll (events, &ev)) {
262 switch (ev.type) {
263 case VISUAL_EVENT_RESIZE:
264 act_oinksie_dimension (plugin, ev.event.resize.video,
265 ev.event.resize.width, ev.event.resize.height);
266
267 break;
268
269 case VISUAL_EVENT_PARAM:
270 param = ev.event.param.param;
271
272 if (visual_param_entry_is (param, "color mode")) {
273 priv->color_mode = visual_param_entry_get_integer (param);
274
275 if (priv->color_mode == 0)
276 priv->currentcomp = composite_blend1_32_c;
277 else if (priv->color_mode == 1)
278 priv->currentcomp = composite_blend2_32_c;
279 else if (priv->color_mode == 2)
280 priv->currentcomp = composite_blend3_32_c;
281 else if (priv->color_mode == 3)
282 priv->currentcomp = composite_blend4_32_c;
283 else if (priv->color_mode == 4)
284 priv->currentcomp = composite_blend5_32_c;
285 else
286 priv->currentcomp = composite_blend2_32_c;
287 } else if (visual_param_entry_is (param, "acid palette")) {
288 priv->priv1.config.acidpalette = visual_param_entry_get_integer (param);
289 }
290
291 break;
292
293 default: /* to avoid warnings */
294 break;
295 }
296 }
297
298 return 0;
299 }
300
301 VisPalette *act_oinksie_palette (VisPluginData *plugin)
302 {
303 OinksiePrivContainer *priv = visual_object_get_private (VISUAL_OBJECT (plugin));
304 VisPalette *pal;
305
306 pal = oinksie_palette_get (&priv->priv1);
307
308 return pal;
309 }
310
311 int act_oinksie_render (VisPluginData *plugin, VisVideo *video, VisAudio *audio)
312 {
313 OinksiePrivContainer *priv = visual_object_get_private (VISUAL_OBJECT (plugin));
314 VisBuffer pcmbuf1;
315 VisBuffer pcmbuf2;
316 VisBuffer pcmmix;
317 VisBuffer spmbuf;
318 int i = 0;
319
320 /* Left audio */
321 visual_buffer_set_data_pair (&pcmbuf1, priv->priv1.audio.pcm[0], sizeof (float) * 4096);
322 visual_audio_get_sample (audio, &pcmbuf1, VISUAL_AUDIO_CHANNEL_LEFT);
323
324 visual_buffer_set_data_pair (&spmbuf, &priv->priv1.audio.freq[0], sizeof (float) * 256);
325 visual_audio_get_spectrum_for_sample (&spmbuf, &pcmbuf1, FALSE);
326
327 /* Right audio */
328 visual_buffer_set_data_pair (&pcmbuf2, priv->priv1.audio.pcm[1], sizeof (float) * 4096);
329 visual_audio_get_sample (audio, &pcmbuf2, VISUAL_AUDIO_CHANNEL_RIGHT);
330
331 visual_buffer_set_data_pair (&spmbuf, priv->priv1.audio.freq[1], sizeof (float) * 256);
332 visual_audio_get_spectrum_for_sample (&spmbuf, &pcmbuf2, FALSE);
333
334 /* Mix channels */
335 visual_buffer_set_data_pair (&pcmmix, priv->priv1.audio.pcm[2], sizeof (float) * 4096);
336 visual_audio_sample_buffer_mix_many (&pcmmix, TRUE, 2, &pcmbuf1, &pcmbuf2, 1.0, 1.0);
337
338 visual_buffer_set_data_pair (&spmbuf, priv->priv1.audio.freqsmall, sizeof (float) * 4);
339 visual_audio_get_spectrum_for_sample (&spmbuf, &pcmmix, FALSE);
340
341 /* Duplicate for second oinksie instance */
342 visual_mem_copy (&priv->priv2.audio.pcm, &priv->priv1.audio.pcm, sizeof (float) * 4096 * 3);
343 visual_mem_copy (&priv->priv2.audio.freq, &priv->priv1.audio.freq, sizeof (float) * 256 * 2);
344 visual_mem_copy (&priv->priv2.audio.freqsmall, &priv->priv1.audio.freqsmall, sizeof (float) * 4);
345
346 /* Audio energy */
347 priv->priv1.audio.energy = audio->energy;
348 priv->priv2.audio.energy = audio->energy;
349
350 /* Let's get rendering */
351 if (priv->depth == VISUAL_VIDEO_DEPTH_8BIT) {
352 oinksie_sample (&priv->priv1);
353
354 /* FIXME this is not pitch safe, will screw up region buffers.
355 * pass on the VisVideo to the plugin, and be pitch safe.
356 */
357 priv->priv1.drawbuf = visual_video_get_pixels (video);
358 oinksie_render (&priv->priv1);
359 } else {
360 VisVideo vid1;
361 VisVideo vid2;
362
363 visual_video_init (&vid1);
364 visual_video_init (&vid2);
365
366 oinksie_sample (&priv->priv1);
367 oinksie_sample (&priv->priv2);
368
369 priv->priv1.drawbuf = priv->buf1;
370 priv->priv2.drawbuf = priv->buf2;
371
372 oinksie_render (&priv->priv1);
373 oinksie_render (&priv->priv2);
374
375 visual_video_set_depth (&vid1, VISUAL_VIDEO_DEPTH_8BIT);
376 visual_video_set_dimension (&vid1, video->width, video->height);
377 visual_video_set_buffer (&vid1, priv->buf1);
378 visual_video_set_palette (&vid1, oinksie_palette_get (&priv->priv1));
379
380 visual_video_blit_overlay (video, &vid1, 0, 0, FALSE);
381
382 visual_video_set_depth (&vid2, VISUAL_VIDEO_DEPTH_8BIT);
383 visual_video_set_dimension (&vid2, video->width, video->height);
384 visual_video_set_buffer (&vid2, priv->buf2);
385 visual_video_set_palette (&vid2, oinksie_palette_get (&priv->priv2));
386
387 visual_video_composite_set_type (&vid2, VISUAL_VIDEO_COMPOSITE_TYPE_CUSTOM);
388 visual_video_composite_set_function (&vid2, priv->currentcomp);
389
390 visual_video_blit_overlay (video, &vid2, 0, 0, TRUE);
391
392 visual_object_unref (VISUAL_OBJECT (&vid1));
393 visual_object_unref (VISUAL_OBJECT (&vid2));
394 }
395
396 return 0;
397 }
398
399 static int composite_blend1_32_c (VisVideo *dest, VisVideo *src)
400 {
401 int i, j;
402 uint8_t *destbuf = visual_video_get_pixels (dest);
403 uint8_t *srcbuf = visual_video_get_pixels (src);
404 uint8_t alpha = 128;
405
406 for (i = 0; i < src->height; i++) {
407 for (j = 0; j < src->width; j++) {
408 *destbuf = ((alpha * (*destbuf - *srcbuf) >> 8) + *srcbuf);
409 *(destbuf + 1) = ((alpha * (*(destbuf + 1) - *(srcbuf + 1)) >> 8) + *(srcbuf + 1));
410 *(destbuf + 2) = ((alpha * (*(destbuf + 2) - *(srcbuf + 2)) >> 8) + *(srcbuf + 2));
411
412 destbuf += 4;
413 srcbuf += 4;
414 }
415
416 destbuf += dest->pitch - (dest->width * dest->bpp);
417 srcbuf += src->pitch - (src->width * src->bpp);
418 }
419
420 return VISUAL_OK;
421 }
422
423 static int composite_blend2_32_c (VisVideo *dest, VisVideo *src)
424 {
425 int i, j;
426 uint8_t *destbuf = visual_video_get_pixels (dest);
427 uint8_t *srcbuf = visual_video_get_pixels (src);
428 uint8_t alpha = 128;
429
430 for (i = 0; i < src->height; i++) {
431 for (j = 0; j < src->width; j++) {
432 *destbuf = ((*destbuf * (*destbuf - *srcbuf) >> 8) + *srcbuf);
433 *(destbuf + 1) = ((alpha * (*(destbuf + 1) - *(srcbuf + 1)) >> 8) + *(srcbuf + 1));
434 *(destbuf + 2) = ((0 * (*(destbuf + 2) - *(srcbuf + 2)) >> 8) + *(srcbuf + 2));
435
436 destbuf += 4;
437 srcbuf += 4;
438 }
439
440 destbuf += dest->pitch - (dest->width * dest->bpp);
441 srcbuf += src->pitch - (src->width * src->bpp);
442 }
443
444 return VISUAL_OK;
445 }
446
447 static int composite_blend3_32_c (VisVideo *dest, VisVideo *src)
448 {
449 int i, j;
450 uint8_t *destbuf = visual_video_get_pixels (dest);
451 uint8_t *srcbuf = visual_video_get_pixels (src);
452 uint8_t alpha = 128;
453
454 for (i = 0; i < src->height; i++) {
455 for (j = 0; j < src->width; j++) {
456 *destbuf = ((0 * (*destbuf - *srcbuf) >> 8) + *srcbuf);
457 *(destbuf + 1) = ((alpha * (*(destbuf + 1) - *(srcbuf + 1)) >> 8) + *(srcbuf + 1));
458 *(destbuf + 2) = ((*destbuf * (*(destbuf + 2) - *(srcbuf + 2)) >> 8) + *(srcbuf + 2));
459
460 destbuf += 4;
461 srcbuf += 4;
462 }
463
464 destbuf += dest->pitch - (dest->width * dest->bpp);
465 srcbuf += src->pitch - (src->width * src->bpp);
466 }
467
468 return VISUAL_OK;
469 }
470
471 static int composite_blend4_32_c (VisVideo *dest, VisVideo *src)
472 {
473 int i, j;
474 uint8_t *destbuf = visual_video_get_pixels (dest);
475 uint8_t *srcbuf = visual_video_get_pixels (src);
476 uint8_t alpha = 128;
477
478 for (i = 0; i < src->height; i++) {
479 for (j = 0; j < src->width; j++) {
480 *destbuf = ((*destbuf * (*destbuf - *srcbuf) >> 8) + *srcbuf);
481 *(destbuf + 1) = ((alpha * (*(destbuf + 1) - *(srcbuf + 1)) >> 8) + *(srcbuf + 1));
482 *(destbuf + 2) = ((*srcbuf * (*(destbuf + 2) - *(srcbuf + 2)) >> 8) + *(srcbuf + 2));
483
484 destbuf += 4;
485 srcbuf += 4;
486 }
487
488 destbuf += dest->pitch - (dest->width * dest->bpp);
489 srcbuf += src->pitch - (src->width * src->bpp);
490 }
491
492 return VISUAL_OK;
493 }
494
495 static int composite_blend5_32_c (VisVideo *dest, VisVideo *src)
496 {
497 int i, j;
498 uint8_t *destbuf = visual_video_get_pixels (dest);
499 uint8_t *srcbuf = visual_video_get_pixels (src);
500 uint8_t alpha = 128;
501
502 for (i = 0; i < src->height; i++) {
503 for (j = 0; j < src->width; j++) {
504 *destbuf = ((*destbuf * (*destbuf - *srcbuf) >> 8) + *srcbuf);
505 *(destbuf + 1) = ((*srcbuf * (*(destbuf + 1) - *(srcbuf + 1)) >> 8) + *(srcbuf + 1));
506 *(destbuf + 2) = ((*destbuf * (*(destbuf + 2) - *(srcbuf + 2)) >> 8) + *(srcbuf + 2));
507
508 destbuf += 4;
509 srcbuf += 4;
510 }
511
512 destbuf += dest->pitch - (dest->width * dest->bpp);
513 srcbuf += src->pitch - (src->width * src->bpp);
514 }
515
516 return VISUAL_OK;
517 }
518
519 /* FIXME make a color mode out of this one as well (yeah I was fooling around with mmx) */
520 #if 0
521 static int alpha_blend3_32_mmx (uint8_t *dest, uint8_t *src1, uint8_t *src2, int size, float alpha)
522 {
523 uint32_t ialpha = (alpha * 255);
524 int i;
525
526 /* Reset some regs */
527 __asm __volatile
528 ("\n\t emms"
529 "\n\t pxor %%mm6, %%mm6"
530 "\n\t pxor %%mm7, %%mm7"
531 ::: "mm6", "mm7");
532
533 for (i = 0; i < size; i++) {
534 __asm __volatile
535 ("\n\t movd %[spix1], %%mm0"
536 "\n\t movd %[spix2], %%mm1"
537 "\n\t movq %%mm0, %%mm3" // [ 2 pixels ]
538 "\n\t movd %[alpha], %%mm2" // [ alpha ]
539 "\n\t psllq $24, %%mm3"
540 "\n\t movq %%mm0, %%mm4"
541 "\n\t psrld $24, %%mm3"
542 "\n\t psrld $24, %%mm4"
543 "\n\t psllq $32, %%mm2"
544 "\n\t psllq $16, %%mm3"
545 "\n\t por %%mm4, %%mm2"
546 "\n\t punpcklbw %%mm6, %%mm0" /* interleaving source 1 */
547 "\n\t por %%mm3, %%mm2"
548 "\n\t punpcklbw %%mm6, %%mm1" /* interleaving source 2 */
549 "\n\t paddsw %%mm7, %%mm2"
550 "\n\t psubsw %%mm1, %%mm0" /* (src - dest) part */
551 "\n\t pxor %%mm3, %%mm3"
552 "\n\t pmullw %%mm2, %%mm0" /* alpha * (src - dest) */
553 "\n\t pxor %%mm4, %%mm4"
554 "\n\t psrlw $8, %%mm0" /* / 256 */
555 "\n\t pxor %%mm2, %%mm2"
556 "\n\t paddb %%mm1, %%mm0" /* + dest */
557 "\n\t packuswb %%mm0, %%mm0"
558 "\n\t pxor %%mm1, %%mm1"
559 "\n\t movd %%mm0, %[dest]"
560 : [dest] "=m" (*dest)
561 : [spix1] "m" (*src1)
562 , [spix2] "m" (*src2)
563 , [alpha] "m" (ialpha)
564 : "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7");
565
566 dest += 4;
567 src1 += 4;
568 src2 += 4;
569 }
570
571 __asm __volatile
572 ("\n\t emms");
573
574 return 0;
575 }
576
577 #endif