Commit 25d874ec authored by David Schleef's avatar David Schleef
Browse files

gst/videoscale/: Add a 4-tap image scaler. Theoretically looks much prettier.

Original commit message from CVS:
* gst/videoscale/Makefile.am:
* gst/videoscale/gstvideoscale.c:
* gst/videoscale/gstvideoscale.h:
* gst/videoscale/vs_4tap.c:
* gst/videoscale/vs_4tap.h:
* gst/videoscale/vs_image.c:
* gst/videoscale/vs_image.h:
* gst/videoscale/vs_scanline.c:
* gst/videoscale/vs_scanline.h:
Add a 4-tap image scaler.  Theoretically looks much prettier.
The tap calculation could use some improvement.
parent d24ad6ac
2006-11-14 David Schleef <ds@schleef.org>
* gst/videoscale/Makefile.am:
* gst/videoscale/gstvideoscale.c:
* gst/videoscale/gstvideoscale.h:
* gst/videoscale/vs_4tap.c:
* gst/videoscale/vs_4tap.h:
* gst/videoscale/vs_image.c:
* gst/videoscale/vs_image.h:
* gst/videoscale/vs_scanline.c:
* gst/videoscale/vs_scanline.h:
Add a 4-tap image scaler. Theoretically looks much prettier.
The tap calculation could use some improvement.
2006-11-14 Wim Taymans <wim@fluendo.com>
 
Patch by: Jan David Mol <j dot j dot d dot mol at tudelft dot nl>
......@@ -3,7 +3,8 @@ plugin_LTLIBRARIES = libgstvideoscale.la
libgstvideoscale_la_SOURCES = \
gstvideoscale.c \
vs_image.c \
vs_scanline.c
vs_scanline.c \
vs_4tap.c
libgstvideoscale_la_CFLAGS = $(GST_CFLAGS) $(GST_BASE_CFLAGS) $(LIBOIL_CFLAGS)
libgstvideoscale_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
......@@ -14,4 +15,5 @@ libgstvideoscale_la_LIBADD = \
noinst_HEADERS = \
gstvideoscale.h \
vs_image.h \
vs_scanline.h
vs_scanline.h \
vs_4tap.h
......@@ -67,6 +67,7 @@
#include "gstvideoscale.h"
#include "vs_image.h"
#include "vs_4tap.h"
/* debug variable definition */
......@@ -141,6 +142,7 @@ gst_video_scale_method_get_type (void)
static const GEnumValue video_scale_methods[] = {
{GST_VIDEO_SCALE_NEAREST, "Nearest Neighbour", "nearest-neighbour"},
{GST_VIDEO_SCALE_BILINEAR, "Bilinear", "bilinear"},
{GST_VIDEO_SCALE_4TAP, "4-tap", "4-tap"},
{0, NULL, NULL},
};
......@@ -513,7 +515,7 @@ gst_video_scale_set_caps (GstBaseTransform * trans, GstCaps * in, GstCaps * out)
if (videoscale->tmp_buf)
g_free (videoscale->tmp_buf);
videoscale->tmp_buf = g_malloc (videoscale->dest.stride * 2);
videoscale->tmp_buf = g_malloc (videoscale->dest.stride * 4);
/* FIXME: par */
GST_DEBUG_OBJECT (videoscale, "from=%dx%d, size %d -> to=%dx%d, size %d",
......@@ -681,7 +683,7 @@ gst_video_scale_prepare_image (gint format, GstBuffer * buf,
img_u->pixels = img->pixels + GST_ROUND_UP_2 (img->height) * img->stride;
img_u->height = GST_ROUND_UP_2 (img->height) / 2;
img_u->width = GST_ROUND_UP_2 (img->width) / 2;
img_u->stride = GST_ROUND_UP_4 (img->stride / 2);
img_u->stride = GST_ROUND_UP_4 (img_u->width);
memcpy (img_v, img_u, sizeof (*img_v));
img_v->pixels = img_u->pixels + img_u->height * img_u->stride;
break;
......@@ -789,8 +791,6 @@ gst_video_scale_transform (GstBaseTransform * trans, GstBuffer * in,
case GST_VIDEO_SCALE_I420:
case GST_VIDEO_SCALE_YV12:
vs_image_scale_linear_Y (dest, src, videoscale->tmp_buf);
//memset (dest_u.pixels, 128, dest_u.stride * dest_u.height);
//memset (dest_v.pixels, 128, dest_v.stride * dest_v.height);
vs_image_scale_linear_Y (&dest_u, &src_u, videoscale->tmp_buf);
vs_image_scale_linear_Y (&dest_v, &src_v, videoscale->tmp_buf);
break;
......@@ -804,6 +804,18 @@ gst_video_scale_transform (GstBaseTransform * trans, GstBuffer * in,
goto unsupported;
}
break;
case GST_VIDEO_SCALE_4TAP:
switch (videoscale->format) {
case GST_VIDEO_SCALE_I420:
case GST_VIDEO_SCALE_YV12:
vs_image_scale_4tap_Y (dest, src, videoscale->tmp_buf);
vs_image_scale_4tap_Y (&dest_u, &src_u, videoscale->tmp_buf);
vs_image_scale_4tap_Y (&dest_v, &src_v, videoscale->tmp_buf);
break;
default:
goto unsupported;
}
break;
default:
goto unknown_mode;
}
......@@ -880,6 +892,8 @@ plugin_init (GstPlugin * plugin)
GST_DEBUG_CATEGORY_INIT (video_scale_debug, "videoscale", 0,
"videoscale element");
vs_4tap_init ();
return TRUE;
}
......
......@@ -45,12 +45,14 @@ GST_DEBUG_CATEGORY_EXTERN (video_scale_debug);
* GstVideoScaleMethod:
* @GST_VIDEO_SCALE_NEAREST: use nearest neighbour scaling (fast and ugly)
* @GST_VIDEO_SCALE_BILINEAR: use bilinear scaling (slower but prettier).
* @GST_VIDEO_SCALE_4TAP: use a 4-tap filter for scaling (slow).
*
* The videoscale method to use.
*/
typedef enum {
GST_VIDEO_SCALE_NEAREST,
GST_VIDEO_SCALE_BILINEAR,
GST_VIDEO_SCALE_4TAP
} GstVideoScaleMethod;
typedef struct _GstVideoScale GstVideoScale;
......
/*
* Image Scaling Functions (4 tap)
* Copyright (c) 2005 David A. Schleef <ds@schleef.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "vs_image.h"
#include "vs_scanline.h"
#include <liboil/liboil.h>
#include <math.h>
#define SHIFT 10
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#define CLAMP(x,a,b) MAX(MIN((x),(b)),(a))
int16_t vs_4tap_taps[256][4];
double
vs_4tap_func (double x)
{
#if 0
if (x < -1)
return 0;
if (x > 1)
return 0;
if (x < 0)
return 1 + x;
return 1 - x;
#endif
#if 0
if (x == 0)
return 1;
return sin (M_PI * x) / (M_PI * x) * (1 - 0.25 * x * x);
#endif
#if 1
if (x == 0)
return 1;
return sin (M_PI * x) / (M_PI * x);
#endif
}
void
vs_4tap_init (void)
{
int i;
double a, b, c, d;
double sum;
for (i = 0; i < 256; i++) {
a = vs_4tap_func (-1 - i / 256.0);
b = vs_4tap_func (0 - i / 256.0);
c = vs_4tap_func (1 - i / 256.0);
d = vs_4tap_func (2 - i / 256.0);
sum = a + b + c + d;
vs_4tap_taps[i][0] = rint ((1 << SHIFT) * (a / sum));
vs_4tap_taps[i][1] = rint ((1 << SHIFT) * (b / sum));
vs_4tap_taps[i][2] = rint ((1 << SHIFT) * (c / sum));
vs_4tap_taps[i][3] = rint ((1 << SHIFT) * (d / sum));
}
}
void
vs_scanline_resample_4tap_Y (uint8_t * dest, uint8_t * src,
int n, int *xacc, int increment)
{
int i;
int j;
int acc;
int x;
int y;
acc = *xacc;
for (i = 0; i < n; i++) {
j = acc >> 16;
x = (acc & 0xff00) >> 8;
y = (vs_4tap_taps[x][0] * src[MAX (j - 1, 0)] +
vs_4tap_taps[x][1] * src[j] +
vs_4tap_taps[x][2] * src[j + 1] +
vs_4tap_taps[x][3] * src[j + 2] + (1 << (SHIFT - 1))) >> SHIFT;
if (y < 0)
y = 0;
if (y > 255)
y = 255;
dest[i] = y;
acc += increment;
}
*xacc = acc;
}
void
vs_scanline_merge_4tap_Y (uint8_t * dest, uint8_t * src1, uint8_t * src2,
uint8_t * src3, uint8_t * src4, int n, int acc)
{
int i;
int y;
acc = (acc >> 8) & 0xff;
for (i = 0; i < n; i++) {
y = (vs_4tap_taps[acc][0] * src1[i] +
vs_4tap_taps[acc][1] * src2[i] +
vs_4tap_taps[acc][2] * src3[i] +
vs_4tap_taps[acc][3] * src4[i] + (1 << (SHIFT - 1))) >> SHIFT;
if (y < 0)
y = 0;
if (y > 255)
y = 255;
dest[i] = y;
}
}
void
vs_image_scale_4tap_Y (const VSImage * dest, const VSImage * src,
uint8_t * tmpbuf)
{
int yacc;
int y_increment;
int x_increment;
int i;
int j;
int xacc;
int k;
y_increment = ((src->height - 1) << 16) / (dest->height - 1);
x_increment = ((src->width - 1) << 16) / (dest->width - 1);
k = 0;
for (i = 0; i < 4; i++) {
xacc = 0;
vs_scanline_resample_4tap_Y (tmpbuf + i * dest->width,
src->pixels + i * src->stride, dest->width, &xacc, x_increment);
}
yacc = 0;
for (i = 0; i < dest->height; i++) {
uint8_t *t0, *t1, *t2, *t3;
j = yacc >> 16;
if (j > k) {
k++;
if (k + 3 < src->height) {
xacc = 0;
vs_scanline_resample_4tap_Y (tmpbuf + ((k + 3) & 3) * dest->width,
src->pixels + (k + 3) * src->stride,
dest->width, &xacc, x_increment);
}
}
t0 = tmpbuf + (CLAMP (j - 1, 0, src->height - 1) & 3) * dest->width;
t1 = tmpbuf + (CLAMP (j, 0, src->height - 1) & 3) * dest->width;
t2 = tmpbuf + (CLAMP (j + 1, 0, src->height - 1) & 3) * dest->width;
t3 = tmpbuf + (CLAMP (j + 2, 0, src->height - 1) & 3) * dest->width;
vs_scanline_merge_4tap_Y (dest->pixels + i * dest->stride,
t0, t1, t2, t3, dest->width, yacc & 0xffff);
yacc += y_increment;
}
}
/*
* Image Scaling Functions (4 tap)
* Copyright (c) 2005 David A. Schleef <ds@schleef.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _VS_4TAP_H_
#define _VS_4TAP_H_
#include "vs_image.h"
#include <liboil/liboil.h>
void vs_4tap_init (void);
void vs_scanline_resample_4tap_Y (uint8_t *dest, uint8_t *src,
int n, int *xacc, int increment);
void vs_scanline_merge_4tap_Y (uint8_t *dest, uint8_t *src1, uint8_t *src2,
uint8_t *src3, uint8_t *src4, int n, int acc);
void vs_image_scale_4tap_Y (const VSImage * dest, const VSImage * src,
uint8_t * tmpbuf);
#endif
......@@ -25,7 +25,6 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <glib.h>
#include <string.h>
#include "vs_scanline.h"
......@@ -37,7 +36,7 @@
void
vs_image_scale_nearest_RGBA (const VSImage * dest, const VSImage * src,
guint8 * tmpbuf)
uint8_t * tmpbuf)
{
int acc;
int y_increment;
......@@ -65,13 +64,13 @@ vs_image_scale_nearest_RGBA (const VSImage * dest, const VSImage * src,
void
vs_image_scale_linear_RGBA (const VSImage * dest, const VSImage * src,
guint8 * tmpbuf)
uint8_t * tmpbuf)
{
int acc;
int y_increment;
int x_increment;
guint8 *tmp1;
guint8 *tmp2;
uint8_t *tmp1;
uint8_t *tmp2;
int y1;
int y2;
int i;
......@@ -153,7 +152,7 @@ vs_image_scale_linear_RGBA (const VSImage * dest, const VSImage * src,
void
vs_image_scale_nearest_RGB (const VSImage * dest, const VSImage * src,
guint8 * tmpbuf)
uint8_t * tmpbuf)
{
int acc;
int y_increment;
......@@ -181,13 +180,13 @@ vs_image_scale_nearest_RGB (const VSImage * dest, const VSImage * src,
void
vs_image_scale_linear_RGB (const VSImage * dest, const VSImage * src,
guint8 * tmpbuf)
uint8_t * tmpbuf)
{
int acc;
int y_increment;
int x_increment;
guint8 *tmp1;
guint8 *tmp2;
uint8_t *tmp1;
uint8_t *tmp2;
int y1;
int y2;
int i;
......@@ -270,7 +269,7 @@ vs_image_scale_linear_RGB (const VSImage * dest, const VSImage * src,
void
vs_image_scale_nearest_YUYV (const VSImage * dest, const VSImage * src,
guint8 * tmpbuf)
uint8_t * tmpbuf)
{
int acc;
int y_increment;
......@@ -300,13 +299,13 @@ vs_image_scale_nearest_YUYV (const VSImage * dest, const VSImage * src,
void
vs_image_scale_linear_YUYV (const VSImage * dest, const VSImage * src,
guint8 * tmpbuf)
uint8_t * tmpbuf)
{
int acc;
int y_increment;
int x_increment;
guint8 *tmp1;
guint8 *tmp2;
uint8_t *tmp1;
uint8_t *tmp2;
int y1;
int y2;
int i;
......@@ -388,7 +387,7 @@ vs_image_scale_linear_YUYV (const VSImage * dest, const VSImage * src,
void
vs_image_scale_nearest_UYVY (const VSImage * dest, const VSImage * src,
guint8 * tmpbuf)
uint8_t * tmpbuf)
{
int acc;
int y_increment;
......@@ -418,13 +417,13 @@ vs_image_scale_nearest_UYVY (const VSImage * dest, const VSImage * src,
void
vs_image_scale_linear_UYVY (const VSImage * dest, const VSImage * src,
guint8 * tmpbuf)
uint8_t * tmpbuf)
{
int acc;
int y_increment;
int x_increment;
guint8 *tmp1;
guint8 *tmp2;
uint8_t *tmp1;
uint8_t *tmp2;
int y1;
int y2;
int i;
......@@ -506,7 +505,7 @@ vs_image_scale_linear_UYVY (const VSImage * dest, const VSImage * src,
void
vs_image_scale_nearest_Y (const VSImage * dest, const VSImage * src,
guint8 * tmpbuf)
uint8_t * tmpbuf)
{
int acc;
int y_increment;
......@@ -534,13 +533,13 @@ vs_image_scale_nearest_Y (const VSImage * dest, const VSImage * src,
void
vs_image_scale_linear_Y (const VSImage * dest, const VSImage * src,
guint8 * tmpbuf)
uint8_t * tmpbuf)
{
int acc;
int y_increment;
int x_increment;
guint8 *tmp1;
guint8 *tmp2;
uint8_t *tmp1;
uint8_t *tmp2;
int y1;
int y2;
int i;
......@@ -623,7 +622,7 @@ vs_image_scale_linear_Y (const VSImage * dest, const VSImage * src,
void
vs_image_scale_nearest_RGB565 (const VSImage * dest, const VSImage * src,
guint8 * tmpbuf)
uint8_t * tmpbuf)
{
int acc;
int y_increment;
......@@ -651,13 +650,13 @@ vs_image_scale_nearest_RGB565 (const VSImage * dest, const VSImage * src,
void
vs_image_scale_linear_RGB565 (const VSImage * dest, const VSImage * src,
guint8 * tmpbuf)
uint8_t * tmpbuf)
{
int acc;
int y_increment;
int x_increment;
guint8 *tmp1;
guint8 *tmp2;
uint8_t *tmp1;
uint8_t *tmp2;
int y1;
int y2;
int i;
......@@ -740,7 +739,7 @@ vs_image_scale_linear_RGB565 (const VSImage * dest, const VSImage * src,
void
vs_image_scale_nearest_RGB555 (const VSImage * dest, const VSImage * src,
guint8 * tmpbuf)
uint8_t * tmpbuf)
{
int acc;
int y_increment;
......@@ -768,13 +767,13 @@ vs_image_scale_nearest_RGB555 (const VSImage * dest, const VSImage * src,
void
vs_image_scale_linear_RGB555 (const VSImage * dest, const VSImage * src,
guint8 * tmpbuf)
uint8_t * tmpbuf)
{
int acc;
int y_increment;
int x_increment;
guint8 *tmp1;
guint8 *tmp2;
uint8_t *tmp1;
uint8_t *tmp2;
int y1;
int y2;
int i;
......
......@@ -28,55 +28,51 @@
#ifndef __VS_IMAGE_H__
#define __VS_IMAGE_H__
#include <glib.h>
G_BEGIN_DECLS
#include <stdint.h>
typedef struct _VSImage VSImage;
struct _VSImage {
guint8 *pixels;
uint8_t *pixels;
int width;
int height;
int stride;
};
void vs_image_scale_nearest_RGBA (const VSImage *dest, const VSImage *src,
guint8 *tmpbuf);
uint8_t *tmpbuf);
void vs_image_scale_linear_RGBA (const VSImage *dest, const VSImage *src,
guint8 *tmpbuf);
uint8_t *tmpbuf);
void vs_image_scale_nearest_RGB (const VSImage *dest, const VSImage *src,
guint8 *tmpbuf);
uint8_t *tmpbuf);
void vs_image_scale_linear_RGB (const VSImage *dest, const VSImage *src,
guint8 *tmpbuf);
uint8_t *tmpbuf);
void vs_image_scale_nearest_YUYV (const VSImage *dest, const VSImage *src,
guint8 *tmpbuf);
uint8_t *tmpbuf);
void vs_image_scale_linear_YUYV (const VSImage *dest, const VSImage *src,
guint8 *tmpbuf);
uint8_t *tmpbuf);
void vs_image_scale_nearest_UYVY (const VSImage *dest, const VSImage *src,
guint8 *tmpbuf);
uint8_t *tmpbuf);
void vs_image_scale_linear_UYVY (const VSImage *dest, const VSImage *src,
guint8 *tmpbuf);
uint8_t *tmpbuf);
void vs_image_scale_nearest_Y (const VSImage *dest, const VSImage *src,
guint8 *tmpbuf);
uint8_t *tmpbuf);
void vs_image_scale_linear_Y (const VSImage *dest, const VSImage *src,
guint8 *tmpbuf);
uint8_t *tmpbuf);
void vs_image_scale_nearest_RGB565 (const VSImage *dest, const VSImage *src,
guint8 *tmpbuf);
uint8_t *tmpbuf);
void vs_image_scale_linear_RGB565 (const VSImage *dest, const VSImage *src,
guint8 *tmpbuf);
uint8_t *tmpbuf);
void vs_image_scale_nearest_RGB555 (const VSImage *dest, const VSImage *src,
guint8 *tmpbuf);
uint8_t *tmpbuf);
void vs_image_scale_linear_RGB555 (const VSImage *dest, const VSImage *src,
guint8 *tmpbuf);
G_END_DECLS
uint8_t *tmpbuf);
#endif
......@@ -28,12 +28,11 @@
#include "vs_scanline.h"
#include <liboil/liboil.h>
#include <glib.h>
/* greyscale, i.e., single componenet */
void
vs_scanline_downsample_Y (guint8 * dest, guint8 * src, int n)
vs_scanline_downsample_Y (uint8_t * dest, uint8_t * src, int n)
{
int i;
......@@ -43,7 +42,7 @@ vs_scanline_downsample_Y (guint8 * dest, guint8 * src, int n)
}
void
vs_scanline_resample_nearest_Y (guint8 * dest, guint8 * src, int n,
vs_scanline_resample_nearest_Y (uint8_t * dest, uint8_t * src, int n,
int *accumulator, int increment)