Arpi | 37e8dcd | 2001-12-09 12:39:54 | [diff] [blame] | 1 | /* |
Fabrice Bellard | ff4ec49 | 2002-05-25 22:45:33 | [diff] [blame] | 2 | * Simple IDCT |
| 3 | * |
| 4 | * Copyright (c) 2001 Michael Niedermayer <[email protected]> |
| 5 | * |
Diego Biurrun | b78e719 | 2006-10-07 15:30:46 | [diff] [blame] | 6 | * This file is part of FFmpeg. |
| 7 | * |
| 8 | * FFmpeg is free software; you can redistribute it and/or |
Fabrice Bellard | ff4ec49 | 2002-05-25 22:45:33 | [diff] [blame] | 9 | * modify it under the terms of the GNU Lesser General Public |
| 10 | * License as published by the Free Software Foundation; either |
Diego Biurrun | b78e719 | 2006-10-07 15:30:46 | [diff] [blame] | 11 | * version 2.1 of the License, or (at your option) any later version. |
Fabrice Bellard | ff4ec49 | 2002-05-25 22:45:33 | [diff] [blame] | 12 | * |
Diego Biurrun | b78e719 | 2006-10-07 15:30:46 | [diff] [blame] | 13 | * FFmpeg is distributed in the hope that it will be useful, |
Fabrice Bellard | ff4ec49 | 2002-05-25 22:45:33 | [diff] [blame] | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * Lesser General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Lesser General Public |
Diego Biurrun | b78e719 | 2006-10-07 15:30:46 | [diff] [blame] | 19 | * License along with FFmpeg; if not, write to the Free Software |
Diego Biurrun | 5509bff | 2006-01-12 22:43:26 | [diff] [blame] | 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
Fabrice Bellard | ff4ec49 | 2002-05-25 22:45:33 | [diff] [blame] | 21 | */ |
Diego Biurrun | 115329f | 2005-12-17 18:14:38 | [diff] [blame] | 22 | |
Michael Niedermayer | 983e324 | 2003-03-06 11:32:04 | [diff] [blame] | 23 | /** |
Diego Biurrun | ba87f08 | 2010-04-20 14:45:34 | [diff] [blame] | 24 | * @file |
Michael Niedermayer | 983e324 | 2003-03-06 11:32:04 | [diff] [blame] | 25 | * simpleidct in C. |
| 26 | */ |
Diego Biurrun | 115329f | 2005-12-17 18:14:38 | [diff] [blame] | 27 | |
Mans Rullgard | 3e9409b | 2011-07-18 15:04:25 | [diff] [blame] | 28 | #include "libavutil/intreadwrite.h" |
Måns Rullgård | edf7c2b | 2008-12-27 16:25:36 | [diff] [blame] | 29 | #include "mathops.h" |
Arpi | 37e8dcd | 2001-12-09 12:39:54 | [diff] [blame] | 30 | #include "simple_idct.h" |
| 31 | |
Kieran Kunhya | 699fa8f | 2017-12-27 01:08:39 | [diff] [blame] | 32 | #define IN_IDCT_DEPTH 16 |
| 33 | |
Mans Rullgard | e7a972e | 2011-07-20 15:05:05 | [diff] [blame] | 34 | #define BIT_DEPTH 8 |
| 35 | #include "simple_idct_template.c" |
| 36 | #undef BIT_DEPTH |
Michael Niedermayer | 4973971 | 2002-01-14 04:39:59 | [diff] [blame] | 37 | |
Mans Rullgard | e7a972e | 2011-07-20 15:05:05 | [diff] [blame] | 38 | #define BIT_DEPTH 10 |
| 39 | #include "simple_idct_template.c" |
Mans Rullgard | e7a972e | 2011-07-20 15:05:05 | [diff] [blame] | 40 | #undef BIT_DEPTH |
Michael Niedermayer | 86748db | 2002-09-01 09:33:05 | [diff] [blame] | 41 | |
Michael Niedermayer | ae57e82 | 2013-09-05 22:10:45 | [diff] [blame] | 42 | #define BIT_DEPTH 12 |
| 43 | #include "simple_idct_template.c" |
| 44 | #undef BIT_DEPTH |
Kieran Kunhya | 699fa8f | 2017-12-27 01:08:39 | [diff] [blame] | 45 | #undef IN_IDCT_DEPTH |
| 46 | |
| 47 | #define IN_IDCT_DEPTH 32 |
| 48 | #define BIT_DEPTH 10 |
| 49 | #include "simple_idct_template.c" |
| 50 | #undef BIT_DEPTH |
| 51 | #undef IN_IDCT_DEPTH |
Michael Niedermayer | ae57e82 | 2013-09-05 22:10:45 | [diff] [blame] | 52 | |
Fabrice Bellard | 9bf7151 | 2002-10-03 13:41:33 | [diff] [blame] | 53 | /* 2x4x8 idct */ |
| 54 | |
| 55 | #define CN_SHIFT 12 |
| 56 | #define C_FIX(x) ((int)((x) * (1 << CN_SHIFT) + 0.5)) |
Fabrice Bellard | 652f019 | 2002-10-03 21:03:59 | [diff] [blame] | 57 | #define C1 C_FIX(0.6532814824) |
| 58 | #define C2 C_FIX(0.2705980501) |
Fabrice Bellard | 9bf7151 | 2002-10-03 13:41:33 | [diff] [blame] | 59 | |
Fabrice Bellard | 652f019 | 2002-10-03 21:03:59 | [diff] [blame] | 60 | /* row idct is multiple by 16 * sqrt(2.0), col idct4 is normalized, |
| 61 | and the butterfly must be multiplied by 0.5 * sqrt(2.0) */ |
| 62 | #define C_SHIFT (4+1+12) |
Fabrice Bellard | 9bf7151 | 2002-10-03 13:41:33 | [diff] [blame] | 63 | |
Diego Biurrun | 2ec9fa5 | 2016-09-07 15:02:06 | [diff] [blame] | 64 | static inline void idct4col_put(uint8_t *dest, ptrdiff_t line_size, const int16_t *col) |
Fabrice Bellard | 9bf7151 | 2002-10-03 13:41:33 | [diff] [blame] | 65 | { |
| 66 | int c0, c1, c2, c3, a0, a1, a2, a3; |
Fabrice Bellard | 9bf7151 | 2002-10-03 13:41:33 | [diff] [blame] | 67 | |
| 68 | a0 = col[8*0]; |
| 69 | a1 = col[8*2]; |
| 70 | a2 = col[8*4]; |
| 71 | a3 = col[8*6]; |
Michael Niedermayer | 28dc6e7 | 2017-02-21 02:14:49 | [diff] [blame] | 72 | c0 = ((a0 + a2) * (1 << CN_SHIFT - 1)) + (1 << (C_SHIFT - 1)); |
| 73 | c2 = ((a0 - a2) * (1 << CN_SHIFT - 1)) + (1 << (C_SHIFT - 1)); |
Fabrice Bellard | 9bf7151 | 2002-10-03 13:41:33 | [diff] [blame] | 74 | c1 = a1 * C1 + a3 * C2; |
| 75 | c3 = a1 * C2 - a3 * C1; |
Michael Niedermayer | 689f651 | 2012-03-02 21:09:44 | [diff] [blame] | 76 | dest[0] = av_clip_uint8((c0 + c1) >> C_SHIFT); |
Fabrice Bellard | 9bf7151 | 2002-10-03 13:41:33 | [diff] [blame] | 77 | dest += line_size; |
Michael Niedermayer | 689f651 | 2012-03-02 21:09:44 | [diff] [blame] | 78 | dest[0] = av_clip_uint8((c2 + c3) >> C_SHIFT); |
Fabrice Bellard | 9bf7151 | 2002-10-03 13:41:33 | [diff] [blame] | 79 | dest += line_size; |
Michael Niedermayer | 689f651 | 2012-03-02 21:09:44 | [diff] [blame] | 80 | dest[0] = av_clip_uint8((c2 - c3) >> C_SHIFT); |
Fabrice Bellard | 9bf7151 | 2002-10-03 13:41:33 | [diff] [blame] | 81 | dest += line_size; |
Michael Niedermayer | 689f651 | 2012-03-02 21:09:44 | [diff] [blame] | 82 | dest[0] = av_clip_uint8((c0 - c1) >> C_SHIFT); |
Fabrice Bellard | 9bf7151 | 2002-10-03 13:41:33 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | #define BF(k) \ |
| 86 | {\ |
| 87 | int a0, a1;\ |
| 88 | a0 = ptr[k];\ |
| 89 | a1 = ptr[8 + k];\ |
| 90 | ptr[k] = a0 + a1;\ |
| 91 | ptr[8 + k] = a0 - a1;\ |
| 92 | } |
| 93 | |
| 94 | /* only used by DV codec. The input must be interlaced. 128 is added |
| 95 | to the pixels before clamping to avoid systematic error |
| 96 | (1024*sqrt(2)) offset would be needed otherwise. */ |
| 97 | /* XXX: I think a 1.0/sqrt(2) normalization should be needed to |
| 98 | compensate the extra butterfly stage - I don't have the full DV |
| 99 | specification */ |
Diego Biurrun | 2ec9fa5 | 2016-09-07 15:02:06 | [diff] [blame] | 100 | void ff_simple_idct248_put(uint8_t *dest, ptrdiff_t line_size, int16_t *block) |
Fabrice Bellard | 9bf7151 | 2002-10-03 13:41:33 | [diff] [blame] | 101 | { |
| 102 | int i; |
Diego Biurrun | 88bd7fd | 2013-01-20 00:02:29 | [diff] [blame] | 103 | int16_t *ptr; |
Diego Biurrun | 115329f | 2005-12-17 18:14:38 | [diff] [blame] | 104 | |
Fabrice Bellard | 9bf7151 | 2002-10-03 13:41:33 | [diff] [blame] | 105 | /* butterfly */ |
| 106 | ptr = block; |
| 107 | for(i=0;i<4;i++) { |
| 108 | BF(0); |
| 109 | BF(1); |
| 110 | BF(2); |
| 111 | BF(3); |
| 112 | BF(4); |
| 113 | BF(5); |
| 114 | BF(6); |
| 115 | BF(7); |
| 116 | ptr += 2 * 8; |
| 117 | } |
| 118 | |
| 119 | /* IDCT8 on each line */ |
| 120 | for(i=0; i<8; i++) { |
Kieran Kunhya | 699fa8f | 2017-12-27 01:08:39 | [diff] [blame] | 121 | idctRowCondDC_int16_8bit(block + i*8, 0); |
Fabrice Bellard | 9bf7151 | 2002-10-03 13:41:33 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /* IDCT4 and store */ |
| 125 | for(i=0;i<8;i++) { |
Kostya Shishkov | fda767b | 2007-12-03 06:59:48 | [diff] [blame] | 126 | idct4col_put(dest + i, 2 * line_size, block + i); |
| 127 | idct4col_put(dest + line_size + i, 2 * line_size, block + 8 + i); |
Fabrice Bellard | 9bf7151 | 2002-10-03 13:41:33 | [diff] [blame] | 128 | } |
| 129 | } |
Michael Niedermayer | 1457ab5 | 2002-12-27 23:51:46 | [diff] [blame] | 130 | |
| 131 | /* 8x4 & 4x8 WMV2 IDCT */ |
| 132 | #undef CN_SHIFT |
| 133 | #undef C_SHIFT |
| 134 | #undef C_FIX |
| 135 | #undef C1 |
| 136 | #undef C2 |
| 137 | #define CN_SHIFT 12 |
Ganesh Ajjanagadde | 618b3ae | 2015-11-14 15:55:10 | [diff] [blame] | 138 | #define C_FIX(x) ((int)((x) * M_SQRT2 * (1 << CN_SHIFT) + 0.5)) |
Michael Niedermayer | 1457ab5 | 2002-12-27 23:51:46 | [diff] [blame] | 139 | #define C1 C_FIX(0.6532814824) |
| 140 | #define C2 C_FIX(0.2705980501) |
| 141 | #define C3 C_FIX(0.5) |
| 142 | #define C_SHIFT (4+1+12) |
Diego Biurrun | 2ec9fa5 | 2016-09-07 15:02:06 | [diff] [blame] | 143 | static inline void idct4col_add(uint8_t *dest, ptrdiff_t line_size, const int16_t *col) |
Michael Niedermayer | 1457ab5 | 2002-12-27 23:51:46 | [diff] [blame] | 144 | { |
| 145 | int c0, c1, c2, c3, a0, a1, a2, a3; |
Michael Niedermayer | 1457ab5 | 2002-12-27 23:51:46 | [diff] [blame] | 146 | |
| 147 | a0 = col[8*0]; |
| 148 | a1 = col[8*1]; |
| 149 | a2 = col[8*2]; |
| 150 | a3 = col[8*3]; |
| 151 | c0 = (a0 + a2)*C3 + (1 << (C_SHIFT - 1)); |
| 152 | c2 = (a0 - a2)*C3 + (1 << (C_SHIFT - 1)); |
| 153 | c1 = a1 * C1 + a3 * C2; |
| 154 | c3 = a1 * C2 - a3 * C1; |
Ronald S. Bultje | c23acba | 2012-03-06 00:01:19 | [diff] [blame] | 155 | dest[0] = av_clip_uint8(dest[0] + ((c0 + c1) >> C_SHIFT)); |
Michael Niedermayer | 1457ab5 | 2002-12-27 23:51:46 | [diff] [blame] | 156 | dest += line_size; |
Ronald S. Bultje | c23acba | 2012-03-06 00:01:19 | [diff] [blame] | 157 | dest[0] = av_clip_uint8(dest[0] + ((c2 + c3) >> C_SHIFT)); |
Michael Niedermayer | 1457ab5 | 2002-12-27 23:51:46 | [diff] [blame] | 158 | dest += line_size; |
Ronald S. Bultje | c23acba | 2012-03-06 00:01:19 | [diff] [blame] | 159 | dest[0] = av_clip_uint8(dest[0] + ((c2 - c3) >> C_SHIFT)); |
Michael Niedermayer | 1457ab5 | 2002-12-27 23:51:46 | [diff] [blame] | 160 | dest += line_size; |
Ronald S. Bultje | c23acba | 2012-03-06 00:01:19 | [diff] [blame] | 161 | dest[0] = av_clip_uint8(dest[0] + ((c0 - c1) >> C_SHIFT)); |
Michael Niedermayer | 1457ab5 | 2002-12-27 23:51:46 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | #define RN_SHIFT 15 |
Ganesh Ajjanagadde | 618b3ae | 2015-11-14 15:55:10 | [diff] [blame] | 165 | #define R_FIX(x) ((int)((x) * M_SQRT2 * (1 << RN_SHIFT) + 0.5)) |
Michael Niedermayer | 1457ab5 | 2002-12-27 23:51:46 | [diff] [blame] | 166 | #define R1 R_FIX(0.6532814824) |
| 167 | #define R2 R_FIX(0.2705980501) |
| 168 | #define R3 R_FIX(0.5) |
| 169 | #define R_SHIFT 11 |
Diego Biurrun | 88bd7fd | 2013-01-20 00:02:29 | [diff] [blame] | 170 | static inline void idct4row(int16_t *row) |
Michael Niedermayer | 1457ab5 | 2002-12-27 23:51:46 | [diff] [blame] | 171 | { |
Michael Niedermayer | 57f7e5c | 2020-12-17 23:31:08 | [diff] [blame] | 172 | unsigned c0, c1, c2, c3; |
| 173 | int a0, a1, a2, a3; |
Michael Niedermayer | 1457ab5 | 2002-12-27 23:51:46 | [diff] [blame] | 174 | |
| 175 | a0 = row[0]; |
| 176 | a1 = row[1]; |
| 177 | a2 = row[2]; |
| 178 | a3 = row[3]; |
| 179 | c0 = (a0 + a2)*R3 + (1 << (R_SHIFT - 1)); |
| 180 | c2 = (a0 - a2)*R3 + (1 << (R_SHIFT - 1)); |
| 181 | c1 = a1 * R1 + a3 * R2; |
| 182 | c3 = a1 * R2 - a3 * R1; |
| 183 | row[0]= (c0 + c1) >> R_SHIFT; |
| 184 | row[1]= (c2 + c3) >> R_SHIFT; |
| 185 | row[2]= (c2 - c3) >> R_SHIFT; |
| 186 | row[3]= (c0 - c1) >> R_SHIFT; |
| 187 | } |
| 188 | |
Diego Biurrun | 2ec9fa5 | 2016-09-07 15:02:06 | [diff] [blame] | 189 | void ff_simple_idct84_add(uint8_t *dest, ptrdiff_t line_size, int16_t *block) |
Michael Niedermayer | 1457ab5 | 2002-12-27 23:51:46 | [diff] [blame] | 190 | { |
| 191 | int i; |
| 192 | |
| 193 | /* IDCT8 on each line */ |
| 194 | for(i=0; i<4; i++) { |
Kieran Kunhya | 699fa8f | 2017-12-27 01:08:39 | [diff] [blame] | 195 | idctRowCondDC_int16_8bit(block + i*8, 0); |
Michael Niedermayer | 1457ab5 | 2002-12-27 23:51:46 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /* IDCT4 and store */ |
| 199 | for(i=0;i<8;i++) { |
| 200 | idct4col_add(dest + i, line_size, block + i); |
| 201 | } |
| 202 | } |
| 203 | |
Diego Biurrun | 2ec9fa5 | 2016-09-07 15:02:06 | [diff] [blame] | 204 | void ff_simple_idct48_add(uint8_t *dest, ptrdiff_t line_size, int16_t *block) |
Michael Niedermayer | 1457ab5 | 2002-12-27 23:51:46 | [diff] [blame] | 205 | { |
| 206 | int i; |
| 207 | |
| 208 | /* IDCT4 on each line */ |
| 209 | for(i=0; i<8; i++) { |
| 210 | idct4row(block + i*8); |
| 211 | } |
| 212 | |
| 213 | /* IDCT8 and store */ |
| 214 | for(i=0; i<4; i++){ |
Kieran Kunhya | 699fa8f | 2017-12-27 01:08:39 | [diff] [blame] | 215 | idctSparseColAdd_int16_8bit(dest + i, line_size, block + i); |
Michael Niedermayer | 1457ab5 | 2002-12-27 23:51:46 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
Diego Biurrun | 2ec9fa5 | 2016-09-07 15:02:06 | [diff] [blame] | 219 | void ff_simple_idct44_add(uint8_t *dest, ptrdiff_t line_size, int16_t *block) |
Kostya Shishkov | 91823a6 | 2007-12-09 05:54:59 | [diff] [blame] | 220 | { |
| 221 | int i; |
| 222 | |
| 223 | /* IDCT4 on each line */ |
| 224 | for(i=0; i<4; i++) { |
| 225 | idct4row(block + i*8); |
| 226 | } |
| 227 | |
| 228 | /* IDCT4 and store */ |
| 229 | for(i=0; i<4; i++){ |
| 230 | idct4col_add(dest + i, line_size, block + i); |
| 231 | } |
| 232 | } |