A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 274 lines 7.3 kB view raw
1/* 2 * idct.c 3 * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org> 4 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca> 5 * 6 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder. 7 * See http://libmpeg2.sourceforge.net/ for updates. 8 * 9 * mpeg2dec is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation; either version 2 of the License, or 12 * (at your option) any later version. 13 * 14 * mpeg2dec is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program; if not, write to the Free Software 21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 22 * 23 * $Id$ 24 * libmpeg2 sync history: 25 * 2008-07-01 - CVS revision 1.36 26 */ 27 28#include "plugin.h" 29 30#include "mpeg2dec_config.h" 31 32#include "mpeg2.h" 33#include "attributes.h" 34#include "mpeg2_internal.h" 35 36#if defined(CPU_COLDFIRE) || defined (CPU_ARM) 37#define IDCT_ASM 38#endif 39 40#ifndef IDCT_ASM 41 42#define W1 2841 /* 2048 * sqrt (2) * cos (1 * pi / 16) */ 43#define W2 2676 /* 2048 * sqrt (2) * cos (2 * pi / 16) */ 44#define W3 2408 /* 2048 * sqrt (2) * cos (3 * pi / 16) */ 45#define W5 1609 /* 2048 * sqrt (2) * cos (5 * pi / 16) */ 46#define W6 1108 /* 2048 * sqrt (2) * cos (6 * pi / 16) */ 47#define W7 565 /* 2048 * sqrt (2) * cos (7 * pi / 16) */ 48 49/* 50 * In legal streams, the IDCT output should be between -384 and +384. 51 * In corrupted streams, it is possible to force the IDCT output to go 52 * to +-3826 - this is the worst case for a column IDCT where the 53 * column inputs are 16-bit values. 54 */ 55#define CLIP(i) \ 56 ({ typeof (i) _i = (i); \ 57 if ((_i & 0xff) != _i) \ 58 _i = ~(_i >> (8*sizeof(_i) - 1)); \ 59 _i; }) 60 61#if 0 62#define BUTTERFLY(t0,t1,W0,W1,d0,d1) \ 63 do { \ 64 t0 = W0 * d0 + W1 * d1; \ 65 t1 = W0 * d1 - W1 * d0; \ 66 } while (0) 67#else 68#define BUTTERFLY(t0,t1,W0,W1,d0,d1) \ 69 do { \ 70 int tmp = W0 * (d0 + d1); \ 71 t0 = tmp + (W1 - W0) * d1; \ 72 t1 = tmp - (W1 + W0) * d0; \ 73 } while (0) 74#endif 75 76static inline void idct_row (int16_t * const block) 77{ 78 int d0, d1, d2, d3; 79 int a0, a1, a2, a3, b0, b1, b2, b3; 80 int t0, t1, t2, t3; 81 82 /* shortcut */ 83 if (likely (!(block[1] | ((int32_t *)block)[1] | ((int32_t *)block)[2] | 84 ((int32_t *)block)[3]))) 85 { 86 uint32_t tmp = (uint16_t) (block[0] >> 1); 87 tmp |= tmp << 16; 88 ((int32_t *)block)[0] = tmp; 89 ((int32_t *)block)[1] = tmp; 90 ((int32_t *)block)[2] = tmp; 91 ((int32_t *)block)[3] = tmp; 92 return; 93 } 94 95 d0 = (block[0] << 11) + 2048; 96 d1 = block[1]; 97 d2 = block[2] << 11; 98 d3 = block[3]; 99 t0 = d0 + d2; 100 t1 = d0 - d2; 101 BUTTERFLY (t2, t3, W6, W2, d3, d1); 102 a0 = t0 + t2; 103 a1 = t1 + t3; 104 a2 = t1 - t3; 105 a3 = t0 - t2; 106 107 d0 = block[4]; 108 d1 = block[5]; 109 d2 = block[6]; 110 d3 = block[7]; 111 BUTTERFLY (t0, t1, W7, W1, d3, d0); 112 BUTTERFLY (t2, t3, W3, W5, d1, d2); 113 b0 = t0 + t2; 114 b3 = t1 + t3; 115 t0 -= t2; 116 t1 -= t3; 117 b1 = ((t0 + t1) >> 8) * 181; 118 b2 = ((t0 - t1) >> 8) * 181; 119 120 block[0] = (a0 + b0) >> 12; 121 block[1] = (a1 + b1) >> 12; 122 block[2] = (a2 + b2) >> 12; 123 block[3] = (a3 + b3) >> 12; 124 block[4] = (a3 - b3) >> 12; 125 block[5] = (a2 - b2) >> 12; 126 block[6] = (a1 - b1) >> 12; 127 block[7] = (a0 - b0) >> 12; 128} 129 130static inline void idct_col (int16_t * const block) 131{ 132 int d0, d1, d2, d3; 133 int a0, a1, a2, a3, b0, b1, b2, b3; 134 int t0, t1, t2, t3; 135 136 d0 = (block[8*0] << 11) + 65536; 137 d1 = block[8*1]; 138 d2 = block[8*2] << 11; 139 d3 = block[8*3]; 140 t0 = d0 + d2; 141 t1 = d0 - d2; 142 BUTTERFLY (t2, t3, W6, W2, d3, d1); 143 a0 = t0 + t2; 144 a1 = t1 + t3; 145 a2 = t1 - t3; 146 a3 = t0 - t2; 147 148 d0 = block[8*4]; 149 d1 = block[8*5]; 150 d2 = block[8*6]; 151 d3 = block[8*7]; 152 BUTTERFLY (t0, t1, W7, W1, d3, d0); 153 BUTTERFLY (t2, t3, W3, W5, d1, d2); 154 b0 = t0 + t2; 155 b3 = t1 + t3; 156 t0 -= t2; 157 t1 -= t3; 158 b1 = ((t0 + t1) >> 8) * 181; 159 b2 = ((t0 - t1) >> 8) * 181; 160 161 block[8*0] = (a0 + b0) >> 17; 162 block[8*1] = (a1 + b1) >> 17; 163 block[8*2] = (a2 + b2) >> 17; 164 block[8*3] = (a3 + b3) >> 17; 165 block[8*4] = (a3 - b3) >> 17; 166 block[8*5] = (a2 - b2) >> 17; 167 block[8*6] = (a1 - b1) >> 17; 168 block[8*7] = (a0 - b0) >> 17; 169} 170 171void mpeg2_idct_copy (int16_t * block, uint8_t * dest, 172 const int stride) 173{ 174 int i; 175 176 for (i = 0; i < 8; i++) 177 idct_row (block + 8 * i); 178 179 for (i = 0; i < 8; i++) 180 idct_col (block + i); 181 182 do 183 { 184 dest[0] = CLIP (block[0]); 185 dest[1] = CLIP (block[1]); 186 dest[2] = CLIP (block[2]); 187 dest[3] = CLIP (block[3]); 188 dest[4] = CLIP (block[4]); 189 dest[5] = CLIP (block[5]); 190 dest[6] = CLIP (block[6]); 191 dest[7] = CLIP (block[7]); 192 193 ((int32_t *)block)[0] = 0; 194 ((int32_t *)block)[1] = 0; 195 ((int32_t *)block)[2] = 0; 196 ((int32_t *)block)[3] = 0; 197 198 dest += stride; 199 block += 8; 200 } 201 while (--i); 202} 203 204void mpeg2_idct_add (const int last, int16_t * block, 205 uint8_t * dest, const int stride) 206{ 207 int i; 208 209 if (last != 129 || (block[0] & (7 << 4)) == (4 << 4)) 210 { 211 for (i = 0; i < 8; i++) 212 idct_row (block + 8 * i); 213 214 for (i = 0; i < 8; i++) 215 idct_col (block + i); 216 217 do 218 { 219 dest[0] = CLIP (block[0] + dest[0]); 220 dest[1] = CLIP (block[1] + dest[1]); 221 dest[2] = CLIP (block[2] + dest[2]); 222 dest[3] = CLIP (block[3] + dest[3]); 223 dest[4] = CLIP (block[4] + dest[4]); 224 dest[5] = CLIP (block[5] + dest[5]); 225 dest[6] = CLIP (block[6] + dest[6]); 226 dest[7] = CLIP (block[7] + dest[7]); 227 228 ((int32_t *)block)[0] = 0; 229 ((int32_t *)block)[1] = 0; 230 ((int32_t *)block)[2] = 0; 231 ((int32_t *)block)[3] = 0; 232 233 dest += stride; 234 block += 8; 235 } 236 while (--i); 237 } 238 else 239 { 240 int DC = (block[0] + 64) >> 7; 241 block[0] = block[63] = 0; 242 i = 8; 243 244 do 245 { 246 dest[0] = CLIP (DC + dest[0]); 247 dest[1] = CLIP (DC + dest[1]); 248 dest[2] = CLIP (DC + dest[2]); 249 dest[3] = CLIP (DC + dest[3]); 250 dest[4] = CLIP (DC + dest[4]); 251 dest[5] = CLIP (DC + dest[5]); 252 dest[6] = CLIP (DC + dest[6]); 253 dest[7] = CLIP (DC + dest[7]); 254 dest += stride; 255 } 256 while (--i); 257 } 258} 259 260#endif /* IDCT_ASM */ 261 262void mpeg2_idct_init (void) 263{ 264 int i, j; 265 266 for (i = 0; i < 64; i++) 267 { 268 j = default_mpeg2_scan_norm[i]; 269 mpeg2_scan_norm[i] = ((j & 0x36) >> 1) | ((j & 0x09) << 2); 270 271 j = default_mpeg2_scan_alt[i]; 272 mpeg2_scan_alt[i] = ((j & 0x36) >> 1) | ((j & 0x09) << 2); 273 } 274}