All pastes #148194 Raw Edit

Untitled

public c v1 · immutable
#148194 ·published 2006-08-24 23:03 UTC
rendered paste body
/* * idct.c * Copyright (C) 2000-2003 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 */#include "plugin.h"#include "mpeg2dec_config.h"#include "mpeg2.h"#include "attributes.h"#include "mpeg2_internal.h"#define W1 2841 /* 2048 * sqrt (2) * cos (1 * pi / 16) */#define W2 2676 /* 2048 * sqrt (2) * cos (2 * pi / 16) */#define W3 2408 /* 2048 * sqrt (2) * cos (3 * pi / 16) */#define W5 1609 /* 2048 * sqrt (2) * cos (5 * pi / 16) */#define W6 1108 /* 2048 * sqrt (2) * cos (6 * pi / 16) */#define W7 565  /* 2048 * sqrt (2) * cos (7 * pi / 16) */#define SC(x) #x#define WA 1448 //=cos(pi/4)		2048*cos(4pi/16)  	0,707106781#define WB 2009 //=cos(pi/16)	2048*cos(pi/16)	 	0,98078528	#define WC 1892 //=cos(pi/8)		2048*cos(2pi/16)		0,923879533#define WD 1703 //=cos(3pi/16)	2048*cos(3pi/16)		0,831469612#define WE 1448 //=sin(pi/4)		2048*sin(4pi/16)		0,707106781#define WF 1138 //=sin(3pi/16)	2048*sin(3pi/16)		0,555570233#define WG 784 //=sin(pi/8)		2048*sin(2pi/16)		0,382683432#define WH 400 //=sin(pi/16)	2048*sin(pi/16)		0,195090322/* the two matrix *//* static const int mat_coef_1[] ICONST_ATTR = {WA,  WC,  WE,  WG, WA,  WG, -WE, -WC, WA, -WG, -WE,  WC, WA, -WC,  WE, -WG};static const int mat_coef_2[] ICONST_ATTR = {WB,  WD,  WF,  WH, WD, -WH, -WB, -WF, WF, -WB,  WH,  WD, WH, -WF,  WD, -WB};*//* optimised for incrementation *//*static const int mat_coef[] ICONST_ATTR = {WA,  WC,  WE,  WG, WB,  WD,  WF,  WH, WA,  WG, -WE, -WC, WD, -WH, -WB, -WF, WA, -WG, -WE,  WC, WF, -WB,  WH,  WD, WA, -WC,  WE, -WG, WH, -WF,  WD, -WB};*//* takes more space but optimised for incrementation static const int16_t mat_coef[] ICONST_ATTR = {WA,  WC,  WE,  WG, WB,  WD,  WF,  WH, WA,  WC,  WE,  WG, WB,  WD,  WF,  WH, WA,  WG, -WE, -WC, WD, -WH, -WB, -WF, WA,  WG, -WE, -WC, WD, -WH, -WB, -WF, WA, -WG, -WE,  WC, WF, -WB,  WH,  WD, WA, -WG, -WE,  WC, WF, -WB,  WH,  WD, WA, -WC,  WE, -WG, WH, -WF,  WD, -WB, WA, -WC,  WE, -WG, WH, -WF,  WD, -WB};*//* consecutive */ static const int mat_coef[] ICONST_ATTR = {WA,  WC,  WE,  WG, WA,  WG, -WE, -WC, WA, -WG, -WE,  WC, WA, -WC,  WE, -WG, WB,  WD,  WF,  WH, WD, -WH, -WB, -WF, WF, -WB,  WH,  WD, WH, -WF,  WD, -WB};/* idct main entry point  */void (* mpeg2_idct_copy) (int16_t * block, uint8_t * dest, int stride);void (* mpeg2_idct_add) (int last, int16_t * block,                         uint8_t * dest, int stride);/* * In legal streams, the IDCT output should be between -384 and +384. * In corrupted streams, it is possible to force the IDCT output to go * to +-3826 - this is the worst case for a column IDCT where the * column inputs are 16-bit values. */#ifdef CPU_COLDFIREstatic inline unsigned CLIP(int value){    asm (  /* Note: Uses knowledge that only the low byte of the result is used */        "cmp.l   #255,%[v]   \n"  /* overflow? */        "bls.b   1f          \n"  /* no: return value */        "spl.b   %[v]        \n"  /* yes: set low byte to appropriate boundary */    "1:                      \n"        : /* outputs */        [v]"+d"(value)    );    return value;}#elif defined CPU_ARMstatic inline unsigned CLIP(int value){    asm ( /* Note: Uses knowledge that only the low byte of the result is used */        "cmp     %[v], #255          \n"        "mvnhi   %[v], %[v], asr #31 \n"        : /* outputs */        [v]"+r"(value)    );    return value;}#elseuint8_t mpeg2_clip[3840 * 2 + 256] IBSS_ATTR;#define CLIP(i) ((mpeg2_clip + 3840)[i])#endif#ifdef CPU_COLDFIRE#define BUTTERFLY(t0,t1,W0,W1,d0,d1) 			\	asm(										\		"move.l #"SC(W0)", %%d0			\n\t"		\		"move.l #"SC(W1)", %%d1			\n\t"		\		"move.l #0xFFFFFF80,%%macsr		\n\t"		\		"mac.l 	%%d0, %[e], %%acc0		\n\t"		\		"mac.l 	%%d1, %[f], %%acc0		\n\t"		\		"movclr.l %%acc0, %[a]			\n\t"		\ 		"mac.l 	%%d0, %[f], %%acc0		\n\t"		\		"msac.l %%d1, %[e], %%acc0		\n\t"		\		"movclr.l %%acc0, %[b]			\n\t"		\		:	[a]"+d"(t0),[b]"+d"(t1)				\		:	[e]"d"(d0),[f]"d"(d1)		\		:	"%d0","%d1"	\	   )#define BUTTERFLY_OK(t0,t1,W0,W1,d0,d1) 				\	asm(											\		"move.l #"SC(W0)", %%d0			\n\t"		\		"move.l #"SC(W1)", %%d1			\n\t"		\		"move.l %[e], %%d2				\n\t"		\		"mulu.l %%d0, %%d2				\n\t"		\		"move.l %[f], %[a]				\n\t"		\		"mulu.l %%d1, %[a]				\n\t"		\		"add.l %%d2,%[a]				\n\t"		\ 		"move.l %[e], %%d2				\n\t"		\		"mulu.l %%d1, %%d2				\n\t"		\		"move.l %[f], %[b]				\n\t"		\		"mulu.l %%d0, %[b]				\n\t"		\		"sub.l %%d2,%[b]				\n\t"		\		:	[a]"+d"(t0),[b]"+d"(t1)					\		:	[e]"d"(d0),[f]"d"(d1)					\		:	"%d0","%d1","%d2"					\	   )#define BUTTERFLY__(t0,t1,W0,W1,d0,d1) 			\	asm(										\		"move.l #"SC(W0)", %%d0			\n\t"		\		"move.l #"SC(W1)", %%d1			\n\t"		\		"move.l #0xFFFFFF80,%%macsr		\n\t"		\		"mac.l 	%%d0, %[e], %%acc0		\n\t"		\		"mac.l 	%%d1, %[f], %%acc0		\n\t"		\		"movclr.l %%acc0, %[a]			\n\t"		\ 		"mac.l 	%%d0, %[f], %%acc0		\n\t"		\		"msac.l %%d1, %[e], %%acc0		\n\t"		\		"movclr.l %%acc0, %[b]			\n\t"		\		:	[a]"+d"(t0),[b]"+d"(t1)				\		:	[e]"d"(d0),[f]"d"(d1)		\		:	"%d0","%d1"	\	   )#else#if 0#define BUTTERFLY(t0,t1,W0,W1,d0,d1)        \do {                                        \    t0 = W0 * d0 + W1 * d1;                \    t1 = W0 * d1 - W1 * d0;                \} while (0)#else#define BUTTERFLY(t0,t1,W0,W1,d0,d1)        \do {                                        \    int tmp = W0 * (d0 + d1);                \    t0 = tmp + (W1 - W0) * d1;                \    t1 = tmp - (W1 + W0) * d0;                \} while (0)#endif#endif#ifdef CPU_COLDFIREstatic inline void idct_row (int16_t * const block){	asm volatile(		"move.l #0xFFFFFF80,%%macsr			\n\t"				/* load the row */			"move.l (%[block]), %%d0			\n\t"		"move.l (1,%[block]), %%d1			\n\t"		"move.l (2,%[block]), %%d2			\n\t"		"move.l (3,%[block]), %%d3		\n\t"				/* matrix start */			"move.l %[mat_coef], %%a0			\n\t"		"move.l (%%a0), %%a1				\n\t"			"lea.l (1,%%a0), %%a0				\n\t"		/* clear the accs */			"move.l #0,%%acc0						\n\t"		"move.l #0,%%acc1						\n\t"		"move.l #0,%%acc2						\n\t"		"move.l #0,%%acc3						\n\t"		/* compute second matrix * P[1],P[3],P[5],P[7]*/			"mac.w 	%%d0l, %%a1u,  %%acc0					\n\t"			"mac.w 	%%d1l, %%a1l, (%%a0)+, %%a1, %%acc0		\n\t"		"mac.w 	%%d2l, %%a1u, %%acc0					\n\t"			"mac.w 	%%d3l, %%a1l, (%%a0)+, %%a1, %%acc0		\n\t"					"mac.w 	%%d0l, %%a1u, %%acc1					\n\t"			"mac.w 	%%d1l, %%a1l, (%%a0)+, %%a1,  %%acc1	\n\t"		"mac.w 	%%d2l, %%a1u, %%acc1					\n\t"			"mac.w 	%%d3l, %%a1l, (%%a0)+, %%a1, %%acc1		\n\t"		"mac.w 	%%d0l, %%a1u,  %%acc2					\n\t"			"mac.w 	%%d1l, %%a1l, (%%a0)+, %%a1,%%acc2		\n\t"		"mac.w 	%%d2l, %%a1u, %%acc2					\n\t"			"mac.w 	%%d3l, %%a1l, (%%a0)+, %%a1, %%acc2		\n\t"		"mac.w 	%%d0l, %%a1u, %%acc3					\n\t"			"mac.w 	%%d1l, %%a1l, (%%a0)+, %%a1,%%acc3		\n\t"		"mac.w 	%%d2l, %%a1u, %%acc3					\n\t"			"mac.w 	%%d3l, %%a1l, (%%a0)+, %%a1,  %%acc3	\n\t"					/* save second matrix row */		"movclr.l %%acc0, %%d4	\n\t"		"movclr.l %%acc1, %%d5	\n\t"		"movclr.l %%acc2, %%d6	\n\t"		"movclr.l %%acc3, %%d7	\n\t"		/* compute first matrix * P[0],P[2],P[4],P[6]*/			"mac.w 	%%d0u, %%a1u,  %%acc0								\n\t"			"mac.w 	%%d1u, %%a1l, (%%a0)+, %%a1,%%acc0					\n\t"		"mac.w 	%%d2u, %%a1u, %%acc0								\n\t"			"mac.w 	%%d3u, %%a1l,  (%%a0)+, %%a1,%%acc0					\n\t"					"mac.w 	%%d0u, %%a1u,  %%acc1								\n\t"			"mac.w 	%%d1u, %%a1l,(%%a0)+, %%a1, %%acc1					\n\t"		"mac.w 	%%d2u, %%a1u,  %%acc1								\n\t"			"mac.w 	%%d3u, %%a1l,(%%a0)+, %%a1, %%acc1					\n\t"		"mac.w 	%%d0u, %%a1u,  %%acc2								\n\t"			"mac.w 	%%d1u, %%a1l, (%%a0)+, %%a1,%%acc2					\n\t"		"mac.w 	%%d2u, %%a1u, %%acc2								\n\t"			"mac.w 	%%d3u, %%a1l,(%%a0)+, %%a1,  %%acc2					\n\t"		"mac.w 	%%d0u, %%a1u, %%acc3								\n\t"			"mac.w 	%%d1u, %%a1l, (%%a0)+, %%a1, %%acc3					\n\t"		"mac.w 	%%d2u, %%a1u, %%acc3								\n\t"			"mac.w 	%%d3u, %%a1l, (%%a0)+, %%a1, %%acc3					\n\t"		/* right shift value */			"movq.l #11, %%d1			\n\t"		/* mat 1 + mat 2 */			"move.l %%acc0, %%d0		\n\t"		"add.l	%%d4,%%d0			\n\t"			"asr.l %%d1, %%d0			\n\t"				"move.w %%d0,(%[block])		\n\t"					"move.l %%acc1, %%d0		\n\t"		"add.l	%%d5,%%d0			\n\t"			"asr.l %%d1, %%d0			\n\t"				"move.w %%d0,(2,%[block])	\n\t"					"move.l %%acc2, %%d0		\n\t"		"add.l	%%d6,%%d0			\n\t"			"asr.l %%d1, %%d0			\n\t"				"move.w %%d0,(4,%[block])	\n\t"					"move.l %%acc1, %%d0		\n\t"		"add.l	%%d7,%%d0			\n\t"			"asr.l %%d1, %%d0			\n\t"				"move.w %%d0,(6,%[block])	\n\t"							/* mat 1 - mat2*/						"movclr.l %%acc0, %%d0		\n\t"		"sub.l	%%d4,%%d0			\n\t"			"asr.l %%d1, %%d0			\n\t"				"move.w %%d0,(1,%[block])	\n\t"					"movclr.l %%acc1, %%d0		\n\t"		"sub.l	%%d5,%%d0			\n\t"			"asr.l %%d1, %%d0			\n\t"				"move.w %%d0,(3,%[block])	\n\t"					"movclr.l %%acc2, %%d0		\n\t"		"sub.l	%%d6,%%d0			\n\t"			"asr.l %%d1, %%d0			\n\t"				"move.w %%d0,(5,%[block])	\n\t"					"movclr.l %%acc1, %%d0		\n\t"		"sub.l	%%d7,%%d0			\n\t"			"asr.l %%d1, %%d0			\n\t"				"move.w %%d0,(7,%[block])	\n\t"		:		:	[mat_coef]"a"(mat_coef),[block]"a"(block)			:	"%a0","%a1","%d0","%d1","%d2","%d3","%d4","%d5","%d6","%d7"	);	}#elsestatic inline void idct_row (int16_t * const block){    int d0, d1, d2, d3;    int a0, a1, a2, a3, b0, b1, b2, b3;    int t0, t1, t2, t3;    /* shortcut */    if (likely (!(block[1] | ((int32_t *)block)[1] | ((int32_t *)block)[2] |                  ((int32_t *)block)[3]))) {        uint32_t tmp = (uint16_t) (block[0] >> 1);        tmp |= tmp << 16;        ((int32_t *)block)[0] = tmp;        ((int32_t *)block)[1] = tmp;        ((int32_t *)block)[2] = tmp;        ((int32_t *)block)[3] = tmp;        return;    }    d0 = (block[0] << 11) + 2048;    d1 = block[1];    d2 = block[2] << 11;    d3 = block[3];    t0 = d0 + d2;    t1 = d0 - d2;    BUTTERFLY (t2, t3, W6, W2, d3, d1);    a0 = t0 + t2;    a1 = t1 + t3;    a2 = t1 - t3;    a3 = t0 - t2;    d0 = block[4];    d1 = block[5];    d2 = block[6];    d3 = block[7];    BUTTERFLY (t0, t1, W7, W1, d3, d0);    BUTTERFLY (t2, t3, W3, W5, d1, d2);    b0 = t0 + t2;    b3 = t1 + t3;    t0 -= t2;    t1 -= t3;    b1 = ((t0 + t1) >> 8) * 181;    b2 = ((t0 - t1) >> 8) * 181;    block[0] = (a0 + b0) >> 12;    block[1] = (a1 + b1) >> 12;    block[2] = (a2 + b2) >> 12;    block[3] = (a3 + b3) >> 12;    block[4] = (a3 - b3) >> 12;    block[5] = (a2 - b2) >> 12;    block[6] = (a1 - b1) >> 12;    block[7] = (a0 - b0) >> 12;}#endifstatic inline void idct_col (int16_t * const block){    int d0, d1, d2, d3;    int a0, a1, a2, a3, b0, b1, b2, b3;    int t0, t1, t2, t3;    d0 = (block[8*0] << 11) + 65536;    d1 = block[8*1];    d2 = block[8*2] << 11;    d3 = block[8*3];    t0 = d0 + d2;    t1 = d0 - d2;    BUTTERFLY (t2, t3, W6, W2, d3, d1);    a0 = t0 + t2;    a1 = t1 + t3;    a2 = t1 - t3;    a3 = t0 - t2;    d0 = block[8*4];    d1 = block[8*5];    d2 = block[8*6];    d3 = block[8*7];    BUTTERFLY (t0, t1, W7, W1, d3, d0);    BUTTERFLY (t2, t3, W3, W5, d1, d2);    b0 = t0 + t2;    b3 = t1 + t3;    t0 -= t2;    t1 -= t3;    b1 = ((t0 + t1) >> 8) * 181;    b2 = ((t0 - t1) >> 8) * 181;    block[8*0] = (a0 + b0) >> 17;    block[8*1] = (a1 + b1) >> 17;    block[8*2] = (a2 + b2) >> 17;    block[8*3] = (a3 + b3) >> 17;    block[8*4] = (a3 - b3) >> 17;    block[8*5] = (a2 - b2) >> 17;    block[8*6] = (a1 - b1) >> 17;    block[8*7] = (a0 - b0) >> 17;}static void mpeg2_idct_copy_c (int16_t * block, uint8_t * dest,                               const int stride){    int i;    for (i = 0; i < 8; i++)        idct_row (block + 8 * i);    for (i = 0; i < 8; i++)        idct_col (block + i);    do {        dest[0] = CLIP (block[0]);        dest[1] = CLIP (block[1]);        dest[2] = CLIP (block[2]);        dest[3] = CLIP (block[3]);        dest[4] = CLIP (block[4]);        dest[5] = CLIP (block[5]);        dest[6] = CLIP (block[6]);        dest[7] = CLIP (block[7]);        ((int32_t *)block)[0] = 0;        ((int32_t *)block)[1] = 0;        ((int32_t *)block)[2] = 0;        ((int32_t *)block)[3] = 0;        dest += stride;        block += 8;    } while (--i);}static void mpeg2_idct_add_c (const int last, int16_t * block,                              uint8_t * dest, const int stride){    int i;    if (last != 129 || (block[0] & (7 << 4)) == (4 << 4)) {        for (i = 0; i < 8; i++)            idct_row (block + 8 * i);        for (i = 0; i < 8; i++)            idct_col (block + i);        do {            dest[0] = CLIP (block[0] + dest[0]);            dest[1] = CLIP (block[1] + dest[1]);            dest[2] = CLIP (block[2] + dest[2]);            dest[3] = CLIP (block[3] + dest[3]);            dest[4] = CLIP (block[4] + dest[4]);            dest[5] = CLIP (block[5] + dest[5]);            dest[6] = CLIP (block[6] + dest[6]);            dest[7] = CLIP (block[7] + dest[7]);            ((int32_t *)block)[0] = 0;        ((int32_t *)block)[1] = 0;            ((int32_t *)block)[2] = 0;        ((int32_t *)block)[3] = 0;            dest += stride;            block += 8;        } while (--i);    } else {        int DC;        DC = (block[0] + 64) >> 7;        block[0] = block[63] = 0;        i = 8;        do {            dest[0] = CLIP (DC + dest[0]);            dest[1] = CLIP (DC + dest[1]);            dest[2] = CLIP (DC + dest[2]);            dest[3] = CLIP (DC + dest[3]);            dest[4] = CLIP (DC + dest[4]);            dest[5] = CLIP (DC + dest[5]);            dest[6] = CLIP (DC + dest[6]);            dest[7] = CLIP (DC + dest[7]);            dest += stride;        } while (--i);    }}void mpeg2_idct_init (uint32_t accel){    (void)accel;#ifdef ARCH_X86    if (accel & MPEG2_ACCEL_X86_MMXEXT) {        mpeg2_idct_copy = mpeg2_idct_copy_mmxext;        mpeg2_idct_add = mpeg2_idct_add_mmxext;        mpeg2_idct_mmx_init ();    } else if (accel & MPEG2_ACCEL_X86_MMX) {        mpeg2_idct_copy = mpeg2_idct_copy_mmx;        mpeg2_idct_add = mpeg2_idct_add_mmx;        mpeg2_idct_mmx_init ();    } else#endif#ifdef ARCH_PPC    if (accel & MPEG2_ACCEL_PPC_ALTIVEC) {        mpeg2_idct_copy = mpeg2_idct_copy_altivec;        mpeg2_idct_add = mpeg2_idct_add_altivec;        mpeg2_idct_altivec_init ();    } else#endif#ifdef ARCH_ALPHA    if (accel & MPEG2_ACCEL_ALPHA_MVI) {        mpeg2_idct_copy = mpeg2_idct_copy_mvi;        mpeg2_idct_add = mpeg2_idct_add_mvi;        mpeg2_idct_alpha_init ();    } else if (accel & MPEG2_ACCEL_ALPHA) {        int i;        mpeg2_idct_copy = mpeg2_idct_copy_alpha;        mpeg2_idct_add = mpeg2_idct_add_alpha;        mpeg2_idct_alpha_init ();        for (i = -3840; i < 3840 + 256; i++)            CLIP(i) = (i < 0) ? 0 : ((i > 255) ? 255 : i);    } else#endif    {        extern uint8_t mpeg2_scan_norm[64];        extern uint8_t mpeg2_scan_alt[64];        int i, j;        mpeg2_idct_copy = mpeg2_idct_copy_c;        mpeg2_idct_add = mpeg2_idct_add_c;#if !defined(CPU_COLDFIRE) && !defined(CPU_ARM)        for (i = -3840; i < 3840 + 256; i++)            CLIP(i) = (i < 0) ? 0 : ((i > 255) ? 255 : i);#endif        for (i = 0; i < 64; i++) {            j = mpeg2_scan_norm[i];            mpeg2_scan_norm[i] = ((j & 0x36) >> 1) | ((j & 0x09) << 2);            j = mpeg2_scan_alt[i];            mpeg2_scan_alt[i] = ((j & 0x36) >> 1) | ((j & 0x09) << 2);        }    }}