Split off floating point AAN (I)DCT into separate components
[libav.git] / libavcodec / idctdsp.c
1 /*
2 * This file is part of Libav.
3 *
4 * Libav is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * Libav is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "config.h"
20 #include "libavutil/attributes.h"
21 #include "libavutil/common.h"
22 #include "avcodec.h"
23 #include "dct.h"
24 #include "faanidct.h"
25 #include "idctdsp.h"
26 #include "simple_idct.h"
27
28 av_cold void ff_init_scantable(uint8_t *permutation, ScanTable *st,
29 const uint8_t *src_scantable)
30 {
31 int i, end;
32
33 st->scantable = src_scantable;
34
35 for (i = 0; i < 64; i++) {
36 int j = src_scantable[i];
37 st->permutated[i] = permutation[j];
38 }
39
40 end = -1;
41 for (i = 0; i < 64; i++) {
42 int j = st->permutated[i];
43 if (j > end)
44 end = j;
45 st->raster_end[i] = end;
46 }
47 }
48
49 av_cold void ff_init_scantable_permutation(uint8_t *idct_permutation,
50 enum idct_permutation_type perm_type)
51 {
52 int i;
53
54 if (ARCH_X86)
55 if (ff_init_scantable_permutation_x86(idct_permutation,
56 perm_type))
57 return;
58
59 switch (perm_type) {
60 case FF_IDCT_PERM_NONE:
61 for (i = 0; i < 64; i++)
62 idct_permutation[i] = i;
63 break;
64 case FF_IDCT_PERM_LIBMPEG2:
65 for (i = 0; i < 64; i++)
66 idct_permutation[i] = (i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2);
67 break;
68 case FF_IDCT_PERM_TRANSPOSE:
69 for (i = 0; i < 64; i++)
70 idct_permutation[i] = ((i & 7) << 3) | (i >> 3);
71 break;
72 case FF_IDCT_PERM_PARTTRANS:
73 for (i = 0; i < 64; i++)
74 idct_permutation[i] = (i & 0x24) | ((i & 3) << 3) | ((i >> 3) & 3);
75 break;
76 default:
77 av_log(NULL, AV_LOG_ERROR,
78 "Internal error, IDCT permutation not set\n");
79 }
80 }
81
82 void (*ff_put_pixels_clamped)(const int16_t *block, uint8_t *pixels, int line_size);
83 void (*ff_add_pixels_clamped)(const int16_t *block, uint8_t *pixels, int line_size);
84
85 static void put_pixels_clamped_c(const int16_t *block, uint8_t *restrict pixels,
86 int line_size)
87 {
88 int i;
89
90 /* read the pixels */
91 for (i = 0; i < 8; i++) {
92 pixels[0] = av_clip_uint8(block[0]);
93 pixels[1] = av_clip_uint8(block[1]);
94 pixels[2] = av_clip_uint8(block[2]);
95 pixels[3] = av_clip_uint8(block[3]);
96 pixels[4] = av_clip_uint8(block[4]);
97 pixels[5] = av_clip_uint8(block[5]);
98 pixels[6] = av_clip_uint8(block[6]);
99 pixels[7] = av_clip_uint8(block[7]);
100
101 pixels += line_size;
102 block += 8;
103 }
104 }
105
106 static void put_signed_pixels_clamped_c(const int16_t *block,
107 uint8_t *restrict pixels,
108 int line_size)
109 {
110 int i, j;
111
112 for (i = 0; i < 8; i++) {
113 for (j = 0; j < 8; j++) {
114 if (*block < -128)
115 *pixels = 0;
116 else if (*block > 127)
117 *pixels = 255;
118 else
119 *pixels = (uint8_t) (*block + 128);
120 block++;
121 pixels++;
122 }
123 pixels += (line_size - 8);
124 }
125 }
126
127 static void add_pixels_clamped_c(const int16_t *block, uint8_t *restrict pixels,
128 int line_size)
129 {
130 int i;
131
132 /* read the pixels */
133 for (i = 0; i < 8; i++) {
134 pixels[0] = av_clip_uint8(pixels[0] + block[0]);
135 pixels[1] = av_clip_uint8(pixels[1] + block[1]);
136 pixels[2] = av_clip_uint8(pixels[2] + block[2]);
137 pixels[3] = av_clip_uint8(pixels[3] + block[3]);
138 pixels[4] = av_clip_uint8(pixels[4] + block[4]);
139 pixels[5] = av_clip_uint8(pixels[5] + block[5]);
140 pixels[6] = av_clip_uint8(pixels[6] + block[6]);
141 pixels[7] = av_clip_uint8(pixels[7] + block[7]);
142 pixels += line_size;
143 block += 8;
144 }
145 }
146
147 av_cold void ff_idctdsp_init(IDCTDSPContext *c, AVCodecContext *avctx)
148 {
149 const unsigned high_bit_depth = avctx->bits_per_raw_sample > 8;
150
151 if (avctx->bits_per_raw_sample == 10) {
152 c->idct_put = ff_simple_idct_put_10;
153 c->idct_add = ff_simple_idct_add_10;
154 c->idct = ff_simple_idct_10;
155 c->perm_type = FF_IDCT_PERM_NONE;
156 } else if (avctx->idct_algo == FF_IDCT_INT) {
157 c->idct_put = ff_jref_idct_put;
158 c->idct_add = ff_jref_idct_add;
159 c->idct = ff_j_rev_dct;
160 c->perm_type = FF_IDCT_PERM_LIBMPEG2;
161 #if CONFIG_FAANIDCT
162 } else if (avctx->idct_algo == FF_IDCT_FAAN) {
163 c->idct_put = ff_faanidct_put;
164 c->idct_add = ff_faanidct_add;
165 c->idct = ff_faanidct;
166 c->perm_type = FF_IDCT_PERM_NONE;
167 #endif /* CONFIG_FAANIDCT */
168 } else { // accurate/default
169 c->idct_put = ff_simple_idct_put_8;
170 c->idct_add = ff_simple_idct_add_8;
171 c->idct = ff_simple_idct_8;
172 c->perm_type = FF_IDCT_PERM_NONE;
173 }
174
175 c->put_pixels_clamped = put_pixels_clamped_c;
176 c->put_signed_pixels_clamped = put_signed_pixels_clamped_c;
177 c->add_pixels_clamped = add_pixels_clamped_c;
178
179 ff_put_pixels_clamped = c->put_pixels_clamped;
180 ff_add_pixels_clamped = c->add_pixels_clamped;
181
182 if (ARCH_ARM)
183 ff_idctdsp_init_arm(c, avctx, high_bit_depth);
184 if (ARCH_PPC)
185 ff_idctdsp_init_ppc(c, avctx, high_bit_depth);
186 if (ARCH_X86)
187 ff_idctdsp_init_x86(c, avctx, high_bit_depth);
188
189 ff_init_scantable_permutation(c->idct_permutation,
190 c->perm_type);
191 }