1 /***************************************************************************/
2 /*** emotion xine display engine ***/
3 /***************************************************************************/
18 #include "emotion_private.h"
19 #include "emotion_xine.h"
22 #include <xine/video_out.h>
23 #include <xine/xine_internal.h>
24 #include <xine/xineutils.h>
25 #include <xine/vo_scale.h>
27 #define BLEND_BYTE(dst, src, o) (((src)*o + ((dst)*(0xf-o)))/0xf)
29 /***************************************************************************/
30 typedef struct _Emotion_Frame Emotion_Frame;
31 typedef struct _Emotion_Driver Emotion_Driver;
32 typedef struct _Emotion_Class Emotion_Class;
33 typedef struct _Emotion_Lut Emotion_Lut;
44 Emotion_Xine_Video_Frame frame;
45 unsigned char in_use : 1;
48 struct _Emotion_Driver
50 vo_driver_t vo_driver;
51 config_values_t *config;
54 Emotion_Xine_Video *ev;
59 video_driver_class_t driver_class;
60 config_values_t *config;
70 } __attribute__ ((packed));
72 typedef void (*done_func_type)(void *data);
74 /***************************************************************************/
75 static void *_emotion_class_init (xine_t *xine, void *visual);
76 static void _emotion_class_dispose (video_driver_class_t *driver_class);
77 static char *_emotion_class_identifier_get (video_driver_class_t *driver_class);
78 static char *_emotion_class_description_get (video_driver_class_t *driver_class);
80 static vo_driver_t *_emotion_open (video_driver_class_t *driver_class, const void *visual);
81 static void _emotion_dispose (vo_driver_t *vo_driver);
83 static int _emotion_redraw (vo_driver_t *vo_driver);
85 static uint32_t _emotion_capabilities_get (vo_driver_t *vo_driver);
86 static int _emotion_gui_data_exchange (vo_driver_t *vo_driver, int data_type, void *data);
88 static int _emotion_property_set (vo_driver_t *vo_driver, int property, int value);
89 static int _emotion_property_get (vo_driver_t *vo_driver, int property);
90 static void _emotion_property_min_max_get (vo_driver_t *vo_driver, int property, int *min, int *max);
92 static vo_frame_t *_emotion_frame_alloc (vo_driver_t *vo_driver);
93 static void _emotion_frame_dispose (vo_frame_t *vo_frame);
94 static void _emotion_frame_format_update (vo_driver_t *vo_driver, vo_frame_t *vo_frame, uint32_t width, uint32_t height, double ratio, int format, int flags);
95 static void _emotion_frame_display (vo_driver_t *vo_driver, vo_frame_t *vo_frame);
96 static void _emotion_frame_field (vo_frame_t *vo_frame, int which_field);
98 static void _emotion_frame_data_free (Emotion_Frame *fr);
99 static void _emotion_frame_data_unlock (Emotion_Frame *fr);
101 static void _emotion_overlay_begin (vo_driver_t *vo_driver, vo_frame_t *vo_frame, int changed);
102 static void _emotion_overlay_end (vo_driver_t *vo_driver, vo_frame_t *vo_frame);
103 static void _emotion_overlay_blend (vo_driver_t *vo_driver, vo_frame_t *vo_frame, vo_overlay_t *vo_overlay);
105 static void _emotion_overlay_mem_blend_8 (uint8_t *mem, uint8_t val, uint8_t o, size_t sz);
106 static void _emotion_overlay_blend_yuv (uint8_t *dst_base[3], vo_overlay_t * img_overl, int dst_width, int dst_height, int dst_pitches[3]);
108 static void _emotion_yuy2_to_bgra32 (int width, int height, unsigned char *src, unsigned char *dst);
110 /***************************************************************************/
111 static vo_info_t _emotion_info =
114 XINE_VISUAL_TYPE_NONE /* visual type */
117 plugin_info_t emotion_xine_plugin_info[] =
119 { PLUGIN_VIDEO_OUT, 21, "emotion", XINE_VERSION_CODE, &_emotion_info, _emotion_class_init },
120 { PLUGIN_NONE, 0, "", 0, NULL, NULL }
123 /***************************************************************************/
125 _emotion_class_init(xine_t *xine, void *visual __UNUSED__)
130 cl = (Emotion_Class *) malloc(sizeof(Emotion_Class));
131 if (!cl) return NULL;
132 cl->driver_class.open_plugin = _emotion_open;
133 cl->driver_class.get_identifier = _emotion_class_identifier_get;
134 cl->driver_class.get_description = _emotion_class_description_get;
135 cl->driver_class.dispose = _emotion_class_dispose;
136 cl->config = xine->config;
143 _emotion_class_dispose(video_driver_class_t *driver_class)
147 cl = (Emotion_Class *)driver_class;
152 _emotion_class_identifier_get(video_driver_class_t *driver_class __UNUSED__)
158 _emotion_class_description_get(video_driver_class_t *driver_class __UNUSED__)
160 return "Emotion xine video output plugin";
163 /***************************************************************************/
165 _emotion_open(video_driver_class_t *driver_class, const void *visual)
170 cl = (Emotion_Class *)driver_class;
171 /* visual here is the data ptr passed to xine_open_video_driver() */
173 dv = (Emotion_Driver *)malloc(sizeof(Emotion_Driver));
174 if (!dv) return NULL;
176 dv->config = cl->config;
178 dv->ratio = XINE_VO_ASPECT_AUTO;
179 dv->vo_driver.get_capabilities = _emotion_capabilities_get;
180 dv->vo_driver.alloc_frame = _emotion_frame_alloc;
181 dv->vo_driver.update_frame_format = _emotion_frame_format_update;
182 dv->vo_driver.overlay_begin = _emotion_overlay_begin;
183 dv->vo_driver.overlay_blend = _emotion_overlay_blend;
184 dv->vo_driver.overlay_end = _emotion_overlay_end;
185 dv->vo_driver.display_frame = _emotion_frame_display;
186 dv->vo_driver.get_property = _emotion_property_get;
187 dv->vo_driver.set_property = _emotion_property_set;
188 dv->vo_driver.get_property_min_max = _emotion_property_min_max_get;
189 dv->vo_driver.gui_data_exchange = _emotion_gui_data_exchange;
190 dv->vo_driver.dispose = _emotion_dispose;
191 dv->vo_driver.redraw_needed = _emotion_redraw;
192 dv->ev = (Emotion_Xine_Video *)visual;
194 DBG("vo_driver = %p", &dv->vo_driver);
195 return &dv->vo_driver;
199 _emotion_dispose(vo_driver_t *vo_driver)
203 dv = (Emotion_Driver *)vo_driver;
205 DBG("vo_driver = %p", dv);
209 /***************************************************************************/
211 _emotion_redraw(vo_driver_t *vo_driver __UNUSED__)
217 /***************************************************************************/
219 _emotion_capabilities_get(vo_driver_t *vo_driver __UNUSED__)
222 return VO_CAP_YV12 | VO_CAP_YUY2;
225 /***************************************************************************/
227 _emotion_gui_data_exchange(vo_driver_t *vo_driver __UNUSED__, int data_type, void *data __UNUSED__)
232 case XINE_GUI_SEND_COMPLETION_EVENT:
234 case XINE_GUI_SEND_DRAWABLE_CHANGED:
236 case XINE_GUI_SEND_EXPOSE_EVENT:
238 case XINE_GUI_SEND_TRANSLATE_GUI_TO_VIDEO:
240 case XINE_GUI_SEND_VIDEOWIN_VISIBLE:
242 case XINE_GUI_SEND_SELECT_VISUAL:
250 /***************************************************************************/
252 _emotion_property_set(vo_driver_t *vo_driver, int property, int value)
256 dv = (Emotion_Driver *)vo_driver;
260 case VO_PROP_ASPECT_RATIO:
261 if (value >= XINE_VO_ASPECT_NUM_RATIOS)
262 value = XINE_VO_ASPECT_AUTO;
263 // DBG("DRIVER RATIO SET %i!", value);
273 _emotion_property_get(vo_driver_t *vo_driver, int property)
277 dv = (Emotion_Driver *)vo_driver;
281 case VO_PROP_ASPECT_RATIO:
291 _emotion_property_min_max_get(vo_driver_t *vo_driver __UNUSED__, int property __UNUSED__, int *min, int *max)
298 /***************************************************************************/
300 _emotion_frame_alloc(vo_driver_t *vo_driver __UNUSED__)
305 fr = (Emotion_Frame *)calloc(1, sizeof(Emotion_Frame));
306 if (!fr) return NULL;
308 fr->vo_frame.base[0] = NULL;
309 fr->vo_frame.base[1] = NULL;
310 fr->vo_frame.base[2] = NULL;
312 fr->vo_frame.proc_slice = NULL;
313 fr->vo_frame.proc_frame = NULL;
314 fr->vo_frame.field = _emotion_frame_field;
315 fr->vo_frame.dispose = _emotion_frame_dispose;
316 fr->vo_frame.driver = vo_driver;
318 return (vo_frame_t *)fr;
322 _emotion_frame_dispose(vo_frame_t *vo_frame)
326 fr = (Emotion_Frame *)vo_frame;
328 _emotion_frame_data_free(fr);
333 _emotion_frame_format_update(vo_driver_t *vo_driver, vo_frame_t *vo_frame, uint32_t width, uint32_t height, double ratio, int format, int flags __UNUSED__)
338 dv = (Emotion_Driver *)vo_driver;
339 fr = (Emotion_Frame *)vo_frame;
341 if ((fr->width != (int)width) || (fr->height != (int)height) ||
342 (fr->format != format) || (!fr->vo_frame.base[0]))
345 _emotion_frame_data_free(fr);
353 case XINE_IMGFMT_YV12:
357 fr->frame.format = EMOTION_FORMAT_YV12;
358 fr->vo_frame.pitches[0] = 8 * ((width + 7) / 8);
359 fr->vo_frame.pitches[1] = 8 * ((width + 15) / 16);
360 fr->vo_frame.pitches[2] = 8 * ((width + 15) / 16);
362 y_size = fr->vo_frame.pitches[0] * height;
363 uv_size = fr->vo_frame.pitches[1] * ((height + 1) / 2);
365 fr->vo_frame.base[0] = malloc(y_size + (2 * uv_size));
366 fr->vo_frame.base[1] = fr->vo_frame.base[0] + y_size + uv_size;
367 fr->vo_frame.base[2] = fr->vo_frame.base[0] + y_size;
368 fr->frame.w = fr->width;
369 fr->frame.h = fr->height;
370 fr->frame.ratio = fr->vo_frame.ratio;
371 fr->frame.y = fr->vo_frame.base[0];
372 fr->frame.u = fr->vo_frame.base[1];
373 fr->frame.v = fr->vo_frame.base[2];
374 fr->frame.bgra_data = NULL;
375 fr->frame.y_stride = fr->vo_frame.pitches[0];
376 fr->frame.u_stride = fr->vo_frame.pitches[1];
377 fr->frame.v_stride = fr->vo_frame.pitches[2];
378 fr->frame.obj = dv->ev->obj;
381 case XINE_IMGFMT_YUY2:
383 fr->frame.format = EMOTION_FORMAT_BGRA;
384 fr->vo_frame.pitches[0] = 8 * ((width + 3) / 4);
385 fr->vo_frame.pitches[1] = 0;
386 fr->vo_frame.pitches[2] = 0;
388 fr->vo_frame.base[0] = malloc(fr->vo_frame.pitches[0] * height);
389 fr->vo_frame.base[1] = NULL;
390 fr->vo_frame.base[2] = NULL;
392 fr->frame.w = fr->width;
393 fr->frame.h = fr->height;
394 fr->frame.ratio = fr->vo_frame.ratio;
398 fr->frame.bgra_data = malloc(fr->width * fr->height * 4);
399 fr->frame.y_stride = 0;
400 fr->frame.u_stride = 0;
401 fr->frame.v_stride = 0;
402 fr->frame.obj = dv->ev->obj;
408 if (((format == XINE_IMGFMT_YV12)
409 && ((!fr->vo_frame.base[0])
410 || (!fr->vo_frame.base[1])
411 || (!fr->vo_frame.base[2])))
412 || ((format == XINE_IMGFMT_YUY2)
413 && ((!fr->vo_frame.base[0])
414 || (!fr->frame.bgra_data))))
416 _emotion_frame_data_free(fr);
419 fr->frame.ratio = fr->vo_frame.ratio;
424 _emotion_frame_display(vo_driver_t *vo_driver, vo_frame_t *vo_frame)
429 dv = (Emotion_Driver *)vo_driver;
430 fr = (Emotion_Frame *)vo_frame;
431 // DBG("fq %i %p", dv->ev->fq, dv->ev);
432 // if my frame queue is too deep ( > 4 frames) simply block and wait for them
434 // while (dv->ev->fq > 4) usleep(1);
439 if (dv->ev->closing) return;
440 if (fr->format == XINE_IMGFMT_YUY2)
442 _emotion_yuy2_to_bgra32(fr->width, fr->height, fr->vo_frame.base[0], fr->frame.bgra_data);
446 fr->frame.timestamp = (double)fr->vo_frame.vpts / 90000.0;
447 fr->frame.done_func = (done_func_type)_emotion_frame_data_unlock;
448 fr->frame.done_data = fr;
449 // DBG("FRAME FOR %p", dv->ev);
450 if (write(dv->ev->fd_write, &buf, sizeof(void *)) < 0) perror("write");
451 // DBG("-- FRAME DEC %p == %i", fr->frame.obj, ret);
455 /* hmm - must find a way to sanely copy data out... FIXME problem */
456 // fr->vo_frame.free(&fr->vo_frame);
460 _emotion_frame_field(vo_frame_t *vo_frame __UNUSED__, int which_field __UNUSED__)
465 /***************************************************************************/
467 _emotion_frame_data_free(Emotion_Frame *fr)
469 if (fr->vo_frame.base[0])
471 free(fr->vo_frame.base[0]);
472 fr->vo_frame.base[0] = NULL;
473 fr->vo_frame.base[1] = NULL;
474 fr->vo_frame.base[2] = NULL;
475 fr->frame.y = fr->vo_frame.base[0];
476 fr->frame.u = fr->vo_frame.base[1];
477 fr->frame.v = fr->vo_frame.base[2];
479 if (fr->frame.bgra_data)
481 free(fr->frame.bgra_data);
482 fr->frame.bgra_data = NULL;
487 _emotion_frame_data_unlock(Emotion_Frame *fr)
492 fr->vo_frame.free(&fr->vo_frame);
497 /***************************************************************************/
499 _emotion_overlay_begin(vo_driver_t *vo_driver __UNUSED__, vo_frame_t *vo_frame __UNUSED__, int changed __UNUSED__)
505 _emotion_overlay_end(vo_driver_t *vo_driver __UNUSED__, vo_frame_t *vo_frame __UNUSED__)
511 _emotion_overlay_blend(vo_driver_t *vo_driver __UNUSED__, vo_frame_t *vo_frame, vo_overlay_t *vo_overlay __UNUSED__)
515 fr = (Emotion_Frame *)vo_frame;
517 _emotion_overlay_blend_yuv(fr->vo_frame.base, vo_overlay,
518 fr->width, fr->height,
519 fr->vo_frame.pitches);
522 static void _emotion_overlay_mem_blend_8(uint8_t *mem, uint8_t val, uint8_t o, size_t sz)
524 uint8_t *limit = mem + sz;
527 *mem = BLEND_BYTE(*mem, val, o);
532 static void _emotion_overlay_blend_yuv(uint8_t *dst_base[3], vo_overlay_t * img_overl, int dst_width, int dst_height, int dst_pitches[3])
534 Emotion_Lut *my_clut;
539 rle_elem_t *rle_limit;
550 src_width = img_overl->width;
551 src_height = img_overl->height;
552 rle = img_overl->rle;
553 rle_limit = rle + img_overl->num_rle;
554 x_off = img_overl->x;
555 y_off = img_overl->y;
559 uint8_t *dst_y = dst_base[0] + dst_pitches[0] * y_off + x_off;
560 uint8_t *dst_cr = dst_base[2] + (y_off / 2) * dst_pitches[1] + (x_off / 2) + 1;
561 uint8_t *dst_cb = dst_base[1] + (y_off / 2) * dst_pitches[2] + (x_off / 2) + 1;
562 my_clut = (Emotion_Lut *) img_overl->hili_color;
563 my_trans = img_overl->hili_trans;
565 /* avoid wraping overlay if drawing to small image */
566 if( (x_off + img_overl->hili_right) < dst_width )
567 hili_right = img_overl->hili_right;
569 hili_right = dst_width - 1 - x_off;
571 /* avoid buffer overflow */
572 if( (src_height + y_off) >= dst_height )
573 src_height = dst_height - 1 - y_off;
575 rlelen=rle_remainder=0;
576 for (y = 0; y < src_height; y++)
578 ymask = ((img_overl->hili_top > y) || (img_overl->hili_bottom < y));
581 for (x = 0; x < src_width;)
587 rle_remainder = rlelen = rle->len;
591 if (rle_remainder == 0)
593 rle_remainder = rlelen;
595 if ((rle_remainder + x) > src_width)
597 /* Do something for long rlelengths */
598 rle_remainder = src_width - x;
603 if (x <= img_overl->hili_left)
605 /* Starts outside clip area */
606 if ((x + rle_remainder - 1) > img_overl->hili_left )
608 /* Cutting needed, starts outside, ends inside */
609 rle_this_bite = (img_overl->hili_left - x + 1);
610 rle_remainder -= rle_this_bite;
611 rlelen -= rle_this_bite;
612 my_clut = (Emotion_Lut *) img_overl->color;
613 my_trans = img_overl->trans;
618 /* no cutting needed, starts outside, ends outside */
619 rle_this_bite = rle_remainder;
621 rlelen -= rle_this_bite;
622 my_clut = (Emotion_Lut *) img_overl->color;
623 my_trans = img_overl->trans;
627 else if (x < hili_right)
629 /* Starts inside clip area */
630 if ((x + rle_remainder) > hili_right )
632 /* Cutting needed, starts inside, ends outside */
633 rle_this_bite = (hili_right - x);
634 rle_remainder -= rle_this_bite;
635 rlelen -= rle_this_bite;
636 my_clut = (Emotion_Lut *) img_overl->hili_color;
637 my_trans = img_overl->hili_trans;
642 /* no cutting needed, starts inside, ends inside */
643 rle_this_bite = rle_remainder;
645 rlelen -= rle_this_bite;
646 my_clut = (Emotion_Lut *) img_overl->hili_color;
647 my_trans = img_overl->hili_trans;
651 else if (x >= hili_right)
653 /* Starts outside clip area, ends outsite clip area */
654 if ((x + rle_remainder ) > src_width )
656 /* Cutting needed, starts outside, ends at right edge */
657 /* It should never reach here due to the earlier test of src_width */
658 rle_this_bite = (src_width - x );
659 rle_remainder -= rle_this_bite;
660 rlelen -= rle_this_bite;
661 my_clut = (Emotion_Lut *) img_overl->color;
662 my_trans = img_overl->trans;
667 /* no cutting needed, starts outside, ends outside */
668 rle_this_bite = rle_remainder;
670 rlelen -= rle_this_bite;
671 my_clut = (Emotion_Lut *) img_overl->color;
672 my_trans = img_overl->trans;
679 /* Outside clip are due to y */
680 /* no cutting needed, starts outside, ends outside */
681 rle_this_bite = rle_remainder;
683 rlelen -= rle_this_bite;
684 my_clut = (Emotion_Lut *) img_overl->color;
685 my_trans = img_overl->trans;
693 memset(dst_y + x, my_clut[clr].y, rle_this_bite);
696 memset(dst_cr + (x >> 1), my_clut[clr].cr, (rle_this_bite+1) >> 1);
697 memset(dst_cb + (x >> 1), my_clut[clr].cb, (rle_this_bite+1) >> 1);
702 _emotion_overlay_mem_blend_8(dst_y + x, my_clut[clr].y, o, rle_this_bite);
705 /* Blending cr and cb should use a different function, with pre -128 to each sample */
706 _emotion_overlay_mem_blend_8(dst_cr + (x >> 1), my_clut[clr].cr, o, (rle_this_bite+1) >> 1);
707 _emotion_overlay_mem_blend_8(dst_cb + (x >> 1), my_clut[clr].cb, o, (rle_this_bite+1) >> 1);
712 if (rle >= rle_limit)
717 if (rle >= rle_limit)
722 dst_y += dst_pitches[0];
726 dst_cr += dst_pitches[2];
727 dst_cb += dst_pitches[1];
732 //TODO: Really need to improve this converter!
733 #define LIMIT(x) ((x) > 0xff ? 0xff : ((x) < 0 ? 0 : (x)))
736 _emotion_yuy2_to_bgra32(int width, int height, unsigned char *src, unsigned char *dst)
739 unsigned char *y, *u, *v;
744 for (i = 0; i < width; i++)
746 for (j = 0; j < height; j++)
748 *dst++ = LIMIT(1.164 * (*y - 16) + 2.018 * (*u - 128));
749 *dst++ = LIMIT(1.164 * (*y - 16) - 0.813 * (*v - 128) - 0.391 * (*u - 128));
750 *dst++ = LIMIT(1.164 * (*y - 16) + 1.596 * (*v - 128));