/*
 * motion.c
 * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
 *
 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
 * See http://libmpeg2.sourceforge.net/ for updates.
 *
 * mpeg2dec 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.
 *
 * mpeg2dec 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Modifications for this RISCOS port, Copyright (c) 2002 P.Everett
 * <peter@everett9981.freeserve.co.uk>
 */

#include "inttypes.h"
#include "mpeg2_inte.h"

static void put_16(uint8_t *dst, const uint8_t *src, int32_t stride, int32_t count)
{
	int i;

	do
	{
		for (i=0; i<16; i++)
			dst[i] = src[i];
		dst += stride;
		src += stride;
	}
	while(--count);
}

static void put_x16(uint8_t *dst, const uint8_t *src, int32_t stride, int32_t count)
{
	int i;

	do
	{
		for (i=0; i<16; i++)
			dst[i] = (src[i] + src[i+1] + 1) / 2;
		dst += stride;
		src += stride;
	}
	while(--count);
}

static void put_y16(uint8_t *dst, const uint8_t *src, int32_t stride, int32_t count)
{
	int i;
	do
	{
		for (i=0; i<16; i++)
			dst[i] = (src[i] + src[i+stride] + 1) / 2;
		dst += stride;
		src += stride;
	}
	while(--count);
}

static void put_xy16(uint8_t *dst, const uint8_t *src, int32_t stride, int32_t count)
{
	int i;

	do
	{
		for (i=0; i<16; i++)
			dst[i] = (src[i] + src[i+1] + src[i+stride] + src[i+stride+1] + 2) / 4;
		dst += stride;
		src += stride;
	}
	while(--count);
}

static void avg_16(uint8_t *dst, const uint8_t *src, int32_t stride, int32_t count)
{
	int i;

	do
	{
		for (i=0; i<16; i++)
			dst[i] = (dst[i] + src[i] + 1) / 2;
		dst += stride;
		src += stride;
	}
	while(--count);
}

static void avg_x16(uint8_t *dst, const uint8_t *src, int32_t stride, int32_t count)
{
	int i;

	do
	{
		for (i=0; i<16; i++)
			dst[i] = ((src[i] + src[i+1] + 1) / 2 + dst[i] + 1) / 2;
		dst += stride;
		src += stride;
	}
	while(--count);
}

static void avg_y16(uint8_t *dst, const uint8_t *src, int32_t stride, int32_t count)
{
	int i;

	do
	{
		for (i=0; i<16; i++)
			dst[i] = ((src[i] + src[i+stride] + 1) / 2 + dst[i] + 1) / 2;
		dst += stride;
		src += stride;
	}
	while(--count);
}

static void avg_xy16(uint8_t *dst, const uint8_t *src, int32_t stride, int32_t count)
{
	int i;

	do
	{
		for (i=0; i<16; i++)
			dst[i] = (src[i] + src[i+1] + src[i+stride] + src[i+stride+1] + 4*dst[i] + 4) / 8;
		dst += stride;
		src += stride;
	}
	while(--count);
}

static void put_8(uint8_t *dst, const uint8_t *src, int32_t stride, int32_t count)
{
	int i;

	do
	{
		for (i=0; i<8; i++)
			dst[i] = src[i];
		dst += stride;
		src += stride;
	}
	while(--count);
}

static void put_x8(uint8_t *dst, const uint8_t *src, int32_t stride, int32_t count)
{
	int i;

	do
	{
		for (i=0; i<8; i++)
			dst[i] = (src[i] + src[i+1] + 1) / 2;
		dst += stride;
		src += stride;
	}
	while(--count);
}

static void put_y8(uint8_t *dst, const uint8_t *src, int32_t stride, int32_t count)
{
	int i;

	do
	{
		for (i=0; i<8; i++)
			dst[i] = (src[i] + src[i+stride] + 1) / 2;
		dst += stride;
		src += stride;
	}
	while(--count);
}

static void put_xy8(uint8_t *dst, const uint8_t *src, int32_t stride, int32_t count)
{
	int i;

	do
	{
		for (i=0; i<8; i++)
			dst[i] = (src[i] + src[i+1] + src[i+stride] + src[i+stride+1] + 2) / 4;
		dst += stride;
		src += stride;
	}
	while(--count);
}

static void avg_8(uint8_t *dst, const uint8_t *src, int32_t stride, int32_t count)
{
	int i;

	do
	{
		for (i=0; i<8; i++)
			dst[i] = (dst[i] + src[i] + 1) / 2;
		dst += stride;
		src += stride;
	}
	while(--count);
}

static void avg_x8(uint8_t *dst, const uint8_t *src, int32_t stride, int32_t count)
{
	int i;

	do
	{
		for (i=0; i<8; i++)
			dst[i] = ((src[i] + src[i+1] + 1) / 2 + dst[i] + 1) / 2;
		dst += stride;
		src += stride;
	}
	while(--count);
}

static void avg_y8(uint8_t *dst, const uint8_t *src, int32_t stride, int32_t count)
{
	int i;

	do
	{
		for (i=0; i<8; i++)
			dst[i] = ((src[i] + src[i+stride] + 1) / 2 + dst[i] + 1) / 2;
		dst += stride;
		src += stride;
	}
	while(--count);
}

static void avg_xy8(uint8_t *dst, const uint8_t *src, int32_t stride, int32_t count)
{
	int i;

	do
	{
		for (i=0; i<8; i++)
			dst[i] = (src[i] + src[i+1] + src[i+stride] + src[i+stride+1] + 4*dst[i] + 4) / 8;
		dst += stride;
		src += stride;
	}
	while(--count);
}

mpeg2_mc_t mc_functions_c =
{{ put_16
, put_x16
, put_y16
, put_xy16
, put_8
, put_x8
, put_y8
, put_xy8
},{ avg_16
, avg_x16
, avg_y16
, avg_xy16
, avg_8
, avg_x8
, avg_y8
, avg_xy8
}};
