2001-02-24 20:28:24 +00:00
|
|
|
/*
|
|
|
|
* idct.c
|
2004-08-02 11:26:43 +00:00
|
|
|
* Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
|
2003-04-06 16:36:02 +00:00
|
|
|
* Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
|
2001-02-24 20:28:24 +00:00
|
|
|
*
|
|
|
|
* This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
|
2003-04-06 16:36:02 +00:00
|
|
|
* See http://libmpeg2.sourceforge.net/ for updates.
|
2001-02-24 20:28:24 +00:00
|
|
|
*
|
|
|
|
* 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
|
2005-02-19 02:32:12 +00:00
|
|
|
*
|
|
|
|
* Modified for use with MPlayer, see libmpeg-0.4.0.diff for the exact changes.
|
|
|
|
* detailed CVS changelog at http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/
|
|
|
|
* $Id$
|
2001-02-24 20:28:24 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
#include <stdlib.h>
|
2001-02-24 20:28:24 +00:00
|
|
|
#include <inttypes.h>
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
#include "mpeg2.h"
|
|
|
|
#include "attributes.h"
|
2004-08-02 11:26:43 +00:00
|
|
|
#include "mpeg2_internal.h"
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2004-08-02 11:26:43 +00:00
|
|
|
#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) */
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2001-03-04 21:01:54 +00:00
|
|
|
/* idct main entry point */
|
2003-04-06 16:36:02 +00:00
|
|
|
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);
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2004-08-02 11:26:43 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
uint8_t mpeg2_clip[3840 * 2 + 256];
|
|
|
|
#define CLIP(i) ((mpeg2_clip + 3840)[i])
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
#if 0
|
|
|
|
#define BUTTERFLY(t0,t1,W0,W1,d0,d1) \
|
|
|
|
do { \
|
2004-08-02 11:26:43 +00:00
|
|
|
t0 = W0 * d0 + W1 * d1; \
|
|
|
|
t1 = W0 * d1 - W1 * d0; \
|
2003-04-06 16:36:02 +00:00
|
|
|
} 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)
|
2001-02-24 20:28:24 +00:00
|
|
|
#endif
|
|
|
|
|
2003-07-07 17:12:45 +00:00
|
|
|
static inline void idct_row (int16_t * const block)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
2003-04-06 16:36:02 +00:00
|
|
|
int d0, d1, d2, d3;
|
|
|
|
int a0, a1, a2, a3, b0, b1, b2, b3;
|
|
|
|
int t0, t1, t2, t3;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
/* shortcut */
|
2003-04-06 16:36:02 +00:00
|
|
|
if (likely (!(block[1] | ((int32_t *)block)[1] | ((int32_t *)block)[2] |
|
|
|
|
((int32_t *)block)[3]))) {
|
2004-08-02 11:26:43 +00:00
|
|
|
uint32_t tmp = (uint16_t) (block[0] >> 1);
|
2003-04-06 16:36:02 +00:00
|
|
|
tmp |= tmp << 16;
|
|
|
|
((int32_t *)block)[0] = tmp;
|
|
|
|
((int32_t *)block)[1] = tmp;
|
|
|
|
((int32_t *)block)[2] = tmp;
|
|
|
|
((int32_t *)block)[3] = tmp;
|
2001-02-24 20:28:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-08-02 11:26:43 +00:00
|
|
|
d0 = (block[0] << 11) + 2048;
|
2003-04-06 16:36:02 +00:00
|
|
|
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;
|
2004-08-02 11:26:43 +00:00
|
|
|
b1 = ((t0 + t1) >> 8) * 181;
|
|
|
|
b2 = ((t0 - t1) >> 8) * 181;
|
2003-04-06 16:36:02 +00:00
|
|
|
|
2004-08-02 11:26:43 +00:00
|
|
|
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;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2003-07-07 17:12:45 +00:00
|
|
|
static inline void idct_col (int16_t * const block)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
2003-04-06 16:36:02 +00:00
|
|
|
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;
|
2004-08-02 11:26:43 +00:00
|
|
|
t0 -= t2;
|
|
|
|
t1 -= t3;
|
|
|
|
b1 = ((t0 + t1) >> 8) * 181;
|
|
|
|
b2 = ((t0 - t1) >> 8) * 181;
|
2003-04-06 16:36:02 +00:00
|
|
|
|
|
|
|
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;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
static void mpeg2_idct_copy_c (int16_t * block, uint8_t * dest,
|
|
|
|
const int stride)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
|
|
|
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]);
|
|
|
|
|
2004-08-02 11:26:43 +00:00
|
|
|
((int32_t *)block)[0] = 0; ((int32_t *)block)[1] = 0;
|
|
|
|
((int32_t *)block)[2] = 0; ((int32_t *)block)[3] = 0;
|
2003-04-06 16:36:02 +00:00
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
dest += stride;
|
|
|
|
block += 8;
|
|
|
|
} while (--i);
|
|
|
|
}
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
static void mpeg2_idct_add_c (const int last, int16_t * block,
|
|
|
|
uint8_t * dest, const int stride)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2004-08-02 11:26:43 +00:00
|
|
|
if (last != 129 || (block[0] & (7 << 4)) == (4 << 4)) {
|
2003-04-06 16:36:02 +00:00
|
|
|
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]);
|
|
|
|
|
2004-08-02 11:26:43 +00:00
|
|
|
((int32_t *)block)[0] = 0; ((int32_t *)block)[1] = 0;
|
|
|
|
((int32_t *)block)[2] = 0; ((int32_t *)block)[3] = 0;
|
2003-04-06 16:36:02 +00:00
|
|
|
|
|
|
|
dest += stride;
|
|
|
|
block += 8;
|
|
|
|
} while (--i);
|
|
|
|
} else {
|
|
|
|
int DC;
|
|
|
|
|
2004-08-02 11:26:43 +00:00
|
|
|
DC = (block[0] + 64) >> 7;
|
2003-04-06 16:36:02 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
void mpeg2_idct_init (uint32_t accel)
|
|
|
|
{
|
2004-11-03 17:12:01 +00:00
|
|
|
#if defined(ARCH_X86) || defined(ARCH_X86_64)
|
2003-04-06 16:36:02 +00:00
|
|
|
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
|
2004-10-17 18:49:19 +00:00
|
|
|
#if defined( ARCH_PPC ) && defined( HAVE_ALTIVEC )
|
2003-04-06 16:36:02 +00:00
|
|
|
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
|
2003-07-27 22:55:25 +00:00
|
|
|
#ifdef CAN_COMPILE_ALPHA_MVI
|
2003-04-06 16:36:02 +00:00
|
|
|
if (accel & MPEG2_ACCEL_ALPHA_MVI) {
|
|
|
|
mpeg2_idct_copy = mpeg2_idct_copy_mvi;
|
|
|
|
mpeg2_idct_add = mpeg2_idct_add_mvi;
|
2004-08-02 11:26:43 +00:00
|
|
|
mpeg2_idct_alpha_init ();
|
2003-07-27 22:55:25 +00:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
if (accel & MPEG2_ACCEL_ALPHA) {
|
2004-08-02 11:26:43 +00:00
|
|
|
int i;
|
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
mpeg2_idct_copy = mpeg2_idct_copy_alpha;
|
|
|
|
mpeg2_idct_add = mpeg2_idct_add_alpha;
|
2004-08-02 11:26:43 +00:00
|
|
|
mpeg2_idct_alpha_init ();
|
|
|
|
for (i = -3840; i < 3840 + 256; i++)
|
|
|
|
CLIP(i) = (i < 0) ? 0 : ((i > 255) ? 255 : i);
|
2003-04-06 16:36:02 +00:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
extern uint8_t mpeg2_scan_norm[64];
|
|
|
|
extern uint8_t mpeg2_scan_alt[64];
|
|
|
|
int i, j;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2003-04-06 16:36:02 +00:00
|
|
|
mpeg2_idct_copy = mpeg2_idct_copy_c;
|
|
|
|
mpeg2_idct_add = mpeg2_idct_add_c;
|
2004-08-02 11:26:43 +00:00
|
|
|
for (i = -3840; i < 3840 + 256; i++)
|
|
|
|
CLIP(i) = (i < 0) ? 0 : ((i > 255) ? 255 : i);
|
2003-04-06 16:36:02 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|